R25071 - /trunk/etemplate/inc/class.so_sql.inc.php

Author: ralfbecker
New Revision: 25071

URL: http://www.egroupware.org/viewvc?rev=3D25071&view=3Drev
Log:
allow to use global db-object (and no clone&set_app), if told so via a sepa=
rate parameter to the constructor (not by default as the app using so_sql m=
ight not be ready for it)

Modified:
trunk/etemplate/inc/class.so_sql.inc.php

Modified: trunk/etemplate/inc/class.so_sql.inc.php
URL: http://www.egroupware.org/viewvc/trunk/etemplate/inc/class.so_sql.inc.=
php?rev=3D25071&r1=3D25070&r2=3D25071&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
— trunk/etemplate/inc/class.so_sql.inc.php (original)
+++ trunk/etemplate/inc/class.so_sql.inc.php Thu Mar 13 12:08:54 2008
@@ -6,6 +6,7 @@

@@ -61,11 +62,11 @@
/
var $total =3D false;
/
*

    • privat instance of the db-object
    • protected instance or reference (depeding on $no_clone param of constr=
      uctor) of the db-object

    • =

    • @var egw_db
      */

  • var $db;
  • protected $db;
    /**
    • unique keys/index, set by derived class or via so_sql($app,$table)
    • =

@@ -94,6 +95,12 @@
* @var array
*/
var $table_def =3D array();

  • /**
    • Appname to use in all queries, set via constructor
    • @var string
  • */
  • var $app;
    /**
    • holds the content of all columns
    • =

@@ -129,17 +136,28 @@
* @param string $table should be set if table-defs to be read from =
/setup/tables_current.inc.php
* @param object/db $db database object, if not the one in $GLOBALS[‘egw’=
]->db should be used, eg. for an other database
* @param string $colum_prefix=3D’’ column prefix to automatic remove fro=
m the column-name, if the column name starts with it

    • @param boolean $no_clone=3Dfalse can we avoid to clone the db-object, =
      default no =
    • new code using appnames and foreach(select(…,$app) can set it to av=
      oid an extra instance of the db object

    • =

    • @return so_sql
      */

  • function so_sql($app=3D’’,$table=3D’’,$db=3Dnull,$column_prefix=3D’’)
  • {
  •   $this->db =3D is_object($db) ? clone($db) : clone($GLOBALS['egw']->db);
    
  • function so_sql($app=3D’’,$table=3D’’,$db=3Dnull,$column_prefix=3D’’,$no_=
    clone=3Dfalse)
  • {
  •   if ($no_clone)
    
  •   {
    
  •   	$this->db =3D is_object($db) ? $db : $GLOBALS['egw']->db;
    
  •   }
    
  •   else
    
  •   {
    
  •   	$this->db =3D is_object($db) ? clone($db) : clone($GLOBALS['egw']->db);
    
  •   }
      $this->db_cols =3D $this->db_key_cols + $this->db_data_cols;
    

=

	if ($app)
	{
  •   	$this->db->set_app($app);
    
  •   	$this->app =3D $app;
    
  •   	=
    
  •   	if (!$no_clone) $this->db->set_app($app);
    

=

		if ($table) $this->setup_table($app,$table,$column_prefix);
	}

@@ -369,41 +387,39 @@
unset($query[$col]);
}
}

  •   $this->db->select($this->table_name,'*'.($extra_cols?','.(is_array($extr=
    

a_cols)?implode(’,’,$extra_cols):$extra_cols):’’),

  •   	$query,__LINE__,__FILE__,False,'',False,0,$join);
    
  •   if (!($row =3D $this->db->row(true)))
    
  •   {
    
  •   	if ($this->autoinc_id)
    
  •   	{
    
  •   		unset($this->data[$this->db_key_cols[$this->autoinc_id]]);
    
  •   	}
    
  •   	if ((int) $this->debug >=3D 4) echo "nothing found !!!</p>\n";
    
  •   foreach($this->db->select($this->table_name,'*'.($extra_cols?','.(is_arr=
    

ay($extra_cols)?implode(’,’,$extra_cols):$extra_cols):’’),

  •   	$query,__LINE__,__FILE__,False,'',$this->app,0,$join) as $row)
    
  •   {
    
  •   	$cols =3D $this->db_cols;
    
  •   	if ($extra_cols)	// extra columns to report
    
  •   	{
    
  •   		foreach(is_array($extra_cols) ? $extra_cols : array($extra_cols) as $c=
    

ol)

  •   		{
    
  •   			if (FALSE!=3D=3Dstripos($col,' as ')) $col =3D preg_replace('/^.* as =
    

*([a-z0-9_]+) *$/i’,’\1’,$col);

  •   			$cols[$col] =3D $col;
    
  •   		}
    
  •   	}
    
  •   	foreach ($cols as $db_col =3D> $col)
    
  •   	{
    
  •   		$this->data[$col] =3D $row[$db_col];
    
  •   	}
      	$this->db2data();
    
  •   	return False;
    
  •   }
    
  •   $cols =3D $this->db_cols;
    
  •   if ($extra_cols)	// extra columns to report
    
  •   {
    
  •   	foreach(is_array($extra_cols) ? $extra_cols : array($extra_cols) as $co=
    

l)

  •   	{
    
  •   		if (FALSE!=3D=3Dstripos($col,' as ')) $col =3D preg_replace('/^.* as *=
    

([a-z0-9_]+) *$/i’,’\1’,$col);

  •   		$cols[$col] =3D $col;
    
  •   	}
    
  •   }
    
  •   foreach ($cols as $db_col =3D> $col)
    
  •   {
    
  •   	$this->data[$col] =3D $row[$db_col];
    
  •   }
    
  • =

  •   	if ((int) $this->debug >=3D 4)
    
  •   	{
    
  •   		echo "data =3D\n"; _debug_array($this->data);
    
  •   	}
    
  •   	return $this->data;
    
  •   }
    
  •   if ($this->autoinc_id)
    
  •   {
    
  •   	unset($this->data[$this->db_key_cols[$this->autoinc_id]]);
    
  •   }
    
  •   if ((int) $this->debug >=3D 4) echo "nothing found !!!</p>\n";
    
  •   $this->db2data();
    

