Ejemplo n.º 1
0
        private IDbCommand BuildUpdateCommand(DataTableMapping mappings, DataRow dataRow)
        {
            if (IsEmpty(this.quotedBaseTableName))
            {
                return(null);
            }
            IDbCommand    command  = this.BuildNewCommand(this.updateCommand);
            int           pcount   = 1;
            int           toupdate = 0;
            int           index    = 0;
            int           filter   = 0;
            StringBuilder sb       = new StringBuilder();

            sb.Append("UPDATE ");
            sb.Append(this.QuotedBaseTableName);
            sb.Append(" SET ");
            int rows = this.dbSchemaRows.Length;

            for (int i = 0; i < rows; i++)
            {
                DataRow row = this.dbSchemaRows[i];
                if (((row != null) && (row["BaseColumnName"].ToString().Length != 0)) && !this.ExcludeFromUpdateSet(row))
                {
                    toupdate++;
                    object value = null;
                    string name  = this.sourceColumnNames[i];
                    if ((mappings != null) && (dataRow != null))
                    {
                        value = this.GetParameterUpdateValue(name, mappings, dataRow, (bool)row["IsReadOnly"]);
                        if (value == null)
                        {
                            if ((bool)row["IsReadOnly"])
                            {
                                toupdate--;
                            }

                            continue;
                        }
                    }
                    if (0 < index)
                    {
                        sb.Append(" , ");
                    }
                    sb.Append(this.QuotedColumn(row["BaseColumnName"].ToString()));
                    this.AppendParameterText(sb, pcount);
                    IDataParameter parameter = CommandBuilder.GetNextParameter(command, index);
                    parameter.ParameterName = "@p" + pcount.ToString();
                    parameter.Direction     = ParameterDirection.Input;
                    parameter.SourceColumn  = name;
                    parameter.SourceVersion = DataRowVersion.Current;
                    parameter.Value         = value;
                    if (parameter is SharpHsqlParameter)
                    {
                        this.ApplyParameterInfo((SharpHsqlParameter)parameter, pcount, row);
                    }
                    if (!command.Parameters.Contains(parameter))
                    {
                        command.Parameters.Add(parameter);
                    }
                    pcount++;
                    index++;
                }
            }
            filter = index;
            sb.Append(" WHERE ( ");
            string sql = "";

            int where = 0;
            string tname   = null;
            string colname = null;

            for (int i = 0; i < rows; i++)
            {
                DataRow row = this.dbSchemaRows[i];
                if (((row != null) && (row["BaseColumnName"].ToString().Length != 0)) && this.IncludeForUpdateWhereClause(row))
                {
                    sb.Append(sql);
                    sql = " AND ";
                    object value = null;
                    string name  = this.sourceColumnNames[i];
                    if ((mappings != null) && (dataRow != null))
                    {
                        value = this.GetParameterValue(name, mappings, dataRow, DataRowVersion.Original);
                    }
                    bool   ispk    = this.IsPKey(row);
                    string basecol = this.QuotedColumn(row["BaseColumnName"].ToString());
                    if (ispk)
                    {
                        if (Convert.IsDBNull(value))
                        {
                            sb.Append(string.Format("({0} IS NULL)", basecol));
                        }
                        else if (this.namedParameters)
                        {
                            sb.Append(string.Format("({0} = @p{1})", basecol, pcount));
                        }
                        else
                        {
                            sb.Append(string.Format("({0} = ?)", basecol));
                        }
                    }
                    else if (this.namedParameters)
                    {
                        sb.Append(string.Format("((@p{1} = 1 AND {0} IS NULL) OR ({0} = @p{2}))", basecol, pcount, 1 + pcount));
                    }
                    else
                    {
                        sb.Append(string.Format("((? = 1 AND {0} IS NULL) OR ({0} = ?))", basecol));
                    }
                    if (!ispk || !Convert.IsDBNull(value))
                    {
                        IDataParameter parameter = CommandBuilder.GetNextParameter(command, index);
                        parameter.ParameterName = "@p" + pcount.ToString();
                        parameter.Direction     = ParameterDirection.Input;
                        if (ispk)
                        {
                            parameter.SourceColumn  = name;
                            parameter.SourceVersion = DataRowVersion.Original;
                            parameter.Value         = value;
                        }
                        else
                        {
                            parameter.SourceColumn = null;
                            parameter.Value        = IsNull(value) ? 1 : 0;
                        }
                        pcount++;
                        index++;
                        if (parameter is SharpHsqlParameter)
                        {
                            this.ApplyParameterInfo((SharpHsqlParameter)parameter, pcount, row);
                        }
                        if (!ispk)
                        {
                            parameter.DbType = DbType.Int32;
                        }
                        if (!command.Parameters.Contains(parameter))
                        {
                            command.Parameters.Add(parameter);
                        }
                    }
                    if (!ispk)
                    {
                        IDataParameter parameter = CommandBuilder.GetNextParameter(command, index);
                        parameter.ParameterName = "@p" + pcount.ToString();
                        parameter.Direction     = ParameterDirection.Input;
                        parameter.SourceColumn  = name;
                        parameter.SourceVersion = DataRowVersion.Original;
                        parameter.Value         = value;
                        pcount++;
                        index++;
                        if (parameter is SharpHsqlParameter)
                        {
                            this.ApplyParameterInfo((SharpHsqlParameter)parameter, pcount, row);
                        }
                        if (!command.Parameters.Contains(parameter))
                        {
                            command.Parameters.Add(parameter);
                        }
                    }
                    if (this.IncrementUpdateWhereCount(row))
                    {
                        where++;
                    }
                }
            }
            sb.Append(" )");
            command.CommandText = sb.ToString();
            CommandBuilder.RemoveExtraParameters(command, index);
            this.updateCommand = command;
            if (toupdate == 0)
            {
                throw new InvalidOperationException("Dynamic SQL is read only.");
            }
            if (filter == 0)
            {
                command = null;
            }
            if (where == 0)
            {
                throw new InvalidOperationException("Dynamic SQL has no primary key information to perform an update.");
            }
            if (tname != null)
            {
                DataColumn col = this.GetParameterDataColumn(colname, mappings, dataRow);
                throw new InvalidOperationException("Where Clause Unspecified Value");
            }
            return(command);
        }
