Skip to content

Commit

Permalink
added Scan method in adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
nayanvijay committed Jun 17, 2022
1 parent 4702db2 commit 12f459d
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 6 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/datasage-io/datasage

go 1.18

require github.com/go-sql-driver/mysql v1.6.0
require (
github.com/go-sql-driver/mysql v1.6.0
github.com/lib/pq v1.10.6
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
98 changes: 96 additions & 2 deletions src/adaptors/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package adaptors
import (
"database/sql"
"fmt"
"log"

_ "github.com/go-sql-driver/mysql"
)
Expand All @@ -13,9 +14,9 @@ type Mysql struct {
Connc *sql.DB
}

func NewMysqlClient(username, password, address, dbname string) (Mysql, error) {
func NewMysqlClient(username, password, host string) (Mysql, error) {
//Create data source name with given information
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s", username, password, address, dbname)
dsn := fmt.Sprintf("%s:%s@tcp(%s)/", username, password, host)

//open connection with database
connc, err := sql.Open("mysql", dsn)
Expand All @@ -27,3 +28,96 @@ func NewMysqlClient(username, password, address, dbname string) (Mysql, error) {
func (my *Mysql) Close() {
my.Connc.Close()
}

func (my Mysql) Scan() (DbMetaInfo, error) {
var dbMetaInfo DbMetaInfo

schemas, err := my.GetSchemaDetails()
if err != nil {
return DbMetaInfo{}, err
}

schemaData := []Schema{}
for _, sc := range schemas {
tables, err := my.GetTableNamesFromDB(sc)
if err != nil {
log.Println(err.Error())
}
tbData := []Table{}
for _, tb := range tables {
tbcol, err := my.GetTableDetails(sc, tb)
if err != nil {
log.Println(err.Error())
}
tbData = append(tbData, tbcol)
}
schemaData = append(schemaData, Schema{Name: sc, Tables: tbData})
}

dbMetaInfo.Schemas = schemaData
return dbMetaInfo, nil
}

func (mysql Mysql) GetSchemaDetails() ([]string, error) {

var schemaNames []string
rows, Qerr := mysql.Connc.Query("select SCHEMA_NAME from information_schema.schemata order by schema_name")
if Qerr != nil {
return schemaNames, Qerr
}
for rows.Next() {
var tmp string
if err := rows.Scan(&tmp); err != nil {
Qerr = err
} else {
schemaNames = append(schemaNames, tmp)
}
}
return schemaNames, Qerr
}

func (mysql Mysql) GetTableNamesFromDB(dbname string) ([]string, error) {

var tableNames []string
rows, Qerr := mysql.Connc.Query(`SELECT TABLE_NAME from information_schema.tables
WHERE table_type = 'BASE TABLE' and table_schema = ?`, dbname)
if Qerr != nil {
return tableNames, Qerr
}
for rows.Next() {
var tmp string
if err := rows.Scan(&tmp); err != nil {
Qerr = err
} else {
tableNames = append(tableNames, tmp)
}
}
return tableNames, Qerr
}

func (mysql Mysql) GetTableDetails(dbname string, tableName string) (Table, error) {
var tableInfo Table

tableInfo.Name = tableName

rows, Qerr := mysql.Connc.Query(`SELECT COLUMN_NAME as column_name,
COLUMN_TYPE as column_type,
COLUMN_COMMENT as column_comment
from information_schema.columns
where table_schema = ? and table_name = ?`, dbname, tableName)
if Qerr != nil {
return tableInfo, Qerr
}

for rows.Next() {
tempCol := Column{}
if err := rows.Scan(
&tempCol.ColumnName, &tempCol.ColumnType,
&tempCol.ColumnComment); err != nil {
Qerr = err
} else {
tableInfo.Cols = append(tableInfo.Cols, tempCol)
}
}
return tableInfo, Qerr
}
127 changes: 127 additions & 0 deletions src/adaptors/postgres.go
Original file line number Diff line number Diff line change
@@ -1 +1,128 @@
package adaptors

import (
"database/sql"
"fmt"
"log"

_ "github.com/lib/pq"
)

type Postgres struct {
ConecStr string
Connc *sql.DB
}

//BUG: solve Postgres client connection string error
func NewPostgresClient(username, password, host string) (Postgres, error) {

dsn := fmt.Sprintf("host=%s port=5432 dbname='postgres' user=%s password=%s sslmode=disable",
host, username, password)

connc, err := sql.Open("postgres", dsn)

return Postgres{ConecStr: dsn, Connc: connc}, err
}

func (pg Postgres) Close() error {
return pg.Connc.Close()
}
func (pg Postgres) Scan() (DbMetaInfo, error) {
var dbMetaInfo DbMetaInfo

schemas, err := pg.GetSchemaDetails()
if err != nil {
return DbMetaInfo{}, err
}

schemaData := []Schema{}
for _, sc := range schemas {
tables, err := pg.GetTableNamesFromDB(sc)
if err != nil {
log.Println(err.Error())
}
tbData := []Table{}
for _, tb := range tables {
tbcol, err := pg.GetTableDetails(sc, tb)
if err != nil {
log.Println(err.Error())
}
tbData = append(tbData, tbcol)
}
schemaData = append(schemaData, Schema{Name: sc, Tables: tbData})
}

dbMetaInfo.Schemas = schemaData
return dbMetaInfo, nil

}
func (pg Postgres) GetSchemaDetails() ([]string, error) {

var schemaNames []string
rows, Qerr := pg.Connc.Query("SELECT datname FROM pg_database")
if Qerr != nil {
return schemaNames, Qerr
}
for rows.Next() {
var tmp string
if err := rows.Scan(&tmp); err != nil {
Qerr = err
} else {
schemaNames = append(schemaNames, tmp)
}
}
return schemaNames, Qerr
}

func (pg Postgres) GetTableNamesFromDB(dbname string) ([]string, error) {

var tableNames []string
rows, Qerr := pg.Connc.Query(`SELECT table_name FROM information_schema.tables
WHERE table_schema='public' AND table_type = 'BASE TABLE' AND table_catalog=$1`, dbname)
if Qerr != nil {
return tableNames, Qerr
}
for rows.Next() {
var tmp string
if err := rows.Scan(&tmp); err != nil {
Qerr = err
} else {
tableNames = append(tableNames, tmp)
}
}
return tableNames, Qerr
}

func (pg Postgres) GetTableDetails(dbname string, tableName string) (Table, error) {
var tableInfo Table

tableInfo.Name = tableName

q := fmt.Sprintf(`
SELECT column_name,
data_type as column_type,
col_description('public.%s'::regclass, ordinal_position)
from information_schema.columns where table_name=$1
`, tableName)

rows, Qerr := pg.Connc.Query(q, tableName)
if Qerr != nil {
return tableInfo, Qerr
}

for rows.Next() {
tempCol := Column{}
tempComment := sql.NullString{}
if err := rows.Scan(
&tempCol.ColumnName, &tempCol.ColumnType,
&tempComment); err != nil {
Qerr = err
} else {
if tempComment.Valid {
tempCol.ColumnComment = tempComment.String
}
tableInfo.Cols = append(tableInfo.Cols, tempCol)
}
}
return tableInfo, Qerr
}
3 changes: 0 additions & 3 deletions src/application/main.go

This file was deleted.

36 changes: 36 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"log"

"github.com/datasage-io/datasage/src/adaptors"
)

func main() {
adpt, err := adaptors.New(adaptors.AdaptorConfig{
Type: "mysql",
Username: "appuser",
Password: "appuserpassword",
Host: "localhost:3306"})

if err != nil {
log.Fatal(err.Error())
}
info, err := adpt.Scan()

if err != nil {
log.Fatal(err.Error())
}

for _, sc := range info.Schemas {
log.Println("DB name:= ", sc.Name)

for _, tb := range sc.Tables {
log.Println("Table Name:= ", tb.Name)

for _, cols := range tb.Cols {
log.Println("columns:= ", cols)
}
}
}
}

0 comments on commit 12f459d

Please sign in to comment.