Пример #1
0
        protected void ExecuteQuery(string query, DataReaderDelegate reader_func)
        {
            dbcon.Open ();

            IDbCommand dbcmd = dbcon.CreateCommand ();
            dbcmd.CommandText = query;
            IDataReader reader = dbcmd.ExecuteReader ();

            reader_func (reader);

            reader.Close ();
            reader = null;
            dbcmd.Dispose ();
            dbcmd = null;

            dbcon.Close ();
        }
Пример #2
0
 /// <exclude/>
 public override void ExecuteQuery(ITransaction transaction, ClassMapping mapping, IDaQuery query,
     DataReaderDelegate invokeMe, Hashtable parameters)
 {
     if (query == null)
     {
         throw new ArgumentNullException("query", "Cannot execute a null query.");
     }
     if (!(query is SqlDaQuery))
     {
         throw new ArgumentException("Cannot execute a query not created by me.");
     }
     SqlDaQuery sqlQuery = (SqlDaQuery)query;
     SqlConnectionUtilities.XSafeQuery(_connDesc, (SqlTransaction)transaction, sqlQuery.Sql.ToString(),
         sqlQuery.Params, invokeMe, parameters);
 }
Пример #3
0
 /// <summary>
 /// Executes a query and invokes a method with a DataReader of results.
 /// </summary>
 /// <param name="transaction">Should be null, transactions are not supported.</param>
 /// <param name="mapping">Class mapping for the table we're querying against.  Optional,
 ///                       but not all columns may be properly typed if it is null.</param>
 /// <param name="query">The query to execute, should have come from CreateQuery.</param>
 /// <param name="invokeMe">The method to invoke with the IDataReader results.</param>
 /// <param name="parameters">A hashtable containing any values that need to be persisted through invoked method.
 ///                          The list of objects from the query will be placed here.</param>
 public override void ExecuteQuery(ITransaction transaction, ClassMapping mapping, IDaQuery query, DataReaderDelegate invokeMe, Hashtable parameters)
 {
     switch (_connDesc.Type)
     {
         case CsvConnectionType.Directory:
         case CsvConnectionType.FileName:
         case CsvConnectionType.Reader:
             // These are OK.
             break;
         default:
             throw new LoggingException("Connection does not support querying: " + _connDesc);
     }
     CsvDataReader reader = new CsvDataReader(this, mapping, ((UnqueryableQuery)query).Criteria);
     try
     {
         invokeMe.Invoke(parameters, reader);
     }
     finally
     {
         reader.Close();
     }
 }
Пример #4
0
 /// <summary>
 /// Executes a query and invokes a method with a DataReader of results.
 /// </summary>
 /// <param name="transaction">The transaction to do this as part of.</param>
 /// <param name="mapping">Class mapping for the table we're querying against.  Optional,
 ///                       but not all columns may be properly typed if it is null.</param>
 /// <param name="query">The query to execute, should have come from CreateQuery.</param>
 /// <param name="invokeMe">The method to invoke with the IDataReader results.</param>
 /// <param name="parameters">A hashtable containing any values that need to be persisted through invoked method.
 ///                          The list of objects from the query will be placed here.</param>
 public abstract void ExecuteQuery(ITransaction transaction, ClassMapping mapping, IDaQuery query, DataReaderDelegate invokeMe, Hashtable parameters);
