2008-08-22 14:17:14 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Prototype Castable Object.. for DataObject queries
|
|
|
|
*
|
|
|
|
* Storage for Data that may be cast into a variety of formats.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-10-15 09:49:45 +01:00
|
|
|
* LICENSE: This source file is subject to version 3.01 of the PHP license
|
2008-08-22 14:17:14 +01:00
|
|
|
* that is available through the world-wide-web at the following URI:
|
2009-10-15 09:49:45 +01:00
|
|
|
* http://www.php.net/license/3_01.txt. If you did not receive a copy of
|
2008-08-22 14:17:14 +01:00
|
|
|
* the PHP License and are unable to obtain it through the web, please
|
|
|
|
* send a note to license@php.net so we can mail you a copy immediately.
|
|
|
|
*
|
|
|
|
* @category Database
|
|
|
|
* @package DB_DataObject
|
|
|
|
* @author Alan Knowles <alan@akbkhome.com>
|
2009-07-16 04:35:20 +01:00
|
|
|
* @copyright 1997-2008 The PHP Group
|
2009-10-15 09:49:45 +01:00
|
|
|
* @license http://www.php.net/license/3_01.txt PHP License 3.01
|
2015-02-12 21:17:02 +00:00
|
|
|
* @version CVS: $Id: Cast.php 287158 2009-08-12 13:58:31Z alan_k $
|
2008-08-22 14:17:14 +01:00
|
|
|
* @link http://pear.php.net/package/DB_DataObject
|
|
|
|
*/
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
*
|
|
|
|
* Common usages:
|
|
|
|
* // blobs
|
|
|
|
* $data = DB_DataObject_Cast::blob($somefile);
|
|
|
|
* $data = DB_DataObject_Cast::string($somefile);
|
|
|
|
* $dataObject->someblobfield = $data
|
|
|
|
*
|
|
|
|
* // dates?
|
|
|
|
* $d1 = new DB_DataObject_Cast::date('12/12/2000');
|
|
|
|
* $d2 = new DB_DataObject_Cast::date(2000,12,30);
|
|
|
|
* $d3 = new DB_DataObject_Cast::date($d1->year, $d1->month+30, $d1->day+30);
|
|
|
|
*
|
|
|
|
* // time, datetime.. ?????????
|
|
|
|
*
|
|
|
|
* // raw sql????
|
|
|
|
* $data = DB_DataObject_Cast::sql('cast("123123",datetime)');
|
|
|
|
* $data = DB_DataObject_Cast::sql('NULL');
|
|
|
|
*
|
2019-11-02 00:26:25 +00:00
|
|
|
* // int's/string etc. are probably pretty pointless..!!!!
|
2019-04-27 18:21:14 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* inside DB_DataObject,
|
|
|
|
* if (is_a($v,'db_dataobject_class')) {
|
|
|
|
* $value .= $v->toString(DB_DATAOBJECT_INT,'mysql');
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
|
|
|
|
*/
|
2019-04-16 00:20:20 +01:00
|
|
|
class DB_DataObject_Cast
|
|
|
|
{
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* Type of data Stored in the object..
|
|
|
|
*
|
|
|
|
* @var string (date|blob|.....?)
|
|
|
|
* @access public
|
|
|
|
*/
|
2019-04-16 00:20:20 +01:00
|
|
|
public $type;
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* Data For date representation
|
|
|
|
*
|
|
|
|
* @var int day/month/year
|
|
|
|
* @access public
|
|
|
|
*/
|
2019-04-16 00:20:20 +01:00
|
|
|
public $day;
|
|
|
|
public $month;
|
|
|
|
public $year;
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* Generic Data..
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-16 00:20:20 +01:00
|
|
|
public $value;
|
2019-04-27 18:21:14 +01:00
|
|
|
/**
|
|
|
|
* Data For time representation ** does not handle timezones!!
|
|
|
|
*
|
|
|
|
* @var int hour/minute/second
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $hour;
|
|
|
|
public $minute;
|
|
|
|
public $second;
|
2008-08-22 14:17:14 +01:00
|
|
|
|
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* Blob consructor
|
|
|
|
*
|
|
|
|
* create a Cast object from some raw data.. (binary)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string (with binary data!)
|
|
|
|
*
|
|
|
|
* @return object DB_DataObject_Cast
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
|
2020-01-07 14:16:07 +00:00
|
|
|
public static function blob($value)
|
2019-04-16 00:20:20 +01:00
|
|
|
{
|
2008-08-22 14:17:14 +01:00
|
|
|
$r = new DB_DataObject_Cast;
|
|
|
|
$r->type = 'blob';
|
|
|
|
$r->value = $value;
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* String consructor (actually use if for ints and everything else!!!
|
|
|
|
*
|
|
|
|
* create a Cast object from some string (not binary)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string (with binary data!)
|
|
|
|
*
|
|
|
|
* @return object DB_DataObject_Cast
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
|
2020-01-07 14:16:07 +00:00
|
|
|
public static function string($value)
|
2019-04-16 00:20:20 +01:00
|
|
|
{
|
2008-08-22 14:17:14 +01:00
|
|
|
$r = new DB_DataObject_Cast;
|
|
|
|
$r->type = 'string';
|
|
|
|
$r->value = $value;
|
|
|
|
return $r;
|
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* SQL constructor (for raw SQL insert)
|
|
|
|
*
|
|
|
|
* create a Cast object from some sql
|
|
|
|
*
|
|
|
|
* @param string (with binary data!)
|
|
|
|
*
|
|
|
|
* @return object DB_DataObject_Cast
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
|
2020-01-07 14:16:07 +00:00
|
|
|
public static function sql($value)
|
2008-08-22 14:17:14 +01:00
|
|
|
{
|
|
|
|
$r = new DB_DataObject_Cast;
|
|
|
|
$r->type = 'sql';
|
|
|
|
$r->value = $value;
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
2019-04-27 18:21:14 +01:00
|
|
|
/**
|
|
|
|
* DateTime Constructor
|
|
|
|
*
|
|
|
|
* create a Cast object from a Date/Time
|
|
|
|
* Maybe should accept a Date object.!
|
|
|
|
* NO VALIDATION DONE, although some crappy re-calcing done!
|
|
|
|
*
|
|
|
|
* @param vargs... accepts
|
|
|
|
* noargs (now)
|
|
|
|
* yyyy-mm-dd HH:MM:SS (Iso)
|
|
|
|
* array(yyyy,mm,dd,HH,MM,SS)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return bool|object
|
|
|
|
* @access public
|
|
|
|
* @author therion 5 at hotmail
|
|
|
|
*/
|
|
|
|
|
2020-01-07 14:16:07 +00:00
|
|
|
public static function dateTime()
|
2019-04-27 18:21:14 +01:00
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
switch (count($args)) {
|
|
|
|
case 0: // no args = now!
|
|
|
|
$datetime = date('Y-m-d G:i:s', mktime());
|
|
|
|
|
|
|
|
// no break
|
|
|
|
case 1:
|
|
|
|
// continue on from 0 args.
|
|
|
|
if (!isset($datetime)) {
|
|
|
|
$datetime = $args[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = explode(' ', $datetime);
|
|
|
|
$bits = explode('-', $parts[0]);
|
|
|
|
$bits = array_merge($bits, explode(':', $parts[1]));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // 2 or more..
|
|
|
|
$bits = $args;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($bits) != 6) {
|
|
|
|
// PEAR ERROR?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = DB_DataObject_Cast::date($bits[0], $bits[1], $bits[2]);
|
|
|
|
if (!$r) {
|
|
|
|
return $r; // pass thru error (False) - doesnt happen at present!
|
|
|
|
}
|
|
|
|
// change the type!
|
|
|
|
$r->type = 'datetime';
|
|
|
|
|
|
|
|
// should we mathematically sort this out..
|
|
|
|
// (or just assume that no-one's dumb enough to enter 26:90:90 as a time!
|
|
|
|
$r->hour = $bits[3];
|
|
|
|
$r->minute = $bits[4];
|
|
|
|
$r->second = $bits[5];
|
|
|
|
return $r;
|
|
|
|
}
|
2008-08-22 14:17:14 +01:00
|
|
|
|
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* Date Constructor
|
|
|
|
*
|
|
|
|
* create a Cast object from some string (not binary)
|
|
|
|
* NO VALIDATION DONE, although some crappy re-calcing done!
|
|
|
|
*
|
|
|
|
* @param vargs... accepts
|
|
|
|
* dd/mm
|
|
|
|
* dd/mm/yyyy
|
|
|
|
* yyyy-mm
|
|
|
|
* yyyy-mm-dd
|
|
|
|
* array(yyyy,dd)
|
|
|
|
* array(yyyy,dd,mm)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return object DB_DataObject_Cast
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
|
2020-01-07 14:16:07 +00:00
|
|
|
public static function date()
|
2019-04-16 00:20:20 +01:00
|
|
|
{
|
2008-08-22 14:17:14 +01:00
|
|
|
$args = func_get_args();
|
2019-04-16 00:20:20 +01:00
|
|
|
switch (count($args)) {
|
2008-08-22 14:17:14 +01:00
|
|
|
case 0: // no args = today!
|
2019-04-27 18:21:14 +01:00
|
|
|
$bits = explode('-', date('Y-m-d'));
|
2008-08-22 14:17:14 +01:00
|
|
|
break;
|
2019-04-16 00:20:20 +01:00
|
|
|
case 1: // one arg = a string
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2019-04-16 00:20:20 +01:00
|
|
|
if (strpos($args[0], '/') !== false) {
|
|
|
|
$bits = array_reverse(explode('/', $args[0]));
|
2008-08-22 14:17:14 +01:00
|
|
|
} else {
|
2019-04-16 00:20:20 +01:00
|
|
|
$bits = explode('-', $args[0]);
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // 2 or more..
|
|
|
|
$bits = $args;
|
|
|
|
}
|
|
|
|
if (count($bits) == 1) { // if YYYY set day = 1st..
|
|
|
|
$bits[] = 1;
|
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
if (count($bits) == 2) { // if YYYY-DD set day = 1st..
|
|
|
|
$bits[] = 1;
|
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
// if year < 1970 we cant use system tools to check it...
|
|
|
|
// so we make a few best gueses....
|
|
|
|
// basically do date calculations for the year 2000!!!
|
|
|
|
// fix me if anyone has more time...
|
|
|
|
if (($bits[0] < 1975) || ($bits[0] > 2030)) {
|
|
|
|
$oldyear = $bits[0];
|
2019-04-16 00:20:20 +01:00
|
|
|
$bits = explode('-', date('Y-m-d', mktime(1, 1, 1, $bits[1], $bits[2], 2000)));
|
2008-08-22 14:17:14 +01:00
|
|
|
$bits[0] = ($bits[0] - 2000) + $oldyear;
|
|
|
|
} else {
|
|
|
|
// now mktime
|
2019-04-16 00:20:20 +01:00
|
|
|
$bits = explode('-', date('Y-m-d', mktime(1, 1, 1, $bits[1], $bits[2], $bits[0])));
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|
|
|
|
$r = new DB_DataObject_Cast;
|
|
|
|
$r->type = 'date';
|
2019-04-16 00:20:20 +01:00
|
|
|
list($r->year, $r->month, $r->day) = $bits;
|
2008-08-22 14:17:14 +01:00
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* time Constructor
|
|
|
|
*
|
|
|
|
* create a Cast object from a Date/Time
|
|
|
|
* Maybe should accept a Date object.!
|
|
|
|
* NO VALIDATION DONE, and no-recalcing done!
|
|
|
|
*
|
|
|
|
* @param vargs... accepts
|
|
|
|
* noargs (now)
|
|
|
|
* HH:MM:SS (Iso)
|
|
|
|
* array(HH,MM,SS)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return bool|object
|
|
|
|
* @access public
|
|
|
|
* @author therion 5 at hotmail
|
|
|
|
*/
|
2020-01-07 14:16:07 +00:00
|
|
|
public static function time()
|
2008-08-22 14:17:14 +01:00
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
switch (count($args)) {
|
|
|
|
case 0: // no args = now!
|
|
|
|
$time = date('G:i:s', mktime());
|
2019-04-27 18:21:14 +01:00
|
|
|
|
|
|
|
// no break
|
2008-08-22 14:17:14 +01:00
|
|
|
case 1:
|
|
|
|
// continue on from 0 args.
|
|
|
|
if (!isset($time)) {
|
|
|
|
$time = $args[0];
|
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
$bits = explode(':', $time);
|
2008-08-22 14:17:14 +01:00
|
|
|
break;
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
default: // 2 or more..
|
|
|
|
$bits = $args;
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
if (count($bits) != 3) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
// now take data from bits into object fields
|
|
|
|
$r = new DB_DataObject_Cast;
|
|
|
|
$r->type = 'time';
|
|
|
|
$r->hour = $bits[0];
|
|
|
|
$r->minute = $bits[1];
|
|
|
|
$r->second = $bits[2];
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* get the string to use in the SQL statement for this...
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param bool $to Type (DB_DATAOBJECT_*
|
|
|
|
* @param object $db DB Connection Object
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function toString($to = false, $db)
|
2008-08-22 14:17:14 +01:00
|
|
|
{
|
|
|
|
// if $this->type is not set, we are in serious trouble!!!!
|
|
|
|
// values for to:
|
2019-04-27 18:21:14 +01:00
|
|
|
$method = 'toStringFrom' . $this->type;
|
2019-04-16 00:20:20 +01:00
|
|
|
return $this->$method($to, $db);
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* get the string to use in the SQL statement from a blob of binary data
|
|
|
|
* ** Suppots only blob->postgres::bytea
|
|
|
|
*
|
|
|
|
* @param int $to Type (DB_DATAOBJECT_*
|
|
|
|
* @param object $db DB Connection Object
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
*/
|
2019-04-16 00:20:20 +01:00
|
|
|
public function toStringFromBlob($to, $db)
|
2008-08-22 14:17:14 +01:00
|
|
|
{
|
|
|
|
// first weed out invalid casts..
|
|
|
|
// in blobs can only be cast to blobs.!
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
// perhaps we should support TEXT fields???
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
if (!($to & DB_DATAOBJECT_BLOB)) {
|
2019-04-27 18:21:14 +01:00
|
|
|
return (new PEAR)->raiseError('Invalid Cast from a DB_DataObject_Cast::blob to something other than a blob!');
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
switch ($db->dsn["phptype"]) {
|
|
|
|
case 'pgsql':
|
2019-04-27 18:21:14 +01:00
|
|
|
return "'" . pg_escape_bytea($this->value) . "'::bytea";
|
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
case 'mysql':
|
2019-04-27 18:21:14 +01:00
|
|
|
return "'" . mysql_real_escape_string($this->value, $db->connection) . "'";
|
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
case 'mysqli':
|
|
|
|
// this is funny - the parameter order is reversed ;)
|
2019-04-27 18:21:14 +01:00
|
|
|
return "'" . mysqli_real_escape_string($db->connection, $this->value) . "'";
|
|
|
|
|
2009-07-16 04:35:20 +01:00
|
|
|
case 'sqlite':
|
|
|
|
// this is funny - the parameter order is reversed ;)
|
2019-04-27 18:21:14 +01:00
|
|
|
return "'" . sqlite_escape_string($this->value) . "'";
|
|
|
|
|
2013-08-12 11:32:39 +01:00
|
|
|
case 'mssql':
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2019-04-16 00:20:20 +01:00
|
|
|
if (is_numeric($this->value)) {
|
2013-08-12 11:32:39 +01:00
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
$unpacked = unpack('H*hex', $this->value);
|
|
|
|
return '0x' . $unpacked['hex'];
|
2019-04-27 18:21:14 +01:00
|
|
|
|
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
default:
|
2019-04-27 18:21:14 +01:00
|
|
|
return (new PEAR)->raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* get the string to use in the SQL statement for a blob from a string!
|
|
|
|
* ** Suppots only string->postgres::bytea
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param int $to Type (DB_DATAOBJECT_*
|
|
|
|
* @param object $db DB Connection Object
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
*/
|
2019-04-16 00:20:20 +01:00
|
|
|
public function toStringFromString($to, $db)
|
2008-08-22 14:17:14 +01:00
|
|
|
{
|
|
|
|
// first weed out invalid casts..
|
|
|
|
// in blobs can only be cast to blobs.!
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
// perhaps we should support TEXT fields???
|
2019-04-16 00:20:20 +01:00
|
|
|
//
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2013-08-12 11:32:39 +01:00
|
|
|
// $to == a string field which is the default type (0)
|
|
|
|
// so we do not test it here. - we assume that number fields
|
|
|
|
// will accept a string?? - which is stretching it a bit ...
|
2019-04-16 00:20:20 +01:00
|
|
|
// should probaly add that test as some point.
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
switch ($db->dsn['phptype']) {
|
|
|
|
case 'pgsql':
|
2019-04-27 18:21:14 +01:00
|
|
|
return "'" . pg_escape_string($this->value) . "'::bytea";
|
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
case 'mysql':
|
2019-04-27 18:21:14 +01:00
|
|
|
return "'" . mysql_real_escape_string($this->value, $db->connection) . "'";
|
|
|
|
|
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
case 'mysqli':
|
2019-04-27 18:21:14 +01:00
|
|
|
return "'" . mysqli_real_escape_string($db->connection, $this->value) . "'";
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2013-08-12 11:32:39 +01:00
|
|
|
case 'mssql':
|
|
|
|
// copied from the old DB mssql code...?? not sure how safe this is.
|
|
|
|
return "'" . str_replace(
|
2019-04-27 18:21:14 +01:00
|
|
|
array("'", "\\\r\n", "\\\n"),
|
|
|
|
array("''", "\\\\\r\n\r\n", "\\\\\n\n"),
|
|
|
|
$this->value
|
2013-08-12 11:32:39 +01:00
|
|
|
) . "'";
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2013-08-12 11:32:39 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
default:
|
2019-04-27 18:21:14 +01:00
|
|
|
return (new PEAR)->raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* get the string to use in the SQL statement for a date
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param int $to Type (DB_DATAOBJECT_*
|
|
|
|
* @param object $db DB Connection Object
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
*/
|
2019-04-16 00:20:20 +01:00
|
|
|
public function toStringFromDate($to, $db)
|
2008-08-22 14:17:14 +01:00
|
|
|
{
|
|
|
|
// first weed out invalid casts..
|
|
|
|
// in blobs can only be cast to blobs.!
|
2019-04-16 00:20:20 +01:00
|
|
|
// perhaps we should support TEXT fields???
|
|
|
|
//
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
if (($to !== false) && !($to & DB_DATAOBJECT_DATE)) {
|
2019-04-27 18:21:14 +01:00
|
|
|
return (new PEAR)->raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a date!' .
|
2008-08-22 14:17:14 +01:00
|
|
|
' (why not just use native features)');
|
|
|
|
}
|
|
|
|
return "'{$this->year}-{$this->month}-{$this->day}'";
|
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* get the string to use in the SQL statement for a datetime
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param int $to Type (DB_DATAOBJECT_*
|
|
|
|
* @param object $db DB Connection Object
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
* @author therion 5 at hotmail
|
|
|
|
*/
|
|
|
|
|
2019-04-16 00:20:20 +01:00
|
|
|
public function toStringFromDateTime($to, $db)
|
2008-08-22 14:17:14 +01:00
|
|
|
{
|
|
|
|
// first weed out invalid casts..
|
|
|
|
// in blobs can only be cast to blobs.!
|
|
|
|
// perhaps we should support TEXT fields???
|
2019-04-16 00:20:20 +01:00
|
|
|
if (($to !== false) &&
|
2008-08-22 14:17:14 +01:00
|
|
|
!($to & (DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME))) {
|
2019-04-27 18:21:14 +01:00
|
|
|
return (new PEAR)->raiseError('Invalid Cast from a ' .
|
2008-08-22 14:17:14 +01:00
|
|
|
' DB_DataObject_Cast::dateTime to something other than a datetime!' .
|
|
|
|
' (try using native features)');
|
|
|
|
}
|
|
|
|
return "'{$this->year}-{$this->month}-{$this->day} {$this->hour}:{$this->minute}:{$this->second}'";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* get the string to use in the SQL statement for a time
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param int $to Type (DB_DATAOBJECT_*
|
|
|
|
* @param object $db DB Connection Object
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
* @author therion 5 at hotmail
|
|
|
|
*/
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-16 00:20:20 +01:00
|
|
|
public function toStringFromTime($to, $db)
|
2008-08-22 14:17:14 +01:00
|
|
|
{
|
|
|
|
// first weed out invalid casts..
|
|
|
|
// in blobs can only be cast to blobs.!
|
|
|
|
// perhaps we should support TEXT fields???
|
|
|
|
if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) {
|
2019-04-27 18:21:14 +01:00
|
|
|
return (new PEAR)->raiseError('Invalid Cast from a' .
|
|
|
|
' DB_DataObject_Cast::time to something other than a time!' .
|
2008-08-22 14:17:14 +01:00
|
|
|
' (try using native features)');
|
|
|
|
}
|
|
|
|
return "'{$this->hour}:{$this->minute}:{$this->second}'";
|
|
|
|
}
|
2019-04-27 18:21:14 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-27 18:21:14 +01:00
|
|
|
* get the string to use in the SQL statement for a raw sql statement.
|
|
|
|
*
|
|
|
|
* @param int $to Type (DB_DATAOBJECT_*
|
|
|
|
* @param object $db DB Connection Object
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
*/
|
2019-04-16 00:20:20 +01:00
|
|
|
public function toStringFromSql($to, $db)
|
2008-08-22 14:17:14 +01:00
|
|
|
{
|
2019-04-16 00:20:20 +01:00
|
|
|
return $this->value;
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|
|
|
|
}
|