psql first commit
This commit is contained in:
0
storage/accounts.go
Normal file → Executable file
0
storage/accounts.go
Normal file → Executable file
0
storage/etcd.go
Normal file → Executable file
0
storage/etcd.go
Normal file → Executable file
0
storage/mongodb.go
Normal file → Executable file
0
storage/mongodb.go
Normal file → Executable file
63
storage/postgresql.go
Normal file
63
storage/postgresql.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/spf13/viper"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type PostgresqlStorage struct {
|
||||
DbConnection *sql.DB
|
||||
}
|
||||
|
||||
func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) {
|
||||
var (
|
||||
host = cfg.GetString("storage.db.psql.host")
|
||||
port = cfg.GetString("storage.db.psql.port")
|
||||
user = cfg.GetString("storage.db.psql.user")
|
||||
password = cfg.GetString("storage.db.psql.password")
|
||||
dbname = cfg.GetString("storage.db.psql.dbname")
|
||||
)
|
||||
portInt, _ := strconv.Atoi(port)
|
||||
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, portInt,
|
||||
user, password, dbname)
|
||||
db, err := sql.Open("postgres", psqlconn)
|
||||
if err != nil {
|
||||
fmt.Println("error", err)
|
||||
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql failed")
|
||||
}
|
||||
defer db.Close()
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql database failed")
|
||||
}
|
||||
return PostgresqlStorage{
|
||||
DbConnection: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (PostgresqlStorage) GetAccount(id string) (*Account, error) {
|
||||
return nil, fmt.Errorf("")
|
||||
}
|
||||
|
||||
func (PostgresqlStorage) LocalAuthentication(namespace string, username string, email string, phone_number string) (*Account, error) {
|
||||
return nil, fmt.Errorf("")
|
||||
}
|
||||
|
||||
func (PostgresqlStorage) GetAccounts(namespaces []string) ([]Account, error) {
|
||||
return nil, fmt.Errorf("")
|
||||
}
|
||||
|
||||
func (PostgresqlStorage) GetAccountsByIds(accountids []string) ([]Account, error) {
|
||||
return nil, fmt.Errorf("")
|
||||
}
|
||||
|
||||
func (PostgresqlStorage) CreateAccount(account Account) error {
|
||||
return fmt.Errorf("")
|
||||
}
|
||||
|
||||
func (PostgresqlStorage) UpdateAccount(account Account) error {
|
||||
return fmt.Errorf("")
|
||||
}
|
||||
21
storage/postgresql_test.go
Normal file
21
storage/postgresql_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewPostgresqlStorage(t *testing.T) {
|
||||
cfg := viper.New()
|
||||
cfg.Set("storage.db.psql.host", "localhost")
|
||||
cfg.Set("storage.db.psql.port", "5432")
|
||||
cfg.Set("storage.db.psql.user", "postgres")
|
||||
cfg.Set("storage.db.psql.password", "postgres")
|
||||
cfg.Set("storage.db.psql.dbname", "mobilityaccounts")
|
||||
|
||||
storage, err := NewPostgresqlStorage(cfg)
|
||||
if err != nil {
|
||||
t.Errorf("error creating new PostgreSQL storage: %v", err)
|
||||
}
|
||||
defer storage.DbConnection.Close()
|
||||
}
|
||||
3
storage/storage.go
Normal file → Executable file
3
storage/storage.go
Normal file → Executable file
@@ -46,6 +46,9 @@ func NewDBStorage(cfg *viper.Viper) (DBStorage, error) {
|
||||
case "mongodb":
|
||||
s, err := NewMongoDBStorage(cfg)
|
||||
return s, err
|
||||
case "psql":
|
||||
s, err := NewPostgresqlStorage(cfg)
|
||||
return s, err
|
||||
default:
|
||||
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user