2019-02-26 17:06:21 +00:00
|
|
|
package storage
|
2019-02-22 03:18:08 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
2019-02-26 17:06:21 +00:00
|
|
|
"internal-tools-server/models"
|
|
|
|
|
"log"
|
2019-02-22 03:18:08 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
|
)
|
|
|
|
|
|
2019-02-26 17:06:21 +00:00
|
|
|
type PostgresDataStore struct {
|
|
|
|
|
Dbhost string
|
|
|
|
|
Dbport string
|
|
|
|
|
Dbuser string
|
|
|
|
|
Dbpass string
|
|
|
|
|
Dbname string
|
|
|
|
|
MaxOpenConnections int
|
|
|
|
|
db *sql.DB
|
|
|
|
|
}
|
2019-02-22 03:18:08 +00:00
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
dbhost = "DBHOST"
|
|
|
|
|
dbport = "DBPORT"
|
|
|
|
|
dbuser = "DBUSER"
|
|
|
|
|
dbpass = "DBPASS"
|
|
|
|
|
dbname = "DBNAME"
|
|
|
|
|
)
|
|
|
|
|
|
2019-02-26 17:06:21 +00:00
|
|
|
// InitPostgresDb initializes the database
|
|
|
|
|
func InitPostgresDb() (datastore DataStore, err error) {
|
|
|
|
|
|
|
|
|
|
d := PostgresDataStore{}
|
2019-02-22 03:18:08 +00:00
|
|
|
|
|
|
|
|
// Initialize the database
|
|
|
|
|
d.dbConfig()
|
|
|
|
|
|
|
|
|
|
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s "+
|
|
|
|
|
"password=%s dbname=%s sslmode=disable",
|
|
|
|
|
d.Dbhost, d.Dbport,
|
|
|
|
|
d.Dbuser, d.Dbpass, d.Dbname)
|
|
|
|
|
|
2019-02-26 17:06:21 +00:00
|
|
|
d.db, err = sql.Open("postgres", psqlInfo)
|
2019-02-22 03:18:08 +00:00
|
|
|
|
|
|
|
|
if err != nil {
|
2019-02-26 17:06:21 +00:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Since the error returned from “Open” does not check if the datasource is valid calling
|
|
|
|
|
// Ping on the database is required
|
|
|
|
|
err = d.db.Ping()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("Error: Could not establish a connection with the database")
|
2019-02-22 03:18:08 +00:00
|
|
|
}
|
2019-02-26 17:06:21 +00:00
|
|
|
|
2019-02-22 03:18:08 +00:00
|
|
|
// Setup connection pool
|
2019-02-26 17:06:21 +00:00
|
|
|
d.db.SetMaxOpenConns(d.MaxOpenConnections)
|
2019-02-22 03:18:08 +00:00
|
|
|
|
2019-02-26 17:06:21 +00:00
|
|
|
err = d.db.Ping()
|
2019-02-22 03:18:08 +00:00
|
|
|
if err != nil {
|
2019-02-26 17:06:21 +00:00
|
|
|
return nil, err
|
2019-02-22 03:18:08 +00:00
|
|
|
}
|
|
|
|
|
fmt.Println("Successfully connected!")
|
2019-02-26 17:06:21 +00:00
|
|
|
// listTables()
|
|
|
|
|
return &d, nil
|
2019-02-22 03:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-26 17:06:21 +00:00
|
|
|
func (d *PostgresDataStore) dbConfig() {
|
2019-02-22 03:18:08 +00:00
|
|
|
var ok bool
|
|
|
|
|
d.Dbhost, ok = os.LookupEnv(dbhost)
|
|
|
|
|
if !ok {
|
|
|
|
|
d.Dbhost = "localhost"
|
|
|
|
|
}
|
|
|
|
|
d.Dbport, ok = os.LookupEnv(dbport)
|
|
|
|
|
if !ok {
|
|
|
|
|
d.Dbport = "5432"
|
|
|
|
|
}
|
|
|
|
|
d.Dbuser, ok = os.LookupEnv(dbuser)
|
|
|
|
|
if !ok {
|
|
|
|
|
d.Dbuser = "postgres"
|
|
|
|
|
}
|
|
|
|
|
d.Dbpass, ok = os.LookupEnv(dbpass)
|
|
|
|
|
if !ok {
|
|
|
|
|
d.Dbpass = "root"
|
|
|
|
|
}
|
|
|
|
|
d.Dbname, ok = os.LookupEnv(dbname)
|
|
|
|
|
if !ok {
|
2019-02-26 17:06:21 +00:00
|
|
|
d.Dbname = "mobtools"
|
2019-02-22 03:18:08 +00:00
|
|
|
}
|
|
|
|
|
d.MaxOpenConnections = 5
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-26 17:06:21 +00:00
|
|
|
// ExecuteQuery executes the query on the DB
|
|
|
|
|
func (d *PostgresDataStore) ExecuteQuery(query string) ([]models.Component, error) {
|
|
|
|
|
rows, err := d.db.Query(query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
components := []models.Component{}
|
|
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
component := models.Component{}
|
|
|
|
|
err = rows.Scan(
|
|
|
|
|
&component.ID,
|
|
|
|
|
&component.Name,
|
|
|
|
|
&component.Type,
|
|
|
|
|
)
|
|
|
|
|
components = append(components, component)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return components, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 03:18:08 +00:00
|
|
|
type schemaSummary struct {
|
|
|
|
|
tableName string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// listTables first fetches the tables from the db
|
|
|
|
|
func listTables() error {
|
|
|
|
|
tables := []schemaSummary{}
|
|
|
|
|
|
|
|
|
|
rows, err := db.Query(`
|
|
|
|
|
SELECT table_name as tableName FROM information_schema.tables WHERE table_schema='public'; `)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
schema := schemaSummary{}
|
|
|
|
|
err = rows.Scan(
|
|
|
|
|
&schema.tableName,
|
|
|
|
|
)
|
|
|
|
|
fmt.Printf("Name: %s\n", schema.tableName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
tables = append(tables, schema)
|
|
|
|
|
}
|
|
|
|
|
err = rows.Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("\nTables: %x\n", tables)
|
|
|
|
|
return nil
|
|
|
|
|
}
|