File: 0.00.1a/core/database/MySQL.php (View as HTML)

  1: <?php 
  2: /* -------------------------------------------------------------
  3: This file is part of FreeDESK
  4: 
  5: FreeDESK is (C) Copyright 2012 David Cutting
  6: 
  7: FreeDESK is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeDESK is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeDESK.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freedesk/
 21: -------------------------------------------------------------- */
 22: 
 23: /**
 24:  * MySQL concrete implementation of DatabaseBase
 25: **/
 26: 
 27: class MySQL extends DatabaseBase
 28: {
 29: 	/**
 30: 	 * Pointer to the FreeDESK instance
 31: 	**/
 32: 	private $DESK = null;
 33: 	/**
 34: 	 * MySQL data connection
 35: 	**/
 36: 	private $connection = null;
 37: 
 38: 	/**
 39: 	 * Table prefix
 40: 	**/
 41: 	var $prefix = "";
 42: 
 43: 	/**
 44: 	 * Constructor
 45: 	 * @param object $freeDESK FreeDESK instance
 46: 	**/
 47: 	function MySQL(&$freeDESK)
 48: 	{
 49: 		$this->DESK = &$freeDESK;
 50: 		
 51: 		$this->DESK->PluginManager->Register(new Plugin(
 52: 			"MySQL Database Engine","0.01","Core","DB" ));
 53: 	}
 54: 	
 55: 	/**
 56: 	 * Connect
 57: 	 * @param string $server Database server
 58: 	 * @param string $username Database username
 59: 	 * @param string $password Database password
 60: 	 * @param string $database Database name
 61: 	 * @param string $prefix Database table prefix (optonal, default "")
 62: 	 * @return bool Successful connection or not
 63: 	**/
 64: 	function Connect($server, $username, $password, 
 65: 		$database, $prefix="")
 66: 	{
 67: 		$this->prefix = $prefix;
 68: 	
 69: 		$this->connection = mysql_connect($server, $username, $password);
 70: 		if ($this->connection <= 0) return false;
 71: 		
 72: 		if (!mysql_select_db($database, $this->connection))
 73: 			return false;
 74: 			
 75: 		return true;
 76: 	}
 77: 	
 78: 	/**
 79: 	 * Disconnect
 80: 	**/
 81: 	function Disconnect()
 82: 	{
 83: 		mysql_close($this->connection);
 84: 	}
 85: 	
 86: 	/**
 87: 	 * Return table name with correct prefix and escaping
 88: 	 * @param string $table table un-prefixed
 89: 	 * @return string table with prefix and escape
 90: 	**/
 91: 	function Table($table)
 92: 	{
 93: 		return "`".$this->prefix.$table."`";
 94: 	}
 95: 	
 96: 	/**
 97: 	 * Sanitise user-input using correct escaping
 98: 	 * @param string $input user input
 99: 	 * @return string Sanitised output
100: 	**/
101: 	function Safe($input)
102: 	{
103: 		return mysql_real_escape_string($input, $this->connection);
104: 	}
105: 	
106: 	
107: 	/**
108: 	 * Sanitise user-input string and quote
109: 	 * @param string $input user input
110: 	 * @return string Sanitised quoted output
111: 	**/
112: 	function SafeQuote($input)
113: 	{
114: 		return "\"".$this->Safe($input)."\"";
115: 	}
116: 	
117: 	
118: 	/**
119: 	 * Contain a field correctly
120: 	 * @param string $field The field name
121: 	 * @return string Escaped field
122: 	**/
123: 	function Field($field)
124: 	{
125: 		return "`".$field."`";
126: 	}
127: 	
128: 	/**
129: 	 * Escape and contain a field correctly
130: 	 * @param string $value The value of the field
131: 	 * @return string Escaped and prefixed+suffixed data
132: 	**/
133: 	function FieldSafe($value)
134: 	{
135: 		return $this->Field($this->Safe($value));
136: 	}
137: 	
138: 	/**
139: 	 * Perform a query
140: 	 * @param string $query SQL query
141: 	 * @param bool $report Record any errors using LoggingEngine (optonal, default true)
142: 	 * @return mixed Results of query
143: 	**/
144: 	function Query($query, $report=true)
145: 	{
146: 		$result=mysql_query($query, $this->connection);
147: 		
148: 		if ($report && $this->Error()) // has an error and to be reported
149: 		{
150: 			$err="Query Failed: ".$query;
151: 			$error="SQL Error: ".$this->LastError();
152: 			$this->DESK->LoggingEngine->Log($err, "SQL", "Fail", 1);
153: 			$this->DESK->LoggingEngine->Log($error, "SQL", "Error", 1);
154: 		}
155: 		
156: 		return $result;
157: 	}
158: 	
159: 	/**
160: 	 * Number of rows affected by last query
161: 	 * @return int number of rows affected
162: 	**/
163: 	function RowsAffected()
164: 	{
165: 		return mysql_affected_rows($this->connection);
166: 	}
167: 	
168: 	/**
169: 	 * Number of rows in a result set
170: 	 * @param mixed $result Result set
171: 	 * @return int number of rows in the set
172: 	**/
173: 	function NumRows(&$result)
174: 	{
175: 		return mysql_num_rows($result);
176: 	}
177: 	
178: 	/**
179: 	 * Fetch next associated array from result set
180: 	 * @param mixed $result Result Set
181: 	 * @return array Assocative Array of Results
182: 	**/
183: 	function FetchAssoc(&$result)
184: 	{
185: 		return mysql_fetch_assoc($result);
186: 	}
187: 	
188: 	/**
189: 	 * Free a result set
190: 	 * @param mixed $result Result Set
191: 	**/
192: 	function Free(&$result)
193: 	{
194: 		mysql_free_result($result);
195: 	}
196: 	
197: 	/**
198: 	 * Return an error flag
199: 	 * @return bool Experienced error on last command
200: 	**/
201: 	function Error()
202: 	{
203: 		if (mysql_errno($this->connection)>0)
204: 			return true;
205: 		return false;
206: 	}
207: 	
208: 	/**
209: 	 * Last error code
210: 	 * @return int Error code
211: 	**/
212: 	function ErrorCode()
213: 	{
214: 		return mysql_errno($this->connection);
215: 	}
216: 	
217: 	/**
218: 	 * Last error description
219: 	 * @return string Error description
220: 	**/
221: 	function ErrorDescription()
222: 	{
223: 		return mysql_error($this->connection);
224: 	}
225: 	
226: 	/**
227: 	 * The last inserted ID
228: 	 * @return mixed Last inserted ID
229: 	**/
230: 	function InsertID()
231: 	{
232: 		return mysql_insert_id($this->connection);
233: 	}
234: 	
235: 	/**
236: 	 * Generate a clause from a QueryBuilder object
237: 	 * @param object &$query QueryBuilder object
238: 	 * @return string query string
239: 	**/
240: 	function Clause(&$query)
241: 	{
242: 		$c = "";
243: 		foreach($query->items as $item)
244: 		{
245: 			if (isset($item['field']))
246: 			{
247: 				if ($c!="")
248: 					$c.=" ";
249: 				$c.=$this->Field($item['field']);
250: 				
251: 				switch($item['type'])
252: 				{
253: 					case QueryType::Equal:
254: 						$c.="=";
255: 						break;
256: 					case QueryType::Like:
257: 						$c.=" LIKE ";
258: 						break;
259: 					case QueryType::MoreThan:
260: 						$c.=" > ";
261: 						break;
262: 					case QueryType::MoreThanEqual:
263: 						$c.=" >= ";
264: 						break;
265: 					case QueryType::LessThan:
266: 						$c.=" < ";
267: 						break;
268: 					case QueryType::LessThanEqual:
269: 						$c.=" <= ";
270: 						break;
271: 					case QueryType::NotEqual;
272: 						$c.=" != ";
273: 						break;
274: 				}
275: 				
276: 				$c.=$item['value'];
277: 			}
278: 			else
279: 			{
280: 				switch($item['type'])
281: 				{
282: 					case QueryType::OpenBracket:
283: 						$c.=" ( ";
284: 						break;
285: 					case QueryType::CloseBracket:
286: 						$c.=" ) ";
287: 						break;
288: 					case QueryType::opAND:
289: 						$c.=" AND ";
290: 						break;
291: 					case QueryType::opOR:
292: 						$c.=" OR ";
293: 						break;
294: 				}
295: 			}
296: 		}
297: 		
298: 		if ($c=="")
299: 			$c="1";
300: 		
301: 		return $c;
302: 	}
303: }
304: 
305: ?>
306: