Skip to content

Commit

Permalink
feat(client): add a postgres database querier that implements databas…
Browse files Browse the repository at this point in the history
…e querier interface
  • Loading branch information
danvergara committed Aug 6, 2023
1 parent ed6906b commit c485c56
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions pkg/client/postgres.go
@@ -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

View workflow job for this annotation

GitHub Actions / lint

a blank import should be only in a main or test package, or have a comment justifying it (golint)

Check failure on line 7 in pkg/client/postgres.go

View workflow job for this annotation

GitHub Actions / lint

a blank import should be only in a main or test package, or have a comment justifying it (golint)
_ "github.com/lib/pq"
_ "modernc.org/sqlite"
)

type postgres struct {

Check failure on line 12 in pkg/client/postgres.go

View workflow job for this annotation

GitHub Actions / lint

type `postgres` is unused (unused)

Check failure on line 12 in pkg/client/postgres.go

View workflow job for this annotation

GitHub Actions / lint

type `postgres` is unused (unused)
}

func (p *postgres) ShowTables(schema string) (string, []interface{}, error) {

Check failure on line 15 in pkg/client/postgres.go

View workflow job for this annotation

GitHub Actions / lint

func `(*postgres).ShowTables` is unused (unused)

Check failure on line 15 in pkg/client/postgres.go

View workflow job for this annotation

GitHub Actions / lint

func `(*postgres).ShowTables` is unused (unused)
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

View workflow job for this annotation

GitHub Actions / lint

func `(*postgres).TableStructure` is unused (unused)

Check failure on line 33 in pkg/client/postgres.go

View workflow job for this annotation

GitHub Actions / lint

func `(*postgres).TableStructure` is unused (unused)
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

View workflow job for this annotation

GitHub Actions / lint

func `(*postgres).Constraints` is unused (unused)

Check failure on line 68 in pkg/client/postgres.go

View workflow job for this annotation

GitHub Actions / lint

func `(*postgres).Constraints` is unused (unused)
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

View workflow job for this annotation

GitHub Actions / lint

func `(*postgres).Indexes` is unused (unused)

Check failure on line 91 in pkg/client/postgres.go

View workflow job for this annotation

GitHub Actions / lint

func `(*postgres).Indexes` is unused (unused)
return "SELECT * FROM pg_indexes WHERE tablename = $1;"
}

0 comments on commit c485c56

Please sign in to comment.