bug fixed #379
This commit is contained in:
parent
21d219c872
commit
5c116a34d6
18
doc.go
18
doc.go
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2013 - 2015 The Xorm Authors. All rights reserved.
|
// Copyright 2013 - 2016 The XORM Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD
|
// Use of this source code is governed by a BSD
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
@ -129,26 +129,28 @@ Attention: the above 7 methods should be the last chainable method.
|
||||||
|
|
||||||
5. Sql, let you custom SQL
|
5. Sql, let you custom SQL
|
||||||
|
|
||||||
engine.Sql("select * from user").Find()
|
var users []User
|
||||||
|
engine.Sql("select * from user").Find(&users)
|
||||||
|
|
||||||
6. Cols, Omit, Distinct
|
6. Cols, Omit, Distinct
|
||||||
|
|
||||||
engine.Cols("col1, col2").Find()
|
var users []*User
|
||||||
|
engine.Cols("col1, col2").Find(&users)
|
||||||
// SELECT col1, col2 FROM user
|
// SELECT col1, col2 FROM user
|
||||||
engine.Cols("col1", "col2").Where().Update(user)
|
engine.Cols("col1", "col2").Where().Update(user)
|
||||||
// UPDATE user set col1 = ?, col2 = ? Where ...
|
// UPDATE user set col1 = ?, col2 = ? Where ...
|
||||||
engine.Omit("col1").Find()
|
engine.Omit("col1").Find(&users)
|
||||||
// SELECT col2, col3 FROM user
|
// SELECT col2, col3 FROM user
|
||||||
engine.Omit("col1").Insert()
|
engine.Omit("col1").Insert(&user)
|
||||||
// INSERT INTO table (non-col1) VALUES ()
|
// INSERT INTO table (non-col1) VALUES ()
|
||||||
engine.Distinct("col1").Find()
|
engine.Distinct("col1").Find(&users)
|
||||||
// SELECT DISTINCT col1 FROM user
|
// SELECT DISTINCT col1 FROM user
|
||||||
|
|
||||||
7. Join, GroupBy, Having
|
7. Join, GroupBy, Having
|
||||||
|
|
||||||
engine.GroupBy("name").Having("name='xlw'").Find()
|
engine.GroupBy("name").Having("name='xlw'").Find(&users)
|
||||||
//SELECT * FROM user GROUP BY name HAVING name='xlw'
|
//SELECT * FROM user GROUP BY name HAVING name='xlw'
|
||||||
engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find()
|
engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find(&users)
|
||||||
//SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id
|
//SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id
|
||||||
|
|
||||||
More usage, please visit http://xorm.io/docs
|
More usage, please visit http://xorm.io/docs
|
||||||
|
|
|
@ -13,10 +13,6 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func init() {
|
|
||||||
// RegisterDialect("mssql", &mssql{})
|
|
||||||
// }
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
mssqlReservedWords = map[string]bool{
|
mssqlReservedWords = map[string]bool{
|
||||||
"ADD": true,
|
"ADD": true,
|
||||||
|
|
|
@ -12,10 +12,6 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func init() {
|
|
||||||
// core.RegisterDriver("mymysql", &mymysqlDriver{})
|
|
||||||
// }
|
|
||||||
|
|
||||||
type mymysqlDriver struct {
|
type mymysqlDriver struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,10 +15,6 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func init() {
|
|
||||||
// RegisterDialect("mysql", &mysql{})
|
|
||||||
// }
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
mysqlReservedWords = map[string]bool{
|
mysqlReservedWords = map[string]bool{
|
||||||
"ADD": true,
|
"ADD": true,
|
||||||
|
|
|
@ -11,10 +11,6 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func init() {
|
|
||||||
// core.RegisterDriver("mysql", &mysqlDriver{})
|
|
||||||
// }
|
|
||||||
|
|
||||||
type mysqlDriver struct {
|
type mysqlDriver struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,10 +11,6 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func init() {
|
|
||||||
// core.RegisterDriver("oci8", &oci8Driver{})
|
|
||||||
// }
|
|
||||||
|
|
||||||
type oci8Driver struct {
|
type oci8Driver struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,10 +11,6 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func init() {
|
|
||||||
// core.RegisterDriver("odbc", &odbcDriver{})
|
|
||||||
// }
|
|
||||||
|
|
||||||
type odbcDriver struct {
|
type odbcDriver struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,6 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func init() {
|
|
||||||
// RegisterDialect("oracle", &oracle{})
|
|
||||||
// }
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
oracleReservedWords = map[string]bool{
|
oracleReservedWords = map[string]bool{
|
||||||
"ACCESS": true,
|
"ACCESS": true,
|
||||||
|
|
|
@ -13,9 +13,6 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func init() {
|
|
||||||
// RegisterDialect("postgres", &postgres{})
|
|
||||||
// }
|
|
||||||
// from http://www.postgresql.org/docs/current/static/sql-keywords-appendix.html
|
// from http://www.postgresql.org/docs/current/static/sql-keywords-appendix.html
|
||||||
var (
|
var (
|
||||||
postgresReservedWords = map[string]bool{
|
postgresReservedWords = map[string]bool{
|
||||||
|
@ -913,7 +910,8 @@ func (db *postgres) IsColumnExist(tableName, colName string) (bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *postgres) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
func (db *postgres) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
||||||
args := []interface{}{tableName, db.URI().Schema}
|
// FIXME: the schema should be replaced by user custom's
|
||||||
|
args := []interface{}{tableName, "public"}
|
||||||
s := `SELECT column_name, column_default, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_precision_radix ,
|
s := `SELECT column_name, column_default, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_precision_radix ,
|
||||||
CASE WHEN p.contype = 'p' THEN true ELSE false END AS primarykey,
|
CASE WHEN p.contype = 'p' THEN true ELSE false END AS primarykey,
|
||||||
CASE WHEN p.contype = 'u' THEN true ELSE false END AS uniquekey
|
CASE WHEN p.contype = 'u' THEN true ELSE false END AS uniquekey
|
||||||
|
@ -986,11 +984,13 @@ WHERE c.relkind = 'r'::char AND c.relname = $1 AND s.table_schema = $2 AND f.att
|
||||||
col.SQLType = core.SQLType{core.Bool, 0, 0}
|
col.SQLType = core.SQLType{core.Bool, 0, 0}
|
||||||
case "time without time zone":
|
case "time without time zone":
|
||||||
col.SQLType = core.SQLType{core.Time, 0, 0}
|
col.SQLType = core.SQLType{core.Time, 0, 0}
|
||||||
|
case "oid":
|
||||||
|
col.SQLType = core.SQLType{core.BigInt, 0, 0}
|
||||||
default:
|
default:
|
||||||
col.SQLType = core.SQLType{strings.ToUpper(dataType), 0, 0}
|
col.SQLType = core.SQLType{strings.ToUpper(dataType), 0, 0}
|
||||||
}
|
}
|
||||||
if _, ok := core.SqlTypes[col.SQLType.Name]; !ok {
|
if _, ok := core.SqlTypes[col.SQLType.Name]; !ok {
|
||||||
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", dataType))
|
return nil, nil, errors.New(fmt.Sprintf("unknow colType: %v", dataType))
|
||||||
}
|
}
|
||||||
|
|
||||||
col.Length = maxLen
|
col.Length = maxLen
|
||||||
|
@ -1012,8 +1012,9 @@ WHERE c.relkind = 'r'::char AND c.relname = $1 AND s.table_schema = $2 AND f.att
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *postgres) GetTables() ([]*core.Table, error) {
|
func (db *postgres) GetTables() ([]*core.Table, error) {
|
||||||
args := []interface{}{}
|
// FIXME: replace public to user customrize schema
|
||||||
s := fmt.Sprintf("SELECT tablename FROM pg_tables where schemaname = '%s'", db.Uri.Schema)
|
args := []interface{}{"public"}
|
||||||
|
s := fmt.Sprintf("SELECT tablename FROM pg_tables WHERE schemaname = $1")
|
||||||
db.LogSQL(s, args)
|
db.LogSQL(s, args)
|
||||||
|
|
||||||
rows, err := db.DB().Query(s, args...)
|
rows, err := db.DB().Query(s, args...)
|
||||||
|
@ -1037,8 +1038,9 @@ func (db *postgres) GetTables() ([]*core.Table, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error) {
|
func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error) {
|
||||||
args := []interface{}{tableName}
|
// FIXME: replace the public schema to user specify schema
|
||||||
s := fmt.Sprintf("SELECT indexname, indexdef FROM pg_indexes WHERE schemaname='%s' AND tablename=$1", db.URI().Schema)
|
args := []interface{}{"public", tableName}
|
||||||
|
s := fmt.Sprintf("SELECT indexname, indexdef FROM pg_indexes WHERE schemaname=$1 AND tablename=$2")
|
||||||
db.LogSQL(s, args)
|
db.LogSQL(s, args)
|
||||||
|
|
||||||
rows, err := db.DB().Query(s, args...)
|
rows, err := db.DB().Query(s, args...)
|
||||||
|
|
|
@ -14,10 +14,6 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func init() {
|
|
||||||
// core.RegisterDriver("postgres", &pqDriver{})
|
|
||||||
// }
|
|
||||||
|
|
||||||
type pqDriver struct {
|
type pqDriver struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,9 +111,9 @@ func (p *pqDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
|
||||||
if db.DbName == "" {
|
if db.DbName == "" {
|
||||||
return nil, errors.New("dbname is empty")
|
return nil, errors.New("dbname is empty")
|
||||||
}
|
}
|
||||||
db.Schema = o.Get("schema")
|
/*db.Schema = o.Get("schema")
|
||||||
if len(db.Schema) == 0 {
|
if len(db.Schema) == 0 {
|
||||||
db.Schema = "public"
|
db.Schema = "public"
|
||||||
}
|
}*/
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3888,9 +3888,9 @@ func (session *Session) tbName(table *core.Table) string {
|
||||||
tbName = session.Statement.AltTableName
|
tbName = session.Statement.AltTableName
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(session.Engine.dialect.URI().Schema) > 0 {
|
/*if len(session.Engine.dialect.URI().Schema) > 0 {
|
||||||
return session.Engine.dialect.URI().Schema + "." + tbName
|
return session.Engine.dialect.URI().Schema + "." + tbName
|
||||||
}
|
}*/
|
||||||
return tbName
|
return tbName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -674,10 +674,10 @@ func (statement *Statement) TableName() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.RefTable != nil {
|
if statement.RefTable != nil {
|
||||||
schema := statement.Engine.dialect.URI().Schema
|
/*schema := statement.Engine.dialect.URI().Schema
|
||||||
if len(schema) > 0 {
|
if len(schema) > 0 {
|
||||||
return schema + "." + statement.RefTable.Name
|
return schema + "." + statement.RefTable.Name
|
||||||
}
|
}*/
|
||||||
return statement.RefTable.Name
|
return statement.RefTable.Name
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
|
|
Loading…
Reference in New Issue