MDB_QueryTool
[ class tree: MDB_QueryTool ] [ index: MDB_QueryTool ] [ all elements ]

Source for file example.php

Documentation is available at example.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.02 of the PHP license,      |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors:  Wolfram Kriesing, Paolo Panto, vision:produktion <wk@visionp.de>
  16. // |           Lorenzo Alberton <l.alberton@quipo.it>                     |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: example.php 126432 2003-05-10 00:10:00Z quipo $
  20. //
  21.  
  22. require_once 'MDB/QueryTool.php';
  23.  
  24.  
  25. // change this!!!!! and the DSN to your DB
  26. $DB_BACKEND 'pgsql';
  27. $DB_BACKEND 'mysql';
  28.  
  29.  
  30.  
  31. define ('TABLE_TIME',   'time');
  32. // the mysql setup!
  33. if ($DB_BACKEND == 'mysql'{
  34.     $dbDSN 'mysql://root@localhost/test';
  35.     define ('TABLE_USER',   'user');
  36. }
  37.  
  38. // postgreSQL setup, use PEAR::DB >1.4b1
  39. if ($DB_BACKEND == 'pgsql'{
  40.     $dbDSN "pgsql://test:test@/test";
  41.     define ('TABLE_USER',   'uuser');     // user is a reserved word in postgres
  42. }
  43.  
  44.                         
  45. class user extends MDB_QueryTool
  46. {
  47.     var $table =        TABLE_USER;
  48.     var $sequenceName = TABLE_USER;
  49.  
  50.     // this is default, but to demonstrate it here ...
  51.     var $primaryCol =   'id';
  52.  
  53.     /**
  54.     *   this table spec assigns a short name to a table name
  55.     *   this short name is needed in case the table name changes
  56.     *   i.e. when u put the appl on a providers db, where you have to prefix
  57.     *   each table, and you dont need to change the entire appl to where you refer
  58.     *   to joined table columns, for that joined results the short name is used
  59.     *   instead of the table name
  60.     */
  61.     var $tableSpec = array(
  62.                             array('name'    =>  TABLE_USER'shortName' =>  'user')
  63.                             ,array('name'   =>  TABLE_TIME'shortName' =>  'time')
  64.                         );
  65.  
  66.  
  67. }
  68.  
  69.  
  70.  
  71. // this set an error-callback method, which will be called in case something really went wrong
  72. // you can use 'setErrorLogCallback' to define the callback method for log-messages - non critical.
  73. // and you can use 'setErrorSetCallback' to define a callback for errors which prevent the query
  74. // from properly executing
  75. $user = new user($dbDSNarray('errorCallback'=>'myPrint'));
  76. //$user->setErrorCallback('myPrint'); this could be used too
  77.  
  78.  
  79. //
  80. //      1
  81. //
  82. // get a single entry with a given 'id'
  83. headline('$user->get(3)');
  84. $user->reset();     // reset the query-builder, so no where, order, etc. are set
  85. $res $user->get(3);
  86. myPrint($res);
  87. // query: SELECT * FROM user WHERE id=3
  88.  
  89.  
  90. //
  91. //      2
  92. //
  93. // get all entries from the table
  94. headline('$user->getAll()');
  95. $user->reset();     // reset the query-builder, so no where, order, etc. are set
  96. $res $user->getAll();
  97. myPrint($res);
  98. // query: SELECT * FROM user
  99.  
  100.  
  101. //
  102. //      3
  103. //
  104. // get the first 10 entries from the table (LIMIT 0,10)
  105. headline('$user->getAll(0,10)');
  106. $user->reset();     // reset the query-builder, so no where, order, etc. are set
  107. $res $user->getAll(0,10);
  108. myPrint($res);
  109. // query: SELECT * FROM user LIMIT 0,10
  110.  
  111.  
  112. //
  113. //      4
  114. //
  115. // get all data where the id>3
  116. headline('using setWhere');
  117. $user->reset();     // reset the query-builder, so no where, order, etc. are set
  118. $user->setWhere('id>3');
  119. $res $user->getAll();
  120. myPrint($res);
  121. // query: SELECT * FROM user WHERE id>3
  122.  
  123.  
  124. //
  125. //      5
  126. //
  127. // setting multiple query-parts
  128. headline('using set[Where,Order,Group]');
  129. $user->reset();     // reset the query-builder, so no where, order, etc. are set
  130. // for proper SQL92 i think we need to select only the col we use in group
  131. // in mysql you can also leave out the following line
  132. $user->setSelect('name');
  133. $user->setWhere('name IS NOT NULL');
  134. $user->setOrder('name');
  135. $user->setGroup('name');
  136. $res $user->getAll();
  137. myPrint($res);
  138. // query: SELECT * FROM user WHERE name IS NOT NULL GROUP BY name ORDER BY name
  139.  
  140.  
  141. //
  142. //      6
  143. //
  144. // setting multiple query-parts
  145. headline('using setIndex');
  146. $user->reset();     // reset the query-builder, so no where, order, etc. are set
  147. $user->setIndex('name');
  148. $res $user->getAll();
  149. myPrint($res);
  150. // query: SELECT * FROM user
  151. // the result-array is indexed by 'name'
  152.  
  153.  
  154.  
  155. //
  156. //      7
  157. //
  158. // setting multiple query-parts
  159. headline('using setIndex using 2 columns!');
  160. $user->reset();     // reset the query-builder, so no where, order, etc. are set
  161. $user->setIndex('name,surname');
  162. $res $user->getAll();
  163. myPrint($res);
  164. // query: SELECT * FROM user
  165. // the result-array is indexed by 'name,surname'
  166.  
  167.  
  168.  
  169. //
  170. //      8
  171. //
  172. // join the table 'time' this automatically detects where there are
  173. // columns that refer to one another, this uses a regExp that you can simply
  174. // modify (see $_tableNamePreg and $_columnNamePreg), by default this maps
  175. // stuff like 'user_id'=>'user.id'
  176. headline('using autoJoin');
  177. $user->reset();     // reset the query-builder, so no where, order, etc. are set
  178. $user->autoJoin('time');
  179. $user->setOrder('surname');
  180. $res $user->getAll();
  181. myPrint($res);
  182. // query: SELECT * FROM user,time WHERE user.id=time.user_id ORDER BY surname
  183.  
  184.  
  185. //
  186. //      9
  187. //
  188. // does the same as the example above
  189. // only that you have to hardcode the join by hand, no autoJoin here
  190. headline('using setJoin instead of autoJoin');
  191. $user->reset();     // reset the query-builder, so no where, order, etc. are set
  192. $user->setJoin('time',TABLE_USER.'.id=time.user_id');
  193. $user->setOrder('surname');
  194. $res $user->getAll();
  195. myPrint($res);
  196. // query: SELECT * FROM user,time WHERE user.id=time.user_id ORDER BY surname
  197.  
  198.  
  199. //
  200. //      10
  201. //
  202. headline('adding data using $user->save($data)');
  203. $data = array('login'=>'new','name'=>'foo','surname'=>'bar');
  204. $fooBarId $user->save($data);
  205. myPrint($fooBarId);
  206. // query: INSERT INTO user (id,login,name,surname) VALUES (<sequences>,"new","foo","bar")
  207.  
  208.  
  209. //
  210. //      11
  211. //
  212. headline('updating using $user->save($data)');
  213. $data = array('id'=>$fooBarId,'login'=>'NEW','name'=>'Mr. foo');
  214. $res $user->save($data);
  215. myPrint($res);
  216. // query: UPDATE user (id,login,name,surname) VALUES (<sequences>,"new","foo","bar")
  217.  
  218.  
  219. //
  220. //      12
  221. //
  222. headline('updating using $user->update($data)');
  223. $res $user->update($data);
  224. myPrint($res);
  225. // query: UPDATE user (id,login,name,surname) VALUES (<sequences>,"new","foo","bar")
  226.  
  227.  
  228. //
  229. //      13
  230. //
  231. headline("remove the entry \$user->remove($fooBarId)");
  232. $res $user->remove($fooBarId);
  233. myPrint($res);
  234. // query: DELETE FROM "uuser" WHERE "id" = $fooBarId
  235.  
  236.  
  237.  
  238.  
  239.  
  240. //
  241. //  helper functions
  242. //
  243. function headline$string='' )
  244. {
  245.     global $headlineCnt;
  246.  
  247.     if$string )
  248.     {
  249.         $headlineCnt++;
  250.         print "<h1>$headlineCnt - $string</h1>";
  251.     }
  252. }
  253. function myPrint($data)
  254. {
  255.     print "<pre>";
  256.     print_r($data);
  257.     print '</pre>';
  258. }
  259.  
  260. ?>

Documentation generated on Mon, 11 Mar 2019 15:48:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.