Upgrade xorm to v1.0.0 (#10646)
* Upgrade xorm to v1.0.0 * small nit * Fix tests * Update xorm * Update xorm * fix go.sum * fix test * Fix bug when dump * Fix bug * update xorm to latest * Fix migration test * update xorm to latest * Fix import order * Use xorm tagtokarchuk/v1.17
parent
dcaa5643d7
commit
c61b902538
@ -0,0 +1 @@ |
||||
.idea |
@ -1,33 +0,0 @@ |
||||
--- |
||||
kind: pipeline |
||||
name: go1.12 |
||||
|
||||
steps: |
||||
|
||||
- name: test |
||||
pull: default |
||||
image: golang:1.12 |
||||
commands: |
||||
- go vet |
||||
- "go test -v -race -coverprofile=coverage.txt -covermode=atomic -dbConn=\"root:@tcp(mysql:3306)/core_test?charset=utf8mb4\"" |
||||
environment: |
||||
GO111MODULE: "on" |
||||
GOPROXY: https://goproxy.cn |
||||
when: |
||||
event: |
||||
- push |
||||
- tag |
||||
- pull_request |
||||
|
||||
services: |
||||
- name: mysql |
||||
pull: default |
||||
image: mysql:5.7 |
||||
environment: |
||||
MYSQL_ALLOW_EMPTY_PASSWORD: yes |
||||
MYSQL_DATABASE: core_test |
||||
when: |
||||
event: |
||||
- push |
||||
- tag |
||||
- pull_request |
@ -1 +0,0 @@ |
||||
*.db |
@ -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,118 +0,0 @@ |
||||
Core is a lightweight wrapper of sql.DB. |
||||
|
||||
[![Build Status](https://drone.gitea.com/api/badges/xorm/core/status.svg)](https://drone.gitea.com/xorm/core) |
||||
[![Test Coverage](https://gocover.io/_badge/xorm.io/core)](https://gocover.io/xorm.io/core) |
||||
[![Go Report Card](https://goreportcard.com/badge/code.gitea.io/gitea)](https://goreportcard.com/report/xorm.io/core) |
||||
|
||||
# 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,327 +0,0 @@ |
||||
// Copyright 2019 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 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 |
||||
|
||||
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 |
||||
SetParams(params map[string]string) |
||||
} |
||||
|
||||
func OpenDialect(dialect Dialect) (*DB, error) { |
||||
return Open(dialect.DriverName(), dialect.DataSourceName()) |
||||
} |
||||
|
||||
// Base represents a basic dialect and all real dialects could embed this struct
|
||||
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 { |
||||
quote := db.dialect.Quote |
||||
return fmt.Sprintf("DROP TABLE IF EXISTS %s", quote(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 := fmt.Sprintf( |
||||
"SELECT %v FROM %v.%v WHERE %v = ? AND %v = ? AND %v = ?", |
||||
db.dialect.Quote("COLUMN_NAME"), |
||||
db.dialect.Quote("INFORMATION_SCHEMA"), |
||||
db.dialect.Quote("COLUMNS"), |
||||
db.dialect.Quote("TABLE_SCHEMA"), |
||||
db.dialect.Quote("TABLE_NAME"), |
||||
db.dialect.Quote("COLUMN_NAME"), |
||||
) |
||||
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) |
||||
if b.DriverName() == MYSQL && len(col.Comment) > 0 { |
||||
sql += " COMMENT '" + col.Comment + "'" |
||||
} |
||||
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) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func (b *Base) SetParams(params map[string]string) { |
||||
} |
||||
|
||||
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 registered database dialect
|
||||
func QueryDialect(dbName DbType) Dialect { |
||||
if d, ok := dialects[strings.ToLower(string(dbName))]; ok { |
||||
return d() |
||||
} |
||||
return nil |
||||
} |
@ -1,31 +0,0 @@ |
||||
// Copyright 2019 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 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,93 +0,0 @@ |
||||
// Copyright 2019 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 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 { |
||||
dummy := dialect.Quote("") |
||||
if len(dummy) != 2 { |
||||
return sql |
||||
} |
||||
prefix, suffix := dummy[0], dummy[1] |
||||
raw := []byte(sql) |
||||
for i, cnt := 0, 0; i < len(raw); i = i + 1 { |
||||
if raw[i] == '`' { |
||||
if cnt%2 == 0 { |
||||
raw[i] = prefix |
||||
} else { |
||||
raw[i] = suffix |
||||
} |
||||
cnt++ |
||||
} |
||||
} |
||||
return string(raw) |
||||
} |
||||
|
||||
// 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.Quote(content) |
||||
} |
||||
|
||||
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 convertQuestionMark(sql, prefix string, start int) string { |
||||
var buf strings.Builder |
||||
var beginSingleQuote bool |
||||
var index = start |
||||
for _, c := range sql { |
||||
if !beginSingleQuote && c == '?' { |
||||
buf.WriteString(fmt.Sprintf("%s%v", prefix, index)) |
||||
index++ |
||||
} else { |
||||
if c == '\'' { |
||||
beginSingleQuote = !beginSingleQuote |
||||
} |
||||
buf.WriteRune(c) |
||||
} |
||||
} |
||||
return buf.String() |
||||
} |
||||
|
||||
func (s *SeqFilter) Do(sql string, dialect Dialect, table *Table) string { |
||||
return convertQuestionMark(sql, s.Prefix, s.Start) |
||||
} |
@ -1,8 +0,0 @@ |
||||
module xorm.io/core |
||||
|
||||
require ( |
||||
github.com/go-sql-driver/mysql v1.4.1 |
||||
github.com/mattn/go-sqlite3 v1.10.0 |
||||
github.com/stretchr/testify v1.4.0 |
||||
google.golang.org/appengine v1.6.0 // indirect |
||||
) |
@ -1,20 +0,0 @@ |
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= |
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= |
||||
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= |
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= |
||||
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o= |
||||
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= |
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= |
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= |
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= |
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
||||
google.golang.org/appengine v1.6.0 h1:Tfd7cKwKbFRsI8RMAD3oqqw7JPFRrvFlOsfbgVkjOOw= |
||||
google.golang.org/appengine v1.6.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= |
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= |
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= |
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
@ -1,37 +0,0 @@ |
||||
// Copyright 2019 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 core |
||||
|
||||
// LogLevel defines a log level
|
||||
type LogLevel int |
||||
|
||||
// enumerate all LogLevels
|
||||
const ( |
||||
// !nashtsai! following level also match syslog.Priority value
|
||||
LOG_DEBUG LogLevel = iota |
||||
LOG_INFO |
||||
LOG_WARNING |
||||
LOG_ERR |
||||
LOG_OFF |
||||
LOG_UNKNOWN |
||||
) |
||||
|
||||
// ILogger is a 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 |
||||
} |
@ -0,0 +1,53 @@ |
||||
# The full repository name |
||||
repo: xorm/xorm |
||||
|
||||
# Service type (gitea or github) |
||||
service: gitea |
||||
|
||||
# Base URL for Gitea instance if using gitea service type (optional) |
||||
# Default: https://gitea.com |
||||
base-url: |
||||
|
||||
# Changelog groups and which labeled PRs to add to each group |
||||
groups: |
||||
- |
||||
name: BREAKING |
||||
labels: |
||||
- kind/breaking |
||||
- |
||||
name: FEATURES |
||||
labels: |
||||
- kind/feature |
||||
- |
||||
name: SECURITY |
||||
labels: |
||||
- kind/security |
||||
- |
||||
name: BUGFIXES |
||||
labels: |
||||
- kind/bug |
||||
- |
||||
name: ENHANCEMENTS |
||||
labels: |
||||
- kind/enhancement |
||||
- kind/refactor |
||||
- kind/ui |
||||
- |
||||
name: TESTING |
||||
labels: |
||||
- kind/testing |
||||
- |
||||
name: BUILD |
||||
labels: |
||||
- kind/build |
||||
- kind/lint |
||||
- |
||||
name: DOCS |
||||
labels: |
||||
- kind/docs |
||||
- |
||||
name: MISC |
||||
default: true |
||||
|
||||
# regex indicating which labels to skip for the changelog |
||||
skip-labels: skip-changelog|backport\/.+ |
@ -0,0 +1,25 @@ |
||||
ignoreGeneratedHeader = false |
||||
severity = "warning" |
||||
confidence = 0.8 |
||||
errorCode = 1 |
||||
warningCode = 1 |
||||
|
||||
[rule.blank-imports] |
||||
[rule.context-as-argument] |
||||
[rule.context-keys-type] |
||||
[rule.dot-imports] |
||||
[rule.error-return] |
||||
[rule.error-strings] |
||||
[rule.error-naming] |
||||
[rule.exported] |
||||
[rule.if-return] |
||||
[rule.increment-decrement] |
||||
[rule.var-naming] |
||||
[rule.var-declaration] |
||||
[rule.package-comments] |
||||
[rule.range] |
||||
[rule.receiver-naming] |
||||
[rule.time-naming] |
||||
[rule.unexported-return] |
||||
[rule.indent-error-flow] |
||||
[rule.errorf] |
@ -0,0 +1,173 @@ |
||||
# Changelog |
||||
|
||||
This changelog goes through all the changes that have been made in each release |
||||
without substantial changes to our git log. |
||||
|
||||
## [1.0.0](https://gitea.com/xorm/xorm/pulls?q=&type=all&state=closed&milestone=1242) - 2020-03-22 |
||||
|
||||
* BREAKING |
||||
* Add context for dialects (#1558) |
||||
* Move zero functions to a standalone package (#1548) |
||||
* Merge core package back into the main repository and split into serval sub packages. (#1543) |
||||
* FEATURES |
||||
* Use a new ContextLogger interface to implement logger (#1557) |
||||
* BUGFIXES |
||||
* Fix setschema (#1606) |
||||
* Fix dump/import bug (#1603) |
||||
* Fix pk bug (#1602) |
||||
* Fix master/slave bug (#1601) |
||||
* Fix bug when dump (#1597) |
||||
* Ignore schema when dbtype is not postgres (#1593) |
||||
* Fix table name (#1590) |
||||
* Fix find alias bug (#1581) |
||||
* Fix rows bug (#1576) |
||||
* Fix map with cols (#1575) |
||||
* Fix bug on deleted with join (#1570) |
||||
* Improve quote policy (#1567) |
||||
* Fix break session sql enable feature (#1566) |
||||
* Fix mssql quote (#1535) |
||||
* Fix join table name quote bug (#1534) |
||||
* Fix mssql issue with duplicate columns. (#1225) |
||||
* Fix mysql8.0 sync failed (#808) |
||||
* ENHANCEMENTS |
||||
* Fix batch insert interface slice be panic (#1598) |
||||
* Move some codes to statement sub package (#1574) |
||||
* Remove circle file (#1569) |
||||
* Move statement as a sub package (#1564) |
||||
* Move maptype to tag parser (#1561) |
||||
* Move caches to manager (#1553) |
||||
* Improve code (#1552) |
||||
* Improve some codes (#1551) |
||||
* Improve statement (#1549) |
||||
* Move tag parser related codes as a standalone sub package (#1547) |
||||
* Move reserve words related files into dialects sub package (#1544) |
||||
* Fix `Conversion` method `ToDB() ([]byte, error)` return type is nil (#1296) |
||||
* Check driver.Valuer response, and skip the column if nil (#1167) |
||||
* Add cockroach support and tests (#896) |
||||
* TESTING |
||||
* Improve tests (#1572) |
||||
* BUILD |
||||
* Add changelog file and tool configuration (#1546) |
||||
* DOCS |
||||
* Fix outdate changelog (#1565) |
||||
|
||||
## old changelog |
||||
|
||||
* **v0.6.5** |
||||
* Postgres schema support |
||||
* vgo support |
||||
* Add FindAndCount |
||||
* Database special params support via NewEngineWithParams |
||||
* Some bugs fixed |
||||
|
||||
* **v0.6.4** |
||||
* Automatical Read/Write seperatelly |
||||
* Query/QueryString/QueryInterface and action with Where/And |
||||
* Get support non-struct variables |
||||
* BufferSize on Iterate |
||||
* fix some other bugs. |
||||
|
||||
* **v0.6.3** |
||||
* merge tests to main project |
||||
* add `Exist` function |
||||
* add `SumInt` function |
||||
* Mysql now support read and create column comment. |
||||
* fix time related bugs. |
||||
* fix some other bugs. |
||||
|
||||
* **v0.6.2** |
||||
* refactor tag parse methods |
||||
* add Scan features to Get |
||||
* add QueryString method |
||||
|
||||
* **v0.4.5** |
||||
* many bugs fixed |
||||
* extends support unlimited deep |
||||
* Delete Limit support |
||||
|
||||
* **v0.4.4** |
||||
* ql database expriment support |
||||
* tidb database expriment support |
||||
* sql.NullString and etc. field support |
||||
* select ForUpdate support |
||||
* many bugs fixed |
||||
|
||||
* **v0.4.3** |
||||
* Json column type support |
||||
* oracle expirement support |
||||
* bug fixed |
||||
|
||||
* **v0.4.2** |
||||
* Transaction will auto rollback if not Rollback or Commit be called. |
||||
* Gonic Mapper support |
||||
* bug fixed |
||||
|
||||
* **v0.4.1** |
||||
* deleted tag support for soft delete |
||||
* bug fixed |
||||
|
||||
* **v0.4.0 RC1** |
||||
Changes: |
||||
* moved xorm cmd to [github.com/go-xorm/cmd](github.com/go-xorm/cmd) |
||||
* refactored general DB operation a core lib at [github.com/go-xorm/core](https://github.com/go-xorm/core) |
||||
* moved tests to github.com/go-xorm/tests [github.com/go-xorm/tests](github.com/go-xorm/tests) |
||||
|
||||
Improvements: |
||||
* Prepared statement cache |
||||
* Add Incr API |
||||
* Specify Timezone Location |
||||
|
||||
* **v0.3.2** |
||||
Improvements: |
||||
* Add AllCols & MustCols function |
||||
* Add TableName for custom table name |
||||
|
||||
Bug Fixes: |
||||
* #46 |
||||
* #51 |
||||
* #53 |
||||
* #89 |
||||
* #86 |
||||
* #92 |
||||
|
||||
* **v0.3.1** |
||||
|
||||
Features: |
||||
* Support MSSQL DB via ODBC driver ([github.com/lunny/godbc](https://github.com/lunny/godbc)); |
||||
* Composite Key, using multiple pk xorm tag |
||||
* Added Row() API as alternative to Iterate() API for traversing result set, provide similar usages to sql.Rows type |
||||
* ORM struct allowed declaration of pointer builtin type as members to allow null DB fields |
||||
* Before and After Event processors |
||||
|
||||
Improvements: |
||||
* Allowed int/int32/int64/uint/uint32/uint64/string as Primary Key type |
||||
* Performance improvement for Get()/Find()/Iterate() |
||||
|
||||
|
||||
* **v0.2.3** : Improved documents; Optimistic Locking support; Timestamp with time zone support; Mapper change to tableMapper and columnMapper & added PrefixMapper & SuffixMapper support custom table or column name's prefix and suffix;Insert now return affected, err instead of id, err; Added UseBool & Distinct; |
||||
|
||||
* **v0.2.2** : Postgres drivers now support lib/pq; Added method Iterate for record by record to handler;Added SetMaxConns(go1.2+) support; some bugs fixed. |
||||
|
||||
* **v0.2.1** : Added database reverse tool, now support generate go & c++ codes, see [Xorm Tool README](https://github.com/go-xorm/xorm/blob/master/xorm/README.md); some bug fixed. |
||||
|
||||
* **v0.2.0** : Added Cache supported, select is speeder up 3~5x; Added SameMapper for same name between struct and table; Added Sync method for auto added tables, columns, indexes; |
||||
|
||||
* **v0.1.9** : Added postgres and mymysql supported; Added ` and ? supported on Raw SQL even if postgres; Added Cols, StoreEngine, Charset function, Added many column data type supported, please see [Mapping Rules](#mapping). |
||||
|
||||
* **v0.1.8** : Added union index and union unique supported, please see [Mapping Rules](#mapping). |
||||
|
||||
* **v0.1.7** : Added IConnectPool interface and NoneConnectPool, SysConnectPool, SimpleConnectPool the three implements. You can choose one of them and the default is SysConnectPool. You can customrize your own connection pool. struct Engine added Close method, It should be invoked before system exit. |
||||
|
||||
* **v0.1.6** : Added conversion interface support; added struct derive support; added single mapping support |
||||
|
||||
* **v0.1.5** : Added multi threads support; added Sql() function for struct query; Get function changed return inteface; MakeSession and Create are instead with NewSession and NewEngine. |
||||
|
||||
* **v0.1.4** : Added simple cascade load support; added more data type supports. |
||||
|
||||
* **v0.1.3** : Find function now supports both slice and map; Add Table function for multi tables and temperory tables support |
||||
|
||||
* **v0.1.2** : Insert function now supports both struct and slice pointer parameters, batch inserting and auto transaction |
||||
|
||||
* **v0.1.1** : Add Id, In functions and improved README |
||||
|
||||
* **v0.1.0** : Initial release. |
@ -0,0 +1,214 @@ |
||||
IMPORT := xorm.io/xorm
|
||||
export GO111MODULE=on
|
||||
|
||||
GO ?= go
|
||||
GOFMT ?= gofmt -s
|
||||
TAGS ?=
|
||||
SED_INPLACE := sed -i
|
||||
|
||||
GOFILES := $(shell find . -name "*.go" -type f)
|
||||
|
||||
PACKAGES ?= $(shell GO111MODULE=on $(GO) list ./...)
|
||||
|
||||
TEST_COCKROACH_HOST ?= cockroach:26257
|
||||
TEST_COCKROACH_SCHEMA ?=
|
||||
TEST_COCKROACH_DBNAME ?= xorm_test
|
||||
TEST_COCKROACH_USERNAME ?= postgres
|
||||
TEST_COCKROACH_PASSWORD ?=
|
||||
|
||||
TEST_MSSQL_HOST ?= mssql:1433
|
||||
TEST_MSSQL_DBNAME ?= gitea
|
||||
TEST_MSSQL_USERNAME ?= sa
|
||||
TEST_MSSQL_PASSWORD ?= MwantsaSecurePassword1
|
||||
|
||||
TEST_MYSQL_HOST ?= mysql:3306
|
||||
TEST_MYSQL_CHARSET ?= utf8
|
||||
TEST_MYSQL_DBNAME ?= xorm_test
|
||||
TEST_MYSQL_USERNAME ?= root
|
||||
TEST_MYSQL_PASSWORD ?=
|
||||
|
||||
TEST_PGSQL_HOST ?= pgsql:5432
|
||||
TEST_PGSQL_SCHEMA ?=
|
||||
TEST_PGSQL_DBNAME ?= xorm_test
|
||||
TEST_PGSQL_USERNAME ?= postgres
|
||||
TEST_PGSQL_PASSWORD ?= mysecretpassword
|
||||
|
||||
TEST_TIDB_HOST ?= tidb:4000
|
||||
TEST_TIDB_DBNAME ?= xorm_test
|
||||
TEST_TIDB_USERNAME ?= root
|
||||
TEST_TIDB_PASSWORD ?=
|
||||
|
||||
TEST_CACHE_ENABLE ?= false
|
||||
TEST_QUOTE_POLICY ?= always
|
||||
|
||||
.PHONY: all |
||||
all: build |
||||
|
||||
.PHONY: build |
||||
build: go-check $(GO_SOURCES) |
||||
$(GO) build
|
||||
|
||||
.PHONY: clean |
||||
clean: |
||||
$(GO) clean -i ./...
|
||||
rm -rf *.sql *.log test.db *coverage.out coverage.all
|
||||
|
||||
.PHONY: coverage |
||||
coverage: |
||||
@hash gocovmerge > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/wadey/gocovmerge; \
|
||||
fi
|
||||
gocovmerge $(shell find . -type f -name "coverage.out") > coverage.all;\
|
||||
|
||||
.PHONY: fmt |
||||
fmt: |
||||
$(GOFMT) -w $(GOFILES)
|
||||
|
||||
.PHONY: fmt-check |
||||
fmt-check: |
||||
# get all go files and run go fmt on them
|
||||
@diff=$$($(GOFMT) -d $(GOFILES)); \
|
||||
if [ -n "$$diff" ]; then \
|
||||
echo "Please run 'make fmt' and commit the result:"; \
|
||||
echo "$${diff}"; \
|
||||
exit 1; \
|
||||
fi;
|
||||
|
||||
.PHONY: go-check |
||||
go-check: |
||||
$(eval GO_VERSION := $(shell printf "%03d%03d%03d" $(shell go version | grep -Eo '[0-9]+\.?[0-9]+?\.?[0-9]?\s' | tr '.' ' ');))
|
||||
@if [ "$(GO_VERSION)" -lt "001011000" ]; then \
|
||||
echo "Gitea requires Go 1.11.0 or greater to build. You can get it at https://golang.org/dl/"; \
|
||||
exit 1; \
|
||||
fi
|
||||
|
||||
.PHONY: help |
||||
help: |
||||
@echo "Make Routines:"
|
||||
@echo " - equivalent to \"build\""
|
||||
@echo " - build creates the entire project"
|
||||
@echo " - clean delete integration files and build files but not css and js files"
|
||||
@echo " - fmt format the code"
|
||||
@echo " - lint run code linter revive"
|
||||
@echo " - misspell check if a word is written wrong"
|
||||
@echo " - test run default unit test"
|
||||
@echo " - test-sqlite run unit test for sqlite"
|
||||
@echo " - vet examines Go source code and reports suspicious constructs"
|
||||
|
||||
.PHONY: lint |
||||
lint: revive |
||||
|
||||
.PHONY: revive |
||||
revive: |
||||
@hash revive > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/mgechev/revive; \
|
||||
fi
|
||||
revive -config .revive.toml -exclude=./vendor/... ./... || exit 1
|
||||
|
||||
.PHONY: misspell |
||||
misspell: |
||||
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/client9/misspell/cmd/misspell; \
|
||||
fi
|
||||
misspell -w -i unknwon $(GOFILES)
|
||||
|
||||
.PHONY: misspell-check |
||||
misspell-check: |
||||
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
$(GO) get -u github.com/client9/misspell/cmd/misspell; \
|
||||
fi
|
||||
misspell -error -i unknwon,destory $(GOFILES)
|
||||
|
||||
.PHONY: test |
||||
test: test-sqlite |
||||
|
||||
.PNONY: test-cockroach |
||||
test-cockroach: go-check |
||||
$(GO) test -race -db=postgres -schema='$(TEST_COCKROACH_SCHEMA)' -cache=$(TEST_CACHE_ENABLE) \
|
||||
-conn_str="postgres://$(TEST_COCKROACH_USERNAME):$(TEST_COCKROACH_PASSWORD)@$(TEST_COCKROACH_HOST)/$(TEST_COCKROACH_DBNAME)?sslmode=disable&experimental_serial_normalization=sql_sequence" \
|
||||
-ignore_update_limit=true -coverprofile=cockroach.$(TEST_COCKROACH_SCHEMA).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PHONY: test-cockroach\#%
|
||||
test-cockroach\#%: go-check |
||||
$(GO) test -race -run $* -db=postgres -schema='$(TEST_COCKROACH_SCHEMA)' -cache=$(TEST_CACHE_ENABLE) \
|
||||
-conn_str="postgres://$(TEST_COCKROACH_USERNAME):$(TEST_COCKROACH_PASSWORD)@$(TEST_COCKROACH_HOST)/$(TEST_COCKROACH_DBNAME)?sslmode=disable&experimental_serial_normalization=sql_sequence" \
|
||||
-ignore_update_limit=true -coverprofile=cockroach.$(TEST_COCKROACH_SCHEMA).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PNONY: test-mssql |
||||
test-mssql: go-check |
||||
$(GO) test -v -race -db=mssql -cache=$(TEST_CACHE_ENABLE) -quote=$(TEST_QUOTE_POLICY) \
|
||||
-conn_str="server=$(TEST_MSSQL_HOST);user id=$(TEST_MSSQL_USERNAME);password=$(TEST_MSSQL_PASSWORD);database=$(TEST_MSSQL_DBNAME)" \
|
||||
-coverprofile=mssql.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PNONY: test-mssql\#%
|
||||
test-mssql\#%: go-check |
||||
$(GO) test -v -race -run $* -db=mssql -cache=$(TEST_CACHE_ENABLE) -quote=$(TEST_QUOTE_POLICY) \
|
||||
-conn_str="server=$(TEST_MSSQL_HOST);user id=$(TEST_MSSQL_USERNAME);password=$(TEST_MSSQL_PASSWORD);database=$(TEST_MSSQL_DBNAME)" \
|
||||
-coverprofile=mssql.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PNONY: test-mymysql |
||||
test-mymysql: go-check |
||||
$(GO) test -v -race -db=mymysql -cache=$(TEST_CACHE_ENABLE) -quote=$(TEST_QUOTE_POLICY) \
|
||||
-conn_str="tcp:$(TEST_MYSQL_HOST)*$(TEST_MYSQL_DBNAME)/$(TEST_MYSQL_USERNAME)/$(TEST_MYSQL_PASSWORD)" \
|
||||
-coverprofile=mymysql.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PNONY: test-mymysql\#%
|
||||
test-mymysql\#%: go-check |
||||
$(GO) test -v -race -run $* -db=mymysql -cache=$(TEST_CACHE_ENABLE) -quote=$(TEST_QUOTE_POLICY) \
|
||||
-conn_str="tcp:$(TEST_MYSQL_HOST)*$(TEST_MYSQL_DBNAME)/$(TEST_MYSQL_USERNAME)/$(TEST_MYSQL_PASSWORD)" \
|
||||
-coverprofile=mymysql.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PNONY: test-mysql |
||||
test-mysql: go-check |
||||
$(GO) test -v -race -db=mysql -cache=$(TEST_CACHE_ENABLE) -quote=$(TEST_QUOTE_POLICY) \
|
||||
-conn_str="$(TEST_MYSQL_USERNAME):$(TEST_MYSQL_PASSWORD)@tcp($(TEST_MYSQL_HOST))/$(TEST_MYSQL_DBNAME)?charset=$(TEST_MYSQL_CHARSET)" \
|
||||
-coverprofile=mysql.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PHONY: test-mysql\#%
|
||||
test-mysql\#%: go-check |
||||
$(GO) test -v -race -run $* -db=mysql -cache=$(TEST_CACHE_ENABLE) -quote=$(TEST_QUOTE_POLICY) \
|
||||
-conn_str="$(TEST_MYSQL_USERNAME):$(TEST_MYSQL_PASSWORD)@tcp($(TEST_MYSQL_HOST))/$(TEST_MYSQL_DBNAME)?charset=$(TEST_MYSQL_CHARSET)" \
|
||||
-coverprofile=mysql.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PNONY: test-postgres |
||||
test-postgres: go-check |
||||
$(GO) test -v -race -db=postgres -schema='$(TEST_PGSQL_SCHEMA)' -cache=$(TEST_CACHE_ENABLE) \
|
||||
-conn_str="postgres://$(TEST_PGSQL_USERNAME):$(TEST_PGSQL_PASSWORD)@$(TEST_PGSQL_HOST)/$(TEST_PGSQL_DBNAME)?sslmode=disable" \
|
||||
-quote=$(TEST_QUOTE_POLICY) -coverprofile=postgres.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PHONY: test-postgres\#%
|
||||
test-postgres\#%: go-check |
||||
$(GO) test -v -race -run $* -db=postgres -schema='$(TEST_PGSQL_SCHEMA)' -cache=$(TEST_CACHE_ENABLE) \
|
||||
-conn_str="postgres://$(TEST_PGSQL_USERNAME):$(TEST_PGSQL_PASSWORD)@$(TEST_PGSQL_HOST)/$(TEST_PGSQL_DBNAME)?sslmode=disable" \
|
||||
-quote=$(TEST_QUOTE_POLICY) -coverprofile=postgres.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PHONY: test-sqlite |
||||
test-sqlite: go-check |
||||
$(GO) test -v -race -cache=$(TEST_CACHE_ENABLE) -db=sqlite3 -conn_str="./test.db?cache=shared&mode=rwc" \
|
||||
-quote=$(TEST_QUOTE_POLICY) -coverprofile=sqlite.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PHONY: test-sqlite-schema |
||||
test-sqlite-schema: go-check |
||||
$(GO) test -v -race -schema=xorm -cache=$(TEST_CACHE_ENABLE) -db=sqlite3 -conn_str="./test.db?cache=shared&mode=rwc" \
|
||||
-quote=$(TEST_QUOTE_POLICY) -coverprofile=sqlite.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PHONY: test-sqlite\#%
|
||||
test-sqlite\#%: go-check |
||||
$(GO) test -v -race -run $* -cache=$(TEST_CACHE_ENABLE) -db=sqlite3 -conn_str="./test.db?cache=shared&mode=rwc" \
|
||||
-quote=$(TEST_QUOTE_POLICY) -coverprofile=sqlite.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PNONY: test-tidb |
||||
test-tidb: go-check |
||||
$(GO) test -v -race -db=mysql -cache=$(TEST_CACHE_ENABLE) -ignore_select_update=true \
|
||||
-conn_str="$(TEST_TIDB_USERNAME):$(TEST_TIDB_PASSWORD)@tcp($(TEST_TIDB_HOST))/$(TEST_TIDB_DBNAME)" \
|
||||
-quote=$(TEST_QUOTE_POLICY) -coverprofile=tidb.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PHONY: test-tidb\#%
|
||||
test-tidb\#%: go-check |
||||
$(GO) test -v -race -run $* -db=mysql -cache=$(TEST_CACHE_ENABLE) -ignore_select_update=true \
|
||||
-conn_str="$(TEST_TIDB_USERNAME):$(TEST_TIDB_PASSWORD)@tcp($(TEST_TIDB_HOST))/$(TEST_TIDB_DBNAME)" \
|
||||
-quote=$(TEST_QUOTE_POLICY) -coverprofile=tidb.$(TEST_QUOTE_POLICY).$(TEST_CACHE_ENABLE).coverage.out -covermode=atomic
|
||||
|
||||
.PHONY: vet |
||||
vet: |
||||
$(GO) vet $(PACKAGES)
|
@ -0,0 +1,58 @@ |
||||
// Copyright 2020 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 caches |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/md5" |
||||
"encoding/gob" |
||||
"encoding/json" |
||||
"fmt" |
||||
"io" |
||||
) |
||||
|
||||
// md5 hash string
|
||||
func Md5(str string) string { |
||||
m := md5.New() |
||||
io.WriteString(m, str) |
||||
return fmt.Sprintf("%x", m.Sum(nil)) |
||||
} |
||||
func Encode(data interface{}) ([]byte, error) { |
||||
//return JsonEncode(data)
|
||||
return GobEncode(data) |
||||
} |
||||
|
||||
func Decode(data []byte, to interface{}) error { |
||||
//return JsonDecode(data, to)
|
||||
return GobDecode(data, to) |
||||
} |
||||
|
||||
func GobEncode(data interface{}) ([]byte, error) { |
||||
var buf bytes.Buffer |
||||
enc := gob.NewEncoder(&buf) |
||||
err := enc.Encode(&data) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return buf.Bytes(), nil |
||||
} |
||||
|
||||
func GobDecode(data []byte, to interface{}) error { |
||||
buf := bytes.NewBuffer(data) |
||||
dec := gob.NewDecoder(buf) |
||||
return dec.Decode(to) |
||||
} |
||||
|
||||
func JsonEncode(data interface{}) ([]byte, error) { |
||||
val, err := json.Marshal(data) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return val, nil |
||||
} |
||||
|
||||
func JsonDecode(data []byte, to interface{}) error { |
||||
return json.Unmarshal(data, to) |
||||
} |
@ -0,0 +1,94 @@ |
||||
// Copyright 2020 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 caches |
||||
|
||||
import ( |
||||
"log" |
||||
|
||||
"github.com/syndtr/goleveldb/leveldb" |
||||
) |
||||
|
||||
// LevelDBStore implements CacheStore provide local machine
|
||||
type LevelDBStore struct { |
||||
store *leveldb.DB |
||||
Debug bool |
||||
v interface{} |
||||
} |
||||
|
||||
var _ CacheStore = &LevelDBStore{} |
||||
|
||||
func NewLevelDBStore(dbfile string) (*LevelDBStore, error) { |
||||
db := &LevelDBStore{} |
||||
h, err := leveldb.OpenFile(dbfile, nil) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
db.store = h |
||||
return db, nil |
||||
} |
||||
|
||||
func (s *LevelDBStore) Put(key string, value interface{}) error { |
||||
val, err := Encode(value) |
||||
if err != nil { |
||||
if s.Debug { |
||||
log.Println("[LevelDB]EncodeErr: ", err, "Key:", key) |
||||
} |
||||
return err |
||||
} |
||||
err = s.store.Put([]byte(key), val, nil) |
||||
if err != nil { |
||||
if s.Debug { |
||||
log.Println("[LevelDB]PutErr: ", err, "Key:", key) |
||||
} |
||||
return err |
||||
} |
||||
if s.Debug { |
||||
log.Println("[LevelDB]Put: ", key) |
||||
} |
||||
return err |
||||
} |
||||
|
||||
func (s *LevelDBStore) Get(key string) (interface{}, error) { |
||||
data, err := s.store.Get([]byte(key), nil) |
||||
if err != nil { |
||||
if s.Debug { |
||||
log.Println("[LevelDB]GetErr: ", err, "Key:", key) |
||||
} |
||||
if err == leveldb.ErrNotFound { |
||||
return nil, ErrNotExist |
||||
} |
||||
return nil, err |
||||
} |
||||
|
||||
err = Decode(data, &s.v) |
||||
if err != nil { |
||||
if s.Debug { |
||||
log.Println("[LevelDB]DecodeErr: ", err, "Key:", key) |
||||
} |
||||
return nil, err |
||||
} |
||||
if s.Debug { |
||||
log.Println("[LevelDB]Get: ", key, s.v) |
||||
} |
||||
return s.v, err |
||||
} |
||||
|
||||
func (s *LevelDBStore) Del(key string) error { |
||||
err := s.store.Delete([]byte(key), nil) |
||||
if err != nil { |
||||
if s.Debug { |
||||
log.Println("[LevelDB]DelErr: ", err, "Key:", key) |
||||
} |
||||
return err |
||||
} |
||||
if s.Debug { |
||||
log.Println("[LevelDB]Del: ", key) |
||||
} |
||||
return err |
||||
} |
||||
|
||||
func (s *LevelDBStore) Close() { |
||||
s.store.Close() |
||||
} |
@ -0,0 +1,56 @@ |
||||
// Copyright 2020 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 caches |
||||
|
||||
import "sync" |
||||
|
||||
type Manager struct { |
||||
cacher Cacher |
||||
disableGlobalCache bool |
||||
|
||||
cachers map[string]Cacher |
||||
cacherLock sync.RWMutex |
||||
} |
||||
|
||||
func NewManager() *Manager { |
||||
return &Manager{ |
||||
cachers: make(map[string]Cacher), |
||||
} |
||||
} |
||||
|
||||
// SetDisableGlobalCache disable global cache or not
|
||||
func (mgr *Manager) SetDisableGlobalCache(disable bool) { |
||||
if mgr.disableGlobalCache != disable { |
||||
mgr.disableGlobalCache = disable |
||||
} |
||||
} |
||||
|
||||
func (mgr *Manager) SetCacher(tableName string, cacher Cacher) { |
||||
mgr.cacherLock.Lock() |
||||
mgr.cachers[tableName] = cacher |
||||
mgr.cacherLock.Unlock() |
||||
} |
||||
|
||||
func (mgr *Manager) GetCacher(tableName string) Cacher { |
||||
var cacher Cacher |
||||
var ok bool |
||||
mgr.cacherLock.RLock() |
||||
cacher, ok = mgr.cachers[tableName] |
||||
mgr.cacherLock.RUnlock() |
||||
if !ok && !mgr.disableGlobalCache { |
||||
cacher = mgr.cacher |
||||
} |
||||
return cacher |
||||
} |
||||
|
||||
// SetDefaultCacher set the default cacher. Xorm's default not enable cacher.
|
||||
func (mgr *Manager) SetDefaultCacher(cacher Cacher) { |
||||
mgr.cacher = cacher |
||||
} |
||||
|
||||
// GetDefaultCacher returns the default cacher
|
||||
func (mgr *Manager) GetDefaultCacher() Cacher { |
||||
return mgr.cacher |
||||
} |
2
vendor/xorm.io/xorm/context_cache.go → vendor/xorm.io/xorm/contexts/context_cache.go
generated
vendored
2
vendor/xorm.io/xorm/context_cache.go → vendor/xorm.io/xorm/contexts/context_cache.go
generated
vendored
@ -1,8 +1,8 @@ |
||||
// Copyright 2019 The Xorm Authors. All rights reserved.
|
||||
// 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 core |
||||
package convert |
||||
|
||||
// Conversion is an interface. A type implements Conversion will according
|
||||
// the custom method to fill into database and retrieve from database.
|
@ -0,0 +1,278 @@ |
||||
// Copyright 2019 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 dialects |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"strings" |
||||
"time" |
||||
|
||||
"xorm.io/xorm/core" |
||||
"xorm.io/xorm/schemas" |
||||
) |
||||
|
||||
// URI represents an uri to visit database
|
||||
type URI struct { |
||||
DBType schemas.DBType |
||||
Proto string |
||||
Host string |
||||
Port string |
||||
DBName string |
||||
User string |
||||
Passwd string |
||||
Charset string |
||||
Laddr string |
||||
Raddr string |
||||
Timeout time.Duration |
||||
Schema string |
||||
} |
||||
|
||||
// SetSchema set schema
|
||||
func (uri *URI) SetSchema(schema string) { |
||||
if uri.DBType == schemas.POSTGRES { |
||||
uri.Schema = schema |
||||
} |
||||
} |
||||
|
||||
// Dialect represents a kind of database
|
||||
type Dialect interface { |
||||
Init(*core.DB, *URI) error |
||||
URI() *URI |
||||
DB() *core.DB |
||||
SQLType(*schemas.Column) string |
||||
FormatBytes(b []byte) string |
||||
DefaultSchema() string |
||||
|
||||
IsReserved(string) bool |
||||
Quoter() schemas.Quoter |
||||
SetQuotePolicy(quotePolicy QuotePolicy) |
||||
|
||||
AutoIncrStr() string |
||||
|
||||
GetIndexes(ctx context.Context, tableName string) (map[string]*schemas.Index, error) |
||||
IndexCheckSQL(tableName, idxName string) (string, []interface{}) |
||||
CreateIndexSQL(tableName string, index *schemas.Index) string |
||||
DropIndexSQL(tableName string, index *schemas.Index) string |
||||
|
||||
GetTables(ctx context.Context) ([]*schemas.Table, error) |
||||
IsTableExist(ctx context.Context, tableName string) (bool, error) |
||||
CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool) |
||||
DropTableSQL(tableName string) (string, bool) |
||||
|
||||
GetColumns(ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) |
||||
IsColumnExist(ctx context.Context, tableName string, colName string) (bool, error) |
||||
AddColumnSQL(tableName string, col *schemas.Column) string |
||||
ModifyColumnSQL(tableName string, col *schemas.Column) string |
||||
|
||||
ForUpdateSQL(query string) string |
||||
|
||||
Filters() []Filter |
||||
SetParams(params map[string]string) |
||||
} |
||||
|
||||
// Base represents a basic dialect and all real dialects could embed this struct
|
||||
type Base struct { |
||||
db *core.DB |
||||
dialect Dialect |
||||
uri *URI |
||||
quoter schemas.Quoter |
||||
} |
||||
|
||||
func (b *Base) Quoter() schemas.Quoter { |
||||
return b.quoter |
||||
} |
||||
|
||||
func (b *Base) DB() *core.DB { |
||||
return b.db |
||||
} |
||||
|
||||
func (b *Base) DefaultSchema() string { |
||||
return "" |
||||
} |
||||
|
||||
func (b *Base) Init(db *core.DB, dialect Dialect, uri *URI) error { |
||||
b.db, b.dialect, b.uri = db, dialect, uri |
||||
return nil |
||||
} |
||||
|
||||
func (b *Base) URI() *URI { |
||||
return b.uri |
||||
} |
||||
|
||||
func (b *Base) DBType() schemas.DBType { |
||||
return b.uri.DBType |
||||
} |
||||
|
||||
// String generate column description string according dialect
|
||||
func (b *Base) String(col *schemas.Column) string { |
||||
sql := b.dialect.Quoter().Quote(col.Name) + " " |
||||
|
||||
sql += b.dialect.SQLType(col) + " " |
||||
|
||||
if col.IsPrimaryKey { |
||||
sql += "PRIMARY KEY " |
||||
if col.IsAutoIncrement { |
||||
sql += b.dialect.AutoIncrStr() + " " |
||||
} |
||||
} |
||||
|
||||
if col.Default != "" { |
||||
sql += "DEFAULT " + col.Default + " " |
||||
} |
||||
|
||||
if col.Nullable { |
||||
sql += "NULL " |
||||
} else { |
||||
sql += "NOT NULL " |
||||
} |
||||
|
||||
return sql |
||||
} |
||||
|
||||
// StringNoPk generate column description string according dialect without primary keys
|
||||
func (b *Base) StringNoPk(col *schemas.Column) string { |
||||
sql := b.dialect.Quoter().Quote(col.Name) + " " |
||||
|
||||
sql += b.dialect.SQLType(col) + " " |
||||
|
||||
if col.Default != "" { |
||||
sql += "DEFAULT " + col.Default + " " |
||||
} |
||||
|
||||
if col.Nullable { |
||||
sql += "NULL " |
||||
} else { |
||||
sql += "NOT NULL " |
||||
} |
||||
|
||||
return sql |
||||
} |
||||
|
||||
func (b *Base) FormatBytes(bs []byte) string { |
||||
return fmt.Sprintf("0x%x", bs) |
||||
} |
||||
|
||||
func (db *Base) DropTableSQL(tableName string) (string, bool) { |
||||
quote := db.dialect.Quoter().Quote |
||||
return fmt.Sprintf("DROP TABLE IF EXISTS %s", quote(tableName)), true |
||||
} |
||||
|
||||
func (db *Base) HasRecords(ctx context.Context, query string, args ...interface{}) (bool, error) { |
||||
rows, err := db.DB().QueryContext(ctx, query, args...) |
||||
if err != nil { |
||||
return false, err |
||||
} |
||||
defer rows.Close() |
||||
|
||||
if rows.Next() { |
||||
return true, nil |
||||
} |
||||
return false, nil |
||||
} |
||||
|
||||
func (db *Base) IsColumnExist(ctx context.Context, tableName, colName string) (bool, error) { |
||||
quote := db.dialect.Quoter().Quote |
||||
query := fmt.Sprintf( |
||||
"SELECT %v FROM %v.%v WHERE %v = ? AND %v = ? AND %v = ?", |
||||
quote("COLUMN_NAME"), |
||||
quote("INFORMATION_SCHEMA"), |
||||
quote("COLUMNS"), |
||||
quote("TABLE_SCHEMA"), |
||||
quote("TABLE_NAME"), |
||||
quote("COLUMN_NAME"), |
||||
) |
||||
return db.HasRecords(ctx, query, db.uri.DBName, tableName, colName) |
||||
} |
||||
|
||||
func (db *Base) AddColumnSQL(tableName string, col *schemas.Column) string { |
||||
return fmt.Sprintf("ALTER TABLE %v ADD %v", db.dialect.Quoter().Quote(tableName), |
||||
db.String(col)) |
||||
} |
||||
|
||||
func (db *Base) CreateIndexSQL(tableName string, index *schemas.Index) string { |
||||
quoter := db.dialect.Quoter() |
||||
var unique string |
||||
var idxName string |
||||
if index.Type == schemas.UniqueType { |
||||
unique = " UNIQUE" |
||||
} |
||||
idxName = index.XName(tableName) |
||||
return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v)", unique, |
||||
quoter.Quote(idxName), quoter.Quote(tableName), |
||||
quoter.Join(index.Cols, ",")) |
||||
} |
||||
|
||||
func (db *Base) DropIndexSQL(tableName string, index *schemas.Index) string { |
||||
quote := db.dialect.Quoter().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 *schemas.Column) string { |
||||
return fmt.Sprintf("alter table %s MODIFY COLUMN %s", tableName, db.StringNoPk(col)) |
||||
} |
||||
|
||||
func (b *Base) ForUpdateSQL(query string) string { |
||||
return query + " FOR UPDATE" |
||||
} |
||||
|
||||
func (b *Base) SetParams(params map[string]string) { |
||||
} |
||||
|
||||
var ( |
||||
dialects = map[string]func() Dialect{} |
||||
) |
||||
|
||||
// RegisterDialect register database dialect
|
||||
func RegisterDialect(dbName schemas.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 registered database dialect
|
||||
func QueryDialect(dbName schemas.DBType) Dialect { |
||||
if d, ok := dialects[strings.ToLower(string(dbName))]; ok { |
||||
return d() |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func regDrvsNDialects() bool { |
||||
providedDrvsNDialects := map[string]struct { |
||||
dbType schemas.DBType |
||||
getDriver func() Driver |
||||
getDialect func() Dialect |
||||
}{ |
||||
"mssql": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }}, |
||||
"odbc": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }}, // !nashtsai! TODO change this when supporting MS Access
|
||||
"mysql": {"mysql", func() Driver { return &mysqlDriver{} }, func() Dialect { return &mysql{} }}, |
||||
"mymysql": {"mysql", func() Driver { return &mymysqlDriver{} }, func() Dialect { return &mysql{} }}, |
||||
"postgres": {"postgres", func() Driver { return &pqDriver{} }, func() Dialect { return &postgres{} }}, |
||||
"pgx": {"postgres", func() Driver { return &pqDriverPgx{} }, func() Dialect { return &postgres{} }}, |
||||
"sqlite3": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }}, |
||||
"oci8": {"oracle", func() Driver { return &oci8Driver{} }, func() Dialect { return &oracle{} }}, |
||||
"goracle": {"oracle", func() Driver { return &goracleDriver{} }, func() Dialect { return &oracle{} }}, |
||||
} |
||||
|
||||
for driverName, v := range providedDrvsNDialects { |
||||
if driver := QueryDriver(driverName); driver == nil { |
||||
RegisterDriver(driverName, v.getDriver()) |
||||
RegisterDialect(v.dbType, v.getDialect) |
||||
} |
||||
} |
||||
return true |
||||
} |
||||
|
||||
func init() { |
||||
regDrvsNDialects() |
||||
} |
@ -0,0 +1,63 @@ |
||||
// Copyright 2019 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 dialects |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"xorm.io/xorm/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) |
||||
} |
||||
|
||||
// OpenDialect opens a dialect via driver name and connection string
|
||||
func OpenDialect(driverName, connstr string) (Dialect, error) { |
||||
driver := QueryDriver(driverName) |
||||
if driver == nil { |
||||
return nil, fmt.Errorf("Unsupported driver name: %v", driverName) |
||||
} |
||||
|
||||
uri, err := driver.Parse(driverName, connstr) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
dialect := QueryDialect(uri.DBType) |
||||
if dialect == nil { |
||||
return nil, fmt.Errorf("Unsupported dialect type: %v", uri.DBType) |
||||
} |
||||
|
||||
db, err := core.Open(driverName, connstr) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
dialect.Init(db, uri) |
||||
|
||||
return dialect, nil |
||||
} |
@ -0,0 +1,43 @@ |
||||
// Copyright 2019 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 dialects |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
) |
||||
|
||||
// Filter is an interface to filter SQL
|
||||
type Filter interface { |
||||
Do(sql string) string |
||||
} |
||||
|
||||
// SeqFilter filter SQL replace ?, ? ... to $1, $2 ...
|
||||
type SeqFilter struct { |
||||
Prefix string |
||||
Start int |
||||
} |
||||
|
||||
func convertQuestionMark(sql, prefix string, start int) string { |
||||
var buf strings.Builder |
||||
var beginSingleQuote bool |
||||
var index = start |
||||
for _, c := range sql { |
||||
if !beginSingleQuote && c == '?' { |
||||
buf.WriteString(fmt.Sprintf("%s%v", prefix, index)) |
||||
index++ |
||||
} else { |
||||
if c == '\'' { |
||||
beginSingleQuote = !beginSingleQuote |
||||
} |
||||
buf.WriteRune(c) |
||||
} |
||||
} |
||||
return buf.String() |
||||
} |
||||
|
||||
func (s *SeqFilter) Do(sql string) string { |
||||
return convertQuestionMark(sql, s.Prefix, s.Start) |
||||
} |
0
vendor/xorm.io/xorm/gen_reserved.sh → vendor/xorm.io/xorm/dialects/gen_reserved.sh
generated
vendored
0
vendor/xorm.io/xorm/gen_reserved.sh → vendor/xorm.io/xorm/dialects/gen_reserved.sh
generated
vendored
227
vendor/xorm.io/xorm/dialect_oracle.go → vendor/xorm.io/xorm/dialects/oracle.go
generated
vendored
227
vendor/xorm.io/xorm/dialect_oracle.go → vendor/xorm.io/xorm/dialects/oracle.go
generated
vendored
0
vendor/xorm.io/xorm/pg_reserved.txt → vendor/xorm.io/xorm/dialects/pg_reserved.txt
generated
vendored
0
vendor/xorm.io/xorm/pg_reserved.txt → vendor/xorm.io/xorm/dialects/pg_reserved.txt
generated
vendored
338
vendor/xorm.io/xorm/dialect_postgres.go → vendor/xorm.io/xorm/dialects/postgres.go
generated
vendored
338
vendor/xorm.io/xorm/dialect_postgres.go → vendor/xorm.io/xorm/dialects/postgres.go
generated
vendored
@ -0,0 +1,15 @@ |
||||
// Copyright 2020 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 dialects |
||||
|
||||
// QuotePolicy describes quote handle policy
|
||||
type QuotePolicy int |
||||
|
||||
// All QuotePolicies
|
||||
const ( |
||||
QuotePolicyAlways QuotePolicy = iota |
||||
QuotePolicyNone |
||||
QuotePolicyReserved |
||||
) |
218
vendor/xorm.io/xorm/dialect_sqlite3.go → vendor/xorm.io/xorm/dialects/sqlite3.go
generated
vendored
218
vendor/xorm.io/xorm/dialect_sqlite3.go → vendor/xorm.io/xorm/dialects/sqlite3.go
generated
vendored
@ -0,0 +1,90 @@ |
||||
// Copyright 2015 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 dialects |
||||
|
||||
import ( |
||||
"fmt" |
||||
"reflect" |
||||
"strings" |
||||
|
||||
"xorm.io/xorm/internal/utils" |
||||
"xorm.io/xorm/names" |
||||
) |
||||
|
||||
// TableNameWithSchema will add schema prefix on table name if possible
|
||||
func TableNameWithSchema(dialect Dialect, tableName string) string { |
||||
// Add schema name as prefix of table name.
|
||||
// Only for postgres database.
|
||||
if dialect.URI().Schema != "" && |
||||
dialect.URI().Schema != dialect.DefaultSchema() && |
||||
strings.Index(tableName, ".") == -1 { |
||||
return fmt.Sprintf("%s.%s", dialect.URI().Schema, tableName) |
||||
} |
||||
return tableName |
||||
} |
||||
|
||||
// TableNameNoSchema returns table name with given tableName
|
||||
func TableNameNoSchema(dialect Dialect, mapper names.Mapper, tableName interface{}) string { |
||||
quote := dialect.Quoter().Quote |
||||
switch tableName.(type) { |
||||
case []string: |
||||
t := tableName.([]string) |
||||
if len(t) > 1 { |
||||
return fmt.Sprintf("%v AS %v", quote(t[0]), quote(t[1])) |
||||
} else if len(t) == 1 { |
||||
return quote(t[0]) |
||||
} |
||||
case []interface{}: |
||||
t := tableName.([]interface{}) |
||||
l := len(t) |
||||
var table string |
||||
if l > 0 { |
||||
f := t[0] |
||||
switch f.(type) { |
||||
case string: |
||||
table = f.(string) |
||||
case names.TableName: |
||||
table = f.(names.TableName).TableName() |
||||
default: |
||||
v := utils.ReflectValue(f) |
||||
t := v.Type() |
||||
if t.Kind() == reflect.Struct { |
||||
table = names.GetTableName(mapper, v) |
||||
} else { |
||||
table = quote(fmt.Sprintf("%v", f)) |
||||
} |
||||
} |
||||
} |
||||
if l > 1 { |
||||
return fmt.Sprintf("%v AS %v", quote(table), quote(fmt.Sprintf("%v", t[1]))) |
||||
} else if l == 1 { |
||||
return quote(table) |
||||
} |
||||
case names.TableName: |
||||
return tableName.(names.TableName).TableName() |
||||
case string: |
||||
return tableName.(string) |
||||
case reflect.Value: |
||||
v := tableName.(reflect.Value) |
||||
return names.GetTableName(mapper, v) |
||||
default: |
||||
v := utils.ReflectValue(tableName) |
||||
t := v.Type() |
||||
if t.Kind() == reflect.Struct { |
||||
return names.GetTableName(mapper, v) |
||||
} |
||||
return quote(fmt.Sprintf("%v", tableName)) |
||||
} |
||||
return "" |
||||
} |
||||
|
||||
// FullTableName returns table name with quote and schema according parameter
|
||||
func FullTableName(dialect Dialect, mapper names.Mapper, bean interface{}, includeSchema ...bool) string { |
||||
tbName := TableNameNoSchema(dialect, mapper, bean) |
||||
if len(includeSchema) > 0 && includeSchema[0] && !utils.IsSubQuery(tbName) { |
||||
tbName = TableNameWithSchema(dialect, tbName) |
||||
} |
||||
return tbName |
||||
} |
@ -0,0 +1,49 @@ |
||||
// Copyright 2015 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 dialects |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
"xorm.io/xorm/schemas" |
||||
) |
||||
|
||||
// FormatTime format time as column type
|
||||
func FormatTime(dialect Dialect, sqlTypeName string, t time.Time) (v interface{}) { |
||||
switch sqlTypeName { |
||||
case schemas.Time: |
||||
s := t.Format("2006-01-02 15:04:05") // time.RFC3339
|
||||
v = s[11:19] |
||||
case schemas.Date: |
||||
v = t.Format("2006-01-02") |
||||
case schemas.DateTime, schemas.TimeStamp, schemas.Varchar: // !DarthPestilane! format time when sqlTypeName is schemas.Varchar.
|
||||
v = t.Format("2006-01-02 15:04:05") |
||||
case schemas.TimeStampz: |
||||
if dialect.URI().DBType == schemas.MSSQL { |
||||
v = t.Format("2006-01-02T15:04:05.9999999Z07:00") |
||||
} else { |
||||
v = t.Format(time.RFC3339Nano) |
||||
} |
||||
case schemas.BigInt, schemas.Int: |
||||
v = t.Unix() |
||||
default: |
||||
v = t |
||||
} |
||||
return |
||||
} |
||||
|
||||
func FormatColumnTime(dialect Dialect, defaultTimeZone *time.Location, col *schemas.Column, t time.Time) (v interface{}) { |
||||
if t.IsZero() { |
||||
if col.Nullable { |
||||
return nil |
||||
} |
||||
return "" |
||||
} |
||||
|
||||
if col.TimeZone != nil { |
||||
return FormatTime(dialect, col.SQLType.Name, t.In(col.TimeZone)) |
||||
} |
||||
return FormatTime(dialect, col.SQLType.Name, t.In(defaultTimeZone)) |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,232 +0,0 @@ |
||||
// 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 ( |
||||
"database/sql/driver" |
||||
"fmt" |
||||
"reflect" |
||||
"strings" |
||||
"time" |
||||
|
||||
"xorm.io/builder" |
||||
"xorm.io/core" |
||||
) |
||||
|
||||
func (engine *Engine) buildConds(table *core.Table, bean interface{}, |
||||
includeVersion bool, includeUpdated bool, includeNil bool, |
||||
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool, |
||||
mustColumnMap map[string]bool, tableName, aliasName string, addedTableName bool) (builder.Cond, error) { |
||||
var conds []builder.Cond |
||||
for _, col := range table.Columns() { |
||||
if !includeVersion && col.IsVersion { |
||||
continue |
||||
} |
||||
if !includeUpdated && col.IsUpdated { |
||||
continue |
||||
} |
||||
if !includeAutoIncr && col.IsAutoIncrement { |
||||
continue |
||||
} |
||||
|
||||
if engine.dialect.DBType() == core.MSSQL && (col.SQLType.Name == core.Text || col.SQLType.IsBlob() || col.SQLType.Name == core.TimeStampz) { |
||||
continue |
||||
} |
||||
if col.SQLType.IsJson() { |
||||
continue |
||||
} |
||||
|
||||
var colName string |
||||
if addedTableName { |
||||
var nm = tableName |
||||
if len(aliasName) > 0 { |
||||
nm = aliasName |
||||
} |
||||
colName = engine.Quote(nm) + "." + engine.Quote(col.Name) |
||||
} else { |
||||
colName = engine.Quote(col.Name) |
||||
} |
||||
|
||||
fieldValuePtr, err := col.ValueOf(bean) |
||||
if err != nil { |
||||
if !strings.Contains(err.Error(), "is not valid") { |
||||
engine.logger.Warn(err) |
||||
} |
||||
continue |
||||
} |
||||
|
||||
if col.IsDeleted && !unscoped { // tag "deleted" is enabled
|
||||
conds = append(conds, engine.CondDeleted(colName)) |
||||
} |
||||
|
||||
fieldValue := *fieldValuePtr |
||||
if fieldValue.Interface() == nil { |
||||
continue |
||||
} |
||||
|
||||
fieldType := reflect.TypeOf(fieldValue.Interface()) |
||||
requiredField := useAllCols |
||||
|
||||
if b, ok := getFlagForColumn(mustColumnMap, col); ok { |
||||
if b { |
||||
requiredField = true |
||||
} else { |
||||
continue |
||||
} |
||||
} |
||||
|
||||
if fieldType.Kind() == reflect.Ptr { |
||||
if fieldValue.IsNil() { |
||||
if includeNil { |
||||
conds = append(conds, builder.Eq{colName: nil}) |
||||
} |
||||
continue |
||||
} else if !fieldValue.IsValid() { |
||||
continue |
||||
} else { |
||||
// dereference ptr type to instance type
|
||||
fieldValue = fieldValue.Elem() |
||||
fieldType = reflect.TypeOf(fieldValue.Interface()) |
||||
requiredField = true |
||||
} |
||||
} |
||||
|
||||
var val interface{} |
||||
switch fieldType.Kind() { |
||||
case reflect.Bool: |
||||
if allUseBool || requiredField { |
||||
val = fieldValue.Interface() |
||||
} else { |
||||
// if a bool in a struct, it will not be as a condition because it default is false,
|
||||
// please use Where() instead
|
||||
continue |
||||
} |
||||
case reflect.String: |
||||
if !requiredField && fieldValue.String() == "" { |
||||
continue |
||||
} |
||||
// for MyString, should convert to string or panic
|
||||
if fieldType.String() != reflect.String.String() { |
||||
val = fieldValue.String() |
||||
} else { |
||||
val = fieldValue.Interface() |
||||
} |
||||
case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64: |
||||
if !requiredField && fieldValue.Int() == 0 { |
||||
continue |
||||
} |
||||
val = fieldValue.Interface() |
||||
case reflect.Float32, reflect.Float64: |
||||
if !requiredField && fieldValue.Float() == 0.0 { |
||||
continue |
||||
} |
||||
val = fieldValue.Interface() |
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64: |
||||
if !requiredField && fieldValue.Uint() == 0 { |
||||
continue |
||||
} |
||||
t := int64(fieldValue.Uint()) |
||||
val = reflect.ValueOf(&t).Interface() |
||||
case reflect.Struct: |
||||
if fieldType.ConvertibleTo(core.TimeType) { |
||||
t := fieldValue.Convert(core.TimeType).Interface().(time.Time) |
||||
if !requiredField && (t.IsZero() || !fieldValue.IsValid()) { |
||||
continue |
||||
} |
||||
val = engine.formatColTime(col, t) |
||||
} else if _, ok := reflect.New(fieldType).Interface().(core.Conversion); ok { |
||||
continue |
||||
} else if valNul, ok := fieldValue.Interface().(driver.Valuer); ok { |
||||
val, _ = valNul.Value() |
||||
if val == nil { |
||||
continue |
||||
} |
||||
} else { |
||||
if col.SQLType.IsJson() { |
||||
if col.SQLType.IsText() { |
||||
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
engine.logger.Error(err) |
||||
continue |
||||
} |
||||
val = string(bytes) |
||||
} else if col.SQLType.IsBlob() { |
||||
var bytes []byte |
||||
var err error |
||||
bytes, err = DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
engine.logger.Error(err) |
||||
continue |
||||
} |
||||
val = bytes |
||||
} |
||||
} else { |
||||
engine.autoMapType(fieldValue) |
||||
if table, ok := engine.Tables[fieldValue.Type()]; ok { |
||||
if len(table.PrimaryKeys) == 1 { |
||||
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) |
||||
// fix non-int pk issues
|
||||
//if pkField.Int() != 0 {
|
||||
if pkField.IsValid() && !isZero(pkField.Interface()) { |
||||
val = pkField.Interface() |
||||
} else { |
||||
continue |
||||
} |
||||
} else { |
||||
//TODO: how to handler?
|
||||
return nil, fmt.Errorf("not supported %v as %v", fieldValue.Interface(), table.PrimaryKeys) |
||||
} |
||||
} else { |
||||
val = fieldValue.Interface() |
||||
} |
||||
} |
||||
} |
||||
case reflect.Array: |
||||
continue |
||||
case reflect.Slice, reflect.Map: |
||||
if fieldValue == reflect.Zero(fieldType) { |
||||
continue |
||||
} |
||||
if fieldValue.IsNil() || !fieldValue.IsValid() || fieldValue.Len() == 0 { |
||||
continue |
||||
} |
||||
|
||||
if col.SQLType.IsText() { |
||||
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
engine.logger.Error(err) |
||||
continue |
||||
} |
||||
val = string(bytes) |
||||
} else if col.SQLType.IsBlob() { |
||||
var bytes []byte |
||||
var err error |
||||
if (fieldType.Kind() == reflect.Array || fieldType.Kind() == reflect.Slice) && |
||||
fieldType.Elem().Kind() == reflect.Uint8 { |
||||
if fieldValue.Len() > 0 { |
||||
val = fieldValue.Bytes() |
||||
} else { |
||||
continue |
||||
} |
||||
} else { |
||||
bytes, err = DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
engine.logger.Error(err) |
||||
continue |
||||
} |
||||
val = bytes |
||||
} |
||||
} else { |
||||
continue |
||||
} |
||||
default: |
||||
val = fieldValue.Interface() |
||||
} |
||||
|
||||
conds = append(conds, builder.Eq{colName: val}) |
||||
} |
||||
|
||||
return builder.And(conds...), nil |
||||
} |
@ -1,28 +0,0 @@ |
||||
// Copyright 2019 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.
|
||||
|
||||
// +build go1.8
|
||||
|
||||
package xorm |
||||
|
||||
import "context" |
||||
|
||||
// Context creates a session with the context
|
||||
func (engine *Engine) Context(ctx context.Context) *Session { |
||||
session := engine.NewSession() |
||||
session.isAutoClose = true |
||||
return session.Context(ctx) |
||||
} |
||||
|
||||
// SetDefaultContext set the default context
|
||||
func (engine *Engine) SetDefaultContext(ctx context.Context) { |
||||
engine.defaultContext = ctx |
||||
} |
||||
|
||||
// PingContext tests if database is alive
|
||||
func (engine *Engine) PingContext(ctx context.Context) error { |
||||
session := engine.NewSession() |
||||
defer session.Close() |
||||
return session.PingContext(ctx) |
||||
} |
@ -1,113 +0,0 @@ |
||||
// Copyright 2018 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 ( |
||||
"fmt" |
||||
"reflect" |
||||
"strings" |
||||
|
||||
"xorm.io/core" |
||||
) |
||||
|
||||
// tbNameWithSchema will automatically add schema prefix on table name
|
||||
func (engine *Engine) tbNameWithSchema(v string) string { |
||||
// Add schema name as prefix of table name.
|
||||
// Only for postgres database.
|
||||
if engine.dialect.DBType() == core.POSTGRES && |
||||
engine.dialect.URI().Schema != "" && |
||||
engine.dialect.URI().Schema != postgresPublicSchema && |
||||
strings.Index(v, ".") == -1 { |
||||
return engine.dialect.URI().Schema + "." + v |
||||
} |
||||
return v |
||||
} |
||||
|
||||
// TableName returns table name with schema prefix if has
|
||||
func (engine *Engine) TableName(bean interface{}, includeSchema ...bool) string { |
||||
tbName := engine.tbNameNoSchema(bean) |
||||
if len(includeSchema) > 0 && includeSchema[0] { |
||||
tbName = engine.tbNameWithSchema(tbName) |
||||
} |
||||
|
||||
return tbName |
||||
} |
||||
|
||||
// tbName get some table's table name
|
||||
func (session *Session) tbNameNoSchema(table *core.Table) string { |
||||
if len(session.statement.AltTableName) > 0 { |
||||
return session.statement.AltTableName |
||||
} |
||||
|
||||
return table.Name |
||||
} |
||||
|
||||
func (engine *Engine) tbNameForMap(v reflect.Value) string { |
||||
if v.Type().Implements(tpTableName) { |
||||
return v.Interface().(TableName).TableName() |
||||
} |
||||
if v.Kind() == reflect.Ptr { |
||||
v = v.Elem() |
||||
if v.Type().Implements(tpTableName) { |
||||
return v.Interface().(TableName).TableName() |
||||
} |
||||
} |
||||
|
||||
return engine.TableMapper.Obj2Table(v.Type().Name()) |
||||
} |
||||
|
||||
func (engine *Engine) tbNameNoSchema(tablename interface{}) string { |
||||
switch tablename.(type) { |
||||
case []string: |
||||
t := tablename.([]string) |
||||
if len(t) > 1 { |
||||
return fmt.Sprintf("%v AS %v", engine.Quote(t[0]), engine.Quote(t[1])) |
||||
} else if len(t) == 1 { |
||||
return engine.Quote(t[0]) |
||||
} |
||||
case []interface{}: |
||||
t := tablename.([]interface{}) |
||||
l := len(t) |
||||
var table string |
||||
if l > 0 { |
||||
f := t[0] |
||||
switch f.(type) { |
||||
case string: |
||||
table = f.(string) |
||||
case TableName: |
||||
table = f.(TableName).TableName() |
||||
default: |
||||
v := rValue(f) |
||||
t := v.Type() |
||||
if t.Kind() == reflect.Struct { |
||||
table = engine.tbNameForMap(v) |
||||
} else { |
||||
table = engine.Quote(fmt.Sprintf("%v", f)) |
||||
} |
||||
} |
||||
} |
||||
if l > 1 { |
||||
return fmt.Sprintf("%v AS %v", engine.Quote(table), |
||||
engine.Quote(fmt.Sprintf("%v", t[1]))) |
||||
} else if l == 1 { |
||||
return engine.Quote(table) |
||||
} |
||||
case TableName: |
||||
return tablename.(TableName).TableName() |
||||
case string: |
||||
return tablename.(string) |
||||
case reflect.Value: |
||||
v := tablename.(reflect.Value) |
||||
return engine.tbNameForMap(v) |
||||
default: |
||||
v := rValue(tablename) |
||||
t := v.Type() |
||||
if t.Kind() == reflect.Struct { |
||||
return engine.tbNameForMap(v) |
||||
} |
||||
return engine.Quote(fmt.Sprintf("%v", tablename)) |
||||
} |
||||
return "" |
||||
} |
@ -1,332 +0,0 @@ |
||||
// Copyright 2015 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 ( |
||||
"errors" |
||||
"fmt" |
||||
"reflect" |
||||
"sort" |
||||
"strconv" |
||||
"strings" |
||||
|
||||
"xorm.io/core" |
||||
) |
||||
|
||||
// str2PK convert string value to primary key value according to tp
|
||||
func str2PKValue(s string, tp reflect.Type) (reflect.Value, error) { |
||||
var err error |
||||
var result interface{} |
||||
var defReturn = reflect.Zero(tp) |
||||
|
||||
switch tp.Kind() { |
||||
case reflect.Int: |
||||
result, err = strconv.Atoi(s) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as int: %s", s, err.Error()) |
||||
} |
||||
case reflect.Int8: |
||||
x, err := strconv.Atoi(s) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as int8: %s", s, err.Error()) |
||||
} |
||||
result = int8(x) |
||||
case reflect.Int16: |
||||
x, err := strconv.Atoi(s) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as int16: %s", s, err.Error()) |
||||
} |
||||
result = int16(x) |
||||
case reflect.Int32: |
||||
x, err := strconv.Atoi(s) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as int32: %s", s, err.Error()) |
||||
} |
||||
result = int32(x) |
||||
case reflect.Int64: |
||||
result, err = strconv.ParseInt(s, 10, 64) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as int64: %s", s, err.Error()) |
||||
} |
||||
case reflect.Uint: |
||||
x, err := strconv.ParseUint(s, 10, 64) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as uint: %s", s, err.Error()) |
||||
} |
||||
result = uint(x) |
||||
case reflect.Uint8: |
||||
x, err := strconv.ParseUint(s, 10, 64) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as uint8: %s", s, err.Error()) |
||||
} |
||||
result = uint8(x) |
||||
case reflect.Uint16: |
||||
x, err := strconv.ParseUint(s, 10, 64) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as uint16: %s", s, err.Error()) |
||||
} |
||||
result = uint16(x) |
||||
case reflect.Uint32: |
||||
x, err := strconv.ParseUint(s, 10, 64) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as uint32: %s", s, err.Error()) |
||||
} |
||||
result = uint32(x) |
||||
case reflect.Uint64: |
||||
result, err = strconv.ParseUint(s, 10, 64) |
||||
if err != nil { |
||||
return defReturn, fmt.Errorf("convert %s as uint64: %s", s, err.Error()) |
||||
} |
||||
case reflect.String: |
||||
result = s |
||||
default: |
||||
return defReturn, errors.New("unsupported convert type") |
||||
} |
||||
return reflect.ValueOf(result).Convert(tp), nil |
||||
} |
||||
|
||||
func str2PK(s string, tp reflect.Type) (interface{}, error) { |
||||
v, err := str2PKValue(s, tp) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return v.Interface(), nil |
||||
} |
||||
|
||||
func splitTag(tag string) (tags []string) { |
||||
tag = strings.TrimSpace(tag) |
||||
var hasQuote = false |
||||
var lastIdx = 0 |
||||
for i, t := range tag { |
||||
if t == '\'' { |
||||
hasQuote = !hasQuote |
||||
} else if t == ' ' { |
||||
if lastIdx < i && !hasQuote { |
||||
tags = append(tags, strings.TrimSpace(tag[lastIdx:i])) |
||||
lastIdx = i + 1 |
||||
} |
||||
} |
||||
} |
||||
if lastIdx < len(tag) { |
||||
tags = append(tags, strings.TrimSpace(tag[lastIdx:])) |
||||
} |
||||
return |
||||
} |
||||
|
||||
type zeroable interface { |
||||
IsZero() bool |
||||
} |
||||
|
||||
func isZero(k interface{}) bool { |
||||
switch k.(type) { |
||||
case int: |
||||
return k.(int) == 0 |
||||
case int8: |
||||
return k.(int8) == 0 |
||||
case int16: |
||||
return k.(int16) == 0 |
||||
case int32: |
||||
return k.(int32) == 0 |
||||
case int64: |
||||
return k.(int64) == 0 |
||||
case uint: |
||||
return k.(uint) == 0 |
||||
case uint8: |
||||
return k.(uint8) == 0 |
||||
case uint16: |
||||
return k.(uint16) == 0 |
||||
case uint32: |
||||
return k.(uint32) == 0 |
||||
case uint64: |
||||
return k.(uint64) == 0 |
||||
case float32: |
||||
return k.(float32) == 0 |
||||
case float64: |
||||
return k.(float64) == 0 |
||||
case bool: |
||||
return k.(bool) == false |
||||
case string: |
||||
return k.(string) == "" |
||||
case zeroable: |
||||
return k.(zeroable).IsZero() |
||||
} |
||||
return false |
||||
} |
||||
|
||||
func isStructZero(v reflect.Value) bool { |
||||
if !v.IsValid() { |
||||
return true |
||||
} |
||||
|
||||
for i := 0; i < v.NumField(); i++ { |
||||
field := v.Field(i) |
||||
switch field.Kind() { |
||||
case reflect.Ptr: |
||||
field = field.Elem() |
||||
fallthrough |
||||
case reflect.Struct: |
||||
if !isStructZero(field) { |
||||
return false |
||||
} |
||||
default: |
||||
if field.CanInterface() && !isZero(field.Interface()) { |
||||
return false |
||||
} |
||||
} |
||||
} |
||||
return true |
||||
} |
||||
|
||||
func isArrayValueZero(v reflect.Value) bool { |
||||
if !v.IsValid() || v.Len() == 0 { |
||||
return true |
||||
} |
||||
|
||||
for i := 0; i < v.Len(); i++ { |
||||
if !isZero(v.Index(i).Interface()) { |
||||
return false |
||||
} |
||||
} |
||||
|
||||
return true |
||||
} |
||||
|
||||
func int64ToIntValue(id int64, tp reflect.Type) reflect.Value { |
||||
var v interface{} |
||||
kind := tp.Kind() |
||||
|
||||
if kind == reflect.Ptr { |
||||
kind = tp.Elem().Kind() |
||||
} |
||||
|
||||
switch kind { |
||||
case reflect.Int16: |
||||
temp := int16(id) |
||||
v = &temp |
||||
case reflect.Int32: |
||||
temp := int32(id) |
||||
v = &temp |
||||
case reflect.Int: |
||||
temp := int(id) |
||||
v = &temp |
||||
case reflect.Int64: |
||||
temp := id |
||||
v = &temp |
||||
case reflect.Uint16: |
||||
temp := uint16(id) |
||||
v = &temp |
||||
case reflect.Uint32: |
||||
temp := uint32(id) |
||||
v = &temp |
||||
case reflect.Uint64: |
||||
temp := uint64(id) |
||||
v = &temp |
||||
case reflect.Uint: |
||||
temp := uint(id) |
||||
v = &temp |
||||
} |
||||
|
||||
if tp.Kind() == reflect.Ptr { |
||||
return reflect.ValueOf(v).Convert(tp) |
||||
} |
||||
return reflect.ValueOf(v).Elem().Convert(tp) |
||||
} |
||||
|
||||
func int64ToInt(id int64, tp reflect.Type) interface{} { |
||||
return int64ToIntValue(id, tp).Interface() |
||||
} |
||||
|
||||
func isPKZero(pk core.PK) bool { |
||||
for _, k := range pk { |
||||
if isZero(k) { |
||||
return true |
||||
} |
||||
} |
||||
return false |
||||
} |
||||
|
||||
func indexNoCase(s, sep string) int { |
||||
return strings.Index(strings.ToLower(s), strings.ToLower(sep)) |
||||
} |
||||
|
||||
func splitNoCase(s, sep string) []string { |
||||
idx := indexNoCase(s, sep) |
||||
if idx < 0 { |
||||
return []string{s} |
||||
} |
||||
return strings.Split(s, s[idx:idx+len(sep)]) |
||||
} |
||||
|
||||
func splitNNoCase(s, sep string, n int) []string { |
||||
idx := indexNoCase(s, sep) |
||||
if idx < 0 { |
||||
return []string{s} |
||||
} |
||||
return strings.SplitN(s, s[idx:idx+len(sep)], n) |
||||
} |
||||
|
||||
func makeArray(elem string, count int) []string { |
||||
res := make([]string, count) |
||||
for i := 0; i < count; i++ { |
||||
res[i] = elem |
||||
} |
||||
return res |
||||
} |
||||
|
||||
func rValue(bean interface{}) reflect.Value { |
||||
return reflect.Indirect(reflect.ValueOf(bean)) |
||||
} |
||||
|
||||
func rType(bean interface{}) reflect.Type { |
||||
sliceValue := reflect.Indirect(reflect.ValueOf(bean)) |
||||
// return reflect.TypeOf(sliceValue.Interface())
|
||||
return sliceValue.Type() |
||||
} |
||||
|
||||
func structName(v reflect.Type) string { |
||||
for v.Kind() == reflect.Ptr { |
||||
v = v.Elem() |
||||
} |
||||
return v.Name() |
||||
} |
||||
|
||||
func sliceEq(left, right []string) bool { |
||||
if len(left) != len(right) { |
||||
return false |
||||
} |
||||
sort.Sort(sort.StringSlice(left)) |
||||
sort.Sort(sort.StringSlice(right)) |
||||
for i := 0; i < len(left); i++ { |
||||
if left[i] != right[i] { |
||||
return false |
||||
} |
||||
} |
||||
return true |
||||
} |
||||
|
||||
func indexName(tableName, idxName string) string { |
||||
return fmt.Sprintf("IDX_%v_%v", tableName, idxName) |
||||
} |
||||
|
||||
func eraseAny(value string, strToErase ...string) string { |
||||
if len(strToErase) == 0 { |
||||
return value |
||||
} |
||||
var replaceSeq []string |
||||
for _, s := range strToErase { |
||||
replaceSeq = append(replaceSeq, s, "") |
||||
} |
||||
|
||||
replacer := strings.NewReplacer(replaceSeq...) |
||||
|
||||
return replacer.Replace(value) |
||||
} |
||||
|
||||
func quoteColumns(cols []string, quoteFunc func(string) string, sep string) string { |
||||
for i := range cols { |
||||
cols[i] = quoteFunc(cols[i]) |
||||
} |
||||
return strings.Join(cols, sep+" ") |
||||
} |
@ -1,21 +0,0 @@ |
||||
// 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 "time" |
||||
|
||||
const ( |
||||
zeroTime0 = "0000-00-00 00:00:00" |
||||
zeroTime1 = "0001-01-01 00:00:00" |
||||
) |
||||
|
||||
func formatTime(t time.Time) string { |
||||
return t.Format("2006-01-02 15:04:05") |
||||
} |
||||
|
||||
func isTimeZero(t time.Time) bool { |
||||
return t.IsZero() || formatTime(t) == zeroTime0 || |
||||
formatTime(t) == zeroTime1 |
||||
} |
@ -0,0 +1,79 @@ |
||||
// Copyright 2019 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 statements |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
|
||||
"xorm.io/xorm/internal/utils" |
||||
"xorm.io/xorm/schemas" |
||||
) |
||||
|
||||
func (statement *Statement) ConvertIDSQL(sqlStr string) string { |
||||
if statement.RefTable != nil { |
||||
cols := statement.RefTable.PKColumns() |
||||
if len(cols) == 0 { |
||||
return "" |
||||
} |
||||
|
||||
colstrs := statement.joinColumns(cols, false) |
||||
sqls := utils.SplitNNoCase(sqlStr, " from ", 2) |
||||
if len(sqls) != 2 { |
||||
return "" |
||||
} |
||||
|
||||
var top string |
||||
pLimitN := statement.LimitN |
||||
if pLimitN != nil && statement.dialect.URI().DBType == schemas.MSSQL { |
||||
top = fmt.Sprintf("TOP %d ", *pLimitN) |
||||
} |
||||
|
||||
newsql := fmt.Sprintf("SELECT %s%s FROM %v", top, colstrs, sqls[1]) |
||||
return newsql |
||||
} |
||||
return "" |
||||
} |
||||
|
||||
func (statement *Statement) ConvertUpdateSQL(sqlStr string) (string, string) { |
||||
if statement.RefTable == nil || len(statement.RefTable.PrimaryKeys) != 1 { |
||||
return "", "" |
||||
} |
||||
|
||||
colstrs := statement.joinColumns(statement.RefTable.PKColumns(), true) |
||||
sqls := utils.SplitNNoCase(sqlStr, "where", 2) |
||||
if len(sqls) != 2 { |
||||
if len(sqls) == 1 { |
||||
return sqls[0], fmt.Sprintf("SELECT %v FROM %v", |
||||
colstrs, statement.quote(statement.TableName())) |
||||
} |
||||
return "", "" |
||||
} |
||||
|
||||
var whereStr = sqls[1] |
||||
|
||||
// TODO: for postgres only, if any other database?
|
||||
var paraStr string |
||||
if statement.dialect.URI().DBType == schemas.POSTGRES { |
||||
paraStr = "$" |
||||
} else if statement.dialect.URI().DBType == schemas.MSSQL { |
||||
paraStr = ":" |
||||
} |
||||
|
||||
if paraStr != "" { |
||||
if strings.Contains(sqls[1], paraStr) { |
||||
dollers := strings.Split(sqls[1], paraStr) |
||||
whereStr = dollers[0] |
||||
for i, c := range dollers[1:] { |
||||
ccs := strings.SplitN(c, " ", 2) |
||||
whereStr += fmt.Sprintf(paraStr+"%v %v", i+1, ccs[1]) |
||||
} |
||||
} |
||||
} |
||||
|
||||
return sqls[0], fmt.Sprintf("SELECT %v FROM %v WHERE %v", |
||||
colstrs, statement.quote(statement.TableName()), |
||||
whereStr) |
||||
} |
@ -0,0 +1,66 @@ |
||||
// Copyright 2019 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 statements |
||||
|
||||
import ( |
||||
"strings" |
||||
|
||||
"xorm.io/xorm/schemas" |
||||
) |
||||
|
||||
type columnMap []string |
||||
|
||||
func (m columnMap) Contain(colName string) bool { |
||||
if len(m) == 0 { |
||||
return false |
||||
} |
||||
|
||||
n := len(colName) |
||||
for _, mk := range m { |
||||
if len(mk) != n { |
||||
continue |
||||
} |
||||
if strings.EqualFold(mk, colName) { |
||||
return true |
||||
} |
||||
} |
||||
|
||||
return false |
||||
} |
||||
|
||||
func (m columnMap) Len() int { |
||||
return len(m) |
||||
} |
||||
|
||||
func (m columnMap) IsEmpty() bool { |
||||
return len(m) == 0 |
||||
} |
||||
|
||||
func (m *columnMap) Add(colName string) bool { |
||||
if m.Contain(colName) { |
||||
return false |
||||
} |
||||
*m = append(*m, colName) |
||||
return true |
||||
} |
||||
|
||||
func getFlagForColumn(m map[string]bool, col *schemas.Column) (val bool, has bool) { |
||||
if len(m) == 0 { |
||||
return false, false |
||||
} |
||||
|
||||
n := len(col.Name) |
||||
|
||||
for mk := range m { |
||||
if len(mk) != n { |
||||
continue |
||||
} |
||||
if strings.EqualFold(mk, col.Name) { |
||||
return m[mk], true |
||||
} |
||||
} |
||||
|
||||
return false, false |
||||
} |
@ -0,0 +1,143 @@ |
||||
// Copyright 2020 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 statements |
||||
|
||||
import ( |
||||
"strings" |
||||
|
||||
"xorm.io/builder" |
||||
"xorm.io/xorm/schemas" |
||||
) |
||||
|
||||
func (statement *Statement) writeInsertOutput(buf *strings.Builder, table *schemas.Table) error { |
||||
if statement.dialect.URI().DBType == schemas.MSSQL && len(table.AutoIncrement) > 0 { |
||||
if _, err := buf.WriteString(" OUTPUT Inserted."); err != nil { |
||||
return err |
||||
} |
||||
if _, err := buf.WriteString(table.AutoIncrement); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (statement *Statement) GenInsertSQL(colNames []string, args []interface{}) (string, []interface{}, error) { |
||||
var ( |
||||
table = statement.RefTable |
||||
tableName = statement.TableName() |
||||
exprs = statement.ExprColumns |
||||
colPlaces = strings.Repeat("?, ", len(colNames)) |
||||
) |
||||
if exprs.Len() <= 0 && len(colPlaces) > 0 { |
||||
colPlaces = colPlaces[0 : len(colPlaces)-2] |
||||
} |
||||
|
||||
var buf = builder.NewWriter() |
||||
if _, err := buf.WriteString("INSERT INTO "); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if err := statement.dialect.Quoter().QuoteTo(buf.Builder, tableName); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if len(colPlaces) <= 0 { |
||||
if statement.dialect.URI().DBType == schemas.MYSQL { |
||||
if _, err := buf.WriteString(" VALUES ()"); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} else { |
||||
if err := statement.writeInsertOutput(buf.Builder, table); err != nil { |
||||
return "", nil, err |
||||
} |
||||
if _, err := buf.WriteString(" DEFAULT VALUES"); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} |
||||
} else { |
||||
if _, err := buf.WriteString(" ("); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if err := statement.dialect.Quoter().JoinWrite(buf.Builder, append(colNames, exprs.ColNames...), ","); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if statement.Conds().IsValid() { |
||||
if _, err := buf.WriteString(")"); err != nil { |
||||
return "", nil, err |
||||
} |
||||
if err := statement.writeInsertOutput(buf.Builder, table); err != nil { |
||||
return "", nil, err |
||||
} |
||||
if _, err := buf.WriteString(" SELECT "); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if err := statement.WriteArgs(buf, args); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if len(exprs.Args) > 0 { |
||||
if _, err := buf.WriteString(","); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} |
||||
if err := exprs.WriteArgs(buf); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if _, err := buf.WriteString(" FROM "); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if err := statement.dialect.Quoter().QuoteTo(buf.Builder, tableName); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if _, err := buf.WriteString(" WHERE "); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if err := statement.Conds().WriteTo(buf); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} else { |
||||
buf.Append(args...) |
||||
|
||||
if _, err := buf.WriteString(")"); err != nil { |
||||
return "", nil, err |
||||
} |
||||
if err := statement.writeInsertOutput(buf.Builder, table); err != nil { |
||||
return "", nil, err |
||||
} |
||||
if _, err := buf.WriteString(" VALUES ("); err != nil { |
||||
return "", nil, err |
||||
} |
||||
if _, err := buf.WriteString(colPlaces); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if err := exprs.WriteArgs(buf); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if _, err := buf.WriteString(")"); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} |
||||
} |
||||
|
||||
if len(table.AutoIncrement) > 0 && statement.dialect.URI().DBType == schemas.POSTGRES { |
||||
if _, err := buf.WriteString(" RETURNING "); err != nil { |
||||
return "", nil, err |
||||
} |
||||
if err := statement.dialect.Quoter().QuoteTo(buf.Builder, table.AutoIncrement); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} |
||||
|
||||
return buf.String(), buf.Args(), nil |
||||
} |
@ -0,0 +1,79 @@ |
||||
// 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 statements |
||||
|
||||
import ( |
||||
"fmt" |
||||
"reflect" |
||||
|
||||
"xorm.io/builder" |
||||
"xorm.io/xorm/schemas" |
||||
) |
||||
|
||||
var ( |
||||
ptrPkType = reflect.TypeOf(&schemas.PK{}) |
||||
pkType = reflect.TypeOf(schemas.PK{}) |
||||
stringType = reflect.TypeOf("") |
||||
intType = reflect.TypeOf(int64(0)) |
||||
uintType = reflect.TypeOf(uint64(0)) |
||||
) |
||||
|
||||
// ID generate "where id = ? " statement or for composite key "where key1 = ? and key2 = ?"
|
||||
func (statement *Statement) ID(id interface{}) *Statement { |
||||
switch t := id.(type) { |
||||
case *schemas.PK: |
||||
statement.idParam = *t |
||||
case schemas.PK: |
||||
statement.idParam = t |
||||
case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: |
||||
statement.idParam = schemas.PK{id} |
||||
default: |
||||
idValue := reflect.ValueOf(id) |
||||
idType := idValue.Type() |
||||
|
||||
switch idType.Kind() { |
||||
case reflect.String: |
||||
statement.idParam = schemas.PK{idValue.Convert(stringType).Interface()} |
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
||||
statement.idParam = schemas.PK{idValue.Convert(intType).Interface()} |
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
||||
statement.idParam = schemas.PK{idValue.Convert(uintType).Interface()} |
||||
case reflect.Slice: |
||||
if idType.ConvertibleTo(pkType) { |
||||
statement.idParam = idValue.Convert(pkType).Interface().(schemas.PK) |
||||
} |
||||
case reflect.Ptr: |
||||
if idType.ConvertibleTo(ptrPkType) { |
||||
statement.idParam = idValue.Convert(ptrPkType).Elem().Interface().(schemas.PK) |
||||
} |
||||
} |
||||
} |
||||
|
||||
if statement.idParam == nil { |
||||
statement.LastError = fmt.Errorf("ID param %#v is not supported", id) |
||||
} |
||||
|
||||
return statement |
||||
} |
||||
|
||||
func (statement *Statement) ProcessIDParam() error { |
||||
if statement.idParam == nil || statement.RefTable == nil { |
||||
return nil |
||||
} |
||||
|
||||
if len(statement.RefTable.PrimaryKeys) != len(statement.idParam) { |
||||
fmt.Println("=====", statement.RefTable.PrimaryKeys, statement.idParam) |
||||
return fmt.Errorf("ID condition is error, expect %d primarykeys, there are %d", |
||||
len(statement.RefTable.PrimaryKeys), |
||||
len(statement.idParam), |
||||
) |
||||
} |
||||
|
||||
for i, col := range statement.RefTable.PKColumns() { |
||||
var colName = statement.colName(col, statement.TableName()) |
||||
statement.cond = statement.cond.And(builder.Eq{colName: statement.idParam[i]}) |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,441 @@ |
||||
// Copyright 2019 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 statements |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
"reflect" |
||||
"strings" |
||||
|
||||
"xorm.io/builder" |
||||
"xorm.io/xorm/schemas" |
||||
) |
||||
|
||||
func (statement *Statement) GenQuerySQL(sqlOrArgs ...interface{}) (string, []interface{}, error) { |
||||
if len(sqlOrArgs) > 0 { |
||||
return statement.ConvertSQLOrArgs(sqlOrArgs...) |
||||
} |
||||
|
||||
if statement.RawSQL != "" { |
||||
return statement.GenRawSQL(), statement.RawParams, nil |
||||
} |
||||
|
||||
if len(statement.TableName()) <= 0 { |
||||
return "", nil, ErrTableNotFound |
||||
} |
||||
|
||||
var columnStr = statement.ColumnStr() |
||||
if len(statement.SelectStr) > 0 { |
||||
columnStr = statement.SelectStr |
||||
} else { |
||||
if statement.JoinStr == "" { |
||||
if columnStr == "" { |
||||
if statement.GroupByStr != "" { |
||||
columnStr = statement.quoteColumnStr(statement.GroupByStr) |
||||
} else { |
||||
columnStr = statement.genColumnStr() |
||||
} |
||||
} |
||||
} else { |
||||
if columnStr == "" { |
||||
if statement.GroupByStr != "" { |
||||
columnStr = statement.quoteColumnStr(statement.GroupByStr) |
||||
} else { |
||||
columnStr = "*" |
||||
} |
||||
} |
||||
} |
||||
if columnStr == "" { |
||||
columnStr = "*" |
||||
} |
||||
} |
||||
|
||||
if err := statement.ProcessIDParam(); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
sqlStr, condArgs, err := statement.genSelectSQL(columnStr, true, true) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
args := append(statement.joinArgs, condArgs...) |
||||
|
||||
// for mssql and use limit
|
||||
qs := strings.Count(sqlStr, "?") |
||||
if len(args)*2 == qs { |
||||
args = append(args, args...) |
||||
} |
||||
|
||||
return sqlStr, args, nil |
||||
} |
||||
|
||||
func (statement *Statement) GenSumSQL(bean interface{}, columns ...string) (string, []interface{}, error) { |
||||
if statement.RawSQL != "" { |
||||
return statement.GenRawSQL(), statement.RawParams, nil |
||||
} |
||||
|
||||
statement.SetRefBean(bean) |
||||
|
||||
var sumStrs = make([]string, 0, len(columns)) |
||||
for _, colName := range columns { |
||||
if !strings.Contains(colName, " ") && !strings.Contains(colName, "(") { |
||||
colName = statement.quote(colName) |
||||
} else { |
||||
colName = statement.ReplaceQuote(colName) |
||||
} |
||||
sumStrs = append(sumStrs, fmt.Sprintf("COALESCE(sum(%s),0)", colName)) |
||||
} |
||||
sumSelect := strings.Join(sumStrs, ", ") |
||||
|
||||
if err := statement.mergeConds(bean); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
sqlStr, condArgs, err := statement.genSelectSQL(sumSelect, true, true) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
return sqlStr, append(statement.joinArgs, condArgs...), nil |
||||
} |
||||
|
||||
func (statement *Statement) GenGetSQL(bean interface{}) (string, []interface{}, error) { |
||||
v := rValue(bean) |
||||
isStruct := v.Kind() == reflect.Struct |
||||
if isStruct { |
||||
statement.SetRefBean(bean) |
||||
} |
||||
|
||||
var columnStr = statement.ColumnStr() |
||||
if len(statement.SelectStr) > 0 { |
||||
columnStr = statement.SelectStr |
||||
} else { |
||||
// TODO: always generate column names, not use * even if join
|
||||
if len(statement.JoinStr) == 0 { |
||||
if len(columnStr) == 0 { |
||||
if len(statement.GroupByStr) > 0 { |
||||
columnStr = statement.quoteColumnStr(statement.GroupByStr) |
||||
} else { |
||||
columnStr = statement.genColumnStr() |
||||
} |
||||
} |
||||
} else { |
||||
if len(columnStr) == 0 { |
||||
if len(statement.GroupByStr) > 0 { |
||||
columnStr = statement.quoteColumnStr(statement.GroupByStr) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
if len(columnStr) == 0 { |
||||
columnStr = "*" |
||||
} |
||||
|
||||
if isStruct { |
||||
if err := statement.mergeConds(bean); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} else { |
||||
if err := statement.ProcessIDParam(); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} |
||||
|
||||
sqlStr, condArgs, err := statement.genSelectSQL(columnStr, true, true) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
return sqlStr, append(statement.joinArgs, condArgs...), nil |
||||
} |
||||
|
||||
// GenCountSQL generates the SQL for counting
|
||||
func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interface{}, error) { |
||||
if statement.RawSQL != "" { |
||||
return statement.GenRawSQL(), statement.RawParams, nil |
||||
} |
||||
|
||||
var condArgs []interface{} |
||||
var err error |
||||
if len(beans) > 0 { |
||||
statement.SetRefBean(beans[0]) |
||||
if err := statement.mergeConds(beans[0]); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} |
||||
|
||||
var selectSQL = statement.SelectStr |
||||
if len(selectSQL) <= 0 { |
||||
if statement.IsDistinct { |
||||
selectSQL = fmt.Sprintf("count(DISTINCT %s)", statement.ColumnStr()) |
||||
} else if statement.ColumnStr() != "" { |
||||
selectSQL = fmt.Sprintf("count(%s)", statement.ColumnStr()) |
||||
} else { |
||||
selectSQL = "count(*)" |
||||
} |
||||
} |
||||
sqlStr, condArgs, err := statement.genSelectSQL(selectSQL, false, false) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
return sqlStr, append(statement.joinArgs, condArgs...), nil |
||||
} |
||||
|
||||
func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderBy bool) (string, []interface{}, error) { |
||||
var ( |
||||
distinct string |
||||
dialect = statement.dialect |
||||
quote = statement.quote |
||||
fromStr = " FROM " |
||||
top, mssqlCondi, whereStr string |
||||
) |
||||
if statement.IsDistinct && !strings.HasPrefix(columnStr, "count") { |
||||
distinct = "DISTINCT " |
||||
} |
||||
|
||||
condSQL, condArgs, err := statement.GenCondSQL(statement.cond) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
if len(condSQL) > 0 { |
||||
whereStr = " WHERE " + condSQL |
||||
} |
||||
|
||||
if dialect.URI().DBType == schemas.MSSQL && strings.Contains(statement.TableName(), "..") { |
||||
fromStr += statement.TableName() |
||||
} else { |
||||
fromStr += quote(statement.TableName()) |
||||
} |
||||
|
||||
if statement.TableAlias != "" { |
||||
if dialect.URI().DBType == schemas.ORACLE { |
||||
fromStr += " " + quote(statement.TableAlias) |
||||
} else { |
||||
fromStr += " AS " + quote(statement.TableAlias) |
||||
} |
||||
} |
||||
if statement.JoinStr != "" { |
||||
fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr) |
||||
} |
||||
|
||||
pLimitN := statement.LimitN |
||||
if dialect.URI().DBType == schemas.MSSQL { |
||||
if pLimitN != nil { |
||||
LimitNValue := *pLimitN |
||||
top = fmt.Sprintf("TOP %d ", LimitNValue) |
||||
} |
||||
if statement.Start > 0 { |
||||
var column string |
||||
if len(statement.RefTable.PKColumns()) == 0 { |
||||
for _, index := range statement.RefTable.Indexes { |
||||
if len(index.Cols) == 1 { |
||||
column = index.Cols[0] |
||||
break |
||||
} |
||||
} |
||||
if len(column) == 0 { |
||||
column = statement.RefTable.ColumnsSeq()[0] |
||||
} |
||||
} else { |
||||
column = statement.RefTable.PKColumns()[0].Name |
||||
} |
||||
if statement.needTableName() { |
||||
if len(statement.TableAlias) > 0 { |
||||
column = statement.TableAlias + "." + column |
||||
} else { |
||||
column = statement.TableName() + "." + column |
||||
} |
||||
} |
||||
|
||||
var orderStr string |
||||
if needOrderBy && len(statement.OrderStr) > 0 { |
||||
orderStr = " ORDER BY " + statement.OrderStr |
||||
} |
||||
|
||||
var groupStr string |
||||
if len(statement.GroupByStr) > 0 { |
||||
groupStr = " GROUP BY " + statement.GroupByStr |
||||
} |
||||
mssqlCondi = fmt.Sprintf("(%s NOT IN (SELECT TOP %d %s%s%s%s%s))", |
||||
column, statement.Start, column, fromStr, whereStr, orderStr, groupStr) |
||||
} |
||||
} |
||||
|
||||
var buf strings.Builder |
||||
fmt.Fprintf(&buf, "SELECT %v%v%v%v%v", distinct, top, columnStr, fromStr, whereStr) |
||||
if len(mssqlCondi) > 0 { |
||||
if len(whereStr) > 0 { |
||||
fmt.Fprint(&buf, " AND ", mssqlCondi) |
||||
} else { |
||||
fmt.Fprint(&buf, " WHERE ", mssqlCondi) |
||||
} |
||||
} |
||||
|
||||
if statement.GroupByStr != "" { |
||||
fmt.Fprint(&buf, " GROUP BY ", statement.GroupByStr) |
||||
} |
||||
if statement.HavingStr != "" { |
||||
fmt.Fprint(&buf, " ", statement.HavingStr) |
||||
} |
||||
if needOrderBy && statement.OrderStr != "" { |
||||
fmt.Fprint(&buf, " ORDER BY ", statement.OrderStr) |
||||
} |
||||
if needLimit { |
||||
if dialect.URI().DBType != schemas.MSSQL && dialect.URI().DBType != schemas.ORACLE { |
||||
if statement.Start > 0 { |
||||
if pLimitN != nil { |
||||
fmt.Fprintf(&buf, " LIMIT %v OFFSET %v", *pLimitN, statement.Start) |
||||
} else { |
||||
fmt.Fprintf(&buf, "LIMIT 0 OFFSET %v", statement.Start) |
||||
} |
||||
} else if pLimitN != nil { |
||||
fmt.Fprint(&buf, " LIMIT ", *pLimitN) |
||||
} |
||||
} else if dialect.URI().DBType == schemas.ORACLE { |
||||
if statement.Start != 0 || pLimitN != nil { |
||||
oldString := buf.String() |
||||
buf.Reset() |
||||
rawColStr := columnStr |
||||
if rawColStr == "*" { |
||||
rawColStr = "at.*" |
||||
} |
||||
fmt.Fprintf(&buf, "SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", |
||||
columnStr, rawColStr, oldString, statement.Start+*pLimitN, statement.Start) |
||||
} |
||||
} |
||||
} |
||||
if statement.IsForUpdate { |
||||
return dialect.ForUpdateSQL(buf.String()), condArgs, nil |
||||
} |
||||
|
||||
return buf.String(), condArgs, nil |
||||
} |
||||
|
||||
func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interface{}, error) { |
||||
if statement.RawSQL != "" { |
||||
return statement.GenRawSQL(), statement.RawParams, nil |
||||
} |
||||
|
||||
var sqlStr string |
||||
var args []interface{} |
||||
var joinStr string |
||||
var err error |
||||
if len(bean) == 0 { |
||||
tableName := statement.TableName() |
||||
if len(tableName) <= 0 { |
||||
return "", nil, ErrTableNotFound |
||||
} |
||||
|
||||
tableName = statement.quote(tableName) |
||||
if len(statement.JoinStr) > 0 { |
||||
joinStr = statement.JoinStr |
||||
} |
||||
|
||||
if statement.Conds().IsValid() { |
||||
condSQL, condArgs, err := statement.GenCondSQL(statement.Conds()) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
if statement.dialect.URI().DBType == schemas.MSSQL { |
||||
sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s %s WHERE %s", tableName, joinStr, condSQL) |
||||
} else if statement.dialect.URI().DBType == schemas.ORACLE { |
||||
sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE (%s) %s AND ROWNUM=1", tableName, joinStr, condSQL) |
||||
} else { |
||||
sqlStr = fmt.Sprintf("SELECT * FROM %s %s WHERE %s LIMIT 1", tableName, joinStr, condSQL) |
||||
} |
||||
args = condArgs |
||||
} else { |
||||
if statement.dialect.URI().DBType == schemas.MSSQL { |
||||
sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s %s", tableName, joinStr) |
||||
} else if statement.dialect.URI().DBType == schemas.ORACLE { |
||||
sqlStr = fmt.Sprintf("SELECT * FROM %s %s WHERE ROWNUM=1", tableName, joinStr) |
||||
} else { |
||||
sqlStr = fmt.Sprintf("SELECT * FROM %s %s LIMIT 1", tableName, joinStr) |
||||
} |
||||
args = []interface{}{} |
||||
} |
||||
} else { |
||||
beanValue := reflect.ValueOf(bean[0]) |
||||
if beanValue.Kind() != reflect.Ptr { |
||||
return "", nil, errors.New("needs a pointer") |
||||
} |
||||
|
||||
if beanValue.Elem().Kind() == reflect.Struct { |
||||
if err := statement.SetRefBean(bean[0]); err != nil { |
||||
return "", nil, err |
||||
} |
||||
} |
||||
|
||||
if len(statement.TableName()) <= 0 { |
||||
return "", nil, ErrTableNotFound |
||||
} |
||||
statement.Limit(1) |
||||
sqlStr, args, err = statement.GenGetSQL(bean[0]) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
} |
||||
|
||||
return sqlStr, args, nil |
||||
} |
||||
|
||||
func (statement *Statement) GenFindSQL(autoCond builder.Cond) (string, []interface{}, error) { |
||||
if statement.RawSQL != "" { |
||||
return statement.GenRawSQL(), statement.RawParams, nil |
||||
} |
||||
|
||||
var sqlStr string |
||||
var args []interface{} |
||||
var err error |
||||
|
||||
if len(statement.TableName()) <= 0 { |
||||
return "", nil, ErrTableNotFound |
||||
} |
||||
|
||||
var columnStr = statement.ColumnStr() |
||||
if len(statement.SelectStr) > 0 { |
||||
columnStr = statement.SelectStr |
||||
} else { |
||||
if statement.JoinStr == "" { |
||||
if columnStr == "" { |
||||
if statement.GroupByStr != "" { |
||||
columnStr = statement.quoteColumnStr(statement.GroupByStr) |
||||
} else { |
||||
columnStr = statement.genColumnStr() |
||||
} |
||||
} |
||||
} else { |
||||
if columnStr == "" { |
||||
if statement.GroupByStr != "" { |
||||
columnStr = statement.quoteColumnStr(statement.GroupByStr) |
||||
} else { |
||||
columnStr = "*" |
||||
} |
||||
} |
||||
} |
||||
if columnStr == "" { |
||||
columnStr = "*" |
||||
} |
||||
} |
||||
|
||||
statement.cond = statement.cond.And(autoCond) |
||||
|
||||
sqlStr, condArgs, err := statement.genSelectSQL(columnStr, true, true) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
args = append(statement.joinArgs, condArgs...) |
||||
// for mssql and use limit
|
||||
qs := strings.Count(sqlStr, "?") |
||||
if len(args)*2 == qs { |
||||
args = append(args, args...) |
||||
} |
||||
|
||||
return sqlStr, args, nil |
||||
} |
@ -0,0 +1,996 @@ |
||||
// Copyright 2015 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 statements |
||||
|
||||
import ( |
||||
"database/sql/driver" |
||||
"errors" |
||||
"fmt" |
||||
"reflect" |
||||
"strings" |
||||
"time" |
||||
|
||||
"xorm.io/builder" |
||||
"xorm.io/xorm/contexts" |
||||
"xorm.io/xorm/convert" |
||||
"xorm.io/xorm/dialects" |
||||
"xorm.io/xorm/internal/json" |
||||
"xorm.io/xorm/internal/utils" |
||||
"xorm.io/xorm/schemas" |
||||
"xorm.io/xorm/tags" |
||||
) |
||||
|
||||
var ( |
||||
// ErrConditionType condition type unsupported
|
||||
ErrConditionType = errors.New("Unsupported condition type") |
||||
// ErrUnSupportedSQLType parameter of SQL is not supported
|
||||
ErrUnSupportedSQLType = errors.New("Unsupported sql type") |
||||
// ErrUnSupportedType unsupported error
|
||||
ErrUnSupportedType = errors.New("Unsupported type error") |
||||
// ErrTableNotFound table not found error
|
||||
ErrTableNotFound = errors.New("Table not found") |
||||
) |
||||
|
||||
// Statement save all the sql info for executing SQL
|
||||
type Statement struct { |
||||
RefTable *schemas.Table |
||||
dialect dialects.Dialect |
||||
defaultTimeZone *time.Location |
||||
tagParser *tags.Parser |
||||
Start int |
||||
LimitN *int |
||||
idParam schemas.PK |
||||
OrderStr string |
||||
JoinStr string |
||||
joinArgs []interface{} |
||||
GroupByStr string |
||||
HavingStr string |
||||
SelectStr string |
||||
useAllCols bool |
||||
AltTableName string |
||||
tableName string |
||||
RawSQL string |
||||
RawParams []interface{} |
||||
UseCascade bool |
||||
UseAutoJoin bool |
||||
StoreEngine string |
||||
Charset string |
||||
UseCache bool |
||||
UseAutoTime bool |
||||
NoAutoCondition bool |
||||
IsDistinct bool |
||||
IsForUpdate bool |
||||
TableAlias string |
||||
allUseBool bool |
||||
CheckVersion bool |
||||
unscoped bool |
||||
ColumnMap columnMap |
||||
OmitColumnMap columnMap |
||||
MustColumnMap map[string]bool |
||||
NullableMap map[string]bool |
||||
IncrColumns exprParams |
||||
DecrColumns exprParams |
||||
ExprColumns exprParams |
||||
cond builder.Cond |
||||
BufferSize int |
||||
Context contexts.ContextCache |
||||
LastError error |
||||
} |
||||
|
||||
// NewStatement creates a new statement
|
||||
func NewStatement(dialect dialects.Dialect, tagParser *tags.Parser, defaultTimeZone *time.Location) *Statement { |
||||
statement := &Statement{ |
||||
dialect: dialect, |
||||
tagParser: tagParser, |
||||
defaultTimeZone: defaultTimeZone, |
||||
} |
||||
statement.Reset() |
||||
return statement |
||||
} |
||||
|
||||
func (statement *Statement) SetTableName(tableName string) { |
||||
statement.tableName = tableName |
||||
} |
||||
|
||||
func (statement *Statement) omitStr() string { |
||||
return statement.dialect.Quoter().Join(statement.OmitColumnMap, " ,") |
||||
} |
||||
|
||||
// GenRawSQL generates correct raw sql
|
||||
func (statement *Statement) GenRawSQL() string { |
||||
return statement.ReplaceQuote(statement.RawSQL) |
||||
} |
||||
|
||||
func (statement *Statement) GenCondSQL(condOrBuilder interface{}) (string, []interface{}, error) { |
||||
condSQL, condArgs, err := builder.ToSQL(condOrBuilder) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
return statement.ReplaceQuote(condSQL), condArgs, nil |
||||
} |
||||
|
||||
func (statement *Statement) ReplaceQuote(sql string) string { |
||||
if sql == "" || statement.dialect.URI().DBType == schemas.MYSQL || |
||||
statement.dialect.URI().DBType == schemas.SQLITE { |
||||
return sql |
||||
} |
||||
return statement.dialect.Quoter().Replace(sql) |
||||
} |
||||
|
||||
func (statement *Statement) SetContextCache(ctxCache contexts.ContextCache) { |
||||
statement.Context = ctxCache |
||||
} |
||||
|
||||
// Init reset all the statement's fields
|
||||
func (statement *Statement) Reset() { |
||||
statement.RefTable = nil |
||||
statement.Start = 0 |
||||
statement.LimitN = nil |
||||
statement.OrderStr = "" |
||||
statement.UseCascade = true |
||||
statement.JoinStr = "" |
||||
statement.joinArgs = make([]interface{}, 0) |
||||
statement.GroupByStr = "" |
||||
statement.HavingStr = "" |
||||
statement.ColumnMap = columnMap{} |
||||
statement.OmitColumnMap = columnMap{} |
||||
statement.AltTableName = "" |
||||
statement.tableName = "" |
||||
statement.idParam = nil |
||||
statement.RawSQL = "" |
||||
statement.RawParams = make([]interface{}, 0) |
||||
statement.UseCache = true |
||||
statement.UseAutoTime = true |
||||
statement.NoAutoCondition = false |
||||
statement.IsDistinct = false |
||||
statement.IsForUpdate = false |
||||
statement.TableAlias = "" |
||||
statement.SelectStr = "" |
||||
statement.allUseBool = false |
||||
statement.useAllCols = false |
||||
statement.MustColumnMap = make(map[string]bool) |
||||
statement.NullableMap = make(map[string]bool) |
||||
statement.CheckVersion = true |
||||
statement.unscoped = false |
||||
statement.IncrColumns = exprParams{} |
||||
statement.DecrColumns = exprParams{} |
||||
statement.ExprColumns = exprParams{} |
||||
statement.cond = builder.NewCond() |
||||
statement.BufferSize = 0 |
||||
statement.Context = nil |
||||
statement.LastError = nil |
||||
} |
||||
|
||||
// NoAutoCondition if you do not want convert bean's field as query condition, then use this function
|
||||
func (statement *Statement) SetNoAutoCondition(no ...bool) *Statement { |
||||
statement.NoAutoCondition = true |
||||
if len(no) > 0 { |
||||
statement.NoAutoCondition = no[0] |
||||
} |
||||
return statement |
||||
} |
||||
|
||||
// Alias set the table alias
|
||||
func (statement *Statement) Alias(alias string) *Statement { |
||||
statement.TableAlias = alias |
||||
return statement |
||||
} |
||||
|
||||
// SQL adds raw sql statement
|
||||
func (statement *Statement) SQL(query interface{}, args ...interface{}) *Statement { |
||||
switch query.(type) { |
||||
case (*builder.Builder): |
||||
var err error |
||||
statement.RawSQL, statement.RawParams, err = query.(*builder.Builder).ToSQL() |
||||
if err != nil { |
||||
statement.LastError = err |
||||
} |
||||
case string: |
||||
statement.RawSQL = query.(string) |
||||
statement.RawParams = args |
||||
default: |
||||
statement.LastError = ErrUnSupportedSQLType |
||||
} |
||||
|
||||
return statement |
||||
} |
||||
|
||||
// Where add Where statement
|
||||
func (statement *Statement) Where(query interface{}, args ...interface{}) *Statement { |
||||
return statement.And(query, args...) |
||||
} |
||||
|
||||
func (statement *Statement) quote(s string) string { |
||||
return statement.dialect.Quoter().Quote(s) |
||||
} |
||||
|
||||
// And add Where & and statement
|
||||
func (statement *Statement) And(query interface{}, args ...interface{}) *Statement { |
||||
switch query.(type) { |
||||
case string: |
||||
cond := builder.Expr(query.(string), args...) |
||||
statement.cond = statement.cond.And(cond) |
||||
case map[string]interface{}: |
||||
queryMap := query.(map[string]interface{}) |
||||
newMap := make(map[string]interface{}) |
||||
for k, v := range queryMap { |
||||
newMap[statement.quote(k)] = v |
||||
} |
||||
statement.cond = statement.cond.And(builder.Eq(newMap)) |
||||
case builder.Cond: |
||||
cond := query.(builder.Cond) |
||||
statement.cond = statement.cond.And(cond) |
||||
for _, v := range args { |
||||
if vv, ok := v.(builder.Cond); ok { |
||||
statement.cond = statement.cond.And(vv) |
||||
} |
||||
} |
||||
default: |
||||
statement.LastError = ErrConditionType |
||||
} |
||||
|
||||
return statement |
||||
} |
||||
|
||||
// Or add Where & Or statement
|
||||
func (statement *Statement) Or(query interface{}, args ...interface{}) *Statement { |
||||
switch query.(type) { |
||||
case string: |
||||
cond := builder.Expr(query.(string), args...) |
||||
statement.cond = statement.cond.Or(cond) |
||||
case map[string]interface{}: |
||||
cond := builder.Eq(query.(map[string]interface{})) |
||||
statement.cond = statement.cond.Or(cond) |
||||
case builder.Cond: |
||||
cond := query.(builder.Cond) |
||||
statement.cond = statement.cond.Or(cond) |
||||
for _, v := range args { |
||||
if vv, ok := v.(builder.Cond); ok { |
||||
statement.cond = statement.cond.Or(vv) |
||||
} |
||||
} |
||||
default: |
||||
// TODO: not support condition type
|
||||
} |
||||
return statement |
||||
} |
||||
|
||||
// In generate "Where column IN (?) " statement
|
||||
func (statement *Statement) In(column string, args ...interface{}) *Statement { |
||||
in := builder.In(statement.quote(column), args...) |
||||
statement.cond = statement.cond.And(in) |
||||
return statement |
||||
} |
||||
|
||||
// NotIn generate "Where column NOT IN (?) " statement
|
||||
func (statement *Statement) NotIn(column string, args ...interface{}) *Statement { |
||||
notIn := builder.NotIn(statement.quote(column), args...) |
||||
statement.cond = statement.cond.And(notIn) |
||||
return statement |
||||
} |
||||
|
||||
func (statement *Statement) SetRefValue(v reflect.Value) error { |
||||
var err error |
||||
statement.RefTable, err = statement.tagParser.ParseWithCache(reflect.Indirect(v)) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
statement.tableName = dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), v, true) |
||||
return nil |
||||
} |
||||
|
||||
func rValue(bean interface{}) reflect.Value { |
||||
return reflect.Indirect(reflect.ValueOf(bean)) |
||||
} |
||||
|
||||
func (statement *Statement) SetRefBean(bean interface{}) error { |
||||
var err error |
||||
statement.RefTable, err = statement.tagParser.ParseWithCache(rValue(bean)) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
statement.tableName = dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), bean, true) |
||||
return nil |
||||
} |
||||
|
||||
func (statement *Statement) needTableName() bool { |
||||
return len(statement.JoinStr) > 0 |
||||
} |
||||
|
||||
func (statement *Statement) colName(col *schemas.Column, tableName string) string { |
||||
if statement.needTableName() { |
||||
var nm = tableName |
||||
if len(statement.TableAlias) > 0 { |
||||
nm = statement.TableAlias |
||||
} |
||||
return statement.quote(nm) + "." + statement.quote(col.Name) |
||||
} |
||||
return statement.quote(col.Name) |
||||
} |
||||
|
||||
// TableName return current tableName
|
||||
func (statement *Statement) TableName() string { |
||||
if statement.AltTableName != "" { |
||||
return statement.AltTableName |
||||
} |
||||
|
||||
return statement.tableName |
||||
} |
||||
|
||||
// Incr Generate "Update ... Set column = column + arg" statement
|
||||
func (statement *Statement) Incr(column string, arg ...interface{}) *Statement { |
||||
if len(arg) > 0 { |
||||
statement.IncrColumns.addParam(column, arg[0]) |
||||
} else { |
||||
statement.IncrColumns.addParam(column, 1) |
||||
} |
||||
return statement |
||||
} |
||||
|
||||
// Decr Generate "Update ... Set column = column - arg" statement
|
||||
func (statement *Statement) Decr(column string, arg ...interface{}) *Statement { |
||||
if len(arg) > 0 { |
||||
statement.DecrColumns.addParam(column, arg[0]) |
||||
} else { |
||||
statement.DecrColumns.addParam(column, 1) |
||||
} |
||||
return statement |
||||
} |
||||
|
||||
// SetExpr Generate "Update ... Set column = {expression}" statement
|
||||
func (statement *Statement) SetExpr(column string, expression interface{}) *Statement { |
||||
if e, ok := expression.(string); ok { |
||||
statement.ExprColumns.addParam(column, statement.dialect.Quoter().Replace(e)) |
||||
} else { |
||||
statement.ExprColumns.addParam(column, expression) |
||||
} |
||||
return statement |
||||
} |
||||
|
||||
// Distinct generates "DISTINCT col1, col2 " statement
|
||||
func (statement *Statement) Distinct(columns ...string) *Statement { |
||||
statement.IsDistinct = true |
||||
statement.Cols(columns...) |
||||
return statement |
||||
} |
||||
|
||||
// ForUpdate generates "SELECT ... FOR UPDATE" statement
|
||||
func (statement *Statement) ForUpdate() *Statement { |
||||
statement.IsForUpdate = true |
||||
return statement |
||||
} |
||||
|
||||
// Select replace select
|
||||
func (statement *Statement) Select(str string) *Statement { |
||||
statement.SelectStr = statement.ReplaceQuote(str) |
||||
return statement |
||||
} |
||||
|
||||
func col2NewCols(columns ...string) []string { |
||||
newColumns := make([]string, 0, len(columns)) |
||||
for _, col := range columns { |
||||
col = strings.Replace(col, "`", "", -1) |
||||
col = strings.Replace(col, `"`, "", -1) |
||||
ccols := strings.Split(col, ",") |
||||
for _, c := range ccols { |
||||
newColumns = append(newColumns, strings.TrimSpace(c)) |
||||
} |
||||
} |
||||
return newColumns |
||||
} |
||||
|
||||
// Cols generate "col1, col2" statement
|
||||
func (statement *Statement) Cols(columns ...string) *Statement { |
||||
cols := col2NewCols(columns...) |
||||
for _, nc := range cols { |
||||
statement.ColumnMap.Add(nc) |
||||
} |
||||
return statement |
||||
} |
||||
|
||||
func (statement *Statement) ColumnStr() string { |
||||
return statement.dialect.Quoter().Join(statement.ColumnMap, ", ") |
||||
} |
||||
|
||||
// AllCols update use only: update all columns
|
||||
func (statement *Statement) AllCols() *Statement { |
||||
statement.useAllCols = true |
||||
return statement |
||||
} |
||||
|
||||
// MustCols update use only: must update columns
|
||||
func (statement *Statement) MustCols(columns ...string) *Statement { |
||||
newColumns := col2NewCols(columns...) |
||||
for _, nc := range newColumns { |
||||
statement.MustColumnMap[strings.ToLower(nc)] = true |
||||
} |
||||
return statement |
||||
} |
||||
|
||||
// UseBool indicates that use bool fields as update contents and query contiditions
|
||||
func (statement *Statement) UseBool(columns ...string) *Statement { |
||||
if len(columns) > 0 { |
||||
statement.MustCols(columns...) |
||||
} else { |
||||
statement.allUseBool = true |
||||
} |
||||
return statement |
||||
} |
||||
|
||||
// Omit do not use the columns
|
||||
func (statement *Statement) Omit(columns ...string) { |
||||
newColumns := col2NewCols(columns...) |
||||
for _, nc := range newColumns { |
||||
statement.OmitColumnMap = append(statement.OmitColumnMap, nc) |
||||
} |
||||
} |
||||
|
||||
// Nullable Update use only: update columns to null when value is nullable and zero-value
|
||||
func (statement *Statement) Nullable(columns ...string) { |
||||
newColumns := col2NewCols(columns...) |
||||
for _, nc := range newColumns { |
||||
statement.NullableMap[strings.ToLower(nc)] = true |
||||
} |
||||
} |
||||
|
||||
// Top generate LIMIT limit statement
|
||||
func (statement *Statement) Top(limit int) *Statement { |
||||
statement.Limit(limit) |
||||
return statement |
||||
} |
||||
|
||||
// Limit generate LIMIT start, limit statement
|
||||
func (statement *Statement) Limit(limit int, start ...int) *Statement { |
||||
statement.LimitN = &limit |
||||
if len(start) > 0 { |
||||
statement.Start = start[0] |
||||
} |
||||
return statement |
||||
} |
||||
|
||||
// OrderBy generate "Order By order" statement
|
||||
func (statement *Statement) OrderBy(order string) *Statement { |
||||
if len(statement.OrderStr) > 0 { |
||||
statement.OrderStr += ", " |
||||
} |
||||
statement.OrderStr += statement.ReplaceQuote(order) |
||||
return statement |
||||
} |
||||
|
||||
// Desc generate `ORDER BY xx DESC`
|
||||
func (statement *Statement) Desc(colNames ...string) *Statement { |
||||
var buf strings.Builder |
||||
if len(statement.OrderStr) > 0 { |
||||
fmt.Fprint(&buf, statement.OrderStr, ", ") |
||||
} |
||||
for i, col := range colNames { |
||||
if i > 0 { |
||||
fmt.Fprint(&buf, ", ") |
||||
} |
||||
statement.dialect.Quoter().QuoteTo(&buf, col) |
||||
fmt.Fprint(&buf, " DESC") |
||||
} |
||||
statement.OrderStr = buf.String() |
||||
return statement |
||||
} |
||||
|
||||
// Asc provide asc order by query condition, the input parameters are columns.
|
||||
func (statement *Statement) Asc(colNames ...string) *Statement { |
||||
var buf strings.Builder |
||||
if len(statement.OrderStr) > 0 { |
||||
fmt.Fprint(&buf, statement.OrderStr, ", ") |
||||
} |
||||
for i, col := range colNames { |
||||
if i > 0 { |
||||
fmt.Fprint(&buf, ", ") |
||||
} |
||||
statement.dialect.Quoter().QuoteTo(&buf, col) |
||||
fmt.Fprint(&buf, " ASC") |
||||
} |
||||
statement.OrderStr = buf.String() |
||||
return statement |
||||
} |
||||
|
||||
func (statement *Statement) Conds() builder.Cond { |
||||
return statement.cond |
||||
} |
||||
|
||||
// Table tempororily set table name, the parameter could be a string or a pointer of struct
|
||||
func (statement *Statement) SetTable(tableNameOrBean interface{}) error { |
||||
v := rValue(tableNameOrBean) |
||||
t := v.Type() |
||||
if t.Kind() == reflect.Struct { |
||||
var err error |
||||
statement.RefTable, err = statement.tagParser.ParseWithCache(v) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
} |
||||
|
||||
statement.AltTableName = dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), tableNameOrBean, true) |
||||
return nil |
||||
} |
||||
|
||||
// Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||
func (statement *Statement) Join(joinOP string, tablename interface{}, condition string, args ...interface{}) *Statement { |
||||
var buf strings.Builder |
||||
if len(statement.JoinStr) > 0 { |
||||
fmt.Fprintf(&buf, "%v %v JOIN ", statement.JoinStr, joinOP) |
||||
} else { |
||||
fmt.Fprintf(&buf, "%v JOIN ", joinOP) |
||||
} |
||||
|
||||
switch tp := tablename.(type) { |
||||
case builder.Builder: |
||||
subSQL, subQueryArgs, err := tp.ToSQL() |
||||
if err != nil { |
||||
statement.LastError = err |
||||
return statement |
||||
} |
||||
|
||||
fields := strings.Split(tp.TableName(), ".") |
||||
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1]) |
||||
aliasName = schemas.CommonQuoter.Trim(aliasName) |
||||
|
||||
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), aliasName, statement.ReplaceQuote(condition)) |
||||
statement.joinArgs = append(statement.joinArgs, subQueryArgs...) |
||||
case *builder.Builder: |
||||
subSQL, subQueryArgs, err := tp.ToSQL() |
||||
if err != nil { |
||||
statement.LastError = err |
||||
return statement |
||||
} |
||||
|
||||
fields := strings.Split(tp.TableName(), ".") |
||||
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1]) |
||||
aliasName = schemas.CommonQuoter.Trim(aliasName) |
||||
|
||||
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), aliasName, statement.ReplaceQuote(condition)) |
||||
statement.joinArgs = append(statement.joinArgs, subQueryArgs...) |
||||
default: |
||||
tbName := dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), tablename, true) |
||||
if !utils.IsSubQuery(tbName) { |
||||
var buf strings.Builder |
||||
statement.dialect.Quoter().QuoteTo(&buf, tbName) |
||||
tbName = buf.String() |
||||
} |
||||
fmt.Fprintf(&buf, "%s ON %v", tbName, statement.ReplaceQuote(condition)) |
||||
} |
||||
|
||||
statement.JoinStr = buf.String() |
||||
statement.joinArgs = append(statement.joinArgs, args...) |
||||
return statement |
||||
} |
||||
|
||||
// tbName get some table's table name
|
||||
func (statement *Statement) tbNameNoSchema(table *schemas.Table) string { |
||||
if len(statement.AltTableName) > 0 { |
||||
return statement.AltTableName |
||||
} |
||||
|
||||
return table.Name |
||||
} |
||||
|
||||
// GroupBy generate "Group By keys" statement
|
||||
func (statement *Statement) GroupBy(keys string) *Statement { |
||||
statement.GroupByStr = statement.ReplaceQuote(keys) |
||||
return statement |
||||
} |
||||
|
||||
// Having generate "Having conditions" statement
|
||||
func (statement *Statement) Having(conditions string) *Statement { |
||||
statement.HavingStr = fmt.Sprintf("HAVING %v", statement.ReplaceQuote(conditions)) |
||||
return statement |
||||
} |
||||
|
||||
// Unscoped always disable struct tag "deleted"
|
||||
func (statement *Statement) SetUnscoped() *Statement { |
||||
statement.unscoped = true |
||||
return statement |
||||
} |
||||
|
||||
func (statement *Statement) GetUnscoped() bool { |
||||
return statement.unscoped |
||||
} |
||||
|
||||
func (statement *Statement) genColumnStr() string { |
||||
if statement.RefTable == nil { |
||||
return "" |
||||
} |
||||
|
||||
var buf strings.Builder |
||||
columns := statement.RefTable.Columns() |
||||
|
||||
for _, col := range columns { |
||||
if statement.OmitColumnMap.Contain(col.Name) { |
||||
continue |
||||
} |
||||
|
||||
if len(statement.ColumnMap) > 0 && !statement.ColumnMap.Contain(col.Name) { |
||||
continue |
||||
} |
||||
|
||||
if col.MapType == schemas.ONLYTODB { |
||||
continue |
||||
} |
||||
|
||||
if buf.Len() != 0 { |
||||
buf.WriteString(", ") |
||||
} |
||||
|
||||
if statement.JoinStr != "" { |
||||
if statement.TableAlias != "" { |
||||
buf.WriteString(statement.TableAlias) |
||||
} else { |
||||
buf.WriteString(statement.TableName()) |
||||
} |
||||
|
||||
buf.WriteString(".") |
||||
} |
||||
|
||||
statement.dialect.Quoter().QuoteTo(&buf, col.Name) |
||||
} |
||||
|
||||
return buf.String() |
||||
} |
||||
|
||||
func (statement *Statement) GenCreateTableSQL() []string { |
||||
statement.RefTable.StoreEngine = statement.StoreEngine |
||||
statement.RefTable.Charset = statement.Charset |
||||
s, _ := statement.dialect.CreateTableSQL(statement.RefTable, statement.TableName()) |
||||
return s |
||||
} |
||||
|
||||
func (statement *Statement) GenIndexSQL() []string { |
||||
var sqls []string |
||||
tbName := statement.TableName() |
||||
for _, index := range statement.RefTable.Indexes { |
||||
if index.Type == schemas.IndexType { |
||||
sql := statement.dialect.CreateIndexSQL(tbName, index) |
||||
sqls = append(sqls, sql) |
||||
} |
||||
} |
||||
return sqls |
||||
} |
||||
|
||||
func uniqueName(tableName, uqeName string) string { |
||||
return fmt.Sprintf("UQE_%v_%v", tableName, uqeName) |
||||
} |
||||
|
||||
func (statement *Statement) GenUniqueSQL() []string { |
||||
var sqls []string |
||||
tbName := statement.TableName() |
||||
for _, index := range statement.RefTable.Indexes { |
||||
if index.Type == schemas.UniqueType { |
||||
sql := statement.dialect.CreateIndexSQL(tbName, index) |
||||
sqls = append(sqls, sql) |
||||
} |
||||
} |
||||
return sqls |
||||
} |
||||
|
||||
func (statement *Statement) GenDelIndexSQL() []string { |
||||
var sqls []string |
||||
tbName := statement.TableName() |
||||
idx := strings.Index(tbName, ".") |
||||
if idx > -1 { |
||||
tbName = tbName[idx+1:] |
||||
} |
||||
for _, index := range statement.RefTable.Indexes { |
||||
sqls = append(sqls, statement.dialect.DropIndexSQL(tbName, index)) |
||||
} |
||||
return sqls |
||||
} |
||||
|
||||
func (statement *Statement) buildConds2(table *schemas.Table, bean interface{}, |
||||
includeVersion bool, includeUpdated bool, includeNil bool, |
||||
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool, |
||||
mustColumnMap map[string]bool, tableName, aliasName string, addedTableName bool) (builder.Cond, error) { |
||||
var conds []builder.Cond |
||||
for _, col := range table.Columns() { |
||||
if !includeVersion && col.IsVersion { |
||||
continue |
||||
} |
||||
if !includeUpdated && col.IsUpdated { |
||||
continue |
||||
} |
||||
if !includeAutoIncr && col.IsAutoIncrement { |
||||
continue |
||||
} |
||||
|
||||
if statement.dialect.URI().DBType == schemas.MSSQL && (col.SQLType.Name == schemas.Text || |
||||
col.SQLType.IsBlob() || col.SQLType.Name == schemas.TimeStampz) { |
||||
continue |
||||
} |
||||
if col.SQLType.IsJson() { |
||||
continue |
||||
} |
||||
|
||||
var colName string |
||||
if addedTableName { |
||||
var nm = tableName |
||||
if len(aliasName) > 0 { |
||||
nm = aliasName |
||||
} |
||||
colName = statement.quote(nm) + "." + statement.quote(col.Name) |
||||
} else { |
||||
colName = statement.quote(col.Name) |
||||
} |
||||
|
||||
fieldValuePtr, err := col.ValueOf(bean) |
||||
if err != nil { |
||||
if !strings.Contains(err.Error(), "is not valid") { |
||||
//engine.logger.Warn(err)
|
||||
} |
||||
continue |
||||
} |
||||
|
||||
if col.IsDeleted && !unscoped { // tag "deleted" is enabled
|
||||
conds = append(conds, statement.CondDeleted(col)) |
||||
} |
||||
|
||||
fieldValue := *fieldValuePtr |
||||
if fieldValue.Interface() == nil { |
||||
continue |
||||
} |
||||
|
||||
fieldType := reflect.TypeOf(fieldValue.Interface()) |
||||
requiredField := useAllCols |
||||
|
||||
if b, ok := getFlagForColumn(mustColumnMap, col); ok { |
||||
if b { |
||||
requiredField = true |
||||
} else { |
||||
continue |
||||
} |
||||
} |
||||
|
||||
if fieldType.Kind() == reflect.Ptr { |
||||
if fieldValue.IsNil() { |
||||
if includeNil { |
||||
conds = append(conds, builder.Eq{colName: nil}) |
||||
} |
||||
continue |
||||
} else if !fieldValue.IsValid() { |
||||
continue |
||||
} else { |
||||
// dereference ptr type to instance type
|
||||
fieldValue = fieldValue.Elem() |
||||
fieldType = reflect.TypeOf(fieldValue.Interface()) |
||||
requiredField = true |
||||
} |
||||
} |
||||
|
||||
var val interface{} |
||||
switch fieldType.Kind() { |
||||
case reflect.Bool: |
||||
if allUseBool || requiredField { |
||||
val = fieldValue.Interface() |
||||
} else { |
||||
// if a bool in a struct, it will not be as a condition because it default is false,
|
||||
// please use Where() instead
|
||||
continue |
||||
} |
||||
case reflect.String: |
||||
if !requiredField && fieldValue.String() == "" { |
||||
continue |
||||
} |
||||
// for MyString, should convert to string or panic
|
||||
if fieldType.String() != reflect.String.String() { |
||||
val = fieldValue.String() |
||||
} else { |
||||
val = fieldValue.Interface() |
||||
} |
||||
case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64: |
||||
if !requiredField && fieldValue.Int() == 0 { |
||||
continue |
||||
} |
||||
val = fieldValue.Interface() |
||||
case reflect.Float32, reflect.Float64: |
||||
if !requiredField && fieldValue.Float() == 0.0 { |
||||
continue |
||||
} |
||||
val = fieldValue.Interface() |
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64: |
||||
if !requiredField && fieldValue.Uint() == 0 { |
||||
continue |
||||
} |
||||
t := int64(fieldValue.Uint()) |
||||
val = reflect.ValueOf(&t).Interface() |
||||
case reflect.Struct: |
||||
if fieldType.ConvertibleTo(schemas.TimeType) { |
||||
t := fieldValue.Convert(schemas.TimeType).Interface().(time.Time) |
||||
if !requiredField && (t.IsZero() || !fieldValue.IsValid()) { |
||||
continue |
||||
} |
||||
val = dialects.FormatColumnTime(statement.dialect, statement.defaultTimeZone, col, t) |
||||
} else if _, ok := reflect.New(fieldType).Interface().(convert.Conversion); ok { |
||||
continue |
||||
} else if valNul, ok := fieldValue.Interface().(driver.Valuer); ok { |
||||
val, _ = valNul.Value() |
||||
if val == nil && !requiredField { |
||||
continue |
||||
} |
||||
} else { |
||||
if col.SQLType.IsJson() { |
||||
if col.SQLType.IsText() { |
||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
val = string(bytes) |
||||
} else if col.SQLType.IsBlob() { |
||||
var bytes []byte |
||||
var err error |
||||
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
val = bytes |
||||
} |
||||
} else { |
||||
table, err := statement.tagParser.ParseWithCache(fieldValue) |
||||
if err != nil { |
||||
val = fieldValue.Interface() |
||||
} else { |
||||
if len(table.PrimaryKeys) == 1 { |
||||
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) |
||||
// fix non-int pk issues
|
||||
//if pkField.Int() != 0 {
|
||||
if pkField.IsValid() && !utils.IsZero(pkField.Interface()) { |
||||
val = pkField.Interface() |
||||
} else { |
||||
continue |
||||
} |
||||
} else { |
||||
//TODO: how to handler?
|
||||
return nil, fmt.Errorf("not supported %v as %v", fieldValue.Interface(), table.PrimaryKeys) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
case reflect.Array: |
||||
continue |
||||
case reflect.Slice, reflect.Map: |
||||
if fieldValue == reflect.Zero(fieldType) { |
||||
continue |
||||
} |
||||
if fieldValue.IsNil() || !fieldValue.IsValid() || fieldValue.Len() == 0 { |
||||
continue |
||||
} |
||||
|
||||
if col.SQLType.IsText() { |
||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
val = string(bytes) |
||||
} else if col.SQLType.IsBlob() { |
||||
var bytes []byte |
||||
var err error |
||||
if (fieldType.Kind() == reflect.Array || fieldType.Kind() == reflect.Slice) && |
||||
fieldType.Elem().Kind() == reflect.Uint8 { |
||||
if fieldValue.Len() > 0 { |
||||
val = fieldValue.Bytes() |
||||
} else { |
||||
continue |
||||
} |
||||
} else { |
||||
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
val = bytes |
||||
} |
||||
} else { |
||||
continue |
||||
} |
||||
default: |
||||
val = fieldValue.Interface() |
||||
} |
||||
|
||||
conds = append(conds, builder.Eq{colName: val}) |
||||
} |
||||
|
||||
return builder.And(conds...), nil |
||||
} |
||||
|
||||
func (statement *Statement) BuildConds(table *schemas.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, addedTableName bool) (builder.Cond, error) { |
||||
return statement.buildConds2(table, bean, includeVersion, includeUpdated, includeNil, includeAutoIncr, statement.allUseBool, statement.useAllCols, |
||||
statement.unscoped, statement.MustColumnMap, statement.TableName(), statement.TableAlias, addedTableName) |
||||
} |
||||
|
||||
func (statement *Statement) mergeConds(bean interface{}) error { |
||||
if !statement.NoAutoCondition { |
||||
var addedTableName = (len(statement.JoinStr) > 0) |
||||
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, addedTableName) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
statement.cond = statement.cond.And(autoCond) |
||||
} |
||||
|
||||
if err := statement.ProcessIDParam(); err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (statement *Statement) GenConds(bean interface{}) (string, []interface{}, error) { |
||||
if err := statement.mergeConds(bean); err != nil { |
||||
return "", nil, err |
||||
} |
||||
|
||||
return statement.GenCondSQL(statement.cond) |
||||
} |
||||
|
||||
func (statement *Statement) quoteColumnStr(columnStr string) string { |
||||
columns := strings.Split(columnStr, ",") |
||||
return statement.dialect.Quoter().Join(columns, ",") |
||||
} |
||||
|
||||
func (statement *Statement) ConvertSQLOrArgs(sqlOrArgs ...interface{}) (string, []interface{}, error) { |
||||
sql, args, err := convertSQLOrArgs(sqlOrArgs...) |
||||
if err != nil { |
||||
return "", nil, err |
||||
} |
||||
return statement.ReplaceQuote(sql), args, nil |
||||
} |
||||
|
||||
func convertSQLOrArgs(sqlOrArgs ...interface{}) (string, []interface{}, error) { |
||||
switch sqlOrArgs[0].(type) { |
||||
case string: |
||||
return sqlOrArgs[0].(string), sqlOrArgs[1:], nil |
||||
case *builder.Builder: |
||||
return sqlOrArgs[0].(*builder.Builder).ToSQL() |
||||
case builder.Builder: |
||||
bd := sqlOrArgs[0].(builder.Builder) |
||||
return bd.ToSQL() |
||||
} |
||||
|
||||
return "", nil, ErrUnSupportedType |
||||
} |
||||
|
||||
func (statement *Statement) joinColumns(cols []*schemas.Column, includeTableName bool) string { |
||||
var colnames = make([]string, len(cols)) |
||||
for i, col := range cols { |
||||
if includeTableName { |
||||
colnames[i] = statement.quote(statement.TableName()) + |
||||
"." + statement.quote(col.Name) |
||||
} else { |
||||
colnames[i] = statement.quote(col.Name) |
||||
} |
||||
} |
||||
return strings.Join(colnames, ", ") |
||||
} |
||||
|
||||
// CondDeleted returns the conditions whether a record is soft deleted.
|
||||
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond { |
||||
var colName = col.Name |
||||
if statement.JoinStr != "" { |
||||
var prefix string |
||||
if statement.TableAlias != "" { |
||||
prefix = statement.TableAlias |
||||
} else { |
||||
prefix = statement.TableName() |
||||
} |
||||
colName = statement.quote(prefix) + "." + statement.quote(col.Name) |
||||
} |
||||
var cond = builder.NewCond() |
||||
if col.SQLType.IsNumeric() { |
||||
cond = builder.Eq{colName: 0} |
||||
} else { |
||||
// FIXME: mssql: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
|
||||
if statement.dialect.URI().DBType != schemas.MSSQL { |
||||
cond = builder.Eq{colName: utils.ZeroTime1} |
||||
} |
||||
} |
||||
|
||||
if col.Nullable { |
||||
cond = cond.Or(builder.IsNull{colName}) |
||||
} |
||||
|
||||
return cond |
||||
} |
@ -0,0 +1,295 @@ |
||||
// 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 statements |
||||
|
||||
import ( |
||||
"database/sql/driver" |
||||
"errors" |
||||
"fmt" |
||||
"reflect" |
||||
"time" |
||||
|
||||
"xorm.io/xorm/convert" |
||||
"xorm.io/xorm/dialects" |
||||
"xorm.io/xorm/internal/json" |
||||
"xorm.io/xorm/internal/utils" |
||||
"xorm.io/xorm/schemas" |
||||
) |
||||
|
||||
func (statement *Statement) ifAddColUpdate(col *schemas.Column, includeVersion, includeUpdated, includeNil, |
||||
includeAutoIncr, update bool) (bool, error) { |
||||
columnMap := statement.ColumnMap |
||||
omitColumnMap := statement.OmitColumnMap |
||||
unscoped := statement.unscoped |
||||
|
||||
if !includeVersion && col.IsVersion { |
||||
return false, nil |
||||
} |
||||
if col.IsCreated && !columnMap.Contain(col.Name) { |
||||
return false, nil |
||||
} |
||||
if !includeUpdated && col.IsUpdated { |
||||
return false, nil |
||||
} |
||||
if !includeAutoIncr && col.IsAutoIncrement { |
||||
return false, nil |
||||
} |
||||
if col.IsDeleted && !unscoped { |
||||
return false, nil |
||||
} |
||||
if omitColumnMap.Contain(col.Name) { |
||||
return false, nil |
||||
} |
||||
if len(columnMap) > 0 && !columnMap.Contain(col.Name) { |
||||
return false, nil |
||||
} |
||||
|
||||
if col.MapType == schemas.ONLYFROMDB { |
||||
return false, nil |
||||
} |
||||
|
||||
if statement.IncrColumns.IsColExist(col.Name) { |
||||
return false, nil |
||||
} else if statement.DecrColumns.IsColExist(col.Name) { |
||||
return false, nil |
||||
} else if statement.ExprColumns.IsColExist(col.Name) { |
||||
return false, nil |
||||
} |
||||
|
||||
return true, nil |
||||
} |
||||
|
||||
// BuildUpdates auto generating update columnes and values according a struct
|
||||
func (statement *Statement) BuildUpdates(tableValue reflect.Value, |
||||
includeVersion, includeUpdated, includeNil, |
||||
includeAutoIncr, update bool) ([]string, []interface{}, error) { |
||||
table := statement.RefTable |
||||
allUseBool := statement.allUseBool |
||||
useAllCols := statement.useAllCols |
||||
mustColumnMap := statement.MustColumnMap |
||||
nullableMap := statement.NullableMap |
||||
|
||||
var colNames = make([]string, 0) |
||||
var args = make([]interface{}, 0) |
||||
|
||||
for _, col := range table.Columns() { |
||||
ok, err := statement.ifAddColUpdate(col, includeVersion, includeUpdated, includeNil, |
||||
includeAutoIncr, update) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
if !ok { |
||||
continue |
||||
} |
||||
|
||||
fieldValuePtr, err := col.ValueOfV(&tableValue) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
fieldValue := *fieldValuePtr |
||||
fieldType := reflect.TypeOf(fieldValue.Interface()) |
||||
if fieldType == nil { |
||||
continue |
||||
} |
||||
|
||||
requiredField := useAllCols |
||||
includeNil := useAllCols |
||||
|
||||
if b, ok := getFlagForColumn(mustColumnMap, col); ok { |
||||
if b { |
||||
requiredField = true |
||||
} else { |
||||
continue |
||||
} |
||||
} |
||||
|
||||
// !evalphobia! set fieldValue as nil when column is nullable and zero-value
|
||||
if b, ok := getFlagForColumn(nullableMap, col); ok { |
||||
if b && col.Nullable && utils.IsZero(fieldValue.Interface()) { |
||||
var nilValue *int |
||||
fieldValue = reflect.ValueOf(nilValue) |
||||
fieldType = reflect.TypeOf(fieldValue.Interface()) |
||||
includeNil = true |
||||
} |
||||
} |
||||
|
||||
var val interface{} |
||||
|
||||
if fieldValue.CanAddr() { |
||||
if structConvert, ok := fieldValue.Addr().Interface().(convert.Conversion); ok { |
||||
data, err := structConvert.ToDB() |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
val = data |
||||
goto APPEND |
||||
} |
||||
} |
||||
|
||||
if structConvert, ok := fieldValue.Interface().(convert.Conversion); ok { |
||||
data, err := structConvert.ToDB() |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
val = data |
||||
goto APPEND |
||||
} |
||||
|
||||
if fieldType.Kind() == reflect.Ptr { |
||||
if fieldValue.IsNil() { |
||||
if includeNil { |
||||
args = append(args, nil) |
||||
colNames = append(colNames, fmt.Sprintf("%v=?", statement.quote(col.Name))) |
||||
} |
||||
continue |
||||
} else if !fieldValue.IsValid() { |
||||
continue |
||||
} else { |
||||
// dereference ptr type to instance type
|
||||
fieldValue = fieldValue.Elem() |
||||
fieldType = reflect.TypeOf(fieldValue.Interface()) |
||||
requiredField = true |
||||
} |
||||
} |
||||
|
||||
switch fieldType.Kind() { |
||||
case reflect.Bool: |
||||
if allUseBool || requiredField { |
||||
val = fieldValue.Interface() |
||||
} else { |
||||
// if a bool in a struct, it will not be as a condition because it default is false,
|
||||
// please use Where() instead
|
||||
continue |
||||
} |
||||
case reflect.String: |
||||
if !requiredField && fieldValue.String() == "" { |
||||
continue |
||||
} |
||||
// for MyString, should convert to string or panic
|
||||
if fieldType.String() != reflect.String.String() { |
||||
val = fieldValue.String() |
||||
} else { |
||||
val = fieldValue.Interface() |
||||
} |
||||
case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64: |
||||
if !requiredField && fieldValue.Int() == 0 { |
||||
continue |
||||
} |
||||
val = fieldValue.Interface() |
||||
case reflect.Float32, reflect.Float64: |
||||
if !requiredField && fieldValue.Float() == 0.0 { |
||||
continue |
||||
} |
||||
val = fieldValue.Interface() |
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64: |
||||
if !requiredField && fieldValue.Uint() == 0 { |
||||
continue |
||||
} |
||||
t := int64(fieldValue.Uint()) |
||||
val = reflect.ValueOf(&t).Interface() |
||||
case reflect.Struct: |
||||
if fieldType.ConvertibleTo(schemas.TimeType) { |
||||
t := fieldValue.Convert(schemas.TimeType).Interface().(time.Time) |
||||
if !requiredField && (t.IsZero() || !fieldValue.IsValid()) { |
||||
continue |
||||
} |
||||
val = dialects.FormatColumnTime(statement.dialect, statement.defaultTimeZone, col, t) |
||||
} else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok { |
||||
val, _ = nulType.Value() |
||||
if val == nil && !requiredField { |
||||
continue |
||||
} |
||||
} else { |
||||
if !col.SQLType.IsJson() { |
||||
table, err := statement.tagParser.ParseWithCache(fieldValue) |
||||
if err != nil { |
||||
val = fieldValue.Interface() |
||||
} else { |
||||
if len(table.PrimaryKeys) == 1 { |
||||
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName) |
||||
// fix non-int pk issues
|
||||
if pkField.IsValid() && (!requiredField && !utils.IsZero(pkField.Interface())) { |
||||
val = pkField.Interface() |
||||
} else { |
||||
continue |
||||
} |
||||
} else { |
||||
return nil, nil, errors.New("Not supported multiple primary keys") |
||||
} |
||||
} |
||||
} else { |
||||
// Blank struct could not be as update data
|
||||
if requiredField || !utils.IsStructZero(fieldValue) { |
||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, nil, fmt.Errorf("mashal %v failed", fieldValue.Interface()) |
||||
} |
||||
if col.SQLType.IsText() { |
||||
val = string(bytes) |
||||
} else if col.SQLType.IsBlob() { |
||||
val = bytes |
||||
} |
||||
} else { |
||||
continue |
||||
} |
||||
} |
||||
} |
||||
case reflect.Array, reflect.Slice, reflect.Map: |
||||
if !requiredField { |
||||
if fieldValue == reflect.Zero(fieldType) { |
||||
continue |
||||
} |
||||
if fieldType.Kind() == reflect.Array { |
||||
if utils.IsArrayZero(fieldValue) { |
||||
continue |
||||
} |
||||
} else if fieldValue.IsNil() || !fieldValue.IsValid() || fieldValue.Len() == 0 { |
||||
continue |
||||
} |
||||
} |
||||
|
||||
if col.SQLType.IsText() { |
||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
val = string(bytes) |
||||
} else if col.SQLType.IsBlob() { |
||||
var bytes []byte |
||||
var err error |
||||
if fieldType.Kind() == reflect.Slice && |
||||
fieldType.Elem().Kind() == reflect.Uint8 { |
||||
if fieldValue.Len() > 0 { |
||||
val = fieldValue.Bytes() |
||||
} else { |
||||
continue |
||||
} |
||||
} else if fieldType.Kind() == reflect.Array && |
||||
fieldType.Elem().Kind() == reflect.Uint8 { |
||||
val = fieldValue.Slice(0, 0).Interface() |
||||
} else { |
||||
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
val = bytes |
||||
} |
||||
} else { |
||||
continue |
||||
} |
||||
default: |
||||
val = fieldValue.Interface() |
||||
} |
||||
|
||||
APPEND: |
||||
args = append(args, val) |
||||
colNames = append(colNames, fmt.Sprintf("%v = ?", statement.quote(col.Name))) |
||||
} |
||||
|
||||
return colNames, args, nil |
||||
} |
@ -0,0 +1,154 @@ |
||||
// 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 statements |
||||
|
||||
import ( |
||||
"database/sql" |
||||
"database/sql/driver" |
||||
"fmt" |
||||
"reflect" |
||||
"time" |
||||
|
||||
"xorm.io/xorm/convert" |
||||
"xorm.io/xorm/dialects" |
||||
"xorm.io/xorm/internal/json" |
||||
"xorm.io/xorm/schemas" |
||||
) |
||||
|
||||
var ( |
||||
nullFloatType = reflect.TypeOf(sql.NullFloat64{}) |
||||
) |
||||
|
||||
// Value2Interface convert a field value of a struct to interface for puting into database
|
||||
func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue reflect.Value) (interface{}, error) { |
||||
if fieldValue.CanAddr() { |
||||
if fieldConvert, ok := fieldValue.Addr().Interface().(convert.Conversion); ok { |
||||
data, err := fieldConvert.ToDB() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if col.SQLType.IsBlob() { |
||||
return data, nil |
||||
} |
||||
return string(data), nil |
||||
} |
||||
} |
||||
|
||||
if fieldConvert, ok := fieldValue.Interface().(convert.Conversion); ok { |
||||
data, err := fieldConvert.ToDB() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if col.SQLType.IsBlob() { |
||||
return data, nil |
||||
} |
||||
if nil == data { |
||||
return nil, nil |
||||
} |
||||
return string(data), nil |
||||
} |
||||
|
||||
fieldType := fieldValue.Type() |
||||
k := fieldType.Kind() |
||||
if k == reflect.Ptr { |
||||
if fieldValue.IsNil() { |
||||
return nil, nil |
||||
} else if !fieldValue.IsValid() { |
||||
return nil, nil |
||||
} else { |
||||
// !nashtsai! deference pointer type to instance type
|
||||
fieldValue = fieldValue.Elem() |
||||
fieldType = fieldValue.Type() |
||||
k = fieldType.Kind() |
||||
} |
||||
} |
||||
|
||||
switch k { |
||||
case reflect.Bool: |
||||
return fieldValue.Bool(), nil |
||||
case reflect.String: |
||||
return fieldValue.String(), nil |
||||
case reflect.Struct: |
||||
if fieldType.ConvertibleTo(schemas.TimeType) { |
||||
t := fieldValue.Convert(schemas.TimeType).Interface().(time.Time) |
||||
tf := dialects.FormatColumnTime(statement.dialect, statement.defaultTimeZone, col, t) |
||||
return tf, nil |
||||
} else if fieldType.ConvertibleTo(nullFloatType) { |
||||
t := fieldValue.Convert(nullFloatType).Interface().(sql.NullFloat64) |
||||
if !t.Valid { |
||||
return nil, nil |
||||
} |
||||
return t.Float64, nil |
||||
} |
||||
|
||||
if !col.SQLType.IsJson() { |
||||
// !<winxxp>! 增加支持driver.Valuer接口的结构,如sql.NullString
|
||||
if v, ok := fieldValue.Interface().(driver.Valuer); ok { |
||||
return v.Value() |
||||
} |
||||
|
||||
fieldTable, err := statement.tagParser.ParseWithCache(fieldValue) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if len(fieldTable.PrimaryKeys) == 1 { |
||||
pkField := reflect.Indirect(fieldValue).FieldByName(fieldTable.PKColumns()[0].FieldName) |
||||
return pkField.Interface(), nil |
||||
} |
||||
return nil, fmt.Errorf("no primary key for col %v", col.Name) |
||||
} |
||||
|
||||
if col.SQLType.IsText() { |
||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return string(bytes), nil |
||||
} else if col.SQLType.IsBlob() { |
||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return bytes, nil |
||||
} |
||||
return nil, fmt.Errorf("Unsupported type %v", fieldValue.Type()) |
||||
case reflect.Complex64, reflect.Complex128: |
||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return string(bytes), nil |
||||
case reflect.Array, reflect.Slice, reflect.Map: |
||||
if !fieldValue.IsValid() { |
||||
return fieldValue.Interface(), nil |
||||
} |
||||
|
||||
if col.SQLType.IsText() { |
||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return string(bytes), nil |
||||
} else if col.SQLType.IsBlob() { |
||||
var bytes []byte |
||||
var err error |
||||
if (k == reflect.Slice) && |
||||
(fieldValue.Type().Elem().Kind() == reflect.Uint8) { |
||||
bytes = fieldValue.Bytes() |
||||
} else { |
||||
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
return bytes, nil |
||||
} |
||||
return nil, ErrUnSupportedType |
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: |
||||
return int64(fieldValue.Uint()), nil |
||||
default: |
||||
return fieldValue.Interface(), nil |
||||
} |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue