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

     1  /*
     2  *
     3  * Gosora MSSQL Interface
     4  * Copyright Azareal 2017 - 2018
     5  *
     6   */
     7  package install
     8  
     9  import (
    10  	"bytes"
    11  	"database/sql"
    12  	"fmt"
    13  	"io/ioutil"
    14  	"log"
    15  	"net/url"
    16  	"path/filepath"
    17  	"strconv"
    18  	"strings"
    19  
    20  	"github.com/Azareal/Gosora/query_gen"
    21  	_ "github.com/denisenkom/go-mssqldb"
    22  )
    23  
    24  func init() {
    25  	adapters["mssql"] = &MssqlInstaller{dbHost: ""}
    26  }
    27  
    28  type MssqlInstaller struct {
    29  	db         *sql.DB
    30  	dbHost     string
    31  	dbUsername string
    32  	dbPassword string
    33  	dbName     string
    34  	dbInstance string
    35  	dbPort     string
    36  }
    37  
    38  func (ins *MssqlInstaller) SetConfig(dbHost string, dbUsername string, dbPassword string, dbName string, dbPort string) {
    39  	ins.dbHost = dbHost
    40  	ins.dbUsername = dbUsername
    41  	ins.dbPassword = dbPassword
    42  	ins.dbName = dbName
    43  	ins.dbInstance = "" // You can't set this from the installer right now, it allows you to connect to a named instance instead of a port
    44  	ins.dbPort = dbPort
    45  }
    46  
    47  func (ins *MssqlInstaller) Name() string {
    48  	return "mssql"
    49  }
    50  
    51  func (ins *MssqlInstaller) DefaultPort() string {
    52  	return "1433"
    53  }
    54  
    55  func (ins *MssqlInstaller) InitDatabase() (err error) {
    56  	query := url.Values{}
    57  	query.Add("database", ins.dbName)
    58  	u := &url.URL{
    59  		Scheme:   "sqlserver",
    60  		User:     url.UserPassword(ins.dbUsername, ins.dbPassword),
    61  		Host:     ins.dbHost + ":" + ins.dbPort,
    62  		Path:     ins.dbInstance,
    63  		RawQuery: query.Encode(),
    64  	}
    65  	log.Print("u.String() ", u.String())
    66  
    67  	db, err := sql.Open("mssql", u.String())
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	// Make sure that the connection is alive..
    73  	err = db.Ping()
    74  	if err != nil {
    75  		return err
    76  	}
    77  	fmt.Println("Successfully connected to the database")
    78  
    79  	// TODO: Create the database, if it doesn't exist
    80  
    81  	// Ready the query builder
    82  	ins.db = db
    83  	qgen.Builder.SetConn(db)
    84  	return qgen.Builder.SetAdapter("mssql")
    85  }
    86  
    87  func (ins *MssqlInstaller) TableDefs() (err error) {
    88  	//fmt.Println("Creating the tables")
    89  	files, _ := ioutil.ReadDir("./schema/mssql/")
    90  	for _, f := range files {
    91  		if !strings.HasPrefix(f.Name(), "query_") {
    92  			continue
    93  		}
    94  
    95  		var table, ext string
    96  		table = strings.TrimPrefix(f.Name(), "query_")
    97  		ext = filepath.Ext(table)
    98  		if ext != ".sql" {
    99  			continue
   100  		}
   101  		table = strings.TrimSuffix(table, ext)
   102  
   103  		// ? - This is mainly here for tests, although it might allow the installer to overwrite a production database, so we might want to proceed with caution
   104  		_, err = ins.db.Exec("DROP TABLE IF EXISTS [" + table + "];")
   105  		if err != nil {
   106  			fmt.Println("Failed query:", "DROP TABLE IF EXISTS ["+table+"]")
   107  			return err
   108  		}
   109  
   110  		fmt.Println("Creating table '" + table + "'")
   111  		data, err := ioutil.ReadFile("./schema/mssql/" + f.Name())
   112  		if err != nil {
   113  			return err
   114  		}
   115  		data = bytes.TrimSpace(data)
   116  
   117  		_, err = ins.db.Exec(string(data))
   118  		if err != nil {
   119  			fmt.Println("Failed query:", string(data))
   120  			return err
   121  		}
   122  	}
   123  	return nil
   124  }
   125  
   126  func (ins *MssqlInstaller) InitialData() (err error) {
   127  	//fmt.Println("Seeding the tables")
   128  	data, err := ioutil.ReadFile("./schema/mssql/inserts.sql")
   129  	if err != nil {
   130  		return err
   131  	}
   132  	data = bytes.TrimSpace(data)
   133  
   134  	statements := bytes.Split(data, []byte(";"))
   135  	for key, statement := range statements {
   136  		if len(statement) == 0 {
   137  			continue
   138  		}
   139  
   140  		fmt.Println("Executing query #" + strconv.Itoa(key) + " " + string(statement))
   141  		_, err = ins.db.Exec(string(statement))
   142  		if err != nil {
   143  			return err
   144  		}
   145  	}
   146  	return nil
   147  }
   148  
   149  func (ins *MssqlInstaller) CreateAdmin() error {
   150  	return createAdmin()
   151  }
   152  
   153  func (ins *MssqlInstaller) DBHost() string {
   154  	return ins.dbHost
   155  }
   156  
   157  func (ins *MssqlInstaller) DBUsername() string {
   158  	return ins.dbUsername
   159  }
   160  
   161  func (ins *MssqlInstaller) DBPassword() string {
   162  	return ins.dbPassword
   163  }
   164  
   165  func (ins *MssqlInstaller) DBName() string {
   166  	return ins.dbName
   167  }
   168  
   169  func (ins *MssqlInstaller) DBPort() string {
   170  	return ins.dbPort
   171  }