mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql: change boolean operators wrapping (#678)
The reason is to parentheses only when it's necessary.
This commit is contained in:
@@ -46,13 +46,20 @@ func (d *MySQL) init(ctx context.Context, tx dialect.Tx) error {
|
||||
|
||||
func (d *MySQL) tableExist(ctx context.Context, tx dialect.Tx, name string) (bool, error) {
|
||||
query, args := sql.Select(sql.Count("*")).From(sql.Table("INFORMATION_SCHEMA.TABLES").Unquote()).
|
||||
Where(sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")).And().EQ("TABLE_NAME", name)).Query()
|
||||
Where(sql.And(
|
||||
sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")),
|
||||
sql.EQ("TABLE_NAME", name),
|
||||
)).Query()
|
||||
return exist(ctx, tx, query, args...)
|
||||
}
|
||||
|
||||
func (d *MySQL) fkExist(ctx context.Context, tx dialect.Tx, name string) (bool, error) {
|
||||
query, args := sql.Select(sql.Count("*")).From(sql.Table("INFORMATION_SCHEMA.TABLE_CONSTRAINTS").Unquote()).
|
||||
Where(sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")).And().EQ("CONSTRAINT_TYPE", "FOREIGN KEY").And().EQ("CONSTRAINT_NAME", name)).Query()
|
||||
Where(sql.And(
|
||||
sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")),
|
||||
sql.EQ("CONSTRAINT_TYPE", "FOREIGN KEY"),
|
||||
sql.EQ("CONSTRAINT_NAME", name),
|
||||
)).Query()
|
||||
return exist(ctx, tx, query, args...)
|
||||
}
|
||||
|
||||
@@ -61,7 +68,10 @@ func (d *MySQL) table(ctx context.Context, tx dialect.Tx, name string) (*Table,
|
||||
rows := &sql.Rows{}
|
||||
query, args := sql.Select("column_name", "column_type", "is_nullable", "column_key", "column_default", "extra", "character_set_name", "collation_name").
|
||||
From(sql.Table("INFORMATION_SCHEMA.COLUMNS").Unquote()).
|
||||
Where(sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")).And().EQ("TABLE_NAME", name)).Query()
|
||||
Where(sql.And(
|
||||
sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")),
|
||||
sql.EQ("TABLE_NAME", name)),
|
||||
).Query()
|
||||
if err := tx.Query(ctx, query, args, rows); err != nil {
|
||||
return nil, fmt.Errorf("mysql: reading table description %v", err)
|
||||
}
|
||||
@@ -100,7 +110,10 @@ func (d *MySQL) indexes(ctx context.Context, tx dialect.Tx, name string) ([]*Ind
|
||||
rows := &sql.Rows{}
|
||||
query, args := sql.Select("index_name", "column_name", "non_unique", "seq_in_index").
|
||||
From(sql.Table("INFORMATION_SCHEMA.STATISTICS").Unquote()).
|
||||
Where(sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")).And().EQ("TABLE_NAME", name)).
|
||||
Where(sql.And(
|
||||
sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")),
|
||||
sql.EQ("TABLE_NAME", name),
|
||||
)).
|
||||
OrderBy("index_name", "seq_in_index").
|
||||
Query()
|
||||
if err := tx.Query(ctx, query, args, rows); err != nil {
|
||||
@@ -125,7 +138,10 @@ func (d *MySQL) verifyRange(ctx context.Context, tx dialect.Tx, t *Table, expect
|
||||
rows := &sql.Rows{}
|
||||
query, args := sql.Select("AUTO_INCREMENT").
|
||||
From(sql.Table("INFORMATION_SCHEMA.TABLES").Unquote()).
|
||||
Where(sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")).And().EQ("TABLE_NAME", t.Name)).
|
||||
Where(sql.And(
|
||||
sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")),
|
||||
sql.EQ("TABLE_NAME", t.Name),
|
||||
)).
|
||||
Query()
|
||||
if err := tx.Query(ctx, query, args, rows); err != nil {
|
||||
return fmt.Errorf("mysql: query auto_increment %v", err)
|
||||
@@ -521,13 +537,13 @@ func parseColumn(typ string) (parts []string, size int64, unsigned bool, err err
|
||||
// fkNames returns the foreign-key names of a column.
|
||||
func fkNames(ctx context.Context, tx dialect.Tx, table, column string) ([]string, error) {
|
||||
query, args := sql.Select("CONSTRAINT_NAME").From(sql.Table("INFORMATION_SCHEMA.KEY_COLUMN_USAGE").Unquote()).
|
||||
Where(sql.
|
||||
EQ("TABLE_NAME", table).
|
||||
And().EQ("COLUMN_NAME", column).
|
||||
Where(sql.And(
|
||||
sql.EQ("TABLE_NAME", table),
|
||||
sql.EQ("COLUMN_NAME", column),
|
||||
// NULL for unique and primary-key constraints.
|
||||
And().NotNull("POSITION_IN_UNIQUE_CONSTRAINT").
|
||||
And().EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")),
|
||||
).
|
||||
sql.NotNull("POSITION_IN_UNIQUE_CONSTRAINT"),
|
||||
sql.EQ("TABLE_SCHEMA", sql.Raw("(SELECT DATABASE())")),
|
||||
)).
|
||||
Query()
|
||||
var (
|
||||
names []string
|
||||
|
||||
@@ -700,7 +700,7 @@ func TestMySQL_Create(t *testing.T) {
|
||||
AddRow("old_index", "old", "0", "1").
|
||||
AddRow("parent_id", "parent_id", "0", "1"))
|
||||
mock.fkExists("parent_id", true)
|
||||
mock.ExpectQuery(escape("SELECT `column_name` FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS t1 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t2 ON `t1`.`constraint_name` = `t2`.`constraint_name` WHERE (`t2`.`constraint_type` = 'FOREIGN KEY') AND (`t2`.`table_schema` = (SELECT DATABASE())) AND (`t1`.`table_schema` = (SELECT DATABASE())) AND (`t2`.`constraint_name` = ?)")).
|
||||
mock.ExpectQuery(escape("SELECT `column_name` FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS t1 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t2 ON `t1`.`constraint_name` = `t2`.`constraint_name` WHERE `t2`.`constraint_type` = 'FOREIGN KEY' AND `t2`.`table_schema` = (SELECT DATABASE()) AND `t1`.`table_schema` = (SELECT DATABASE()) AND `t2`.`constraint_name` = ?")).
|
||||
WithArgs("parent_id").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"COLUMN_NAME"}).
|
||||
AddRow("parent_id"))
|
||||
|
||||
@@ -52,7 +52,10 @@ func (d *Postgres) init(ctx context.Context, tx dialect.Tx) error {
|
||||
func (d *Postgres) tableExist(ctx context.Context, tx dialect.Tx, name string) (bool, error) {
|
||||
query, args := sql.Dialect(dialect.Postgres).
|
||||
Select(sql.Count("*")).From(sql.Table("INFORMATION_SCHEMA.TABLES").Unquote()).
|
||||
Where(sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")).And().EQ("table_name", name)).Query()
|
||||
Where(sql.And(
|
||||
sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")),
|
||||
sql.EQ("table_name", name),
|
||||
)).Query()
|
||||
return exist(ctx, tx, query, args...)
|
||||
}
|
||||
|
||||
@@ -60,7 +63,11 @@ func (d *Postgres) tableExist(ctx context.Context, tx dialect.Tx, name string) (
|
||||
func (d *Postgres) fkExist(ctx context.Context, tx dialect.Tx, name string) (bool, error) {
|
||||
query, args := sql.Dialect(dialect.Postgres).
|
||||
Select(sql.Count("*")).From(sql.Table("INFORMATION_SCHEMA.TABLE_CONSTRAINTS").Unquote()).
|
||||
Where(sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")).And().EQ("constraint_type", "FOREIGN KEY").And().EQ("constraint_name", name)).Query()
|
||||
Where(sql.And(
|
||||
sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")),
|
||||
sql.EQ("constraint_type", "FOREIGN KEY"),
|
||||
sql.EQ("constraint_name", name),
|
||||
)).Query()
|
||||
return exist(ctx, tx, query, args...)
|
||||
}
|
||||
|
||||
@@ -82,7 +89,10 @@ func (d *Postgres) table(ctx context.Context, tx dialect.Tx, name string) (*Tabl
|
||||
query, args := sql.Dialect(dialect.Postgres).
|
||||
Select("column_name", "data_type", "is_nullable", "column_default").
|
||||
From(sql.Table("INFORMATION_SCHEMA.COLUMNS").Unquote()).
|
||||
Where(sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")).And().EQ("table_name", name)).Query()
|
||||
Where(sql.And(
|
||||
sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")),
|
||||
sql.EQ("table_name", name),
|
||||
)).Query()
|
||||
if err := tx.Query(ctx, query, args, rows); err != nil {
|
||||
return nil, fmt.Errorf("postgres: reading table description %v", err)
|
||||
}
|
||||
@@ -373,7 +383,11 @@ func (d *Postgres) dropIndex(ctx context.Context, tx dialect.Tx, idx *Index, tab
|
||||
}
|
||||
query, args := sql.Dialect(dialect.Postgres).
|
||||
Select(sql.Count("*")).From(sql.Table("INFORMATION_SCHEMA.TABLE_CONSTRAINTS").Unquote()).
|
||||
Where(sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")).And().EQ("constraint_type", "UNIQUE").And().EQ("constraint_name", name)).
|
||||
Where(sql.And(
|
||||
sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")),
|
||||
sql.EQ("constraint_type", "UNIQUE"),
|
||||
sql.EQ("constraint_name", name),
|
||||
)).
|
||||
Query()
|
||||
exists, err := exist(ctx, tx, query, args...)
|
||||
if err != nil {
|
||||
|
||||
@@ -36,7 +36,10 @@ func (d *SQLite) init(ctx context.Context, tx dialect.Tx) error {
|
||||
func (d *SQLite) tableExist(ctx context.Context, tx dialect.Tx, name string) (bool, error) {
|
||||
query, args := sql.Select().Count().
|
||||
From(sql.Table("sqlite_master")).
|
||||
Where(sql.EQ("type", "table").And().EQ("name", name)).
|
||||
Where(sql.And(
|
||||
sql.EQ("type", "table"),
|
||||
sql.EQ("name", name),
|
||||
)).
|
||||
Query()
|
||||
return exist(ctx, tx, query, args...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user