2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2008-04-09 01:10:56 +00:00
|
|
|
require_once "compat.inc.php";
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-07-21 06:36:12 +00:00
|
|
|
/** @privatesection */
|
2009-07-19 07:38:13 +00:00
|
|
|
// Querylet {{{
|
2007-12-06 11:01:18 +00:00
|
|
|
class Querylet {
|
2007-04-16 11:58:25 +00:00
|
|
|
var $sql;
|
|
|
|
var $variables;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-02-06 17:24:08 +00:00
|
|
|
public function Querylet($sql, $variables=array()) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->sql = $sql;
|
|
|
|
$this->variables = $variables;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function append($querylet) {
|
2008-02-06 17:24:08 +00:00
|
|
|
assert(!is_null($querylet));
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->sql .= $querylet->sql;
|
|
|
|
$this->variables = array_merge($this->variables, $querylet->variables);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function append_sql($sql) {
|
|
|
|
$this->sql .= $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_variable($var) {
|
|
|
|
$this->variables[] = $var;
|
|
|
|
}
|
2008-05-19 15:59:58 +00:00
|
|
|
}
|
|
|
|
class TagQuerylet {
|
|
|
|
var $tag;
|
|
|
|
var $positive;
|
|
|
|
|
|
|
|
public function TagQuerylet($tag, $positive) {
|
|
|
|
$this->tag = $tag;
|
|
|
|
$this->positive = $positive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class ImgQuerylet {
|
|
|
|
var $qlet;
|
|
|
|
var $positive;
|
|
|
|
|
|
|
|
public function ImgQuerylet($qlet, $positive) {
|
|
|
|
$this->qlet = $qlet;
|
|
|
|
$this->positive = $positive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
2009-01-19 18:18:23 +00:00
|
|
|
// {{{ db engines
|
2008-05-18 02:14:51 +00:00
|
|
|
class DBEngine {
|
|
|
|
var $name = null;
|
2009-01-22 12:05:55 +00:00
|
|
|
|
2010-02-01 16:13:02 +00:00
|
|
|
public function init($db) {}
|
2009-01-22 13:53:30 +00:00
|
|
|
|
2009-12-30 08:54:04 +00:00
|
|
|
public function scoreql_to_sql($scoreql) {
|
|
|
|
return $scoreql;
|
|
|
|
}
|
|
|
|
|
2009-01-22 12:05:55 +00:00
|
|
|
public function create_table_sql($name, $data) {
|
|
|
|
return "CREATE TABLE $name ($data)";
|
|
|
|
}
|
2008-05-18 02:14:51 +00:00
|
|
|
}
|
|
|
|
class MySQL extends DBEngine {
|
|
|
|
var $name = "mysql";
|
2008-08-11 20:27:24 +00:00
|
|
|
|
2009-01-22 12:05:55 +00:00
|
|
|
public function init($db) {
|
2010-12-31 19:29:15 +00:00
|
|
|
$db->query("SET NAMES utf8;");
|
2008-08-11 20:27:24 +00:00
|
|
|
}
|
2009-01-22 12:05:55 +00:00
|
|
|
|
2009-12-30 08:54:04 +00:00
|
|
|
public function scoreql_to_sql($data) {
|
2009-01-22 12:11:43 +00:00
|
|
|
$data = str_replace("SCORE_AIPK", "INTEGER PRIMARY KEY auto_increment", $data);
|
|
|
|
$data = str_replace("SCORE_INET", "CHAR(15)", $data);
|
|
|
|
$data = str_replace("SCORE_BOOL_Y", "'Y'", $data);
|
|
|
|
$data = str_replace("SCORE_BOOL_N", "'N'", $data);
|
2009-01-22 12:14:38 +00:00
|
|
|
$data = str_replace("SCORE_BOOL", "ENUM('Y', 'N')", $data);
|
2009-07-15 22:29:14 +00:00
|
|
|
$data = str_replace("SCORE_DATETIME", "DATETIME", $data);
|
2009-01-22 12:12:15 +00:00
|
|
|
$data = str_replace("SCORE_NOW", "\"1970-01-01\"", $data);
|
2010-02-02 02:13:45 +00:00
|
|
|
$data = str_replace("SCORE_STRNORM", "", $data);
|
2009-12-30 08:54:04 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create_table_sql($name, $data) {
|
|
|
|
$data = $this->scoreql_to_sql($data);
|
2009-01-22 12:05:55 +00:00
|
|
|
$ctes = "TYPE=InnoDB DEFAULT CHARSET='utf8'";
|
|
|
|
return "CREATE TABLE $name ($data) $ctes";
|
|
|
|
}
|
2008-05-18 02:14:51 +00:00
|
|
|
}
|
|
|
|
class PostgreSQL extends DBEngine {
|
|
|
|
var $name = "pgsql";
|
2008-08-11 20:27:24 +00:00
|
|
|
|
2009-12-30 08:54:04 +00:00
|
|
|
public function scoreql_to_sql($data) {
|
2009-01-22 12:11:43 +00:00
|
|
|
$data = str_replace("SCORE_AIPK", "SERIAL PRIMARY KEY", $data);
|
|
|
|
$data = str_replace("SCORE_INET", "INET", $data);
|
|
|
|
$data = str_replace("SCORE_BOOL_Y", "'t'", $data);
|
|
|
|
$data = str_replace("SCORE_BOOL_N", "'f'", $data);
|
2009-01-22 12:14:38 +00:00
|
|
|
$data = str_replace("SCORE_BOOL", "BOOL", $data);
|
2009-07-15 22:29:14 +00:00
|
|
|
$data = str_replace("SCORE_DATETIME", "TIMESTAMP", $data);
|
2009-01-22 13:53:30 +00:00
|
|
|
$data = str_replace("SCORE_NOW", "current_time", $data);
|
2010-02-02 02:13:45 +00:00
|
|
|
$data = str_replace("SCORE_STRNORM", "lower", $data);
|
2009-12-30 08:54:04 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create_table_sql($name, $data) {
|
|
|
|
$data = $this->scoreql_to_sql($data);
|
2009-01-22 13:53:30 +00:00
|
|
|
return "CREATE TABLE $name ($data)";
|
|
|
|
}
|
|
|
|
}
|
2009-01-25 12:10:10 +00:00
|
|
|
|
|
|
|
// shimmie functions for export to sqlite
|
|
|
|
function _unix_timestamp($date) { return strtotime($date); }
|
|
|
|
function _now() { return date("Y-m-d h:i:s"); }
|
|
|
|
function _floor($a) { return floor($a); }
|
2009-07-17 00:55:07 +00:00
|
|
|
function _log($a, $b=null) {
|
|
|
|
if(is_null($b)) return log($a);
|
|
|
|
else return log($a, $b);
|
|
|
|
}
|
2009-01-25 12:10:10 +00:00
|
|
|
function _isnull($a) { return is_null($a); }
|
|
|
|
function _md5($a) { return md5($a); }
|
|
|
|
function _concat($a, $b) { return $a . $b; }
|
|
|
|
function _lower($a) { return strtolower($a); }
|
|
|
|
|
2009-01-22 13:53:30 +00:00
|
|
|
class SQLite extends DBEngine {
|
|
|
|
var $name = "sqlite";
|
|
|
|
|
2009-01-22 15:08:37 +00:00
|
|
|
public function init($db) {
|
2009-01-25 12:10:10 +00:00
|
|
|
ini_set('sqlite.assoc_case', 0);
|
2010-02-01 16:13:02 +00:00
|
|
|
$db->execute("PRAGMA foreign_keys = ON;");
|
2009-01-25 12:10:10 +00:00
|
|
|
@sqlite_create_function($db->_connectionID, 'UNIX_TIMESTAMP', '_unix_timestamp', 1);
|
|
|
|
@sqlite_create_function($db->_connectionID, 'now', '_now', 0);
|
|
|
|
@sqlite_create_function($db->_connectionID, 'floor', '_floor', 1);
|
2009-07-17 00:55:07 +00:00
|
|
|
@sqlite_create_function($db->_connectionID, 'log', '_log');
|
2009-01-25 12:10:10 +00:00
|
|
|
@sqlite_create_function($db->_connectionID, 'isnull', '_isnull', 1);
|
|
|
|
@sqlite_create_function($db->_connectionID, 'md5', '_md5', 1);
|
|
|
|
@sqlite_create_function($db->_connectionID, 'concat', '_concat', 2);
|
|
|
|
@sqlite_create_function($db->_connectionID, 'lower', '_lower', 1);
|
2009-01-22 15:08:37 +00:00
|
|
|
}
|
|
|
|
|
2009-01-22 13:53:30 +00:00
|
|
|
public function create_table_sql($name, $data) {
|
|
|
|
$data = str_replace("SCORE_AIPK", "INTEGER PRIMARY KEY", $data);
|
2009-01-22 15:08:37 +00:00
|
|
|
$data = str_replace("SCORE_INET", "VARCHAR(15)", $data);
|
2009-01-22 13:53:30 +00:00
|
|
|
$data = str_replace("SCORE_BOOL_Y", "'Y'", $data);
|
|
|
|
$data = str_replace("SCORE_BOOL_N", "'N'", $data);
|
2009-01-22 15:08:37 +00:00
|
|
|
$data = str_replace("SCORE_BOOL", "CHAR(1)", $data);
|
|
|
|
$data = str_replace("SCORE_NOW", "\"1970-01-01\"", $data);
|
2010-02-02 02:13:45 +00:00
|
|
|
$data = str_replace("SCORE_STRNORM", "", $data);
|
2009-01-22 15:08:37 +00:00
|
|
|
$cols = array();
|
|
|
|
$extras = "";
|
|
|
|
foreach(explode(",", $data) as $bit) {
|
|
|
|
$matches = array();
|
2009-01-22 15:51:50 +00:00
|
|
|
if(preg_match("/INDEX\s*\((.*)\)/", $bit, $matches)) {
|
2009-01-22 15:08:37 +00:00
|
|
|
$col = $matches[1];
|
|
|
|
$extras .= "CREATE INDEX {$name}_{$col} on $name($col);";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$cols[] = $bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$cols_redone = implode(", ", $cols);
|
|
|
|
return "CREATE TABLE $name ($cols_redone); $extras";
|
2008-08-11 20:27:24 +00:00
|
|
|
}
|
2008-05-18 02:14:51 +00:00
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
// }}}
|
|
|
|
// {{{ cache engines
|
|
|
|
interface CacheEngine {
|
2009-01-20 10:47:20 +00:00
|
|
|
public function get($key);
|
2009-01-20 11:54:43 +00:00
|
|
|
public function set($key, $val, $time=0);
|
2009-01-20 10:47:20 +00:00
|
|
|
public function delete($key);
|
2009-01-20 11:54:43 +00:00
|
|
|
|
|
|
|
public function get_hits();
|
|
|
|
public function get_misses();
|
2009-01-20 10:47:20 +00:00
|
|
|
}
|
|
|
|
class NoCache implements CacheEngine {
|
|
|
|
public function get($key) {return false;}
|
2009-01-20 11:54:43 +00:00
|
|
|
public function set($key, $val, $time=0) {}
|
2009-01-20 10:47:20 +00:00
|
|
|
public function delete($key) {}
|
2009-01-20 11:54:43 +00:00
|
|
|
|
|
|
|
public function get_hits() {return 0;}
|
|
|
|
public function get_misses() {return 0;}
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
2010-02-01 16:17:00 +00:00
|
|
|
class MemcacheCache implements CacheEngine {
|
2010-05-15 13:53:37 +00:00
|
|
|
var $memcache=null, $hits=0, $misses=0;
|
2009-01-20 11:54:43 +00:00
|
|
|
|
2009-01-20 10:47:20 +00:00
|
|
|
public function __construct($args) {
|
2010-02-09 15:13:24 +00:00
|
|
|
$hp = split(":", $args);
|
2010-05-15 13:53:37 +00:00
|
|
|
if(class_exists("Memcache")) {
|
|
|
|
$this->memcache = new Memcache;
|
|
|
|
@$this->memcache->pconnect($hp[0], $hp[1]);
|
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
|
|
|
|
2009-01-20 10:47:20 +00:00
|
|
|
public function get($key) {
|
2009-01-19 18:18:23 +00:00
|
|
|
assert(!is_null($key));
|
2009-01-20 10:47:20 +00:00
|
|
|
$val = $this->memcache->get($key);
|
2009-01-19 18:18:23 +00:00
|
|
|
if($val) {
|
2009-01-20 10:47:20 +00:00
|
|
|
$this->hits++;
|
2009-01-19 18:18:23 +00:00
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
else {
|
2009-01-20 10:47:20 +00:00
|
|
|
$this->misses++;
|
2009-01-19 18:18:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 10:47:20 +00:00
|
|
|
public function set($key, $val, $time=0) {
|
2009-01-19 18:18:23 +00:00
|
|
|
assert(!is_null($key));
|
2009-01-20 10:47:20 +00:00
|
|
|
$this->memcache->set($key, $val, false, $time);
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
|
|
|
|
2009-01-20 10:47:20 +00:00
|
|
|
public function delete($key) {
|
2009-01-19 18:18:23 +00:00
|
|
|
assert(!is_null($key));
|
2009-01-20 10:47:20 +00:00
|
|
|
$this->memcache->delete($key);
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
2009-01-20 11:54:43 +00:00
|
|
|
|
|
|
|
public function get_hits() {return $this->hits;}
|
|
|
|
public function get_misses() {return $this->misses;}
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
2010-02-02 17:12:40 +00:00
|
|
|
class APCCache implements CacheEngine {
|
|
|
|
var $hits=0, $misses=0;
|
|
|
|
|
|
|
|
public function __construct($args) {}
|
|
|
|
|
|
|
|
public function get($key) {
|
|
|
|
assert(!is_null($key));
|
|
|
|
$val = apc_fetch($key);
|
|
|
|
if($val) {
|
|
|
|
$this->hits++;
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->misses++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set($key, $val, $time=0) {
|
|
|
|
assert(!is_null($key));
|
|
|
|
apc_store($key, $val, $time);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($key) {
|
|
|
|
assert(!is_null($key));
|
|
|
|
apc_delete($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_hits() {return $this->hits;}
|
|
|
|
public function get_misses() {return $this->misses;}
|
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
// }}}
|
2009-07-21 06:36:12 +00:00
|
|
|
/** @publicsection */
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-01-03 20:32:57 +00:00
|
|
|
* A class for controlled database access
|
2007-12-06 11:01:18 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
class Database {
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2010-12-31 19:29:15 +00:00
|
|
|
* The PDO database connection object, for anyone who wants direct access
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
var $db;
|
2009-07-19 07:38:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Meta info about the database engine
|
|
|
|
*/
|
2008-05-18 02:14:51 +00:00
|
|
|
var $engine = null;
|
2009-07-19 07:38:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The currently active cache engine
|
|
|
|
*/
|
2009-01-19 18:18:23 +00:00
|
|
|
var $cache = null;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2007-12-11 18:37:11 +00:00
|
|
|
* Create a new database object using connection info
|
|
|
|
* stored in config.php in the root shimmie folder
|
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function Database() {
|
2010-02-02 17:34:45 +00:00
|
|
|
global $database_dsn, $cache_dsn;
|
2009-01-22 15:08:37 +00:00
|
|
|
|
2011-01-01 16:59:10 +00:00
|
|
|
# FIXME: detect ADODB URI, automatically translate PDO DSN
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Why does the abstraction layer act differently depending on the
|
|
|
|
* back-end? Because PHP is deliberately retarded.
|
|
|
|
*
|
|
|
|
* http://stackoverflow.com/questions/237367
|
|
|
|
*/
|
|
|
|
$matches = array(); $db_user=null; $db_pass=null;
|
|
|
|
if(preg_match("/user=([^;]*)/", $database_dsn, $matches)) $db_user=$matches[1];
|
|
|
|
if(preg_match("/password=([^;]*)/", $database_dsn, $matches)) $db_pass=$matches[1];
|
|
|
|
|
|
|
|
$this->db = new PDO($database_dsn, $db_user, $db_pass);
|
|
|
|
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
2010-12-31 19:29:15 +00:00
|
|
|
|
2011-01-01 16:59:10 +00:00
|
|
|
$db_proto = $this->db->getAttribute(PDO::ATTR_DRIVER_NAME);
|
2010-12-31 19:29:15 +00:00
|
|
|
if($db_proto == "mysql") {
|
2009-08-09 12:10:59 +00:00
|
|
|
$this->engine = new MySQL();
|
|
|
|
}
|
2010-12-31 19:29:15 +00:00
|
|
|
else if($db_proto == "pgsql") {
|
2009-08-09 12:10:59 +00:00
|
|
|
$this->engine = new PostgreSQL();
|
|
|
|
}
|
2010-12-31 19:29:15 +00:00
|
|
|
else if($db_proto == "sqlite") {
|
2009-08-09 12:10:59 +00:00
|
|
|
$this->engine = new SQLite();
|
|
|
|
}
|
2011-01-01 16:59:10 +00:00
|
|
|
else {
|
|
|
|
die("Unknown PDO driver: $db_proto");
|
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
|
2010-02-02 17:34:45 +00:00
|
|
|
if(isset($cache_dsn) && !empty($cache_dsn)) {
|
2009-08-09 12:10:59 +00:00
|
|
|
$matches = array();
|
2010-02-02 17:34:45 +00:00
|
|
|
preg_match("#(memcache|apc)://(.*)#", $cache_dsn, $matches);
|
2009-08-09 12:10:59 +00:00
|
|
|
if($matches[1] == "memcache") {
|
2010-02-01 16:17:00 +00:00
|
|
|
$this->cache = new MemcacheCache($matches[2]);
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
2010-02-02 17:34:45 +00:00
|
|
|
else if($matches[1] == "apc") {
|
2010-02-02 18:12:16 +00:00
|
|
|
$this->cache = new APCCache($matches[2]);
|
2010-02-02 17:34:45 +00:00
|
|
|
}
|
2009-08-09 12:10:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->cache = new NoCache();
|
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
|
2010-12-31 19:56:28 +00:00
|
|
|
$this->engine->init($this->db);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2010-12-31 19:29:15 +00:00
|
|
|
* Execute an SQL query and return an PDO resultset
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-05-23 23:26:11 +00:00
|
|
|
public function execute($query, $args=array()) {
|
2011-01-01 15:27:24 +00:00
|
|
|
try {
|
|
|
|
$stmt = $this->db->prepare($query);
|
|
|
|
foreach($args as $name=>$value) {
|
|
|
|
if(is_numeric($value)) {
|
|
|
|
$stmt->bindValue(":$name", $value, PDO::PARAM_INT);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$stmt->bindValue(":$name", $value, PDO::PARAM_STR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$stmt->execute();
|
|
|
|
return $stmt;
|
|
|
|
}
|
|
|
|
catch(PDOException $pdoe) {
|
|
|
|
print "Message: ".$pdoe->getMessage();
|
|
|
|
print "<p>Error: $query";
|
|
|
|
exit;
|
|
|
|
}
|
2007-08-08 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Execute an SQL query and return a 2D array
|
|
|
|
*/
|
2008-01-05 00:22:19 +00:00
|
|
|
public function get_all($query, $args=array()) {
|
2011-01-01 15:27:24 +00:00
|
|
|
return $this->execute($query, $args)->fetchAll();
|
2008-01-05 00:22:19 +00:00
|
|
|
}
|
2009-01-04 16:15:00 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Execute an SQL query and return a single row
|
|
|
|
*/
|
2008-08-23 12:05:24 +00:00
|
|
|
public function get_row($query, $args=array()) {
|
2011-01-01 15:27:24 +00:00
|
|
|
return $this->execute($query, $args)->fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an SQL query and return the first column of each row
|
|
|
|
*/
|
|
|
|
public function get_col($query, $args=array()) {
|
|
|
|
$stmt = $this->execute($query, $args);
|
|
|
|
$res = array();
|
|
|
|
foreach($stmt as $row) {
|
|
|
|
$res[] = $row[0];
|
2008-08-23 12:05:24 +00:00
|
|
|
}
|
2011-01-01 15:27:24 +00:00
|
|
|
return $res;
|
2008-08-23 12:05:24 +00:00
|
|
|
}
|
|
|
|
|
2011-01-01 16:27:56 +00:00
|
|
|
/**
|
|
|
|
* Execute an SQL query and return the the first row => the second rown
|
|
|
|
*/
|
|
|
|
public function get_pairs($query, $args=array()) {
|
|
|
|
$stmt = $this->execute($query, $args);
|
|
|
|
$res = array();
|
|
|
|
foreach($stmt as $row) {
|
|
|
|
$res[$row[0]] = $row[1];
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2011-01-01 15:27:24 +00:00
|
|
|
/**
|
|
|
|
* Execute an SQL query and return a single value
|
|
|
|
*/
|
|
|
|
public function get_one($query, $args=array()) {
|
|
|
|
$row = $this->execute($query, $args)->fetch();
|
|
|
|
return $row[0];
|
|
|
|
}
|
|
|
|
|
2011-01-03 15:18:24 +00:00
|
|
|
/**
|
|
|
|
* get the ID of the last inserted row
|
|
|
|
*/
|
|
|
|
public function get_last_insert_id() {
|
2011-01-26 12:19:18 +00:00
|
|
|
return $this->db->lastInsertId();
|
2011-01-03 15:18:24 +00:00
|
|
|
}
|
|
|
|
|
2011-01-01 15:27:24 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* Create a table from pseudo-SQL
|
|
|
|
*/
|
2009-01-22 11:22:55 +00:00
|
|
|
public function create_table($name, $data) {
|
2009-01-22 12:05:55 +00:00
|
|
|
$this->execute($this->engine->create_table_sql($name, $data));
|
2009-01-22 11:22:55 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
?>
|