예제 #1
0
        private void SetObject(object ob, DataRow Row, PropertyInfo[] properties, bool GetDetail = false)
        {
            MSSQL.MSSQLScriptSqlPatternFactory patternFactory = new MSSQL.MSSQLScriptSqlPatternFactory();

            if (ob is Soway.Data.IObjectFromDataBase)
            {
                (ob as Soway.Data.IObjectFromDataBase).Row = Row;
            }
            ORMHelper helper = new ORMHelper();

            for (int j = 0; j < properties.Length; j++)
            {
                try
                {
                    var attribute = helper.GetColNameAttributes(properties[j]);


                    object propertyob   = new object();
                    var    isBO         = new ORMHelper().IsBusinessType(properties[j].PropertyType);
                    var    propertyType = properties[j].PropertyType;



                    for (int i = 0; i < attribute.Count; i++)
                    {
                        var attr = attribute[i];
                        if (attr.NoMap)
                        {
                            continue;
                        }
                        String ColName = helper.GetColName(attr, properties[j]);
                        if (String.IsNullOrEmpty(ColName))
                        {
                            continue;
                        }

                        if (Row.Table.Columns.Contains(ColName))
                        {
                            var dbObject = Row[ColName];



                            if ((dbObject is DBNull))
                            {
                                continue;
                            }
                            if (propertyType == typeof(DateTime) && String.IsNullOrEmpty(attr.FormatStr) == false && dbObject != null)
                            {
                                dbObject = DateTime.ParseExact(dbObject.ToString(), attr.FormatStr, null);
                            }
                            if (attribute.Count == 1)
                            {
                                if (isBO)
                                {
                                    if (GetDetail)
                                    {
                                        if (!(dbObject is DBNull))
                                        {
                                            properties[j].SetValue(ob, DBContext.GetMethod.MakeGenericMethod(
                                                                       new Type[] { properties[j].PropertyType, properties[j].PropertyType }).Invoke(
                                                                       this, new object[] { Row[ColName] }), new object[] { });
                                        }
                                    }
                                    else
                                    {
                                        propertyob = propertyType.Assembly.CreateInstance(propertyType.FullName);
                                        properties[j].SetValue(ob, propertyob
                                                               , new object[] { });

                                        PropertyInfo key;
                                        if (String.IsNullOrEmpty(attr.PropertyName))
                                        {
                                            key = new ORMHelper().GetKeyPropertyInfo(propertyType);
                                        }
                                        else
                                        {
                                            key = propertyType.GetProperty(attr.PropertyName);
                                        }
                                        if (!(dbObject is DBNull))
                                        {
                                            key.SetValue(propertyob, dbObject, new object[] { });
                                        }
                                    }
                                }
                                else
                                if (String.IsNullOrEmpty(attr.PropertyName))
                                {
                                    if (!(dbObject is DBNull))
                                    {
                                        properties[j].SetValue(ob, dbObject is DBNull ? null : dbObject, new object[] { });
                                    }
                                }
                                else if (GetDetail && isBO)
                                {
                                    if (!(dbObject is DBNull))
                                    {
                                        properties[j].SetValue(ob, DBContext.GetMethod.MakeGenericMethod(new Type[] { properties[j].PropertyType, properties[j].PropertyType }).Invoke(
                                                                   new Soway.DB.DBContext(), new object[] { Row[ColName] }), new object[] { });
                                    }
                                }
                            }
                            else
                            {
                                if (i == 0)//{
                                {
                                    propertyob = propertyType.Assembly.CreateInstance(propertyType.FullName);
                                    properties[j].SetValue(ob, propertyob, new object[] { });
                                }

                                if (String.IsNullOrEmpty(attr.PropertyName) == false)
                                {
                                    if (!(dbObject is DBNull))
                                    {
                                        if (propertyType.GetProperty(attr.PropertyName).PropertyType
                                            == typeof(DateTime) && String.IsNullOrEmpty(attr.FormatStr) == false && dbObject != null)
                                        {
                                            dbObject = DateTime.ParseExact(dbObject.ToString(), attr.FormatStr, null);
                                        }
                                        propertyType.GetProperty(attr.PropertyName).SetValue(propertyob, dbObject, new object[] { });
                                    }
                                }

                                //  }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Properties"></param>
        /// <param name="ob"></param>
        /// <param name="IsIncludeAutoGerateCols">在查询时是否包含自动生成列</param>
        /// <param name="ParamNamePre"></param>
        /// <returns></returns>
        public SqlCommandPaterns  GetExistSql <T>(System.Reflection.PropertyInfo[] Properties, T ob,
                                                  bool IsIncludeAutoGerateCols = true, String ParamNamePre = null)
        {
            SqlCommandPaterns pataerns = new SqlCommandPaterns();

            //如果有主键,则生成键查找的条件,如果没有,则生成所有列的查询
            Hashtable hashTable = new Hashtable();

            hashTable.Add(0, ""); //
            String ColName       = "";
            String parameterName = "";
            var    helper        = new ORMHelper();

            for (int i = 0; i < Properties.Length; i++)
            {
                var    attribute = helper.GetColNameAttributes(Properties[i]);
                object keyName   = 0;
                ColName = Properties[i].Name;
                foreach (var Col in attribute)
                {
                    if (Col.NoMap)
                    {
                        continue;
                    }

                    ColName = helper.GetColName(Col, Properties[i]);


                    parameterName = "@"
                                    + ParamNamePre + ColName;

                    if (IsIncludeAutoGerateCols == false && (Col.IsAutoGenerate != GenerationType.Never))
                    {
                        continue;
                    }
                    if (Col.IsKey)
                    {
                        keyName = Col.KeyGroupName;
                        keyName = keyName ?? 1;
                        if (!hashTable.ContainsKey(keyName))
                        {
                            //如果不含主键组,则加入
                            hashTable.Add(keyName, "");
                        }
                        if (hashTable.ContainsKey(0))
                        {
                            //有主键了就不需要对所有的值进行判断了
                            hashTable.Remove(0);
                        }
                    }
                    else
                    {
                        keyName = 0;
                    }

                    if (hashTable.ContainsKey(keyName))
                    {
                        var dbValue = new ORMHelper().GetDbObject(Properties[i], ob);
                        if (dbValue != null)
                        {
                            pataerns.Params.Add(new System.Data.SqlClient.SqlParameter(parameterName,
                                                                                       dbValue));
                            string exp = "[" + ColName + "]=" + parameterName;
                            hashTable[keyName] = hashTable[keyName].ToString() + " AND [" + (ColName) + "]=" + parameterName;
                        }
                    }
                }
            }
            foreach (var item in hashTable.Values)
            {
                pataerns.SqlScript += "OR (" + item.ToString().Substring(4) + ")";
            }
            pataerns.SqlScript = pataerns.SqlScript.Substring(3);
            return(pataerns);
        }
예제 #3
0
        public System.Data.IDbCommand GenerateSelectItemsSql <T>(object ob, PropertyInfo ParentInfo)
        {
            //初始化一个Command
            System.Data.SqlClient.SqlCommand command = null;
            var       TargetType = typeof(T);
            var       ParentType = ob.GetType();
            ORMHelper helper     = new ORMHelper();

            command = new System.Data.SqlClient.SqlCommand();
            String TableName = helper.GetTable(TargetType);

            var Properties = TargetType.GetProperties();


            String Cols = "";


            String       WhereSql   = "";
            PropertyInfo ParentKey  = null;
            String       ColumnName = "";


            var parentAttribute = ParentInfo.GetCustomAttributes(typeof(ParentRelationAttribute), true);

            if (parentAttribute.Length > 0)
            {
                ColumnName = (parentAttribute[0] as ParentRelationAttribute).ColumnName;
                //  ParentKey = ParentType.GetProperty(parentInfo.PropertyName);
            }
            else
            {
                var columAttribute = ParentInfo.GetCustomAttributes(typeof(ColumnAttribute), true);

                if (columAttribute.Length > 0)
                {
                    ColumnName = (columAttribute[0] as ColumnAttribute).ColumnName;
                }
            }


            ParentKey = new ORMHelper().GetKeyPropertyInfo(ParentInfo.PropertyType);
            if (ParentKey != null)
            {
                command.Parameters.Add(new SqlParameter("@" + ColumnName,
                                                        ParentKey.GetValue(ob, null) ?? ""));
                WhereSql = "where  [" + ColumnName + "]=@" + ColumnName
                ;
            }

            for (int i = 0; i < Properties.Length; i++)
            {
                if (Properties[i] != ParentInfo)
                {
                    foreach (var Col in helper.GetColNameAttributes(Properties[i]))
                    {
                        String paramName = "@" + helper.GetColName(Col, Properties[i]);
                        string ColName   = helper.GetColName(Col, Properties[i]);

                        if (Col.NoMap)
                        {
                            continue;
                        }
                        ColName = Col.ColumnName;

                        if (String.IsNullOrEmpty(ColName))
                        {
                            ColName = Properties[i].Name;
                        }
                        Cols += TableName + ".[" + ColName + "],";
                        //   }
                    }
                }
            }

            command.CommandText = "SELECT " + Cols.Substring(0, Cols.Length - 1) + " FROM " + TableName + WhereSql;   //+ "(" +

            return(command);
        }
예제 #4
0
        public System.Data.IDbCommand GenerateDynamicUpateSql <T>(T ob)
        {
            //初始化一个Command
            System.Data.SqlClient.SqlCommand command = null;
            var TargetType     = typeof(T);
            var patternfactory = new MSSQLScriptSqlPatternFactory();
            var helper         = new ORMHelper();

            command = new System.Data.SqlClient.SqlCommand();
            String TableName  = helper.GetTable(TargetType);
            var    Properties = TargetType.GetProperties();
            String Cols       = "";

            String update = "SELECT ";

            String WhereSql = "";

            for (int i = 0; i < Properties.Length; i++)
            {
                var attribute = Properties[i].GetCustomAttributes(typeof(DynamicColumnAttribute), true);
                if (attribute.Length > 0)
                {
                    var dynamicAttr = attribute[0] as DynamicColumnAttribute;
                    var proerpty    = TargetType.GetProperty(dynamicAttr.SourcePropertyName);
                    if (proerpty != null)
                    {
                        var colattributes = helper.GetColNameAttributes(Properties[i]);

                        if (colattributes.Count > 0)
                        {
                            if (colattributes[0].NoMap)
                            {
                                continue;
                            }
                            else
                            {
                                string ColName   = helper.GetColName(colattributes[0], Properties[i]);
                                String paramName = "@" + dynamicAttr.SourcePropertyName;
                                ColName = TableName + ".[" + ColName + "]";
                                command.Parameters.Add(paramName, proerpty.GetValue(ob, new object[] {}));
                                Cols += ColName + "=" + ColName + (dynamicAttr.Operation == DanymicOperationType.Add?"+":"-") + paramName + ",";
                            }
                        }
                    }
                }
                else
                {
                    var cols = helper.GetColNameAttributes(Properties[i]);
                    if (cols.Count > 0)
                    {
                        foreach (var col in cols)
                        {
                            if (col.IsKey == true && String.IsNullOrEmpty(col.KeyGroupName))
                            {
                                var colname   = helper.GetColName(col, Properties[i]);
                                var paramName = "@Pre" + colname;
                                WhereSql = " WHERE " + TableName + ".[" + colname + "]=" + paramName;
                                command.Parameters.Add(paramName, Properties[i].GetValue(ob, new object[] {}));
                            }
                        }
                    }
                }
            }

            command.CommandText = "UPDATE " + TableName + " SET " + Cols.Substring(0, Cols.Length - 1) + WhereSql;
            //selectedCommand.Add(TargetType, command);
            return(command);
        }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ob"></param>
        /// <param name="cxt"></param>
        /// <param name="TargetType"></param>
        /// <returns></returns>
        /// <remarks>
        /// 2013.8.7
        /// 将MSSQLScriptSqlPatternFactory改为可以得到列信息和名称信息
        /// </remarks>
        private System.Data.IDbCommand GenerateCreateSql(object ob, IDBContext cxt, Type TargetType)
        {
            System.Data.SqlClient.SqlCommand command = null;

            var patternFactory = new MSSQLScriptSqlPatternFactory();

            if (insertCommand.ContainsKey(TargetType) == false)
            {
                //得到表名
                command = new System.Data.SqlClient.SqlCommand();
                String TableName  = helper.GetTable(TargetType);
                var    Properties = TargetType.GetProperties();
                String Cols       = "";
                String Values     = "";
                for (int i = 0; i < Properties.Length; i++)
                {
                    if (Properties[i].PropertyType.IsArray)
                    {
                        continue;
                    }
                    var    attribute = helper.GetColNameAttributes(Properties[i]);
                    String paramName = "@" + Properties[i].Name;
                    string ColName   = "";
                    if (attribute.Count > 0)
                    {
                        for (int k = 0; k < attribute.Count; k++)
                        {
                            var Col = attribute[k];
                            if (Col.NoMap == true)
                            {
                                continue;
                            }
                            else if (Col.IsAutoGenerate == GenerationType.OnInSert)
                            {
                                var auto = Properties[i].GetCustomAttributes(typeof(SerialNoObject), true);
                                if (auto.Length > 0)
                                {
                                    var seriSetting = auto[0] as SerialNoObject;
                                    Properties[i].SetValue(ob,
                                                           cxt.GetSerialNo(seriSetting.SerialPre, seriSetting.Len, seriSetting.DateFormateStr, ""), null);
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            ColName   = helper.GetColName(Col, Properties[i]);
                            paramName = "@" + ColName;
                            var valueObject = new ORMHelper().GetDbObject(Properties[i], ob, Col.PropertyName);
                            if (valueObject is DateTime && valueObject != null && String.IsNullOrEmpty(Col.FormatStr) == false)
                            {
                                valueObject = ((DateTime)valueObject).ToString(Col.FormatStr);
                            }
                            Cols   += "[" + ColName + "],";
                            Values += paramName + ",";
                            command.Parameters.Add(new System.Data.SqlClient.SqlParameter(paramName, valueObject ?? ""));
                        }
                    }
                }
                command.CommandText = "INSERT " + TableName + "(" + Cols.Substring(0, Cols.Length - 1) + ") VALUES(" + Values.Substring(0, Values.Length - 1) + ")";
                insertCommand.Add(TargetType, command);
                //return command;
            }

            command =
                (insertCommand[TargetType] as System.Data.SqlClient.SqlCommand).Clone();
            SetValues(ob, command, TargetType, "", true, cxt);
            return(command);
        }