Example #1
0
        private TableInfo clone(bool asError, bool includeFields, bool includeIndexes, bool includeConstraints)
        {
            TableInfo ti = TableInfo.GetInstance(DataConnectionSpec);

            ti.CharacterSet              = this.CharacterSet;
            ti.Collation                 = this.Collation;
            ti.Engine                    = this.Engine;
            ti.PrimaryKeySortType        = this.PrimaryKeySortType;
            ti.UseCaseSensitiveCollation = this.UseCaseSensitiveCollation;
            ti.SyncAction                = this.SyncAction;

            if (asError)
            {
                FieldInfo fiErr = FieldInfo.GetInstance(ti);
                fiErr.DataType   = typeof(string);
                fiErr.DbType     = DbType.String;
                fiErr.Name       = "err_message";
                fiErr.MaxLength  = 300;
                fiErr.IsNullable = true;
                ti.Fields.Add(fiErr);

                ti.IsSelected = false;
                ti.TableName  = this.TableName + "__importerror";
                ti.RowCount   = 0;

                if (includeFields)
                {
                    foreach (FieldInfo fi in Fields)
                    {
                        // for error table, we assume no auto-increments
                        FieldInfo fiClone = fi.Clone(ti);
                        fiClone.IsAutoIncrement = false;
                        fiClone.IsNullable      = true;
                        ti.Fields.Add(fiClone);
                    }
                }
            }
            else
            {
                ti.IsSelected = this.IsSelected;
                ti.TableName  = this.TableName;
                ti.RowCount   = this.RowCount;

                if (includeFields)
                {
                    foreach (FieldInfo fi in Fields)
                    {
                        ti.Fields.Add(fi.Clone(ti));
                    }
                }
            }

            if (includeIndexes)
            {
                foreach (IndexInfo ii in Indexes)
                {
                    ti.Indexes.Add(ii.Clone(ti));
                }
            }

            if (includeConstraints)
            {
                foreach (ConstraintInfo ci in Constraints)
                {
                    ti.Constraints.Add(ci.Clone(ti));
                }
            }


            return(ti);
        }
Example #2
0
        protected override void fillFromDataSet(DataSet ds, DataRow dr, List <TableInfo> tables)
        {
            TableName           = dr["table_name"].ToString().ToLower();
            ConstraintName      = dr["constraint_name"].ToString().ToLower();
            ReferencesTableName = dr["references_table_name"].ToString().ToLower();
            _tableList          = tables;
            _dsToFillFrom       = ds;
            _drToFillFrom       = dr;

            if (!mapSourceFields(ds, dr))
            {
                // source fields not found yet. means the parent of this constraint is not mapped yet.
                Debug.Write("Constraint " + ConstraintName + " from " + TableName + " to " + ReferencesTableName + " has no source fields as " + ReferencesTableName + " has not been resolved yet.");
                Debug.WriteLine("");
            }

            // make sure the reference table is mapped
            TableInfo target = null;

            if (ReferencesTableName.ToUpper().Trim() == this.TableName.ToUpper().Trim())
            {
                // self-referential
                target = this.Table;
            }
            else
            {
                foreach (TableInfo ti in tables)
                {
                    if (ti.TableName.ToUpper().Trim() == ReferencesTableName.ToUpper().Trim())
                    {
                        // table has already been inspected
                        target = ti;
                        break;
                    }
                }
            }

            if (!tables.Contains(this.Table))
            {
                tables.Add(this.Table);
            }

            this.SchemaName = this.Table.SchemaName;

            if (target == null)
            {
                // the table we are referencing hasn't been inspected yet.
                // do that now.
                DataRow[] drT = ds.Tables["TableInfo"].Select("table_name = '" + ReferencesTableName.ToLower() + "'");
                if (drT == null || drT.Length == 0)
                {
                    throw new InvalidOperationException(getDisplayMember("fillFromDataSet", "Could not find row in TableInfo table for table name={0}", ReferencesTableName.ToLower()));
                }
                else
                {
                    target = TableInfo.GetInstance(this.DataConnectionSpec);
                    target.FillFromDataSet(ds, drT[0], tables);

                    if (!tables.Exists(te => {
                        return(target.TableName.ToUpper().Trim() == te.TableName.ToUpper().Trim());
                    }))
                    {
                        tables.Add(target);
                    }
                }
            }

            ReferencesTable = target;


            DataRow[] drRefFields = ds.Tables["ConstraintReferencesFieldInfo"].Select("parent_id = " + dr["id"]);
            foreach (DataRow drRef in drRefFields)
            {
                DataRow[] drReferenceTableField = ds.Tables["TableFieldInfo"].Select("table_name = '" + ReferencesTableName.ToLower() + "' and field_name = '" + drRef["field_name"].ToString().ToLower() + "'");
                if (drReferenceTableField == null || drReferenceTableField.Length == 0)
                {
                    throw new InvalidOperationException(getDisplayMember("fillFromDataSet{reffield}", "Could not find field '{0}' in table '{1}' that constraint '{2}' in table '{3}' references.", drRef["field_name"].ToString().ToLower(), drRef["table_name"].ToString().ToLower(), this.ConstraintName.ToLower(), this.TableName.ToLower()));
                }
                FieldInfo fi = target.Fields.Find(f => {
                    return(f.Name.ToUpper().Trim() == drRef["field_name"].ToString().ToUpper().Trim());
                });
                if (fi == null)
                {
                    throw new InvalidOperationException(getDisplayMember("fillFromDataSet{reffieldmissing}", "Could not find field named {0} in table {1}", drRef["field_name"].ToString().ToLower(), target.TableName.ToLower()));
                }
                else
                {
                    SourceFields[ReferencesFields.Count].IsForeignKey        = true;
                    SourceFields[ReferencesFields.Count].ForeignKeyTable     = fi.Table;
                    SourceFields[ReferencesFields.Count].ForeignKeyTableName = fi.TableName.ToLower();
                    SourceFields[ReferencesFields.Count].ForeignKeyFieldName = fi.Name.ToLower();
                    ReferencesFields.Add(fi);
                    ReferencesFieldNames.Add(fi.Name.ToLower());
                }
            }
        }