forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
3.0 KiB
80 lines
3.0 KiB
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
// _ "github.com/mattn/go-oci8"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
types, usr, pwd, url, database, fileName, tables := "mysql", "root", "admin", "127.0.0.1:35017", "hy_qggwy", "hy_qggwy", "'a01','a02','a05','a06','a08','a14','a15','a17','a30','a33','a36','a57','a65','a99z1','b01'"
|
|
types = strings.ToUpper(types)
|
|
if strings.Contains(types, "MYSQL") {
|
|
tables = writeMySQL(usr, pwd, url, database, tables)
|
|
} else if strings.Contains(types, "ORACLE") || strings.Contains(types, "ORCL") {
|
|
tables = writeOracle(usr, pwd, url, database, tables)
|
|
}
|
|
createFile(tables, fileName)
|
|
}
|
|
|
|
func writeOracle(usr, pwd, url, database, tables string) string {
|
|
// 待完成
|
|
var sql1 = "SELECT concat(upper(COLUMN_NAME), case when DATA_TYPE='decimal' then ' int `json:\"' when DATA_TYPE='timestamp' then ' time.Time `json:\"' else ' string `json:\"' end, lower(COLUMN_NAME), '\"`') tName FROM information_schema.COLUMNS where upper(TABLE_NAME)=? order by ORDINAL_POSITION;"
|
|
var sql0 = "SELECT upper(TABLE_NAME) tName FROM information_schema.COLUMNS where TABLE_NAME in (" + tables + ") group by TABLE_NAME order by TABLE_NAME;"
|
|
|
|
allStr := "package main\nimport (\n\"time\"\n)\n"
|
|
db, _ := sql.Open("oci8", fmt.Sprintf("%s/%s@%s/%s", usr, pwd, url, database))
|
|
row0, _ := db.Query(sql0)
|
|
defer row0.Close()
|
|
columns0, _ := row0.Columns()
|
|
for row0.Next() {
|
|
_ = row0.Scan(&columns0[0])
|
|
t := strings.ToUpper(columns0[0])
|
|
allStr += "type " + t + " struct {\n\t"
|
|
// 查询脚本属性
|
|
row1, _ := db.Query(sql1, t)
|
|
defer row1.Close()
|
|
columns1, _ := row1.Columns()
|
|
for row1.Next() {
|
|
_ = row1.Scan(&columns1[0])
|
|
allStr += columns1[0] + "\n\t"
|
|
}
|
|
allStr += "\n}\n"
|
|
}
|
|
return allStr
|
|
}
|
|
|
|
func writeMySQL(usr, pwd, url, database, tables string) string {
|
|
var sql1 = "SELECT concat(upper(COLUMN_NAME), case when DATA_TYPE='decimal' then ' int `json:\"' when DATA_TYPE='timestamp' then ' time.Time `json:\"' else ' string `json:\"' end, lower(COLUMN_NAME), '\"`') tName FROM information_schema.COLUMNS where upper(TABLE_NAME)=? order by ORDINAL_POSITION;"
|
|
var sql0 = "SELECT upper(TABLE_NAME) tName FROM information_schema.COLUMNS where TABLE_NAME in (" + tables + ") group by TABLE_NAME order by TABLE_NAME;"
|
|
|
|
allStr := "package main\nimport (\n\"time\"\n)\n"
|
|
db, _ := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", usr, pwd, url, database))
|
|
row0, _ := db.Query(sql0)
|
|
defer row0.Close()
|
|
columns0, _ := row0.Columns()
|
|
for row0.Next() {
|
|
_ = row0.Scan(&columns0[0])
|
|
t := strings.ToUpper(columns0[0])
|
|
allStr += "type " + t + " struct {\n\t"
|
|
// 查询脚本属性
|
|
row1, _ := db.Query(sql1, t)
|
|
defer row1.Close()
|
|
columns1, _ := row1.Columns()
|
|
for row1.Next() {
|
|
_ = row1.Scan(&columns1[0])
|
|
allStr += columns1[0] + "\n\t"
|
|
}
|
|
allStr += "\n}\n"
|
|
}
|
|
return allStr
|
|
}
|
|
|
|
func createFile(allStr, fileName string) {
|
|
file3, _ := os.Create(fileName + ".go")
|
|
file3.WriteString(allStr)
|
|
file3.Close()
|
|
}
|
|
|