PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/yadal/class.MySQL.php

https://github.com/reshadf/Library
PHP | 516 lines | 241 code | 63 blank | 212 comment | 27 complexity | 4537b7f1b0e030caad9a23480b042a56 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Yadal interface for the MySQL database type
  4. *
  5. * @package Yadal
  6. */
  7. /**
  8. * class MySQL
  9. *
  10. * Yadal - Yet Another Database Abstraction Layer
  11. * MySQL class
  12. *
  13. * @author Teye Heimans
  14. * @package Yadal
  15. */
  16. class MySQL extends Yadal
  17. {
  18. /**
  19. * MySQL::MySQL()
  20. *
  21. * Constructor: set the database we should be using
  22. *
  23. * @param string $db: The database which should be used
  24. * @author Teye Heimans
  25. */
  26. function MySQL( $db )
  27. {
  28. $this->Yadal( $db );
  29. $this->_quoteNumbers = true;
  30. $this->_nameQuote = '`';
  31. }
  32. /**
  33. * MySQL::connect()
  34. *
  35. * Make a connection with the database and
  36. * select the database.
  37. *
  38. * @param string host: the host to connect to
  39. * @param string username: the username which should be used to login
  40. * @param string password: the password which should be used to login
  41. * @return resource: The connection resource
  42. * @access public
  43. * @author Teye Heimans
  44. */
  45. function connect( $host = 'localhost', $username = '', $password = '' )
  46. {
  47. // connect with the mysql database
  48. $this->_conn = mysql_connect( $host, $username, $password );
  49. // connection made?
  50. if( $this->_conn )
  51. {
  52. // select the database
  53. if(mysql_select_db( $this->_db, $this->_conn ))
  54. {
  55. $this->_isConnected = true;
  56. // return the connection resource
  57. return $this->_conn;
  58. }
  59. }
  60. return false;
  61. }
  62. /**
  63. * MySQL::close()
  64. *
  65. * Close the connection
  66. *
  67. * @return bool
  68. * @access public
  69. * @author Teye Heimans
  70. */
  71. function close()
  72. {
  73. if( $this->_isConnected )
  74. {
  75. $this->_isConnected = false;
  76. return mysql_close( $this->_conn );
  77. }
  78. return true;
  79. }
  80. /**
  81. * MySQL::query()
  82. *
  83. * Execute the query
  84. *
  85. * @param string $query: the query which should be executed
  86. * @return resource
  87. * @access public
  88. * @author Teye Heimans
  89. */
  90. function query( $query )
  91. {
  92. $this->_lastQuery = $query;
  93. return mysql_query( $query, $this->_conn );
  94. }
  95. /**
  96. * MySQL::getInsertId()
  97. *
  98. * Get the id of the last inserted record
  99. *
  100. * @return int
  101. * @access public
  102. * @author Teye Heimans
  103. */
  104. function getInsertId()
  105. {
  106. return mysql_insert_id();
  107. }
  108. /**
  109. * MySQL::result()
  110. *
  111. * Return a specific result of a sql resource
  112. *
  113. * @param resource $sql: The sql where you want to get a result from
  114. * @param int $row: The row where you want a result from
  115. * @param string $field: The field which result you want
  116. * @return string
  117. * @access public
  118. * @author Teye Heimans
  119. */
  120. function result( $sql, $row = 0, $field = null )
  121. {
  122. return mysql_result( $sql, $row, $field );
  123. }
  124. /**
  125. * MySQL::getError()
  126. *
  127. * Return the last error
  128. *
  129. * @return string
  130. * @access public
  131. * @author Teye Heimans
  132. */
  133. function getError()
  134. {
  135. return mysql_error();
  136. }
  137. /**
  138. * MySQL::getErrorNo()
  139. *
  140. * Return the error number
  141. *
  142. * @return int
  143. * @access public
  144. * @author Teye Heimans
  145. */
  146. function getErrorNo()
  147. {
  148. return mysql_errno();
  149. }
  150. /**
  151. * MySQL::recordCount()
  152. *
  153. * Return the number of records found by the query
  154. *
  155. * @param resource $sql: The resource which should be counted
  156. * @return int
  157. * @access public
  158. * @author Teye Heimans
  159. */
  160. function recordCount( $sql )
  161. {
  162. return mysql_num_rows( $sql );
  163. }
  164. /**
  165. * MySQL::getRecord()
  166. *
  167. * Fetch a record in assoc mode and return it
  168. *
  169. * @param resource $sql: The resource which should be used to retireve a record from
  170. * @return assoc array or false when there are no records left
  171. * @access public
  172. * @author Teye Heimans
  173. */
  174. function getRecord( $sql )
  175. {
  176. return mysql_fetch_assoc( $sql );
  177. }
  178. /**
  179. * MySQL::getFieldNames()
  180. *
  181. * Return the field names of the table
  182. *
  183. * @param string $table: The table where the field names should be collected from
  184. * @return array
  185. * @access public
  186. * @author Teye Heimans
  187. */
  188. function getFieldNames( $table )
  189. {
  190. $t = strtolower($table);
  191. // return the data from the cache if it exists
  192. if( isset( $this->_cache['fields'][$t] ) )
  193. {
  194. return $this->_cache['fields'][$t];
  195. }
  196. $result = array();
  197. // check if we have a connection handler..
  198. // if so, fetch the column names
  199. if( $this->_conn && !empty($this->_db) )
  200. {
  201. $fields = mysql_list_fields( $this->_db, $table, $this->_conn );
  202. $columns = mysql_num_fields($fields);
  203. for ($i = 0; $i < $columns; $i++)
  204. {
  205. $result[] = mysql_field_name($fields, $i);
  206. }
  207. }
  208. // no connection handler available
  209. else
  210. {
  211. // try to get a record and fetch the field names..
  212. $sql = $this->query( 'DESCRIBE ' . $this->quote( $table ) );
  213. // query succeeded?
  214. if( $sql )
  215. {
  216. while( $row = mysql_fetch_assoc( $sql ) )
  217. {
  218. $result[] = $row['Field'];
  219. }
  220. }
  221. else
  222. {
  223. trigger_error(
  224. "Could not retrieve the field names for the table '".$table."'.\n".
  225. "Query: ".$this->getLastQuery()."\n".
  226. "Error: ".$this->getError(),
  227. E_USER_WARNING
  228. );
  229. return false;
  230. }
  231. mysql_free_result( $sql );
  232. }
  233. // save the result in the cache
  234. $this->_cache['fields'][$t] = $result;
  235. return $result;
  236. }
  237. /**
  238. * MySQL::getTables()
  239. *
  240. * Return the tables from the database
  241. *
  242. * @return array
  243. * @access public
  244. * @author Teye Heimans
  245. */
  246. function getTables()
  247. {
  248. // return the data from the cache if it exists
  249. if( isset( $this->_cache['tables'] ) )
  250. {
  251. return $this->_cache['tables'];
  252. }
  253. $sql = $this->query('SHOW TABLES;');
  254. // query failed ?
  255. if( !$sql )
  256. {
  257. trigger_error(
  258. "Could not retrieve the tables from the database!\n".
  259. "Query: ".$this->getLastQuery()."\n".
  260. "Error: ".$this->getError(),
  261. E_USER_WARNING
  262. );
  263. return false;
  264. }
  265. // save the table names in an array and return them
  266. $result = array();
  267. $num = $this->recordCount( $sql );
  268. for( $i = 0; $i < $num; $i++ )
  269. {
  270. $result[] = $this->result( $sql, $i);
  271. }
  272. // save the result in the cache
  273. $this->_cache['tables'] = $result;
  274. return $result;
  275. }
  276. /**
  277. * MySQL::getNotNullFields()
  278. *
  279. * Retrieve the fields that can not contain NULL
  280. *
  281. * @param string $table: The table which fields we should retrieve
  282. * @return array
  283. * @access public
  284. * @author Teye Heimans
  285. */
  286. function getNotNullFields ( $table )
  287. {
  288. $t = strtolower($table);
  289. // return the data from the cache if it exists
  290. if( isset( $this->_cache['notnull'][$t] ) )
  291. {
  292. return $this->_cache['notnull'][$t];
  293. }
  294. $sql = $this->query('DESCRIBE '.$this->quote( $table ) );
  295. if( $sql )
  296. {
  297. // save the not null fields in an array
  298. $result = array();
  299. while( $r = mysql_fetch_assoc( $sql ) ) {
  300. if( $r['Null'] == 'NO' || empty($r['Null']) ) {
  301. $result[] = $r['Field'];
  302. }
  303. }
  304. }
  305. else
  306. {
  307. // display the error message when the not null fields could not be retrieved
  308. trigger_error(
  309. "Could not retrieve the not-null-field from the table '".$table."'.\n".
  310. "Query: ".$this->getLastQuery()."\n".
  311. "Error: ".$this->getError(),
  312. E_USER_WARNING
  313. );
  314. return false;
  315. }
  316. // save the result in the cache
  317. $this->_cache['notnull'][$t] = $result;
  318. return $result;
  319. }
  320. /**
  321. * MySQL::getFieldTypes()
  322. *
  323. * Retrieve the field types of the given table
  324. *
  325. * @param string $table: The table where we should fetch the fields and their types from
  326. * @return array
  327. * @access public
  328. * @author Teye Heimans
  329. */
  330. function getFieldTypes( $table )
  331. {
  332. $t = strtolower($table);
  333. // return the data from the cache if it exists
  334. if( isset( $this->_cache['fieldtypes'][$t] ) )
  335. {
  336. return $this->_cache['fieldtypes'][$t];
  337. }
  338. // Get the default values for the fields
  339. $sql = $this->query("DESCRIBE ".$this->quote($table));
  340. // query failed ?
  341. if( !$sql )
  342. {
  343. trigger_error(
  344. "Could not fetch the meta data of the columns for table '".$table."'.\n".
  345. "Query: ".$this->getLastQuery()."\n".
  346. "Error: ".$this->getError(),
  347. E_USER_WARNING
  348. );
  349. return false;
  350. }
  351. $result = array();
  352. while( $row = $this->getRecord( $sql ) )
  353. {
  354. // split the size from the type
  355. if( preg_match('/^(.*)\((\d+)\)$/', $row['Type'], $match) )
  356. {
  357. $type = $match[1];
  358. $length = $match[2];
  359. }
  360. else
  361. {
  362. $type = $row['Type'];
  363. $length = null;
  364. }
  365. $result[ $row['Field'] ] = array(
  366. $type,
  367. $length,
  368. $row['Default']
  369. );
  370. }
  371. // save the result in the cache
  372. $this->_cache['fieldtypes'][$t] = $result;
  373. return $result;
  374. }
  375. /**
  376. * MySQL::escapeString()
  377. *
  378. * Escape the string we are going to save from dangerous characters
  379. *
  380. * @param string $string: The string to escape
  381. * @return string
  382. * @access public
  383. * @author Teye Heimans
  384. */
  385. function escapeString( $string )
  386. {
  387. return mysql_real_escape_string( $string );
  388. }
  389. /**
  390. * MySQL::getPrKeys()
  391. *
  392. * Fetch the keys from the table
  393. *
  394. * @param string $table: The table where we should fetch the keys from
  395. * @return array of the keys which are found
  396. * @access public
  397. * @author Teye Heimans
  398. */
  399. function getPrKeys( $table )
  400. {
  401. $t = strtolower($table);
  402. // return the data from the cache if it exists
  403. if( isset( $this->_cache['keys'][$t] ) ) {
  404. return $this->_cache['keys'][$t];
  405. }
  406. $sql = $this->query("SHOW KEYS FROM `".$table."`");
  407. $keys = array();
  408. while( $r = $this->getRecord($sql) ) {
  409. if ( $r['Key_name'] == 'PRIMARY' ) {
  410. $keys[] = $r['Column_name'];
  411. }
  412. }
  413. mysql_free_result($sql);
  414. // save the result in the cache
  415. $this->_cache['keys'][$t] = $keys;
  416. return $keys;
  417. }
  418. /**
  419. * MySQL::getUniqueFields()
  420. *
  421. * Fetch the unique fields from the table
  422. *
  423. * @param string $table: The table where the unique-value-field should be collected from
  424. * @return array: multidimensional array of the unique indexes on the table
  425. * @access public
  426. * @author Teye Heimans
  427. */
  428. function getUniqueFields( $table )
  429. {
  430. $t = strtolower( $table );
  431. // return the data from the cache if it exists
  432. if( isset( $this->_cache['unique'][$t] ) )
  433. {
  434. return $this->_cache['unique'][$t];
  435. }
  436. // get the keys
  437. $sql = $this->query("SHOW KEYS FROM ". $this->quote($table) );
  438. $unique = array();
  439. // save all keys which have to be unique
  440. while( $r = $this->getRecord($sql) )
  441. {
  442. if ( $r['Non_unique'] == 0 )
  443. {
  444. $unique[$r['Key_name']][] = $r['Column_name'];
  445. }
  446. }
  447. mysql_free_result($sql);
  448. // save the result in the cache
  449. $this->_cache['unique'][$t] = $unique;
  450. return $unique;
  451. }
  452. }
  453. ?>