Esempio n. 1
0
        /// <summary>
        /// 获取所有关系
        /// </summary>
        /// <param name="chileName">null则查询所有表</param>
        /// <returns></returns>
        public List <TableRelationAttribute> GetRelation(DataBaseOperate oper, DBInfo info, IEnumerable <string> childNames)
        {
            List <TableRelationAttribute> lst = new List <TableRelationAttribute>();

            using (BatchAction ba = oper.StarBatchAction())
            {
                OleDbConnection conn = oper.Connection as OleDbConnection;
                oper.ConnectDataBase();
                DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys, null);

                foreach (DataRow dr in dt.Rows)
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.CreateName();
                    tinfo.SourceTable = dr["FK_TABLE_NAME"] as string;
                    tinfo.SourceName  = dr["FK_COLUMN_NAME"] as string;
                    tinfo.TargetTable = dr["PK_TABLE_NAME"] as string;
                    tinfo.TargetName  = dr["PK_COLUMN_NAME"] as string;
                    tinfo.IsParent    = true;
                    lst.Add(tinfo);

                    tinfo = new TableRelationAttribute();
                    tinfo.CreateName();
                    tinfo.SourceTable = dr["PK_TABLE_NAME"] as string;
                    tinfo.SourceName  = dr["PK_COLUMN_NAME"] as string;
                    tinfo.TargetTable = dr["FK_TABLE_NAME"] as string;
                    tinfo.TargetName  = dr["FK_COLUMN_NAME"] as string;
                    tinfo.IsParent    = false;
                    lst.Add(tinfo);
                }
            }
            return(lst);
        }
