2022-02-27 21:17:43 +01:00
|
|
|
// Copyright 2022 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-01 14:47:14 +01:00
|
|
|
type DBClient interface {
|
|
|
|
QueryCuboid(cuboid Cuboid, fn func(*Block) *Block) (count int, err error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type DBClientFactory interface {
|
|
|
|
Create() (DBClient, error)
|
|
|
|
Close() error
|
|
|
|
}
|
2022-02-27 21:17:43 +01:00
|
|
|
|
2022-02-28 11:07:50 +01:00
|
|
|
func IsPostgreSQL(host string) (string, bool) {
|
|
|
|
if strings.HasPrefix(host, "postgres:") {
|
|
|
|
return host[len("postgres:"):], true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2022-03-01 14:47:14 +01:00
|
|
|
func CreateDBClientFactory(host string, port int) (DBClientFactory, error) {
|
2022-02-27 21:17:43 +01:00
|
|
|
|
2022-03-01 14:47:14 +01:00
|
|
|
if connS, ok := IsPostgreSQL(host); ok {
|
|
|
|
return NewPGClientFactory(connS)
|
2022-02-27 21:17:43 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 14:47:14 +01:00
|
|
|
return NewRedisClientFactory(host, port)
|
2022-02-27 21:17:43 +01:00
|
|
|
}
|