//TODO:Build table base on custome attribute
        public TableDefinition Build(Type type)
        {
            if (!type.IsDefined(typeof(TableAttribute), false))
            {
                return(null);
            }

            TableAttribute ta = (TableAttribute)type.GetCustomAttributes(
                typeof(TableAttribute), false)[0];

            TableDefinition table = new TableDefinition(ta.Name)
                                    .Schema(ta.Schema).Sequence(ta.Sequence);

            BindingFlags flags = BindingFlags.Instance
                                 | BindingFlags.Public
                                 | BindingFlags.NonPublic;

            List <MemberInfo> members = new List <MemberInfo>();

            members.AddRange(type.GetFields(flags));
            members.AddRange(type.GetProperties(flags));

            foreach (MemberInfo member in members)
            {
                if (!member.IsDefined(typeof(FieldAttribute), false))
                {
                    continue;
                }

                FieldAttribute fa = (FieldAttribute)member.GetCustomAttributes(
                    typeof(FieldAttribute), false)[0];

                table.Field(fa.Name).MapTo(member.Name).Flags(fa.Flags)
                .ReferenceTo(fa.TableReference, fa.columnReference).Add();
            }

            return(table);
        }
示例#2
0
        //TODO: move to new class
        public List <TableDefinition> listTable()
        {
            List <TableDefinition> result = new List <TableDefinition>();
            //TODO: get all table infomation
            string query = "SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_TYPE ='BASE TABLE' order by TABLE_NAME";

            DataTable db = GetDataTalbe(query);

            for (int i = 0; i < db.Rows.Count; i++)
            {
                string tableName = db.Rows[i][0].ToString();
                if (tableName == "sysdiagrams")
                {
                    continue;
                }
                else
                {
                    TableDefinition table = new TableDefinition(tableName).Schema("dbo");

                    DataTable column = getTableColumn(tableName);
                    //get identity field
                    DataTable identites      = GetDataTalbe(string.Format(BaseQuery.GET_TABLE_IDENTITY, tableName));
                    string    identity_Field = "null";
                    if (identites.Rows.Count > 0)
                    {
                        identity_Field = identites.Rows[0][0].ToString();
                    }

                    for (int j = 0; j < column.Rows.Count; j++)
                    {
                        string fieldName      = column.Rows[j][1].ToString();
                        string fieldData      = column.Rows[j][2].ToString();
                        string CONTRAINT_TYPE = column.Rows[j][3].ToString();
                        string RF_TABLE       = column.Rows[j][4].ToString();
                        string RF_COLUMN      = column.Rows[j][5].ToString();

                        if (CONTRAINT_TYPE.Contains("PRIMARY KEY"))
                        {
                            int lastIndex = table.fields.FindIndex(x => x.columnName == fieldName);
                            if (lastIndex < 0)
                            {
                                //check if is identity
                                if (fieldName.Equals(identity_Field))
                                {
                                    table.Field(fieldName).MapTo(MSSQLDataType.MsqlToCSharp(fieldData)).Key().ReadOnly().Auto().Add();
                                }
                                else
                                {
                                    table.Field(fieldName).MapTo(MSSQLDataType.MsqlToCSharp(fieldData)).Key().ReadOnly().Add();
                                }
                            }
                            else
                            {
                                //check if is identity
                                if (fieldName.Equals(identity_Field))
                                {
                                    table.fields[lastIndex].flags = FieldFlags.Key | FieldFlags.ForeignKey | FieldFlags.Auto;
                                }
                                else
                                {
                                    table.fields[lastIndex].flags = FieldFlags.Key | FieldFlags.ForeignKey;
                                }
                            }
                        }
                        else if (CONTRAINT_TYPE.Contains("FOREIGN KEY"))
                        {
                            //check if this is primary key
                            //check if contains key and foreikey
                            //update field
                            int lastIndex = table.fields.FindIndex(x => x.columnName == fieldName);
                            if (lastIndex < 0)
                            {
                                table.Field(fieldName).MapTo(MSSQLDataType.MsqlToCSharp(fieldData))
                                .ForeignKey()
                                .ReferenceTo(RF_TABLE, RF_COLUMN).Add();
                            }
                            else
                            {
                                if (fieldName.Equals(identity_Field))
                                {
                                    table.fields[lastIndex].flags = FieldFlags.Key | FieldFlags.ForeignKey | FieldFlags.Auto;
                                }
                                else
                                {
                                    table.fields[lastIndex].flags = FieldFlags.Key | FieldFlags.ForeignKey;
                                }

                                table.fields[lastIndex].fieldReference = new FieldReference
                                {
                                    table  = RF_TABLE,
                                    column = RF_COLUMN
                                };
                            }
                        }
                        else
                        {
                            table.Field(fieldName).MapTo(MSSQLDataType.MsqlToCSharp(fieldData)).Add();
                        }
                    }
                    result.Add(table);
                }
            }
            return(result);
        }