Giter VIP home page Giter VIP logo

core's Introduction

Core is a lightweight wrapper of sql.DB.

CircleCI

Open

db, _ := core.Open(db, connstr)

SetMapper

db.SetMapper(SameMapper())

Scan usage

Scan

rows, _ := db.Query()
for rows.Next() {
    rows.Scan()
}

ScanMap

rows, _ := db.Query()
for rows.Next() {
    rows.ScanMap()

ScanSlice

You can use []string, [][]byte, []interface{}, []*string, []sql.NullString to ScanSclice. Notice, slice's length should be equal or less than select columns.

rows, _ := db.Query()
cols, _ := rows.Columns()
for rows.Next() {
    var s = make([]string, len(cols))
    rows.ScanSlice(&s)
}
rows, _ := db.Query()
cols, _ := rows.Columns()
for rows.Next() {
    var s = make([]*string, len(cols))
    rows.ScanSlice(&s)
}

ScanStruct

rows, _ := db.Query()
for rows.Next() {
    rows.ScanStructByName()
    rows.ScanStructByIndex()
}

Query usage

rows, err := db.Query("select * from table where name = ?", name)

user = User{
    Name:"lunny",
}
rows, err := db.QueryStruct("select * from table where name = ?Name",
            &user)

var user = map[string]interface{}{
    "name": "lunny",
}
rows, err = db.QueryMap("select * from table where name = ?name",
            &user)

QueryRow usage

row := db.QueryRow("select * from table where name = ?", name)

user = User{
    Name:"lunny",
}
row := db.QueryRowStruct("select * from table where name = ?Name",
            &user)

var user = map[string]interface{}{
    "name": "lunny",
}
row = db.QueryRowMap("select * from table where name = ?name",
            &user)

Exec usage

db.Exec("insert into user (`name`, title, age, alias, nick_name,created) values (?,?,?,?,?,?)", name, title, age, alias...)

user = User{
    Name:"lunny",
    Title:"test",
    Age: 18,
}
result, err = db.ExecStruct("insert into user (`name`, title, age, alias, nick_name,created) values (?Name,?Title,?Age,?Alias,?NickName,?Created)",
            &user)

var user = map[string]interface{}{
    "Name": "lunny",
    "Title": "test",
    "Age": 18,
}
result, err = db.ExecMap("insert into user (`name`, title, age, alias, nick_name,created) values (?Name,?Title,?Age,?Alias,?NickName,?Created)",
            &user)

core's People

Contributors

admpub avatar aviau avatar edwardbetts avatar evalphobia avatar gavv avatar hechen0 avatar leafsoar avatar lunny avatar m2nlight avatar micanzhang avatar nashtsai avatar nemec784 avatar neopallium avatar oinume avatar p581581 avatar psmit avatar tossp avatar u0x01 avatar wangsongyan avatar xpzouying avatar ymhhh avatar yonekawa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

core's Issues

rows.go的ReflectNew当大数据量时,并发多个请求出现out of range错误

func ReflectNew(typ reflect.Type) reflect.Value {
	reflectCacheMutex.RLock()
	cs, ok := reflectCache[typ]
	reflectCacheMutex.RUnlock()

	const newSize = 200

	if !ok || cs.idx+1 > newSize-1 {
		cs = &cacheStruct{reflect.MakeSlice(reflect.SliceOf(typ), newSize, newSize), 0}
		reflectCacheMutex.Lock()
		reflectCache[typ] = cs
		reflectCacheMutex.Unlock()
	} else {
		reflectCacheMutex.Lock()
		cs.idx = cs.idx + 1
		reflectCacheMutex.Unlock()
	}
	//todo 当大数据量时,并发多个请求出现out of range错误
	return cs.value.Index(cs.idx).Addr()
}

failing tests

Hello,

I am getting the following test failures:

=== RUN   TestExecMap
-- {1 xlw tester 1.2 lunny lunny xiao {0 0 <nil>}}
--- FAIL: TestExecMap (0.13s)
    db_test.go:580: sql: Scan error on column index 6: unsupported driver -> Scan pair: time.Time -> *core.NullTime
=== RUN   TestExecStruct
1-- {1 xlw tester 1.2 lunny lunny xiao {0 0 <nil>}}
--- FAIL: TestExecStruct (0.13s)
    db_test.go:622: sql: Scan error on column index 6: unsupported driver -> Scan pair: time.Time -> *core.NullTime

any idea how to fix this?

Thanks

Add db.InsertStruct(Table, *interface{}) support

This package is extremely helpful when you want to simplify sql usage in your app without requiring the fullness of xorm so thank you for writing it.

One thing that would make this library a lot more useful is a new func on core.DB, It should allow users to specify the table and provide the struct and automatically generate the required sql for it.

For example

user = User{
    Name:"lunny",
    Title:"test",
    Age: 18,
}
result, err = db.ExecStruct("insert into user (`name`, title, age, alias, nick_name,created) values (?Name,?Title,?Age,?Alias,?NickName,?Created)",
            &user)

would become

user = User{
    Name:"lunny",
    Title:"test",
    Age: 18,
}
result, err = db.InsertStruct("user", &user)

What do you think?

License

Hello!

I would like to package go-xorm/core for Debian

However, I cound not find a license.

Can you please add one?

*sql.DB

Hey guys,

Is where an easy way to get the *sql.DB object from the *core.DB? I want to pass it into mysql store instead of creating another connection.
selection_007

selection_005
selection_006

selection_008
selection_009

core QueryMap paramaters Error

sql=delete from t1 where id=?id delete from t2 where id=?id
QueryMap(sql,[id]value)
but sql change to
' delete from t1 where id=value
delete from t2 where id=?
'

benchmark test error

after execute go test -v -bench=., got this error message.

goos: darwin
goarch: amd64
pkg: github.com/go-xorm/core
BenchmarkOriQuery-8                  	    1000	   1266147 ns/op
BenchmarkStructQuery-8               	    1000	   1921657 ns/op
BenchmarkStruct2Query-8              	     500	   2829271 ns/op
panic: interface conversion: interface {} is []uint8, not *string

goroutine 2903 [running]:
github.com/go-xorm/core.BenchmarkSliceInterfaceQuery(0xc42055b980)
	/Users/carllhw/Projects/go/src/github.com/go-xorm/core/db_test.go:232 +0x7b2
testing.(*B).runN(0xc42055b980, 0x1)
	/usr/local/Cellar/go/1.10.2/libexec/src/testing/benchmark.go:141 +0xb2
testing.(*B).run1.func1(0xc42055b980)
	/usr/local/Cellar/go/1.10.2/libexec/src/testing/benchmark.go:214 +0x5a
created by testing.(*B).run1
	/usr/local/Cellar/go/1.10.2/libexec/src/testing/benchmark.go:207 +0x80
exit status 2
FAIL	github.com/go-xorm/core	5.919s

Please tag xorm and core together

Hello,

There is no rush, as we have already uploaded xorm and core to Debian, but it would help us if you tagged xorm and core so that they build together. I have ran into the issue that the last xorm tag did not build with the tip of core.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.