2019-03-02 12:34:08 +01:00
|
|
|
// Modeling of tables. This is where query preparation starts
|
|
|
|
|
|
|
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/dropbox/godropbox/errors"
|
|
|
|
|
)
|
|
|
|
|
|
2019-05-05 13:49:24 +02:00
|
|
|
type tableInterface interface {
|
2019-04-07 09:58:12 +02:00
|
|
|
SchemaName() string
|
|
|
|
|
TableName() string
|
2019-05-05 13:49:24 +02:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
Columns() []column
|
2019-05-05 13:49:24 +02:00
|
|
|
// Generates the sql string for the current tableName expression.
|
2019-05-07 19:06:21 +02:00
|
|
|
serializeSql(out *queryData) error
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The sql tableName read interface. NOTE: NATURAL JOINs, and join "USING" clause
|
|
|
|
|
// are not supported.
|
2019-05-07 19:06:21 +02:00
|
|
|
type readableTable interface {
|
2019-05-05 13:49:24 +02:00
|
|
|
tableInterface
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Generates a select query on the current tableName.
|
2019-05-07 19:06:21 +02:00
|
|
|
SELECT(projections ...projection) selectStatement
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Creates a inner join tableName expression using onCondition.
|
2019-05-07 19:06:21 +02:00
|
|
|
INNER_JOIN(table readableTable, onCondition boolExpression) readableTable
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Creates a left join tableName expression using onCondition.
|
2019-05-07 19:06:21 +02:00
|
|
|
LEFT_JOIN(table readableTable, onCondition boolExpression) readableTable
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Creates a right join tableName expression using onCondition.
|
2019-05-07 19:06:21 +02:00
|
|
|
RIGHT_JOIN(table readableTable, onCondition boolExpression) readableTable
|
2019-03-16 14:02:45 +01:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
FULL_JOIN(table readableTable, onCondition boolExpression) readableTable
|
2019-03-16 14:02:45 +01:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
CROSS_JOIN(table readableTable) readableTable
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// The sql tableName write interface.
|
2019-05-07 19:06:21 +02:00
|
|
|
type writableTable interface {
|
2019-05-05 13:49:24 +02:00
|
|
|
tableInterface
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
INSERT(columns ...column) insertStatement
|
|
|
|
|
UPDATE(columns ...column) updateStatement
|
|
|
|
|
DELETE() deleteStatement
|
2019-05-07 13:44:30 +02:00
|
|
|
|
|
|
|
|
LOCK() lockStatement
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Defines a physical tableName in the database that is both readable and writable.
|
2019-03-02 12:34:08 +01:00
|
|
|
// This function will panic if name is not valid
|
2019-05-07 19:06:21 +02:00
|
|
|
func NewTable(schemaName, name string, columns ...column) *Table {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
t := &Table{
|
2019-05-05 13:49:24 +02:00
|
|
|
schemaName: schemaName,
|
|
|
|
|
name: name,
|
|
|
|
|
columns: columns,
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
for _, c := range columns {
|
2019-05-05 13:49:24 +02:00
|
|
|
c.setTableName(name)
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Table struct {
|
2019-05-05 13:49:24 +02:00
|
|
|
schemaName string
|
|
|
|
|
name string
|
|
|
|
|
alias string
|
2019-05-07 19:06:21 +02:00
|
|
|
columns []column
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *Table) Column(name string) column {
|
2019-03-30 10:17:32 +01:00
|
|
|
return &baseColumn{
|
|
|
|
|
name: name,
|
|
|
|
|
nullable: NotNullable,
|
|
|
|
|
tableName: t.name,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-03-16 20:41:06 +01:00
|
|
|
func (t *Table) SetAlias(alias string) {
|
|
|
|
|
t.alias = alias
|
|
|
|
|
|
|
|
|
|
for _, c := range t.columns {
|
2019-05-05 13:49:24 +02:00
|
|
|
c.setTableName(alias)
|
2019-03-16 20:41:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Returns the tableName's name in the database
|
2019-04-07 09:58:12 +02:00
|
|
|
func (t *Table) SchemaName() string {
|
|
|
|
|
return t.schemaName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the tableName's name in the database
|
|
|
|
|
func (t *Table) TableName() string {
|
2019-03-02 12:34:08 +01:00
|
|
|
return t.name
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 09:58:12 +02:00
|
|
|
func (t *Table) SchemaTableName() string {
|
2019-03-09 09:52:03 +01:00
|
|
|
return t.schemaName
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Returns a list of the tableName's columns
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *Table) Columns() []column {
|
2019-03-02 12:34:08 +01:00
|
|
|
return t.columns
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Generates the sql string for the current tableName expression. Note: the
|
2019-03-02 12:34:08 +01:00
|
|
|
// generated string may not be a valid/executable sql statement.
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *Table) serializeSql(out *queryData) error {
|
2019-04-20 19:49:29 +02:00
|
|
|
if t == nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return errors.Newf("nil tableName.")
|
2019-03-09 09:52:03 +01:00
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
out.WriteString(t.schemaName)
|
|
|
|
|
out.WriteString(".")
|
|
|
|
|
out.WriteString(t.TableName())
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-03-16 20:41:06 +01:00
|
|
|
if len(t.alias) > 0 {
|
|
|
|
|
out.WriteString(" AS ")
|
|
|
|
|
out.WriteString(t.alias)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 12:34:08 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Generates a select query on the current tableName.
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *Table) SELECT(projections ...projection) selectStatement {
|
2019-03-02 12:34:08 +01:00
|
|
|
return newSelectStatement(t, projections)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Creates a inner join tableName expression using onCondition.
|
2019-04-03 11:03:07 +02:00
|
|
|
func (t *Table) INNER_JOIN(
|
2019-05-07 19:06:21 +02:00
|
|
|
table readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return InnerJoinOn(t, table, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Creates a left join tableName expression using onCondition.
|
2019-05-05 12:37:23 +02:00
|
|
|
func (t *Table) LEFT_JOIN(
|
2019-05-07 19:06:21 +02:00
|
|
|
table readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return LeftJoinOn(t, table, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
// Creates a right join tableName expression using onCondition.
|
2019-05-05 12:37:23 +02:00
|
|
|
func (t *Table) RIGHT_JOIN(
|
2019-05-07 19:06:21 +02:00
|
|
|
table readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return RightJoinOn(t, table, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *Table) FULL_JOIN(table readableTable, onCondition boolExpression) readableTable {
|
2019-03-31 14:07:58 +02:00
|
|
|
return FullJoin(t, table, onCondition)
|
2019-03-16 14:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *Table) CROSS_JOIN(table readableTable) readableTable {
|
2019-03-16 14:02:45 +01:00
|
|
|
return CrossJoin(t, table)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *Table) INSERT(columns ...column) insertStatement {
|
2019-03-02 12:34:08 +01:00
|
|
|
return newInsertStatement(t, columns...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *Table) UPDATE(columns ...column) updateStatement {
|
2019-04-14 17:55:10 +02:00
|
|
|
return newUpdateStatement(t, columns)
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *Table) DELETE() deleteStatement {
|
2019-03-02 12:34:08 +01:00
|
|
|
return newDeleteStatement(t)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 13:44:30 +02:00
|
|
|
func (t *Table) LOCK() lockStatement {
|
|
|
|
|
return LOCK(t)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 12:34:08 +01:00
|
|
|
type joinType int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
INNER_JOIN joinType = iota
|
|
|
|
|
LEFT_JOIN
|
|
|
|
|
RIGHT_JOIN
|
2019-03-16 14:02:45 +01:00
|
|
|
FULL_JOIN
|
|
|
|
|
CROSS_JOIN
|
2019-03-02 12:34:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Join expressions are pseudo readable tables.
|
|
|
|
|
type joinTable struct {
|
2019-05-07 19:06:21 +02:00
|
|
|
lhs readableTable
|
|
|
|
|
rhs readableTable
|
2019-03-02 12:34:08 +01:00
|
|
|
join_type joinType
|
2019-05-07 19:06:21 +02:00
|
|
|
onCondition boolExpression
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newJoinTable(
|
2019-05-07 19:06:21 +02:00
|
|
|
lhs readableTable,
|
|
|
|
|
rhs readableTable,
|
2019-03-02 12:34:08 +01:00
|
|
|
join_type joinType,
|
2019-05-07 19:06:21 +02:00
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return &joinTable{
|
|
|
|
|
lhs: lhs,
|
|
|
|
|
rhs: rhs,
|
|
|
|
|
join_type: join_type,
|
|
|
|
|
onCondition: onCondition,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func InnerJoinOn(
|
2019-05-07 19:06:21 +02:00
|
|
|
lhs readableTable,
|
|
|
|
|
rhs readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return newJoinTable(lhs, rhs, INNER_JOIN, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func LeftJoinOn(
|
2019-05-07 19:06:21 +02:00
|
|
|
lhs readableTable,
|
|
|
|
|
rhs readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return newJoinTable(lhs, rhs, LEFT_JOIN, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RightJoinOn(
|
2019-05-07 19:06:21 +02:00
|
|
|
lhs readableTable,
|
|
|
|
|
rhs readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return newJoinTable(lhs, rhs, RIGHT_JOIN, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-16 14:02:45 +01:00
|
|
|
func FullJoin(
|
2019-05-07 19:06:21 +02:00
|
|
|
lhs readableTable,
|
|
|
|
|
rhs readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-16 14:02:45 +01:00
|
|
|
|
|
|
|
|
return newJoinTable(lhs, rhs, FULL_JOIN, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CrossJoin(
|
2019-05-07 19:06:21 +02:00
|
|
|
lhs readableTable,
|
|
|
|
|
rhs readableTable) readableTable {
|
2019-03-16 14:02:45 +01:00
|
|
|
|
|
|
|
|
return newJoinTable(lhs, rhs, CROSS_JOIN, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 09:58:12 +02:00
|
|
|
func (t *joinTable) SchemaName() string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *joinTable) TableName() string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *joinTable) Columns() []column {
|
|
|
|
|
columns := make([]column, 0)
|
2019-03-02 12:34:08 +01:00
|
|
|
columns = append(columns, t.lhs.Columns()...)
|
|
|
|
|
columns = append(columns, t.rhs.Columns()...)
|
|
|
|
|
|
|
|
|
|
return columns
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *joinTable) Column(name string) column {
|
2019-05-05 13:49:24 +02:00
|
|
|
return &baseColumn{
|
|
|
|
|
name: name,
|
|
|
|
|
nullable: NotNullable,
|
|
|
|
|
}
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *joinTable) serializeSql(out *queryData) (err error) {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
if t.lhs == nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return errors.Newf("nil lhs.")
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
if t.rhs == nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return errors.Newf("nil rhs.")
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
2019-03-16 14:02:45 +01:00
|
|
|
if t.onCondition == nil && t.join_type != CROSS_JOIN {
|
2019-04-29 14:39:48 +02:00
|
|
|
return errors.Newf("nil onCondition.")
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
if err = t.lhs.serializeSql(out); err != nil {
|
2019-03-02 12:34:08 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch t.join_type {
|
|
|
|
|
case INNER_JOIN:
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" JOIN ")
|
2019-03-02 12:34:08 +01:00
|
|
|
case LEFT_JOIN:
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" LEFT JOIN ")
|
2019-03-02 12:34:08 +01:00
|
|
|
case RIGHT_JOIN:
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" RIGHT JOIN ")
|
2019-03-16 14:02:45 +01:00
|
|
|
case FULL_JOIN:
|
|
|
|
|
out.WriteString(" FULL JOIN ")
|
|
|
|
|
case CROSS_JOIN:
|
|
|
|
|
out.WriteString(" CROSS JOIN ")
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
if err = t.rhs.serializeSql(out); err != nil {
|
2019-03-02 12:34:08 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-16 14:02:45 +01:00
|
|
|
if t.onCondition != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" ON ")
|
2019-05-07 19:06:21 +02:00
|
|
|
if err = t.onCondition.serialize(out); err != nil {
|
2019-03-16 14:02:45 +01:00
|
|
|
return
|
|
|
|
|
}
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *joinTable) SELECT(projections ...projection) selectStatement {
|
2019-03-02 12:34:08 +01:00
|
|
|
return newSelectStatement(t, projections)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 11:03:07 +02:00
|
|
|
func (t *joinTable) INNER_JOIN(
|
2019-05-07 19:06:21 +02:00
|
|
|
table readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return InnerJoinOn(t, table, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 12:37:23 +02:00
|
|
|
func (t *joinTable) LEFT_JOIN(
|
2019-05-07 19:06:21 +02:00
|
|
|
table readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return LeftJoinOn(t, table, onCondition)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *joinTable) FULL_JOIN(table readableTable, onCondition boolExpression) readableTable {
|
2019-03-31 14:07:58 +02:00
|
|
|
return FullJoin(t, table, onCondition)
|
2019-03-16 14:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (t *joinTable) CROSS_JOIN(table readableTable) readableTable {
|
2019-03-16 14:02:45 +01:00
|
|
|
return CrossJoin(t, table)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 12:37:23 +02:00
|
|
|
func (t *joinTable) RIGHT_JOIN(
|
2019-05-07 19:06:21 +02:00
|
|
|
table readableTable,
|
|
|
|
|
onCondition boolExpression) readableTable {
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
return RightJoinOn(t, table, onCondition)
|
|
|
|
|
}
|