@@ -6,7 +6,10 @@ package sqlc
66
77import (
88 "encoding/json"
9+ "fmt"
910 "os"
11+ "slices"
12+ "strings"
1013)
1114
1215// Catalog represents the root structure of a sqlc catalog file.
@@ -35,7 +38,8 @@ func LoadCatalog(path string) (*Catalog, error) {
3538type Schema struct {
3639 Name string `json:"name"`
3740 Tables []Table `json:"tables,omitempty"`
38- Attrs
41+ // Inherit common attributes
42+ Attributes
3943}
4044
4145// Table represents a database table with its columns, indexes, and constraints.
@@ -45,15 +49,66 @@ type Table struct {
4549 Indexes []Index `json:"indexes,omitempty"`
4650 PrimaryKey * Index `json:"primary_key,omitempty"`
4751 ForeignKeys []ForeignKey `json:"foreign_keys,omitempty"`
48- Attrs
52+ // Inherit common attributes
53+ Attributes
54+ }
55+
56+ // GetColumn retrieves a column by name from the table.
57+ func (x * Table ) GetColumn (name string ) * Column {
58+ for _ , column := range x .Columns {
59+ if column .Name == name {
60+ return & column
61+ }
62+ }
63+
64+ return nil
65+ }
66+
67+ // GetNonUniqueIndexes retrieves all non-unique indexes from the table.
68+ func (x * Table ) GetNonUniqueIndexes () []* Index {
69+ var keys []* Index
70+
71+ for _ , index := range x .Indexes {
72+ if index .HasExpr () {
73+ continue
74+ }
75+
76+ if ! index .Unique {
77+ keys = append (keys , & index )
78+ }
79+ }
80+
81+ return keys
82+ }
83+
84+ // GetUniqueKeys retrieves all unique keys (primary key and unique indexes) from the table.
85+ func (x * Table ) GetUniqueKeys () []* Index {
86+ var keys []* Index
87+
88+ if x .PrimaryKey != nil {
89+ keys = append (keys , x .PrimaryKey )
90+ }
91+
92+ for _ , index := range x .Indexes {
93+ if index .HasExpr () {
94+ continue
95+ }
96+
97+ if index .Unique {
98+ keys = append (keys , & index )
99+ }
100+ }
101+
102+ return keys
49103}
50104
51105// Column represents a table column with its name, data type, and nullability.
52106type Column struct {
53107 Name string `json:"name"`
54108 Type string `json:"type,omitempty"`
55109 Null bool `json:"null,omitempty"`
56- Attrs
110+ // Inherit common attributes
111+ Attributes
57112}
58113
59114// Index represents a database index on one or more columns or expressions.
@@ -63,6 +118,13 @@ type Index struct {
63118 Parts []IndexPart `json:"parts,omitempty"`
64119}
65120
121+ // HasExpr checks if the index contains any expression-based parts.
122+ func (x * Index ) HasExpr () bool {
123+ return slices .ContainsFunc (x .Parts , func (x IndexPart ) bool {
124+ return x .Expr != ""
125+ })
126+ }
127+
66128// IndexPart represents a single part of an index, which can be either a column or an expression.
67129type IndexPart struct {
68130 Desc bool `json:"desc,omitempty"`
@@ -81,9 +143,68 @@ type ForeignKey struct {
81143 } `json:"references"`
82144}
83145
84- // Attrs represents common attributes that can be applied to schemas, tables, and columns.
146+ // Condition represents a simple equality condition between a column and an argument.
147+ type Condition struct {
148+ Column * Column
149+ Argument * Argument
150+ }
151+
152+ // String returns the string representation of the Condition for use in SQL queries.
153+ func (x * Condition ) String () string {
154+ return fmt .Sprintf ("%s = %v" , x .Column .Name , x .Argument )
155+ }
156+
157+ // CompositeCondition represents a combination of multiple conditions using a logical operator (e.g., AND, OR).
158+ type CompositeCondition struct {
159+ Operator string
160+ Conditions []fmt.Stringer
161+ }
162+
163+ // AddColumn adds a new condition for the specified column to the CompositeCondition.
164+ func (x * CompositeCondition ) AddColumn (column * Column ) {
165+ x .Conditions = append (x .Conditions ,
166+ & Condition {
167+ Column : column ,
168+ Argument : & Argument {
169+ Column : column ,
170+ },
171+ },
172+ )
173+ }
174+
175+ // String returns the string representation of the CompositeCondition for use in SQL queries.
176+ func (x * CompositeCondition ) String () string {
177+ var items []string
178+
179+ for _ , condition := range x .Conditions {
180+ if expression := condition .String (); expression != "" {
181+ items = append (items , expression )
182+ }
183+ }
184+
185+ if len (items ) == 0 {
186+ return ""
187+ }
188+
189+ return strings .Join (items , fmt .Sprintf (" %s " , x .Operator ))
190+ }
191+
192+ // Argument represents SQL argument corresponding to a column.
193+ type Argument struct {
194+ Column * Column `json:"column"`
195+ }
196+
197+ // String returns the string representation of the Argument for use in SQL queries.
198+ func (x * Argument ) String () string {
199+ if x .Column .Null {
200+ return fmt .Sprintf ("sqlc.narg(%s)" , x .Column .Name )
201+ }
202+ return fmt .Sprintf ("sqlc.arg(%s)" , x .Column .Name )
203+ }
204+
205+ // Attributes represents common attributes that can be applied to schemas, tables, and columns.
85206// These are typically dialect-specific metadata.
86- type Attrs struct {
207+ type Attributes struct {
87208 Comment string `json:"comment,omitempty"`
88209 Charset string `json:"charset,omitempty"`
89210 Collate string `json:"collate,omitempty"`
0 commit comments