3 * FusionForge MySQL connection layer
5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2007, Robert Nelson
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * db_connect() - Connect to the database
29 * Notice the global vars that must be set up
30 * Sets up a global $gfconn variable which is used
31 * in other functions in this library
33 function db_connect() {
34 global $sys_dbhost, $sys_dbuser, $sys_dbpasswd, $gfconn;
36 db_log_entry('db_connect', NULL);
38 db_log_dbentry('mysqli_connect',"$sys_dbhost, $sys_dbuser, $sys_dbpasswd");
39 $gfconn = @mysqli_connect($sys_dbhost, $sys_dbuser, $sys_dbpasswd);
40 db_log_dbexit('mysqli_connect',"$gfconn");
43 // Now map the physical database connections to the
44 // "virtual" list that is used to distribute load in db_query()
45 // Define dummy values to eliminate log messages
47 define('SYS_DB_PRIMARY', 0);
48 define('SYS_DB_STATS', 1);
49 define('SYS_DB_TROVE', 2);
50 define('SYS_DB_SEARCH', 3);
52 db_log_exit('db_connect');
58 * db_query() - Query the database
60 * @param string SQL statement
61 * @param int How many rows do you want returned
62 * @param int Of matching rows, return only rows starting here
64 function db_query($qstring, $limit = '-1', $offset = 0) {
65 global $sys_dbname, $gfconn;
67 db_log_entry('db_query',"$qstring, $limit, $offset");
70 if (!$offset || $offset < 0) {
73 $qstring=$qstring." LIMIT $offset,$limit";
75 // if ($GLOBALS['IS_DEBUG'])
76 // $GLOBALS['G_DEBUGQUERY'] .= $qstring . "<p><br />\n";
78 db_log_dbentry('mysqli_select_db',"$gfconn, $sys_dbname");
79 if (!mysqli_select_db($gfconn, $sys_dbname)) {
80 db_log_dbexit('mysqli_select_db',false);
81 db_log_exit('db_query');
84 db_log_dbexit('mysqli_select_db',true);
86 db_log_dbentry('mysqli_query',"$gfconn, $sys_dbname");
87 $res = mysqli_query($gfconn, $qstring);
88 db_log_dbexit('mysqli_query',$res);
90 db_log_exit('db_query',"$res");
95 * db_mquery() - Query the database supporting multi-statements
97 * @param string SQL statement
99 function db_mquery($qstring) {
100 global $sys_dbname, $gfconn;
102 // if ($GLOBALS['IS_DEBUG'])
103 // $GLOBALS['G_DEBUGQUERY'] .= $qstring . "<p><br />\n";
105 db_log_entry('db_mquery',"$qstring");
107 db_log_dbentry('mysqli_select_db',"$gfconn, $sys_dbname");
108 if (!mysqli_select_db($gfconn, $sys_dbname)) {
109 db_log_dbexit('mysqli_select_db',false);
110 $err = mysqli_error($gfconn);
112 db_log('DB Error = '.$err);
114 db_log_exit('db_mquery',NULL);
117 db_log_dbexit('mysqli_select_db', true);
119 db_log_dbentry('mysqli_multi_query',"$gfconn, $qstring");
120 if (!mysqli_multi_query($gfconn, $qstring)) {
121 db_log_dbexit('mysqli_multi_query','false');
122 $err = mysqli_error($gfconn);
124 db_log('DB Error = '.$err);
126 db_log_exit('db_mquery');
129 db_log_dbexit('mysqli_multi_query',true);
131 db_log_dbentry('mysqli_store_result',"$gfconn");
132 if ($res = mysqli_store_result($gfconn)) {
133 db_log_dbexit('mysqli_store_result',"$res");
134 db_log_exit('db_mquery',"$res");
137 $err = mysqli_error($gfconn);
139 db_log('DB Error = '.$err);
141 db_log_dbexit('mysqli_store_result');
143 db_log_exit('db_mquery',true);
148 * db_next_result() - Get the next result from query with multiple statements.
150 * @param string SQL statement
151 * @param int How many rows do you want returned
152 * @param int Of matching rows, return only rows starting here
154 function db_next_result() {
157 db_log_entry('db_next_result',NULL);
159 db_log_dbentry('mysqli_next_result',"$gfconn");
160 $ret = mysqli_next_result($gfconn);
161 db_log_dbexit('mysqli_next_result',"$ret");
162 $err = mysqli_error($gfconn);
164 db_log('DB Error = '.$err);
168 db_log_dbentry('mysqli_store_result',"$gfconn");
169 $res = mysqli_store_result($gfconn);
170 db_log_dbexit('mysqli_store_result',"$res");
175 $err = mysqli_error($gfconn);
177 db_log('DB Error = '.$err);
181 db_log_exit('db_next_result',$res);
186 * db_begin() - Begin a transaction
188 * Begin a transaction for databases that support them
189 * may cause unexpected behavior in databases that don't
191 function db_begin() {
192 return db_query("BEGIN WORK");
196 * db_commit() - Commit a transaction
198 * Commit a transaction for databases that support them
199 * may cause unexpected behavior in databases that don't
201 function db_commit() {
202 return db_query("COMMIT");
206 * db_rollback() - Roll back a transaction
208 * Rollback a transaction for databases that support them
209 * may cause unexpected behavior in databases that don't
211 function db_rollback() {
212 return db_query("ROLLBACK");
216 * db_numrows() - Returns the number of rows in this result set
218 * @param string Query result set handle
220 function db_numrows($qhandle) {
221 // return only if qhandle exists, otherwise 0
223 return @mysqli_num_rows($qhandle);
230 * db_free_result() - Frees a database result properly
232 * @param string Query result set handle
234 function db_free_result($qhandle) {
235 db_log_entry('db_free_result',"$qhandle");
236 if (!is_object($qhandle)) {
237 db_log_exit('db_free_result');
240 db_log_dbentry('mysqli_free_result',"$qhandle");
241 $res = mysqli_free_result($qhandle);
242 db_log_dbexit('mysqli_free_result',"$res");
243 db_log_exit('db_free_result');
247 * db_reset_result() - Reset a result set.
249 * Reset is useful for db_fetch_array sometimes you need to start over
251 * @param string Query result set handle
252 * @param int Row number
254 function db_reset_result($qhandle,$row=0) {
255 return mysqli_data_seek($qhandle,$row);
259 * db_result() - Returns a field from a result set
261 * @param string Query result set handle
262 * @param int Row number
263 * @param string Field name
265 function db_result($qhandle,$row,$field) {
267 if (!mysqli_data_seek($qhandle,$row)) {
271 $row_data = mysqli_fetch_array($qhandle, MYSQLI_BOTH);
275 return $row_data[$field];
279 * db_numfields() - Returns the number of fields in this result set
281 * @param string Query result set handle
283 function db_numfields($lhandle) {
284 return mysqli_num_fields($lhandle);
288 * db_fieldname() - Returns the name of a field in this result set
290 * @param string Query result set handle
291 * @param int Column number
293 function db_fieldname($lhandle,$fnumber) {
294 $fieldinfo=mysqli_fetch_field_direct($lhandle,$fnumber);
296 return $fieldinfo->name;
303 * db_affected_rows() - Returns the number of rows changed in the last query
305 * @param string Query result set handle
307 function db_affected_rows() {
310 return mysqli_affected_rows($gfconn);
314 * db_fetch_array() - Fetch an array
316 * Returns an associative array from
317 * the current row of this database result
318 * Use db_reset_result to seek a particular row
320 * @param string Query result set handle
322 function db_fetch_array($qhandle) {
323 return @mysqli_fetch_array($qhandle);
327 * db_insertid() - Returns the last primary key from an insert
329 * @param string Query result set handle
330 * @param string Is the name of the table you inserted into
331 * @param string Is the field name of the primary key
333 function db_insertid($qhandle,$table_name,$pkey_field_name) {
336 return mysqli_insert_id($gfconn);
340 * db_error() - Returns the last error from the database
342 function db_error() {
345 return mysqli_error($gfconn);
349 * system_cleanup() - In the future, we may wish to do a number
350 * of cleanup functions at script termination.
352 * For now, we just abort any in-process transaction.
354 function system_cleanup() {
355 global $_sys_db_transaction_level;
356 if ($_sys_db_transaction_level > 0) {
357 echo "Open transaction detected!!!";
358 db_query("ROLLBACK");
362 function db_drop_table_if_exists ($tn) {
363 $sql = "DROP TABLE IF EXISTS $tn;";
364 $rel = db_query ($sql);
368 function db_drop_sequence_if_exists ($tn) {
371 function db_log($message) {
375 $fhlog = fopen('/tmp/db.log', 'a');
379 fwrite($fhlog, $message);
384 function db_log_entry($func, $args) {
385 db_log("\nEntered ".$func.'('.$args.")\n");
388 function db_log_exit($func, $result = NULL) {
389 db_log('Exited '.$func.' returned '.$result."\n");
392 function db_log_dbentry($func, $args) {
393 db_log('Entered '.$func.'('.$args.")\n");
396 function db_log_dbexit($func, $result = NULL) {
397 db_log('Exited '.$func.' returned '.$result."\n");
401 db_log('Database error = '.$err."\n");
408 // c-file-style: "bsd"