Cast.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <?php
  2. /**
  3. * Prototype Castable Object.. for DataObject queries
  4. *
  5. * Storage for Data that may be cast into a variety of formats.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * LICENSE: This source file is subject to version 3.01 of the PHP license
  10. * that is available through the world-wide-web at the following URI:
  11. * http://www.php.net/license/3_01.txt. If you did not receive a copy of
  12. * the PHP License and are unable to obtain it through the web, please
  13. * send a note to license@php.net so we can mail you a copy immediately.
  14. *
  15. * @category Database
  16. * @package DB_DataObject
  17. * @author Alan Knowles <alan@akbkhome.com>
  18. * @copyright 1997-2008 The PHP Group
  19. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  20. * @version CVS: $Id: Cast.php 287158 2009-08-12 13:58:31Z alan_k $
  21. * @link http://pear.php.net/package/DB_DataObject
  22. */
  23. /**
  24. *
  25. * Common usages:
  26. * // blobs
  27. * $data = DB_DataObject_Cast::blob($somefile);
  28. * $data = DB_DataObject_Cast::string($somefile);
  29. * $dataObject->someblobfield = $data
  30. *
  31. * // dates?
  32. * $d1 = new DB_DataObject_Cast::date('12/12/2000');
  33. * $d2 = new DB_DataObject_Cast::date(2000,12,30);
  34. * $d3 = new DB_DataObject_Cast::date($d1->year, $d1->month+30, $d1->day+30);
  35. *
  36. * // time, datetime.. ?????????
  37. *
  38. * // raw sql????
  39. * $data = DB_DataObject_Cast::sql('cast("123123",datetime)');
  40. * $data = DB_DataObject_Cast::sql('NULL');
  41. *
  42. * // int's/string etc. are proably pretty pointless..!!!!
  43. *
  44. *
  45. * inside DB_DataObject,
  46. * if (is_a($v,'db_dataobject_class')) {
  47. * $value .= $v->toString(DB_DATAOBJECT_INT,'mysql');
  48. * }
  49. *
  50. *
  51. *
  52. *
  53. */
  54. class DB_DataObject_Cast {
  55. /**
  56. * Type of data Stored in the object..
  57. *
  58. * @var string (date|blob|.....?)
  59. * @access public
  60. */
  61. var $type;
  62. /**
  63. * Data For date representation
  64. *
  65. * @var int day/month/year
  66. * @access public
  67. */
  68. var $day;
  69. var $month;
  70. var $year;
  71. /**
  72. * Generic Data..
  73. *
  74. * @var string
  75. * @access public
  76. */
  77. var $value;
  78. /**
  79. * Blob consructor
  80. *
  81. * create a Cast object from some raw data.. (binary)
  82. *
  83. *
  84. * @param string (with binary data!)
  85. *
  86. * @return object DB_DataObject_Cast
  87. * @access public
  88. */
  89. function blob($value) {
  90. $r = new DB_DataObject_Cast;
  91. $r->type = 'blob';
  92. $r->value = $value;
  93. return $r;
  94. }
  95. /**
  96. * String consructor (actually use if for ints and everything else!!!
  97. *
  98. * create a Cast object from some string (not binary)
  99. *
  100. *
  101. * @param string (with binary data!)
  102. *
  103. * @return object DB_DataObject_Cast
  104. * @access public
  105. */
  106. function string($value) {
  107. $r = new DB_DataObject_Cast;
  108. $r->type = 'string';
  109. $r->value = $value;
  110. return $r;
  111. }
  112. /**
  113. * SQL constructor (for raw SQL insert)
  114. *
  115. * create a Cast object from some sql
  116. *
  117. * @param string (with binary data!)
  118. *
  119. * @return object DB_DataObject_Cast
  120. * @access public
  121. */
  122. function sql($value)
  123. {
  124. $r = new DB_DataObject_Cast;
  125. $r->type = 'sql';
  126. $r->value = $value;
  127. return $r;
  128. }
  129. /**
  130. * Date Constructor
  131. *
  132. * create a Cast object from some string (not binary)
  133. * NO VALIDATION DONE, although some crappy re-calcing done!
  134. *
  135. * @param vargs... accepts
  136. * dd/mm
  137. * dd/mm/yyyy
  138. * yyyy-mm
  139. * yyyy-mm-dd
  140. * array(yyyy,dd)
  141. * array(yyyy,dd,mm)
  142. *
  143. *
  144. *
  145. * @return object DB_DataObject_Cast
  146. * @access public
  147. */
  148. function date()
  149. {
  150. $args = func_get_args();
  151. switch(count($args)) {
  152. case 0: // no args = today!
  153. $bits = explode('-',date('Y-m-d'));
  154. break;
  155. case 1: // one arg = a string
  156. if (strpos($args[0],'/') !== false) {
  157. $bits = array_reverse(explode('/',$args[0]));
  158. } else {
  159. $bits = explode('-',$args[0]);
  160. }
  161. break;
  162. default: // 2 or more..
  163. $bits = $args;
  164. }
  165. if (count($bits) == 1) { // if YYYY set day = 1st..
  166. $bits[] = 1;
  167. }
  168. if (count($bits) == 2) { // if YYYY-DD set day = 1st..
  169. $bits[] = 1;
  170. }
  171. // if year < 1970 we cant use system tools to check it...
  172. // so we make a few best gueses....
  173. // basically do date calculations for the year 2000!!!
  174. // fix me if anyone has more time...
  175. if (($bits[0] < 1975) || ($bits[0] > 2030)) {
  176. $oldyear = $bits[0];
  177. $bits = explode('-',date('Y-m-d',mktime(1,1,1,$bits[1],$bits[2],2000)));
  178. $bits[0] = ($bits[0] - 2000) + $oldyear;
  179. } else {
  180. // now mktime
  181. $bits = explode('-',date('Y-m-d',mktime(1,1,1,$bits[1],$bits[2],$bits[0])));
  182. }
  183. $r = new DB_DataObject_Cast;
  184. $r->type = 'date';
  185. list($r->year,$r->month,$r->day) = $bits;
  186. return $r;
  187. }
  188. /**
  189. * Data For time representation ** does not handle timezones!!
  190. *
  191. * @var int hour/minute/second
  192. * @access public
  193. */
  194. var $hour;
  195. var $minute;
  196. var $second;
  197. /**
  198. * DateTime Constructor
  199. *
  200. * create a Cast object from a Date/Time
  201. * Maybe should accept a Date object.!
  202. * NO VALIDATION DONE, although some crappy re-calcing done!
  203. *
  204. * @param vargs... accepts
  205. * noargs (now)
  206. * yyyy-mm-dd HH:MM:SS (Iso)
  207. * array(yyyy,mm,dd,HH,MM,SS)
  208. *
  209. *
  210. * @return object DB_DataObject_Cast
  211. * @access public
  212. * @author therion 5 at hotmail
  213. */
  214. function dateTime()
  215. {
  216. $args = func_get_args();
  217. switch(count($args)) {
  218. case 0: // no args = now!
  219. $datetime = date('Y-m-d G:i:s', mktime());
  220. case 1:
  221. // continue on from 0 args.
  222. if (!isset($datetime)) {
  223. $datetime = $args[0];
  224. }
  225. $parts = explode(' ', $datetime);
  226. $bits = explode('-', $parts[0]);
  227. $bits = array_merge($bits, explode(':', $parts[1]));
  228. break;
  229. default: // 2 or more..
  230. $bits = $args;
  231. }
  232. if (count($bits) != 6) {
  233. // PEAR ERROR?
  234. return false;
  235. }
  236. $r = DB_DataObject_Cast::date($bits[0], $bits[1], $bits[2]);
  237. if (!$r) {
  238. return $r; // pass thru error (False) - doesnt happen at present!
  239. }
  240. // change the type!
  241. $r->type = 'datetime';
  242. // should we mathematically sort this out..
  243. // (or just assume that no-one's dumb enough to enter 26:90:90 as a time!
  244. $r->hour = $bits[3];
  245. $r->minute = $bits[4];
  246. $r->second = $bits[5];
  247. return $r;
  248. }
  249. /**
  250. * time Constructor
  251. *
  252. * create a Cast object from a Date/Time
  253. * Maybe should accept a Date object.!
  254. * NO VALIDATION DONE, and no-recalcing done!
  255. *
  256. * @param vargs... accepts
  257. * noargs (now)
  258. * HH:MM:SS (Iso)
  259. * array(HH,MM,SS)
  260. *
  261. *
  262. * @return object DB_DataObject_Cast
  263. * @access public
  264. * @author therion 5 at hotmail
  265. */
  266. function time()
  267. {
  268. $args = func_get_args();
  269. switch (count($args)) {
  270. case 0: // no args = now!
  271. $time = date('G:i:s', mktime());
  272. case 1:
  273. // continue on from 0 args.
  274. if (!isset($time)) {
  275. $time = $args[0];
  276. }
  277. $bits = explode(':', $time);
  278. break;
  279. default: // 2 or more..
  280. $bits = $args;
  281. }
  282. if (count($bits) != 3) {
  283. return false;
  284. }
  285. // now take data from bits into object fields
  286. $r = new DB_DataObject_Cast;
  287. $r->type = 'time';
  288. $r->hour = $bits[0];
  289. $r->minute = $bits[1];
  290. $r->second = $bits[2];
  291. return $r;
  292. }
  293. /**
  294. * get the string to use in the SQL statement for this...
  295. *
  296. *
  297. * @param int $to Type (DB_DATAOBJECT_*
  298. * @param object $db DB Connection Object
  299. *
  300. *
  301. * @return string
  302. * @access public
  303. */
  304. function toString($to=false,$db)
  305. {
  306. // if $this->type is not set, we are in serious trouble!!!!
  307. // values for to:
  308. $method = 'toStringFrom'.$this->type;
  309. return $this->$method($to,$db);
  310. }
  311. /**
  312. * get the string to use in the SQL statement from a blob of binary data
  313. * ** Suppots only blob->postgres::bytea
  314. *
  315. * @param int $to Type (DB_DATAOBJECT_*
  316. * @param object $db DB Connection Object
  317. *
  318. *
  319. * @return string
  320. * @access public
  321. */
  322. function toStringFromBlob($to,$db)
  323. {
  324. // first weed out invalid casts..
  325. // in blobs can only be cast to blobs.!
  326. // perhaps we should support TEXT fields???
  327. if (!($to & DB_DATAOBJECT_BLOB)) {
  328. return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::blob to something other than a blob!');
  329. }
  330. switch ($db->dsn["phptype"]) {
  331. case 'pgsql':
  332. return "'".pg_escape_bytea($this->value)."'::bytea";
  333. case 'mysql':
  334. return "'".mysql_real_escape_string($this->value,$db->connection)."'";
  335. case 'mysqli':
  336. // this is funny - the parameter order is reversed ;)
  337. return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
  338. case 'sqlite':
  339. // this is funny - the parameter order is reversed ;)
  340. return "'".sqlite_escape_string($this->value)."'";
  341. case 'mssql':
  342. if(is_numeric($this->value)) {
  343. return $this->value;
  344. }
  345. $unpacked = unpack('H*hex', $this->value);
  346. return '0x' . $unpacked['hex'];
  347. default:
  348. return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
  349. }
  350. }
  351. /**
  352. * get the string to use in the SQL statement for a blob from a string!
  353. * ** Suppots only string->postgres::bytea
  354. *
  355. *
  356. * @param int $to Type (DB_DATAOBJECT_*
  357. * @param object $db DB Connection Object
  358. *
  359. *
  360. * @return string
  361. * @access public
  362. */
  363. function toStringFromString($to,$db)
  364. {
  365. // first weed out invalid casts..
  366. // in blobs can only be cast to blobs.!
  367. // perhaps we should support TEXT fields???
  368. //
  369. // $to == a string field which is the default type (0)
  370. // so we do not test it here. - we assume that number fields
  371. // will accept a string?? - which is stretching it a bit ...
  372. // should probaly add that test as some point.
  373. switch ($db->dsn['phptype']) {
  374. case 'pgsql':
  375. return "'".pg_escape_string($this->value)."'::bytea";
  376. case 'mysql':
  377. return "'".mysql_real_escape_string($this->value,$db->connection)."'";
  378. case 'mysqli':
  379. return "'".mysqli_real_escape_string($db->connection, $this->value)."'";
  380. case 'mssql':
  381. // copied from the old DB mssql code...?? not sure how safe this is.
  382. return "'" . str_replace(
  383. array("'", "\\\r\n", "\\\n"),
  384. array("''", "\\\\\r\n\r\n", "\\\\\n\n"),
  385. $this->value
  386. ) . "'";
  387. default:
  388. return PEAR::raiseError("DB_DataObject_Cast cant handle blobs for Database:{$db->dsn['phptype']} Yet");
  389. }
  390. }
  391. /**
  392. * get the string to use in the SQL statement for a date
  393. *
  394. *
  395. *
  396. * @param int $to Type (DB_DATAOBJECT_*
  397. * @param object $db DB Connection Object
  398. *
  399. *
  400. * @return string
  401. * @access public
  402. */
  403. function toStringFromDate($to,$db)
  404. {
  405. // first weed out invalid casts..
  406. // in blobs can only be cast to blobs.!
  407. // perhaps we should support TEXT fields???
  408. //
  409. if (($to !== false) && !($to & DB_DATAOBJECT_DATE)) {
  410. return PEAR::raiseError('Invalid Cast from a DB_DataObject_Cast::string to something other than a date!'.
  411. ' (why not just use native features)');
  412. }
  413. return "'{$this->year}-{$this->month}-{$this->day}'";
  414. }
  415. /**
  416. * get the string to use in the SQL statement for a datetime
  417. *
  418. *
  419. *
  420. * @param int $to Type (DB_DATAOBJECT_*
  421. * @param object $db DB Connection Object
  422. *
  423. *
  424. * @return string
  425. * @access public
  426. * @author therion 5 at hotmail
  427. */
  428. function toStringFromDateTime($to,$db)
  429. {
  430. // first weed out invalid casts..
  431. // in blobs can only be cast to blobs.!
  432. // perhaps we should support TEXT fields???
  433. if (($to !== false) &&
  434. !($to & (DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME))) {
  435. return PEAR::raiseError('Invalid Cast from a ' .
  436. ' DB_DataObject_Cast::dateTime to something other than a datetime!' .
  437. ' (try using native features)');
  438. }
  439. return "'{$this->year}-{$this->month}-{$this->day} {$this->hour}:{$this->minute}:{$this->second}'";
  440. }
  441. /**
  442. * get the string to use in the SQL statement for a time
  443. *
  444. *
  445. *
  446. * @param int $to Type (DB_DATAOBJECT_*
  447. * @param object $db DB Connection Object
  448. *
  449. *
  450. * @return string
  451. * @access public
  452. * @author therion 5 at hotmail
  453. */
  454. function toStringFromTime($to,$db)
  455. {
  456. // first weed out invalid casts..
  457. // in blobs can only be cast to blobs.!
  458. // perhaps we should support TEXT fields???
  459. if (($to !== false) && !($to & DB_DATAOBJECT_TIME)) {
  460. return PEAR::raiseError('Invalid Cast from a' .
  461. ' DB_DataObject_Cast::time to something other than a time!'.
  462. ' (try using native features)');
  463. }
  464. return "'{$this->hour}:{$this->minute}:{$this->second}'";
  465. }
  466. /**
  467. * get the string to use in the SQL statement for a raw sql statement.
  468. *
  469. * @param int $to Type (DB_DATAOBJECT_*
  470. * @param object $db DB Connection Object
  471. *
  472. *
  473. * @return string
  474. * @access public
  475. */
  476. function toStringFromSql($to,$db)
  477. {
  478. return $this->value;
  479. }
  480. }