Make System.Web.Script.Serialization.JavaScriptSerializer.ConvertToType(Type, object...
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlCommand.cs
index 531ebbc6b0849d708d86d672a0d4d96fa0cbc160..4a536daec4ed9373808cb85e8dbd90fdc0ba10a4 100644 (file)
@@ -29,6 +29,7 @@ using System.Text;
 using System.Resources;
 using System.ComponentModel;
 using System.Collections;
+using System.IO;
 
 using NpgsqlTypes;
 
@@ -384,8 +385,23 @@ namespace Npgsql
         {
             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Cancel");
 
-            // [TODO] Finish method implementation.
-            throw new NotImplementedException();
+            try
+            {
+                // get copy for thread safety of null test
+                NpgsqlConnector connector = Connector;
+                if (connector != null)
+                {
+                    connector.CancelRequest();
+                }
+            }
+            catch (IOException)
+            {
+                Connection.ClearPool();
+            }   
+            catch (NpgsqlException)
+            {
+                // Cancel documentation says the Cancel doesn't throw on failure
+            }
         }
         
         /// <summary>
@@ -679,6 +695,7 @@ namespace Npgsql
 
             ExecuteCommand();
 
+
             // Now get the results.
             // Only the first column of the first row must be returned.
 
@@ -798,6 +815,10 @@ namespace Npgsql
                     
                     bind = new NpgsqlBind("", planName, new Int16[Parameters.Count], null, resultFormatCodes);
                 }    
+                catch (IOException e)
+                {
+                    ClearPoolAndThrowException(e);
+                }
                 catch
                 {
                     // See ExecuteCommand method for a discussion of this.
@@ -1028,7 +1049,11 @@ namespace Npgsql
         
         private Boolean CheckFunctionReturn(String ReturnType)
         {
-            String returnRecordQuery = "select count(*) > 0 from pg_proc p left join pg_namespace n on p.pronamespace = n.oid where prorettype = ( select oid from pg_type where typname = :typename ) and proargtypes=:proargtypes and proname=:proname and n.nspname=:nspname";
+            // Updated after 0.99.3 to support the optional existence of a name qualifying schema and allow for case insensitivity
+            // when the schema or procedure name do not contain a quote.
+            // The hard-coded schema name 'public' was replaced with code that uses schema as a qualifier, only if it is provided.
+
+            String returnRecordQuery;
 
             StringBuilder parameterTypes = new StringBuilder("");
 
@@ -1051,17 +1076,22 @@ namespace Npgsql
             String procedureName = String.Empty;
             
             
-            String[] schemaProcedureName = CommandText.Split('.');
+            String[] fullName = CommandText.Split('.');
             
-            if (schemaProcedureName.Length == 2)
+            if (fullName.Length == 2)
             {
-                schemaName = schemaProcedureName[0];
-                procedureName = schemaProcedureName[1];
+                returnRecordQuery = "select count(*) > 0 from pg_proc p left join pg_namespace n on p.pronamespace = n.oid where prorettype = ( select oid from pg_type where typname = :typename ) and proargtypes=:proargtypes and proname=:proname and n.nspname=:nspname";
+
+                schemaName = (fullName[0].IndexOf("\"") != -1) ? fullName[0] : fullName[0].ToLower();
+                procedureName = (fullName[1].IndexOf("\"") != -1) ? fullName[1] : fullName[1].ToLower();
             }
             else
             {
-                schemaName = "public";
-                procedureName = CommandText;
+                // Instead of defaulting don't use the nspname, as an alternative, query pg_proc and pg_namespace to try and determine the nspname.
+                //schemaName = "public"; // This was removed after build 0.99.3 because the assumption that a function is in public is often incorrect.
+                returnRecordQuery = "select count(*) > 0 from pg_proc p where prorettype = ( select oid from pg_type where typname = :typename ) and proargtypes=:proargtypes and proname=:proname";
+                
+                procedureName = (CommandText.IndexOf("\"") != -1) ? CommandText : CommandText.ToLower();
             }
                 
             
@@ -1072,12 +1102,16 @@ namespace Npgsql
             c.Parameters.Add(new NpgsqlParameter("typename", NpgsqlDbType.Text));
             c.Parameters.Add(new NpgsqlParameter("proargtypes", NpgsqlDbType.Text));
             c.Parameters.Add(new NpgsqlParameter("proname", NpgsqlDbType.Text));
-            c.Parameters.Add(new NpgsqlParameter("nspname", NpgsqlDbType.Text));
             
             c.Parameters[0].Value = ReturnType;
             c.Parameters[1].Value = parameterTypes.ToString();
             c.Parameters[2].Value = procedureName;
-            c.Parameters[3].Value = schemaName;
+
+            if (schemaName != null && schemaName.Length > 0)
+            {
+                c.Parameters.Add(new NpgsqlParameter("nspname", NpgsqlDbType.Text));
+                c.Parameters[3].Value = schemaName;
+            }
             
 
             Boolean ret = (Boolean) c.ExecuteScalar();
@@ -1418,6 +1452,9 @@ namespace Npgsql
 
         private void ExecuteCommand()
         {
+            try
+            {
+                
             // Check the connection state first.
             CheckConnectionState();
 
@@ -1474,6 +1511,13 @@ namespace Npgsql
                 }
             }
 
+            }
+
+            catch(IOException e)
+            {
+                ClearPoolAndThrowException(e);
+            }
+
         }
 
         private void SetCommandTimeout()
@@ -1484,6 +1528,13 @@ namespace Npgsql
                 timeout = ConnectionStringDefaults.CommandTimeout;
         }
 
+        private void ClearPoolAndThrowException(Exception e)
+        {
+            Connection.ClearPool();
+            throw new NpgsqlException(resman.GetString("Exception_ConnectionBroken"), e);
+
+        }
+