Source for file MySQLView.class.php
Documentation is available at MySQLView.class.php
* 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";
* Loading the superior class on which this class depends.
require_once( CLASSPATH. "databases/ABSTView.class.php" );
* Loading functions to handle arrays.
include_once( CLASSPATH. "core/Arrays.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) 2005 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
* Stores the condition on every query with this view. It is helpful to define
* inner joins in the where clause.
* @author Daniel Plücken <daniel@debakel.net>
* Sets the condition of first priority.
* @author Daniel Plücken <daniel@debakel.net>
* Returns the correct where clause from given where concate the fixed where.
* @author Daniel Plücken <daniel@debakel.net>
"!^(?:GROUP BY|ORDER BY|LIMIT|PROCEDURE|INTO OUTFILE)!i",
$string = "1=1 ". $string;
$int_pos[] = strpos( $str_copy, "GROUP BY" );
$int_pos[] = strpos( $str_copy, "ORDER BY" );
$int_pos[] = strpos( $str_copy, "LIMIT" );
$int_pos[] = strpos( $str_copy, "PROCEDURE" );
$int_pos[] = strpos( $str_copy, "INTO OUTFILE" );
// remove failed matching entries
for ( $i = count( $int_pos ) - 1; $i >= 0; $i-- )
if ( $int_pos[$i] === false )
if ( count( $int_pos ) <= 0 )
$closing_pos = min( $int_pos );
* Returns the order to join the database tables in the query.
* @author Daniel Plücken <daniel@debakel.net>
* @param array $field_arr In-out-parameter.
// Fields of the left database table
// Fields of the right database table
for ( $i = 0; $i < count( $this->join ); $i++ )
$tmpFArr1 = $this->join[$i]->dbt_left->getFieldnames(
!$this->join[$i]->dbt_left->overruled_fieldnames
for( $k = 0; $k < count( $tmpFArr1 ); $k++ )
"!^(.+?) +AS +[-_a-z0-9]+$!is",
// Setting up the field list
$this->join[$i]->alias_left. ".$1",
. "AS ". $this->join[$i]->alias_left. "_"
. preg_replace( "!^.*?([-_a-z0-9]+)`?$!is", "$1", $tmpFArr1[$k] )
$tmpFArr2 = $this->join[$i]->dbt_right->getFieldnames(
!$this->join[$i]->dbt_right->overruled_fieldnames
for( $k = 0; $k < count( $tmpFArr2 ); $k++ )
"!^(.+?) +AS +[-_a-z0-9]+$!is",
// Setting up the field list
$this->join[$i]->alias_right. ".$1",
. "AS ". $this->join[$i]->alias_right. "_"
. preg_replace( "!^.*?([-_a-z0-9]+)`?$!is", "$1", $tmpFArr2[$k] )
$tmp_joins .= $this->join[$i]->type
. " JOIN `". $this->join[$i]->dbt_right->name. "` "
. "AS ". $this->join[$i]->alias_right. " "
strpos( $this->type, "NATURAL" ) === false
if( empty( $this->join[$i]->relational_operator[$k] ) )
$this->join[$i]->relational_operator[$k] = "=";
). $this->join[$i]->alias_left. "."
. $this->join[$i]->left_field[$k]. " "
. $this->join[$i]->relational_operator[$k]. " "
? $this->join[$i]->alias_right. "."
. $this->join[$i]->right_field[$k]. " \r\n";
while( $k <= count( $this->join[$i]->logical_operator ) );
* Returns the datasets as an array if the query could be execute.
* Notice that the fieldnames will be output in the special form that the
* given alias of a table and the responsed fieldname are separated by an
* underscore, for example "alias_fieldname".
* @author Daniel Plücken <daniel@debakel.net>
* @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.
* @author Daniel Plücken <daniel@debakel.net>
* @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;
foreach ( $value_fields as $str_value_field )
if ( !empty( $str_value ) )
$str_value .= $str_value_field;
$sqlOrder = "SELECT ". $primary_key. ", "
. "FROM `". $this->join[0]->dbt_left->name. "` "
. "AS ". $this->join[0]->alias_left. " "
$result = $this->join[0]->dbt_left->parent->query( $sqlOrder, false, $this );
"!^.+? +AS +([-_a-z0-9]+)$!is",
"!^.+? +AS +([-_a-z0-9]+)$!is",
$datasets[ $resultArr[$primary_key] ] = $resultArr[$value_fields];
$datasets[ $resultArr[$primary_key] ] = $resultArr;
* Returns the datasets as array with the values of the primary key as index.
* @author Daniel Plücken <daniel@debakel.net>
* @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;
foreach ( $value_fields as $str_value_field )
if ( !empty( $str_value ) )
$str_value .= $str_value_field;
$sqlOrder = "SELECT ". $primary_key. ", "
. "FROM `". $this->join[0]->dbt_left->name. "` "
. "AS ". $this->join[0]->alias_left. " "
. "ORDER BY `". $primary_key. "` ASC";
$result = $this->join[0]->dbt_left->parent->query( $sqlOrder, false, $this );
"!^.+? +AS +([-_a-z0-9]+)$!is",
"!^.+? +AS +([-_a-z0-9]+)$!is",
$datasets[ $resultArr[$primary_key] ][] = $resultArr[$value_fields];
$datasets[ $resultArr[$primary_key] ][] = $resultArr;
* Returns a array of values of a data field.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $fieldname
$sqlOrder = " SELECT ". $fieldname. " \r\n"
. " FROM `". $this->join[0]->dbt_left->name. "` "
. "AS ". $this->join[0]->alias_left. " \r\n"
$result = $this->join[0]->dbt_left->parent->query( $sqlOrder );
$tmp_arr[] = $tmp_row[0];
* 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, $only_one );
* @author Daniel Plücken <daniel@debakel.net>
* @param array $field_arr Is a blind parameter whithout any funktion.
* @param string $where The where clause of the query.
* @param boolean $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
$sqlOrder = "SELECT ". implode( ", ", $field_arr ). " "
. "FROM `". $this->join[0]->dbt_left->name. "` "
. "AS ". $this->join[0]->alias_left. " "
$result = $this->join[0]->dbt_left->parent->query( $sqlOrder );
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;
// looking for callback function of the database host object.
!empty( $this->join[0]->dbt_left->parent->parent->error_callback )
$this->join[0]->dbt_left->parent->parent->error_callback
// Access denied for user
$tmp_name = $this->join[0]->dbt_left
->parent->parent->error_callback;
$tmp_name( $errno, $err );
$tmp_name( $errno, $err );
echo "<b>An error occurs!</b>\r\n";
echo "<b>BACKTRACE DEBUG:</b>\r\n";
echo "<b>MySQL says:</b><br />\r\n";
echo "<b>Your query is:</b><br />\r\n";
die( $sqlOrder. "<br />" );
* Returns a single dataset as an array if the query could be execute.
* Notice that the fieldnames will be output in the special form that the
* given alias of a table and the responsed fieldname are separated by an
* underscore, for example "alias_fieldname".
* @author Daniel Plücken <daniel@debakel.net>
* @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
* Returns a single value of a data field.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $fieldname
array( $fieldname ), $where, true
* Returns the sum value of a field.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $fieldname
* @param string $where The where-clause is optional.
* @param string $additional_arithmetic Additional calculations to the
function getSumOf( $fieldname, $where = "1=1", $additional_arithmetic = "" )
$sqlOrder = "SELECT SUM( ". $fieldname. " )"
. $additional_arithmetic. " "
. "FROM `". $this->join[0]->dbt_left->name. "` "
. "AS ". $this->join[0]->alias_left. " \r\n"
$res = $this->join[0]->dbt_left->parent->query( $sqlOrder, false, $this );
* Returns the count of datasets.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $fieldname
function getCount( $where = "1=1", $fieldname = "*" )
$sqlOrder = "SELECT count(". $fieldname. ") "
. "FROM `". $this->join[0]->dbt_left->name. "` "
. "AS ". $this->join[0]->alias_left. " "
$res = $this->join[0]->dbt_left->parent->query( $sqlOrder, false, $this );
} // End of class MySQLView
|