PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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