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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
<?php
/**
* This is a utility class, every method is static.
*
* $Horde: horde/lib/SQL.php,v 1.10.2.5 2004/09/03 14:46:00 chuck Exp $
*
* Copyright 1999-2003 Chuck Hagenbuch <chuck@horde.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @version $Revision: 1.10.2.5 $
* @since Horde 2.2
* @package horde
*/
class Horde_SQL {
function readBlob($dbh, $table, $field, $criteria)
{
if (!count($criteria)) {
return PEAR::raiseError('You must specify the fetch criteria');
}
$where = '';
switch ($dbh->dbsyntax) {
case 'oci8':
foreach ($criteria as $key => $value) {
if (!empty($where)) {
$where .= ' AND ';
}
if (empty($value)) {
$where .= $key . ' IS NULL';
} else {
$where .= $key . ' = ' . $dbh->quote($value);
}
}
$statement = OCIParse($dbh->connection,
sprintf('SELECT %s FROM %s WHERE %s',
$field, $table, $where));
OCIExecute($statement);
if (OCIFetchInto($statement, $lob)) {
$result = $lob[0]->load();
} else {
$result = PEAR::raiseError('Unable to load SQL Data');
}
OCIFreeStatement($statement);
break;
default:
foreach ($criteria as $key => $value) {
if (!empty($where)) {
$where .= ' AND ';
}
$where .= $key . ' = ' . $dbh->quote($value);
}
$result = $dbh->getOne(sprintf('SELECT %s FROM %s WHERE %s',
$field, $table, $where));
switch ($dbh->dbsyntax) {
case 'pgsql':
$result = pack('H' . strlen($result), $result);
break;
}
}
return $result;
}
function insertBlob($dbh, $table, $field, $data, $attributes)
{
$fields = array();
$values = array();
switch ($dbh->dbsyntax) {
case 'oci8':
foreach ($attributes as $key => $value) {
$fields[] = $key;
$values[] = $dbh->quote($value);
}
$statement = OCIParse($dbh->connection,
sprintf('INSERT INTO %s (%s, %s)' .
' VALUES (%s, EMPTY_BLOB()) RETURNING %s INTO :blob',
$table,
implode(', ', $fields),
$field,
implode(', ', $values),
$field));
$lob = OCINewDescriptor($dbh->connection);
OCIBindByName($statement, ':blob', $lob, -1, SQLT_BLOB);
OCIExecute($statement, OCI_DEFAULT);
$lob->save($data);
$result = OCICommit($dbh->connection);
$lob->free();
OCIFreeStatement($statement);
return $result ? true : PEAR::raiseError('Unknown Error');
default:
foreach ($attributes as $key => $value) {
$fields[] = $key;
$values[] = $value;
}
$query = sprintf('INSERT INTO %s (%s, %s) VALUES (%s)',
$table,
implode(', ', $fields),
$field,
'?' . str_repeat(', ?', count($values)));
break;
}
switch ($dbh->dbsyntax) {
case 'mssql':
case 'pgsql':
$values[] = bin2hex($data);
break;
default:
$values[] = $data;
}
/* Log the query at a DEBUG log level. */
Horde::logMessage(sprintf('SQL Query by Horde_SQL::insertBlob(): query = "%s"', $query),
__FILE__, __LINE__, LOG_DEBUG);
/* Execute the query. */
$stmt = $this->_db->prepare($query);
return $this->_db->execute($stmt, $values);
}
function updateBlob($dbh, $table, $field, $data, $where, $alsoupdate)
{
$fields = array();
$values = array();
switch ($dbh->dbsyntax) {
case 'oci8':
$wherestring = '';
foreach ($where as $key => $value) {
if (!empty($wherestring)) {
$wherestring .= ' AND ';
}
$wherestring .= $key . ' = ' . $dbh->quote($value);
}
$statement = OCIParse($dbh->connection,
sprintf('SELECT %s FROM %s WHERE %s FOR UPDATE',
$field,
$table,
$wherestring));
OCIExecute($statement, OCI_DEFAULT);
OCIFetchInto($statement, $lob);
$lob[0]->save($data);
$result = OCICommit($dbh->connection);
$lob[0]->free();
OCIFreeStatement($statement);
return $result ? true : PEAR::raiseError('Unknown Error');
default:
$updatestring = '';
$values = array();
foreach ($alsoupdate as $key => $value) {
$updatestring .= $key . ' = ?, ';
$values[] = $value;
}
$updatestring .= $field . ' = ?';
switch ($dbh->dbsyntax) {
case 'mssql':
case 'pgsql':
$values[] = bin2hex($data);
break;
default:
$values[] = $data;
}
$wherestring = '';
foreach ($where as $key => $value) {
if (!empty($wherestring)) {
$wherestring .= ' AND ';
}
$wherestring .= $key . ' = ?';
$values[] = $value;
}
$query = sprintf('UPDATE %s SET %s WHERE %s',
$table,
$updatestring,
$wherestring);
break;
}
/* Log the query at a DEBUG log level. */
Horde::logMessage(sprintf('SQL Query by Horde_SQL::updateBlob(): query = "%s"', $query),
__FILE__, __LINE__, LOG_DEBUG);
/* Execute the query. */
$stmt = $dbh->prepare($query);
return $dbh->execute($stmt, $values);
}
}
|