예제 #1
0
        public static DataView GetAttributes(dbdefinition pDbDefinition, string pEntity)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Attribute", typeof(string));
            dt.Columns.Add("Type", typeof(string));
            dt.Columns.Add("Size", typeof(string));
            dt.Columns.Add("Default", typeof(string));
            dt.Columns.Add("Required", typeof(bool));
            dt.Columns.Add("Description", typeof(string));

            dbdefinitionEntity entity = null;

            foreach (dbdefinitionEntity e in pDbDefinition.entities)
            {
                if (e.name.Equals(pEntity))
                {
                    entity = e;
                    break;
                }
            }

            if (entity != null)
            {
                foreach (dbdefinitionEntityAttribute attr in entity.attributes)
                {
                    DataRow row = dt.NewRow();
                    row["Attribute"] = attr.name;
                    row["Type"]      = attr.type.ToString();
                    if (attr.size != null)
                    {
                        row["Size"] = attr.size;
                        if (attr.scale != null)
                        {
                            row["Size"] = row["Size"] + "/" + attr.scale;
                        }
                    }
                    if (attr.defaultvalue != null)
                    {
                        row["Default"] = attr.defaultvalue;
                    }
                    row["Required"] = attr.required;
                    if (attr.description != null)
                    {
                        row["Description"] = attr.description;
                    }
                    dt.Rows.Add(row);
                }
            }

            DataView dv = new DataView(dt);

            dv.AllowEdit   = false;
            dv.AllowNew    = false;
            dv.AllowDelete = false;
            return(dv);
        }
        private dbdefinitionEntity GenerateEntity(ADOX.Table ptbl, ADODB.Connection pcon)
        {
            dbdefinitionEntity entity = new dbdefinitionEntity();

            entity.name        = ptbl.Name;
            entity.description = GetTableDescription(ptbl, pcon);

            // Generate attributes
            ArrayList alAttributes = new ArrayList();
            int       intColIndex  = 0;

            ADODB.Recordset rs = new ADODB.RecordsetClass();
            rs.Open("SELECT * FROM [" + ptbl.Name + "] WHERE (3=4)", pcon, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly, 0);
            foreach (ADODB.Field fld in rs.Fields)
            {
                alAttributes.Add(GenerateColumn(ptbl.Columns[fld.Name], intColIndex));
                intColIndex++;
            }
            rs.Close();
            if (alAttributes.Count != 0)
            {
                entity.attributes = (dbdefinitionEntityAttribute[])alAttributes.ToArray(typeof(dbdefinitionEntityAttribute));
            }

            // Generate primary/unique keys/constraints
            entity.keys = new dbdefinitionEntityKeys();
            ArrayList alUniqueKeys  = new ArrayList();
            ArrayList alForeignKeys = new ArrayList();
            int       intIndex      = 0;

            foreach (ADOX.Key key in ptbl.Keys)
            {
                switch (key.Type)
                {
                case ADOX.KeyTypeEnum.adKeyPrimary:
                    entity.keys.primarykey      = new dbdefinitionEntityKeysPrimarykey();
                    entity.keys.primarykey.name = ptbl.Name + "_pk";

                    // get all primary key columns
                    ArrayList aAttr = new ArrayList();
                    foreach (ADOX.Column col in key.Columns)
                    {
                        dbdefinitionEntityKeysPrimarykeyAttributeref attr = new dbdefinitionEntityKeysPrimarykeyAttributeref();
                        attr.attribute = col.Name;

                        aAttr.Add(attr);
                    }

                    // create the primary key attribute array
                    entity.keys.primarykey.attributeref = (dbdefinitionEntityKeysPrimarykeyAttributeref [])
                                                          aAttr.ToArray(typeof(dbdefinitionEntityKeysPrimarykeyAttributeref));
                    break;

                case ADOX.KeyTypeEnum.adKeyUnique:
                    dbdefinitionEntityKeysUniquekey uniquekey = new dbdefinitionEntityKeysUniquekey();

                    ArrayList alAttributeRefs = new ArrayList();
                    foreach (ADOX.Column col in key.Columns)
                    {
                        dbdefinitionEntityKeysUniquekeyAttributeref attributeref = new dbdefinitionEntityKeysUniquekeyAttributeref();
                        attributeref.attribute = col.Name;
                        alAttributeRefs.Add(attributeref);
                    }
                    if (alAttributeRefs.Count != 0)
                    {
                        uniquekey.attributeref = (dbdefinitionEntityKeysUniquekeyAttributeref[])alAttributeRefs.ToArray(typeof(dbdefinitionEntityKeysUniquekeyAttributeref));
                    }

                    // check for duplicate indexes - ignore duplicate index defintions
                    bool isDuplicate = false;
                    foreach (dbdefinitionEntityKeysUniquekey existingKey in alUniqueKeys)
                    {
                        if (uniquekey.attributeref.Length == existingKey.attributeref.Length)
                        {
                            isDuplicate = true;
                            for (int i = 0; i < uniquekey.attributeref.Length; i++)
                            {
                                if (!uniquekey.attributeref[i].attribute.Equals(existingKey.attributeref[i].attribute))
                                {
                                    isDuplicate = false;
                                    break;
                                }
                            }
                            if (isDuplicate)
                            {
                                break;
                            }
                        }
                    }
                    if (!isDuplicate)
                    {
                        uniquekey.name = ptbl.Name + "_uk" + intIndex;
                        alUniqueKeys.Add(uniquekey);
                    }
                    break;

                case ADOX.KeyTypeEnum.adKeyForeign:
                    dbdefinitionEntityKeysForeignkey foreignkey = new dbdefinitionEntityKeysForeignkey();
                    foreignkey.name            = ptbl.Name + "_fk" + intIndex;
                    foreignkey.foreignentity   = key.RelatedTable;
                    foreignkey.cascadingdelete = (key.DeleteRule & ADOX.RuleEnum.adRICascade) != 0;

                    foreignkey.attributeref                  = new dbdefinitionEntityKeysForeignkeyAttributeref();
                    foreignkey.attributeref.attribute        = key.Columns[0].Name;
                    foreignkey.attributeref.foreignattribute = key.Columns[0].RelatedColumn;

                    alForeignKeys.Add(foreignkey);
                    break;
                }

                intIndex++;
            }
            if (alUniqueKeys.Count != 0)
            {
                entity.keys.uniquekey = (dbdefinitionEntityKeysUniquekey[])alUniqueKeys.ToArray(typeof(dbdefinitionEntityKeysUniquekey));
            }
            if (alForeignKeys.Count != 0)
            {
                entity.keys.foreignkey = (dbdefinitionEntityKeysForeignkey[])alForeignKeys.ToArray(typeof(dbdefinitionEntityKeysForeignkey));
            }

            // Generate indices
            ArrayList alIndexes = new ArrayList();

            intIndex = 0;
            foreach (ADOX.Index idx in ptbl.Indexes)
            {
                if (!idx.PrimaryKey)
                {
                    dbdefinitionEntityIndex index = new dbdefinitionEntityIndex();
                    index.unique      = idx.Unique;
                    index.ignorenulls = (idx.IndexNulls == ADOX.AllowNullsEnum.adIndexNullsIgnore);

                    ArrayList alAttributeRefs = new ArrayList();
                    foreach (ADOX.Column col in idx.Columns)
                    {
                        dbdefinitionEntityIndexAttributeref attributeref = new dbdefinitionEntityIndexAttributeref();
                        attributeref.attribute = col.Name;
                        alAttributeRefs.Add(attributeref);
                    }
                    index.attributeref = (dbdefinitionEntityIndexAttributeref[])alAttributeRefs.ToArray(typeof(dbdefinitionEntityIndexAttributeref));

                    // check for duplicate indexes - ignore duplicate index defintions
                    bool isDuplicate = false;
                    foreach (dbdefinitionEntityIndex existingIndex in alIndexes)
                    {
                        if (index.attributeref.Length == existingIndex.attributeref.Length)
                        {
                            isDuplicate = true;
                            for (int i = 0; i < index.attributeref.Length; i++)
                            {
                                if (!index.attributeref[i].attribute.Equals(existingIndex.attributeref[i].attribute))
                                {
                                    isDuplicate = false;
                                    break;
                                }
                            }
                            if (isDuplicate)
                            {
                                break;
                            }
                        }
                    }
                    if (!isDuplicate)
                    {
                        index.name = ptbl.Name + "_idx" + intIndex;
                        alIndexes.Add(index);
                    }
                    intIndex++;
                }
            }
            if (alIndexes.Count != 0)
            {
                entity.indexes = (dbdefinitionEntityIndex[])alIndexes.ToArray(typeof(dbdefinitionEntityIndex));
            }

            // mark 1:1 relationship foreign keys with "one-to-one" attribute
            if (entity.keys != null && entity.keys.foreignkey != null && entity.indexes != null)
            {
                foreach (dbdefinitionEntityKeysForeignkey foreignkey in entity.keys.foreignkey)
                {
                    foreach (dbdefinitionEntityIndex index in entity.indexes)
                    {
                        if (index.attributeref.Length == 1 && index.attributeref[0].attribute.Equals(foreignkey.attributeref.foreignattribute))
                        {
                            if (index.unique)
                            {
                                foreignkey.onetoone = true;
                            }
                        }
                    }
                }
            }

            // Generate hashcode of entity definition
            XmlSerializer entityDefSerializer = new XmlSerializer(typeof(dbdefinitionEntity));
            StringWriter  stringWriter        = new StringWriter();

            entityDefSerializer.Serialize(stringWriter, entity);
            String entityDef = stringWriter.ToString();

            entity.hashcode = entityDef.GetHashCode().ToString();

            return(entity);
        }