-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
87 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,13 @@ | ||
package branch | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/go-sql-driver/mysql" | ||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
type MysqlService struct { | ||
db *sql.DB | ||
} | ||
|
||
func NewMysqlService(db *sql.DB) (*MysqlService, error) { | ||
if err := db.Ping(); err != nil { | ||
return nil, fmt.Errorf("failed to ping MySQL: %w", err) | ||
} | ||
return &MysqlService{db: db}, nil | ||
} | ||
|
||
func NewMysqlServiceWithConfig(config *mysql.Config) (*MysqlService, error) { | ||
config.MultiStatements = true | ||
db, err := sql.Open("mysql", config.FormatDSN()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to MySQL: %w", err) | ||
} | ||
|
||
service, err := NewMysqlService(db) | ||
if err != nil { | ||
db.Close() | ||
return nil, err | ||
} | ||
|
||
return service, nil | ||
} | ||
|
||
func (m *MysqlService) Close() error { | ||
return m.db.Close() | ||
} | ||
|
||
func (m *MysqlService) Query(query string) (*sql.Rows, error) { | ||
return m.db.Query(query) | ||
} | ||
|
||
func (m *MysqlService) Exec(database, query string) (sql.Result, error) { | ||
ctx := context.Background() | ||
if database != "" { | ||
query = fmt.Sprintf("USE %s; %s", database, query) | ||
} | ||
conn, err := m.db.Conn(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
return conn.ExecContext(ctx, query) | ||
} | ||
|
||
func (m *MysqlService) ExecuteInTxn(queries ...string) error { | ||
tx, err := m.db.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
// make sure to rollback if any query fails | ||
defer tx.Rollback() | ||
|
||
for _, query := range queries { | ||
_, err := tx.Exec(query) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return tx.Commit() | ||
type MysqlService interface { | ||
Query(query string) (*sql.Rows, error) | ||
Exec(database, query string) (sql.Result, error) | ||
ExecuteInTxn(queries ...string) error | ||
} |
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,76 @@ | ||
package branch | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/go-sql-driver/mysql" | ||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
type NativeMysqlService struct { | ||
db *sql.DB | ||
} | ||
|
||
func NewMysqlService(db *sql.DB) (*NativeMysqlService, error) { | ||
if err := db.Ping(); err != nil { | ||
return nil, fmt.Errorf("failed to ping MySQL: %w", err) | ||
} | ||
return &NativeMysqlService{db: db}, nil | ||
} | ||
|
||
func NewMysqlServiceWithConfig(config *mysql.Config) (*NativeMysqlService, error) { | ||
config.MultiStatements = true | ||
db, err := sql.Open("mysql", config.FormatDSN()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to MySQL: %w", err) | ||
} | ||
|
||
service, err := NewMysqlService(db) | ||
if err != nil { | ||
db.Close() | ||
return nil, err | ||
} | ||
|
||
return service, nil | ||
} | ||
|
||
func (m *NativeMysqlService) Close() error { | ||
return m.db.Close() | ||
} | ||
|
||
func (m *NativeMysqlService) Query(query string) (*sql.Rows, error) { | ||
return m.db.Query(query) | ||
} | ||
|
||
func (m *NativeMysqlService) Exec(database, query string) (sql.Result, error) { | ||
ctx := context.Background() | ||
if database != "" { | ||
query = fmt.Sprintf("USE %s; %s", database, query) | ||
} | ||
conn, err := m.db.Conn(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
return conn.ExecContext(ctx, query) | ||
} | ||
|
||
func (m *NativeMysqlService) ExecuteInTxn(queries ...string) error { | ||
tx, err := m.db.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
// make sure to rollback if any query fails | ||
defer tx.Rollback() | ||
|
||
for _, query := range queries { | ||
_, err := tx.Exec(query) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return tx.Commit() | ||
} |
File renamed without changes.
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