Source for file MySQLDatabaseTable.class.php
Documentation is available at MySQLDatabaseTable.class.php
if( !extension_loaded( 'mysql' ) )
* For including this file you have to define the constant "CLASSPATH".
* Because every include in the framework depends on the CLASSPATH definition.
* The CLASSPATH means the relative path to the folder that contains the
echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
include_once CLASSPATH. "core/Arrays.class.php";
* Loading the supirior class on which this class depends.
require_once CLASSPATH. "databases/ABSTDatabaseTable.class.php";
require_once CLASSPATH. "databases/mysql/MySQL_constants.inc.php";
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (C) 2004 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* @return MySQLDatabaseTable
* @todo This isn't programmed yet.
//"SHOW COLUMNS FROM ".$this -> name;
* Iports data from a csv file. You have to create a CSVFiles-Object first.
* @param CSVFiles $csv_file_obj
* @param array $field_arr
$sqlOrder = "LOAD DATA INFILE '". $csv_file_obj->path. "/"
. $csv_file_obj->filename. "' "
. "INTO TABLE ". $this->name. " "
. "FIELDS TERMINATED BY '". $csv_file_obj->separator_char. "' "
. "ENCLOSED BY '". $csv_file_obj->enclosing_char. "' "
. "ESCAPED BY '". $csv_file_obj->escape_char. "' "
. "LINES TERMINATED BY '". $csv_file_obj->line_terminator_char. "'"
? "(`". implode( "`, `", $field_arr ). "`)"
return $this->parent->query( $sqlOrder, false, $this );
* Stores the Flags: (!)not_null <=> $this->field_can_be_null[FIELDINDEX],
* primary_key <=> $this->field_is_primary_member[FIELDINDEX], auto_increment
* <=> $this->field_has_auto_increment[FIELDINDEX]. This is necessary to get
* dump of the table data.
. "FROM `". $this->name. "` "
$result = $this->parent->query( $sqlOrder, false, $this );
strpos( $tmp_str, "not_null" ) === false;
strpos( $tmp_str, "primary_key" ) !== false;
strpos( $tmp_str, "auto_increment" ) !== false;
* Returns all field members of the primary key of this database
$sqlOrder = "SHOW KEYS FROM `". $this->name. "`";
$result = $this->parent->query( $sqlOrder, false, $this );
if ( $res_arr["Key_name"] == "PRIMARY" )
$primary_key[] = $res_arr["Column_name"];
* Returns the fieldname of the auto increment field if there is one.
$sqlOrder = "SHOW COLUMNS FROM `". $this->name. "`";
$result = $this->parent->query( $sqlOrder, false, $this );
if ( $res_arr["Extra"] == "auto_increment" )
* Returns an SQL-string to create this table. You can give a new name for
* @param string $newTableName
* @param boolean $drop_order
* @param boolean $if_not_exists_order
$if_not_exists_order = false
$sqlOrder = "SHOW CREATE TABLE `". $this->name. "`";
$result = $this->parent->query( $sqlOrder, false, $this );
if ( $if_not_exists_order )
$begin = substr( $row[1], 0, 13 ). "IF NOT EXISTS `";
$begin = substr( $row[1], 0, 14 );
if ( empty( $newTableName ) )
$row[1] = $begin. substr( $row[1], 14 );
$row[1] = $begin. $newTableName. substr(
$row[1] = "DROP TABLE IF EXISTS `". $this->name. "`;\n". $row[1];
* Returns an SQL-string to fill this table. You can give a new name for
* the table to insert in.
* @param string $newTableName
* @param boolean $full_inserts
$sqlWhere = " WHERE ". preg_replace( "!^ *WHERE *!i", "", $where );
if ( empty( $newTableName ) )
$newTableName = $this->name;
. "FROM `". $this->name. "`". $sqlWhere;
$result = $this->parent->query( $sqlOrder, false, $this );
for ( $i = 0; $i < count( $row ); $i++ )
$out .= "INSERT INTO `". $newTableName. "` "
. "VALUES ( '". implode( "', '", $row ). "' );\r\n";
for ( $i = 0; $i < count( $row ); $i++ )
$tmp .= ", '". $row[$i]. "'";
$out .= "INSERT INTO `". $newTableName. "` "
. "VALUES ( ". substr( $tmp, 2 ). " );\r\n";
* @param string $newTableName
* @param string $targetDatabase
function copyTable( $newTableName, $targetDatabase = "" )
if( empty( $targetDatabase ) )
$targetDatabase = $this->parent->name;
$sql = "INSERT INTO `". $targetDatabase. "`.`". $newTableName. "` "
. "FROM `". $this->parent->name. "`.`". $this->name. "`";
"TRUNCATE TABLE `". $this->name. "`",
* @param string $newTableName
$sqlOrder = "ALTER TABLE `". $this->name. "` RENAME `". $newTableName. "`";
$res = $this->parent->query( $sqlOrder, false, $this );
* @param string $newTableName
* @param boolean $full_inserts
* @param boolean $drop_order
* @param boolean $if_not_exists_order
$if_not_exists_order = false
. $this->getDataOrder( $newTableName, $full_inserts ). "\r\n\r\n";
* @param string $newTableName
* @param boolean $full_inserts
* @param boolean $drop_order
* @param boolean $if_not_exists_order
* @param integer $flush_row_count
$if_not_exists_order = false,
$tmp_cnt = $this->getCount( $sqlWhere );
for ( $i = 0; $i < $tmp_cnt; $i += $flush_row_count )
$tmp_where = " LIMIT ". $i. ", ". $flush_row_count;
* Returns the minimum value from a field.
* @param string $fieldname
* @param string $where The where-clause is optional.
function getMinOf( $fieldname, $where = "" )
"SELECT MIN( ". $fieldname. " ) \r\n"
. " FROM `". $this->name. "`\r\n"
* Returns the maximum value from a field.
* @param string $fieldname
* @param string $where The where-clause is optional.
function getMaxOf( $fieldname, $where = "" )
"SELECT MAX( ". $fieldname. " ) \r\n"
. " FROM `". $this->name. "`\r\n"
* Returns the sum value of a field.
* @param string $fieldname
* @param string $where The where-clause is optional.
function getSumOf( $fieldname, $where = "" )
"SELECT SUM( ". $fieldname. " ) \r\n"
. " FROM `". $this->name. "`\r\n"
* Returns the count of the fields in the where clause.
. " FROM `". $this->name. "`\r\n"
* Returns the datasets as an two dimensional array if the query could be
* execute. The first dimension carries the datasets. The second dimension
* carries the fields's values of the result list.
* getDatasetsWithFields( $fieldnamesArr, $where );
* getDatasetsWithFields( $where, $array_kind );
* getDatasetsWithFields( $where );
* @param array $field_arr The fields that should be in the result list.
* @param string $where The where clause of the query.
* @param string $only_one
* @param string $array_kind The kind how the tuples should be output.
* MYSQL_OUTPUT_ASSOC - to get an associative
* array, with the fieldnames as indices.
* MYSQL_OUTPUT_NUM - to get an array, with
* numbers as indices in order of the
* $fieldnamesArr or of the underlying table.
* MYSQL_OUTPUT_DEFAULT - to get a mix of both
$array_kind = MYSQL_OUTPUT_DEFAULT
$sqlWhere = " WHERE ". preg_replace( "!^ *WHERE *!i", "", $where );
$sqlWhere = " WHERE ". preg_replace( "!^ *WHERE *!i", "", $field_arr );
. "FROM `". $this->name. "`". $sqlWhere;
$sqlResultFields = implode( ", ", $field_arr );
$sqlOrder = "SELECT ". $sqlResultFields
. " FROM `". $this->name. "`". $sqlWhere;
$result = $this->parent->query( $sqlOrder, false, $this );
else // MYSQL_OUTPUT_DEFAULT
$dataset_arr[$i] = array();
$dataset_arr[$i++ ] = $result_arr;
$dataset_arr[$i] = array();
$dataset_arr[$i++ ] = $result_arr;
else // MYSQL_OUTPUT_DEFAULT
$dataset_arr[$i] = array();
$dataset_arr[$i++ ] = $result_arr;
* This method is an alias for the method "getDatasetsWithFields".
* Returns the datasets as an two dimensional array if the query could be
* execute. The first dimension carries the datasets. The second dimension
* carries the fields's values of the result list.
* getDatasets( $fieldnamesArr, $where, $array_kind );
* @param string $fields_or_where The fields to extract from relation.
* @param string $where_or_only_one The where clause of the query.
* @param string $array_kind The kind how the tuples should be
* MYSQL_OUTPUT_ASSOC - to get an
* associative array, with the fieldnames
* MYSQL_OUTPUT_NUM - to get an array,
* with numbers as indices in order of the
* $fieldnamesArr or of the underlying
* MYSQL_OUTPUT_DEFAULT - to get a mix of
* @param boolean $only_one Whether only one Dataset should be
* @param string $fields_or_where The where clause of the query.
* @param boolean $where_or_only_one Whether only one Dataset should be
* @param string $array_kind The kind how the tuples should be
* MYSQL_OUTPUT_ASSOC - to get an
* associative array, with the fieldnames
* MYSQL_OUTPUT_NUM - to get an array,
* with numbers as indices in order of the
* $fieldnamesArr or of the underlying
* MYSQL_OUTPUT_DEFAULT - to get a mix of
$fields_or_where = "1=1",
$where_or_only_one = false,
$array_kind = MYSQL_OUTPUT_DEFAULT,
$field_arr = explode( ",", $fields_or_where );
$field_arr = & $fields_or_where;
* Returns the datasets as array with the values of the primary key as index.
* @param array $primary_key The fields that should be in the result list.
* @param array $value_fields The fields with the values for the content.
* @param string $where The where clause of the query.
{ $sqlWhere = " WHERE ". $where; }
{ $str_value = $value_fields; }
{ $str_value = implode( ", ", $value_fields ); }
$sqlOrder = "SELECT `". $primary_key. "`, "
. "FROM `". $this->name. "`". $sqlWhere;
{ $result = $this->parent->query( $sqlOrder, false, $this ); }
// strip aliases from primary field list
"!^.+? +AS +([-_a-z0-9]+)$!is",
{ $datasets[ $resultArr[$primary_key] ] = $resultArr; }
// strip aliases from field list
"!^.+? +AS +([-_a-z0-9]+)$!is",
$datasets[ $resultArr[$primary_key] ] = $resultArr[$value_fields];
* Returns the datasets as array with the values of the primary key as index.
* @param array $primary_key The fields that should be in the result list.
* @param array $value_fields The fields with the values for the content.
* @param string $where The where clause of the query.
{ $sqlWhere = "WHERE ". $where. " "; }
{ $str_value = $value_fields; }
{ $str_value = implode( ", ", $value_fields ); }
$sqlOrder = "SELECT `". $primary_key. "`, "
. "FROM `". $this->name. "` "
. "ORDER BY `". $primary_key. "` ASC";
{ $result = $this->parent->query( $sqlOrder, false, $this ); }
// strip aliases from primary field list
"!^.+? +AS +([-_a-z0-9]+)$!is",
{ $datasets[ $resultArr[$primary_key] ][] = $resultArr; }
// strip aliases from field list
"!^.+? +AS +([-_a-z0-9]+)$!is",
$datasets[ $resultArr[$primary_key] ][] = $resultArr[$value_fields];
* Returns a single dataset as an array if the query could be execute.
* @param string $fields_or_where
* @param string $array_kind The kind how the tuples should be output.
* MYSQL_OUTPUT_ASSOC - to get an associative
* array, with the fieldnames as indices.
* MYSQL_OUTPUT_NUM - to get an array, with
* numbers as indices in order of the
* $fieldnamesArr or of the underlying table.
* MYSQL_OUTPUT_DEFAULT - to get a mix of both
$array_kind = MYSQL_OUTPUT_DEFAULT
$tmp = implode( ", ", $fields_or_where );
$where = & $fields_or_where;
$sqlWhere = " WHERE ". $where;
$sqlOrder = "SELECT ". $tmp. " \r\n"
. " FROM `". $this->name. "`\r\n". $sqlWhere;
$result = $this->parent->query( $sqlOrder, false, $this );
else // MYSQL_OUTPUT_DEFAULT
* Returns a single value of a data field.
* @param string $fieldname
$sqlWhere = " WHERE ". $where;
$sqlOrder = "SELECT ". $fieldname. " \r\n"
. " FROM `". $this->name. "`\r\n". $sqlWhere;
$result = $this->parent->query( $sqlOrder, false, $this );
* Returns a array of values of a data field.
* @param string $fieldname
$sqlWhere = " WHERE ". $where;
$sqlOrder = "SELECT ". $fieldname. " \r\n"
. " FROM `". $this->name. "`\r\n". $sqlWhere;
$result = $this->parent->query( $sqlOrder, false, $this );
$tmp_arr[] = $tmp_row[0];
* Puts a single dataset into request-variables. The names of the request-vars
* accord to the fieldnames of the Databasetable. You can fill variables of
* the types: $_REQUEST, $_GET and $_POST. You only have to list them in an
* array passed as third parameter in this function (see the description for
* @param string|array$where_or_field_arr This has to be the where-clause
* of the query to the underlying
* database table. If you only want
* to fetch and put a few fields of
* the table, you schould pass here
* an array of fieldnames and pass
* the where-clause to the with
* @param string $where This has to be the where-clause
* of the query to the underlying
* database table, if the first
* parameter should carry the fields
* that are only to fetch.
* @param array $request_type_arr Valid Values are:
* array( "GET", "POST", "REQUEST" )
* You can leave out the array
* values you don't need that the
* field values put in according
$request_type_arr = array( "GET", "POST", "REQUEST" )
{ $field_arr = & $where_or_field_arr; }
$where = & $where_or_field_arr;
if ( $res_arr && $field_arr )
if ( in_array( "GET", $request_type_arr ) )
foreach ( $field_arr as $fieldname )
{ $_GET[ $fieldname ] = $res_arr[ $fieldname ]; }
if ( in_array( "POST", $request_type_arr ) )
foreach ( $field_arr as $fieldname )
{ $_POST[ $fieldname ] = $res_arr[ $fieldname ]; }
if ( in_array( "REQUEST", $request_type_arr ) )
foreach ( $field_arr as $fieldname )
{ $_REQUEST[ $fieldname ] = $res_arr[ $fieldname ]; }
* Puts a single dataset into "post"-variables. The names of the "Post"-vars
* accord to the fieldnames of the Databasetable.
* @see MySQLDatabaseTable::putSingleDatasetIntoRequestVars()
* @param string|array$where_or_field_arr
* Puts a single dataset into "get"-variables. The names of the "get"-vars
* accord to the fieldnames of the Databasetable.
* @see MySQLDatabaseTable::putSingleDatasetIntoRequestVars()
* @param string|array$where_or_field_arr
* Deletes the specified dataset.
"It is not allowed to leave the where-clause blank.\r\n<br />"
. "Use the method \"emptyTable\" to remove all datasets.\r\n<br />"
. "If you want to keep the actual auto-increment-value, then try\r\n"
. "to call this function like delete( \"1=1\" );\r\n<br />"
$sqlOrder = "DELETE FROM `". $this->name. "`\r\n"
$result = $this->parent->query( $sqlOrder, false, $this );
* Inserts the specified dataset. Note that this function will create a global
* variable $GLOBALS["insert_id"] which contains the result of
* @param array $fieldArr The array that carries the fieldnames.
* @param array $valueArr The array that carries the values.
function insert( $fieldArr, $valueArr )
for ( $i = 0; $i < count( $fieldArr ); $i++ )
if ( in_array( $fieldArr[ $tmp_fields_key_arr[$i] ], $fieldname_arr ) )
$sqlSetFields .= "`". $fieldArr[ $tmp_fields_key_arr[$i] ]. "` = "
$valueArr[ $tmp_value_key_arr[$i] ]
$sqlSetFields = substr( $sqlSetFields, 0, strlen( $sqlSetFields ) - 2 );
$sqlOrder = "INSERT INTO `". $this->name. "` "
$result = $this->parent->query( $sqlOrder, false, $this );
* Updates the specified dataset.
* @param array $fieldArr The array that carries the fieldnames.
* @param array $valueArr The array that carries the values.
* @param string $where The wheredefinition of the query.
function update( $fieldArr, $valueArr, $where = "1=1" )
for ( $i = 0; $i < count( $fieldArr ); $i++ )
if ( in_array( $fieldArr[ $tmp_fields_key_arr[$i] ], $fieldname_arr ) )
$sqlSetFields .= "`". $fieldArr[ $tmp_fields_key_arr[$i] ]. "` = "
$valueArr[ $tmp_value_key_arr[$i] ]
$sqlSetFields = substr( $sqlSetFields, 0, strlen( $sqlSetFields ) - 2 );
$sqlOrder = "UPDATE `". $this->name. "` "
. "SET ". $sqlSetFields. " "
$result = $this->parent->query( $sqlOrder, false, $this );
* Replaces the specified dataset.
* @param array $fieldArr The array that carries the fieldnames.
* @param array $valueArr The array that carries the values.
function replace( $fieldArr, $valueArr )
for ( $i = 0; $i < count( $fieldArr ); $i++ )
if ( in_array( $fieldArr[ $tmp_fields_key_arr[$i] ], $fieldname_arr ) )
$sqlSetFields .= "`". $fieldArr[ $tmp_fields_key_arr[$i] ]. "` = "
$valueArr[ $tmp_value_key_arr[$i] ]
$sqlSetFields = substr( $sqlSetFields, 0, strlen( $sqlSetFields ) - 2 );
$sqlOrder = "REPLACE INTO `". $this->name. "` "
$result = $this->parent->query( $sqlOrder, false, $this );
* Swaps values of a specified field in two tupel.
* @param string $fieldname
// because of unique conflicts
$this->update( array( $fieldname ), array( '' ), $where_a );
$this->update( array( $fieldname ), array( $help_a ), $where_b );
$this->update( array( $fieldname ), array( $help_b ), $where_a );
* Stores the names of fields in this object.
$result = $this->parent->query(
"SHOW COLUMNS FROM `". $this->name. "`",
* Returns the table's fieldnames independence of overruled fieldnames. If you
* want you can get the fieldnames enclosed in backticks.
* @param boolean $backticks
$fieldname_arr = array();
$result = $this->parent->query(
"SHOW COLUMNS FROM `". $this->name. "`",
$fieldname_arr[] = $arr["Field"];
$tmp_str = "`". implode( "`[[SEPARATOR]]`", $fieldname_arr ). "`";
return explode( "[[SEPARATOR]]", $fieldname_arr );
* Returns the table's fieldnames. If you want you can get the fieldnames
* @param boolean $backticks
return explode( "[[SEPARATOR]]", $tmp_str );
* Overrules the table's fieldnames. The given array should contain the comma
* separated list of the query between the keywords SELECT and FROM.
* Each element of the array should represent one element of the list.
* Unsets the overrules of fieldnames.
* Probes whether there is a connection to the databasehost, if not it will
* attempt to connect to the host.
echo "There is no valid parent-object for the object of the "
. "databasetable named \"". $this -> name. "\".<br />\r\n"
. "Please add this table-object to a database-object before you "
. "make a call such as on <b>line "
. $debugArr[count($debugArr)- 1]["line"]. "</b> in <b>file \""
. $debugArr[count($debugArr)- 1]["file"]. "\"</b>."
echo "<b>BACKTRACE DEBUG:</b><br />\r\n";
&& !is_a( $this->parent->parent, "MySQLDatabaseHost" )
echo "The parent object of this databasetable-object has no parent."
. "Please add the object of the database named "
. "\"". $this->parent->name. "\" and which contains the object of "
. "the table \"". $this->name. "\" to a databasehost-object before "
. "you make a call such as on <b>line "
. $debugArr[count($debugArr)- 1]["line"]. "</b> in <b>file \""
. $debugArr[count($debugArr)- 1]["file"]. "\"</b>."
echo "<b>BACKTRACE DEBUG</b><br />\r\n";
if( !$this->parent->parent->is_connected )
$this->parent->parent->connect();
if( $this->parent->parent->is_connected )
} // End of class MySQLDatabaseTable
|