/// <summary>
        /// Creates a <see cref="DbQueryReaderDataReaderContainer"/>.
        /// </summary>
        /// <param name="dbQueryBase">The <see cref="DbQueryBase"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/>.</param>
        /// <param name="dataReader">The <see cref="IDataReader"/>.</param>
        /// <returns>The <see cref="DbQueryReaderDataReaderContainer"/> instance.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="dbQueryBase" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="command" /> is <c>null</c>.</exception>
        public static DbQueryReaderDataReaderContainer Create(DbQueryBase dbQueryBase, DbCommand command, IDataReader dataReader)
        {
            if (dbQueryBase == null)
                throw new ArgumentNullException("dbQueryBase");
            if (command == null)
                throw new ArgumentNullException("command");

            DbQueryReaderDataReaderContainer r = null;

            // Try to grab from the pool first
            lock (_poolSync)
            {
                if (_pool.Count > 0)
                    r = _pool.Pop();
            }

            // Couldn't grab from pool - create new instance
            if (r == null)
                r = new DbQueryReaderDataReaderContainer();

            Debug.Assert(r._dbQueryBase == null);
            Debug.Assert(r._command == null);
            Debug.Assert(r.DataReader == null);

            // Initialize
            r._dbQueryBase = dbQueryBase;
            r._command = command;
            r.DataReader = dataReader;

            return r;
        }
        /// <summary>
        /// Creates a <see cref="DbQueryReaderDataReaderContainer"/>.
        /// </summary>
        /// <param name="dbQueryBase">The <see cref="DbQueryBase"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/>.</param>
        /// <param name="dataReader">The <see cref="IDataReader"/>.</param>
        /// <returns>The <see cref="DbQueryReaderDataReaderContainer"/> instance.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="dbQueryBase" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="command" /> is <c>null</c>.</exception>
        public static DbQueryReaderDataReaderContainer Create(DbQueryBase dbQueryBase, DbCommand command, IDataReader dataReader)
        {
            if (dbQueryBase == null)
            {
                throw new ArgumentNullException("dbQueryBase");
            }
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            DbQueryReaderDataReaderContainer r = null;

            // Try to grab from the pool first
            lock (_poolSync)
            {
                if (_pool.Count > 0)
                {
                    r = _pool.Pop();
                }
            }

            // Couldn't grab from pool - create new instance
            if (r == null)
            {
                r = new DbQueryReaderDataReaderContainer();
            }

            Debug.Assert(r._dbQueryBase == null);
            Debug.Assert(r._command == null);
            Debug.Assert(r.DataReader == null);

            // Initialize
            r._dbQueryBase = dbQueryBase;
            r._command     = command;
            r.DataReader   = dataReader;

            return(r);
        }
示例#3
0
        /// <summary>
        /// Executes the query on the database.
        /// </summary>
        /// <returns>IDataReader used to read the results of the query.</returns>
        /// <exception cref="DuplicateKeyException">An insert is being performed using a key that already exists.</exception>
        IDataReader IDbQueryReader.ExecuteReader()
        {
            // Update the query stats
            var stats = ConnectionPool.QueryStats;

            if (stats != null)
            {
                stats.QueryExecuted(this);
            }

            var r = ConnectionPool.QueryRunner;

            // Get and set up the command
            IDataReader retReader;
            var         cmd = GetCommand(r.Connection);

            try
            {
                retReader = r.BeginExecuteReader(cmd);
            }
            catch (MySqlException ex)
            {
                // Throw a custom exception for common errors
                if (ex.Number == 1062)
                {
                    throw new DuplicateKeyException(ex);
                }

                // Everything else, just throw the default exception
                throw;
            }

            // Return the DbDataReader wrapped in a custom container that will allow us to
            // properly free the command and close the connection when the DbDataReader is disposed
            return(DbQueryReaderDataReaderContainer.Create(this, cmd, retReader));
        }