提交 8f528fd8 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

open sqlite3 db

上级 3c66d54b
/conf/
/def/
/log/
/.idea/
/bin/
......
github.com/360EntSecGroup-Skylar/excelize v1.4.1 h1:l55mJb6rkkaUzOpSsgEeKYtS6/0gHwBYyfo5Jcjv/Ks=
github.com/360EntSecGroup-Skylar/excelize/v2 v2.2.0 h1:5DuRTdH6M8yPjvFfBkACVmuk7SoTzmaB8yM6KVqEhP8=
github.com/360EntSecGroup-Skylar/excelize/v2 v2.2.0/go.mod h1:Uwb0d1GgxJieUWZG5WylTrgQ2SrldfjagAxheU8W6MQ=
github.com/awesome-gocui/gocui v0.6.0 h1:hhDJiQC12tEsJNJ+iZBBVaSSLFYo9llFuYpQlL5JZVI=
github.com/awesome-gocui/gocui v0.6.0/go.mod h1:1QikxFaPhe2frKeKvEwZEIGia3haiOxOUXKinrv17mA=
github.com/awesome-gocui/termbox-go v0.0.0-20190427202837-c0aef3d18bcc h1:wGNpKcHU8Aadr9yOzsT3GEsFLS7HQu8HxQIomnekqf0=
github.com/awesome-gocui/termbox-go v0.0.0-20190427202837-c0aef3d18bcc/go.mod h1:tOy3o5Nf1bA17mnK4W41gD7PS3u4Cv0P0pqFcoWMy8s=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
......@@ -10,6 +15,9 @@ github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
......@@ -18,23 +26,42 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnHyvtYjDeq0zlVHn9K/ZXoy17ylucdo=
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5HgEKCvEIIrSpFI3ozzG5xOKA2DVlEX/gGnewM=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/xuri/efp v0.0.0-20191019043341-b7dc4fe9aa91 h1:gp02YctZuIPTk0t7qI+wvg3VQwTPyNmSGG6ZqOsjSL8=
github.com/xuri/efp v0.0.0-20191019043341-b7dc4fe9aa91/go.mod h1:uBiSUepVYMhGTfDeBKKasV4GpgBlzJ46gXUBAqV8qLk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/image v0.0.0-20200430140353-33d19683fad8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f h1:QBjCr1Fz5kw158VqdE9JfI9cJnl/ymnJWAdMuinqL7Y=
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.55.0 h1:E8yzL5unfpW3M6fz/eB7Cb5MQAYSZ7GKo4Qth+N2sgQ=
gopkg.in/ini.v1 v1.55.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
package action
import (
"encoding/json"
"encoding/xml"
"fmt"
"github.com/easysoft/zendata/src/gen"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log"
stringUtils "github.com/easysoft/zendata/src/utils/string"
......@@ -11,29 +14,68 @@ import (
"strconv"
)
func Generate(file string, total int, fieldsToExport string, out string, table string) {
constant.ResDir = filepath.Dir(file) + string(os.PathSeparator)
func Generate(def string, total int, fieldsToExport string, out string, format string, table string) {
constant.ResDir = filepath.Dir(def) + string(os.PathSeparator)
gen.LoadDefinitionFromFile("conf/buildin.yaml")
gen.LoadDefinitionFromFile(file)
gen.LoadDefinitionFromFile("def/buildin.yaml")
gen.LoadDefinitionFromFile(def)
rows := gen.GenerateForDefinition(total, fieldsToExport, out, table)
Print(rows)
rows, colTypes := gen.GenerateForDefinition(total, fieldsToExport, out, table)
content := Print(rows, format, table, colTypes, fieldsToExport)
WriteToFile(out, content)
}
func Print(rows [][]string) {
func Print(rows [][]string, format string, table string, colTypes []bool, fields string) string {
width := stringUtils.GetNumbWidth(len(rows))
content := ""
sql := ""
testData := model.TestData{}
testData.Title = "测试数据"
for i, cols := range rows {
line := ""
row := model.Row{}
valueList := ""
for j, col := range cols {
if j >0 {
line = line + ", "
valueList = valueList + ", "
}
line = line + col
row.Cols = append(row.Cols, col)
colVal := col
if !colTypes[j] { colVal = "'" + colVal + "'" }
valueList = valueList + colVal
}
if format == "text" && i < len(rows) - 1 { content = content + line + "\n" }
idStr := fmt.Sprintf("%" + strconv.Itoa(width) + "d", i+1)
logUtils.Screen(fmt.Sprintf("%s: %s", idStr, line))
testData.Table.Rows = append(testData.Table.Rows, row)
if format == "sql" {
sent := fmt.Sprintf("INSERT INTO %s(%s) VALUES(%s)", table, fields, valueList)
sql = sql + sent + ";\n"
}
}
if format == "json" {
json, _ := json.Marshal(rows)
content = string(json)
} else if format == "xml" {
xml, _ := xml.Marshal(testData)
content = string(xml)
} else if format == "sql" {
content = sql
}
return content
}
\ No newline at end of file
package action
import (
"fmt"
fileUtils "github.com/easysoft/zendata/src/utils/file"
logUtils "github.com/easysoft/zendata/src/utils/log"
)
func WriteToFile(path string, content string) {
fileUtils.WriteFile(path, content)
logUtils.Screen(fmt.Sprintf("Test data saved to %s", path))
}
package action
import (
"database/sql"
"fmt"
constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log"
_ "github.com/mattn/go-sqlite3"
)
func Upgrade() {
db, err := sql.Open(constant.SqliteDriver, constant.SqliteSource)
if err != nil {
logUtils.Screen("fail to open " + constant.SqliteSource)
return
}
sql := "SELECT * FROM cn_city"
rows, err := db.Query(sql)
if err != nil {
logUtils.Screen("fail to exec query " + sql)
return
}
for rows.Next() {
var id int
var level int
var parentCode int
var areaCode int
var zipCode int
var cityCode int
var name string
err = rows.Scan(&id, &level, &parentCode, &areaCode, zipCode, cityCode, name)
if err != nil {
logUtils.Screen("fail to get sqlite3 row")
return
}
fmt.Println(id, name, level, parentCode, areaCode, zipCode, cityCode)
}
}
\ No newline at end of file
......@@ -24,7 +24,7 @@ func LoadDefinitionFromFile(file string) {
return
}
if strings.Index(file, "conf") != 0 && constant.Definition.Title == "" { // only add the fields in first level yaml file
if strings.Index(file, "def") != 0 && constant.Definition.Title == "" { // only add the fields in first level yaml file
constant.Definition = def
}
......
......@@ -9,12 +9,14 @@ import (
"strings"
)
func GenerateForDefinition(total int, fieldsToExport string, out string, table string) [][]string {
func GenerateForDefinition(total int, fieldsToExport string, out string, table string) ([][]string, []bool) {
def := constant.Definition
fieldsToExportArr := strings.Split(fieldsToExport, ",")
fieldNameToValues := map[string][]string{}
colTypes := make([]bool, 0)
// 为每个field生成值列表
for index, field := range def.Fields {
if !stringUtils.FindInArr(field.Name, fieldsToExportArr) {
......@@ -25,6 +27,7 @@ func GenerateForDefinition(total int, fieldsToExport string, out string, table s
def.Fields[index].Precision = field.Precision
fieldNameToValues[field.Name] = values
colTypes = append(colTypes, field.IsNumb)
}
// 生成指定数量行的数据
......@@ -42,7 +45,7 @@ func GenerateForDefinition(total int, fieldsToExport string, out string, table s
}
}
return rows
return rows, colTypes
}
func GenerateForField(field *model.Field, total int) []string {
......
......@@ -113,7 +113,7 @@ func GenerateFieldValuesFromText(field *model.Field, fieldValue *model.FieldValu
if err != nil {
logUtils.Screen("fail to read " + relaPath + ", try to use global config")
relaPath = "conf" + string(os.PathSeparator) + file
relaPath = "def" + string(os.PathSeparator) + file
content, err = ioutil.ReadFile(relaPath)
if err != nil {
logUtils.Screen("fail to read " + relaPath + ", will return")
......
......@@ -3,4 +3,5 @@ package model
type Config struct {
Version int
Language string
Server string
}
......@@ -19,6 +19,7 @@ type Field struct {
Loop int `yaml:"loop"`
Loopfix string `yaml:"loopfix"`
Format string `yaml:"format"`
IsNumb bool `yaml:"isNumb"`
Expect string `yaml:"expect"`
Fields []Field `yaml:"fields,flow"`
......
package model
import "encoding/xml"
type TestData struct {
XMLName xml.Name `xml:"testdata"`
Title string `json:"title" xml:"title"`
Table Table `json:"table" xml:"table"`
}
type Table struct {
Rows []Row `json:"row" xml:"row"`
}
type Row struct {
Cols []string `json:"col" xml:"col"`
}
\ No newline at end of file
......@@ -36,7 +36,7 @@ func InitScreenSize() {
}
func SaveConfig(conf model.Config) error {
fileUtils.MkDirIfNeeded(fileUtils.GetZTFDir() + "conf")
fileUtils.MkDirIfNeeded(fileUtils.GetZTFDir() + "def")
conf.Version = constant.ConfigVer
......@@ -105,7 +105,7 @@ func getInst() model.Config {
func CheckConfigPermission() {
//err := syscall.Access(vari.ZDataDir, syscall.O_RDWR)
err := fileUtils.MkDirIfNeeded(vari.ZDataDir + "conf")
err := fileUtils.MkDirIfNeeded(vari.ZDataDir + "def")
if err != nil {
logUtils.PrintToWithColor(
fmt.Sprintf("Permission denied to open %s for write. Please change work dir.", vari.ZDataDir), color.FgRed)
......
......@@ -8,7 +8,7 @@ import (
var (
ConfigVer = 1
ConfigFile = fmt.Sprintf("conf%szdata.conf", string(os.PathSeparator))
ConfigFile = fmt.Sprintf("def%szdata.conf", string(os.PathSeparator))
UrlZentaoSettings = "zentaoSettings"
UrlImportProject = "importProject"
......@@ -47,4 +47,7 @@ var (
ResDir string = ""
Definition model.Definition = model.Definition{}
LoadedFields map[string]model.Field = map[string]model.Field{}
SqliteDriver string = "sqlite3"
SqliteSource string = "file:def/data/data.sqlite3"
)
......@@ -14,12 +14,13 @@ import (
var (
language string
file string
count int
def string
count int
fields string
parse bool
parse bool
out string
format string
table string
help bool
......@@ -37,8 +38,8 @@ func main() {
flagSet = flag.NewFlagSet("zdata", flag.ContinueOnError)
flagSet.StringVar(&file, "f", "", "")
flagSet.StringVar(&file, "file", "", "")
flagSet.StringVar(&def, "d", "", "")
flagSet.StringVar(&def, "def", "", "")
flagSet.IntVar(&count, "c", 10, "")
flagSet.IntVar(&count, "count", 10, "")
......@@ -54,6 +55,9 @@ func main() {
flagSet.StringVar(&table, "t", "", "")
flagSet.StringVar(&table, "table", "", "")
flagSet.StringVar(&format, "f", "", "")
flagSet.StringVar(&format, "format", "", "")
flagSet.BoolVar(&help, "h", false, "")
flagSet.BoolVar(&help, "help", false, "")
......@@ -66,6 +70,8 @@ func main() {
gen(os.Args)
case "set", "-set":
action.Set()
case "upgrade":
upgrade(os.Args)
case "help", "-h":
logUtils.PrintUsage()
......@@ -81,9 +87,15 @@ func main() {
}
}
func upgrade(args []string) {
if err := flagSet.Parse(args[2:]); err == nil {
action.Upgrade()
}
}
func gen(args []string) {
if err := flagSet.Parse(args[2:]); err == nil {
action.Generate(file, count, fields, out, table)
action.Generate(def, count, fields, out, format, table)
}
}
......
......@@ -79,11 +79,17 @@ fields:
type: custom
range: custom.yaml
- name: field5
- name: id
note: 数字
type: list
range: 1-10000
isNumb: true
- name: field6
note: 复合字段的某一个字段,指定查询条件。
type: user.name
range: age>10
- name: field5
- name: field7
note: 复合字段取多个字段。
type: user
range: age>10
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册