private void InferRepeatedElement(TableMapping parentTable, XmlElement el) { string localName = XmlHelper.Decode(el.LocalName); // FIXME: can be checked later CheckExtraneousElementColumn(parentTable, el); TableMapping table = GetMappedTable(parentTable, localName, el.NamespaceURI); // If the mapping is actually complex type (not simple // repeatable), then ignore it. if (table.Elements.Count > 0) { return; } // If simple column already exists, do nothing if (table.SimpleContent != null) { return; } GetMappedColumn(table, localName + "_Column", el.Prefix, el.NamespaceURI, MappingType.SimpleContent, null); }
public TableOrViewOptions(TableMapping tableOrViewMapping) { InitializeComponent(); _tableOrViewMapping = tableOrViewMapping; }
public void Add (TableMapping map) { this.List.Add (map); }
private DataColumn GetMappedColumn (TableMapping table, string name, string prefix, string ns, MappingType type, Type optColType) { DataColumn col = table.GetColumn (name); // Infer schema if (col == null) { col = new DataColumn (name); col.Prefix = prefix; col.Namespace = ns; col.ColumnMapping = type; switch (type) { case MappingType.Element: table.Elements.Add (col); break; case MappingType.Attribute: table.Attributes.Add (col); break; case MappingType.SimpleContent: table.SimpleContent = col; break; case MappingType.Hidden: // To generate parent key col.DataType = optColType; table.ReferenceKey = col; break; } } else if (col.ColumnMapping != type) // Check mapping type throw new DataException (String.Format ("There are already another column that has different mapping type. Column is {0}, existing mapping type is {1}", col.ColumnName, col.ColumnMapping)); return col; }
private TableMapping GetMappedTable (TableMapping parent, string tableName, string ns) { TableMapping map = tables [tableName]; if (map != null) { if (parent != null && map.ParentTable != null && map.ParentTable != parent) throw new DataException (String.Format ("The table '{0}' is already allocated as a child of another table '{1}'. Cannot set table '{2}' as parent table.", tableName, map.ParentTable.Table.TableName, parent.Table.TableName)); } else { map = new TableMapping (tableName, ns); map.ParentTable = parent; tables.Add (map); } if (parent != null) { bool shouldAdd = true; foreach (TableMapping child in parent.ChildTables) { if (child.Table.TableName == tableName) { shouldAdd = false; break; } } if (shouldAdd) parent.ChildTables.Add (map); } return map; }
private void InferTableElement (TableMapping parentTable, XmlElement el) { // If parent table already has the same name column but // mapped as Element, that must be removed. // FIXME: This can be done later (doing it here is // loss of performance. CheckExtraneousElementColumn (parentTable, el); string localName = XmlHelper.Decode (el.LocalName); TableMapping table = GetMappedTable (parentTable, localName, el.NamespaceURI); bool hasChildElements = false; bool hasAttributes = false; bool hasText = false; bool isElementRepeated = false; foreach (XmlAttribute attr in el.Attributes) { if (attr.NamespaceURI == XmlConstants.XmlnsNS || attr.NamespaceURI == XmlConstants.XmlNS ) continue; if (ignoredNamespaces != null && ignoredNamespaces.Contains (attr.NamespaceURI)) continue; hasAttributes = true; GetMappedColumn (table, XmlHelper.Decode (attr.LocalName), attr.Prefix, attr.NamespaceURI, MappingType.Attribute, null); } foreach (XmlNode n in el.ChildNodes) { switch (n.NodeType) { case XmlNodeType.Comment: case XmlNodeType.ProcessingInstruction: // ignore continue; default: // text content hasText = true; if (GetElementMappingType (el, ignoredNamespaces, null) == ElementMappingType.Repeated) isElementRepeated = true; break; case XmlNodeType.Element: // child hasChildElements = true; XmlElement cel = n as XmlElement; string childLocalName = XmlHelper.Decode (cel.LocalName); switch (GetElementMappingType (cel, ignoredNamespaces, null)) { case ElementMappingType.Simple: InferColumnElement (table, cel); break; case ElementMappingType.Repeated: if (table.PrimaryKey == null) PopulatePrimaryKey (table); PopulateRelationStructure (table.Table.TableName, childLocalName, table.PrimaryKey.ColumnName); InferRepeatedElement (table, cel); break; case ElementMappingType.Complex: if (table.PrimaryKey == null) PopulatePrimaryKey (table); PopulateRelationStructure (table.Table.TableName, childLocalName, table.PrimaryKey.ColumnName); InferTableElement (table, cel); break; } break; } } // Attributes + !Children + Text = SimpleContent if (table.SimpleContent == null // no need to create && !hasChildElements && hasText && (hasAttributes || isElementRepeated)) { GetMappedColumn (table, table.Table.TableName + "_Text", String.Empty, String.Empty, MappingType.SimpleContent, null); } }
private void InferRepeatedElement (TableMapping parentTable, XmlElement el) { string localName = XmlHelper.Decode (el.LocalName); // FIXME: can be checked later CheckExtraneousElementColumn (parentTable, el); TableMapping table = GetMappedTable (parentTable, localName, el.NamespaceURI); // If the mapping is actually complex type (not simple // repeatable), then ignore it. if (table.Elements.Count > 0) return; // If simple column already exists, do nothing if (table.SimpleContent != null) return; GetMappedColumn (table, localName + "_Column", el.Prefix, el.NamespaceURI, MappingType.SimpleContent, null); }
private void PopulatePrimaryKey (TableMapping table) { DataColumn col = new DataColumn (table.Table.TableName + "_Id"); col.ColumnMapping = MappingType.Hidden; col.DataType = typeof (int); col.AllowDBNull = false; col.AutoIncrement = true; col.Namespace = table.Table.Namespace; col.Prefix = table.Table.Prefix; table.PrimaryKey = col; }
private void CheckExtraneousElementColumn (TableMapping parentTable, XmlElement el) { if (parentTable == null) return; string localName = XmlHelper.Decode (el.LocalName); DataColumn elc = parentTable.GetColumn (localName); if (elc != null) parentTable.RemoveElementColumn (localName); }
private void InferColumnElement (TableMapping table, XmlElement el) { string localName = XmlHelper.Decode (el.LocalName); DataColumn col = table.GetColumn (localName); if (col != null) { if (col.ColumnMapping != MappingType.Element) throw new DataException (String.Format ("Column {0} is already mapped to {1}.", localName, col.ColumnMapping)); table.lastElementIndex = table.Elements.IndexOf (col); return; } if (table.ChildTables [localName] != null) // Child is already mapped, or inferred as a table // (in that case, that takes precedence than // this simple column inference.) return; col = new DataColumn (localName, typeof (string)); col.Namespace = el.NamespaceURI; col.Prefix = el.Prefix; table.Elements.Insert (++table.lastElementIndex, col); }
public void Add(TableMapping map) { this.List.Add(map); }
private void InferTableElement(TableMapping parentTable, XmlElement el) { // If parent table already has the same name column but // mapped as Element, that must be removed. // FIXME: This can be done later (doing it here is // loss of performance. CheckExtraneousElementColumn(parentTable, el); string localName = XmlHelper.Decode(el.LocalName); TableMapping table = GetMappedTable(parentTable, localName, el.NamespaceURI); bool hasChildElements = false; bool hasAttributes = false; bool hasText = false; bool isElementRepeated = false; foreach (XmlAttribute attr in el.Attributes) { if (attr.NamespaceURI == XmlConstants.XmlnsNS || attr.NamespaceURI == XmlConstants.XmlNS ) { continue; } if (ignoredNamespaces != null && ignoredNamespaces.Contains(attr.NamespaceURI)) { continue; } hasAttributes = true; GetMappedColumn(table, XmlHelper.Decode(attr.LocalName), attr.Prefix, attr.NamespaceURI, MappingType.Attribute, null); } foreach (XmlNode n in el.ChildNodes) { switch (n.NodeType) { case XmlNodeType.Comment: case XmlNodeType.ProcessingInstruction: // ignore continue; default: // text content hasText = true; if (GetElementMappingType(el, ignoredNamespaces, null) == ElementMappingType.Repeated) { isElementRepeated = true; } break; case XmlNodeType.Element: // child hasChildElements = true; XmlElement cel = n as XmlElement; string childLocalName = XmlHelper.Decode(cel.LocalName); switch (GetElementMappingType(cel, ignoredNamespaces, null)) { case ElementMappingType.Simple: InferColumnElement(table, cel); break; case ElementMappingType.Repeated: if (table.PrimaryKey == null) { PopulatePrimaryKey(table); } PopulateRelationStructure(table.Table.TableName, childLocalName, table.PrimaryKey.ColumnName); InferRepeatedElement(table, cel); break; case ElementMappingType.Complex: if (table.PrimaryKey == null) { PopulatePrimaryKey(table); } PopulateRelationStructure(table.Table.TableName, childLocalName, table.PrimaryKey.ColumnName); InferTableElement(table, cel); break; } break; } } // Attributes + !Children + Text = SimpleContent if (table.SimpleContent == null && // no need to create !hasChildElements && hasText && (hasAttributes || isElementRepeated)) { GetMappedColumn(table, table.Table.TableName + "_Text", String.Empty, String.Empty, MappingType.SimpleContent, null); } }
private void PopulatePrimaryKey (TableMapping table) { if (table.PrimaryKey != null) { if (table.PrimaryKey.ColumnName != table.Table.TableName + "_Id") throw new DataException ("There is already a primary key column."); return; } DataColumn col = new DataColumn (table.Table.TableName + "_Id"); col.ColumnMapping = MappingType.Hidden; col.DataType = typeof (int); col.AllowDBNull = false; col.AutoIncrement = true; col.Namespace = table.Table.Namespace; col.Prefix = table.Table.Prefix; table.PrimaryKey = col; }
public DbCommand GetUpdateCommand(TableMapping mapping, ConflictResolution extra, object[] args) { var key = new Tuple<TableMapping, ConflictResolution>(mapping, extra); bool created = false; var command = updateCommands.GetOrAdd(key, tuple => { created = true; DbCommand cmd = this.Connection.CreateCommand(); cmd.CommandText = mapping.GetUpdateSql(extra); return cmd; }); if (SqliteSessionBase.Trace) { Debug.WriteLine("Creating update command: {0}", created); } if (args != null) { AddCommandParameters(command, args); } return command; }