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,6 +1,6 @@
<?php
/**************************************************************************\
* phpGroupWare API - MySQL database support *
* phpGroupWare API - database support *
* Copyright (c) 1998-2000 NetUSE AG Boris Erdmann, Kristian Koehntopp *
* ------------------------------------------------------------------------ *
* This is not part of phpGroupWare, but is used by phpGroupWare. *
@ -339,9 +339,9 @@
}
/**
* 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
* @return string the field value
*/
@ -368,6 +368,29 @@
print $this->f($Name, $strip_slashes);
}
/**
* Returns a query-result-row as an associative array (no numerical keys !!!)
*
* @param bool $do_next_record should next_record() be called or not (default not)
* @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!
*
@ -450,5 +473,83 @@
*/
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];
}
}
?>