Duplex client has its own listener loop, so special care on reply is needed.
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlCommand.cs
index 7776feb359617fce5dc23a66043d72b63db19694..b0962359876ee29436e75ccbf9974bb4459915db 100644 (file)
@@ -65,7 +65,6 @@ namespace System.Data.SqlClient {
 
                const int DEFAULT_COMMAND_TIMEOUT = 30;
 
-               bool disposed;
                int commandTimeout;
                bool designTimeVisible;
                string commandText;
@@ -77,6 +76,7 @@ namespace System.Data.SqlClient {
                SqlParameterCollection parameters;
                string preparedStatement;
 #if NET_2_0
+               bool disposed;
                SqlNotificationRequest notification;
                bool notificationAutoEnlist;
 #endif
@@ -157,7 +157,7 @@ namespace System.Data.SqlClient {
                        set {
                                if (value != commandText && preparedStatement != null)
                                        Unprepare ();
-                               commandText = value; 
+                               commandText = value;
                        }
                }
 
@@ -378,6 +378,54 @@ namespace System.Data.SqlClient {
                        return new SqlParameter ();
                }
 
+               private string EscapeProcName (string name, bool schema)
+               {
+                       string procName;
+                       string tmpProcName = name.Trim ();
+                       int procNameLen = tmpProcName.Length;
+                       char[] brkts = new char [] {'[', ']'};
+                       bool foundMatching = false;
+                       int start = 0, count = procNameLen;
+                       int sindex = -1, eindex = -1;
+                       
+                       // We try to match most of the "brackets" combination here, however
+                       // there could be other combinations that may generate a different type
+                       // of exception in MS.NET
+                       
+                       if (procNameLen > 1) {
+                               if ((sindex = tmpProcName.IndexOf ('[')) <= 0)
+                                       foundMatching = true;
+                               else
+                                       foundMatching = false;
+                       
+                               if (foundMatching == true && sindex > -1) {
+                                       eindex = tmpProcName.IndexOf (']');
+                                       if (sindex > eindex && eindex != -1) {
+                                               foundMatching = false;
+                                       } else if (eindex == procNameLen-1) {
+                                               if (tmpProcName.IndexOfAny (brkts, 1, procNameLen-2) != -1) {
+                                                       foundMatching = false;
+                                               } else {
+                                                       start = 1;
+                                                       count = procNameLen - 2;
+                                               }
+                                       } else if (eindex == -1 && schema) {
+                                               foundMatching = true;
+                                       } else {
+                                               foundMatching = false;
+                                       }
+                               }
+                       
+                               if (foundMatching)
+                                       procName = tmpProcName.Substring (start, count);
+                               else
+                                       throw new ArgumentException (String.Format ("SqlCommand.CommandText property value is an invalid multipart name {0}, incorrect usage of quotes", CommandText));
+                       } else {
+                               procName = tmpProcName;
+                       }
+                       
+                       return procName;
+               }
                internal void DeriveParameters ()
                {
                        if (commandType != CommandType.StoredProcedure)
@@ -386,12 +434,19 @@ namespace System.Data.SqlClient {
 
                        string procName = CommandText;
                        string schemaName = String.Empty;
-                       int dotPosition = procName.IndexOf ('.');
+                       int dotPosition = procName.LastIndexOf ('.');
+
+                       // Procedure name can be: [database].[user].[procname]
                        if (dotPosition >= 0) {
                                schemaName = procName.Substring (0, dotPosition);
                                procName = procName.Substring (dotPosition + 1);
+                               if ((dotPosition = schemaName.LastIndexOf ('.')) >= 0)
+                                       schemaName = schemaName.Substring (dotPosition + 1);
                        }
                        
+                       procName = EscapeProcName (procName, false);
+                       schemaName = EscapeProcName (schemaName, true);
+                       
                        SqlParameterCollection localParameters = new SqlParameterCollection (this);
                        localParameters.Add ("@procedure_name", SqlDbType.NVarChar, procName.Length).Value = procName;
                        if (schemaName.Length > 0)
@@ -402,6 +457,7 @@ namespace System.Data.SqlClient {
                        try {
                                Connection.Tds.ExecProc (sql, localParameters.MetaParameters, 0, true);
                        } catch (TdsTimeoutException ex) {
+                               Connection.Tds.Reset ();
                                throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
                        } catch (TdsInternalException ex) {
                                Connection.Close ();
@@ -462,6 +518,7 @@ namespace System.Data.SqlClient {
                                                // 1) Network is down/server is down/not reachable
                                                // 2) Somebody has an exclusive lock on Table/DB
                                                // In any of these cases, don't close the connection. Let the user do it
+                                               Connection.Tds.Reset ();
                                                throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
                                        } catch (TdsInternalException ex) {
                                                Connection.Close ();
@@ -478,6 +535,7 @@ namespace System.Data.SqlClient {
                                        try {
                                                Connection.Tds.Execute (sql, parms, CommandTimeout, wantResults);
                                        } catch (TdsTimeoutException ex) {
+                                               Connection.Tds.Reset ();
                                                throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
                                        } catch (TdsInternalException ex) {
                                                Connection.Close ();
@@ -490,7 +548,8 @@ namespace System.Data.SqlClient {
                                try {
                                        Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
                                } catch (TdsTimeoutException ex) {
-                                               throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
+                                       Connection.Tds.Reset ();
+                                       throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
                                } catch (TdsInternalException ex) {
                                        Connection.Close ();
                                        throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
@@ -512,6 +571,7 @@ namespace System.Data.SqlClient {
                                Execute (false);
                                result = Connection.Tds.RecordsAffected;
                        } catch (TdsTimeoutException e) {
+                               Connection.Tds.Reset ();
                                throw SqlException.FromTdsInternalException ((TdsInternalException) e);
                        }
 
@@ -532,10 +592,15 @@ namespace System.Data.SqlClient {
                        this.behavior = behavior;
                        if ((behavior & CommandBehavior.SequentialAccess) != 0)
                                Tds.SequentialAccess = true;
-                       Execute (true);
-                       Connection.DataReader = new SqlDataReader (this);
-                       
-                       return Connection.DataReader;
+                       try {
+                               Execute (true);
+                               Connection.DataReader = new SqlDataReader (this);
+                               return Connection.DataReader;
+                       } catch {
+                               if ((behavior & CommandBehavior.CloseConnection) != 0)
+                                       Connection.Close ();
+                               throw;
+                       }
                }
 
                public
@@ -563,6 +628,7 @@ namespace System.Data.SqlClient {
                                                GetOutputParameters ();
                                        }
                                } catch (TdsTimeoutException ex) {
+                                       Connection.Tds.Reset ();
                                        throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
                                } catch (TdsInternalException ex) {
                                        Connection.Close ();
@@ -582,6 +648,7 @@ namespace System.Data.SqlClient {
                        try {
                                Execute (true);
                        } catch (TdsTimeoutException e) {
+                               Connection.Tds.Reset ();
                                throw SqlException.FromTdsInternalException ((TdsInternalException) e);
                        }
 
@@ -639,6 +706,8 @@ namespace System.Data.SqlClient {
                        if (disposed) return;
                        if (disposing) {
                                parameters.Clear();
+                               if (Connection != null)
+                                       Connection.DataReader = null;
                        }
                        base.Dispose (disposing);
                        disposed = true;
@@ -699,7 +768,7 @@ namespace System.Data.SqlClient {
                        if (Connection.State != ConnectionState.Open)
                                throw new InvalidOperationException (String.Format ("{0} requires an open connection to continue. This connection is closed.", method));
                        if (CommandText.Length == 0)
-                               throw new InvalidOperationException ("The command text for this Command has not been set.");
+                               throw new InvalidOperationException (String.Format ("{0}: CommandText has not been set for this Command.", method));
                        if (Connection.DataReader != null)
                                throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
                        if (Connection.XmlReader != null)
@@ -785,6 +854,7 @@ namespace System.Data.SqlClient {
                                                                                      callback,
                                                                                      state);
                                        } catch (TdsTimeoutException ex) {
+                                               Connection.Tds.Reset ();
                                                throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
                                        } catch (TdsInternalException ex) {
                                                Connection.Close ();
@@ -799,6 +869,7 @@ namespace System.Data.SqlClient {
                                                else
                                                        ar = Connection.Tds.BeginExecuteNonQuery (sql, parms, callback, state);
                                        } catch (TdsTimeoutException ex) {
+                                               Connection.Tds.Reset ();
                                                throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
                                        } catch (TdsInternalException ex) {
                                                Connection.Close ();
@@ -811,6 +882,7 @@ namespace System.Data.SqlClient {
                                try {
                                        Connection.Tds.ExecPrepared (preparedStatement, parms, CommandTimeout, wantResults);
                                } catch (TdsTimeoutException ex) {
+                                       Connection.Tds.Reset ();
                                        throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
                                } catch (TdsInternalException ex) {
                                        Connection.Close ();