=

  •   if ((int) $this->debug >=3D 4)
    
  •   {
    
  •   	echo "data =3D\n"; _debug_array($this->data);
    
  •   }
    
  •   return $this->data;
    
  •   return False;
    

    }
    =

    /**
    @@ -435,7 +451,7 @@
    $data[$db_col] =3D (string) $this->data[$col] =3D=3D=3D ‘’ && $this->=
    empty_on_write =3D=3D ‘NULL’ ? null : $this->data[$col];
    }
    }

  •   	$this->db->insert($this->table_name,$data,false,__LINE__,__FILE__);
    
  •   	$this->db->insert($this->table_name,$data,false,__LINE__,__FILE__,$this=
    

->app);

		if ($this->autoinc_id)
		{

@@ -478,11 +494,11 @@
}
if (!$this->autoinc_id) // always try an insert if we have no autoinc_i=
d, as we dont know if the data exists
{

  •   		$this->db->insert($this->table_name,$data,$keys,__LINE__,__FILE__);
    
  •   		$this->db->insert($this->table_name,$data,$keys,__LINE__,__FILE__,$thi=
    

s->app);
}
else
{

  •   		$this->db->update($this->table_name,$data,$keys,__LINE__,__FILE__);
    
  •   		$this->db->update($this->table_name,$data,$keys,__LINE__,__FILE__,$thi=
    

s->app);
}
}
$this->db2data();
@@ -554,7 +570,7 @@
{
$query[$db_col] =3D $data[$col];
}

  •   $this->db->delete($this->table_name,$query,__LINE__,__FILE__);
    
  •   $this->db->delete($this->table_name,$query,__LINE__,__FILE__,$this->app);
    

=

	return $this->db->affected_rows();
}

@@ -620,8 +636,16 @@
list($table,$only_col) =3D explode(’.’,$db_col);
=

					$table_def =3D $this->db->get_table_definitions(true,$table);
  •   				=
    
  •   				$query[] =3D $db_col.(is_array($val) ? ' IN ' : '=3D').$this->db->qu=
    

ote($val,$table_def[‘fd’][$only_col]);
+

  •   				if (is_array($val) && count($val) > 1)
    
  •   				{
    
  •   					array_walk($val,array($this->db,'quote'),$table_def['fd'][$only_col=
    

][‘type’]);

  •   					$query[] =3D $sql =3D $db_col.' IN (' .implode(',',$val).')';
    
  •   				}
    
  •   				else
    
  •   				{
    
  •   					$query[] =3D $db_col.'=3D'.$this->db->quote(is_array($val)?array_sh=
    

ift($val):$val,$table_def[‘fd’][$only_col][‘type’]);

  •   				}
      			}
      			else
      			{
    

@@ -739,11 +763,10 @@
}
else // cant do a count, have to run the query without limit
{

  •   			$this->db->union($union,__LINE__,__FILE__);
    
  •   			$this->total =3D $this->db->num_rows();
    
  •   		}
    
  •   	}
    
  •   	$this->db->union($union,__LINE__,__FILE__,$order_by,$start,$num_rows);
    
  •   			$this->total =3D $this->db->union($union,__LINE__,__FILE__)->NumRows(=
    

);

  •   		}
    
  •   	}
    
  •   	$rs =3D $this->db->union($union,__LINE__,__FILE__,$order_by,$start,$num=
    

_rows);

		$cols =3D $union_cols;
		$union =3D $union_cols =3D array();

@@ -758,17 +781,15 @@
}
elseif (!$need_full_no_count && (!$join || stripos($join,‘LEFT JOIN’)!=
=3D=3Dfalse))
{

  •   			$this->db->select($this->table_name,'COUNT(*)',$query,__LINE__,__FILE=
    

__,false,’’,false,0,$join);

  •   			$this->total =3D $this->db->next_record() ? (int) $this->db->f(0) : f=
    

alse;

  •   			$this->total =3D $this->db->select($this->table_name,'COUNT(*)',$quer=
    

y,LINE,FILE,false,’’,$this->app,0,$join)->fetchSingle();
}
else // cant do a count, have to run the query without limit
{

  •   			$this->db->select($this->table_name,$colums,$query,__LINE__,__FILE__,=
    

false,$order_by,false,0,$join);

  •   			$this->total =3D $this->db->num_rows();
    
  •   		}
    
  •   	}
    
  •   	$this->db->select($this->table_name,$mysql_calc_rows.$colums,$query,__L=
    

INE__,FILE,

  •   		$start,$order_by,false,$num_rows,$join);
    
  •   			$this->total =3D $this->db->select($this->table_name,$colums,$query,_=
    

LINE_,FILE,false,$order_by,false,0,$join)->NumRows();

  •   		}
    
  •   	}
    
  •   	$rs =3D $this->db->select($this->table_name,$mysql_calc_rows.$colums,$q=
    

uery,LINE,FILE,

  •   		$start,$order_by,$this->app,$num_rows,$join);
    

=

		$cols =3D $this->_get_columns($only_keys,$extra_cols);
	}

@@ -776,10 +797,10 @@

	if ($mysql_calc_rows)
	{
  •   	$this->total =3D $this->db->Link_ID->GetOne('SELECT FOUND_ROWS()');
    
  •   	$this->total =3D $this->db->query('SELECT FOUND_ROWS()')->fetchSingle();
      }
      $arr =3D array();
    
  •   for ($n =3D 0; ($row =3D $this->db->row(true)); ++$n)
    
  •   if ($rs) foreach($rs as $row)
      {
      	$data =3D array();
      	foreach($cols as $db_col =3D> $col)
    

@@ -787,6 +808,7 @@
$data[$col] =3D $row[$db_col];
}
$arr[] =3D $this->db2data($data);

  •   	$n++;
      }
      return $n ? $arr : False;
    
    }
    @@ -857,9 +879,10 @@
    • @param string $join=3D’’ sql to do a join, added as is after the table=
      -name, eg. “, table2 WHERE x=3Dy” or =

    • “LEFT JOIN table2 ON (x=3Dy)”, Note: there’s no quoting done on $join!

    • @param boolean $need_full_no_count=3Dfalse If true an unlimited query =
      is run to determine the total number of rows, default false

    • @param mixed $only_keys=3Dfalse, see search
    • @return int total number of rows
      */
  • function get_rows($query,&$rows,&$readonlys,$join=3D’’,$need_full_no_coun=
    t=3Dfalse)
  • function get_rows($query,&$rows,&$readonlys,$join=3D’’,$need_full_no_coun=
    t=3Dfalse,$only_keys=3Dfalse)
    {
    if ((int) $this->debug >=3D 4)
    {
    @@ -873,7 +896,7 @@
    $criteria[$col] =3D $query[‘search’];
    }
    }
  •   $rows =3D $this->search($criteria,false,$query['order']?$query['order'].=
    

’ ‘.$query[‘sort’]:’’,

  •   $rows =3D $this->search($criteria,$only_keys,$query['order']?$query['ord=
    

er’].’ ‘.$query[‘sort’]:’’,
’’,’%’,false,‘OR’,$query[‘num_rows’]?array((int)$query[‘start’],$query[=
‘num_rows’]):(int)$query[‘start’],
$query[‘col_filter’],$join,$need_full_no_count);
=

@@ -977,14 +1000,3 @@
return $cache[$cache_key] =3D& $ret;
}
}

-if (!function_exists(“stripos”)) =

-{

  • /**
    • stripos for php < 5
  • */
  • function stripos($str,$needle,$offset=3D0)
  • {
  •   return strpos(strtolower($str),strtolower($needle),$offset);
    
  • }
    -}

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft® Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/


eGroupWare-cvs mailing list
eGroupWare-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/egroupware-cvs