Remove methods from Table interface that affects receiver object

Modifying SQL builder receiver object can produce unwanted side effects.
This commit is contained in:
go-jet 2021-03-21 17:14:15 +01:00
parent 5b4a1ebd63
commit 753d3bac9a
3 changed files with 15 additions and 60 deletions

View file

@ -15,22 +15,27 @@ type Table interface {
columns() []Column columns() []Column
SchemaName() string SchemaName() string
TableName() string TableName() string
AS(alias string)
Alias() string Alias() string
Schema(schemaName string)
} }
// NewTable creates new table with schema Name, table Name and list of columns // NewTable creates new table with schema Name, table Name and list of columns
func NewTable(schemaName, name string, columns ...ColumnExpression) SerializerTable { func NewTable(schemaName, name, alias string, columns ...ColumnExpression) SerializerTable {
t := tableImpl{ t := tableImpl{
schemaName: schemaName, schemaName: schemaName,
name: name, name: name,
alias: alias,
columnList: columns, columnList: columns,
} }
columnTableName := name
if alias != "" {
columnTableName = alias
}
for _, c := range columns { for _, c := range columns {
c.setTableName(name) c.setTableName(columnTableName)
} }
return &t return &t
@ -43,14 +48,6 @@ type tableImpl struct {
columnList []ColumnExpression columnList []ColumnExpression
} }
func (t *tableImpl) AS(alias string) {
t.alias = alias
for _, c := range t.columnList {
c.setTableName(alias)
}
}
func (t *tableImpl) SchemaName() string { func (t *tableImpl) SchemaName() string {
return t.schemaName return t.schemaName
} }
@ -73,10 +70,6 @@ func (t *tableImpl) Alias() string {
return t.alias return t.alias
} }
func (t *tableImpl) Schema(schemaName string) {
t.schemaName = schemaName
}
func (t *tableImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) { func (t *tableImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
if t == nil { if t == nil {
panic("jet: tableImpl is nil") panic("jet: tableImpl is nil")
@ -163,9 +156,6 @@ func (t *joinTableImpl) Alias() string {
return "" return ""
} }
func (t *joinTableImpl) Schema(schemaName string) {
}
func (t *joinTableImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) { func (t *joinTableImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
if t == nil { if t == nil {
panic("jet: Join table is nil. ") panic("jet: Join table is nil. ")

View file

@ -6,7 +6,7 @@ import (
) )
func TestNewTable(t *testing.T) { func TestNewTable(t *testing.T) {
newTable := NewTable("schema", "table", IntegerColumn("intCol")) newTable := NewTable("schema", "table", "", IntegerColumn("intCol"))
require.Equal(t, newTable.SchemaName(), "schema") require.Equal(t, newTable.SchemaName(), "schema")
require.Equal(t, newTable.TableName(), "table") require.Equal(t, newTable.TableName(), "table")
@ -16,8 +16,8 @@ func TestNewTable(t *testing.T) {
} }
func TestNewJoinTable(t *testing.T) { func TestNewJoinTable(t *testing.T) {
newTable1 := NewTable("schema", "table", IntegerColumn("intCol1")) newTable1 := NewTable("schema", "table", "", IntegerColumn("intCol1"))
newTable2 := NewTable("schema", "table2", IntegerColumn("intCol2")) newTable2 := NewTable("schema", "table2", "", IntegerColumn("intCol2"))
joinTable := NewJoinTable(newTable1, newTable2, InnerJoin, IntegerColumn("intCol1").EQ(IntegerColumn("intCol2"))) joinTable := NewJoinTable(newTable1, newTable2, InnerJoin, IntegerColumn("intCol1").EQ(IntegerColumn("intCol2")))
@ -31,38 +31,3 @@ INNER JOIN schema.table2 ON ("intCol1" = "intCol2")`)
require.Equal(t, joinTable.columns()[0].Name(), "intCol1") require.Equal(t, joinTable.columns()[0].Name(), "intCol1")
require.Equal(t, joinTable.columns()[1].Name(), "intCol2") require.Equal(t, joinTable.columns()[1].Name(), "intCol2")
} }
func TestSchemaNameSet(t *testing.T) {
newTable := NewTable("schema", "table")
newTable.Schema("foo")
newTable.AS("bar")
assertClauseSerialize(t, newTable, `foo.table AS bar`)
}
func TestSchemaNameClear(t *testing.T) {
newTable := NewTable("schema", "table")
newTable.Schema("")
newTable.AS("bar")
assertClauseSerialize(t, newTable, `table AS bar`)
}
func TestNewJoinTableSchemaNameSet(t *testing.T) {
newTable1 := NewTable("schema", "table", IntegerColumn("intCol1"))
newTable2 := NewTable("schema", "table2", IntegerColumn("intCol2"))
newTable1.Schema("foo")
newTable2.Schema("foo")
joinTable := NewJoinTable(newTable1, newTable2, InnerJoin, IntegerColumn("intCol1").EQ(IntegerColumn("intCol2")))
joinTable.Schema("xxx")
assertClauseSerialize(t, joinTable, `foo.table
INNER JOIN foo.table2 ON ("intCol1" = "intCol2")`)
require.Equal(t, joinTable.SchemaName(), "foo")
require.Equal(t, joinTable.TableName(), "")
require.Equal(t, len(joinTable.columns()), 2)
require.Equal(t, joinTable.columns()[0].Name(), "intCol1")
require.Equal(t, joinTable.columns()[1].Name(), "intCol2")
}

View file

@ -26,7 +26,7 @@ var (
table1ColBool = BoolColumn("col_bool") table1ColBool = BoolColumn("col_bool")
table1ColDate = DateColumn("col_date") table1ColDate = DateColumn("col_date")
) )
var table1 = NewTable("db", "table1", table1Col1, table1ColInt, table1ColFloat, table1Col3, table1ColTime, table1ColTimez, table1ColBool, table1ColDate, table1ColTimestamp, table1ColTimestampz) var table1 = NewTable("db", "table1", "", table1Col1, table1ColInt, table1ColFloat, table1Col3, table1ColTime, table1ColTimez, table1ColBool, table1ColDate, table1ColTimestamp, table1ColTimestampz)
var ( var (
table2Col3 = IntegerColumn("col3") table2Col3 = IntegerColumn("col3")
@ -41,14 +41,14 @@ var (
table2ColTimestampz = TimestampzColumn("col_timestampz") table2ColTimestampz = TimestampzColumn("col_timestampz")
table2ColDate = DateColumn("col_date") table2ColDate = DateColumn("col_date")
) )
var table2 = NewTable("db", "table2", table2Col3, table2Col4, table2ColInt, table2ColFloat, table2ColStr, table2ColBool, table2ColTime, table2ColTimez, table2ColDate, table2ColTimestamp, table2ColTimestampz) var table2 = NewTable("db", "table2", "", table2Col3, table2Col4, table2ColInt, table2ColFloat, table2ColStr, table2ColBool, table2ColTime, table2ColTimez, table2ColDate, table2ColTimestamp, table2ColTimestampz)
var ( var (
table3Col1 = IntegerColumn("col1") table3Col1 = IntegerColumn("col1")
table3ColInt = IntegerColumn("col_int") table3ColInt = IntegerColumn("col_int")
table3StrCol = StringColumn("col2") table3StrCol = StringColumn("col2")
) )
var table3 = NewTable("db", "table3", table3Col1, table3ColInt, table3StrCol) var table3 = NewTable("db", "table3", "", table3Col1, table3ColInt, table3StrCol)
func assertClauseSerialize(t *testing.T, clause Serializer, query string, args ...interface{}) { func assertClauseSerialize(t *testing.T, clause Serializer, query string, args ...interface{}) {
out := SQLBuilder{Dialect: defaultDialect} out := SQLBuilder{Dialect: defaultDialect}