Пример #1
0
        protected override void BuildCommand()
        {
            BeeDataAdapter columnDataAdapter = new BeeDataAdapter();

            columnDataAdapter.Merge(this.dataAdapter, true);

            // 移除不存在的列名 及 计算列
            TableSchema tableSchema = owner.GetTableSchema(tableName);

            ThrowExceptionUtil.ArgumentConditionTrue(tableSchema != null, "tableName", "can not find table. Name:{0}".FormatWith(tableName));

            List <string> needToRemovedList = new List <string>();

            foreach (string key in columnDataAdapter.Keys)
            {
                ColumnSchema columnSchema = tableSchema.GetColumn(key);

                if (columnSchema == null)
                {
                    needToRemovedList.Add(key);
                }
                else if (columnSchema.IsComputeField)
                {
                    needToRemovedList.Add(key);
                }
                else
                {
                    // do nothing here.
                }
            }

            if (tableSchema.IdentityColumn != null)
            {
                needToRemovedList.Add(tableSchema.IdentityColumn.ColumnName);
            }

            foreach (string key in needToRemovedList)
            {
                columnDataAdapter.RemoveKey(key);
            }

            DataAdapterParser dataAdapterParser = new DataAdapterParser(owner, columnDataAdapter);

            base.dbCommand.Parameters.AddRange(dataAdapterParser.DbParameterList.ToArray());

            string whereClause = string.Empty;

            if (sqlCriteria != null)
            {
                this.sqlCriteria.Owner = owner;
                whereClause            = string.Format("where {0}", sqlCriteria.WhereClause);

                base.dbCommand.Parameters.AddRange(sqlCriteria.DbParameters.ToArray());
            }

            if (owner.DbDriver.GetType() == typeof(OracleDriver))
            {
                string sqlText = string.Format("update {0} set {1} {2}",
                                               owner.DbDriver.FormatField(this.tableName), dataAdapterParser.UpdateClause, whereClause);

                sqlText = sqlText.Replace(Environment.NewLine, " ");
                base.dbCommand.CommandText = sqlText;
            }
            else
            {
                base.dbCommand.CommandText = string.Format("update {0} \r\nset {1} \r\n{2}",
                                                           owner.DbDriver.FormatField(this.tableName), dataAdapterParser.UpdateClause, whereClause);
            }
        }
Пример #2
0
        protected override void BuildCommand()
        {
            base.dbCommand.CommandType = CommandType.Text;

            BeeDataAdapter columnDataAdapter = new BeeDataAdapter();

            columnDataAdapter.Merge(this.dataAdapter, true);

            // 移除不存在的列名
            TableSchema tableSchema = owner.GetTableSchema(tableName);

            ThrowExceptionUtil.ArgumentConditionTrue(tableSchema != null, "tableName", "can not find table. Name:{0}".FormatWith(tableName));

            List <string> needToRemovedList = new List <string>();

            foreach (string key in columnDataAdapter.Keys)
            {
                ColumnSchema columnSchema = tableSchema.GetColumn(key);

                if (columnSchema == null)
                {
                    needToRemovedList.Add(key);
                }
                else if (columnSchema.IsComputeField)
                {
                    needToRemovedList.Add(key);
                }
                else
                {
                    // do nothing here.
                }
            }

            foreach (string key in needToRemovedList)
            {
                columnDataAdapter.RemoveKey(key);
            }

            DataAdapterParser dataAdapterParser = new DataAdapterParser(owner, columnDataAdapter);

            base.dbCommand.CommandText = string.Format(@" 
insert into {0} 
({1})
values
({2});"
                                                       , owner.DbDriver.FormatField(this.tableName)
                                                       , dataAdapterParser.ColumnClause, dataAdapterParser.ParameterClause);

            if (owner.DbServerType == DBServerType.Oracle)
            {
                string sqlText = string.Format(@"begin
    {0}
                    end;", base.dbCommand.CommandText);

                sqlText = sqlText.Replace(Environment.NewLine, " ");
                base.dbCommand.CommandText = sqlText;
            }
            else if (owner.DbServerType == DBServerType.Pgsql)
            {
                if (!string.IsNullOrEmpty(identityColumnName))
                {
                    string identitySelectString = string.Format(@"; SELECT {0} ", owner.DbDriver.IdentitySelectString);
                    identitySelectString = string.Format(identitySelectString, tableName).ToLower();

                    base.dbCommand.CommandText = base.dbCommand.CommandText + identitySelectString;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(identityColumnName))
                {
                    string identitySelectString = string.Format(@" SELECT {0}", owner.DbDriver.IdentitySelectString);

                    base.dbCommand.CommandText = base.dbCommand.CommandText + identitySelectString;
                }
            }

            base.dbCommand.Parameters.AddRange(dataAdapterParser.DbParameterList.ToArray());
        }