github.com/team-ide/go-dialect@v1.9.20/worker/table.go (about) 1 package worker 2 3 import ( 4 "database/sql" 5 "errors" 6 "github.com/team-ide/go-dialect/dialect" 7 ) 8 9 func TablesSelect(db *sql.DB, dia dialect.Dialect, param *dialect.ParamModel, ownerName string) (list []*dialect.TableModel, err error) { 10 sqlInfo, err := dia.TablesSelectSql(param, ownerName) 11 if err != nil { 12 return 13 } 14 if sqlInfo == "" { 15 return 16 } 17 dataList, err := DoQuery(db, sqlInfo, nil) 18 if err != nil { 19 err = errors.New("TablesSelect error sql:" + sqlInfo + ",error:" + err.Error()) 20 return 21 } 22 for _, data := range dataList { 23 model, e := dia.TableModel(data) 24 if e != nil { 25 model = &dialect.TableModel{ 26 Error: e.Error(), 27 } 28 } 29 30 list = append(list, model) 31 } 32 return 33 } 34 35 func TableSelect(db *sql.DB, dia dialect.Dialect, param *dialect.ParamModel, ownerName string, tableName string, ignoreError bool) (one *dialect.TableModel, err error) { 36 sqlInfo, err := dia.TableSelectSql(param, ownerName, tableName) 37 if err != nil { 38 return 39 } 40 if sqlInfo == "" { 41 return 42 } 43 dataList, err := DoQuery(db, sqlInfo, nil) 44 if err != nil { 45 err = errors.New("TableSelect error sql:" + sqlInfo + ",error:" + err.Error()) 46 return 47 } 48 for _, data := range dataList { 49 model, e := dia.TableModel(data) 50 if e != nil { 51 if !ignoreError { 52 err = e 53 return 54 } 55 model = &dialect.TableModel{ 56 Error: e.Error(), 57 } 58 } 59 one = model 60 return 61 } 62 return 63 } 64 65 func TablesDetail(db *sql.DB, dia dialect.Dialect, param *dialect.ParamModel, ownerName string, ignoreError bool) (list []*dialect.TableModel, err error) { 66 sqlInfo, err := dia.TablesSelectSql(param, ownerName) 67 if err != nil { 68 return 69 } 70 if sqlInfo == "" { 71 return 72 } 73 dataList, err := DoQuery(db, sqlInfo, nil) 74 if err != nil { 75 err = errors.New("TablesSelect error sql:" + sqlInfo + ",error:" + err.Error()) 76 return 77 } 78 for _, data := range dataList { 79 model, e := dia.TableModel(data) 80 if e != nil { 81 if !ignoreError { 82 err = e 83 return 84 } 85 model = &dialect.TableModel{ 86 Error: e.Error(), 87 } 88 } 89 90 err = appendTableDetail(db, dia, param, ownerName, model, ignoreError) 91 if err != nil { 92 return 93 } 94 list = append(list, model) 95 } 96 return 97 } 98 func TableDetail(db *sql.DB, dia dialect.Dialect, param *dialect.ParamModel, ownerName string, tableName string, ignoreError bool) (table *dialect.TableModel, err error) { 99 sqlInfo, err := dia.TableSelectSql(param, ownerName, tableName) 100 if err != nil { 101 return 102 } 103 if sqlInfo == "" { 104 return 105 } 106 dataList, err := DoQuery(db, sqlInfo, nil) 107 if err != nil { 108 err = errors.New("TableDetail error sql:" + sqlInfo + ",error:" + err.Error()) 109 return 110 } 111 if len(dataList) > 0 { 112 model, e := dia.TableModel(dataList[0]) 113 if e != nil { 114 if !ignoreError { 115 err = e 116 return 117 } 118 model = &dialect.TableModel{ 119 Error: e.Error(), 120 } 121 } 122 table = model 123 err = appendTableDetail(db, dia, param, ownerName, table, ignoreError) 124 } 125 return 126 } 127 128 func appendTableDetail(db *sql.DB, dia dialect.Dialect, param *dialect.ParamModel, ownerName string, table *dialect.TableModel, ignoreError bool) (err error) { 129 130 var e error 131 columnList, e := ColumnsSelect(db, dia, param, ownerName, table.TableName, ignoreError) 132 if e != nil { 133 if !ignoreError { 134 err = e 135 return 136 } 137 table.Error += e.Error() 138 } else { 139 for _, one := range columnList { 140 if table.FindColumnByName(one.ColumnName) == nil { 141 table.AddColumn(one) 142 } 143 } 144 145 ps, e := PrimaryKeysSelect(db, dia, param, ownerName, table.TableName, ignoreError) 146 if e != nil { 147 if !ignoreError { 148 err = e 149 return 150 } 151 table.Error += e.Error() 152 } else { 153 table.AddPrimaryKey(ps...) 154 is, e := IndexesSelect(db, dia, param, ownerName, table.TableName, ignoreError) 155 if e != nil { 156 if !ignoreError { 157 err = e 158 return 159 } 160 table.Error += e.Error() 161 } else { 162 table.AddIndex(is...) 163 } 164 } 165 } 166 167 return 168 } 169 170 func TableCreate(db *sql.DB, dia dialect.Dialect, param *dialect.ParamModel, ownerName string, tableDetail *dialect.TableModel) (err error) { 171 sqlList, err := dia.TableCreateSql(param, ownerName, tableDetail) 172 if err != nil { 173 return 174 } 175 if len(sqlList) == 0 { 176 return 177 } 178 _, errorSql, _, err := DoOwnerExecs(dia, db, ownerName, sqlList, nil) 179 if err != nil { 180 err = errors.New("TableCreate error sql:" + errorSql + ",error:" + err.Error()) 181 return 182 } 183 return 184 } 185 186 func TableUpdate(db *sql.DB, oldDia dialect.Dialect, oldTableDetail *dialect.TableModel, newDia dialect.Dialect, newTableDetail *dialect.TableModel) (err error) { 187 188 return 189 } 190 191 func TableDelete(db *sql.DB, dia dialect.Dialect, param *dialect.ParamModel, ownerName string, tableName string) (err error) { 192 sqlList, err := dia.TableDeleteSql(param, ownerName, tableName) 193 if err != nil { 194 return 195 } 196 if len(sqlList) == 0 { 197 return 198 } 199 _, errorSql, _, err := DoOwnerExecs(dia, db, ownerName, sqlList, nil) 200 if err != nil { 201 err = errors.New("TableDelete error sql:" + errorSql + ",error:" + err.Error()) 202 return 203 } 204 return 205 } 206 207 // TableCover 表 覆盖,如果 表 已经存在,则删除后 再创建 208 func TableCover(db *sql.DB, dia dialect.Dialect, param *dialect.ParamModel, ownerName string, table *dialect.TableModel) (err error) { 209 find, err := TableSelect(db, dia, param, ownerName, table.TableName, true) 210 if err != nil { 211 return 212 } 213 if find != nil { 214 err = TableDelete(db, dia, param, ownerName, table.TableName) 215 if err != nil { 216 return 217 } 218 } 219 err = TableCreate(db, dia, param, ownerName, table) 220 if err != nil { 221 return 222 } 223 return 224 }