File: 0.00.1a/core/database/DatabaseBase.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:  * Database Query Type
 24: **/
 25: abstract class QueryType
 26: {
 27: 	const Equal = 0;
 28: 	const Like = 1;
 29: 	const MoreThan = 2;
 30: 	const MoreThanEqual = 3;
 31: 	const LessThan = 4;
 32: 	const LessThanEqual = 5;
 33: 	const NotEqual = 6;
 34: 	
 35: 	const OpenBracket = 100;
 36: 	const CloseBracket = 101;
 37: 	
 38: 	const opAND = 200;
 39: 	const opOR = 201;
 40: }
 41: 
 42: /**
 43:  * Query Builder Class
 44: **/
 45: class QueryBuilder
 46: {
 47: 	/**
 48: 	 * Query items array
 49: 	**/
 50: 	var $items = array();
 51: 	
 52: 	/**
 53: 	 * Limit Flag
 54: 	**/
 55: 	var $limit = false;
 56: 	
 57: 	/**
 58: 	 * Start (for limit)
 59: 	**/
 60: 	var $start = 0;
 61: 	
 62: 	/**
 63: 	 * Entries (for limit)
 64: 	**/
 65: 	var $entries = 30;
 66: 	
 67: 	/**
 68: 	 * Order Flag
 69: 	**/
 70: 	var $order = false;
 71: 	
 72: 	/**
 73: 	 * Order fields
 74: 	**/
 75: 	var $orderlist = array();
 76: 	
 77: 	/**
 78: 	 * Add item
 79: 	 * @param string $field Field
 80: 	 * @param mixed $type QueryType const
 81: 	 * @param mixed $value Value
 82: 	**/
 83: 	function Add($field, $type, $value)
 84: 	{
 85: 		$this->items[] = array(
 86: 			"field" => $field,
 87: 			"type" => $type,
 88: 			"value" => $value );
 89: 	}
 90: 	
 91: 	/**
 92: 	 * Add an order field
 93: 	 * @param string $field Field
 94: 	 * @param bool $asc Ascending (optional, default true) - false is descending
 95: 	**/
 96: 	function AddOrder($field, $asc = true)
 97: 	{
 98: 		if (!$this->order)
 99: 			$this->order = true;
100: 		$this->orderlist[$field]=$asc;
101: 	}
102: 	
103: 	/**
104: 	 * Open bracket
105: 	**/
106: 	function OpenBracket()
107: 	{
108: 		$this->items[] = array("type" => QueryType::OpenBracket);
109: 	}
110: 	
111: 	/**
112: 	 * Close bracket
113: 	**/
114: 	function CloseBracket()
115: 	{
116: 		$this->items[] = array("type" => QueryType::CloseBracket);
117: 	}
118: 	
119: 	/**
120: 	 * Add operation
121: 	 * @param mixed $operation Op of type QueryType
122: 	**/
123: 	function AddOperation($operation)
124: 	{
125: 		$this->items[] = array("type" => $operation);
126: 	}
127: }
128: 
129: 
130: /**
131:  * DatabaseBase is the abstract base class for database system implementations
132: **/
133: 
134: abstract class DatabaseBase
135: {
136: 	/**
137: 	 * Constructor
138: 	 * @param object $freeDESK FreeDESK instance
139: 	**/
140: 	//abstract function DatabaseBase(&$freekDESK);
141: 	
142: 	/**
143: 	 * Connect
144: 	 * @param string $server Database server
145: 	 * @param string $username Database username
146: 	 * @param string $password Database password
147: 	 * @param string $database Database name
148: 	 * @param string $prefix Database table prefix (optonal, default "")
149: 	 * @return bool Successful connection or not
150: 	**/
151: 	abstract function Connect($server, $username, $password, 
152: 		$database, $prefix="");
153: 	
154: 	/**
155: 	 * Disconnect
156: 	**/
157: 	abstract function Disconnect();
158: 	
159: 	/**
160: 	 * Return table name with correct prefix and escaping
161: 	 * @param string $table table un-prefixed
162: 	 * @return string table with prefix and escape
163: 	**/
164: 	abstract function Table($table);
165: 	
166: 	/**
167: 	 * Sanitise user-input using correct escaping
168: 	 * @param string $input user input
169: 	 * @return string Sanitised output
170: 	**/
171: 	abstract function Safe($input);
172: 	
173: 	/**
174: 	 * Sanitise user-input string and quote
175: 	 * @param string $input user input
176: 	 * @return string Sanitised quoted output
177: 	**/
178: 	abstract function SafeQuote($input);
179: 	
180: 	
181: 	
182: 	/**
183: 	 * Contain a field correctly
184: 	 * @param string $field The field name
185: 	 * @return string Escaped field
186: 	**/
187: 	abstract function Field($field);
188: 	
189: 	/**
190: 	 * Escape and contain a field correctly
191: 	 * @param string $value The value of the field
192: 	 * @return string Escaped and prefixed+suffixed data
193: 	**/
194: 	function FieldSafe($value)
195: 	{
196: 		return $this->Field($this->Safe($value));
197: 	}
198: 	
199: 	/**
200: 	 * Perform a query
201: 	 * @param string $query SQL query
202: 	 * @param bool $report Record any errors using LoggingEngine (optonal, default true)
203: 	 * @return mixed Results of query
204: 	**/
205: 	abstract function Query($query, $report=true);
206: 	
207: 	/**
208: 	 * Number of rows affected by last query
209: 	 * @return int number of rows affected
210: 	**/
211: 	abstract function RowsAffected();
212: 	
213: 	/**
214: 	 * Number of rows in a result set
215: 	 * @param mixed $result Result set
216: 	 * @return int number of rows in the set
217: 	**/
218: 	abstract function NumRows(&$result);
219: 	
220: 	/**
221: 	 * Fetch next associated array from result set
222: 	 * @param mixed $result Result Set
223: 	 * @return array Assocative Array of Results
224: 	**/
225: 	abstract function FetchAssoc(&$result);
226: 	
227: 	/**
228: 	 * Free a result set
229: 	 * @param mixed $result Result Set
230: 	**/
231: 	abstract function Free(&$result);
232: 	
233: 	/**
234: 	 * Return an error flag
235: 	 * @return bool Experienced error on last command
236: 	**/
237: 	abstract function Error();
238: 	
239: 	/**
240: 	 * Last error code
241: 	 * @return int Error code
242: 	**/
243: 	abstract function ErrorCode();
244: 	
245: 	/**
246: 	 * Last error description
247: 	 * @return string Error description
248: 	**/
249: 	abstract function ErrorDescription();
250: 	
251: 	/**
252: 	 * Details of the last error
253: 	 * @return string Code and error description
254: 	**/
255: 	function LastError()
256: 	{
257: 		return $this->ErrorCode().": ".$this->ErrorDescription();
258: 	}
259: 	
260: 	/**
261: 	 * The last inserted ID
262: 	 * @return mixed Last inserted ID
263: 	**/
264: 	abstract function InsertID();
265: 	
266: 	/**
267: 	 * Generate a clause from a QueryBuilder object
268: 	 * @param object &$query QueryBuilder object
269: 	 * @return string query string
270: 	**/
271: 	abstract function Clause(&$query);
272: 	
273: }
274: 
275: ?>
276: