@@ -22,7 +22,6 @@ import (
2222 "golang.org/x/sync/semaphore"
2323 "net"
2424 "net/url"
25- "reflect"
2625 "strconv"
2726 "strings"
2827 "sync"
@@ -37,6 +36,7 @@ type DB struct {
3736 Options * Options
3837
3938 addr string
39+ columnMap ColumnMap
4040 logger * logging.Logger
4141 tableSemaphores map [string ]* semaphore.Weighted
4242 tableSemaphoresMu sync.Mutex
@@ -215,6 +215,7 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry
215215 return & DB {
216216 DB : db ,
217217 Options : & c .Options ,
218+ columnMap : NewColumnMap (db .Mapper ),
218219 addr : addr ,
219220 logger : logger ,
220221 tableSemaphores : make (map [string ]* semaphore.Weighted ),
@@ -226,20 +227,6 @@ func (db *DB) GetAddr() string {
226227 return db .addr
227228}
228229
229- // BuildColumns returns all columns of the given struct.
230- func (db * DB ) BuildColumns (subject interface {}) []string {
231- fields := db .Mapper .TypeMap (reflect .TypeOf (subject )).Names
232- columns := make ([]string , 0 , len (fields ))
233- for _ , f := range fields {
234- if f .Field .Tag == "" {
235- continue
236- }
237- columns = append (columns , f .Name )
238- }
239-
240- return columns
241- }
242-
243230// BuildDeleteStmt returns a DELETE statement for the given struct.
244231func (db * DB ) BuildDeleteStmt (from interface {}) string {
245232 return fmt .Sprintf (
@@ -250,7 +237,7 @@ func (db *DB) BuildDeleteStmt(from interface{}) string {
250237
251238// BuildInsertStmt returns an INSERT INTO statement for the given struct.
252239func (db * DB ) BuildInsertStmt (into interface {}) (string , int ) {
253- columns := db .BuildColumns (into )
240+ columns := db .columnMap . Columns (into )
254241
255242 return fmt .Sprintf (
256243 `INSERT INTO "%s" ("%s") VALUES (%s)` ,
@@ -264,7 +251,7 @@ func (db *DB) BuildInsertStmt(into interface{}) (string, int) {
264251// which the database ignores rows that have already been inserted.
265252func (db * DB ) BuildInsertIgnoreStmt (into interface {}) (string , int ) {
266253 table := TableName (into )
267- columns := db .BuildColumns (into )
254+ columns := db .columnMap . Columns (into )
268255 var clause string
269256
270257 switch db .DriverName () {
@@ -289,7 +276,7 @@ func (db *DB) BuildInsertIgnoreStmt(into interface{}) (string, int) {
289276func (db * DB ) BuildSelectStmt (table interface {}, columns interface {}) string {
290277 q := fmt .Sprintf (
291278 `SELECT "%s" FROM "%s"` ,
292- strings .Join (db .BuildColumns (columns ), `", "` ),
279+ strings .Join (db .columnMap . Columns (columns ), `", "` ),
293280 TableName (table ),
294281 )
295282
@@ -303,7 +290,7 @@ func (db *DB) BuildSelectStmt(table interface{}, columns interface{}) string {
303290
304291// BuildUpdateStmt returns an UPDATE statement for the given struct.
305292func (db * DB ) BuildUpdateStmt (update interface {}) (string , int ) {
306- columns := db .BuildColumns (update )
293+ columns := db .columnMap . Columns (update )
307294 set := make ([]string , 0 , len (columns ))
308295
309296 for _ , col := range columns {
@@ -319,12 +306,12 @@ func (db *DB) BuildUpdateStmt(update interface{}) (string, int) {
319306
320307// BuildUpsertStmt returns an upsert statement for the given struct.
321308func (db * DB ) BuildUpsertStmt (subject interface {}) (stmt string , placeholders int ) {
322- insertColumns := db .BuildColumns (subject )
309+ insertColumns := db .columnMap . Columns (subject )
323310 table := TableName (subject )
324311 var updateColumns []string
325312
326313 if upserter , ok := subject .(Upserter ); ok {
327- updateColumns = db .BuildColumns (upserter .Upsert ())
314+ updateColumns = db .columnMap . Columns (upserter .Upsert ())
328315 } else {
329316 updateColumns = insertColumns
330317 }
@@ -358,7 +345,7 @@ func (db *DB) BuildUpsertStmt(subject interface{}) (stmt string, placeholders in
358345// BuildWhere returns a WHERE clause with named placeholder conditions built from the specified struct
359346// combined with the AND operator.
360347func (db * DB ) BuildWhere (subject interface {}) (string , int ) {
361- columns := db .BuildColumns (subject )
348+ columns := db .columnMap . Columns (subject )
362349 where := make ([]string , 0 , len (columns ))
363350 for _ , col := range columns {
364351 where = append (where , fmt .Sprintf (`"%s" = :%s` , col , col ))
0 commit comments