![]() |
|---|
| [ Index ] |
Source Code Reference for V1.00 |
[Summary view] [Print] [Text view]
1 <?php /* $Id: db_adodb.php 92 2008-03-17 03:41:32Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/includes/db_adodb.php $ */ 2 /* 3 Based on Leo West's (west_leo@yahooREMOVEME.com): 4 lib.DB 5 Database abstract layer 6 ----------------------- 7 ADODB VERSION 8 ----------------------- 9 A generic database layer providing a set of low to middle level functions 10 originally written for WEBO project, see webo source for "real life" usages 11 */ 12 if (!defined('W2P_BASE_DIR')) { 13 die('You should not access this file directly.'); 14 } 15 16 require_once (W2P_BASE_DIR . '/lib/adodb/adodb.inc.php'); 17 18 $db = NewADOConnection(w2PgetConfig('dbtype')); 19 20 function db_connect($host = 'localhost', $dbname, $user = 'root', $passwd = '', $persist = false) { 21 global $db, $ADODB_FETCH_MODE; 22 23 switch (strtolower(trim(w2PgetConfig('dbtype')))) { 24 case 'oci8': 25 case 'oracle': 26 if ($persist) { 27 $db->PConnect($host, $user, $passwd, $dbname) or die('FATAL ERROR: Connection to database server failed'); 28 } else { 29 $db->Connect($host, $user, $passwd, $dbname) or die('FATAL ERROR: Connection to database server failed'); 30 } 31 if (!defined('ADODB_ASSOC_CASE')) define('ADODB_ASSOC_CASE', 0); 32 break; 33 default: 34 //mySQL 35 if ($persist) { 36 $db->PConnect($host, $user, $passwd, $dbname) or die('FATAL ERROR: Connection to database server failed'); 37 } else { 38 $db->Connect($host, $user, $passwd, $dbname) or die('FATAL ERROR: Connection to database server failed'); 39 } 40 } 41 42 $ADODB_FETCH_MODE = ADODB_FETCH_BOTH; 43 } 44 45 function db_error() { 46 global $db; 47 if (!is_object($db)) { 48 dprint(__file__, __line__, 0, 'Database object does not exist.'); 49 } 50 return $db->ErrorMsg(); 51 } 52 53 function db_errno() { 54 global $db; 55 if (!is_object($db)) { 56 dprint(__file__, __line__, 0, 'Database object does not exist.'); 57 } 58 return $db->ErrorNo(); 59 } 60 61 function db_insert_id() { 62 global $db; 63 if (!is_object($db)) { 64 dprint(__file__, __line__, 0, 'Database object does not exist.'); 65 } 66 return $db->Insert_ID(); 67 } 68 69 function db_exec($sql) { 70 global $db, $w2p_performance_dbtime, $w2p_performance_old_dbqueries; 71 72 if (W2P_PERFORMANCE_DEBUG) { 73 $startTime = array_sum(explode(' ', microtime())); 74 } 75 76 if (!is_object($db)) { 77 dprint(__file__, __line__, 0, 'Database object does not exist.'); 78 } 79 $qid = $db->Execute($sql); 80 dprint(__file__, __line__, 10, $sql); 81 if ($msg = db_error()) { 82 global $AppUI; 83 dprint(__file__, __line__, 0, "Error executing: <pre>$sql</pre>"); 84 // Useless statement, but it is being executed only on error, 85 // and it stops infinite loop. 86 $db->Execute($sql); 87 if (!db_error()) { 88 echo '<script language="JavaScript"> location.reload(); </script>'; 89 } 90 } 91 if (!$qid && preg_match('/^\<select\>/i', $sql)) { 92 dprint(__file__, __line__, 0, $sql); 93 } 94 95 if (W2P_PERFORMANCE_DEBUG) { 96 ++$w2p_performance_old_dbqueries; 97 $w2p_performance_dbtime += array_sum(explode(' ', microtime())) - $startTime; 98 } 99 100 return $qid; 101 } 102 103 function db_free_result($cur) { 104 // TODO 105 // mysql_free_result( $cur ); 106 // Maybe it's done my Adodb 107 if (!is_object($cur)) { 108 dprint(__file__, __line__, 0, 'Invalid object passed to db_free_result.'); 109 } 110 $cur->Close(); 111 } 112 113 function db_num_rows($qid) { 114 if (!is_object($qid)) { 115 dprint(__file__, __line__, 0, 'Invalid object passed to db_num_rows.'); 116 } 117 return $qid->RecordCount(); 118 //return $db->Affected_Rows(); 119 } 120 121 function db_fetch_row(&$qid) { 122 if (!is_object($qid)) { 123 dprint(__file__, __line__, 0, 'Invalid object passed to db_fetch_row.'); 124 } 125 return $qid->FetchRow(); 126 } 127 128 function db_fetch_assoc(&$qid) { 129 if (!is_object($qid)) { 130 dprint(__file__, __line__, 0, 'Invalid object passed to db_fetch_assoc.'); 131 } 132 return $qid->FetchRow(); 133 } 134 135 function db_fetch_array(&$qid) { 136 if (!is_object($qid)) { 137 dprint(__file__, __line__, 0, 'Invalid object passed to db_fetch_array.'); 138 } 139 $result = $qid->FetchRow(); 140 // Ensure there are numerics in the result. 141 if ($result && !isset($result[0])) { 142 $ak = array_keys($result); 143 foreach ($ak as $k => $v) { 144 $result[$k] = $result[$v]; 145 } 146 } 147 return $result; 148 } 149 150 function db_fetch_object($qid) { 151 if (!is_object($qid)) { 152 dprint(__file__, __line__, 0, 'Invalid object passed to db_fetch_object.'); 153 } 154 return $qid->FetchNextObject(false); 155 } 156 157 function db_escape($str) { 158 global $db; 159 return substr($db->qstr($str), 1, -1); 160 } 161 162 function db_version() { 163 return 'ADODB'; 164 } 165 166 function db_unix2dateTime($time) { 167 global $db; 168 return $db->DBDate($time); 169 } 170 171 function db_dateTime2unix($time) { 172 global $db; 173 174 return $db->UnixDate($time); 175 176 // TODO - check if it's used anywhere... 177 // if ($time == '0000-00-00 00:00:00') { 178 // return -1; 179 // } 180 } 181 182 // make the connection to the db 183 db_connect(w2PgetConfig('dbhost'), w2PgetConfig('dbname'), w2PgetConfig('dbuser'), w2PgetConfig('dbpass'), w2PgetConfig('dbpersist')); 184 185 /* 186 * Having successfully established the database connection now, 187 * we will hurry up to load the system configuration details from the database. 188 */ 189 190 $sql = 'SELECT config_name, config_value, config_type FROM ' . w2PgetConfig('dbprefix') . 'config'; 191 $rs = $db->Execute($sql); 192 193 if ($rs) { // Won't work in install mode. 194 $rsArr = $rs->GetArray(); 195 196 switch (strtolower(trim(w2PgetConfig('dbtype')))) { 197 case 'oci8': 198 case 'oracle': 199 foreach ($rsArr as $c) { 200 if ($c['CONFIG_TYPE'] == 'checkbox') { 201 $c['CONFIG_VALUE'] = ($c['CONFIG_VALUES'] == 'true') ? true : false; 202 } 203 $w2Pconfig[$c['CONFIG_NAME']] = $c['CONFIG_VALUE']; 204 } 205 break; 206 default: 207 //mySQL 208 foreach ($rsArr as $c) { 209 if ($c['config_type'] == 'checkbox') { 210 $c['config_value'] = ($c['config_value'] == 'true') ? true : false; 211 } 212 $w2Pconfig[$c['config_name']] = $c['config_value']; 213 } 214 } 215 } 216 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Nov 20 03:00:14 2008 | Cross-referenced by PHPXref 0.7 |