First commit
This commit is contained in:
commit
c6e2478c40
13918 changed files with 2303184 additions and 0 deletions
755
sites/all/modules/civicrm/packages/DB/Table/Base.php
Normal file
755
sites/all/modules/civicrm/packages/DB/Table/Base.php
Normal file
|
@ -0,0 +1,755 @@
|
|||
<?php
|
||||
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* DB_Table_Base Base class for DB_Table and DB_Table_Database
|
||||
*
|
||||
* This utility class contains properties and methods that are common
|
||||
* to DB_Table and DB_Table database. These are all related to one of:
|
||||
* - DB/MDB2 connection object [ $db and $backend properties ]
|
||||
* - Error handling [ throwError() method, $error and $_primary_subclass ]
|
||||
* - SELECT queries [ select*() methods, $sql & $fetchmode* properties]
|
||||
* - buildSQL() and quote() SQL utilities
|
||||
* - _swapModes() method
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE:
|
||||
*
|
||||
* Copyright (c) 1997-2007, Paul M. Jones <pmjones@php.net>
|
||||
* David C. Morse <morse@php.net>
|
||||
* Mark Wiesemann <wiesemann@php.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * The names of the authors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Paul M. Jones <pmjones@php.net>
|
||||
* @author David C. Morse <morse@php.net>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||
* @version CVS: $Id: Base.php,v 1.4 2007/12/13 16:52:14 wiesemann Exp $
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
require_once 'PEAR.php';
|
||||
|
||||
// {{{ DB_Table_Base
|
||||
|
||||
/**
|
||||
* Base class for DB_Table and DB_Table_Database
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Paul M. Jones <pmjones@php.net>
|
||||
* @author David C. Morse <morse@php.net>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @version Release: 1.5.6
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
class DB_Table_Base
|
||||
{
|
||||
|
||||
// {{{ properties
|
||||
|
||||
/**
|
||||
* The PEAR DB/MDB2 object that connects to the database.
|
||||
*
|
||||
* @var object
|
||||
* @access public
|
||||
*/
|
||||
var $db = null;
|
||||
|
||||
/**
|
||||
* The backend type, which must be 'db' or 'mdb2'
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $backend = null;
|
||||
|
||||
/**
|
||||
* If there is an error on instantiation, this captures that error.
|
||||
*
|
||||
* This property is used only for errors encountered in the constructor
|
||||
* at instantiation time. To check if there was an instantiation error...
|
||||
*
|
||||
* <code>
|
||||
* $obj = new DB_Table_*();
|
||||
* if ($obj->error) {
|
||||
* // ... error handling code here ...
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @var object PEAR_Error
|
||||
* @access public
|
||||
*/
|
||||
var $error = null;
|
||||
|
||||
/**
|
||||
* Baseline SELECT maps for buildSQL() and select*() methods.
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $sql = array();
|
||||
|
||||
/**
|
||||
* Format of rows in sets returned by the select() method
|
||||
*
|
||||
* This should be one of the DB/MDB2_FETCHMODE_* constant values, such as
|
||||
* MDB2_FETCHMODE_ASSOC, MDB2_FETCHMODE_ORDERED, or MDB2_FETCHMODE_OBJECT.
|
||||
* It determines whether select() returns represents individual rows as
|
||||
* associative arrays with column name keys, ordered/sequential arrays,
|
||||
* or objects with column names mapped to properties. Use corresponding
|
||||
* DB_FETCHMODE_* constants for use with the DB backend. It has no effect
|
||||
* upon the return value of selectResult().
|
||||
*
|
||||
* If a 'fetchmode' element is set for a specific query array, the query
|
||||
* fetchmode will override this DB_Table or DB_Table_Database property.
|
||||
* If no value is set for the query or the DB_Table_Base object, the value
|
||||
* or default set in the underlying DB/MDB2 object will be used.
|
||||
*
|
||||
* @var int
|
||||
* @access public
|
||||
*/
|
||||
var $fetchmode = null;
|
||||
|
||||
/**
|
||||
* Class of objects to use for rows returned as objects by select()
|
||||
*
|
||||
* When fetchmode is DB/MDB2_FETCHMODE_OBJECT, use this class for each
|
||||
* returned row in rsults of select(). May be overridden by value of
|
||||
* 'fetchmode_object_class'. If no class name is set in the query or
|
||||
* the DB_Table_Base, defaults to that set in the DB/MDB2 object, or
|
||||
* to default of StdObject.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $fetchmode_object_class = null;
|
||||
|
||||
/**
|
||||
* Upper case name of primary subclass, 'DB_TABLE' or 'DB_TABLE_DATABASE'
|
||||
*
|
||||
* This should be set in the constructor of the child class, and is
|
||||
* used in the DB_Table_Base::throwError() method to determine the
|
||||
* location of the relevant error codes and messages. Error codes and
|
||||
* error code messages are defined in class $this->_primary_subclass.
|
||||
* Messages are stored in $GLOBALS['_' . $this->_primary_subclass]['error']
|
||||
*
|
||||
* @var string
|
||||
* @access private
|
||||
*/
|
||||
var $_primary_subclass = null;
|
||||
|
||||
// }}}
|
||||
// {{{ Methods
|
||||
|
||||
/**
|
||||
* Specialized version of throwError() modeled on PEAR_Error.
|
||||
*
|
||||
* Throws a PEAR_Error with an error message based on an error code
|
||||
* and corresponding error message defined in $this->_primary_subclass
|
||||
*
|
||||
* @param string $code An error code constant
|
||||
* @param string $extra Extra text for the error (in addition to the
|
||||
* regular error message).
|
||||
* @return object PEAR_Error
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
function &throwError($code, $extra = null)
|
||||
{
|
||||
// get the error message text based on the error code
|
||||
$index = '_' . $this->_primary_subclass;
|
||||
$text = $this->_primary_subclass . " Error - \n"
|
||||
. $GLOBALS[$index]['error'][$code];
|
||||
|
||||
// add any additional error text
|
||||
if ($extra) {
|
||||
$text .= ' ' . $extra;
|
||||
}
|
||||
|
||||
// done!
|
||||
$error = PEAR::throwError($text, $code);
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrites one or more error messages, e.g., to internationalize them.
|
||||
*
|
||||
* May be used to change messages stored in global array $GLOBALS[$class_key]
|
||||
* @param mixed $code If string, the error message with code $code will be
|
||||
* overwritten by $message. If array, each key is a code
|
||||
* and each value is a new message.
|
||||
*
|
||||
* @param string $message Only used if $key is not an array.
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function setErrorMessage($code, $message = null) {
|
||||
$index = '_' . $this->_primary_subclass;
|
||||
if (is_array($code)) {
|
||||
foreach ($code as $single_code => $single_message) {
|
||||
$GLOBALS[$index]['error'][$single_code] = $single_message;
|
||||
}
|
||||
} else {
|
||||
$GLOBALS[$index]['error'][$code] = $message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns SQL SELECT string constructed from sql query array
|
||||
*
|
||||
* @param mixed $query SELECT query array, or key string of $this->sql
|
||||
* @param string $filter SQL snippet to AND with default WHERE clause
|
||||
* @param string $order SQL snippet to override default ORDER BY clause
|
||||
* @param int $start The row number from which to start result set
|
||||
* @param int $count The number of rows to list in the result set.
|
||||
*
|
||||
* @return string SQL SELECT command string (or PEAR_Error on failure)
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function buildSQL($query, $filter = null, $order = null,
|
||||
$start = null, $count = null)
|
||||
{
|
||||
|
||||
// Is $query a query array or a key of $this->sql ?
|
||||
if (!is_array($query)) {
|
||||
if (is_string($query)) {
|
||||
if (isset($this->sql[$query])) {
|
||||
$query = $this->sql[$query];
|
||||
} else {
|
||||
return $this->throwError(
|
||||
constant($this->_primary_subclass . '_ERR_SQL_UNDEF'),
|
||||
$query);
|
||||
}
|
||||
} else {
|
||||
return $this->throwError(
|
||||
constant($this->_primary_subclass . '_ERR_SQL_NOT_STRING'));
|
||||
}
|
||||
}
|
||||
|
||||
// Construct SQL command from parts
|
||||
$s = array();
|
||||
if (isset($query['select'])) {
|
||||
$s[] = 'SELECT ' . $query['select'];
|
||||
} else {
|
||||
$s[] = 'SELECT *';
|
||||
}
|
||||
if (isset($query['from'])) {
|
||||
$s[] = 'FROM ' . $query['from'];
|
||||
} elseif ($this->_primary_subclass == 'DB_TABLE') {
|
||||
$s[] = 'FROM ' . $this->table;
|
||||
}
|
||||
if (isset($query['join'])) {
|
||||
$s[] = $query['join'];
|
||||
}
|
||||
if (isset($query['where'])) {
|
||||
if ($filter) {
|
||||
$s[] = 'WHERE ( ' . $query['where'] . ' )';
|
||||
$s[] = ' AND ( '. $filter . ' )';
|
||||
} else {
|
||||
$s[] = 'WHERE ' . $query['where'];
|
||||
}
|
||||
} elseif ($filter) {
|
||||
$s[] = 'WHERE ' . $filter;
|
||||
}
|
||||
if (isset($query['group'])) {
|
||||
$s[] = 'GROUP BY ' . $query['group'];
|
||||
}
|
||||
if (isset($query['having'])) {
|
||||
$s[] = 'HAVING '. $query['having'];
|
||||
}
|
||||
// If $order parameter is set, override 'order' element
|
||||
if (!is_null($order)) {
|
||||
$s[] = 'ORDER BY '. $order;
|
||||
} elseif (isset($query['order'])) {
|
||||
$s[] = 'ORDER BY ' . $query['order'];
|
||||
}
|
||||
$cmd = implode("\n", $s);
|
||||
|
||||
// add LIMIT if requested
|
||||
if (!is_null($start) && !is_null($count)) {
|
||||
$db =& $this->db;
|
||||
if ($this->backend == 'mdb2') {
|
||||
$db->setLimit($count, $start);
|
||||
} else {
|
||||
$cmd = $db->modifyLimitQuery(
|
||||
$cmd, $start, $count);
|
||||
}
|
||||
}
|
||||
|
||||
// Return command string
|
||||
return $cmd;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Selects rows using one of the DB/MDB2 get*() methods.
|
||||
*
|
||||
* @param string $query SQL SELECT query array, or a key of the
|
||||
* $this->sql property array.
|
||||
* @param string $filter SQL snippet to AND with default WHERE clause
|
||||
* @param string $order SQL snippet to override default ORDER BY clause
|
||||
* @param int $start The row number from which to start result set
|
||||
* @param int $count The number of rows to list in the result set.
|
||||
* @param array $params Parameters for placeholder substitutions, if any
|
||||
* @return mixed An array of records from the table if anything but
|
||||
* ('getOne'), a single value (if 'getOne'), or a PEAR_Error
|
||||
* @see DB::getAll()
|
||||
* @see MDB2::getAll()
|
||||
* @see DB::getAssoc()
|
||||
* @see MDB2::getAssoc()
|
||||
* @see DB::getCol()
|
||||
* @see MDB2::getCol()
|
||||
* @see DB::getOne()
|
||||
* @see MDB2::getOne()
|
||||
* @see DB::getRow()
|
||||
* @see MDB2::getRow()
|
||||
* @see DB_Table_Base::_swapModes()
|
||||
* @access public
|
||||
*/
|
||||
function select($query, $filter = null, $order = null,
|
||||
$start = null, $count = null, $params = array())
|
||||
{
|
||||
|
||||
// Is $query a query array or a key of $this->sql ?
|
||||
// On output from this block, $query is an array
|
||||
if (!is_array($query)) {
|
||||
if (is_string($query)) {
|
||||
if (isset($this->sql[$query])) {
|
||||
$query = $this->sql[$query];
|
||||
} else {
|
||||
return $this->throwError(
|
||||
constant($this->_primary_subclass . '_ERR_SQL_UNDEF'),
|
||||
$query);
|
||||
}
|
||||
} else {
|
||||
return $this->throwError(
|
||||
constant($this->_primary_subclass . '_ERR_SQL_NOT_STRING'));
|
||||
}
|
||||
}
|
||||
|
||||
// build the base command
|
||||
$sql = $this->buildSQL($query, $filter, $order, $start, $count);
|
||||
if (PEAR::isError($sql)) {
|
||||
return $sql;
|
||||
}
|
||||
|
||||
// set the get*() method name
|
||||
if (isset($query['get'])) {
|
||||
$method = ucwords(strtolower(trim($query['get'])));
|
||||
$method = "get$method";
|
||||
} else {
|
||||
$method = 'getAll';
|
||||
}
|
||||
|
||||
// DB_Table assumes you are using a shared PEAR DB/MDB2 object.
|
||||
// Record fetchmode settings, to be restored before returning.
|
||||
$db =& $this->db;
|
||||
$restore_mode = $db->fetchmode;
|
||||
if ($this->backend == 'mdb2') {
|
||||
$restore_class = $db->getOption('fetch_class');
|
||||
} else {
|
||||
$restore_class = $db->fetchmode_object_class;
|
||||
}
|
||||
|
||||
// swap modes
|
||||
$fetchmode = $this->fetchmode;
|
||||
$fetchmode_object_class = $this->fetchmode_object_class;
|
||||
if (isset($query['fetchmode'])) {
|
||||
$fetchmode = $query['fetchmode'];
|
||||
}
|
||||
if (isset($query['fetchmode_object_class'])) {
|
||||
$fetchmode_object_class = $query['fetchmode_object_class'];
|
||||
}
|
||||
$this->_swapModes($fetchmode, $fetchmode_object_class);
|
||||
|
||||
// make sure params is an array
|
||||
if (!is_null($params)) {
|
||||
$params = (array) $params;
|
||||
}
|
||||
|
||||
// get the result
|
||||
if ($this->backend == 'mdb2') {
|
||||
$result = $db->extended->$method($sql, null, $params);
|
||||
} else {
|
||||
switch ($method) {
|
||||
|
||||
case 'getCol':
|
||||
$result = $db->$method($sql, 0, $params);
|
||||
break;
|
||||
|
||||
case 'getAssoc':
|
||||
$result = $db->$method($sql, false, $params);
|
||||
break;
|
||||
|
||||
default:
|
||||
$result = $db->$method($sql, $params);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// restore old fetch_mode and fetch_object_class back
|
||||
$this->_swapModes($restore_mode, $restore_class);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Selects rows as a DB_Result/MDB2_Result_* object.
|
||||
*
|
||||
* @param string $query The name of the SQL SELECT to use from the
|
||||
* $this->sql property array.
|
||||
* @param string $filter SQL snippet to AND to the default WHERE clause
|
||||
* @param string $order SQL snippet to override default ORDER BY clause
|
||||
* @param int $start The record number from which to start result set
|
||||
* @param int $count The number of records to list in result set.
|
||||
* @param array $params Parameters for placeholder substitutions, if any.
|
||||
* @return object DB_Result/MDB2_Result_* object on success
|
||||
* (PEAR_Error on failure)
|
||||
* @see DB_Table::_swapModes()
|
||||
* @access public
|
||||
*/
|
||||
function selectResult($query, $filter = null, $order = null,
|
||||
$start = null, $count = null, $params = array())
|
||||
{
|
||||
// Is $query a query array or a key of $this->sql ?
|
||||
// On output from this block, $query is an array
|
||||
if (!is_array($query)) {
|
||||
if (is_string($query)) {
|
||||
if (isset($this->sql[$query])) {
|
||||
$query = $this->sql[$query];
|
||||
} else {
|
||||
return $this->throwError(
|
||||
constant($this->_primary_subclass . '_ERR_SQL_UNDEF'),
|
||||
$query);
|
||||
}
|
||||
} else {
|
||||
return $this->throwError(
|
||||
constant($this->_primary_subclass . '_ERR_SQL_NOT_STRING'));
|
||||
}
|
||||
}
|
||||
|
||||
// build the base command
|
||||
$sql = $this->buildSQL($query, $filter, $order, $start, $count);
|
||||
if (PEAR::isError($sql)) {
|
||||
return $sql;
|
||||
}
|
||||
|
||||
// DB_Table assumes you are using a shared PEAR DB/MDB2 object.
|
||||
// Record fetchmode settings, to be restored afterwards.
|
||||
$db =& $this->db;
|
||||
$restore_mode = $db->fetchmode;
|
||||
if ($this->backend == 'mdb2') {
|
||||
$restore_class = $db->getOption('fetch_class');
|
||||
} else {
|
||||
$restore_class = $db->fetchmode_object_class;
|
||||
}
|
||||
|
||||
// swap modes
|
||||
$fetchmode = $this->fetchmode;
|
||||
$fetchmode_object_class = $this->fetchmode_object_class;
|
||||
if (isset($query['fetchmode'])) {
|
||||
$fetchmode = $query['fetchmode'];
|
||||
}
|
||||
if (isset($query['fetchmode_object_class'])) {
|
||||
$fetchmode_object_class = $query['fetchmode_object_class'];
|
||||
}
|
||||
$this->_swapModes($fetchmode, $fetchmode_object_class);
|
||||
|
||||
// make sure params is an array
|
||||
if (!is_null($params)) {
|
||||
$params = (array) $params;
|
||||
}
|
||||
|
||||
// get the result
|
||||
if ($this->backend == 'mdb2') {
|
||||
$stmt =& $db->prepare($sql);
|
||||
if (PEAR::isError($stmt)) {
|
||||
return $stmt;
|
||||
}
|
||||
$result =& $stmt->execute($params);
|
||||
} else {
|
||||
$result =& $db->query($sql, $params);
|
||||
}
|
||||
|
||||
// swap modes back
|
||||
$this->_swapModes($restore_mode, $restore_class);
|
||||
|
||||
// return the result
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Counts the number of rows which will be returned by a query.
|
||||
*
|
||||
* This function works identically to {@link select()}, but it
|
||||
* returns the number of rows returned by a query instead of the
|
||||
* query results themselves.
|
||||
*
|
||||
* @author Ian Eure <ian@php.net>
|
||||
* @param string $query The name of the SQL SELECT to use from the
|
||||
* $this->sql property array.
|
||||
* @param string $filter Ad-hoc SQL snippet to AND with the default
|
||||
* SELECT WHERE clause.
|
||||
* @param string $order Ad-hoc SQL snippet to override the default
|
||||
* SELECT ORDER BY clause.
|
||||
* @param int $start Row number from which to start listing in result
|
||||
* @param int $count Number of rows to list in result set
|
||||
* @param array $params Parameters to use in placeholder substitutions
|
||||
* (if any).
|
||||
* @return int Number of records from the table (or PEAR_Error on failure)
|
||||
*
|
||||
* @see DB_Table::select()
|
||||
* @access public
|
||||
*/
|
||||
function selectCount($query, $filter = null, $order = null,
|
||||
$start = null, $count = null, $params = array())
|
||||
{
|
||||
|
||||
// Is $query a query array or a key of $this->sql ?
|
||||
if (is_array($query)) {
|
||||
$sql_key = null;
|
||||
$count_query = $query;
|
||||
} else {
|
||||
if (is_string($query)) {
|
||||
if (isset($this->sql[$query])) {
|
||||
$sql_key = $query;
|
||||
$count_query = $this->sql[$query];
|
||||
} else {
|
||||
return $this->throwError(
|
||||
constant($this->_primary_subclass . '_ERR_SQL_UNDEF'),
|
||||
$query);
|
||||
}
|
||||
} else {
|
||||
return $this->throwError(
|
||||
constant($this->_primary_subclass . '_ERR_SQL_NOT_STRING'));
|
||||
}
|
||||
}
|
||||
|
||||
// Use Table name as default 'from' if child class is DB_TABLE
|
||||
if ($this->_primary_subclass == 'DB_TABLE') {
|
||||
if (!isset($query['from'])) {
|
||||
$count_query['from'] = $this->table;
|
||||
}
|
||||
}
|
||||
|
||||
// If the query is a stored query in $this->sql, then create a corresponding
|
||||
// key for the count query, or check if the count-query already exists
|
||||
$ready = false;
|
||||
if ($sql_key) {
|
||||
// Create an sql key name for this count-query
|
||||
$count_key = '__count_' . $sql_key;
|
||||
// Check if a this count query alread exists in $this->sql
|
||||
if (isset($this->sql[$count_key])) {
|
||||
$ready = true;
|
||||
}
|
||||
}
|
||||
|
||||
// If a count-query does not already exist, create $count_query array
|
||||
if ($ready) {
|
||||
|
||||
$count_query = $this->sql[$count_key];
|
||||
|
||||
} else {
|
||||
|
||||
// Is a count-field set for the query?
|
||||
if (!isset($count_query['count']) ||
|
||||
trim($count_query['count']) == '') {
|
||||
$count_query['count'] = '*';
|
||||
}
|
||||
|
||||
// Replace the SELECT fields with a COUNT() command
|
||||
$count_query['select'] = "COUNT({$count_query['count']})";
|
||||
|
||||
// Replace the 'get' key so we only get one result item
|
||||
$count_query['get'] = 'one';
|
||||
|
||||
// Create a new count-query in $this->sql
|
||||
if ($sql_key) {
|
||||
$this->sql[$count_key] = $count_query;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Retrieve the count results
|
||||
return $this->select($count_query, $filter, $order,
|
||||
$start, $count, $params);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the $this->db PEAR DB/MDB2 object fetchmode and
|
||||
* fetchmode_object_class.
|
||||
*
|
||||
* @param string $new_mode A DB/MDB2_FETCHMODE_* constant. If null,
|
||||
* defaults to whatever the DB/MDB2 object is currently using.
|
||||
*
|
||||
* @param string $new_class The object class to use for results when
|
||||
* the $db object is in DB/MDB2_FETCHMODE_OBJECT fetch mode. If null,
|
||||
* defaults to whatever the the DB/MDB2 object is currently using.
|
||||
*
|
||||
* @return void
|
||||
* @access private
|
||||
*/
|
||||
function _swapModes($new_mode, $new_class)
|
||||
{
|
||||
// get the old (current) mode and class
|
||||
$db =& $this->db;
|
||||
$old_mode = $db->fetchmode;
|
||||
if ($this->backend == 'mdb2') {
|
||||
$old_class = $db->getOption('fetch_class');
|
||||
} else {
|
||||
$old_class = $db->fetchmode_object_class;
|
||||
}
|
||||
|
||||
// don't need to swap anything if the new modes are both
|
||||
// null or if the old and new modes already match.
|
||||
if ((is_null($new_mode) && is_null($new_class)) ||
|
||||
($old_mode == $new_mode && $old_class == $new_class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// set the default new mode
|
||||
if (is_null($new_mode)) {
|
||||
$new_mode = $old_mode;
|
||||
}
|
||||
|
||||
// set the default new class
|
||||
if (is_null($new_class)) {
|
||||
$new_class = $old_class;
|
||||
}
|
||||
|
||||
// swap modes
|
||||
$db->setFetchMode($new_mode, $new_class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns SQL condition equating columns to literal values.
|
||||
*
|
||||
* The parameter $data is an associative array in which keys are
|
||||
* column names and values are corresponding values. The method
|
||||
* returns an SQL string that is true if the value of every
|
||||
* specified database columns is equal to the corresponding
|
||||
* value in $data.
|
||||
*
|
||||
* For example, if:
|
||||
* <code>
|
||||
* $data = array( 'c1' => 'thing', 'c2' => 23, 'c3' => 0.32 )
|
||||
* </code>
|
||||
* then buildFilter($data) returns a string
|
||||
* <code>
|
||||
* c1 => 'thing' AND c2 => 23 AND c3 = 0.32
|
||||
* </code>
|
||||
* in which string values are replaced by SQL literal values,
|
||||
* quoted and escaped as necessary.
|
||||
*
|
||||
* Values are quoted and escaped as appropriate for each data
|
||||
* type and the backend RDBMS, using the MDB2::quote() or
|
||||
* DB::smartQuote() method. The behavior depends on the PHP type
|
||||
* of the value: string values are quoted and escaped, while
|
||||
* integer and float numerical values are not. Boolean values
|
||||
* in $data are represented as 0 or 1, consistent with the way
|
||||
* booleans are stored by DB_Table.
|
||||
*
|
||||
* Null values: The treatment of null values in $data depends upon
|
||||
* the value of the $match parameter . If $match == 'simple', an
|
||||
* empty string is returned if any $value of $data with a key in
|
||||
* $data_key is null. If $match == 'partial', the returned SQL
|
||||
* expression equates only the relevant non-null values of $data
|
||||
* to the values of corresponding database columns. If
|
||||
* $match == 'full', the function returns an empty string if all
|
||||
* of the relevant values of data are null, and returns a
|
||||
* PEAR_Error if some of the selected values are null and others
|
||||
* are not null.
|
||||
*
|
||||
* @param array $data associative array, keys are column names
|
||||
* @return string SQL expression equating values in $data to
|
||||
* values of columns named by keys.
|
||||
* @access public
|
||||
*/
|
||||
function buildFilter($data, $match = 'simple')
|
||||
{
|
||||
// Check $match type value
|
||||
if (!in_array($match, array('simple', 'partial', 'full'))) {
|
||||
return $this->throwError(
|
||||
DB_TABLE_DATABASE_ERR_MATCH_TYPE);
|
||||
}
|
||||
|
||||
if (count($data) == 0) {
|
||||
return '';
|
||||
}
|
||||
$filter = array();
|
||||
foreach ($data as $key => $value) {
|
||||
if (!is_null($value)) {
|
||||
if ($match == 'full' && isset($found_null)) {
|
||||
return $this->throwError(
|
||||
DB_TABLE_DATABASE_ERR_FULL_KEY);
|
||||
}
|
||||
if (is_bool($value)) {
|
||||
$value = $value ? '1' : '0';
|
||||
} else {
|
||||
if ($this->backend == 'mdb2') {
|
||||
$value = $this->db->quote($value);
|
||||
} else {
|
||||
$value = $this->db->quoteSmart($value);
|
||||
}
|
||||
}
|
||||
$filter[] = "$key = $value";
|
||||
} else {
|
||||
if ($match == 'simple') {
|
||||
return ''; // if any value in $data is null
|
||||
} elseif ($match == 'full') {
|
||||
$found_null = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return implode(' AND ', $filter);
|
||||
}
|
||||
|
||||
// }}}
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
/* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* c-hanging-comment-ender-p: nil
|
||||
* End:
|
||||
*/
|
||||
|
||||
?>
|
3495
sites/all/modules/civicrm/packages/DB/Table/Database.php
Normal file
3495
sites/all/modules/civicrm/packages/DB/Table/Database.php
Normal file
File diff suppressed because it is too large
Load diff
195
sites/all/modules/civicrm/packages/DB/Table/Date.php
Normal file
195
sites/all/modules/civicrm/packages/DB/Table/Date.php
Normal file
|
@ -0,0 +1,195 @@
|
|||
<?php
|
||||
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* Generic date handling class for DB_Table.
|
||||
*
|
||||
* Stripped down to two essential methods specially for DB_Table from the
|
||||
* PEAR Date package by Paul M. Jones <pmjones@php.net>.
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE:
|
||||
*
|
||||
* Copyright (c) 1997-2007, Paul M. Jones <pmjones@php.net>
|
||||
* David C. Morse <morse@php.net>
|
||||
* Mark Wiesemann <wiesemann@php.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * The names of the authors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Baba Buehler <baba@babaz.com>
|
||||
* @author Pierre-Alain Joye <pajoye@php.net>
|
||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||
* @version CVS: $Id: Date.php,v 1.3 2007/12/13 16:52:14 wiesemann Exp $
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generic date handling class for DB_Table.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Baba Buehler <baba@babaz.com>
|
||||
* @author Pierre-Alain Joye <pajoye@php.net>
|
||||
* @version Release: 1.5.6
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
class DB_Table_Date {
|
||||
|
||||
/**
|
||||
* the year
|
||||
* @var int
|
||||
*/
|
||||
var $year;
|
||||
/**
|
||||
* the month
|
||||
* @var int
|
||||
*/
|
||||
var $month;
|
||||
/**
|
||||
* the day
|
||||
* @var int
|
||||
*/
|
||||
var $day;
|
||||
/**
|
||||
* the hour
|
||||
* @var int
|
||||
*/
|
||||
var $hour;
|
||||
/**
|
||||
* the minute
|
||||
* @var int
|
||||
*/
|
||||
var $minute;
|
||||
/**
|
||||
* the second
|
||||
* @var int
|
||||
*/
|
||||
var $second;
|
||||
/**
|
||||
* the parts of a second
|
||||
* @var float
|
||||
*/
|
||||
var $partsecond;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Creates a new DB_Table_Date Object. The date should be near to
|
||||
* ISO 8601 format.
|
||||
*
|
||||
* @access public
|
||||
* @param string $date A date in ISO 8601 format.
|
||||
*/
|
||||
function __construct($date)
|
||||
{
|
||||
// This regex is very loose and accepts almost any butchered
|
||||
// format you could throw at it. e.g. 2003-10-07 19:45:15 and
|
||||
// 2003-10071945:15 are the same thing in the eyes of this
|
||||
// regex, even though the latter is not a valid ISO 8601 date.
|
||||
preg_match('/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)?$/i', $date, $regs);
|
||||
$this->year = $regs[1];
|
||||
$this->month = $regs[2];
|
||||
$this->day = $regs[3];
|
||||
$this->hour = isset($regs[5])?$regs[5]:0;
|
||||
$this->minute = isset($regs[6])?$regs[6]:0;
|
||||
$this->second = isset($regs[7])?$regs[7]:0;
|
||||
$this->partsecond = isset($regs[8])?(float)$regs[8]:(float)0;
|
||||
|
||||
// if an offset is defined, convert time to UTC
|
||||
// Date currently can't set a timezone only by offset,
|
||||
// so it has to store it as UTC
|
||||
if (isset($regs[9])) {
|
||||
$this->toUTCbyOffset($regs[9]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Date pretty printing, similar to strftime()
|
||||
*
|
||||
* Formats the date in the given format, much like
|
||||
* strftime(). Most strftime() options are supported.<br><br>
|
||||
*
|
||||
* formatting options:<br><br>
|
||||
*
|
||||
* <code>%Y </code> year as decimal including century (range 0000 to 9999) <br>
|
||||
* <code>%m </code> month as decimal number (range 01 to 12) <br>
|
||||
* <code>%d </code> day of month (range 00 to 31) <br>
|
||||
* <code>%H </code> hour as decimal number (00 to 23) <br>
|
||||
* <code>%M </code> minute as a decimal number (00 to 59) <br>
|
||||
* <code>%S </code> seconds as a decimal number (00 to 59) <br>
|
||||
* <code>%% </code> literal '%' <br>
|
||||
* <br>
|
||||
*
|
||||
* @access public
|
||||
* @param string format the format string for returned date/time
|
||||
* @return string date/time in given format
|
||||
*/
|
||||
function format($format)
|
||||
{
|
||||
$output = "";
|
||||
|
||||
for($strpos = 0; $strpos < strlen($format); $strpos++) {
|
||||
$char = substr($format,$strpos,1);
|
||||
if ($char == "%") {
|
||||
$nextchar = substr($format,$strpos + 1,1);
|
||||
switch ($nextchar) {
|
||||
case "Y":
|
||||
$output .= $this->year;
|
||||
break;
|
||||
case "m":
|
||||
$output .= sprintf("%02d",$this->month);
|
||||
break;
|
||||
case "d":
|
||||
$output .= sprintf("%02d",$this->day);
|
||||
break;
|
||||
case "H":
|
||||
$output .= sprintf("%02d", $this->hour);
|
||||
break;
|
||||
case "M":
|
||||
$output .= sprintf("%02d",$this->minute);
|
||||
break;
|
||||
case "S":
|
||||
$output .= sprintf("%02d", $this->second);
|
||||
break;
|
||||
default:
|
||||
$output .= $char.$nextchar;
|
||||
}
|
||||
$strpos++;
|
||||
} else {
|
||||
$output .= $char;
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
1332
sites/all/modules/civicrm/packages/DB/Table/Generator.php
Normal file
1332
sites/all/modules/civicrm/packages/DB/Table/Generator.php
Normal file
File diff suppressed because it is too large
Load diff
2349
sites/all/modules/civicrm/packages/DB/Table/Manager.php
Normal file
2349
sites/all/modules/civicrm/packages/DB/Table/Manager.php
Normal file
File diff suppressed because it is too large
Load diff
443
sites/all/modules/civicrm/packages/DB/Table/Manager/mysql.php
Normal file
443
sites/all/modules/civicrm/packages/DB/Table/Manager/mysql.php
Normal file
|
@ -0,0 +1,443 @@
|
|||
<?php
|
||||
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* Index, constraint and alter methods for DB_Table usage with
|
||||
* PEAR::DB as backend.
|
||||
*
|
||||
* The code in this class was adopted from the MDB2 PEAR package.
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE:
|
||||
*
|
||||
* Copyright (c) 1997-2007, Lukas Smith <smith@pooteeweet.org>
|
||||
* Paul M. Jones <pmjones@php.net>
|
||||
* David C. Morse <morse@php.net>
|
||||
* Mark Wiesemann <wiesemann@php.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * The names of the authors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Lukas Smith <smith@pooteeweet.org>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||
* @version CVS: $Id: mysql.php,v 1.5 2007/12/13 16:52:15 wiesemann Exp $
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
/**
|
||||
* require DB_Table class
|
||||
*/
|
||||
require_once 'DB/Table.php';
|
||||
|
||||
/**
|
||||
* Index, constraint and alter methods for DB_Table usage with
|
||||
* PEAR::DB as backend.
|
||||
*
|
||||
* The code in this class was adopted from the MDB2 PEAR package.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Lukas Smith <smith@pooteeweet.org>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @version Release: 1.5.6
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
class DB_Table_Manager_mysql {
|
||||
|
||||
/**
|
||||
*
|
||||
* The PEAR DB object that connects to the database.
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @var object
|
||||
*
|
||||
*/
|
||||
|
||||
var $_db = null;
|
||||
|
||||
|
||||
/**
|
||||
* list all indexes in a table
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function listTableIndexes($table)
|
||||
{
|
||||
$key_name = 'Key_name';
|
||||
$non_unique = 'Non_unique';
|
||||
|
||||
$query = "SHOW INDEX FROM $table";
|
||||
$indexes = $this->_db->getAll($query, null, DB_FETCHMODE_ASSOC);
|
||||
if (PEAR::isError($indexes)) {
|
||||
return $indexes;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
foreach ($indexes as $index_data) {
|
||||
if ($index_data[$non_unique]) {
|
||||
$result[$index_data[$key_name]] = true;
|
||||
}
|
||||
}
|
||||
$result = array_change_key_case($result, CASE_LOWER);
|
||||
|
||||
return array_keys($result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* list all constraints in a table
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function listTableConstraints($table)
|
||||
{
|
||||
$key_name = 'Key_name';
|
||||
$non_unique = 'Non_unique';
|
||||
|
||||
$query = "SHOW INDEX FROM $table";
|
||||
$indexes = $this->_db->getAll($query, null, DB_FETCHMODE_ASSOC);
|
||||
if (PEAR::isError($indexes)) {
|
||||
return $indexes;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
foreach ($indexes as $index_data) {
|
||||
if (!$index_data[$non_unique]) {
|
||||
if ($index_data[$key_name] !== 'PRIMARY') {
|
||||
$index = $index_data[$key_name];
|
||||
} else {
|
||||
$index = 'PRIMARY';
|
||||
}
|
||||
$result[$index] = true;
|
||||
}
|
||||
}
|
||||
$result = array_change_key_case($result, CASE_LOWER);
|
||||
|
||||
return array_keys($result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get the structure of an index into an array
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $index_name name of index that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function getTableIndexDefinition($table, $index_name)
|
||||
{
|
||||
$result = $this->_db->query("SHOW INDEX FROM $table");
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$definition = array();
|
||||
while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC))) {
|
||||
$row = array_change_key_case($row, CASE_LOWER);
|
||||
$key_name = $row['key_name'];
|
||||
$key_name = strtolower($key_name);
|
||||
|
||||
if ($index_name == $key_name) {
|
||||
$column_name = $row['column_name'];
|
||||
$column_name = strtolower($column_name);
|
||||
$definition['fields'][$column_name] = array();
|
||||
if (array_key_exists('collation', $row)) {
|
||||
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
|
||||
? 'ascending' : 'descending');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result->free();
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get the structure of a constraint into an array
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $index_name name of index that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function getTableConstraintDefinition($table, $index_name)
|
||||
{
|
||||
$result = $this->_db->query("SHOW INDEX FROM $table");
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$definition = array();
|
||||
while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC))) {
|
||||
$row = array_change_key_case($row, CASE_LOWER);
|
||||
$key_name = $row['key_name'];
|
||||
$key_name = strtolower($key_name);
|
||||
|
||||
if ($index_name == $key_name) {
|
||||
if ($row['key_name'] == 'PRIMARY') {
|
||||
$definition['primary'] = true;
|
||||
} else {
|
||||
$definition['unique'] = true;
|
||||
}
|
||||
$column_name = $row['column_name'];
|
||||
$column_name = strtolower($column_name);
|
||||
$definition['fields'][$column_name] = array();
|
||||
if (array_key_exists('collation', $row)) {
|
||||
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
|
||||
? 'ascending' : 'descending');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result->free();
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* drop existing index
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $name name of the index to be dropped
|
||||
* @return mixed DB_OK on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function dropIndex($table, $name)
|
||||
{
|
||||
$table = $this->_db->quoteIdentifier($table);
|
||||
$name = $this->_db->quoteIdentifier($name);
|
||||
return $this->_db->query("DROP INDEX $name ON $table");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* drop existing constraint
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $name name of the constraint to be dropped
|
||||
* @return mixed DB_OK on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function dropConstraint($table, $name)
|
||||
{
|
||||
$table = $this->_db->quoteIdentifier($table);
|
||||
if (strtolower($name) == 'primary') {
|
||||
$query = "ALTER TABLE $table DROP PRIMARY KEY";
|
||||
} else {
|
||||
$name = $this->_db->quoteIdentifier($name);
|
||||
$query = "ALTER TABLE $table DROP INDEX $name";
|
||||
}
|
||||
return $this->_db->query($query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* alter an existing table
|
||||
*
|
||||
* @param string $name name of the table that is intended to be changed.
|
||||
* @param array $changes associative array that contains the details of each type
|
||||
* of change that is intended to be performed. The types of
|
||||
* changes that are currently supported are defined as follows:
|
||||
*
|
||||
* name
|
||||
*
|
||||
* New name for the table.
|
||||
*
|
||||
* add
|
||||
*
|
||||
* Associative array with the names of fields to be added as
|
||||
* indexes of the array. The value of each entry of the array
|
||||
* should be set to another associative array with the properties
|
||||
* of the fields to be added. The properties of the fields should
|
||||
* be the same as defined by the Metabase parser.
|
||||
*
|
||||
*
|
||||
* remove
|
||||
*
|
||||
* Associative array with the names of fields to be removed as indexes
|
||||
* of the array. Currently the values assigned to each entry are ignored.
|
||||
* An empty array should be used for future compatibility.
|
||||
*
|
||||
* rename
|
||||
*
|
||||
* Associative array with the names of fields to be renamed as indexes
|
||||
* of the array. The value of each entry of the array should be set to
|
||||
* another associative array with the entry named name with the new
|
||||
* field name and the entry named Declaration that is expected to contain
|
||||
* the portion of the field declaration already in DBMS specific SQL code
|
||||
* as it is used in the CREATE TABLE statement.
|
||||
*
|
||||
* change
|
||||
*
|
||||
* Associative array with the names of the fields to be changed as indexes
|
||||
* of the array. Keep in mind that if it is intended to change either the
|
||||
* name of a field and any other properties, the change array entries
|
||||
* should have the new names of the fields as array indexes.
|
||||
*
|
||||
* The value of each entry of the array should be set to another associative
|
||||
* array with the properties of the fields to that are meant to be changed as
|
||||
* array entries. These entries should be assigned to the new values of the
|
||||
* respective properties. The properties of the fields should be the same
|
||||
* as defined by the Metabase parser.
|
||||
*
|
||||
* Example
|
||||
* array(
|
||||
* 'name' => 'userlist',
|
||||
* 'add' => array(
|
||||
* 'quota' => array(
|
||||
* 'type' => 'integer',
|
||||
* 'unsigned' => 1
|
||||
* )
|
||||
* ),
|
||||
* 'remove' => array(
|
||||
* 'file_limit' => array(),
|
||||
* 'time_limit' => array()
|
||||
* ),
|
||||
* 'change' => array(
|
||||
* 'name' => array(
|
||||
* 'length' => '20',
|
||||
* 'definition' => array(
|
||||
* 'type' => 'text',
|
||||
* 'length' => 20,
|
||||
* ),
|
||||
* )
|
||||
* ),
|
||||
* 'rename' => array(
|
||||
* 'sex' => array(
|
||||
* 'name' => 'gender',
|
||||
* 'definition' => array(
|
||||
* 'type' => 'text',
|
||||
* 'length' => 1,
|
||||
* 'default' => 'M',
|
||||
* ),
|
||||
* )
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @param boolean $check (ignored in DB_Table)
|
||||
* @access public
|
||||
*
|
||||
* @return mixed DB_OK on success, a PEAR error on failure
|
||||
*/
|
||||
function alterTable($name, $changes, $check)
|
||||
{
|
||||
foreach ($changes as $change_name => $change) {
|
||||
switch ($change_name) {
|
||||
case 'add':
|
||||
case 'remove':
|
||||
case 'change':
|
||||
case 'rename':
|
||||
case 'name':
|
||||
break;
|
||||
default:
|
||||
return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
|
||||
}
|
||||
}
|
||||
|
||||
$query = '';
|
||||
if (array_key_exists('name', $changes)) {
|
||||
$change_name = $this->_db->quoteIdentifier($changes['name']);
|
||||
$query .= 'RENAME TO ' . $change_name;
|
||||
}
|
||||
|
||||
if (array_key_exists('add', $changes)) {
|
||||
foreach ($changes['add'] as $field_name => $field) {
|
||||
if ($query) {
|
||||
$query.= ', ';
|
||||
}
|
||||
$query.= 'ADD ' . $field_name . ' ' . $field;
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('remove', $changes)) {
|
||||
foreach ($changes['remove'] as $field_name => $field) {
|
||||
if ($query) {
|
||||
$query.= ', ';
|
||||
}
|
||||
$field_name = $this->_db->quoteIdentifier($field_name);
|
||||
$query.= 'DROP ' . $field_name;
|
||||
}
|
||||
}
|
||||
|
||||
$rename = array();
|
||||
if (array_key_exists('rename', $changes)) {
|
||||
foreach ($changes['rename'] as $field_name => $field) {
|
||||
$rename[$field['name']] = $field_name;
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('change', $changes)) {
|
||||
foreach ($changes['change'] as $field_name => $field) {
|
||||
if ($query) {
|
||||
$query.= ', ';
|
||||
}
|
||||
if (isset($rename[$field_name])) {
|
||||
$old_field_name = $rename[$field_name];
|
||||
unset($rename[$field_name]);
|
||||
} else {
|
||||
$old_field_name = $field_name;
|
||||
}
|
||||
$old_field_name = $this->_db->quoteIdentifier($old_field_name);
|
||||
$query.= "CHANGE $old_field_name " . $field_name . ' ' . $field['definition'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($rename)) {
|
||||
foreach ($rename as $rename_name => $renamed_field) {
|
||||
if ($query) {
|
||||
$query.= ', ';
|
||||
}
|
||||
$field = $changes['rename'][$renamed_field];
|
||||
$renamed_field = $this->_db->quoteIdentifier($renamed_field);
|
||||
$query.= 'CHANGE ' . $renamed_field . ' ' . $field['name'] . ' ' . $renamed_field['definition'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$query) {
|
||||
return DB_OK;
|
||||
}
|
||||
|
||||
$name = $this->_db->quoteIdentifier($name);
|
||||
return $this->_db->query("ALTER TABLE $name $query");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
440
sites/all/modules/civicrm/packages/DB/Table/Manager/mysqli.php
Normal file
440
sites/all/modules/civicrm/packages/DB/Table/Manager/mysqli.php
Normal file
|
@ -0,0 +1,440 @@
|
|||
<?php
|
||||
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* Index, constraint and alter methods for DB_Table usage with
|
||||
* PEAR::DB as backend.
|
||||
*
|
||||
* The code in this class was adopted from the MDB2 PEAR package.
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE:
|
||||
*
|
||||
* Copyright (c) 1997-2007, Lukas Smith <smith@pooteeweet.org>
|
||||
* Paul M. Jones <pmjones@php.net>
|
||||
* David C. Morse <morse@php.net>
|
||||
* Mark Wiesemann <wiesemann@php.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * The names of the authors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Lukas Smith <smith@pooteeweet.org>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||
* @version CVS: $Id: mysqli.php,v 1.6 2007/12/13 16:52:15 wiesemann Exp $
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
/**
|
||||
* require DB_Table class
|
||||
*/
|
||||
require_once 'DB/Table.php';
|
||||
|
||||
/**
|
||||
* Index, constraint and alter methods for DB_Table usage with
|
||||
* PEAR::DB as backend.
|
||||
*
|
||||
* The code in this class was adopted from the MDB2 PEAR package.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Lukas Smith <smith@pooteeweet.org>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @version Release: 1.5.6
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
class DB_Table_Manager_mysqli {
|
||||
|
||||
/**
|
||||
*
|
||||
* The PEAR DB object that connects to the database.
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @var object
|
||||
*
|
||||
*/
|
||||
|
||||
var $_db = null;
|
||||
|
||||
|
||||
/**
|
||||
* list all indexes in a table
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function listTableIndexes($table)
|
||||
{
|
||||
$key_name = 'Key_name';
|
||||
$non_unique = 'Non_unique';
|
||||
|
||||
$query = "SHOW INDEX FROM $table";
|
||||
$indexes = $this->_db->getAll($query, null, DB_FETCHMODE_ASSOC);
|
||||
if (PEAR::isError($indexes)) {
|
||||
return $indexes;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
foreach ($indexes as $index_data) {
|
||||
if ($index_data[$non_unique]) {
|
||||
$result[$index_data[$key_name]] = true;
|
||||
}
|
||||
}
|
||||
$result = array_change_key_case($result, CASE_LOWER);
|
||||
|
||||
return array_keys($result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* list all constraints in a table
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function listTableConstraints($table)
|
||||
{
|
||||
$key_name = 'Key_name';
|
||||
$non_unique = 'Non_unique';
|
||||
|
||||
$query = "SHOW INDEX FROM $table";
|
||||
$indexes = $this->_db->getAll($query, null, DB_FETCHMODE_ASSOC);
|
||||
if (PEAR::isError($indexes)) {
|
||||
return $indexes;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
foreach ($indexes as $index_data) {
|
||||
if (!$index_data[$non_unique]) {
|
||||
if ($index_data[$key_name] !== 'PRIMARY') {
|
||||
$index = $index_data[$key_name];
|
||||
} else {
|
||||
$index = 'PRIMARY';
|
||||
}
|
||||
$result[$index] = true;
|
||||
}
|
||||
}
|
||||
$result = array_change_key_case($result, CASE_LOWER);
|
||||
|
||||
return array_keys($result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get the structure of an index into an array
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $index_name name of index that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function getTableIndexDefinition($table, $index_name)
|
||||
{
|
||||
$result = $this->_db->query("SHOW INDEX FROM $table");
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$definition = array();
|
||||
while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC))) {
|
||||
$row = array_change_key_case($row, CASE_LOWER);
|
||||
$key_name = $row['key_name'];
|
||||
$key_name = strtolower($key_name);
|
||||
if ($index_name == $key_name) {
|
||||
$column_name = $row['column_name'];
|
||||
$column_name = strtolower($column_name);
|
||||
$definition['fields'][$column_name] = array();
|
||||
if (array_key_exists('collation', $row)) {
|
||||
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
|
||||
? 'ascending' : 'descending');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result->free();
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get the structure of a constraint into an array
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $index_name name of index that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function getTableConstraintDefinition($table, $index_name)
|
||||
{
|
||||
$result = $this->_db->query("SHOW INDEX FROM $table");
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$definition = array();
|
||||
while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC))) {
|
||||
$row = array_change_key_case($row, CASE_LOWER);
|
||||
$key_name = $row['key_name'];
|
||||
$key_name = strtolower($key_name);
|
||||
if ($index_name == $key_name) {
|
||||
if ($row['key_name'] == 'PRIMARY') {
|
||||
$definition['primary'] = true;
|
||||
} else {
|
||||
$definition['unique'] = true;
|
||||
}
|
||||
$column_name = $row['column_name'];
|
||||
$column_name = strtolower($column_name);
|
||||
$definition['fields'][$column_name] = array();
|
||||
if (array_key_exists('collation', $row)) {
|
||||
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
|
||||
? 'ascending' : 'descending');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result->free();
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* drop existing index
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $name name of the index to be dropped
|
||||
* @return mixed DB_OK on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function dropIndex($table, $name)
|
||||
{
|
||||
$table = $this->_db->quoteIdentifier($table);
|
||||
$name = $this->_db->quoteIdentifier($name);
|
||||
return $this->_db->query("DROP INDEX $name ON $table");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* drop existing constraint
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $name name of the constraint to be dropped
|
||||
* @return mixed DB_OK on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function dropConstraint($table, $name)
|
||||
{
|
||||
$table = $this->_db->quoteIdentifier($table);
|
||||
if (strtolower($name) == 'primary') {
|
||||
$query = "ALTER TABLE $table DROP PRIMARY KEY";
|
||||
} else {
|
||||
$name = $this->_db->quoteIdentifier($name);
|
||||
$query = "ALTER TABLE $table DROP INDEX $name";
|
||||
}
|
||||
return $this->_db->query($query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* alter an existing table
|
||||
*
|
||||
* @param string $name name of the table that is intended to be changed.
|
||||
* @param array $changes associative array that contains the details of each type
|
||||
* of change that is intended to be performed. The types of
|
||||
* changes that are currently supported are defined as follows:
|
||||
*
|
||||
* name
|
||||
*
|
||||
* New name for the table.
|
||||
*
|
||||
* add
|
||||
*
|
||||
* Associative array with the names of fields to be added as
|
||||
* indexes of the array. The value of each entry of the array
|
||||
* should be set to another associative array with the properties
|
||||
* of the fields to be added. The properties of the fields should
|
||||
* be the same as defined by the Metabase parser.
|
||||
*
|
||||
*
|
||||
* remove
|
||||
*
|
||||
* Associative array with the names of fields to be removed as indexes
|
||||
* of the array. Currently the values assigned to each entry are ignored.
|
||||
* An empty array should be used for future compatibility.
|
||||
*
|
||||
* rename
|
||||
*
|
||||
* Associative array with the names of fields to be renamed as indexes
|
||||
* of the array. The value of each entry of the array should be set to
|
||||
* another associative array with the entry named name with the new
|
||||
* field name and the entry named Declaration that is expected to contain
|
||||
* the portion of the field declaration already in DBMS specific SQL code
|
||||
* as it is used in the CREATE TABLE statement.
|
||||
*
|
||||
* change
|
||||
*
|
||||
* Associative array with the names of the fields to be changed as indexes
|
||||
* of the array. Keep in mind that if it is intended to change either the
|
||||
* name of a field and any other properties, the change array entries
|
||||
* should have the new names of the fields as array indexes.
|
||||
*
|
||||
* The value of each entry of the array should be set to another associative
|
||||
* array with the properties of the fields to that are meant to be changed as
|
||||
* array entries. These entries should be assigned to the new values of the
|
||||
* respective properties. The properties of the fields should be the same
|
||||
* as defined by the Metabase parser.
|
||||
*
|
||||
* Example
|
||||
* array(
|
||||
* 'name' => 'userlist',
|
||||
* 'add' => array(
|
||||
* 'quota' => array(
|
||||
* 'type' => 'integer',
|
||||
* 'unsigned' => 1
|
||||
* )
|
||||
* ),
|
||||
* 'remove' => array(
|
||||
* 'file_limit' => array(),
|
||||
* 'time_limit' => array()
|
||||
* ),
|
||||
* 'change' => array(
|
||||
* 'name' => array(
|
||||
* 'length' => '20',
|
||||
* 'definition' => array(
|
||||
* 'type' => 'text',
|
||||
* 'length' => 20,
|
||||
* ),
|
||||
* )
|
||||
* ),
|
||||
* 'rename' => array(
|
||||
* 'sex' => array(
|
||||
* 'name' => 'gender',
|
||||
* 'definition' => array(
|
||||
* 'type' => 'text',
|
||||
* 'length' => 1,
|
||||
* 'default' => 'M',
|
||||
* ),
|
||||
* )
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @param boolean $check (ignored in DB_Table)
|
||||
* @access public
|
||||
*
|
||||
* @return mixed DB_OK on success, a PEAR error on failure
|
||||
*/
|
||||
function alterTable($name, $changes, $check)
|
||||
{
|
||||
foreach ($changes as $change_name => $change) {
|
||||
switch ($change_name) {
|
||||
case 'add':
|
||||
case 'remove':
|
||||
case 'change':
|
||||
case 'rename':
|
||||
case 'name':
|
||||
break;
|
||||
default:
|
||||
return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
|
||||
}
|
||||
}
|
||||
|
||||
$query = '';
|
||||
if (array_key_exists('name', $changes)) {
|
||||
$change_name = $this->_db->quoteIdentifier($changes['name']);
|
||||
$query .= 'RENAME TO ' . $change_name;
|
||||
}
|
||||
|
||||
if (array_key_exists('add', $changes)) {
|
||||
foreach ($changes['add'] as $field_name => $field) {
|
||||
if ($query) {
|
||||
$query.= ', ';
|
||||
}
|
||||
$query.= 'ADD ' . $field_name . ' ' . $field;
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('remove', $changes)) {
|
||||
foreach ($changes['remove'] as $field_name => $field) {
|
||||
if ($query) {
|
||||
$query.= ', ';
|
||||
}
|
||||
$field_name = $db->quoteIdentifier($field_name);
|
||||
$query.= 'DROP ' . $field_name;
|
||||
}
|
||||
}
|
||||
|
||||
$rename = array();
|
||||
if (array_key_exists('rename', $changes)) {
|
||||
foreach ($changes['rename'] as $field_name => $field) {
|
||||
$rename[$field['name']] = $field_name;
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('change', $changes)) {
|
||||
foreach ($changes['change'] as $field_name => $field) {
|
||||
if ($query) {
|
||||
$query.= ', ';
|
||||
}
|
||||
if (isset($rename[$field_name])) {
|
||||
$old_field_name = $rename[$field_name];
|
||||
unset($rename[$field_name]);
|
||||
} else {
|
||||
$old_field_name = $field_name;
|
||||
}
|
||||
$old_field_name = $this->_db->quoteIdentifier($old_field_name);
|
||||
$query.= "CHANGE $old_field_name " . $field_name . ' ' . $field['definition'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($rename)) {
|
||||
foreach ($rename as $rename_name => $renamed_field) {
|
||||
if ($query) {
|
||||
$query.= ', ';
|
||||
}
|
||||
$field = $changes['rename'][$renamed_field];
|
||||
$renamed_field = $this->_db->quoteIdentifier($renamed_field);
|
||||
$query.= 'CHANGE ' . $renamed_field . ' ' . $field['name'] . ' ' . $renamed_field['definition'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$query) {
|
||||
return DB_OK;
|
||||
}
|
||||
|
||||
$name = $this->_db->quoteIdentifier($name);
|
||||
return $this->_db->query("ALTER TABLE $name $query");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
440
sites/all/modules/civicrm/packages/DB/Table/Manager/pgsql.php
Normal file
440
sites/all/modules/civicrm/packages/DB/Table/Manager/pgsql.php
Normal file
|
@ -0,0 +1,440 @@
|
|||
<?php
|
||||
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* Index, constraint and alter methods for DB_Table usage with
|
||||
* PEAR::DB as backend.
|
||||
*
|
||||
* The code in this class was adopted from the MDB2 PEAR package.
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE:
|
||||
*
|
||||
* Copyright (c) 1997-2007, Paul Cooper <pgc@ucecom.com>
|
||||
* Paul M. Jones <pmjones@php.net>
|
||||
* David C. Morse <morse@php.net>
|
||||
* Mark Wiesemann <wiesemann@php.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * The names of the authors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Paul Cooper <pgc@ucecom.com>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||
* @version CVS: $Id: pgsql.php,v 1.5 2007/12/13 16:52:15 wiesemann Exp $
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
/**
|
||||
* require DB_Table class
|
||||
*/
|
||||
require_once 'DB/Table.php';
|
||||
|
||||
/**
|
||||
* Index, constraint and alter methods for DB_Table usage with
|
||||
* PEAR::DB as backend.
|
||||
*
|
||||
* The code in this class was adopted from the MDB2 PEAR package.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Paul Cooper <pgc@ucecom.com>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @version Release: 1.5.6
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
class DB_Table_Manager_pgsql {
|
||||
|
||||
/**
|
||||
*
|
||||
* The PEAR DB object that connects to the database.
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @var object
|
||||
*
|
||||
*/
|
||||
|
||||
var $_db = null;
|
||||
|
||||
|
||||
/**
|
||||
* list all indexes in a table
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function listTableIndexes($table)
|
||||
{
|
||||
$subquery = "SELECT indexrelid FROM pg_index, pg_class";
|
||||
$subquery.= " WHERE pg_class.relname='$table' AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'";
|
||||
$query = "SELECT relname FROM pg_class WHERE oid IN ($subquery)";
|
||||
$indexes = $this->_db->getCol($query);
|
||||
if (PEAR::isError($indexes)) {
|
||||
return $indexes;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
foreach ($indexes as $index) {
|
||||
$result[$index] = true;
|
||||
}
|
||||
$result = array_change_key_case($result, CASE_LOWER);
|
||||
|
||||
return array_keys($result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* list all constraints in a table
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function listTableConstraints($table)
|
||||
{
|
||||
$subquery = "SELECT indexrelid FROM pg_index, pg_class";
|
||||
$subquery.= " WHERE pg_class.relname='$table' AND pg_class.oid=pg_index.indrelid AND (indisunique = 't' OR indisprimary = 't')";
|
||||
$query = "SELECT relname FROM pg_class WHERE oid IN ($subquery)";
|
||||
$constraints = $this->_db->getCol($query);
|
||||
if (PEAR::isError($constraints)) {
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
foreach ($constraints as $constraint) {
|
||||
$result[$constraint] = true;
|
||||
}
|
||||
$result = array_change_key_case($result, CASE_LOWER);
|
||||
|
||||
return array_keys($result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get the structure of an index into an array
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $index_name name of index that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function getTableIndexDefinition($table, $index_name)
|
||||
{
|
||||
$query = "SELECT relname, indkey FROM pg_index, pg_class
|
||||
WHERE pg_class.relname = ".$this->_db->quoteSmart($index_name)." AND pg_class.oid = pg_index.indexrelid
|
||||
AND indisunique != 't' AND indisprimary != 't'";
|
||||
$row = $this->_db->getRow($query, null, DB_FETCHMODE_ASSOC);
|
||||
if (PEAR::isError($row)) {
|
||||
return $row;
|
||||
}
|
||||
|
||||
$columns = $this->_listTableFields($table);
|
||||
|
||||
$definition = array();
|
||||
|
||||
$index_column_numbers = explode(' ', $row['indkey']);
|
||||
|
||||
foreach ($index_column_numbers as $number) {
|
||||
$definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
|
||||
}
|
||||
return $definition;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get the structure of a constraint into an array
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $index_name name of index that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function getTableConstraintDefinition($table, $index_name)
|
||||
{
|
||||
$query = "SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class
|
||||
WHERE pg_class.relname = ".$this->_db->quoteSmart($index_name)." AND pg_class.oid = pg_index.indexrelid
|
||||
AND (indisunique = 't' OR indisprimary = 't')";
|
||||
$row = $this->_db->getRow($query, null, DB_FETCHMODE_ASSOC);
|
||||
if (PEAR::isError($row)) {
|
||||
return $row;
|
||||
}
|
||||
|
||||
$columns = $this->_listTableFields($table);
|
||||
|
||||
$definition = array();
|
||||
if ($row['indisprimary'] == 't') {
|
||||
$definition['primary'] = true;
|
||||
} elseif ($row['indisunique'] == 't') {
|
||||
$definition['unique'] = true;
|
||||
}
|
||||
|
||||
$index_column_numbers = explode(' ', $row['indkey']);
|
||||
|
||||
foreach ($index_column_numbers as $number) {
|
||||
$definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending');
|
||||
}
|
||||
return $definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* list all fields in a tables in the current database
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @return mixed data array on success, a PEAR error on failure
|
||||
* @access private
|
||||
*/
|
||||
function _listTableFields($table)
|
||||
{
|
||||
$table = $this->_db->quoteIdentifier($table);
|
||||
$result2 = $this->_db->query("SELECT * FROM $table");
|
||||
if (PEAR::isError($result2)) {
|
||||
return $result2;
|
||||
}
|
||||
$columns = array();
|
||||
$numcols = $result2->numCols();
|
||||
for ($column = 0; $column < $numcols; $column++) {
|
||||
$column_name = @pg_field_name($result2->result, $column);
|
||||
$columns[$column_name] = $column;
|
||||
}
|
||||
$result2->free();
|
||||
return array_flip($columns);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* drop existing index
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $name name of the index to be dropped
|
||||
* @return mixed DB_OK on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function dropIndex($table, $name)
|
||||
{
|
||||
$name = $this->_db->quoteIdentifier($name);
|
||||
return $this->_db->query("DROP INDEX $name");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* drop existing constraint
|
||||
*
|
||||
* @param string $table name of table that should be used in method
|
||||
* @param string $name name of the constraint to be dropped
|
||||
* @return mixed DB_OK on success, a PEAR error on failure
|
||||
* @access public
|
||||
*/
|
||||
function dropConstraint($table, $name)
|
||||
{
|
||||
$table = $this->_db->quoteIdentifier($table);
|
||||
$name = $this->_db->quoteIdentifier($name);
|
||||
return $this->_db->query("ALTER TABLE $table DROP CONSTRAINT $name");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* alter an existing table
|
||||
*
|
||||
* @param string $name name of the table that is intended to be changed.
|
||||
* @param array $changes associative array that contains the details of each type
|
||||
* of change that is intended to be performed. The types of
|
||||
* changes that are currently supported are defined as follows:
|
||||
*
|
||||
* name
|
||||
*
|
||||
* New name for the table.
|
||||
*
|
||||
* add
|
||||
*
|
||||
* Associative array with the names of fields to be added as
|
||||
* indexes of the array. The value of each entry of the array
|
||||
* should be set to another associative array with the properties
|
||||
* of the fields to be added. The properties of the fields should
|
||||
* be the same as defined by the Metabase parser.
|
||||
*
|
||||
*
|
||||
* remove
|
||||
*
|
||||
* Associative array with the names of fields to be removed as indexes
|
||||
* of the array. Currently the values assigned to each entry are ignored.
|
||||
* An empty array should be used for future compatibility.
|
||||
*
|
||||
* rename
|
||||
*
|
||||
* Associative array with the names of fields to be renamed as indexes
|
||||
* of the array. The value of each entry of the array should be set to
|
||||
* another associative array with the entry named name with the new
|
||||
* field name and the entry named Declaration that is expected to contain
|
||||
* the portion of the field declaration already in DBMS specific SQL code
|
||||
* as it is used in the CREATE TABLE statement.
|
||||
*
|
||||
* change
|
||||
*
|
||||
* Associative array with the names of the fields to be changed as indexes
|
||||
* of the array. Keep in mind that if it is intended to change either the
|
||||
* name of a field and any other properties, the change array entries
|
||||
* should have the new names of the fields as array indexes.
|
||||
*
|
||||
* The value of each entry of the array should be set to another associative
|
||||
* array with the properties of the fields to that are meant to be changed as
|
||||
* array entries. These entries should be assigned to the new values of the
|
||||
* respective properties. The properties of the fields should be the same
|
||||
* as defined by the Metabase parser.
|
||||
*
|
||||
* Example
|
||||
* array(
|
||||
* 'name' => 'userlist',
|
||||
* 'add' => array(
|
||||
* 'quota' => array(
|
||||
* 'type' => 'integer',
|
||||
* 'unsigned' => 1
|
||||
* )
|
||||
* ),
|
||||
* 'remove' => array(
|
||||
* 'file_limit' => array(),
|
||||
* 'time_limit' => array()
|
||||
* ),
|
||||
* 'change' => array(
|
||||
* 'name' => array(
|
||||
* 'length' => '20',
|
||||
* 'definition' => array(
|
||||
* 'type' => 'text',
|
||||
* 'length' => 20,
|
||||
* ),
|
||||
* )
|
||||
* ),
|
||||
* 'rename' => array(
|
||||
* 'sex' => array(
|
||||
* 'name' => 'gender',
|
||||
* 'definition' => array(
|
||||
* 'type' => 'text',
|
||||
* 'length' => 1,
|
||||
* 'default' => 'M',
|
||||
* ),
|
||||
* )
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @param boolean $check (ignored in DB_Table)
|
||||
* @access public
|
||||
*
|
||||
* @return mixed DB_OK on success, a PEAR error on failure
|
||||
*/
|
||||
function alterTable($name, $changes, $check)
|
||||
{
|
||||
foreach ($changes as $change_name => $change) {
|
||||
switch ($change_name) {
|
||||
case 'add':
|
||||
case 'remove':
|
||||
case 'change':
|
||||
case 'name':
|
||||
case 'rename':
|
||||
break;
|
||||
default:
|
||||
return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('add', $changes)) {
|
||||
foreach ($changes['add'] as $field_name => $field) {
|
||||
$query = 'ADD ' . $field_name . ' ' . $field;
|
||||
$result = $this->_db->query("ALTER TABLE $name $query");
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('remove', $changes)) {
|
||||
foreach ($changes['remove'] as $field_name => $field) {
|
||||
$field_name = $this->_db->quoteIdentifier($field_name);
|
||||
$query = 'DROP ' . $field_name;
|
||||
$result = $this->_db->query("ALTER TABLE $name $query");
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('change', $changes)) {
|
||||
foreach ($changes['change'] as $field_name => $field) {
|
||||
$field_name = $this->_db->quoteIdentifier($field_name);
|
||||
if (array_key_exists('type', $field)) {
|
||||
$query = "ALTER $field_name TYPE " . $field['definition'];
|
||||
$result = $this->_db->query("ALTER TABLE $name $query");
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
/* default / notnull changes not (yet) supported in DB_Table
|
||||
if (array_key_exists('default', $field)) {
|
||||
$query = "ALTER $field_name SET DEFAULT ".$db->quote($field['definition']['default'], $field['definition']['type']);
|
||||
$result = $db->exec("ALTER TABLE $name $query");
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
if (array_key_exists('notnull', $field)) {
|
||||
$query.= "ALTER $field_name ".($field['definition']['notnull'] ? "SET" : "DROP").' NOT NULL';
|
||||
$result = $db->exec("ALTER TABLE $name $query");
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('rename', $changes)) {
|
||||
foreach ($changes['rename'] as $field_name => $field) {
|
||||
$field_name = $this->_db->quoteIdentifier($field_name);
|
||||
$result = $this->_db->query("ALTER TABLE $name RENAME COLUMN $field_name TO ".$this->_db->quoteIdentifier($field['name']));
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$name = $this->_db->quoteIdentifier($name);
|
||||
if (array_key_exists('name', $changes)) {
|
||||
$change_name = $this->_db->quoteIdentifier($changes['name']);
|
||||
$result = $this->_db->query("ALTER TABLE $name RENAME TO ".$change_name);
|
||||
if (PEAR::isError($result)) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
return DB_OK;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
1173
sites/all/modules/civicrm/packages/DB/Table/QuickForm.php
Normal file
1173
sites/all/modules/civicrm/packages/DB/Table/QuickForm.php
Normal file
File diff suppressed because it is too large
Load diff
454
sites/all/modules/civicrm/packages/DB/Table/Valid.php
Normal file
454
sites/all/modules/civicrm/packages/DB/Table/Valid.php
Normal file
|
@ -0,0 +1,454 @@
|
|||
<?php
|
||||
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* DB_Table_Valid validates values against DB_Table column types.
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE:
|
||||
*
|
||||
* Copyright (c) 1997-2007, Paul M. Jones <pmjones@php.net>
|
||||
* David C. Morse <morse@php.net>
|
||||
* Mark Wiesemann <wiesemann@php.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * The names of the authors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Paul M. Jones <pmjones@php.net>
|
||||
* @author David C. Morse <morse@php.net>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||
* @version CVS: $Id: Valid.php,v 1.10 2007/12/13 16:52:15 wiesemann Exp $
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
/**
|
||||
* DB_Table class for constants and other globals.
|
||||
*/
|
||||
require_once 'DB/Table.php';
|
||||
|
||||
|
||||
/**
|
||||
* validation ranges for integers
|
||||
*/
|
||||
if (! isset($GLOBALS['_DB_TABLE']['valid'])) {
|
||||
$GLOBALS['_DB_TABLE']['valid'] = array(
|
||||
'smallint' => array(pow(-2, 15), pow(+2, 15) - 1),
|
||||
'integer' => array(pow(-2, 31), pow(+2, 31) - 1),
|
||||
'bigint' => array(pow(-2, 63), pow(+2, 63) - 1)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* DB_Table_Valid validates values against DB_Table column types.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author Paul M. Jones <pmjones@php.net>
|
||||
* @author David C. Morse <morse@php.net>
|
||||
* @author Mark Wiesemann <wiesemann@php.net>
|
||||
* @version Release: 1.5.6
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
class DB_Table_Valid {
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'boolean' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isBoolean($value)
|
||||
{
|
||||
if ($value === true || $value === false) {
|
||||
return true;
|
||||
} elseif (is_numeric($value) && ($value == 0 || $value == 1)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'char' and 'varchar' data type.
|
||||
*
|
||||
* We allow most anything here, only checking that the length is in range.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isChar($value, $colsize)
|
||||
{
|
||||
$is_scalar = (! is_array($value) && ! is_object($value));
|
||||
$in_range = (strlen($value) <= $colsize);
|
||||
return $is_scalar && $in_range;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'smallint' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isSmallint($value)
|
||||
{
|
||||
return is_integer($value) &&
|
||||
($value >= $GLOBALS['_DB_TABLE']['valid']['smallint'][0]) &&
|
||||
($value <= $GLOBALS['_DB_TABLE']['valid']['smallint'][1]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'integer' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isInteger($value)
|
||||
{
|
||||
return is_integer($value) &&
|
||||
($value >= $GLOBALS['_DB_TABLE']['valid']['integer'][0]) &&
|
||||
($value <= $GLOBALS['_DB_TABLE']['valid']['integer'][1]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'bigint' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isBigint($value)
|
||||
{
|
||||
return is_integer($value) &&
|
||||
($value >= $GLOBALS['_DB_TABLE']['valid']['bigint'][0]) &&
|
||||
($value <= $GLOBALS['_DB_TABLE']['valid']['bigint'][1]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'decimal' data type.
|
||||
*
|
||||
* For the column defined "DECIMAL(5,2)" standard SQL requires that
|
||||
* the column be able to store any value with 5 digits and 2
|
||||
* decimals. In this case, therefore, the range of values that can be
|
||||
* stored in the column is from -999.99 to 999.99. DB_Table attempts
|
||||
* to enforce this behavior regardless of the RDBMS backend behavior.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @param string $colsize The 'size' to use for validation (to make
|
||||
* sure of min/max and decimal places).
|
||||
*
|
||||
* @param string $colscope The 'scope' to use for validation (to make
|
||||
* sure of min/max and decimal places).
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isDecimal($value, $colsize, $colscope)
|
||||
{
|
||||
if (! is_numeric($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// maximum number of digits allowed to the left
|
||||
// and right of the decimal point.
|
||||
$right_max = $colscope;
|
||||
$left_max = $colsize - $colscope;
|
||||
|
||||
// ignore negative signs in all validation
|
||||
$value = str_replace('-', '', $value);
|
||||
|
||||
// find the decimal point, then get the left
|
||||
// and right portions.
|
||||
$pos = strpos($value, '.');
|
||||
if ($pos === false) {
|
||||
$left = $value;
|
||||
$right = '';
|
||||
} else {
|
||||
$left = substr($value, 0, $pos);
|
||||
$right = substr($value, $pos+1);
|
||||
}
|
||||
|
||||
// how long are the left and right portions?
|
||||
$left_len = strlen($left);
|
||||
$right_len = strlen($right);
|
||||
|
||||
// do the portions exceed their maxes?
|
||||
if ($left_len > $left_max ||
|
||||
$right_len > $right_max) {
|
||||
// one or the other exceeds the max lengths
|
||||
return false;
|
||||
} else {
|
||||
// both are within parameters
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'single' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isSingle($value)
|
||||
{
|
||||
return is_float($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'double' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isDouble($value)
|
||||
{
|
||||
return is_float($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'time' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isTime($value)
|
||||
{
|
||||
// hh:ii:ss
|
||||
// 01234567
|
||||
$h = substr($value, 0, 2);
|
||||
$s1 = substr($value, 2, 1);
|
||||
$i = substr($value, 3, 2);
|
||||
$s2 = substr($value, 5, 1);
|
||||
$s = substr($value, 6, 2);
|
||||
|
||||
// time check
|
||||
if (strlen($value) != 8 ||
|
||||
! is_numeric($h) || $h < 0 || $h > 23 ||
|
||||
$s1 != ':' ||
|
||||
! is_numeric($i) || $i < 0 || $i > 59 ||
|
||||
$s2 != ':' ||
|
||||
! is_numeric($s) || $s < 0 || $s > 59) {
|
||||
|
||||
return false;
|
||||
|
||||
} else {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'date' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isDate($value)
|
||||
{
|
||||
// yyyy-mm-dd
|
||||
// 0123456789
|
||||
$y = substr($value, 0, 4);
|
||||
$s1 = substr($value, 4, 1);
|
||||
$m = substr($value, 5, 2);
|
||||
$s2 = substr($value, 7, 1);
|
||||
$d = substr($value, 8, 2);
|
||||
|
||||
// date check
|
||||
if (strlen($value) != 10 || $s1 != '-' || $s2 != '-' ||
|
||||
! checkdate($m, $d, $y)) {
|
||||
|
||||
return false;
|
||||
|
||||
} else {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'timestamp' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isTimestamp($value)
|
||||
{
|
||||
// yyyy-mm-dd hh:ii:ss
|
||||
// 0123456789012345678
|
||||
$date = substr($value, 0, 10);
|
||||
$sep = substr($value, 10, 1);
|
||||
$time = substr($value, 11, 8);
|
||||
|
||||
if (strlen($value) != 19 || $sep != ' ' ||
|
||||
! DB_Table_Valid::isDate($date) ||
|
||||
! DB_Table_Valid::isTime($time)) {
|
||||
|
||||
return false;
|
||||
|
||||
} else {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Check if a value validates against the 'clob' data type.
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to validate.
|
||||
*
|
||||
* @return boolean True if the value is valid for the data type, false
|
||||
* if not.
|
||||
*
|
||||
*/
|
||||
|
||||
function isClob($value)
|
||||
{
|
||||
return is_string($value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
111
sites/all/modules/civicrm/packages/DB/Table/XML.php
Normal file
111
sites/all/modules/civicrm/packages/DB/Table/XML.php
Normal file
|
@ -0,0 +1,111 @@
|
|||
<?php
|
||||
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* A few simple static methods for writing XML
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE:
|
||||
*
|
||||
* Copyright (c) 1997-2007, Paul M. Jones <pmjones@php.net>
|
||||
* David C. Morse <morse@php.net>
|
||||
* Mark Wiesemann <wiesemann@php.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * The names of the authors may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author David C. Morse <morse@php.net>
|
||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||
* @version CVS: $Id: XML.php,v 1.2 2007/12/13 16:52:15 wiesemann Exp $
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class DB_Table_XML contains a few simple static methods for writing XML
|
||||
*
|
||||
* @category Database
|
||||
* @package DB_Table
|
||||
* @author David C. Morse <morse@php.net>
|
||||
* @version Release: 1.5.6
|
||||
* @link http://pear.php.net/package/DB_Table
|
||||
*/
|
||||
|
||||
class DB_Table_XML
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns XML closing tag <tag>, increases $indent by 3 spaces
|
||||
*
|
||||
* @static
|
||||
* @param string $tag XML element tag name
|
||||
* @param string $indent current indentation, string of spaces
|
||||
* @return string XML opening tag
|
||||
* @access public
|
||||
*/
|
||||
function openTag($tag, &$indent)
|
||||
{
|
||||
$old_indent = $indent;
|
||||
$indent = $indent . ' ';
|
||||
return $old_indent . "<$tag>";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns XML closing tag </tag>, decreases $indent by 3 spaces
|
||||
*
|
||||
* @static
|
||||
* @param string $tag XML element tag name
|
||||
* @param string $indent current indentation, string of spaces
|
||||
* @return string XML closing tag
|
||||
* @access public
|
||||
*/
|
||||
function closeTag($tag, &$indent)
|
||||
{
|
||||
$indent = substr($indent, 0, -3);
|
||||
return $indent . "</$tag>";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns string single line XML element <tag>text</tag>
|
||||
*
|
||||
* @static
|
||||
* @param string $tag XML element tag name
|
||||
* @param string $text element contents
|
||||
* @param string $indent current indentation, string of spaces
|
||||
* @return string single-line XML element
|
||||
* @access public
|
||||
*/
|
||||
function lineElement($tag, $text, $indent)
|
||||
{
|
||||
return $indent . "<$tag>$text</$tag>";
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue