added some db-unspecific functions to deal with db-results-sets as associative arrays and form new querys from column/value arrays with automatical addslash/intval based on the column-type

This commit is contained in:
Ralf Becker 2003-10-05 10:42:07 +00:00
parent 89e281008c
commit 90faf20801

View File

@ -1,19 +1,19 @@
<?php <?php
/**************************************************************************\ /**************************************************************************\
* phpGroupWare API - MySQL database support * * phpGroupWare API - database support *
* Copyright (c) 1998-2000 NetUSE AG Boris Erdmann, Kristian Koehntopp * * Copyright (c) 1998-2000 NetUSE AG Boris Erdmann, Kristian Koehntopp *
* ------------------------------------------------------------------------ * * ------------------------------------------------------------------------ *
* This is not part of phpGroupWare, but is used by phpGroupWare. * * This is not part of phpGroupWare, but is used by phpGroupWare. *
* http://www.phpgroupware.org/ * * http://www.phpgroupware.org/ *
* ------------------------------------------------------------------------ * * ------------------------------------------------------------------------ *
* This program is free software; you can redistribute it and/or modify it * * This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published * * under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation; either version 2.1 of the License, or * * by the Free Software Foundation; either version 2.1 of the License, or *
* any later version. * * any later version. *
\**************************************************************************/ \**************************************************************************/
/* $Id$ */
/* $Id$ */
if (empty($GLOBALS['phpgw_info']['server']['db_type'])) if (empty($GLOBALS['phpgw_info']['server']['db_type']))
{ {
$GLOBALS['phpgw_info']['server']['db_type'] = 'mysql'; $GLOBALS['phpgw_info']['server']['db_type'] = 'mysql';
@ -21,84 +21,84 @@
include(PHPGW_API_INC.'/class.db_'.$GLOBALS['phpgw_info']['server']['db_type'].'.inc.php'); include(PHPGW_API_INC.'/class.db_'.$GLOBALS['phpgw_info']['server']['db_type'].'.inc.php');
/** /**
* Database abstraction library * Database abstraction library
* *
* This allows phpGroupWare to use multiple database backends * This allows phpGroupWare to use multiple database backends
* *
* @package phpgwapi * @package phpgwapi
* @subpackage db * @subpackage db
* @abstract * @abstract
* @author NetUSE AG Boris Erdmann, Kristian Koehntopp <br> hacked on by phpGW * @author NetUSE AG Boris Erdmann, Kristian Koehntopp <br> hacked on by phpGW
* @copyright &copy; 1998-2000 NetUSE AG Boris Erdmann, Kristian Koehntopp <br> 2003 FreeSoftware Foundation * @copyright &copy; 1998-2000 NetUSE AG Boris Erdmann, Kristian Koehntopp <br> 2003 FreeSoftware Foundation
* @license LGPL * @license LGPL
* @link http://www.sanisoft.com/phplib/manual/DB_sql.php * @link http://www.sanisoft.com/phplib/manual/DB_sql.php
*/ */
class db_ class db_
{ {
/** /**
* @var string $Host database host to connect to * @var string $Host database host to connect to
*/ */
var $Host = ''; var $Host = '';
/** /**
* @var string $Database name of database to use * @var string $Database name of database to use
*/ */
var $Database = ''; var $Database = '';
/** /**
* @var string $User name of database user * @var string $User name of database user
*/ */
var $User = ''; var $User = '';
/** /**
* @var string $Password password for database user * @var string $Password password for database user
*/ */
var $Password = ''; var $Password = '';
/** /**
* @var bool $auto_stripslashes automatically remove slashes when returning field values - default False * @var bool $auto_stripslashes automatically remove slashes when returning field values - default False
*/ */
var $auto_stripslashes = False; var $auto_stripslashes = False;
/** /**
* @var int $Auto_Free automatically free results - 0 no, 1 yes * @var int $Auto_Free automatically free results - 0 no, 1 yes
*/ */
var $Auto_Free = 0; var $Auto_Free = 0;
/** /**
* @var int $Debug enable debuging - 0 no, 1 yes * @var int $Debug enable debuging - 0 no, 1 yes
*/ */
var $Debug = 0; var $Debug = 0;
/** /**
* @var string $Halt_On_Error "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning) * @var string $Halt_On_Error "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
*/ */
var $Halt_On_Error = 'yes'; var $Halt_On_Error = 'yes';
/** /**
* @var string $Seq_Table table for storing sequences ???? * @var string $Seq_Table table for storing sequences ????
*/ */
var $Seq_Table = 'db_sequence'; var $Seq_Table = 'db_sequence';
/** /**
* @var array $Record current record * @var array $Record current record
*/ */
var $Record = array(); var $Record = array();
/** /**
* @var int row number for current record * @var int row number for current record
*/ */
var $Row; var $Row;
/** /**
* @var int $Errno internal rdms error number for last error * @var int $Errno internal rdms error number for last error
*/ */
var $Errno = 0; var $Errno = 0;
/** /**
* @var string descriptive text from last error * @var string descriptive text from last error
*/ */
var $Error = ''; var $Error = '';
//i am not documenting private vars - skwashd :) //i am not documenting private vars - skwashd :)
@ -106,54 +106,54 @@
var $soap = False; var $soap = False;
/** /**
* @param string $query query to be executed (optional) * @param string $query query to be executed (optional)
*/ */
function db_($query = '') function db_($query = '')
{ {
$this->query($query); $this->query($query);
} }
/** /**
* @return int current connection id * @return int current connection id
*/ */
function link_id() function link_id()
{ {
return $this->Link_ID; return $this->Link_ID;
} }
/** /**
* @return int id of current query * @return int id of current query
*/ */
function query_id() function query_id()
{ {
return $this->Query_ID; return $this->Query_ID;
} }
/** /**
* Open a connection to a database * Open a connection to a database
* *
* @param string $Database name of database to use (optional) * @param string $Database name of database to use (optional)
* @param string $Host database host to connect to (optional) * @param string $Host database host to connect to (optional)
* @param string $User name of database user (optional) * @param string $User name of database user (optional)
* @var string $Password password for database user (optional) * @var string $Password password for database user (optional)
*/ */
function connect($Database = '', $Host = '', $User = '', $Password = '') function connect($Database = '', $Host = '', $User = '', $Password = '')
{} {}
/** /**
* Close a connection to a database - only needed for persistent connections * Close a connection to a database - only needed for persistent connections
*/ */
function disconnect() function disconnect()
{} {}
/** /**
* Escape strings before sending them to the database * Escape strings before sending them to the database
* *
* @param string $str the string to be escaped * @param string $str the string to be escaped
* @return string escaped sting * @return string escaped sting
*/ */
function db_addslashes($str) function db_addslashes($str)
{ {
if (!isset($str) || $str == '') if (!isset($str) || $str == '')
{ {
@ -164,33 +164,33 @@
} }
/** /**
* Convert a unix timestamp to a rdms specific timestamp * Convert a unix timestamp to a rdms specific timestamp
* *
* @param int unix timestamp * @param int unix timestamp
* @return string rdms specific timestamp * @return string rdms specific timestamp
*/ */
function to_timestamp($epoch) function to_timestamp($epoch)
{} {}
/** /**
* Convert a rdms specific timestamp to a unix timestamp * Convert a rdms specific timestamp to a unix timestamp
* *
* @param string rdms specific timestamp * @param string rdms specific timestamp
* @return int unix timestamp * @return int unix timestamp
*/ */
function from_timestamp($timestamp) function from_timestamp($timestamp)
{} {}
/** /**
* @deprecated * @deprecated
* @see limit_query() * @see limit_query()
*/ */
function limit($start) function limit($start)
{} {}
/** /**
* Discard the current query result * Discard the current query result
*/ */
function free() function free()
{ {
@mysql_free_result($this->Query_ID); @mysql_free_result($this->Query_ID);
@ -198,153 +198,153 @@
} }
/** /**
* Execute a query * Execute a query
* *
* @param string $Query_String the query to be executed * @param string $Query_String the query to be executed
* @param mixed $line the line method was called from - use __LINE__ * @param mixed $line the line method was called from - use __LINE__
* @param string $file the file method was called from - use __FILE__ * @param string $file the file method was called from - use __FILE__
* @return int current query id if sucesful and null if fails * @return int current query id if sucesful and null if fails
*/ */
function query($Query_String, $line = '', $file = '') function query($Query_String, $line = '', $file = '')
{} {}
/** /**
* Execute a query with limited result set * Execute a query with limited result set
* *
* @param string $Query_String the query to be executed * @param string $Query_String the query to be executed
* @param int $offset row to start from * @param int $offset row to start from
* @param mixed $line the line method was called from - use __LINE__ * @param mixed $line the line method was called from - use __LINE__
* @param string $file the file method was called from - use __FILE__ * @param string $file the file method was called from - use __FILE__
* @param int $num_rows number of rows to return (optional), if unset will use $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'] * @param int $num_rows number of rows to return (optional), if unset will use $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs']
* @return int current query id if sucesful and null if fails * @return int current query id if sucesful and null if fails
*/ */
function limit_query($Query_String, $offset, $line = '', $file = '', $num_rows = '') function limit_query($Query_String, $offset, $line = '', $file = '', $num_rows = '')
{} {}
/** /**
* Move to the next row in the results set * Move to the next row in the results set
* *
* @return bool was another row found? * @return bool was another row found?
*/ */
function next_record() function next_record()
{} {}
/** /**
* Move to position in result set * Move to position in result set
* *
* @param int $pos required row (optional), default first row * @param int $pos required row (optional), default first row
* @return int 1 if sucessful or 0 if not found * @return int 1 if sucessful or 0 if not found
*/ */
function seek($pos = 0) function seek($pos = 0)
{} {}
/** /**
* Begin Transaction * Begin Transaction
* *
* @return int current transaction id * @return int current transaction id
*/ */
function transaction_begin() function transaction_begin()
{ {
return True; return True;
} }
/** /**
* Complete the transaction * Complete the transaction
* *
* @return bool True if sucessful, False if fails * @return bool True if sucessful, False if fails
*/ */
function transaction_commit() function transaction_commit()
{ {
return True; return True;
} }
/** /**
* Rollback the current transaction * Rollback the current transaction
* *
* @return bool True if sucessful, False if fails * @return bool True if sucessful, False if fails
*/ */
function transaction_abort() function transaction_abort()
{ {
return True; return True;
} }
/** /**
* Find the primary key of the last insertion on the current db connection * Find the primary key of the last insertion on the current db connection
* *
* @param string $table name of table the insert was performed on * @param string $table name of table the insert was performed on
* @param string $field the autoincrement primary key of the table * @param string $field the autoincrement primary key of the table
* @return int the id, -1 if fails * @return int the id, -1 if fails
*/ */
function get_last_insert_id($table, $field) function get_last_insert_id($table, $field)
{} {}
/** /**
* Lock a table * Lock a table
* *
* @param string $table name of table to lock * @param string $table name of table to lock
* @param string $mode type of lock required (optional), default write * @param string $mode type of lock required (optional), default write
* @return bool True if sucessful, False if fails * @return bool True if sucessful, False if fails
*/ */
function lock($table, $mode='write') function lock($table, $mode='write')
{} {}
/** /**
* Unlock a table * Unlock a table
* *
* @return bool True if sucessful, False if fails * @return bool True if sucessful, False if fails
*/ */
function unlock() function unlock()
{} {}
/** /**
* Get the number of rows affected by last update * Get the number of rows affected by last update
* *
* @return int number of rows * @return int number of rows
*/ */
function affected_rows() function affected_rows()
{} {}
/** /**
* Number of rows in current result set * Number of rows in current result set
* *
* @return int number of rows * @return int number of rows
*/ */
function num_rows() function num_rows()
{} {}
/** /**
* Number of fields in current row * Number of fields in current row
* *
* @return int number of fields * @return int number of fields
*/ */
function num_fields() function num_fields()
{} {}
/** /**
* short hand for @see num_rows() * short hand for @see num_rows()
*/ */
function nf() function nf()
{ {
return $this->num_rows(); return $this->num_rows();
} }
/** /**
* short hand for print @see num_rows * short hand for print @see num_rows
*/ */
function np() function np()
{ {
print $this->num_rows(); print $this->num_rows();
} }
/** /**
* Return the value of a filed * Return the value of a column
* *
* @param string $String name of field * @param string/integer $Name name of field or positional index starting from 0
* @param bool $strip_slashes string escape chars from field(optional), default false * @param bool $strip_slashes string escape chars from field(optional), default false
* @return string the field value * @return string the field value
*/ */
function f($Name, $strip_slashes = False) function f($Name, $strip_slashes = False)
{ {
if ($strip_slashes || ($this->auto_stripslashes && ! $strip_slashes)) if ($strip_slashes || ($this->auto_stripslashes && ! $strip_slashes))
@ -358,32 +358,55 @@
} }
/** /**
* Print the value of a field * Print the value of a field
* *
* @param string $Name name of field to print * @param string $Name name of field to print
* @param bool $strip_slashes string escape chars from field(optional), default false * @param bool $strip_slashes string escape chars from field(optional), default false
*/ */
function p($Name, $strip_slashes = True) function p($Name, $strip_slashes = True)
{ {
print $this->f($Name, $strip_slashes); print $this->f($Name, $strip_slashes);
} }
/** /**
* Get the id for the next sequence - not implemented! * Returns a query-result-row as an associative array (no numerical keys !!!)
* *
* @param string $seq_name name of the sequence * @param bool $do_next_record should next_record() be called or not (default not)
* @return int sequence id * @return array/bool the associative array or False if no (more) result-row is availible
*/ */
function row($do_next_record=False)
{
if ($do_next_record && !$this->next_record() || !is_array($this->Record))
{
return False;
}
$result = array();
foreach($this->Record as $column => $value)
{
if (!is_numeric($column))
{
$result[$column] = $value;
}
}
return $result;
}
/**
* Get the id for the next sequence - not implemented!
*
* @param string $seq_name name of the sequence
* @return int sequence id
*/
function nextid($seq_name) function nextid($seq_name)
{} {}
/** /**
* Get description of a table * Get description of a table
* *
* @param string $table name of table to describe * @param string $table name of table to describe
* @param bool $full optional, default False summary information, True full information * @param bool $full optional, default False summary information, True full information
* @return array table meta data * @return array table meta data
*/ */
function metadata($table='',$full=false) function metadata($table='',$full=false)
{ {
/* /*
@ -414,41 +437,119 @@
} }
/** /**
* Error handler * Error handler
* *
* @param string $msg error message * @param string $msg error message
* @param int $line line of calling method/function (optional) * @param int $line line of calling method/function (optional)
* @param string $file file of calling method/function (optional) * @param string $file file of calling method/function (optional)
*/ */
function halt($msg, $line = '', $file = '') function halt($msg, $line = '', $file = '')
{} {}
/** /**
* Get a list of table names in the current database * Get a list of table names in the current database
* *
* @return array list of the tables * @return array list of the tables
*/ */
function table_names() function table_names()
{ {
} }
/** /**
* Return a list of indexes in current database * Return a list of indexes in current database
* *
* @return array list of indexes * @return array list of indexes
*/ */
function index_names() function index_names()
{ {
return array(); return array();
} }
/** /**
* Create a new database * Create a new database
* *
* @param string $adminname name of database administrator user (optional) * @param string $adminname name of database administrator user (optional)
* @param string $adminpasswd password for the database administrator user (optional) * @param string $adminpasswd password for the database administrator user (optional)
*/ */
function create_database($adminname = '', $adminpasswd = '') function create_database($adminname = '', $adminpasswd = '')
{} {}
/**
* Implodes an array of column-value pairs for the use in sql-querys.
* All data is either run through addslashes() or intval().
*
* @author RalfBecker<at>outdoor-training.de
*
* @param string $glue in most cases this will be either ',' or ' AND ', depending you your query
* @param array $array column-value pairs
* @param boolean $use_key should a "$key=" prefix each value, typicaly set to False for insert querys
* @param array/boolean $only if set to an array only colums which are set (as data !!!) are written
* typicaly used to form a WHERE-clause from the primary keys
* @param array/boolean $column_definitions this can be set to the column-definitions-array
* of your table ($tables_baseline[$table]['fd'] of the setup/tables_current.inc.php file).
* If its set, the column-type-data determinates if intval() or addslashes is used.
*/
function column_data_implode($glue,$array,$use_key=True,$only=False,$column_definitions=False)
{
if (!$column_definitions)
{
$column_definitions = $this->column_definitions;
}
$pairs = array();
foreach($array as $key => $data)
{
if (!$only || in_array($key,$only))
{
$column_type = is_array($column_definitions) ? @$colum_definitions[$key]['type'] : False;
$values[] = ($use_key ? $key.'=' : '').
($column_type == 'int' || $colum_type == 'auto' ?
intval($data) : "'".$this->db_addslashes($data)."'");
}
}
return implode($glue,$values);
}
/**
* Sets the default column-definitions for use with column_data_implode()
*
* @author RalfBecker<at>outdoor-training.de
*
* @param array/boolean $column_definitions this can be set to the column-definitions-array
* of your table ($tables_baseline[$table]['fd'] of the setup/tables_current.inc.php file).
* If its set, the column-type-data determinates if intval() or addslashes is used.
*/
function set_column_definitions($column_definitions=False)
{
$this->column_definitions=$column_definitions;
}
/**
* reads the table-definitions from the app's setup/tables_current.inc.php file
*
* @author RalfBecker<at>outdoor-training.de
*
* @param string $app name of the app
* @param bool/string $table if set return only defintions of that table, else return all defintions
* @return the table-defintions or False if file not found
*/
function get_table_definitions($app,$table=False)
{
if (!isset($this->table_definitions[$app]))
{
$tables_current = PHPGW_INCLUDE_ROOT . "/$app/setup/tables_current.inc.php";
if (!@file_exists($tables_current))
{
return $this->table_definitions[$app] = False;
}
include($tables_current);
$this->table_definitions[$app] = $phpgw_baseline;
}
if ($table && (!$this->table_definitions[$app] || !isset($this->table_definitions[$app][$table])))
{
return False;
}
return $table ? $this->table_definitions[$app][$table] : $this->table_definitions[$app];
}
} }
?> ?>