[Mapbender-commits] r2651 - trunk/mapbender/lib
svn_mapbender at osgeo.org
svn_mapbender at osgeo.org
Fri Jul 11 05:29:37 EDT 2008
Author: christoph
Date: 2008-07-11 05:29:36 -0400 (Fri, 11 Jul 2008)
New Revision: 2651
Added:
trunk/mapbender/lib/database-mysqli.php
Modified:
trunk/mapbender/lib/database-mysql.php
Log:
(incomplete) MySQLi wrapper by Mathias Trunte + improvements to MySQL wrapper
Modified: trunk/mapbender/lib/database-mysql.php
===================================================================
--- trunk/mapbender/lib/database-mysql.php 2008-07-11 08:15:18 UTC (rev 2650)
+++ trunk/mapbender/lib/database-mysql.php 2008-07-11 09:29:36 UTC (rev 2651)
@@ -1,5 +1,5 @@
<?php
-# $Id:database-mysql.php 2619 2008-07-08 15:46:11Z christoph $
+# $Id$
# http://www.mapbender.org/index.php/database-mysql.php
# Copyright (C) 2002 CCGIS
#
@@ -53,8 +53,8 @@
* Notice the global vars $sys_dbhost,$sys_dbuser,$sys_dbpasswd,$sys_dbname that must be set up
* in other functions in this library
*/
-include_once(dirname(__FILE__)."/../http/classes/class_mb_exception.php");
-include_once(dirname(__FILE__)."/../http/classes/class_checkInput.php");
+include_once(dirname(__FILE__)."/../../http/classes/class_mb_exception.php");
+include_once(dirname(__FILE__)."/../../http/classes/class_checkInput.php");
function db_escapestring($unescaped_string){
return @mysql_escape_string($unescaped_string);
}
@@ -230,11 +230,12 @@
* Returns the number of rows in this result set
*
* @param $qhandle (string) Query result set handle
+ * (!!! Deprecated? Changed the MySQL-Command to match the current syntax)
*/
function db_numrows($qhandle) {
// return only if qhandle exists, otherwise 0
if ($qhandle) {
- return @mysql_numrows($qhandle);
+ return @mysql_num_rows($qhandle);
} else {
return 0;
}
@@ -290,9 +291,10 @@
* Returns the number of fields in this result set
*
* @param $lhandle (string) Query result set handle
+ * (!!! Deprecated? Changed the MySQL-Command to match the current syntax)
*/
function db_numfields($lhandle) {
- return @mysql_numfields($lhandle);
+ return @mysql_num_fields($lhandle);
}
/**
Added: trunk/mapbender/lib/database-mysqli.php
===================================================================
--- trunk/mapbender/lib/database-mysqli.php (rev 0)
+++ trunk/mapbender/lib/database-mysqli.php 2008-07-11 09:29:36 UTC (rev 2651)
@@ -0,0 +1,481 @@
+<?php
+/**
+ * System-wide database type
+ *
+ * @var constant $sys_database_type
+ */
+$sys_database_type='mysqli';
+
+/**
+ * Connect to the database
+ *
+ * Notice the global vars that must be set up
+ * Notice the global vars $sys_dbhost,$sys_dbuser,$sys_dbpasswd,$sys_dbname that must be set up
+ * in other functions in this library
+ */
+include_once(dirname(__FILE__)."/../../http/classes/class_mb_exception.php");
+include_once(dirname(__FILE__)."/../../http/classes/class_checkInput.php");
+function db_escapestring($unescaped_string){
+ global $conn;
+ return mysqli_escape_string($conn, $unescaped_string);
+}
+function db_escape_string($unescaped_string){
+ global $conn;
+ return mysqli_escape_string($conn, $unescaped_string);
+}
+
+function db_connect($DBSERVER="",$OWNER="",$PW="") {
+ global $sys_dbhost,$sys_dbuser,$sys_dbpasswd,$sys_dbname,
+ $conn,$conn_update,$sys_db_use_replication,$sys_dbreadhost;
+
+
+ if ($DBSERVER)
+ $sys_dbhost = $DBSERVER;
+ if ($OWNER)
+ $sys_dbuser = $OWNER;
+ if ($PW)
+ $sys_dbpasswd = $PW;
+ if (PORT!=''){
+ $sys_dbport = ':'.PORT;
+ }
+ else{
+ $sys_dbport = '';
+ }
+
+ if ($sys_db_use_replication) {
+ //
+ // if configured for replication, $conn is the read-only host
+ // we do not connect to update server until needed
+ //
+ $conn = mysqli_connect($sys_dbreadhost,$sys_dbuser,$sys_dbpasswd);
+ $conn_update=mysqli_connect($sys_dbhost,$sys_dbuser,$sys_dbpasswd);
+ } else {
+ $conn = mysqli_connect($sys_dbhost,$sys_dbuser,$sys_dbpasswd,$sys_dbname,intval($sys_dbport));
+ #echo "@mysql_pconnect($sys_dbhost.$sys_dbport,$sys_dbuser,$sys_dbpasswd)";
+ }
+ if ($sys_dbname)
+ mysqli_select_db($conn, $sys_dbname);
+ return $conn;
+}
+
+function db_select_db($DB,$con="") {
+ global $conn,$sys_dbname;
+ $sys_dbname = $DB;
+ $_con = $con ? $con : $conn;
+ $ret = mysqli_select_db($_con, $sys_dbname);
+ if ($ret){
+ return true;
+ }
+ else {
+ return false;
+ }
+// echo "$ret=@mysql_select_db($sys_dbname,$_con);";
+}
+
+/**
+ * Query the database
+ *
+ * @param $qstring (string) SQL statement
+ * @param $limit (int) How many rows do you want returned
+ * @param $offset (int) Of matching rows, return only rows starting here
+ */
+function db_query($qstring,$limit='-1',$offset=0) {
+ /* Ausmisten! Der Code ist unübersichtlich, und bei manchen Zweigen ist nicht klar,
+ * ob sie überhaupt ausgeführt werden...
+ */
+ global $QUERY_COUNT,$sys_db_use_replication,$sys_db_is_dirty,$DB,
+ $sys_dbname,$conn,$conn_update,$sys_dbhost,$sys_dbuser,$sys_dbpasswd;
+
+ $QUERY_COUNT++;
+ if(!$sys_dbname && $DB)
+ $sys_dbname = $DB;
+ db_select_db($sys_dbname,$conn);
+
+ if ($limit > 0) {
+ if (!$offset || $offset < 0) {
+ $offset=0;
+ }
+ $qstring=$qstring." LIMIT $offset,$limit";
+ }
+// if ($GLOBALS['IS_DEBUG'])
+ $GLOBALS['G_DEBUGQUERY'] .= $qstring . "<P><BR>\n";
+
+ //
+ //are we configured to try to use replication?
+ //
+ if ($sys_db_use_replication) {
+ //
+ //if we haven't yet done an insert/update,
+ //read from the read-only db
+ //
+ if (!$sys_db_is_dirty && mb_eregi("^( )*(select)",$qstring)) {
+ if ($QUERY_COUNT%3==0) {
+ // 1/3rd of read queries go to master for now
+ return mysqli_query($sys_dbname,$qstring,$conn_update);
+ } else {
+ return mysqli_query($sys_dbname,$qstring,$conn);
+ }
+ } else {
+ //must be an update/insert/delete query - go to master server
+ $sys_db_is_dirty=true;
+ return mysqli_query($sys_dbname,$qstring,$conn_update);
+ }
+ } else {
+ $ret = mysqli_query($conn, $qstring);
+// echo "@mysql_db_query($sys_dbname,$qstring,$conn); ret=$ret<br>";
+ if(!$ret){
+ $e = new mb_exception("db_query($qstring)=$ret db_error=".db_error());
+ }
+ return $ret;
+ }
+ //echo "SQL__".$qstring;
+}
+
+function db_prepare_typestr($types) {
+ $result = '';
+ foreach ($types as $current_type) {
+ $result .= $current_type;
+ }
+ return $result;
+}
+
+/**
+ * prepare and query the database
+ *
+ * @param $qstring (string) SQL statement
+ * @param $params (array string params)
+ * @param $types (array string types)
+ */
+function db_prep_query($qstring, $params, $types){
+ // Beim Umschreiben an der PostGreSQL-Bibliothek orientieren
+ global $conn, $stmt;
+
+ @mysqli_stmt_close($stmt);
+
+ $ci = new checkInput($qstring,$params,$types);
+ $params = $ci->v;
+ if(PREPAREDSTATEMENTS == false){
+ for ($i=0; $i<count($params); $i++){
+ $needle = "$".strval($i+1);
+ $tmp = '';
+ if($params[$i] !== NULL){
+ if($types[$i] == 's'){ $tmp .= "'"; }
+ $tmp .= $params[$i];
+ if($types[$i] == 's'){ $tmp .= "'"; }
+ }
+ else{
+ $tmp .= "NULL";
+ }
+ $posa = mb_strpos($qstring, $needle);
+ $posb = mb_strlen($needle);
+ $qstring = mb_substr($qstring,0,$posa).$tmp.mb_substr($qstring,($posa + $posb));
+ }
+ $r = db_query($qstring);
+ if(!$r){
+ $e = new mb_exception("Error while executing sql statement in ".$_SERVER['SCRIPT_FILENAME'].": Sql: ".$qstring.", Error: ".db_error());
+ }
+ }
+ else{
+ if (!$conn) {
+ $e = new mb_exception("Error while connecting to the database in ".$_SERVER['SCRIPT_FILENAME'].", Error: ".db_error());
+ }
+ echo $qstring;
+ $qstring = preg_replace('/\$[0-9]+/', '?', $qstring);
+ echo $qstring;
+ $stmt = mysqli_prepare($conn, $qstring);
+ //echo $result;
+ if(!$stmt){
+ echo "Error while preparing statement in ".$_SERVER['SCRIPT_FILENAME'].": Sql: ".$qstring.", Error: ".db_error();
+ $e = new mb_exception("Error while preparing statement in ".$_SERVER['SCRIPT_FILENAME'].": Sql: ".$qstring.", Error: ".db_error());
+ }
+ $type_string = '"';
+ $param_string = '';
+ $val_def_string = '';
+ $val_string = '';
+ /*for ($i = 0; $i < count($params); $i++) {
+ if ($params[$i] !== NULL) {
+ $type_string .= $types[$i];
+ if ($i > 0) {
+ $param_string .= ', ';
+ $val_string .= ', ';
+ }
+
+ $param_string .= '"'.$params[$i].'"';
+ if ($types[$i] == 's')
+ $val_def_string .= '$val'.$i.'="'.$params[$i].'";';
+ else
+ $val_def_string .= '$val'.$i.'='.$params[$i].';';
+ $val_string .= '$val'.$i;
+
+ //mysqli_stmt_bind_param($result, $types[$i], $params[$i]);
+ }
+ }*/
+
+
+ for( $i = 0; $i < count($params); ++$i ) {
+ $thisVar = "v$i";
+ $variadicParams .= '$' . $thisVar;
+ if( $i != count($params) - 1 ) // don't append a comma on the last variable
+ $variadicParams .= ', ';
+
+ $variadicExtVars[ $thisVar ] = $params[ $i ];
+ }
+ echo $variadicParams;
+
+ $bindParamFunc = create_function( '$vars, &$stmt','extract( $vars ); $stmt->bind_param( \'' . implode($types) . '\', ' . $variadicParams . ' );' );
+ $bindParamFunc( $variadicExtVars, $stmt );
+
+ /* $typeStr = call_user_func_array( 'db_prepare_typestr', $types );
+ array_unshift( $params, $typeStr );
+ call_user_func_array( array( 'mysqli_bind_param', $qstring ), $params ); */
+
+ /*$type_string .= '"';
+ $eval_string = $val_def_string.'mysqli_stmt_bind_param($result,'.$type_string.','.$val_string.');';
+ $xyz=1;
+ //$eval_string = 'eead results, this function cancelcho $result;';
+ //$eval_string = 'return mysqli_stmt_bind_param($result,'.$type_string.',$xyz);';
+ echo $eval_string;
+ //echo "mysqli_stmt_bind_param($result,$type_string,$param_string)";
+ eval($eval_string);*/
+ $r = mysqli_stmt_execute($stmt);
+ if(!$r){
+ $e = new mb_exception("Error while executing prepared statement in ".$_SERVER['SCRIPT_FILENAME'].": Sql: ".$qstring.", Error: ".db_error());
+ }
+ $result2 = $stmt;
+ }
+
+ return $stmt;
+}
+/**
+ * Begin a transaction
+ *
+ * Begin a transaction for databases that support them
+ * may cause unexpected behavior in databases that don't
+ */
+function db_begin() {
+ return db_query("BEGIN WORK");
+}
+
+/**
+ * Commit a transaction
+ *
+ * Commit a transaction for databases that support them
+ * may cause unexpected behavior in databases that don't
+ */
+function db_commit() {
+ return db_query("COMMIT");
+}
+
+/**
+ * Roll back a transaction
+ *
+ * Rollback a transaction for databases that support them
+ * may cause unexpected behavior in databases that don't
+ */
+function db_rollback() {
+ $str = db_error();
+ db_query("ROLLBACK");
+ die('sql error: ' . $str . " ROLLBACK performed....");
+}
+
+/**
+ * Returns the number of rows in this result set
+ *
+ * @param $qhandle (string) Query result set handle
+ */
+function db_num_rows($qhandle) {
+ // return only if qhandle exists, otherwise 0
+ if ($qhandle) {
+ return mysqli_num_rows($qhandle);
+ } else {
+ return 0;
+ }
+}
+
+/**
+ * Frees a database result properly
+ *
+ * @param $qhandle (string) Query result set handle
+ */
+function db_free_result($qhandle) {
+ return mysqli_free_result($qhandle);
+}
+
+/**
+ * Reset a result set.
+ *
+ * Reset is useful for db_fetch_array sometimes you need to start over
+ *
+ * @param $qhandle (string) Query result set handle
+ * @param $row (int) Row number
+ */
+function db_reset_result($qhandle,$row=0) {
+ return mysqli_data_seek($qhandle,$row);
+}
+
+/**
+ * Returns a field from a result set
+ *
+ * @param $qhandle (string) Query result set handle
+ * @param $row (int) Row number
+ * @param $field (string) Field name
+ */
+function db_result($qhandle,$row,$field) {
+ //return mysqli_result($qhandle,$row,$field);
+ if( mysqli_num_rows( $qhandle ) ) {
+ $x = 0;
+ while( $array = mysqli_fetch_array( $qhandle, MYSQLI_NUM ) ) {
+ if( $row == $x++ ) {
+ return isset( $array[ $field ] ) ? $array[ $field ] : '';
+ }
+ }
+ }
+}
+
+/**
+ * Returns the number of fields in this result set
+ *
+ * @param $lhandle (string) Query result set handle
+ * (!!! Deprecated? Changed the MySQL-Command to match the current syntax)
+ */
+function db_numfields($lhandle) {
+ return mysqli_num_fields($lhandle);
+}
+
+/**
+ * Returns the number of fields in this result set
+ *
+ * @param $lhandle (string) Query result set handle
+ * php 3,4,5
+ */
+function db_num_fields($lhandle) {
+ return mysqli_num_fields($lhandle);
+}
+
+/**
+ * Returns the number of rows changed in the last query
+ *
+ * @param $lhandle (string) Query result set handle
+ * @param $fnumber (int) Column number
+ */
+function db_fieldname($lhandle,$fnumber) {
+ return mysql_field_name($lhandle,$fnumber);
+}
+
+/**
+ * Returns the number of rows changed in the last query
+ *
+ * @param $qhandle (string) Query result set handle
+ */
+function db_affected_rows($qhandle) {
+ return mysqli_affected_rows($qhandle);
+}
+
+/**
+ * Fetch an array
+ *
+ * Returns an associative array from
+ * the current row of this database result
+ * Use db_reset_result to seek a particular row
+ *
+ * @param $qhandle (string) Query result set handle
+ */
+function db_fetch_array($qhandle) {
+ global $conn;
+
+ $data = mysqli_stmt_result_metadata($qhandle);
+ $fields = array();
+ $out = array();
+
+ $fields[0] = &$qhandle;
+ $count = 1;
+
+ while($field = mysqli_fetch_field($data)) {
+ $fields[$count] = &$out[$field->name];
+ $count++;
+ }
+
+ call_user_func_array(mysqli_stmt_bind_result, $fields);
+ mysqli_stmt_fetch($qhandle);
+ return (count($out) == 0) ? false : $out;
+
+ /*if (!$qhandle)
+ return NULL;
+ else
+ return mysqli_fetch_array($conn); */
+}
+
+/**
+ * fetch a row into an array
+ *
+ * @param $qhandle (string) Query result set handle
+ * @param $fnumber (int) Column number
+ */
+function db_fetch_row($qhandle,$fnumber=0) {
+ global $stmt;
+ if ($qhandle)
+ return mysqli_fetch_row($stmt);
+ else
+ return NULL;
+}
+
+/**
+ * Returns the last primary key from an insert
+ *
+ * @param $qhandle (string) Query result set handle
+ * @param $table_name (string) Is the name of the table you inserted into
+ * @param $pkey_field_name (string) Is the field name of the primary key
+ */
+function db_insertid($qhandle="",$table_name="",$pkey_field_name="") {
+ return mysqli_insert_id();
+}
+
+function db_insert_id($qhandle="",$table_name="",$pkey_field_name="") {
+ return mysqli_insert_id();
+}
+
+/**
+ * Returns the last error from the database
+ */
+function db_error() {
+ global $conn;
+ return mysqli_error($conn);
+}
+
+/**
+ * Get the flags associated with the specified field in a result
+ *
+ * @param $lhandle (string) Query result set handle
+ * @param $fnumber (int) Column number
+ *
+ * Examples: "not_null", "primary_key", "unique_key", "multiple_key",
+ * "blob", "unsigned", "zerofill","binary", "enum",
+ * "auto_increment", "timestamp"
+ */
+
+function db_field_flags($lhandle,$fnumber) {
+ return mysqli_field_flags($lhandle,$fnumber);
+}
+
+/**
+ * Get the type of the specified field
+ *
+ * @param $lhandle (string) Query result set handle
+ * @param $fnumber (int) Column number
+ */
+
+function db_field_type($lhandle,$fnumber) {
+ return mysqli_field_type($lhandle,$fnumber);
+}
+
+/**
+ * Get the length of the specified field
+ *
+ * @param $lhandle (string) Query result set handle
+ * @param $fnumber (int) Column number
+ */
+
+function db_field_len($lhandle,$fnumber) {
+ return mysqli_field_len($lhandle,$fnumber);
+}
+?>
\ No newline at end of file
More information about the Mapbender_commits
mailing list