(data [][]string, headers []string, entityName string, crud *DbResource, req api2go.Request, transaction *sqlx.Tx)
| 1205 | } |
| 1206 | |
| 1207 | func ImportDataStringArray(data [][]string, headers []string, entityName string, crud *DbResource, req api2go.Request, transaction *sqlx.Tx) []error { |
| 1208 | errs := make([]error, 0) |
| 1209 | |
| 1210 | uniqueColumns := make([]api2go.ColumnInfo, 0) |
| 1211 | |
| 1212 | for _, col := range crud.TableInfo().Columns { |
| 1213 | |
| 1214 | if col.IsUnique { |
| 1215 | uniqueColumns = append(uniqueColumns, col) |
| 1216 | } |
| 1217 | |
| 1218 | } |
| 1219 | |
| 1220 | for _, rowArray := range data { |
| 1221 | |
| 1222 | rowMap := make(map[string]interface{}) |
| 1223 | |
| 1224 | for i, header := range headers { |
| 1225 | rowMap[header] = rowArray[i] |
| 1226 | } |
| 1227 | model := api2go.NewApi2GoModelWithData(entityName, nil, int64(crud.TableInfo().DefaultPermission), nil, rowMap) |
| 1228 | _, err := crud.CreateWithTransaction(model, req, transaction) |
| 1229 | if err != nil { |
| 1230 | errs = append(errs, err) |
| 1231 | } |
| 1232 | |
| 1233 | if err != nil { |
| 1234 | // create row failed, try to update row by unique columns |
| 1235 | |
| 1236 | if len(uniqueColumns) > 0 { |
| 1237 | for _, uniqueCol := range uniqueColumns { |
| 1238 | log.Printf("Try to update data by unique column: %v", uniqueCol.ColumnName) |
| 1239 | uniqueColumnValue, ok := rowMap[uniqueCol.ColumnName] |
| 1240 | if !ok || uniqueColumnValue == nil { |
| 1241 | continue |
| 1242 | } |
| 1243 | stringVal, isString := uniqueColumnValue.(string) |
| 1244 | if isString && len(stringVal) == 0 { |
| 1245 | continue |
| 1246 | } |
| 1247 | existingRow, err := crud.GetObjectByWhereClauseWithTransaction(entityName, uniqueCol.ColumnName, uniqueColumnValue, transaction) |
| 1248 | if err != nil { |
| 1249 | continue |
| 1250 | } |
| 1251 | |
| 1252 | for _, key := range headers { |
| 1253 | existingRow[key] = rowMap[key] |
| 1254 | } |
| 1255 | |
| 1256 | obj := api2go.NewApi2GoModelWithData(entityName, nil, 0, nil, existingRow) |
| 1257 | _, err = crud.UpdateWithTransaction(obj, req, transaction) |
| 1258 | if err != nil { |
| 1259 | log.Errorf("Failed to update table 873 [%v] update row by unique column [%v]: %v", entityName, uniqueCol.ColumnName, err) |
| 1260 | } |
| 1261 | break |
| 1262 | |
| 1263 | } |
| 1264 | } |
no test coverage detected