protected override bool TryPrepareRow(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, DataRowInfo row, out Dictionary<string, object> preparedRow) { if (table.Name == "tenants_tenants" && string.IsNullOrEmpty(Convert.ToString(row["payment_id"]))) { var oldTenantID = Convert.ToInt32(row["id"]); row["payment_id"] = Core.CoreContext.Configuration.GetKey(oldTenantID); } return base.TryPrepareRow(connection, columnMapper, table, row, out preparedRow); }
public static IEnumerable <DataRowInfo> ReadFromStream(Stream stream) { var readerSettings = new XmlReaderSettings { CheckCharacters = false, CloseInput = false }; using (var xmlReader = XmlReader.Create(stream, readerSettings)) { xmlReader.MoveToContent(); xmlReader.ReadToFollowing("schema", XmlSchemaNamespace); var schema = new Dictionary <string, string>(); var schemaElement = XNode.ReadFrom(xmlReader) as XElement; if (schemaElement != null) { foreach (var entry in schemaElement.Descendants(XName.Get("sequence", XmlSchemaNamespace)).Single().Elements(XName.Get("element", XmlSchemaNamespace))) { schema.Add(entry.Attribute("name").ValueOrDefault(), entry.Attribute("type").ValueOrDefault()); } } while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element) { var el = XNode.ReadFrom(xmlReader) as XElement; if (el != null) { var dataRowInfo = new DataRowInfo(el.Name.LocalName); foreach (var column in schema) { object value = ConvertToType(el.Element(column.Key).ValueOrDefault(), column.Value); dataRowInfo.SetValue(column.Key, value); } yield return(dataRowInfo); } } } } }
public static IEnumerable<DataRowInfo> ReadFromStream(Stream stream) { var readerSettings = new XmlReaderSettings { CheckCharacters = false, CloseInput = false }; using (var xmlReader = XmlReader.Create(stream, readerSettings)) { xmlReader.MoveToContent(); xmlReader.ReadToFollowing("schema", XmlSchemaNamespace); var schema = new Dictionary<string, string>(); var schemaElement = XNode.ReadFrom(xmlReader) as XElement; if (schemaElement != null) { foreach (var entry in schemaElement.Descendants(XName.Get("sequence", XmlSchemaNamespace)).Single().Elements(XName.Get("element", XmlSchemaNamespace))) { schema.Add(entry.Attribute("name").ValueOrDefault(), entry.Attribute("type").ValueOrDefault()); } } while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element) { var el = XNode.ReadFrom(xmlReader) as XElement; if (el != null) { var dataRowInfo = new DataRowInfo(el.Name.LocalName); foreach (var column in schema) { object value = ConvertToType(el.Element(column.Key).ValueOrDefault(), column.Value); dataRowInfo.SetValue(column.Key, value); } yield return dataRowInfo; } } } } }
public IDbCommand CreateInsertCommand(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, DataRowInfo row) { if (table.InsertMethod == InsertMethod.None) return null; Dictionary<string, object> valuesForInsert; if (!TryPrepareRow(connection, columnMapper, table, row, out valuesForInsert)) return null; var insertCommantText = string.Format("{0} into {1}({2}) values({3});", table.InsertMethod != InsertMethod.Ignore ? table.InsertMethod.ToString().ToLower() : "insert ignore", table.Name, string.Join(",", valuesForInsert.Keys), string.Join(",", valuesForInsert.Keys.Select(x => "@" + x))); IDbCommand command = connection.CreateCommand(insertCommantText); foreach (var parameter in valuesForInsert) { command.AddParameter(parameter.Key, parameter.Value); } return command; }
private static bool ValidateSource(Guid expectedValue, DataRowInfo row) { var source = Convert.ToString(row["source"]); try { return expectedValue == new Guid(source); } catch { return false; } }
protected override bool TryPrepareRow(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, DataRowInfo row, out Dictionary<string, object> preparedRow) { if (row.TableName == "files_thirdparty_id_mapping") { //todo: think... preparedRow = new Dictionary<string, object>(); object folderId = null; var sboxId = Regex.Replace(row[1].ToString(), @"(?<=sbox-)\d+", match => { folderId = columnMapper.GetMapping("files_thirdparty_account", "id", match.Value); return Convert.ToString(folderId); }, RegexOptions.Compiled); if (folderId == null) return false; var hashBytes = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(sboxId)); var hashedId = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); preparedRow.Add("hash_id", hashedId); preparedRow.Add("id", sboxId); preparedRow.Add("tenant_id", columnMapper.GetTenantMapping()); columnMapper.SetMapping("files_thirdparty_id_mapping", "hash_id", row["hash_id"], hashedId); return true; } return base.TryPrepareRow(connection, columnMapper, table, row, out preparedRow); }
private static bool ResolveRelation(DataRowInfo row, params int[] matchingTypes) { var entityType = Convert.ToInt32(row["entity_type"]); return matchingTypes.Contains(entityType); }
public bool FitsForRow(DataRowInfo row) { return FitsForTable(row.TableName) && (CollisionResolver == null || CollisionResolver(row)); }
public bool FitsForRow(DataRowInfo row) { return(FitsForTable(row.TableName) && (CollisionResolver == null || CollisionResolver(row))); }
protected virtual bool TryPrepareRow(IDbConnection connection, ColumnMapper columnMapper, TableInfo table, DataRowInfo row, out Dictionary<string, object> preparedRow) { preparedRow = new Dictionary<string, object>(); var parentRelations = TableRelations .Where(x => x.FitsForRow(row) && x.Importance != RelationImportance.Low) .GroupBy(x => x.ChildColumn) .ToDictionary(x => x.Key); foreach (var columnName in row.ColumnNames) { if (table.IdType == IdType.Autoincrement && columnName.Equals(table.IdColumn, StringComparison.OrdinalIgnoreCase)) continue; var val = row[columnName]; if (!parentRelations.ContainsKey(columnName)) { if (!TryPrepareValue(connection, columnMapper, table, columnName, ref val)) return false; } else { if (!TryPrepareValue(connection, columnMapper, table, columnName, parentRelations[columnName], ref val)) return false; } preparedRow.Add(columnName, val); } return true; }
private void WarnCantInsertRow(DataRowInfo row) { InvokeWarning(string.Format("Can't create command to insert row with values [{0}]", row)); }
private void WarnCantInsertRow(DataRowInfo row) { var sb = new StringBuilder("Can't create command to insert row with values [\"", 100); sb.Append(row.Values[0]); for (int i = 1; i < Math.Min(row.Values.Length, 4); i++) { sb.AppendFormat(", {0}", row.Values[i]); } if (row.Values.Length > 5) { sb.Append(", ..."); } sb.AppendFormat("\"] into table {0}", row.TableName); InvokeWarning(sb.ToString()); }