diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-03-25 22:32:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-25 16:32:23 +0200 |
commit | 73f5ca70de9254b3a031570de8a8a5f3ca00ed95 (patch) | |
tree | 2fc6a0b797ba58b21873d67dce23db649ae0072c /vendor/xorm.io/xorm/dialects | |
parent | b10cb7b258ec3bb5091a214a2ffeedec06c207ff (diff) | |
download | gitea-73f5ca70de9254b3a031570de8a8a5f3ca00ed95.tar.gz gitea-73f5ca70de9254b3a031570de8a8a5f3ca00ed95.zip |
Upgrade xorm to v1.0.1 and fix start fail with potgres (#10819)
* upgrade xorm to v1.0.1
* fix start fail with postgres
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'vendor/xorm.io/xorm/dialects')
-rw-r--r-- | vendor/xorm.io/xorm/dialects/dialect.go | 38 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/dialects/driver.go | 8 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/dialects/mssql.go | 24 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/dialects/mysql.go | 20 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/dialects/oracle.go | 26 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/dialects/postgres.go | 107 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/dialects/sqlite3.go | 24 | ||||
-rw-r--r-- | vendor/xorm.io/xorm/dialects/table_name.go | 1 |
8 files changed, 129 insertions, 119 deletions
diff --git a/vendor/xorm.io/xorm/dialects/dialect.go b/vendor/xorm.io/xorm/dialects/dialect.go index 4fdf35e94e..3c98a4a5b0 100644 --- a/vendor/xorm.io/xorm/dialects/dialect.go +++ b/vendor/xorm.io/xorm/dialects/dialect.go @@ -32,19 +32,18 @@ type URI struct { // SetSchema set schema func (uri *URI) SetSchema(schema string) { + // hack me if uri.DBType == schemas.POSTGRES { - uri.Schema = schema + uri.Schema = strings.TrimSpace(schema) } } // Dialect represents a kind of database type Dialect interface { - Init(*core.DB, *URI) error + Init(*URI) error URI() *URI - DB() *core.DB SQLType(*schemas.Column) string FormatBytes(b []byte) string - DefaultSchema() string IsReserved(string) bool Quoter() schemas.Quoter @@ -52,18 +51,18 @@ type Dialect interface { AutoIncrStr() string - GetIndexes(ctx context.Context, tableName string) (map[string]*schemas.Index, error) + GetIndexes(queryer core.Queryer, 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) + GetTables(queryer core.Queryer, ctx context.Context) ([]*schemas.Table, error) + IsTableExist(queryer core.Queryer, 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) + GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) + IsColumnExist(queryer core.Queryer, ctx context.Context, tableName string, colName string) (bool, error) AddColumnSQL(tableName string, col *schemas.Column) string ModifyColumnSQL(tableName string, col *schemas.Column) string @@ -75,7 +74,6 @@ type Dialect interface { // 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 @@ -85,16 +83,8 @@ 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 +func (b *Base) Init(dialect Dialect, uri *URI) error { + b.dialect, b.uri = dialect, uri return nil } @@ -160,8 +150,8 @@ func (db *Base) DropTableSQL(tableName string) (string, bool) { 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...) +func (db *Base) HasRecords(queryer core.Queryer, ctx context.Context, query string, args ...interface{}) (bool, error) { + rows, err := queryer.QueryContext(ctx, query, args...) if err != nil { return false, err } @@ -173,7 +163,7 @@ func (db *Base) HasRecords(ctx context.Context, query string, args ...interface{ return false, nil } -func (db *Base) IsColumnExist(ctx context.Context, tableName, colName string) (bool, error) { +func (db *Base) IsColumnExist(queryer core.Queryer, 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 = ?", @@ -184,7 +174,7 @@ func (db *Base) IsColumnExist(ctx context.Context, tableName, colName string) (b quote("TABLE_NAME"), quote("COLUMN_NAME"), ) - return db.HasRecords(ctx, query, db.uri.DBName, tableName, colName) + return db.HasRecords(queryer, ctx, query, db.uri.DBName, tableName, colName) } func (db *Base) AddColumnSQL(tableName string, col *schemas.Column) string { diff --git a/vendor/xorm.io/xorm/dialects/driver.go b/vendor/xorm.io/xorm/dialects/driver.go index 89d21bfc34..ae3afe4254 100644 --- a/vendor/xorm.io/xorm/dialects/driver.go +++ b/vendor/xorm.io/xorm/dialects/driver.go @@ -6,8 +6,6 @@ package dialects import ( "fmt" - - "xorm.io/xorm/core" ) type Driver interface { @@ -53,11 +51,7 @@ func OpenDialect(driverName, connstr string) (Dialect, error) { 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) + dialect.Init(uri) return dialect, nil } diff --git a/vendor/xorm.io/xorm/dialects/mssql.go b/vendor/xorm.io/xorm/dialects/mssql.go index dd3f4247eb..92457ff928 100644 --- a/vendor/xorm.io/xorm/dialects/mssql.go +++ b/vendor/xorm.io/xorm/dialects/mssql.go @@ -212,9 +212,9 @@ type mssql struct { Base } -func (db *mssql) Init(d *core.DB, uri *URI) error { +func (db *mssql) Init(uri *URI) error { db.quoter = mssqlQuoter - return db.Base.Init(d, db, uri) + return db.Base.Init(db, uri) } func (db *mssql) SQLType(c *schemas.Column) string { @@ -319,18 +319,18 @@ func (db *mssql) IndexCheckSQL(tableName, idxName string) (string, []interface{} return sql, args } -func (db *mssql) IsColumnExist(ctx context.Context, tableName, colName string) (bool, error) { +func (db *mssql) IsColumnExist(queryer core.Queryer, ctx context.Context, tableName, colName string) (bool, error) { query := `SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "COLUMN_NAME" = ?` - return db.HasRecords(ctx, query, tableName, colName) + return db.HasRecords(queryer, ctx, query, tableName, colName) } -func (db *mssql) IsTableExist(ctx context.Context, tableName string) (bool, error) { +func (db *mssql) IsTableExist(queryer core.Queryer, ctx context.Context, tableName string) (bool, error) { sql := "select * from sysobjects where id = object_id(N'" + tableName + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1" - return db.HasRecords(ctx, sql) + return db.HasRecords(queryer, ctx, sql) } -func (db *mssql) GetColumns(ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { +func (db *mssql) GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { args := []interface{}{} s := `select a.name as name, b.name as ctype,a.max_length,a.precision,a.scale,a.is_nullable as nullable, "default_is_null" = (CASE WHEN c.text is null THEN 1 ELSE 0 END), @@ -346,7 +346,7 @@ func (db *mssql) GetColumns(ctx context.Context, tableName string) ([]string, ma ) as p on p.object_id = a.object_id AND p.column_id = a.column_id where a.object_id=object_id('` + tableName + `')` - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, nil, err } @@ -401,11 +401,11 @@ func (db *mssql) GetColumns(ctx context.Context, tableName string) ([]string, ma return colSeq, cols, nil } -func (db *mssql) GetTables(ctx context.Context) ([]*schemas.Table, error) { +func (db *mssql) GetTables(queryer core.Queryer, ctx context.Context) ([]*schemas.Table, error) { args := []interface{}{} s := `select name from sysobjects where xtype ='U'` - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } @@ -425,7 +425,7 @@ func (db *mssql) GetTables(ctx context.Context) ([]*schemas.Table, error) { return tables, nil } -func (db *mssql) GetIndexes(ctx context.Context, tableName string) (map[string]*schemas.Index, error) { +func (db *mssql) GetIndexes(queryer core.Queryer, ctx context.Context, tableName string) (map[string]*schemas.Index, error) { args := []interface{}{tableName} s := `SELECT IXS.NAME AS [INDEX_NAME], @@ -439,7 +439,7 @@ AND IXCS.COLUMN_ID=C.COLUMN_ID WHERE IXS.TYPE_DESC='NONCLUSTERED' and OBJECT_NAME(IXS.OBJECT_ID) =? ` - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } diff --git a/vendor/xorm.io/xorm/dialects/mysql.go b/vendor/xorm.io/xorm/dialects/mysql.go index b75986809b..289e0ec7e8 100644 --- a/vendor/xorm.io/xorm/dialects/mysql.go +++ b/vendor/xorm.io/xorm/dialects/mysql.go @@ -179,9 +179,9 @@ type mysql struct { rowFormat string } -func (db *mysql) Init(d *core.DB, uri *URI) error { +func (db *mysql) Init(uri *URI) error { db.quoter = mysqlQuoter - return db.Base.Init(d, db, uri) + return db.Base.Init(db, uri) } func (db *mysql) SetParams(params map[string]string) { @@ -286,9 +286,9 @@ func (db *mysql) IndexCheckSQL(tableName, idxName string) (string, []interface{} return sql, args } -func (db *mysql) IsTableExist(ctx context.Context, tableName string) (bool, error) { +func (db *mysql) IsTableExist(queryer core.Queryer, ctx context.Context, tableName string) (bool, error) { sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?" - return db.HasRecords(ctx, sql, db.uri.DBName, tableName) + return db.HasRecords(queryer, ctx, sql, db.uri.DBName, tableName) } func (db *mysql) AddColumnSQL(tableName string, col *schemas.Column) string { @@ -301,12 +301,12 @@ func (db *mysql) AddColumnSQL(tableName string, col *schemas.Column) string { return sql } -func (db *mysql) GetColumns(ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { +func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { args := []interface{}{db.uri.DBName, tableName} s := "SELECT `COLUMN_NAME`, `IS_NULLABLE`, `COLUMN_DEFAULT`, `COLUMN_TYPE`," + " `COLUMN_KEY`, `EXTRA`,`COLUMN_COMMENT` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?" - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, nil, err } @@ -411,12 +411,12 @@ func (db *mysql) GetColumns(ctx context.Context, tableName string) ([]string, ma return colSeq, cols, nil } -func (db *mysql) GetTables(ctx context.Context) ([]*schemas.Table, error) { +func (db *mysql) GetTables(queryer core.Queryer, ctx context.Context) ([]*schemas.Table, error) { args := []interface{}{db.uri.DBName} s := "SELECT `TABLE_NAME`, `ENGINE`, `AUTO_INCREMENT`, `TABLE_COMMENT` from " + "`INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? AND (`ENGINE`='MyISAM' OR `ENGINE` = 'InnoDB' OR `ENGINE` = 'TokuDB')" - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } @@ -459,11 +459,11 @@ func (db *mysql) SetQuotePolicy(quotePolicy QuotePolicy) { } } -func (db *mysql) GetIndexes(ctx context.Context, tableName string) (map[string]*schemas.Index, error) { +func (db *mysql) GetIndexes(queryer core.Queryer, ctx context.Context, tableName string) (map[string]*schemas.Index, error) { args := []interface{}{db.uri.DBName, tableName} s := "SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?" - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } diff --git a/vendor/xorm.io/xorm/dialects/oracle.go b/vendor/xorm.io/xorm/dialects/oracle.go index c48d32b973..db823e95c0 100644 --- a/vendor/xorm.io/xorm/dialects/oracle.go +++ b/vendor/xorm.io/xorm/dialects/oracle.go @@ -506,9 +506,9 @@ type oracle struct { Base } -func (db *oracle) Init(d *core.DB, uri *URI) error { +func (db *oracle) Init(uri *URI) error { db.quoter = oracleQuoter - return db.Base.Init(d, db, uri) + return db.Base.Init(db, uri) } func (db *oracle) SQLType(c *schemas.Column) string { @@ -611,23 +611,23 @@ func (db *oracle) IndexCheckSQL(tableName, idxName string) (string, []interface{ `WHERE TABLE_NAME = :1 AND INDEX_NAME = :2`, args } -func (db *oracle) IsTableExist(ctx context.Context, tableName string) (bool, error) { - return db.HasRecords(ctx, `SELECT table_name FROM user_tables WHERE table_name = :1`, tableName) +func (db *oracle) IsTableExist(queryer core.Queryer, ctx context.Context, tableName string) (bool, error) { + return db.HasRecords(queryer, ctx, `SELECT table_name FROM user_tables WHERE table_name = :1`, tableName) } -func (db *oracle) IsColumnExist(ctx context.Context, tableName, colName string) (bool, error) { +func (db *oracle) IsColumnExist(queryer core.Queryer, ctx context.Context, tableName, colName string) (bool, error) { args := []interface{}{tableName, colName} query := "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = :1" + " AND column_name = :2" - return db.HasRecords(ctx, query, args...) + return db.HasRecords(queryer, ctx, query, args...) } -func (db *oracle) GetColumns(ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { +func (db *oracle) GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { args := []interface{}{tableName} s := "SELECT column_name,data_default,data_type,data_length,data_precision,data_scale," + "nullable FROM USER_TAB_COLUMNS WHERE table_name = :1" - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, nil, err } @@ -719,11 +719,11 @@ func (db *oracle) GetColumns(ctx context.Context, tableName string) ([]string, m return colSeq, cols, nil } -func (db *oracle) GetTables(ctx context.Context) ([]*schemas.Table, error) { +func (db *oracle) GetTables(queryer core.Queryer, ctx context.Context) ([]*schemas.Table, error) { args := []interface{}{} s := "SELECT table_name FROM user_tables" - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } @@ -742,12 +742,12 @@ func (db *oracle) GetTables(ctx context.Context) ([]*schemas.Table, error) { return tables, nil } -func (db *oracle) GetIndexes(ctx context.Context, tableName string) (map[string]*schemas.Index, error) { +func (db *oracle) GetIndexes(queryer core.Queryer, ctx context.Context, tableName string) (map[string]*schemas.Index, error) { args := []interface{}{tableName} s := "SELECT t.column_name,i.uniqueness,i.index_name FROM user_ind_columns t,user_indexes i " + "WHERE t.index_name = i.index_name and t.table_name = i.table_name and t.table_name =:1" - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } @@ -842,7 +842,7 @@ func (p *oci8Driver) Parse(driverName, dataSourceName string) (*URI, error) { db.DBName = match } } - if db.DBName == "" { + if db.DBName == "" && len(matches) != 0 { return nil, errors.New("dbname is empty") } return db, nil diff --git a/vendor/xorm.io/xorm/dialects/postgres.go b/vendor/xorm.io/xorm/dialects/postgres.go index 0a851fe26f..16213c7629 100644 --- a/vendor/xorm.io/xorm/dialects/postgres.go +++ b/vendor/xorm.io/xorm/dialects/postgres.go @@ -770,22 +770,25 @@ var ( postgresQuoter = schemas.Quoter{'"', '"', schemas.AlwaysReserve} ) -const postgresPublicSchema = "public" +var ( + // DefaultPostgresSchema default postgres schema + DefaultPostgresSchema = "public" +) type postgres struct { Base } -func (db *postgres) Init(d *core.DB, uri *URI) error { +func (db *postgres) Init(uri *URI) error { db.quoter = postgresQuoter - err := db.Base.Init(d, db, uri) - if err != nil { - return err - } - if db.uri.Schema == "" { - db.uri.Schema = postgresPublicSchema + return db.Base.Init(db, uri) +} + +func (db *postgres) getSchema() string { + if db.uri.Schema != "" { + return db.uri.Schema } - return nil + return DefaultPostgresSchema } func (db *postgres) needQuote(name string) bool { @@ -817,10 +820,6 @@ func (db *postgres) SetQuotePolicy(quotePolicy QuotePolicy) { } } -func (db *postgres) DefaultSchema() string { - return postgresPublicSchema -} - func (db *postgres) SQLType(c *schemas.Column) string { var res string switch t := c.SQLType.Name; t { @@ -932,32 +931,32 @@ func (db *postgres) CreateTableSQL(table *schemas.Table, tableName string) ([]st } func (db *postgres) IndexCheckSQL(tableName, idxName string) (string, []interface{}) { - if len(db.uri.Schema) == 0 { + if len(db.getSchema()) == 0 { args := []interface{}{tableName, idxName} return `SELECT indexname FROM pg_indexes WHERE tablename = ? AND indexname = ?`, args } - args := []interface{}{db.uri.Schema, tableName, idxName} + args := []interface{}{db.getSchema(), tableName, idxName} return `SELECT indexname FROM pg_indexes ` + `WHERE schemaname = ? AND tablename = ? AND indexname = ?`, args } -func (db *postgres) IsTableExist(ctx context.Context, tableName string) (bool, error) { - if len(db.uri.Schema) == 0 { - return db.HasRecords(ctx, `SELECT tablename FROM pg_tables WHERE tablename = $1`, tableName) +func (db *postgres) IsTableExist(queryer core.Queryer, ctx context.Context, tableName string) (bool, error) { + if len(db.getSchema()) == 0 { + return db.HasRecords(queryer, ctx, `SELECT tablename FROM pg_tables WHERE tablename = $1`, tableName) } - return db.HasRecords(ctx, `SELECT tablename FROM pg_tables WHERE schemaname = $1 AND tablename = $2`, - db.uri.Schema, tableName) + return db.HasRecords(queryer, ctx, `SELECT tablename FROM pg_tables WHERE schemaname = $1 AND tablename = $2`, + db.getSchema(), tableName) } func (db *postgres) ModifyColumnSQL(tableName string, col *schemas.Column) string { - if len(db.uri.Schema) == 0 || strings.Contains(tableName, ".") { + if len(db.getSchema()) == 0 || strings.Contains(tableName, ".") { return fmt.Sprintf("alter table %s ALTER COLUMN %s TYPE %s", tableName, col.Name, db.SQLType(col)) } return fmt.Sprintf("alter table %s.%s ALTER COLUMN %s TYPE %s", - db.uri.Schema, tableName, col.Name, db.SQLType(col)) + db.getSchema(), tableName, col.Name, db.SQLType(col)) } func (db *postgres) DropIndexSQL(tableName string, index *schemas.Index) string { @@ -974,23 +973,23 @@ func (db *postgres) DropIndexSQL(tableName string, index *schemas.Index) string idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name) } } - if db.uri.Schema != "" { - idxName = db.uri.Schema + "." + idxName + if db.getSchema() != "" { + idxName = db.getSchema() + "." + idxName } return fmt.Sprintf("DROP INDEX %v", db.Quoter().Quote(idxName)) } -func (db *postgres) IsColumnExist(ctx context.Context, tableName, colName string) (bool, error) { - args := []interface{}{db.uri.Schema, tableName, colName} +func (db *postgres) IsColumnExist(queryer core.Queryer, ctx context.Context, tableName, colName string) (bool, error) { + args := []interface{}{db.getSchema(), tableName, colName} query := "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = $1 AND table_name = $2" + " AND column_name = $3" - if len(db.uri.Schema) == 0 { + if len(db.getSchema()) == 0 { args = []interface{}{tableName, colName} query = "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1" + " AND column_name = $2" } - rows, err := db.DB().QueryContext(ctx, query, args...) + rows, err := queryer.QueryContext(ctx, query, args...) if err != nil { return false, err } @@ -999,8 +998,8 @@ func (db *postgres) IsColumnExist(ctx context.Context, tableName, colName string return rows.Next(), nil } -func (db *postgres) GetColumns(ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { - args := []interface{}{db.uri.Schema, tableName, db.uri.Schema} +func (db *postgres) GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { + args := []interface{}{tableName} s := `SELECT column_name, column_default, is_nullable, data_type, character_maximum_length, CASE WHEN p.contype = 'p' THEN true ELSE false END AS primarykey, CASE WHEN p.contype = 'u' THEN true ELSE false END AS uniquekey @@ -1011,9 +1010,17 @@ FROM pg_attribute f LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey) LEFT JOIN pg_class AS g ON p.confrelid = g.oid LEFT JOIN INFORMATION_SCHEMA.COLUMNS s ON s.column_name=f.attname AND c.relname=s.table_name -WHERE n.nspname= $1 AND c.relkind = 'r'::char AND c.relname = $2 AND s.table_schema = $3 AND f.attnum > 0 ORDER BY f.attnum;` +WHERE n.nspname= s.table_schema AND c.relkind = 'r'::char AND c.relname = $1%s AND f.attnum > 0 ORDER BY f.attnum;` - rows, err := db.DB().QueryContext(ctx, s, args...) + schema := db.getSchema() + if schema != "" { + s = fmt.Sprintf(s, "AND s.table_schema = $2") + args = append(args, schema) + } else { + s = fmt.Sprintf(s, "") + } + + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, nil, err } @@ -1132,15 +1139,16 @@ WHERE n.nspname= $1 AND c.relkind = 'r'::char AND c.relname = $2 AND s.table_sch return colSeq, cols, nil } -func (db *postgres) GetTables(ctx context.Context) ([]*schemas.Table, error) { +func (db *postgres) GetTables(queryer core.Queryer, ctx context.Context) ([]*schemas.Table, error) { args := []interface{}{} s := "SELECT tablename FROM pg_tables" - if len(db.uri.Schema) != 0 { - args = append(args, db.uri.Schema) + schema := db.getSchema() + if schema != "" { + args = append(args, schema) s = s + " WHERE schemaname = $1" } - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } @@ -1171,15 +1179,15 @@ func getIndexColName(indexdef string) []string { return colNames } -func (db *postgres) GetIndexes(ctx context.Context, tableName string) (map[string]*schemas.Index, error) { +func (db *postgres) GetIndexes(queryer core.Queryer, ctx context.Context, tableName string) (map[string]*schemas.Index, error) { args := []interface{}{tableName} s := fmt.Sprintf("SELECT indexname, indexdef FROM pg_indexes WHERE tablename=$1") - if len(db.uri.Schema) != 0 { - args = append(args, db.uri.Schema) + if len(db.getSchema()) != 0 { + args = append(args, db.getSchema()) s = s + " AND schemaname=$2" } - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } @@ -1319,3 +1327,22 @@ func (pgx *pqDriverPgx) Parse(driverName, dataSourceName string) (*URI, error) { } return pgx.pqDriver.Parse(driverName, dataSourceName) } + +// QueryDefaultPostgresSchema returns the default postgres schema +func QueryDefaultPostgresSchema(ctx context.Context, queryer core.Queryer) (string, error) { + rows, err := queryer.QueryContext(ctx, "SHOW SEARCH_PATH") + if err != nil { + return "", err + } + defer rows.Close() + if rows.Next() { + var defaultSchema string + if err = rows.Scan(&defaultSchema); err != nil { + return "", err + } + parts := strings.Split(defaultSchema, ",") + return strings.TrimSpace(parts[len(parts)-1]), nil + } + + return "", errors.New("No default schema") +} diff --git a/vendor/xorm.io/xorm/dialects/sqlite3.go b/vendor/xorm.io/xorm/dialects/sqlite3.go index 710babe6db..0e95ebc72d 100644 --- a/vendor/xorm.io/xorm/dialects/sqlite3.go +++ b/vendor/xorm.io/xorm/dialects/sqlite3.go @@ -151,9 +151,9 @@ type sqlite3 struct { Base } -func (db *sqlite3) Init(d *core.DB, uri *URI) error { +func (db *sqlite3) Init(uri *URI) error { db.quoter = sqlite3Quoter - return db.Base.Init(d, db, uri) + return db.Base.Init(db, uri) } func (db *sqlite3) SetQuotePolicy(quotePolicy QuotePolicy) { @@ -225,8 +225,8 @@ func (db *sqlite3) IndexCheckSQL(tableName, idxName string) (string, []interface return "SELECT name FROM sqlite_master WHERE type='index' and name = ?", args } -func (db *sqlite3) IsTableExist(ctx context.Context, tableName string) (bool, error) { - return db.HasRecords(ctx, "SELECT name FROM sqlite_master WHERE type='table' and name = ?", tableName) +func (db *sqlite3) IsTableExist(queryer core.Queryer, ctx context.Context, tableName string) (bool, error) { + return db.HasRecords(queryer, ctx, "SELECT name FROM sqlite_master WHERE type='table' and name = ?", tableName) } func (db *sqlite3) DropIndexSQL(tableName string, index *schemas.Index) string { @@ -286,9 +286,9 @@ func (db *sqlite3) ForUpdateSQL(query string) string { return query } -func (db *sqlite3) IsColumnExist(ctx context.Context, tableName, colName string) (bool, error) { +func (db *sqlite3) IsColumnExist(queryer core.Queryer, ctx context.Context, tableName, colName string) (bool, error) { query := "SELECT * FROM " + tableName + " LIMIT 0" - rows, err := db.DB().QueryContext(ctx, query) + rows, err := queryer.QueryContext(ctx, query) if err != nil { return false, err } @@ -370,11 +370,11 @@ func parseString(colStr string) (*schemas.Column, error) { return col, nil } -func (db *sqlite3) GetColumns(ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { +func (db *sqlite3) GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) { args := []interface{}{tableName} s := "SELECT sql FROM sqlite_master WHERE type='table' and name = ?" - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, nil, err } @@ -427,11 +427,11 @@ func (db *sqlite3) GetColumns(ctx context.Context, tableName string) ([]string, return colSeq, cols, nil } -func (db *sqlite3) GetTables(ctx context.Context) ([]*schemas.Table, error) { +func (db *sqlite3) GetTables(queryer core.Queryer, ctx context.Context) ([]*schemas.Table, error) { args := []interface{}{} s := "SELECT name FROM sqlite_master WHERE type='table'" - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } @@ -452,11 +452,11 @@ func (db *sqlite3) GetTables(ctx context.Context) ([]*schemas.Table, error) { return tables, nil } -func (db *sqlite3) GetIndexes(ctx context.Context, tableName string) (map[string]*schemas.Index, error) { +func (db *sqlite3) GetIndexes(queryer core.Queryer, ctx context.Context, tableName string) (map[string]*schemas.Index, error) { args := []interface{}{tableName} s := "SELECT sql FROM sqlite_master WHERE type='index' and tbl_name = ?" - rows, err := db.DB().QueryContext(ctx, s, args...) + rows, err := queryer.QueryContext(ctx, s, args...) if err != nil { return nil, err } diff --git a/vendor/xorm.io/xorm/dialects/table_name.go b/vendor/xorm.io/xorm/dialects/table_name.go index a989b38639..e190cd4bfa 100644 --- a/vendor/xorm.io/xorm/dialects/table_name.go +++ b/vendor/xorm.io/xorm/dialects/table_name.go @@ -18,7 +18,6 @@ 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) } |