Remove func QuoteStr() in interface Dialect (#51)
This commit is contained in:
parent
5d3ffb50de
commit
65822bd505
|
@ -73,7 +73,7 @@ func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable
|
||||||
|
|
||||||
// String generate column description string according dialect
|
// String generate column description string according dialect
|
||||||
func (col *Column) String(d Dialect) string {
|
func (col *Column) String(d Dialect) string {
|
||||||
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
|
sql := d.Quote(col.Name) + " "
|
||||||
|
|
||||||
sql += d.SqlType(col) + " "
|
sql += d.SqlType(col) + " "
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ func (col *Column) String(d Dialect) string {
|
||||||
|
|
||||||
// StringNoPk generate column description string according dialect without primary keys
|
// StringNoPk generate column description string according dialect without primary keys
|
||||||
func (col *Column) StringNoPk(d Dialect) string {
|
func (col *Column) StringNoPk(d Dialect) string {
|
||||||
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
|
sql := d.Quote(col.Name)
|
||||||
|
|
||||||
sql += d.SqlType(col) + " "
|
sql += d.SqlType(col) + " "
|
||||||
|
|
||||||
|
|
18
dialect.go
18
dialect.go
|
@ -40,9 +40,10 @@ type Dialect interface {
|
||||||
DriverName() string
|
DriverName() string
|
||||||
DataSourceName() string
|
DataSourceName() string
|
||||||
|
|
||||||
QuoteStr() string
|
|
||||||
IsReserved(string) bool
|
IsReserved(string) bool
|
||||||
Quote(string) string
|
Quote(string) string
|
||||||
|
// Deprecated: use Quote(string) string instead
|
||||||
|
QuoteStr() string
|
||||||
AndStr() string
|
AndStr() string
|
||||||
OrStr() string
|
OrStr() string
|
||||||
EqStr() string
|
EqStr() string
|
||||||
|
@ -70,8 +71,8 @@ type Dialect interface {
|
||||||
|
|
||||||
ForUpdateSql(query string) string
|
ForUpdateSql(query string) string
|
||||||
|
|
||||||
//CreateTableIfNotExists(table *Table, tableName, storeEngine, charset string) error
|
// CreateTableIfNotExists(table *Table, tableName, storeEngine, charset string) error
|
||||||
//MustDropTable(tableName string) error
|
// MustDropTable(tableName string) error
|
||||||
|
|
||||||
GetColumns(tableName string) ([]string, map[string]*Column, error)
|
GetColumns(tableName string) ([]string, map[string]*Column, error)
|
||||||
GetTables() ([]*Table, error)
|
GetTables() ([]*Table, error)
|
||||||
|
@ -173,8 +174,15 @@ func (db *Base) HasRecords(query string, args ...interface{}) (bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Base) IsColumnExist(tableName, colName string) (bool, error) {
|
func (db *Base) IsColumnExist(tableName, colName string) (bool, error) {
|
||||||
query := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
query := fmt.Sprintf(
|
||||||
query = strings.Replace(query, "`", db.dialect.QuoteStr(), -1)
|
"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)
|
return db.HasRecords(query, db.DbName, tableName, colName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
20
filter.go
20
filter.go
|
@ -19,7 +19,23 @@ type QuoteFilter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *QuoteFilter) Do(sql string, dialect Dialect, table *Table) string {
|
func (s *QuoteFilter) Do(sql string, dialect Dialect, table *Table) string {
|
||||||
return strings.Replace(sql, "`", dialect.QuoteStr(), -1)
|
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
|
// IdFilter filter SQL replace (id) to primary key column name
|
||||||
|
@ -35,7 +51,7 @@ func NewQuoter(dialect Dialect) *Quoter {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Quoter) Quote(content string) string {
|
func (q *Quoter) Quote(content string) string {
|
||||||
return q.dialect.QuoteStr() + content + q.dialect.QuoteStr()
|
return q.dialect.Quote(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IdFilter) Do(sql string, dialect Dialect, table *Table) string {
|
func (i *IdFilter) Do(sql string, dialect Dialect, table *Table) string {
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type quoterOnly struct {
|
||||||
|
Dialect
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *quoterOnly) Quote(item string) string {
|
||||||
|
return "[" + item + "]"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuoteFilter_Do(t *testing.T) {
|
||||||
|
f := QuoteFilter{}
|
||||||
|
sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
||||||
|
res := f.Do(sql, new(quoterOnly), nil)
|
||||||
|
assert.EqualValues(t,
|
||||||
|
"SELECT [COLUMN_NAME] FROM [INFORMATION_SCHEMA].[COLUMNS] WHERE [TABLE_SCHEMA] = ? AND [TABLE_NAME] = ? AND [COLUMN_NAME] = ?",
|
||||||
|
res,
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue