common.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Contains the DB_common base class
  5. *
  6. * PHP version 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category Database
  15. * @package DB
  16. * @author Stig Bakken <ssb@php.net>
  17. * @author Tomas V.V. Cox <cox@idecnet.com>
  18. * @author Daniel Convissor <danielc@php.net>
  19. * @copyright 1997-2007 The PHP Group
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version CVS: $Id$
  22. * @link http://pear.php.net/package/DB
  23. */
  24. /**
  25. * Obtain the PEAR class so it can be extended from
  26. */
  27. require_once 'PEAR.php';
  28. /**
  29. * DB_common is the base class from which each database driver class extends
  30. *
  31. * All common methods are declared here. If a given DBMS driver contains
  32. * a particular method, that method will overload the one here.
  33. *
  34. * @category Database
  35. * @package DB
  36. * @author Stig Bakken <ssb@php.net>
  37. * @author Tomas V.V. Cox <cox@idecnet.com>
  38. * @author Daniel Convissor <danielc@php.net>
  39. * @copyright 1997-2007 The PHP Group
  40. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  41. * @version Release: 1.9.2
  42. * @link http://pear.php.net/package/DB
  43. */
  44. class DB_common extends PEAR
  45. {
  46. // {{{ properties
  47. /**
  48. * The current default fetch mode
  49. * @var integer
  50. */
  51. public $fetchmode = DB_FETCHMODE_ORDERED;
  52. /**
  53. * The name of the class into which results should be fetched when
  54. * DB_FETCHMODE_OBJECT is in effect
  55. *
  56. * @var string
  57. */
  58. public $fetchmode_object_class = 'stdClass';
  59. /**
  60. * Was a connection present when the object was serialized()?
  61. * @var bool
  62. * @see DB_common::__sleep(), DB_common::__wake()
  63. */
  64. public $was_connected = null;
  65. /**
  66. * The most recently executed query
  67. * @var string
  68. */
  69. public $last_query = '';
  70. /**
  71. * Run-time configuration options
  72. *
  73. * The 'optimize' option has been deprecated. Use the 'portability'
  74. * option instead.
  75. *
  76. * @var array
  77. * @see DB_common::setOption()
  78. */
  79. public $options = array(
  80. 'result_buffering' => 500,
  81. 'persistent' => false,
  82. 'ssl' => false,
  83. 'debug' => 0,
  84. 'seqname_format' => '%s_seq',
  85. 'autofree' => false,
  86. 'portability' => DB_PORTABILITY_NONE,
  87. 'optimize' => 'performance', // Deprecated. Use 'portability'.
  88. );
  89. /**
  90. * The parameters from the most recently executed query
  91. * @var array
  92. * @since Property available since Release 1.7.0
  93. */
  94. public $last_parameters = array();
  95. /**
  96. * The elements from each prepared statement
  97. * @var array
  98. */
  99. public $prepare_tokens = array();
  100. /**
  101. * The data types of the various elements in each prepared statement
  102. * @var array
  103. */
  104. public $prepare_types = array();
  105. /**
  106. * The prepared queries
  107. * @var array
  108. */
  109. public $prepared_queries = array();
  110. /**
  111. * Flag indicating that the last query was a manipulation query.
  112. * @access protected
  113. * @var boolean
  114. */
  115. public $_last_query_manip = false;
  116. /**
  117. * Flag indicating that the next query <em>must</em> be a manipulation
  118. * query.
  119. * @access protected
  120. * @var boolean
  121. */
  122. public $_next_query_manip = false;
  123. // }}}
  124. // {{{ DB_common
  125. /**
  126. * This constructor calls <kbd>$this->PEAR('DB_Error')</kbd>
  127. *
  128. * @return void
  129. */
  130. public function __construct()
  131. {
  132. $this->PEAR('DB_Error');
  133. }
  134. // }}}
  135. // {{{ __sleep()
  136. /**
  137. * Automatically indicates which properties should be saved
  138. * when PHP's serialize() function is called
  139. *
  140. * @return array the array of properties names that should be saved
  141. */
  142. public function __sleep()
  143. {
  144. if ($this->connection) {
  145. // Don't disconnect(), people use serialize() for many reasons
  146. $this->was_connected = true;
  147. } else {
  148. $this->was_connected = false;
  149. }
  150. if (isset($this->autocommit)) {
  151. return array('autocommit',
  152. 'dbsyntax',
  153. 'dsn',
  154. 'features',
  155. 'fetchmode',
  156. 'fetchmode_object_class',
  157. 'options',
  158. 'was_connected',
  159. );
  160. } else {
  161. return array('dbsyntax',
  162. 'dsn',
  163. 'features',
  164. 'fetchmode',
  165. 'fetchmode_object_class',
  166. 'options',
  167. 'was_connected',
  168. );
  169. }
  170. }
  171. // }}}
  172. // {{{ __wakeup()
  173. /**
  174. * Automatically reconnects to the database when PHP's unserialize()
  175. * function is called
  176. *
  177. * The reconnection attempt is only performed if the object was connected
  178. * at the time PHP's serialize() function was run.
  179. *
  180. * @return void
  181. */
  182. public function __wakeup()
  183. {
  184. if ($this->was_connected) {
  185. $this->connect($this->dsn, $this->options['persistent']);
  186. }
  187. }
  188. // }}}
  189. // {{{ __toString()
  190. /**
  191. * DEPRECATED: String conversion method
  192. *
  193. * @return string a string describing the current PEAR DB object
  194. *
  195. * @deprecated Method deprecated in Release 1.7.0
  196. */
  197. public function toString()
  198. {
  199. return $this->__toString();
  200. }
  201. // }}}
  202. // {{{ toString()
  203. /**
  204. * Automatic string conversion for PHP 5
  205. *
  206. * @return string a string describing the current PEAR DB object
  207. *
  208. * @since Method available since Release 1.7.0
  209. */
  210. public function __toString()
  211. {
  212. $info = strtolower(get_class($this));
  213. $info .= ': (phptype=' . $this->phptype .
  214. ', dbsyntax=' . $this->dbsyntax .
  215. ')';
  216. if ($this->connection) {
  217. $info .= ' [connected]';
  218. }
  219. return $info;
  220. }
  221. // }}}
  222. // {{{ quoteString()
  223. /**
  224. * DEPRECATED: Quotes a string so it can be safely used within string
  225. * delimiters in a query
  226. *
  227. * @param string $string the string to be quoted
  228. *
  229. * @return string the quoted string
  230. *
  231. * @see DB_common::quoteSmart(), DB_common::escapeSimple()
  232. * @deprecated Method deprecated some time before Release 1.2
  233. */
  234. public function quoteString($string)
  235. {
  236. $string = $this->quoteSmart($string);
  237. if ($string{0} == "'") {
  238. return substr($string, 1, -1);
  239. }
  240. return $string;
  241. }
  242. // }}}
  243. // {{{ quote()
  244. /**
  245. * Formats input so it can be safely used in a query
  246. *
  247. * The output depends on the PHP data type of input and the database
  248. * type being used.
  249. *
  250. * @param mixed $in the data to be formatted
  251. *
  252. * @return mixed the formatted data. The format depends on the input's
  253. * PHP type:
  254. * <ul>
  255. * <li>
  256. * <kbd>input</kbd> -> <samp>returns</samp>
  257. * </li>
  258. * <li>
  259. * <kbd>null</kbd> -> the string <samp>NULL</samp>
  260. * </li>
  261. * <li>
  262. * <kbd>integer</kbd> or <kbd>double</kbd> -> the unquoted number
  263. * </li>
  264. * <li>
  265. * <kbd>bool</kbd> -> output depends on the driver in use
  266. * Most drivers return integers: <samp>1</samp> if
  267. * <kbd>true</kbd> or <samp>0</samp> if
  268. * <kbd>false</kbd>.
  269. * Some return strings: <samp>TRUE</samp> if
  270. * <kbd>true</kbd> or <samp>FALSE</samp> if
  271. * <kbd>false</kbd>.
  272. * Finally one returns strings: <samp>T</samp> if
  273. * <kbd>true</kbd> or <samp>F</samp> if
  274. * <kbd>false</kbd>. Here is a list of each DBMS,
  275. * the values returned and the suggested column type:
  276. * <ul>
  277. * <li>
  278. * <kbd>dbase</kbd> -> <samp>T/F</samp>
  279. * (<kbd>Logical</kbd>)
  280. * </li>
  281. * <li>
  282. * <kbd>fbase</kbd> -> <samp>TRUE/FALSE</samp>
  283. * (<kbd>BOOLEAN</kbd>)
  284. * </li>
  285. * <li>
  286. * <kbd>ibase</kbd> -> <samp>1/0</samp>
  287. * (<kbd>SMALLINT</kbd>) [1]
  288. * </li>
  289. * <li>
  290. * <kbd>ifx</kbd> -> <samp>1/0</samp>
  291. * (<kbd>SMALLINT</kbd>) [1]
  292. * </li>
  293. * <li>
  294. * <kbd>msql</kbd> -> <samp>1/0</samp>
  295. * (<kbd>INTEGER</kbd>)
  296. * </li>
  297. * <li>
  298. * <kbd>mssql</kbd> -> <samp>1/0</samp>
  299. * (<kbd>BIT</kbd>)
  300. * </li>
  301. * <li>
  302. * <kbd>mysql</kbd> -> <samp>1/0</samp>
  303. * (<kbd>TINYINT(1)</kbd>)
  304. * </li>
  305. * <li>
  306. * <kbd>mysqli</kbd> -> <samp>1/0</samp>
  307. * (<kbd>TINYINT(1)</kbd>)
  308. * </li>
  309. * <li>
  310. * <kbd>oci8</kbd> -> <samp>1/0</samp>
  311. * (<kbd>NUMBER(1)</kbd>)
  312. * </li>
  313. * <li>
  314. * <kbd>odbc</kbd> -> <samp>1/0</samp>
  315. * (<kbd>SMALLINT</kbd>) [1]
  316. * </li>
  317. * <li>
  318. * <kbd>pgsql</kbd> -> <samp>TRUE/FALSE</samp>
  319. * (<kbd>BOOLEAN</kbd>)
  320. * </li>
  321. * <li>
  322. * <kbd>sqlite</kbd> -> <samp>1/0</samp>
  323. * (<kbd>INTEGER</kbd>)
  324. * </li>
  325. * <li>
  326. * <kbd>sybase</kbd> -> <samp>1/0</samp>
  327. * (<kbd>TINYINT(1)</kbd>)
  328. * </li>
  329. * </ul>
  330. * [1] Accommodate the lowest common denominator because not all
  331. * versions of have <kbd>BOOLEAN</kbd>.
  332. * </li>
  333. * <li>
  334. * other (including strings and numeric strings) ->
  335. * the data with single quotes escaped by preceeding
  336. * single quotes, backslashes are escaped by preceeding
  337. * backslashes, then the whole string is encapsulated
  338. * between single quotes
  339. * </li>
  340. * </ul>
  341. *
  342. * @see DB_common::escapeSimple()
  343. * @since Method available since Release 1.6.0
  344. */
  345. public function quoteSmart($in)
  346. {
  347. if (is_int($in)) {
  348. return $in;
  349. } elseif (is_float($in)) {
  350. return $this->quoteFloat($in);
  351. } elseif (is_bool($in)) {
  352. return $this->quoteBoolean($in);
  353. } elseif (is_null($in)) {
  354. return 'NULL';
  355. } else {
  356. if ($this->dbsyntax == 'access'
  357. && preg_match('/^#.+#$/', $in)) {
  358. return $this->escapeSimple($in);
  359. }
  360. return "'" . $this->escapeSimple($in) . "'";
  361. }
  362. }
  363. // }}}
  364. // {{{ quoteIdentifier()
  365. /**
  366. * Formats a float value for use within a query in a locale-independent
  367. * manner.
  368. *
  369. * @param float the float value to be quoted.
  370. * @return string the quoted string.
  371. * @see DB_common::quoteSmart()
  372. * @since Method available since release 1.7.8.
  373. */
  374. public function quoteFloat($float)
  375. {
  376. return "'" . $this->escapeSimple(str_replace(',', '.', strval(floatval($float)))) . "'";
  377. }
  378. // }}}
  379. // {{{ quoteSmart()
  380. /**
  381. * Escapes a string according to the current DBMS's standards
  382. *
  383. * In SQLite, this makes things safe for inserts/updates, but may
  384. * cause problems when performing text comparisons against columns
  385. * containing binary data. See the
  386. * {@link http://php.net/sqlite_escape_string PHP manual} for more info.
  387. *
  388. * @param string $str the string to be escaped
  389. *
  390. * @return string the escaped string
  391. *
  392. * @see DB_common::quoteSmart()
  393. * @since Method available since Release 1.6.0
  394. */
  395. public function escapeSimple($str)
  396. {
  397. return str_replace("'", "''", $str);
  398. }
  399. // }}}
  400. // {{{ quoteBoolean()
  401. /**
  402. * Formats a boolean value for use within a query in a locale-independent
  403. * manner.
  404. *
  405. * @param boolean the boolean value to be quoted.
  406. * @return string the quoted string.
  407. * @see DB_common::quoteSmart()
  408. * @since Method available since release 1.7.8.
  409. */
  410. public function quoteBoolean($boolean)
  411. {
  412. return $boolean ? '1' : '0';
  413. }
  414. // }}}
  415. // {{{ quoteFloat()
  416. /**
  417. * DEPRECATED: Quotes a string so it can be safely used in a query
  418. *
  419. * @param string $string the string to quote
  420. *
  421. * @return string the quoted string or the string <samp>NULL</samp>
  422. * if the value submitted is <kbd>null</kbd>.
  423. *
  424. * @see DB_common::quoteSmart(), DB_common::escapeSimple()
  425. * @deprecated Deprecated in release 1.6.0
  426. */
  427. public function quote($string = null)
  428. {
  429. return $this->quoteSmart($string);
  430. }
  431. // }}}
  432. // {{{ escapeSimple()
  433. /**
  434. * Quotes a string so it can be safely used as a table or column name
  435. *
  436. * Delimiting style depends on which database driver is being used.
  437. *
  438. * NOTE: just because you CAN use delimited identifiers doesn't mean
  439. * you SHOULD use them. In general, they end up causing way more
  440. * problems than they solve.
  441. *
  442. * Portability is broken by using the following characters inside
  443. * delimited identifiers:
  444. * + backtick (<kbd>`</kbd>) -- due to MySQL
  445. * + double quote (<kbd>"</kbd>) -- due to Oracle
  446. * + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
  447. *
  448. * Delimited identifiers are known to generally work correctly under
  449. * the following drivers:
  450. * + mssql
  451. * + mysql
  452. * + mysqli
  453. * + oci8
  454. * + odbc(access)
  455. * + odbc(db2)
  456. * + pgsql
  457. * + sqlite
  458. * + sybase (must execute <kbd>set quoted_identifier on</kbd> sometime
  459. * prior to use)
  460. *
  461. * InterBase doesn't seem to be able to use delimited identifiers
  462. * via PHP 4. They work fine under PHP 5.
  463. *
  464. * @param string $str the identifier name to be quoted
  465. *
  466. * @return string the quoted identifier
  467. *
  468. * @since Method available since Release 1.6.0
  469. */
  470. public function quoteIdentifier($str)
  471. {
  472. return '"' . str_replace('"', '""', $str) . '"';
  473. }
  474. // }}}
  475. // {{{ provides()
  476. /**
  477. * Tells whether the present driver supports a given feature
  478. *
  479. * @param string $feature the feature you're curious about
  480. *
  481. * @return bool whether this driver supports $feature
  482. */
  483. public function provides($feature)
  484. {
  485. return $this->features[$feature];
  486. }
  487. // }}}
  488. // {{{ setFetchMode()
  489. /**
  490. * Sets the fetch mode that should be used by default for query results
  491. *
  492. * @param integer $fetchmode DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC
  493. * or DB_FETCHMODE_OBJECT
  494. * @param string $object_class the class name of the object to be returned
  495. * by the fetch methods when the
  496. * DB_FETCHMODE_OBJECT mode is selected.
  497. * If no class is specified by default a cast
  498. * to object from the assoc array row will be
  499. * done. There is also the posibility to use
  500. * and extend the 'DB_row' class.
  501. *
  502. * @return object
  503. * @see DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC, DB_FETCHMODE_OBJECT
  504. */
  505. public function setFetchMode($fetchmode, $object_class = 'stdClass')
  506. {
  507. switch ($fetchmode) {
  508. case DB_FETCHMODE_OBJECT:
  509. $this->fetchmode_object_class = $object_class;
  510. // no break
  511. case DB_FETCHMODE_ORDERED:
  512. case DB_FETCHMODE_ASSOC:
  513. $this->fetchmode = $fetchmode;
  514. break;
  515. default:
  516. return $this->raiseError('invalid fetchmode mode');
  517. }
  518. return null;
  519. }
  520. // }}}
  521. // {{{ setOption()
  522. /**
  523. * Communicates an error and invoke error callbacks, etc
  524. *
  525. * Basically a wrapper for PEAR::raiseError without the message string.
  526. *
  527. * @param mixed integer error code, or a PEAR error object (all
  528. * other parameters are ignored if this parameter is
  529. * an object
  530. * @param int error mode, see PEAR_Error docs
  531. * @param mixed if error mode is PEAR_ERROR_TRIGGER, this is the
  532. * error level (E_USER_NOTICE etc). If error mode is
  533. * PEAR_ERROR_CALLBACK, this is the callback function,
  534. * either as a function name, or as an array of an
  535. * object and method name. For other error modes this
  536. * parameter is ignored.
  537. * @param string extra debug information. Defaults to the last
  538. * query and native error code.
  539. * @param mixed native error code, integer or string depending the
  540. * backend
  541. * @param mixed dummy parameter for E_STRICT compatibility with
  542. * PEAR::raiseError
  543. * @param mixed dummy parameter for E_STRICT compatibility with
  544. * PEAR::raiseError
  545. *
  546. * @return object the PEAR_Error object
  547. *
  548. * @see PEAR_Error
  549. */
  550. public function &raiseError(
  551. $code = DB_ERROR,
  552. $mode = null,
  553. $options = null,
  554. $userinfo = null,
  555. $nativecode = null,
  556. $dummy1 = null,
  557. $dummy2 = null
  558. )
  559. {
  560. // The error is yet a DB error object
  561. if (is_object($code)) {
  562. // because we the static PEAR::raiseError, our global
  563. // handler should be used if it is set
  564. if ($mode === null && !empty($this->_default_error_mode)) {
  565. $mode = $this->_default_error_mode;
  566. $options = $this->_default_error_options;
  567. }
  568. $tmp = PEAR::raiseError(
  569. $code,
  570. null,
  571. $mode,
  572. $options,
  573. null,
  574. null,
  575. true
  576. );
  577. return $tmp;
  578. }
  579. if ($userinfo === null) {
  580. $userinfo = $this->last_query;
  581. }
  582. if ($nativecode) {
  583. $userinfo .= ' [nativecode=' . trim($nativecode) . ']';
  584. } else {
  585. $userinfo .= ' [DB Error: ' . DB::errorMessage($code) . ']';
  586. }
  587. $tmp = PEAR::raiseError(
  588. null,
  589. $code,
  590. $mode,
  591. $options,
  592. $userinfo,
  593. 'DB_Error',
  594. true
  595. );
  596. return $tmp;
  597. }
  598. // }}}
  599. // {{{ getOption()
  600. /**
  601. * Sets run-time configuration options for PEAR DB
  602. *
  603. * Options, their data types, default values and description:
  604. * <ul>
  605. * <li>
  606. * <var>autofree</var> <kbd>boolean</kbd> = <samp>false</samp>
  607. * <br />should results be freed automatically when there are no
  608. * more rows?
  609. * </li><li>
  610. * <var>result_buffering</var> <kbd>integer</kbd> = <samp>500</samp>
  611. * <br />how many rows of the result set should be buffered?
  612. * <br />In mysql: mysql_unbuffered_query() is used instead of
  613. * mysql_query() if this value is 0. (Release 1.7.0)
  614. * <br />In oci8: this value is passed to ocisetprefetch().
  615. * (Release 1.7.0)
  616. * </li><li>
  617. * <var>debug</var> <kbd>integer</kbd> = <samp>0</samp>
  618. * <br />debug level
  619. * </li><li>
  620. * <var>persistent</var> <kbd>boolean</kbd> = <samp>false</samp>
  621. * <br />should the connection be persistent?
  622. * </li><li>
  623. * <var>portability</var> <kbd>integer</kbd> = <samp>DB_PORTABILITY_NONE</samp>
  624. * <br />portability mode constant (see below)
  625. * </li><li>
  626. * <var>seqname_format</var> <kbd>string</kbd> = <samp>%s_seq</samp>
  627. * <br />the sprintf() format string used on sequence names. This
  628. * format is applied to sequence names passed to
  629. * createSequence(), nextID() and dropSequence().
  630. * </li><li>
  631. * <var>ssl</var> <kbd>boolean</kbd> = <samp>false</samp>
  632. * <br />use ssl to connect?
  633. * </li>
  634. * </ul>
  635. *
  636. * -----------------------------------------
  637. *
  638. * PORTABILITY MODES
  639. *
  640. * These modes are bitwised, so they can be combined using <kbd>|</kbd>
  641. * and removed using <kbd>^</kbd>. See the examples section below on how
  642. * to do this.
  643. *
  644. * <samp>DB_PORTABILITY_NONE</samp>
  645. * turn off all portability features
  646. *
  647. * This mode gets automatically turned on if the deprecated
  648. * <var>optimize</var> option gets set to <samp>performance</samp>.
  649. *
  650. *
  651. * <samp>DB_PORTABILITY_LOWERCASE</samp>
  652. * convert names of tables and fields to lower case when using
  653. * <kbd>get*()</kbd>, <kbd>fetch*()</kbd> and <kbd>tableInfo()</kbd>
  654. *
  655. * This mode gets automatically turned on in the following databases
  656. * if the deprecated option <var>optimize</var> gets set to
  657. * <samp>portability</samp>:
  658. * + oci8
  659. *
  660. *
  661. * <samp>DB_PORTABILITY_RTRIM</samp>
  662. * right trim the data output by <kbd>get*()</kbd> <kbd>fetch*()</kbd>
  663. *
  664. *
  665. * <samp>DB_PORTABILITY_DELETE_COUNT</samp>
  666. * force reporting the number of rows deleted
  667. *
  668. * Some DBMS's don't count the number of rows deleted when performing
  669. * simple <kbd>DELETE FROM tablename</kbd> queries. This portability
  670. * mode tricks such DBMS's into telling the count by adding
  671. * <samp>WHERE 1=1</samp> to the end of <kbd>DELETE</kbd> queries.
  672. *
  673. * This mode gets automatically turned on in the following databases
  674. * if the deprecated option <var>optimize</var> gets set to
  675. * <samp>portability</samp>:
  676. * + fbsql
  677. * + mysql
  678. * + mysqli
  679. * + sqlite
  680. *
  681. *
  682. * <samp>DB_PORTABILITY_NUMROWS</samp>
  683. * enable hack that makes <kbd>numRows()</kbd> work in Oracle
  684. *
  685. * This mode gets automatically turned on in the following databases
  686. * if the deprecated option <var>optimize</var> gets set to
  687. * <samp>portability</samp>:
  688. * + oci8
  689. *
  690. *
  691. * <samp>DB_PORTABILITY_ERRORS</samp>
  692. * makes certain error messages in certain drivers compatible
  693. * with those from other DBMS's
  694. *
  695. * + mysql, mysqli: change unique/primary key constraints
  696. * DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT
  697. *
  698. * + odbc(access): MS's ODBC driver reports 'no such field' as code
  699. * 07001, which means 'too few parameters.' When this option is on
  700. * that code gets mapped to DB_ERROR_NOSUCHFIELD.
  701. * DB_ERROR_MISMATCH -> DB_ERROR_NOSUCHFIELD
  702. *
  703. * <samp>DB_PORTABILITY_NULL_TO_EMPTY</samp>
  704. * convert null values to empty strings in data output by get*() and
  705. * fetch*(). Needed because Oracle considers empty strings to be null,
  706. * while most other DBMS's know the difference between empty and null.
  707. *
  708. *
  709. * <samp>DB_PORTABILITY_ALL</samp>
  710. * turn on all portability features
  711. *
  712. * -----------------------------------------
  713. *
  714. * Example 1. Simple setOption() example
  715. * <code>
  716. * $db->setOption('autofree', true);
  717. * </code>
  718. *
  719. * Example 2. Portability for lowercasing and trimming
  720. * <code>
  721. * $db->setOption('portability',
  722. * DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_RTRIM);
  723. * </code>
  724. *
  725. * Example 3. All portability options except trimming
  726. * <code>
  727. * $db->setOption('portability',
  728. * DB_PORTABILITY_ALL ^ DB_PORTABILITY_RTRIM);
  729. * </code>
  730. *
  731. * @param string $option option name
  732. * @param mixed $value value for the option
  733. *
  734. * @return int|object
  735. *
  736. * @see DB_common::$options
  737. */
  738. public function setOption($option, $value)
  739. {
  740. if (isset($this->options[$option])) {
  741. $this->options[$option] = $value;
  742. /*
  743. * Backwards compatibility check for the deprecated 'optimize'
  744. * option. Done here in case settings change after connecting.
  745. */
  746. if ($option == 'optimize') {
  747. if ($value == 'portability') {
  748. switch ($this->phptype) {
  749. case 'oci8':
  750. $this->options['portability'] =
  751. DB_PORTABILITY_LOWERCASE |
  752. DB_PORTABILITY_NUMROWS;
  753. break;
  754. case 'fbsql':
  755. case 'mysql':
  756. case 'mysqli':
  757. case 'sqlite':
  758. $this->options['portability'] =
  759. DB_PORTABILITY_DELETE_COUNT;
  760. break;
  761. }
  762. } else {
  763. $this->options['portability'] = DB_PORTABILITY_NONE;
  764. }
  765. }
  766. return DB_OK;
  767. }
  768. return $this->raiseError("unknown option $option");
  769. }
  770. // }}}
  771. // {{{ prepare()
  772. /**
  773. * Automaticaly generates an insert or update query and call prepare()
  774. * and execute() with it
  775. *
  776. * @param string $table the table name
  777. * @param array $fields_values the associative array where $key is a
  778. * field name and $value its value
  779. * @param int $mode a type of query to make:
  780. * DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
  781. * @param bool $where for update queries: the WHERE clause to
  782. * append to the SQL statement. Don't
  783. * include the "WHERE" keyword.
  784. *
  785. * @return mixed a new DB_result object for successful SELECT queries
  786. * or DB_OK for successul data manipulation queries.
  787. * A DB_Error object on failure.
  788. *
  789. * @uses DB_common::autoPrepare(), DB_common::execute()
  790. */
  791. public function autoExecute(
  792. $table,
  793. $fields_values,
  794. $mode = DB_AUTOQUERY_INSERT,
  795. $where = false
  796. )
  797. {
  798. $sth = $this->autoPrepare(
  799. $table,
  800. array_keys($fields_values),
  801. $mode,
  802. $where
  803. );
  804. if (DB::isError($sth)) {
  805. return $sth;
  806. }
  807. $ret = $this->execute($sth, array_values($fields_values));
  808. $this->freePrepared($sth);
  809. return $ret;
  810. }
  811. // }}}
  812. // {{{ autoPrepare()
  813. /**
  814. * Automaticaly generates an insert or update query and pass it to prepare()
  815. *
  816. * @param string $table the table name
  817. * @param array $table_fields the array of field names
  818. * @param int $mode a type of query to make:
  819. * DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
  820. * @param bool $where for update queries: the WHERE clause to
  821. * append to the SQL statement. Don't
  822. * include the "WHERE" keyword.
  823. *
  824. * @return resource|string
  825. *
  826. * @uses DB_common::prepare(), DB_common::buildManipSQL()
  827. */
  828. public function autoPrepare(
  829. $table,
  830. $table_fields,
  831. $mode = DB_AUTOQUERY_INSERT,
  832. $where = false
  833. )
  834. {
  835. $query = $this->buildManipSQL($table, $table_fields, $mode, $where);
  836. if (DB::isError($query)) {
  837. return $query;
  838. }
  839. return $this->prepare($query);
  840. }
  841. // }}}
  842. // {{{ autoExecute()
  843. /**
  844. * Produces an SQL query string for autoPrepare()
  845. *
  846. * Example:
  847. * <pre>
  848. * buildManipSQL('table_sql', array('field1', 'field2', 'field3'),
  849. * DB_AUTOQUERY_INSERT);
  850. * </pre>
  851. *
  852. * That returns
  853. * <samp>
  854. * INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?)
  855. * </samp>
  856. *
  857. * NOTES:
  858. * - This belongs more to a SQL Builder class, but this is a simple
  859. * facility.
  860. * - Be carefull! If you don't give a $where param with an UPDATE
  861. * query, all the records of the table will be updated!
  862. *
  863. * @param string $table the table name
  864. * @param array $table_fields the array of field names
  865. * @param int $mode a type of query to make:
  866. * DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
  867. * @param bool $where for update queries: the WHERE clause to
  868. * append to the SQL statement. Don't
  869. * include the "WHERE" keyword.
  870. *
  871. * @return string the sql query for autoPrepare()
  872. */
  873. public function buildManipSQL($table, $table_fields, $mode, $where = false)
  874. {
  875. if (count($table_fields) == 0) {
  876. return $this->raiseError(DB_ERROR_NEED_MORE_DATA);
  877. }
  878. $first = true;
  879. switch ($mode) {
  880. case DB_AUTOQUERY_INSERT:
  881. $values = '';
  882. $names = '';
  883. foreach ($table_fields as $value) {
  884. if ($first) {
  885. $first = false;
  886. } else {
  887. $names .= ',';
  888. $values .= ',';
  889. }
  890. $names .= $value;
  891. $values .= '?';
  892. }
  893. return "INSERT INTO $table ($names) VALUES ($values)";
  894. case DB_AUTOQUERY_UPDATE:
  895. $set = '';
  896. foreach ($table_fields as $value) {
  897. if ($first) {
  898. $first = false;
  899. } else {
  900. $set .= ',';
  901. }
  902. $set .= "$value = ?";
  903. }
  904. $sql = "UPDATE $table SET $set";
  905. if ($where) {
  906. $sql .= " WHERE $where";
  907. }
  908. return $sql;
  909. default:
  910. return $this->raiseError(DB_ERROR_SYNTAX);
  911. }
  912. }
  913. // }}}
  914. // {{{ buildManipSQL()
  915. /**
  916. * Prepares a query for multiple execution with execute()
  917. *
  918. * Creates a query that can be run multiple times. Each time it is run,
  919. * the placeholders, if any, will be replaced by the contents of
  920. * execute()'s $data argument.
  921. *
  922. * Three types of placeholders can be used:
  923. * + <kbd>?</kbd> scalar value (i.e. strings, integers). The system
  924. * will automatically quote and escape the data.
  925. * + <kbd>!</kbd> value is inserted 'as is'
  926. * + <kbd>&</kbd> requires a file name. The file's contents get
  927. * inserted into the query (i.e. saving binary
  928. * data in a db)
  929. *
  930. * Example 1.
  931. * <code>
  932. * $sth = $db->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)');
  933. * $data = array(
  934. * "John's text",
  935. * "'it''s good'",
  936. * 'filename.txt'
  937. * );
  938. * $res = $db->execute($sth, $data);
  939. * </code>
  940. *
  941. * Use backslashes to escape placeholder characters if you don't want
  942. * them to be interpreted as placeholders:
  943. * <pre>
  944. * "UPDATE foo SET col=? WHERE col='over \& under'"
  945. * </pre>
  946. *
  947. * With some database backends, this is emulated.
  948. *
  949. * {@internal ibase and oci8 have their own prepare() methods.}}
  950. *
  951. * @param string $query the query to be prepared
  952. *
  953. * @return mixed DB statement resource on success. A DB_Error object
  954. * on failure.
  955. *
  956. * @see DB_common::execute()
  957. */
  958. public function prepare($query)
  959. {
  960. $tokens = preg_split(
  961. '/((?<!\\\)[&?!])/',
  962. $query,
  963. -1,
  964. PREG_SPLIT_DELIM_CAPTURE
  965. );
  966. $token = 0;
  967. $types = array();
  968. $newtokens = array();
  969. foreach ($tokens as $val) {
  970. switch ($val) {
  971. case '?':
  972. $types[$token++] = DB_PARAM_SCALAR;
  973. break;
  974. case '&':
  975. $types[$token++] = DB_PARAM_OPAQUE;
  976. break;
  977. case '!':
  978. $types[$token++] = DB_PARAM_MISC;
  979. break;
  980. default:
  981. $newtokens[] = preg_replace('/\\\([&?!])/', "\\1", $val);
  982. }
  983. }
  984. $this->prepare_tokens[] = &$newtokens;
  985. end($this->prepare_tokens);
  986. $k = key($this->prepare_tokens);
  987. $this->prepare_types[$k] = $types;
  988. $this->prepared_queries[$k] = implode(' ', $newtokens);
  989. return $k;
  990. }
  991. // }}}
  992. // {{{ execute()
  993. /**
  994. * Executes a DB statement prepared with prepare()
  995. *
  996. * Example 1.
  997. * <code>
  998. * $sth = $db->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)');
  999. * $data = array(
  1000. * "John's text",
  1001. * "'it''s good'",
  1002. * 'filename.txt'
  1003. * );
  1004. * $res = $db->execute($sth, $data);
  1005. * </code>
  1006. *
  1007. * @param resource $stmt a DB statement resource returned from prepare()
  1008. * @param mixed $data array, string or numeric data to be used in
  1009. * execution of the statement. Quantity of items
  1010. * passed must match quantity of placeholders in
  1011. * query: meaning 1 placeholder for non-array
  1012. * parameters or 1 placeholder per array element.
  1013. *
  1014. * @return mixed a new DB_result object for successful SELECT queries
  1015. * or DB_OK for successul data manipulation queries.
  1016. * A DB_Error object on failure.
  1017. *
  1018. * {@internal ibase and oci8 have their own execute() methods.}}
  1019. *
  1020. * @see DB_common::prepare()
  1021. */
  1022. public function &execute($stmt, $data = array())
  1023. {
  1024. $realquery = $this->executeEmulateQuery($stmt, $data);
  1025. if (DB::isError($realquery)) {
  1026. return $realquery;
  1027. }
  1028. $result = $this->simpleQuery($realquery);
  1029. if ($result === DB_OK || DB::isError($result)) {
  1030. return $result;
  1031. } else {
  1032. $tmp = new DB_result($this, $result);
  1033. return $tmp;
  1034. }
  1035. }
  1036. // }}}
  1037. // {{{ executeEmulateQuery()
  1038. /**
  1039. * Emulates executing prepared statements if the DBMS not support them
  1040. *
  1041. * @param resource $stmt a DB statement resource returned from execute()
  1042. * @param mixed $data array, string or numeric data to be used in
  1043. * execution of the statement. Quantity of items
  1044. * passed must match quantity of placeholders in
  1045. * query: meaning 1 placeholder for non-array
  1046. * parameters or 1 placeholder per array element.
  1047. *
  1048. * @return mixed a string containing the real query run when emulating
  1049. * prepare/execute. A DB_Error object on failure.
  1050. *
  1051. * @access protected
  1052. * @see DB_common::execute()
  1053. */
  1054. public function executeEmulateQuery($stmt, $data = array())
  1055. {
  1056. $stmt = (int)$stmt;
  1057. $data = (array)$data;
  1058. $this->last_parameters = $data;
  1059. if (count($this->prepare_types[$stmt]) != count($data)) {
  1060. $this->last_query = $this->prepared_queries[$stmt];
  1061. return $this->raiseError(DB_ERROR_MISMATCH);
  1062. }
  1063. $realquery = $this->prepare_tokens[$stmt][0];
  1064. $i = 0;
  1065. foreach ($data as $value) {
  1066. if ($this->prepare_types[$stmt][$i] == DB_PARAM_SCALAR) {
  1067. $realquery .= $this->quoteSmart($value);
  1068. } elseif ($this->prepare_types[$stmt][$i] == DB_PARAM_OPAQUE) {
  1069. $fp = @fopen($value, 'rb');
  1070. if (!$fp) {
  1071. return $this->raiseError(DB_ERROR_ACCESS_VIOLATION);
  1072. }
  1073. $realquery .= $this->quoteSmart(fread($fp, filesize($value)));
  1074. fclose($fp);
  1075. } else {
  1076. $realquery .= $value;
  1077. }
  1078. $realquery .= $this->prepare_tokens[$stmt][++$i];
  1079. }
  1080. return $realquery;
  1081. }
  1082. // }}}
  1083. // {{{ executeMultiple()
  1084. /**
  1085. * Frees the internal resources associated with a prepared query
  1086. *
  1087. * @param resource $stmt the prepared statement's PHP resource
  1088. * @param bool $free_resource should the PHP resource be freed too?
  1089. * Use false if you need to get data
  1090. * from the result set later.
  1091. *
  1092. * @return bool TRUE on success, FALSE if $result is invalid
  1093. *
  1094. * @see DB_common::prepare()
  1095. */
  1096. public function freePrepared($stmt, $free_resource = true)
  1097. {
  1098. $stmt = (int)$stmt;
  1099. if (isset($this->prepare_tokens[$stmt])) {
  1100. unset($this->prepare_tokens[$stmt]);
  1101. unset($this->prepare_types[$stmt]);
  1102. unset($this->prepared_queries[$stmt]);
  1103. return true;
  1104. }
  1105. return false;
  1106. }
  1107. // }}}
  1108. // {{{ freePrepared()
  1109. /**
  1110. * Performs several execute() calls on the same statement handle
  1111. *
  1112. * $data must be an array indexed numerically
  1113. * from 0, one execute call is done for every "row" in the array.
  1114. *
  1115. * If an error occurs during execute(), executeMultiple() does not
  1116. * execute the unfinished rows, but rather returns that error.
  1117. *
  1118. * @param resource $stmt query handle from prepare()
  1119. * @param array $data numeric array containing the
  1120. * data to insert into the query
  1121. *
  1122. * @return int DB_OK on success. A DB_Error object on failure.
  1123. *
  1124. * @see DB_common::prepare(), DB_common::execute()
  1125. */
  1126. public function executeMultiple($stmt, $data)
  1127. {
  1128. foreach ($data as $value) {
  1129. $res = $this->execute($stmt, $value);
  1130. if (DB::isError($res)) {
  1131. return $res;
  1132. }
  1133. }
  1134. return DB_OK;
  1135. }
  1136. // }}}
  1137. // {{{ modifyQuery()
  1138. /**
  1139. * Changes a query string for various DBMS specific reasons
  1140. *
  1141. * It is defined here to ensure all drivers have this method available.
  1142. *
  1143. * @param string $query the query string to modify
  1144. *
  1145. * @return string the modified query string
  1146. *
  1147. * @access protected
  1148. * @see DB_mysql::modifyQuery(), DB_oci8::modifyQuery(),
  1149. * DB_sqlite::modifyQuery()
  1150. */
  1151. public function modifyQuery($query)
  1152. {
  1153. return $query;
  1154. }
  1155. // }}}
  1156. // {{{ modifyLimitQuery()
  1157. /**
  1158. * Generates and executes a LIMIT query
  1159. *
  1160. * @param string $query the query
  1161. * @param intr $from the row to start to fetching (0 = the first row)
  1162. * @param int $count the numbers of rows to fetch
  1163. * @param mixed $params array, string or numeric data to be used in
  1164. * execution of the statement. Quantity of items
  1165. * passed must match quantity of placeholders in
  1166. * query: meaning 1 placeholder for non-array
  1167. * parameters or 1 placeholder per array element.
  1168. *
  1169. * @return mixed a new DB_result object for successful SELECT queries
  1170. * or DB_OK for successul data manipulation queries.
  1171. * A DB_Error object on failure.
  1172. */
  1173. public function &limitQuery($query, $from, $count, $params = array())
  1174. {
  1175. $query = $this->modifyLimitQuery($query, $from, $count, $params);
  1176. if (DB::isError($query)) {
  1177. return $query;
  1178. }
  1179. $result = $this->query($query, $params);
  1180. if (is_object($result) && is_a($result, 'DB_result')) {
  1181. $result->setOption('limit_from', $from);
  1182. $result->setOption('limit_count', $count);
  1183. }
  1184. return $result;
  1185. }
  1186. // }}}
  1187. // {{{ query()
  1188. /**
  1189. * Adds LIMIT clauses to a query string according to current DBMS standards
  1190. *
  1191. * It is defined here to assure that all implementations
  1192. * have this method defined.
  1193. *
  1194. * @param string $query the query to modify
  1195. * @param int $from the row to start to fetching (0 = the first row)
  1196. * @param int $count the numbers of rows to fetch
  1197. * @param mixed $params array, string or numeric data to be used in
  1198. * execution of the statement. Quantity of items
  1199. * passed must match quantity of placeholders in
  1200. * query: meaning 1 placeholder for non-array
  1201. * parameters or 1 placeholder per array element.
  1202. *
  1203. * @return string the query string with LIMIT clauses added
  1204. *
  1205. * @access protected
  1206. */
  1207. public function modifyLimitQuery($query, $from, $count, $params = array())
  1208. {
  1209. return $query;
  1210. }
  1211. // }}}
  1212. // {{{ limitQuery()
  1213. /**
  1214. * Sends a query to the database server
  1215. *
  1216. * The query string can be either a normal statement to be sent directly
  1217. * to the server OR if <var>$params</var> are passed the query can have
  1218. * placeholders and it will be passed through prepare() and execute().
  1219. *
  1220. * @param string $query the SQL query or the statement to prepare
  1221. * @param mixed $params array, string or numeric data to be used in
  1222. * execution of the statement. Quantity of items
  1223. * passed must match quantity of placeholders in
  1224. * query: meaning 1 placeholder for non-array
  1225. * parameters or 1 placeholder per array element.
  1226. *
  1227. * @return mixed a new DB_result object for successful SELECT queries
  1228. * or DB_OK for successul data manipulation queries.
  1229. * A DB_Error object on failure.
  1230. *
  1231. * @see DB_result, DB_common::prepare(), DB_common::execute()
  1232. */
  1233. public function &query($query, $params = array())
  1234. {
  1235. $params = (array)$params;
  1236. if (count($params)) {
  1237. $sth = $this->prepare($query);
  1238. if (DB::isError($sth)) {
  1239. return $sth;
  1240. }
  1241. $ret = $this->execute($sth, $params);
  1242. $this->freePrepared($sth, false);
  1243. return $ret;
  1244. } else {
  1245. $this->last_parameters = array();
  1246. $result = $this->simpleQuery($query);
  1247. if ($result === DB_OK || DB::isError($result)) {
  1248. return $result;
  1249. } else {
  1250. $tmp = new DB_result($this, $result);
  1251. return $tmp;
  1252. }
  1253. }
  1254. }
  1255. // }}}
  1256. // {{{ getOne()
  1257. /**
  1258. * Fetches the first column of the first row from a query result
  1259. *
  1260. * Takes care of doing the query and freeing the results when finished.
  1261. *
  1262. * @param string $query the SQL query
  1263. * @param mixed $params array, string or numeric data to be used in
  1264. * execution of the statement. Quantity of items
  1265. * passed must match quantity of placeholders in
  1266. * query: meaning 1 placeholder for non-array
  1267. * parameters or 1 placeholder per array element.
  1268. *
  1269. * @return mixed the returned value of the query.
  1270. * A DB_Error object on failure.
  1271. */
  1272. public function &getOne($query, $params = array())
  1273. {
  1274. $params = (array)$params;
  1275. // modifyLimitQuery() would be nice here, but it causes BC issues
  1276. $params = (array)$params;
  1277. if (count($params)) {
  1278. $sth = $this->prepare($query);
  1279. if (DB::isError($sth)) {
  1280. return $sth;
  1281. }
  1282. $res = $this->execute($sth, $params);
  1283. $this->freePrepared($sth);
  1284. } else {
  1285. $res = $this->query($query);
  1286. }
  1287. if (DB::isError($res)) {
  1288. return $res;
  1289. }
  1290. $err = $res->fetchInto($row, DB_FETCHMODE_ORDERED);
  1291. $res->free();
  1292. if ($err !== DB_OK) {
  1293. return $err;
  1294. }
  1295. return $row[0];
  1296. }
  1297. // }}}
  1298. // {{{ getRow()
  1299. /**
  1300. * Fetches the first row of data returned from a query result
  1301. *
  1302. * Takes care of doing the query and freeing the results when finished.
  1303. *
  1304. * @param string $query the SQL query
  1305. * @param mixed $params array, string or numeric data to be used in
  1306. * execution of the statement. Quantity of items
  1307. * passed must match quantity of placeholders in
  1308. * query: meaning 1 placeholder for non-array
  1309. * parameters or 1 placeholder per array element.
  1310. * @param int $fetchmode the fetch mode to use
  1311. *
  1312. * @return array the first row of results as an array.
  1313. * A DB_Error object on failure.
  1314. */
  1315. public function &getRow(
  1316. $query,
  1317. $params = array(),
  1318. $fetchmode = DB_FETCHMODE_DEFAULT
  1319. )
  1320. {
  1321. // compat check, the params and fetchmode parameters used to
  1322. // have the opposite order
  1323. if (!is_array($params)) {
  1324. if (is_array($fetchmode)) {
  1325. if ($params === null) {
  1326. $tmp = DB_FETCHMODE_DEFAULT;
  1327. } else {
  1328. $tmp = $params;
  1329. }
  1330. $params = $fetchmode;
  1331. $fetchmode = $tmp;
  1332. } elseif ($params !== null) {
  1333. $fetchmode = $params;
  1334. $params = array();
  1335. }
  1336. }
  1337. // modifyLimitQuery() would be nice here, but it causes BC issues
  1338. if (sizeof($params) > 0) {
  1339. $sth = $this->prepare($query);
  1340. if (DB::isError($sth)) {
  1341. return $sth;
  1342. }
  1343. $res = $this->execute($sth, $params);
  1344. $this->freePrepared($sth);
  1345. } else {
  1346. $res = $this->query($query);
  1347. }
  1348. if (DB::isError($res)) {
  1349. return $res;
  1350. }
  1351. $err = $res->fetchInto($row, $fetchmode);
  1352. $res->free();
  1353. if ($err !== DB_OK) {
  1354. return $err;
  1355. }
  1356. return $row;
  1357. }
  1358. // }}}
  1359. // {{{ getCol()
  1360. /**
  1361. * Fetches an entire query result and returns it as an
  1362. * associative array using the first column as the key
  1363. *
  1364. * If the result set contains more than two columns, the value
  1365. * will be an array of the values from column 2-n. If the result
  1366. * set contains only two columns, the returned value will be a
  1367. * scalar with the value of the second column (unless forced to an
  1368. * array with the $force_array parameter). A DB error code is
  1369. * returned on errors. If the result set contains fewer than two
  1370. * columns, a DB_ERROR_TRUNCATED error is returned.
  1371. *
  1372. * For example, if the table "mytable" contains:
  1373. *
  1374. * <pre>
  1375. * ID TEXT DATE
  1376. * --------------------------------
  1377. * 1 'one' 944679408
  1378. * 2 'two' 944679408
  1379. * 3 'three' 944679408
  1380. * </pre>
  1381. *
  1382. * Then the call getAssoc('SELECT id,text FROM mytable') returns:
  1383. * <pre>
  1384. * array(
  1385. * '1' => 'one',
  1386. * '2' => 'two',
  1387. * '3' => 'three',
  1388. * )
  1389. * </pre>
  1390. *
  1391. * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
  1392. * <pre>
  1393. * array(
  1394. * '1' => array('one', '944679408'),
  1395. * '2' => array('two', '944679408'),
  1396. * '3' => array('three', '944679408')
  1397. * )
  1398. * </pre>
  1399. *
  1400. * If the more than one row occurs with the same value in the
  1401. * first column, the last row overwrites all previous ones by
  1402. * default. Use the $group parameter if you don't want to
  1403. * overwrite like this. Example:
  1404. *
  1405. * <pre>
  1406. * getAssoc('SELECT category,id,name FROM mytable', false, null,
  1407. * DB_FETCHMODE_ASSOC, true) returns:
  1408. *
  1409. * array(
  1410. * '1' => array(array('id' => '4', 'name' => 'number four'),
  1411. * array('id' => '6', 'name' => 'number six')
  1412. * ),
  1413. * '9' => array(array('id' => '4', 'name' => 'number four'),
  1414. * array('id' => '6', 'name' => 'number six')
  1415. * )
  1416. * )
  1417. * </pre>
  1418. *
  1419. * Keep in mind that database functions in PHP usually return string
  1420. * values for results regardless of the database's internal type.
  1421. *
  1422. * @param string $query the SQL query
  1423. * @param bool $force_array used only when the query returns
  1424. * exactly two columns. If true, the values
  1425. * of the returned array will be one-element
  1426. * arrays instead of scalars.
  1427. * @param mixed $params array, string or numeric data to be used in
  1428. * execution of the statement. Quantity of
  1429. * items passed must match quantity of
  1430. * placeholders in query: meaning 1
  1431. * placeholder for non-array parameters or
  1432. * 1 placeholder per array element.
  1433. * @param int $fetchmode the fetch mode to use
  1434. * @param bool $group if true, the values of the returned array
  1435. * is wrapped in another array. If the same
  1436. * key value (in the first column) repeats
  1437. * itself, the values will be appended to
  1438. * this array instead of overwriting the
  1439. * existing values.
  1440. *
  1441. * @return array|object
  1442. * A DB_Error object on failure.
  1443. */
  1444. public function &getAssoc(
  1445. $query,
  1446. $force_array = false,
  1447. $params = array(),
  1448. $fetchmode = DB_FETCHMODE_DEFAULT,
  1449. $group = false
  1450. )
  1451. {
  1452. $params = (array)$params;
  1453. if (sizeof($params) > 0) {
  1454. $sth = $this->prepare($query);
  1455. if (DB::isError($sth)) {
  1456. return $sth;
  1457. }
  1458. $res = $this->execute($sth, $params);
  1459. $this->freePrepared($sth);
  1460. } else {
  1461. $res = $this->query($query);
  1462. }
  1463. if (DB::isError($res)) {
  1464. return $res;
  1465. }
  1466. if ($fetchmode == DB_FETCHMODE_DEFAULT) {
  1467. $fetchmode = $this->fetchmode;
  1468. }
  1469. $cols = $res->numCols();
  1470. if ($cols < 2) {
  1471. $tmp = $this->raiseError(DB_ERROR_TRUNCATED);
  1472. return $tmp;
  1473. }
  1474. $results = array();
  1475. if ($cols > 2 || $force_array) {
  1476. // return array values
  1477. // XXX this part can be optimized
  1478. if ($fetchmode == DB_FETCHMODE_ASSOC) {
  1479. while (is_array($row = $res->fetchRow(DB_FETCHMODE_ASSOC))) {
  1480. reset($row);
  1481. $key = current($row);
  1482. unset($row[key($row)]);
  1483. if ($group) {
  1484. $results[$key][] = $row;
  1485. } else {
  1486. $results[$key] = $row;
  1487. }
  1488. }
  1489. } elseif ($fetchmode == DB_FETCHMODE_OBJECT) {
  1490. while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
  1491. $arr = get_object_vars($row);
  1492. $key = current($arr);
  1493. if ($group) {
  1494. $results[$key][] = $row;
  1495. } else {
  1496. $results[$key] = $row;
  1497. }
  1498. }
  1499. } else {
  1500. while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
  1501. // we shift away the first element to get
  1502. // indices running from 0 again
  1503. $key = array_shift($row);
  1504. if ($group) {
  1505. $results[$key][] = $row;
  1506. } else {
  1507. $results[$key] = $row;
  1508. }
  1509. }
  1510. }
  1511. if (DB::isError($row)) {
  1512. $results = $row;
  1513. }
  1514. } else {
  1515. // return scalar values
  1516. while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
  1517. if ($group) {
  1518. $results[$row[0]][] = $row[1];
  1519. } else {
  1520. $results[$row[0]] = $row[1];
  1521. }
  1522. }
  1523. if (DB::isError($row)) {
  1524. $results = $row;
  1525. }
  1526. }
  1527. $res->free();
  1528. return $results;
  1529. }
  1530. // }}}
  1531. // {{{ getAssoc()
  1532. /**
  1533. * Fetches all of the rows from a query result
  1534. *
  1535. * @param string $query the SQL query
  1536. * @param mixed $params array, string or numeric data to be used in
  1537. * execution of the statement. Quantity of
  1538. * items passed must match quantity of
  1539. * placeholders in query: meaning 1
  1540. * placeholder for non-array parameters or
  1541. * 1 placeholder per array element.
  1542. * @param int $fetchmode the fetch mode to use:
  1543. * + DB_FETCHMODE_ORDERED
  1544. * + DB_FETCHMODE_ASSOC
  1545. * + DB_FETCHMODE_ORDERED | DB_FETCHMODE_FLIPPED
  1546. * + DB_FETCHMODE_ASSOC | DB_FETCHMODE_FLIPPED
  1547. *
  1548. * @return array|object
  1549. */
  1550. public function &getAll(
  1551. $query,
  1552. $params = array(),
  1553. $fetchmode = DB_FETCHMODE_DEFAULT
  1554. )
  1555. {
  1556. // compat check, the params and fetchmode parameters used to
  1557. // have the opposite order
  1558. if (!is_array($params)) {
  1559. if (is_array($fetchmode)) {
  1560. if ($params === null) {
  1561. $tmp = DB_FETCHMODE_DEFAULT;
  1562. } else {
  1563. $tmp = $params;
  1564. }
  1565. $params = $fetchmode;
  1566. $fetchmode = $tmp;
  1567. } elseif ($params !== null) {
  1568. $fetchmode = $params;
  1569. $params = array();
  1570. }
  1571. }
  1572. $params = (array)$params;
  1573. if (count($params)) {
  1574. $sth = $this->prepare($query);
  1575. if (DB::isError($sth)) {
  1576. return $sth;
  1577. }
  1578. $res = $this->execute($sth, $params);
  1579. $this->freePrepared($sth);
  1580. } else {
  1581. $res = $this->query($query);
  1582. }
  1583. if ($res === DB_OK || DB::isError($res)) {
  1584. return $res;
  1585. }
  1586. $results = array();
  1587. while (DB_OK === $res->fetchInto($row, $fetchmode)) {
  1588. if ($fetchmode & DB_FETCHMODE_FLIPPED) {
  1589. foreach ($row as $key => $val) {
  1590. $results[$key][] = $val;
  1591. }
  1592. } else {
  1593. $results[] = $row;
  1594. }
  1595. }
  1596. $res->free();
  1597. if (DB::isError($row)) {
  1598. $tmp = $this->raiseError($row);
  1599. return $tmp;
  1600. }
  1601. return $results;
  1602. }
  1603. // }}}
  1604. // {{{ getAll()
  1605. /**
  1606. * Enables or disables automatic commits
  1607. *
  1608. * @param bool $onoff true turns it on, false turns it off
  1609. *
  1610. * @return int|object
  1611. * doesn't support auto-committing transactions.
  1612. */
  1613. public function autoCommit($onoff = false)
  1614. {
  1615. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1616. }
  1617. // }}}
  1618. // {{{ autoCommit()
  1619. /**
  1620. * Commits the current transaction
  1621. *
  1622. * @return int|object
  1623. */
  1624. public function commit()
  1625. {
  1626. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1627. }
  1628. // }}}
  1629. // {{{ commit()
  1630. /**
  1631. * Reverts the current transaction
  1632. *
  1633. * @return int|object
  1634. */
  1635. public function rollback()
  1636. {
  1637. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1638. }
  1639. // }}}
  1640. // {{{ rollback()
  1641. /**
  1642. * Determines the number of rows in a query result
  1643. *
  1644. * @param resource $result the query result idenifier produced by PHP
  1645. *
  1646. * @return int|object
  1647. */
  1648. public function numRows($result)
  1649. {
  1650. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1651. }
  1652. // }}}
  1653. // {{{ numRows()
  1654. /**
  1655. * Determines the number of rows affected by a data maniuplation query
  1656. *
  1657. * 0 is returned for queries that don't manipulate data.
  1658. *
  1659. * @return int|object
  1660. */
  1661. public function affectedRows()
  1662. {
  1663. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1664. }
  1665. // }}}
  1666. // {{{ affectedRows()
  1667. /**
  1668. * Generates the name used inside the database for a sequence
  1669. *
  1670. * The createSequence() docblock contains notes about storing sequence
  1671. * names.
  1672. *
  1673. * @param string $sqn the sequence's public name
  1674. *
  1675. * @return string the sequence's name in the backend
  1676. *
  1677. * @access protected
  1678. * @see DB_common::createSequence(), DB_common::dropSequence(),
  1679. * DB_common::nextID(), DB_common::setOption()
  1680. */
  1681. public function getSequenceName($sqn)
  1682. {
  1683. return sprintf(
  1684. $this->getOption('seqname_format'),
  1685. preg_replace('/[^a-z0-9_.]/i', '_', $sqn)
  1686. );
  1687. }
  1688. // }}}
  1689. // {{{ getSequenceName()
  1690. /**
  1691. * Returns the value of an option
  1692. *
  1693. * @param string $option the option name you're curious about
  1694. *
  1695. * @return mixed the option's value
  1696. */
  1697. public function getOption($option)
  1698. {
  1699. if (isset($this->options[$option])) {
  1700. return $this->options[$option];
  1701. }
  1702. return $this->raiseError("unknown option $option");
  1703. }
  1704. // }}}
  1705. // {{{ nextId()
  1706. /**
  1707. * Returns the next free id in a sequence
  1708. *
  1709. * @param string $seq_name name of the sequence
  1710. * @param boolean $ondemand when true, the seqence is automatically
  1711. * created if it does not exist
  1712. *
  1713. * @return int|object
  1714. * A DB_Error object on failure.
  1715. *
  1716. * @see DB_common::createSequence(), DB_common::dropSequence(),
  1717. * DB_common::getSequenceName()
  1718. */
  1719. public function nextId($seq_name, $ondemand = true)
  1720. {
  1721. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1722. }
  1723. // }}}
  1724. // {{{ createSequence()
  1725. /**
  1726. * Creates a new sequence
  1727. *
  1728. * The name of a given sequence is determined by passing the string
  1729. * provided in the <var>$seq_name</var> argument through PHP's sprintf()
  1730. * function using the value from the <var>seqname_format</var> option as
  1731. * the sprintf()'s format argument.
  1732. *
  1733. * <var>seqname_format</var> is set via setOption().
  1734. *
  1735. * @param string $seq_name name of the new sequence
  1736. *
  1737. * @return int|object
  1738. *
  1739. * @see DB_common::dropSequence(), DB_common::getSequenceName(),
  1740. * DB_common::nextID()
  1741. */
  1742. public function createSequence($seq_name)
  1743. {
  1744. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1745. }
  1746. // }}}
  1747. // {{{ dropSequence()
  1748. /**
  1749. * Deletes a sequence
  1750. *
  1751. * @param string $seq_name name of the sequence to be deleted
  1752. *
  1753. * @return int|object
  1754. *
  1755. * @see DB_common::createSequence(), DB_common::getSequenceName(),
  1756. * DB_common::nextID()
  1757. */
  1758. public function dropSequence($seq_name)
  1759. {
  1760. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1761. }
  1762. // }}}
  1763. // {{{ raiseError()
  1764. /**
  1765. * Gets the DBMS' native error code produced by the last query
  1766. *
  1767. * @return mixed the DBMS' error code. A DB_Error object on failure.
  1768. */
  1769. public function errorNative()
  1770. {
  1771. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1772. }
  1773. // }}}
  1774. // {{{ errorNative()
  1775. /**
  1776. * Maps native error codes to DB's portable ones
  1777. *
  1778. * Uses the <var>$errorcode_map</var> property defined in each driver.
  1779. *
  1780. * @param string|int $nativecode the error code returned by the DBMS
  1781. *
  1782. * @return int the portable DB error code. Return DB_ERROR if the
  1783. * current driver doesn't have a mapping for the
  1784. * $nativecode submitted.
  1785. */
  1786. public function errorCode($nativecode)
  1787. {
  1788. if (isset($this->errorcode_map[$nativecode])) {
  1789. return $this->errorcode_map[$nativecode];
  1790. }
  1791. // Fall back to DB_ERROR if there was no mapping.
  1792. return DB_ERROR;
  1793. }
  1794. // }}}
  1795. // {{{ errorCode()
  1796. /**
  1797. * Maps a DB error code to a textual message
  1798. *
  1799. * @param integer $dbcode the DB error code
  1800. *
  1801. * @return string the error message corresponding to the error code
  1802. * submitted. FALSE if the error code is unknown.
  1803. *
  1804. * @see DB::errorMessage()
  1805. */
  1806. public function errorMessage($dbcode)
  1807. {
  1808. return DB::errorMessage($this->errorcode_map[$dbcode]);
  1809. }
  1810. // }}}
  1811. // {{{ errorMessage()
  1812. /**
  1813. * Returns information about a table or a result set
  1814. *
  1815. * The format of the resulting array depends on which <var>$mode</var>
  1816. * you select. The sample output below is based on this query:
  1817. * <pre>
  1818. * SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
  1819. * FROM tblFoo
  1820. * JOIN tblBar ON tblFoo.fldId = tblBar.fldId
  1821. * </pre>
  1822. *
  1823. * <ul>
  1824. * <li>
  1825. *
  1826. * <kbd>null</kbd> (default)
  1827. * <pre>
  1828. * [0] => Array (
  1829. * [table] => tblFoo
  1830. * [name] => fldId
  1831. * [type] => int
  1832. * [len] => 11
  1833. * [flags] => primary_key not_null
  1834. * )
  1835. * [1] => Array (
  1836. * [table] => tblFoo
  1837. * [name] => fldPhone
  1838. * [type] => string
  1839. * [len] => 20
  1840. * [flags] =>
  1841. * )
  1842. * [2] => Array (
  1843. * [table] => tblBar
  1844. * [name] => fldId
  1845. * [type] => int
  1846. * [len] => 11
  1847. * [flags] => primary_key not_null
  1848. * )
  1849. * </pre>
  1850. *
  1851. * </li><li>
  1852. *
  1853. * <kbd>DB_TABLEINFO_ORDER</kbd>
  1854. *
  1855. * <p>In addition to the information found in the default output,
  1856. * a notation of the number of columns is provided by the
  1857. * <samp>num_fields</samp> element while the <samp>order</samp>
  1858. * element provides an array with the column names as the keys and
  1859. * their location index number (corresponding to the keys in the
  1860. * the default output) as the values.</p>
  1861. *
  1862. * <p>If a result set has identical field names, the last one is
  1863. * used.</p>
  1864. *
  1865. * <pre>
  1866. * [num_fields] => 3
  1867. * [order] => Array (
  1868. * [fldId] => 2
  1869. * [fldTrans] => 1
  1870. * )
  1871. * </pre>
  1872. *
  1873. * </li><li>
  1874. *
  1875. * <kbd>DB_TABLEINFO_ORDERTABLE</kbd>
  1876. *
  1877. * <p>Similar to <kbd>DB_TABLEINFO_ORDER</kbd> but adds more
  1878. * dimensions to the array in which the table names are keys and
  1879. * the field names are sub-keys. This is helpful for queries that
  1880. * join tables which have identical field names.</p>
  1881. *
  1882. * <pre>
  1883. * [num_fields] => 3
  1884. * [ordertable] => Array (
  1885. * [tblFoo] => Array (
  1886. * [fldId] => 0
  1887. * [fldPhone] => 1
  1888. * )
  1889. * [tblBar] => Array (
  1890. * [fldId] => 2
  1891. * )
  1892. * )
  1893. * </pre>
  1894. *
  1895. * </li>
  1896. * </ul>
  1897. *
  1898. * The <samp>flags</samp> element contains a space separated list
  1899. * of extra information about the field. This data is inconsistent
  1900. * between DBMS's due to the way each DBMS works.
  1901. * + <samp>primary_key</samp>
  1902. * + <samp>unique_key</samp>
  1903. * + <samp>multiple_key</samp>
  1904. * + <samp>not_null</samp>
  1905. *
  1906. * Most DBMS's only provide the <samp>table</samp> and <samp>flags</samp>
  1907. * elements if <var>$result</var> is a table name. The following DBMS's
  1908. * provide full information from queries:
  1909. * + fbsql
  1910. * + mysql
  1911. *
  1912. * If the 'portability' option has <samp>DB_PORTABILITY_LOWERCASE</samp>
  1913. * turned on, the names of tables and fields will be lowercased.
  1914. *
  1915. * @param object|string $result DB_result object from a query or a
  1916. * string containing the name of a table.
  1917. * While this also accepts a query result
  1918. * resource identifier, this behavior is
  1919. * deprecated.
  1920. * @param int $mode either unused or one of the tableInfo modes:
  1921. * <kbd>DB_TABLEINFO_ORDERTABLE</kbd>,
  1922. * <kbd>DB_TABLEINFO_ORDER</kbd> or
  1923. * <kbd>DB_TABLEINFO_FULL</kbd> (which does both).
  1924. * These are bitwise, so the first two can be
  1925. * combined using <kbd>|</kbd>.
  1926. *
  1927. * @return array|object
  1928. * A DB_Error object on failure.
  1929. *
  1930. * @see DB_common::setOption()
  1931. */
  1932. public function tableInfo($result, $mode = null)
  1933. {
  1934. /*
  1935. * If the DB_<driver> class has a tableInfo() method, that one
  1936. * overrides this one. But, if the driver doesn't have one,
  1937. * this method runs and tells users about that fact.
  1938. */
  1939. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1940. }
  1941. // }}}
  1942. // {{{ tableInfo()
  1943. /**
  1944. * Lists the tables in the current database
  1945. *
  1946. * @return array the list of tables. A DB_Error object on failure.
  1947. *
  1948. * @deprecated Method deprecated some time before Release 1.2
  1949. */
  1950. public function getTables()
  1951. {
  1952. return $this->getListOf('tables');
  1953. }
  1954. // }}}
  1955. // {{{ getTables()
  1956. /**
  1957. * Lists internal database information
  1958. *
  1959. * @param string $type type of information being sought.
  1960. * Common items being sought are:
  1961. * tables, databases, users, views, functions
  1962. * Each DBMS's has its own capabilities.
  1963. *
  1964. * @return array|object
  1965. * A DB DB_Error object on failure.
  1966. */
  1967. public function getListOf($type)
  1968. {
  1969. $sql = $this->getSpecialQuery($type);
  1970. if ($sql === null) {
  1971. $this->last_query = '';
  1972. return $this->raiseError(DB_ERROR_UNSUPPORTED);
  1973. } elseif (is_int($sql) || DB::isError($sql)) {
  1974. // Previous error
  1975. return $this->raiseError($sql);
  1976. } elseif (is_array($sql)) {
  1977. // Already the result
  1978. return $sql;
  1979. }
  1980. // Launch this query
  1981. return $this->getCol($sql);
  1982. }
  1983. // }}}
  1984. // {{{ getListOf()
  1985. /**
  1986. * Obtains the query string needed for listing a given type of objects
  1987. *
  1988. * @param string $type the kind of objects you want to retrieve
  1989. *
  1990. * @return string the SQL query string or null if the driver doesn't
  1991. * support the object type requested
  1992. *
  1993. * @access protected
  1994. * @see DB_common::getListOf()
  1995. */
  1996. public function getSpecialQuery($type)
  1997. {
  1998. return $this->raiseError(DB_ERROR_UNSUPPORTED);
  1999. }
  2000. // }}}
  2001. // {{{ getSpecialQuery()
  2002. /**
  2003. * Fetches a single column from a query result and returns it as an
  2004. * indexed array
  2005. *
  2006. * @param string $query the SQL query
  2007. * @param mixed $col which column to return (integer [column number,
  2008. * starting at 0] or string [column name])
  2009. * @param mixed $params array, string or numeric data to be used in
  2010. * execution of the statement. Quantity of items
  2011. * passed must match quantity of placeholders in
  2012. * query: meaning 1 placeholder for non-array
  2013. * parameters or 1 placeholder per array element.
  2014. *
  2015. * @return array the results as an array. A DB_Error object on failure.
  2016. *
  2017. * @see DB_common::query()
  2018. */
  2019. public function &getCol($query, $col = 0, $params = array())
  2020. {
  2021. $params = (array)$params;
  2022. if (sizeof($params) > 0) {
  2023. $sth = $this->prepare($query);
  2024. if (DB::isError($sth)) {
  2025. return $sth;
  2026. }
  2027. $res = $this->execute($sth, $params);
  2028. $this->freePrepared($sth);
  2029. } else {
  2030. $res = $this->query($query);
  2031. }
  2032. if (DB::isError($res)) {
  2033. return $res;
  2034. }
  2035. $fetchmode = is_int($col) ? DB_FETCHMODE_ORDERED : DB_FETCHMODE_ASSOC;
  2036. if (!is_array($row = $res->fetchRow($fetchmode))) {
  2037. $ret = array();
  2038. } else {
  2039. if (!array_key_exists($col, $row)) {
  2040. $ret = $this->raiseError(DB_ERROR_NOSUCHFIELD);
  2041. } else {
  2042. $ret = array($row[$col]);
  2043. while (is_array($row = $res->fetchRow($fetchmode))) {
  2044. $ret[] = $row[$col];
  2045. }
  2046. }
  2047. }
  2048. $res->free();
  2049. if (DB::isError($row)) {
  2050. $ret = $row;
  2051. }
  2052. return $ret;
  2053. }
  2054. // }}}
  2055. // {{{ nextQueryIsManip()
  2056. /**
  2057. * Sets (or unsets) a flag indicating that the next query will be a
  2058. * manipulation query, regardless of the usual DB::isManip() heuristics.
  2059. *
  2060. * @param boolean true to set the flag overriding the isManip() behaviour,
  2061. * false to clear it and fall back onto isManip()
  2062. *
  2063. * @return void
  2064. *
  2065. * @access public
  2066. */
  2067. public function nextQueryIsManip($manip)
  2068. {
  2069. $this->_next_query_manip = $manip;
  2070. }
  2071. // }}}
  2072. // {{{ _checkManip()
  2073. /**
  2074. * Checks if the given query is a manipulation query. This also takes into
  2075. * account the _next_query_manip flag and sets the _last_query_manip flag
  2076. * (and resets _next_query_manip) according to the result.
  2077. *
  2078. * @param string The query to check.
  2079. *
  2080. * @return boolean true if the query is a manipulation query, false
  2081. * otherwise
  2082. *
  2083. * @access protected
  2084. */
  2085. public function _checkManip($query)
  2086. {
  2087. if ($this->_next_query_manip || DB::isManip($query)) {
  2088. $this->_last_query_manip = true;
  2089. } else {
  2090. $this->_last_query_manip = false;
  2091. }
  2092. $this->_next_query_manip = false;
  2093. return $this->_last_query_manip;
  2094. }
  2095. // }}}
  2096. // {{{ _rtrimArrayValues()
  2097. /**
  2098. * Right-trims all strings in an array
  2099. *
  2100. * @param array $array the array to be trimmed (passed by reference)
  2101. *
  2102. * @return void
  2103. *
  2104. * @access protected
  2105. */
  2106. public function _rtrimArrayValues(&$array)
  2107. {
  2108. foreach ($array as $key => $value) {
  2109. if (is_string($value)) {
  2110. $array[$key] = rtrim($value);
  2111. }
  2112. }
  2113. }
  2114. // }}}
  2115. // {{{ _convertNullArrayValuesToEmpty()
  2116. /**
  2117. * Converts all null values in an array to empty strings
  2118. *
  2119. * @param array $array the array to be de-nullified (passed by reference)
  2120. *
  2121. * @return void
  2122. *
  2123. * @access protected
  2124. */
  2125. public function _convertNullArrayValuesToEmpty(&$array)
  2126. {
  2127. foreach ($array as $key => $value) {
  2128. if (is_null($value)) {
  2129. $array[$key] = '';
  2130. }
  2131. }
  2132. }
  2133. // }}}
  2134. }
  2135. /*
  2136. * Local variables:
  2137. * tab-width: 4
  2138. * c-basic-offset: 4
  2139. * End:
  2140. */