Skip to content

Commit

Permalink
Add whitespace and punctuation handling for sqlserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Da Ros committed Jun 20, 2019
1 parent 6cebc82 commit 9ffe8d1
Showing 1 changed file with 51 additions and 48 deletions.
99 changes: 51 additions & 48 deletions internal/pkg/sql/sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,57 @@ import (

var (
QueryTemplates = map[string]string{
"GetSingle": "SELECT * " +
"FROM {{.TableName}} AS _{{.TableName}} " +
"{{range .Relations}}" +
" LEFT JOIN {{.Relationship.WithTable}}" +
" ON {{.Relationship.WithTable}}.{{.Relationship.ForeignTableUniqueIDColumn}}" +
" = _{{$.TableName}}.{{.Relationship.LocalTableUniqueIDColumn}}" +
"{{end}}" +
"WHERE _{{$.TableName}}.{{.UniqueIDColumn}} = @p1",
"GetSingleAsOption": "SELECT {{.UniqueIDColumn}}, {{.ColumnAsOptionName}} " +
"FROM {{.TableName}} " +
"WHERE {{.UniqueIDColumn}} = @p1",
"GetCollection": "SELECT *" +
"FROM {{.TableName}}",
"GetCollectionAsOptions": "SELECT {{.UniqueIDColumn}}, {{.ColumnAsOptionName}} " +
"FROM {{.TableName}}",
"GetCollectionAsOptionsFilterable": "SELECT {{.UniqueIDColumn}}, {{.ColumnAsOptionName}} " +
"FROM {{.TableName}} " +
"WHERE CAST ({{.ColumnAsOptionName}} AS TEXT) LIKE @p1",
"GetCollectionAsOptionsWithParams": "SELECT {{.UniqueIDColumn}}, {{.ColumnAsOptionName}} " +
"FROM {{.TableName}} " +
"WHERE CAST ({{.ColumnAsOptionName}} AS TEXT) LIKE @p1 " +
"{{range $key, $value := .ParamsWithValues}}" +
"AND {{$key}} = '{{$value}}'" +
"{{end}}",
"UpdateSingle": "UPDATE {{.TableName}} " +
"SET {{.ColumnNames | head}} = @p1" +
"{{range $index, $element := .ColumnNames | tail}}," +
" {{$element}} = @p{{(add2 $index)}}" +
"{{end}} " +
"WHERE {{.UniqueIDColumn}}= @p{{(lenPlus1 .ColumnNames)}}",
"CreateSingle": "INSERT INTO {{.TableName}}" +
"({{.ColumnNames | head}}" +
"{{range .ColumnNames | tail}}," +
" {{.}}" +
"{{end}}) " +
"VALUES(@p1" +
"{{range $index, $element := .ColumnNames | tail}}," +
" @p{{$index | add2}}" +
"{{end}}) " +
"RETURNING {{.UniqueIDColumn}}",
"DeleteSingle": "DELETE FROM {{.TableName}} WHERE {{.UniqueIDColumn}} = ?",
"GetTableSchema": "SELECT TOP 1 * " +
"FROM {{.TableName}}",
"GetTableWithRelationshipsSchema": "SELECT TOP 1 * FROM {{.TableName}} AS _{{.TableName}}" +
"{{range .Relations}}" +
" LEFT JOIN {{.Relationship.WithTable}}" +
" ON {{.Relationship.WithTable}}.{{.Relationship.ForeignTableUniqueIDColumn}}" +
" = _{{$.TableName}}.{{.Relationship.LocalTableUniqueIDColumn}}{{end}}",
`GetSingle`: `SELECT * ` +
`FROM "{{.TableName}}" AS "_{{.TableName}}" ` +
`{{range .Relations}}` +
` LEFT JOIN "{{.Relationship.WithTable}}"` +
` ON "{{.Relationship.WithTable}}"."{{.Relationship.ForeignTableUniqueIDColumn}}"` +
` = "_{{$.TableName}}"."{{.Relationship.LocalTableUniqueIDColumn}}"` +
`{{end}}` +
`WHERE "_{{$.TableName}}"."{{.UniqueIDColumn}}" = @p1`,
`GetSingleAsOption`: `SELECT "{{.UniqueIDColumn}}", "{{.ColumnAsOptionName}}" ` +
`FROM "{{.TableName}}" ` +
`WHERE "{{.UniqueIDColumn}}" = @p1`,
`GetCollection`: `SELECT * ` +
`FROM "{{.TableName}}"`,
`GetCollectionFilterable`: `SELECT * ` +
`FROM "{{.TableName}} ` +
`WHERE "{{.FilterOnColumn}}" {{.Operator}} @p1`,
`GetCollectionAsOptions`: `SELECT "{{.UniqueIDColumn}}", "{{.ColumnAsOptionName}}" ` +
`FROM "{{.TableName}}"`,
`GetCollectionAsOptionsFilterable`: `SELECT "{{.UniqueIDColumn}}", "{{.ColumnAsOptionName}}" ` +
`FROM "{{.TableName}}" ` +
`WHERE CAST ("{{.ColumnAsOptionName}}" AS TEXT) LIKE @p1`,
`GetCollectionAsOptionsWithParams`: `SELECT "{{.UniqueIDColumn}}", "{{.ColumnAsOptionName}}" ` +
`FROM "{{.TableName}}" ` +
`WHERE CAST ("{{.ColumnAsOptionName}}" AS TEXT) LIKE @p1 ` +
`{{range $key, $value := .ParamsWithValues}}` +
`AND "{{$key}}" = '{{$value}}'` +
`{{end}}`,
`UpdateSingle`: `UPDATE "{{.TableName}}" ` +
`SET "{{.ColumnNames | head}}" = @p1` +
`{{range $index, $element := .ColumnNames | tail}},` +
` "{{$element}}" = @p{{(add2 $index)}}` +
`{{end}} ` +
`WHERE "{{.UniqueIDColumn}}"= @p{{(lenPlus1 .ColumnNames)}}`,
`CreateSingle`: `INSERT INTO "{{.TableName}}"` +
`("{{.ColumnNames | head}}"` +
`{{range .ColumnNames | tail}},` +
` "{{.}}"` +
`{{end}}) ` +
`VALUES(@p1` +
`{{range $index, $element := .ColumnNames | tail}},` +
` @p{{$index | add2}}` +
`{{end}}) ` +
`RETURNING "{{.UniqueIDColumn}}"`,
`DeleteSingle`: `DELETE FROM "{{.TableName}}" WHERE "{{.UniqueIDColumn}}" = ?`,
`GetTableSchema`: `SELECT TOP 1 * ` +
`FROM "{{.TableName}}"`,
`GetTableWithRelationshipsSchema`: `SELECT TOP 1 * FROM "{{.TableName}}" AS "_{{.TableName}}"` +
`{{range .Relations}}` +
` LEFT JOIN "{{.Relationship.WithTable}}"` +
` ON "{{.Relationship.WithTable}}"."{{.Relationship.ForeignTableUniqueIDColumn}}"` +
` = "_{{$.TableName}}"."{{.Relationship.LocalTableUniqueIDColumn}}"{{end}}`,
}
integer = []string{
"TINYINT",
Expand Down

0 comments on commit 9ffe8d1

Please sign in to comment.