Ejemplo n.º 2
0
        private IDbCommand BuildInsertCommand(DataTableMapping mappings, DataRow dataRow)
        {
            if (IsEmpty(this.quotedBaseTableName))
            {
                return(null);
            }
            IDbCommand    command = this.BuildNewCommand(this.insertCommand);
            int           pcount  = 0;
            int           idx     = 1;
            StringBuilder sb      = new StringBuilder();

            sb.Append("INSERT INTO ");
            sb.Append(this.QuotedBaseTableName);
            int rows = this.dbSchemaRows.Length;

            for (int i = 0; i < rows; i++)
            {
                DataRow row = this.dbSchemaRows[i];
                if ((((row == null) || (row["BaseColumnName"].ToString().Length == 0)) || ((bool)row["IsAutoIncrement"] || (bool)row["IsHidden"])) || ((bool)row["IsExpression"] || (bool)row["IsRowVersion"]))
                {
                    continue;
                }
                object value = null;
                string name  = this.sourceColumnNames[i];
                if ((mappings != null) && (dataRow != null))
                {
                    value = this.GetParameterInsertValue(name, mappings, dataRow, (bool)row["IsReadOnly"]);
                    if (value == null)
                    {
                        if (!(bool)row["IsReadOnly"] && !(command is SharpHsqlCommand))
                        {
                            goto Close;
                        }
                        continue;
                    }
                    if (Convert.IsDBNull(value) && !(bool)row["AllowDBNull"])
                    {
                        continue;
                    }
                }
Close:
                if (pcount == 0)
                {
                    sb.Append("( ");
                }
                else
                {
                    sb.Append(" , ");
                }
                sb.Append(this.QuotedColumn(row["BaseColumnName"].ToString()));
                IDataParameter parameter = CommandBuilder.GetNextParameter(command, pcount);
                parameter.ParameterName = "@p" + idx.ToString();
                parameter.Direction     = ParameterDirection.Input;
                parameter.SourceColumn  = name;
                parameter.SourceVersion = DataRowVersion.Current;
                parameter.Value         = value;
                if (parameter is SharpHsqlParameter)
                {
                    this.ApplyParameterInfo((SharpHsqlParameter)parameter, idx, row);
                }
                if (!command.Parameters.Contains(parameter))
                {
                    command.Parameters.Add(parameter);
                }
                pcount++;
                idx++;
            }
            if (pcount == 0)
            {
                sb.Append(" DEFAULT VALUES");
            }
            else if (this.namedParameters)
            {
                sb.Append(" ) VALUES ( @p1");
                for (int num5 = 2; num5 <= pcount; num5++)
                {
                    sb.Append(" , @p");
                    sb.Append(num5.ToString());
                }
                sb.Append(" )");
            }
            else
            {
                sb.Append(" ) VALUES ( ?");
                for (int num6 = 2; num6 <= pcount; num6++)
                {
                    sb.Append(" , ?");
                }
                sb.Append(" )");
            }
            command.CommandText = sb.ToString();
            CommandBuilder.RemoveExtraParameters(command, pcount);
            this.insertCommand = command;
            return(command);
        }