common.php 71 KB

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