psql first commit

This commit is contained in:
root
2023-04-25 15:14:42 +03:00
parent c96b6ed943
commit 15ab461a5e
49 changed files with 110 additions and 0 deletions

0
storage/accounts.go Normal file → Executable file
View File

0
storage/etcd.go Normal file → Executable file
View File

0
storage/mongodb.go Normal file → Executable file
View File

63
storage/postgresql.go Normal file
View 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("")
}

View 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
View 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)
}