github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/install/pgsql.go (about)

     1  /*
     2  *
     3  * Gosora PostgreSQL Interface
     4  * Under heavy development
     5  * Copyright Azareal 2017 - 2019
     6  *
     7   */
     8  package install
     9  
    10  import (
    11  	"database/sql"
    12  	"errors"
    13  	"fmt"
    14  	"strings"
    15  
    16  	"github.com/Azareal/Gosora/query_gen"
    17  	_ "github.com/go-sql-driver/mysql"
    18  )
    19  
    20  // We don't need SSL to run an installer... Do we?
    21  var dbSslmode = "disable"
    22  
    23  func init() {
    24  	adapters["pgsql"] = &PgsqlInstaller{dbHost: ""}
    25  }
    26  
    27  type PgsqlInstaller struct {
    28  	db         *sql.DB
    29  	dbHost     string
    30  	dbUsername string
    31  	dbPassword string
    32  	dbName     string
    33  	dbPort     string
    34  }
    35  
    36  func (ins *PgsqlInstaller) SetConfig(dbHost string, dbUsername string, dbPassword string, dbName string, dbPort string) {
    37  	ins.dbHost = dbHost
    38  	ins.dbUsername = dbUsername
    39  	ins.dbPassword = dbPassword
    40  	ins.dbName = dbName
    41  	ins.dbPort = dbPort
    42  }
    43  
    44  func (ins *PgsqlInstaller) Name() string {
    45  	return "pgsql"
    46  }
    47  
    48  func (ins *PgsqlInstaller) DefaultPort() string {
    49  	return "5432"
    50  }
    51  
    52  func (ins *PgsqlInstaller) InitDatabase() (err error) {
    53  	_dbPassword := ins.dbPassword
    54  	if _dbPassword != "" {
    55  		_dbPassword = " password=" + pgEscapeBit(_dbPassword)
    56  	}
    57  	db, err := sql.Open("postgres", "host='"+pgEscapeBit(ins.dbHost)+"' port='"+pgEscapeBit(ins.dbPort)+"' user='"+pgEscapeBit(ins.dbUsername)+"' dbname='"+pgEscapeBit(ins.dbName)+"'"+_dbPassword+" sslmode='"+dbSslmode+"'")
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	// Make sure that the connection is alive..
    63  	err = db.Ping()
    64  	if err != nil {
    65  		return err
    66  	}
    67  	fmt.Println("Successfully connected to the database")
    68  
    69  	// TODO: Create the database, if it doesn't exist
    70  
    71  	// Ready the query builder
    72  	ins.db = db
    73  	qgen.Builder.SetConn(db)
    74  	return qgen.Builder.SetAdapter("pgsql")
    75  }
    76  
    77  func (ins *PgsqlInstaller) TableDefs() (err error) {
    78  	return errors.New("TableDefs() not implemented")
    79  }
    80  
    81  func (ins *PgsqlInstaller) InitialData() (err error) {
    82  	return errors.New("InitialData() not implemented")
    83  }
    84  
    85  func (ins *PgsqlInstaller) CreateAdmin() error {
    86  	return createAdmin()
    87  }
    88  
    89  func (ins *PgsqlInstaller) DBHost() string {
    90  	return ins.dbHost
    91  }
    92  
    93  func (ins *PgsqlInstaller) DBUsername() string {
    94  	return ins.dbUsername
    95  }
    96  
    97  func (ins *PgsqlInstaller) DBPassword() string {
    98  	return ins.dbPassword
    99  }
   100  
   101  func (ins *PgsqlInstaller) DBName() string {
   102  	return ins.dbName
   103  }
   104  
   105  func (ins *PgsqlInstaller) DBPort() string {
   106  	return ins.dbPort
   107  }
   108  
   109  func pgEscapeBit(bit string) string {
   110  	// TODO: Write a custom parser, so that backslashes work properly in the sql.Open string. Do something similar for the database driver, if possible?
   111  	return strings.Replace(bit, "'", "\\'", -1)
   112  }