|
8 | 8 | "strings" |
9 | 9 | ) |
10 | 10 |
|
11 | | -type ColInfo struct { |
| 11 | +type ColInfoParsed struct { |
12 | 12 | TableName string `json:"table_name"` |
13 | 13 | Colnames []string `json:"column_names"` |
14 | 14 | Coltypes []string `json:"column_types"` |
@@ -38,34 +38,71 @@ func loadOneDbInfo(dbInfoDir string, sqliteDir string, dbName string) (*DbInfo, |
38 | 38 | // replace NaN with null, NaN is not a valid json value |
39 | 39 | jsonData = []byte(strings.ReplaceAll(string(jsonData), ": NaN", ": null")) |
40 | 40 |
|
41 | | - var colInfo ColInfo |
42 | | - if err := json.Unmarshal(jsonData, &colInfo); err != nil { |
| 41 | + var colParse ColInfoParsed |
| 42 | + if err := json.Unmarshal(jsonData, &colParse); err != nil { |
43 | 43 | return nil, fmt.Errorf("cannot unmarshal json file: %s, %w", jsonFile, err) |
44 | 44 | } |
45 | 45 |
|
46 | 46 | var tableInfo TableInfo |
47 | | - tableInfo.Name = colInfo.TableName |
48 | | - tableInfo.Sql = fmt.Sprintf("CREATE TABLE %s (", tableInfo.Name) |
| 47 | + tableInfo.Name = colParse.TableName |
| 48 | + switch strings.ToLower(tableInfo.Name) { |
| 49 | + case "match": |
| 50 | + tableInfo.Name = "match_table" |
| 51 | + } |
49 | 52 |
|
50 | | - for i, colname := range colInfo.Colnames { |
51 | | - coltype := colInfo.Coltypes[i] |
| 53 | + tableInfo.Sql = fmt.Sprintf("CREATE TABLE %s (", tableInfo.Name) |
| 54 | + for i, colname := range colParse.Colnames { |
| 55 | + coltype := colParse.Coltypes[i] |
52 | 56 | switch colname { |
53 | | - case "rank": |
54 | | - colname = fmt.Sprintf("%s_rank", tableInfo.Name) |
| 57 | + case "rank", "index", "table", "column", "group", "range", |
| 58 | + "cross", "change": |
| 59 | + colname = fmt.Sprintf("%s_%s", tableInfo.Name, colname) |
55 | 60 | } |
56 | | - if coltype == "" { |
57 | | - coltype = "text" |
| 61 | + |
| 62 | + if strings.HasPrefix(colname, "Unnamed: ") { |
| 63 | + colname = fmt.Sprintf("unnamed_%s", colname[len("Unnamed: "):]) |
| 64 | + } |
| 65 | + |
| 66 | + colname = strings.ReplaceAll(colname, "%", "percent") |
| 67 | + colname = strings.ReplaceAll(colname, "/", "_over_") |
| 68 | + colname = strings.ReplaceAll(colname, "(", "_") |
| 69 | + colname = strings.ReplaceAll(colname, ")", "_") |
| 70 | + colname = strings.ReplaceAll(colname, "-", "_") |
| 71 | + |
| 72 | + if strings.HasSuffix(colname, "%") { |
| 73 | + colname = fmt.Sprintf("%s_percent", colname[:len(colname)-1]) |
58 | 74 | } |
59 | | - if coltype == "NUM" { |
| 75 | + |
| 76 | + switch coltype { |
| 77 | + case "NUM": |
60 | 78 | coltype = "float" |
| 79 | + case "", "BLOB SUB_TYPE TEXT", "point": |
| 80 | + coltype = "text" |
| 81 | + case "jsonb": |
| 82 | + coltype = "json" |
| 83 | + case "timestamp with time zone": |
| 84 | + coltype = "timestamp" |
| 85 | + } |
| 86 | + |
| 87 | + if strings.HasPrefix(strings.ToLower(coltype), "nvarchar") { |
| 88 | + coltype = "text" |
| 89 | + } |
| 90 | + |
| 91 | + if strings.HasPrefix(strings.ToLower(coltype), "character") { |
| 92 | + coltype = "varchar(255)" |
61 | 93 | } |
62 | 94 |
|
63 | 95 | tableInfo.Sql += fmt.Sprintf("%s %s", colname, coltype) |
64 | | - if i == len(colInfo.Colnames)-1 { |
| 96 | + if i == len(colParse.Colnames)-1 { |
65 | 97 | tableInfo.Sql += ");\n" |
66 | 98 | } else { |
67 | 99 | tableInfo.Sql += ",\n" |
68 | 100 | } |
| 101 | + tableInfo.ColInfos = append(tableInfo.ColInfos, ColInfo{ |
| 102 | + Name: colname, |
| 103 | + Type: coltype, |
| 104 | + Description: colParse.Description[i], |
| 105 | + }) |
69 | 106 | } |
70 | 107 | dbInfo.TableInfos = append(dbInfo.TableInfos, tableInfo) |
71 | 108 | } |
@@ -94,3 +131,24 @@ func Spider2LoadDbInfo() (map[string]*DbInfo, error) { |
94 | 131 |
|
95 | 132 | return dbInfos, nil |
96 | 133 | } |
| 134 | + |
| 135 | +func Spider2CreateMoTables(dbInfo *DbInfo) error { |
| 136 | + dbName := dbInfo.Name |
| 137 | + mo, err := OpenMoDB() |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("failed to open mo db: %s, %w", dbName, err) |
| 140 | + } |
| 141 | + defer mo.Close() |
| 142 | + |
| 143 | + MustExec(mo, "DROP DATABASE IF EXISTS "+dbName) |
| 144 | + MustExec(mo, "CREATE DATABASE "+dbName) |
| 145 | + MustExec(mo, "USE "+dbName) |
| 146 | + |
| 147 | + for _, tableInfo := range dbInfo.TableInfos { |
| 148 | + _, err = mo.Exec(tableInfo.Sql) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("failed to create db %s, table %s: %w", dbName, tableInfo.Name, err) |
| 151 | + } |
| 152 | + } |
| 153 | + return nil |
| 154 | +} |
0 commit comments