예제 #1
0
        /// <summary>
        /// 生成主键条件
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="pkValues"></param>
        /// <returns></returns>
        internal static WhereClip GetPrimaryKeyWhere <TEntity>(Array pkValues)//params object[] pkValues  2015-08-20
            where TEntity : Entity
        {
            WhereClip where = new WhereClip();
            Field[] keyfields = EntityCache.GetPrimaryKeyFields <TEntity>();

            if (keyfields == null)
            {
                return(where);
            }

            Check.Require(keyfields.Length == pkValues.Length, "主键列与主键值无法对应!");

            int index = keyfields.Length;

            for (int i = 0; i < index; i++)
            {
                where = where.And(new WhereClip(keyfields[i], pkValues.GetValue(i), QueryOperator.Equal));
                //2015-08-20注释
                //where = where.And(new WhereClip(keyfields[i], pkValues[i], QueryOperator.Equal));
                //where = where.And(keyfields[i].In(pkValues));//2015-06-09
            }
            return(where);
        }
예제 #2
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="fileName">字段名</param>
 public Field(string fileName) : base(fileName, EntityCache.GetTableName <T>())
 {
 }
예제 #3
0
        /// <summary>
        /// 创建更新DbCommand
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="fields"></param>
        /// <param name="values"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public DbCommand CreateUpdateCommand <TEntity>(Field[] fields, object[] values, WhereClip where)
            where TEntity : Entity
        {
            Check.Require(!EntityCache.IsReadOnly <TEntity>(), string.Concat("Entity(", EntityCache.GetTableName <TEntity>(), ") is readonly!"));

            if (null == fields || fields.Length == 0 || null == values || values.Length == 0)
            {
                return(null);
            }

            Check.Require(fields.Length == values.Length, "fields.Length must be equal values.Length");

            int length = fields.Length;

            if (WhereClip.IsNullOrEmpty(where))
            {
                where = WhereClip.All;
            }

            StringBuilder sql = new StringBuilder();

            sql.Append("UPDATE ");
            sql.Append(db.DbProvider.BuildTableName(EntityCache.GetTableName <TEntity>()));
            sql.Append(" SET ");

            Field            identityField = EntityCache.GetIdentityField <TEntity>();
            bool             identityExist = !Field.IsNullOrEmpty(identityField);
            List <Parameter> list          = new List <Parameter>();
            StringBuilder    colums        = new StringBuilder();

            for (int i = 0; i < length; i++)
            {
                if (identityExist)
                {
                    //标识列  排除
                    if (fields[i].PropertyName.Equals(identityField.PropertyName))
                    {
                        continue;
                    }
                }

                colums.Append(",");
                colums.Append(fields[i].FieldName);
                colums.Append("=");

                if (values[i] is Expression)
                {
                    Expression expression = (Expression)values[i];
                    colums.Append(expression.ToString());
                    list.AddRange(expression.Parameters);
                }
                else if (values[i] is Field)
                {
                    Field fieldValue = (Field)values[i];
                    colums.Append(fieldValue.TableFieldName);
                }
                else
                {
                    string pname = DataUtils.MakeUniqueKey(fields[i]);
                    //string pname = fields[i].tableName + fields[i].Name + i;
                    colums.Append(pname);
                    Parameter p = new Parameter(pname, values[i], fields[i].ParameterDbType, fields[i].ParameterSize);
                    list.Add(p);
                }
            }
            sql.Append(colums.ToString().Substring(1));
            sql.Append(where.WhereString);
            list.AddRange(where.Parameters);

            DbCommand cmd = db.GetSqlStringCommand(sql.ToString());

            db.AddCommandParameter(cmd, list.ToArray());
            return(cmd);
        }
