From 74288d5418a599e162a16ead7a51b701a0b34887 Mon Sep 17 00:00:00 2001 From: go-jet Date: Mon, 12 Aug 2019 11:33:46 +0200 Subject: [PATCH] Hide private methods. --- internal/jet/clause.go | 2 +- internal/jet/column.go | 28 ++++++++++++++-------------- internal/jet/column_test.go | 2 +- internal/jet/column_types.go | 36 ++++++++++++++++++------------------ internal/jet/table.go | 27 +++++++++++++++++++-------- internal/jet/utils.go | 2 +- 6 files changed, 54 insertions(+), 43 deletions(-) diff --git a/internal/jet/clause.go b/internal/jet/clause.go index ffddbb6..c4a5469 100644 --- a/internal/jet/clause.go +++ b/internal/jet/clause.go @@ -316,7 +316,7 @@ func (i *ClauseInsert) GetColumns() []Column { return i.Columns } - return i.Table.Columns() + return i.Table.columns() } func (i *ClauseInsert) Serialize(statementType StatementType, out *SqlBuilder) error { diff --git a/internal/jet/column.go b/internal/jet/column.go index 06173fd..fed37d0 100644 --- a/internal/jet/column.go +++ b/internal/jet/column.go @@ -6,9 +6,9 @@ type Column interface { Name() string TableName() string - SetTableName(table string) - SetSubQuery(subQuery SelectTable) - DefaultAlias() string + setTableName(table string) + setSubQuery(subQuery SelectTable) + defaultAlias() string } // Column is common column interface for all types of columns. @@ -46,15 +46,15 @@ func (c *columnImpl) TableName() string { return c.tableName } -func (c *columnImpl) SetTableName(table string) { +func (c *columnImpl) setTableName(table string) { c.tableName = table } -func (c *columnImpl) SetSubQuery(subQuery SelectTable) { +func (c *columnImpl) setSubQuery(subQuery SelectTable) { c.subQuery = subQuery } -func (c *columnImpl) DefaultAlias() string { +func (c *columnImpl) defaultAlias() string { if c.tableName != "" { return c.tableName + "." + c.name } @@ -65,7 +65,7 @@ func (c *columnImpl) DefaultAlias() string { func (c *columnImpl) serializeForOrderBy(statement StatementType, out *SqlBuilder) error { if statement == SetStatementType { // 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 } @@ -81,7 +81,7 @@ func (c columnImpl) serializeForProjection(statement StatementType, out *SqlBuil } out.WriteString("AS") - out.WriteAlias(c.DefaultAlias()) + out.WriteAlias(c.defaultAlias()) return nil } @@ -91,7 +91,7 @@ func (c columnImpl) serialize(statement StatementType, out *SqlBuilder, options if c.subQuery != nil { out.WriteIdentifier(c.subQuery.Alias()) out.WriteByte('.') - out.WriteIdentifier(c.DefaultAlias(), true) + out.WriteIdentifier(c.defaultAlias(), true) } else { if c.tableName != "" { out.WriteIdentifier(c.tableName) @@ -110,7 +110,7 @@ type IColumnList interface { Projection Column - Columns() []ColumnExpression + columns() []ColumnExpression } func ColumnList(columns ...ColumnExpression) IColumnList { @@ -120,7 +120,7 @@ func ColumnList(columns ...ColumnExpression) IColumnList { // ColumnList is redefined type to support list of columns as single Projection type columnListImpl []ColumnExpression -func (cl columnListImpl) Columns() []ColumnExpression { +func (cl columnListImpl) columns() []ColumnExpression { return cl } @@ -153,6 +153,6 @@ func (cl columnListImpl) Name() string { return "" } // TableName is placeholder for ColumnList to implement Column interface func (cl columnListImpl) TableName() string { return "" } -func (cl columnListImpl) SetTableName(name string) {} -func (cl columnListImpl) SetSubQuery(subQuery SelectTable) {} -func (cl columnListImpl) DefaultAlias() string { return "" } +func (cl columnListImpl) setTableName(name string) {} +func (cl columnListImpl) setSubQuery(subQuery SelectTable) {} +func (cl columnListImpl) defaultAlias() string { return "" } diff --git a/internal/jet/column_test.go b/internal/jet/column_test.go index 2c766d9..ca3f5f6 100644 --- a/internal/jet/column_test.go +++ b/internal/jet/column_test.go @@ -7,7 +7,7 @@ func TestColumn(t *testing.T) { column.ExpressionInterfaceImpl.Parent = &column assertClauseSerialize(t, column, "col") - column.SetTableName("table1") + column.setTableName("table1") assertClauseSerialize(t, column, "table1.col") assertProjectionSerialize(t, &column, `table1.col AS "table1.col"`) assertProjectionSerialize(t, column.AS("alias1"), `table1.col AS "alias1"`) diff --git a/internal/jet/column_types.go b/internal/jet/column_types.go index c82a5a1..5443911 100644 --- a/internal/jet/column_types.go +++ b/internal/jet/column_types.go @@ -16,8 +16,8 @@ type boolColumnImpl struct { func (i *boolColumnImpl) fromImpl(subQuery SelectTable) Projection { newBoolColumn := BoolColumn(i.name) - newBoolColumn.SetTableName(i.tableName) - newBoolColumn.SetSubQuery(subQuery) + newBoolColumn.setTableName(i.tableName) + newBoolColumn.setSubQuery(subQuery) return newBoolColumn } @@ -54,8 +54,8 @@ type floatColumnImpl struct { func (i *floatColumnImpl) fromImpl(subQuery SelectTable) Projection { newFloatColumn := FloatColumn(i.name) - newFloatColumn.SetTableName(i.tableName) - newFloatColumn.SetSubQuery(subQuery) + newFloatColumn.setTableName(i.tableName) + newFloatColumn.setSubQuery(subQuery) return newFloatColumn } @@ -93,8 +93,8 @@ type integerColumnImpl struct { func (i *integerColumnImpl) fromImpl(subQuery SelectTable) Projection { newIntColumn := IntegerColumn(i.name) - newIntColumn.SetTableName(i.tableName) - newIntColumn.SetSubQuery(subQuery) + newIntColumn.setTableName(i.tableName) + newIntColumn.setSubQuery(subQuery) return newIntColumn } @@ -131,8 +131,8 @@ type stringColumnImpl struct { func (i *stringColumnImpl) fromImpl(subQuery SelectTable) Projection { newStrColumn := StringColumn(i.name) - newStrColumn.SetTableName(i.tableName) - newStrColumn.SetSubQuery(subQuery) + newStrColumn.setTableName(i.tableName) + newStrColumn.setSubQuery(subQuery) return newStrColumn } @@ -167,8 +167,8 @@ type timeColumnImpl struct { func (i *timeColumnImpl) fromImpl(subQuery SelectTable) Projection { newTimeColumn := TimeColumn(i.name) - newTimeColumn.SetTableName(i.tableName) - newTimeColumn.SetSubQuery(subQuery) + newTimeColumn.setTableName(i.tableName) + newTimeColumn.setSubQuery(subQuery) return newTimeColumn } @@ -203,8 +203,8 @@ type timezColumnImpl struct { func (i *timezColumnImpl) fromImpl(subQuery SelectTable) Projection { newTimezColumn := TimezColumn(i.name) - newTimezColumn.SetTableName(i.tableName) - newTimezColumn.SetSubQuery(subQuery) + newTimezColumn.setTableName(i.tableName) + newTimezColumn.setSubQuery(subQuery) return newTimezColumn } @@ -240,8 +240,8 @@ type timestampColumnImpl struct { func (i *timestampColumnImpl) fromImpl(subQuery SelectTable) Projection { newTimestampColumn := TimestampColumn(i.name) - newTimestampColumn.SetTableName(i.tableName) - newTimestampColumn.SetSubQuery(subQuery) + newTimestampColumn.setTableName(i.tableName) + newTimestampColumn.setSubQuery(subQuery) return newTimestampColumn } @@ -277,8 +277,8 @@ type timestampzColumnImpl struct { func (i *timestampzColumnImpl) fromImpl(subQuery SelectTable) Projection { newTimestampzColumn := TimestampzColumn(i.name) - newTimestampzColumn.SetTableName(i.tableName) - newTimestampzColumn.SetSubQuery(subQuery) + newTimestampzColumn.setTableName(i.tableName) + newTimestampzColumn.setSubQuery(subQuery) return newTimestampzColumn } @@ -314,8 +314,8 @@ type dateColumnImpl struct { func (i *dateColumnImpl) fromImpl(subQuery SelectTable) Projection { newDateColumn := DateColumn(i.name) - newDateColumn.SetTableName(i.tableName) - newDateColumn.SetSubQuery(subQuery) + newDateColumn.setTableName(i.tableName) + newDateColumn.setSubQuery(subQuery) return newDateColumn } diff --git a/internal/jet/table.go b/internal/jet/table.go index 61f5fc8..7551d90 100644 --- a/internal/jet/table.go +++ b/internal/jet/table.go @@ -11,7 +11,7 @@ type SerializerTable interface { } type TableInterface interface { - Columns() []Column + columns() []Column SchemaName() string TableName() string AS(alias string) @@ -27,7 +27,7 @@ func NewTable(schemaName, name string, columns ...ColumnExpression) TableImpl { } for _, c := range columns { - c.SetTableName(name) + c.setTableName(name) } return t @@ -44,7 +44,7 @@ func (t *TableImpl) AS(alias string) { t.alias = alias for _, c := range t.columnList { - c.SetTableName(alias) + c.setTableName(alias) } } @@ -56,7 +56,7 @@ func (t *TableImpl) TableName() string { return t.name } -func (t *TableImpl) Columns() []Column { +func (t *TableImpl) columns() []Column { ret := []Column{} for _, col := range t.columnList { @@ -114,6 +114,9 @@ func NewJoinTableImpl(lhs Serializer, rhs Serializer, joinType JoinType, onCondi } func (t *JoinTableImpl) SchemaName() string { + if table, ok := t.lhs.(TableInterface); ok { + return table.SchemaName() + } return "" } @@ -122,8 +125,16 @@ func (t *JoinTableImpl) TableName() string { } func (t *JoinTableImpl) Columns() []Column { - //return append(t.lhs.columns(), t.rhs.columns()...) - panic("Unimplemented") + var ret []Column + + if lhsTable, ok := t.lhs.(TableInterface); ok { + ret = append(ret, lhsTable.columns()...) + } + if rhsTable, ok := t.rhs.(TableInterface); ok { + ret = append(ret, rhsTable.columns()...) + } + + return ret } func (t *JoinTableImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) (err error) { @@ -180,7 +191,7 @@ func UnwindColumns(column1 Column, columns ...Column) []Column { columnList := []Column{} if val, ok := column1.(IColumnList); ok { - for _, col := range val.Columns() { + for _, col := range val.columns() { columnList = append(columnList, col) } columnList = append(columnList, columns...) @@ -197,7 +208,7 @@ func UnwidColumnList(columns []Column) []Column { for _, col := range columns { if columnList, ok := col.(IColumnList); ok { - for _, c := range columnList.Columns() { + for _, c := range columnList.columns() { ret = append(ret, c) } } else { diff --git a/internal/jet/utils.go b/internal/jet/utils.go index 9e8a69f..2000f83 100644 --- a/internal/jet/utils.go +++ b/internal/jet/utils.go @@ -146,7 +146,7 @@ func UnwindRowFromModel(columns []Column, data interface{}) []Serializer { structField := structValue.FieldByName(structFieldName) if !structField.IsValid() { - panic("missing struct field for column : " + column.Name()) + panic("missing struct field for column : " + columnName) } var field interface{}