Ugly patch to maintain old DB handle code working quietly

We have to replace this database engine with a modern one
This commit is contained in:
Diogo Cordeiro
2019-04-27 18:21:14 +01:00
parent d75b5d2f4a
commit daa5f87fd4
21 changed files with 7017 additions and 7099 deletions

View File

@@ -20,84 +20,91 @@
* @version CVS: $Id: Cast.php 287158 2009-08-12 13:58:31Z alan_k $
* @link http://pear.php.net/package/DB_DataObject
*/
/**
*
* 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');
*
* // int's/string etc. are proably pretty pointless..!!!!
*
*
* inside DB_DataObject,
* if (is_a($v,'db_dataobject_class')) {
* $value .= $v->toString(DB_DATAOBJECT_INT,'mysql');
* }
*
*
*
*
*/
/**
*
* 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');
*
* // int's/string etc. are proably pretty pointless..!!!!
*
*
* inside DB_DataObject,
* if (is_a($v,'db_dataobject_class')) {
* $value .= $v->toString(DB_DATAOBJECT_INT,'mysql');
* }
*
*
*
*
*/
class DB_DataObject_Cast
{
/**
* Type of data Stored in the object..
*
* @var string (date|blob|.....?)
* @access public
*/
* Type of data Stored in the object..
*
* @var string (date|blob|.....?)
* @access public
*/
public $type;
/**
* Data For date representation
*
* @var int day/month/year
* @access public
*/
* Data For date representation
*
* @var int day/month/year
* @access public
*/
public $day;
public $month;
public $year;
/**
* Generic Data..
*
* @var string
* @access public
*/
* Generic Data..
*
* @var string
* @access public
*/
public $value;
/**
* Data For time representation ** does not handle timezones!!
*
* @var int hour/minute/second
* @access public
*/
public $hour;
public $minute;
public $second;
/**
* Blob consructor
*
* create a Cast object from some raw data.. (binary)
*
*
* @param string (with binary data!)
*
* @return object DB_DataObject_Cast
* @access public
*/
* Blob consructor
*
* create a Cast object from some raw data.. (binary)
*
*
* @param string (with binary data!)
*
* @return object DB_DataObject_Cast
* @access public
*/
public function blob($value)
{
$r = new DB_DataObject_Cast;
@@ -106,19 +113,18 @@ class DB_DataObject_Cast
return $r;
}
/**
* 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
*/
* 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
*/
public function string($value)
{
$r = new DB_DataObject_Cast;
@@ -126,18 +132,18 @@ class DB_DataObject_Cast
$r->value = $value;
return $r;
}
/**
* 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
*/
* 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
*/
public function sql($value)
{
$r = new DB_DataObject_Cast;
@@ -146,36 +152,97 @@ class DB_DataObject_Cast
return $r;
}
/**
* 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
*/
public function dateTime()
{
$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;
}
/**
* 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
*/
* 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
*/
public function date()
{
$args = func_get_args();
switch (count($args)) {
case 0: // no args = today!
$bits = explode('-', date('Y-m-d'));
$bits = explode('-', date('Y-m-d'));
break;
case 1: // one arg = a string
if (strpos($args[0], '/') !== false) {
$bits = array_reverse(explode('/', $args[0]));
} else {
@@ -188,11 +255,11 @@ class DB_DataObject_Cast
if (count($bits) == 1) { // if YYYY set day = 1st..
$bits[] = 1;
}
if (count($bits) == 2) { // if YYYY-DD set day = 1st..
$bits[] = 1;
}
// 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!!!
@@ -210,126 +277,49 @@ class DB_DataObject_Cast
list($r->year, $r->month, $r->day) = $bits;
return $r;
}
/**
* Data For time representation ** does not handle timezones!!
*
* @var int hour/minute/second
* @access public
*/
public $hour;
public $minute;
public $second;
/**
* 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 object DB_DataObject_Cast
* @access public
* @author therion 5 at hotmail
*/
public function dateTime()
{
$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;
}
/**
* 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 object DB_DataObject_Cast
* @access public
* @author therion 5 at hotmail
*/
* 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
*/
public function time()
{
$args = func_get_args();
switch (count($args)) {
case 0: // no args = now!
$time = date('G:i:s', mktime());
// no break
// no break
case 1:
// continue on from 0 args.
if (!isset($time)) {
$time = $args[0];
}
$bits = explode(':', $time);
$bits = explode(':', $time);
break;
default: // 2 or more..
$bits = $args;
}
if (count($bits) != 3) {
return false;
}
// now take data from bits into object fields
$r = new DB_DataObject_Cast;
$r->type = 'time';
@@ -339,171 +329,169 @@ class DB_DataObject_Cast
return $r;
}
/**
* get the string to use in the SQL statement for this...
*
*
* @param int $to Type (DB_DATAOBJECT_*
* @param object $db DB Connection Object
*
*
* @return string
* @access public
*/
public function toString($to=false, $db)
* 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)
{
// if $this->type is not set, we are in serious trouble!!!!
// values for to:
$method = 'toStringFrom'.$this->type;
$method = 'toStringFrom' . $this->type;
return $this->$method($to, $db);
}
/**
* 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
*/
* 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
*/
public function toStringFromBlob($to, $db)
{
// first weed out invalid casts..
// in blobs can only be cast to blobs.!
// perhaps we should support TEXT fields???
if (!($to & DB_DATAOBJECT_BLOB)) {
return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::blob to something other than a blob!');
return (new PEAR)->raiseError('Invalid Cast from a DB_DataObject_Cast::blob to something other than a blob!');
}
switch ($db->dsn["phptype"]) {
case 'pgsql':
return "'".pg_escape_bytea($this->value)."'::bytea";
return "'" . pg_escape_bytea($this->value) . "'::bytea";
case 'mysql':
return "'".mysql_real_escape_string($this->value, $db->connection)."'";
return "'" . mysql_real_escape_string($this->value, $db->connection) . "'";
case 'mysqli':
// this is funny - the parameter order is reversed ;)
return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
return "'" . mysqli_real_escape_string($db->connection, $this->value) . "'";
case 'sqlite':
// this is funny - the parameter order is reversed ;)
return "'".sqlite_escape_string($this->value)."'";
return "'" . sqlite_escape_string($this->value) . "'";
case 'mssql':
if (is_numeric($this->value)) {
return $this->value;
}
$unpacked = unpack('H*hex', $this->value);
return '0x' . $unpacked['hex'];
default:
return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
return (new PEAR)->raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
}
}
/**
* 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
*/
* 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
*/
public function toStringFromString($to, $db)
{
// first weed out invalid casts..
// in blobs can only be cast to blobs.!
// perhaps we should support TEXT fields???
//
// $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 ...
// should probaly add that test as some point.
switch ($db->dsn['phptype']) {
case 'pgsql':
return "'".pg_escape_string($this->value)."'::bytea";
return "'" . pg_escape_string($this->value) . "'::bytea";
case 'mysql':
return "'".mysql_real_escape_string($this->value, $db->connection)."'";
return "'" . mysql_real_escape_string($this->value, $db->connection) . "'";
case 'mysqli':
return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
return "'" . mysqli_real_escape_string($db->connection, $this->value) . "'";
case 'mssql':
// copied from the old DB mssql code...?? not sure how safe this is.
return "'" . str_replace(
array("'", "\\\r\n", "\\\n"),
array("''", "\\\\\r\n\r\n", "\\\\\n\n"),
$this->value
array("'", "\\\r\n", "\\\n"),
array("''", "\\\\\r\n\r\n", "\\\\\n\n"),
$this->value
) . "'";
default:
return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
return (new PEAR)->raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
}
}
/**
* 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
*/
* 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
*/
public function toStringFromDate($to, $db)
{
// 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_DATE)) {
return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a date!'.
return (new PEAR)->raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a date!' .
' (why not just use native features)');
}
return "'{$this->year}-{$this->month}-{$this->day}'";
}
/**
* 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
*/
* 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
*/
public function toStringFromDateTime($to, $db)
{
// first weed out invalid casts..
@@ -511,7 +499,7 @@ class DB_DataObject_Cast
// perhaps we should support TEXT fields???
if (($to !== false) &&
!($to & (DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME))) {
return PEAR::raiseError('Invalid Cast from a ' .
return (new PEAR)->raiseError('Invalid Cast from a ' .
' DB_DataObject_Cast::dateTime to something other than a datetime!' .
' (try using native features)');
}
@@ -519,18 +507,18 @@ class DB_DataObject_Cast
}
/**
* 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
*/
* 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
*/
public function toStringFromTime($to, $db)
{
@@ -538,23 +526,23 @@ class DB_DataObject_Cast
// in blobs can only be cast to blobs.!
// perhaps we should support TEXT fields???
if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) {
return PEAR::raiseError('Invalid Cast from a' .
' DB_DataObject_Cast::time to something other than a time!'.
return (new PEAR)->raiseError('Invalid Cast from a' .
' DB_DataObject_Cast::time to something other than a time!' .
' (try using native features)');
}
return "'{$this->hour}:{$this->minute}:{$this->second}'";
}
/**
* 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
*/
* 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
*/
public function toStringFromSql($to, $db)
{
return $this->value;

View File

@@ -21,32 +21,32 @@
* @version CVS: $Id: Error.php 287158 2009-08-12 13:58:31Z alan_k $
* @link http://pear.php.net/package/DB_DataObject
*/
class DB_DataObject_Error extends PEAR_Error
{
/**
* DB_DataObject_Error constructor.
*
* @param mixed $code DB error code, or string with error message.
* @param integer $mode what "error mode" to operate in
* @param integer $level what error level to use for $mode & PEAR_ERROR_TRIGGER
* @param mixed $debuginfo additional debug info, such as the last query
*
* @param string $message
* @param mixed $code DB error code, or string with error message.
* @param integer $mode what "error mode" to operate in
* @param integer $level what error level to use for $mode & PEAR_ERROR_TRIGGER
* @access public
*
* @see PEAR_Error
*/
public function DB_DataObject_Error(
public function __construct(
$message = '',
$code = DB_ERROR,
$mode = PEAR_ERROR_RETURN,
$level = E_USER_NOTICE
) {
)
{
$this->PEAR_Error('DB_DataObject Error: ' . $message, $code, $mode, $level);
}
// todo : - support code -> message handling, and translated error messages...
}

File diff suppressed because it is too large Load Diff

View File

@@ -29,8 +29,8 @@
* Currenly only supports existing methods, and new 'link()' method
*
*/
/**
* Links class
*
@@ -39,11 +39,11 @@
class DB_DataObject_Links
{
/**
* @property {DB_DataObject} do DataObject to apply this to.
*/
* @property {DB_DataObject} do DataObject to apply this to.
*/
public $do = false;
/**
* @property {Array|String} load What to load, 'all' or an array of properties. (default all)
*/
@@ -67,180 +67,32 @@ class DB_DataObject_Links
* @property {Boolean} apply apply the result to this object, (default true)
*/
public $apply = true;
//------------------------- RETURN ------------------------------------
/**
* @property {Array} links key value associative array of links.
*/
public $links;
/**
* Constructor
* -- good ole style..
* @param {DB_DataObject} do DataObject to apply to.
* @param {Array} cfg Configuration (basically properties of this object)
* @param {DB_DataObject} do DataObject to apply to.
* @param array $cfg
*/
public function DB_DataObject_Links($do, $cfg= array())
public function __construct($do, $cfg = array())
{
// check if do is set!!!?
$this->do = $do;
foreach ($cfg as $k=>$v) {
foreach ($cfg as $k => $v) {
$this->$k = $v;
}
}
/**
* return name from related object
*
* The relies on a <dbname>.links.ini file, unless you specify the arguments.
*
* you can also use $this->getLink('thisColumnName','otherTable','otherTableColumnName')
*
*
* @param string $field|array either row or row.xxxxx or links spec.
* @param string|DB_DataObject $table (optional) name of table to look up value in
* @param string $link (optional) name of column in other table to match
* @author Tim White <tim@cyface.com>
* @access public
* @return mixed object on success false on failure or '0' when not linked
*/
public function getLink($field, $table= false, $link='')
{
static $cache = array();
// GUESS THE LINKED TABLE.. (if found - recursevly call self)
if ($table == false) {
$info = $this->linkInfo($field);
if ($info) {
return $this->getLink($field, $info[0], $link === false ? $info[1] : $link);
}
// no links defined.. - use borked BC method...
// use the old _ method - this shouldnt happen if called via getLinks()
if (!($p = strpos($field, '_'))) {
return false;
}
$table = substr($field, 0, $p);
return $this->getLink($field, $table);
}
$tn = is_string($table) ? $table : $table->tableName();
if (!isset($this->do->$field)) {
$this->do->raiseError("getLink: row not set $field", DB_DATAOBJECT_ERROR_NODATA);
return false;
}
// check to see if we know anything about this table..
if (empty($this->do->$field) || $this->do->$field < 0) {
return 0; // no record.
}
if ($this->cached && isset($cache[$tn.':'. $link .':'. $this->do->$field])) {
return $cache[$tn.':'. $link .':'. $this->do->$field];
}
$obj = is_string($table) ? $this->do->factory($tn) : $table;
;
if (!is_a($obj, 'DB_DataObject')) {
$this->do->raiseError(
"getLink:Could not find class for row $field, table $tn",
DB_DATAOBJECT_ERROR_INVALIDCONFIG
);
return false;
}
// -1 or 0 -- no referenced record..
$ret = false;
if ($link) {
if ($obj->get($link, $this->do->$field)) {
$ret = $obj;
}
// this really only happens when no link config is set (old BC stuff)
} elseif ($obj->get($this->do->$field)) {
$ret= $obj;
}
if ($this->cached) {
$cache[$tn.':'. $link .':'. $this->do->$field] = $ret;
}
return $ret;
}
/**
* get link information for a field or field specification
*
* alll link (and join methods accept the 'link' info ) in various ways
* string : 'field' = which field to get (uses ???.links.ini to work out what)
* array(2) : 'field', 'table:remote_col' << just like the links.ini def.
* array(3) : 'field', $dataobject, 'remote_col' (handy for joinAdd to do nested joins.)
*
* @param string|array $field or link spec to use.
* @return (false|array) array of dataobject and linked field or false.
*
*
*/
public function linkInfo($field)
{
if (is_array($field)) {
if (count($field) == 3) {
// array with 3 args:
// local_col , dataobject, remote_col
return array(
$field[1],
$field[2],
$field[0]
);
}
list($table, $link) = explode(':', $field[1]);
return array(
$this->do->factory($table),
$link,
$field[0]
);
}
// work out the link.. (classic way)
$links = $this->do->links();
if (empty($links) || !is_array($links)) {
return false;
}
if (!isset($links[$field])) {
return false;
}
list($table, $link) = explode(':', $links[$field]);
//??? needed???
if ($p = strpos($field, ".")) {
$field = substr($field, 0, $p);
}
return array(
$this->do->factory($table),
$link,
$field
);
}
/**
* a generic geter/setter provider..
*
@@ -254,15 +106,15 @@ class DB_DataObject_Links
* $link->link(array('company_id', 'company:id'))
*
*
* @param string|array $field the field to fetch or link spec.
* @params array $args the arguments sent to the getter setter
* @return mixed true of false on set, the object on getter.
*
* @param string|array $field the field to fetch or link spec.
* @param array $args
* @return mixed true of false on set, the object on getter.
* @params array $args the arguments sent to the getter setter
*/
public function link($field, $args = array())
{
$info = $this->linkInfo($field);
if (!$info) {
$this->do->raiseError(
"getLink:Could not find link for row $field",
@@ -271,24 +123,24 @@ class DB_DataObject_Links
return false;
}
$field = $info[2];
if (empty($args)) { // either an empty array or really empty....
if (!isset($this->do->$field)) {
return $info[0]; // empty dataobject.
}
$ret = $this->getLink($field);
// nothing linked -- return new object..
return ($ret === 0) ? $info[0] : $ret;
}
$assign = is_array($args) ? $args[0] : $args;
// otherwise it's a set call..
if (!is_a($assign, 'DB_DataObject')) {
if (is_numeric($assign) && is_integer($assign * 1)) {
if ($assign > 0) {
if ($assign > 0) {
if (!$info) {
return false;
}
@@ -297,19 +149,164 @@ class DB_DataObject_Links
return false;
}
}
$this->do->$field = $assign ;
$this->do->$field = $assign;
return true;
}
return false;
}
// otherwise we are assigning it ...
$this->do->$field = $assign->{$info[1]};
return true;
}
/**
* get link information for a field or field specification
*
* alll link (and join methods accept the 'link' info ) in various ways
* string : 'field' = which field to get (uses ???.links.ini to work out what)
* array(2) : 'field', 'table:remote_col' << just like the links.ini def.
* array(3) : 'field', $dataobject, 'remote_col' (handy for joinAdd to do nested joins.)
*
* @param string|array $field or link spec to use.
* @return array|bool (false|array) array of dataobject and linked field or false.
*
*/
public function linkInfo($field)
{
if (is_array($field)) {
if (count($field) == 3) {
// array with 3 args:
// local_col , dataobject, remote_col
return array(
$field[1],
$field[2],
$field[0]
);
}
list($table, $link) = explode(':', $field[1]);
return array(
$this->do->factory($table),
$link,
$field[0]
);
}
// work out the link.. (classic way)
$links = $this->do->links();
if (empty($links) || !is_array($links)) {
return false;
}
if (!isset($links[$field])) {
return false;
}
list($table, $link) = explode(':', $links[$field]);
//??? needed???
if ($p = strpos($field, ".")) {
$field = substr($field, 0, $p);
}
return array(
$this->do->factory($table),
$link,
$field
);
}
/**
* return name from related object
*
* The relies on a <dbname>.links.ini file, unless you specify the arguments.
*
* you can also use $this->getLink('thisColumnName','otherTable','otherTableColumnName')
*
*
* @param string $field |array either row or row.xxxxx or links spec.
* @param bool $table (optional) name of table to look up value in
* @param string $link (optional) name of column in other table to match
* @return mixed object on success false on failure or '0' when not linked
* @author Tim White <tim@cyface.com>
* @access public
*/
public function getLink($field, $table = false, $link = '')
{
static $cache = array();
// GUESS THE LINKED TABLE.. (if found - recursevly call self)
if ($table == false) {
$info = $this->linkInfo($field);
if ($info) {
return $this->getLink($field, $info[0], $link === false ? $info[1] : $link);
}
// no links defined.. - use borked BC method...
// use the old _ method - this shouldnt happen if called via getLinks()
if (!($p = strpos($field, '_'))) {
return false;
}
$table = substr($field, 0, $p);
return $this->getLink($field, $table);
}
$tn = is_string($table) ? $table : $table->tableName();
if (!isset($this->do->$field)) {
$this->do->raiseError("getLink: row not set $field", DB_DATAOBJECT_ERROR_NODATA);
return false;
}
// check to see if we know anything about this table..
if (empty($this->do->$field) || $this->do->$field < 0) {
return 0; // no record.
}
if ($this->cached && isset($cache[$tn . ':' . $link . ':' . $this->do->$field])) {
return $cache[$tn . ':' . $link . ':' . $this->do->$field];
}
$obj = is_string($table) ? $this->do->factory($tn) : $table;;
if (!is_a($obj, 'DB_DataObject')) {
$this->do->raiseError(
"getLink:Could not find class for row $field, table $tn",
DB_DATAOBJECT_ERROR_INVALIDCONFIG
);
return false;
}
// -1 or 0 -- no referenced record..
$ret = false;
if ($link) {
if ($obj->get($link, $this->do->$field)) {
$ret = $obj;
}
// this really only happens when no link config is set (old BC stuff)
} elseif ($obj->get($this->do->$field)) {
$ret = $obj;
}
if ($this->cached) {
$cache[$tn . ':' . $link . ':' . $this->do->$field] = $ret;
}
return $ret;
}
/**
* load related objects
*
@@ -322,26 +319,26 @@ class DB_DataObject_Links
* object vars the links are stored in by changeing the format parameter
*
*
* @param string format (default _%s) where %s is the table name.
* @param string format (default _%s) where %s is the table name.
* @return boolean , true on success
* @author Tim White <tim@cyface.com>
* @access public
* @return boolean , true on success
*/
public function applyLinks($format = '_%s')
{
// get table will load the options.
if ($this->do->_link_loaded) {
return true;
}
$this->do->_link_loaded = false;
$cols = $this->do->table();
$cols = $this->do->table();
$links = $this->do->links();
$loaded = array();
if ($links) {
foreach ($links as $key => $match) {
list($table, $link) = explode(':', $match);
@@ -350,9 +347,9 @@ class DB_DataObject_Links
if ($p = strpos($key, '.')) {
$key = substr($key, 0, $p);
}
$this->do->$k = $this->getLink($key, $table, $link);
if (is_object($this->do->$k)) {
$loaded[] = $k;
}
@@ -367,14 +364,14 @@ class DB_DataObject_Links
if (!is_null($links)) {
return false;
}
foreach (array_keys($cols) as $key) {
if (!($p = strpos($key, '_'))) {
continue;
}
// does the table exist.
$k =sprintf($format, $key);
$k = sprintf($format, $key);
$this->do->$k = $this->getLink($key);
if (is_object($this->do->$k)) {
$loaded[] = $k;
@@ -383,7 +380,7 @@ class DB_DataObject_Links
$this->do->_link_loaded = $loaded;
return true;
}
/**
* getLinkArray
* Fetch an array of related objects. This should be used in conjunction with a
@@ -392,11 +389,11 @@ class DB_DataObject_Links
* You may also use this with all parameters to specify, the column and related table.
*
* @access public
* @param string $field- either column or column.xxxxx
* @param string $field - either column or column.xxxxx
* @param string $table (optional) name of table to look up value in
* @param string $fkey (optional) fetchall key see DB_DataObject::fetchAll()
* @param string $fval (optional)fetchall val DB_DataObject::fetchAll()
* @param string $fval (optional) fetchall method DB_DataObject::fetchAll()
* @param bool $fkey (optional) fetchall key see DB_DataObject::fetchAll()
* @param bool $fval (optional) fetchall method DB_DataObject::fetchAll()
* @param bool $fmethod
* @return array - array of results (empty array on failure)
*
* Example - Getting the related objects
@@ -409,14 +406,13 @@ class DB_DataObject_Links
* foreach ($children as $child) {
* echo $child->name, '<br />';
* }
*
*/
public function getLinkArray($field, $table = null, $fkey = false, $fval = false, $fmethod = false)
{
$ret = array();
if (!$table) {
$links = $this->do->links();
if (is_array($links)) {
if (!isset($links[$field])) {
// failed..
@@ -430,9 +426,9 @@ class DB_DataObject_Links
}
return $this->getLinkArray($field, substr($field, 0, $p));
}
$c = $this->do->factory($table);
$c = $this->do->factory($table);
if (!is_object($c) || !is_a($c, 'DB_DataObject')) {
$this->do->raiseError(
"getLinkArray:Could not find class for row $field, table $table",

View File

@@ -25,32 +25,32 @@
define('DB_DATAOBJECT_NO_OVERLOAD', 1);
//require_once 'DB/DataObject/Generator.php';
require_once 'DB/DataObject/Generator.php';
require_once 'Generator.php';
if (php_sapi_name() != 'cli') {
PEAR::raiseError("\nERROR: You must turn use the cli sapi to run this", null, PEAR_ERROR_DIE);
(new PEAR)->raiseError("\nERROR: You must turn use the cli sapi to run this", null, PEAR_ERROR_DIE);
}
if (!ini_get('register_argc_argv')) {
PEAR::raiseError("\nERROR: You must turn register_argc_argv On in you php.ini file for this to work\neg.\n\nregister_argc_argv = On\n\n", null, PEAR_ERROR_DIE);
(new PEAR)->raiseError("\nERROR: You must turn register_argc_argv On in you php.ini file for this to work\neg.\n\nregister_argc_argv = On\n\n", null, PEAR_ERROR_DIE);
exit;
}
if (!@$_SERVER['argv'][1]) {
PEAR::raiseError("\nERROR: createTable.php usage:\n\n" .$_SERVER['argv'][0] . " example.ini\n\n", null, PEAR_ERROR_DIE);
(new PEAR)->raiseError("\nERROR: createTable.php usage:\n\n" . $_SERVER['argv'][0] . " example.ini\n\n", null, PEAR_ERROR_DIE);
exit;
}
$config = parse_ini_file($_SERVER['argv'][1], true);
foreach ($config as $class=>$values) {
$options = &PEAR::getStaticProperty($class, 'options');
foreach ($config as $class => $values) {
$options = &(new PEAR)->getStaticProperty($class, 'options');
$options = $values;
}
$options = &PEAR::getStaticProperty('DB_DataObject', 'options');
$options = &(new PEAR)->getStaticProperty('DB_DataObject', 'options');
if (empty($options)) {
PEAR::raiseError("\nERROR: could not read ini file\n\n", null, PEAR_ERROR_DIE);
(new PEAR)->raiseError("\nERROR: could not read ini file\n\n", null, PEAR_ERROR_DIE);
exit;
}
set_time_limit(0);