2017-10-14 07:39:22 +00:00
// +build mssql
/ *
*
* Gosora MSSQL Interface
2018-08-15 07:02:57 +00:00
* Copyright Azareal 2016 - 2019
2017-10-14 07:39:22 +00:00
*
* /
package main
import (
"database/sql"
2017-10-16 07:32:58 +00:00
"log"
2017-10-14 07:39:22 +00:00
"net/url"
2018-08-15 07:02:57 +00:00
"./common"
2017-10-14 07:39:22 +00:00
"./query_gen/lib"
_ "github.com/denisenkom/go-mssqldb"
)
var dbInstance string = ""
func init ( ) {
dbAdapter = "mssql"
_initDatabase = initMSSQL
}
func initMSSQL ( ) ( err error ) {
// TODO: Move this bit to the query gen lib
query := url . Values { }
2018-08-15 07:02:57 +00:00
query . Add ( "database" , common . DbConfig . Dbname )
2017-10-14 07:39:22 +00:00
u := & url . URL {
Scheme : "sqlserver" ,
2018-08-15 07:02:57 +00:00
User : url . UserPassword ( common . DbConfig . Username , common . DbConfig . Password ) ,
Host : common . DbConfig . Host + ":" + common . DbConfig . Port ,
2017-10-14 07:39:22 +00:00
Path : dbInstance ,
RawQuery : query . Encode ( ) ,
}
db , err = sql . Open ( "mssql" , u . String ( ) )
if err != nil {
return err
}
// Make sure that the connection is alive
err = db . Ping ( )
if err != nil {
return err
}
// Set the number of max open connections
db . SetMaxOpenConns ( 64 )
db . SetMaxIdleConns ( 32 )
// Only hold connections open for five seconds to avoid accumulating a large number of stale connections
//db.SetConnMaxLifetime(5 * time.Second)
// 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.
err = _gen_mssql ( )
if err != nil {
return err
}
// Ready the query builder
qgen . Builder . SetConn ( db )
err = qgen . Builder . SetAdapter ( "mssql" )
if err != nil {
return err
}
2017-10-16 07:32:58 +00:00
setter , ok := qgen . Builder . GetAdapter ( ) . ( qgen . SetPrimaryKeys )
if ok {
setter . SetPrimaryKeys ( dbTablePrimaryKeys )
}
// TODO: Is there a less noisy way of doing this for tests?
2018-08-15 07:02:57 +00:00
/ * log . Print ( "Preparing getActivityFeedByWatcher statement." )
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 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 8 ROWS ONLY" )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
}
2018-01-21 11:17:43 +00:00
log . Print ( "Preparing getActivityCountByWatcher statement." )
2018-08-15 07:02:57 +00:00
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] = ?" )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
}
2018-01-21 11:17:43 +00:00
log . Print ( "Preparing todaysPostCount statement." )
2018-08-15 07:02:57 +00:00
stmts . todaysPostCountStmt , err = db . Prepare ( "select count(*) from replies where createdAt >= DATEADD(DAY, -1, GETUTCDATE())" )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
}
2018-01-21 11:17:43 +00:00
log . Print ( "Preparing todaysTopicCount statement." )
2018-08-15 07:02:57 +00:00
stmts . todaysTopicCountStmt , err = db . Prepare ( "select count(*) from topics where createdAt >= DATEADD(DAY, -1, GETUTCDATE())" )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
2018-08-15 07:02:57 +00:00
} * /
2017-10-16 07:32:58 +00:00
2018-06-06 00:21:22 +00:00
log . Print ( "Preparing todaysTopicCountByForum statement." )
// TODO: Stop hard-coding this query
2018-08-15 07:02:57 +00:00
stmts . todaysTopicCountByForum , err = db . Prepare ( "select count(*) from topics where createdAt >= DATEADD(DAY, -1, GETUTCDATE()) and parentID = ?" )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
}
2018-08-15 07:02:57 +00:00
/ * log . Print ( "Preparing todaysNewUserCount statement." )
stmts . todaysNewUserCountStmt , err = db . Prepare ( "select count(*) from users where createdAt >= DATEADD(DAY, -1, GETUTCDATE())" )
2017-10-16 07:32:58 +00:00
if err != nil {
return err
2018-08-15 07:02:57 +00:00
} * /
2017-10-16 07:32:58 +00:00
2018-01-21 11:17:43 +00:00
return nil
2017-10-14 07:39:22 +00:00
}