From ef73d146fe687580e6b4000c280c01dac1b66b26 Mon Sep 17 00:00:00 2001 From: ysandler Date: Sun, 27 Apr 2025 20:09:40 -0500 Subject: [PATCH] refact: using only Table struct instea of string --- q/dto.go | 17 +++++++++-------- q/select.go | 4 ++-- q/select_test.go | 19 +++++++++++-------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/q/dto.go b/q/dto.go index cffa7d4..7dbc991 100644 --- a/q/dto.go +++ b/q/dto.go @@ -124,28 +124,29 @@ func JoinTypeString(t JoinType) string { type Column struct { Name string `json:"name"` Alias string `json:"alias"` - AggregateFunction AggregateFunctionType `json:"aggregate_function"` // Changed type name to match Go naming conventions + AggregateFunction AggregateFunctionType `json:"aggregateFunction"` } type Join struct { - Type JoinType `json:"type"` - Table Table `json:"table"` - Ons []Conditional `json:"ons"` + Type JoinType `json:"type"` + MainTable Table `json:"mainTable"` + JoiningTable Table `json:"joiningTable"` + Ons []Conditional `json:"ons"` } type Select struct { - Table string `json:"table"` + Table Table `json:"table"` Columns []Column `json:"columns"` Conditionals []Conditional `json:"conditionals"` - OrderBys []OrderBy `json:"order_bys"` + OrderBys []OrderBy `json:"orderBys"` Joins []Join `json:"joins"` - IsWildcard bool `json:"is_wildcard"` + IsWildcard bool `json:"isWildcard"` IsDistinct bool `json:"is_distinct"` } type OrderBy struct { Key string `json:"key"` - IsDescend bool `json:"is_descend"` + IsDescend bool `json:"isDescend"` } func MarshalSelect(selectStatement Select) ([]byte, error) { diff --git a/q/select.go b/q/select.go index eb1e249..a530c98 100644 --- a/q/select.go +++ b/q/select.go @@ -45,7 +45,7 @@ func (q *Select) GetFullSql() string { } } - workingSqlSlice = append(workingSqlSlice, "FROM "+q.Table) + workingSqlSlice = append(workingSqlSlice, "FROM "+q.Table.Name) // TODO: figure out what to do from alias for _, condition := range q.Conditionals { workingSqlSlice = append(workingSqlSlice, condition.Key) @@ -156,7 +156,7 @@ func ParseSelectStatement(sql string) Select { if !passedTable && token.Type == sqllexer.IDENT { passedTable = true - query.Table = token.Value + query.Table.Name = token.Value continue } else if !passedTable { continue diff --git a/q/select_test.go b/q/select_test.go index 555e516..da3f2fb 100644 --- a/q/select_test.go +++ b/q/select_test.go @@ -15,7 +15,7 @@ func TestParseSelectStatement(t *testing.T) { { input: "SELECT * FROM users WHERE age >= 30", expected: Select{ - Table: "users", + Table: Table{Name: "users"}, IsWildcard: true, Conditionals: []Conditional{ { @@ -29,7 +29,7 @@ func TestParseSelectStatement(t *testing.T) { { input: "SELECT CustomerName, City FROM Customers", expected: Select{ - Table: "Customers", + Table: Table{Name: "Customers"}, IsWildcard: false, Columns: []Column{ { @@ -44,7 +44,7 @@ func TestParseSelectStatement(t *testing.T) { { input: "SELECT DISTINCT Country FROM Nations;", expected: Select{ - Table: "Nations", + Table: Table{Name: "Nations"}, Columns: []Column{ { Name: "Country", @@ -55,7 +55,7 @@ func TestParseSelectStatement(t *testing.T) { { input: "SELECT * FROM Orders ORDER BY StreetNumber, CountryCode;", expected: Select{ - Table: "Orders", + Table: Table{Name: "Orders"}, IsWildcard: true, OrderBys: []OrderBy{ { @@ -70,7 +70,7 @@ func TestParseSelectStatement(t *testing.T) { { input: "SELECT * FROM ZipCodes ORDER BY Code ASC, StateName DESC", expected: Select{ - Table: "ZipCodes", + Table: Table{Name: "ZipCodes"}, IsWildcard: true, OrderBys: []OrderBy{ { @@ -87,7 +87,7 @@ func TestParseSelectStatement(t *testing.T) { { input: "SELECT id, streetNumber, streetName, city, state FROM Addresses WHERE state = 'AL' AND zip > 9000 OR zip <= 12000 ORDER BY zip DESC, streetNumber", expected: Select{ - Table: "Addresses", + Table: Table{Name: "Addresses"}, IsWildcard: false, Columns: []Column{ { @@ -172,8 +172,11 @@ func TestParseSelectStatement(t *testing.T) { t.Run(testName, func(t *testing.T) { answer := ParseSelectStatement(sql.input) - if answer.Table != expected.Table { - t.Errorf("got %s for Select.Table, expected %s", answer.Table, expected.Table) + if answer.Table.Alias != expected.Table.Alias { + t.Errorf("got %s for Select.Table.Alias, expected %s", answer.Table.Alias, expected.Table.Alias) + } + if answer.Table.Name != expected.Table.Name { + t.Errorf("got %s for Select.Table.Name, expected %s", answer.Table.Name, expected.Table.Name) } if answer.IsWildcard != expected.IsWildcard { t.Errorf("got %#v for Select.IsWildcard, expected %#v", answer.IsWildcard, expected.IsWildcard)