1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
<?php
abstract class dbeng_abs {
protected $_batchInsertChunks = 500;
private $_error = '';
/*
* Connects to the database
*/
abstract function connect();
/*
* Executes the query and discards any output. Returns true of no
* error was found. No handling of the SQL statement is done
*/
abstract function rawExec($sql);
/*
* Executes the query with $params as parameters. All parameters are
* parsed through sthe safe() function to prevent SQL injection.
*
* Returns a single associative array when query succeeds, returns
* an exception when the query fails.
*/
abstract function singleQuery($sql, $params = array());
/*
* Executes the query with $params as parameters. All parameters are
* parsed through sthe safe() function to prevent SQL injection.
*
*
* Returns an array of associative arrays when query succeeds, returns
* an exception when the query fails.
*/
abstract function arrayQuery($sql, $params = array());
/*
* Database specific 'escape' or 'safe' function to escape strings
*/
abstract function safe($s);
/*
* Returns a database specific representation of a boolean value
*/
abstract function bool2dt($b);
/*
* Returns the amount of effected rows
*/
abstract function rows();
/*
* Begins an transaction
*/
abstract function beginTransaction();
/*
* Commits an transaction
*/
abstract function commit();
/*
* Rolls back an transaction
*/
abstract function rollback();
/*
* Returns the last insertid
*/
abstract function lastInsertId($tableName);
/*
* Prepares the query string by running vsprintf() met safe() thrown around it
*/
function prepareSql($s, $p) {
/*
* When no parameters are given, we don't run vsprintf(). This makes sure
* we can use arrayQuery() and singleQuery() with for example LIKE statements
*/
if (empty($p)) {
return $s;
} else {
$p = array_map(array($this, 'safe'), $p);
return vsprintf($s, $p);
} # else
} # prepareSql()
/*
* Executes the query and returns the (resource or handle)
*/
function exec($s, $p = array()) {
return $this->rawExec($this->prepareSql($s, $p));
} # exec()
/*
* INSERT or UPDATE statement, doesn't return anything. Exception
* thrown if a error occurs
*/
abstract function modify($s, $p = array());
/*
* Transforms an array of keys to an list usable by an
* IN statement
*/
function arrayKeyToIn($ar) {
$tmpList = '';
foreach($ar as $k => $v) {
$tmpList .= "'" . $this->safe($k) . "', ";
} # foreach
return substr($tmpList, 0, -2);
} # arrayKeyToIn
/*
* Transforms an array of values to an list usable by an
* IN statement
*/
function arrayValToInOffset($ar, $val, $valOffset, $valEnd) {
$tmpList = '';
foreach($ar as $k => $v) {
$tmpList .= "'" . $this->safe(substr($v[$val], $valOffset, $valEnd)) . "', ";
} # foreach
return substr($tmpList, 0, -2);
} # arrayValToInOffset
/*
* Transforms an array of values to an list usable by an
* IN statement
*/
function arrayValToIn($ar, $val) {
$tmpList = '';
foreach($ar as $k => $v) {
$tmpList .= "'" . $this->safe($v[$val]) . "', ";
} # foreach
return substr($tmpList, 0, -2);
} # arrayValToIn
/*
* Transforms an array of values to an list usable by an
* IN statement
*/
function batchInsert($ar, $sql, $tpl, $fields) {
$this->beginTransaction();
/*
* Databases usually have a maximum packet size length,
* so just sending down 100kbyte of text usually ends
* up in tears.
*/
$chunks = array_chunk($ar, $this->_batchInsertChunks);
foreach($chunks as $items) {
$insertArray = array();
foreach($items as $item) {
/*
* Add this items' fields to an array in
* the correct order and nicely escaped
* from any injection
*/
$itemValues = array();
foreach($fields as $idx => $field) {
$itemValues[] = $this->safe($item[$field]);
} # foreach
$insertArray[] = vsprintf($tpl, $itemValues);
} # foreach
# Actually insert the batch
if (!empty($insertArray)) {
$this->modify($sql . implode(',', $insertArray), array());
} # if
} # foreach
$this->commit();
} # batchInsert
} # dbeng_abs
|