fix bug and add mysql driver

This commit is contained in:
2022-09-18 16:17:25 +08:00
parent 3dd53caea4
commit 5296272dc3
12 changed files with 263 additions and 23 deletions

46
util/db_utils.go Normal file
View 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
}
}

View 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{}

View 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
}

View 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
}