@@ -22,52 +22,52 @@ import (
2222 "github.com/jackc/pgx/v5"
2323)
2424
25- // DbInfo provides information about tables in a database.
26- type DbInfo struct {
25+ // NumericScaleInfo provides information about the scale of numeric columns
26+ // in a database.
27+ type NumericScaleInfo struct {
2728 conn * pgx.Conn
2829 cache map [string ]* tableCache
2930}
3031
3132// tableCache stores information about a table.
32- // The information is cached and refreshed every 'cacheExpiration'.
3333type tableCache struct {
3434 columns map [string ]int
3535}
3636
37- func NewDbInfo (conn * pgx.Conn ) * DbInfo {
38- return & DbInfo {
37+ func NewDbInfo (conn * pgx.Conn ) * NumericScaleInfo {
38+ return & NumericScaleInfo {
3939 conn : conn ,
4040 cache : map [string ]* tableCache {},
4141 }
4242}
4343
44- func (d * DbInfo ) GetNumericColumnScale (ctx context.Context , table string , column string ) (int , error ) {
45- // Check if table exists in cache and is not expired
46- tableInfo , ok := d .cache [table ]
44+ func (i * NumericScaleInfo ) Get (ctx context.Context , table string , column string ) (int , error ) {
45+ // Check if table exists in cache
46+ tableInfo , ok := i .cache [table ]
4747 if ok {
4848 scale , ok := tableInfo .columns [column ]
4949 if ok {
5050 return scale , nil
5151 }
5252 } else {
5353 // Table info has expired, refresh the cache
54- d .cache [table ] = & tableCache {
54+ i .cache [table ] = & tableCache {
5555 columns : map [string ]int {},
5656 }
5757 }
5858
5959 // Fetch scale from database
60- scale , err := d . numericScaleFromDb (ctx , table , column )
60+ scale , err := i . fetchFromDB (ctx , table , column )
6161 if err != nil {
6262 return 0 , err
6363 }
6464
65- d .cache [table ].columns [column ] = scale
65+ i .cache [table ].columns [column ] = scale
6666
6767 return scale , nil
6868}
6969
70- func (d * DbInfo ) numericScaleFromDb (ctx context.Context , table string , column string ) (int , error ) {
70+ func (i * NumericScaleInfo ) fetchFromDB (ctx context.Context , table string , column string ) (int , error ) {
7171 // Query to get the column type and numeric scale
7272 query := `
7373 SELECT
@@ -83,7 +83,7 @@ func (d *DbInfo) numericScaleFromDb(ctx context.Context, table string, column st
8383 var dataType string
8484 var numericScale * int
8585
86- err := d .conn .QueryRow (ctx , query , table , column ).Scan (& dataType , & numericScale )
86+ err := i .conn .QueryRow (ctx , query , table , column ).Scan (& dataType , & numericScale )
8787 if err != nil {
8888 if errors .Is (err , pgx .ErrNoRows ) {
8989 return 0 , fmt .Errorf ("column %s not found in table %s" , column , table )
0 commit comments