common.php 71 KB

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