Skip to content

Repository files navigation

Mix XSQL

A lightweight database based on database/sql, feature complete and supports any database driver.

Installation

go get github.com/mix-go/xsql

Initialization

import _ "github.com/go-sql-driver/mysql"db, err:=sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8")
iferr!=nil {
log.Fatal(err)
}
DB:=xsql.New(db)
  • Oracle initialization, using sijms/go-ora/v2 driver (no need to install instantclient).
import _ "github.com/sijms/go-ora/v2"db, err:=sql.Open("oracle", "oracle://root:123456@127.0.0.1:1521/orcl")
iferr!=nil {
log.Fatal(err)
}
DB:=xsql.New(db, xsql.UseOracle())

Query

You can use it like a scripting language, not binding the struct, directly and freely get the value of each field.

Oracle field, table name needs to be uppercase

rows, err:=DB.Query(context.Background(), "SELECT * FROM xsql")
iferr!=nil {
log.Fatal(err)
}
id:=rows[0].Get("id").Int()
foo:=rows[0].Get("foo").String()
bar:=rows[0].Get("bar").Time() // time.Timeval:=rows[0].Get("bar").Value() // interface{}
row, err:=DB.QueryFirst(context.Background(), "SELECT * FROM xsql WHERE id = ?", 1)
iferr!=nil {
log.Fatal(err)
}
id:=row.Get("id").Int()
foo:=row.Get("foo").String()
bar:=row.Get("bar").Time() // time.Timeval:=row.Get("bar").Value() // interface{}

Mapping

Of course, you can also map usage like gorm, xorm.

Oracle field, table name needs to be uppercase

typeTeststruct {
Idint`xsql:"id"`Foostring`xsql:"foo"`Bar time.Time`xsql:"bar"`// oracle uses go_ora.TimeStamp
}
func (t*Test) TableName() string {
return"tableName"
}

First

Map the first row

Oracle placeholder needs to be modified to :id

vartestTesterr:=DB.First(context.Background(), &test, "SELECT * FROM ${TABLE} WHERE id = ?", 1).Erroriferr!=nil {
log.Fatal(err)
}

Find

Map all rows

vartests []*Testerr:=DB.Find(context.Background(), &tests, "SELECT * FROM ${TABLE}").Erroriferr!=nil {
log.Fatal(err)
}

Insert

test:=Test{
Id: 0,
Foo: "test",
Bar: time.Now(),
}
err:=DB.Insert(context.Background(), &test).Erroriferr!=nil {
log.Fatal(err)
}

BatchInsert

tests:= []Test{
{
Id: 0,
Foo: "test",
Bar: time.Now(),
},
{
Id: 0,
Foo: "test",
Bar: time.Now(),
},
}
err:=DB.BatchInsert(context.Background(), &tests).Erroriferr!=nil {
log.Fatal(err)
}

Update

Oracle placeholder needs to be modified to :id

Update all columns

test:=Test{
Id: 8,
Foo: "test",
Bar: time.Now(),
}
err:=DB.Update(context.Background(), &test, "id = ?", test.Id).Erroriferr!=nil {
log.Fatal(err)
}

Update specific columns by map

data:=map[string]interface{}{
"foo": "test",
}
err:=DB.Model(&Test{}).Update(context.Background(), data, "id = ?", 8).Erroriferr!=nil {
log.Fatal(err)
}

Update specific columns by struct pointer

test:=Test{}
data, err:=xsql.TagValuesMap(DB.Options.Tag, &test,
xsql.TagValues{
{&test.Foo, "test"},
},
)
iferr!=nil {
log.Fatal(err)
}
err=DB.Model(&test).Update(context.Background(), data, "id = ?", 8).Erroriferr!=nil {
log.Fatal(err)
}

Delete

Oracle placeholder needs to be modified to :id

test:=Test{
Id: 8,
Foo: "test",
Bar: time.Now(),
}
err:=DB.Model(&test).Delete(context.Background(), "id = ?", test.Id).Erroriferr!=nil {
log.Fatal(err)
}
err:=DB.Model(&Test{}).Delete(context.Background(), "id = ?", 8).Erroriferr!=nil {
log.Fatal(err)
}

Exec

Use Exec() to manually execute the delete, you can also manually execute the update operation.

Oracle placeholder needs to be modified to :id

err:=DB.Exec(context.Background(), "DELETE FROM xsql WHERE id = ?", 8).Erroriferr!=nil {
log.Fatal(err)
}

Transaction

tx, err:=DB.Begin()
iferr!=nil {
log.Fatal(err)
}
test:=Test{
Id: 0,
Foo: "test",
Bar: time.Now(),
}
err=tx.Insert(context.Background(), &test).Erroriferr!=nil {
tx.Rollback()
log.Fatal(err)
}
tx.Commit()

Configuration

You can pass the following configuration object in the xsql.New() method

  • Default to mysql mode, when switching to oracle, you need to modify the configuration
  • Insert(), BatchInsert() can pass in configuration during execution to override insert related configuration, such as modifying InsertKey to REPLACE INTO
typesqlOptionsstruct {
// Default: xsqlTagstring// Default: INSERT INTOInsertKeystring// Default: ${TABLE}TableKeystring// Default: ?// For oracle, can be configured as :%dPlaceholderstring// Default: `// For oracle, can be configured as "ColumnQuotesstring// Default: 2006-01-02 15:04:05TimeLayoutstring// Default: time.LocalTimeLocation*time.Location// Default: func(placeholder string) string { return placeholder }// For oracle, this closure can be modified to add TO_TIMESTAMPTimeFuncTimeFunc// Global debug SQLDebugFuncDebugFunc
}

Log

Pass in the configuration DebugFunc when using the xsql.New() method, you can print SQL information using any log library here.

DB:=xsql.New(
db,
xsql.WithDebugFunc(func(l*xsql.Log) {
log.Println(l)
}),
)

The log object contains the following fields

typeLogstruct {
Context context.Context`json:"context"`Duration time.Duration`json:"duration"`SQLstring`json:"sql"`Bindings []interface{} `json:"bindings"`RowsAffectedint64`json:"rowsAffected"`Errorerror`json:"error"`
}

License

Apache License Version 2.0, http://www.apache.org/licenses/

About

A lightweight database based on database/sql | 基于 database/sql 的轻量数据库,功能完备且支持任何数据库驱动 (支持Oracle)

Resources

Stars

9 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages