Zend_Db_Adapter.xml 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. <sect1 id="zend.db.adapter">
  2. <title>Zend_Db_Adapter</title>
  3. <para>
  4. Zend_Db와 관련 클래스들은 Zend Frmework에 기본 데이터 베이스 인터페이스를 제공합니다.
  5. Zend_DB_Adapter는 Zend Framework로 만들어진 PHP 어플리케이션이 관계형 데이터베이스에 접속하기 위한 기본 클라스들을 제공합니다. 각 RDBMS의 종류에 따라 각기 다른 아답터가 제공됩니다.
  6. </para>
  7. <para>
  8. Zend_Db의 어댑터는 벤더별 PHP 모듈과 범용 모듈사이에 다리역활을 수행하여,
  9. 하나의 완성된 PHP 어플리케이션이 최소한의 절차를 거쳐 다른 여러 RDBMS에 적용될수 있게 해줍니다 .
  10. </para>
  11. <para>
  12. 어댑터 클래스의 인터페이스는 <ulink url="http://www.php.net/pdo">PHP Data Objects</ulink> 확장 모듈과 매우 유사합니다.
  13. Zend_Db는 다음의 RDBMS의 PDO 드라이버를에 대해서 아댑테 클라스를 제공합니다:
  14. </para>
  15. <itemizedlist>
  16. <listitem>
  17. <para>
  18. MySQL
  19. </para>
  20. </listitem>
  21. <listitem>
  22. <para>
  23. Microsoft SQL Server
  24. </para>
  25. </listitem>
  26. <listitem>
  27. <para>
  28. Oracle
  29. </para>
  30. </listitem>
  31. <listitem>
  32. <para>
  33. PostgreSQL
  34. </para>
  35. </listitem>
  36. <listitem>
  37. <para>
  38. SQLite
  39. </para>
  40. </listitem>
  41. </itemizedlist>
  42. <para>
  43. 또한 Zend_DB는 다음 RDBMS에 대하여 사용이 용이한 PHP database의 확장 모듈도 제공하고 있습니다:
  44. </para>
  45. <itemizedlist>
  46. <listitem>
  47. <para>
  48. MySQL, using the <ulink url="http://www.php.net/mysqli">mysqli</ulink> PHP extension
  49. </para>
  50. </listitem>
  51. <listitem>
  52. <para>
  53. Oracle, using the <ulink url="http://www.php.net/oci8">oci8</ulink> PHP extension
  54. </para>
  55. </listitem>
  56. <listitem>
  57. <para>
  58. IBM DB2, using the <ulink url="http://www.php.net/ibm_db2">ibm_db2</ulink> PHP extension
  59. </para>
  60. </listitem>
  61. </itemizedlist>
  62. <note>
  63. <para>
  64. 각각의 Zend_Db 아댑터들은 PHP extension을 이용하고 있습니다. Zend_DB Adapter를 이용하기 위해서는 PHP 환경 설정을 해당 extension에 맞게 설정해주셔야 합니다.
  65. 예를 들어, PDO Zend_DB 아댑터를 이용하시려면, PDE extension과 PDO 드라이버를 해당 RDBMS에 맞게 enable 해주셔야 합니다.
  66. </para>
  67. </note>
  68. <sect2 id="zend.db.adapter.connecting">
  69. <title>어댑터를 사용한 데이타베이스 접속</title>
  70. <para>
  71. 본 단락에서는 데이타베이스 어댑터의 인스턴스를 생성에대해 다룹니다.
  72. 이는 PHP 어플리케이션으로 부터 해당 RDBMS서버로의 연결을 생성하는 것을 의미합니다.
  73. </para>
  74. <sect3 id="zend.db.adapter.connecting.constructor">
  75. <title>어댑터 constructor 이용</title>
  76. <para>
  77. Constructor를 이용하여 아답터의 인스탄트를 생성할수 있습니다.
  78. 아답터의 Constructor는 인수 하나를 요구하며 그 인수는 커넥션을 생성하기 위한 파라메터의 배열형식입니다.
  79. </para>
  80. <example id="zend.db.adapter.connecting.constructor.example">
  81. <title>아답터의 constructor 사용</title>
  82. <programlisting role="php"><![CDATA[<?php
  83. require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
  84. $db = new Zend_Db_Adapter_Pdo_Mysql(array(
  85. 'host' => '127.0.0.1',
  86. 'username' => 'webuser',
  87. 'password' => 'xxxxxxxx',
  88. 'dbname' => 'test'
  89. ));]]>
  90. </programlisting>
  91. </example>
  92. </sect3>
  93. <sect3 id="zend.db.adapter.connecting.factory">
  94. <title>Zend_Db Factory의 사용</title>
  95. <para>
  96. 직접적인 아답터의 Constructor를 이용하는 방법으로, 정적 메소드 <code>Zend_Db::factory()</code>를
  97. 이용한 아답터의 인스턴스 생성이 있습니다. 이 메소드는 <link linkend="zend.loader.load.class">Zend_Loader::loadClass()</link>를 이용하여 요청에 따라 동적으로 아답터 클라스를 로드 합니다.
  98. </para>
  99. <para>
  100. 첫번째 인수는 아답터 클라스의 베이스명을 문자열로 지정합니다. 예를 들어 문자열 'Pdo_Mysql'은 Zend_DB_Adapter_Pdo_Mysql 클라스에 상응하게 됩니다. 두번째 인수는 아답터의 constructor에 넘겨주는 인수의 배열과 같은 형식의 배열이 됩니다.
  101. </para>
  102. <example id="zend.db.adapter.connecting.factory.example">
  103. <title>Adapter factory 메소드의 사용</title>
  104. <programlisting role="php"><![CDATA[<?php
  105. require_once 'Zend/Db.php';
  106. // 자동으로 Zend_Db_Adapter_Pdo_Mysql 클래스를 읽어, 그 인스턴스를 작성합니다.
  107. $db = Zend_Db::factory('Pdo_Mysql', array(
  108. 'host' => '127.0.0.1',
  109. 'username' => 'webuser',
  110. 'password' => 'xxxxxxxx',
  111. 'dbname' => 'test'
  112. ));]]>
  113. </programlisting>
  114. </example>
  115. <para>
  116. Zend_Db_Adapter_Abstract 클라스를 상속한 독자적인 클라스를 구성하면서, 그 이름에
  117. "Zend_Db_Adapter"라는 접두어를 붙이지 않으실경우, 파라메터 배열을 "adapternamespace" 키값으로 시작하셨다면 <code>factory()</code> 메소드를 이용하신 만드신 아답터를 로드하실수 있습니다.
  118. </para>
  119. <example id="zend.db.adapter.connecting.factory.example2">
  120. <title> 커스텀 아답터 클래스를 위한 Adapter factory 메소드 이용하기</title>
  121. <programlisting role="php"><![CDATA[<?php
  122. require_once 'Zend/Db.php';
  123. // Automatically load class MyProject_Db_Adapter_Pdo_Mysql and create an instance of it.
  124. $db = Zend_Db::factory('Pdo_Mysql', array(
  125. 'host' => '127.0.0.1',
  126. 'username' => 'webuser',
  127. 'password' => 'xxxxxxxx',
  128. 'dbname' => 'test',
  129. 'adapterNamespace' => 'MyProject_Db_Adapter'
  130. ));]]>
  131. </programlisting>
  132. </example>
  133. </sect3>
  134. <sect3 id="zend.db.adapter.connecting.factory-config">
  135. <title>Zend_Db_Factory와 Zend_Config 이용하기</title>
  136. <para>
  137. <code>factory()</code> 메소드의 인수로 <link linkend="zend.config">Zend_Config</link>의 오브젝트를 건내줄수도 있습니다.
  138. </para>
  139. <para>
  140. If the first argument is a config object, it is expected to
  141. contain a property named <code>adapter</code>, containing the
  142. string naming the adapter class name base. Optionally, the object
  143. may contain a property named <code>params</code>, with
  144. subproperties corresponding to adapter parameter names.
  145. This is used only if the second argument of the
  146. <code>factory()</code> method is absent.
  147. </para>
  148. <example id="zend.db.adapter.connecting.factory.example1">
  149. <title>Zend_Config 오브젝트와 함께 아댑터 factory 메소드 이용하기</title>
  150. <para>
  151. 아래의 예는, 배열로 부터 생성된 Zend_Config 오브젝트입니다.
  152. <link linkend="zend.config.adapters.ini">Zend_Config_Ini</link>
  153. 또는 <link linkend="zend.config.adapters.xml">Zend_Config_Xml</link>을 이용하여
  154. 외부 파일로부터도 데이터를 로드 할수 있습니다.
  155. </para>
  156. <programlisting role="php"><![CDATA[<?php
  157. require_once 'Zend/Config.php';
  158. require_once 'Zend/Db.php';
  159. $config = new Zend_Config(
  160. array(
  161. 'database' => array(
  162. 'adapter' => 'Mysqli',
  163. 'params' => array(
  164. 'dbname' => 'test',
  165. 'username' => 'webuser',
  166. 'password' => 'secret',
  167. )
  168. )
  169. )
  170. );
  171. $db = Zend_Db::factory($config->database);
  172. ));]]>
  173. </programlisting>
  174. </example>
  175. <para>
  176. <code>factory()</code> 메소드의 두번째 인자는 Zend_Config의 다른 오브젝트 혹은 배열의 형태입니다.
  177. It should
  178. contain entries corresponding to adapter parameters.
  179. This argument is optional, but if it is present, it takes
  180. priority over any parameters supplied in the first argument.
  181. </para>
  182. </sect3>
  183. <sect3 id="zend.db.adapter.connecting.parameters">
  184. <title>아답터 파라메터</title>
  185. <para>
  186. 아래의 리스트는 Zend_Db Adapter 클래스에서 인식하는 일반적인 파라메터들입니다.
  187. </para>
  188. <itemizedlist>
  189. <listitem>
  190. <para>
  191. <emphasis role="strong">host</emphasis>:
  192. 데이터 베이스 서벙의 아이피나 호스트네임의 문자열입니다. 만약 데이터 베이스와 PHP 어플리케이션이 같은 호스트 상에서 운영되고 있으면 'localhost' 혹은 '127.0.0.1'을 이용하시면 됩니다.
  193. </para>
  194. </listitem>
  195. <listitem>
  196. <para>
  197. <emphasis role="strong">username</emphasis>:
  198. 관계형 데이터베이스 서버에 접속하기 위한 어카운트의 ID입니다.
  199. </para>
  200. </listitem>
  201. <listitem>
  202. <para>
  203. <emphasis role="strong">password</emphasis>:
  204. 관계형 데이터베이스 서버에 인증을 위한 패스워드입니다.
  205. </para>
  206. </listitem>
  207. <listitem>
  208. <para>
  209. <emphasis role="strong">dbname</emphasis>:
  210. 관계형 데이터베이스 서버의 인스탄스 이름입니다.
  211. </para>
  212. </listitem>
  213. <listitem>
  214. <para>
  215. <emphasis role="strong">port</emphasis>:
  216. 관계형 데이터베이스에 따라서는 관리자가 지정한 특수한 포트로만 커녁션을 지원하기도 합니다.
  217. 포트 파라메터는 관계형 데이터베이스 서버로의 접속히 해당 포트로의 설정을 지원합니다.
  218. </para>
  219. </listitem>
  220. <listitem>
  221. <para>
  222. <emphasis role="strong">options</emphasis>:
  223. 이 파라메트는 모든 Zend_Db_Adapter 클라스들의 옵션들을 배열의 형태로 지정하게 되어있습니다.
  224. </para>
  225. </listitem>
  226. <listitem>
  227. <para>
  228. <emphasis role="strong">driver_options</emphasis>:
  229. 이 파라메터는 해당 데이터베이스에서 요구하는 추가 옵션을 배열의 형태로 입력반습니다.
  230. 일반적으로 이 파라메터는 PDO driver의 attributes를 설정하는데 쓰입니다.
  231. </para>
  232. </listitem>
  233. <listitem>
  234. <para>
  235. <emphasis role="strong">adapterNamespace</emphasis>:
  236. 어댑터 클래스의 접두어가 'Zend_Db_Adapter' 이외인 경우에 어댑터 클래스의 이름을 지정하기 위해 쓰입니다. 젠드 아답터 클라스 이외의 클라스를 로드 하기 위해서 <code>factory()</code>를 이용하셨을 경우 adapterNamespace를 이용하시기 바랍니다.
  237. </para>
  238. </listitem>
  239. </itemizedlist>
  240. <example id="zend.db.adapter.connecting.parameters.example1">
  241. <title>Factory에 대소문자 변환 옵션 지정하기</title>
  242. <para>
  243. <code>Zend_Db::CASE_FOLDING</code>를 이용하여 대소문자 변환 옵션을 지정할수 있습니다.
  244. PDO나 IBM DB2 데이터베이스 드라이버의 <code>ATTR_CASE</code>속성에 상응하는 것으로,
  245. 쿼리 리절트 셋의 문자 키 값을 변환합니다. 옵션 값으로는
  246. <code>Zend_Db::CASE_NATURAL</code> (기본값),
  247. <code>Zend_Db::CASE_UPPER</code>, 그리고
  248. <code>Zend_Db::CASE_LOWER</code> 가 있습니다.
  249. </para>
  250. <programlisting role="php"><![CDATA[<?php
  251. $options = array(
  252. Zend_Db::CASE_FOLDING => Zend_Db::CASE_UPPER
  253. );
  254. $params = array(
  255. 'host' => '127.0.0.1',
  256. 'username' => 'webuser',
  257. 'password' => 'xxxxxxxx',
  258. 'dbname' => 'test',
  259. 'options' => $options
  260. );
  261. $db = Zend_Db::factory('Db2', $params);]]>
  262. </programlisting>
  263. </example>
  264. <example id="zend.db.adapter.connecting.parameters.example2">
  265. <title>팩토리에 자동 쿼팅(auto-quoting) 옵션 지정하기</title>
  266. <para>
  267. <code>Zend_Db::AUTO_QUOTE_IDENTIFIERS</code>를 이용하여 오토 쿼팅 옵션을 지정할수 있습니다.
  268. 해당 값이 <code>true</code>(기본값)일 경우 데이블 이름, 컬럼 이름, 그리고 알리아스등의 Adapter 오브젝트에 의해 생성되는 모든 인자들이 모두 쿼팅 됩니다. 이는 SQL 키워드나 특수문자를 포함한 식별자 사용시 유리합니다. 만약 해당 값이 <code>false</code>일 경우 식별자의 자동쿼팅은 적용 되지않습니다. 만약 쿼트를 이용해야 할경우 <code>quoteIdentifier()</code> 메소드를 이용하여 쿼팅할수 있습니다.
  269. </para>
  270. <programlisting role="php"><![CDATA[<?php
  271. $options = array(
  272. Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
  273. );
  274. $params = array(
  275. 'host' => '127.0.0.1',
  276. 'username' => 'webuser',
  277. 'password' => 'xxxxxxxx',
  278. 'dbname' => 'test',
  279. 'options' => $options
  280. );
  281. $db = Zend_Db::factory('Pdo_Mysql', $params);]]>
  282. </programlisting>
  283. </example>
  284. <example id="zend.db.adapter.connecting.parameters.example3">
  285. <title>팩토리에 PDP 드라이버 옵션 지정하기</title>
  286. <programlisting role="php"><![CDATA[<?php
  287. $pdoParams = array(
  288. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
  289. );
  290. $params = array(
  291. 'host' => '127.0.0.1',
  292. 'username' => 'webuser',
  293. 'password' => 'xxxxxxxx',
  294. 'dbname' => 'test',
  295. 'driver_options' => $pdoParams
  296. );
  297. $db = Zend_Db::factory('Pdo_Mysql', $params);
  298. echo $db->getConnection()->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);]]>
  299. </programlisting>
  300. </example>
  301. </sect3>
  302. <sect3 id="zend.db.adapter.connecting.getconnection">
  303. <title>접속 지연 관리하기</title>
  304. <para>
  305. Creating an instance of an Adapter class does not immediately
  306. connect to the RDBMS server. The Adapter saves the connection
  307. parameters, and makes the actual connection on demand, the
  308. first time you need to execute a query. This ensures that
  309. creating an Adapter object is quick and inexpensive. You can
  310. create an instance of an Adapter even if you are not certain
  311. that you need to run any database queries during the current
  312. request your application is serving.
  313. </para>
  314. <para>
  315. If you need to force the Adapter to connect to the RDBMS, use
  316. the <code>getConnection()</code> method. This method returns
  317. an object for the connection as represented by the respective
  318. PHP database extension. For example, if you use any of the
  319. Adapter classes for PDO drivers, then
  320. <code>getConnection()</code> returns the PDO object, after
  321. initiating it as a live connection to the specific database.
  322. </para>
  323. <para>
  324. It can be useful to force the connection if you want to catch
  325. any exceptions it throws as a result of invalid account
  326. credentials, or other failure to connect to the RDBMS server.
  327. These exceptions are not thrown until the connection is made,
  328. so it can help simplify your application code if you handle the
  329. exceptions in one place, instead of at the time of
  330. the first query against the database.
  331. </para>
  332. <example id="zend.db.adapter.connecting.getconnection.example">
  333. <title>접속 예외 처리</title>
  334. <programlisting role="php"><![CDATA[<?php
  335. try {
  336. $db = Zend_Db::factory('Pdo_Mysql', $parameters);
  337. $db->getConnection();
  338. } catch (Zend_Db_Adapter_Exception $e) {
  339. // perhaps a failed login credential, or perhaps the RDBMS is not running
  340. } catch (Zend_Exception $e) {
  341. // perhaps factory() failed to load the specified Adapter class
  342. }]]>
  343. </programlisting>
  344. </example>
  345. </sect3>
  346. </sect2>
  347. <sect2 id="zend.db.adapter.example-database">
  348. <title>The example database</title>
  349. <para>
  350. In the documentation for Zend_Db classes, we use a set of simple
  351. tables to illustrate usage of the classes and methods. These
  352. example tables could store information for tracking bugs in a
  353. software development project. The database contains four tables:
  354. </para>
  355. <itemizedlist>
  356. <listitem>
  357. <para>
  358. <emphasis role="strong">accounts</emphasis> stores
  359. information about each user of the bug-tracking database.
  360. </para>
  361. </listitem>
  362. <listitem>
  363. <para>
  364. <emphasis role="strong">products</emphasis> stores
  365. information about each product for which a bug can be
  366. logged.
  367. </para>
  368. </listitem>
  369. <listitem>
  370. <para>
  371. <emphasis role="strong">bugs</emphasis> stores information
  372. about bugs, including that current state of the bug, the
  373. person who reported the bug, the person who is assigned to
  374. fix the bug, and the person who is assigned to verify the
  375. fix.
  376. </para>
  377. </listitem>
  378. <listitem>
  379. <para>
  380. <emphasis role="strong">bugs_products</emphasis> stores a
  381. relationship between bugs and products. This implements a
  382. many-to-many relationship, because a given bug may be
  383. relevant to multiple products, and of course a given
  384. product can have multiple bugs.
  385. </para>
  386. </listitem>
  387. </itemizedlist>
  388. <para>
  389. The following SQL data definition language pseudocode describes the
  390. tables in this example database. These example tables are used
  391. extensively by the automated unit tests for Zend_Db.
  392. </para>
  393. <programlisting role="sql"><![CDATA[
  394. CREATE TABLE accounts (
  395. account_name VARCHAR(100) NOT NULL PRIMARY KEY
  396. );
  397. CREATE TABLE products (
  398. product_id INTEGER NOT NULL PRIMARY KEY,
  399. product_name VARCHAR(100)
  400. );
  401. CREATE TABLE bugs (
  402. bug_id INTEGER NOT NULL PRIMARY KEY,
  403. bug_description VARCHAR(100),
  404. bug_status VARCHAR(20),
  405. reported_by VARCHAR(100) REFERENCES accounts(account_name),
  406. assigned_to VARCHAR(100) REFERENCES accounts(account_name),
  407. verified_by VARCHAR(100) REFERENCES accounts(account_name)
  408. );
  409. CREATE TABLE bugs_products (
  410. bug_id INTEGER NOT NULL REFERENCES bugs,
  411. product_id INTEGER NOT NULL REFERENCES products,
  412. PRIMARY KEY (bug_id, product_id)
  413. );]]>
  414. </programlisting>
  415. <para>
  416. Also notice that the <code>bugs</code> table contains multiple
  417. foreign key references to the <code>accounts</code> table.
  418. Each of these foreign keys may reference a different row in the
  419. <code>accounts</code> table for a given bug.
  420. </para>
  421. <para>
  422. The diagram below illustrates the physical data model of the
  423. example database.
  424. </para>
  425. <para>
  426. <inlinegraphic width="387" scale="100" align="center" valign="middle"
  427. fileref="figures/zend.db.adapter.example-database.png" format="PNG" />
  428. </para>
  429. </sect2>
  430. <sect2 id="zend.db.adapter.select">
  431. <title>Reading Query Results</title>
  432. <para>
  433. This section describes methods of the Adapter class with which you
  434. can run SELECT queries and retrieve the query results.
  435. </para>
  436. <sect3 id="zend.db.adapter.select.fetchall">
  437. <title>Fetching a Complete Result Set</title>
  438. <para>
  439. You can run a SQL SELECT query and retrieve its results in one
  440. step using the <code>fetchAll()</code> method.
  441. </para>
  442. <para>
  443. The first argument to this method is a string containing a
  444. SELECT statement. Alternatively, the first argument can be an
  445. object of class <link linkend="zend.db.select">Zend_Db_Select</link>.
  446. The Adapter automatically converts this object to a string
  447. representation of the SELECT statement.
  448. </para>
  449. <para>
  450. The second argument to <code>fetchAll()</code> is an array of
  451. values to substitute for parameter placeholders in the SQL
  452. statement.
  453. </para>
  454. <example id="zend.db.adapter.select.fetchall.example">
  455. <title>Using fetchAll()</title>
  456. <programlisting role="php"><![CDATA[<?php
  457. $sql = 'SELECT * FROM bugs WHERE bug_id = ?';
  458. $result = $db->fetchAll($sql, 2);]]>
  459. </programlisting>
  460. </example>
  461. </sect3>
  462. <sect3 id="zend.db.adapter.select.fetch-mode">
  463. <title>Changing the Fetch Mode</title>
  464. <para>
  465. By default, <code>fetchAll()</code> returns an array of
  466. rows, each of which is an associative array. The keys of the
  467. associative array are the columns or column aliases named in
  468. the select query.
  469. </para>
  470. <para>
  471. You can specify a different style of fetching results using the
  472. <code>setFetchMode()</code> method. The modes supported are
  473. identified by constants:
  474. </para>
  475. <itemizedlist>
  476. <listitem>
  477. <para>
  478. <emphasis role="strong">Zend_Db::FETCH_ASSOC</emphasis>:
  479. return data in an array of associative arrays.
  480. The array keys are column names, as strings.
  481. This is the default fetch mode for Zend_Db_Adapter classes.
  482. </para>
  483. <para>
  484. Note that if your select-list contains more than one
  485. column with the same name, for example if they are from
  486. two different tables in a JOIN, there can be only one
  487. entry in the associative array for a given name.
  488. If you use the FETCH_ASSOC mode, you should specify
  489. column aliases in your SELECT query to ensure that the
  490. names result in unique array keys.
  491. </para>
  492. <para>
  493. By default, these strings are returned as they are
  494. returned by the database driver. This is typically the
  495. spelling of the column in the RDBMS server. You can
  496. specify the case for these strings, using the
  497. <code>Zend_Db::CASE_FOLDING</code> option.
  498. Specify this when instantiating the Adapter.
  499. See <xref linkend="zend.db.adapter.connecting.parameters.example1" />.
  500. </para>
  501. </listitem>
  502. <listitem>
  503. <para>
  504. <emphasis role="strong">Zend_Db::FETCH_NUM</emphasis>:
  505. return data in an array of arrays. The arrays are
  506. indexed by integers, corresponding to the position of
  507. the respective field in the select-list of the query.
  508. </para>
  509. </listitem>
  510. <listitem>
  511. <para>
  512. <emphasis role="strong">Zend_Db::FETCH_BOTH</emphasis>:
  513. return data in an array of arrays. The array keys are
  514. both strings as used in the FETCH_ASSOC mode, and
  515. integers as used in the FETCH_NUM mode. Note that the
  516. number of elements in the array is double that which
  517. would be in the array if you used iether FETCH_ASSOC
  518. or FETCH_NUM.
  519. </para>
  520. </listitem>
  521. <listitem>
  522. <para>
  523. <emphasis role="strong">Zend_Db::FETCH_COLUMN</emphasis>:
  524. return data in an array of values. The value in each array
  525. is the value returned by one column of the result set.
  526. By default, this is the first column, indexed by 0.
  527. </para>
  528. </listitem>
  529. <listitem>
  530. <para>
  531. <emphasis role="strong">Zend_Db::FETCH_OBJ</emphasis>:
  532. return data in an array of objects. The default class
  533. is the PHP built-in class stdClass. Columns of the
  534. result set are available as public properties of the
  535. object.
  536. </para>
  537. </listitem>
  538. </itemizedlist>
  539. <example id="zend.db.adapter.select.fetch-mode.example">
  540. <title>Using setFetchMode()</title>
  541. <programlisting role="php"><![CDATA[<?php
  542. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  543. $result = $db->fetchAll('SELECT * FROM bugs WHERE bug_id = ?', 2);
  544. // $result is an array of objects
  545. echo $result[0]->bug_description;]]>
  546. </programlisting>
  547. </example>
  548. </sect3>
  549. <sect3 id="zend.db.adapter.select.fetchassoc">
  550. <title>Fetching a Result Set as an Associative Array</title>
  551. <para>
  552. The <code>fetchAssoc()</code> method returns data in an array
  553. of associative arrays, regardless of what value you have set
  554. for the fetch mode.
  555. </para>
  556. <example id="zend.db.adapter.select.fetchassoc.example">
  557. <title>Using fetchAssoc()</title>
  558. <programlisting role="php"><![CDATA[<?php
  559. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  560. $result = $db->fetchAssoc('SELECT * FROM bugs WHERE bug_id = ?', 2);
  561. // $result is an array of associative arrays, in spite of the fetch mode
  562. echo $result[0]['bug_description'];]]>
  563. </programlisting>
  564. </example>
  565. </sect3>
  566. <sect3 id="zend.db.adapter.select.fetchcol">
  567. <title>Fetching a Single Column from a Result Set</title>
  568. <para>
  569. The <code>fetchCol()</code> method returns data in an array
  570. of values, regardless of the value you have set for the fetch mode.
  571. This only returns the first column returned by the query.
  572. Any other columns returned by the query are discarded.
  573. If you need to return a column other than the first, see <xref linkend="zend.db.statement.fetching.fetchcolumn" />.
  574. </para>
  575. <example id="zend.db.adapter.select.fetchcol.example">
  576. <title>Using fetchCol()</title>
  577. <programlisting role="php"><![CDATA[<?php
  578. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  579. $result = $db->fetchCol('SELECT bug_description, bug_id FROM bugs WHERE bug_id = ?', 2);
  580. // contains bug_description; bug_id is not returned
  581. echo $result[0];]]>
  582. </programlisting>
  583. </example>
  584. </sect3>
  585. <sect3 id="zend.db.adapter.select.fetchpairs">
  586. <title>Fetching Key-Value Pairs from a Result Set</title>
  587. <para>
  588. The <code>fetchPairs()</code> method returns data in an array
  589. of key-value pairs, as an associative array with a single entry
  590. per row. The key of this associative array is taken from the
  591. first column returned by the SELECT query. The value is taken
  592. from the second column returned by the SELECT query. Any other
  593. columns returned by the query are discarded.
  594. </para>
  595. <para>
  596. You should design the SELECT query so that the first column
  597. returned has unique values. If there are duplicates values in
  598. the first column, entries in the associative array will be
  599. overwritten.
  600. </para>
  601. <example id="zend.db.adapter.select.fetchpairs.example">
  602. <title>Using fetchPairs()</title>
  603. <programlisting role="php"><![CDATA[<?php
  604. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  605. $result = $db->fetchPairs('SELECT bug_id, bug_status FROM bugs');
  606. echo $result[2];]]>
  607. </programlisting>
  608. </example>
  609. </sect3>
  610. <sect3 id="zend.db.adapter.select.fetchrow">
  611. <title>Fetching a Single Row from a Result Set</title>
  612. <para>
  613. The <code>fetchRow()</code> method returns data using the
  614. current fetch mode, but it returns only the first row
  615. fetched from the result set.
  616. </para>
  617. <example id="zend.db.adapter.select.fetchrow.example">
  618. <title>Using fetchRow()</title>
  619. <programlisting role="php"><![CDATA[<?php
  620. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  621. $result = $db->fetchRow('SELECT * FROM bugs WHERE bug_id = 2');
  622. // note that $result is a single object, not an array of objects
  623. echo $result->bug_description;]]>
  624. </programlisting>
  625. </example>
  626. </sect3>
  627. <sect3 id="zend.db.adapter.select.fetchone">
  628. <title>Fetching a Single Scalar from a Result Set</title>
  629. <para>
  630. The <code>fetchOne()</code> method is like a combination
  631. of <code>fetchRow()</code> with <code>fetchCol()</code>,
  632. in that it returns data only for the first row fetched from
  633. the result set, and it returns only the value of the first
  634. column in that row. Therefore it returns only a single
  635. scalar value, not an array or an object.
  636. </para>
  637. <example id="zend.db.adapter.select.fetchone.example">
  638. <title>Using fetchOne()</title>
  639. <programlisting role="php"><![CDATA[<?php
  640. $result = $db->fetchOne('SELECT bug_status FROM bugs WHERE bug_id = 2');
  641. // this is a single string value
  642. echo $result;]]>
  643. </programlisting>
  644. </example>
  645. </sect3>
  646. </sect2>
  647. <sect2 id="zend.db.adapter.write">
  648. <title>Writing Changes to the Database</title>
  649. <para>
  650. You can use the Adapter class to write new data or change existing
  651. data in your database. This section describes methods to do these
  652. operations.
  653. </para>
  654. <sect3 id="zend.db.adapter.write.insert">
  655. <title>Inserting Data</title>
  656. <para>
  657. You can add new rows to a table in your database using the
  658. <code>insert()</code> method. The first argument is a string
  659. that names the table, and the second argument is an associative
  660. array, mapping column names to data values.
  661. </para>
  662. <example id="zend.db.adapter.write.insert.example">
  663. <title>Inserting to a table</title>
  664. <programlisting role="php"><![CDATA[<?php
  665. $data = array(
  666. 'created_on' => '2007-03-22',
  667. 'bug_description' => 'Something wrong',
  668. 'bug_status' => 'NEW'
  669. );
  670. $db->insert('bugs', $data);]]>
  671. </programlisting>
  672. </example>
  673. <para>
  674. Columns you exclude from the array of data are not specified to
  675. the database. Therefore, they follow the same rules that an
  676. SQL INSERT statement follows: if the column has a DEFAULT
  677. clause, the column takes that value in the row created,
  678. otherwise the column is left in a NULL state.
  679. </para>
  680. <para>
  681. By default, the values in your data array are inserted using
  682. parameters. This reduces risk of some types of security
  683. issues. You don't need to apply escaping or quoting to values
  684. in the data array.
  685. </para>
  686. <para>
  687. You might need values in the data array to be treated as SQL
  688. expressions, in which case they should not be quoted. By
  689. default, all data values passed as strings are treated as
  690. string literals. To specify that the value is an SQL
  691. expression and therefore should not be quoted, pass the value
  692. in the data array as an object of type Zend_Db_Expr instead of
  693. a plain string.
  694. </para>
  695. <example id="zend.db.adapter.write.insert.example2">
  696. <title>Inserting expressions to a table</title>
  697. <programlisting role="php"><![CDATA[<?php
  698. $data = array(
  699. 'created_on' => new Zend_Db_Expr('CURDATE()'),
  700. 'bug_description' => 'Something wrong',
  701. 'bug_status' => 'NEW'
  702. );
  703. $db->insert('bugs', $data);]]>
  704. </programlisting>
  705. </example>
  706. </sect3>
  707. <sect3 id="zend.db.adapter.write.lastinsertid">
  708. <title>Retrieving a Generated Value</title>
  709. <para>
  710. Some RDBMS brands support auto-incrementing primary keys.
  711. A table defined this way generates a primary key value
  712. automatically during an INSERT of a new row. The return value
  713. of the <code>insert()</code> method is <emphasis>not</emphasis>
  714. the last inserted ID, because the table might not have an
  715. auto-incremented column. Instead, the return value is the
  716. number of rows affected (usually 1).
  717. </para>
  718. <para>
  719. If your table is defined with an auto-incrementing primary key,
  720. you can call the <code>lastInsertId()</code> method after the
  721. insert. This method returns the last value generated in the
  722. scope of the current database connection.
  723. </para>
  724. <example id="zend.db.adapter.write.lastinsertid.example-1">
  725. <title>Using lastInsertId() for an auto-increment key</title>
  726. <programlisting role="php"><![CDATA[<?php
  727. $db->insert('bugs', $data);
  728. // return the last value generated by an auto-increment column
  729. $id = $db->lastInsertId();]]>
  730. </programlisting>
  731. </example>
  732. <para>
  733. Some RDBMS brands support a sequence object, which generates
  734. unique values to serve as primary key values. To support
  735. sequences, the <code>lastInsertId()</code> method accepts two
  736. optional string arguments. These arguments name the table and
  737. the column, assuming you have followed the convention that a
  738. sequence is named using the table and column names for which
  739. the sequence generates values, and a suffix "_seq". This is
  740. based on the convention used by PostgreSQL when naming
  741. sequences for SERIAL columns. For example, a table "bugs" with
  742. primary key column "bug_id" would use a sequence named
  743. "bugs_bug_id_seq".
  744. </para>
  745. <example id="zend.db.adapter.write.lastinsertid.example-2">
  746. <title>Using lastInsertId() for a sequence</title>
  747. <programlisting role="php"><![CDATA[<?php
  748. $db->insert('bugs', $data);
  749. // return the last value generated by sequence 'bugs_bug_id_seq'.
  750. $id = $db->lastInsertId('bugs', 'bug_id');
  751. // alternatively, return the last value generated by sequence 'bugs_seq'.
  752. $id = $db->lastInsertId('bugs');]]>
  753. </programlisting>
  754. </example>
  755. <para>
  756. If the name of your sequence object does not follow this naming
  757. convention, use the <code>lastSequenceId()</code> method
  758. instead. This method takes a single string argument, naming
  759. the sequence literally.
  760. </para>
  761. <example id="zend.db.adapter.write.lastinsertid.example-3">
  762. <title>Using lastSequenceId()</title>
  763. <programlisting role="php"><![CDATA[<?php
  764. $db->insert('bugs', $data);
  765. // return the last value generated by sequence 'bugs_id_gen'.
  766. $id = $db->lastSequenceId('bugs_id_gen');]]>
  767. </programlisting>
  768. </example>
  769. <para>
  770. For RDBMS brands that don't support sequences, including MySQL,
  771. Microsoft SQL Server, and SQLite, the arguments to the
  772. lastInsertId() method are ignored, and the value returned is the
  773. most recent value generated for any table by INSERT operations
  774. during the current connection. For these RDBMS brands, the
  775. lastSequenceId() method always returns <code>null</code>.
  776. </para>
  777. <note>
  778. <title>Why not use "SELECT MAX(id) FROM table"?</title>
  779. <para>
  780. Sometimes this query returns the most recent primary key
  781. value inserted into the table. However, this technique
  782. is not safe to use in an environment where multiple clients are
  783. inserting records to the database. It is possible, and
  784. therefore is bound to happen eventually, that another
  785. client inserts another row in the instant between the
  786. insert performed by your client application and your query
  787. for the MAX(id) value. Thus the value returned does not
  788. identify the row you inserted, it identifies the row
  789. inserted by some other client. There is no way to know
  790. when this has happened.
  791. </para>
  792. <para>
  793. Using a strong transaction isolation mode such as
  794. "repeatable read" can mitigate this risk, but some RDBMS
  795. brands don't support the transaction isolation required for
  796. this, or else your application may use a lower transaction
  797. isolation mode by design.
  798. </para>
  799. <para>
  800. Furthermore, using an expression like "MAX(id)+1" to generate
  801. a new value for a primary key is not safe, because two clients
  802. could do this query simultaneously, and then both use the same
  803. calculated value for their next INSERT operation.
  804. </para>
  805. <para>
  806. All RDBMS brands provide mechanisms to generate unique
  807. values, and to return the last value generated. These
  808. mechanisms necessarily work outside of the scope of
  809. transaction isolation, so there is no chance of two clients
  810. generating the same value, and there is no chance that the
  811. value generated by another client could be reported to your
  812. client's connection as the last value generated.
  813. </para>
  814. </note>
  815. </sect3>
  816. <sect3 id="zend.db.adapter.write.update">
  817. <title>Updating Data</title>
  818. <para>
  819. You can update rows in a database table using the
  820. <code>update()</code> method of an Adapter. This method takes
  821. three arguments: the first is the name of the table; the
  822. second is an associative array mapping columns to change to new
  823. values to assign to these columns.
  824. </para>
  825. <para>
  826. The values in the data array are treated as string literals.
  827. See <xref linkend="zend.db.adapter.write.insert" />
  828. for information on using SQL expressions in the data array.
  829. </para>
  830. <para>
  831. The third argument is a string containing an SQL expression
  832. that is used as criteria for the rows to change. The values
  833. and identifiers in this argument are not quoted or escaped.
  834. You are responsible for ensuring that any dynamic content is
  835. interpolated into this string safely.
  836. See <xref linkend="zend.db.adapter.quoting" />
  837. for methods to help you do this.
  838. </para>
  839. <para>
  840. The return value is the number of rows affected by the update
  841. operation.
  842. </para>
  843. <example id="zend.db.adapter.write.update.example">
  844. <title>Updating rows</title>
  845. <programlisting role="php"><![CDATA[<?php
  846. $data = array(
  847. 'updated_on' => '2007-03-23',
  848. 'bug_status' => 'FIXED'
  849. );
  850. $n = $db->update('bugs', $data, 'bug_id = 2');]]>
  851. </programlisting>
  852. </example>
  853. <para>
  854. If you omit the third argument, then all rows in the database
  855. table are updated with the values specified in the data array.
  856. </para>
  857. <para>
  858. If you provide an array of strings as the third argument, these
  859. strings are joined together as terms in an expression separated
  860. by <code>AND</code> operators.
  861. </para>
  862. <example id="zend.db.adapter.write.update.example-array">
  863. <title>Updating rows using an array of expressions</title>
  864. <programlisting role="php"><![CDATA[<?php
  865. $data = array(
  866. 'updated_on' => '2007-03-23',
  867. 'bug_status' => 'FIXED'
  868. );
  869. $where[] = "reported_by = 'goofy'";
  870. $where[] = "bug_status = 'OPEN'";
  871. $n = $db->update('bugs', $data, $where);
  872. // Resulting SQL is:
  873. // UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED'
  874. // WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')]]>
  875. </programlisting>
  876. </example>
  877. </sect3>
  878. <sect3 id="zend.db.adapter.write.delete">
  879. <title>Deleting Data</title>
  880. <para>
  881. You can delete rows from a database table using the
  882. <code>delete()</code> method. This method takes two arguments:
  883. the first is a string naming the table.
  884. </para>
  885. <para>
  886. The second argument is a string containing an SQL expression
  887. that is used as criteria for the rows to delete. The values
  888. and identifiers in this argument are not quoted or escaped.
  889. You are responsible for ensuring that any dynamic content is
  890. interpolated into this string safely.
  891. See <xref linkend="zend.db.adapter.quoting" />
  892. for methods to help you do this.
  893. </para>
  894. <para>
  895. The return value is the number of rows affected by the delete
  896. operation.
  897. </para>
  898. <example id="zend.db.adapter.write.delete.example">
  899. <title>Deleting rows</title>
  900. <programlisting role="php"><![CDATA[<?php
  901. $n = $db->delete('bugs', 'bug_id = 3');]]>
  902. </programlisting>
  903. </example>
  904. <para>
  905. If you omit the second argument, the result is that all rows in
  906. the database table are deleted.
  907. </para>
  908. <para>
  909. If you provide an array of strings as the second argument, these
  910. strings are joined together as terms in an expression separated
  911. by <code>AND</code> operators.
  912. </para>
  913. </sect3>
  914. </sect2>
  915. <sect2 id="zend.db.adapter.quoting">
  916. <title>Quoting Values and Identifiers</title>
  917. <para>
  918. When you form SQL queries, often it is the case that you need to
  919. include the values of PHP variables in SQL expressions. This is
  920. risky, because if the value in a PHP string contains certain
  921. symbols, such as the quote symbol, it could result in invalid SQL.
  922. For example, notice the imbalanced quote characters in the
  923. following query:
  924. <programlisting role="php"><![CDATA[
  925. $name = "O'Reilly";
  926. $sql = "SELECT * FROM bugs WHERE reported_by = '$name'";
  927. echo $sql;
  928. // SELECT * FROM bugs WHERE reported_by = 'O'Reilly']]>
  929. </programlisting>
  930. </para>
  931. <para>
  932. Even worse is the risk that such code mistakes might be exploited
  933. deliberately by a person who is trying to manipulate the function
  934. of your web application. If they can specify the value of a PHP
  935. variable through the use of an HTTP parameter or other mechanism,
  936. they might be able to make your SQL queries do things that you
  937. didn't intend them to do, such as return data to which the person
  938. should not have privilege to read. This is a serious and widespread
  939. technique for violating application security, known as "SQL Injection"
  940. (see <ulink url="http://en.wikipedia.org/wiki/SQL_Injection">http://en.wikipedia.org/wiki/SQL_Injection</ulink>).
  941. </para>
  942. <para>
  943. The Zend_Db Adapter class provides convenient functions to help you
  944. reduce vulnerabilities to SQL Injection attacks in your PHP code.
  945. The solution is to escape special characters such as quotes in PHP
  946. values before they are interpolated into your SQL strings.
  947. This protects against both accidental and deliberate manipulation
  948. of SQL strings by PHP variables that contain special characters.
  949. </para>
  950. <sect3 id="zend.db.adapter.quoting.quote">
  951. <title>Using <code>quote()</code></title>
  952. <para>
  953. The <code>quote()</code> method accepts a single argument, a
  954. scalar string value. It returns the value with special
  955. characters escaped in a manner appropriate for the RDBMS you
  956. are using, and surrounded by string value delimiters. The
  957. standard SQL string value delimiter is the single-quote
  958. (<code>'</code>).
  959. </para>
  960. <example id="zend.db.adapter.quoting.quote.example">
  961. <title>Using quote()</title>
  962. <programlisting role="php"><![CDATA[<?php
  963. $name = $db->quote("O'Reilly");
  964. echo $name;
  965. // 'O\'Reilly'
  966. $sql = "SELECT * FROM bugs WHERE reported_by = $name";
  967. echo $sql;
  968. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly']]>
  969. </programlisting>
  970. </example>
  971. <para>
  972. Note that the return value of <code>quote()</code> includes the
  973. quote delimiters around the string. This is different from
  974. some functions that escape special characters but do not add
  975. the quote delimiters, for example
  976. <ulink url="http://www.php.net/mysqli_real_escape_string">mysql_real_escape_string()</ulink>.
  977. </para>
  978. <para>
  979. Values may need to be quoted or not quoted according to the SQL
  980. datatype context in which they are used. For instance, in some
  981. RDBMS brands, an integer value must not be quoted as a string
  982. if it is compared to an integer-type column or expression.
  983. In other words, the following is an error in some SQL
  984. implementations, assuming <code>intColumn</code> has a SQL
  985. datatype of <code>INTEGER</code>
  986. <programlisting role="php"><![CDATA[
  987. SELECT * FROM atable WHERE intColumn = '123']]>
  988. </programlisting>
  989. </para>
  990. <para>
  991. You can use the optional second argument to the
  992. <code>quote()</code> method to apply quoting selectively for
  993. the SQL datatype you specify.
  994. </para>
  995. <example id="zend.db.adapter.quoting.quote.example-2">
  996. <title>Using quote() with a SQL type</title>
  997. <programlisting role="php"><![CDATA[<?php
  998. $value = '1234';
  999. $sql = 'SELECT * FROM atable WHERE intColumn = '
  1000. . $db->quoteType($value, 'INTEGER');
  1001. ]]>
  1002. </programlisting>
  1003. </example>
  1004. <para>
  1005. Each Zend_Db_Adapter class has encoded the names of numeric
  1006. SQL datatypes for the respective brand of RDBMS. You can also
  1007. use the constants <code>Zend_Db::INT_TYPE</code>,
  1008. <code>Zend_Db::BIGINT_TYPE</code>, and
  1009. <code>Zend_Db::FLOAT_TYPE</code> to write code in a more
  1010. RDBMS-independent way.
  1011. </para>
  1012. <para>
  1013. Zend_Db_Table specifies SQL types to <code>quote()</code>
  1014. automatically when generating SQL queries that reference a
  1015. table's key columns.
  1016. </para>
  1017. </sect3>
  1018. <sect3 id="zend.db.adapter.quoting.quote-into">
  1019. <title>Using <code>quoteInto()</code></title>
  1020. <para>
  1021. The most typical usage of quoting is to interpolate a PHP
  1022. variable into a SQL expression or statement. You can use the
  1023. <code>quoteInto()</code> method to do this in one step. This
  1024. method takes two arguments: the first argument is a string
  1025. containing a placeholder symbol (<code>?</code>), and the
  1026. second argument is a value or PHP variable that should be
  1027. substituted for that placeholder.
  1028. </para>
  1029. <para>
  1030. The placeholder symbol is the same symbol used by many RDBMS
  1031. brands for positional parameters, but the
  1032. <code>quoteInto()</code> method only emulates query parameters.
  1033. The method simply interpolates the value into the string,
  1034. escapes special characters, and applies quotes around it.
  1035. True query parameters maintain the separation between the SQL
  1036. string and the parameters as the statement is parsed in the
  1037. RDBMS server.
  1038. </para>
  1039. <example id="zend.db.adapter.quoting.quote-into.example">
  1040. <title>Using quoteInto()</title>
  1041. <programlisting role="php"><![CDATA[<?php
  1042. $sql = $db->quoteInto("SELECT * FROM bugs WHERE reported_by = ?", "O'Reilly");
  1043. echo $sql;
  1044. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly']]>
  1045. </programlisting>
  1046. </example>
  1047. <para>
  1048. You can use the optional third parameter of
  1049. <code>quoteInto()</code> to specify the SQL datatype. Numeric
  1050. datatypes are not quoted, and other types are quoted.
  1051. </para>
  1052. <example id="zend.db.adapter.quoting.quote-into.example-2">
  1053. <title>Using quoteInto() with a SQL type</title>
  1054. <programlisting role="php"><![CDATA[<?php
  1055. $sql = $db->quoteInto("SELECT * FROM bugs WHERE bug_id = ?", '1234', 'INTEGER');
  1056. echo $sql;
  1057. // SELECT * FROM bugs WHERE reported_by = 1234]]>
  1058. </programlisting>
  1059. </example>
  1060. </sect3>
  1061. <sect3 id="zend.db.adapter.quoting.quote-identifier">
  1062. <title>Using <code>quoteIdentifier()</code></title>
  1063. <para>
  1064. Values are not the only part of SQL syntax that might need to
  1065. be variable. If you use PHP variables to name tables, columns,
  1066. or other identifiers in your SQL statements, you might need to
  1067. quote these strings too. By default, SQL identifiers have
  1068. syntax rules like PHP and most other programming languages.
  1069. For example, identifiers should not contain spaces, certain
  1070. punctuation or special characters, or international characters.
  1071. Also certain words are reserved for SQL syntax, and should not
  1072. be used as identifiers.
  1073. </para>
  1074. <para>
  1075. However, SQL has a feature called <emphasis>delimited identifiers</emphasis>,
  1076. which allows broader choices for the spelling of identifiers.
  1077. If you enclose a SQL identifier in the proper types of quotes,
  1078. you can use identifiers with spellings that would be invalid
  1079. without the quotes. Delimited identifiers can contain spaces,
  1080. punctuation, or international characters. You can also use SQL
  1081. reserved words if you enclose them in identifier delimiters.
  1082. </para>
  1083. <para>
  1084. The <code>quoteIdentifier()</code> method works like
  1085. <code>quote()</code>, but it applies the identifier delimiter
  1086. characters to the string according to the type of Adapter you
  1087. use. For example, standard SQL uses double-quotes
  1088. (<code>"</code>) for identifier delimiters, and most RDBMS
  1089. brands use that symbol. MySQL uses back-quotes
  1090. (<code>`</code>) by default. The
  1091. <code>quoteIdentifier()</code> method also escapes special
  1092. characters within the string argument.
  1093. </para>
  1094. <example id="zend.db.adapter.quoting.quote-identifier.example">
  1095. <title>Using quoteIdentifier()</title>
  1096. <programlisting role="php"><![CDATA[<?php
  1097. // we might have a table name that is an SQL reserved word
  1098. $tableName = $db->quoteIdentifier("order");
  1099. $sql = "SELECT * FROM $tableName";
  1100. echo $sql
  1101. // SELECT * FROM "order"]]>
  1102. </programlisting>
  1103. </example>
  1104. <para>
  1105. SQL delimited identifiers are case-sensitive, unlike unquoted
  1106. identifiers. Therefore, if you use delimited identifiers, you
  1107. must use the spelling of the identifier exactly as it is stored
  1108. in your schema, including the case of the letters.
  1109. </para>
  1110. <para>
  1111. In most cases where SQL is generated within Zend_Db classes,
  1112. the default is that all identifiers are delimited
  1113. automatically. You can change this behavior with the option
  1114. <code>Zend_Db::AUTO_QUOTE_IDENTIFIERS</code>. Specify this
  1115. when instantiating the Adapter.
  1116. See <xref linkend="zend.db.adapter.connecting.parameters.example2" />.
  1117. </para>
  1118. </sect3>
  1119. </sect2>
  1120. <sect2 id="zend.db.adapter.transactions">
  1121. <title>Controlling Database Transactions</title>
  1122. <para>
  1123. Databases define transactions as logical units of work that can be
  1124. committed or rolled back as a single change, even if they operate
  1125. on multiple tables. All queries to a database are executed within
  1126. the context of a transaction, even if the database driver manages
  1127. them implicitly. This is called <emphasis>auto-commit</emphasis>
  1128. mode, in which the database driver creates a transaction for every
  1129. statement you execute, and commits that transaction after your
  1130. SQL statement has been executed. By default, all Zend_Db Adapter
  1131. classes operate in auto-commit mode.
  1132. </para>
  1133. <para>
  1134. Alternatively, you can specify the beginning and resolution of a
  1135. transaction, and thus control how many SQL queries are included in
  1136. a single group that is committed (or rolled back) as a single
  1137. operation. Use the <code>beginTransaction()</code> method to
  1138. initiate a transaction. Subsequent SQL statements are executed in
  1139. the context of the same transaction until you resolve it
  1140. explicitly.
  1141. </para>
  1142. <para>
  1143. To resolve the transaction, use either the <code>commit()</code> or
  1144. <code>rollBack()</code> methods. The <code>commit()</code> method
  1145. marks changes made during your transaction as committed, which
  1146. means the effects of these changes are shown in queries run in
  1147. other transactions.
  1148. </para>
  1149. <para>
  1150. The <code>rollBack()</code> method does the opposite: it discards
  1151. the changes made during your transaction. The changes are
  1152. effectively undone, and the state of the data returns to how it was
  1153. before you began your transaction. However, rolling back your
  1154. transaction has no effect on changes made by other transactions
  1155. running concurrently.
  1156. </para>
  1157. <para>
  1158. After you resolve this transaction, <code>Zend_Db_Adapter</code>
  1159. returns to auto-commit mode until you call
  1160. <code>beginTransaction()</code> again.
  1161. </para>
  1162. <example id="zend.db.adapter.transactions.example">
  1163. <title>Managing a transaction to ensure consistency</title>
  1164. <programlisting role="php"><![CDATA[<?php
  1165. // Start a transaction explicitly.
  1166. $db->beginTransaction();
  1167. try {
  1168. // Attempt to execute one or more queries:
  1169. $db->query(...);
  1170. $db->query(...);
  1171. $db->query(...);
  1172. // If all succeed, commit the transaction and all changes
  1173. // are committed at once.
  1174. $db->commit();
  1175. } catch (Exception $e) {
  1176. // If any of the queries failed and threw an exception,
  1177. // we want to roll back the whole transaction, reversing
  1178. // changes made in the transaction, even those that succeeded.
  1179. // Thus all changes are committed together, or none are.
  1180. $db->rollBack();
  1181. echo $e->getMessage();
  1182. }]]>
  1183. </programlisting>
  1184. </example>
  1185. </sect2>
  1186. <sect2 id="zend.db.adapter.list-describe">
  1187. <title>Listing and Describing Tables</title>
  1188. <para>
  1189. The <code>listTables()</code> method returns an array of strings,
  1190. naming all tables in the current database.
  1191. </para>
  1192. <para>
  1193. The <code>describeTable()</code> method returns an associative
  1194. array of metadata about a table. Specify the name of the table
  1195. as a string in the first argument to this method. The second
  1196. argument is optional, and names the schema in which the table
  1197. exists.
  1198. </para>
  1199. <para>
  1200. The keys of the associative array returned are the column names of
  1201. the table. The value corresponding to each column is also an
  1202. associative array, with the following keys and values:
  1203. </para>
  1204. <table frame="all" cellpadding="5" id="zend.db.adapter.list-describe.metadata">
  1205. <title>Metadata fields returned by describeTable()</title>
  1206. <tgroup cols="3" align="left" colsep="1" rowsep="1">
  1207. <thead>
  1208. <row>
  1209. <entry>Key</entry>
  1210. <entry>Type</entry>
  1211. <entry>Description</entry>
  1212. </row>
  1213. </thead>
  1214. <tbody>
  1215. <row>
  1216. <entry>SCHEMA_NAME</entry>
  1217. <entry>(string)</entry>
  1218. <entry>Name of the database schema in which this table exists.</entry>
  1219. </row>
  1220. <row>
  1221. <entry>TABLE_NAME</entry>
  1222. <entry>(string)</entry>
  1223. <entry>Name of the table to which this column belongs.</entry>
  1224. </row>
  1225. <row>
  1226. <entry>COLUMN_NAME</entry>
  1227. <entry>(string)</entry>
  1228. <entry>Name of the column.</entry>
  1229. </row>
  1230. <row>
  1231. <entry>COLUMN_POSITION</entry>
  1232. <entry>(integer)</entry>
  1233. <entry>Ordinal position of the column in the table.</entry>
  1234. </row>
  1235. <row>
  1236. <entry>DATA_TYPE</entry>
  1237. <entry>(string)</entry>
  1238. <entry>RDBMS name of the datatype of the column.</entry>
  1239. </row>
  1240. <row>
  1241. <entry>DEFAULT</entry>
  1242. <entry>(string)</entry>
  1243. <entry>Default value for the column, if any.</entry>
  1244. </row>
  1245. <row>
  1246. <entry>NULLABLE</entry>
  1247. <entry>(boolean)</entry>
  1248. <entry>True if the column accepts SQL NULLs, false if the column has a NOT NULL constraint.</entry>
  1249. </row>
  1250. <row>
  1251. <entry>LENGTH</entry>
  1252. <entry>(integer)</entry>
  1253. <entry>Length or size of the column as reported by the RDBMS.</entry>
  1254. </row>
  1255. <row>
  1256. <entry>SCALE</entry>
  1257. <entry>(integer)</entry>
  1258. <entry>Scale of SQL NUMERIC or DECIMAL type.</entry>
  1259. </row>
  1260. <row>
  1261. <entry>PRECISION</entry>
  1262. <entry>(integer)</entry>
  1263. <entry>Precision of SQL NUMERIC or DECIMAL type.</entry>
  1264. </row>
  1265. <row>
  1266. <entry>UNSIGNED</entry>
  1267. <entry>(boolean)</entry>
  1268. <entry>True if an integer-based type is reported as UNSIGNED.</entry>
  1269. </row>
  1270. <row>
  1271. <entry>PRIMARY</entry>
  1272. <entry>(boolean)</entry>
  1273. <entry>True if the column is part of the primary key of this table.</entry>
  1274. </row>
  1275. <row>
  1276. <entry>PRIMARY_POSITION</entry>
  1277. <entry>(integer)</entry>
  1278. <entry>Ordinal position (1-based) of the column in the primary key.</entry>
  1279. </row>
  1280. <row>
  1281. <entry>IDENTITY</entry>
  1282. <entry>(boolean)</entry>
  1283. <entry>True if the column uses an auto-generated value.</entry>
  1284. </row>
  1285. </tbody>
  1286. </tgroup>
  1287. </table>
  1288. <para>
  1289. If no table exists matching the table name and optional schema name
  1290. specified, then <code>describeTable()</code> returns an empty array.
  1291. </para>
  1292. </sect2>
  1293. <sect2 id="zend.db.adapter.closing">
  1294. <title>Closing a Connection</title>
  1295. <para>
  1296. Normally it is not necessary to close a database connection. PHP
  1297. automatically cleans up all resources and the end of a request.
  1298. Database extensions are designed to close the connection as the
  1299. reference to the resource object is cleaned up.
  1300. </para>
  1301. <para>
  1302. However, if you have a long-duration PHP script that initiates many
  1303. database connections, you might need to close the connection, to avoid
  1304. exhausting the capacity of your RDBMS server. You can use the
  1305. Adapter's <code>closeConnection()</code> method to explicitly close
  1306. the underlying database connection.
  1307. </para>
  1308. <example id="zend.db.adapter.closing.example">
  1309. <title>Closing a database connection</title>
  1310. <programlisting role="php"><![CDATA[<?php
  1311. $db->closeConnection();]]>
  1312. </programlisting>
  1313. </example>
  1314. <note>
  1315. <title>Does Zend_Db support persistent connections?</title>
  1316. <para>
  1317. The usage of persistent connections is not supported
  1318. or encouraged in Zend_Db.
  1319. </para>
  1320. <para>
  1321. Using persistent connections can cause an excess of idle
  1322. connections on the RDBMS server, which causes more problems
  1323. than any performance gain you might achieve by reducing the
  1324. overhead of making connections.
  1325. </para>
  1326. <para>
  1327. Database connections have state. That is, some objects in the
  1328. RDBMS server exist in session scope. Examples are locks, user
  1329. variables, temporary tables, and information about the most
  1330. recently executed query, such as rows affected, and last
  1331. generated id value. If you use persistent connections, your
  1332. application could access invalid or privileged data that were
  1333. created in a previous PHP request.
  1334. </para>
  1335. </note>
  1336. </sect2>
  1337. <sect2 id="zend.db.adapter.other-statements">
  1338. <title>Running Other Database Statements</title>
  1339. <para>
  1340. There might be cases in which you need to access the connection
  1341. object directly, as provided by the PHP database extension. Some
  1342. of these extensions may offer features that are not surfaced by
  1343. methods of Zend_Db_Adapter_Abstract.
  1344. </para>
  1345. <para>
  1346. For example, all SQL statements run by Zend_Db are prepared, then
  1347. executed. However, some database features are incompatible with
  1348. prepared statements. DDL statements like CREATE and ALTER cannot
  1349. be prepared in MySQL. Also, SQL statements don't benefit
  1350. from the <ulink url="http://dev.mysql.com/doc/refman/5.1/en/query-cache-how.html">MySQL Query Cache</ulink>,
  1351. prior to MySQL 5.1.17.
  1352. </para>
  1353. <para>
  1354. Most PHP database extensions provide a method to execute SQL
  1355. statements without preparing them. For example, in PDO, this
  1356. method is <code>exec()</code>. You can access the connection
  1357. object in the PHP extension directly using getConnection().
  1358. </para>
  1359. <example id="zend.db.adapter.other-statements.example">
  1360. <title>Running a non-prepared statement in a PDO adapter</title>
  1361. <programlisting role="php"><![CDATA[<?php
  1362. $result = $db->getConnection()->exec('DROP TABLE bugs');]]>
  1363. </programlisting>
  1364. </example>
  1365. <para>
  1366. Similarly, you can access other methods or properties that are
  1367. specific to PHP database extensions. Be aware, though, that by
  1368. doing this you might constrain your application to the interface
  1369. provided by the extension for a specific brand of RDBMS.
  1370. </para>
  1371. <para>
  1372. In future versions of Zend_Db, there will be opportunities to
  1373. add method entry points for functionality that is common to
  1374. the supported PHP database extensions. This will not affect
  1375. backward compatibility.
  1376. </para>
  1377. </sect2>
  1378. <sect2 id="zend.db.adapter.adapter-notes">
  1379. <title>Notes on Specific Adapters</title>
  1380. <para>
  1381. This section lists differences between the Adapter classes of which
  1382. you should be aware.
  1383. </para>
  1384. <sect3 id="zend.db.adapter.adapter-notes.ibm-db2">
  1385. <title>IBM DB2</title>
  1386. <itemizedlist>
  1387. <listitem>
  1388. <para>
  1389. Specify this Adapter to the factory() method with the
  1390. name 'Db2'.
  1391. </para>
  1392. </listitem>
  1393. <listitem>
  1394. <para>
  1395. This Adapter uses the PHP extension ibm_db2.
  1396. </para>
  1397. </listitem>
  1398. <listitem>
  1399. <para>
  1400. IBM DB2 supports both sequences and auto-incrementing
  1401. keys. Therefore the arguments to
  1402. <code>lastInsertId()</code> are optional. If you give
  1403. no arguments, the Adapter returns the last value
  1404. generated for an auto-increment key. If you give
  1405. arguments, the Adapter returns the last value generated
  1406. by the sequence named according to the convention
  1407. '<emphasis>table</emphasis>_<emphasis>column</emphasis>_seq'.
  1408. </para>
  1409. </listitem>
  1410. </itemizedlist>
  1411. </sect3>
  1412. <sect3 id="zend.db.adapter.adapter-notes.mysqli">
  1413. <title>MySQLi</title>
  1414. <itemizedlist>
  1415. <listitem>
  1416. <para>
  1417. Specify this Adapter to the <code>factory()</code>
  1418. method with the name 'Mysqli'.
  1419. </para>
  1420. </listitem>
  1421. <listitem>
  1422. <para>
  1423. This Adapter utilizes the PHP extension mysqli.
  1424. </para>
  1425. </listitem>
  1426. <listitem>
  1427. <para>
  1428. MySQL does not support sequences, so
  1429. <code>lastInsertId()</code> ignores its arguments and
  1430. always returns the last value generated for an
  1431. auto-increment key. The <code>lastSequenceId()</code>
  1432. method returns <code>null</code>.
  1433. </para>
  1434. </listitem>
  1435. </itemizedlist>
  1436. </sect3>
  1437. <sect3 id="zend.db.adapter.adapter-notes.oracle">
  1438. <title>Oracle</title>
  1439. <itemizedlist>
  1440. <listitem>
  1441. <para>
  1442. Specify this Adapter to the <code>factory()</code>
  1443. method with the name 'Oracle'.
  1444. </para>
  1445. </listitem>
  1446. <listitem>
  1447. <para>
  1448. This Adapter uses the PHP extension oci8.
  1449. </para>
  1450. </listitem>
  1451. <listitem>
  1452. <para>
  1453. Oracle does not support auto-incrementing keys, so you
  1454. should specify the name of a sequence to
  1455. <code>lastInsertId()</code> or
  1456. <code>lastSequenceId()</code>.
  1457. </para>
  1458. </listitem>
  1459. <listitem>
  1460. <para>
  1461. The Oracle extension does not support positional
  1462. parameters. You must use named parameters.
  1463. </para>
  1464. </listitem>
  1465. <listitem>
  1466. <para>
  1467. Currently the <code>Zend_Db::CASE_FOLDING</code> option
  1468. is not supported by the Oracle adapter. To use this
  1469. option with Oracle, you must use the PDO OCI adapter.
  1470. </para>
  1471. </listitem>
  1472. </itemizedlist>
  1473. </sect3>
  1474. <sect3 id="zend.db.adapter.adapter-notes.pdo-mssql">
  1475. <title>PDO Microsoft SQL Server</title>
  1476. <itemizedlist>
  1477. <listitem>
  1478. <para>
  1479. Specify this Adapter to the <code>factory()</code>
  1480. method with the name 'Pdo_Mssql'.
  1481. </para>
  1482. </listitem>
  1483. <listitem>
  1484. <para>
  1485. This Adapter uses the PHP extensions pdo and pdo_mssql.
  1486. </para>
  1487. </listitem>
  1488. <listitem>
  1489. <para>
  1490. Microsoft SQL Server does not support sequences, so
  1491. <code>lastInsertId()</code> ignores its arguments and
  1492. always returns the last value generated for an
  1493. auto-increment key. The <code>lastSequenceId()</code>
  1494. method returns <code>null</code>.
  1495. </para>
  1496. </listitem>
  1497. <listitem>
  1498. <para>
  1499. Zend_Db_Adapter_Pdo_Mssql sets <code>QUOTED_IDENTIFIER ON</code>
  1500. immediately after connecting to a SQL Server database.
  1501. This makes the driver use the standard SQL identifier
  1502. delimiter symbol (<code>"</code>) instead of the
  1503. proprietary square-brackets syntax SQL Server uses for
  1504. delimiting identifiers.
  1505. </para>
  1506. </listitem>
  1507. <listitem>
  1508. <para>
  1509. You can specify <code>pdoType</code> as a key in the
  1510. options array. The value can be "mssql" (the default),
  1511. "dblib", "freetds", or "sybase". This option affects
  1512. the DSN prefix the adapter uses when constructing the
  1513. DSN string. Both "freetds" and "sybase" imply a prefix
  1514. of "sybase:", which is used for the
  1515. <ulink url="http://www.freetds.org/">FreeTDS</ulink> set
  1516. of libraries.
  1517. See also
  1518. <ulink url="http://www.php.net/manual/en/ref.pdo-dblib.connection.php">
  1519. http://www.php.net/manual/en/ref.pdo-dblib.connection.php</ulink>
  1520. for more information on the DSN prefixes used in this driver.
  1521. </para>
  1522. </listitem>
  1523. </itemizedlist>
  1524. </sect3>
  1525. <sect3 id="zend.db.adapter.adapter-notes.pdo-mysql">
  1526. <title>PDO MySQL</title>
  1527. <itemizedlist>
  1528. <listitem>
  1529. <para>
  1530. Specify this Adapter to the <code>factory()</code>
  1531. method with the name 'Pdo_Mysql'.
  1532. </para>
  1533. </listitem>
  1534. <listitem>
  1535. <para>
  1536. This Adapter uses the PHP extensions pdo and pdo_mysql.
  1537. </para>
  1538. </listitem>
  1539. <listitem>
  1540. <para>
  1541. MySQL does not support sequences, so
  1542. <code>lastInsertId()</code> ignores its arguments and
  1543. always returns the last value generated for an
  1544. auto-increment key. The <code>lastSequenceId()</code>
  1545. method returns <code>null</code>.
  1546. </para>
  1547. </listitem>
  1548. </itemizedlist>
  1549. </sect3>
  1550. <sect3 id="zend.db.adapter.adapter-notes.pdo-oci">
  1551. <title>PDO Oracle</title>
  1552. <itemizedlist>
  1553. <listitem>
  1554. <para>
  1555. Specify this Adapter to the <code>factory()</code>
  1556. method with the name 'Pdo_Oci'.
  1557. </para>
  1558. </listitem>
  1559. <listitem>
  1560. <para>
  1561. This Adapter uses the PHP extensions pdo and pdo_oci.
  1562. </para>
  1563. </listitem>
  1564. <listitem>
  1565. <para>
  1566. Oracle does not support auto-incrementing keys, so you
  1567. should specify the name of a sequence to
  1568. <code>lastInsertId()</code> or
  1569. <code>lastSequenceId()</code>.
  1570. </para>
  1571. </listitem>
  1572. </itemizedlist>
  1573. </sect3>
  1574. <sect3 id="zend.db.adapter.adapter-notes.pdo-pgsql">
  1575. <title>PDO PostgreSQL</title>
  1576. <itemizedlist>
  1577. <listitem>
  1578. <para>
  1579. Specify this Adapter to the <code>factory()</code>
  1580. method with the name 'Pdo_Pgsql'.
  1581. </para>
  1582. </listitem>
  1583. <listitem>
  1584. <para>
  1585. This Adapter uses the PHP extensions pdo and pdo_pgsql.
  1586. </para>
  1587. </listitem>
  1588. <listitem>
  1589. <para>
  1590. PostgreSQL supports both sequences and auto-incrementing
  1591. keys. Therefore the arguments to
  1592. <code>lastInsertId()</code> are optional. If you give
  1593. no arguments, the Adapter returns the last value
  1594. generated for an auto-increment key. If you give
  1595. arguments, the Adapter returns the last value generated
  1596. by the sequence named according to the convention
  1597. '<emphasis>table</emphasis>_<emphasis>column</emphasis>_seq'.
  1598. </para>
  1599. </listitem>
  1600. </itemizedlist>
  1601. </sect3>
  1602. <sect3 id="zend.db.adapter.adapter-notes.pdo-sqlite">
  1603. <title>PDO SQLite</title>
  1604. <itemizedlist>
  1605. <listitem>
  1606. <para>
  1607. Specify this Adapter to the <code>factory()</code>
  1608. method with the name 'Pdo_Sqlite'.
  1609. </para>
  1610. </listitem>
  1611. <listitem>
  1612. <para>
  1613. This Adapter uses the PHP extensions pdo and pdo_sqlite.
  1614. </para>
  1615. </listitem>
  1616. <listitem>
  1617. <para>
  1618. SQLite does not support sequences, so
  1619. <code>lastInsertId()</code> ignores its arguments and
  1620. always returns the last value generated for an
  1621. auto-increment key. The <code>lastSequenceId()</code>
  1622. method returns <code>null</code>.
  1623. </para>
  1624. </listitem>
  1625. <listitem>
  1626. <para>
  1627. To connect to an SQLite2 database, specify
  1628. <code>'dsnprefix'=>'sqlite2'</code> in the array of
  1629. parameters when creating an instance of the
  1630. Pdo_Sqlite Adapter.
  1631. </para>
  1632. </listitem>
  1633. <listitem>
  1634. <para>
  1635. To connect to an in-memory SQLite database,
  1636. specify <code>'dbname'=>':memory:'</code> in the
  1637. array of parameters when creating an instance of
  1638. the Pdo_Sqlite Adapter.
  1639. </para>
  1640. </listitem>
  1641. <listitem>
  1642. <para>
  1643. Older versions of the SQLite driver for PHP do not seem
  1644. to support the PRAGMA commands necessary to ensure that
  1645. short column names are used in result sets. If you
  1646. have problems that your result sets are returned with
  1647. keys of the form "tablename.columnname" when you do a
  1648. join query, then you should upgrade to the current
  1649. version of PHP.
  1650. </para>
  1651. </listitem>
  1652. </itemizedlist>
  1653. </sect3>
  1654. </sect2>
  1655. </sect1>