sew /class/sqlManager.Class.php

Language PHP Lines 210
MD5 Hash 2f9f76a3eacf504278efb8655ed823d4
Repository https://github.com/myszor/sew.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/configs/config.php';
require_once("Exceptions_handler_lib.php");

/**
 * sql manager class
 * @author jacek(at)jacoor.net
 *
 */
final class sqlManager extends mysqli { //don't suppose that i have to extend this
	private static $instance;
	
	public static function GetInstance()
	{
		if ( !isset(self::$instance))
		{
			self::$instance = new sqlManager();
		}
		return self::$instance;
	}

	/**
	 * private constructor
	 *
	 */
private function __construct(){		
	//this is singletone :-)
	self::I_hate_fucking_magic_quotes(); //clear magic quotes	
	try{
		@parent::__construct(config::hostname(),config::username(),config::pass(), config::dbname(), config::port());
		
		if (mysqli_connect_errno()!==0) 
			throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
		
		$result=@$this->query("SET NAMES 'utf8'");
		$result=@$this->set_charset("utf8");
		if ($result===FALSE) 
			{
			$error = $this->error;
			$errcode = $this->errno;
			$this->close();	
			throw new Exception($error, $errcode);
			}			
		}
		catch (Exception $dberror)
		{
			throw new MySQLIConnectException($dberror->getMessage(),$dberror->getCode());
		}
	}

	/**
	 * fetch rows with desired criteria
	 * @param $tableName
	 * @param $search associative array with data to search (key => val)
	 * @param $get indexed array of columns name to get , if null all are fetched
	 * @param $order order of records to return (withoud 'order by' part)
	 * @return associative of fetched row(s)
	 * @todo implement limit
	 */
	public function getRows ($tableName, $search = null, $get = null, $order = null) {
		if ($tableName===null)
			trigger_error("Niepoprawna wartość tableName", E_USER_ERROR);
		if (is_array($search)){		
			$where = "WHERE ";
	
			foreach ($search as $key => $val){
				$where = $where . addslashes($key) .' = \''.addslashes($val).'\' AND ';  
			}
		}
		if ($get === null) 
			$get = ' * ';
		else{
			foreach ($get as $key => &$val)
				$get[$key] = addslashes($val);
			$get  = implode(array_values($get),',');
		} 
		
		if ($order != null ) $order = "ORDER BY ".addslashes($order);
		
		$where = substr($where,0,-5);//remove last char
		$query  = "
							SELECT $get FROM `$tableName`
							$where 
							$order
						;";
		$res = $this->query($query);
		if (!$this->error){
			while ($row = $res->fetch_assoc())
				$result[] = $row;
		}	
	else trigger_error("Bład zapytania mysqli" .$this->error , E_USER_ERROR); 
	return ($result);
	}
	 
	/**
	 * inserts single row to table
	 * @param $tableName
	 * @param $fields array of fields to be inserted $key => $val
	 * @return boolean 
	 * @exception throws some exception on mysqli error
	 */
	public function insertRow ($tableName, array $fields){
		if ($tableName === null) trigger_error("Niepoprawna wartość tableName", E_USER_ERROR);
		if (!is_array($fields)) trigger_error("Niepoprawna wartość fields", E_USER_ERROR);
			$cols = implode(',', array_keys($fields));
		foreach ($fields as $key => &$val){
			$fields[$key] = addslashes($val);
		}
		$vals = implode($fields,'\',\'');
		$vals = '\''.$vals.'\'';
		$query  = "
							INSERT INTO `$tableName`
							($cols) 
							VALUES($vals);";
		
		$this->query($query);
		if ($this->error)
			trigger_error("Bład zapytania mysqli" .$this->error , E_USER_ERROR);
	return $this->insert_id;
	}
	
	/**
	 * fetch rows with desired criteria
	 * @param $tableName
	 * @param $update associative array with data to update (key => val)
	 * @param $search associative array with data to search (key => val)
	 * @return mysqli_affected_rows
	 */
	public function updateRow ($tableName, array $update, array $search) {
		if ($tableName===null) trigger_error("Niepoprawna wartość tableName", E_USER_ERROR);
		if (!is_array($search)) trigger_error("Niepoprawna wartość search", E_USER_ERROR);
		if (!is_array($update)) trigger_error("Niepoprawna wartość update", E_USER_ERROR);	
			
		$set = "SET ";	
		foreach ($update as $key => $val)
			$set = $set . addslashes($key) ." = '".addslashes($val)."', ";
		
		$set = substr($set,0,-2);//remove last char
		
		$where = "WHERE ";	
		foreach ($search as $key => $val)
			$where = $where . addslashes($key) ." = '".addslashes($val)."' AND ";  
	
		$where = substr($where,0,-5);//remove last char  
	
		$query  = "
							UPDATE `$tableName` 
							$set 
							$where
						;";	

		$this->query($query);
	
		if ($this->error)
			trigger_error("Bład zapytania mysqli" .$this->error , E_USER_ERROR);
		return $this->affected_rows;
	}	

	/**
	 * deletes rows with desired criteria. Does not allow to delete all.
	 * @param $tableName
	 * @param $search associative array with data to search (key => val) 
	 * @return myqli_affected_rows
	 * @exception MysqliQueryException
	 */
	public function deleteRows ($tableName, $search) {
		if ($tableName===null) trigger_error("Niepoprawna wartość tableName", E_USER_ERROR);
		if (!is_array($search)) trigger_error("Niepoprawna wartość search", E_USER_ERROR);
		$where = "WHERE ";
	
		foreach ($search as $key => $val){
			$where = $where . addslashes($key) .' = '.addslashes($val).' AND ';  
		}
	
		$where = substr($where,0,-5);//remove last char
		$query  = "
							DELETE FROM `$tableName`
							$where
						;";	
			$this->query($query);
		if ($this->error)
			trigger_error("Bład zapytania mysqli" .$this->error , E_USER_ERROR);
	return $this->affected_rows;
	}

	/**
	 * I really hate magic_quotes_gpc - this stuff reverts changes of magic quotes
	 * @return unknown_type
	 */
	private static function I_hate_fucking_magic_quotes(){
		if (ini_get ('magic_quotes_sybase') != false)
		 die('Yo! I will not work untill you disable magic_quotes_sybase in your php.ini');
		if (get_magic_quotes_gpc()){
			array_walk_recursive(&$_REQUEST, 'sqlManager::clearslashes');
			array_walk_recursive(&$_POST, 'sqlManager::clearslashes');
			array_walk_recursive(&$_GET, 'sqlManager::clearslashes');
			array_walk_recursive(&$_COOKIE, 'sqlManager::clearslashes');
		}
	} 
	/**
	* clears slashes from magic quotes
	* @param $input
	* @return modified $input
	*/
	private static function clearslashes(&$input){
   	$input = stripslashes($input);
  	return true;
 	}
} 
?>
Back to Top