From 523f25edde4d0469183cfb676eca8f9460b1ecb9 Mon Sep 17 00:00:00 2001 From: ycc Date: Sat, 30 Oct 2021 22:21:54 +0200 Subject: [PATCH] Column comments added --- go.mod | 2 +- go.sum | 2 ++ pg.go | 36 +++++++++++++++++++++++++++++++----- pg_test.go | 9 +-------- test_table.json | 6 +++--- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 03dae42..81e34ce 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index a4a764e..9d1653a 100644 --- a/go.sum +++ b/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= diff --git a/pg.go b/pg.go index 8070969..fefbe93 100755 --- a/pg.go +++ b/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 != "" && 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 } diff --git a/pg_test.go b/pg_test.go index 32475c4..db84649 100755 --- a/pg_test.go +++ b/pg_test.go @@ -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") - } } diff --git a/test_table.json b/test_table.json index 507166b..b37dd18 100644 --- a/test_table.json +++ b/test_table.json @@ -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",