Esempio n. 2
0
        /// <summary>
        /// 去掉基类已有字段
        /// </summary>
        private void FilterBaseTypeParam()
        {
            Dictionary <string, EntityParamField>   dicParam    = new Dictionary <string, EntityParamField>(StringComparer.CurrentCultureIgnoreCase);
            Dictionary <string, EntityRelationItem> dicRelation = new Dictionary <string, EntityRelationItem>(StringComparer.CurrentCultureIgnoreCase);

            FillBaseTypeParam(_baseType, _designerInfo, dicParam, dicRelation);
            int count = _belongTable.Params.Count;

            for (int i = count - 1; i >= 0; i--)
            {
                string key = _belongTable.Params[i].ParamName;
                if (dicParam.ContainsKey(key))
                {
                    _belongTable.Params.RemoveAt(i);
                }
            }

            count = _belongTable.RelationItems.Count;
            for (int i = count - 1; i >= 0; i--)
            {
                TableRelationAttribute er = _belongTable.RelationItems[i];
                string key = EntityFieldBase.ToPascalName(er.SourceName) + "_" + EntityFieldBase.ToPascalName(er.TargetName);
                if (dicRelation.ContainsKey(key))
                {
                    _belongTable.RelationItems.RemoveAt(i);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 获取所有关系
        /// </summary>
        /// <param name="oper"></param>
        /// <param name="info"></param>
        /// <param name="childName"></param>
        /// <returns></returns>
        public List <TableRelationAttribute> GetRelation(DataBaseOperate oper, DBInfo info, IEnumerable <string> childName)
        {
            return(null);

            DataTable dtRelation = oper.GetSchema("ForeignKeys");
            List <TableRelationAttribute> lstRet = new List <TableRelationAttribute>();

            foreach (DataRow row in dtRelation.Rows)
            {
                TableRelationAttribute tr = new TableRelationAttribute();
                tr.Name        = row["CONSTRAINT_NAME"] as string;
                tr.SourceName  = row["FKEY_FROM_COLUMN"] as string;
                tr.SourceTable = row["TABLE_NAME"] as string;
                tr.TargetName  = row["FKEY_TO_COLUMN"] as string;
                tr.TargetTable = row["FKEY_TO_TABLE"] as string;
                tr.IsParent    = true;
                lstRet.Add(tr);

                tr             = new TableRelationAttribute();
                tr.Name        = row["CONSTRAINT_NAME"] as string;
                tr.TargetName  = row["FKEY_FROM_COLUMN"] as string;
                tr.TargetTable = row["TABLE_NAME"] as string;
                tr.SourceName  = row["FKEY_TO_COLUMN"] as string;
                tr.SourceTable = row["FKEY_TO_TABLE"] as string;
                tr.IsParent    = false;
                lstRet.Add(tr);
            }
            return(lstRet);
        }
 /// <summary>
 /// 外键
 /// </summary>
 /// <param name="parentTable">主表</param>
 /// <param name="childTable">子表</param>
 /// <param name="parentParam">主表属性</param>
 /// <param name="childParam">子表属性</param>
 /// <param name="previous">上一个</param>
 public KeyWordAddForeignkeyItem(string name, string parentTable, string childTable,
                                 string parentParam, string childParam,
                                 BQLQuery previous)
     : base(previous)
 {
     _item = new TableRelationAttribute("", name, childTable, parentTable, childParam, parentParam, "", false);
 }
Esempio n. 5
0
        /// <summary>
        /// 创建属性的信息类
        /// </summary>
        /// <param name="belong">所属的实体</param>
        /// <param name="getHandle">get委托</param>
        /// <param name="setHandle">set委托</param>
        /// <param name="tableMappingAtt">映射标识类</param>
        /// <param name="fieldName">属性名</param>
        /// <param name="fieldType">属性类型</param>
        public EntityMappingInfo(Type belong, GetFieldValueHandle getHandle, SetFieldValueHandle setHandle,
                                 TableRelationAttribute mappingInfo, string fieldName, Type fieldType, FieldInfo belongFieldInfo,
                                 PropertyInfo belongPropertyInfo)

            : base(belong, getHandle, setHandle, fieldType, fieldName, belongFieldInfo)
        {
            this._mappingInfo        = mappingInfo;
            this._belongPropertyInfo = belongPropertyInfo;
        }
Esempio n. 6
0
        /// <summary>
        /// 填充映射信息
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="entity"></param>
        internal static void FillRelationInfo(XmlDocument doc, Dictionary <string, TableRelationAttribute> dicRelation)
        {
            XmlNodeList lstProperty = doc.GetElementsByTagName("relation");

            foreach (XmlNode node in lstProperty)
            {
                XmlAttribute att = node.Attributes["FieldName"];
                if (att == null)
                {
                    continue;
                }
                string fName = att.InnerText;
                if (string.IsNullOrEmpty(fName))
                {
                    continue;
                }
                TableRelationAttribute tr = new TableRelationAttribute();

                tr.FieldName = fName;

                att = node.Attributes["PropertyName"];
                if (att != null)
                {
                    tr.PropertyName = att.InnerText;
                }
                att = node.Attributes["SourceProperty"];
                if (att != null)
                {
                    tr.SourceName = att.InnerText;
                }

                att = node.Attributes["TargetProperty"];
                if (att != null)
                {
                    tr.TargetName = att.InnerText;
                }

                att = node.Attributes["IsParent"];
                if (att != null)
                {
                    tr.IsParent = att.InnerText == "1";
                }
                att = node.Attributes["IsToDB"];
                if (att != null)
                {
                    tr.IsToDB = att.InnerText == "1";
                }
                att = node.Attributes["Description"];
                if (att != null)
                {
                    tr.Description = att.InnerText;
                }
                dicRelation[tr.FieldName] = tr;
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 获取所有关系
        /// </summary>
        /// <param name="oper"> </param>
        /// <param name="info"> </param>
        /// <param name="childNames">null则查询所有表</param>
        /// <returns></returns>
        public List <TableRelationAttribute> GetRelation(DataBaseOperate oper, DBInfo info, IEnumerable <string> childNames)
        {
            string        sql   = "SELECT t1.CONSTRAINT_NAME,t1.TABLE_NAME, t1.COLUMN_NAME, t1.POSITION_IN_UNIQUE_CONSTRAINT,  t1.REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE t1  INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t2  ON t2.TABLE_SCHEMA = t1.TABLE_SCHEMA  AND t2.TABLE_NAME = t1.TABLE_NAME  AND t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME WHERE t1.TABLE_SCHEMA = ?dbName  AND t2.CONSTRAINT_TYPE = 'FOREIGN KEY'";
            StringBuilder sqlFk = new StringBuilder();
            StringBuilder sqlPk = new StringBuilder();

            //sql.Append("SELECT constraint_schema,constraint_name,unique_constraint_name,table_name,referenced_table_name FROM `information_schema`.`REFERENTIAL_CONSTRAINTS`;");
            sqlFk.Append(sql);
            sqlPk.Append(sql);
            ParamList lstParam = new ParamList();

            lstParam.AddNew("?dbName", DbType.String, oper.DataBaseName);
            string childName = Buffalo.DB.DataBaseAdapter.SqlServer2KAdapter.DBStructure.AllInTableNames(childNames);

            if (!string.IsNullOrEmpty(childName))
            {
                sqlFk.Append(" and t1.TABLE_NAME in(" + childName + ")");
            }
            if (!string.IsNullOrEmpty(childName))
            {
                sqlPk.Append(" and t1.REFERENCED_TABLE_NAME in(" + childName + ")");
            }
            List <TableRelationAttribute> lst = new List <TableRelationAttribute>();

            using (IDataReader reader = info.DefaultOperate.Query(sqlFk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name        = reader["CONSTRAINT_NAME"] as string;
                    tinfo.SourceTable = reader["TABLE_NAME"] as string;
                    tinfo.SourceName  = reader["COLUMN_NAME"] as string;
                    tinfo.TargetTable = reader["REFERENCED_TABLE_NAME"] as string;
                    tinfo.TargetName  = reader["REFERENCED_COLUMN_NAME"] as string;
                    tinfo.IsParent    = true;
                    lst.Add(tinfo);
                }
            }
            using (IDataReader reader = info.DefaultOperate.Query(sqlPk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name        = reader["CONSTRAINT_NAME"] as string;
                    tinfo.TargetTable = reader["TABLE_NAME"] as string;
                    tinfo.TargetName  = reader["COLUMN_NAME"] as string;
                    tinfo.SourceTable = reader["REFERENCED_TABLE_NAME"] as string;
                    tinfo.SourceName  = reader["REFERENCED_COLUMN_NAME"] as string;
                    tinfo.IsParent    = false;
                    lst.Add(tinfo);
                }
            }
            return(lst);
        }
Esempio n. 8
0
        /// <summary>
        /// 获取所有关系
        /// </summary>
        /// <param name="chileName">null则查询所有表</param>
        /// <returns></returns>
        public List <TableRelationAttribute> GetRelation(DataBaseOperate oper, DBInfo info, IEnumerable <string> childNames)
        {
            string        fksql = "select * from (select fk.name fkname ,ftable.[name] childname, cn.[name] childparam, rtable.[name] parentname,pn.[name] parentparam from sysforeignkeys join sysobjects fk on sysforeignkeys.constid = fk.id join sysobjects ftable on sysforeignkeys.fkeyid = ftable.id join sysobjects rtable on sysforeignkeys.rkeyid = rtable.id join syscolumns cn on sysforeignkeys.fkeyid = cn.id and sysforeignkeys.fkey = cn.colid join syscolumns pn on sysforeignkeys.rkeyid = pn.id and sysforeignkeys.rkey = pn.colid) a where 1=1";
            StringBuilder sqlfk = new StringBuilder();
            StringBuilder sqlpk = new StringBuilder();

            sqlfk.Append(fksql);
            sqlpk.Append(fksql);
            ParamList lstParam  = new ParamList();
            string    childName = AllInTableNames(childNames);

            if (!string.IsNullOrEmpty(childName))
            {
                sqlfk.Append(" and childname in(" + childName + ")");
            }
            if (!string.IsNullOrEmpty(childName))
            {
                sqlpk.Append(" and parentname in(" + childName + ")");
            }
            List <TableRelationAttribute> lst = new List <TableRelationAttribute>();

            using (IDataReader reader = info.DefaultOperate.Query(sqlfk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name        = reader["fkname"] as string;
                    tinfo.SourceTable = reader["childname"] as string;
                    tinfo.SourceName  = reader["childparam"] as string;
                    tinfo.TargetTable = reader["parentname"] as string;
                    tinfo.TargetName  = reader["parentparam"] as string;
                    tinfo.IsParent    = true;

                    lst.Add(tinfo);
                }
            }
            using (IDataReader reader = info.DefaultOperate.Query(sqlpk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name        = reader["fkname"] as string;
                    tinfo.TargetTable = reader["childname"] as string;
                    tinfo.TargetName  = reader["childparam"] as string;
                    tinfo.SourceTable = reader["parentname"] as string;
                    tinfo.SourceName  = reader["parentparam"] as string;
                    tinfo.IsParent    = false;
                    lst.Add(tinfo);
                }
            }
            return(lst);
        }
Esempio n. 9
0
        /// <summary>
        /// 添加字段的语句xrrrr

        /// <summary>
        /// 获取所有关系
        /// </summary>
        /// <param name="oper"> </param>
        /// <param name="info"> </param>
        /// <param name="childNames">null则查询所有表</param>
        /// <returns></returns>
        public List <TableRelationAttribute> GetRelation(DataBaseOperate oper, DBInfo info, IEnumerable <string> childNames)
        {
            StringBuilder sqlFk = new StringBuilder();
            StringBuilder sqlPk = new StringBuilder();
            string        sql   = "select px.conname as constraintname, px.contype, home.relname as thisname, fore.relname as theirname, px.conrelid as homeid, px.confrelid as foreid, px.conkey as thiscols, px.confkey as fcols, att.attname as colname, fatt.attname as fcolname, px.confupdtype, px.confdeltype from information_schema.table_constraints tc inner join pg_constraint px on (px.conname=tc.constraint_name) left join pg_class home on (home.oid = px.conrelid) left join pg_class fore on (fore.oid = px.confrelid) right join pg_attribute att on (att.attrelid = px.conrelid AND att.attnum = ANY(px.conkey)) right join pg_attribute fatt on (fatt.attrelid = px.confrelid AND fatt.attnum = ANY(px.confkey)) where tc.constraint_type='FOREIGN KEY'";

            //sql.Append("SELECT constraint_schema,constraint_name,unique_constraint_name,table_name,referenced_table_name FROM `information_schema`.`REFERENTIAL_CONSTRAINTS`;");
            sqlFk.Append(sql);
            sqlPk.Append(sql);
            ParamList lstParam  = new ParamList();
            string    childName = Buffalo.DB.DataBaseAdapter.SqlServer2KAdapter.DBStructure.AllInTableNames(childNames);

            if (!string.IsNullOrEmpty(childName))
            {
                sqlFk.Append(" and home.relname in(" + childName + ")");
            }
            if (!string.IsNullOrEmpty(childName))
            {
                sqlPk.Append(" and fore.relname in(" + childName + ")");
            }
            List <TableRelationAttribute> lst = new List <TableRelationAttribute>();

            using (IDataReader reader = info.DefaultOperate.Query(sqlFk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name        = reader["constraintname"] as string;
                    tinfo.SourceTable = reader["thisname"] as string;
                    tinfo.SourceName  = reader["colname"] as string;
                    tinfo.TargetTable = reader["theirname"] as string;
                    tinfo.TargetName  = reader["fcolname"] as string;
                    tinfo.IsParent    = true;
                    lst.Add(tinfo);
                }
            }
            using (IDataReader reader = info.DefaultOperate.Query(sqlPk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name        = reader["constraintname"] as string;
                    tinfo.TargetTable = reader["thisname"] as string;
                    tinfo.TargetName  = reader["colname"] as string;
                    tinfo.SourceTable = reader["theirname"] as string;
                    tinfo.SourceName  = reader["fcolname"] as string;
                    tinfo.IsParent    = false;
                    lst.Add(tinfo);
                }
            }
            return(lst);
        }
Esempio n. 10
0
        public List <TableRelationAttribute> GetRelation(DataBaseOperate oper, DBInfo info, IEnumerable <string> childNames)
        {
            string        sql      = "select b.table_name as pktable_name,b.column_name pkcolumn_name,c.table_name fktable_name,c.column_name fkcolumn_name,c.position ke_seq,c.constraint_name fk_name from (select * from user_cons_columns ) b left join (select * from user_constraints where user_constraints.constraint_type='R' ) a on  b.constraint_name=a.r_constraint_name left join user_cons_columns c on  c.constraint_name=a.constraint_name where c.position is not null and c.position=b.position ";
            StringBuilder sqlFk    = new StringBuilder(1024);
            StringBuilder sqlPk    = new StringBuilder(1024);
            ParamList     lstParam = new ParamList();

            sqlFk.Append(sql);
            sqlPk.Append(sql);
            string childName = Buffalo.DB.DataBaseAdapter.SqlServer2KAdapter.DBStructure.AllInTableNames(childNames);

            if (!string.IsNullOrEmpty(childName))
            {
                sqlFk.Append("and c.table_name in(" + childName + ")");
            }
            if (!string.IsNullOrEmpty(childName))
            {
                sqlPk.Append("and  b.table_name in(" + childName + ")");
            }
            List <TableRelationAttribute> lst = new List <TableRelationAttribute>();

            using (IDataReader reader = info.DefaultOperate.Query(sqlFk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name        = reader["FK_NAME"] as string;
                    tinfo.SourceTable = reader["FKTABLE_NAME"] as string;
                    tinfo.SourceName  = reader["FKCOLUMN_NAME"] as string;
                    tinfo.TargetTable = reader["PKTABLE_NAME"] as string;
                    tinfo.TargetName  = reader["PKCOLUMN_NAME"] as string;
                    tinfo.IsParent    = true;
                    lst.Add(tinfo);
                }
            }
            using (IDataReader reader = info.DefaultOperate.Query(sqlPk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name        = reader["FK_NAME"] as string;
                    tinfo.TargetTable = reader["FKTABLE_NAME"] as string;
                    tinfo.TargetName  = reader["FKCOLUMN_NAME"] as string;
                    tinfo.SourceTable = reader["PKTABLE_NAME"] as string;
                    tinfo.SourceName  = reader["PKCOLUMN_NAME"] as string;
                    tinfo.IsParent    = false;
                    lst.Add(tinfo);
                }
            }
            return(lst);
        }
Esempio n. 11
0
        /// <summary>
        /// 填充关系代码
        /// </summary>
        /// <param name="er"></param>
        /// <param name="sb"></param>
        internal static void FillRelationsInfo(TableRelationAttribute er, StringBuilder sb)
        {
            if (er.IsParent)
            {
                er.FieldTypeName = EntityFieldBase.ToPascalName(er.TargetTable);
                //string name = EntityFieldBase.ToPascalName(er.TargetTable) + "_" + EntityFieldBase.ToPascalName(er.SourceName) + "_" + EntityFieldBase.ToPascalName(er.TargetName);
                string name = EntityFieldBase.ToPascalName(er.SourceName) + "2" + EntityFieldBase.ToPascalName(er.TargetTable);
                er.FieldName    = "_belong" + name;
                er.PropertyName = name;
                er.IsToDB       = true;
                sb.AppendLine("        /// <summary>");
                sb.AppendLine(FormatSummary(er.Description));
                sb.AppendLine("        /// </summary>");
                sb.AppendLine("        protected " + er.FieldTypeName + " " + er.FieldName + ";");
                sb.AppendLine("");

                sb.AppendLine("        /// <summary>");
                sb.AppendLine("        /// " + er.Description);
                sb.AppendLine("        /// </summary>");
                sb.AppendLine("        public virtual " + er.FieldTypeName + " " + er.PropertyName);
                sb.AppendLine("        {");
                sb.AppendLine("            get{ return " + er.FieldName + "; }");
                sb.AppendLine("            set{ " + er.FieldName + " = value; }");
                sb.AppendLine("        }");
            }
            else
            {
                er.FieldTypeName = "List<" + EntityFieldBase.ToPascalName(er.TargetTable) + ">";
                string name = EntityFieldBase.ToPascalName(er.TargetTable) + "2" + EntityFieldBase.ToPascalName(er.TargetName);
                er.FieldName = "_lst" + name;
                //er.PropertyName = "Lst" + name;

                er.PropertyName = name;
                er.IsToDB       = false;
                sb.AppendLine("        /// <summary>");
                sb.AppendLine(FormatSummary(er.Description));
                sb.AppendLine("        /// </summary>");
                sb.AppendLine("        protected " + er.FieldTypeName + " " + er.FieldName + ";");
                sb.AppendLine("");

                sb.AppendLine("        /// <summary>");
                sb.AppendLine("        /// " + er.Description);
                sb.AppendLine("        /// </summary>");
                sb.AppendLine("        public virtual " + er.FieldTypeName + " " + er.PropertyName);
                sb.AppendLine("        {");
                sb.AppendLine("            get{ return " + er.FieldName + "; }");
                sb.AppendLine("            set{ " + er.FieldName + " = value; }");
                sb.AppendLine("        }");
            }
        }
Esempio n. 12
0
        public TableRelationAttribute GetRelationInfo()
        {
            TableRelationAttribute tr = new TableRelationAttribute();

            tr.Description   = Summary;
            tr.FieldName     = FieldName;
            tr.FieldTypeName = FInfo.MemberTypeShortName;
            tr.IsParent      = IsParent;
            tr.PropertyName  = PropertyName;
            tr.SourceName    = SourceProperty;
            tr.SourceTable   = BelongEntity.TableName;
            tr.TargetName    = TargetProperty;
            tr.IsParent      = IsParent;
            return(tr);
        }
Esempio n. 13
0
        /// <summary>
        /// 填充关系
        /// </summary>
        /// <param name="tableInfo">表信息</param>
        /// <param name="entityInfo">实体信息</param>
        private static void FillRelation(KeyWordTableParamItem tableInfo, EntityInfoHandle entityInfo)
        {
            List <TableRelationAttribute> trs = new List <TableRelationAttribute>();

            foreach (EntityMappingInfo info in entityInfo.MappingInfo)
            {
                TableRelationAttribute tableAttr = info.MappingInfo;
                if (!tableAttr.IsParent || tableAttr.IsToDB)
                {
                    continue;
                }
                trs.Add(tableAttr);
            }
            tableInfo.RelationItems = trs;
        }
Esempio n. 14
0
        /// <summary>
        /// 填充特殊信息指定标签
        /// </summary>
        /// <param name="type"></param>
        /// <param name="dicParamsInfo"></param>
        /// <param name="tableAtt"></param>
        private static void FillAttributeInfo(Type type, EntityInfoHandle classInfo)
        {
            object[] atts = type.GetCustomAttributes(true);
            foreach (object objAtt in atts)
            {
                SequenceAttributes satt = objAtt as SequenceAttributes;
                if (satt != null)
                {
                    EntityPropertyInfo info = classInfo.PropertyInfo[satt.PropertyName];
                    if (info == null)
                    {
                        throw new System.MissingMemberException("实体:" + classInfo.EntityType.FullName
                                                                + "  ,不包含属性找不到属性:" + satt.PropertyName);
                    }
                    info.ParamInfo.SequenceName = satt.SequenceName;
                    continue;
                }

                EntityParam ep = objAtt as EntityParam;
                if (ep != null)
                {
                    EntityPropertyInfo info = classInfo.PropertyInfo[ep.PropertyName];
                    if (info == null)
                    {
                        throw new System.MissingMemberException("实体:" + classInfo.EntityType.FullName
                                                                + "  ,不包含属性找不到属性:" + ep.PropertyName);
                    }
                    info.ParamInfo = ep;
                    continue;
                }

                TableRelationAttribute tra = objAtt as TableRelationAttribute;
                if (tra != null)
                {
                    EntityMappingInfo info = classInfo.MappingInfo[tra.PropertyName];
                    if (info == null)
                    {
                        throw new System.MissingMemberException("实体:" + classInfo.EntityType.FullName
                                                                + "  ,不包含属性找不到属性:" + tra.PropertyName);
                    }
                    info.MappingInfo = tra;
                    continue;
                }
            }
        }
Esempio n. 15
0
 /// <summary>
 /// 添加约束
 /// </summary>
 /// <param name="info"></param>
 /// <returns></returns>
 public KeyWordAddForeignkeyItem AddForeignkey(TableRelationAttribute info)
 {
     return(new KeyWordAddForeignkeyItem(info, this));
 }
Esempio n. 16
0
        public List <TableRelationAttribute> GetRelation(DataBaseOperate oper, DBInfo info, IEnumerable <string> childNames)
        {
            string        sql   = "select CONSTNAME,FK_COLNAMES,TABSCHEMA,TABNAME,REFTABSCHEMA, REFTABNAME,REFKEYNAME,PK_COLNAMES from SYSCAT.REFERENCES where 1=1";
            StringBuilder sqlFk = new StringBuilder();
            StringBuilder sqlPk = new StringBuilder();

            sqlFk.Append(sql);
            sqlPk.Append(sql);
            ParamList lstParam  = new ParamList();
            string    childName = Buffalo.DB.DataBaseAdapter.SqlServer2KAdapter.DBStructure.AllInTableNames(childNames);

            if (!string.IsNullOrEmpty(childName))
            {
                sqlFk.Append(" and TABNAME in(" + childName + ")");
            }
            if (!string.IsNullOrEmpty(childName))
            {
                sqlPk.Append(" and REFTABNAME in(" + childName + ")");
            }
            List <TableRelationAttribute> lst = new List <TableRelationAttribute>();

            using (IDataReader reader = info.DefaultOperate.Query(sqlFk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name = (reader["CONSTNAME"] as string);
                    if (!string.IsNullOrEmpty(tinfo.Name))
                    {
                        tinfo.Name = tinfo.Name.Trim();
                    }
                    tinfo.SourceTable = reader["TABNAME"] as string;
                    if (!string.IsNullOrEmpty(tinfo.SourceTable))
                    {
                        tinfo.SourceTable = tinfo.SourceTable.Trim();
                    }
                    tinfo.SourceName = reader["FK_COLNAMES"] as string;
                    if (!string.IsNullOrEmpty(tinfo.SourceName))
                    {
                        tinfo.SourceName = tinfo.SourceName.Trim();
                    }
                    tinfo.TargetTable = reader["REFTABNAME"] as string;
                    if (!string.IsNullOrEmpty(tinfo.TargetTable))
                    {
                        tinfo.TargetTable = tinfo.TargetTable.Trim();
                    }
                    tinfo.TargetName = reader["PK_COLNAMES"] as string;
                    if (!string.IsNullOrEmpty(tinfo.TargetName))
                    {
                        tinfo.TargetName = tinfo.TargetName.Trim();
                    }
                    tinfo.IsParent = true;
                    lst.Add(tinfo);
                }
            }
            using (IDataReader reader = info.DefaultOperate.Query(sqlPk.ToString(), lstParam, null))
            {
                while (reader.Read())
                {
                    TableRelationAttribute tinfo = new TableRelationAttribute();
                    tinfo.Name = (reader["CONSTNAME"] as string);
                    if (!string.IsNullOrEmpty(tinfo.Name))
                    {
                        tinfo.Name = tinfo.Name.Trim();
                    }
                    tinfo.SourceTable = reader["REFTABNAME"] as string;
                    if (!string.IsNullOrEmpty(tinfo.SourceTable))
                    {
                        tinfo.SourceTable = tinfo.SourceTable.Trim();
                    }
                    tinfo.SourceName = reader["PK_COLNAMES"] as string;
                    if (!string.IsNullOrEmpty(tinfo.SourceName))
                    {
                        tinfo.SourceName = tinfo.SourceName.Trim();
                    }
                    tinfo.TargetTable = reader["TABNAME"] as string;
                    if (!string.IsNullOrEmpty(tinfo.TargetTable))
                    {
                        tinfo.TargetTable = tinfo.TargetTable.Trim();
                    }
                    tinfo.TargetName = reader["FK_COLNAMES"] as string;
                    if (!string.IsNullOrEmpty(tinfo.TargetName))
                    {
                        tinfo.TargetName = tinfo.TargetName.Trim();
                    }
                    tinfo.IsParent = false;
                    lst.Add(tinfo);
                }
            }
            return(lst);
        }
Esempio n. 17
0
        /// <summary>
        /// 初始化类型的属性信息
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns>如果已经初始化过侧返回false</returns>
        private static EntityInfoHandle InitEntityPropertyInfos(Type type,
                                                                Dictionary <string, EntityConfigInfo> dicConfigs)
        {
            if (type == null)
            {
                return(null);
            }


            string                fullName      = type.FullName;
            TableAttribute        tableAtt      = new TableAttribute();
            CreateInstanceHandler createrHandle = null;

            //实例化本类型的句柄
            if (!type.IsGenericType)
            {
                createrHandle = FastValueGetSet.GetCreateInstanceHandlerWithOutCache(type);
            }
            Dictionary <string, EntityPropertyInfo> dicPropertys = new Dictionary <string, EntityPropertyInfo>();
            Dictionary <string, EntityMappingInfo>  dicMapping   = new Dictionary <string, EntityMappingInfo>();

            Dictionary <string, EntityParam>            dicParamsInfo   = new Dictionary <string, EntityParam>();
            Dictionary <string, TableRelationAttribute> dicRelationInfo = new Dictionary <string, TableRelationAttribute>();

            FillEntityInfos(dicParamsInfo, dicRelationInfo, type, tableAtt, dicConfigs);
            DBInfo           db        = DataAccessLoader.GetDBInfo(tableAtt.BelongDB);
            IDBAdapter       idb       = db.CurrentDbAdapter;
            EntityInfoHandle classInfo = new EntityInfoHandle(type, createrHandle, tableAtt, db);

            Dictionary <string, bool> dicNotFoundParam    = new Dictionary <string, bool>();
            Dictionary <string, bool> dicNotFoundRelation = new Dictionary <string, bool>();

            FillNotFoundField(dicParamsInfo, dicRelationInfo, dicNotFoundParam, dicNotFoundRelation);

            //属性信息句柄
            List <FieldInfoHandle> lstFields = FieldInfoHandle.GetFieldInfos(type, FastValueGetSet.AllBindingFlags, true);
            DataBaseOperate        oper      = db.DefaultOperate;

            ///读取属性别名
            foreach (FieldInfoHandle finf in lstFields)
            {
                ///通过属性来反射
                EntityParam ep = null;


                if (dicParamsInfo.TryGetValue(finf.FieldName, out ep))
                {
                    //if (tableAtt.IsParamNameUpper)
                    //{
                    //    ep.ParamName = ep.ParamName.ToUpper();
                    //}
                    string proName = ep.PropertyName;
                    //GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(finf);
                    //SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(finf);
                    if (finf.HasGetHandle || finf.HasSetHandle)
                    {
                        PropertyInfo       pinfo          = type.GetProperty(ep.PropertyName, FastValueGetSet.AllBindingFlags);
                        EntityPropertyInfo entityProperty = new EntityPropertyInfo(
                            classInfo, finf.GetHandle, finf.SetHandle, ep, finf.FieldType, finf.FieldName,
                            finf.BelongFieldInfo, pinfo);
                        dicPropertys.Add(proName, entityProperty);
                        dicNotFoundParam.Remove(finf.FieldName);
                    }
                }
                else
                {
                    TableRelationAttribute tableMappingAtt = null;

                    if (dicRelationInfo.TryGetValue(finf.FieldName, out tableMappingAtt))
                    {
                        Type targetType = DefaultType.GetRealValueType(finf.FieldType);
                        tableMappingAtt.SetEntity(type, targetType);
                        //GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(finf);
                        //SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(finf);
                        PropertyInfo      pinfo             = type.GetProperty(tableMappingAtt.PropertyName, FastValueGetSet.AllBindingFlags);
                        EntityMappingInfo entityMappingInfo = new EntityMappingInfo(
                            type, finf.GetHandle, finf.SetHandle, tableMappingAtt,
                            finf.FieldName, finf.FieldType, finf.BelongFieldInfo, pinfo);
                        dicMapping.Add(tableMappingAtt.PropertyName, entityMappingInfo);
                        dicNotFoundRelation.Remove(finf.FieldName);
                    }
                }
            }



            if (dicNotFoundParam.Count > 0 || dicNotFoundRelation.Count > 0)
            {
                StringBuilder message = new StringBuilder();

                foreach (KeyValuePair <string, bool> kvp in dicNotFoundParam)
                {
                    message.Append(kvp.Key + "、");
                }


                foreach (KeyValuePair <string, bool> kvp in dicNotFoundRelation)
                {
                    message.Append(kvp.Key + "、");
                }
                if (message.Length > 0)
                {
                    message.Remove(message.Length - 1, 1);
                }
                message.Insert(0, "类:" + type.FullName + " 找不到字段");
                throw new MissingFieldException(message.ToString());
            }
            classInfo.SetInfoHandles(dicPropertys, dicMapping);
            FillAttributeInfo(type, classInfo);
            _dicClass[fullName] = classInfo;
            return(classInfo);
        }
 /// <summary>
 /// Insert的字段关键字项
 /// </summary>
 /// <param name="paramHandles">字段集合</param>
 /// <param name="previous">上一个关键字</param>
 public KeyWordAddForeignkeyItem(TableRelationAttribute info, BQLQuery previous)
     : base(previous)
 {
     _item = info;
 }
Esempio n. 19
0
        /// <summary>
        /// 填充类信息
        /// </summary>
        /// <param name="dicParam">字段</param>
        /// <param name="dicRelation">关系</param>
        private static void FillEntityInfos(Dictionary <string, EntityParam> dicParam,
                                            Dictionary <string, TableRelationAttribute> dicRelation, Type type,
                                            TableAttribute tableAtt, Dictionary <string, EntityConfigInfo> dicConfigs)
        {
            string key = type.FullName;

            Stack <XmlDocument> stkXml = new Stack <XmlDocument>();//配置栈

            EntityConfigInfo curConfig = null;

            if (!dicConfigs.TryGetValue(key, out curConfig))
            {
                throw new Exception("找不到类:" + key + "所属的配置文件");
            }
            XmlDocument docCur = curConfig.ConfigXML;

            FillEntityInfo(docCur, tableAtt);
            stkXml.Push(docCur);

            Type baseType = type.BaseType;

            List <TableRelationAttribute> lstSetAtt = new List <TableRelationAttribute>();
            List <EntityParam>            lstepAtt  = new List <EntityParam>();

            object[] attRels = type.GetCustomAttributes(false);
            foreach (object objAtt in attRels)
            {
                TableRelationAttribute tr = objAtt as TableRelationAttribute;
                if (tr != null)
                {
                    lstSetAtt.Add(tr);
                    continue;
                }
                EntityParam ep = objAtt as EntityParam;
                if (ep != null)
                {
                    lstepAtt.Add(ep);
                    continue;
                }
            }


            while (baseType != null && !IsSysBaseType(baseType)) //填充父类配置
            {
                EntityConfigInfo config  = null;
                string           baseKey = null;
                if (baseType.IsGenericType)
                {
                    baseKey = baseType.GetGenericTypeDefinition().FullName;
                }
                else
                {
                    baseKey = baseType.FullName;
                }
                if (dicConfigs.TryGetValue(baseKey, out config))
                {
                    stkXml.Push(config.ConfigXML);
                }

                baseType = baseType.BaseType;
            }


            while (stkXml.Count > 0)
            {
                XmlDocument doc = stkXml.Pop();
                //初始化属性
                FillPropertyInfo(doc, dicParam);
                FillRelationInfo(doc, dicRelation);
            }

            foreach (TableRelationAttribute tra in lstSetAtt)
            {
                dicRelation[tra.FieldName] = tra;
            }

            foreach (EntityParam ep in lstepAtt)
            {
                dicParam[ep.FieldName] = ep;
            }
        }