PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/yadal/class.Yadal.php

https://github.com/reshadf/Library
PHP | 457 lines | 263 code | 40 blank | 154 comment | 16 complexity | 1cc60462eec310a03950b27987dd1fbd MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Yadal mail class and the yadal object creator function
  4. *
  5. * @package Yadal
  6. */
  7. define('YADAL_DEFAULT_DB_TYPE', 'mysql');
  8. /**
  9. * newYadal()
  10. *
  11. * Create a new database object of the correct type
  12. *
  13. * @param string $database: the database name to connect to
  14. * @param string $type: the type of database to use (default MySQL)
  15. * @return object
  16. * @access public
  17. * @author Teye Heimans
  18. */
  19. function newYadal( $database = null, $type = null )
  20. {
  21. // set the default database type if none is given
  22. if( is_null($type) )
  23. {
  24. $type = YADAL_DEFAULT_DB_TYPE;
  25. }
  26. switch( strtolower($type) )
  27. {
  28. // mysql
  29. case 'mysql':
  30. include_once dirname(__FILE__).'/class.MySQL.php';
  31. return new MySQL( $database );
  32. break;
  33. // postgresql
  34. case 'postgresql':
  35. case 'postgres':
  36. case 'pgsql':
  37. include_once dirname(__FILE__).'/class.PostgreSQL.php';
  38. return new PostgreSQL( $database );
  39. break;
  40. // Microsoft SQL server (MSSQL)
  41. case 'mssql':
  42. include_once dirname(__FILE__).'/class.MSSQL.php';
  43. return new MSSQL( $database );
  44. break;
  45. // Microsoft access database (Windows only)
  46. case 'access':
  47. include_once dirname(__FILE__).'/class.Access.php';
  48. return new Access( $database );
  49. break;
  50. // ODBC
  51. /* NOT SUPPORTED YET
  52. case 'odbc':
  53. include_once 'class.ODBC.php';
  54. return new ODBC( $database );
  55. break;
  56. */
  57. // wrong type given!
  58. default:
  59. trigger_error(
  60. 'Error, database type "'.$type.'" not supported!',
  61. E_USER_ERROR
  62. );
  63. return null;
  64. }
  65. }
  66. /**
  67. * class Yadal
  68. *
  69. * Yadal - Yet Another Database Abstraction Layer
  70. * Abstract database class
  71. *
  72. * @author Teye Heimans
  73. * @package Yadal
  74. */
  75. class Yadal
  76. {
  77. var $_conn; // resource: contains the connection resource
  78. var $_db; // string: contains the database name
  79. var $_table; // string: contains the table name
  80. var $_keys; // array: contains the primary keys
  81. var $_isConnected; // boolean: do we have a connection ?
  82. var $_quoteNumbers; // boolean: do we have to quote numbers?
  83. var $_nameQuote; // char/array: quote to use around table and field names (for possible spaces in the names)
  84. var $_lastQuery; // string: the last query executed
  85. var $_cache; // array: cache of most actions.
  86. /**
  87. * Yadal::Yadal()
  88. *
  89. * Abstract constructor: store the db name.
  90. * Dont use this class to make a new Yadal object!!! Use the function
  91. * newYadal() instead!!
  92. *
  93. * @param string $db: the database we are using
  94. * @author Teye Heimans
  95. */
  96. function Yadal( $db = null )
  97. {
  98. if( !is_null( $db ) )
  99. {
  100. $this->_db = $db;
  101. }
  102. $this->_isConnected = false;
  103. $this->_quoteNumbers = false;
  104. if( !isset($this->_nameQuote) || $this->_nameQuote == null )
  105. {
  106. $this->_nameQuote = '"';
  107. }
  108. }
  109. /**
  110. * Yadal::setConnectionResource()
  111. *
  112. * Instead of opening a new connection, set the
  113. * connection resource of the already opend connection
  114. *
  115. * @param resource $conn: The connection resource
  116. * @return void
  117. * @access public
  118. * @author Teye Heimans
  119. */
  120. function setConnectionResource( &$conn )
  121. {
  122. $this->_conn = &$conn;
  123. $this->_isConnected = true;
  124. }
  125. /**
  126. * Yadal::isConnected()
  127. *
  128. * Return if we have a connection or not
  129. *
  130. * @return boolean
  131. * @access public
  132. * @author Teye Heimans
  133. */
  134. function isConnected()
  135. {
  136. return $this->_isConnected;
  137. }
  138. /**
  139. * Yadal::dbDate()
  140. *
  141. * Convert the given date to the correct database format.
  142. *
  143. * @param string $y: The year of the date which should be converteds
  144. * @param string $m: The month of the date which should be converteds
  145. * @param string $d: The day of the date which should be converteds
  146. * @return string the date in the correct format or null when the date could not be converted
  147. * @access public
  148. * @author Teye Heimans
  149. */
  150. function dbDate( $y, $m, $d )
  151. {
  152. return "'$y-$m-$d'";
  153. }
  154. /**
  155. * Yadal::array2case()
  156. *
  157. * Change an array to a CASE statement which can be used in an query.
  158. * Example:
  159. * array2case( 'UserType', array( 1 => 'Admin', 2 => 'Moderator', 3 => 'User' ) );
  160. *
  161. * Result:
  162. * CASE `UserType`
  163. * WHEN 1 THEN 'Admin'
  164. * WHEN 2 THEN 'Moderator'
  165. * WHEN 3 THEN 'User'
  166. * ELSE 'Unknown'
  167. * END
  168. *
  169. * @param string $field: The field which we should use in the case or if statement
  170. * @param array $options: Array of options. The array key will be used as statement compare-item and the value will be used as value
  171. * @param string $default: The default value if none of the array keys are matched. Default is "Unknown". If you dont want to have a default value, use false
  172. * @return string
  173. * @access public
  174. * @author Teye Heimans
  175. */
  176. function array2case( $field, $options, $default = 'Unknown' )
  177. {
  178. // if there are 2 options, use an if statement
  179. if( sizeof( $options ) == 2 )
  180. {
  181. list( $key1, $value1 ) = $options;
  182. list( $key2, $value2 ) = $options;
  183. return
  184. "IF( ". $this->quote($field)." == '".$key1."', ".
  185. "'".$this->escapeString($value1)."', ".
  186. "'".$this->escapeString($value2)."' )";
  187. }
  188. // there are more then 2 options, use a case statement
  189. else
  190. {
  191. $result = "CASE ". $this->quote($field) ."\n";
  192. foreach( $options as $key => $value )
  193. {
  194. $result .= " WHEN '".$key."' THEN '".$this->escapeString( $value )."'\n";
  195. }
  196. if( $default )
  197. {
  198. $result .= " ELSE '".$this->escapeString($default)."'\n";
  199. }
  200. $result .= "END";
  201. return $result ;
  202. }
  203. }
  204. /**
  205. * Yadal::quoteNumbers()
  206. *
  207. * Do we have to quote numbers ?
  208. *
  209. * @return boolean
  210. * @access public
  211. * @author Teye Heimans
  212. */
  213. function quoteNumbers()
  214. {
  215. return $this->_quoteNumbers;
  216. }
  217. /**
  218. * Yadal::quote()
  219. *
  220. * Return the table name or field name quoted (so that spaces can be used)
  221. *
  222. * @param string $name: The table or field name which should me quoted
  223. * @return string
  224. * @access public
  225. * @author Teye Heimans
  226. */
  227. function quote( $name )
  228. {
  229. // is there a dot in the name ? (like table.name)
  230. $pos = strpos($name, '.');
  231. if( $pos !== false)
  232. {
  233. return
  234. $this->quote( substr($name, 0, $pos ) ) .'.'.
  235. $this->quote( substr($name, $pos + 1) );
  236. }
  237. if( is_array($this->_nameQuote))
  238. {
  239. return $this->_nameQuote[0] . $name . $this->_nameQuote[1];
  240. }
  241. else
  242. {
  243. return $this->_nameQuote . $name . $this->_nameQuote;
  244. }
  245. }
  246. /**
  247. * Yadal::clearCache()
  248. *
  249. * Clears the cache of most functions, like getUniqueFields, getNotNullFields, etc.
  250. *
  251. * @return void
  252. * @access public
  253. * @author Teye Heimans
  254. */
  255. function clearCache()
  256. {
  257. $this->_cache = null;
  258. }
  259. /**
  260. * Yadal::getLastQuery()
  261. *
  262. * Return the last query which was executed
  263. *
  264. * @return string
  265. * @access public
  266. * @author Teye Heimans
  267. */
  268. function getLastQuery()
  269. {
  270. return $this->_lastQuery;
  271. }
  272. /**
  273. * Yadal::result()
  274. *
  275. * Return a specific result of a sql resource
  276. *
  277. * @param resource $sql: The sql where you want to get a result from
  278. * @param int $row: The row where you want a result from
  279. * @param string $field: The field which result you want
  280. * @return string
  281. * @access public
  282. * @author Teye Heimans
  283. */
  284. function result( $sql, $row = 0, $field = null )
  285. {
  286. $i = 0;
  287. while( $data = $this->getRecord( $sql ) )
  288. {
  289. if( $i++ == $row )
  290. {
  291. break;
  292. }
  293. }
  294. if( is_null( $field ) )
  295. {
  296. list( , $value ) = each($data);
  297. return $value;
  298. }
  299. else
  300. {
  301. foreach( $data as $column => $value )
  302. {
  303. if( strtolower($column) == strtolower( $field ) )
  304. {
  305. return $value;
  306. }
  307. }
  308. }
  309. return false;
  310. }
  311. function getTables( )
  312. {
  313. trigger_error(
  314. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  315. E_USER_WARNING
  316. );
  317. }
  318. function getFieldTypes( )
  319. {
  320. trigger_error(
  321. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  322. E_USER_WARNING
  323. );
  324. }
  325. function connect( )
  326. {
  327. trigger_error(
  328. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  329. E_USER_WARNING
  330. );
  331. }
  332. function getNotNullFields()
  333. {
  334. trigger_error(
  335. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  336. E_USER_WARNING
  337. );
  338. }
  339. function getUniqueFields()
  340. {
  341. trigger_error(
  342. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  343. E_USER_WARNING
  344. );
  345. }
  346. function getPrKeys()
  347. {
  348. trigger_error(
  349. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  350. E_USER_WARNING
  351. );
  352. }
  353. function recordCount()
  354. {
  355. trigger_error(
  356. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  357. E_USER_WARNING
  358. );
  359. }
  360. function getFieldNames()
  361. {
  362. trigger_error(
  363. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  364. E_USER_WARNING
  365. );
  366. }
  367. function escapeString()
  368. {
  369. trigger_error(
  370. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  371. E_USER_WARNING
  372. );
  373. }
  374. function getRecord()
  375. {
  376. trigger_error(
  377. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  378. E_USER_WARNING
  379. );
  380. }
  381. function getInsertId()
  382. {
  383. trigger_error(
  384. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  385. E_USER_WARNING
  386. );
  387. }
  388. function query()
  389. {
  390. trigger_error(
  391. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  392. E_USER_WARNING
  393. );
  394. }
  395. function close()
  396. {
  397. trigger_error(
  398. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  399. E_USER_WARNING
  400. );
  401. }
  402. function getError()
  403. {
  404. trigger_error(
  405. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  406. E_USER_WARNING
  407. );
  408. }
  409. function GetErrorNo()
  410. {
  411. trigger_error(
  412. 'Error, abstract function '.__FUNCTION__.' has not been overwritten by class '.get_class( $this ),
  413. E_USER_WARNING
  414. );
  415. }
  416. }
  417. ?>