-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
348 additions
and
1 deletion.
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
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
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
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,111 @@ | ||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/tgallant/db2jsonschema/internal/schema" | ||
) | ||
|
||
type Driver struct { | ||
DataSource string | ||
} | ||
|
||
var ( | ||
typesMap = map[string]*schema.FieldType{ | ||
"bigint unsigned": {Name: "number", Format: ""}, | ||
"longtext": {Name: "string", Format: ""}, | ||
"datetime(3)": {Name: "string", Format: "date-time"}, | ||
} | ||
) | ||
|
||
func MapMySQLType(t string) (*schema.FieldType, error) { | ||
schemaType, exists := typesMap[t] | ||
if !exists { | ||
return &schema.FieldType{}, fmt.Errorf("Unknown data type: %s", t) | ||
} | ||
return schemaType, nil | ||
} | ||
|
||
func SelectTables(conn *sql.DB) ([]string, error) { | ||
row, err := conn.Query(`show tables`) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer row.Close() | ||
var tables []string | ||
for row.Next() { | ||
var table string | ||
err = row.Scan(&table) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tables = append(tables, table) | ||
} | ||
return tables, nil | ||
} | ||
|
||
func DescribeTable(conn *sql.DB, tableName string) (*schema.Table, error) { | ||
query := fmt.Sprintf("describe %s", tableName) | ||
row, err := conn.Query(query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer row.Close() | ||
var fields []*schema.Field | ||
for row.Next() { | ||
var name string | ||
var datatype string | ||
var nullable sql.NullString | ||
var key sql.NullString | ||
var defaultValue sql.NullString | ||
var extra sql.NullString | ||
err := row.Scan( | ||
&name, | ||
&datatype, | ||
&nullable, | ||
&key, | ||
&defaultValue, | ||
&extra, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fieldType, err := MapMySQLType(datatype) | ||
if err != nil { | ||
return nil, err | ||
} | ||
field := &schema.Field{ | ||
Name: name, | ||
Type: fieldType, | ||
} | ||
fields = append(fields, field) | ||
} | ||
table := &schema.Table{ | ||
Name: tableName, | ||
Fields: fields, | ||
} | ||
return table, nil | ||
} | ||
|
||
func (d *Driver) ReadTables() ([]*schema.Table, error) { | ||
conn, err := sql.Open("mysql", d.DataSource) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
tables, err := SelectTables(conn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var parsedTables []*schema.Table | ||
for _, table := range tables { | ||
parsedTable, err := DescribeTable(conn, table) | ||
if err != nil { | ||
return nil, err | ||
} | ||
parsedTables = append(parsedTables, parsedTable) | ||
} | ||
return parsedTables, nil | ||
} |
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
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
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,18 @@ | ||
#!/bin/sh | ||
|
||
TAG=${1-8.0} | ||
|
||
docker_exists() { | ||
type docker > /dev/null 2> /dev/null | ||
} | ||
|
||
if ! docker_exists; then | ||
echo "Docker is not installed. Please install it on your system to continue." | ||
exit 1 | ||
fi | ||
|
||
docker run \ | ||
-e MYSQL_ROOT_PASSWORD=root \ | ||
-e MYSQL_DATABASE=testing \ | ||
-p 3306:3306 \ | ||
"mysql:$TAG" |
Oops, something went wrong.