remove core and builder from vendor since it will break go get
This commit is contained in:
parent
41aff46cc7
commit
47cb51deca
|
@ -0,0 +1,59 @@
|
||||||
|
// Copyright 2017 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package xorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJoinLimit(t *testing.T) {
|
||||||
|
assert.NoError(t, prepareEngine())
|
||||||
|
|
||||||
|
type Salary struct {
|
||||||
|
Id int64
|
||||||
|
Lid int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type CheckList struct {
|
||||||
|
Id int64
|
||||||
|
Eid int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Empsetting struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, testEngine.Sync2(new(Salary), new(CheckList), new(Empsetting)))
|
||||||
|
|
||||||
|
var emp Empsetting
|
||||||
|
cnt, err := testEngine.Insert(&emp)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, cnt)
|
||||||
|
|
||||||
|
var checklist = CheckList{
|
||||||
|
Eid: emp.Id,
|
||||||
|
}
|
||||||
|
cnt, err = testEngine.Insert(&checklist)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, cnt)
|
||||||
|
|
||||||
|
var salary = Salary{
|
||||||
|
Lid: checklist.Id,
|
||||||
|
}
|
||||||
|
cnt, err = testEngine.Insert(&salary)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, cnt)
|
||||||
|
|
||||||
|
var salaries []Salary
|
||||||
|
err = testEngine.Table("salary").
|
||||||
|
Join("INNER", "check_list", "check_list.id = salary.lid").
|
||||||
|
Join("LEFT", "empsetting", "empsetting.id = check_list.eid").
|
||||||
|
Limit(10, 0).
|
||||||
|
Find(&salaries)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
|
@ -1,27 +0,0 @@
|
||||||
Copyright (c) 2016 The Xorm Authors
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
* Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
* Neither the name of the {organization} nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
@ -1,175 +0,0 @@
|
||||||
# SQL builder
|
|
||||||
|
|
||||||
[](https://circleci.com/gh/go-xorm/builder/tree/master)
|
|
||||||
|
|
||||||
Package builder is a lightweight and fast SQL builder for Go and XORM.
|
|
||||||
|
|
||||||
Make sure you have installed Go 1.1+ and then:
|
|
||||||
|
|
||||||
go get github.com/go-xorm/builder
|
|
||||||
|
|
||||||
# Insert
|
|
||||||
|
|
||||||
```Go
|
|
||||||
sql, args, err := Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()
|
|
||||||
```
|
|
||||||
|
|
||||||
# Select
|
|
||||||
|
|
||||||
```Go
|
|
||||||
sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
|
|
||||||
|
|
||||||
sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
|
|
||||||
RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
|
|
||||||
```
|
|
||||||
|
|
||||||
# Update
|
|
||||||
|
|
||||||
```Go
|
|
||||||
sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()
|
|
||||||
```
|
|
||||||
|
|
||||||
# Delete
|
|
||||||
|
|
||||||
```Go
|
|
||||||
sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()
|
|
||||||
```
|
|
||||||
|
|
||||||
# Conditions
|
|
||||||
|
|
||||||
* `Eq` is a redefine of a map, you can give one or more conditions to `Eq`
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Eq{"a":1})
|
|
||||||
// a=? [1]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
|
|
||||||
// b=? AND c=? ["c", 0]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
|
|
||||||
// b=? AND c=? ["c", 0]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
|
|
||||||
// b=? OR b=? ["c", "d"]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
|
|
||||||
// b IN (?,?) ["c", "d"]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
|
|
||||||
// b=? AND c IN (?,?) [1, 2, 3]
|
|
||||||
```
|
|
||||||
|
|
||||||
* `Neq` is the same to `Eq`
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Neq{"a":1})
|
|
||||||
// a<>? [1]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
|
|
||||||
// b<>? AND c<>? ["c", 0]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
|
|
||||||
// b<>? AND c<>? ["c", 0]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
|
|
||||||
// b<>? OR b<>? ["c", "d"]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
|
|
||||||
// b NOT IN (?,?) ["c", "d"]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
|
|
||||||
// b<>? AND c NOT IN (?,?) [1, 2, 3]
|
|
||||||
```
|
|
||||||
|
|
||||||
* `Gt`, `Gte`, `Lt`, `Lte`
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
|
|
||||||
// a>? AND b>=? [1, 2]
|
|
||||||
sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
|
|
||||||
// a<? OR b<=? [1, 2]
|
|
||||||
```
|
|
||||||
|
|
||||||
* `Like`
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Like{"a", "c"})
|
|
||||||
// a LIKE ? [%c%]
|
|
||||||
```
|
|
||||||
|
|
||||||
* `Expr` you can customerize your sql with `Expr`
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Expr("a = ? ", 1))
|
|
||||||
// a = ? [1]
|
|
||||||
sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
|
|
||||||
// a=(select id from table where c = ?) [1]
|
|
||||||
```
|
|
||||||
|
|
||||||
* `In` and `NotIn`
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(In("a", 1, 2, 3))
|
|
||||||
// a IN (?,?,?) [1,2,3]
|
|
||||||
sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
|
|
||||||
// a IN (?,?,?) [1,2,3]
|
|
||||||
sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
|
|
||||||
// a IN (select id from b where c = ?) [1]
|
|
||||||
```
|
|
||||||
|
|
||||||
* `IsNull` and `NotNull`
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(IsNull{"a"})
|
|
||||||
// a IS NULL []
|
|
||||||
sql, args, _ := ToSQL(NotNull{"b"})
|
|
||||||
// b IS NOT NULL []
|
|
||||||
```
|
|
||||||
|
|
||||||
* `And(conds ...Cond)`, And can connect one or more condtions via And
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
|
|
||||||
// a=? AND b LIKE ? AND d<>? [1, %c%, 2]
|
|
||||||
```
|
|
||||||
|
|
||||||
* `Or(conds ...Cond)`, Or can connect one or more conditions via Or
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
|
|
||||||
// a=? OR b LIKE ? OR d<>? [1, %c%, 2]
|
|
||||||
sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
|
|
||||||
// a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
|
|
||||||
```
|
|
||||||
|
|
||||||
* `Between`
|
|
||||||
|
|
||||||
```Go
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Between{"a", 1, 2})
|
|
||||||
// a BETWEEN 1 AND 2
|
|
||||||
```
|
|
||||||
|
|
||||||
* Define yourself conditions
|
|
||||||
|
|
||||||
Since `Cond` is an interface.
|
|
||||||
|
|
||||||
```Go
|
|
||||||
type Cond interface {
|
|
||||||
WriteTo(Writer) error
|
|
||||||
And(...Cond) Cond
|
|
||||||
Or(...Cond) Cond
|
|
||||||
IsValid() bool
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
You can define yourself conditions and compose with other `Cond`.
|
|
|
@ -1,190 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
type optype byte
|
|
||||||
|
|
||||||
const (
|
|
||||||
condType optype = iota // only conditions
|
|
||||||
selectType // select
|
|
||||||
insertType // insert
|
|
||||||
updateType // update
|
|
||||||
deleteType // delete
|
|
||||||
)
|
|
||||||
|
|
||||||
type join struct {
|
|
||||||
joinType string
|
|
||||||
joinTable string
|
|
||||||
joinCond Cond
|
|
||||||
}
|
|
||||||
|
|
||||||
// Builder describes a SQL statement
|
|
||||||
type Builder struct {
|
|
||||||
optype
|
|
||||||
tableName string
|
|
||||||
cond Cond
|
|
||||||
selects []string
|
|
||||||
joins []join
|
|
||||||
inserts Eq
|
|
||||||
updates []Eq
|
|
||||||
}
|
|
||||||
|
|
||||||
// Select creates a select Builder
|
|
||||||
func Select(cols ...string) *Builder {
|
|
||||||
builder := &Builder{cond: NewCond()}
|
|
||||||
return builder.Select(cols...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert creates an insert Builder
|
|
||||||
func Insert(eq Eq) *Builder {
|
|
||||||
builder := &Builder{cond: NewCond()}
|
|
||||||
return builder.Insert(eq)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update creates an update Builder
|
|
||||||
func Update(updates ...Eq) *Builder {
|
|
||||||
builder := &Builder{cond: NewCond()}
|
|
||||||
return builder.Update(updates...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete creates a delete Builder
|
|
||||||
func Delete(conds ...Cond) *Builder {
|
|
||||||
builder := &Builder{cond: NewCond()}
|
|
||||||
return builder.Delete(conds...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Where sets where SQL
|
|
||||||
func (b *Builder) Where(cond Cond) *Builder {
|
|
||||||
b.cond = b.cond.And(cond)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// From sets the table name
|
|
||||||
func (b *Builder) From(tableName string) *Builder {
|
|
||||||
b.tableName = tableName
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Into sets insert table name
|
|
||||||
func (b *Builder) Into(tableName string) *Builder {
|
|
||||||
b.tableName = tableName
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Join sets join table and contions
|
|
||||||
func (b *Builder) Join(joinType, joinTable string, joinCond interface{}) *Builder {
|
|
||||||
switch joinCond.(type) {
|
|
||||||
case Cond:
|
|
||||||
b.joins = append(b.joins, join{joinType, joinTable, joinCond.(Cond)})
|
|
||||||
case string:
|
|
||||||
b.joins = append(b.joins, join{joinType, joinTable, Expr(joinCond.(string))})
|
|
||||||
}
|
|
||||||
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// InnerJoin sets inner join
|
|
||||||
func (b *Builder) InnerJoin(joinTable string, joinCond interface{}) *Builder {
|
|
||||||
return b.Join("INNER", joinTable, joinCond)
|
|
||||||
}
|
|
||||||
|
|
||||||
// LeftJoin sets left join SQL
|
|
||||||
func (b *Builder) LeftJoin(joinTable string, joinCond interface{}) *Builder {
|
|
||||||
return b.Join("LEFT", joinTable, joinCond)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RightJoin sets right join SQL
|
|
||||||
func (b *Builder) RightJoin(joinTable string, joinCond interface{}) *Builder {
|
|
||||||
return b.Join("RIGHT", joinTable, joinCond)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CrossJoin sets cross join SQL
|
|
||||||
func (b *Builder) CrossJoin(joinTable string, joinCond interface{}) *Builder {
|
|
||||||
return b.Join("CROSS", joinTable, joinCond)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FullJoin sets full join SQL
|
|
||||||
func (b *Builder) FullJoin(joinTable string, joinCond interface{}) *Builder {
|
|
||||||
return b.Join("FULL", joinTable, joinCond)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Select sets select SQL
|
|
||||||
func (b *Builder) Select(cols ...string) *Builder {
|
|
||||||
b.selects = cols
|
|
||||||
b.optype = selectType
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// And sets AND condition
|
|
||||||
func (b *Builder) And(cond Cond) *Builder {
|
|
||||||
b.cond = And(b.cond, cond)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or sets OR condition
|
|
||||||
func (b *Builder) Or(cond Cond) *Builder {
|
|
||||||
b.cond = Or(b.cond, cond)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert sets insert SQL
|
|
||||||
func (b *Builder) Insert(eq Eq) *Builder {
|
|
||||||
b.inserts = eq
|
|
||||||
b.optype = insertType
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update sets update SQL
|
|
||||||
func (b *Builder) Update(updates ...Eq) *Builder {
|
|
||||||
b.updates = updates
|
|
||||||
b.optype = updateType
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete sets delete SQL
|
|
||||||
func (b *Builder) Delete(conds ...Cond) *Builder {
|
|
||||||
b.cond = b.cond.And(conds...)
|
|
||||||
b.optype = deleteType
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteTo implements Writer interface
|
|
||||||
func (b *Builder) WriteTo(w Writer) error {
|
|
||||||
switch b.optype {
|
|
||||||
case condType:
|
|
||||||
return b.cond.WriteTo(w)
|
|
||||||
case selectType:
|
|
||||||
return b.selectWriteTo(w)
|
|
||||||
case insertType:
|
|
||||||
return b.insertWriteTo(w)
|
|
||||||
case updateType:
|
|
||||||
return b.updateWriteTo(w)
|
|
||||||
case deleteType:
|
|
||||||
return b.deleteWriteTo(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ErrNotSupportType
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToSQL convert a builder to SQL and args
|
|
||||||
func (b *Builder) ToSQL() (string, []interface{}, error) {
|
|
||||||
w := NewWriter()
|
|
||||||
if err := b.WriteTo(w); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return w.writer.String(), w.args, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToSQL convert a builder or condtions to SQL and args
|
|
||||||
func ToSQL(cond interface{}) (string, []interface{}, error) {
|
|
||||||
switch cond.(type) {
|
|
||||||
case Cond:
|
|
||||||
return condToSQL(cond.(Cond))
|
|
||||||
case *Builder:
|
|
||||||
return cond.(*Builder).ToSQL()
|
|
||||||
}
|
|
||||||
return "", nil, ErrNotSupportType
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (b *Builder) deleteWriteTo(w Writer) error {
|
|
||||||
if len(b.tableName) <= 0 {
|
|
||||||
return errors.New("no table indicated")
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, "DELETE FROM %s WHERE ", b.tableName); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.cond.WriteTo(w)
|
|
||||||
}
|
|
|
@ -1,64 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (b *Builder) insertWriteTo(w Writer) error {
|
|
||||||
if len(b.tableName) <= 0 {
|
|
||||||
return errors.New("no table indicated")
|
|
||||||
}
|
|
||||||
if len(b.inserts) <= 0 {
|
|
||||||
return errors.New("no column to be update")
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, "INSERT INTO %s (", b.tableName); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var args = make([]interface{}, 0)
|
|
||||||
var bs []byte
|
|
||||||
var valBuffer = bytes.NewBuffer(bs)
|
|
||||||
var i = 0
|
|
||||||
for col, value := range b.inserts {
|
|
||||||
fmt.Fprint(w, col)
|
|
||||||
if e, ok := value.(expr); ok {
|
|
||||||
fmt.Fprint(valBuffer, e.sql)
|
|
||||||
args = append(args, e.args...)
|
|
||||||
} else {
|
|
||||||
fmt.Fprint(valBuffer, "?")
|
|
||||||
args = append(args, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
if i != len(b.inserts)-1 {
|
|
||||||
if _, err := fmt.Fprint(w, ","); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprint(valBuffer, ","); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i = i + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprint(w, ") Values ("); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := w.Write(valBuffer.Bytes()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprint(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Append(args...)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,53 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (b *Builder) selectWriteTo(w Writer) error {
|
|
||||||
if len(b.tableName) <= 0 {
|
|
||||||
return errors.New("no table indicated")
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprint(w, "SELECT "); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(b.selects) > 0 {
|
|
||||||
for i, s := range b.selects {
|
|
||||||
if _, err := fmt.Fprint(w, s); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if i != len(b.selects)-1 {
|
|
||||||
if _, err := fmt.Fprint(w, ","); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if _, err := fmt.Fprint(w, "*"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, " FROM %s", b.tableName); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range b.joins {
|
|
||||||
fmt.Fprintf(w, " %s JOIN %s ON ", v.joinType, v.joinTable)
|
|
||||||
if err := v.joinCond.WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprint(w, " WHERE "); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.cond.WriteTo(w)
|
|
||||||
}
|
|
|
@ -1,197 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MyInt int
|
|
||||||
|
|
||||||
func TestBuilderCond(t *testing.T) {
|
|
||||||
var cases = []struct {
|
|
||||||
cond Cond
|
|
||||||
sql string
|
|
||||||
args []interface{}
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
Eq{"a": 1}.And(Like{"b", "c"}).Or(Eq{"a": 2}.And(Like{"b", "g"})),
|
|
||||||
"(a=? AND b LIKE ?) OR (a=? AND b LIKE ?)",
|
|
||||||
[]interface{}{1, "%c%", 2, "%g%"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Eq{"a": 1}.Or(Like{"b", "c"}).And(Eq{"a": 2}.Or(Like{"b", "g"})),
|
|
||||||
"(a=? OR b LIKE ?) AND (a=? OR b LIKE ?)",
|
|
||||||
[]interface{}{1, "%c%", 2, "%g%"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Eq{"d": []string{"e", "f"}},
|
|
||||||
"d IN (?,?)",
|
|
||||||
[]interface{}{"e", "f"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Neq{"d": []string{"e", "f"}},
|
|
||||||
"d NOT IN (?,?)",
|
|
||||||
[]interface{}{"e", "f"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Lt{"d": 3},
|
|
||||||
"d<?",
|
|
||||||
[]interface{}{3},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Lte{"d": 3},
|
|
||||||
"d<=?",
|
|
||||||
[]interface{}{3},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Gt{"d": 3},
|
|
||||||
"d>?",
|
|
||||||
[]interface{}{3},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Gte{"d": 3},
|
|
||||||
"d>=?",
|
|
||||||
[]interface{}{3},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Between{"d", 0, 2},
|
|
||||||
"d BETWEEN ? AND ?",
|
|
||||||
[]interface{}{0, 2},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
IsNull{"d"},
|
|
||||||
"d IS NULL",
|
|
||||||
[]interface{}{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
NotIn("a", 1, 2).And(NotIn("b", "c", "d")),
|
|
||||||
"a NOT IN (?,?) AND b NOT IN (?,?)",
|
|
||||||
[]interface{}{1, 2, "c", "d"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
In("a", 1, 2).Or(In("b", "c", "d")),
|
|
||||||
"a IN (?,?) OR b IN (?,?)",
|
|
||||||
[]interface{}{1, 2, "c", "d"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
In("a", []int{1, 2}).Or(In("b", []string{"c", "d"})),
|
|
||||||
"a IN (?,?) OR b IN (?,?)",
|
|
||||||
[]interface{}{1, 2, "c", "d"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
In("a", Expr("select id from x where name > ?", "b")),
|
|
||||||
"a IN (select id from x where name > ?)",
|
|
||||||
[]interface{}{"b"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
In("a", []MyInt{1, 2}).Or(In("b", []string{"c", "d"})),
|
|
||||||
"a IN (?,?) OR b IN (?,?)",
|
|
||||||
[]interface{}{MyInt(1), MyInt(2), "c", "d"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
In("a", []int{}),
|
|
||||||
"a IN ()",
|
|
||||||
[]interface{}{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
NotIn("a", Expr("select id from x where name > ?", "b")),
|
|
||||||
"a NOT IN (select id from x where name > ?)",
|
|
||||||
[]interface{}{"b"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
NotIn("a", []int{}),
|
|
||||||
"a NOT IN ()",
|
|
||||||
[]interface{}{},
|
|
||||||
},
|
|
||||||
// FIXME: since map will not guarantee the sequence, this may be failed random
|
|
||||||
/*{
|
|
||||||
Or(Eq{"a": 1, "b": 2}, Eq{"c": 3, "d": 4}),
|
|
||||||
"(a=? AND b=?) OR (c=? AND d=?)",
|
|
||||||
[]interface{}{1, 2, 3, 4},
|
|
||||||
},*/
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, k := range cases {
|
|
||||||
sql, args, err := ToSQL(k.cond)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if sql != k.sql {
|
|
||||||
t.Error("want", k.sql, "get", sql)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(sql)
|
|
||||||
|
|
||||||
if !(len(args) == 0 && len(k.args) == 0) {
|
|
||||||
if !reflect.DeepEqual(args, k.args) {
|
|
||||||
t.Error("want", k.args, "get", args)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Println(args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBuilderSelect(t *testing.T) {
|
|
||||||
sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(sql, args)
|
|
||||||
|
|
||||||
sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
|
|
||||||
RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(sql, args)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBuilderInsert(t *testing.T) {
|
|
||||||
sql, args, err := Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(sql, args)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBuilderUpdate(t *testing.T) {
|
|
||||||
sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(sql, args)
|
|
||||||
|
|
||||||
sql, args, err = Update(Eq{"a": 2, "b": Incr(1)}).From("table2").Where(Eq{"a": 1}).ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(sql, args)
|
|
||||||
|
|
||||||
sql, args, err = Update(Eq{"a": 2, "b": Incr(1), "c": Decr(1), "d": Expr("select count(*) from table2")}).From("table2").Where(Eq{"a": 1}).ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(sql, args)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBuilderDelete(t *testing.T) {
|
|
||||||
sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(sql, args)
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (b *Builder) updateWriteTo(w Writer) error {
|
|
||||||
if len(b.tableName) <= 0 {
|
|
||||||
return errors.New("no table indicated")
|
|
||||||
}
|
|
||||||
if len(b.updates) <= 0 {
|
|
||||||
return errors.New("no column to be update")
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, "UPDATE %s SET ", b.tableName); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, s := range b.updates {
|
|
||||||
if err := s.opWriteTo(",", w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if i != len(b.updates)-1 {
|
|
||||||
if _, err := fmt.Fprint(w, ","); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprint(w, " WHERE "); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.cond.WriteTo(w)
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
dependencies:
|
|
||||||
override:
|
|
||||||
# './...' is a relative pattern which means all subdirectories
|
|
||||||
- go get -t -d -v ./...
|
|
||||||
- go build -v
|
|
||||||
- go get -u github.com/golang/lint/golint
|
|
||||||
|
|
||||||
test:
|
|
||||||
override:
|
|
||||||
# './...' is a relative pattern which means all subdirectories
|
|
||||||
- golint ./...
|
|
||||||
- go test -v -race
|
|
|
@ -1,87 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Writer defines the interface
|
|
||||||
type Writer interface {
|
|
||||||
io.Writer
|
|
||||||
Append(...interface{})
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Writer = NewWriter()
|
|
||||||
|
|
||||||
// BytesWriter implments Writer and save SQL in bytes.Buffer
|
|
||||||
type BytesWriter struct {
|
|
||||||
writer *bytes.Buffer
|
|
||||||
buffer []byte
|
|
||||||
args []interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewWriter creates a new string writer
|
|
||||||
func NewWriter() *BytesWriter {
|
|
||||||
w := &BytesWriter{}
|
|
||||||
w.writer = bytes.NewBuffer(w.buffer)
|
|
||||||
return w
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write writes data to Writer
|
|
||||||
func (s *BytesWriter) Write(buf []byte) (int, error) {
|
|
||||||
return s.writer.Write(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append appends args to Writer
|
|
||||||
func (s *BytesWriter) Append(args ...interface{}) {
|
|
||||||
s.args = append(s.args, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cond defines an interface
|
|
||||||
type Cond interface {
|
|
||||||
WriteTo(Writer) error
|
|
||||||
And(...Cond) Cond
|
|
||||||
Or(...Cond) Cond
|
|
||||||
IsValid() bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type condEmpty struct{}
|
|
||||||
|
|
||||||
var _ Cond = condEmpty{}
|
|
||||||
|
|
||||||
// NewCond creates an empty condition
|
|
||||||
func NewCond() Cond {
|
|
||||||
return condEmpty{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condEmpty) WriteTo(w Writer) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condEmpty) And(conds ...Cond) Cond {
|
|
||||||
return And(conds...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condEmpty) Or(conds ...Cond) Cond {
|
|
||||||
return Or(conds...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condEmpty) IsValid() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func condToSQL(cond Cond) (string, []interface{}, error) {
|
|
||||||
if cond == nil || !cond.IsValid() {
|
|
||||||
return "", nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
w := NewWriter()
|
|
||||||
if err := cond.WriteTo(w); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
return w.writer.String(), w.args, nil
|
|
||||||
}
|
|
|
@ -1,59 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type condAnd []Cond
|
|
||||||
|
|
||||||
var _ Cond = condAnd{}
|
|
||||||
|
|
||||||
// And generates AND conditions
|
|
||||||
func And(conds ...Cond) Cond {
|
|
||||||
var result = make(condAnd, 0, len(conds))
|
|
||||||
for _, cond := range conds {
|
|
||||||
if cond == nil || !cond.IsValid() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
result = append(result, cond)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (and condAnd) WriteTo(w Writer) error {
|
|
||||||
for i, cond := range and {
|
|
||||||
_, isOr := cond.(condOr)
|
|
||||||
if isOr {
|
|
||||||
fmt.Fprint(w, "(")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := cond.WriteTo(w)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if isOr {
|
|
||||||
fmt.Fprint(w, ")")
|
|
||||||
}
|
|
||||||
|
|
||||||
if i != len(and)-1 {
|
|
||||||
fmt.Fprint(w, " AND ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (and condAnd) And(conds ...Cond) Cond {
|
|
||||||
return And(and, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (and condAnd) Or(conds ...Cond) Cond {
|
|
||||||
return Or(and, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (and condAnd) IsValid() bool {
|
|
||||||
return len(and) > 0
|
|
||||||
}
|
|
|
@ -1,40 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// Between implmentes between condition
|
|
||||||
type Between struct {
|
|
||||||
Col string
|
|
||||||
LessVal interface{}
|
|
||||||
MoreVal interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Cond = Between{}
|
|
||||||
|
|
||||||
// WriteTo write data to Writer
|
|
||||||
func (between Between) WriteTo(w Writer) error {
|
|
||||||
if _, err := fmt.Fprintf(w, "%s BETWEEN ? AND ?", between.Col); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.Append(between.LessVal, between.MoreVal)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implments And with other conditions
|
|
||||||
func (between Between) And(conds ...Cond) Cond {
|
|
||||||
return And(between, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implments Or with other conditions
|
|
||||||
func (between Between) Or(conds ...Cond) Cond {
|
|
||||||
return Or(between, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if the condition is valid
|
|
||||||
func (between Between) IsValid() bool {
|
|
||||||
return len(between.Col) > 0
|
|
||||||
}
|
|
|
@ -1,154 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// WriteMap writes conditions' SQL to Writer, op could be =, <>, >, <, <=, >= and etc.
|
|
||||||
func WriteMap(w Writer, data map[string]interface{}, op string) error {
|
|
||||||
var args = make([]interface{}, 0, len(data))
|
|
||||||
var i = 0
|
|
||||||
for k, v := range data {
|
|
||||||
switch v.(type) {
|
|
||||||
case expr:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := v.(expr).WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case *Builder:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := v.(*Builder).WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s%s?", k, op); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
args = append(args, v)
|
|
||||||
}
|
|
||||||
if i != len(data)-1 {
|
|
||||||
if _, err := fmt.Fprint(w, " AND "); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i = i + 1
|
|
||||||
}
|
|
||||||
w.Append(args...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lt defines < condition
|
|
||||||
type Lt map[string]interface{}
|
|
||||||
|
|
||||||
var _ Cond = Lt{}
|
|
||||||
|
|
||||||
// WriteTo write SQL to Writer
|
|
||||||
func (lt Lt) WriteTo(w Writer) error {
|
|
||||||
return WriteMap(w, lt, "<")
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (lt Lt) And(conds ...Cond) Cond {
|
|
||||||
return condAnd{lt, And(conds...)}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (lt Lt) Or(conds ...Cond) Cond {
|
|
||||||
return condOr{lt, Or(conds...)}
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this Eq is valid
|
|
||||||
func (lt Lt) IsValid() bool {
|
|
||||||
return len(lt) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lte defines <= condition
|
|
||||||
type Lte map[string]interface{}
|
|
||||||
|
|
||||||
var _ Cond = Lte{}
|
|
||||||
|
|
||||||
// WriteTo write SQL to Writer
|
|
||||||
func (lte Lte) WriteTo(w Writer) error {
|
|
||||||
return WriteMap(w, lte, "<=")
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (lte Lte) And(conds ...Cond) Cond {
|
|
||||||
return And(lte, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (lte Lte) Or(conds ...Cond) Cond {
|
|
||||||
return Or(lte, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this Eq is valid
|
|
||||||
func (lte Lte) IsValid() bool {
|
|
||||||
return len(lte) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gt defines > condition
|
|
||||||
type Gt map[string]interface{}
|
|
||||||
|
|
||||||
var _ Cond = Gt{}
|
|
||||||
|
|
||||||
// WriteTo write SQL to Writer
|
|
||||||
func (gt Gt) WriteTo(w Writer) error {
|
|
||||||
return WriteMap(w, gt, ">")
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (gt Gt) And(conds ...Cond) Cond {
|
|
||||||
return And(gt, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (gt Gt) Or(conds ...Cond) Cond {
|
|
||||||
return Or(gt, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this Eq is valid
|
|
||||||
func (gt Gt) IsValid() bool {
|
|
||||||
return len(gt) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gte defines >= condition
|
|
||||||
type Gte map[string]interface{}
|
|
||||||
|
|
||||||
var _ Cond = Gte{}
|
|
||||||
|
|
||||||
// WriteTo write SQL to Writer
|
|
||||||
func (gte Gte) WriteTo(w Writer) error {
|
|
||||||
return WriteMap(w, gte, ">=")
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (gte Gte) And(conds ...Cond) Cond {
|
|
||||||
return And(gte, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (gte Gte) Or(conds ...Cond) Cond {
|
|
||||||
return Or(gte, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this Eq is valid
|
|
||||||
func (gte Gte) IsValid() bool {
|
|
||||||
return len(gte) > 0
|
|
||||||
}
|
|
|
@ -1,96 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// Incr implements a type used by Eq
|
|
||||||
type Incr int
|
|
||||||
|
|
||||||
// Decr implements a type used by Eq
|
|
||||||
type Decr int
|
|
||||||
|
|
||||||
// Eq defines equals conditions
|
|
||||||
type Eq map[string]interface{}
|
|
||||||
|
|
||||||
var _ Cond = Eq{}
|
|
||||||
|
|
||||||
func (eq Eq) opWriteTo(op string, w Writer) error {
|
|
||||||
var i = 0
|
|
||||||
for k, v := range eq {
|
|
||||||
switch v.(type) {
|
|
||||||
case []int, []int64, []string, []int32, []int16, []int8, []uint, []uint64, []uint32, []uint16, []interface{}:
|
|
||||||
if err := In(k, v).WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case expr:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s=(", k); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := v.(expr).WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case *Builder:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s=(", k); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := v.(*Builder).WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case Incr:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s=%s+?", k, k); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.Append(int(v.(Incr)))
|
|
||||||
case Decr:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s=%s-?", k, k); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.Append(int(v.(Decr)))
|
|
||||||
default:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s=?", k); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.Append(v)
|
|
||||||
}
|
|
||||||
if i != len(eq)-1 {
|
|
||||||
if _, err := fmt.Fprint(w, op); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i = i + 1
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteTo writes SQL to Writer
|
|
||||||
func (eq Eq) WriteTo(w Writer) error {
|
|
||||||
return eq.opWriteTo(" AND ", w)
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (eq Eq) And(conds ...Cond) Cond {
|
|
||||||
return And(eq, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (eq Eq) Or(conds ...Cond) Cond {
|
|
||||||
return Or(eq, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this Eq is valid
|
|
||||||
func (eq Eq) IsValid() bool {
|
|
||||||
return len(eq) > 0
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type expr struct {
|
|
||||||
sql string
|
|
||||||
args []interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Cond = expr{}
|
|
||||||
|
|
||||||
// Expr generate customerize SQL
|
|
||||||
func Expr(sql string, args ...interface{}) Cond {
|
|
||||||
return expr{sql, args}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (expr expr) WriteTo(w Writer) error {
|
|
||||||
if _, err := fmt.Fprint(w, expr.sql); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.Append(expr.args...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (expr expr) And(conds ...Cond) Cond {
|
|
||||||
return And(expr, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (expr expr) Or(conds ...Cond) Cond {
|
|
||||||
return Or(expr, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (expr expr) IsValid() bool {
|
|
||||||
return len(expr.sql) > 0
|
|
||||||
}
|
|
|
@ -1,239 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type condIn struct {
|
|
||||||
col string
|
|
||||||
vals []interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Cond = condIn{}
|
|
||||||
|
|
||||||
// In generates IN condition
|
|
||||||
func In(col string, values ...interface{}) Cond {
|
|
||||||
return condIn{col, values}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condIn condIn) handleBlank(w Writer) error {
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN ()", condIn.col); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condIn condIn) WriteTo(w Writer) error {
|
|
||||||
if len(condIn.vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch condIn.vals[0].(type) {
|
|
||||||
case []int8:
|
|
||||||
vals := condIn.vals[0].([]int8)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []int16:
|
|
||||||
vals := condIn.vals[0].([]int16)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []int:
|
|
||||||
vals := condIn.vals[0].([]int)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []int32:
|
|
||||||
vals := condIn.vals[0].([]int32)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []int64:
|
|
||||||
vals := condIn.vals[0].([]int64)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint8:
|
|
||||||
vals := condIn.vals[0].([]uint8)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint16:
|
|
||||||
vals := condIn.vals[0].([]uint16)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint:
|
|
||||||
vals := condIn.vals[0].([]uint)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint32:
|
|
||||||
vals := condIn.vals[0].([]uint32)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint64:
|
|
||||||
vals := condIn.vals[0].([]uint64)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []string:
|
|
||||||
vals := condIn.vals[0].([]string)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []interface{}:
|
|
||||||
vals := condIn.vals[0].([]interface{})
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.Append(vals...)
|
|
||||||
case expr:
|
|
||||||
val := condIn.vals[0].(expr)
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (", condIn.col); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := val.WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case *Builder:
|
|
||||||
bd := condIn.vals[0].(*Builder)
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (", condIn.col); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := bd.WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
v := reflect.ValueOf(condIn.vals[0])
|
|
||||||
if v.Kind() == reflect.Slice {
|
|
||||||
l := v.Len()
|
|
||||||
if l == 0 {
|
|
||||||
return condIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
questionMark := strings.Repeat("?,", l)
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < l; i++ {
|
|
||||||
w.Append(v.Index(i).Interface())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
questionMark := strings.Repeat("?,", len(condIn.vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.Append(condIn.vals...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condIn condIn) And(conds ...Cond) Cond {
|
|
||||||
return And(condIn, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condIn condIn) Or(conds ...Cond) Cond {
|
|
||||||
return Or(condIn, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condIn condIn) IsValid() bool {
|
|
||||||
return len(condIn.col) > 0 && len(condIn.vals) > 0
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// Like defines like condition
|
|
||||||
type Like [2]string
|
|
||||||
|
|
||||||
var _ Cond = Like{"", ""}
|
|
||||||
|
|
||||||
// WriteTo write SQL to Writer
|
|
||||||
func (like Like) WriteTo(w Writer) error {
|
|
||||||
if _, err := fmt.Fprintf(w, "%s LIKE ?", like[0]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// FIXME: if use other regular express, this will be failed. but for compitable, keep this
|
|
||||||
if like[1][0] == '%' || like[1][len(like[1])-1] == '%' {
|
|
||||||
w.Append(like[1])
|
|
||||||
} else {
|
|
||||||
w.Append("%" + like[1] + "%")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (like Like) And(conds ...Cond) Cond {
|
|
||||||
return And(like, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (like Like) Or(conds ...Cond) Cond {
|
|
||||||
return Or(like, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this condition is valid
|
|
||||||
func (like Like) IsValid() bool {
|
|
||||||
return len(like[0]) > 0 && len(like[1]) > 0
|
|
||||||
}
|
|
|
@ -1,78 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// Neq defines not equal conditions
|
|
||||||
type Neq map[string]interface{}
|
|
||||||
|
|
||||||
var _ Cond = Neq{}
|
|
||||||
|
|
||||||
// WriteTo writes SQL to Writer
|
|
||||||
func (neq Neq) WriteTo(w Writer) error {
|
|
||||||
var args = make([]interface{}, 0, len(neq))
|
|
||||||
var i = 0
|
|
||||||
for k, v := range neq {
|
|
||||||
switch v.(type) {
|
|
||||||
case []int, []int64, []string, []int32, []int16, []int8:
|
|
||||||
if err := NotIn(k, v).WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case expr:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := v.(expr).WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case *Builder:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := v.(*Builder).WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
if _, err := fmt.Fprintf(w, "%s<>?", k); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
args = append(args, v)
|
|
||||||
}
|
|
||||||
if i != len(neq)-1 {
|
|
||||||
if _, err := fmt.Fprint(w, " AND "); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i = i + 1
|
|
||||||
}
|
|
||||||
w.Append(args...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (neq Neq) And(conds ...Cond) Cond {
|
|
||||||
return And(neq, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (neq Neq) Or(conds ...Cond) Cond {
|
|
||||||
return Or(neq, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this condition is valid
|
|
||||||
func (neq Neq) IsValid() bool {
|
|
||||||
return len(neq) > 0
|
|
||||||
}
|
|
|
@ -1,53 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// Not defines NOT condition
|
|
||||||
type Not [1]Cond
|
|
||||||
|
|
||||||
var _ Cond = Not{}
|
|
||||||
|
|
||||||
// WriteTo writes SQL to Writer
|
|
||||||
func (not Not) WriteTo(w Writer) error {
|
|
||||||
if _, err := fmt.Fprint(w, "NOT "); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
switch not[0].(type) {
|
|
||||||
case condAnd, condOr:
|
|
||||||
if _, err := fmt.Fprint(w, "("); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := not[0].WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch not[0].(type) {
|
|
||||||
case condAnd, condOr:
|
|
||||||
if _, err := fmt.Fprint(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (not Not) And(conds ...Cond) Cond {
|
|
||||||
return And(not, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (not Not) Or(conds ...Cond) Cond {
|
|
||||||
return Or(not, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this condition is valid
|
|
||||||
func (not Not) IsValid() bool {
|
|
||||||
return not[0] != nil && not[0].IsValid()
|
|
||||||
}
|
|
|
@ -1,236 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type condNotIn condIn
|
|
||||||
|
|
||||||
var _ Cond = condNotIn{}
|
|
||||||
|
|
||||||
// NotIn generate NOT IN condition
|
|
||||||
func NotIn(col string, values ...interface{}) Cond {
|
|
||||||
return condNotIn{col, values}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condNotIn condNotIn) handleBlank(w Writer) error {
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN ()", condNotIn.col); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|
||||||
if len(condNotIn.vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch condNotIn.vals[0].(type) {
|
|
||||||
case []int8:
|
|
||||||
vals := condNotIn.vals[0].([]int8)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []int16:
|
|
||||||
vals := condNotIn.vals[0].([]int16)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []int:
|
|
||||||
vals := condNotIn.vals[0].([]int)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []int32:
|
|
||||||
vals := condNotIn.vals[0].([]int32)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []int64:
|
|
||||||
vals := condNotIn.vals[0].([]int64)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint8:
|
|
||||||
vals := condNotIn.vals[0].([]uint8)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint16:
|
|
||||||
vals := condNotIn.vals[0].([]uint16)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint:
|
|
||||||
vals := condNotIn.vals[0].([]uint)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint32:
|
|
||||||
vals := condNotIn.vals[0].([]uint32)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []uint64:
|
|
||||||
vals := condNotIn.vals[0].([]uint64)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []string:
|
|
||||||
vals := condNotIn.vals[0].([]string)
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, val := range vals {
|
|
||||||
w.Append(val)
|
|
||||||
}
|
|
||||||
case []interface{}:
|
|
||||||
vals := condNotIn.vals[0].([]interface{})
|
|
||||||
if len(vals) <= 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
questionMark := strings.Repeat("?,", len(vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.Append(vals...)
|
|
||||||
case expr:
|
|
||||||
val := condNotIn.vals[0].(expr)
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := val.WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case *Builder:
|
|
||||||
val := condNotIn.vals[0].(*Builder)
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := val.WriteTo(w); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
v := reflect.ValueOf(condNotIn.vals[0])
|
|
||||||
if v.Kind() == reflect.Slice {
|
|
||||||
l := v.Len()
|
|
||||||
if l == 0 {
|
|
||||||
return condNotIn.handleBlank(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
questionMark := strings.Repeat("?,", l)
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < l; i++ {
|
|
||||||
w.Append(v.Index(i).Interface())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
questionMark := strings.Repeat("?,", len(condNotIn.vals))
|
|
||||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.Append(condNotIn.vals...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condNotIn condNotIn) And(conds ...Cond) Cond {
|
|
||||||
return And(condNotIn, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condNotIn condNotIn) Or(conds ...Cond) Cond {
|
|
||||||
return Or(condNotIn, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (condNotIn condNotIn) IsValid() bool {
|
|
||||||
return len(condNotIn.col) > 0 && len(condNotIn.vals) > 0
|
|
||||||
}
|
|
|
@ -1,59 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// IsNull defines IS NULL condition
|
|
||||||
type IsNull [1]string
|
|
||||||
|
|
||||||
var _ Cond = IsNull{""}
|
|
||||||
|
|
||||||
// WriteTo write SQL to Writer
|
|
||||||
func (isNull IsNull) WriteTo(w Writer) error {
|
|
||||||
_, err := fmt.Fprintf(w, "%s IS NULL", isNull[0])
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (isNull IsNull) And(conds ...Cond) Cond {
|
|
||||||
return And(isNull, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (isNull IsNull) Or(conds ...Cond) Cond {
|
|
||||||
return Or(isNull, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this condition is valid
|
|
||||||
func (isNull IsNull) IsValid() bool {
|
|
||||||
return len(isNull[0]) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotNull defines NOT NULL condition
|
|
||||||
type NotNull [1]string
|
|
||||||
|
|
||||||
var _ Cond = NotNull{""}
|
|
||||||
|
|
||||||
// WriteTo write SQL to Writer
|
|
||||||
func (notNull NotNull) WriteTo(w Writer) error {
|
|
||||||
_, err := fmt.Fprintf(w, "%s IS NOT NULL", notNull[0])
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// And implements And with other conditions
|
|
||||||
func (notNull NotNull) And(conds ...Cond) Cond {
|
|
||||||
return And(notNull, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or implements Or with other conditions
|
|
||||||
func (notNull NotNull) Or(conds ...Cond) Cond {
|
|
||||||
return Or(notNull, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid tests if this condition is valid
|
|
||||||
func (notNull NotNull) IsValid() bool {
|
|
||||||
return len(notNull[0]) > 0
|
|
||||||
}
|
|
|
@ -1,67 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type condOr []Cond
|
|
||||||
|
|
||||||
var _ Cond = condOr{}
|
|
||||||
|
|
||||||
// Or sets OR conditions
|
|
||||||
func Or(conds ...Cond) Cond {
|
|
||||||
var result = make(condOr, 0, len(conds))
|
|
||||||
for _, cond := range conds {
|
|
||||||
if cond == nil || !cond.IsValid() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
result = append(result, cond)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteTo implments Cond
|
|
||||||
func (o condOr) WriteTo(w Writer) error {
|
|
||||||
for i, cond := range o {
|
|
||||||
var needQuote bool
|
|
||||||
switch cond.(type) {
|
|
||||||
case condAnd:
|
|
||||||
needQuote = true
|
|
||||||
case Eq:
|
|
||||||
needQuote = (len(cond.(Eq)) > 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if needQuote {
|
|
||||||
fmt.Fprint(w, "(")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := cond.WriteTo(w)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if needQuote {
|
|
||||||
fmt.Fprint(w, ")")
|
|
||||||
}
|
|
||||||
|
|
||||||
if i != len(o)-1 {
|
|
||||||
fmt.Fprint(w, " OR ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o condOr) And(conds ...Cond) Cond {
|
|
||||||
return And(o, And(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o condOr) Or(conds ...Cond) Cond {
|
|
||||||
return Or(o, Or(conds...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o condOr) IsValid() bool {
|
|
||||||
return len(o) > 0
|
|
||||||
}
|
|
|
@ -1,120 +0,0 @@
|
||||||
// Copyright 2016 The XORM Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
Package builder is a simple and powerful sql builder for Go.
|
|
||||||
|
|
||||||
Make sure you have installed Go 1.1+ and then:
|
|
||||||
|
|
||||||
go get github.com/go-xorm/builder
|
|
||||||
|
|
||||||
WARNNING: Currently, only query conditions are supported. Below is the supported conditions.
|
|
||||||
|
|
||||||
1. Eq is a redefine of a map, you can give one or more conditions to Eq
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Eq{"a":1})
|
|
||||||
// a=? [1]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
|
|
||||||
// b=? AND c=? ["c", 0]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
|
|
||||||
// b=? AND c=? ["c", 0]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
|
|
||||||
// b=? OR b=? ["c", "d"]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
|
|
||||||
// b IN (?,?) ["c", "d"]
|
|
||||||
sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
|
|
||||||
// b=? AND c IN (?,?) [1, 2, 3]
|
|
||||||
|
|
||||||
2. Neq is the same to Eq
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Neq{"a":1})
|
|
||||||
// a<>? [1]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
|
|
||||||
// b<>? AND c<>? ["c", 0]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
|
|
||||||
// b<>? AND c<>? ["c", 0]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
|
|
||||||
// b<>? OR b<>? ["c", "d"]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
|
|
||||||
// b NOT IN (?,?) ["c", "d"]
|
|
||||||
sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
|
|
||||||
// b<>? AND c NOT IN (?,?) [1, 2, 3]
|
|
||||||
|
|
||||||
3. Gt, Gte, Lt, Lte
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
|
|
||||||
// a>? AND b>=? [1, 2]
|
|
||||||
sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
|
|
||||||
// a<? OR b<=? [1, 2]
|
|
||||||
|
|
||||||
4. Like
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Like{"a", "c"})
|
|
||||||
// a LIKE ? [%c%]
|
|
||||||
|
|
||||||
5. Expr you can customerize your sql with Expr
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Expr("a = ? ", 1))
|
|
||||||
// a = ? [1]
|
|
||||||
sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
|
|
||||||
// a=(select id from table where c = ?) [1]
|
|
||||||
|
|
||||||
6. In and NotIn
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(In("a", 1, 2, 3))
|
|
||||||
// a IN (?,?,?) [1,2,3]
|
|
||||||
sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
|
|
||||||
// a IN (?,?,?) [1,2,3]
|
|
||||||
sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
|
|
||||||
// a IN (select id from b where c = ?) [1]
|
|
||||||
|
|
||||||
7. IsNull and NotNull
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(IsNull{"a"})
|
|
||||||
// a IS NULL []
|
|
||||||
sql, args, _ := ToSQL(NotNull{"b"})
|
|
||||||
// b IS NOT NULL []
|
|
||||||
|
|
||||||
8. And(conds ...Cond), And can connect one or more condtions via AND
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
|
|
||||||
// a=? AND b LIKE ? AND d<>? [1, %c%, 2]
|
|
||||||
|
|
||||||
9. Or(conds ...Cond), Or can connect one or more conditions via Or
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
|
|
||||||
// a=? OR b LIKE ? OR d<>? [1, %c%, 2]
|
|
||||||
sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
|
|
||||||
// a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
|
|
||||||
|
|
||||||
10. Between
|
|
||||||
|
|
||||||
import . "github.com/go-xorm/builder"
|
|
||||||
|
|
||||||
sql, args, _ := ToSQL(Between("a", 1, 2))
|
|
||||||
// a BETWEEN 1 AND 2
|
|
||||||
|
|
||||||
11. define yourself conditions
|
|
||||||
Since Cond is a interface, you can define yourself conditions and compare with them
|
|
||||||
*/
|
|
||||||
package builder
|
|
|
@ -1,16 +0,0 @@
|
||||||
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package builder
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var (
|
|
||||||
// ErrNotSupportType not supported SQL type error
|
|
||||||
ErrNotSupportType = errors.New("not supported SQL type")
|
|
||||||
// ErrNoNotInConditions no NOT IN params error
|
|
||||||
ErrNoNotInConditions = errors.New("No NOT IN conditions")
|
|
||||||
// ErrNoInConditions no IN params error
|
|
||||||
ErrNoInConditions = errors.New("No IN conditions")
|
|
||||||
)
|
|
|
@ -1,27 +0,0 @@
|
||||||
Copyright (c) 2013 - 2015 Lunny Xiao <xiaolunwen@gmail.com>
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
* Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
* Neither the name of the {organization} nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
@ -1,116 +0,0 @@
|
||||||
Core is a lightweight wrapper of sql.DB.
|
|
||||||
|
|
||||||
[](https://circleci.com/gh/go-xorm/core/tree/master)
|
|
||||||
|
|
||||||
# Open
|
|
||||||
```Go
|
|
||||||
db, _ := core.Open(db, connstr)
|
|
||||||
```
|
|
||||||
|
|
||||||
# SetMapper
|
|
||||||
```Go
|
|
||||||
db.SetMapper(SameMapper())
|
|
||||||
```
|
|
||||||
|
|
||||||
## Scan usage
|
|
||||||
|
|
||||||
### Scan
|
|
||||||
```Go
|
|
||||||
rows, _ := db.Query()
|
|
||||||
for rows.Next() {
|
|
||||||
rows.Scan()
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### ScanMap
|
|
||||||
```Go
|
|
||||||
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.
|
|
||||||
|
|
||||||
```Go
|
|
||||||
rows, _ := db.Query()
|
|
||||||
cols, _ := rows.Columns()
|
|
||||||
for rows.Next() {
|
|
||||||
var s = make([]string, len(cols))
|
|
||||||
rows.ScanSlice(&s)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```Go
|
|
||||||
rows, _ := db.Query()
|
|
||||||
cols, _ := rows.Columns()
|
|
||||||
for rows.Next() {
|
|
||||||
var s = make([]*string, len(cols))
|
|
||||||
rows.ScanSlice(&s)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### ScanStruct
|
|
||||||
```Go
|
|
||||||
rows, _ := db.Query()
|
|
||||||
for rows.Next() {
|
|
||||||
rows.ScanStructByName()
|
|
||||||
rows.ScanStructByIndex()
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Query usage
|
|
||||||
```Go
|
|
||||||
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
|
|
||||||
```Go
|
|
||||||
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
|
|
||||||
```Go
|
|
||||||
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)
|
|
||||||
```
|
|
|
@ -1 +0,0 @@
|
||||||
go test -v -bench=. -run=XXX
|
|
|
@ -1,87 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
"bytes"
|
|
||||||
"encoding/gob"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// default cache expired time
|
|
||||||
CacheExpired = 60 * time.Minute
|
|
||||||
// not use now
|
|
||||||
CacheMaxMemory = 256
|
|
||||||
// evey ten minutes to clear all expired nodes
|
|
||||||
CacheGcInterval = 10 * time.Minute
|
|
||||||
// each time when gc to removed max nodes
|
|
||||||
CacheGcMaxRemoved = 20
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrCacheMiss = errors.New("xorm/cache: key not found.")
|
|
||||||
ErrNotStored = errors.New("xorm/cache: not stored.")
|
|
||||||
)
|
|
||||||
|
|
||||||
// CacheStore is a interface to store cache
|
|
||||||
type CacheStore interface {
|
|
||||||
// key is primary key or composite primary key
|
|
||||||
// value is struct's pointer
|
|
||||||
// key format : <tablename>-p-<pk1>-<pk2>...
|
|
||||||
Put(key string, value interface{}) error
|
|
||||||
Get(key string) (interface{}, error)
|
|
||||||
Del(key string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cacher is an interface to provide cache
|
|
||||||
// id format : u-<pk1>-<pk2>...
|
|
||||||
type Cacher interface {
|
|
||||||
GetIds(tableName, sql string) interface{}
|
|
||||||
GetBean(tableName string, id string) interface{}
|
|
||||||
PutIds(tableName, sql string, ids interface{})
|
|
||||||
PutBean(tableName string, id string, obj interface{})
|
|
||||||
DelIds(tableName, sql string)
|
|
||||||
DelBean(tableName string, id string)
|
|
||||||
ClearIds(tableName string)
|
|
||||||
ClearBeans(tableName string)
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeIds(ids []PK) (string, error) {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
enc := gob.NewEncoder(buf)
|
|
||||||
err := enc.Encode(ids)
|
|
||||||
|
|
||||||
return buf.String(), err
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func decodeIds(s string) ([]PK, error) {
|
|
||||||
pks := make([]PK, 0)
|
|
||||||
|
|
||||||
dec := gob.NewDecoder(bytes.NewBufferString(s))
|
|
||||||
err := dec.Decode(&pks)
|
|
||||||
|
|
||||||
return pks, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]PK, error) {
|
|
||||||
bytes := m.GetIds(tableName, GenSqlKey(sql, args))
|
|
||||||
if bytes == nil {
|
|
||||||
return nil, errors.New("Not Exist")
|
|
||||||
}
|
|
||||||
return decodeIds(bytes.(string))
|
|
||||||
}
|
|
||||||
|
|
||||||
func PutCacheSql(m Cacher, ids []PK, tableName, sql string, args interface{}) error {
|
|
||||||
bytes, err := encodeIds(ids)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m.PutIds(tableName, GenSqlKey(sql, args), bytes)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenSqlKey(sql string, args interface{}) string {
|
|
||||||
return fmt.Sprintf("%v-%v", sql, args)
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
dependencies:
|
|
||||||
override:
|
|
||||||
# './...' is a relative pattern which means all subdirectories
|
|
||||||
- go get -t -d -v ./...
|
|
||||||
- go build -v
|
|
||||||
|
|
||||||
database:
|
|
||||||
override:
|
|
||||||
- mysql -u root -e "CREATE DATABASE core_test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"
|
|
||||||
|
|
||||||
test:
|
|
||||||
override:
|
|
||||||
# './...' is a relative pattern which means all subdirectories
|
|
||||||
- go test -v -race
|
|
|
@ -1,167 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
TWOSIDES = iota + 1
|
|
||||||
ONLYTODB
|
|
||||||
ONLYFROMDB
|
|
||||||
)
|
|
||||||
|
|
||||||
type AssociateType int
|
|
||||||
|
|
||||||
const (
|
|
||||||
AssociateNone AssociateType = iota
|
|
||||||
AssociateBelongsTo
|
|
||||||
)
|
|
||||||
|
|
||||||
// database column
|
|
||||||
type Column struct {
|
|
||||||
Name string
|
|
||||||
TableName string
|
|
||||||
FieldName string
|
|
||||||
SQLType SQLType
|
|
||||||
Length int
|
|
||||||
Length2 int
|
|
||||||
Nullable bool
|
|
||||||
Default string
|
|
||||||
Indexes map[string]int
|
|
||||||
IsPrimaryKey bool
|
|
||||||
IsAutoIncrement bool
|
|
||||||
MapType int
|
|
||||||
IsCreated bool
|
|
||||||
IsUpdated bool
|
|
||||||
IsDeleted bool
|
|
||||||
IsCascade bool
|
|
||||||
IsVersion bool
|
|
||||||
DefaultIsEmpty bool
|
|
||||||
EnumOptions map[string]int
|
|
||||||
SetOptions map[string]int
|
|
||||||
DisableTimeZone bool
|
|
||||||
TimeZone *time.Location // column specified time zone
|
|
||||||
AssociateType
|
|
||||||
AssociateTable *Table
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
|
|
||||||
return &Column{
|
|
||||||
Name: name,
|
|
||||||
TableName: "",
|
|
||||||
FieldName: fieldName,
|
|
||||||
SQLType: sqlType,
|
|
||||||
Length: len1,
|
|
||||||
Length2: len2,
|
|
||||||
Nullable: nullable,
|
|
||||||
Default: "",
|
|
||||||
Indexes: make(map[string]int),
|
|
||||||
IsPrimaryKey: false,
|
|
||||||
IsAutoIncrement: false,
|
|
||||||
MapType: TWOSIDES,
|
|
||||||
IsCreated: false,
|
|
||||||
IsUpdated: false,
|
|
||||||
IsDeleted: false,
|
|
||||||
IsCascade: false,
|
|
||||||
IsVersion: false,
|
|
||||||
DefaultIsEmpty: false,
|
|
||||||
EnumOptions: make(map[string]int),
|
|
||||||
AssociateType: AssociateNone,
|
|
||||||
AssociateTable: nil,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// generate column description string according dialect
|
|
||||||
func (col *Column) String(d Dialect) string {
|
|
||||||
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
|
|
||||||
|
|
||||||
sql += d.SqlType(col) + " "
|
|
||||||
|
|
||||||
if col.IsPrimaryKey {
|
|
||||||
sql += "PRIMARY KEY "
|
|
||||||
if col.IsAutoIncrement {
|
|
||||||
sql += d.AutoIncrStr() + " "
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.ShowCreateNull() {
|
|
||||||
if col.Nullable {
|
|
||||||
sql += "NULL "
|
|
||||||
} else {
|
|
||||||
sql += "NOT NULL "
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if col.Default != "" {
|
|
||||||
sql += "DEFAULT " + col.Default + " "
|
|
||||||
}
|
|
||||||
|
|
||||||
return sql
|
|
||||||
}
|
|
||||||
|
|
||||||
func (col *Column) StringNoPk(d Dialect) string {
|
|
||||||
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
|
|
||||||
|
|
||||||
sql += d.SqlType(col) + " "
|
|
||||||
|
|
||||||
if d.ShowCreateNull() {
|
|
||||||
if col.Nullable {
|
|
||||||
sql += "NULL "
|
|
||||||
} else {
|
|
||||||
sql += "NOT NULL "
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if col.Default != "" {
|
|
||||||
sql += "DEFAULT " + col.Default + " "
|
|
||||||
}
|
|
||||||
|
|
||||||
return sql
|
|
||||||
}
|
|
||||||
|
|
||||||
// return col's filed of struct's value
|
|
||||||
func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
|
|
||||||
dataStruct := reflect.Indirect(reflect.ValueOf(bean))
|
|
||||||
return col.ValueOfV(&dataStruct)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
|
|
||||||
var fieldValue reflect.Value
|
|
||||||
fieldPath := strings.Split(col.FieldName, ".")
|
|
||||||
|
|
||||||
if dataStruct.Type().Kind() == reflect.Map {
|
|
||||||
keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
|
|
||||||
fieldValue = dataStruct.MapIndex(keyValue)
|
|
||||||
return &fieldValue, nil
|
|
||||||
} else if dataStruct.Type().Kind() == reflect.Interface {
|
|
||||||
structValue := reflect.ValueOf(dataStruct.Interface())
|
|
||||||
dataStruct = &structValue
|
|
||||||
}
|
|
||||||
|
|
||||||
level := len(fieldPath)
|
|
||||||
fieldValue = dataStruct.FieldByName(fieldPath[0])
|
|
||||||
for i := 0; i < level-1; i++ {
|
|
||||||
if !fieldValue.IsValid() {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if fieldValue.Kind() == reflect.Struct {
|
|
||||||
fieldValue = fieldValue.FieldByName(fieldPath[i+1])
|
|
||||||
} else if fieldValue.Kind() == reflect.Ptr {
|
|
||||||
if fieldValue.IsNil() {
|
|
||||||
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
|
|
||||||
}
|
|
||||||
fieldValue = fieldValue.Elem().FieldByName(fieldPath[i+1])
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !fieldValue.IsValid() {
|
|
||||||
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &fieldValue, nil
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
// Conversion is an interface. A type implements Conversion will according
|
|
||||||
// the custom method to fill into database and retrieve from database.
|
|
||||||
type Conversion interface {
|
|
||||||
FromDB([]byte) error
|
|
||||||
ToDB() ([]byte, error)
|
|
||||||
}
|
|
|
@ -1,368 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"database/sql/driver"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
func MapToSlice(query string, mp interface{}) (string, []interface{}, error) {
|
|
||||||
vv := reflect.ValueOf(mp)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
|
||||||
return "", []interface{}{}, ErrNoMapPointer
|
|
||||||
}
|
|
||||||
|
|
||||||
args := make([]interface{}, 0, len(vv.Elem().MapKeys()))
|
|
||||||
var err error
|
|
||||||
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
|
||||||
v := vv.Elem().MapIndex(reflect.ValueOf(src[1:]))
|
|
||||||
if !v.IsValid() {
|
|
||||||
err = fmt.Errorf("map key %s is missing", src[1:])
|
|
||||||
} else {
|
|
||||||
args = append(args, v.Interface())
|
|
||||||
}
|
|
||||||
return "?"
|
|
||||||
})
|
|
||||||
|
|
||||||
return query, args, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func StructToSlice(query string, st interface{}) (string, []interface{}, error) {
|
|
||||||
vv := reflect.ValueOf(st)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
|
||||||
return "", []interface{}{}, ErrNoStructPointer
|
|
||||||
}
|
|
||||||
|
|
||||||
args := make([]interface{}, 0)
|
|
||||||
var err error
|
|
||||||
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
|
||||||
fv := vv.Elem().FieldByName(src[1:]).Interface()
|
|
||||||
if v, ok := fv.(driver.Valuer); ok {
|
|
||||||
var value driver.Value
|
|
||||||
value, err = v.Value()
|
|
||||||
if err != nil {
|
|
||||||
return "?"
|
|
||||||
}
|
|
||||||
args = append(args, value)
|
|
||||||
} else {
|
|
||||||
args = append(args, fv)
|
|
||||||
}
|
|
||||||
return "?"
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return "", []interface{}{}, err
|
|
||||||
}
|
|
||||||
return query, args, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type DB struct {
|
|
||||||
*sql.DB
|
|
||||||
Mapper IMapper
|
|
||||||
}
|
|
||||||
|
|
||||||
func Open(driverName, dataSourceName string) (*DB, error) {
|
|
||||||
db, err := sql.Open(driverName, dataSourceName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &DB{db, NewCacheMapper(&SnakeMapper{})}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func FromDB(db *sql.DB) *DB {
|
|
||||||
return &DB{db, NewCacheMapper(&SnakeMapper{})}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
|
|
||||||
rows, err := db.DB.Query(query, args...)
|
|
||||||
if err != nil {
|
|
||||||
if rows != nil {
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Rows{rows, db.Mapper}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) QueryMap(query string, mp interface{}) (*Rows, error) {
|
|
||||||
query, args, err := MapToSlice(query, mp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return db.Query(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) QueryStruct(query string, st interface{}) (*Rows, error) {
|
|
||||||
query, args, err := StructToSlice(query, st)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return db.Query(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) QueryRow(query string, args ...interface{}) *Row {
|
|
||||||
rows, err := db.Query(query, args...)
|
|
||||||
if err != nil {
|
|
||||||
return &Row{nil, err}
|
|
||||||
}
|
|
||||||
return &Row{rows, nil}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) QueryRowMap(query string, mp interface{}) *Row {
|
|
||||||
query, args, err := MapToSlice(query, mp)
|
|
||||||
if err != nil {
|
|
||||||
return &Row{nil, err}
|
|
||||||
}
|
|
||||||
return db.QueryRow(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) QueryRowStruct(query string, st interface{}) *Row {
|
|
||||||
query, args, err := StructToSlice(query, st)
|
|
||||||
if err != nil {
|
|
||||||
return &Row{nil, err}
|
|
||||||
}
|
|
||||||
return db.QueryRow(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Stmt struct {
|
|
||||||
*sql.Stmt
|
|
||||||
Mapper IMapper
|
|
||||||
names map[string]int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) Prepare(query string) (*Stmt, error) {
|
|
||||||
names := make(map[string]int)
|
|
||||||
var i int
|
|
||||||
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
|
||||||
names[src[1:]] = i
|
|
||||||
i += 1
|
|
||||||
return "?"
|
|
||||||
})
|
|
||||||
|
|
||||||
stmt, err := db.DB.Prepare(query)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Stmt{stmt, db.Mapper, names}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stmt) ExecMap(mp interface{}) (sql.Result, error) {
|
|
||||||
vv := reflect.ValueOf(mp)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
|
||||||
return nil, errors.New("mp should be a map's pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
args := make([]interface{}, len(s.names))
|
|
||||||
for k, i := range s.names {
|
|
||||||
args[i] = vv.Elem().MapIndex(reflect.ValueOf(k)).Interface()
|
|
||||||
}
|
|
||||||
return s.Stmt.Exec(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stmt) ExecStruct(st interface{}) (sql.Result, error) {
|
|
||||||
vv := reflect.ValueOf(st)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
|
||||||
return nil, errors.New("mp should be a map's pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
args := make([]interface{}, len(s.names))
|
|
||||||
for k, i := range s.names {
|
|
||||||
args[i] = vv.Elem().FieldByName(k).Interface()
|
|
||||||
}
|
|
||||||
return s.Stmt.Exec(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
|
|
||||||
rows, err := s.Stmt.Query(args...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Rows{rows, s.Mapper}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stmt) QueryMap(mp interface{}) (*Rows, error) {
|
|
||||||
vv := reflect.ValueOf(mp)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
|
||||||
return nil, errors.New("mp should be a map's pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
args := make([]interface{}, len(s.names))
|
|
||||||
for k, i := range s.names {
|
|
||||||
args[i] = vv.Elem().MapIndex(reflect.ValueOf(k)).Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.Query(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stmt) QueryStruct(st interface{}) (*Rows, error) {
|
|
||||||
vv := reflect.ValueOf(st)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
|
||||||
return nil, errors.New("mp should be a map's pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
args := make([]interface{}, len(s.names))
|
|
||||||
for k, i := range s.names {
|
|
||||||
args[i] = vv.Elem().FieldByName(k).Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.Query(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stmt) QueryRow(args ...interface{}) *Row {
|
|
||||||
rows, err := s.Query(args...)
|
|
||||||
return &Row{rows, err}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stmt) QueryRowMap(mp interface{}) *Row {
|
|
||||||
vv := reflect.ValueOf(mp)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
|
||||||
return &Row{nil, errors.New("mp should be a map's pointer")}
|
|
||||||
}
|
|
||||||
|
|
||||||
args := make([]interface{}, len(s.names))
|
|
||||||
for k, i := range s.names {
|
|
||||||
args[i] = vv.Elem().MapIndex(reflect.ValueOf(k)).Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.QueryRow(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stmt) QueryRowStruct(st interface{}) *Row {
|
|
||||||
vv := reflect.ValueOf(st)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
|
||||||
return &Row{nil, errors.New("st should be a struct's pointer")}
|
|
||||||
}
|
|
||||||
|
|
||||||
args := make([]interface{}, len(s.names))
|
|
||||||
for k, i := range s.names {
|
|
||||||
args[i] = vv.Elem().FieldByName(k).Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.QueryRow(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
re = regexp.MustCompile(`[?](\w+)`)
|
|
||||||
)
|
|
||||||
|
|
||||||
// insert into (name) values (?)
|
|
||||||
// insert into (name) values (?name)
|
|
||||||
func (db *DB) ExecMap(query string, mp interface{}) (sql.Result, error) {
|
|
||||||
query, args, err := MapToSlice(query, mp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return db.DB.Exec(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) ExecStruct(query string, st interface{}) (sql.Result, error) {
|
|
||||||
query, args, err := StructToSlice(query, st)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return db.DB.Exec(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
type EmptyScanner struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (EmptyScanner) Scan(src interface{}) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Tx struct {
|
|
||||||
*sql.Tx
|
|
||||||
Mapper IMapper
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) Begin() (*Tx, error) {
|
|
||||||
tx, err := db.DB.Begin()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Tx{tx, db.Mapper}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) Prepare(query string) (*Stmt, error) {
|
|
||||||
names := make(map[string]int)
|
|
||||||
var i int
|
|
||||||
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
|
||||||
names[src[1:]] = i
|
|
||||||
i += 1
|
|
||||||
return "?"
|
|
||||||
})
|
|
||||||
|
|
||||||
stmt, err := tx.Tx.Prepare(query)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Stmt{stmt, tx.Mapper, names}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
|
|
||||||
// TODO:
|
|
||||||
return stmt
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error) {
|
|
||||||
query, args, err := MapToSlice(query, mp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return tx.Tx.Exec(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error) {
|
|
||||||
query, args, err := StructToSlice(query, st)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return tx.Tx.Exec(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
|
|
||||||
rows, err := tx.Tx.Query(query, args...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &Rows{rows, tx.Mapper}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error) {
|
|
||||||
query, args, err := MapToSlice(query, mp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return tx.Query(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error) {
|
|
||||||
query, args, err := StructToSlice(query, st)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return tx.Query(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
|
|
||||||
rows, err := tx.Query(query, args...)
|
|
||||||
return &Row{rows, err}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row {
|
|
||||||
query, args, err := MapToSlice(query, mp)
|
|
||||||
if err != nil {
|
|
||||||
return &Row{nil, err}
|
|
||||||
}
|
|
||||||
return tx.QueryRow(query, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row {
|
|
||||||
query, args, err := StructToSlice(query, st)
|
|
||||||
if err != nil {
|
|
||||||
return &Row{nil, err}
|
|
||||||
}
|
|
||||||
return tx.QueryRow(query, args...)
|
|
||||||
}
|
|
|
@ -1,659 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
//dbtype string = "sqlite3"
|
|
||||||
dbtype string = "mysql"
|
|
||||||
createTableSql string
|
|
||||||
)
|
|
||||||
|
|
||||||
type User struct {
|
|
||||||
Id int64
|
|
||||||
Name string
|
|
||||||
Title string
|
|
||||||
Age float32
|
|
||||||
Alias string
|
|
||||||
NickName string
|
|
||||||
Created NullTime
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
switch dbtype {
|
|
||||||
case "sqlite3":
|
|
||||||
createTableSql = "CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NULL, " +
|
|
||||||
"`title` TEXT NULL, `age` FLOAT NULL, `alias` TEXT NULL, `nick_name` TEXT NULL, `created` datetime);"
|
|
||||||
case "mysql":
|
|
||||||
createTableSql = "CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, `name` TEXT NULL, " +
|
|
||||||
"`title` TEXT NULL, `age` FLOAT NULL, `alias` TEXT NULL, `nick_name` TEXT NULL, `created` datetime);"
|
|
||||||
default:
|
|
||||||
panic("no db type")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testOpen() (*DB, error) {
|
|
||||||
switch dbtype {
|
|
||||||
case "sqlite3":
|
|
||||||
os.Remove("./test.db")
|
|
||||||
return Open("sqlite3", "./test.db")
|
|
||||||
case "mysql":
|
|
||||||
return Open("mysql", "root:@/core_test?charset=utf8")
|
|
||||||
default:
|
|
||||||
panic("no db type")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkOriQuery(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
_, err = db.Exec("insert into user (`name`, title, age, alias, nick_name, created) values (?,?,?,?,?, ?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var Id int64
|
|
||||||
var Name, Title, Alias, NickName string
|
|
||||||
var Age float32
|
|
||||||
var Created NullTime
|
|
||||||
err = rows.Scan(&Id, &Name, &Title, &Age, &Alias, &NickName, &Created)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
//fmt.Println(Id, Name, Title, Age, Alias, NickName)
|
|
||||||
}
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkStructQuery(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
_, err = db.Exec("insert into user (`name`, title, age, alias, nick_name, created) values (?,?,?,?,?, ?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var user User
|
|
||||||
err = rows.ScanStructByIndex(&user)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
if user.Name != "xlw" {
|
|
||||||
fmt.Println(user)
|
|
||||||
b.Error(errors.New("name should be xlw"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkStruct2Query(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
_, err = db.Exec("insert into user (`name`, title, age, alias, nick_name, created) values (?,?,?,?,?,?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
db.Mapper = NewCacheMapper(&SnakeMapper{})
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var user User
|
|
||||||
err = rows.ScanStructByName(&user)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
if user.Name != "xlw" {
|
|
||||||
fmt.Println(user)
|
|
||||||
b.Error(errors.New("name should be xlw"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkSliceInterfaceQuery(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
_, err = db.Exec("insert into user (`name`, title, age, alias, nick_name,created) values (?,?,?,?,?,?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cols, err := rows.Columns()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
slice := make([]interface{}, len(cols))
|
|
||||||
err = rows.ScanSlice(&slice)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
fmt.Println(slice)
|
|
||||||
if *slice[1].(*string) != "xlw" {
|
|
||||||
fmt.Println(slice)
|
|
||||||
b.Error(errors.New("name should be xlw"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*func BenchmarkSliceBytesQuery(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
os.Remove("./test.db")
|
|
||||||
db, err := Open("sqlite3", "./test.db")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
_, err = db.Exec("insert into user (name, title, age, alias, nick_name,created) values (?,?,?,?,?,?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cols, err := rows.Columns()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
slice := make([][]byte, len(cols))
|
|
||||||
err = rows.ScanSlice(&slice)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
if string(slice[1]) != "xlw" {
|
|
||||||
fmt.Println(slice)
|
|
||||||
b.Error(errors.New("name should be xlw"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
func BenchmarkSliceStringQuery(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
_, err = db.Exec("insert into user (name, title, age, alias, nick_name, created) values (?,?,?,?,?,?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cols, err := rows.Columns()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
slice := make([]*string, len(cols))
|
|
||||||
err = rows.ScanSlice(&slice)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
if (*slice[1]) != "xlw" {
|
|
||||||
fmt.Println(slice)
|
|
||||||
b.Error(errors.New("name should be xlw"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkMapInterfaceQuery(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
_, err = db.Exec("insert into user (name, title, age, alias, nick_name,created) values (?,?,?,?,?,?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
m := make(map[string]interface{})
|
|
||||||
err = rows.ScanMap(&m)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
if m["name"].(string) != "xlw" {
|
|
||||||
fmt.Println(m)
|
|
||||||
b.Error(errors.New("name should be xlw"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*func BenchmarkMapBytesQuery(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
os.Remove("./test.db")
|
|
||||||
db, err := Open("sqlite3", "./test.db")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
_, err = db.Exec("insert into user (name, title, age, alias, nick_name,created) values (?,?,?,?,?,?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
m := make(map[string][]byte)
|
|
||||||
err = rows.ScanMap(&m)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
if string(m["name"]) != "xlw" {
|
|
||||||
fmt.Println(m)
|
|
||||||
b.Error(errors.New("name should be xlw"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
func BenchmarkMapStringQuery(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
os.Remove("./test.db")
|
|
||||||
db, err := Open("sqlite3", "./test.db")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
|
||||||
_, err = db.Exec("insert into user (name, title, age, alias, nick_name,created) values (?,?,?,?,?,?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
m := make(map[string]string)
|
|
||||||
err = rows.ScanMap(&m)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
if m["name"] != "xlw" {
|
|
||||||
fmt.Println(m)
|
|
||||||
b.Error(errors.New("name should be xlw"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
func BenchmarkExec(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
_, err = db.Exec("insert into user (`name`, title, age, alias, nick_name,created) values (?,?,?,?,?,?)",
|
|
||||||
"xlw", "tester", 1.2, "lunny", "lunny xiao", time.Now())
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkExecMap(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
mp := map[string]interface{}{
|
|
||||||
"name": "xlw",
|
|
||||||
"title": "tester",
|
|
||||||
"age": 1.2,
|
|
||||||
"alias": "lunny",
|
|
||||||
"nick_name": "lunny xiao",
|
|
||||||
"created": time.Now(),
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
_, err = db.ExecMap("insert into user (`name`, title, age, alias, nick_name, created) "+
|
|
||||||
"values (?name,?title,?age,?alias,?nick_name,?created)",
|
|
||||||
&mp)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExecMap(t *testing.T) {
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
mp := map[string]interface{}{
|
|
||||||
"name": "xlw",
|
|
||||||
"title": "tester",
|
|
||||||
"age": 1.2,
|
|
||||||
"alias": "lunny",
|
|
||||||
"nick_name": "lunny xiao",
|
|
||||||
"created": time.Now(),
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = db.ExecMap("insert into user (`name`, title, age, alias, nick_name,created) "+
|
|
||||||
"values (?name,?title,?age,?alias,?nick_name,?created)",
|
|
||||||
&mp)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err := db.Query("select * from user")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var user User
|
|
||||||
err = rows.ScanStructByName(&user)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
fmt.Println("--", user)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExecStruct(t *testing.T) {
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
user := User{Name: "xlw",
|
|
||||||
Title: "tester",
|
|
||||||
Age: 1.2,
|
|
||||||
Alias: "lunny",
|
|
||||||
NickName: "lunny xiao",
|
|
||||||
Created: NullTime(time.Now()),
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = db.ExecStruct("insert into user (`name`, title, age, alias, nick_name,created) "+
|
|
||||||
"values (?Name,?Title,?Age,?Alias,?NickName,?Created)",
|
|
||||||
&user)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err := db.QueryStruct("select * from user where `name` = ?Name", &user)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var user User
|
|
||||||
err = rows.ScanStructByName(&user)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
fmt.Println("1--", user)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkExecStruct(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
db, err := testOpen()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
_, err = db.Exec(createTableSql)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
user := User{Name: "xlw",
|
|
||||||
Title: "tester",
|
|
||||||
Age: 1.2,
|
|
||||||
Alias: "lunny",
|
|
||||||
NickName: "lunny xiao",
|
|
||||||
Created: NullTime(time.Now()),
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
_, err = db.ExecStruct("insert into user (`name`, title, age, alias, nick_name,created) "+
|
|
||||||
"values (?Name,?Title,?Age,?Alias,?NickName,?Created)",
|
|
||||||
&user)
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,307 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type DbType string
|
|
||||||
|
|
||||||
type Uri struct {
|
|
||||||
DbType DbType
|
|
||||||
Proto string
|
|
||||||
Host string
|
|
||||||
Port string
|
|
||||||
DbName string
|
|
||||||
User string
|
|
||||||
Passwd string
|
|
||||||
Charset string
|
|
||||||
Laddr string
|
|
||||||
Raddr string
|
|
||||||
Timeout time.Duration
|
|
||||||
Schema string
|
|
||||||
}
|
|
||||||
|
|
||||||
// a dialect is a driver's wrapper
|
|
||||||
type Dialect interface {
|
|
||||||
SetLogger(logger ILogger)
|
|
||||||
Init(*DB, *Uri, string, string) error
|
|
||||||
URI() *Uri
|
|
||||||
DB() *DB
|
|
||||||
DBType() DbType
|
|
||||||
SqlType(*Column) string
|
|
||||||
FormatBytes(b []byte) string
|
|
||||||
|
|
||||||
DriverName() string
|
|
||||||
DataSourceName() string
|
|
||||||
|
|
||||||
QuoteStr() string
|
|
||||||
IsReserved(string) bool
|
|
||||||
Quote(string) string
|
|
||||||
AndStr() string
|
|
||||||
OrStr() string
|
|
||||||
EqStr() string
|
|
||||||
RollBackStr() string
|
|
||||||
AutoIncrStr() string
|
|
||||||
|
|
||||||
SupportInsertMany() bool
|
|
||||||
SupportEngine() bool
|
|
||||||
SupportCharset() bool
|
|
||||||
SupportDropIfExists() bool
|
|
||||||
IndexOnTable() bool
|
|
||||||
ShowCreateNull() bool
|
|
||||||
|
|
||||||
IndexCheckSql(tableName, idxName string) (string, []interface{})
|
|
||||||
TableCheckSql(tableName string) (string, []interface{})
|
|
||||||
|
|
||||||
IsColumnExist(tableName string, colName string) (bool, error)
|
|
||||||
|
|
||||||
CreateTableSql(table *Table, tableName, storeEngine, charset string) string
|
|
||||||
DropTableSql(tableName string) string
|
|
||||||
CreateIndexSql(tableName string, index *Index) string
|
|
||||||
DropIndexSql(tableName string, index *Index) string
|
|
||||||
|
|
||||||
ModifyColumnSql(tableName string, col *Column) string
|
|
||||||
|
|
||||||
ForUpdateSql(query string) string
|
|
||||||
|
|
||||||
//CreateTableIfNotExists(table *Table, tableName, storeEngine, charset string) error
|
|
||||||
//MustDropTable(tableName string) error
|
|
||||||
|
|
||||||
GetColumns(tableName string) ([]string, map[string]*Column, error)
|
|
||||||
GetTables() ([]*Table, error)
|
|
||||||
GetIndexes(tableName string) (map[string]*Index, error)
|
|
||||||
|
|
||||||
Filters() []Filter
|
|
||||||
}
|
|
||||||
|
|
||||||
func OpenDialect(dialect Dialect) (*DB, error) {
|
|
||||||
return Open(dialect.DriverName(), dialect.DataSourceName())
|
|
||||||
}
|
|
||||||
|
|
||||||
type Base struct {
|
|
||||||
db *DB
|
|
||||||
dialect Dialect
|
|
||||||
driverName string
|
|
||||||
dataSourceName string
|
|
||||||
logger ILogger
|
|
||||||
*Uri
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) DB() *DB {
|
|
||||||
return b.db
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) SetLogger(logger ILogger) {
|
|
||||||
b.logger = logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) Init(db *DB, dialect Dialect, uri *Uri, drivername, dataSourceName string) error {
|
|
||||||
b.db, b.dialect, b.Uri = db, dialect, uri
|
|
||||||
b.driverName, b.dataSourceName = drivername, dataSourceName
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) URI() *Uri {
|
|
||||||
return b.Uri
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) DBType() DbType {
|
|
||||||
return b.Uri.DbType
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) FormatBytes(bs []byte) string {
|
|
||||||
return fmt.Sprintf("0x%x", bs)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) DriverName() string {
|
|
||||||
return b.driverName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) ShowCreateNull() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) DataSourceName() string {
|
|
||||||
return b.dataSourceName
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) AndStr() string {
|
|
||||||
return "AND"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) OrStr() string {
|
|
||||||
return "OR"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) EqStr() string {
|
|
||||||
return "="
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Base) RollBackStr() string {
|
|
||||||
return "ROLL BACK"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Base) SupportDropIfExists() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Base) DropTableSql(tableName string) string {
|
|
||||||
return fmt.Sprintf("DROP TABLE IF EXISTS `%s`", tableName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Base) HasRecords(query string, args ...interface{}) (bool, error) {
|
|
||||||
db.LogSQL(query, args)
|
|
||||||
rows, err := db.DB().Query(query, args...)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
if rows.Next() {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Base) IsColumnExist(tableName, colName string) (bool, error) {
|
|
||||||
query := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
|
||||||
query = strings.Replace(query, "`", db.dialect.QuoteStr(), -1)
|
|
||||||
return db.HasRecords(query, db.DbName, tableName, colName)
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
func (db *Base) CreateTableIfNotExists(table *Table, tableName, storeEngine, charset string) error {
|
|
||||||
sql, args := db.dialect.TableCheckSql(tableName)
|
|
||||||
rows, err := db.DB().Query(sql, args...)
|
|
||||||
if db.Logger != nil {
|
|
||||||
db.Logger.Info("[sql]", sql, args)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
if rows.Next() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
sql = db.dialect.CreateTableSql(table, tableName, storeEngine, charset)
|
|
||||||
_, err = db.DB().Exec(sql)
|
|
||||||
if db.Logger != nil {
|
|
||||||
db.Logger.Info("[sql]", sql)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}*/
|
|
||||||
|
|
||||||
func (db *Base) CreateIndexSql(tableName string, index *Index) string {
|
|
||||||
quote := db.dialect.Quote
|
|
||||||
var unique string
|
|
||||||
var idxName string
|
|
||||||
if index.Type == UniqueType {
|
|
||||||
unique = " UNIQUE"
|
|
||||||
}
|
|
||||||
idxName = index.XName(tableName)
|
|
||||||
return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v)", unique,
|
|
||||||
quote(idxName), quote(tableName),
|
|
||||||
quote(strings.Join(index.Cols, quote(","))))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Base) DropIndexSql(tableName string, index *Index) string {
|
|
||||||
quote := db.dialect.Quote
|
|
||||||
var name string
|
|
||||||
if index.IsRegular {
|
|
||||||
name = index.XName(tableName)
|
|
||||||
} else {
|
|
||||||
name = index.Name
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("DROP INDEX %v ON %s", quote(name), quote(tableName))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *Base) ModifyColumnSql(tableName string, col *Column) string {
|
|
||||||
return fmt.Sprintf("alter table %s MODIFY COLUMN %s", tableName, col.StringNoPk(db.dialect))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset string) string {
|
|
||||||
var sql string
|
|
||||||
sql = "CREATE TABLE IF NOT EXISTS "
|
|
||||||
if tableName == "" {
|
|
||||||
tableName = table.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
sql += b.dialect.Quote(tableName)
|
|
||||||
sql += " ("
|
|
||||||
|
|
||||||
if len(table.ColumnsSeq()) > 0 {
|
|
||||||
pkList := table.PrimaryKeys
|
|
||||||
|
|
||||||
for _, colName := range table.ColumnsSeq() {
|
|
||||||
col := table.GetColumn(colName)
|
|
||||||
if col.IsPrimaryKey && len(pkList) == 1 {
|
|
||||||
sql += col.String(b.dialect)
|
|
||||||
} else {
|
|
||||||
sql += col.StringNoPk(b.dialect)
|
|
||||||
}
|
|
||||||
sql = strings.TrimSpace(sql)
|
|
||||||
sql += ", "
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(pkList) > 1 {
|
|
||||||
sql += "PRIMARY KEY ( "
|
|
||||||
sql += b.dialect.Quote(strings.Join(pkList, b.dialect.Quote(",")))
|
|
||||||
sql += " ), "
|
|
||||||
}
|
|
||||||
|
|
||||||
sql = sql[:len(sql)-2]
|
|
||||||
}
|
|
||||||
sql += ")"
|
|
||||||
|
|
||||||
if b.dialect.SupportEngine() && storeEngine != "" {
|
|
||||||
sql += " ENGINE=" + storeEngine
|
|
||||||
}
|
|
||||||
if b.dialect.SupportCharset() {
|
|
||||||
if len(charset) == 0 {
|
|
||||||
charset = b.dialect.URI().Charset
|
|
||||||
}
|
|
||||||
if len(charset) > 0 {
|
|
||||||
sql += " DEFAULT CHARSET " + charset
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sql
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) ForUpdateSql(query string) string {
|
|
||||||
return query + " FOR UPDATE"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) LogSQL(sql string, args []interface{}) {
|
|
||||||
if b.logger != nil && b.logger.IsShowSQL() {
|
|
||||||
if len(args) > 0 {
|
|
||||||
b.logger.Infof("[SQL] %v %v", sql, args)
|
|
||||||
} else {
|
|
||||||
b.logger.Infof("[SQL] %v", sql)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
dialects = map[string]func() Dialect{}
|
|
||||||
)
|
|
||||||
|
|
||||||
// RegisterDialect register database dialect
|
|
||||||
func RegisterDialect(dbName DbType, dialectFunc func() Dialect) {
|
|
||||||
if dialectFunc == nil {
|
|
||||||
panic("core: Register dialect is nil")
|
|
||||||
}
|
|
||||||
dialects[strings.ToLower(string(dbName))] = dialectFunc // !nashtsai! allow override dialect
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueryDialect query if registed database dialect
|
|
||||||
func QueryDialect(dbName DbType) Dialect {
|
|
||||||
if d, ok := dialects[strings.ToLower(string(dbName))]; ok {
|
|
||||||
return d()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
type Driver interface {
|
|
||||||
Parse(string, string) (*Uri, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
drivers = map[string]Driver{}
|
|
||||||
)
|
|
||||||
|
|
||||||
func RegisterDriver(driverName string, driver Driver) {
|
|
||||||
if driver == nil {
|
|
||||||
panic("core: Register driver is nil")
|
|
||||||
}
|
|
||||||
if _, dup := drivers[driverName]; dup {
|
|
||||||
panic("core: Register called twice for driver " + driverName)
|
|
||||||
}
|
|
||||||
drivers[driverName] = driver
|
|
||||||
}
|
|
||||||
|
|
||||||
func QueryDriver(driverName string) Driver {
|
|
||||||
return drivers[driverName]
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegisteredDriverSize() int {
|
|
||||||
return len(drivers)
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrNoMapPointer = errors.New("mp should be a map's pointer")
|
|
||||||
ErrNoStructPointer = errors.New("mp should be a struct's pointer")
|
|
||||||
)
|
|
|
@ -1,64 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Filter is an interface to filter SQL
|
|
||||||
type Filter interface {
|
|
||||||
Do(sql string, dialect Dialect, table *Table) string
|
|
||||||
}
|
|
||||||
|
|
||||||
// QuoteFilter filter SQL replace ` to database's own quote character
|
|
||||||
type QuoteFilter struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *QuoteFilter) Do(sql string, dialect Dialect, table *Table) string {
|
|
||||||
return strings.Replace(sql, "`", dialect.QuoteStr(), -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IdFilter filter SQL replace (id) to primary key column name
|
|
||||||
type IdFilter struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
type Quoter struct {
|
|
||||||
dialect Dialect
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewQuoter(dialect Dialect) *Quoter {
|
|
||||||
return &Quoter{dialect}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *Quoter) Quote(content string) string {
|
|
||||||
return q.dialect.QuoteStr() + content + q.dialect.QuoteStr()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *IdFilter) Do(sql string, dialect Dialect, table *Table) string {
|
|
||||||
quoter := NewQuoter(dialect)
|
|
||||||
if table != nil && len(table.PrimaryKeys) == 1 {
|
|
||||||
sql = strings.Replace(sql, "`(id)`", quoter.Quote(table.PrimaryKeys[0]), -1)
|
|
||||||
sql = strings.Replace(sql, quoter.Quote("(id)"), quoter.Quote(table.PrimaryKeys[0]), -1)
|
|
||||||
return strings.Replace(sql, "(id)", quoter.Quote(table.PrimaryKeys[0]), -1)
|
|
||||||
}
|
|
||||||
return sql
|
|
||||||
}
|
|
||||||
|
|
||||||
// SeqFilter filter SQL replace ?, ? ... to $1, $2 ...
|
|
||||||
type SeqFilter struct {
|
|
||||||
Prefix string
|
|
||||||
Start int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SeqFilter) Do(sql string, dialect Dialect, table *Table) string {
|
|
||||||
segs := strings.Split(sql, "?")
|
|
||||||
size := len(segs)
|
|
||||||
res := ""
|
|
||||||
for i, c := range segs {
|
|
||||||
if i < size-1 {
|
|
||||||
res += c + fmt.Sprintf("%s%v", s.Prefix, i+s.Start)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res += segs[size-1]
|
|
||||||
return res
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
type LogLevel int
|
|
||||||
|
|
||||||
const (
|
|
||||||
// !nashtsai! following level also match syslog.Priority value
|
|
||||||
LOG_DEBUG LogLevel = iota
|
|
||||||
LOG_INFO
|
|
||||||
LOG_WARNING
|
|
||||||
LOG_ERR
|
|
||||||
LOG_OFF
|
|
||||||
LOG_UNKNOWN
|
|
||||||
)
|
|
||||||
|
|
||||||
// logger interface
|
|
||||||
type ILogger interface {
|
|
||||||
Debug(v ...interface{})
|
|
||||||
Debugf(format string, v ...interface{})
|
|
||||||
Error(v ...interface{})
|
|
||||||
Errorf(format string, v ...interface{})
|
|
||||||
Info(v ...interface{})
|
|
||||||
Infof(format string, v ...interface{})
|
|
||||||
Warn(v ...interface{})
|
|
||||||
Warnf(format string, v ...interface{})
|
|
||||||
|
|
||||||
Level() LogLevel
|
|
||||||
SetLevel(l LogLevel)
|
|
||||||
|
|
||||||
ShowSQL(show ...bool)
|
|
||||||
IsShowSQL() bool
|
|
||||||
}
|
|
|
@ -1,61 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
IndexType = iota + 1
|
|
||||||
UniqueType
|
|
||||||
)
|
|
||||||
|
|
||||||
// database index
|
|
||||||
type Index struct {
|
|
||||||
IsRegular bool
|
|
||||||
Name string
|
|
||||||
Type int
|
|
||||||
Cols []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (index *Index) XName(tableName string) string {
|
|
||||||
if !strings.HasPrefix(index.Name, "UQE_") &&
|
|
||||||
!strings.HasPrefix(index.Name, "IDX_") {
|
|
||||||
if index.Type == UniqueType {
|
|
||||||
return fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
|
|
||||||
}
|
|
||||||
return index.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
// add columns which will be composite index
|
|
||||||
func (index *Index) AddColumn(cols ...string) {
|
|
||||||
for _, col := range cols {
|
|
||||||
index.Cols = append(index.Cols, col)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (index *Index) Equal(dst *Index) bool {
|
|
||||||
if index.Type != dst.Type {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if len(index.Cols) != len(dst.Cols) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
sort.StringSlice(index.Cols).Sort()
|
|
||||||
sort.StringSlice(dst.Cols).Sort()
|
|
||||||
|
|
||||||
for i := 0; i < len(index.Cols); i++ {
|
|
||||||
if index.Cols[i] != dst.Cols[i] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// new an index
|
|
||||||
func NewIndex(name string, indexType int) *Index {
|
|
||||||
return &Index{true, name, indexType, make([]string, 0)}
|
|
||||||
}
|
|
|
@ -1,254 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// name translation between struct, fields names and table, column names
|
|
||||||
type IMapper interface {
|
|
||||||
Obj2Table(string) string
|
|
||||||
Table2Obj(string) string
|
|
||||||
}
|
|
||||||
|
|
||||||
type CacheMapper struct {
|
|
||||||
oriMapper IMapper
|
|
||||||
obj2tableCache map[string]string
|
|
||||||
obj2tableMutex sync.RWMutex
|
|
||||||
table2objCache map[string]string
|
|
||||||
table2objMutex sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCacheMapper(mapper IMapper) *CacheMapper {
|
|
||||||
return &CacheMapper{oriMapper: mapper, obj2tableCache: make(map[string]string),
|
|
||||||
table2objCache: make(map[string]string),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *CacheMapper) Obj2Table(o string) string {
|
|
||||||
m.obj2tableMutex.RLock()
|
|
||||||
t, ok := m.obj2tableCache[o]
|
|
||||||
m.obj2tableMutex.RUnlock()
|
|
||||||
if ok {
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
t = m.oriMapper.Obj2Table(o)
|
|
||||||
m.obj2tableMutex.Lock()
|
|
||||||
m.obj2tableCache[o] = t
|
|
||||||
m.obj2tableMutex.Unlock()
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *CacheMapper) Table2Obj(t string) string {
|
|
||||||
m.table2objMutex.RLock()
|
|
||||||
o, ok := m.table2objCache[t]
|
|
||||||
m.table2objMutex.RUnlock()
|
|
||||||
if ok {
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
o = m.oriMapper.Table2Obj(t)
|
|
||||||
m.table2objMutex.Lock()
|
|
||||||
m.table2objCache[t] = o
|
|
||||||
m.table2objMutex.Unlock()
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
// SameMapper implements IMapper and provides same name between struct and
|
|
||||||
// database table
|
|
||||||
type SameMapper struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m SameMapper) Obj2Table(o string) string {
|
|
||||||
return o
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m SameMapper) Table2Obj(t string) string {
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
// SnakeMapper implements IMapper and provides name transaltion between
|
|
||||||
// struct and database table
|
|
||||||
type SnakeMapper struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func snakeCasedName(name string) string {
|
|
||||||
newstr := make([]rune, 0)
|
|
||||||
for idx, chr := range name {
|
|
||||||
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
|
|
||||||
if idx > 0 {
|
|
||||||
newstr = append(newstr, '_')
|
|
||||||
}
|
|
||||||
chr -= ('A' - 'a')
|
|
||||||
}
|
|
||||||
newstr = append(newstr, chr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(newstr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mapper SnakeMapper) Obj2Table(name string) string {
|
|
||||||
return snakeCasedName(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func titleCasedName(name string) string {
|
|
||||||
newstr := make([]rune, 0)
|
|
||||||
upNextChar := true
|
|
||||||
|
|
||||||
name = strings.ToLower(name)
|
|
||||||
|
|
||||||
for _, chr := range name {
|
|
||||||
switch {
|
|
||||||
case upNextChar:
|
|
||||||
upNextChar = false
|
|
||||||
if 'a' <= chr && chr <= 'z' {
|
|
||||||
chr -= ('a' - 'A')
|
|
||||||
}
|
|
||||||
case chr == '_':
|
|
||||||
upNextChar = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
newstr = append(newstr, chr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(newstr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mapper SnakeMapper) Table2Obj(name string) string {
|
|
||||||
return titleCasedName(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GonicMapper implements IMapper. It will consider initialisms when mapping names.
|
|
||||||
// E.g. id -> ID, user -> User and to table names: UserID -> user_id, MyUID -> my_uid
|
|
||||||
type GonicMapper map[string]bool
|
|
||||||
|
|
||||||
func isASCIIUpper(r rune) bool {
|
|
||||||
return 'A' <= r && r <= 'Z'
|
|
||||||
}
|
|
||||||
|
|
||||||
func toASCIIUpper(r rune) rune {
|
|
||||||
if 'a' <= r && r <= 'z' {
|
|
||||||
r -= ('a' - 'A')
|
|
||||||
}
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func gonicCasedName(name string) string {
|
|
||||||
newstr := make([]rune, 0, len(name)+3)
|
|
||||||
for idx, chr := range name {
|
|
||||||
if isASCIIUpper(chr) && idx > 0 {
|
|
||||||
if !isASCIIUpper(newstr[len(newstr)-1]) {
|
|
||||||
newstr = append(newstr, '_')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isASCIIUpper(chr) && idx > 1 {
|
|
||||||
l := len(newstr)
|
|
||||||
if isASCIIUpper(newstr[l-1]) && isASCIIUpper(newstr[l-2]) {
|
|
||||||
newstr = append(newstr, newstr[l-1])
|
|
||||||
newstr[l-1] = '_'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
newstr = append(newstr, chr)
|
|
||||||
}
|
|
||||||
return strings.ToLower(string(newstr))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mapper GonicMapper) Obj2Table(name string) string {
|
|
||||||
return gonicCasedName(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mapper GonicMapper) Table2Obj(name string) string {
|
|
||||||
newstr := make([]rune, 0)
|
|
||||||
|
|
||||||
name = strings.ToLower(name)
|
|
||||||
parts := strings.Split(name, "_")
|
|
||||||
|
|
||||||
for _, p := range parts {
|
|
||||||
_, isInitialism := mapper[strings.ToUpper(p)]
|
|
||||||
for i, r := range p {
|
|
||||||
if i == 0 || isInitialism {
|
|
||||||
r = toASCIIUpper(r)
|
|
||||||
}
|
|
||||||
newstr = append(newstr, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(newstr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A GonicMapper that contains a list of common initialisms taken from golang/lint
|
|
||||||
var LintGonicMapper = GonicMapper{
|
|
||||||
"API": true,
|
|
||||||
"ASCII": true,
|
|
||||||
"CPU": true,
|
|
||||||
"CSS": true,
|
|
||||||
"DNS": true,
|
|
||||||
"EOF": true,
|
|
||||||
"GUID": true,
|
|
||||||
"HTML": true,
|
|
||||||
"HTTP": true,
|
|
||||||
"HTTPS": true,
|
|
||||||
"ID": true,
|
|
||||||
"IP": true,
|
|
||||||
"JSON": true,
|
|
||||||
"LHS": true,
|
|
||||||
"QPS": true,
|
|
||||||
"RAM": true,
|
|
||||||
"RHS": true,
|
|
||||||
"RPC": true,
|
|
||||||
"SLA": true,
|
|
||||||
"SMTP": true,
|
|
||||||
"SSH": true,
|
|
||||||
"TLS": true,
|
|
||||||
"TTL": true,
|
|
||||||
"UI": true,
|
|
||||||
"UID": true,
|
|
||||||
"UUID": true,
|
|
||||||
"URI": true,
|
|
||||||
"URL": true,
|
|
||||||
"UTF8": true,
|
|
||||||
"VM": true,
|
|
||||||
"XML": true,
|
|
||||||
"XSRF": true,
|
|
||||||
"XSS": true,
|
|
||||||
}
|
|
||||||
|
|
||||||
// provide prefix table name support
|
|
||||||
type PrefixMapper struct {
|
|
||||||
Mapper IMapper
|
|
||||||
Prefix string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mapper PrefixMapper) Obj2Table(name string) string {
|
|
||||||
return mapper.Prefix + mapper.Mapper.Obj2Table(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mapper PrefixMapper) Table2Obj(name string) string {
|
|
||||||
return mapper.Mapper.Table2Obj(name[len(mapper.Prefix):])
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPrefixMapper(mapper IMapper, prefix string) PrefixMapper {
|
|
||||||
return PrefixMapper{mapper, prefix}
|
|
||||||
}
|
|
||||||
|
|
||||||
// provide suffix table name support
|
|
||||||
type SuffixMapper struct {
|
|
||||||
Mapper IMapper
|
|
||||||
Suffix string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mapper SuffixMapper) Obj2Table(name string) string {
|
|
||||||
return mapper.Mapper.Obj2Table(name) + mapper.Suffix
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mapper SuffixMapper) Table2Obj(name string) string {
|
|
||||||
return mapper.Mapper.Table2Obj(name[:len(name)-len(mapper.Suffix)])
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSuffixMapper(mapper IMapper, suffix string) SuffixMapper {
|
|
||||||
return SuffixMapper{mapper, suffix}
|
|
||||||
}
|
|
|
@ -1,45 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGonicMapperFromObj(t *testing.T) {
|
|
||||||
testCases := map[string]string{
|
|
||||||
"HTTPLib": "http_lib",
|
|
||||||
"id": "id",
|
|
||||||
"ID": "id",
|
|
||||||
"IDa": "i_da",
|
|
||||||
"iDa": "i_da",
|
|
||||||
"IDAa": "id_aa",
|
|
||||||
"aID": "a_id",
|
|
||||||
"aaID": "aa_id",
|
|
||||||
"aaaID": "aaa_id",
|
|
||||||
"MyREalFunkYLONgNAME": "my_r_eal_funk_ylo_ng_name",
|
|
||||||
}
|
|
||||||
|
|
||||||
for in, expected := range testCases {
|
|
||||||
out := gonicCasedName(in)
|
|
||||||
if out != expected {
|
|
||||||
t.Errorf("Given %s, expected %s but got %s", in, expected, out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGonicMapperToObj(t *testing.T) {
|
|
||||||
testCases := map[string]string{
|
|
||||||
"http_lib": "HTTPLib",
|
|
||||||
"id": "ID",
|
|
||||||
"ida": "Ida",
|
|
||||||
"id_aa": "IDAa",
|
|
||||||
"aa_id": "AaID",
|
|
||||||
"my_r_eal_funk_ylo_ng_name": "MyREalFunkYloNgName",
|
|
||||||
}
|
|
||||||
|
|
||||||
for in, expected := range testCases {
|
|
||||||
out := LintGonicMapper.Table2Obj(in)
|
|
||||||
if out != expected {
|
|
||||||
t.Errorf("Given %s, expected %s but got %s", in, expected, out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,26 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/gob"
|
|
||||||
)
|
|
||||||
|
|
||||||
type PK []interface{}
|
|
||||||
|
|
||||||
func NewPK(pks ...interface{}) *PK {
|
|
||||||
p := PK(pks)
|
|
||||||
return &p
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PK) ToString() (string, error) {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
enc := gob.NewEncoder(buf)
|
|
||||||
err := enc.Encode(*p)
|
|
||||||
return buf.String(), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PK) FromString(content string) error {
|
|
||||||
dec := gob.NewDecoder(bytes.NewBufferString(content))
|
|
||||||
err := dec.Decode(p)
|
|
||||||
return err
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPK(t *testing.T) {
|
|
||||||
p := NewPK(1, 3, "string")
|
|
||||||
str, err := p.ToString()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
s := &PK{}
|
|
||||||
err = s.FromString(str)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
fmt.Println(s)
|
|
||||||
|
|
||||||
if len(*p) != len(*s) {
|
|
||||||
t.Fatal("p", *p, "should be equal", *s)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, ori := range *p {
|
|
||||||
if ori != (*s)[i] {
|
|
||||||
t.Fatal("ori", ori, reflect.ValueOf(ori), "should be equal", (*s)[i], reflect.ValueOf((*s)[i]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,380 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"errors"
|
|
||||||
"reflect"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Rows struct {
|
|
||||||
*sql.Rows
|
|
||||||
Mapper IMapper
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rs *Rows) ToMapString() ([]map[string]string, error) {
|
|
||||||
cols, err := rs.Columns()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var results = make([]map[string]string, 0, 10)
|
|
||||||
for rs.Next() {
|
|
||||||
var record = make(map[string]string, len(cols))
|
|
||||||
err = rs.ScanMap(&record)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
results = append(results, record)
|
|
||||||
}
|
|
||||||
return results, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// scan data to a struct's pointer according field index
|
|
||||||
func (rs *Rows) ScanStructByIndex(dest ...interface{}) error {
|
|
||||||
if len(dest) == 0 {
|
|
||||||
return errors.New("at least one struct")
|
|
||||||
}
|
|
||||||
|
|
||||||
vvvs := make([]reflect.Value, len(dest))
|
|
||||||
for i, s := range dest {
|
|
||||||
vv := reflect.ValueOf(s)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
|
||||||
return errors.New("dest should be a struct's pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
vvvs[i] = vv.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
cols, err := rs.Columns()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
newDest := make([]interface{}, len(cols))
|
|
||||||
|
|
||||||
var i = 0
|
|
||||||
for _, vvv := range vvvs {
|
|
||||||
for j := 0; j < vvv.NumField(); j++ {
|
|
||||||
newDest[i] = vvv.Field(j).Addr().Interface()
|
|
||||||
i = i + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return rs.Rows.Scan(newDest...)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
fieldCache = make(map[reflect.Type]map[string]int)
|
|
||||||
fieldCacheMutex sync.RWMutex
|
|
||||||
)
|
|
||||||
|
|
||||||
func fieldByName(v reflect.Value, name string) reflect.Value {
|
|
||||||
t := v.Type()
|
|
||||||
fieldCacheMutex.RLock()
|
|
||||||
cache, ok := fieldCache[t]
|
|
||||||
fieldCacheMutex.RUnlock()
|
|
||||||
if !ok {
|
|
||||||
cache = make(map[string]int)
|
|
||||||
for i := 0; i < v.NumField(); i++ {
|
|
||||||
cache[t.Field(i).Name] = i
|
|
||||||
}
|
|
||||||
fieldCacheMutex.Lock()
|
|
||||||
fieldCache[t] = cache
|
|
||||||
fieldCacheMutex.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
if i, ok := cache[name]; ok {
|
|
||||||
return v.Field(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
return reflect.Zero(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
// scan data to a struct's pointer according field name
|
|
||||||
func (rs *Rows) ScanStructByName(dest interface{}) error {
|
|
||||||
vv := reflect.ValueOf(dest)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
|
||||||
return errors.New("dest should be a struct's pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
cols, err := rs.Columns()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
newDest := make([]interface{}, len(cols))
|
|
||||||
var v EmptyScanner
|
|
||||||
for j, name := range cols {
|
|
||||||
f := fieldByName(vv.Elem(), rs.Mapper.Table2Obj(name))
|
|
||||||
if f.IsValid() {
|
|
||||||
newDest[j] = f.Addr().Interface()
|
|
||||||
} else {
|
|
||||||
newDest[j] = &v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return rs.Rows.Scan(newDest...)
|
|
||||||
}
|
|
||||||
|
|
||||||
type cacheStruct struct {
|
|
||||||
value reflect.Value
|
|
||||||
idx int
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
reflectCache = make(map[reflect.Type]*cacheStruct)
|
|
||||||
reflectCacheMutex sync.RWMutex
|
|
||||||
)
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
return cs.value.Index(cs.idx).Addr()
|
|
||||||
}
|
|
||||||
|
|
||||||
// scan data to a slice's pointer, slice's length should equal to columns' number
|
|
||||||
func (rs *Rows) ScanSlice(dest interface{}) error {
|
|
||||||
vv := reflect.ValueOf(dest)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Slice {
|
|
||||||
return errors.New("dest should be a slice's pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
vvv := vv.Elem()
|
|
||||||
cols, err := rs.Columns()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
newDest := make([]interface{}, len(cols))
|
|
||||||
|
|
||||||
for j := 0; j < len(cols); j++ {
|
|
||||||
if j >= vvv.Len() {
|
|
||||||
newDest[j] = reflect.New(vvv.Type().Elem()).Interface()
|
|
||||||
} else {
|
|
||||||
newDest[j] = vvv.Index(j).Addr().Interface()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = rs.Rows.Scan(newDest...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
srcLen := vvv.Len()
|
|
||||||
for i := srcLen; i < len(cols); i++ {
|
|
||||||
vvv = reflect.Append(vvv, reflect.ValueOf(newDest[i]).Elem())
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// scan data to a map's pointer
|
|
||||||
func (rs *Rows) ScanMap(dest interface{}) error {
|
|
||||||
vv := reflect.ValueOf(dest)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
|
||||||
return errors.New("dest should be a map's pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
cols, err := rs.Columns()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
newDest := make([]interface{}, len(cols))
|
|
||||||
vvv := vv.Elem()
|
|
||||||
|
|
||||||
for i, _ := range cols {
|
|
||||||
newDest[i] = ReflectNew(vvv.Type().Elem()).Interface()
|
|
||||||
//v := reflect.New(vvv.Type().Elem())
|
|
||||||
//newDest[i] = v.Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = rs.Rows.Scan(newDest...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, name := range cols {
|
|
||||||
vname := reflect.ValueOf(name)
|
|
||||||
vvv.SetMapIndex(vname, reflect.ValueOf(newDest[i]).Elem())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
/*func (rs *Rows) ScanMap(dest interface{}) error {
|
|
||||||
vv := reflect.ValueOf(dest)
|
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
|
||||||
return errors.New("dest should be a map's pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
cols, err := rs.Columns()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
newDest := make([]interface{}, len(cols))
|
|
||||||
err = rs.ScanSlice(newDest)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
vvv := vv.Elem()
|
|
||||||
|
|
||||||
for i, name := range cols {
|
|
||||||
vname := reflect.ValueOf(name)
|
|
||||||
vvv.SetMapIndex(vname, reflect.ValueOf(newDest[i]).Elem())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}*/
|
|
||||||
type Row struct {
|
|
||||||
rows *Rows
|
|
||||||
// One of these two will be non-nil:
|
|
||||||
err error // deferred error for easy chaining
|
|
||||||
}
|
|
||||||
|
|
||||||
func (row *Row) Columns() ([]string, error) {
|
|
||||||
if row.err != nil {
|
|
||||||
return nil, row.err
|
|
||||||
}
|
|
||||||
return row.rows.Columns()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (row *Row) Scan(dest ...interface{}) error {
|
|
||||||
if row.err != nil {
|
|
||||||
return row.err
|
|
||||||
}
|
|
||||||
defer row.rows.Close()
|
|
||||||
|
|
||||||
for _, dp := range dest {
|
|
||||||
if _, ok := dp.(*sql.RawBytes); ok {
|
|
||||||
return errors.New("sql: RawBytes isn't allowed on Row.Scan")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !row.rows.Next() {
|
|
||||||
if err := row.rows.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sql.ErrNoRows
|
|
||||||
}
|
|
||||||
err := row.rows.Scan(dest...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Make sure the query can be processed to completion with no errors.
|
|
||||||
return row.rows.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (row *Row) ScanStructByName(dest interface{}) error {
|
|
||||||
if row.err != nil {
|
|
||||||
return row.err
|
|
||||||
}
|
|
||||||
defer row.rows.Close()
|
|
||||||
|
|
||||||
if !row.rows.Next() {
|
|
||||||
if err := row.rows.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sql.ErrNoRows
|
|
||||||
}
|
|
||||||
err := row.rows.ScanStructByName(dest)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Make sure the query can be processed to completion with no errors.
|
|
||||||
return row.rows.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (row *Row) ScanStructByIndex(dest interface{}) error {
|
|
||||||
if row.err != nil {
|
|
||||||
return row.err
|
|
||||||
}
|
|
||||||
defer row.rows.Close()
|
|
||||||
|
|
||||||
if !row.rows.Next() {
|
|
||||||
if err := row.rows.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sql.ErrNoRows
|
|
||||||
}
|
|
||||||
err := row.rows.ScanStructByIndex(dest)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Make sure the query can be processed to completion with no errors.
|
|
||||||
return row.rows.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// scan data to a slice's pointer, slice's length should equal to columns' number
|
|
||||||
func (row *Row) ScanSlice(dest interface{}) error {
|
|
||||||
if row.err != nil {
|
|
||||||
return row.err
|
|
||||||
}
|
|
||||||
defer row.rows.Close()
|
|
||||||
|
|
||||||
if !row.rows.Next() {
|
|
||||||
if err := row.rows.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sql.ErrNoRows
|
|
||||||
}
|
|
||||||
err := row.rows.ScanSlice(dest)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure the query can be processed to completion with no errors.
|
|
||||||
return row.rows.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// scan data to a map's pointer
|
|
||||||
func (row *Row) ScanMap(dest interface{}) error {
|
|
||||||
if row.err != nil {
|
|
||||||
return row.err
|
|
||||||
}
|
|
||||||
defer row.rows.Close()
|
|
||||||
|
|
||||||
if !row.rows.Next() {
|
|
||||||
if err := row.rows.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sql.ErrNoRows
|
|
||||||
}
|
|
||||||
err := row.rows.ScanMap(dest)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure the query can be processed to completion with no errors.
|
|
||||||
return row.rows.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (row *Row) ToMapString() (map[string]string, error) {
|
|
||||||
cols, err := row.Columns()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var record = make(map[string]string, len(cols))
|
|
||||||
err = row.ScanMap(&record)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return record, nil
|
|
||||||
}
|
|
|
@ -1,52 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql/driver"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type NullTime time.Time
|
|
||||||
|
|
||||||
var (
|
|
||||||
_ driver.Valuer = NullTime{}
|
|
||||||
)
|
|
||||||
|
|
||||||
func (ns *NullTime) Scan(value interface{}) error {
|
|
||||||
if value == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return convertTime(ns, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Value implements the driver Valuer interface.
|
|
||||||
func (ns NullTime) Value() (driver.Value, error) {
|
|
||||||
if (time.Time)(ns).IsZero() {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return (time.Time)(ns).Format("2006-01-02 15:04:05"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertTime(dest *NullTime, src interface{}) error {
|
|
||||||
// Common cases, without reflect.
|
|
||||||
switch s := src.(type) {
|
|
||||||
case string:
|
|
||||||
t, err := time.Parse("2006-01-02 15:04:05", s)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*dest = NullTime(t)
|
|
||||||
return nil
|
|
||||||
case []uint8:
|
|
||||||
t, err := time.Parse("2006-01-02 15:04:05", string(s))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*dest = NullTime(t)
|
|
||||||
return nil
|
|
||||||
case nil:
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("unsupported driver -> Scan pair: %T -> %T", src, dest)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,151 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// database table
|
|
||||||
type Table struct {
|
|
||||||
Name string
|
|
||||||
Type reflect.Type
|
|
||||||
columnsSeq []string
|
|
||||||
columnsMap map[string][]*Column
|
|
||||||
columns []*Column
|
|
||||||
Indexes map[string]*Index
|
|
||||||
PrimaryKeys []string
|
|
||||||
AutoIncrement string
|
|
||||||
Created map[string]bool
|
|
||||||
Updated string
|
|
||||||
Deleted string
|
|
||||||
Version string
|
|
||||||
Cacher Cacher
|
|
||||||
StoreEngine string
|
|
||||||
Charset string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) Columns() []*Column {
|
|
||||||
return table.columns
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) ColumnsSeq() []string {
|
|
||||||
return table.columnsSeq
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewEmptyTable() *Table {
|
|
||||||
return NewTable("", nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTable(name string, t reflect.Type) *Table {
|
|
||||||
return &Table{Name: name, Type: t,
|
|
||||||
columnsSeq: make([]string, 0),
|
|
||||||
columns: make([]*Column, 0),
|
|
||||||
columnsMap: make(map[string][]*Column),
|
|
||||||
Indexes: make(map[string]*Index),
|
|
||||||
Created: make(map[string]bool),
|
|
||||||
PrimaryKeys: make([]string, 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) columnsByName(name string) []*Column {
|
|
||||||
|
|
||||||
n := len(name)
|
|
||||||
|
|
||||||
for k := range table.columnsMap {
|
|
||||||
if len(k) != n {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if strings.EqualFold(k, name) {
|
|
||||||
return table.columnsMap[k]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) GetColumn(name string) *Column {
|
|
||||||
|
|
||||||
cols := table.columnsByName(name)
|
|
||||||
|
|
||||||
if cols != nil {
|
|
||||||
return cols[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) GetColumnIdx(name string, idx int) *Column {
|
|
||||||
|
|
||||||
cols := table.columnsByName(name)
|
|
||||||
|
|
||||||
if cols != nil && idx < len(cols) {
|
|
||||||
return cols[idx]
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// if has primary key, return column
|
|
||||||
func (table *Table) PKColumns() []*Column {
|
|
||||||
columns := make([]*Column, len(table.PrimaryKeys))
|
|
||||||
for i, name := range table.PrimaryKeys {
|
|
||||||
columns[i] = table.GetColumn(name)
|
|
||||||
}
|
|
||||||
return columns
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) ColumnType(name string) reflect.Type {
|
|
||||||
t, _ := table.Type.FieldByName(name)
|
|
||||||
return t.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) AutoIncrColumn() *Column {
|
|
||||||
return table.GetColumn(table.AutoIncrement)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) VersionColumn() *Column {
|
|
||||||
return table.GetColumn(table.Version)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) UpdatedColumn() *Column {
|
|
||||||
return table.GetColumn(table.Updated)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *Table) DeletedColumn() *Column {
|
|
||||||
return table.GetColumn(table.Deleted)
|
|
||||||
}
|
|
||||||
|
|
||||||
// add a column to table
|
|
||||||
func (table *Table) AddColumn(col *Column) {
|
|
||||||
table.columnsSeq = append(table.columnsSeq, col.Name)
|
|
||||||
table.columns = append(table.columns, col)
|
|
||||||
colName := strings.ToLower(col.Name)
|
|
||||||
if c, ok := table.columnsMap[colName]; ok {
|
|
||||||
table.columnsMap[colName] = append(c, col)
|
|
||||||
} else {
|
|
||||||
table.columnsMap[colName] = []*Column{col}
|
|
||||||
}
|
|
||||||
|
|
||||||
if col.IsPrimaryKey {
|
|
||||||
table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
|
|
||||||
}
|
|
||||||
if col.IsAutoIncrement {
|
|
||||||
table.AutoIncrement = col.Name
|
|
||||||
}
|
|
||||||
if col.IsCreated {
|
|
||||||
table.Created[col.Name] = true
|
|
||||||
}
|
|
||||||
if col.IsUpdated {
|
|
||||||
table.Updated = col.Name
|
|
||||||
}
|
|
||||||
if col.IsDeleted {
|
|
||||||
table.Deleted = col.Name
|
|
||||||
}
|
|
||||||
if col.IsVersion {
|
|
||||||
table.Version = col.Name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// add an index or an unique to table
|
|
||||||
func (table *Table) AddIndex(index *Index) {
|
|
||||||
table.Indexes[index.Name] = index
|
|
||||||
}
|
|
|
@ -1,107 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
var testsGetColumn = []struct {
|
|
||||||
name string
|
|
||||||
idx int
|
|
||||||
}{
|
|
||||||
{"Id", 0},
|
|
||||||
{"Deleted", 0},
|
|
||||||
{"Caption", 0},
|
|
||||||
{"Code_1", 0},
|
|
||||||
{"Code_2", 0},
|
|
||||||
{"Code_3", 0},
|
|
||||||
{"Parent_Id", 0},
|
|
||||||
{"Latitude", 0},
|
|
||||||
{"Longitude", 0},
|
|
||||||
}
|
|
||||||
|
|
||||||
var table *Table
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
|
|
||||||
table = NewEmptyTable()
|
|
||||||
|
|
||||||
var name string
|
|
||||||
|
|
||||||
for i := 0; i < len(testsGetColumn); i++ {
|
|
||||||
// as in Table.AddColumn func
|
|
||||||
name = strings.ToLower(testsGetColumn[i].name)
|
|
||||||
|
|
||||||
table.columnsMap[name] = append(table.columnsMap[name], &Column{})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetColumn(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range testsGetColumn {
|
|
||||||
if table.GetColumn(test.name) == nil {
|
|
||||||
t.Error("Column not found!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetColumnIdx(t *testing.T) {
|
|
||||||
|
|
||||||
for _, test := range testsGetColumn {
|
|
||||||
if table.GetColumnIdx(test.name, test.idx) == nil {
|
|
||||||
t.Errorf("Column %s with idx %d not found!", test.name, test.idx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkGetColumnWithToLower(b *testing.B) {
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
for _, test := range testsGetColumn {
|
|
||||||
|
|
||||||
if _, ok := table.columnsMap[strings.ToLower(test.name)]; !ok {
|
|
||||||
b.Errorf("Column not found:%s", test.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkGetColumnIdxWithToLower(b *testing.B) {
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
for _, test := range testsGetColumn {
|
|
||||||
|
|
||||||
if c, ok := table.columnsMap[strings.ToLower(test.name)]; ok {
|
|
||||||
if test.idx < len(c) {
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
b.Errorf("Bad idx in: %s, %d", test.name, test.idx)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
b.Errorf("Column not found: %s, %d", test.name, test.idx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkGetColumn(b *testing.B) {
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
for _, test := range testsGetColumn {
|
|
||||||
if table.GetColumn(test.name) == nil {
|
|
||||||
b.Errorf("Column not found:%s", test.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkGetColumnIdx(b *testing.B) {
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
for _, test := range testsGetColumn {
|
|
||||||
if table.GetColumnIdx(test.name, test.idx) == nil {
|
|
||||||
b.Errorf("Column not found:%s, %d", test.name, test.idx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,304 +0,0 @@
|
||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
POSTGRES = "postgres"
|
|
||||||
SQLITE = "sqlite3"
|
|
||||||
MYSQL = "mysql"
|
|
||||||
MSSQL = "mssql"
|
|
||||||
ORACLE = "oracle"
|
|
||||||
)
|
|
||||||
|
|
||||||
// xorm SQL types
|
|
||||||
type SQLType struct {
|
|
||||||
Name string
|
|
||||||
DefaultLength int
|
|
||||||
DefaultLength2 int
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
UNKNOW_TYPE = iota
|
|
||||||
TEXT_TYPE
|
|
||||||
BLOB_TYPE
|
|
||||||
TIME_TYPE
|
|
||||||
NUMERIC_TYPE
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *SQLType) IsType(st int) bool {
|
|
||||||
if t, ok := SqlTypes[s.Name]; ok && t == st {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLType) IsText() bool {
|
|
||||||
return s.IsType(TEXT_TYPE)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLType) IsBlob() bool {
|
|
||||||
return s.IsType(BLOB_TYPE)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLType) IsTime() bool {
|
|
||||||
return s.IsType(TIME_TYPE)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLType) IsNumeric() bool {
|
|
||||||
return s.IsType(NUMERIC_TYPE)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SQLType) IsJson() bool {
|
|
||||||
return s.Name == Json || s.Name == Jsonb
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
Bit = "BIT"
|
|
||||||
TinyInt = "TINYINT"
|
|
||||||
SmallInt = "SMALLINT"
|
|
||||||
MediumInt = "MEDIUMINT"
|
|
||||||
Int = "INT"
|
|
||||||
Integer = "INTEGER"
|
|
||||||
BigInt = "BIGINT"
|
|
||||||
|
|
||||||
Enum = "ENUM"
|
|
||||||
Set = "SET"
|
|
||||||
|
|
||||||
Char = "CHAR"
|
|
||||||
Varchar = "VARCHAR"
|
|
||||||
NVarchar = "NVARCHAR"
|
|
||||||
TinyText = "TINYTEXT"
|
|
||||||
Text = "TEXT"
|
|
||||||
Clob = "CLOB"
|
|
||||||
MediumText = "MEDIUMTEXT"
|
|
||||||
LongText = "LONGTEXT"
|
|
||||||
Uuid = "UUID"
|
|
||||||
|
|
||||||
Date = "DATE"
|
|
||||||
DateTime = "DATETIME"
|
|
||||||
Time = "TIME"
|
|
||||||
TimeStamp = "TIMESTAMP"
|
|
||||||
TimeStampz = "TIMESTAMPZ"
|
|
||||||
|
|
||||||
Decimal = "DECIMAL"
|
|
||||||
Numeric = "NUMERIC"
|
|
||||||
|
|
||||||
Real = "REAL"
|
|
||||||
Float = "FLOAT"
|
|
||||||
Double = "DOUBLE"
|
|
||||||
|
|
||||||
Binary = "BINARY"
|
|
||||||
VarBinary = "VARBINARY"
|
|
||||||
TinyBlob = "TINYBLOB"
|
|
||||||
Blob = "BLOB"
|
|
||||||
MediumBlob = "MEDIUMBLOB"
|
|
||||||
LongBlob = "LONGBLOB"
|
|
||||||
Bytea = "BYTEA"
|
|
||||||
|
|
||||||
Bool = "BOOL"
|
|
||||||
|
|
||||||
Serial = "SERIAL"
|
|
||||||
BigSerial = "BIGSERIAL"
|
|
||||||
|
|
||||||
Json = "JSON"
|
|
||||||
Jsonb = "JSONB"
|
|
||||||
|
|
||||||
SqlTypes = map[string]int{
|
|
||||||
Bit: NUMERIC_TYPE,
|
|
||||||
TinyInt: NUMERIC_TYPE,
|
|
||||||
SmallInt: NUMERIC_TYPE,
|
|
||||||
MediumInt: NUMERIC_TYPE,
|
|
||||||
Int: NUMERIC_TYPE,
|
|
||||||
Integer: NUMERIC_TYPE,
|
|
||||||
BigInt: NUMERIC_TYPE,
|
|
||||||
|
|
||||||
Enum: TEXT_TYPE,
|
|
||||||
Set: TEXT_TYPE,
|
|
||||||
Json: TEXT_TYPE,
|
|
||||||
Jsonb: TEXT_TYPE,
|
|
||||||
|
|
||||||
Char: TEXT_TYPE,
|
|
||||||
Varchar: TEXT_TYPE,
|
|
||||||
NVarchar: TEXT_TYPE,
|
|
||||||
TinyText: TEXT_TYPE,
|
|
||||||
Text: TEXT_TYPE,
|
|
||||||
MediumText: TEXT_TYPE,
|
|
||||||
LongText: TEXT_TYPE,
|
|
||||||
Uuid: TEXT_TYPE,
|
|
||||||
Clob: TEXT_TYPE,
|
|
||||||
|
|
||||||
Date: TIME_TYPE,
|
|
||||||
DateTime: TIME_TYPE,
|
|
||||||
Time: TIME_TYPE,
|
|
||||||
TimeStamp: TIME_TYPE,
|
|
||||||
TimeStampz: TIME_TYPE,
|
|
||||||
|
|
||||||
Decimal: NUMERIC_TYPE,
|
|
||||||
Numeric: NUMERIC_TYPE,
|
|
||||||
Real: NUMERIC_TYPE,
|
|
||||||
Float: NUMERIC_TYPE,
|
|
||||||
Double: NUMERIC_TYPE,
|
|
||||||
|
|
||||||
Binary: BLOB_TYPE,
|
|
||||||
VarBinary: BLOB_TYPE,
|
|
||||||
|
|
||||||
TinyBlob: BLOB_TYPE,
|
|
||||||
Blob: BLOB_TYPE,
|
|
||||||
MediumBlob: BLOB_TYPE,
|
|
||||||
LongBlob: BLOB_TYPE,
|
|
||||||
Bytea: BLOB_TYPE,
|
|
||||||
|
|
||||||
Bool: NUMERIC_TYPE,
|
|
||||||
|
|
||||||
Serial: NUMERIC_TYPE,
|
|
||||||
BigSerial: NUMERIC_TYPE,
|
|
||||||
}
|
|
||||||
|
|
||||||
intTypes = sort.StringSlice{"*int", "*int16", "*int32", "*int8"}
|
|
||||||
uintTypes = sort.StringSlice{"*uint", "*uint16", "*uint32", "*uint8"}
|
|
||||||
)
|
|
||||||
|
|
||||||
// !nashtsai! treat following var as interal const values, these are used for reflect.TypeOf comparision
|
|
||||||
var (
|
|
||||||
c_EMPTY_STRING string
|
|
||||||
c_BOOL_DEFAULT bool
|
|
||||||
c_BYTE_DEFAULT byte
|
|
||||||
c_COMPLEX64_DEFAULT complex64
|
|
||||||
c_COMPLEX128_DEFAULT complex128
|
|
||||||
c_FLOAT32_DEFAULT float32
|
|
||||||
c_FLOAT64_DEFAULT float64
|
|
||||||
c_INT64_DEFAULT int64
|
|
||||||
c_UINT64_DEFAULT uint64
|
|
||||||
c_INT32_DEFAULT int32
|
|
||||||
c_UINT32_DEFAULT uint32
|
|
||||||
c_INT16_DEFAULT int16
|
|
||||||
c_UINT16_DEFAULT uint16
|
|
||||||
c_INT8_DEFAULT int8
|
|
||||||
c_UINT8_DEFAULT uint8
|
|
||||||
c_INT_DEFAULT int
|
|
||||||
c_UINT_DEFAULT uint
|
|
||||||
c_TIME_DEFAULT time.Time
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
IntType = reflect.TypeOf(c_INT_DEFAULT)
|
|
||||||
Int8Type = reflect.TypeOf(c_INT8_DEFAULT)
|
|
||||||
Int16Type = reflect.TypeOf(c_INT16_DEFAULT)
|
|
||||||
Int32Type = reflect.TypeOf(c_INT32_DEFAULT)
|
|
||||||
Int64Type = reflect.TypeOf(c_INT64_DEFAULT)
|
|
||||||
|
|
||||||
UintType = reflect.TypeOf(c_UINT_DEFAULT)
|
|
||||||
Uint8Type = reflect.TypeOf(c_UINT8_DEFAULT)
|
|
||||||
Uint16Type = reflect.TypeOf(c_UINT16_DEFAULT)
|
|
||||||
Uint32Type = reflect.TypeOf(c_UINT32_DEFAULT)
|
|
||||||
Uint64Type = reflect.TypeOf(c_UINT64_DEFAULT)
|
|
||||||
|
|
||||||
Float32Type = reflect.TypeOf(c_FLOAT32_DEFAULT)
|
|
||||||
Float64Type = reflect.TypeOf(c_FLOAT64_DEFAULT)
|
|
||||||
|
|
||||||
Complex64Type = reflect.TypeOf(c_COMPLEX64_DEFAULT)
|
|
||||||
Complex128Type = reflect.TypeOf(c_COMPLEX128_DEFAULT)
|
|
||||||
|
|
||||||
StringType = reflect.TypeOf(c_EMPTY_STRING)
|
|
||||||
BoolType = reflect.TypeOf(c_BOOL_DEFAULT)
|
|
||||||
ByteType = reflect.TypeOf(c_BYTE_DEFAULT)
|
|
||||||
BytesType = reflect.SliceOf(ByteType)
|
|
||||||
|
|
||||||
TimeType = reflect.TypeOf(c_TIME_DEFAULT)
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
PtrIntType = reflect.PtrTo(IntType)
|
|
||||||
PtrInt8Type = reflect.PtrTo(Int8Type)
|
|
||||||
PtrInt16Type = reflect.PtrTo(Int16Type)
|
|
||||||
PtrInt32Type = reflect.PtrTo(Int32Type)
|
|
||||||
PtrInt64Type = reflect.PtrTo(Int64Type)
|
|
||||||
|
|
||||||
PtrUintType = reflect.PtrTo(UintType)
|
|
||||||
PtrUint8Type = reflect.PtrTo(Uint8Type)
|
|
||||||
PtrUint16Type = reflect.PtrTo(Uint16Type)
|
|
||||||
PtrUint32Type = reflect.PtrTo(Uint32Type)
|
|
||||||
PtrUint64Type = reflect.PtrTo(Uint64Type)
|
|
||||||
|
|
||||||
PtrFloat32Type = reflect.PtrTo(Float32Type)
|
|
||||||
PtrFloat64Type = reflect.PtrTo(Float64Type)
|
|
||||||
|
|
||||||
PtrComplex64Type = reflect.PtrTo(Complex64Type)
|
|
||||||
PtrComplex128Type = reflect.PtrTo(Complex128Type)
|
|
||||||
|
|
||||||
PtrStringType = reflect.PtrTo(StringType)
|
|
||||||
PtrBoolType = reflect.PtrTo(BoolType)
|
|
||||||
PtrByteType = reflect.PtrTo(ByteType)
|
|
||||||
|
|
||||||
PtrTimeType = reflect.PtrTo(TimeType)
|
|
||||||
)
|
|
||||||
|
|
||||||
// Type2SQLType generate SQLType acorrding Go's type
|
|
||||||
func Type2SQLType(t reflect.Type) (st SQLType) {
|
|
||||||
switch k := t.Kind(); k {
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
|
||||||
st = SQLType{Int, 0, 0}
|
|
||||||
case reflect.Int64, reflect.Uint64:
|
|
||||||
st = SQLType{BigInt, 0, 0}
|
|
||||||
case reflect.Float32:
|
|
||||||
st = SQLType{Float, 0, 0}
|
|
||||||
case reflect.Float64:
|
|
||||||
st = SQLType{Double, 0, 0}
|
|
||||||
case reflect.Complex64, reflect.Complex128:
|
|
||||||
st = SQLType{Varchar, 64, 0}
|
|
||||||
case reflect.Array, reflect.Slice, reflect.Map:
|
|
||||||
if t.Elem() == reflect.TypeOf(c_BYTE_DEFAULT) {
|
|
||||||
st = SQLType{Blob, 0, 0}
|
|
||||||
} else {
|
|
||||||
st = SQLType{Text, 0, 0}
|
|
||||||
}
|
|
||||||
case reflect.Bool:
|
|
||||||
st = SQLType{Bool, 0, 0}
|
|
||||||
case reflect.String:
|
|
||||||
st = SQLType{Varchar, 255, 0}
|
|
||||||
case reflect.Struct:
|
|
||||||
if t.ConvertibleTo(TimeType) {
|
|
||||||
st = SQLType{DateTime, 0, 0}
|
|
||||||
} else {
|
|
||||||
// TODO need to handle association struct
|
|
||||||
st = SQLType{Text, 0, 0}
|
|
||||||
}
|
|
||||||
case reflect.Ptr:
|
|
||||||
st = Type2SQLType(t.Elem())
|
|
||||||
default:
|
|
||||||
st = SQLType{Text, 0, 0}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// default sql type change to go types
|
|
||||||
func SQLType2Type(st SQLType) reflect.Type {
|
|
||||||
name := strings.ToUpper(st.Name)
|
|
||||||
switch name {
|
|
||||||
case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, Serial:
|
|
||||||
return reflect.TypeOf(1)
|
|
||||||
case BigInt, BigSerial:
|
|
||||||
return reflect.TypeOf(int64(1))
|
|
||||||
case Float, Real:
|
|
||||||
return reflect.TypeOf(float32(1))
|
|
||||||
case Double:
|
|
||||||
return reflect.TypeOf(float64(1))
|
|
||||||
case Char, Varchar, NVarchar, TinyText, Text, MediumText, LongText, Enum, Set, Uuid, Clob:
|
|
||||||
return reflect.TypeOf("")
|
|
||||||
case TinyBlob, Blob, LongBlob, Bytea, Binary, MediumBlob, VarBinary:
|
|
||||||
return reflect.TypeOf([]byte{})
|
|
||||||
case Bool:
|
|
||||||
return reflect.TypeOf(true)
|
|
||||||
case DateTime, Date, Time, TimeStamp, TimeStampz:
|
|
||||||
return reflect.TypeOf(c_TIME_DEFAULT)
|
|
||||||
case Decimal, Numeric:
|
|
||||||
return reflect.TypeOf("")
|
|
||||||
default:
|
|
||||||
return reflect.TypeOf("")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -14,18 +14,6 @@
|
||||||
"revision": "0e8690a9f2dd2751f86ab1036f1c5705beb0916a",
|
"revision": "0e8690a9f2dd2751f86ab1036f1c5705beb0916a",
|
||||||
"revisionTime": "2013-03-24T15:51:38Z"
|
"revisionTime": "2013-03-24T15:51:38Z"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"checksumSHA1": "5nvKyr87pTK3kklv6cfFEW/6/Ac=",
|
|
||||||
"path": "github.com/go-xorm/builder",
|
|
||||||
"revision": "c6e604e9c7b7461715091e14ad0c242ec44c26e4",
|
|
||||||
"revisionTime": "2017-02-24T04:30:50Z"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"checksumSHA1": "5Hmt4OFn9e52IpMJUeHDWhts2zU=",
|
|
||||||
"path": "github.com/go-xorm/core",
|
|
||||||
"revision": "6c857c278a22be4947aca85b698efc14cc1dde95",
|
|
||||||
"revisionTime": "2017-03-25T14:16:13Z"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"checksumSHA1": "nQ1LgGMfbEff5JsbR80oPpyOAGY=",
|
"checksumSHA1": "nQ1LgGMfbEff5JsbR80oPpyOAGY=",
|
||||||
"path": "github.com/lib/pq",
|
"path": "github.com/lib/pq",
|
||||||
|
|
Loading…
Reference in New Issue