| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- //
- // System.Data.SqlClient.SqlCommandBuilder.cs
- //
- // Author:
- // Tim Coleman ([email protected])
- // Veerapuram Varadhan ([email protected])
- //
- // Copyright (C) Tim Coleman, 2002
- //
- //
- // Copyright (C) 2004, 2009 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Data.Common;
- using System.Data.SqlTypes;
- using System.Text;
- namespace System.Data.SqlClient
- {
- public sealed class SqlCommandBuilder : DbCommandBuilder
- {
- #region Fields
- readonly string _catalogSeparator = ".";
- readonly string _schemaSeparator = ".";
- readonly CatalogLocation _catalogLocation = CatalogLocation.Start;
-
- #endregion // Fields
- #region Constructors
- public SqlCommandBuilder ()
- {
- QuoteSuffix = "]";
- QuotePrefix = "[";
- }
- public SqlCommandBuilder (SqlDataAdapter adapter)
- : this ()
- {
- DataAdapter = adapter;
- }
- #endregion // Constructors
- #region Properties
- [DefaultValue (null)]
- public new SqlDataAdapter DataAdapter {
- get {
- return (SqlDataAdapter)base.DataAdapter;
- } set {
- base.DataAdapter = value;
- }
- }
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable (EditorBrowsableState.Never)]
- public
- override
- string QuotePrefix {
- get {
- return base.QuotePrefix;
- }
- set {
- if (value != "[" && value != "\"")
- throw new ArgumentException ("Only '[' " +
- "and '\"' are allowed as value " +
- "for the 'QuoteSuffix' property.");
- base.QuotePrefix = value;
- }
- }
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable (EditorBrowsableState.Never)]
- public
- override
- string QuoteSuffix {
- get {
- return base.QuoteSuffix;
- }
- set {
- if (value != "]" && value != "\"")
- throw new ArgumentException ("Only ']' " +
- "and '\"' are allowed as value " +
- "for the 'QuoteSuffix' property.");
- base.QuoteSuffix = value;
- }
- }
- [EditorBrowsable (EditorBrowsableState.Never)]
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public override string CatalogSeparator {
- get { return _catalogSeparator; }
- set {
- if (value != _catalogSeparator)
- throw new ArgumentException ("Only " +
- "'.' is allowed as value " +
- "for the 'CatalogSeparator' " +
- "property.");
- }
- }
- [EditorBrowsable (EditorBrowsableState.Never)]
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public override string SchemaSeparator {
- get { return _schemaSeparator; }
- set {
- if (value != _schemaSeparator)
- throw new ArgumentException ("Only " +
- "'.' is allowed as value " +
- "for the 'SchemaSeparator' " +
- "property.");
- }
- }
- [EditorBrowsable (EditorBrowsableState.Never)]
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public override CatalogLocation CatalogLocation {
- get { return _catalogLocation; }
- set {
- if (value != CatalogLocation.Start)
- throw new ArgumentException ("Only " +
- "'Start' is allowed as value " +
- "for the 'CatalogLocation' " +
- "property.");
- }
- }
- #endregion // Properties
- #region Methods
-
- public static void DeriveParameters (SqlCommand command)
- {
- command.DeriveParameters ();
- }
- public
- new
- SqlCommand GetDeleteCommand ()
- {
- return (SqlCommand) base.GetDeleteCommand (false);
- }
- public
- new
- SqlCommand GetInsertCommand ()
- {
- return (SqlCommand) base.GetInsertCommand (false);
- }
- public
- new
- SqlCommand GetUpdateCommand ()
- {
- return (SqlCommand) base.GetUpdateCommand (false);
- }
- public new SqlCommand GetUpdateCommand (bool useColumnsForParameterNames)
- {
- return (SqlCommand) base.GetUpdateCommand (useColumnsForParameterNames);
- }
- public new SqlCommand GetDeleteCommand (bool useColumnsForParameterNames)
- {
- return (SqlCommand) base.GetDeleteCommand (useColumnsForParameterNames);
- }
- public new SqlCommand GetInsertCommand (bool useColumnsForParameterNames)
- {
- return (SqlCommand) base.GetInsertCommand (useColumnsForParameterNames);
- }
-
- public override string QuoteIdentifier (string unquotedIdentifier)
- {
- if (unquotedIdentifier == null)
- throw new ArgumentNullException ("unquotedIdentifier");
- string prefix = QuotePrefix;
- string suffix = QuoteSuffix;
- if ((prefix == "[" && suffix != "]") || (prefix == "\"" && suffix != "\""))
- throw new ArgumentException ("The QuotePrefix " +
- "and QuoteSuffix properties do not match.");
- string escaped = unquotedIdentifier.Replace (suffix,
- suffix + suffix);
- return string.Concat (prefix, escaped, suffix);
- }
-
- public override string UnquoteIdentifier (string quotedIdentifier)
- {
- return base.UnquoteIdentifier (quotedIdentifier);
- }
- private bool IncludedInInsert (DataRow schemaRow)
- {
- // If the parameter has one of these properties, then we don't include it in the insert:
- // AutoIncrement, Hidden, Expression, RowVersion, ReadOnly
- if (!schemaRow.IsNull ("IsAutoIncrement") && (bool) schemaRow ["IsAutoIncrement"])
- return false;
- if (!schemaRow.IsNull ("IsHidden") && (bool) schemaRow ["IsHidden"])
- return false;
- if (!schemaRow.IsNull ("IsExpression") && (bool) schemaRow ["IsExpression"])
- return false;
- if (!schemaRow.IsNull ("IsRowVersion") && (bool) schemaRow ["IsRowVersion"])
- return false;
- if (!schemaRow.IsNull ("IsReadOnly") && (bool) schemaRow ["IsReadOnly"])
- return false;
- return true;
- }
- private bool IncludedInUpdate (DataRow schemaRow)
- {
- // If the parameter has one of these properties, then we don't include it in the insert:
- // AutoIncrement, Hidden, RowVersion
- if (!schemaRow.IsNull ("IsAutoIncrement") && (bool) schemaRow ["IsAutoIncrement"])
- return false;
- if (!schemaRow.IsNull ("IsHidden") && (bool) schemaRow ["IsHidden"])
- return false;
- if (!schemaRow.IsNull ("IsRowVersion") && (bool) schemaRow ["IsRowVersion"])
- return false;
- if (!schemaRow.IsNull ("IsExpression") && (bool) schemaRow ["IsExpression"])
- return false;
- if (!schemaRow.IsNull ("IsReadOnly") && (bool) schemaRow ["IsReadOnly"])
- return false;
- return true;
- }
- private bool IncludedInWhereClause (DataRow schemaRow)
- {
- if ((bool) schemaRow ["IsLong"])
- return false;
- return true;
- }
- protected override void ApplyParameterInfo (DbParameter parameter,
- DataRow datarow,
- StatementType statementType,
- bool whereClause)
- {
- SqlParameter sqlParam = (SqlParameter) parameter;
- sqlParam.SqlDbType = (SqlDbType) datarow ["ProviderType"];
- object precision = datarow ["NumericPrecision"];
- if (precision != DBNull.Value) {
- short val = (short) precision;
- if (val < byte.MaxValue && val >= byte.MinValue)
- sqlParam.Precision = (byte) val;
- }
- object scale = datarow ["NumericScale"];
- if (scale != DBNull.Value) {
- short val = ((short) scale);
- if (val < byte.MaxValue && val >= byte.MinValue)
- sqlParam.Scale = (byte) val;
- }
- }
- protected override
- string GetParameterName (int parameterOrdinal)
- {
- return String.Format ("@p{0}", parameterOrdinal);
- }
- protected override
- string GetParameterName (string parameterName)
- {
- return String.Format ("@{0}", parameterName);
- }
- protected override string GetParameterPlaceholder (int parameterOrdinal)
- {
- return GetParameterName (parameterOrdinal);
- }
- #endregion // Methods
- #region Event Handlers
- void RowUpdatingHandler (object sender, SqlRowUpdatingEventArgs args)
- {
- base.RowUpdatingHandler (args);
- }
- protected override void SetRowUpdatingHandler (DbDataAdapter adapter)
- {
- SqlDataAdapter sda = adapter as SqlDataAdapter;
- if (sda == null) {
- throw new InvalidOperationException ("Adapter needs to be a SqlDataAdapter");
- }
-
- if (sda != base.DataAdapter)
- sda.RowUpdating += new SqlRowUpdatingEventHandler (RowUpdatingHandler);
- else
- sda.RowUpdating -= new SqlRowUpdatingEventHandler (RowUpdatingHandler);;
- }
- protected override DataTable GetSchemaTable (DbCommand srcCommand)
- {
- using (SqlDataReader rdr = (SqlDataReader) srcCommand.ExecuteReader (CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
- return rdr.GetSchemaTable ();
- }
- protected override DbCommand InitializeCommand (DbCommand command)
- {
- if (command == null) {
- command = new SqlCommand ();
- } else {
- command.CommandTimeout = 30;
- command.Transaction = null;
- command.CommandType = CommandType.Text;
- command.UpdatedRowSource = UpdateRowSource.None;
- }
- return command;
- }
- #endregion // Event Handlers
- }
- }
|