(data []map[string]interface{}, crud *DbResource, req api2go.Request, transaction *sqlx.Tx)
| 1144 | } |
| 1145 | |
| 1146 | func ImportDataMapArray(data []map[string]interface{}, crud *DbResource, req api2go.Request, transaction *sqlx.Tx) []error { |
| 1147 | errs := make([]error, 0) |
| 1148 | |
| 1149 | uniqueColumns := make([]api2go.ColumnInfo, 0) |
| 1150 | |
| 1151 | for _, col := range crud.TableInfo().Columns { |
| 1152 | |
| 1153 | if col.IsUnique { |
| 1154 | uniqueColumns = append(uniqueColumns, col) |
| 1155 | } |
| 1156 | |
| 1157 | } |
| 1158 | |
| 1159 | log.Printf("Process [%d] row import for table %v", len(data), crud.tableInfo.TableName) |
| 1160 | for _, row := range data { |
| 1161 | |
| 1162 | model := api2go.NewApi2GoModelWithData(crud.tableInfo.TableName, nil, int64(crud.TableInfo().DefaultPermission), nil, row) |
| 1163 | _, err := crud.CreateWithTransaction(model, req, transaction) |
| 1164 | if err != nil { |
| 1165 | log.Printf(" [%v] Error while importing insert data row: %v == %v", crud.tableInfo.TableName, err, row) |
| 1166 | errs = append(errs, err) |
| 1167 | |
| 1168 | if len(uniqueColumns) > 0 { |
| 1169 | for _, uniqueCol := range uniqueColumns { |
| 1170 | log.Infof("[901] Try to update data by unique column: [%v] => [%v]", uniqueCol.ColumnName, row[uniqueCol.ColumnName]) |
| 1171 | uniqueColumnValue, ok := row[uniqueCol.ColumnName] |
| 1172 | if !ok || uniqueColumnValue == nil { |
| 1173 | continue |
| 1174 | } |
| 1175 | stringVal, isString := uniqueColumnValue.(string) |
| 1176 | if isString && len(stringVal) == 0 { |
| 1177 | continue |
| 1178 | } |
| 1179 | existingRow, err := crud.GetObjectByWhereClause(crud.tableInfo.TableName, uniqueCol.ColumnName, uniqueColumnValue, transaction) |
| 1180 | if err != nil { |
| 1181 | continue |
| 1182 | } |
| 1183 | log.Printf("Existing [%v] found by unique column: %v = %v", crud.tableInfo.TableName, uniqueCol.ColumnName, uniqueColumnValue) |
| 1184 | |
| 1185 | //for key, val := range row { |
| 1186 | // existingRow[key] = val |
| 1187 | //} |
| 1188 | |
| 1189 | obj := api2go.NewApi2GoModelWithData(crud.tableInfo.TableName, nil, 0, nil, existingRow) |
| 1190 | |
| 1191 | obj.SetAttributes(row) |
| 1192 | |
| 1193 | _, err = crud.UpdateWithTransaction(obj, req, transaction) |
| 1194 | if err != nil { |
| 1195 | log.Errorf("Failed to update table 809 [%v] update row by unique column [%v]: %v", crud.tableInfo.TableName, uniqueCol.ColumnName, err) |
| 1196 | } |
| 1197 | break |
| 1198 | |
| 1199 | } |
| 1200 | |
| 1201 | } |
| 1202 | } |
| 1203 | } |
no test coverage detected