github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/mssql.go (about) 1 // +build mssql 2 3 /* 4 * 5 * Gosora MSSQL Interface 6 * Copyright Azareal 2016 - 2020 7 * 8 */ 9 package main 10 11 import ( 12 "database/sql" 13 "net/url" 14 15 "github.com/Azareal/Gosora/common" 16 c "github.com/Azareal/Gosora/common" 17 qgen "github.com/Azareal/Gosora/query_gen" 18 _ "github.com/denisenkom/go-mssqldb" 19 ) 20 21 var dbInstance string = "" 22 23 func init() { 24 dbAdapter = "mssql" 25 _initDatabase = initMSSQL 26 } 27 28 func initMSSQL() (err error) { 29 // TODO: Move this bit to the query gen lib 30 query := url.Values{} 31 query.Add("database", common.DbConfig.Dbname) 32 u := &url.URL{ 33 Scheme: "sqlserver", 34 User: url.UserPassword(common.DbConfig.Username, common.DbConfig.Password), 35 Host: common.DbConfig.Host + ":" + common.DbConfig.Port, 36 Path: dbInstance, 37 RawQuery: query.Encode(), 38 } 39 db, err = sql.Open("mssql", u.String()) 40 if err != nil { 41 return err 42 } 43 44 // Make sure that the connection is alive 45 err = db.Ping() 46 if err != nil { 47 return err 48 } 49 50 // Set the number of max open connections 51 db.SetMaxOpenConns(64) 52 db.SetMaxIdleConns(32) 53 54 // Only hold connections open for five seconds to avoid accumulating a large number of stale connections 55 //db.SetConnMaxLifetime(5 * time.Second) 56 db.SetConnMaxLifetime(c.DBTimeout()) 57 58 // Build the generated prepared statements, we are going to slowly move the queries over to the query generator rather than writing them all by hand, this'll make it easier for us to implement database adapters for other databases like PostgreSQL, MSSQL, SQlite, etc. 59 err = _gen_mssql() 60 if err != nil { 61 return err 62 } 63 64 // Ready the query builder 65 qgen.Builder.SetConn(db) 66 err = qgen.Builder.SetAdapter("mssql") 67 if err != nil { 68 return err 69 } 70 71 setter, ok := qgen.Builder.GetAdapter().(qgen.SetPrimaryKeys) 72 if ok { 73 setter.SetPrimaryKeys(dbTablePrimaryKeys) 74 } 75 76 // TODO: Is there a less noisy way of doing this for tests? 77 /*log.Print("Preparing getActivityFeedByWatcher statement.") 78 stmts.getActivityFeedByWatcherStmt, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID, activity_stream.createdAt FROM [activity_stream_matches] INNER JOIN [activity_stream] ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE [watcher] = ? ORDER BY activity_stream.asid DESC OFFSET 0 ROWS FETCH NEXT 16 ROWS ONLY") 79 if err != nil { 80 return err 81 } 82 83 log.Print("Preparing getActivityFeedByWatcher statement.") 84 stmts.getActivityFeedByWatcherStmt, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID, activity_stream.createdAt FROM [activity_stream_matches] INNER JOIN [activity_stream] ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE [watcher] = ? ORDER BY activity_stream.asid DESC OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY") 85 if err != nil { 86 return err 87 } 88 89 log.Print("Preparing getActivityCountByWatcher statement.") 90 stmts.getActivityCountByWatcherStmt, err = db.Prepare("SELECT count(*) FROM [activity_stream_matches] INNER JOIN [activity_stream] ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE [watcher] = ?") 91 if err != nil { 92 return err 93 } 94 */ 95 96 return nil 97 }