Пример #5
0
 /// <summary>
 /// This provides a way to query the DB and do something with the results without
 /// having to copy the "try, open, try, execute, finally, close, finally, close"
 /// type logic in a bunch of places.  This method correctly closes the objects
 /// used in the DB access in the event of an exception.
 /// </summary>
 /// <param name="connDesc">The database connection descriptor.  This is used both as
 ///                        a key for caching connections/commands as well as for
 ///                        getting the actual database connection the first time.</param>
 /// <param name="transaction">The transaction to do this as part of.</param>
 /// <param name="sql">The SQL statement to execute.</param>
 /// <param name="sqlParams">A list of objects to use as parameters
 ///							to the SQL statement.  The list may be
 ///							null if there are no parameters.</param>
 /// <param name="invokeMe">The method to delegate to.  If null, nothing is
 ///                        done with the data reader and it is just closed.</param>
 /// <param name="parameters">The other parameters to the delegate, in whatever
 ///							 form makes sense for that delegate method.</param>
 public static void XSafeQuery(AbstractSqlConnectionDescriptor connDesc,
     SqlTransaction transaction, string sql,
     IEnumerable sqlParams, DataReaderDelegate invokeMe, Hashtable parameters)
 {
     IDbConnection conn = transaction != null
         ? transaction.Connection
         : DbCaches.Connections.Get(connDesc);
     try
     {
         IDbCommand cmd = DbCaches.Commands.Get(sql, conn);
         if (transaction != null)
         {
             cmd.Transaction = transaction.Transaction;
         }
         try
         {
             SetSQLOnCommand(connDesc, cmd, sql, sqlParams);
             IDataReader reader;
             try
             {
                 reader = cmd.ExecuteReader();
             }
             catch (Exception e)
             {
                 throw new UnableToRunSqlException(connDesc, sql, sqlParams, e);
             }
             try
             {
                 if (invokeMe != null)
                 {
                     invokeMe(parameters, reader);
                 }
             }
             catch (ExceptionWithConnectionInfo)
             {
                 // The delegate may have thrown an exception that came from another
                 // database utilities call, in which case we don't want to confuse
                 // the issue by wrapping with a different SQL command.
                 throw;
             }
             catch (Exception e)
             {
                 throw new UnableToProcessSqlResultsException(connDesc, sql, sqlParams, e);
             }
             finally
             {
                 try
                 {
                     reader.Close();
                 }
                 catch (Exception e)
                 {
                     // This exception is not rethrown because we don't want to mask any
                     // previous exception that might be being thrown out of "invokeMe".
                     _log.Warn("Caught an exception while trying to close the data reader.", e);
                 }
             }
         }
         finally
         {
             DbCaches.Commands.Return(sql, conn, cmd);
         }
     }
     finally
     {
         if (transaction == null)
         {
             DbCaches.Connections.Return(connDesc, conn);
         }
     }
 }
Пример #6
0
 /// <summary>
 /// This provides a way to query the DB and do something with the results without
 /// having to copy the "try, open, try, execute, finally, close, finally, close"
 /// type logic in a bunch of places.  This method correctly closes the objects
 /// used in the DB access in the event of an exception.
 /// </summary>
 /// <param name="connDesc">The database connection descriptor.  This is used both as
 ///                        a key for caching connections/commands as well as for
 ///                        getting the actual database connection the first time.</param>
 /// <param name="sql">The SQL statement to execute.</param>
 /// <param name="sqlParams">A list of objects to use as parameters
 ///							to the SQL statement.  The list may be
 ///							null if there are no parameters.</param>
 /// <param name="invokeMe">The method to delegate to.  If null, nothing is
 ///                        done with the data reader and it is just closed.</param>
 /// <param name="parameters">The other parameters to the delegate, in whatever
 ///							 form makes sense for that delegate method.</param>
 public static void XSafeQuery(AbstractSqlConnectionDescriptor connDesc, string sql,
     IEnumerable sqlParams, DataReaderDelegate invokeMe, Hashtable parameters)
 {
     XSafeQuery(connDesc, null, sql, sqlParams, invokeMe, parameters);
 }
Пример #7
0
 /// <summary>
 /// Executes a query and invokes a method with a DataReader of results.
 /// </summary>
 /// <param name="transaction">The transaction to do this as part of.</param>
 /// <param name="mapping">Class mapping for the table we're querying against.  Optional,
 ///                       but not all columns may be properly typed if it is null.</param>
 /// <param name="query">The query to execute, should have come from CreateQuery.</param>
 /// <param name="invokeMe">The method to invoke with the IDataReader results.</param>
 /// <param name="parameters">A hashtable containing any values that need to be persisted through invoked method.
 ///                          The list of objects from the query will be placed here.</param>
 public override void ExecuteQuery(ITransaction transaction, ClassMapping mapping, IDaQuery query, DataReaderDelegate invokeMe, Hashtable parameters)
 {
     // Make a copy of the table and iterate over that, that way reading doesn't block writing (or
     // more reading).
     IDictionary<string, MemoryObject> tempTable;
     IDictionary<string, MemoryObject> table = GetTable(mapping);
     lock (table)
     {
         tempTable = new CheckedDictionary<string, MemoryObject>(table);
     }
     MemoryDataReader reader = new MemoryDataReader(this, mapping, ((UnqueryableQuery)query).Criteria,
                                                    tempTable.Values.GetEnumerator());
     try
     {
         invokeMe.Invoke(parameters, reader);
     }
     finally
     {
         reader.Close();
     }
 }