QuickBooks/Driver/Sql.php
protected function _truncate($table, $max_history)
	{
		// Don't do this all the time...
		if (mt_rand(0, 10) == 1)
		{
			return;
		}

		// We only need to run this once per table per HTTP session, so keep track of if we've alrealdy run or not
		static $runs = array();
		if (!empty($runs[$table]))
		{
			return;
		}
		$runs[$table] = true;

		// If max_history is set to -1, we *never* truncate
		if ($max_history <= 0)
		{
			return;
		}

		switch ($table)
		{
			case QUICKBOOKS_DRIVER_SQL_LOGTABLE:
				$sql = "SELECT quickbooks_log_id FROM " . $this->_mapTableName($table) . " ORDER BY quickbooks_log_id ASC LIMIT ";
				$field = 'quickbooks_log_id';
				break;
			case QUICKBOOKS_DRIVER_SQL_QUEUETABLE:
				$sql = "
					SELECT
						quickbooks_queue_id
					FROM
						" . $this->_mapTableName($table) . "
					WHERE
						qb_status IN (
							'" . QUICKBOOKS_STATUS_SUCCESS . "',
							'" . QUICKBOOKS_STATUS_HANDLED . "',
							'" . QUICKBOOKS_STATUS_CANCELLED . "',
							'" . QUICKBOOKS_STATUS_NOOP . "' )
					ORDER BY
						quickbooks_queue_id ASC LIMIT ";
				$field = 'quickbooks_queue_id';
				break;
			case QUICKBOOKS_DRIVER_SQL_TICKETTABLE:
				$sql = "SELECT quickbooks_ticket_id FROM " . $this->_mapTableName($table) . " ORDER BY quickbooks_ticket_id ASC LIMIT ";
				$field = 'quickbooks_ticket_id';
				break;
		}

		// How big is the log file? Should we auto-truncate it?
		$errnum = 0;
		$errmsg = '';
		$arr = $this->_fetch($this->_query("SELECT COUNT(" . $field . ") AS counter FROM " . $this->_mapTableName($table), $errnum, $errmsg));
		if ($arr['counter'] > $max_history)
		{
			// Truncate the log to the size specified

			$start = time();
			$cutoff = 3; 		// 3 seconds max cutoff time to avoid timeouts

			$limit = 100;
			$list = array();

			$errnum = 0;
			$errmsg = '';
			$res = $this->_query($sql . floor($max_history / 2), $errnum, $errmsg);
			while ($arr = $this->_fetch($res) and time() - $start < $cutoff)
			{
				// Delete it batches of $limit, keep under $cutoff seconds
				$list[] = current($arr);

				if (count($list) > $limit)
				{
					$errnum = 0;
					$errmsg = '';

					$this->_query("DELETE FROM " . $this->_mapTableName($table) . " WHERE " . $field . " IN ( " . implode(', ', $list) . " )", $errnum, $errmsg);
					$list = array();
				}
			}
		}

		return;
	}
protected function _truncate($table, $max_history)
	{
		// Don't do this all the time...
		if (mt_rand(0, 10) == 1)
		{
			return;
		}

		// We only need to run this once per table per HTTP session, so keep track of if we've alrealdy run or not
		static $runs = array();
		if (!empty($runs[$table]))
		{
			return;
		}
		$runs[$table] = true;

		// If max_history is set to -1, we *never* truncate
		if ($max_history <= 0)
		{
			return;
		}

		switch ($table)
		{
			case QUICKBOOKS_DRIVER_SQL_LOGTABLE:
				$sql = "SELECT quickbooks_log_id FROM " . $this->_mapTableName($table) . " ORDER BY quickbooks_log_id ASC LIMIT ";
				$field = 'quickbooks_log_id';
				break;
			case QUICKBOOKS_DRIVER_SQL_QUEUETABLE:
				$sql = "
					SELECT
						quickbooks_queue_id
					FROM
						" . $this->_mapTableName($table) . "
					WHERE
						qb_status IN (
							'" . QUICKBOOKS_STATUS_SUCCESS . "',
							'" . QUICKBOOKS_STATUS_HANDLED . "',
							'" . QUICKBOOKS_STATUS_CANCELLED . "',
							'" . QUICKBOOKS_STATUS_NOOP . "' )
					ORDER BY
						quickbooks_queue_id ASC LIMIT ";
				$field = 'quickbooks_queue_id';
				break;
			case QUICKBOOKS_DRIVER_SQL_TICKETTABLE:
				$sql = "SELECT quickbooks_ticket_id FROM " . $this->_mapTableName($table) . " ORDER BY quickbooks_ticket_id ASC LIMIT ";
				$field = 'quickbooks_ticket_id';
				break;
		}

		// How big is the log file? Should we auto-truncate it?
		$errnum = 0;
		$errmsg = '';
		$arr = $this->_fetch($this->_query("SELECT COUNT(" . $field . ") AS counter FROM " . $this->_mapTableName($table), $errnum, $errmsg));
		if ($arr['counter'] > $max_history)
		{
			// Truncate the log to the size specified

			$start = time();
			$cutoff = 3; 		// 3 seconds max cutoff time to avoid timeouts

			$limit = 100;
			$list = array();

			$errnum = 0;
			$errmsg = '';
			$res = $this->_query($sql . floor($max_history / 2), $errnum, $errmsg);
			while ($arr = $this->_fetch($res) and time() - $start < $cutoff)
			{
				// Delete it batches of $limit, keep under $cutoff seconds
				$list[] = current($arr);

				if (count($list) > $limit)
				{
					$errnum = 0;
					$errmsg = '';

					$this->_query("DELETE FROM " . $this->_mapTableName($table) . " WHERE " . $field . " IN ( " . implode(', ', $list) . " )", $errnum, $errmsg);
					$list = array();
				}
			}
		}

		return;
	}
Truncate (if neccessary) the log and queue tables if they grow too large

@param string $table
@param integer $max_history
@return void