Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ func (m *DbMap) createIndexImpl(ctx context.Context, dialect reflect.Type,
s.WriteString(" unique")
}
s.WriteString(" index")
s.WriteString(fmt.Sprintf(" %s on %s", index.IndexName, table.TableName))
s.WriteString(fmt.Sprintf(
" %s on %s",
m.Dialect.QuoteField(index.IndexName),
m.Dialect.QuotedTableForQuery(table.SchemaName, table.TableName),
))
if dname := dialect.Name(); dname == "PostgresDialect" && index.IndexType != "" {
s.WriteString(fmt.Sprintf(" %s %s", m.Dialect.CreateIndexSuffix(), index.IndexType))
}
Expand Down Expand Up @@ -129,10 +133,14 @@ func (t *TableMap) DropIndex(ctx context.Context, name string) error {
for _, idx := range t.indexes {
if idx.IndexName == name {
s := bytes.Buffer{}
s.WriteString(fmt.Sprintf("DROP INDEX %s", idx.IndexName))
s.WriteString(fmt.Sprintf("DROP INDEX %s", t.dbmap.Dialect.QuoteField(idx.IndexName)))

if dname := dialect.Name(); dname == "MySQLDialect" {
s.WriteString(fmt.Sprintf(" %s %s", t.dbmap.Dialect.DropIndexSuffix(), t.TableName))
s.WriteString(fmt.Sprintf(
" %s %s",
t.dbmap.Dialect.DropIndexSuffix(),
t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName),
))
}
s.WriteString(";")
_, e := t.dbmap.ExecContext(ctx, s.String())
Expand Down
4 changes: 2 additions & 2 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ func (d MySQLDialect) InsertAutoIncr(ctx context.Context, exec SqlExecutor, inse
}

func (d MySQLDialect) QuoteField(f string) string {
return "`" + f + "`"
return "`" + strings.ReplaceAll(f, "`", "``") + "`"
}

func (d MySQLDialect) QuotedTableForQuery(schema string, table string) string {
if strings.TrimSpace(schema) == "" {
return d.QuoteField(table)
}

return schema + "." + d.QuoteField(table)
return d.QuoteField(schema) + "." + d.QuoteField(table)
}

func (d MySQLDialect) IfSchemaNotExists(command, schema string) string {
Expand Down
4 changes: 3 additions & 1 deletion dialect_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func TestMySQLDialect(t *testing.T) {

o.Spec("QuoteField", func(tcx testContext) {
tcx.expect(tcx.dialect.QuoteField("foo")).To(matchers.Equal("`foo`"))
tcx.expect(tcx.dialect.QuoteField("fo`o")).To(matchers.Equal("`fo``o`"))
})

o.Group("QuotedTableForQuery", func() {
Expand All @@ -149,7 +150,8 @@ func TestMySQLDialect(t *testing.T) {
})

o.Spec("with a supplied schema", func(tcx testContext) {
tcx.expect(tcx.dialect.QuotedTableForQuery("foo", "bar")).To(matchers.Equal("foo.`bar`"))
tcx.expect(tcx.dialect.QuotedTableForQuery("foo", "bar")).To(matchers.Equal("`foo`.`bar`"))
tcx.expect(tcx.dialect.QuotedTableForQuery("fo`o", "ba`r")).To(matchers.Equal("`fo``o`.`ba``r`"))
})
})

Expand Down
6 changes: 3 additions & 3 deletions dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ func (d PostgresDialect) InsertAutoIncrToTarget(ctx context.Context, exec SqlExe

func (d PostgresDialect) QuoteField(f string) string {
if d.LowercaseFields {
return `"` + strings.ToLower(f) + `"`
f = strings.ToLower(f)
}
return `"` + f + `"`
return `"` + strings.ReplaceAll(f, `"`, `""`) + `"`
}

func (d PostgresDialect) QuotedTableForQuery(schema string, table string) string {
if strings.TrimSpace(schema) == "" {
return d.QuoteField(table)
}

return schema + "." + d.QuoteField(table)
return d.QuoteField(schema) + "." + d.QuoteField(table)
}

func (d PostgresDialect) IfSchemaNotExists(command, schema string) string {
Expand Down
5 changes: 4 additions & 1 deletion dialect_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func TestPostgresDialect(t *testing.T) {
o.Spec("By default, case is preserved", func(tcx postgresTestContext) {
tcx.expect(tcx.dialect.QuoteField("Foo")).To(matchers.Equal(`"Foo"`))
tcx.expect(tcx.dialect.QuoteField("bar")).To(matchers.Equal(`"bar"`))
tcx.expect(tcx.dialect.QuoteField(`Fo"o`)).To(matchers.Equal(`"Fo""o"`))
})

o.Group("With LowercaseFields set to true", func() {
Expand All @@ -130,6 +131,7 @@ func TestPostgresDialect(t *testing.T) {

o.Spec("fields are lowercased", func(tcx postgresTestContext) {
tcx.expect(tcx.dialect.QuoteField("Foo")).To(matchers.Equal(`"foo"`))
tcx.expect(tcx.dialect.QuoteField(`Fo"O`)).To(matchers.Equal(`"fo""o"`))
})
})
})
Expand All @@ -140,7 +142,8 @@ func TestPostgresDialect(t *testing.T) {
})

o.Spec("with a supplied schema", func(tcx postgresTestContext) {
tcx.expect(tcx.dialect.QuotedTableForQuery("foo", "bar")).To(matchers.Equal(`foo."bar"`))
tcx.expect(tcx.dialect.QuotedTableForQuery("foo", "bar")).To(matchers.Equal(`"foo"."bar"`))
tcx.expect(tcx.dialect.QuotedTableForQuery(`fo"o`, `ba"r`)).To(matchers.Equal(`"fo""o"."ba""r"`))
})
})

Expand Down
3 changes: 2 additions & 1 deletion dialect_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"reflect"
"strings"
)

type SqliteDialect struct {
Expand Down Expand Up @@ -92,7 +93,7 @@ func (d SqliteDialect) InsertAutoIncr(ctx context.Context, exec SqlExecutor, ins
}

func (d SqliteDialect) QuoteField(f string) string {
return `"` + f + `"`
return `"` + strings.ReplaceAll(f, `"`, `""`) + `"`
}

// sqlite does not have schemas like PostgreSQL does, so just escape it like normal
Expand Down
Loading
Loading