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
|
|
|
|
* @license GPL
|
|
|
|
*/
|
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 $db_key_cols array of all primary-key-columns in form dbName => internalName
|
|
|
|
* the save function does NOT touch any other cols in the table!!!
|
|
|
|
*/
|
|
|
|
var $db_key_cols = array();
|
|
|
|
/**
|
|
|
|
* @var array $db_data_cols array of all data-cols
|
|
|
|
*/
|
|
|
|
var $db_data_cols = array();
|
|
|
|
/**
|
|
|
|
* @var array $non_db_cols all cols in data which are not (direct)in the db, for data_merge
|
|
|
|
*/
|
|
|
|
var $non_db_cols = array();
|
|
|
|
/**
|
|
|
|
* @var int $debug turns on debug-messages
|
|
|
|
*/
|
|
|
|
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';
|
2002-02-18 07:52:33 +01:00
|
|
|
var $public_functions = array(
|
|
|
|
'init' => True,
|
|
|
|
'data_merge' => True,
|
|
|
|
'read' => True,
|
|
|
|
'save' => True,
|
|
|
|
'delete' => True,
|
|
|
|
'search' => True,
|
|
|
|
);
|
2005-02-13 14:03:36 +01:00
|
|
|
var $db;
|
2002-02-18 07:52:33 +01:00
|
|
|
var $db_uni_cols = array();
|
|
|
|
var $db_cols; // = $db_key_cols + $db_data_cols
|
|
|
|
var $data; // holds the content of all db_cols
|
|
|
|
|
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
|
|
|
|
*/
|
2002-02-18 07:52:33 +01:00
|
|
|
function so_sql($app='',$table='')
|
|
|
|
{
|
|
|
|
$this->db = $GLOBALS['phpgw']->db;
|
|
|
|
$this->db_cols = $this->db_key_cols + $this->db_data_cols;
|
|
|
|
|
|
|
|
if ($app && $table)
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->setup_table($app,$table);
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->init();
|
|
|
|
|
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
echo "<p>so_sql('$app','$table')</p>\n";
|
|
|
|
_debug_array($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-13 14:03:36 +01:00
|
|
|
/**
|
|
|
|
* reads table-definition from <app>/setup/tables_current.inc.php
|
|
|
|
*
|
|
|
|
* Does NOT set a different internal-data-name. If you want this, you have to do so
|
|
|
|
* in a derifed class !!!
|
|
|
|
*/
|
2002-02-18 07:52:33 +01:00
|
|
|
function setup_table($app,$table)
|
|
|
|
{
|
|
|
|
include(PHPGW_SERVER_ROOT . "/$app/setup/tables_current.inc.php");
|
|
|
|
|
|
|
|
if (!isset($phpgw_baseline[$table]))
|
|
|
|
{
|
|
|
|
echo "<p>Can't find table-definitions for App. '$app', Table '$table' !!!</p>\n";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
$this->table_name = $table;
|
|
|
|
|
|
|
|
$table_def = $phpgw_baseline[$table];
|
|
|
|
$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
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
* @return array/boolean data if row could be retrived else False
|
2002-02-18 07:52:33 +01:00
|
|
|
*/
|
2003-03-27 18:21:58 +01:00
|
|
|
function read($keys)
|
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();
|
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
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$query .= ($query ? ' AND ':'')."$db_col='".addslashes($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
|
|
|
{
|
|
|
|
foreach($this->db_data_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
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$query .= ($query ? ' AND ':'')."$db_col='".addslashes($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;
|
|
|
|
}
|
|
|
|
$this->db->query($sql = "SELECT * FROM $this->table_name WHERE $query",__LINE__,__FILE__);
|
|
|
|
|
|
|
|
if ($this->debug)
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
|
|
|
echo "<p>read(): sql = '$sql': ";
|
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
if (!$this->db->next_record())
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
if ($this->debug) echo "nothing found !!!</p>\n";
|
|
|
|
|
|
|
|
$this->db2data();
|
|
|
|
|
|
|
|
return False;
|
|
|
|
}
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach ($this->db_cols as $db_col => $col)
|
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->data[$col] = $this->db->f($db_col);
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db2data();
|
|
|
|
|
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
if (!$this->autoinc_id) // no autoincrement id, so we need to find out with read if key already in db
|
|
|
|
{
|
|
|
|
$data = $this->data;
|
|
|
|
$new = !$this->read($data);
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
else
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$new = !$this->data[$this->db_key_cols[$this->autoinc_id]]; // autoincrement idx is 0 => new
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->data2db();
|
|
|
|
|
|
|
|
if ($new) // prepare an insert
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
$cols .= ($cols ? ',' : '') . $db_col;
|
|
|
|
$vals .= ($vals ? ',' : '') . ($this->data[$col] == '' ?
|
|
|
|
$this->empty_on_write : "'".addslashes($this->data[$col])."'");
|
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
$this->db->query($sql = "INSERT INTO $this->table_name ($cols) VALUES ($vals)",__LINE__,__FILE__);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$vals .= ($vals ? ',':'') . "$db_col=".($this->data[$col] == '' ?
|
|
|
|
$this->empty_on_write : "'".addslashes($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)
|
|
|
|
{
|
2003-11-03 17:19:48 +01:00
|
|
|
$keys .= ($keys ? ' AND ':'') . "$db_col='".addslashes($this->data[$col])."'";
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db->query($sql = "UPDATE $this->table_name SET $vals WHERE $keys",__LINE__,__FILE__);
|
|
|
|
}
|
|
|
|
if ($this->debug)
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
|
|
|
echo "<p>save(): sql = '$sql'</p>\n";
|
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db2data();
|
|
|
|
|
|
|
|
return $this->db->errno;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return affected rows, should be 1 if ok, 0 if an error
|
2002-02-18 07:52:33 +01:00
|
|
|
*/
|
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)
|
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$query .= ($query ? ' AND ' : '') . $db_col . "='" . addslashes($data[$col]) . "'";
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$this->db->query($sql = "DELETE FROM $this->table_name WHERE $query",__LINE__,__FILE__);
|
|
|
|
|
|
|
|
if ($this->debug)
|
2003-03-27 18:21:58 +01:00
|
|
|
{
|
|
|
|
echo "<p>delete(): sql = '$sql'</p>\n";
|
|
|
|
}
|
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 (!)
|
|
|
|
* @param boolean $only_keys True returns only keys, False returns all cols
|
|
|
|
* @param string $order_by fieldnames + {ASC|DESC} separated by colons ','
|
|
|
|
* @param string $extra_cols string to be added to the SELECT, eg. (count(*) as num)
|
|
|
|
* @param string $wildcard appended befor and after each criteria
|
|
|
|
* @param boolean $empty False=empty criteria are ignored in query, True=empty have to be empty in row
|
|
|
|
* @param string $op defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
|
|
|
|
* @return array of matching rows (the row is an array of the cols) or False
|
|
|
|
*/
|
2003-04-16 16:32:15 +02:00
|
|
|
function search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND')
|
2002-02-18 07:52:33 +01:00
|
|
|
{
|
2003-04-16 16:32:15 +02:00
|
|
|
if (!is_array($criteria))
|
|
|
|
{
|
2003-11-03 17:19:48 +01:00
|
|
|
$query = $criteria ? ' WHERE '.$criteria : '';
|
2003-04-16 16:32:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$criteria = $this->data2db($criteria);
|
|
|
|
foreach($this->db_cols as $db_col => $col)
|
|
|
|
{ //echo "testing col='$col', criteria[$col]='".$criteria[$col]."'<br>";
|
2003-11-03 17:19:48 +01:00
|
|
|
if (array_key_exists($col,$criteria) && ($empty || $criteria[$col] != ''))
|
2003-04-16 16:32:15 +02:00
|
|
|
{
|
2003-11-03 17:19:48 +01:00
|
|
|
if ($criteria[$col] === NULL)
|
|
|
|
{
|
|
|
|
$query .= ($query ? " $op " : ' WHERE ') . "$db_col IS NULL";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$query .= ($query ? " $op " : ' WHERE ') . $db_col .
|
2003-04-16 16:32:15 +02:00
|
|
|
($wildcard || strstr($criteria[$col],'*') || strstr($criteria[$col],'?') ?
|
|
|
|
" LIKE '$wildcard".strtr(str_replace('_','\\_',addslashes($criteria[$col])),'*?','%_')."$wildcard'" :
|
|
|
|
"='".addslashes($criteria[$col])."'");
|
2003-11-03 17:19:48 +01:00
|
|
|
}
|
2003-04-16 16:32:15 +02:00
|
|
|
}
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
}
|
|
|
|
$this->db->query($sql = 'SELECT '.($only_keys ? implode(',',$this->db_key_cols) : '*').
|
|
|
|
($extra_cols != '' ? ",$extra_cols" : '')." FROM $this->table_name $query" .
|
|
|
|
($order_by != '' ? " ORDER BY $order_by" : ''),__LINE__,__FILE__);
|
|
|
|
|
|
|
|
if ($this->debug)
|
|
|
|
{
|
2003-03-27 18:21:58 +01:00
|
|
|
echo "<p>search(only_keys=$only_keys,order_by='$order_by',wildcard='$wildcard',empty=$empty)<br>sql = '$sql'</p>\n";
|
2002-02-18 07:52:33 +01:00
|
|
|
echo "<br>criteria = "; _debug_array($criteria);
|
|
|
|
}
|
|
|
|
$arr = array();
|
|
|
|
$cols = $only_keys ? $this->db_key_cols : $this->db_cols;
|
|
|
|
for ($n = 0; $this->db->next_record(); ++$n)
|
|
|
|
{
|
|
|
|
$row = array();
|
2003-03-27 18:21:58 +01:00
|
|
|
foreach($cols as $db_col => $col)
|
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
$row[$col] = $this->db->f($db_col);
|
2003-03-27 18:21:58 +01:00
|
|
|
}
|
2002-02-18 07:52:33 +01:00
|
|
|
$arr[] = $this->db2data($row);
|
|
|
|
}
|
|
|
|
return $n ? $arr : False;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (list($other) = $this->search(array($db_col => $data[$col])))
|
|
|
|
{
|
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
|
|
|
{
|
2003-03-27 18:21:58 +01:00
|
|
|
if ($data[$key_col] != $other[$key_col])
|
|
|
|
{
|
2002-02-18 07:52:33 +01:00
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
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-02-13 14:03:36 +01:00
|
|
|
}
|