-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add a postgres database querier that implements databas…
…e querier interface
- Loading branch information
1 parent
ed6906b
commit c485c56
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
_ "github.com/go-sql-driver/mysql" | ||
Check failure on line 7 in pkg/client/postgres.go GitHub Actions / lint
|
||
_ "github.com/lib/pq" | ||
_ "modernc.org/sqlite" | ||
) | ||
|
||
type postgres struct { | ||
Check failure on line 12 in pkg/client/postgres.go GitHub Actions / lint
|
||
} | ||
|
||
func (p *postgres) ShowTables(schema string) (string, []interface{}, error) { | ||
Check failure on line 15 in pkg/client/postgres.go GitHub Actions / lint
|
||
var ( | ||
query string | ||
err error | ||
args []interface{} | ||
) | ||
psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar) | ||
query, args, err = psql.Select("table_name"). | ||
From("information_schema.tables"). | ||
Where(sq.Eq{"table_schema": schema}). | ||
OrderBy("table_name"). | ||
ToSql() | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
return query, args, nil | ||
} | ||
func (p *postgres) TableStructure(tableName, schema string) (string, []interface{}, error) { | ||
Check failure on line 33 in pkg/client/postgres.go GitHub Actions / lint
|
||
psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar) | ||
|
||
query, args, err := psql.Select( | ||
"c.column_name", | ||
"c.is_nullable", | ||
"c.data_type", | ||
"c.character_maximum_length", | ||
"c.numeric_precision", | ||
"c.numeric_scale", | ||
"c.ordinal_position", | ||
"tc.constraint_type AS pkey", | ||
). | ||
From("information_schema.columns AS c"). | ||
LeftJoin( | ||
`information_schema.constraint_column_usage AS ccu | ||
ON c.table_schema = ccu.table_schema | ||
AND c.table_name = ccu.table_name | ||
AND c.column_name = ccu.column_name`, | ||
). | ||
LeftJoin( | ||
`information_schema.table_constraints AS tc | ||
ON ccu.constraint_schema = tc.constraint_schema | ||
AND ccu.constraint_name = tc.constraint_name`, | ||
). | ||
Where( | ||
sq.And{ | ||
sq.Eq{"c.table_schema": schema}, | ||
sq.Eq{"c.table_name": tableName}, | ||
}, | ||
). | ||
ToSql() | ||
return query, args, err | ||
} | ||
|
||
func (p *postgres) Constraints(tableName, schema string) (string, []interface{}, error) { | ||
Check failure on line 68 in pkg/client/postgres.go GitHub Actions / lint
|
||
var ( | ||
query sq.SelectBuilder | ||
sql string | ||
) | ||
|
||
query = sq.Select( | ||
`tc.constraint_name`, | ||
`tc.table_name`, | ||
`tc.constraint_type`, | ||
). | ||
From("information_schema.table_constraints AS tc"). | ||
Where("tc.table_name = ?") | ||
query = query.Where(fmt.Sprintf("tc.table_schema = '%s'", schema)) | ||
query = query.PlaceholderFormat(sq.Dollar) | ||
|
||
sql, _, err := query.ToSql() | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
return sql, nil, err | ||
} | ||
|
||
func (p *postgres) Indexes(tableName string) string { | ||
Check failure on line 91 in pkg/client/postgres.go GitHub Actions / lint
|
||
return "SELECT * FROM pg_indexes WHERE tablename = $1;" | ||
} |