fix bug and add mysql driver
This commit is contained in:
46
util/db_utils.go
Normal file
46
util/db_utils.go
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"strings"
|
||||
"yggdrasil-go/util/dialector"
|
||||
)
|
||||
|
||||
type DbCfg struct {
|
||||
DatabaseDriver string `ini:"database_driver"`
|
||||
DatabaseDsn string `ini:"database_dsn"`
|
||||
}
|
||||
|
||||
func GetDialector(cfg DbCfg) gorm.Dialector {
|
||||
if driver, ok := dialector.DbDriverDialectors[cfg.DatabaseDriver]; ok {
|
||||
return driver(cfg.DatabaseDsn)
|
||||
} else {
|
||||
keys := make([]string, len(dialector.DbDriverDialectors))
|
||||
p := 0
|
||||
for k := range dialector.DbDriverDialectors {
|
||||
keys[p] = k
|
||||
p++
|
||||
}
|
||||
supported := strings.Join(keys, ",")
|
||||
log.Panicf("Unknown driver: %s\nSupported: %s\n", cfg.DatabaseDriver, supported)
|
||||
return nil
|
||||
}
|
||||
}
|
22
util/dialector/dialectors.go
Normal file
22
util/dialector/dialectors.go
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package dialector
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
var DbDriverDialectors = map[string]func(dsn string) gorm.Dialector{}
|
28
util/dialector/mysql_dialector.go
Normal file
28
util/dialector/mysql_dialector.go
Normal file
@@ -0,0 +1,28 @@
|
||||
//go:build mysql
|
||||
|
||||
/*
|
||||
* Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package dialector
|
||||
|
||||
import (
|
||||
"gorm.io/driver/mysql"
|
||||
)
|
||||
|
||||
func init() {
|
||||
DbDriverDialectors["mysql"] = mysql.Open
|
||||
}
|
26
util/dialector/sqlite_dialector.go
Normal file
26
util/dialector/sqlite_dialector.go
Normal file
@@ -0,0 +1,26 @@
|
||||
//go:build sqlite
|
||||
|
||||
/*
|
||||
* Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package dialector
|
||||
|
||||
import "gorm.io/driver/sqlite"
|
||||
|
||||
func init() {
|
||||
DbDriverDialectors["sqlite"] = sqlite.Open
|
||||
}
|
Reference in New Issue
Block a user