2016-12-11 16:06:17 +00:00
/* Copyright Azareal 2016 - 2017 */
2017-06-06 14:41:06 +00:00
// +build !pgsql !sqlite !mssql
2016-12-09 13:46:29 +00:00
package main
import "log"
2017-01-21 18:16:27 +00:00
import "strconv"
2017-06-13 07:12:58 +00:00
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
2017-06-13 08:56:48 +00:00
import "./query_gen/lib"
2016-12-09 13:46:29 +00:00
var db * sql . DB
2017-02-15 10:49:30 +00:00
var db_version string
2017-04-12 10:10:36 +00:00
var db_collation string = "utf8mb4_general_ci"
2017-02-15 10:49:30 +00:00
2017-06-06 14:41:06 +00:00
var get_topic_replies_offset_stmt * sql . Stmt // I'll need to rewrite this one to stop it hard-coding the per page setting before moving it to the query generator
2017-01-26 13:37:50 +00:00
var get_forum_topics_offset_stmt * sql . Stmt
2017-03-03 16:28:49 +00:00
var notify_watchers_stmt * sql . Stmt
2017-02-28 09:27:28 +00:00
var get_activity_feed_by_watcher_stmt * sql . Stmt
2017-06-10 07:58:15 +00:00
var get_activity_count_by_watcher_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
2017-01-26 13:37:50 +00:00
var forum_entry_exists_stmt * sql . Stmt
2017-03-27 07:09:28 +00:00
var group_entry_exists_stmt * sql . Stmt
2017-02-04 06:19:55 +00:00
var add_forum_perms_to_forum_admins_stmt * sql . Stmt
var add_forum_perms_to_forum_staff_stmt * sql . Stmt
var add_forum_perms_to_forum_members_stmt * sql . Stmt
2017-06-05 11:57:27 +00:00
var update_forum_perms_for_group_stmt * sql . Stmt
2017-06-06 14:41:06 +00:00
var todays_post_count_stmt * sql . Stmt
var todays_topic_count_stmt * sql . Stmt
var todays_report_count_stmt * sql . Stmt
var todays_newuser_count_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
2017-06-14 07:09:44 +00:00
func _init_database ( ) ( err error ) {
2016-12-09 13:46:29 +00:00
if ( dbpassword != "" ) {
dbpassword = ":" + dbpassword
}
2017-06-05 11:57:27 +00:00
2017-05-02 17:24:33 +00:00
// Open the database connection
2017-04-12 10:10:36 +00:00
db , err = sql . Open ( "mysql" , dbuser + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?collation=" + db_collation )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-05-02 17:24:33 +00:00
// Make sure that the connection is alive
2016-12-09 13:46:29 +00:00
err = db . Ping ( )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-05-02 17:24:33 +00:00
// Fetch the database version
2017-02-15 10:49:30 +00:00
db . QueryRow ( "SELECT VERSION()" ) . Scan ( & db_version )
2017-06-05 11:57:27 +00:00
2017-05-02 17:24:33 +00:00
// Set the number of max open connections
db . SetMaxOpenConns ( 64 )
2017-06-05 11:57:27 +00:00
// 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_mysql ( )
2016-12-23 12:35:22 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-23 12:35:22 +00:00
}
2017-06-05 11:57:27 +00:00
2017-06-13 08:56:48 +00:00
// Ready the query builder
qgen . Builder . SetConn ( db )
err = qgen . Builder . SetAdapter ( "mysql" )
if err != nil {
return err
}
2017-01-21 18:16:27 +00:00
log . Print ( "Preparing get_topic_replies_offset statement." )
2017-04-02 13:00:40 +00:00
get_topic_replies_offset_stmt , err = db . Prepare ( "select replies.rid, replies.content, replies.createdBy, replies.createdAt, replies.lastEdit, replies.lastEditBy, users.avatar, users.name, users.group, users.url_prefix, users.url_name, users.level, replies.ipaddress, replies.likeCount, replies.actionType from replies left join users on replies.createdBy = users.uid where tid = ? limit ?, " + strconv . Itoa ( items_per_page ) )
2017-01-17 07:55:46 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-17 07:55:46 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-26 13:37:50 +00:00
log . Print ( "Preparing get_forum_topics_offset statement." )
2017-06-05 11:57:27 +00:00
get_forum_topics_offset_stmt , err = db . Prepare ( "select topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.lastReplyAt, topics.parentID, topics.postCount, topics.likeCount, users.name, users.avatar from topics left join users ON topics.createdBy = users.uid WHERE topics.parentID = ? order by topics.sticky DESC, topics.lastReplyAt DESC, topics.createdBy DESC limit ?, " + strconv . Itoa ( items_per_page ) )
2017-01-17 07:55:46 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-17 07:55:46 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-03 16:28:49 +00:00
log . Print ( "Preparing notify_watchers statement." )
2017-03-05 07:53:41 +00:00
notify_watchers_stmt , err = db . Prepare ( "INSERT INTO activity_stream_matches(watcher, asid) SELECT activity_subscriptions.user, activity_stream.asid FROM activity_stream INNER JOIN activity_subscriptions ON activity_subscriptions.targetType = activity_stream.elementType and activity_subscriptions.targetID = activity_stream.elementID and activity_subscriptions.user != activity_stream.actor where asid = ?" )
2017-03-03 16:28:49 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-03 16:28:49 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-28 09:27:28 +00:00
log . Print ( "Preparing get_activity_feed_by_watcher statement." )
2017-06-10 07:58:15 +00:00
get_activity_feed_by_watcher_stmt , 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 ASC LIMIT 8" )
if err != nil {
return err
}
log . Print ( "Preparing get_activity_count_by_watcher statement." )
get_activity_count_by_watcher_stmt , 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-02-28 09:27:28 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-28 09:27:28 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-26 13:37:50 +00:00
log . Print ( "Preparing forum_entry_exists statement." )
forum_entry_exists_stmt , err = db . Prepare ( "SELECT `fid` FROM `forums` WHERE `name` = '' order by fid asc limit 1" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-27 07:09:28 +00:00
log . Print ( "Preparing group_entry_exists statement." )
group_entry_exists_stmt , err = db . Prepare ( "SELECT `gid` FROM `users_groups` WHERE `name` = '' order by gid asc limit 1" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-27 07:09:28 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-04 06:19:55 +00:00
log . Print ( "Preparing add_forum_perms_to_forum_admins statement." )
2017-02-28 09:27:28 +00:00
add_forum_perms_to_forum_admins_stmt , err = db . Prepare ( "INSERT INTO forums_permissions(gid,fid,preset,permissions) SELECT `gid`,? AS fid,? AS preset,? AS permissions FROM users_groups WHERE is_admin = 1" )
2017-02-04 06:19:55 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-04 06:19:55 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-04 06:19:55 +00:00
log . Print ( "Preparing add_forum_perms_to_forum_staff statement." )
2017-02-28 09:27:28 +00:00
add_forum_perms_to_forum_staff_stmt , err = db . Prepare ( "INSERT INTO forums_permissions(gid,fid,preset,permissions) SELECT `gid`,? AS fid,? AS preset,? AS permissions FROM users_groups WHERE is_admin = 0 AND is_mod = 1" )
2017-02-04 06:19:55 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-04 06:19:55 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-04 06:19:55 +00:00
log . Print ( "Preparing add_forum_perms_to_forum_members statement." )
2017-02-28 09:27:28 +00:00
add_forum_perms_to_forum_members_stmt , err = db . Prepare ( "INSERT INTO forums_permissions(gid,fid,preset,permissions) SELECT `gid`,? AS fid,? AS preset,? AS permissions FROM users_groups WHERE is_admin = 0 AND is_mod = 0 AND is_banned = 0" )
2017-02-04 06:19:55 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-04 06:19:55 +00:00
}
2017-06-05 11:57:27 +00:00
2017-06-06 14:41:06 +00:00
log . Print ( "Preparing todays_post_count statement." )
todays_post_count_stmt , err = db . Prepare ( "select count(*) from replies where createdAt BETWEEN (now() - interval 1 day) and now()" )
if err != nil {
return err
}
log . Print ( "Preparing todays_topic_count statement." )
todays_topic_count_stmt , err = db . Prepare ( "select count(*) from topics where createdAt BETWEEN (now() - interval 1 day) and now()" )
if err != nil {
return err
}
log . Print ( "Preparing todays_report_count statement." )
todays_report_count_stmt , err = db . Prepare ( "select count(*) from topics where createdAt BETWEEN (now() - interval 1 day) and now() and parentID = 1" )
if err != nil {
return err
}
log . Print ( "Preparing todays_newuser_count statement." )
todays_newuser_count_stmt , err = db . Prepare ( "select count(*) from users where createdAt BETWEEN (now() - interval 1 day) and now()" )
if err != nil {
return err
}
2017-04-12 10:10:36 +00:00
return nil
2016-12-09 13:46:29 +00:00
}