Zend_Db_Adapter.xml 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.adapter">
  4. <title>Zend_Db_Adapter</title>
  5. <para>
  6. <classname>Zend_Db</classname> and its related classes provide a simple SQL database
  7. interface for Zend Framework. The <classname>Zend_Db_Adapter</classname> is the basic class
  8. you use to connect your PHP application to an RDBMS. There is a different Adapter
  9. class for each brand of RDBMS.
  10. </para>
  11. <para>
  12. The <classname>Zend_Db</classname> adapters create a bridge from the vendor-specific PHP
  13. extensions to a common interface to help you write PHP applications
  14. once and deploy with multiple brands of RDBMS with very little effort.
  15. </para>
  16. <para>
  17. The interface of the adapter class is similar to the interface of the
  18. <ulink url="http://www.php.net/pdo">PHP Data Objects</ulink> extension.
  19. <classname>Zend_Db</classname> provides Adapter classes to PDO drivers for the following
  20. RDBMS brands:
  21. </para>
  22. <itemizedlist>
  23. <listitem>
  24. <para>
  25. IBM DB2 and Informix Dynamic Server (IDS), using the
  26. <ulink url="http://www.php.net/pdo-ibm">pdo_ibm</ulink> PHP extension
  27. </para>
  28. </listitem>
  29. <listitem>
  30. <para>
  31. MySQL, using the <ulink url="http://www.php.net/pdo-mysql">pdo_mysql</ulink> PHP
  32. extension
  33. </para>
  34. </listitem>
  35. <listitem>
  36. <para>
  37. Microsoft SQL Server, using the <ulink
  38. url="http://www.php.net/pdo-mssql">pdo_mssql</ulink> PHP extension
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>
  43. Oracle, using the <ulink url="http://www.php.net/pdo-oci">pdo_oci</ulink> PHP
  44. extension
  45. </para>
  46. </listitem>
  47. <listitem>
  48. <para>
  49. PostgreSQL, using the <ulink url="http://www.php.net/pdo-pgsql">pdo_pgsql</ulink>
  50. PHP extension
  51. </para>
  52. </listitem>
  53. <listitem>
  54. <para>
  55. SQLite, using the <ulink url="http://www.php.net/pdo-sqlite">pdo_sqlite</ulink> PHP
  56. extension
  57. </para>
  58. </listitem>
  59. </itemizedlist>
  60. <para>
  61. In addition, <classname>Zend_Db</classname> provides Adapter classes that utilize PHP
  62. database extensions for the following RDBMS brands:
  63. </para>
  64. <itemizedlist>
  65. <listitem>
  66. <para>
  67. MySQL, using the <ulink url="http://www.php.net/mysqli">mysqli</ulink> PHP extension
  68. </para>
  69. </listitem>
  70. <listitem>
  71. <para>
  72. Oracle, using the <ulink url="http://www.php.net/oci8">oci8</ulink> PHP extension
  73. </para>
  74. </listitem>
  75. <listitem>
  76. <para>
  77. IBM DB2 and DB2/i5, using the <ulink
  78. url="http://www.php.net/ibm_db2">ibm_db2</ulink> PHP extension
  79. </para>
  80. </listitem>
  81. <listitem>
  82. <para>
  83. Firebird/Interbase, using the <ulink
  84. url="http://www.php.net/ibase">php_interbase</ulink> PHP extension
  85. </para>
  86. </listitem>
  87. </itemizedlist>
  88. <note>
  89. <para>
  90. Each <classname>Zend_Db</classname> Adapter uses a PHP extension. You must have the
  91. respective PHP extension enabled in your PHP environment to use a
  92. <classname>Zend_Db</classname> Adapter. For example, if you use any of the PDO
  93. <classname>Zend_Db</classname> Adapters, you need to enable both the PDO extension and
  94. the PDO driver for the brand of RDBMS you use.
  95. </para>
  96. </note>
  97. <sect2 id="zend.db.adapter.connecting">
  98. <title>Connecting to a Database Using an Adapter</title>
  99. <para>
  100. This section describes how to create an instance of a database Adapter.
  101. This corresponds to making a connection to your RDBMS server from your
  102. PHP application.
  103. </para>
  104. <sect3 id="zend.db.adapter.connecting.constructor">
  105. <title>Using a Zend_Db Adapter Constructor</title>
  106. <para>
  107. You can create an instance of an adapter using its constructor.
  108. An adapter constructor takes one argument, which is an array
  109. of parameters used to declare the connection.
  110. </para>
  111. <example id="zend.db.adapter.connecting.constructor.example">
  112. <title>Using an Adapter Constructor</title>
  113. <programlisting language="php"><![CDATA[
  114. $db = new Zend_Db_Adapter_Pdo_Mysql(array(
  115. 'host' => '127.0.0.1',
  116. 'username' => 'webuser',
  117. 'password' => 'xxxxxxxx',
  118. 'dbname' => 'test'
  119. ));
  120. ]]></programlisting>
  121. </example>
  122. </sect3>
  123. <sect3 id="zend.db.adapter.connecting.factory">
  124. <title>Using the Zend_Db Factory</title>
  125. <para>
  126. As an alternative to using an adapter constructor directly, you
  127. can create an instance of an adapter using the static method
  128. <classname>Zend_Db::factory()</classname>. This method dynamically loads
  129. the adapter class file on demand using
  130. <link linkend="zend.loader.load.class">Zend_Loader::loadClass()</link>.
  131. </para>
  132. <para>
  133. The first argument is a string that names the base name of the
  134. adapter class. For example the string 'Pdo_Mysql' corresponds
  135. to the class Zend_Db_Adapter_Pdo_Mysql. The second argument is
  136. the same array of parameters you would have given to the
  137. adapter constructor.
  138. </para>
  139. <example id="zend.db.adapter.connecting.factory.example">
  140. <title>Using the Adapter Factory Method</title>
  141. <programlisting language="php"><![CDATA[
  142. // We don't need the following statement because the
  143. // Zend_Db_Adapter_Pdo_Mysql file will be loaded for us by the Zend_Db
  144. // factory method.
  145. // require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
  146. // Automatically load class Zend_Db_Adapter_Pdo_Mysql
  147. // and create an instance of it.
  148. $db = Zend_Db::factory('Pdo_Mysql', array(
  149. 'host' => '127.0.0.1',
  150. 'username' => 'webuser',
  151. 'password' => 'xxxxxxxx',
  152. 'dbname' => 'test'
  153. ));
  154. ]]></programlisting>
  155. </example>
  156. <para>
  157. If you create your own class that extends
  158. <classname>Zend_Db_Adapter_Abstract_Adapter</classname>, but you do not name your
  159. class with the "Zend_Db_Adapter" package prefix, you can use the
  160. <methodname>factory()</methodname> method to load your adapter if you
  161. specify the leading portion of the adapter class with the
  162. 'adapterNamespace' key in the parameters array.
  163. </para>
  164. <example id="zend.db.adapter.connecting.factory.example2">
  165. <title>Using the Adapter Factory Method for a Custom Adapter Class</title>
  166. <programlisting language="php"><![CDATA[
  167. // We don't need to load the adapter class file
  168. // because it will be loaded for us by the Zend_Db factory method.
  169. // Automatically load class MyProject_Db_Adapter_Pdo_Mysql and create
  170. // an instance of it.
  171. $db = Zend_Db::factory('Pdo_Mysql', array(
  172. 'host' => '127.0.0.1',
  173. 'username' => 'webuser',
  174. 'password' => 'xxxxxxxx',
  175. 'dbname' => 'test',
  176. 'adapterNamespace' => 'MyProject_Db_Adapter'
  177. ));
  178. ]]></programlisting>
  179. </example>
  180. </sect3>
  181. <sect3 id="zend.db.adapter.connecting.factory-config">
  182. <title>Using Zend_Config with the Zend_Db Factory</title>
  183. <para>
  184. Optionally, you may specify either argument of the
  185. <methodname>factory()</methodname> method as an object of type
  186. <link linkend="zend.config">Zend_Config</link>.
  187. </para>
  188. <para>
  189. If the first argument is a config object, it is expected to
  190. contain a property named <code>adapter</code>, containing the
  191. string naming the adapter class name base. Optionally, the object
  192. may contain a property named <code>params</code>, with
  193. subproperties corresponding to adapter parameter names.
  194. This is used only if the second argument of the
  195. <methodname>factory()</methodname> method is absent.
  196. </para>
  197. <example id="zend.db.adapter.connecting.factory.example1">
  198. <title>Using the Adapter Factory Method with a Zend_Config Object</title>
  199. <para>
  200. In the example below, a <classname>Zend_Config</classname> object is created
  201. from an array. You can also load data from an external file using classes such
  202. as <link linkend="zend.config.adapters.ini">Zend_Config_Ini</link>
  203. and <link linkend="zend.config.adapters.xml">Zend_Config_Xml</link>.
  204. </para>
  205. <programlisting language="php"><![CDATA[
  206. $config = new Zend_Config(
  207. array(
  208. 'database' => array(
  209. 'adapter' => 'Mysqli',
  210. 'params' => array(
  211. 'host' => '127.0.0.1',
  212. 'dbname' => 'test',
  213. 'username' => 'webuser',
  214. 'password' => 'secret',
  215. )
  216. )
  217. )
  218. );
  219. $db = Zend_Db::factory($config->database);
  220. ]]></programlisting>
  221. </example>
  222. <para>
  223. The second argument of the <methodname>factory()</methodname> method may be
  224. an associative array containing entries corresponding to
  225. adapter parameters. This argument is optional. If the first
  226. argument is of type <classname>Zend_Config</classname>, it is assumed to contain all
  227. parameters, and the second argument is ignored.
  228. </para>
  229. </sect3>
  230. <sect3 id="zend.db.adapter.connecting.parameters">
  231. <title>Adapter Parameters</title>
  232. <para>
  233. The following list explains common parameters recognized by
  234. <classname>Zend_Db</classname> Adapter classes.
  235. </para>
  236. <itemizedlist>
  237. <listitem>
  238. <para>
  239. <emphasis>host</emphasis>:
  240. a string containing a hostname or IP address of the
  241. database server. If the database is running on the
  242. same host as the PHP application, you may use
  243. 'localhost' or '127.0.0.1'.
  244. </para>
  245. </listitem>
  246. <listitem>
  247. <para>
  248. <emphasis>username</emphasis>:
  249. account identifier for authenticating a connection to the
  250. RDBMS server.
  251. </para>
  252. </listitem>
  253. <listitem>
  254. <para>
  255. <emphasis>password</emphasis>:
  256. account password credential for authenticating a
  257. connection to the RDBMS server.
  258. </para>
  259. </listitem>
  260. <listitem>
  261. <para>
  262. <emphasis>dbname</emphasis>:
  263. database instance name on the RDBMS server.
  264. </para>
  265. </listitem>
  266. <listitem>
  267. <para>
  268. <emphasis>port</emphasis>:
  269. some RDBMS servers can accept network connections on a
  270. administrator-specified port number. The port
  271. parameter allow you to specify the port to which your
  272. PHP application connects, to match the port configured
  273. on the RDBMS server.
  274. </para>
  275. </listitem>
  276. <listitem>
  277. <para>
  278. <emphasis>options</emphasis>:
  279. this parameter is an associative array of options
  280. that are generic to all <classname>Zend_Db_Adapter</classname> classes.
  281. </para>
  282. </listitem>
  283. <listitem>
  284. <para>
  285. <emphasis>driver_options</emphasis>:
  286. this parameter is an associative array of additional
  287. options that are specific to a given database
  288. extension. One typical use of this parameter is to
  289. set attributes of a PDO driver.
  290. </para>
  291. </listitem>
  292. <listitem>
  293. <para>
  294. <emphasis>adapterNamespace</emphasis>:
  295. names the initial part of the class name for the
  296. adapter, instead of 'Zend_Db_Adapter'. Use this if
  297. you need to use the <methodname>factory()</methodname> method to
  298. load a non-Zend database adapter class.
  299. </para>
  300. </listitem>
  301. </itemizedlist>
  302. <example id="zend.db.adapter.connecting.parameters.example1">
  303. <title>Passing the Case-Folding Option to the Factory</title>
  304. <para>
  305. You can specify this option by the constant
  306. <classname>Zend_Db::CASE_FOLDING</classname>.
  307. This corresponds to the <constant>ATTR_CASE</constant> attribute in
  308. PDO and IBM DB2 database drivers, adjusting the case of
  309. string keys in query result sets. The option takes values
  310. <constant>Zend_Db::CASE_NATURAL</constant> (the default),
  311. <constant>Zend_Db::CASE_UPPER</constant>, and
  312. <constant>Zend_Db::CASE_LOWER</constant>.
  313. </para>
  314. <programlisting language="php"><![CDATA[
  315. $options = array(
  316. Zend_Db::CASE_FOLDING => Zend_Db::CASE_UPPER
  317. );
  318. $params = array(
  319. 'host' => '127.0.0.1',
  320. 'username' => 'webuser',
  321. 'password' => 'xxxxxxxx',
  322. 'dbname' => 'test',
  323. 'options' => $options
  324. );
  325. $db = Zend_Db::factory('Db2', $params);
  326. ]]></programlisting>
  327. </example>
  328. <example id="zend.db.adapter.connecting.parameters.example2">
  329. <title>Passing the Auto-Quoting Option to the Factory</title>
  330. <para>
  331. You can specify this option by the constant
  332. <classname>Zend_Db::AUTO_QUOTE_IDENTIFIERS</classname>. If the value
  333. is <constant>TRUE</constant> (the default), identifiers like table
  334. names, column names, and even aliases are delimited in all
  335. SQL syntax generated by the Adapter object. This makes it
  336. simple to use identifiers that contain SQL keywords, or
  337. special characters. If the value is <constant>FALSE</constant>,
  338. identifiers are not delimited automatically. If you need
  339. to delimit identifiers, you must do so yourself using the
  340. <methodname>quoteIdentifier()</methodname> method.
  341. </para>
  342. <programlisting language="php"><![CDATA[
  343. $options = array(
  344. Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
  345. );
  346. $params = array(
  347. 'host' => '127.0.0.1',
  348. 'username' => 'webuser',
  349. 'password' => 'xxxxxxxx',
  350. 'dbname' => 'test',
  351. 'options' => $options
  352. );
  353. $db = Zend_Db::factory('Pdo_Mysql', $params);
  354. ]]></programlisting>
  355. </example>
  356. <example id="zend.db.adapter.connecting.parameters.example3">
  357. <title>Passing PDO Driver Options to the Factory</title>
  358. <programlisting language="php"><![CDATA[
  359. $pdoParams = array(
  360. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
  361. );
  362. $params = array(
  363. 'host' => '127.0.0.1',
  364. 'username' => 'webuser',
  365. 'password' => 'xxxxxxxx',
  366. 'dbname' => 'test',
  367. 'driver_options' => $pdoParams
  368. );
  369. $db = Zend_Db::factory('Pdo_Mysql', $params);
  370. echo $db->getConnection()
  371. ->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
  372. ]]></programlisting>
  373. </example>
  374. <example id="zend.db.adapter.connecting.parameters.example4">
  375. <title>Passing Serialization Options to the Factory</title>
  376. <programlisting language="php"><![CDATA[
  377. $options = array(
  378. Zend_Db::ALLOW_SERIALIZATION => false
  379. );
  380. $params = array(
  381. 'host' => '127.0.0.1',
  382. 'username' => 'webuser',
  383. 'password' => 'xxxxxxxx',
  384. 'dbname' => 'test',
  385. 'options' => $options
  386. );
  387. $db = Zend_Db::factory('Pdo_Mysql', $params);
  388. ]]></programlisting>
  389. </example>
  390. </sect3>
  391. <sect3 id="zend.db.adapter.connecting.getconnection">
  392. <title>Managing Lazy Connections</title>
  393. <para>
  394. Creating an instance of an Adapter class does not immediately
  395. connect to the RDBMS server. The Adapter saves the connection
  396. parameters, and makes the actual connection on demand, the
  397. first time you need to execute a query. This ensures that
  398. creating an Adapter object is quick and inexpensive. You can
  399. create an instance of an Adapter even if you are not certain
  400. that you need to run any database queries during the current
  401. request your application is serving.
  402. </para>
  403. <para>
  404. If you need to force the Adapter to connect to the RDBMS, use
  405. the <methodname>getConnection()</methodname> method. This method returns
  406. an object for the connection as represented by the respective
  407. PHP database extension. For example, if you use any of the
  408. Adapter classes for PDO drivers, then
  409. <methodname>getConnection()</methodname> returns the PDO object, after
  410. initiating it as a live connection to the specific database.
  411. </para>
  412. <para>
  413. It can be useful to force the connection if you want to catch
  414. any exceptions it throws as a result of invalid account
  415. credentials, or other failure to connect to the RDBMS server.
  416. These exceptions are not thrown until the connection is made,
  417. so it can help simplify your application code if you handle the
  418. exceptions in one place, instead of at the time of
  419. the first query against the database.
  420. </para>
  421. <para>
  422. Additionally, an adapter can get serialized to store it, for example,
  423. in a session variable. This can be very useful not only for the
  424. adapter itself, but for other objects that aggregate it, like a
  425. <classname>Zend_Db_Select</classname> object. By default, adapters are allowed
  426. to be serialized, if you don't want it, you should consider passing the
  427. <classname>Zend_Db::ALLOW_SERIALIZATION=false</classname> option, see the example
  428. above. To respect lazy connections principle, the adapter won't reconnect itself
  429. after being unserialized. You must then call
  430. <methodname>getConnection()</methodname>
  431. yourself. You can make the adapter auto-reconnect by passing the
  432. <classname>Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE=true</classname> as an adapter
  433. option.
  434. </para>
  435. <example id="zend.db.adapter.connecting.getconnection.example">
  436. <title>Handling Connection Exceptions</title>
  437. <programlisting language="php"><![CDATA[
  438. try {
  439. $db = Zend_Db::factory('Pdo_Mysql', $parameters);
  440. $db->getConnection();
  441. } catch (Zend_Db_Adapter_Exception $e) {
  442. // perhaps a failed login credential, or perhaps the RDBMS is not running
  443. } catch (Zend_Exception $e) {
  444. // perhaps factory() failed to load the specified Adapter class
  445. }
  446. ]]></programlisting>
  447. </example>
  448. </sect3>
  449. </sect2>
  450. <sect2 id="zend.db.adapter.example-database">
  451. <title>Example Database</title>
  452. <para>
  453. In the documentation for <classname>Zend_Db</classname> classes, we use a set of simple
  454. tables to illustrate usage of the classes and methods. These
  455. example tables could store information for tracking bugs in a
  456. software development project. The database contains four tables:
  457. </para>
  458. <itemizedlist>
  459. <listitem>
  460. <para>
  461. <emphasis>accounts</emphasis> stores
  462. information about each user of the bug-tracking database.
  463. </para>
  464. </listitem>
  465. <listitem>
  466. <para>
  467. <emphasis>products</emphasis> stores
  468. information about each product for which a bug can be
  469. logged.
  470. </para>
  471. </listitem>
  472. <listitem>
  473. <para>
  474. <emphasis>bugs</emphasis> stores information
  475. about bugs, including that current state of the bug, the
  476. person who reported the bug, the person who is assigned to
  477. fix the bug, and the person who is assigned to verify the
  478. fix.
  479. </para>
  480. </listitem>
  481. <listitem>
  482. <para>
  483. <emphasis>bugs_products</emphasis> stores a
  484. relationship between bugs and products. This implements a
  485. many-to-many relationship, because a given bug may be
  486. relevant to multiple products, and of course a given
  487. product can have multiple bugs.
  488. </para>
  489. </listitem>
  490. </itemizedlist>
  491. <para>
  492. The following SQL data definition language pseudocode describes the
  493. tables in this example database. These example tables are used
  494. extensively by the automated unit tests for <classname>Zend_Db</classname>.
  495. </para>
  496. <programlisting language="sql"><![CDATA[
  497. CREATE TABLE accounts (
  498. account_name VARCHAR(100) NOT NULL PRIMARY KEY
  499. );
  500. CREATE TABLE products (
  501. product_id INTEGER NOT NULL PRIMARY KEY,
  502. product_name VARCHAR(100)
  503. );
  504. CREATE TABLE bugs (
  505. bug_id INTEGER NOT NULL PRIMARY KEY,
  506. bug_description VARCHAR(100),
  507. bug_status VARCHAR(20),
  508. reported_by VARCHAR(100) REFERENCES accounts(account_name),
  509. assigned_to VARCHAR(100) REFERENCES accounts(account_name),
  510. verified_by VARCHAR(100) REFERENCES accounts(account_name)
  511. );
  512. CREATE TABLE bugs_products (
  513. bug_id INTEGER NOT NULL REFERENCES bugs,
  514. product_id INTEGER NOT NULL REFERENCES products,
  515. PRIMARY KEY (bug_id, product_id)
  516. );
  517. ]]></programlisting>
  518. <para>
  519. Also notice that the <code>bugs</code> table contains multiple
  520. foreign key references to the <code>accounts</code> table.
  521. Each of these foreign keys may reference a different row in the
  522. <code>accounts</code> table for a given bug.
  523. </para>
  524. <para>
  525. The diagram below illustrates the physical data model of the
  526. example database.
  527. </para>
  528. <para>
  529. <inlinegraphic width="387" scale="100" align="center" valign="middle"
  530. fileref="figures/zend.db.adapter.example-database.png" format="PNG" />
  531. </para>
  532. </sect2>
  533. <sect2 id="zend.db.adapter.select">
  534. <title>Reading Query Results</title>
  535. <para>
  536. This section describes methods of the Adapter class with which you
  537. can run SELECT queries and retrieve the query results.
  538. </para>
  539. <sect3 id="zend.db.adapter.select.fetchall">
  540. <title>Fetching a Complete Result Set</title>
  541. <para>
  542. You can run a SQL SELECT query and retrieve its results in one
  543. step using the <methodname>fetchAll()</methodname> method.
  544. </para>
  545. <para>
  546. The first argument to this method is a string containing a
  547. SELECT statement. Alternatively, the first argument can be an
  548. object of class <link linkend="zend.db.select">Zend_Db_Select</link>.
  549. The Adapter automatically converts this object to a string
  550. representation of the SELECT statement.
  551. </para>
  552. <para>
  553. The second argument to <methodname>fetchAll()</methodname> is an array of
  554. values to substitute for parameter placeholders in the SQL
  555. statement.
  556. </para>
  557. <example id="zend.db.adapter.select.fetchall.example">
  558. <title>Using fetchAll()</title>
  559. <programlisting language="php"><![CDATA[
  560. $sql = 'SELECT * FROM bugs WHERE bug_id = ?';
  561. $result = $db->fetchAll($sql, 2);
  562. ]]></programlisting>
  563. </example>
  564. </sect3>
  565. <sect3 id="zend.db.adapter.select.fetch-mode">
  566. <title>Changing the Fetch Mode</title>
  567. <para>
  568. By default, <methodname>fetchAll()</methodname> returns an array of
  569. rows, each of which is an associative array. The keys of the
  570. associative array are the columns or column aliases named in
  571. the select query.
  572. </para>
  573. <para>
  574. You can specify a different style of fetching results using the
  575. <methodname>setFetchMode()</methodname> method. The modes supported are
  576. identified by constants:
  577. </para>
  578. <itemizedlist>
  579. <listitem>
  580. <para>
  581. <emphasis>Zend_Db::FETCH_ASSOC</emphasis>:
  582. return data in an array of associative arrays.
  583. The array keys are column names, as strings. This is the default fetch mode
  584. for <classname>Zend_Db_Adapter</classname> classes.
  585. </para>
  586. <para>
  587. Note that if your select-list contains more than one
  588. column with the same name, for example if they are from
  589. two different tables in a JOIN, there can be only one
  590. entry in the associative array for a given name.
  591. If you use the FETCH_ASSOC mode, you should specify
  592. column aliases in your SELECT query to ensure that the
  593. names result in unique array keys.
  594. </para>
  595. <para>
  596. By default, these strings are returned as they are
  597. returned by the database driver. This is typically the
  598. spelling of the column in the RDBMS server. You can
  599. specify the case for these strings, using the
  600. <classname>Zend_Db::CASE_FOLDING</classname> option.
  601. Specify this when instantiating the Adapter.
  602. See <xref linkend="zend.db.adapter.connecting.parameters.example1" />.
  603. </para>
  604. </listitem>
  605. <listitem>
  606. <para>
  607. <emphasis>Zend_Db::FETCH_NUM</emphasis>:
  608. return data in an array of arrays. The arrays are
  609. indexed by integers, corresponding to the position of
  610. the respective field in the select-list of the query.
  611. </para>
  612. </listitem>
  613. <listitem>
  614. <para>
  615. <emphasis>Zend_Db::FETCH_BOTH</emphasis>:
  616. return data in an array of arrays. The array keys are
  617. both strings as used in the FETCH_ASSOC mode, and
  618. integers as used in the FETCH_NUM mode. Note that the
  619. number of elements in the array is double that which
  620. would be in the array if you used either FETCH_ASSOC
  621. or FETCH_NUM.
  622. </para>
  623. </listitem>
  624. <listitem>
  625. <para>
  626. <emphasis>Zend_Db::FETCH_COLUMN</emphasis>:
  627. return data in an array of values. The value in each array
  628. is the value returned by one column of the result set.
  629. By default, this is the first column, indexed by 0.
  630. </para>
  631. </listitem>
  632. <listitem>
  633. <para>
  634. <emphasis>Zend_Db::FETCH_OBJ</emphasis>:
  635. return data in an array of objects. The default class
  636. is the PHP built-in class stdClass. Columns of the
  637. result set are available as public properties of the
  638. object.
  639. </para>
  640. </listitem>
  641. </itemizedlist>
  642. <example id="zend.db.adapter.select.fetch-mode.example">
  643. <title>Using setFetchMode()</title>
  644. <programlisting language="php"><![CDATA[
  645. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  646. $result = $db->fetchAll('SELECT * FROM bugs WHERE bug_id = ?', 2);
  647. // $result is an array of objects
  648. echo $result[0]->bug_description;
  649. ]]></programlisting>
  650. </example>
  651. </sect3>
  652. <sect3 id="zend.db.adapter.select.fetchassoc">
  653. <title>Fetching a Result Set as an Associative Array</title>
  654. <para>
  655. The <methodname>fetchAssoc()</methodname> method returns data in an array
  656. of associative arrays, regardless of what value you have set
  657. for the fetch mode.
  658. </para>
  659. <example id="zend.db.adapter.select.fetchassoc.example">
  660. <title>Using fetchAssoc()</title>
  661. <programlisting language="php"><![CDATA[
  662. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  663. $result = $db->fetchAssoc('SELECT * FROM bugs WHERE bug_id = ?', 2);
  664. // $result is an array of associative arrays, in spite of the fetch mode
  665. echo $result[0]['bug_description'];
  666. ]]></programlisting>
  667. </example>
  668. </sect3>
  669. <sect3 id="zend.db.adapter.select.fetchcol">
  670. <title>Fetching a Single Column from a Result Set</title>
  671. <para>
  672. The <methodname>fetchCol()</methodname> method returns data in an array
  673. of values, regardless of the value you have set for the fetch mode.
  674. This only returns the first column returned by the query.
  675. Any other columns returned by the query are discarded.
  676. If you need to return a column other than the first, see
  677. <xref linkend="zend.db.statement.fetching.fetchcolumn" />.
  678. </para>
  679. <example id="zend.db.adapter.select.fetchcol.example">
  680. <title>Using fetchCol()</title>
  681. <programlisting language="php"><![CDATA[
  682. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  683. $result = $db->fetchCol(
  684. 'SELECT bug_description, bug_id FROM bugs WHERE bug_id = ?', 2);
  685. // contains bug_description; bug_id is not returned
  686. echo $result[0];
  687. ]]></programlisting>
  688. </example>
  689. </sect3>
  690. <sect3 id="zend.db.adapter.select.fetchpairs">
  691. <title>Fetching Key-Value Pairs from a Result Set</title>
  692. <para>
  693. The <methodname>fetchPairs()</methodname> method returns data in an array
  694. of key-value pairs, as an associative array with a single entry
  695. per row. The key of this associative array is taken from the
  696. first column returned by the SELECT query. The value is taken
  697. from the second column returned by the SELECT query. Any other
  698. columns returned by the query are discarded.
  699. </para>
  700. <para>
  701. You should design the SELECT query so that the first column
  702. returned has unique values. If there are duplicates values in
  703. the first column, entries in the associative array will be
  704. overwritten.
  705. </para>
  706. <example id="zend.db.adapter.select.fetchpairs.example">
  707. <title>Using fetchPairs()</title>
  708. <programlisting language="php"><![CDATA[
  709. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  710. $result = $db->fetchPairs('SELECT bug_id, bug_status FROM bugs');
  711. echo $result[2];
  712. ]]></programlisting>
  713. </example>
  714. </sect3>
  715. <sect3 id="zend.db.adapter.select.fetchrow">
  716. <title>Fetching a Single Row from a Result Set</title>
  717. <para>
  718. The <methodname>fetchRow()</methodname> method returns data using the
  719. current fetch mode, but it returns only the first row
  720. fetched from the result set.
  721. </para>
  722. <example id="zend.db.adapter.select.fetchrow.example">
  723. <title>Using fetchRow()</title>
  724. <programlisting language="php"><![CDATA[
  725. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  726. $result = $db->fetchRow('SELECT * FROM bugs WHERE bug_id = 2');
  727. // note that $result is a single object, not an array of objects
  728. echo $result->bug_description;
  729. ]]></programlisting>
  730. </example>
  731. </sect3>
  732. <sect3 id="zend.db.adapter.select.fetchone">
  733. <title>Fetching a Single Scalar from a Result Set</title>
  734. <para>
  735. The <methodname>fetchOne()</methodname> method is like a combination
  736. of <methodname>fetchRow()</methodname> with <methodname>fetchCol()</methodname>,
  737. in that it returns data only for the first row fetched from
  738. the result set, and it returns only the value of the first
  739. column in that row. Therefore it returns only a single
  740. scalar value, not an array or an object.
  741. </para>
  742. <example id="zend.db.adapter.select.fetchone.example">
  743. <title>Using fetchOne()</title>
  744. <programlisting language="php"><![CDATA[
  745. $result = $db->fetchOne('SELECT bug_status FROM bugs WHERE bug_id = 2');
  746. // this is a single string value
  747. echo $result;
  748. ]]></programlisting>
  749. </example>
  750. </sect3>
  751. </sect2>
  752. <sect2 id="zend.db.adapter.write">
  753. <title>Writing Changes to the Database</title>
  754. <para>
  755. You can use the Adapter class to write new data or change existing
  756. data in your database. This section describes methods to do these
  757. operations.
  758. </para>
  759. <sect3 id="zend.db.adapter.write.insert">
  760. <title>Inserting Data</title>
  761. <para>
  762. You can add new rows to a table in your database using the
  763. <methodname>insert()</methodname> method. The first argument is a string
  764. that names the table, and the second argument is an associative
  765. array, mapping column names to data values.
  766. </para>
  767. <example id="zend.db.adapter.write.insert.example">
  768. <title>Inserting in a Table</title>
  769. <programlisting language="php"><![CDATA[
  770. $data = array(
  771. 'created_on' => '2007-03-22',
  772. 'bug_description' => 'Something wrong',
  773. 'bug_status' => 'NEW'
  774. );
  775. $db->insert('bugs', $data);
  776. ]]></programlisting>
  777. </example>
  778. <para>
  779. Columns you exclude from the array of data are not specified to
  780. the database. Therefore, they follow the same rules that an
  781. SQL INSERT statement follows: if the column has a DEFAULT
  782. clause, the column takes that value in the row created,
  783. otherwise the column is left in a NULL state.
  784. </para>
  785. <para>
  786. By default, the values in your data array are inserted using
  787. parameters. This reduces risk of some types of security
  788. issues. You don't need to apply escaping or quoting to values
  789. in the data array.
  790. </para>
  791. <para>
  792. You might need values in the data array to be treated as SQL
  793. expressions, in which case they should not be quoted. By
  794. default, all data values passed as strings are treated as
  795. string literals. To specify that the value is an SQL
  796. expression and therefore should not be quoted, pass the value
  797. in the data array as an object of type <classname>Zend_Db_Expr</classname> instead
  798. of a plain string.
  799. </para>
  800. <example id="zend.db.adapter.write.insert.example2">
  801. <title>Inserting Expressions in a Table</title>
  802. <programlisting language="php"><![CDATA[
  803. $data = array(
  804. 'created_on' => new Zend_Db_Expr('CURDATE()'),
  805. 'bug_description' => 'Something wrong',
  806. 'bug_status' => 'NEW'
  807. );
  808. $db->insert('bugs', $data);
  809. ]]></programlisting>
  810. </example>
  811. </sect3>
  812. <sect3 id="zend.db.adapter.write.lastinsertid">
  813. <title>Retrieving a Generated Value</title>
  814. <para>
  815. Some RDBMS brands support auto-incrementing primary keys.
  816. A table defined this way generates a primary key value
  817. automatically during an INSERT of a new row. The return value
  818. of the <methodname>insert()</methodname> method is <emphasis>not</emphasis>
  819. the last inserted ID, because the table might not have an
  820. auto-incremented column. Instead, the return value is the
  821. number of rows affected (usually 1).
  822. </para>
  823. <para>
  824. If your table is defined with an auto-incrementing primary key,
  825. you can call the <methodname>lastInsertId()</methodname> method after the
  826. insert. This method returns the last value generated in the
  827. scope of the current database connection.
  828. </para>
  829. <example id="zend.db.adapter.write.lastinsertid.example-1">
  830. <title>Using lastInsertId() for an Auto-Increment Key</title>
  831. <programlisting language="php"><![CDATA[
  832. $db->insert('bugs', $data);
  833. // return the last value generated by an auto-increment column
  834. $id = $db->lastInsertId();
  835. ]]></programlisting>
  836. </example>
  837. <para>
  838. Some RDBMS brands support a sequence object, which generates
  839. unique values to serve as primary key values. To support
  840. sequences, the <methodname>lastInsertId()</methodname> method accepts two
  841. optional string arguments. These arguments name the table and
  842. the column, assuming you have followed the convention that a
  843. sequence is named using the table and column names for which
  844. the sequence generates values, and a suffix "_seq". This is
  845. based on the convention used by PostgreSQL when naming
  846. sequences for SERIAL columns. For example, a table "bugs" with
  847. primary key column "bug_id" would use a sequence named
  848. "bugs_bug_id_seq".
  849. </para>
  850. <example id="zend.db.adapter.write.lastinsertid.example-2">
  851. <title>Using lastInsertId() for a Sequence</title>
  852. <programlisting language="php"><![CDATA[
  853. $db->insert('bugs', $data);
  854. // return the last value generated by sequence 'bugs_bug_id_seq'.
  855. $id = $db->lastInsertId('bugs', 'bug_id');
  856. // alternatively, return the last value generated by sequence 'bugs_seq'.
  857. $id = $db->lastInsertId('bugs');
  858. ]]></programlisting>
  859. </example>
  860. <para>
  861. If the name of your sequence object does not follow this naming
  862. convention, use the <methodname>lastSequenceId()</methodname> method
  863. instead. This method takes a single string argument, naming
  864. the sequence literally.
  865. </para>
  866. <example id="zend.db.adapter.write.lastinsertid.example-3">
  867. <title>Using lastSequenceId()</title>
  868. <programlisting language="php"><![CDATA[
  869. $db->insert('bugs', $data);
  870. // return the last value generated by sequence 'bugs_id_gen'.
  871. $id = $db->lastSequenceId('bugs_id_gen');
  872. ]]></programlisting>
  873. </example>
  874. <para>
  875. For RDBMS brands that don't support sequences, including MySQL,
  876. Microsoft SQL Server, and SQLite, the arguments to the
  877. lastInsertId() method are ignored, and the value returned is the
  878. most recent value generated for any table by INSERT operations
  879. during the current connection. For these RDBMS brands, the
  880. lastSequenceId() method always returns <constant>NULL</constant>.
  881. </para>
  882. <note>
  883. <title>Why Not Use "SELECT MAX(id) FROM table"?</title>
  884. <para>
  885. Sometimes this query returns the most recent primary key
  886. value inserted into the table. However, this technique
  887. is not safe to use in an environment where multiple clients are
  888. inserting records to the database. It is possible, and
  889. therefore is bound to happen eventually, that another
  890. client inserts another row in the instant between the
  891. insert performed by your client application and your query
  892. for the MAX(id) value. Thus the value returned does not
  893. identify the row you inserted, it identifies the row
  894. inserted by some other client. There is no way to know
  895. when this has happened.
  896. </para>
  897. <para>
  898. Using a strong transaction isolation mode such as
  899. "repeatable read" can mitigate this risk, but some RDBMS
  900. brands don't support the transaction isolation required for
  901. this, or else your application may use a lower transaction
  902. isolation mode by design.
  903. </para>
  904. <para>
  905. Furthermore, using an expression like "MAX(id)+1" to generate
  906. a new value for a primary key is not safe, because two clients
  907. could do this query simultaneously, and then both use the same
  908. calculated value for their next INSERT operation.
  909. </para>
  910. <para>
  911. All RDBMS brands provide mechanisms to generate unique
  912. values, and to return the last value generated. These
  913. mechanisms necessarily work outside of the scope of
  914. transaction isolation, so there is no chance of two clients
  915. generating the same value, and there is no chance that the
  916. value generated by another client could be reported to your
  917. client's connection as the last value generated.
  918. </para>
  919. </note>
  920. </sect3>
  921. <sect3 id="zend.db.adapter.write.update">
  922. <title>Updating Data</title>
  923. <para>
  924. You can update rows in a database table using the
  925. <methodname>update()</methodname> method of an Adapter. This method takes
  926. three arguments: the first is the name of the table; the
  927. second is an associative array mapping columns to change to new
  928. values to assign to these columns.
  929. </para>
  930. <para>
  931. The values in the data array are treated as string literals.
  932. See <xref linkend="zend.db.adapter.write.insert" />
  933. for information on using SQL expressions in the data array.
  934. </para>
  935. <para>
  936. The third argument is a string containing an SQL expression
  937. that is used as criteria for the rows to change. The values
  938. and identifiers in this argument are not quoted or escaped.
  939. You are responsible for ensuring that any dynamic content is
  940. interpolated into this string safely.
  941. See <xref linkend="zend.db.adapter.quoting" />
  942. for methods to help you do this.
  943. </para>
  944. <para>
  945. The return value is the number of rows affected by the update
  946. operation.
  947. </para>
  948. <example id="zend.db.adapter.write.update.example">
  949. <title>Updating Rows</title>
  950. <programlisting language="php"><![CDATA[
  951. $data = array(
  952. 'updated_on' => '2007-03-23',
  953. 'bug_status' => 'FIXED'
  954. );
  955. $n = $db->update('bugs', $data, 'bug_id = 2');
  956. ]]></programlisting>
  957. </example>
  958. <para>
  959. If you omit the third argument, then all rows in the database
  960. table are updated with the values specified in the data array.
  961. </para>
  962. <para>
  963. If you provide an array of strings as the third argument, these
  964. strings are joined together as terms in an expression separated
  965. by <constant>AND</constant> operators.
  966. </para>
  967. <example id="zend.db.adapter.write.update.example-array">
  968. <title>Updating Rows Using an Array of Expressions</title>
  969. <programlisting language="php"><![CDATA[
  970. $data = array(
  971. 'updated_on' => '2007-03-23',
  972. 'bug_status' => 'FIXED'
  973. );
  974. $where[] = "reported_by = 'goofy'";
  975. $where[] = "bug_status = 'OPEN'";
  976. $n = $db->update('bugs', $data, $where);
  977. // Resulting SQL is:
  978. // UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED'
  979. // WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')
  980. ]]></programlisting>
  981. </example>
  982. </sect3>
  983. <sect3 id="zend.db.adapter.write.delete">
  984. <title>Deleting Data</title>
  985. <para>
  986. You can delete rows from a database table using the
  987. <methodname>delete()</methodname> method. This method takes two arguments:
  988. the first is a string naming the table.
  989. </para>
  990. <para>
  991. The second argument is a string containing an SQL expression
  992. that is used as criteria for the rows to delete. The values
  993. and identifiers in this argument are not quoted or escaped.
  994. You are responsible for ensuring that any dynamic content is
  995. interpolated into this string safely.
  996. See <xref linkend="zend.db.adapter.quoting" />
  997. for methods to help you do this.
  998. </para>
  999. <para>
  1000. The return value is the number of rows affected by the delete
  1001. operation.
  1002. </para>
  1003. <example id="zend.db.adapter.write.delete.example">
  1004. <title>Deleting Rows</title>
  1005. <programlisting language="php"><![CDATA[
  1006. $n = $db->delete('bugs', 'bug_id = 3');
  1007. ]]></programlisting>
  1008. </example>
  1009. <para>
  1010. If you omit the second argument, the result is that all rows in
  1011. the database table are deleted.
  1012. </para>
  1013. <para>
  1014. If you provide an array of strings as the second argument, these
  1015. strings are joined together as terms in an expression separated
  1016. by <constant>AND</constant> operators.
  1017. </para>
  1018. </sect3>
  1019. </sect2>
  1020. <sect2 id="zend.db.adapter.quoting">
  1021. <title>Quoting Values and Identifiers</title>
  1022. <para>
  1023. When you form SQL queries, often it is the case that you need to
  1024. include the values of PHP variables in SQL expressions. This is
  1025. risky, because if the value in a PHP string contains certain
  1026. symbols, such as the quote symbol, it could result in invalid SQL.
  1027. For example, notice the imbalanced quote characters in the
  1028. following query:
  1029. <programlisting language="php"><![CDATA[
  1030. $name = "O'Reilly";
  1031. $sql = "SELECT * FROM bugs WHERE reported_by = '$name'";
  1032. echo $sql;
  1033. // SELECT * FROM bugs WHERE reported_by = 'O'Reilly'
  1034. ]]></programlisting>
  1035. </para>
  1036. <para>
  1037. Even worse is the risk that such code mistakes might be exploited
  1038. deliberately by a person who is trying to manipulate the function
  1039. of your web application. If they can specify the value of a PHP
  1040. variable through the use of an HTTP parameter or other mechanism,
  1041. they might be able to make your SQL queries do things that you
  1042. didn't intend them to do, such as return data to which the person
  1043. should not have privilege to read. This is a serious and widespread
  1044. technique for violating application security, known as "SQL Injection" (see <ulink
  1045. url="http://en.wikipedia.org/wiki/SQL_Injection">http://en.wikipedia.org/wiki/SQL_Injection</ulink>).
  1046. </para>
  1047. <para>
  1048. The <classname>Zend_Db</classname> Adapter class provides convenient functions to help
  1049. you reduce vulnerabilities to SQL Injection attacks in your PHP code.
  1050. The solution is to escape special characters such as quotes in PHP
  1051. values before they are interpolated into your SQL strings.
  1052. This protects against both accidental and deliberate manipulation
  1053. of SQL strings by PHP variables that contain special characters.
  1054. </para>
  1055. <sect3 id="zend.db.adapter.quoting.quote">
  1056. <title>Using quote()</title>
  1057. <para>
  1058. The <methodname>quote()</methodname> method accepts a single argument, a
  1059. scalar string value. It returns the value with special
  1060. characters escaped in a manner appropriate for the RDBMS you
  1061. are using, and surrounded by string value delimiters. The
  1062. standard SQL string value delimiter is the single-quote
  1063. (<code>'</code>).
  1064. </para>
  1065. <example id="zend.db.adapter.quoting.quote.example">
  1066. <title>Using quote()</title>
  1067. <programlisting language="php"><![CDATA[
  1068. $name = $db->quote("O'Reilly");
  1069. echo $name;
  1070. // 'O\'Reilly'
  1071. $sql = "SELECT * FROM bugs WHERE reported_by = $name";
  1072. echo $sql;
  1073. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
  1074. ]]></programlisting>
  1075. </example>
  1076. <para>
  1077. Note that the return value of <methodname>quote()</methodname> includes the
  1078. quote delimiters around the string. This is different from
  1079. some functions that escape special characters but do not add
  1080. the quote delimiters, for example <ulink
  1081. url="http://www.php.net/mysqli_real_escape_string">mysql_real_escape_string()</ulink>.
  1082. </para>
  1083. <para>
  1084. Values may need to be quoted or not quoted according to the SQL
  1085. datatype context in which they are used. For instance, in some
  1086. RDBMS brands, an integer value must not be quoted as a string
  1087. if it is compared to an integer-type column or expression.
  1088. In other words, the following is an error in some SQL
  1089. implementations, assuming <code>intColumn</code> has a SQL
  1090. datatype of <constant>INTEGER</constant>
  1091. <programlisting language="php"><![CDATA[
  1092. SELECT * FROM atable WHERE intColumn = '123'
  1093. ]]></programlisting>
  1094. </para>
  1095. <para>
  1096. You can use the optional second argument to the
  1097. <methodname>quote()</methodname> method to apply quoting selectively for
  1098. the SQL datatype you specify.
  1099. </para>
  1100. <example id="zend.db.adapter.quoting.quote.example-2">
  1101. <title>Using quote() with a SQL Type</title>
  1102. <programlisting language="php"><![CDATA[
  1103. $value = '1234';
  1104. $sql = 'SELECT * FROM atable WHERE intColumn = '
  1105. . $db->quote($value, 'INTEGER');
  1106. ]]></programlisting>
  1107. </example>
  1108. <para>
  1109. Each <classname>Zend_Db_Adapter</classname> class has encoded the names of numeric
  1110. SQL datatypes for the respective brand of RDBMS. You can also
  1111. use the constants <classname>Zend_Db::INT_TYPE</classname>,
  1112. <classname>Zend_Db::BIGINT_TYPE</classname>, and
  1113. <classname>Zend_Db::FLOAT_TYPE</classname> to write code in a more
  1114. RDBMS-independent way.
  1115. </para>
  1116. <para>
  1117. <classname>Zend_Db_Table</classname> specifies SQL types to
  1118. <methodname>quote()</methodname> automatically when generating SQL queries that
  1119. reference a table's key columns.
  1120. </para>
  1121. </sect3>
  1122. <sect3 id="zend.db.adapter.quoting.quote-into">
  1123. <title>Using quoteInto()</title>
  1124. <para>
  1125. The most typical usage of quoting is to interpolate a PHP
  1126. variable into a SQL expression or statement. You can use the
  1127. <methodname>quoteInto()</methodname> method to do this in one step. This
  1128. method takes two arguments: the first argument is a string
  1129. containing a placeholder symbol (<code>?</code>), and the
  1130. second argument is a value or PHP variable that should be
  1131. substituted for that placeholder.
  1132. </para>
  1133. <para>
  1134. The placeholder symbol is the same symbol used by many RDBMS
  1135. brands for positional parameters, but the
  1136. <methodname>quoteInto()</methodname> method only emulates query parameters.
  1137. The method simply interpolates the value into the string,
  1138. escapes special characters, and applies quotes around it.
  1139. True query parameters maintain the separation between the SQL
  1140. string and the parameters as the statement is parsed in the
  1141. RDBMS server.
  1142. </para>
  1143. <example id="zend.db.adapter.quoting.quote-into.example">
  1144. <title>Using quoteInto()</title>
  1145. <programlisting language="php"><![CDATA[
  1146. $sql = $db->quoteInto("SELECT * FROM bugs WHERE reported_by = ?", "O'Reilly");
  1147. echo $sql;
  1148. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
  1149. ]]></programlisting>
  1150. </example>
  1151. <para>
  1152. You can use the optional third parameter of
  1153. <methodname>quoteInto()</methodname> to specify the SQL datatype. Numeric
  1154. datatypes are not quoted, and other types are quoted.
  1155. </para>
  1156. <example id="zend.db.adapter.quoting.quote-into.example-2">
  1157. <title>Using quoteInto() with a SQL Type</title>
  1158. <programlisting language="php"><![CDATA[
  1159. $sql = $db
  1160. ->quoteInto("SELECT * FROM bugs WHERE bug_id = ?", '1234', 'INTEGER');
  1161. echo $sql;
  1162. // SELECT * FROM bugs WHERE reported_by = 1234
  1163. ]]></programlisting>
  1164. </example>
  1165. </sect3>
  1166. <sect3 id="zend.db.adapter.quoting.quote-identifier">
  1167. <title>Using quoteIdentifier()</title>
  1168. <para>
  1169. Values are not the only part of SQL syntax that might need to
  1170. be variable. If you use PHP variables to name tables, columns,
  1171. or other identifiers in your SQL statements, you might need to
  1172. quote these strings too. By default, SQL identifiers have
  1173. syntax rules like PHP and most other programming languages.
  1174. For example, identifiers should not contain spaces, certain
  1175. punctuation or special characters, or international characters.
  1176. Also certain words are reserved for SQL syntax, and should not
  1177. be used as identifiers.
  1178. </para>
  1179. <para>
  1180. However, SQL has a feature called <emphasis>delimited identifiers</emphasis>,
  1181. which allows broader choices for the spelling of identifiers.
  1182. If you enclose a SQL identifier in the proper types of quotes,
  1183. you can use identifiers with spellings that would be invalid
  1184. without the quotes. Delimited identifiers can contain spaces,
  1185. punctuation, or international characters. You can also use SQL
  1186. reserved words if you enclose them in identifier delimiters.
  1187. </para>
  1188. <para>
  1189. The <methodname>quoteIdentifier()</methodname> method works like
  1190. <methodname>quote()</methodname>, but it applies the identifier delimiter
  1191. characters to the string according to the type of Adapter you
  1192. use. For example, standard SQL uses double-quotes
  1193. (<code>"</code>) for identifier delimiters, and most RDBMS
  1194. brands use that symbol. MySQL uses back-quotes
  1195. (<code>`</code>) by default. The
  1196. <methodname>quoteIdentifier()</methodname> method also escapes special
  1197. characters within the string argument.
  1198. </para>
  1199. <example id="zend.db.adapter.quoting.quote-identifier.example">
  1200. <title>Using quoteIdentifier()</title>
  1201. <programlisting language="php"><![CDATA[
  1202. // we might have a table name that is an SQL reserved word
  1203. $tableName = $db->quoteIdentifier("order");
  1204. $sql = "SELECT * FROM $tableName";
  1205. echo $sql
  1206. // SELECT * FROM "order"
  1207. ]]></programlisting>
  1208. </example>
  1209. <para>
  1210. SQL delimited identifiers are case-sensitive, unlike unquoted
  1211. identifiers. Therefore, if you use delimited identifiers, you
  1212. must use the spelling of the identifier exactly as it is stored
  1213. in your schema, including the case of the letters.
  1214. </para>
  1215. <para>
  1216. In most cases where SQL is generated within <classname>Zend_Db</classname> classes,
  1217. the default is that all identifiers are delimited
  1218. automatically. You can change this behavior with the option
  1219. <classname>Zend_Db::AUTO_QUOTE_IDENTIFIERS</classname>. Specify this
  1220. when instantiating the Adapter.
  1221. See <xref linkend="zend.db.adapter.connecting.parameters.example2" />.
  1222. </para>
  1223. </sect3>
  1224. </sect2>
  1225. <sect2 id="zend.db.adapter.transactions">
  1226. <title>Controlling Database Transactions</title>
  1227. <para>
  1228. Databases define transactions as logical units of work that can be
  1229. committed or rolled back as a single change, even if they operate
  1230. on multiple tables. All queries to a database are executed within
  1231. the context of a transaction, even if the database driver manages
  1232. them implicitly. This is called <emphasis>auto-commit</emphasis>
  1233. mode, in which the database driver creates a transaction for every
  1234. statement you execute, and commits that transaction after your
  1235. SQL statement has been executed. By default, all <classname>Zend_Db</classname> Adapter
  1236. classes operate in auto-commit mode.
  1237. </para>
  1238. <para>
  1239. Alternatively, you can specify the beginning and resolution of a
  1240. transaction, and thus control how many SQL queries are included in
  1241. a single group that is committed (or rolled back) as a single
  1242. operation. Use the <methodname>beginTransaction()</methodname> method to
  1243. initiate a transaction. Subsequent SQL statements are executed in
  1244. the context of the same transaction until you resolve it
  1245. explicitly.
  1246. </para>
  1247. <para>
  1248. To resolve the transaction, use either the <methodname>commit()</methodname> or
  1249. <methodname>rollBack()</methodname> methods. The <methodname>commit()</methodname>
  1250. method marks changes made during your transaction as committed, which
  1251. means the effects of these changes are shown in queries run in
  1252. other transactions.
  1253. </para>
  1254. <para>
  1255. The <methodname>rollBack()</methodname> method does the opposite: it discards
  1256. the changes made during your transaction. The changes are
  1257. effectively undone, and the state of the data returns to how it was
  1258. before you began your transaction. However, rolling back your
  1259. transaction has no effect on changes made by other transactions
  1260. running concurrently.
  1261. </para>
  1262. <para>
  1263. After you resolve this transaction, <classname>Zend_Db_Adapter</classname>
  1264. returns to auto-commit mode until you call
  1265. <methodname>beginTransaction()</methodname> again.
  1266. </para>
  1267. <example id="zend.db.adapter.transactions.example">
  1268. <title>Managing a Transaction to Ensure Consistency</title>
  1269. <programlisting language="php"><![CDATA[
  1270. // Start a transaction explicitly.
  1271. $db->beginTransaction();
  1272. try {
  1273. // Attempt to execute one or more queries:
  1274. $db->query(...);
  1275. $db->query(...);
  1276. $db->query(...);
  1277. // If all succeed, commit the transaction and all changes
  1278. // are committed at once.
  1279. $db->commit();
  1280. } catch (Exception $e) {
  1281. // If any of the queries failed and threw an exception,
  1282. // we want to roll back the whole transaction, reversing
  1283. // changes made in the transaction, even those that succeeded.
  1284. // Thus all changes are committed together, or none are.
  1285. $db->rollBack();
  1286. echo $e->getMessage();
  1287. }
  1288. ]]></programlisting>
  1289. </example>
  1290. </sect2>
  1291. <sect2 id="zend.db.adapter.list-describe">
  1292. <title>Listing and Describing Tables</title>
  1293. <para>
  1294. The <methodname>listTables()</methodname> method returns an array of strings,
  1295. naming all tables in the current database.
  1296. </para>
  1297. <para>
  1298. The <methodname>describeTable()</methodname> method returns an associative
  1299. array of metadata about a table. Specify the name of the table
  1300. as a string in the first argument to this method. The second
  1301. argument is optional, and names the schema in which the table
  1302. exists.
  1303. </para>
  1304. <para>
  1305. The keys of the associative array returned are the column names of
  1306. the table. The value corresponding to each column is also an
  1307. associative array, with the following keys and values:
  1308. </para>
  1309. <table frame="all" cellpadding="5" id="zend.db.adapter.list-describe.metadata">
  1310. <title>Metadata Fields Returned by describeTable()</title>
  1311. <tgroup cols="3" align="left" colsep="1" rowsep="1">
  1312. <thead>
  1313. <row>
  1314. <entry>Key</entry>
  1315. <entry>Type</entry>
  1316. <entry>Description</entry>
  1317. </row>
  1318. </thead>
  1319. <tbody>
  1320. <row>
  1321. <entry>SCHEMA_NAME</entry>
  1322. <entry>(string)</entry>
  1323. <entry>Name of the database schema in which this table exists.</entry>
  1324. </row>
  1325. <row>
  1326. <entry>TABLE_NAME</entry>
  1327. <entry>(string)</entry>
  1328. <entry>Name of the table to which this column belongs.</entry>
  1329. </row>
  1330. <row>
  1331. <entry>COLUMN_NAME</entry>
  1332. <entry>(string)</entry>
  1333. <entry>Name of the column.</entry>
  1334. </row>
  1335. <row>
  1336. <entry>COLUMN_POSITION</entry>
  1337. <entry>(integer)</entry>
  1338. <entry>Ordinal position of the column in the table.</entry>
  1339. </row>
  1340. <row>
  1341. <entry>DATA_TYPE</entry>
  1342. <entry>(string)</entry>
  1343. <entry>RDBMS name of the datatype of the column.</entry>
  1344. </row>
  1345. <row>
  1346. <entry>DEFAULT</entry>
  1347. <entry>(string)</entry>
  1348. <entry>Default value for the column, if any.</entry>
  1349. </row>
  1350. <row>
  1351. <entry>NULLABLE</entry>
  1352. <entry>(boolean)</entry>
  1353. <entry>
  1354. True if the column accepts SQL NULLs, false if the column has a NOT NULL
  1355. constraint.
  1356. </entry>
  1357. </row>
  1358. <row>
  1359. <entry>LENGTH</entry>
  1360. <entry>(integer)</entry>
  1361. <entry>Length or size of the column as reported by the RDBMS.</entry>
  1362. </row>
  1363. <row>
  1364. <entry>SCALE</entry>
  1365. <entry>(integer)</entry>
  1366. <entry>Scale of SQL NUMERIC or DECIMAL type.</entry>
  1367. </row>
  1368. <row>
  1369. <entry>PRECISION</entry>
  1370. <entry>(integer)</entry>
  1371. <entry>Precision of SQL NUMERIC or DECIMAL type.</entry>
  1372. </row>
  1373. <row>
  1374. <entry>UNSIGNED</entry>
  1375. <entry>(boolean)</entry>
  1376. <entry>True if an integer-based type is reported as UNSIGNED.</entry>
  1377. </row>
  1378. <row>
  1379. <entry>PRIMARY</entry>
  1380. <entry>(boolean)</entry>
  1381. <entry>True if the column is part of the primary key of this table.</entry>
  1382. </row>
  1383. <row>
  1384. <entry>PRIMARY_POSITION</entry>
  1385. <entry>(integer)</entry>
  1386. <entry>Ordinal position (1-based) of the column in the primary key.</entry>
  1387. </row>
  1388. <row>
  1389. <entry>IDENTITY</entry>
  1390. <entry>(boolean)</entry>
  1391. <entry>True if the column uses an auto-generated value.</entry>
  1392. </row>
  1393. </tbody>
  1394. </tgroup>
  1395. </table>
  1396. <note>
  1397. <title>How the IDENTITY Metadata Field Relates to Specific RDBMSs</title>
  1398. <para>
  1399. The IDENTITY metadata field was chosen as an 'idiomatic' term to
  1400. represent a relation to surrogate keys. This field can be
  1401. commonly known by the following values:-
  1402. </para>
  1403. <itemizedlist>
  1404. <listitem>
  1405. <para>
  1406. <constant>IDENTITY</constant> - DB2, MSSQL
  1407. </para>
  1408. </listitem>
  1409. <listitem>
  1410. <para>
  1411. <constant>AUTO_INCREMENT</constant> - MySQL
  1412. </para>
  1413. </listitem>
  1414. <listitem>
  1415. <para>
  1416. <constant>SERIAL</constant> - PostgreSQL
  1417. </para>
  1418. </listitem>
  1419. <listitem>
  1420. <para>
  1421. <constant>SEQUENCE</constant> - Oracle
  1422. </para>
  1423. </listitem>
  1424. </itemizedlist>
  1425. </note>
  1426. <para>
  1427. If no table exists matching the table name and optional schema name
  1428. specified, then <methodname>describeTable()</methodname> returns an empty array.
  1429. </para>
  1430. </sect2>
  1431. <sect2 id="zend.db.adapter.closing">
  1432. <title>Closing a Connection</title>
  1433. <para>
  1434. Normally it is not necessary to close a database connection. PHP
  1435. automatically cleans up all resources and the end of a request.
  1436. Database extensions are designed to close the connection as the
  1437. reference to the resource object is cleaned up.
  1438. </para>
  1439. <para>
  1440. However, if you have a long-duration PHP script that initiates many
  1441. database connections, you might need to close the connection, to avoid
  1442. exhausting the capacity of your RDBMS server. You can use the
  1443. Adapter's <methodname>closeConnection()</methodname> method to explicitly close
  1444. the underlying database connection.
  1445. </para>
  1446. <para>
  1447. Since release 1.7.2, you could check you are currently connected to the RDBMS
  1448. server with the method <methodname>isConnected()</methodname>. This means that a
  1449. connection resource has been initiated and wasn't closed. This function is not
  1450. currently able to test for example a server side closing of the connection. This is
  1451. internally use to close the connection. It allow you to close the connection
  1452. multiple times without errors. It was already the case before 1.7.2 for PDO
  1453. adapters but not for the others.
  1454. </para>
  1455. <example id="zend.db.adapter.closing.example">
  1456. <title>Closing a Database Connection</title>
  1457. <programlisting language="php"><![CDATA[
  1458. $db->closeConnection();
  1459. ]]></programlisting>
  1460. </example>
  1461. <note>
  1462. <title>Does Zend_Db Support Persistent Connections?</title>
  1463. <para>
  1464. The usage of persistent connections is not supported
  1465. or encouraged in <classname>Zend_Db</classname>.
  1466. </para>
  1467. <para>
  1468. Using persistent connections can cause an excess of idle
  1469. connections on the RDBMS server, which causes more problems
  1470. than any performance gain you might achieve by reducing the
  1471. overhead of making connections.
  1472. </para>
  1473. <para>
  1474. Database connections have state. That is, some objects in the
  1475. RDBMS server exist in session scope. Examples are locks, user
  1476. variables, temporary tables, and information about the most
  1477. recently executed query, such as rows affected, and last
  1478. generated id value. If you use persistent connections, your
  1479. application could access invalid or privileged data that were
  1480. created in a previous PHP request.
  1481. </para>
  1482. </note>
  1483. </sect2>
  1484. <sect2 id="zend.db.adapter.other-statements">
  1485. <title>Running Other Database Statements</title>
  1486. <para>
  1487. There might be cases in which you need to access the connection
  1488. object directly, as provided by the PHP database extension. Some
  1489. of these extensions may offer features that are not surfaced by
  1490. methods of <classname>Zend_Db_Adapter_Abstract</classname>.
  1491. </para>
  1492. <para>
  1493. For example, all SQL statements run by <classname>Zend_Db</classname> are prepared, then
  1494. executed. However, some database features are incompatible with
  1495. prepared statements. DDL statements like CREATE and ALTER cannot
  1496. be prepared in MySQL. Also, SQL statements don't benefit from the <ulink
  1497. url="http://dev.mysql.com/doc/refman/5.1/en/query-cache-how.html">MySQL Query
  1498. Cache</ulink>, prior to MySQL 5.1.17.
  1499. </para>
  1500. <para>
  1501. Most PHP database extensions provide a method to execute SQL
  1502. statements without preparing them. For example, in PDO, this
  1503. method is <methodname>exec()</methodname>. You can access the connection
  1504. object in the PHP extension directly using getConnection().
  1505. </para>
  1506. <example id="zend.db.adapter.other-statements.example">
  1507. <title>Running a Non-Prepared Statement in a PDO Adapter</title>
  1508. <programlisting language="php"><![CDATA[
  1509. $result = $db->getConnection()->exec('DROP TABLE bugs');
  1510. ]]></programlisting>
  1511. </example>
  1512. <para>
  1513. Similarly, you can access other methods or properties that are
  1514. specific to PHP database extensions. Be aware, though, that by
  1515. doing this you might constrain your application to the interface
  1516. provided by the extension for a specific brand of RDBMS.
  1517. </para>
  1518. <para>
  1519. In future versions of <classname>Zend_Db</classname>, there will be opportunities to
  1520. add method entry points for functionality that is common to
  1521. the supported PHP database extensions. This will not affect
  1522. backward compatibility.
  1523. </para>
  1524. </sect2>
  1525. <sect2 id="zend.db.adapter.server-version">
  1526. <title>Retrieving Server Version</title>
  1527. <para>
  1528. Since release 1.7.2, you could retrieve the server version in PHP syntax
  1529. style to be able to use <methodname>version_compare()</methodname>. If the information
  1530. isn't available, you will receive <constant>NULL</constant>.
  1531. </para>
  1532. <example id="zend.db.adapter.server-version.example">
  1533. <title>Verifying server version before running a query</title>
  1534. <programlisting language="php"><![CDATA[
  1535. $version = $db->getServerVersion();
  1536. if (!is_null($version)) {
  1537. if (version_compare($version, '5.0.0', '>=')) {
  1538. // do something
  1539. } else {
  1540. // do something else
  1541. }
  1542. } else {
  1543. // impossible to read server version
  1544. }
  1545. ]]></programlisting>
  1546. </example>
  1547. </sect2>
  1548. <sect2 id="zend.db.adapter.adapter-notes">
  1549. <title>Notes on Specific Adapters</title>
  1550. <para>
  1551. This section lists differences between the Adapter classes of which
  1552. you should be aware.
  1553. </para>
  1554. <sect3 id="zend.db.adapter.adapter-notes.ibm-db2">
  1555. <title>IBM DB2</title>
  1556. <itemizedlist>
  1557. <listitem>
  1558. <para>
  1559. Specify this Adapter to the factory() method with the
  1560. name 'Db2'.
  1561. </para>
  1562. </listitem>
  1563. <listitem>
  1564. <para>
  1565. This Adapter uses the PHP extension ibm_db2.
  1566. </para>
  1567. </listitem>
  1568. <listitem>
  1569. <para>
  1570. IBM DB2 supports both sequences and auto-incrementing
  1571. keys. Therefore the arguments to
  1572. <methodname>lastInsertId()</methodname> are optional. If you give
  1573. no arguments, the Adapter returns the last value
  1574. generated for an auto-increment key. If you give
  1575. arguments, the Adapter returns the last value generated
  1576. by the sequence named according to the convention
  1577. '<emphasis>table</emphasis>_<emphasis>column</emphasis>_seq'.
  1578. </para>
  1579. </listitem>
  1580. </itemizedlist>
  1581. </sect3>
  1582. <sect3 id="zend.db.adapter.adapter-notes.mysqli">
  1583. <title>MySQLi</title>
  1584. <itemizedlist>
  1585. <listitem>
  1586. <para>
  1587. Specify this Adapter to the <methodname>factory()</methodname>
  1588. method with the name 'Mysqli'.
  1589. </para>
  1590. </listitem>
  1591. <listitem>
  1592. <para>
  1593. This Adapter utilizes the PHP extension mysqli.
  1594. </para>
  1595. </listitem>
  1596. <listitem>
  1597. <para>
  1598. MySQL does not support sequences, so
  1599. <methodname>lastInsertId()</methodname> ignores its arguments and
  1600. always returns the last value generated for an
  1601. auto-increment key. The <methodname>lastSequenceId()</methodname>
  1602. method returns <constant>NULL</constant>.
  1603. </para>
  1604. </listitem>
  1605. </itemizedlist>
  1606. </sect3>
  1607. <sect3 id="zend.db.adapter.adapter-notes.oracle">
  1608. <title>Oracle</title>
  1609. <itemizedlist>
  1610. <listitem>
  1611. <para>
  1612. Specify this Adapter to the <methodname>factory()</methodname>
  1613. method with the name 'Oracle'.
  1614. </para>
  1615. </listitem>
  1616. <listitem>
  1617. <para>
  1618. This Adapter uses the PHP extension oci8.
  1619. </para>
  1620. </listitem>
  1621. <listitem>
  1622. <para>
  1623. Oracle does not support auto-incrementing keys, so you
  1624. should specify the name of a sequence to
  1625. <methodname>lastInsertId()</methodname> or
  1626. <methodname>lastSequenceId()</methodname>.
  1627. </para>
  1628. </listitem>
  1629. <listitem>
  1630. <para>
  1631. The Oracle extension does not support positional
  1632. parameters. You must use named parameters.
  1633. </para>
  1634. </listitem>
  1635. <listitem>
  1636. <para>
  1637. Currently the <classname>Zend_Db::CASE_FOLDING</classname> option
  1638. is not supported by the Oracle adapter. To use this
  1639. option with Oracle, you must use the PDO OCI adapter.
  1640. </para>
  1641. </listitem>
  1642. <listitem>
  1643. <para>
  1644. By default, LOB fields are returned as OCI-Lob objects. You could
  1645. retrieve them as string for all requests by using driver options
  1646. <code>'lob_as_string'</code> or for particular request by using
  1647. <methodname>setLobAsString(boolean)</methodname> on adapter or on
  1648. statement.
  1649. </para>
  1650. </listitem>
  1651. </itemizedlist>
  1652. </sect3>
  1653. <sect3 id="zend.db.adapter.adapter-notes.pdo-ibm">
  1654. <title>PDO for IBM DB2 and Informix Dynamic Server (IDS)</title>
  1655. <itemizedlist>
  1656. <listitem>
  1657. <para>
  1658. Specify this Adapter to the <methodname>factory()</methodname>
  1659. method with the name 'Pdo_Ibm'.
  1660. </para>
  1661. </listitem>
  1662. <listitem>
  1663. <para>
  1664. This Adapter uses the PHP extensions pdo and pdo_ibm.
  1665. </para>
  1666. </listitem>
  1667. <listitem>
  1668. <para>
  1669. You must use at least PDO_IBM extension version 1.2.2.
  1670. If you have an earlier version of this extension, you
  1671. must upgrade the PDO_IBM extension from PECL.
  1672. </para>
  1673. </listitem>
  1674. </itemizedlist>
  1675. </sect3>
  1676. <sect3 id="zend.db.adapter.adapter-notes.pdo-mssql">
  1677. <title>PDO Microsoft SQL Server</title>
  1678. <itemizedlist>
  1679. <listitem>
  1680. <para>
  1681. Specify this Adapter to the <methodname>factory()</methodname>
  1682. method with the name 'Pdo_Mssql'.
  1683. </para>
  1684. </listitem>
  1685. <listitem>
  1686. <para>
  1687. This Adapter uses the PHP extensions pdo and pdo_mssql.
  1688. </para>
  1689. </listitem>
  1690. <listitem>
  1691. <para>
  1692. Microsoft SQL Server does not support sequences, so
  1693. <methodname>lastInsertId()</methodname> ignores its arguments and
  1694. always returns the last value generated for an
  1695. auto-increment key. The <methodname>lastSequenceId()</methodname>
  1696. method returns <constant>NULL</constant>.
  1697. </para>
  1698. </listitem>
  1699. <listitem>
  1700. <para>
  1701. If you are working with unicode strings in an encoding other than UCS-2
  1702. (such as UTF-8), you may have to perform a conversion in your application
  1703. code or store the data in a binary column. Please refer to <ulink
  1704. url="http://support.microsoft.com/kb/232580">Microsoft's Knowledge
  1705. Base</ulink> for more information.
  1706. </para>
  1707. </listitem>
  1708. <listitem>
  1709. <para>
  1710. <classname>Zend_Db_Adapter_Pdo_Mssql</classname> sets
  1711. <constant>QUOTED_IDENTIFIER ON</constant> immediately after connecting to a
  1712. SQL Server database. This makes the driver use the standard SQL identifier
  1713. delimiter symbol (<code>"</code>) instead of the
  1714. proprietary square-brackets syntax SQL Server uses for
  1715. delimiting identifiers.
  1716. </para>
  1717. </listitem>
  1718. <listitem>
  1719. <para>
  1720. You can specify <code>pdoType</code> as a key in the
  1721. options array. The value can be "mssql" (the default),
  1722. "dblib", "freetds", or "sybase". This option affects
  1723. the DSN prefix the adapter uses when constructing the
  1724. DSN string. Both "freetds" and "sybase" imply a prefix
  1725. of "sybase:", which is used for the
  1726. <ulink url="http://www.freetds.org/">FreeTDS</ulink> set
  1727. of libraries.
  1728. See also
  1729. <ulink url="http://www.php.net/manual/en/ref.pdo-dblib.connection.php">
  1730. http://www.php.net/manual/en/ref.pdo-dblib.connection.php</ulink>
  1731. for more information on the DSN prefixes used in this driver.
  1732. </para>
  1733. </listitem>
  1734. </itemizedlist>
  1735. </sect3>
  1736. <sect3 id="zend.db.adapter.adapter-notes.pdo-mysql">
  1737. <title>PDO MySQL</title>
  1738. <itemizedlist>
  1739. <listitem>
  1740. <para>
  1741. Specify this Adapter to the <methodname>factory()</methodname>
  1742. method with the name 'Pdo_Mysql'.
  1743. </para>
  1744. </listitem>
  1745. <listitem>
  1746. <para>
  1747. This Adapter uses the PHP extensions pdo and pdo_mysql.
  1748. </para>
  1749. </listitem>
  1750. <listitem>
  1751. <para>
  1752. MySQL does not support sequences, so
  1753. <methodname>lastInsertId()</methodname> ignores its arguments and
  1754. always returns the last value generated for an
  1755. auto-increment key. The <methodname>lastSequenceId()</methodname>
  1756. method returns <constant>NULL</constant>.
  1757. </para>
  1758. </listitem>
  1759. </itemizedlist>
  1760. </sect3>
  1761. <sect3 id="zend.db.adapter.adapter-notes.pdo-oci">
  1762. <title>PDO Oracle</title>
  1763. <itemizedlist>
  1764. <listitem>
  1765. <para>
  1766. Specify this Adapter to the <methodname>factory()</methodname>
  1767. method with the name 'Pdo_Oci'.
  1768. </para>
  1769. </listitem>
  1770. <listitem>
  1771. <para>
  1772. This Adapter uses the PHP extensions pdo and pdo_oci.
  1773. </para>
  1774. </listitem>
  1775. <listitem>
  1776. <para>
  1777. Oracle does not support auto-incrementing keys, so you
  1778. should specify the name of a sequence to
  1779. <methodname>lastInsertId()</methodname> or
  1780. <methodname>lastSequenceId()</methodname>.
  1781. </para>
  1782. </listitem>
  1783. </itemizedlist>
  1784. </sect3>
  1785. <sect3 id="zend.db.adapter.adapter-notes.pdo-pgsql">
  1786. <title>PDO PostgreSQL</title>
  1787. <itemizedlist>
  1788. <listitem>
  1789. <para>
  1790. Specify this Adapter to the <methodname>factory()</methodname>
  1791. method with the name 'Pdo_Pgsql'.
  1792. </para>
  1793. </listitem>
  1794. <listitem>
  1795. <para>
  1796. This Adapter uses the PHP extensions pdo and pdo_pgsql.
  1797. </para>
  1798. </listitem>
  1799. <listitem>
  1800. <para>
  1801. PostgreSQL supports both sequences and auto-incrementing
  1802. keys. Therefore the arguments to
  1803. <methodname>lastInsertId()</methodname> are optional. If you give
  1804. no arguments, the Adapter returns the last value
  1805. generated for an auto-increment key. If you give
  1806. arguments, the Adapter returns the last value generated
  1807. by the sequence named according to the convention
  1808. '<emphasis>table</emphasis>_<emphasis>column</emphasis>_seq'.
  1809. </para>
  1810. </listitem>
  1811. </itemizedlist>
  1812. </sect3>
  1813. <sect3 id="zend.db.adapter.adapter-notes.pdo-sqlite">
  1814. <title>PDO SQLite</title>
  1815. <itemizedlist>
  1816. <listitem>
  1817. <para>
  1818. Specify this Adapter to the <methodname>factory()</methodname>
  1819. method with the name 'Pdo_Sqlite'.
  1820. </para>
  1821. </listitem>
  1822. <listitem>
  1823. <para>
  1824. This Adapter uses the PHP extensions pdo and pdo_sqlite.
  1825. </para>
  1826. </listitem>
  1827. <listitem>
  1828. <para>
  1829. SQLite does not support sequences, so
  1830. <methodname>lastInsertId()</methodname> ignores its arguments and
  1831. always returns the last value generated for an
  1832. auto-increment key. The <methodname>lastSequenceId()</methodname>
  1833. method returns <constant>NULL</constant>.
  1834. </para>
  1835. </listitem>
  1836. <listitem>
  1837. <para>
  1838. To connect to an SQLite2 database, specify
  1839. <code>'sqlite2'=>true</code> in the array of
  1840. parameters when creating an instance of the
  1841. Pdo_Sqlite Adapter.
  1842. </para>
  1843. </listitem>
  1844. <listitem>
  1845. <para>
  1846. To connect to an in-memory SQLite database,
  1847. specify <code>'dbname'=>':memory:'</code> in the
  1848. array of parameters when creating an instance of
  1849. the Pdo_Sqlite Adapter.
  1850. </para>
  1851. </listitem>
  1852. <listitem>
  1853. <para>
  1854. Older versions of the SQLite driver for PHP do not seem
  1855. to support the PRAGMA commands necessary to ensure that
  1856. short column names are used in result sets. If you
  1857. have problems that your result sets are returned with
  1858. keys of the form "tablename.columnname" when you do a
  1859. join query, then you should upgrade to the current
  1860. version of PHP.
  1861. </para>
  1862. </listitem>
  1863. </itemizedlist>
  1864. </sect3>
  1865. <sect3 id="zend.db.adapter.adapter-notes.firebird">
  1866. <title>Firebird/Interbase</title>
  1867. <itemizedlist>
  1868. <listitem>
  1869. <para>
  1870. This Adapter uses the PHP extension php_interbase.
  1871. </para>
  1872. </listitem>
  1873. <listitem>
  1874. <para>
  1875. Firebird/interbase does not support auto-incrementing keys,
  1876. so you should specify the name of a sequence to
  1877. <methodname>lastInsertId()</methodname> or
  1878. <methodname>lastSequenceId()</methodname>.
  1879. </para>
  1880. </listitem>
  1881. <listitem>
  1882. <para>
  1883. Currently the <classname>Zend_Db::CASE_FOLDING</classname> option
  1884. is not supported by the Firebird/interbase adapter.
  1885. Unquoted identifiers are automatically returned in
  1886. upper case.
  1887. </para>
  1888. </listitem>
  1889. <listitem>
  1890. <para>Adapter name is ZendX_Db_Adapter_Firebird.</para>
  1891. <para>
  1892. Remember to use the param adapterNamespace with value ZendX_Db_Adapter.
  1893. </para>
  1894. <para>
  1895. We recommend to update the gds32.dll (or linux equivalent) bundled with php,
  1896. to the same version of the server. For Firebird the equivalent gds32.dll is
  1897. fbclient.dll.
  1898. </para>
  1899. <para>
  1900. By default all identifiers (tables names, fields) are returned in upper
  1901. case.
  1902. </para>
  1903. </listitem>
  1904. </itemizedlist>
  1905. </sect3>
  1906. </sect2>
  1907. </sect1>
  1908. <!--
  1909. vim:se ts=4 sw=4 et:
  1910. -->