diff --git a/postgresql/database.go b/postgresql/database.go index 2a195d707595b9530730711d2f6ad5d7e7e80f11..2b0584b80f6bf2214d9699e075bad5ff4ba73e10 100644 --- a/postgresql/database.go +++ b/postgresql/database.go @@ -28,6 +28,7 @@ import ( "github.com/jmoiron/sqlx" _ "github.com/lib/pq" // PostgreSQL driver. "upper.io/builder/sqlgen" + template "upper.io/builder/template/postgresql" "upper.io/db" "upper.io/db/internal/sqladapter" "upper.io/db/internal/sqlutil/tx" @@ -90,7 +91,7 @@ func (d *database) Open() error { // Setup configures the adapter. func (d *database) Setup(connURL db.ConnectionURL) error { - d.BaseDatabase = sqladapter.NewDatabase(d, connURL, template) + d.BaseDatabase = sqladapter.NewDatabase(d, connURL, template.Template()) return d.Open() } diff --git a/postgresql/postgresql.go b/postgresql/postgresql.go index bb1fa95b58f1079c5d10438a649c09c8862fcb33..8ee55e65f5a0d4b08dbc5684b9bf86666c77956e 100644 --- a/postgresql/postgresql.go +++ b/postgresql/postgresql.go @@ -22,53 +22,12 @@ package postgresql // import "upper.io/db/postgresql" import ( - "upper.io/builder/sqlgen" - "upper.io/cache" "upper.io/db" ) // Adapter is the public name of the adapter. const Adapter = `postgresql` -var template *sqlgen.Template - func init() { - - template = &sqlgen.Template{ - ColumnSeparator: adapterColumnSeparator, - IdentifierSeparator: adapterIdentifierSeparator, - IdentifierQuote: adapterIdentifierQuote, - ValueSeparator: adapterValueSeparator, - ValueQuote: adapterValueQuote, - AndKeyword: adapterAndKeyword, - OrKeyword: adapterOrKeyword, - NotKeyword: adapterNotKeyword, - DescKeyword: adapterDescKeyword, - AscKeyword: adapterAscKeyword, - DefaultOperator: adapterDefaultOperator, - AssignmentOperator: adapterAssignmentOperator, - ClauseGroup: adapterClauseGroup, - ClauseOperator: adapterClauseOperator, - ColumnValue: adapterColumnValue, - TableAliasLayout: adapterTableAliasLayout, - ColumnAliasLayout: adapterColumnAliasLayout, - SortByColumnLayout: adapterSortByColumnLayout, - WhereLayout: adapterWhereLayout, - JoinLayout: adapterJoinLayout, - OnLayout: adapterOnLayout, - UsingLayout: adapterUsingLayout, - OrderByLayout: adapterOrderByLayout, - InsertLayout: adapterInsertLayout, - SelectLayout: adapterSelectLayout, - UpdateLayout: adapterUpdateLayout, - DeleteLayout: adapterDeleteLayout, - TruncateLayout: adapterTruncateLayout, - DropDatabaseLayout: adapterDropDatabaseLayout, - DropTableLayout: adapterDropTableLayout, - CountLayout: adapterSelectCountLayout, - GroupByLayout: adapterGroupByLayout, - Cache: cache.NewCache(), - } - db.Register(Adapter, &database{}) } diff --git a/postgresql/template.go b/postgresql/template.go deleted file mode 100644 index 59f40584766167c8693d2360bfd220a6b0293261..0000000000000000000000000000000000000000 --- a/postgresql/template.go +++ /dev/null @@ -1,172 +0,0 @@ -// Copyright (c) 2012-2015 The upper.io/db authors. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -package postgresql - -const ( - adapterColumnSeparator = `.` - adapterIdentifierSeparator = `, ` - adapterIdentifierQuote = `"{{.Value}}"` - adapterValueSeparator = `, ` - adapterValueQuote = `'{{.}}'` - adapterAndKeyword = `AND` - adapterOrKeyword = `OR` - adapterNotKeyword = `NOT` - adapterDescKeyword = `DESC` - adapterAscKeyword = `ASC` - adapterDefaultOperator = `=` - adapterAssignmentOperator = `=` - adapterClauseGroup = `({{.}})` - adapterClauseOperator = ` {{.}} ` - adapterColumnValue = `{{.Column}} {{.Operator}} {{.Value}}` - adapterTableAliasLayout = `{{.Name}}{{if .Alias}} AS {{.Alias}}{{end}}` - adapterColumnAliasLayout = `{{.Name}}{{if .Alias}} AS {{.Alias}}{{end}}` - adapterSortByColumnLayout = `{{.Column}} {{.Order}}` - - adapterOrderByLayout = ` - {{if .SortColumns}} - ORDER BY {{.SortColumns}} - {{end}} - ` - - adapterWhereLayout = ` - {{if .Conds}} - WHERE {{.Conds}} - {{end}} - ` - - adapterUsingLayout = ` - {{if .Columns}} - USING ({{.Columns}}) - {{end}} - ` - - adapterJoinLayout = ` - {{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}} - ` - - adapterOnLayout = ` - {{if .Conds}} - ON {{.Conds}} - {{end}} - ` - - adapterSelectLayout = ` - SELECT - - {{if .Columns}} - {{.Columns}} - {{else}} - * - {{end}} - - {{if .Table}} - FROM {{.Table}} - {{end}} - - {{.Joins}} - - {{.Where}} - - {{.GroupBy}} - - {{.OrderBy}} - - {{if .Limit}} - LIMIT {{.Limit}} - {{end}} - - {{if .Offset}} - OFFSET {{.Offset}} - {{end}} - ` - adapterDeleteLayout = ` - DELETE - FROM {{.Table}} - {{.Where}} - {{if .Limit}} - LIMIT {{.Limit}} - {{end}} - - {{if .Offset}} - OFFSET {{.Offset}} - {{end}} - ` - adapterUpdateLayout = ` - UPDATE - {{.Table}} - SET {{.ColumnValues}} - {{ .Where }} - ` - - adapterSelectCountLayout = ` - SELECT - COUNT(1) AS _t - FROM {{.Table}} - {{.Where}} - - {{if .Limit}} - LIMIT {{.Limit}} - {{end}} - - {{if .Offset}} - OFFSET {{.Offset}} - {{end}} - ` - - adapterInsertLayout = ` - INSERT INTO {{.Table}} - {{if .Columns }}({{.Columns}}){{end}} - VALUES - {{.Values}} - {{.Extra}} - ` - - adapterTruncateLayout = ` - TRUNCATE TABLE {{.Table}} RESTART IDENTITY - ` - - adapterDropDatabaseLayout = ` - DROP DATABASE {{.Database}} - ` - - adapterDropTableLayout = ` - DROP TABLE {{.Table}} - ` - - adapterGroupByLayout = ` - {{if .GroupColumns}} - GROUP BY {{.GroupColumns}} - {{end}} - ` -)