/** * Load a value from cache. * * @param string $id * @param string $time * @return mixed */ public function load($id, $time) { $value = false; // Determine if the value already exists. $rows = []; $this->prepare("SELECT * FROM " . $this->table . " WHERE id = :id")->bindParams(['id' => sha1($id)])->execute(); if ($this->isPdo) { while (($row = $this->result->fetchAll(\PDO::FETCH_ASSOC)) != false) { $rows[] = $row; } } else { while (($row = $this->result->fetchArray(SQLITE3_ASSOC)) != false) { $rows[] = $row; } } // If the value is found, check expiration and return. if (count($rows) > 0) { $data = $rows[0]['value']; $timestamp = $rows[0]['time']; if ($timestamp == 0 || time() - $timestamp <= $time) { $value = unserialize($data); } } return $value; }
/** * Get the number of rows in the result set * * @return int */ public function count() { switch ($this->mode) { case "mysql": $rows = $this->result->num_rows; break; case "postgres": case "redshift": $rows = pg_num_rows($this->result); break; case "odbc": $rows = odbc_num_rows($this->result); # The above function is unreliable, so if we got a zero count then double check it if ($rows < 1) { $rows = 0; /** * If it is an update/delete then we just have to trust the odbc_num_rows() result, * however it is some kind of select, then we can manually count the rows returned. */ if (odbc_num_fields($this->result) > 0) { $position = $this->position; $this->seek(0); while ($this->getNextRow()) { ++$rows; } $this->seek($position); } } break; case "sqlite": $rows = 0; while ($this->result->fetchArray()) { $rows++; } $this->seek($this->position); break; case "mssql": $rows = mssql_num_rows($this->result); break; } if ($rows === false || $rows < 0) { throw new \Exception("Failed to get the row count from the result set"); } return $rows; }
/** * Get the lifetime of the value. * * @param string $id * @return int */ public function getLifetime($id) { // Determine if the value already exists. $rows = []; $value = 0; $this->prepare('SELECT * FROM "' . $this->table . '" WHERE "id" = :id')->bindParams(['id' => sha1($id)])->execute(); if ($this->isPdo) { while (($row = $this->result->fetchAll(\PDO::FETCH_ASSOC)) != false) { $rows[] = $row; } } else { while (($row = $this->result->fetchArray(SQLITE3_ASSOC)) != false) { $rows[] = $row; } } // If the value is found, check expiration and return. if (count($rows) > 0) { $cacheValue = $rows[0]; $value = $cacheValue['lifetime']; } return $value; }
/** * Fetch an array from a database result set * * @param resource $res * @return array */ protected function _fetch($res) { return $res->fetchArray(SQLITE3_ASSOC); }
/** * Fetches a complete result as an object * * @param resource $result Resultset * * @throws Exception * * @return array of stdClass */ public function fetchAll($result) { $ret = array(); if (false === $result) { throw new Exception('Error while fetching result: ' . $this->error()); } $result->fetchedByPMF = true; while ($row = $result->fetchArray(SQLITE3_ASSOC)) { $ret[] = (object) $row; } return $ret; }
/** * Copies the contents of from_table in the first database into the * to a to_table of suitable schema in a second database. It assumes the * table exists in both databases * * @param string $from_table name of the table to be copied from * @param resource $from_dbm database resource for the from table * @param resource $to_table name of the table to be copied to * @param resource $to_dbm database resource for the to table */ static function copyTable($from_table, $from_dbm, $to_table, $to_dbm) { $sql = "SELECT * FROM {$from_table}"; if (($result = $from_dbm->execute($sql)) === false) { return false; } while ($row = $from_dbm->fetchArray($result)) { $statement = "INSERT INTO {$to_table} VALUES ("; $comma = ""; foreach ($row as $col => $value) { $statement .= $comma . " '" . $to_dbm->escapeString($value) . "'"; $comma = ","; } $statement .= ")"; if ($to_dbm->execute($statement) === false) { return false; } } return true; }
public function nextRecord() { // Coalesce rather than replace common fields. if ($data = @$this->handle->fetchArray(SQLITE3_NUM)) { foreach ($data as $columnIdx => $value) { if (preg_match('/^"([a-z0-9_]+)"\\."([a-z0-9_]+)"$/i', $this->handle->columnName($columnIdx), $matches)) { $columnName = $matches[2]; } else { if (preg_match('/^"([a-z0-9_]+)"$/i', $this->handle->columnName($columnIdx), $matches)) { $columnName = $matches[1]; } else { $columnName = trim($this->handle->columnName($columnIdx), "\"' \t"); } } // $value || !$ouput[$columnName] means that the *last* occurring value is shown // !$ouput[$columnName] means that the *first* occurring value is shown if (isset($value) || !isset($output[$columnName])) { $output[$columnName] = is_null($value) ? null : (string) $value; } } return $output; } else { return false; } }
/** * Returns an array containing all of the result set rows * * @param integer $fetchStyle Controls how the next row will be returned to the caller. * This value must be one of the Doctrine_Core::FETCH_* constants, * defaulting to Doctrine_Core::FETCH_BOTH * * @param integer $columnIndex Returns the indicated 0-indexed column when the value of $fetchStyle is * Doctrine_Core::FETCH_COLUMN. Defaults to 0. * * @return array */ public function fetchAll($fetchStyle = Doctrine_Core::FETCH_BOTH, $colnum = 0) { return $this->connection->fetchArray($this->result, $fetchStyle); }
public function nextRecord() { if ($data = $this->handle->fetchArray(SQLITE3_ASSOC)) { return $data; } else { return false; } }