@@ -6,7 +6,9 @@ package sqlc
66
77import (
88 "encoding/json"
9+ "fmt"
910 "os"
11+ "strings"
1012)
1113
1214// Catalog represents the root structure of a sqlc catalog file.
@@ -35,7 +37,8 @@ func LoadCatalog(path string) (*Catalog, error) {
3537type Schema struct {
3638 Name string `json:"name"`
3739 Tables []Table `json:"tables,omitempty"`
38- Attrs
40+ // Inherit common attributes
41+ Attributes
3942}
4043
4144// Table represents a database table with its columns, indexes, and constraints.
@@ -45,15 +48,58 @@ type Table struct {
4548 Indexes []Index `json:"indexes,omitempty"`
4649 PrimaryKey * Index `json:"primary_key,omitempty"`
4750 ForeignKeys []ForeignKey `json:"foreign_keys,omitempty"`
48- Attrs
51+ // Inherit common attributes
52+ Attributes
53+ }
54+
55+ // GetColumn retrieves a column by name from the table.
56+ func (x * Table ) GetColumn (name string ) * Column {
57+ for _ , column := range x .Columns {
58+ if column .Name == name {
59+ return & column
60+ }
61+ }
62+
63+ return nil
64+ }
65+
66+ // GetIndexKeys retrieves all unique indexes from the table.
67+ func (x * Table ) GetIndexKeys () []* Index {
68+ var keys []* Index
69+
70+ for _ , index := range x .Indexes {
71+ if ! index .Unique {
72+ keys = append (keys , & index )
73+ }
74+ }
75+
76+ return keys
77+ }
78+
79+ // GetUniqueKeys retrieves all unique keys (primary key and unique indexes) from the table.
80+ func (x * Table ) GetUniqueKeys () []* Index {
81+ var keys []* Index
82+
83+ if x .PrimaryKey != nil {
84+ keys = append (keys , x .PrimaryKey )
85+ }
86+
87+ for _ , index := range x .Indexes {
88+ if index .Unique {
89+ keys = append (keys , & index )
90+ }
91+ }
92+
93+ return keys
4994}
5095
5196// Column represents a table column with its name, data type, and nullability.
5297type Column struct {
5398 Name string `json:"name"`
5499 Type string `json:"type,omitempty"`
55100 Null bool `json:"null,omitempty"`
56- Attrs
101+ // Inherit common attributes
102+ Attributes
57103}
58104
59105// Index represents a database index on one or more columns or expressions.
@@ -81,9 +127,68 @@ type ForeignKey struct {
81127 } `json:"references"`
82128}
83129
84- // Attrs represents common attributes that can be applied to schemas, tables, and columns.
130+ // Condition represents a simple equality condition between a column and an argument.
131+ type Condition struct {
132+ Column * Column
133+ Argument * Argument
134+ }
135+
136+ // String returns the string representation of the Condition for use in SQL queries.
137+ func (x * Condition ) String () string {
138+ return fmt .Sprintf ("%s = %v" , x .Column .Name , x .Argument )
139+ }
140+
141+ // CompositeCondition represents a combination of multiple conditions using a logical operator (e.g., AND, OR).
142+ type CompositeCondition struct {
143+ Operator string
144+ Conditions []fmt.Stringer
145+ }
146+
147+ // AddColumn adds a new condition for the specified column to the CompositeCondition.
148+ func (x * CompositeCondition ) AddColumn (column * Column ) {
149+ x .Conditions = append (x .Conditions ,
150+ & Condition {
151+ Column : column ,
152+ Argument : & Argument {
153+ Column : column ,
154+ },
155+ },
156+ )
157+ }
158+
159+ // String returns the string representation of the CompositeCondition for use in SQL queries.
160+ func (x * CompositeCondition ) String () string {
161+ var items []string
162+
163+ for _ , condition := range x .Conditions {
164+ if expression := condition .String (); expression != "" {
165+ items = append (items , expression )
166+ }
167+ }
168+
169+ if len (items ) == 0 {
170+ return ""
171+ }
172+
173+ return strings .Join (items , fmt .Sprintf (" %s " , x .Operator ))
174+ }
175+
176+ // Argument represents SQL argument corresponding to a column.
177+ type Argument struct {
178+ Column * Column `json:"column"`
179+ }
180+
181+ // String returns the string representation of the Argument for use in SQL queries.
182+ func (x * Argument ) String () string {
183+ if x .Column .Null {
184+ return fmt .Sprintf ("sqlc.narg(%s)" , x .Column .Name )
185+ }
186+ return fmt .Sprintf ("sqlc.arg(%s)" , x .Column .Name )
187+ }
188+
189+ // Attributes represents common attributes that can be applied to schemas, tables, and columns.
85190// These are typically dialect-specific metadata.
86- type Attrs struct {
191+ type Attributes struct {
87192 Comment string `json:"comment,omitempty"`
88193 Charset string `json:"charset,omitempty"`
89194 Collate string `json:"collate,omitempty"`
0 commit comments