Skip to content

Commit

Permalink
Display tables and columns
Browse files Browse the repository at this point in the history
  • Loading branch information
yuta17 committed Apr 25, 2020
1 parent fb955c7 commit 46a21e9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
76 changes: 76 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gobulk

import (
"database/sql"
"log"

_ "github.com/go-sql-driver/mysql"
)

type Client struct {
DbmsName string
InputUrl string
OutputUrl string
}

type Column struct {
Field string
Type string
Null string
Key string
Default *string
Extra string
}

var tableName string
var columnName string

func NewClient(DbmsName string, InputUrl string, OutputUrl string) *Client {
return &Client{DbmsName: DbmsName, InputUrl: InputUrl, OutputUrl: OutputUrl}
}

func (c *Client) Sync() error {
// input
inputDb, err := sql.Open(c.DbmsName, c.InputUrl)
if err != nil {
return err
}
err = inputDb.Ping()
if err != nil {
return err
}

// output
outputDb, err := sql.Open(c.DbmsName, c.OutputUrl)
if err != nil {
return err
}
err = outputDb.Ping()
if err != nil {
return err
}
defer inputDb.Close()
defer outputDb.Close()

tables, err := inputDb.Query("show tables")
if err != nil {
return err
}
for tables.Next() {
err := tables.Scan(&tableName)
if err != nil {
return err
}
log.Println("=== table:" + tableName + " ===")
columns, err := inputDb.Query("show columns from " + tableName)
for columns.Next() {
var column Column
err := columns.Scan(&column.Field, &column.Type, &column.Null, &column.Key, &column.Default, &column.Extra)
if err != nil {
return err
}
log.Println("column: " + column.Field)
}
}
return nil
}
16 changes: 16 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"github.com/prometheus/common/log"
"github.com/yuta17/gobulk"
)

func main() {
inputUrl := "root@tcp(127.0.0.1:3306)/campfire_development"
outputUrl := "root@tcp(127.0.0.1:3306)/campfire_development_copy"
client := gobulk.NewClient("mysql", inputUrl, outputUrl)
err := client.Sync()
if err != nil {
log.Errorln(err)
}
}

0 comments on commit 46a21e9

Please sign in to comment.