feat: added modules

This commit is contained in:
Marvin Zhang
2024-06-14 15:59:48 +08:00
parent 0b67fd9ece
commit dc21bce11f
138 changed files with 3231 additions and 120 deletions

36
db/sql/sql.go Normal file
View File

@@ -0,0 +1,36 @@
package sql
import (
"errors"
"fmt"
"github.com/crawlab-team/go-trace"
"github.com/jmoiron/sqlx"
)
func GetSqlDatabaseConnectionString(dataSourceType string, host string, port string, username string, password string, database string) (connStr string, err error) {
if dataSourceType == "mysql" {
connStr = fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", username, password, host, port, database)
} else if dataSourceType == "postgres" {
connStr = fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s", host, port, username, database, password, "disable")
} else {
err = errors.New(dataSourceType + " is not implemented")
return connStr, trace.TraceError(err)
}
return connStr, nil
}
func GetSqlConn(dataSourceType string, host string, port string, username string, password string, database string) (db *sqlx.DB, err error) {
// get database connection string
connStr, err := GetSqlDatabaseConnectionString(dataSourceType, host, port, username, password, database)
if err != nil {
return db, trace.TraceError(err)
}
// get database instance
db, err = sqlx.Open(dataSourceType, connStr)
if err != nil {
return db, trace.TraceError(err)
}
return db, nil
}