Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
add mariadb [nt]
Browse files Browse the repository at this point in the history
  • Loading branch information
flarco committed Feb 3, 2024
1 parent 0fa6656 commit da3d9d9
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 7 deletions.
5 changes: 5 additions & 0 deletions connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ func (c *Connection) setURL() (err error) {
setIfMissing("password", "")
setIfMissing("port", c.Type.DefPort())
template = "mysql://{username}:{password}@{host}:{port}/{database}"
case dbio.TypeDbMariaDB:
setIfMissing("username", c.Data["user"])
setIfMissing("password", "")
setIfMissing("port", c.Type.DefPort())
template = "mariadb://{username}:{password}@{host}:{port}/{database}"
case dbio.TypeDbBigQuery:
setIfMissing("dataset", c.Data["schema"])
setIfMissing("schema", c.Data["dataset"])
Expand Down
4 changes: 3 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ func NewConnContext(ctx context.Context, URL string, props ...string) (Connectio
conn = &MsSQLServerConn{URL: URL}
} else if strings.HasPrefix(URL, "mysql:") {
conn = &MySQLConn{URL: URL}
} else if strings.HasPrefix(URL, "mariadb:") {
conn = &MySQLConn{URL: URL}
} else if strings.HasPrefix(URL, "oracle:") {
conn = &OracleConn{URL: URL}
// concurrency = 2
Expand Down Expand Up @@ -320,7 +322,7 @@ func getDriverName(dbType dbio.Type) (driverName string) {
switch dbType {
case dbio.TypeDbPostgres, dbio.TypeDbRedshift:
driverName = "postgres"
case dbio.TypeDbMySQL:
case dbio.TypeDbMySQL, dbio.TypeDbMariaDB:
driverName = "mysql"
case dbio.TypeDbOracle:
driverName = "godror"
Expand Down
27 changes: 24 additions & 3 deletions database/database_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/xo/dburl"
)

// MySQLConn is a Postgres connection
// MySQLConn is a MySQL or MariaDB connection
type MySQLConn struct {
BaseConn
URL string
Expand All @@ -28,6 +28,10 @@ func (conn *MySQLConn) Init() error {
conn.BaseConn.Type = dbio.TypeDbMySQL
conn.BaseConn.defaultPort = 3306

if strings.HasPrefix(conn.URL, "maria") {
conn.BaseConn.Type = dbio.TypeDbMariaDB
}

// Turn off Bulk export for now
// the LoadDataOutFile needs special circumstances
conn.BaseConn.SetProp("allow_bulk_export", "false")
Expand Down Expand Up @@ -229,11 +233,28 @@ func (conn *MySQLConn) GenerateUpsertSQL(srcTable string, tgtTable string, pkFie
return
}

srcT, err := ParseTableName(srcTable, conn.GetType())
if err != nil {
err = g.Error(err, "could not generate parse srcTable")
return
}

tgtT, err := ParseTableName(tgtTable, conn.GetType())
if err != nil {
err = g.Error(err, "could not generate parse tgtTable")
return
}

// replace src & tgt to make compatible to MariaDB
// see https://github.com/slingdata-io/sling-cli/issues/135
upsertMap["src_tgt_pk_equal"] = strings.ReplaceAll(upsertMap["src_tgt_pk_equal"], "src.", srcT.NameQ()+".")
upsertMap["src_tgt_pk_equal"] = strings.ReplaceAll(upsertMap["src_tgt_pk_equal"], "tgt.", tgtT.NameQ()+".")

sqlTemplate := `
DELETE FROM {tgt_table} tgt
DELETE FROM {tgt_table}
WHERE EXISTS (
SELECT 1
FROM {src_table} src
FROM {src_table}
WHERE {src_tgt_pk_equal}
)
;
Expand Down
2 changes: 1 addition & 1 deletion database/schemata.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func ParseColumnName(text string, dialect dbio.Type) (colName string, err error)
func GetQualifierQuote(dialect dbio.Type) string {
quote := `"`
switch dialect {
case dbio.TypeDbMySQL, dbio.TypeDbBigQuery, dbio.TypeDbClickhouse:
case dbio.TypeDbMySQL, dbio.TypeDbMariaDB, dbio.TypeDbBigQuery, dbio.TypeDbClickhouse:
quote = "`"
case dbio.TypeDbBigTable:
quote = ""
Expand Down
Loading

0 comments on commit da3d9d9

Please sign in to comment.