| 115 | } |
| 116 | |
| 117 | func chooseDB(envName string, isLog bool) (*gorm.DB, error) { |
| 118 | defer func() { |
| 119 | initCol() |
| 120 | }() |
| 121 | dsn := os.Getenv(envName) |
| 122 | if dsn != "" { |
| 123 | if strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://") { |
| 124 | // Use PostgreSQL |
| 125 | common.SysLog("using PostgreSQL as database") |
| 126 | if !isLog { |
| 127 | common.UsingPostgreSQL = true |
| 128 | } else { |
| 129 | common.LogSqlType = common.DatabaseTypePostgreSQL |
| 130 | } |
| 131 | return gorm.Open(postgres.New(postgres.Config{ |
| 132 | DSN: dsn, |
| 133 | PreferSimpleProtocol: true, // disables implicit prepared statement usage |
| 134 | }), &gorm.Config{ |
| 135 | PrepareStmt: true, // precompile SQL |
| 136 | }) |
| 137 | } |
| 138 | if strings.HasPrefix(dsn, "local") { |
| 139 | common.SysLog("SQL_DSN not set, using SQLite as database") |
| 140 | if !isLog { |
| 141 | common.UsingSQLite = true |
| 142 | } else { |
| 143 | common.LogSqlType = common.DatabaseTypeSQLite |
| 144 | } |
| 145 | return gorm.Open(sqlite.Open(common.SQLitePath), &gorm.Config{ |
| 146 | PrepareStmt: true, // precompile SQL |
| 147 | }) |
| 148 | } |
| 149 | // Use MySQL |
| 150 | common.SysLog("using MySQL as database") |
| 151 | // check parseTime |
| 152 | if !strings.Contains(dsn, "parseTime") { |
| 153 | if strings.Contains(dsn, "?") { |
| 154 | dsn += "&parseTime=true" |
| 155 | } else { |
| 156 | dsn += "?parseTime=true" |
| 157 | } |
| 158 | } |
| 159 | if !isLog { |
| 160 | common.UsingMySQL = true |
| 161 | } else { |
| 162 | common.LogSqlType = common.DatabaseTypeMySQL |
| 163 | } |
| 164 | return gorm.Open(mysql.Open(dsn), &gorm.Config{ |
| 165 | PrepareStmt: true, // precompile SQL |
| 166 | }) |
| 167 | } |
| 168 | // Use SQLite |
| 169 | common.SysLog("SQL_DSN not set, using SQLite as database") |
| 170 | common.UsingSQLite = true |
| 171 | return gorm.Open(sqlite.Open(common.SQLitePath), &gorm.Config{ |
| 172 | PrepareStmt: true, // precompile SQL |
| 173 | }) |
| 174 | } |