Codice PHP:
function build_and_exec_query( $a )
{
$this->build_query( $a );
$ci = $this->exec_query();
if ( isset($a['select']) AND $a['select'] )
{
return $this->fetch_row( $ci );
}
}
function exec_query()
{
if ( $this->cur_query != "" )
{
$ci = $this->query( $this->cur_query );
}
$this->cur_query = "";
$this->is_shutdown = 0;
return $ci;
}
function build_query( $a )
{
if ( isset($a['select']) && $a['select'] )
{
if ( isset($a['add_join']) && is_array( $a['add_join'] ) )
{
$this->simple_select_with_join( $a['select'], $a['from'], isset($a['where']) ? $a['where'] : '', $a['add_join'] );
}
else
{
$this->simple_select( $a['select'], $a['from'], isset($a['where']) ? $a['where'] : '' );
}
}
if ( isset($a['update']) && $a['update'] )
{
$this->simple_update( $a['update'], $a['set'], isset($a['where']) ? $a['where'] : '', isset($a['lowpro']) ? $a['lowpro'] : '' );
}
if ( isset($a['delete']) && $a['delete'] )
{
$this->simple_delete( $a['delete'], $a['where'] );
}
if ( isset($a['group']) && $a['group'] )
{
$this->simple_group( $a['group'] );
}
if ( isset($a['order']) && $a['order'] )
{
$this->simple_order( $a['order'] );
}
if ( isset($a['limit']) && is_array( $a['limit'] ) )
{
$this->simple_limit( $a['limit'][0], $a['limit'][1] );
}
}
function simple_select( $get, $table, $where="" )
{
$this->cur_query .= "SELECT $get FROM ".$this->obj['sql_tbl_prefix']."$table";
if ( $where != "" )
{
$this->cur_query .= " WHERE ".$where;
}
}
/*-------------------------------------------------------------------------*/
// SIMPLE: SELECT WITH JOIN
/*-------------------------------------------------------------------------*/
function simple_select_with_join( $get, $table, $where="", $add_join=array() )
{
//-----------------------------------------
// OK, here we go...
//-----------------------------------------
$select_array = array();
$from_array = array();
$joinleft_array = array();
$where_array = array();
$final_from = array();
$select_array[] = $get;
$from_array[] = $table;
if ( $where )
{
$where_array[] = $where;
}
//-----------------------------------------
// Loop through JOINs and sort info
//-----------------------------------------
if ( is_array( $add_join ) and count( $add_join ) )
{
foreach( $add_join as $join )
{
# Push join's select to stack
if ( isset($join['select']) AND $join['select'] )
{
$select_array[] = $join['select'];
}
if ( $join['type'] == 'inner' )
{
# Join is inline
$from_array[] = $join['from'];
if ( $join['where'] )
{
$where_array[] = $join['where'];
}
}
else if ( $join['type'] == 'left' )
{
# Join is left
$tmp = " LEFT JOIN ";
foreach( $join['from'] as $tbl => $alias )
{
$tmp .= $this->obj['sql_tbl_prefix'].$tbl.' '.$alias;
}
if ( $join['where'] )
{
$tmp .= " ON ( ".$join['where']." ) ";
}
$joinleft_array[] = $tmp;
unset( $tmp );
}
else
{
# Not using any other type of join
}
}
}
//-----------------------------------------
// Build it..
//-----------------------------------------
foreach( $from_array as $i )
{
foreach( $i as $tbl => $alias )
{
$final_from[] = $this->obj['sql_tbl_prefix'].$tbl.' '.$alias;
}
}
$get = implode( "," , $select_array );
$table = implode( "," , $final_from );
$where = implode( " AND " , $where_array );
$join = implode( "\n" , $joinleft_array );
$this->cur_query .= "SELECT $get FROM $table";
if ( $join )
{
$this->cur_query .= " ".$join." ";
}
if ( $where != "" )
{
$this->cur_query .= " WHERE ".$where;
}
}
ecc