Skip to content

Commit

Permalink
Merge pull request #339 from upper/issue-338
Browse files Browse the repository at this point in the history
Fix DISTINCT and provide examples for DISTINCT ON
  • Loading branch information
José Carlos authored Feb 16, 2017
2 parents 7cab69c + e217b36 commit 25b45f7
Show file tree
Hide file tree
Showing 12 changed files with 672 additions and 544 deletions.
173 changes: 88 additions & 85 deletions internal/sqladapter/exql/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,50 @@ const (
defaultSortByColumnLayout = `{{.Column}} {{.Order}}`

defaultOrderByLayout = `
{{if .SortColumns}}
ORDER BY {{.SortColumns}}
{{end}}
`
{{if .SortColumns}}
ORDER BY {{.SortColumns}}
{{end}}
`

defaultWhereLayout = `
{{if .Conds}}
WHERE {{.Conds}}
{{end}}
`
{{if .Conds}}
WHERE {{.Conds}}
{{end}}
`

defaultUsingLayout = `
{{if .Columns}}
USING ({{.Columns}})
{{end}}
`
{{if .Columns}}
USING ({{.Columns}})
{{end}}
`

defaultJoinLayout = `
{{if .Table}}
{{ if .On }}
{{.Type}} JOIN {{.Table}}
{{.On}}
{{ else if .Using }}
{{.Type}} JOIN {{.Table}}
{{.Using}}
{{ else if .Type | eq "CROSS" }}
{{.Type}} JOIN {{.Table}}
{{else}}
NATURAL {{.Type}} JOIN {{.Table}}
{{end}}
{{end}}
`
{{if .Table}}
{{ if .On }}
{{.Type}} JOIN {{.Table}}
{{.On}}
{{ else if .Using }}
{{.Type}} JOIN {{.Table}}
{{.Using}}
{{ else if .Type | eq "CROSS" }}
{{.Type}} JOIN {{.Table}}
{{else}}
NATURAL {{.Type}} JOIN {{.Table}}
{{end}}
{{end}}
`

defaultOnLayout = `
{{if .Conds}}
ON {{.Conds}}
{{end}}
`
{{if .Conds}}
ON {{.Conds}}
{{end}}
`

defaultSelectLayout = `
SELECT
{{if .Distinct}}
DISTINCT
{{end}}
{{if .Columns}}
{{.Columns}}
Expand All @@ -92,7 +95,7 @@ const (
{{if .Offset}}
OFFSET {{.Offset}}
{{end}}
`
`
defaultDeleteLayout = `
DELETE
FROM {{.Table}}
Expand All @@ -103,92 +106,92 @@ const (
{{if .Offset}}
OFFSET {{.Offset}}
{{end}}
`
`
defaultUpdateLayout = `
UPDATE
{{.Table}}
SET {{.ColumnValues}}
{{ .Where }}
`
UPDATE
{{.Table}}
SET {{.ColumnValues}}
{{ .Where }}
`

defaultCountLayout = `
SELECT
COUNT(1) AS _t
FROM {{.Table}}
{{.Where}}
SELECT
COUNT(1) AS _t
FROM {{.Table}}
{{.Where}}
{{if .Limit}}
LIMIT {{.Limit}}
{{end}}
{{if .Limit}}
LIMIT {{.Limit}}
{{end}}
{{if .Offset}}
OFFSET {{.Offset}}
{{end}}
`
{{if .Offset}}
OFFSET {{.Offset}}
{{end}}
`

defaultInsertLayout = `
INSERT INTO {{.Table}}
{{if .Columns }}({{.Columns}}){{end}}
VALUES
{{.Values}}
{{if .Returning}}
RETURNING {{.Returning}}
{{end}}
`
{{if .Returning}}
RETURNING {{.Returning}}
{{end}}
`

defaultTruncateLayout = `
TRUNCATE TABLE {{.Table}}
`
TRUNCATE TABLE {{.Table}}
`

defaultDropDatabaseLayout = `
DROP DATABASE {{.Database}}
`
DROP DATABASE {{.Database}}
`

defaultDropTableLayout = `
DROP TABLE {{.Table}}
`
DROP TABLE {{.Table}}
`

defaultGroupByColumnLayout = `{{.Column}}`

defaultGroupByLayout = `
{{if .GroupColumns}}
GROUP BY {{.GroupColumns}}
{{end}}
`
{{if .GroupColumns}}
GROUP BY {{.GroupColumns}}
{{end}}
`
)

var defaultTemplate = &Template{
ColumnSeparator: defaultColumnSeparator,
IdentifierSeparator: defaultIdentifierSeparator,
IdentifierQuote: defaultIdentifierQuote,
ValueSeparator: defaultValueSeparator,
ValueQuote: defaultValueQuote,
AndKeyword: defaultAndKeyword,
OrKeyword: defaultOrKeyword,
NotKeyword: defaultNotKeyword,
DescKeyword: defaultDescKeyword,
AscKeyword: defaultAscKeyword,
DefaultOperator: defaultDefaultOperator,
AssignmentOperator: defaultAssignmentOperator,
ClauseGroup: defaultClauseGroup,
ClauseOperator: defaultClauseOperator,
ColumnValue: defaultColumnValue,
TableAliasLayout: defaultTableAliasLayout,
ColumnAliasLayout: defaultColumnAliasLayout,
SortByColumnLayout: defaultSortByColumnLayout,
WhereLayout: defaultWhereLayout,
OnLayout: defaultOnLayout,
UsingLayout: defaultUsingLayout,
JoinLayout: defaultJoinLayout,
OrderByLayout: defaultOrderByLayout,
InsertLayout: defaultInsertLayout,
SelectLayout: defaultSelectLayout,
UpdateLayout: defaultUpdateLayout,
ColumnSeparator: defaultColumnSeparator,
ColumnValue: defaultColumnValue,
CountLayout: defaultCountLayout,
DeleteLayout: defaultDeleteLayout,
TruncateLayout: defaultTruncateLayout,
DescKeyword: defaultDescKeyword,
DropDatabaseLayout: defaultDropDatabaseLayout,
DropTableLayout: defaultDropTableLayout,
CountLayout: defaultCountLayout,
GroupByLayout: defaultGroupByLayout,
Cache: cache.NewCache(),
IdentifierQuote: defaultIdentifierQuote,
IdentifierSeparator: defaultIdentifierSeparator,
InsertLayout: defaultInsertLayout,
JoinLayout: defaultJoinLayout,
NotKeyword: defaultNotKeyword,
OnLayout: defaultOnLayout,
OrKeyword: defaultOrKeyword,
OrderByLayout: defaultOrderByLayout,
SelectLayout: defaultSelectLayout,
SortByColumnLayout: defaultSortByColumnLayout,
TableAliasLayout: defaultTableAliasLayout,
TruncateLayout: defaultTruncateLayout,
UpdateLayout: defaultUpdateLayout,
UsingLayout: defaultUsingLayout,
ValueQuote: defaultValueQuote,
ValueSeparator: defaultValueSeparator,
WhereLayout: defaultWhereLayout,

Cache: cache.NewCache(),
}
3 changes: 3 additions & 0 deletions internal/sqladapter/exql/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Statement struct {
ColumnValues Fragment
OrderBy Fragment
GroupBy Fragment
Distinct bool
Joins Fragment
Where Fragment
Returning Fragment
Expand All @@ -38,6 +39,7 @@ type statementT struct {
ColumnValues string
OrderBy string
GroupBy string
Distinct bool
Where string
Joins string
Returning string
Expand Down Expand Up @@ -89,6 +91,7 @@ func (s *Statement) Compile(layout *Template) (compiled string) {
data := statementT{
Table: layout.doCompile(s.Table),
Database: layout.doCompile(s.Database),
Distinct: s.Distinct,
Limit: s.Limit,
Offset: s.Offset,
Columns: layout.doCompile(s.Columns),
Expand Down
45 changes: 23 additions & 22 deletions internal/sqladapter/exql/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,39 @@ var (

// Template is an SQL template.
type Template struct {
ColumnSeparator string
IdentifierSeparator string
IdentifierQuote string
ValueSeparator string
ValueQuote string
AndKeyword string
OrKeyword string
NotKeyword string
DescKeyword string
AscKeyword string
DefaultOperator string
AssignmentOperator string
ClauseGroup string
ClauseOperator string
ColumnValue string
TableAliasLayout string
ColumnAliasLayout string
SortByColumnLayout string
WhereLayout string
OnLayout string
UsingLayout string
JoinLayout string
OrderByLayout string
InsertLayout string
SelectLayout string
UpdateLayout string
ColumnSeparator string
ColumnValue string
CountLayout string
DefaultOperator string
DeleteLayout string
TruncateLayout string
DescKeyword string
DropDatabaseLayout string
DropTableLayout string
CountLayout string
GroupByLayout string
IdentifierQuote string
IdentifierSeparator string
InsertLayout string
JoinLayout string
NotKeyword string
OnLayout string
OrKeyword string
OrderByLayout string
SelectLayout string
SortByColumnLayout string
TableAliasLayout string
TruncateLayout string
UpdateLayout string
UsingLayout string
ValueQuote string
ValueSeparator string
WhereLayout string

*cache.Cache
}

Expand Down
Loading

0 comments on commit 25b45f7

Please sign in to comment.