/framework/core/db/DbMysql.php

http://zoop.googlecode.com/ · PHP · 93 lines · 58 code · 12 blank · 23 comment · 3 complexity · 7c0e34ab9bf541fd35f0628ded4d043f MD5 · raw file

  1. <?php
  2. class DbMysql extends DbConnection
  3. {
  4. private $connection;
  5. public function getRequireds()
  6. {
  7. return array('database', 'username');
  8. }
  9. public function getDefaults()
  10. {
  11. return array('host' => 'localhost', 'port' => 3306, 'password' => NULL);
  12. }
  13. /**
  14. * Escapes the field name (to handle fields with special names, i.e. insert, select, update, now, date, etc)
  15. *
  16. * @param string $fieldName name of the field
  17. * @return string escaped string (this method should be overridden by each db class to escape properly if needed)
  18. */
  19. public function escapeIdentifier($fieldName)
  20. {
  21. return "`" . $fieldName . "`";
  22. }
  23. /**
  24. * Checks if a given table exists in the database
  25. *
  26. * @param string $name Name of the table to look for
  27. * @return boolean True if the table exists in this database
  28. */
  29. public function tableExists($name)
  30. {
  31. trigger_error("tableExists method not yet implemented in DbMysql");
  32. }
  33. /**
  34. * Returns an array of table names that exist in the database
  35. *
  36. * @return array Array of table names
  37. */
  38. public function getTableNames()
  39. {
  40. trigger_error("getTableNames is not yet implemented in DbMysql");
  41. }
  42. /**
  43. * Returns field information about the specified table
  44. *
  45. * @param string $tableName Name of the table to return information about
  46. */
  47. public function getTableFieldInfo($tableName)
  48. {
  49. trigger_error("getTableFieldInfo is not yet implemented in DbMysql");
  50. }
  51. function escapeString($string)
  52. {
  53. self::connect();
  54. return "'" . mysql_real_escape_string($string, $this->connection) . "'";
  55. }
  56. function _query($sql)
  57. {
  58. self::connect();
  59. $result = mysql_query($sql, $this->connection)
  60. or trigger_error(mysql_error());
  61. if(gettype($result) == 'boolean')
  62. return $result;
  63. return new DbMysqlResult($this->connection, $result);
  64. }
  65. function getLastInsertId()
  66. {
  67. self::connect();
  68. return mysql_insert_id($this->connection);
  69. }
  70. private function connect()
  71. {
  72. // lazy connection to the database
  73. if(!$this->connection)
  74. {
  75. $this->connection = mysql_connect($this->params['host'], $this->params['username'], $this->params['password'])
  76. or trigger_error(mysql_error());
  77. mysql_select_db($this->params['database'], $this->connection)
  78. or trigger_error(mysql_error());
  79. }
  80. }
  81. }