|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type ColInfo struct { |
| 12 | + TableName string `json:"table_name"` |
| 13 | + Colnames []string `json:"column_names"` |
| 14 | + Coltypes []string `json:"column_types"` |
| 15 | + Description []string `json:"description"` |
| 16 | +} |
| 17 | + |
| 18 | +func loadOneDbInfo(dbInfoDir string, sqliteDir string, dbName string) (*DbInfo, error) { |
| 19 | + dbDir := filepath.Join(dbInfoDir, dbName) |
| 20 | + files, err := os.ReadDir(dbDir) |
| 21 | + if err != nil { |
| 22 | + return nil, fmt.Errorf("failed to read dbDir: %s, %w", dbDir, err) |
| 23 | + } |
| 24 | + |
| 25 | + dbInfo := DbInfo{ |
| 26 | + Name: dbName, |
| 27 | + SqlLite: filepath.Join(sqliteDir, dbName+".sqlite"), |
| 28 | + } |
| 29 | + |
| 30 | + for _, file := range files { |
| 31 | + if strings.HasSuffix(file.Name(), ".json") { |
| 32 | + jsonFile := filepath.Join(dbDir, file.Name()) |
| 33 | + jsonData, err := os.ReadFile(jsonFile) |
| 34 | + if err != nil { |
| 35 | + return nil, fmt.Errorf("cannot read json file: %s, %w", jsonFile, err) |
| 36 | + } |
| 37 | + |
| 38 | + // replace NaN with null, NaN is not a valid json value |
| 39 | + jsonData = []byte(strings.ReplaceAll(string(jsonData), ": NaN", ": null")) |
| 40 | + |
| 41 | + var colInfo ColInfo |
| 42 | + if err := json.Unmarshal(jsonData, &colInfo); err != nil { |
| 43 | + return nil, fmt.Errorf("cannot unmarshal json file: %s, %w", jsonFile, err) |
| 44 | + } |
| 45 | + |
| 46 | + var tableInfo TableInfo |
| 47 | + tableInfo.Name = colInfo.TableName |
| 48 | + tableInfo.Sql = fmt.Sprintf("CREATE TABLE %s (", tableInfo.Name) |
| 49 | + |
| 50 | + for i, colname := range colInfo.Colnames { |
| 51 | + coltype := colInfo.Coltypes[i] |
| 52 | + switch colname { |
| 53 | + case "rank": |
| 54 | + colname = fmt.Sprintf("%s_rank", tableInfo.Name) |
| 55 | + } |
| 56 | + if coltype == "" { |
| 57 | + coltype = "text" |
| 58 | + } |
| 59 | + if coltype == "NUM" { |
| 60 | + coltype = "float" |
| 61 | + } |
| 62 | + |
| 63 | + tableInfo.Sql += fmt.Sprintf("%s %s", colname, coltype) |
| 64 | + if i == len(colInfo.Colnames)-1 { |
| 65 | + tableInfo.Sql += ");\n" |
| 66 | + } else { |
| 67 | + tableInfo.Sql += ",\n" |
| 68 | + } |
| 69 | + } |
| 70 | + dbInfo.TableInfos = append(dbInfo.TableInfos, tableInfo) |
| 71 | + } |
| 72 | + } |
| 73 | + return &dbInfo, nil |
| 74 | +} |
| 75 | + |
| 76 | +func Spider2LoadDbInfo() (map[string]*DbInfo, error) { |
| 77 | + dbInfos := make(map[string]*DbInfo) |
| 78 | + |
| 79 | + rootDir := ProjectRoot() |
| 80 | + dbInfoDir := filepath.Join(rootDir, "repo3/Spider2/spider2-lite/resource/databases/sqlite") |
| 81 | + sqliteDir := filepath.Join(rootDir, "repo3/data/spider2") |
| 82 | + files, err := os.ReadDir(dbInfoDir) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("failed to read dbInfoDir: %s, %w", dbInfoDir, err) |
| 85 | + } |
| 86 | + |
| 87 | + for _, file := range files { |
| 88 | + dbInfo, err := loadOneDbInfo(dbInfoDir, sqliteDir, file.Name()) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + dbInfos[dbInfo.Name] = dbInfo |
| 93 | + } |
| 94 | + |
| 95 | + return dbInfos, nil |
| 96 | +} |
0 commit comments