common.php 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  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. if (sizeof($params) > 0) {
  1236. $sth = $this->prepare($query);
  1237. if (DB::isError($sth)) {
  1238. return $sth;
  1239. }
  1240. $ret = $this->execute($sth, $params);
  1241. $this->freePrepared($sth, false);
  1242. return $ret;
  1243. } else {
  1244. $this->last_parameters = array();
  1245. $result = $this->simpleQuery($query);
  1246. if ($result === DB_OK || DB::isError($result)) {
  1247. return $result;
  1248. } else {
  1249. $tmp = new DB_result($this, $result);
  1250. return $tmp;
  1251. }
  1252. }
  1253. }
  1254. // }}}
  1255. // {{{ getOne()
  1256. /**
  1257. * Fetches the first column of the first row from a query result
  1258. *
  1259. * Takes care of doing the query and freeing the results when finished.
  1260. *
  1261. * @param string $query the SQL query
  1262. * @param mixed $params array, string or numeric data to be used in
  1263. * execution of the statement. Quantity of items
  1264. * passed must match quantity of placeholders in
  1265. * query: meaning 1 placeholder for non-array
  1266. * parameters or 1 placeholder per array element.
  1267. *
  1268. * @return mixed the returned value of the query.
  1269. * A DB_Error object on failure.
  1270. */
  1271. public function &getOne($query, $params = array())
  1272. {
  1273. $params = (array)$params;
  1274. // modifyLimitQuery() would be nice here, but it causes BC issues
  1275. if (sizeof($params) > 0) {
  1276. $sth = $this->prepare($query);
  1277. if (DB::isError($sth)) {
  1278. return $sth;
  1279. }
  1280. $res = $this->execute($sth, $params);
  1281. $this->freePrepared($sth);
  1282. } else {
  1283. $res = $this->query($query);
  1284. }
  1285. if (DB::isError($res)) {
  1286. return $res;
  1287. }
  1288. $err = $res->fetchInto($row, DB_FETCHMODE_ORDERED);
  1289. $res->free();
  1290. if ($err !== DB_OK) {
  1291. return $err;
  1292. }
  1293. return $row[0];
  1294. }
  1295. // }}}
  1296. // {{{ getRow()
  1297. /**
  1298. * Fetches the first row of data returned from a query result
  1299. *
  1300. * Takes care of doing the query and freeing the results when finished.
  1301. *
  1302. * @param string $query the SQL query
  1303. * @param mixed $params array, string or numeric data to be used in
  1304. * execution of the statement. Quantity of items
  1305. * passed must match quantity of placeholders in
  1306. * query: meaning 1 placeholder for non-array
  1307. * parameters or 1 placeholder per array element.
  1308. * @param int $fetchmode the fetch mode to use
  1309. *
  1310. * @return array the first row of results as an array.
  1311. * A DB_Error object on failure.
  1312. */
  1313. public function &getRow(
  1314. $query,
  1315. $params = array(),
  1316. $fetchmode = DB_FETCHMODE_DEFAULT
  1317. )
  1318. {
  1319. // compat check, the params and fetchmode parameters used to
  1320. // have the opposite order
  1321. if (!is_array($params)) {
  1322. if (is_array($fetchmode)) {
  1323. if ($params === null) {
  1324. $tmp = DB_FETCHMODE_DEFAULT;
  1325. } else {
  1326. $tmp = $params;
  1327. }
  1328. $params = $fetchmode;
  1329. $fetchmode = $tmp;
  1330. } elseif ($params !== null) {
  1331. $fetchmode = $params;
  1332. $params = array();
  1333. }
  1334. }
  1335. // modifyLimitQuery() would be nice here, but it causes BC issues
  1336. if (sizeof($params) > 0) {
  1337. $sth = $this->prepare($query);
  1338. if (DB::isError($sth)) {
  1339. return $sth;
  1340. }
  1341. $res = $this->execute($sth, $params);
  1342. $this->freePrepared($sth);
  1343. } else {
  1344. $res = $this->query($query);
  1345. }
  1346. if (DB::isError($res)) {
  1347. return $res;
  1348. }
  1349. $err = $res->fetchInto($row, $fetchmode);
  1350. $res->free();
  1351. if ($err !== DB_OK) {
  1352. return $err;
  1353. }
  1354. return $row;
  1355. }
  1356. // }}}
  1357. // {{{ getCol()
  1358. /**
  1359. * Fetches an entire query result and returns it as an
  1360. * associative array using the first column as the key
  1361. *
  1362. * If the result set contains more than two columns, the value
  1363. * will be an array of the values from column 2-n. If the result
  1364. * set contains only two columns, the returned value will be a
  1365. * scalar with the value of the second column (unless forced to an
  1366. * array with the $force_array parameter). A DB error code is
  1367. * returned on errors. If the result set contains fewer than two
  1368. * columns, a DB_ERROR_TRUNCATED error is returned.
  1369. *
  1370. * For example, if the table "mytable" contains:
  1371. *
  1372. * <pre>
  1373. * ID TEXT DATE
  1374. * --------------------------------
  1375. * 1 'one' 944679408
  1376. * 2 'two' 944679408
  1377. * 3 'three' 944679408
  1378. * </pre>
  1379. *
  1380. * Then the call getAssoc('SELECT id,text FROM mytable') returns:
  1381. * <pre>
  1382. * array(
  1383. * '1' => 'one',
  1384. * '2' => 'two',
  1385. * '3' => 'three',
  1386. * )
  1387. * </pre>
  1388. *
  1389. * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
  1390. * <pre>
  1391. * array(
  1392. * '1' => array('one', '944679408'),
  1393. * '2' => array('two', '944679408'),
  1394. * '3' => array('three', '944679408')
  1395. * )
  1396. * </pre>
  1397. *
  1398. * If the more than one row occurs with the same value in the
  1399. * first column, the last row overwrites all previous ones by
  1400. * default. Use the $group parameter if you don't want to
  1401. * overwrite like this. Example:
  1402. *
  1403. * <pre>
  1404. * getAssoc('SELECT category,id,name FROM mytable', false, null,
  1405. * DB_FETCHMODE_ASSOC, true) returns:
  1406. *
  1407. * array(
  1408. * '1' => array(array('id' => '4', 'name' => 'number four'),
  1409. * array('id' => '6', 'name' => 'number six')
  1410. * ),
  1411. * '9' => array(array('id' => '4', 'name' => 'number four'),
  1412. * array('id' => '6', 'name' => 'number six')
  1413. * )
  1414. * )
  1415. * </pre>
  1416. *
  1417. * Keep in mind that database functions in PHP usually return string
  1418. * values for results regardless of the database's internal type.
  1419. *
  1420. * @param string $query the SQL query
  1421. * @param bool $force_array used only when the query returns
  1422. * exactly two columns. If true, the values
  1423. * of the returned array will be one-element
  1424. * arrays instead of scalars.
  1425. * @param mixed $params array, string or numeric data to be used in
  1426. * execution of the statement. Quantity of
  1427. * items passed must match quantity of
  1428. * placeholders in query: meaning 1
  1429. * placeholder for non-array parameters or
  1430. * 1 placeholder per array element.
  1431. * @param int $fetchmode the fetch mode to use
  1432. * @param bool $group if true, the values of the returned array
  1433. * is wrapped in another array. If the same
  1434. * key value (in the first column) repeats
  1435. * itself, the values will be appended to
  1436. * this array instead of overwriting the
  1437. * existing values.
  1438. *
  1439. * @return array|object
  1440. * A DB_Error object on failure.
  1441. */
  1442. public function &getAssoc(
  1443. $query,
  1444. $force_array = false,
  1445. $params = array(),
  1446. $fetchmode = DB_FETCHMODE_DEFAULT,
  1447. $group = false
  1448. )
  1449. {
  1450. $params = (array)$params;
  1451. if (sizeof($params) > 0) {
  1452. $sth = $this->prepare($query);
  1453. if (DB::isError($sth)) {
  1454. return $sth;
  1455. }
  1456. $res = $this->execute($sth, $params);
  1457. $this->freePrepared($sth);
  1458. } else {
  1459. $res = $this->query($query);
  1460. }
  1461. if (DB::isError($res)) {
  1462. return $res;
  1463. }
  1464. if ($fetchmode == DB_FETCHMODE_DEFAULT) {
  1465. $fetchmode = $this->fetchmode;
  1466. }
  1467. $cols = $res->numCols();
  1468. if ($cols < 2) {
  1469. $tmp = $this->raiseError(DB_ERROR_TRUNCATED);
  1470. return $tmp;
  1471. }
  1472. $results = array();
  1473. if ($cols > 2 || $force_array) {
  1474. // return array values
  1475. // XXX this part can be optimized
  1476. if ($fetchmode == DB_FETCHMODE_ASSOC) {
  1477. while (is_array($row = $res->fetchRow(DB_FETCHMODE_ASSOC))) {
  1478. reset($row);
  1479. $key = current($row);
  1480. unset($row[key($row)]);
  1481. if ($group) {
  1482. $results[$key][] = $row;
  1483. } else {
  1484. $results[$key] = $row;
  1485. }
  1486. }
  1487. } elseif ($fetchmode == DB_FETCHMODE_OBJECT) {
  1488. while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
  1489. $arr = get_object_vars($row);
  1490. $key = current($arr);
  1491. if ($group) {
  1492. $results[$key][] = $row;
  1493. } else {
  1494. $results[$key] = $row;
  1495. }
  1496. }
  1497. } else {
  1498. while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
  1499. // we shift away the first element to get
  1500. // indices running from 0 again
  1501. $key = array_shift($row);
  1502. if ($group) {
  1503. $results[$key][] = $row;
  1504. } else {
  1505. $results[$key] = $row;
  1506. }
  1507. }
  1508. }
  1509. if (DB::isError($row)) {
  1510. $results = $row;
  1511. }
  1512. } else {
  1513. // return scalar values
  1514. while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
  1515. if ($group) {
  1516. $results[$row[0]][] = $row[1];
  1517. } else {
  1518. $results[$row[0]] = $row[1];
  1519. }
  1520. }
  1521. if (DB::isError($row)) {
  1522. $results = $row;
  1523. }
  1524. }
  1525. $res->free();
  1526. return $results;
  1527. }
  1528. // }}}
  1529. // {{{ getAssoc()
  1530. /**
  1531. * Fetches all of the rows from a query result
  1532. *
  1533. * @param string $query the SQL query
  1534. * @param mixed $params array, string or numeric data to be used in
  1535. * execution of the statement. Quantity of
  1536. * items passed must match quantity of
  1537. * placeholders in query: meaning 1
  1538. * placeholder for non-array parameters or
  1539. * 1 placeholder per array element.
  1540. * @param int $fetchmode the fetch mode to use:
  1541. * + DB_FETCHMODE_ORDERED
  1542. * + DB_FETCHMODE_ASSOC
  1543. * + DB_FETCHMODE_ORDERED | DB_FETCHMODE_FLIPPED
  1544. * + DB_FETCHMODE_ASSOC | DB_FETCHMODE_FLIPPED
  1545. *
  1546. * @return array|object
  1547. */
  1548. public function &getAll(
  1549. $query,
  1550. $params = array(),
  1551. $fetchmode = DB_FETCHMODE_DEFAULT
  1552. )
  1553. {
  1554. // compat check, the params and fetchmode parameters used to
  1555. // have the opposite order
  1556. if (!is_array($params)) {
  1557. if (is_array($fetchmode)) {
  1558. if ($params === null) {
  1559. $tmp = DB_FETCHMODE_DEFAULT;
  1560. } else {
  1561. $tmp = $params;
  1562. }
  1563. $params = $fetchmode;
  1564. $fetchmode = $tmp;
  1565. } elseif ($params !== null) {
  1566. $fetchmode = $params;
  1567. $params = array();
  1568. }
  1569. }
  1570. if (sizeof($params) > 0) {
  1571. $sth = $this->prepare($query);
  1572. if (DB::isError($sth)) {
  1573. return $sth;
  1574. }
  1575. $res = $this->execute($sth, $params);
  1576. $this->freePrepared($sth);
  1577. } else {
  1578. $res = $this->query($query);
  1579. }
  1580. if ($res === DB_OK || DB::isError($res)) {
  1581. return $res;
  1582. }
  1583. $results = array();
  1584. while (DB_OK === $res->fetchInto($row, $fetchmode)) {
  1585. if ($fetchmode & DB_FETCHMODE_FLIPPED) {
  1586. foreach ($row as $key => $val) {
  1587. $results[$key][] = $val;
  1588. }
  1589. } else {
  1590. $results[] = $row;
  1591. }
  1592. }
  1593. $res->free();
  1594. if (DB::isError($row)) {
  1595. $tmp = $this->raiseError($row);
  1596. return $tmp;
  1597. }
  1598. return $results;
  1599. }
  1600. // }}}
  1601. // {{{ getAll()
  1602. /**
  1603. * Enables or disables automatic commits
  1604. *
  1605. * @param bool $onoff true turns it on, false turns it off
  1606. *
  1607. * @return int|object
  1608. * doesn't support auto-committing transactions.
  1609. */
  1610. public function autoCommit($onoff = false)
  1611. {
  1612. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1613. }
  1614. // }}}
  1615. // {{{ autoCommit()
  1616. /**
  1617. * Commits the current transaction
  1618. *
  1619. * @return int|object
  1620. */
  1621. public function commit()
  1622. {
  1623. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1624. }
  1625. // }}}
  1626. // {{{ commit()
  1627. /**
  1628. * Reverts the current transaction
  1629. *
  1630. * @return int|object
  1631. */
  1632. public function rollback()
  1633. {
  1634. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1635. }
  1636. // }}}
  1637. // {{{ rollback()
  1638. /**
  1639. * Determines the number of rows in a query result
  1640. *
  1641. * @param resource $result the query result idenifier produced by PHP
  1642. *
  1643. * @return int|object
  1644. */
  1645. public function numRows($result)
  1646. {
  1647. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1648. }
  1649. // }}}
  1650. // {{{ numRows()
  1651. /**
  1652. * Determines the number of rows affected by a data maniuplation query
  1653. *
  1654. * 0 is returned for queries that don't manipulate data.
  1655. *
  1656. * @return int|object
  1657. */
  1658. public function affectedRows()
  1659. {
  1660. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1661. }
  1662. // }}}
  1663. // {{{ affectedRows()
  1664. /**
  1665. * Generates the name used inside the database for a sequence
  1666. *
  1667. * The createSequence() docblock contains notes about storing sequence
  1668. * names.
  1669. *
  1670. * @param string $sqn the sequence's public name
  1671. *
  1672. * @return string the sequence's name in the backend
  1673. *
  1674. * @access protected
  1675. * @see DB_common::createSequence(), DB_common::dropSequence(),
  1676. * DB_common::nextID(), DB_common::setOption()
  1677. */
  1678. public function getSequenceName($sqn)
  1679. {
  1680. return sprintf(
  1681. $this->getOption('seqname_format'),
  1682. preg_replace('/[^a-z0-9_.]/i', '_', $sqn)
  1683. );
  1684. }
  1685. // }}}
  1686. // {{{ getSequenceName()
  1687. /**
  1688. * Returns the value of an option
  1689. *
  1690. * @param string $option the option name you're curious about
  1691. *
  1692. * @return mixed the option's value
  1693. */
  1694. public function getOption($option)
  1695. {
  1696. if (isset($this->options[$option])) {
  1697. return $this->options[$option];
  1698. }
  1699. return $this->raiseError("unknown option $option");
  1700. }
  1701. // }}}
  1702. // {{{ nextId()
  1703. /**
  1704. * Returns the next free id in a sequence
  1705. *
  1706. * @param string $seq_name name of the sequence
  1707. * @param boolean $ondemand when true, the seqence is automatically
  1708. * created if it does not exist
  1709. *
  1710. * @return int|object
  1711. * A DB_Error object on failure.
  1712. *
  1713. * @see DB_common::createSequence(), DB_common::dropSequence(),
  1714. * DB_common::getSequenceName()
  1715. */
  1716. public function nextId($seq_name, $ondemand = true)
  1717. {
  1718. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1719. }
  1720. // }}}
  1721. // {{{ createSequence()
  1722. /**
  1723. * Creates a new sequence
  1724. *
  1725. * The name of a given sequence is determined by passing the string
  1726. * provided in the <var>$seq_name</var> argument through PHP's sprintf()
  1727. * function using the value from the <var>seqname_format</var> option as
  1728. * the sprintf()'s format argument.
  1729. *
  1730. * <var>seqname_format</var> is set via setOption().
  1731. *
  1732. * @param string $seq_name name of the new sequence
  1733. *
  1734. * @return int|object
  1735. *
  1736. * @see DB_common::dropSequence(), DB_common::getSequenceName(),
  1737. * DB_common::nextID()
  1738. */
  1739. public function createSequence($seq_name)
  1740. {
  1741. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1742. }
  1743. // }}}
  1744. // {{{ dropSequence()
  1745. /**
  1746. * Deletes a sequence
  1747. *
  1748. * @param string $seq_name name of the sequence to be deleted
  1749. *
  1750. * @return int|object
  1751. *
  1752. * @see DB_common::createSequence(), DB_common::getSequenceName(),
  1753. * DB_common::nextID()
  1754. */
  1755. public function dropSequence($seq_name)
  1756. {
  1757. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1758. }
  1759. // }}}
  1760. // {{{ raiseError()
  1761. /**
  1762. * Gets the DBMS' native error code produced by the last query
  1763. *
  1764. * @return mixed the DBMS' error code. A DB_Error object on failure.
  1765. */
  1766. public function errorNative()
  1767. {
  1768. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1769. }
  1770. // }}}
  1771. // {{{ errorNative()
  1772. /**
  1773. * Maps native error codes to DB's portable ones
  1774. *
  1775. * Uses the <var>$errorcode_map</var> property defined in each driver.
  1776. *
  1777. * @param string|int $nativecode the error code returned by the DBMS
  1778. *
  1779. * @return int the portable DB error code. Return DB_ERROR if the
  1780. * current driver doesn't have a mapping for the
  1781. * $nativecode submitted.
  1782. */
  1783. public function errorCode($nativecode)
  1784. {
  1785. if (isset($this->errorcode_map[$nativecode])) {
  1786. return $this->errorcode_map[$nativecode];
  1787. }
  1788. // Fall back to DB_ERROR if there was no mapping.
  1789. return DB_ERROR;
  1790. }
  1791. // }}}
  1792. // {{{ errorCode()
  1793. /**
  1794. * Maps a DB error code to a textual message
  1795. *
  1796. * @param integer $dbcode the DB error code
  1797. *
  1798. * @return string the error message corresponding to the error code
  1799. * submitted. FALSE if the error code is unknown.
  1800. *
  1801. * @see DB::errorMessage()
  1802. */
  1803. public function errorMessage($dbcode)
  1804. {
  1805. return DB::errorMessage($this->errorcode_map[$dbcode]);
  1806. }
  1807. // }}}
  1808. // {{{ errorMessage()
  1809. /**
  1810. * Returns information about a table or a result set
  1811. *
  1812. * The format of the resulting array depends on which <var>$mode</var>
  1813. * you select. The sample output below is based on this query:
  1814. * <pre>
  1815. * SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
  1816. * FROM tblFoo
  1817. * JOIN tblBar ON tblFoo.fldId = tblBar.fldId
  1818. * </pre>
  1819. *
  1820. * <ul>
  1821. * <li>
  1822. *
  1823. * <kbd>null</kbd> (default)
  1824. * <pre>
  1825. * [0] => Array (
  1826. * [table] => tblFoo
  1827. * [name] => fldId
  1828. * [type] => int
  1829. * [len] => 11
  1830. * [flags] => primary_key not_null
  1831. * )
  1832. * [1] => Array (
  1833. * [table] => tblFoo
  1834. * [name] => fldPhone
  1835. * [type] => string
  1836. * [len] => 20
  1837. * [flags] =>
  1838. * )
  1839. * [2] => Array (
  1840. * [table] => tblBar
  1841. * [name] => fldId
  1842. * [type] => int
  1843. * [len] => 11
  1844. * [flags] => primary_key not_null
  1845. * )
  1846. * </pre>
  1847. *
  1848. * </li><li>
  1849. *
  1850. * <kbd>DB_TABLEINFO_ORDER</kbd>
  1851. *
  1852. * <p>In addition to the information found in the default output,
  1853. * a notation of the number of columns is provided by the
  1854. * <samp>num_fields</samp> element while the <samp>order</samp>
  1855. * element provides an array with the column names as the keys and
  1856. * their location index number (corresponding to the keys in the
  1857. * the default output) as the values.</p>
  1858. *
  1859. * <p>If a result set has identical field names, the last one is
  1860. * used.</p>
  1861. *
  1862. * <pre>
  1863. * [num_fields] => 3
  1864. * [order] => Array (
  1865. * [fldId] => 2
  1866. * [fldTrans] => 1
  1867. * )
  1868. * </pre>
  1869. *
  1870. * </li><li>
  1871. *
  1872. * <kbd>DB_TABLEINFO_ORDERTABLE</kbd>
  1873. *
  1874. * <p>Similar to <kbd>DB_TABLEINFO_ORDER</kbd> but adds more
  1875. * dimensions to the array in which the table names are keys and
  1876. * the field names are sub-keys. This is helpful for queries that
  1877. * join tables which have identical field names.</p>
  1878. *
  1879. * <pre>
  1880. * [num_fields] => 3
  1881. * [ordertable] => Array (
  1882. * [tblFoo] => Array (
  1883. * [fldId] => 0
  1884. * [fldPhone] => 1
  1885. * )
  1886. * [tblBar] => Array (
  1887. * [fldId] => 2
  1888. * )
  1889. * )
  1890. * </pre>
  1891. *
  1892. * </li>
  1893. * </ul>
  1894. *
  1895. * The <samp>flags</samp> element contains a space separated list
  1896. * of extra information about the field. This data is inconsistent
  1897. * between DBMS's due to the way each DBMS works.
  1898. * + <samp>primary_key</samp>
  1899. * + <samp>unique_key</samp>
  1900. * + <samp>multiple_key</samp>
  1901. * + <samp>not_null</samp>
  1902. *
  1903. * Most DBMS's only provide the <samp>table</samp> and <samp>flags</samp>
  1904. * elements if <var>$result</var> is a table name. The following DBMS's
  1905. * provide full information from queries:
  1906. * + fbsql
  1907. * + mysql
  1908. *
  1909. * If the 'portability' option has <samp>DB_PORTABILITY_LOWERCASE</samp>
  1910. * turned on, the names of tables and fields will be lowercased.
  1911. *
  1912. * @param object|string $result DB_result object from a query or a
  1913. * string containing the name of a table.
  1914. * While this also accepts a query result
  1915. * resource identifier, this behavior is
  1916. * deprecated.
  1917. * @param int $mode either unused or one of the tableInfo modes:
  1918. * <kbd>DB_TABLEINFO_ORDERTABLE</kbd>,
  1919. * <kbd>DB_TABLEINFO_ORDER</kbd> or
  1920. * <kbd>DB_TABLEINFO_FULL</kbd> (which does both).
  1921. * These are bitwise, so the first two can be
  1922. * combined using <kbd>|</kbd>.
  1923. *
  1924. * @return array|object
  1925. * A DB_Error object on failure.
  1926. *
  1927. * @see DB_common::setOption()
  1928. */
  1929. public function tableInfo($result, $mode = null)
  1930. {
  1931. /*
  1932. * If the DB_<driver> class has a tableInfo() method, that one
  1933. * overrides this one. But, if the driver doesn't have one,
  1934. * this method runs and tells users about that fact.
  1935. */
  1936. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  1937. }
  1938. // }}}
  1939. // {{{ tableInfo()
  1940. /**
  1941. * Lists the tables in the current database
  1942. *
  1943. * @return array the list of tables. A DB_Error object on failure.
  1944. *
  1945. * @deprecated Method deprecated some time before Release 1.2
  1946. */
  1947. public function getTables()
  1948. {
  1949. return $this->getListOf('tables');
  1950. }
  1951. // }}}
  1952. // {{{ getTables()
  1953. /**
  1954. * Lists internal database information
  1955. *
  1956. * @param string $type type of information being sought.
  1957. * Common items being sought are:
  1958. * tables, databases, users, views, functions
  1959. * Each DBMS's has its own capabilities.
  1960. *
  1961. * @return array|object
  1962. * A DB DB_Error object on failure.
  1963. */
  1964. public function getListOf($type)
  1965. {
  1966. $sql = $this->getSpecialQuery($type);
  1967. if ($sql === null) {
  1968. $this->last_query = '';
  1969. return $this->raiseError(DB_ERROR_UNSUPPORTED);
  1970. } elseif (is_int($sql) || DB::isError($sql)) {
  1971. // Previous error
  1972. return $this->raiseError($sql);
  1973. } elseif (is_array($sql)) {
  1974. // Already the result
  1975. return $sql;
  1976. }
  1977. // Launch this query
  1978. return $this->getCol($sql);
  1979. }
  1980. // }}}
  1981. // {{{ getListOf()
  1982. /**
  1983. * Obtains the query string needed for listing a given type of objects
  1984. *
  1985. * @param string $type the kind of objects you want to retrieve
  1986. *
  1987. * @return string the SQL query string or null if the driver doesn't
  1988. * support the object type requested
  1989. *
  1990. * @access protected
  1991. * @see DB_common::getListOf()
  1992. */
  1993. public function getSpecialQuery($type)
  1994. {
  1995. return $this->raiseError(DB_ERROR_UNSUPPORTED);
  1996. }
  1997. // }}}
  1998. // {{{ getSpecialQuery()
  1999. /**
  2000. * Fetches a single column from a query result and returns it as an
  2001. * indexed array
  2002. *
  2003. * @param string $query the SQL query
  2004. * @param mixed $col which column to return (integer [column number,
  2005. * starting at 0] or string [column name])
  2006. * @param mixed $params array, string or numeric data to be used in
  2007. * execution of the statement. Quantity of items
  2008. * passed must match quantity of placeholders in
  2009. * query: meaning 1 placeholder for non-array
  2010. * parameters or 1 placeholder per array element.
  2011. *
  2012. * @return array the results as an array. A DB_Error object on failure.
  2013. *
  2014. * @see DB_common::query()
  2015. */
  2016. public function &getCol($query, $col = 0, $params = array())
  2017. {
  2018. $params = (array)$params;
  2019. if (sizeof($params) > 0) {
  2020. $sth = $this->prepare($query);
  2021. if (DB::isError($sth)) {
  2022. return $sth;
  2023. }
  2024. $res = $this->execute($sth, $params);
  2025. $this->freePrepared($sth);
  2026. } else {
  2027. $res = $this->query($query);
  2028. }
  2029. if (DB::isError($res)) {
  2030. return $res;
  2031. }
  2032. $fetchmode = is_int($col) ? DB_FETCHMODE_ORDERED : DB_FETCHMODE_ASSOC;
  2033. if (!is_array($row = $res->fetchRow($fetchmode))) {
  2034. $ret = array();
  2035. } else {
  2036. if (!array_key_exists($col, $row)) {
  2037. $ret = $this->raiseError(DB_ERROR_NOSUCHFIELD);
  2038. } else {
  2039. $ret = array($row[$col]);
  2040. while (is_array($row = $res->fetchRow($fetchmode))) {
  2041. $ret[] = $row[$col];
  2042. }
  2043. }
  2044. }
  2045. $res->free();
  2046. if (DB::isError($row)) {
  2047. $ret = $row;
  2048. }
  2049. return $ret;
  2050. }
  2051. // }}}
  2052. // {{{ nextQueryIsManip()
  2053. /**
  2054. * Sets (or unsets) a flag indicating that the next query will be a
  2055. * manipulation query, regardless of the usual DB::isManip() heuristics.
  2056. *
  2057. * @param boolean true to set the flag overriding the isManip() behaviour,
  2058. * false to clear it and fall back onto isManip()
  2059. *
  2060. * @return void
  2061. *
  2062. * @access public
  2063. */
  2064. public function nextQueryIsManip($manip)
  2065. {
  2066. $this->_next_query_manip = $manip;
  2067. }
  2068. // }}}
  2069. // {{{ _checkManip()
  2070. /**
  2071. * Checks if the given query is a manipulation query. This also takes into
  2072. * account the _next_query_manip flag and sets the _last_query_manip flag
  2073. * (and resets _next_query_manip) according to the result.
  2074. *
  2075. * @param string The query to check.
  2076. *
  2077. * @return boolean true if the query is a manipulation query, false
  2078. * otherwise
  2079. *
  2080. * @access protected
  2081. */
  2082. public function _checkManip($query)
  2083. {
  2084. if ($this->_next_query_manip || DB::isManip($query)) {
  2085. $this->_last_query_manip = true;
  2086. } else {
  2087. $this->_last_query_manip = false;
  2088. }
  2089. $this->_next_query_manip = false;
  2090. return $this->_last_query_manip;
  2091. }
  2092. // }}}
  2093. // {{{ _rtrimArrayValues()
  2094. /**
  2095. * Right-trims all strings in an array
  2096. *
  2097. * @param array $array the array to be trimmed (passed by reference)
  2098. *
  2099. * @return void
  2100. *
  2101. * @access protected
  2102. */
  2103. public function _rtrimArrayValues(&$array)
  2104. {
  2105. foreach ($array as $key => $value) {
  2106. if (is_string($value)) {
  2107. $array[$key] = rtrim($value);
  2108. }
  2109. }
  2110. }
  2111. // }}}
  2112. // {{{ _convertNullArrayValuesToEmpty()
  2113. /**
  2114. * Converts all null values in an array to empty strings
  2115. *
  2116. * @param array $array the array to be de-nullified (passed by reference)
  2117. *
  2118. * @return void
  2119. *
  2120. * @access protected
  2121. */
  2122. public function _convertNullArrayValuesToEmpty(&$array)
  2123. {
  2124. foreach ($array as $key => $value) {
  2125. if (is_null($value)) {
  2126. $array[$key] = '';
  2127. }
  2128. }
  2129. }
  2130. // }}}
  2131. }
  2132. /*
  2133. * Local variables:
  2134. * tab-width: 4
  2135. * c-basic-offset: 4
  2136. * End:
  2137. */