Column comments added
This commit is contained in:
parent
712a5f9ca5
commit
523f25edde
2
go.mod
2
go.mod
@ -2,4 +2,4 @@ module forge.redroom.link/yves/sqldb
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/lib/pq v1.9.0
|
||||
require github.com/lib/pq v1.10.3
|
||||
|
2
go.sum
2
go.sum
@ -1,2 +1,4 @@
|
||||
github.com/lib/pq v1.9.0 h1:L8nSXQQzAYByakOFMTwpjRoHsMJklur4Gi59b6VivR8=
|
||||
github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg=
|
||||
github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
|
36
pg.go
36
pg.go
@ -108,14 +108,14 @@ func (t *TableInfo) GetSchema() (*TableInfo, error) {
|
||||
var ti TableInfo
|
||||
ti.Name = t.Name
|
||||
ti.db = t.db
|
||||
cols, err := t.db.QueryAssociativeArray("SELECT column_name :: varchar as name, REPLACE(REPLACE(data_type,'character varying','varchar'),'character','char') || COALESCE('(' || character_maximum_length || ')', '') as type from INFORMATION_SCHEMA.COLUMNS where table_name ='" + t.Name + "';")
|
||||
cols, err := t.db.QueryAssociativeArray("SELECT column_name :: varchar as name, REPLACE(REPLACE(data_type,'character varying','varchar'),'character','char') || COALESCE('(' || character_maximum_length || ')', '') as type, col_description('public." + t.Name + "'::regclass, ordinal_position) as comment from INFORMATION_SCHEMA.COLUMNS where table_name ='" + t.Name + "';")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
ti.Columns = make(map[string]string)
|
||||
for _, row := range cols {
|
||||
var name, rowtype string
|
||||
var name, rowtype, comment string
|
||||
for key, element := range row {
|
||||
if key == "name" {
|
||||
name = fmt.Sprintf("%v", element)
|
||||
@ -123,8 +123,14 @@ func (t *TableInfo) GetSchema() (*TableInfo, error) {
|
||||
if key == "type" {
|
||||
rowtype = fmt.Sprintf("%v", element)
|
||||
}
|
||||
if key == "comment" {
|
||||
comment = fmt.Sprintf("%v", element)
|
||||
}
|
||||
}
|
||||
ti.Columns[name] = rowtype
|
||||
if comment != "<nil>" && strings.TrimSpace(comment) != "" {
|
||||
ti.Columns[name] = ti.Columns[name] + "|" + comment
|
||||
}
|
||||
}
|
||||
return &ti, nil
|
||||
}
|
||||
@ -141,8 +147,8 @@ func (db *Db) CreateTable(t TableInfo) error {
|
||||
if fmt.Sprintf("%v", name) == "id" {
|
||||
columns += fmt.Sprintf("%v", name) + " " + "SERIAL PRIMARY KEY,"
|
||||
} else {
|
||||
|
||||
columns += fmt.Sprintf("%v", name) + " " + fmt.Sprintf("%v", rowtype)
|
||||
desc := strings.Split(fmt.Sprintf("%v", rowtype), "|")
|
||||
columns += fmt.Sprintf("%v", name) + " " + desc[0]
|
||||
columns += ","
|
||||
}
|
||||
}
|
||||
@ -153,6 +159,18 @@ func (db *Db) CreateTable(t TableInfo) error {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
for name, rowtype := range t.Columns {
|
||||
desc := strings.Split(fmt.Sprintf("%v", rowtype), "|")
|
||||
if len(desc) > 1 {
|
||||
query = "COMMENT ON COLUMN " + t.Name + "." + fmt.Sprintf("%v", name) + " IS '" + desc[1] + "'"
|
||||
_, err := t.db.conn.Query(query)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -172,13 +190,21 @@ func (t *TableInfo) DeleteTable() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TableInfo) AddColumn(name string, sqltype string) error {
|
||||
func (t *TableInfo) AddColumn(name string, sqltype string, comment string) error {
|
||||
query := "alter table " + t.Name + " add " + name + " " + sqltype
|
||||
rows, err := t.db.conn.Query(query)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(comment) != "" {
|
||||
query = "COMMENT ON COLUMN " + t.Name + "." + name + " IS '" + comment + "'"
|
||||
_, err = t.db.conn.Query(query)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
defer rows.Close()
|
||||
return nil
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ func TestAddColumn(t *testing.T) {
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
db.Table("test").AddColumn("addcolumn", "integer")
|
||||
db.Table("test").AddColumn("addcolumn", "integer", "comment")
|
||||
new, err := db.Table("test").GetSchema()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
@ -174,11 +174,4 @@ func TestDeleteTable(t *testing.T) {
|
||||
|
||||
db.Table("test").DeleteTable()
|
||||
|
||||
tbl, err := db.Table("test").GetSchema()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
if len(tbl.Columns) != 0 {
|
||||
t.Errorf("Delete table failed")
|
||||
}
|
||||
}
|
||||
|
@ -3,12 +3,12 @@
|
||||
"columns":
|
||||
{
|
||||
"id":"integer",
|
||||
"name":"varchar(255)",
|
||||
"name":"varchar(255)|comment",
|
||||
"description":"varchar(1000)",
|
||||
"startdate":"timestamp",
|
||||
"enddate":"timestamp",
|
||||
"latitude":"float",
|
||||
"longitude":"float",
|
||||
"latitude":"float|map",
|
||||
"longitude":"float|map",
|
||||
"intvalue":"integer",
|
||||
"floatvalue":"float",
|
||||
"price":"money",
|
||||
|
Loading…
Reference in New Issue
Block a user