[svn] r6767: nemerle/trunk/macros: dataNpgsql.n dataSqlClient.n

nazgul svnadmin at nemerle.org
Wed Oct 18 20:08:10 CEST 2006


Log:
Makeing codebase db-provider independent

Author: nazgul
Date: Wed Oct 18 20:07:56 2006
New Revision: 6767

Modified:
   nemerle/trunk/macros/dataNpgsql.n
   nemerle/trunk/macros/dataSqlClient.n

Modified: nemerle/trunk/macros/dataNpgsql.n
==============================================================================
--- nemerle/trunk/macros/dataNpgsql.n	(original)
+++ nemerle/trunk/macros/dataNpgsql.n	Wed Oct 18 20:07:56 2006
@@ -51,7 +51,7 @@
       Message.FatalError ("Connection with name `" + name + "' is already defined")
     else {
       try {
-        def connection = NpgsqlConnection (con_str);
+        def connection = Helper.CreateConnection (con_str);
         connection.Open ();
         connections.Add (name, connection);
       }
@@ -65,11 +65,11 @@
   macro ExecuteNonQuery (query : string, conn, con_name : string = "")
   {
     def (query, tpars, pars_init) =
-      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, ':');
+      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, Helper.ParameterChar);
 
     // create compile-time query to check syntax and types in query
     def mytran = get_connection (con_name).BeginTransaction ();        
