2002-02-18 07:52:33 +01:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
2004-01-27 17:58:19 +01:00
|
|
|
* eGroupWare - generalized SQL Storage Object *
|
|
|
|
* http://www.egroupware.org *
|
2002-02-18 07:52:33 +01:00
|
|
|
* Written by Ralf Becker <RalfBecker@outdoor-training.de> *
|
|
|
|
* -------------------------------------------- *
|
|
|
|
* This program is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU General Public License as published by the *
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your *
|
|
|
|
* option) any later version. *
|
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* generalized SQL Storage Object
|
|
|
|
*
|
|
|
|
* the class can be used in following ways:
|
|
|
|
* 1) by calling the constructor with an app and table-name or
|
|
|
|
* 2) by setting the following documented class-vars in a class derifed from this one
|
|
|
|
* Of cause can you derife the class and call the constructor with params.
|
|
|
|
*
|
|
|
|
* @package etemplate
|
2005-02-22 23:11:00 +01:00
|
|
|
* @subpackage contrib
|
2005-02-13 14:03:36 +01:00
|
|
|
* @author RalfBecker-AT-outdoor-training.de
|
2005-04-08 20:30:32 +02:00
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
2005-02-13 14:03:36 +01:00
|
|
|
*/
|
2002-02-18 07:52:33 +01:00
|
|
|
class so_sql
|
|
|
|
{
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* @var string $table_name need to be set in the derived class to the db-table-name
|
|
|
|
*/
|
|
|
|
var $table_name;
|
|
|
|
/**
|
|
|
|
* @var string $autoinc_id db-col-name of autoincrement id or ''
|
|
|
|
*/
|
|
|
|
var $autoinc_id = '';
|
|
|
|
/**
|
|
|
|
* @var array $non_db_cols all cols in data which are not (direct)in the db, for data_merge
|
|
|
|
*/
|
|
|
|
var $non_db_cols = array();
|
|
|
|
/**
|
2005-10-16 13:45:59 +02:00
|
|
|
* @var int $debug=0 4 turns on the so_sql debug-messages
|
2005-02-13 14:03:36 +01:00
|
|
|
*/
|
|
|
|
var $debug = 0;
|
|
|
|
/**
|
|
|
|
* @var string $empty_on_write string to be written to db if a col-value is '', eg. "''" or 'NULL' (default)
|
|
|
|
*/
|
|
|
|
var $empty_on_write = 'NULL';
|
2005-02-27 22:31:10 +01:00
|
|
|
/**
|
|
|
|
* @var int/boolean $total total number of entries of last search with start != false
|
|
|
|
*/
|
|
|
|
var $total = false;
|
|
|
|
/**
|
|
|
|
* @var db-object $db privat instance of the db-object
|
|
|
|
*/
|
2005-02-13 14:03:36 +01:00
|
|
|
var $db;
|
2005-02-27 22:31:10 +01:00
|
|
|
/**
|
|
|
|
* @var array $db_uni_cols unique keys/index, set by derived class or via so_sql($app,$table)
|
|
|
|
*/
|
2002-02-18 07:52:33 +01:00
|
|
|
var $db_uni_cols = array();
|
2005-02-27 22:31:10 +01:00
|
|
|
/**
|
|
|
|
* @var array $db_key_cols db-col-name / internal-name pairs, set by derived calls or via so_sql($app,$table)
|
|
|
|
*/
|
|
|
|
var $db_key_cols = array();
|
|
|
|
/**
|
|
|
|
* @var array $db_data_cols db-col-name / internal-name pairs, set by derived calls or via so_sql($app,$table)
|
|
|
|
*/
|
|
|
|
var $db_data_cols = array();
|
|
|
|
/**
|
|
|
|
* @var array $db_cols all columns = $db_key_cols + $db_data_cols, set in the constructor
|
|
|
|
*/
|
|
|
|
var $db_cols = array();
|
|
|
|
/**
|
|
|
|
* @var array $data holds the content of all columns
|
|
|
|
*/
|
|
|
|
var $data = array();
|
|
|
|
/**
|
|
|
|
* @deprecated a SO class dont need to and should NOT export functions (make them callable via menuaction)
|
|
|
|
* @var array $public_functions
|
|
|
|
*/
|
|
|
|
var $public_functions = array();
|
2002-02-18 07:52:33 +01:00
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* constructor of the class
|
|
|
|
*
|
|
|
|
* NEED to be called from the constructor of the derived class !!!
|
|
|
|
*
|
|
|
|
* @param string $app should be set if table-defs to be read from <app>/setup/tables_current.inc.php
|
|
|
|
* @param string $table should be set if table-defs to be read from <app>/setup/tables_current.inc.php
|
2005-03-06 22:39:46 +01:00
|
|
|
* @param object/db $db database object, if not the one in $GLOBALS['egw']->db should be used, eg. for an other database
|
2005-02-13 14:03:36 +01:00
|
|
|
*/
|
2005-03-06 22:39:46 +01:00
|
|
|
function so_sql($app='',$table='',$db=null)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2005-03-06 22:39:46 +01:00
|
|
|
$this->db = is_object($db) ? clone($db) : clone($GLOBALS['egw']->db);
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db_cols = $this->db_key_cols + $this->db_data_cols;
|
|
|
|
|
2005-02-27 22:31:10 +01:00
|
|
|
if ($app)
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2005-02-27 22:31:10 +01:00
|
|
|
$this->db->set_app($app);
|
|
|
|
|
|
|
|
if ($table) $this->setup_table($app,$table);
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->init();
|
|
|
|
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
echo "<p>so_sql('$app','$table')</p>\n";
|
|
|
|
_debug_array($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
2005-02-27 22:31:10 +01:00
|
|
|
* sets up the class for an app and table (by using the table-definition of $app/setup/tables_current.inc.php
|
2005-02-13 14:03:36 +01:00
|
|
|
*
|
2005-02-27 22:31:10 +01:00
|
|
|
* Does NOT set a different internal-data-name. If you want this, you have to do so in a derifed class !!!
|
2005-02-13 14:03:36 +01:00
|
|
|
*/
|
2002-02-18 07:52:33 +01:00
|
|
|
function setup_table($app,$table)
|
|
|
|
{
|
|
|
|
$this->table_name = $table;
|
2005-02-27 22:31:10 +01:00
|
|
|
$table_def = $this->db->get_table_definitions($app,$table);
|
2005-03-06 22:39:46 +01:00
|
|
|
if (!$table_def || !is_array($table_def['fd']))
|
|
|
|
{
|
|
|
|
echo "<p>so_sql::setup_table('$app','$table'): No table definitions found !!!<br>\n".function_backtrace()."</p>\n";
|
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db_key_cols = $this->db_data_cols = $this->db_cols = array();
|
|
|
|
$this->autoinc_id = '';
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach($table_def['fd'] as $name => $def)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
if (in_array($name,$table_def['pk']))
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db_key_cols[$name] = $name;
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
else
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db_data_cols[$name] = $name;
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db_cols[$name] = $name;
|
|
|
|
|
|
|
|
if ($def['type'] == 'auto')
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->autoinc_id = $name;
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
if (in_array($name,$table_def['uc']))
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db_uni_cols[$name] = $name;
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* merges in new values from the given new data-array
|
|
|
|
*
|
|
|
|
* @param $new array in form col => new_value with values to set
|
|
|
|
*/
|
2003-03-27 18:21:58 +01:00
|
|
|
function data_merge($new)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4) echo "<p>so_sql::data_merge(".print_r($new,true).")</p>\n";
|
2005-04-08 20:30:32 +02:00
|
|
|
|
2002-02-18 07:52:33 +01:00
|
|
|
if (!is_array($new) || !count($new))
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
return;
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
|
|
|
foreach($this->db_cols as $db_col => $col)
|
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
if (isset($new[$col]))
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->data[$col] = $new[$col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach($this->non_db_cols as $db_col => $col)
|
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
if (isset($new[$col]))
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->data[$col] = $new[$col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4) _debug_array($this->data);
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* changes the data from the db-format to your work-format
|
|
|
|
*
|
|
|
|
* it gets called everytime when data is read from the db
|
|
|
|
* This function needs to be reimplemented in the derived class
|
|
|
|
*
|
|
|
|
* @param array $data if given works on that array and returns result, else works on internal data-array
|
|
|
|
*/
|
|
|
|
function db2data($data=null)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2005-02-13 14:03:36 +01:00
|
|
|
if (!is_array($data))
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2005-02-13 14:03:36 +01:00
|
|
|
$data = &$this->data;
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
// do the necessare changes here
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* changes the data from your work-format to the db-format
|
|
|
|
*
|
|
|
|
* It gets called everytime when data gets writen into db or on keys for db-searches
|
|
|
|
* this needs to be reimplemented in the derived class
|
|
|
|
*
|
|
|
|
* @param array $data if given works on that array and returns result, else works on internal data-array
|
|
|
|
*/
|
|
|
|
function data2db($data=null)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
if ($intern = !is_array($data))
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2005-02-13 14:03:36 +01:00
|
|
|
$data = &$this->data;
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
// do the necessary changes here
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* initializes data with the content of key
|
|
|
|
*
|
|
|
|
* @param array $keys array with keys in form internalName => value
|
|
|
|
*/
|
2003-03-27 18:21:58 +01:00
|
|
|
function init($keys=array())
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
$this->data = array();
|
|
|
|
|
|
|
|
$this->db2data();
|
|
|
|
|
|
|
|
$this->data_merge($keys);
|
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* reads row matched by key and puts all cols in the data array
|
|
|
|
*
|
|
|
|
* @param array $keys array with keys in form internalName => value, may be a scalar value if only one key
|
2005-04-10 23:15:33 +02:00
|
|
|
* @param string/array $extra_cols string or array of strings to be added to the SELECT, eg. "count(*) as num"
|
|
|
|
* @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or
|
2005-02-13 14:03:36 +01:00
|
|
|
* @return array/boolean data if row could be retrived else False
|
2002-02-18 07:52:33 +01:00
|
|
|
*/
|
2005-04-10 23:15:33 +02:00
|
|
|
function read($keys,$extra_cols='',$join='')
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2003-11-03 17:19:48 +01:00
|
|
|
if (!is_array($keys))
|
|
|
|
{
|
|
|
|
$pk = array_keys($this->db_key_cols);
|
|
|
|
if ($pk) $keys = array($pk[0] => $keys);
|
|
|
|
}
|
|
|
|
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->init($keys);
|
|
|
|
$this->data2db();
|
2005-04-24 19:11:34 +02:00
|
|
|
|
2005-02-27 22:31:10 +01:00
|
|
|
$query = false;
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach ($this->db_key_cols as $db_col => $col)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
if ($this->data[$col] != '')
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2005-02-27 22:31:10 +01:00
|
|
|
$query[$db_col] = $this->data[$col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
if (!$query) // no primary key in keys, lets try the data_cols for a unique key
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2005-04-24 19:11:34 +02:00
|
|
|
foreach($this->db_uni_cols as $db_col => $col)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
if ($this->data[$col] != '')
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2005-02-27 22:31:10 +01:00
|
|
|
$query[$db_col] = $this->data[$col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
if (!$query) // keys has no cols
|
|
|
|
{
|
|
|
|
$this->db2data();
|
|
|
|
|
|
|
|
return False;
|
|
|
|
}
|
2005-04-10 23:15:33 +02:00
|
|
|
$this->db->select($this->table_name,'*'.($extra_cols?','.(is_array($extra_cols)?implode(',',$extra_cols):$extra_cols):''),
|
|
|
|
$query,__LINE__,__FILE__,False,'',False,0,$join);
|
2002-02-18 07:52:33 +01:00
|
|
|
|
2005-04-10 23:15:33 +02:00
|
|
|
if (!($row = $this->db->row(true)))
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
if ($this->autoinc_id)
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
unset($this->data[$this->db_key_cols[$this->autoinc_id]]);
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4) echo "nothing found !!!</p>\n";
|
2002-02-18 07:52:33 +01:00
|
|
|
|
|
|
|
$this->db2data();
|
|
|
|
|
|
|
|
return False;
|
|
|
|
}
|
2005-04-10 23:15:33 +02:00
|
|
|
$cols = $this->db_cols;
|
|
|
|
if ($extra_cols) // extra columns to report
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2005-04-10 23:15:33 +02:00
|
|
|
foreach(is_array($extra_cols) ? $extra_cols : array($extra_cols) as $col)
|
|
|
|
{
|
|
|
|
if (stristr($col,'as')) $col = preg_replace('/^.*as *([a-z0-9_]+) *$/i','\\1',$col);
|
|
|
|
$cols[$col] = $col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($cols as $db_col => $col)
|
|
|
|
{
|
|
|
|
$this->data[$col] = $row[$db_col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db2data();
|
|
|
|
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
echo "data =\n"; _debug_array($this->data);
|
|
|
|
}
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* saves the content of data to the db
|
|
|
|
*
|
|
|
|
* @param array $keys if given $keys are copied to data before saveing => allows a save as
|
|
|
|
* @return int 0 on success and errno != 0 else
|
|
|
|
*/
|
|
|
|
function save($keys=null)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2005-02-13 14:03:36 +01:00
|
|
|
if (is_array($keys) && count($keys)) $this->data_merge($keys);
|
2002-02-18 07:52:33 +01:00
|
|
|
|
|
|
|
$this->data2db();
|
|
|
|
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4) { echo "so_sql::save(".print_r($keys,true).") autoinc_id='$this->autoinc_id', data="; _debug_array($this->data); }
|
2005-04-08 20:30:32 +02:00
|
|
|
|
2005-02-27 22:31:10 +01:00
|
|
|
if ($this->autoinc_id && !$this->data[$this->db_key_cols[$this->autoinc_id]]) // insert
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach($this->db_cols as $db_col => $col)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2003-03-27 18:21:58 +01:00
|
|
|
if (!$this->autoinc_id || $db_col != $this->autoinc_id) // not write auto-inc-id
|
|
|
|
{
|
2005-04-08 20:30:32 +02:00
|
|
|
$data[$db_col] = (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
2005-02-27 22:31:10 +01:00
|
|
|
$this->db->insert($this->table_name,$data,false,__LINE__,__FILE__);
|
2002-02-18 07:52:33 +01:00
|
|
|
|
|
|
|
if ($this->autoinc_id)
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 14:51:04 +01:00
|
|
|
$this->data[$this->db_key_cols[$this->autoinc_id]] = $this->db->get_last_insert_id($this->table_name,$this->autoinc_id);
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
else //update existing row, preserv other cols not used here
|
|
|
|
{
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach($this->db_data_cols as $db_col => $col)
|
|
|
|
{
|
2005-04-08 20:30:32 +02:00
|
|
|
$data[$db_col] = (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$keys = '';
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach($this->db_key_cols as $db_col => $col)
|
|
|
|
{
|
2005-02-27 22:31:10 +01:00
|
|
|
$keys[$db_col] = $this->data[$col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2005-05-21 20:17:21 +02:00
|
|
|
if (!$data && !$this->autoinc_id) // happens if all columns are in the primary key
|
|
|
|
{
|
|
|
|
$data = $keys;
|
|
|
|
$keys = False;
|
|
|
|
}
|
2005-04-08 20:30:32 +02:00
|
|
|
if (!$this->autoinc_id) // always try an insert if we have no autoinc_id, as we dont know if the data exists
|
|
|
|
{
|
|
|
|
$this->db->insert($this->table_name,$data,$keys,__LINE__,__FILE__);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->db->update($this->table_name,$data,$keys,__LINE__,__FILE__);
|
|
|
|
}
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db2data();
|
|
|
|
|
2005-04-08 20:30:32 +02:00
|
|
|
return $this->db->Errno;
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* deletes row representing keys in internal data or the supplied $keys if != null
|
|
|
|
*
|
|
|
|
* @param array $keys if given array with col => value pairs to characterise the rows to delete
|
2005-04-15 00:09:40 +02:00
|
|
|
* @return int affected rows, should be 1 if ok, 0 if an error
|
|
|
|
*/
|
2005-02-13 14:03:36 +01:00
|
|
|
function delete($keys=null)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
if (!is_array($keys) || !count($keys)) // use internal data
|
|
|
|
{
|
|
|
|
$data = $this->data;
|
|
|
|
$keys = $this->db_key_cols;
|
|
|
|
}
|
|
|
|
else // data and keys are supplied in $keys
|
|
|
|
{
|
|
|
|
$data = $keys; $keys = array();
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach($this->db_cols as $db_col => $col)
|
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
if (isset($data[$col]))
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$keys[$db_col] = $col;
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
$data = $this->data2db($data);
|
|
|
|
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach($keys as $db_col => $col)
|
|
|
|
{
|
2005-02-27 22:31:10 +01:00
|
|
|
$query[$db_col] = $data[$col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2005-02-27 22:31:10 +01:00
|
|
|
$this->db->delete($this->table_name,$query,__LINE__,__FILE__);
|
2002-02-18 07:52:33 +01:00
|
|
|
|
|
|
|
return $this->db->affected_rows();
|
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* searches db for rows matching searchcriteria
|
|
|
|
*
|
|
|
|
* '*' and '?' are replaced with sql-wildcards '%' and '_'
|
|
|
|
*
|
|
|
|
* @param array/string $criteria array of key and data cols, OR a SQL query (content for WHERE), fully quoted (!)
|
2005-06-11 12:49:50 +02:00
|
|
|
* @param boolean/string $only_keys=true True returns only keys, False returns all cols. comma seperated list of keys to return
|
2005-04-24 19:11:34 +02:00
|
|
|
* @param string $order_by='' fieldnames + {ASC|DESC} separated by colons ',', can also contain a GROUP BY (if it contains ORDER BY)
|
|
|
|
* @param string/array $extra_cols='' string or array of strings to be added to the SELECT, eg. "count(*) as num"
|
|
|
|
* @param string $wildcard='' appended befor and after each criteria
|
|
|
|
* @param boolean $empty=false False=empty criteria are ignored in query, True=empty have to be empty in row
|
|
|
|
* @param string $op='AND' defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
|
|
|
|
* @param mixed $start=false if != false, return only maxmatch rows begining with start, or array($start,$num)
|
|
|
|
* @param array $filter=null if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards
|
2005-04-08 20:30:32 +02:00
|
|
|
* @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or
|
|
|
|
* "LEFT JOIN table2 ON (x=y)", Note: there's no quoting done on $join!
|
2005-05-19 15:15:19 +02:00
|
|
|
* @param boolean $need_full_no_count=false If true an unlimited query is run to determine the total number of rows, default false
|
2005-02-13 14:03:36 +01:00
|
|
|
* @return array of matching rows (the row is an array of the cols) or False
|
|
|
|
*/
|
2005-05-19 15:15:19 +02:00
|
|
|
function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='',$need_full_no_count=false)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4) echo "<p>so_sql::search(".print_r($criteria,true).",'$only_keys','$order_by','$extra_cols','$wildcard','$empty','$op','$start',".print_r($filter,true).",'$join')</p>\n";
|
2005-04-27 22:47:34 +02:00
|
|
|
|
2003-04-16 16:32:15 +02:00
|
|
|
if (!is_array($criteria))
|
|
|
|
{
|
2005-02-27 22:31:10 +01:00
|
|
|
$query = $criteria;
|
2003-04-16 16:32:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$criteria = $this->data2db($criteria);
|
2005-05-19 15:15:19 +02:00
|
|
|
foreach($criteria as $col => $val)
|
|
|
|
{
|
|
|
|
if (is_int($col))
|
|
|
|
{
|
|
|
|
$query[] = $val;
|
|
|
|
}
|
|
|
|
elseif ($empty || $val != '')
|
2003-04-16 16:32:15 +02:00
|
|
|
{
|
2005-05-19 15:15:19 +02:00
|
|
|
if (!($db_col = array_search($col,$this->db_cols)))
|
|
|
|
{
|
|
|
|
$db_col = $col;
|
|
|
|
}
|
2005-04-08 20:30:32 +02:00
|
|
|
if ($wildcard || strstr($criteria[$col],'*') || strstr($criteria[$col],'?'))
|
|
|
|
{
|
|
|
|
$query[] = $db_col.' LIKE '.$this->db->quote($wildcard.str_replace(array('%','_','*','?'),array('\\%','\\_','%','_'),$criteria[$col]).$wildcard);
|
|
|
|
}
|
2005-05-19 15:15:19 +02:00
|
|
|
elseif (strstr($db_col,'.')) // we have a table-name specified
|
|
|
|
{
|
|
|
|
list($table,$only_col) = explode('.',$db_col);
|
|
|
|
|
|
|
|
$table_def = $this->db->get_table_definitions(false,$table);
|
|
|
|
|
|
|
|
$query[] = $db_col.(is_array($val) ? ' IN ' : '=').$this->db->quote($val,$table_def['fd'][$only_col]);
|
|
|
|
}
|
2005-04-08 20:30:32 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$query[$db_col] = $criteria[$col];
|
|
|
|
}
|
2003-04-16 16:32:15 +02:00
|
|
|
}
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2005-04-08 20:30:32 +02:00
|
|
|
if (is_array($query) && $op != 'AND') $query = $this->db->column_data_implode(' '.$op.' ',$query);
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
2005-02-27 22:31:10 +01:00
|
|
|
if (is_array($filter))
|
|
|
|
{
|
|
|
|
$db_filter = array();
|
2005-03-05 15:54:14 +01:00
|
|
|
$data2db_filter = $this->data2db($filter);
|
|
|
|
if (!is_array($data2db_filter)) {
|
|
|
|
echo function_backtrace()."<br/>\n";
|
|
|
|
echo "filter=";_debug_array($filter);
|
|
|
|
echo "data2db(filter)=";_debug_array($data2db_filter);
|
|
|
|
}
|
2005-03-29 19:06:50 +02:00
|
|
|
foreach($data2db_filter as $col => $val)
|
2005-02-27 22:31:10 +01:00
|
|
|
{
|
2005-04-19 15:54:55 +02:00
|
|
|
if ($val !== '')
|
|
|
|
{
|
2005-04-22 15:49:36 +02:00
|
|
|
// check if a db-internal name conversation necessary
|
|
|
|
if (!is_numeric($col) && ($c = array_search($col,$this->db_cols)))
|
|
|
|
{
|
|
|
|
$col = $c;
|
|
|
|
}
|
2005-04-19 15:54:55 +02:00
|
|
|
$db_filter[$col] = $val;
|
|
|
|
}
|
2005-02-27 22:31:10 +01:00
|
|
|
}
|
2005-04-08 20:30:32 +02:00
|
|
|
if ($query)
|
|
|
|
{
|
|
|
|
if ($op != 'AND')
|
|
|
|
{
|
|
|
|
$db_filter[] = '('.$this->db->column_data_implode(' '.$op.' ',$query).')';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$db_filter = array_merge($db_filter,$query);
|
|
|
|
}
|
|
|
|
}
|
2005-02-27 22:31:10 +01:00
|
|
|
$query = $db_filter;
|
|
|
|
}
|
2005-04-24 19:11:34 +02:00
|
|
|
$num_rows = 0; // as spec. in max_matches in the user-prefs
|
|
|
|
if (is_array($start)) list($start,$num_rows) = $start;
|
|
|
|
|
|
|
|
if ($start !== false && $num_rows != 1) // need to get the total too, saved in $this->total
|
2005-02-27 22:31:10 +01:00
|
|
|
{
|
2005-05-19 15:15:19 +02:00
|
|
|
if ($this->db->Type == 'mysql' && $this->db->ServerInfo['version'] >= 4.0)
|
|
|
|
{
|
|
|
|
$mysql_calc_rows = 'SQL_CALC_FOUND_ROWS ';
|
|
|
|
}
|
|
|
|
elseif (!$need_full_no_count && (!$join || stristr($join,'LEFT JOIN')))
|
2005-04-27 22:47:34 +02:00
|
|
|
{
|
|
|
|
$this->db->select($this->table_name,'COUNT(*)',$query,__LINE__,__FILE__);
|
|
|
|
$this->total = $this->db->next_record() ? (int) $this->db->f(0) : false;
|
|
|
|
}
|
|
|
|
else // cant do a count, have to run the query without limit
|
|
|
|
{
|
|
|
|
$this->db->select($this->table_name,($only_keys === true ? implode(',',$this->db_key_cols) : (!$only_keys ? '*' : $only_keys)).
|
|
|
|
($extra_cols ? ','.(is_array($extra_cols) ? implode(',',$extra_cols) : $extra_cols) : ''),
|
|
|
|
$query,__LINE__,__FILE__,false,$order_by && !stristr($order_by,'ORDER BY') ? 'ORDER BY '.$order_by : $order_by,false,0,$join);
|
|
|
|
$this->total = $this->db->num_rows();
|
|
|
|
}
|
2005-02-27 22:31:10 +01:00
|
|
|
}
|
2005-05-19 15:15:19 +02:00
|
|
|
$this->db->select($this->table_name,$mysql_calc_rows.($only_keys === true ? implode(',',$this->db_key_cols) : (!$only_keys ? '*' : $only_keys)).
|
2005-03-05 15:54:14 +01:00
|
|
|
($extra_cols ? ','.(is_array($extra_cols) ? implode(',',$extra_cols) : $extra_cols) : ''),
|
2005-04-24 19:11:34 +02:00
|
|
|
$query,__LINE__,__FILE__,$start,$order_by && !stristr($order_by,'ORDER BY') ? 'ORDER BY '.$order_by : $order_by,false,$num_rows,$join);
|
2002-02-18 07:52:33 +01:00
|
|
|
|
2005-05-19 15:15:19 +02:00
|
|
|
if ($mysql_calc_rows)
|
|
|
|
{
|
|
|
|
$this->total = $this->db->Link_ID->GetOne('SELECT FOUND_ROWS()');
|
|
|
|
}
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2005-02-27 22:31:10 +01:00
|
|
|
echo "<p>so_sql::search(,only_keys=$only_keys,order_by='$order_by',wildcard='$wildcard',empty=$empty,$op,start='$start',".print_r($filter,true).") query=".print_r($query,true).", total='$this->total'</p>\n";
|
2002-02-18 07:52:33 +01:00
|
|
|
echo "<br>criteria = "; _debug_array($criteria);
|
|
|
|
}
|
2005-04-15 00:09:40 +02:00
|
|
|
if ($only_keys === true) // only primary key
|
2005-04-08 20:30:32 +02:00
|
|
|
{
|
|
|
|
$cols = $this->db_key_cols;
|
|
|
|
}
|
2005-04-24 19:11:34 +02:00
|
|
|
else
|
2005-04-08 20:30:32 +02:00
|
|
|
{
|
2005-04-24 19:11:34 +02:00
|
|
|
$cols = array();
|
|
|
|
foreach(explode(',',str_replace(array('DISTINCT ','distinct '),'',$only_keys)) as $col)
|
2005-04-08 20:30:32 +02:00
|
|
|
{
|
2005-04-24 19:11:34 +02:00
|
|
|
if (!$col || $col == '*' || $col == $this->table_name.'.*') // all columns
|
|
|
|
{
|
|
|
|
$cols = array_merge($cols,$this->db_cols);
|
|
|
|
}
|
|
|
|
else // only the specified columns
|
|
|
|
{
|
2005-05-01 13:38:59 +02:00
|
|
|
if (stristr($col,'as')) $col = preg_replace('/^.*as +([a-z0-9_]+) *$/i','\\1',$col);
|
2005-04-24 19:11:34 +02:00
|
|
|
$cols[$col] = $col;
|
|
|
|
}
|
2005-04-08 20:30:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($extra_cols) // extra columns to report
|
2005-03-05 15:54:14 +01:00
|
|
|
{
|
2005-04-20 19:27:48 +02:00
|
|
|
foreach(is_array($extra_cols) ? $extra_cols : explode(',',$extra_cols) as $col)
|
2005-03-05 15:54:14 +01:00
|
|
|
{
|
2005-05-19 15:15:19 +02:00
|
|
|
if (stristr($col,'as ')) $col = preg_replace('/^.*as +([a-z0-9_]+) *$/i','\\1',$col);
|
2005-03-05 15:54:14 +01:00
|
|
|
$cols[$col] = $col;
|
|
|
|
}
|
|
|
|
}
|
2005-04-08 20:30:32 +02:00
|
|
|
$arr = array();
|
|
|
|
for ($n = 0; ($row = $this->db->row(true)); ++$n)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2005-04-08 20:30:32 +02:00
|
|
|
$data = array();
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach($cols as $db_col => $col)
|
|
|
|
{
|
2005-04-08 20:30:32 +02:00
|
|
|
$data[$col] = $row[$db_col];
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2005-04-08 20:30:32 +02:00
|
|
|
$arr[] = $this->db2data($data);
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
return $n ? $arr : False;
|
|
|
|
}
|
|
|
|
|
2005-02-27 22:31:10 +01:00
|
|
|
/**
|
|
|
|
* query rows for the nextmatch widget
|
|
|
|
*
|
|
|
|
* @param array $query with keys 'start', 'search', 'order', 'sort', 'col_filter'
|
|
|
|
* For other keys like 'filter', 'cat_id' you have to reimplement this method in a derived class.
|
|
|
|
* @param array &$rows returned rows/competitions
|
|
|
|
* @param array &$readonlys eg. to disable buttons based on acl, not use here, maybe in a derived class
|
2005-04-27 22:47:34 +02:00
|
|
|
* @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or
|
|
|
|
* "LEFT JOIN table2 ON (x=y)", Note: there's no quoting done on $join!
|
2005-05-19 15:15:19 +02:00
|
|
|
* @param boolean $need_full_no_count=false If true an unlimited query is run to determine the total number of rows, default false
|
|
|
|
* @return int total number of rows
|
2005-02-27 22:31:10 +01:00
|
|
|
*/
|
2005-05-19 15:15:19 +02:00
|
|
|
function get_rows($query,&$rows,&$readonlys,$join='',$need_full_no_count=false)
|
2005-02-27 22:31:10 +01:00
|
|
|
{
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4)
|
2005-02-27 22:31:10 +01:00
|
|
|
{
|
|
|
|
echo "<p>so_sql::get_rows(".print_r($query,true).",,)</p>\n";
|
|
|
|
}
|
|
|
|
$criteria = array();
|
|
|
|
if ($query['search'])
|
|
|
|
{
|
|
|
|
foreach($this->db_cols as $col) // we search all cols
|
|
|
|
{
|
|
|
|
$criteria[$col] = $query['search'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$rows = (array) $this->search($criteria,false,$query['order']?$query['order'].' '.$query['sort']:'',
|
2005-05-19 15:15:19 +02:00
|
|
|
'','%',false,'OR',(int)$query['start'],$query['col_filter'],$join,$need_full_no_count);
|
2005-02-27 22:31:10 +01:00
|
|
|
|
|
|
|
return $this->total;
|
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* Check if values for unique keys are unique
|
|
|
|
*
|
|
|
|
* @param array $data data-set to check, defaults to $this->data
|
|
|
|
* @return int 0: all keys are unique, 1: first key not unique, 2: ...
|
|
|
|
*/
|
|
|
|
function not_unique($data=null)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
if (!is_array($data))
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$data = $this->data;
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
|
|
|
$n = 1;
|
|
|
|
foreach($this->db_uni_cols as $db_col => $col)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2005-05-19 15:15:19 +02:00
|
|
|
if (list($other) = $this->search(array($db_col => $data[$col]),false,'','','',false,'AND',false,null,''))
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach($this->db_key_cols as $db_key_col => $key_col)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2005-04-08 20:30:32 +02:00
|
|
|
if ($data[$key_col] != $other[$key_col])
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2005-10-16 13:45:59 +02:00
|
|
|
if ((int) $this->debug >= 4)
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
|
|
|
echo "<p>not_unique in '$col' as for '$key_col': '${data[$key_col]}' != '${other[$key_col]}'</p>\n";
|
|
|
|
}
|
|
|
|
return $n; // different entry => $n not unique
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-03-27 18:21:58 +01:00
|
|
|
++$n;
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-05-10 16:44:34 +02:00
|
|
|
|
|
|
|
/**
|
2005-05-21 20:17:21 +02:00
|
|
|
* Query DB for a list / array with one colum as key and an other one(s) as value, eg. id => title pairs
|
2005-05-10 16:44:34 +02:00
|
|
|
*
|
|
|
|
* We do some caching as these kind of function is usualy called multiple times, eg. for option-lists.
|
|
|
|
*
|
2005-05-21 20:17:21 +02:00
|
|
|
* @param string $value_col array of column-names for the values of the array, can also be an expression aliased with AS,
|
|
|
|
* if more then one column given, an array with keys identical to the given ones is returned and not just the value of the column
|
|
|
|
* @param string $key_col='' column-name for the keys, default '' = same as (first) $value_col: returns a distinct list
|
2005-05-10 16:44:34 +02:00
|
|
|
* @param array $filter=array() to filter the entries
|
2005-05-21 20:17:21 +02:00
|
|
|
* @param string $order='' order, default '' = same as (first) $value_col
|
|
|
|
* @return array with key_col => value_col pairs or array if more then one value_col given (keys as in value_col)
|
2005-05-10 16:44:34 +02:00
|
|
|
*/
|
2005-05-19 15:15:19 +02:00
|
|
|
function query_list($value_col,$key_col='',$filter=array(),$order='')
|
2005-05-10 16:44:34 +02:00
|
|
|
{
|
|
|
|
static $cache = array();
|
|
|
|
|
2005-05-21 20:17:21 +02:00
|
|
|
$cache_key = serialize($value_col).'-'.$key_col.'-'.serialize($filter).'-'.$order;
|
2005-05-10 16:44:34 +02:00
|
|
|
|
|
|
|
if (isset($cache[$cache_key]))
|
|
|
|
{
|
|
|
|
return $cache[$cache_key];
|
|
|
|
}
|
2005-05-21 20:17:21 +02:00
|
|
|
if (!is_array($value_col)) $value_col = array($value_col);
|
|
|
|
|
|
|
|
$cols = array();
|
|
|
|
foreach(is_array($value_col) ? $value_col : array($value_col) as $key => $col)
|
2005-05-19 15:15:19 +02:00
|
|
|
{
|
2005-05-21 20:17:21 +02:00
|
|
|
$cols[$key] = preg_match('/AS ([a-z_0-9]+)$/i',$col,$matches) ? $matches[1] : $col;
|
|
|
|
}
|
|
|
|
if (!$order) $order = current($cols);
|
2005-05-19 15:15:19 +02:00
|
|
|
|
2005-05-21 20:17:21 +02:00
|
|
|
if (($search =& $this->search(array(),($key_col ? $key_col.',' : 'DISTINCT ').implode(',',$value_col),$order,'','',false,'AND',false,$filter)))
|
2005-05-10 16:44:34 +02:00
|
|
|
{
|
2005-05-19 15:15:19 +02:00
|
|
|
if (preg_match('/AS ([a-z_0-9]+)$/i',$key_col,$matches))
|
2005-05-10 16:44:34 +02:00
|
|
|
{
|
2005-05-19 15:15:19 +02:00
|
|
|
$key_col = $matches[1];
|
2005-05-10 16:44:34 +02:00
|
|
|
}
|
2005-05-21 20:17:21 +02:00
|
|
|
elseif (!$key_col)
|
|
|
|
{
|
|
|
|
$key_col = current($cols);
|
|
|
|
}
|
2005-05-10 16:44:34 +02:00
|
|
|
foreach($search as $row)
|
|
|
|
{
|
2005-05-21 20:17:21 +02:00
|
|
|
if (count($cols) > 1)
|
|
|
|
{
|
|
|
|
$data = array();
|
|
|
|
foreach($cols as $key => $col)
|
|
|
|
{
|
|
|
|
$data[$key] = $row[$col];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$data = $row[current($cols)];
|
|
|
|
}
|
|
|
|
$ret[$row[$key_col]] = $data;
|
2005-05-10 16:44:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $cache[$cache_key] =& $ret;
|
|
|
|
}
|
2005-02-13 14:03:36 +01:00
|
|
|
}
|