53 function db_extras_subquery(&$db, $sql, $subs, $default_value=null)
56 switch ($db->phptype) {
62 for ($i = 0; $i < count($subs); $i++) {
63 $find[] =
'~SQ'.$i.
'~';
66 $sql = str_replace($find, $subs, $sql);
76 for ($i = 0; $i < count($subs); $i++) {
77 $find[$i] =
'~SQ'.$i.
'~';
82 for ($j = 0, $total= count($result); $j < $total; $j++) {
83 $replace[$i] .= (($replace[$i]) ?
',' :
'').MatrixDAL::quote($result[$j]);
90 $sql = str_replace($find, $replace, $sql);
112 function db_extras_insert_select(&$db, $insert, $select)
115 switch ($db->phptype) {
121 if (DB::isError($result))
return $result;
129 $select_result = $db->query($select);
130 if (DB::isError($select_result))
return $select_result;
133 while (DB_OK === $select_result->fetchInto($row, DB_FETCHMODE_ORDERED)) {
134 $field_total = count($row);
135 if (!$field_total)
continue;
136 $sql = $insert.
' VALUES (';
137 for ($j = 0; $j < $field_total; $j++) {
138 $sql .= (($j) ?
',' :
'').MatrixDAL::quote($row[$j]);
142 $insert_result = $db->query($sql);
143 if (DB::isError($insert_result)) {
144 $select_result->free();
145 return $insert_result;
148 $select_result->free();
174 function db_extras_bitand($db_type, $val1, $val2)
179 if ($db_type ==
'oci') {
180 $bit_op =
'BITAND('.$val1.
', '.$val2.
')';
182 $bit_op =
'('.$val1.
' & '.$val2.
')';
202 function db_extras_todate($db_type, $iso8601_date, $add_quotes=TRUE)
205 if ($add_quotes === TRUE) {
206 $iso8601_date =
'\''.$iso8601_date.
'\'';
209 if ($db_type ==
'pgsql') {
210 return 'TO_TIMESTAMP('.$iso8601_date.
', \'YYYY-MM-DD HH24:MI:SS\')';
211 }
else if ($db_type ==
'oci') {
212 return 'TO_DATE('.$iso8601_date.
',\'YYYY-MM-DD HH24:MI:SS\')';
226 function db_extras_prepare_todate($db_type)
228 if ($db_type ==
'pgsql') {
229 return 'TO_TIMESTAMP(?, ?)';
230 }
else if ($db_type ==
'oci8') {
231 return 'TO_DATE(?, ?)';
248 function db_get_function_result($db_type, $sql, $bind_vars=Array())
253 if ($db_type ==
'pgsql') {
254 $sql =
'SELECT '.$sql;
256 foreach ($bind_vars as $key => $value) {
261 }
else if ($db_type ==
'oci') {
265 $sql =
'BEGIN :res := '.$sql.
'; END;';
272 foreach ($bind_vars as $key => $value) {
277 }
catch (Exception $e) {
278 throw new Exception(
'Could not run function due to database error: '.$e->getMessage());
299 function db_extras_modify_limit_clause($sql, $db_type, $limit, $offset=0)
301 if ($db_type ==
'oci') {
303 $inner_sql =
'SELECT original_query.*, rownum rnum FROM (' . $sql .
') original_query WHERE rownum <= ' . ($offset + $limit);
304 $sql =
'SELECT * FROM (' . $inner_sql .
') WHERE rnum > ' . $offset;
306 $sql =
'SELECT * FROM ('.$sql.
')';
307 $sql .=
' WHERE ROWNUM BETWEEN 0 AND '.$limit;
310 $sql = $sql.
' LIMIT '.$limit.
' OFFSET '.$offset;