Sql builder clean up.
This commit is contained in:
parent
736a650241
commit
01f43d462a
6 changed files with 53 additions and 92 deletions
|
|
@ -28,7 +28,7 @@ func (a *alias) serializeForProjection(statement StatementType, out *SqlBuilder)
|
||||||
}
|
}
|
||||||
|
|
||||||
out.WriteString("AS")
|
out.WriteString("AS")
|
||||||
out.writeAlias(a.alias)
|
out.WriteAlias(a.alias)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,11 +43,18 @@ type ClauseFrom struct {
|
||||||
Table Serializer
|
Table Serializer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *ClauseFrom) Serialize(statementType StatementType, out *SqlBuilder) error {
|
func (f *ClauseFrom) Serialize(statementType StatementType, s *SqlBuilder) error {
|
||||||
if f.Table == nil {
|
if f.Table == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return out.writeFrom(statementType, f.Table)
|
s.NewLine()
|
||||||
|
s.WriteString("FROM")
|
||||||
|
|
||||||
|
s.IncreaseIdent()
|
||||||
|
err := f.Table.serialize(statementType, s)
|
||||||
|
s.DecreaseIdent()
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClauseWhere struct {
|
type ClauseWhere struct {
|
||||||
|
|
@ -55,14 +62,21 @@ type ClauseWhere struct {
|
||||||
Mandatory bool
|
Mandatory bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClauseWhere) Serialize(statementType StatementType, out *SqlBuilder) error {
|
func (c *ClauseWhere) Serialize(statementType StatementType, s *SqlBuilder) error {
|
||||||
if c.Condition == nil {
|
if c.Condition == nil {
|
||||||
if c.Mandatory {
|
if c.Mandatory {
|
||||||
return errors.New("jet: WHERE clause not set")
|
return errors.New("jet: WHERE clause not set")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return out.writeWhere(statementType, c.Condition)
|
s.NewLine()
|
||||||
|
s.WriteString("WHERE")
|
||||||
|
|
||||||
|
s.IncreaseIdent()
|
||||||
|
err := c.Condition.serialize(statementType, s, noWrap)
|
||||||
|
s.DecreaseIdent()
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClauseGroupBy struct {
|
type ClauseGroupBy struct {
|
||||||
|
|
@ -88,24 +102,38 @@ type ClauseHaving struct {
|
||||||
Condition BoolExpression
|
Condition BoolExpression
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClauseHaving) Serialize(statementType StatementType, out *SqlBuilder) error {
|
func (c *ClauseHaving) Serialize(statementType StatementType, s *SqlBuilder) error {
|
||||||
if c.Condition == nil {
|
if c.Condition == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.writeHaving(statementType, c.Condition)
|
s.NewLine()
|
||||||
|
s.WriteString("HAVING")
|
||||||
|
|
||||||
|
s.IncreaseIdent()
|
||||||
|
err := c.Condition.serialize(statementType, s, noWrap)
|
||||||
|
s.DecreaseIdent()
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClauseOrderBy struct {
|
type ClauseOrderBy struct {
|
||||||
List []OrderByClause
|
List []OrderByClause
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *ClauseOrderBy) Serialize(statementType StatementType, out *SqlBuilder) error {
|
func (o *ClauseOrderBy) Serialize(statementType StatementType, s *SqlBuilder) error {
|
||||||
if o.List == nil {
|
if o.List == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.writeOrderBy(statementType, o.List)
|
s.NewLine()
|
||||||
|
s.WriteString("ORDER BY")
|
||||||
|
|
||||||
|
s.IncreaseIdent()
|
||||||
|
err := serializeOrderByClauseList(statementType, o.List, s)
|
||||||
|
s.DecreaseIdent()
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClauseLimit struct {
|
type ClauseLimit struct {
|
||||||
|
|
@ -346,7 +374,7 @@ func (v *ClauseValues) Serialize(statementType StatementType, out *SqlBuilder) e
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
out.writeByte(')')
|
out.WriteByte(')')
|
||||||
out.DecreaseIdent()
|
out.DecreaseIdent()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ func (c *columnImpl) DefaultAlias() string {
|
||||||
func (c *columnImpl) serializeForOrderBy(statement StatementType, out *SqlBuilder) error {
|
func (c *columnImpl) serializeForOrderBy(statement StatementType, out *SqlBuilder) error {
|
||||||
if statement == SetStatementType {
|
if statement == SetStatementType {
|
||||||
// set Statement (UNION, EXCEPT ...) can reference only select projections in order by clause
|
// set Statement (UNION, EXCEPT ...) can reference only select projections in order by clause
|
||||||
out.writeAlias(c.DefaultAlias()) //always quote
|
out.WriteAlias(c.DefaultAlias()) //always quote
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -82,7 +82,7 @@ func (c columnImpl) serializeForProjection(statement StatementType, out *SqlBuil
|
||||||
}
|
}
|
||||||
|
|
||||||
out.WriteString("AS")
|
out.WriteString("AS")
|
||||||
out.writeAlias(c.DefaultAlias())
|
out.WriteAlias(c.DefaultAlias())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -90,16 +90,16 @@ func (c columnImpl) serializeForProjection(statement StatementType, out *SqlBuil
|
||||||
func (c columnImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) error {
|
func (c columnImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) error {
|
||||||
|
|
||||||
if c.subQuery != nil {
|
if c.subQuery != nil {
|
||||||
out.writeIdentifier(c.subQuery.Alias())
|
out.WriteIdentifier(c.subQuery.Alias())
|
||||||
out.writeByte('.')
|
out.WriteByte('.')
|
||||||
out.writeIdentifier(c.DefaultAlias(), true)
|
out.WriteIdentifier(c.DefaultAlias(), true)
|
||||||
} else {
|
} else {
|
||||||
if c.tableName != "" {
|
if c.tableName != "" {
|
||||||
out.writeIdentifier(c.tableName)
|
out.WriteIdentifier(c.tableName)
|
||||||
out.writeByte('.')
|
out.WriteByte('.')
|
||||||
}
|
}
|
||||||
|
|
||||||
out.writeIdentifier(c.name)
|
out.WriteIdentifier(c.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ func (s *SelectTableImpl2) serialize(statement StatementType, out *SqlBuilder, o
|
||||||
}
|
}
|
||||||
|
|
||||||
out.WriteString("AS")
|
out.WriteString("AS")
|
||||||
out.writeIdentifier(s.alias)
|
out.WriteIdentifier(s.alias)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,73 +51,6 @@ func (s *SqlBuilder) WriteProjections(statement StatementType, projections []Pro
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SqlBuilder) writeFrom(statement StatementType, table Serializer) error {
|
|
||||||
s.NewLine()
|
|
||||||
s.WriteString("FROM")
|
|
||||||
|
|
||||||
s.IncreaseIdent()
|
|
||||||
err := table.serialize(statement, s)
|
|
||||||
s.DecreaseIdent()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SqlBuilder) writeWhere(statement StatementType, where Expression) error {
|
|
||||||
s.NewLine()
|
|
||||||
s.WriteString("WHERE")
|
|
||||||
|
|
||||||
s.IncreaseIdent()
|
|
||||||
err := where.serialize(statement, s, noWrap)
|
|
||||||
s.DecreaseIdent()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SqlBuilder) writeGroupBy(statement StatementType, groupBy []GroupByClause) error {
|
|
||||||
s.NewLine()
|
|
||||||
s.WriteString("GROUP BY")
|
|
||||||
|
|
||||||
s.IncreaseIdent()
|
|
||||||
err := serializeGroupByClauseList(statement, groupBy, s)
|
|
||||||
s.DecreaseIdent()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SqlBuilder) writeOrderBy(statement StatementType, orderBy []OrderByClause) error {
|
|
||||||
s.NewLine()
|
|
||||||
s.WriteString("ORDER BY")
|
|
||||||
|
|
||||||
s.IncreaseIdent()
|
|
||||||
err := serializeOrderByClauseList(statement, orderBy, s)
|
|
||||||
s.DecreaseIdent()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SqlBuilder) writeHaving(statement StatementType, having Expression) error {
|
|
||||||
s.NewLine()
|
|
||||||
s.WriteString("HAVING")
|
|
||||||
|
|
||||||
s.IncreaseIdent()
|
|
||||||
err := having.serialize(statement, s, noWrap)
|
|
||||||
s.DecreaseIdent()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SqlBuilder) WriteReturning(statement StatementType, returning []Projection) error {
|
|
||||||
if len(returning) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
s.NewLine()
|
|
||||||
s.WriteString("RETURNING")
|
|
||||||
s.IncreaseIdent()
|
|
||||||
|
|
||||||
return s.WriteProjections(statement, returning)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SqlBuilder) NewLine() {
|
func (s *SqlBuilder) NewLine() {
|
||||||
s.write([]byte{'\n'})
|
s.write([]byte{'\n'})
|
||||||
s.write(bytes.Repeat([]byte{' '}, s.ident))
|
s.write(bytes.Repeat([]byte{' '}, s.ident))
|
||||||
|
|
@ -144,7 +77,7 @@ func isPostSeparator(b byte) bool {
|
||||||
return b == ' ' || b == '.' || b == ',' || b == ')' || b == '\n' || b == ':'
|
return b == ' ' || b == '.' || b == ',' || b == ')' || b == '\n' || b == ':'
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SqlBuilder) writeAlias(str string) {
|
func (s *SqlBuilder) WriteAlias(str string) {
|
||||||
aliasQuoteChar := string(s.Dialect.AliasQuoteChar())
|
aliasQuoteChar := string(s.Dialect.AliasQuoteChar())
|
||||||
s.WriteString(aliasQuoteChar + str + aliasQuoteChar)
|
s.WriteString(aliasQuoteChar + str + aliasQuoteChar)
|
||||||
}
|
}
|
||||||
|
|
@ -153,7 +86,7 @@ func (s *SqlBuilder) WriteString(str string) {
|
||||||
s.write([]byte(str))
|
s.write([]byte(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SqlBuilder) writeIdentifier(name string, alwaysQuote ...bool) {
|
func (s *SqlBuilder) WriteIdentifier(name string, alwaysQuote ...bool) {
|
||||||
quoteWrap := name != strings.ToLower(name) || strings.ContainsAny(name, ". -")
|
quoteWrap := name != strings.ToLower(name) || strings.ContainsAny(name, ". -")
|
||||||
|
|
||||||
if quoteWrap || len(alwaysQuote) > 0 {
|
if quoteWrap || len(alwaysQuote) > 0 {
|
||||||
|
|
@ -164,7 +97,7 @@ func (s *SqlBuilder) writeIdentifier(name string, alwaysQuote ...bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SqlBuilder) writeByte(b byte) {
|
func (s *SqlBuilder) WriteByte(b byte) {
|
||||||
s.write([]byte{b})
|
s.write([]byte{b})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,13 +75,13 @@ func (t *TableImpl) serialize(statement StatementType, out *SqlBuilder, options
|
||||||
return errors.New("jet: tableImpl is nil. ")
|
return errors.New("jet: tableImpl is nil. ")
|
||||||
}
|
}
|
||||||
|
|
||||||
out.writeIdentifier(t.schemaName)
|
out.WriteIdentifier(t.schemaName)
|
||||||
out.WriteString(".")
|
out.WriteString(".")
|
||||||
out.writeIdentifier(t.name)
|
out.WriteIdentifier(t.name)
|
||||||
|
|
||||||
if len(t.alias) > 0 {
|
if len(t.alias) > 0 {
|
||||||
out.WriteString("AS")
|
out.WriteString("AS")
|
||||||
out.writeIdentifier(t.alias)
|
out.WriteIdentifier(t.alias)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue