refact: using only Table struct instea of string

This commit is contained in:
Yehoshua Sandler 2025-04-27 20:09:40 -05:00
parent 60b1033dba
commit ef73d146fe
3 changed files with 22 additions and 18 deletions

View File

@ -124,28 +124,29 @@ func JoinTypeString(t JoinType) string {
type Column struct { type Column struct {
Name string `json:"name"` Name string `json:"name"`
Alias string `json:"alias"` 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 Join struct {
Type JoinType `json:"type"` Type JoinType `json:"type"`
Table Table `json:"table"` MainTable Table `json:"mainTable"`
Ons []Conditional `json:"ons"` JoiningTable Table `json:"joiningTable"`
Ons []Conditional `json:"ons"`
} }
type Select struct { type Select struct {
Table string `json:"table"` Table Table `json:"table"`
Columns []Column `json:"columns"` Columns []Column `json:"columns"`
Conditionals []Conditional `json:"conditionals"` Conditionals []Conditional `json:"conditionals"`
OrderBys []OrderBy `json:"order_bys"` OrderBys []OrderBy `json:"orderBys"`
Joins []Join `json:"joins"` Joins []Join `json:"joins"`
IsWildcard bool `json:"is_wildcard"` IsWildcard bool `json:"isWildcard"`
IsDistinct bool `json:"is_distinct"` IsDistinct bool `json:"is_distinct"`
} }
type OrderBy struct { type OrderBy struct {
Key string `json:"key"` Key string `json:"key"`
IsDescend bool `json:"is_descend"` IsDescend bool `json:"isDescend"`
} }
func MarshalSelect(selectStatement Select) ([]byte, error) { func MarshalSelect(selectStatement Select) ([]byte, error) {

View File

@ -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 { for _, condition := range q.Conditionals {
workingSqlSlice = append(workingSqlSlice, condition.Key) workingSqlSlice = append(workingSqlSlice, condition.Key)
@ -156,7 +156,7 @@ func ParseSelectStatement(sql string) Select {
if !passedTable && token.Type == sqllexer.IDENT { if !passedTable && token.Type == sqllexer.IDENT {
passedTable = true passedTable = true
query.Table = token.Value query.Table.Name = token.Value
continue continue
} else if !passedTable { } else if !passedTable {
continue continue

View File

@ -15,7 +15,7 @@ func TestParseSelectStatement(t *testing.T) {
{ {
input: "SELECT * FROM users WHERE age >= 30", input: "SELECT * FROM users WHERE age >= 30",
expected: Select{ expected: Select{
Table: "users", Table: Table{Name: "users"},
IsWildcard: true, IsWildcard: true,
Conditionals: []Conditional{ Conditionals: []Conditional{
{ {
@ -29,7 +29,7 @@ func TestParseSelectStatement(t *testing.T) {
{ {
input: "SELECT CustomerName, City FROM Customers", input: "SELECT CustomerName, City FROM Customers",
expected: Select{ expected: Select{
Table: "Customers", Table: Table{Name: "Customers"},
IsWildcard: false, IsWildcard: false,
Columns: []Column{ Columns: []Column{
{ {
@ -44,7 +44,7 @@ func TestParseSelectStatement(t *testing.T) {
{ {
input: "SELECT DISTINCT Country FROM Nations;", input: "SELECT DISTINCT Country FROM Nations;",
expected: Select{ expected: Select{
Table: "Nations", Table: Table{Name: "Nations"},
Columns: []Column{ Columns: []Column{
{ {
Name: "Country", Name: "Country",
@ -55,7 +55,7 @@ func TestParseSelectStatement(t *testing.T) {
{ {
input: "SELECT * FROM Orders ORDER BY StreetNumber, CountryCode;", input: "SELECT * FROM Orders ORDER BY StreetNumber, CountryCode;",
expected: Select{ expected: Select{
Table: "Orders", Table: Table{Name: "Orders"},
IsWildcard: true, IsWildcard: true,
OrderBys: []OrderBy{ OrderBys: []OrderBy{
{ {
@ -70,7 +70,7 @@ func TestParseSelectStatement(t *testing.T) {
{ {
input: "SELECT * FROM ZipCodes ORDER BY Code ASC, StateName DESC", input: "SELECT * FROM ZipCodes ORDER BY Code ASC, StateName DESC",
expected: Select{ expected: Select{
Table: "ZipCodes", Table: Table{Name: "ZipCodes"},
IsWildcard: true, IsWildcard: true,
OrderBys: []OrderBy{ 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", 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{ expected: Select{
Table: "Addresses", Table: Table{Name: "Addresses"},
IsWildcard: false, IsWildcard: false,
Columns: []Column{ Columns: []Column{
{ {
@ -172,8 +172,11 @@ func TestParseSelectStatement(t *testing.T) {
t.Run(testName, func(t *testing.T) { t.Run(testName, func(t *testing.T) {
answer := ParseSelectStatement(sql.input) answer := ParseSelectStatement(sql.input)
if answer.Table != expected.Table { if answer.Table.Alias != expected.Table.Alias {
t.Errorf("got %s for Select.Table, expected %s", answer.Table, expected.Table) 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 { if answer.IsWildcard != expected.IsWildcard {
t.Errorf("got %#v for Select.IsWildcard, expected %#v", answer.IsWildcard, expected.IsWildcard) t.Errorf("got %#v for Select.IsWildcard, expected %#v", answer.IsWildcard, expected.IsWildcard)