Skip to content

Commit c8daaa5

Browse files
committed
✨ feat: added func to get table info #5
1 parent fe5534e commit c8daaa5

4 files changed

Lines changed: 42 additions & 0 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ require (
1818
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
1919
github.com/sirupsen/logrus v1.9.3 // indirect
2020
golang.org/x/sys v0.12.0 // indirect
21+
gopkg.in/guregu/null.v3 v3.5.0
2122
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
4242
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
4343
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4444
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
45+
gopkg.in/guregu/null.v3 v3.5.0 h1:xTcasT8ETfMcUHn0zTvIYtQud/9Mx5dJqD554SZct0o=
46+
gopkg.in/guregu/null.v3 v3.5.0/go.mod h1:E4tX2Qe3h7QdL+uZ3a0vqvYwKQsRSQKM5V4YltdgH9Y=
4547
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
4648
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
4749
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

postgresconn/postgresconn_model.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package postgresconn
22

3+
import "gopkg.in/guregu/null.v3"
4+
35
type IFunctionDescriptor struct {
46
RoutineName string `db:"routine_name" json:"routine_name,omitempty"`
57
DataType string `db:"data_type" json:"type,omitempty"`
@@ -12,3 +14,9 @@ type ITableDescriptor struct {
1214
Type string `json:"type,omitempty" db:"type"`
1315
Descriptor string `json:"descriptor,omitempty" db:"descriptor"`
1416
}
17+
18+
type ITableInfo struct {
19+
Column string `json:"column" db:"column_name"`
20+
Type string `json:"type" db:"data_type"`
21+
MaxLength null.Int `json:"max_length" db:"character_maximum_length"`
22+
}

postgresconn/postgresconn_service.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type PostgresService interface {
2727
ExecuteBatch(statements []string) error
2828
ExecuteBatchWithTransaction(statements []string) error
2929
TableDescriptor(table string) ([]ITableDescriptor, error)
30+
TableInfo(table string) ([]ITableInfo, error)
3031
}
3132

3233
type postgresServiceImpl struct {
@@ -275,3 +276,33 @@ func (p *postgresServiceImpl) TableDescriptor(table string) ([]ITableDescriptor,
275276
}
276277
return results, nil
277278
}
279+
280+
func (p *postgresServiceImpl) TableInfo(table string) ([]ITableInfo, error) {
281+
s := `
282+
SELECT
283+
column_name,
284+
data_type,
285+
character_maximum_length
286+
FROM
287+
information_schema.columns
288+
WHERE
289+
table_name = $1;
290+
`
291+
rows, err := p.dbConn.Query(s, table)
292+
if err != nil {
293+
return nil, err
294+
}
295+
defer rows.Close()
296+
var results []ITableInfo
297+
for rows.Next() {
298+
var m ITableInfo
299+
if err := rows.Scan(&m.Column, &m.Type, &m.MaxLength); err != nil {
300+
return nil, err
301+
}
302+
results = append(results, m)
303+
}
304+
if err := rows.Err(); err != nil {
305+
return nil, err
306+
}
307+
return results, nil
308+
}

0 commit comments

Comments
 (0)