From f47e9eaadbec0edc4bb9e6c5169f32a3f5f77261 Mon Sep 17 00:00:00 2001 From: ben Date: Wed, 13 Feb 2008 19:18:41 +0000 Subject: Added some fancy database connection gubbins. If you give it loads of read or write dbs to pick from, Elgg will choose one at random for each category! git-svn-id: https://code.elgg.org/elgg/trunk@29 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/database.php | 231 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 engine/lib/database.php (limited to 'engine/lib/database.php') diff --git a/engine/lib/database.php b/engine/lib/database.php new file mode 100644 index 000000000..0511ee82b --- /dev/null +++ b/engine/lib/database.php @@ -0,0 +1,231 @@ +db[$dblinkname])) { + if (is_array($CONFIG->db[$dblinkname])) { + $index = rand(0,sizeof($CONFIG->db[$dblinkname])); + $dbhost = $CONFIG->db[$dblinkname][$index]->dbhost; + $dbuser = $CONFIG->db[$dblinkname][$index]->dbuser; + $dbpass = $CONFIG->db[$dblinkname][$index]->dbpass; + $dbname = $CONFIG->db[$dblinkname][$index]->dbname; + } else { + $dbhost = $CONFIG->db[$dblinkname]->dbhost; + $dbuser = $CONFIG->db[$dblinkname]->dbuser; + $dbpass = $CONFIG->db[$dblinkname]->dbpass; + $dbname = $CONFIG->db[$dblinkname]->dbname; + } + } else { + $dbhost = $CONFIG->dbhost; + $dbuser = $CONFIG->dbuser; + $dbpass = $CONFIG->dbpass; + $dbname = $CONFIG->dbname; + } + + // Connect to database + if (!$dblink[$dblinkname] = mysql_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, true)) + throw new DatabaseException("Elgg couldn't connect to the database using the given credentials."); + if (!mysql_select_db($CONFIG->dbname, $dblink[$dblinkname])) + throw new DatabaseException("Elgg couldn't select the database {$CONFIG->dbname}."); + + } + + /** + * Establish all database connections + * + * If the configuration has been set up for multiple read/write databases, set those + * links up separately; otherwise just create the one database link + * + */ + + function setup_db_connections() { + + // Get configuration and globalise database link + global $CONFIG, $dblink; + + if (!empty($CONFIG->db->split)) { + establish_db_link('read'); + establish_db_link('write'); + } else { + establish_db_link('readwrite'); + } + + } + + /** + * Alias to setup_db_connections, for use in the event handler + * + * @param string $event The event type + * @param string $object_type The object type + * @param mixed $object Used for nothing in this context + */ + function init_db($event, $object_type, $object = null) { + setup_db_connections(); + } + + /** + * Gets the appropriate db link for the operation mode requested + * + * @param string $dblinktype The type of link we want - "read", "write" or "readwrite" (the default) + * @return database link + */ + function get_db_link($dblinktype) { + + global $dblink; + + if (isset($dblink[$dblinktype])) { + return $dblink[$dblinktype]; + } else { + return $dblink['readwrite']; + } + + } + + /** + * Use this function to get data from the database + * @param $query The query being passed. + */ + + function get_data($query) { + + global $dbcalls; + + $dblink = get_db_link('read'); + + $resultarray = array(); + $dbcalls++; + + if ($result = mysql_query($query, $dblink)) { + while ($row = mysql_fetch_object($result)) { + $resultarray[] = $row; + } + } + if (empty($resultarray)) { + return false; + } + return $resultarray; + } + + /** + * Use this function to get a single data row from the database + * @param $query The query to run. + */ + + function get_data_row($query) { + + global $dbcalls; + + $dblink = get_db_link('read'); + + $dbcalls++; + + if ($result = mysql_query($query, $dblink)) { + while ($row = mysql_fetch_object($result)) { + return $row; + } + } + return false; + } + + /** + * Use this function to insert database data; returns id or false + * @param $query The query to run. + * @return $id the database id of the inserted row. + */ + + function insert_data($query) { + + global $dbcalls; + + $dblink = get_db_link('write'); + + $dbcalls++; + + if (mysql_query($query, $dblink)) { + return mysql_insert_id($dblink); + } else { + return false; + } + + } + + /** + * Use this function to update database data + * @param $query The query to run. + */ + + function update_data($query) { + + global $dbcalls; + + $dblink = get_db_link('write'); + + $dbcalls++; + + if (mysql_query($query, $dblink)) + return mysql_affected_rows(); + + return false; + + } + + // Use this function to delete a record + + function delete_record($query) { + + global $dbcalls; + + $dblink = get_db_link('write'); + + $dbcalls++; + + if (mysql_query($query, $dblink)) { + return mysql_affected_rows(); + } else { + return false; + } + + } + + /** + * Returns the number of rows returned by the last select statement, without the need to re-execute the query. + * This is MYSQL specific. + * + * @return int + */ + function count_last_select() { + $row = get_data_row("SELECT found_rows() as count"); + if ($row) + return $row->count; + return 0; + } + + // Stuff for initialisation + + register_event_handler('init','system','init_db',0); + +?> \ No newline at end of file -- cgit v1.2.3