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 {
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) {

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 {
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

View File

@ -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)