-    def mycmd = NpgsqlCommand (query, get_connection (con_name), mytran);    
+    def mycmd = Helper.CreateCommand (query, get_connection (con_name), mytran);    
 
     try { 
       tpars.Iter (fun (name, tvar : Typedtree.TExpr) {
@@ -80,7 +80,7 @@
       _ = mycmd.ExecuteNonQuery ()
     }
     catch {
-      | e is NpgsqlException =>
+      | e  =>
         Message.FatalError ("sql query error: " + e.Message)
     }
     finally {
@@ -89,7 +89,7 @@
     };
 
     <[
-      using (querycmd = NpgsqlCommand ($(query : string), $conn))
+      using (querycmd = $(Helper.CommandExpr) ($(query : string), $conn))
       {
         { .. $pars_init };
         querycmd.ExecuteNonQuery ();
@@ -100,11 +100,11 @@
   macro ExecuteScalar (query : string, conn, con_name : string = "")
   {
     def (query, tpars, pars_init) =
-      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, ':');
+      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, Helper.ParameterChar);
 
     // create compile-time query to check syntax and types in query
     def mytran = get_connection (con_name).BeginTransaction ();          
-    def mycmd = NpgsqlCommand (query, get_connection (con_name), mytran);
+    def mycmd = Helper.CreateCommand (query, get_connection (con_name), mytran);
 
     mutable col_type = null;
     try {
@@ -123,7 +123,7 @@
       myreader.Close ();
     }
     catch {
-      | e is NpgsqlException =>
+      | e  =>
         Message.FatalError ("sql query error: " + e.Message)
     }
     finally {
@@ -133,7 +133,7 @@
 
     /// final code for entire sql loop
     <[
-      using (querycmd = NpgsqlCommand ($(query : string), $conn))
+      using (querycmd = $(Helper.CommandExpr) ($(query : string), $conn))
       {
         { .. $pars_init };
         (querycmd.ExecuteScalar () :> $col_type);
@@ -144,11 +144,11 @@
   macro ExecuteReader (query : string, conn, con_name : string = "")
   {
     def (query, tpars, pars_init) =
-      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, ':');
+      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, Helper.ParameterChar);
 
     // create compile-time query to check syntax and types in query
     def mytran = get_connection (con_name).BeginTransaction ();                
-    def mycmd = NpgsqlCommand (query, get_connection (con_name), mytran);
+    def mycmd = Helper.CreateCommand (query, get_connection (con_name), mytran);
     try {
       tpars.Iter (fun (name, tvar : Typedtree.TExpr) {
         def (dbtype, dbvalue) = type_representant (tvar.ty.Fix ());
@@ -158,7 +158,7 @@
       _ = mycmd.ExecuteNonQuery ();
     }
     catch {
-      | e is NpgsqlException =>
+      | e  =>
         Message.FatalError ("sql query error: " + e.Message)
     }
     finally {
@@ -168,7 +168,7 @@
 
     /// final code for entire sql loop
     <[
-      using (querycmd = NpgsqlCommand ($(query : string), $conn)) 
+      using (querycmd = $(Helper.CommandExpr) ($(query : string), $conn)) 
       {
         { .. $pars_init };
         querycmd.ExecuteReader ();
@@ -179,14 +179,14 @@
   macro ExecuteReaderLoop (query : string, conn, body)
   {
     def (query, tpars, pars_init) =
-      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, ':');
+      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, Helper.ParameterChar);
 
     // list of definitions of query results inside loop body
     mutable bodyseq = [body];
 
     // create compile-time query to check syntax and types in query
     def mytran = get_connection ("").BeginTransaction ();
-    def mycmd = NpgsqlCommand (query, get_connection (""), mytran);
+    def mycmd = Helper.CreateCommand (query, get_connection (""), mytran);
     try {
       tpars.Iter (fun (name, tvar : Typedtree.TExpr) {
         def (dbtype, dbvalue) = type_representant (tvar.ty.Fix ());
@@ -216,7 +216,7 @@
       myreader.Close ();
     }
     catch {
-      | e is NpgsqlException =>
+      | e  =>
         Message.FatalError ("sql query error: " + e.Message)
     }
     finally {
@@ -226,7 +226,7 @@
 
     /// final code for entire sql loop
     <[
-      using (querycmd = NpgsqlCommand ($(query : string), $conn)) {
+      using (querycmd = $(Helper.CommandExpr) ($(query : string), $conn)) {
         { .. $pars_init };
         def reader = querycmd.ExecuteReader ();
         while (reader.Read ()) { ..$bodyseq };
@@ -236,9 +236,31 @@
   }
 
   module Helper {
-    internal connections : Hashtable [string, NpgsqlConnection] = Hashtable ();
+    internal connections : Hashtable [string, IDbConnection] = Hashtable ();
+
+    public CreateConnection (connStr : string) : IDbConnection
+    {
+      NpgsqlConnection (connStr)
+    }
+
+    public CreateCommand (query : string, conn : IDbConnection, tran : IDbTransaction) : NpgsqlCommand
+    {
+      def cmd = conn.CreateCommand ();
+      cmd.CommandText = query;
+      cmd.Connection = conn;
+      cmd.Transaction = tran;
+      cmd :> NpgsqlCommand
+    }
+    
+    public ParameterChar : char {
+      get { '@' }
+    }
+    
+    public CommandExpr : Parsetree.PExpr {
+      get { Util.ExprOfQid ("Npgsql.NpgsqlCommand") }
+    }
 
-    internal get_connection (name : string) : NpgsqlConnection
+    internal get_connection (name : string) : IDbConnection
     {
       match (connections.Get (name)) {
         | Some (c) => c

Modified: nemerle/trunk/macros/dataSqlClient.n
==============================================================================
--- nemerle/trunk/macros/dataSqlClient.n	(original)
+++ nemerle/trunk/macros/dataSqlClient.n	Wed Oct 18 20:07:56 2006
@@ -50,7 +50,7 @@
       Message.FatalError ("Connection with name `" + name + "' is already defined")
     else {
       try {
-        def connection = SqlConnection (con_str);
+        def connection = Helper.CreateConnection (con_str);
         connection.Open ();
         connections.Add (name, connection);
       }
@@ -64,11 +64,11 @@
   macro ExecuteNonQuery (query : string, conn, con_name : string = "")
   {
     def (query, tpars, pars_init) =
-      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, '@');
+      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, Helper.ParameterChar);
 
     // create compile-time query to check syntax and types in query
     def mytran = get_connection (con_name).BeginTransaction ();        
-    def mycmd = SqlCommand (query, get_connection (con_name), mytran);    
+    def mycmd = Helper.CreateCommand (query, get_connection (con_name), mytran);    
 
     try { 
       tpars.Iter (fun (name, tvar : Typedtree.TExpr) {
@@ -79,7 +79,7 @@
       _ = mycmd.ExecuteNonQuery ()
     }
     catch {
-      | e is SqlException =>
+      | e  =>
         Message.FatalError ("sql query error: " + e.Message)
     }
     finally {
@@ -88,7 +88,7 @@
     };
 
     <[
-      using (querycmd = SqlCommand ($(query : string), $conn))
+      using (querycmd = $(Helper.CommandExpr) ($(query : string), $conn))
       {
         { .. $pars_init };
         querycmd.ExecuteNonQuery ();
@@ -99,11 +99,11 @@
   macro ExecuteScalar (query : string, conn, con_name : string = "")
   {
     def (query, tpars, pars_init) =
-      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, '@');
+      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, Helper.ParameterChar);
 
     // create compile-time query to check syntax and types in query
     def mytran = get_connection (con_name).BeginTransaction ();          
-    def mycmd = SqlCommand (query, get_connection (con_name), mytran);
+    def mycmd = Helper.CreateCommand (query, get_connection (con_name), mytran);
 
     mutable col_type = null;
     try {
@@ -122,7 +122,7 @@
       myreader.Close ();
     }
     catch {
-      | e is SqlException =>
+      | e  =>
         Message.FatalError ("sql query error: " + e.Message)
     }
     finally {
@@ -132,7 +132,7 @@
 
     /// final code for entire sql loop
     <[
-      using (querycmd = SqlCommand ($(query : string), $conn))
+      using (querycmd = $(Helper.CommandExpr) ($(query : string), $conn))
       {
         { .. $pars_init };
         (querycmd.ExecuteScalar () :> $col_type);
@@ -143,11 +143,11 @@
   macro ExecuteReader (query : string, conn, con_name : string = "")
   {
     def (query, tpars, pars_init) =
-      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, '@');
+      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, Helper.ParameterChar);
 
     // create compile-time query to check syntax and types in query
     def mytran = get_connection (con_name).BeginTransaction ();                
-    def mycmd = SqlCommand (query, get_connection (con_name), mytran);
+    def mycmd = Helper.CreateCommand (query, get_connection (con_name), mytran);
     try {
       tpars.Iter (fun (name, tvar : Typedtree.TExpr) {
         def (dbtype, dbvalue) = type_representant (tvar.ty.Fix ());
@@ -157,7 +157,7 @@
       _ = mycmd.ExecuteNonQuery ();
     }
     catch {
-      | e is SqlException =>
+      | e  =>
         Message.FatalError ("sql query error: " + e.Message)
     }
     finally {
@@ -167,7 +167,7 @@
 
     /// final code for entire sql loop
     <[
-      using (querycmd = SqlCommand ($(query : string), $conn)) 
+      using (querycmd = $(Helper.CommandExpr) ($(query : string), $conn)) 
       {
         { .. $pars_init };
         querycmd.ExecuteReader ();
@@ -178,14 +178,14 @@
   macro ExecuteReaderLoop (query : string, conn, body)
   {
     def (query, tpars, pars_init) =
-      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, '@');
+      Tools.ExtractParameters (Nemerle.Macros.ImplicitCTX (), query, Helper.ParameterChar);
 
     // list of definitions of query results inside loop body
     mutable bodyseq = [body];
 
     // create compile-time query to check syntax and types in query
     def mytran = get_connection ("").BeginTransaction ();
-    def mycmd = SqlCommand (query, get_connection (""), mytran);
+    def mycmd = Helper.CreateCommand (query, get_connection (""), mytran);
     try {
       tpars.Iter (fun (name, tvar : Typedtree.TExpr) {
         def (dbtype, dbvalue) = type_representant (tvar.ty.Fix ());
@@ -197,7 +197,7 @@
                                          CommandBehavior.SingleRow);
       def table = myreader.GetSchemaTable ();
       mutable col_num = 0;
-      foreach(myRow :> DataRow in table.Rows){
+      foreach (myRow :> DataRow in table.Rows){
         def col_type = myRow["DataType"].ToString ();
         def col_name = myRow["ColumnName"].ToString ();
         def type_suff = 
@@ -215,7 +215,7 @@
       myreader.Close ();
     }
     catch {
-      | e is SqlException =>
+      | e  =>
         Message.FatalError ("sql query error: " + e.Message)
     }
     finally {
@@ -225,7 +225,7 @@
 
     /// final code for entire sql loop
     <[
-      using (querycmd = SqlCommand ($(query : string), $conn)) {
+      using (querycmd = $(Helper.CommandExpr) ($(query : string), $conn)) {
         { .. $pars_init };
         def reader = querycmd.ExecuteReader ();
         while (reader.Read ()) { ..$bodyseq };
@@ -235,9 +235,31 @@
   }
 
   module Helper {
-    internal connections : Hashtable [string, SqlConnection] = Hashtable ();
+    internal connections : Hashtable [string, IDbConnection] = Hashtable ();
+
+    public CreateConnection (connStr : string) : IDbConnection
+    {
+      SqlConnection (connStr)
+    }
+    
+    public CreateCommand (query : string, conn : IDbConnection, tran : IDbTransaction) : SqlCommand
+    {
+      def cmd = conn.CreateCommand ();
+      cmd.CommandText = query;
+      cmd.Connection = conn;
+      cmd.Transaction = tran;
+      cmd :> SqlCommand
+    }
+    
+    public CommandExpr : Parsetree.PExpr {
+      get { Util.ExprOfQid ("System.Data.SqlClient.SqlCommand") }
+    }
+    
+    public ParameterChar : char {
+      get { ':' }
+    }
 
-    internal get_connection (name : string) : SqlConnection
+    internal get_connection (name : string) : IDbConnection
     {
       match (connections.Get (name)) {
         | Some (c) => c



More information about the svn mailing list