예제 #4
0
        /// <summary>
        /// 创建添加DbCommand
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="fields"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public DbCommand CreateInsertCommand <TEntity>(Field[] fields, object[] values)
            where TEntity : Entity
        {
            Check.Require(!EntityCache.IsReadOnly <TEntity>(), string.Concat("Entity(", EntityCache.GetTableName <TEntity>(), ") is readonly!"));

            if (null == fields || fields.Length == 0 || null == values || values.Length == 0)
            {
                return(null);
            }

            StringBuilder sql = new StringBuilder();

            sql.Append("INSERT INTO ");
            sql.Append(db.DbProvider.BuildTableName(EntityCache.GetTableName <TEntity>()));
            sql.Append(" (");

            Field identityField = EntityCache.GetIdentityField <TEntity>();
            bool  identityExist = !Field.IsNullOrEmpty(identityField);
            bool  isSequence    = false;

            if (db.DbProvider is Dos.ORM.Oracle.OracleProvider)
            {
                if (!string.IsNullOrEmpty(EntityCache.GetSequence <TEntity>()))
                {
                    isSequence = true;
                }
            }

            Dictionary <string, string> insertFields = new Dictionary <string, string>();
            List <Parameter>            parameters   = new List <Parameter>();

            int length = fields.Length;

            for (int i = 0; i < length; i++)
            {
                if (identityExist)
                {
                    if (fields[i].PropertyName.Equals(identityField.PropertyName))
                    {
                        if (isSequence)
                        {
                            insertFields.Add(fields[i].FieldName, string.Concat(EntityCache.GetSequence <TEntity>(), ".nextval"));
                        }

                        continue;
                    }
                }

                string panme = DataUtils.MakeUniqueKey(fields[i]);
                //string panme = fields[i].tableName + fields[i].Name + i;
                insertFields.Add(fields[i].FieldName, panme);
                Parameter p = new Parameter(panme, values[i], fields[i].ParameterDbType, fields[i].ParameterSize);
                parameters.Add(p);
            }
            StringBuilder fs = new StringBuilder();
            StringBuilder ps = new StringBuilder();

            foreach (KeyValuePair <string, string> kv in insertFields)
            {
                fs.Append(",");
                fs.Append(kv.Key);

                ps.Append(",");
                ps.Append(kv.Value);
            }

            sql.Append(fs.ToString().Substring(1));
            sql.Append(") VALUES (");
            sql.Append(ps.ToString().Substring(1));
            sql.Append(")");

            DbCommand cmd = db.GetSqlStringCommand(sql.ToString());

            db.AddCommandParameter(cmd, parameters.ToArray());
            return(cmd);
        }
예제 #5
0
 /// <summary>
 /// 创建删除DbCommand
 /// </summary>
 /// <param name="where"></param>
 /// <returns></returns>
 public DbCommand CreateDeleteCommand <TEntity>(WhereClip where)
     where TEntity : Entity
 {
     return(CreateDeleteCommand(EntityCache.GetTableName <TEntity>(), where));
 }
예제 #6
0
        /// <summary>
        ///  删除
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        public void Delete <TEntity>(WhereClip where)
            where TEntity : Entity
        {
            Check.Require(!EntityCache.IsReadOnly <TEntity>(), string.Concat("Entity(", EntityCache.GetTableName <TEntity>(), ") is readonly!"));

            batchcmd.Process(cmdCreator.CreateDeleteCommand(EntityCache.GetTableName <TEntity>(), where));
        }
예제 #7
0
        /// <summary>
        ///  删除
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="pkValues"></param>
        /// <returns></returns>
        public void Delete <TEntity>(params Guid[] pkValues)
            where TEntity : Entity
        {
            Check.Require(!EntityCache.IsReadOnly <TEntity>(), string.Concat("Entity(", EntityCache.GetTableName <TEntity>(), ") is readonly!"));

            batchcmd.Process(cmdCreator.CreateDeleteCommand(EntityCache.GetTableName <TEntity>(), DataUtils.GetPrimaryKeyWhere <TEntity>(pkValues)));
        }
예제 #8
0
        /// <summary>
        ///  删除
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public void Delete <TEntity>(TEntity entity)
            where TEntity : Entity
        {
            Check.Require(!EntityCache.IsReadOnly <TEntity>(), string.Concat("Entity(", EntityCache.GetTableName <TEntity>(), ") is readonly!"));

            WhereClip where = DataUtils.GetPrimaryKeyWhere(entity);

            Check.Require(!WhereClip.IsNullOrEmpty(where), "entity must have the primarykey!");
            Delete <TEntity>(where);
            //2015-08-20注释
            //Delete<TEntity>(entity, where);
        }