Commit 4036b7c299146337b79fba782850ffbd109fd7f8

Authored by nbm
1 parent 562322d8

Database access utility class to reduce code repetition and complexity

and make it easier to make wide-spread changes in the database area.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3021 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 151 additions and 0 deletions
lib/database/dbutil.inc 0 → 100644
  1 +<?php
  2 +
  3 +/**
  4 + * $Id$
  5 + *
  6 + * Database access utility class
  7 + *
  8 + * Copyright (c) 2004 Jam Warehouse http://www.jamwarehouse.com
  9 + *
  10 + * This program is free software; you can redistribute it and/or modify
  11 + * it under the terms of the GNU General Public License as published by
  12 + * the Free Software Foundation; either version 2 of the License, or
  13 + * (at your option) any later version.
  14 + *
  15 + * This program is distributed in the hope that it will be useful,
  16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18 + * GNU General Public License for more details.
  19 + *
  20 + * You should have received a copy of the GNU General Public License
  21 + * along with this program; if not, write to the Free Software
  22 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23 + *
  24 + * @version $Revision$
  25 + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa
  26 + * @package lib.database
  27 + */
  28 +
  29 +require_once ('PEAR.php');
  30 +
  31 +class DBUtil {
  32 + function &getDB($db = null) {
  33 + global $default;
  34 + if (is_null($db)) {
  35 + return $default->_db;
  36 + }
  37 + return $db;
  38 + }
  39 +
  40 + function runQuery($query, $db = null) {
  41 + global $default;
  42 + $aParams = null;
  43 +
  44 + $db =& DBUtil::getDB($db);
  45 +
  46 + if (is_array($query)) {
  47 + $sQuery = $query[0];
  48 + $aParams = $query[1];
  49 + }
  50 + $res = $db->query($sQuery, $aParams);
  51 + $default->log->info('Query: ' . DBUtil::lastQuery($db));
  52 + return $res;
  53 + }
  54 +
  55 + function getOneResult($query, $db = null) {
  56 + $result = DBUtil::runQuery($query, $db);
  57 + if (PEAR::isError($result)) {
  58 + DBUtil::logQueryError($query, $result);
  59 + return $result;
  60 + }
  61 + $aRow = $result->fetchRow();
  62 + $result->free();
  63 + return $aRow;
  64 + }
  65 +
  66 + function getOneResultKey($query, $key, $db = null) {
  67 + $aRow = DBUtil::getOneResult($query, $db);
  68 + if (PEAR::isError($aRow)) {
  69 + return $aRow;
  70 + }
  71 + return $aRow[$key];
  72 + }
  73 +
  74 + function getResultArray($query, $db = null) {
  75 + $result = DBUtil::runQuery($query, $db);
  76 + if (PEAR::isError($result)) {
  77 + DBUtil::logQueryError($query, $result);
  78 + return $result;
  79 + }
  80 +
  81 + $aReturn = array();
  82 + while ($aRow = $result->fetchRow()) {
  83 + $aReturn[] = $aRow;
  84 + }
  85 + $result->free();
  86 + return $aReturn;
  87 + }
  88 +
  89 + function getResultArrayKey($query, $key, $db = null) {
  90 + $result = DBUtil::runQuery($query, $db);
  91 + if (PEAR::isError($result)) {
  92 + DBUtil::logQueryError($query, $result);
  93 + return $result;
  94 + }
  95 +
  96 + $aReturn = array();
  97 + while ($aRow = $result->fetchRow()) {
  98 + $aReturn[] = $aRow[$key];
  99 + }
  100 + $result->free();
  101 + return $aReturn;
  102 + }
  103 +
  104 + function logQueryError($query, $result) {
  105 + global $default;
  106 + $default->log->error($result->toString());
  107 + }
  108 +
  109 + function nextId($seqname, $ondemand = false, $db = null) {
  110 + $db =& DBUtil::getDB($db);
  111 +
  112 + return $db->nextId($seqname, $ondemand);
  113 + }
  114 +
  115 + function runQueries($aQueries, $db = null) {
  116 + foreach ($aQueries as $sQuery) {
  117 + $res = DBUtil::runQuery($sQuery, $db);
  118 + if (PEAR::isError($res)) {
  119 + return $res;
  120 + }
  121 + }
  122 + return true;
  123 + }
  124 +
  125 + function &autoInsert($sTable, $aFieldValues, $db = null) {
  126 + global $default;
  127 + $default->log->info('AutoInsert called for table ' . $sTable);
  128 + global $default;
  129 + $db =& DBUtil::getDB();
  130 + if (!array_key_exists('id', $aFieldValues)) {
  131 + $aFieldValues['id'] = DBUtil::nextId($sTable, null, $db);
  132 + }
  133 + $res = $db->autoExecute($sTable, $aFieldValues);
  134 + $default->log->info('Query: ' . DBUtil::lastQuery($db));
  135 + if ($res === DB_OK) {
  136 + return $aFieldValues['id'];
  137 + }
  138 + if (PEAR::isError($res)) {
  139 + return $res;
  140 + }
  141 + return PEAR::raiseError('Unknown return value for autoInsert');
  142 + }
  143 +
  144 + function &lastQuery($db) {
  145 + $db =& DBUtil::getDB();
  146 + return $db->last_query;
  147 + }
  148 +
  149 +}
  150 +
  151 +?>