-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sql): create a mysql driver using gorm
- Loading branch information
1 parent
dc018a5
commit 8cf19b2
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package sql | ||
|
||
import ( | ||
"fmt" | ||
|
||
"gorm.io/driver/mysql" | ||
) | ||
|
||
func MySQL(database string, host string, password string, port int, user string) *SQL { | ||
var dsn string | ||
|
||
if user != "" && password != "" { | ||
dsn += fmt.Sprintf("%s:%s@", user, password) | ||
} | ||
if host != "" { | ||
dsn += fmt.Sprintf("tcp(%s:%d)", host, port) | ||
} | ||
dsn += fmt.Sprintf("/%s", database) | ||
|
||
return &SQL{ | ||
driver: mysql.Open(dsn), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package sql | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
type SQL struct { | ||
driver gorm.Dialector | ||
activeConnection *gorm.DB | ||
} | ||
|
||
func (db *SQL) Auth() (err error) { | ||
// Connect to the database | ||
if gormDb, err := gorm.Open(db.driver, &gorm.Config{}); err != nil { | ||
return err | ||
} else { | ||
// Success - store the connection | ||
db.activeConnection = gormDb | ||
} | ||
|
||
// Perform a simple query to ensure connectivity | ||
rows, err := db.activeConnection.Raw("SELECT 1 + 1 AS result").Rows() | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
err = rows.Close() | ||
}() | ||
|
||
return err | ||
} | ||
|
||
func (db *SQL) Close() error { | ||
return nil | ||
} | ||
|
||
func (db *SQL) InsertBulk(table string, data []map[string]interface{}) (int, error) { | ||
return len(data), nil | ||
} | ||
|
||
func (db *SQL) Truncate(table string) error { | ||
return nil | ||
} |