Exemplo n.º 1
0
        /// <summary>
        /// 实体生成删除语句
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="primaryKeyName">主键名称</param>
        /// <param name="primaryKeyValue">主键值</param>
        private void Delete(string tableName, string primaryKeyName, object primaryKeyValue)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException("参数 tableName 不能为null");
            }
            if (string.IsNullOrEmpty(primaryKeyName))
            {
                throw new ArgumentNullException("参数 primaryKeyName 不能为null");
            }
            if (primaryKeyValue == null)
            {
                throw new ArgumentNullException("参数 primaryKeyValue 不能为null");
            }

            // Do it
            Sql = new Sql(string.Format("DELETE FROM {0} WHERE {1}=@0", RuleProvider.Rule(_dbType).EscapeTableName(tableName), RuleProvider.Rule(_dbType).EscapeSqlIdentifier(primaryKeyName)), primaryKeyValue);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 实体生成插入语句
        /// </summary>
        /// <param name="poco">poco实体</param>
        public void Insert(object poco)
        {
            try
            {
                if (poco == null)
                {
                    throw new Exception("输入参数 poco 不能为空!");
                }

                Type type   = poco.GetType();
                var  names  = new List <string>();
                var  values = new List <string>();
                var  paras  = new List <object>();
                var  index  = 0;
                foreach (PropertyInfo pi in type.Properties())
                {
                    // Don't insert result columns
                    if (pi.GetCustomAttributes(typeof(ResultColumnAttribute), false).Length > 0)
                    {
                        continue;
                    }

                    names.Add(RuleProvider.Rule(_dbType).EscapeSqlIdentifier(pi.Name));
                    values.Add(string.Format("{0}{1}", "@", index++));
                    paras.Add(poco.GetPropertyValue(pi.Name));
                }

                string outputClause = String.Empty;

                Sql = new Sql(string.Format("INSERT INTO {0} ({1}){2} VALUES ({3})",
                                            RuleProvider.Rule(_dbType).EscapeTableName(type.Name),
                                            string.Join(",", names.ToArray()),
                                            outputClause,
                                            string.Join(",", values.ToArray())
                                            ), paras.ToArray());
            }
            catch (Exception x)
            {
                throw x;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 实体生成更新语句
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="primaryKeyName">主键名称</param>
        /// <param name="poco">poco实体</param>
        /// <param name="primaryKeyValue">主键值</param>
        /// <param name="columns">更新的列名</param>
        private void ExecuteUpdate(string tableName, string primaryKeyName, object poco, object primaryKeyValue, params string[] columns)
        {
            try
            {
                var sb    = new StringBuilder();
                var index = 0;
                var paras = new List <object>();
                if (columns == null || columns.Length < 1)
                {
                    Type type = poco.GetType();
                    foreach (PropertyInfo pi in type.Properties())
                    {
                        // Don't update result columns
                        if (pi.GetCustomAttributes(typeof(ResultColumnAttribute), false).Length > 0)
                        {
                            continue;
                        }

                        // Don't update the primary key, but grab the value if we don't have it
                        if (string.Compare(pi.Name, primaryKeyName, true) == 0)
                        {
                            if (primaryKeyValue == null)
                            {
                                primaryKeyValue = poco.GetPropertyValue(pi.Name);
                            }
                            continue;
                        }

                        // Build the sql
                        if (index > 0)
                        {
                            sb.Append(", ");
                        }
                        sb.AppendFormat("{0} = {1}{2}", RuleProvider.Rule(_dbType).EscapeSqlIdentifier(pi.Name), "@", index++);

                        // Store the parameter in the command
                        paras.Add(poco.GetPropertyValue(pi.Name));
                    }
                }
                else
                {
                    foreach (var colname in columns)
                    {
                        // Build the sql
                        if (index > 0)
                        {
                            sb.Append(", ");
                        }
                        sb.AppendFormat("{0} = {1}{2}", RuleProvider.Rule(_dbType).EscapeSqlIdentifier(colname), "@", index++);

                        // Store the parameter in the command
                        paras.Add(poco.GetPropertyValue(colname));
                    }

                    // Grab primary key value
                    if (primaryKeyValue == null)
                    {
                        primaryKeyValue = poco.GetPropertyValue(primaryKeyName);
                    }
                }

                //添加主键参数
                paras.Add(primaryKeyValue);

                Sql = new Sql(string.Format("UPDATE {0} SET {1} WHERE {2} = {3}{4}",
                                            RuleProvider.Rule(_dbType).EscapeTableName(tableName), sb.ToString(), RuleProvider.Rule(_dbType).EscapeSqlIdentifier(primaryKeyName), "@", index++), paras.ToArray());
            }
            catch (Exception x)
            {
                throw x;
            }
        }