mirror of https://github.com/writeas/writefreely
parent
462f87919a
commit
cf87ae9096
@ -0,0 +1,53 @@ |
||||
package db |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
) |
||||
|
||||
type CreateIndexSqlBuilder struct { |
||||
Dialect DialectType |
||||
Name string |
||||
Table string |
||||
Unique bool |
||||
Columns []string |
||||
} |
||||
|
||||
type DropIndexSqlBuilder struct { |
||||
Dialect DialectType |
||||
Name string |
||||
Table string |
||||
} |
||||
|
||||
func (b *CreateIndexSqlBuilder) ToSQL() (string, error) { |
||||
var str strings.Builder |
||||
|
||||
str.WriteString("CREATE ") |
||||
if b.Unique { |
||||
str.WriteString("UNIQUE ") |
||||
} |
||||
str.WriteString("INDEX ") |
||||
str.WriteString(b.Name) |
||||
str.WriteString(" on ") |
||||
str.WriteString(b.Table) |
||||
|
||||
if len(b.Columns) == 0 { |
||||
return "", fmt.Errorf("columns provided for this index: %s", b.Name) |
||||
} |
||||
|
||||
str.WriteString(" (") |
||||
columnCount := len(b.Columns) |
||||
for i, thing := range b.Columns { |
||||
str.WriteString(thing) |
||||
if i < columnCount-1 { |
||||
str.WriteString(", ") |
||||
} |
||||
} |
||||
str.WriteString(")") |
||||
|
||||
return str.String(), nil |
||||
} |
||||
|
||||
func (b *DropIndexSqlBuilder) ToSQL() (string, error) { |
||||
return fmt.Sprintf("DROP INDEX %s on %s", b.Name, b.Table), nil |
||||
} |
@ -0,0 +1,9 @@ |
||||
package db |
||||
|
||||
type RawSqlBuilder struct { |
||||
Query string |
||||
} |
||||
|
||||
func (b *RawSqlBuilder) ToSQL() (string, error) { |
||||
return b.Query, nil |
||||
} |
Loading…
Reference in new issue