OssnDatabase.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Open Source Social Network
5  *
6  * @package (Informatikon.com).ossn
7  * @author OSSN Core Team <[email protected]>
8  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
9  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
10  * @link http://www.opensource-socialnetwork.org/licence
11  */
12 class OssnDatabase extends OssnBase {
13  /**
14  * Connect to mysql database
15  *
16  * @return boolean
17  */
18  public function Connect() {
20  $connect = new mysqli($settings->host, $settings->user, $settings->password, $settings->database);
21  if(!$connect->connect_errno) {
22  return $connect;
23  } else {
24  return false;
25  }
26  }
27  /**
28  * Prepare a query to insert data in database
29  *
30  * @param array array();
31  * 'names' Names of columns
32  * 'values' Values that need to be inserted
33  * 'into' Table name
34  *
35  * @return boolean
36  */
37  public function insert($params) {
38  if(is_array($params)) {
39  if(count($params['names']) == count($params['values'])) {
40  $colums = "`" . implode("`, `", $params['names']) . '`';
41  $values = "'" . implode("', '", $params['values']) . "'";
42  $query = "INSERT INTO {$params['into']} ($colums) VALUES ($values);";
43  $this->statement($query);
44  if($this->execute()) {
45  return true;
46  }
47  }
48  }
49  return false;
50  }
51 
52  /**
53  * Prepare a mysqli query
54  *
55  * @return boolean
56  */
57  public function statement($query) {
58  if(!empty($query)) {
59  $this->query = $query;
60  return true;
61  }
62  return false;
63  }
64 
65  /**
66  * Execute a mysqli query and store result in memory
67  *
68  * @return boolean
69  */
70  public function execute() {
71  $this->database = $this->Connect();
72  if(isset($this->query) && !empty($this->query)) {
73  $this->database->set_charset("utf8");
74  $this->exe = $this->database->query($this->query);
75  $exception = ossn_call_hook('database', 'execution:message', false, true);
76  if(!$this->exe && $exception) {
77  throw new OssnDatabaseException("{$this->database->error} \n {$this->query} ");
78  }
79  if(isset($this->database->insert_id)) {
80  $this->last_id = $this->database->insert_id;
81  }
82  unset($this->query);
83  $this->database->close();
84  return true;
85  }
86  return false;
87  }
88 
89  /**
90  * Prepare a query to update data in database
91  *
92  * @param array array();
93  * 'names' Names of columns
94  * 'values' Values that need to be updated
95  * 'table' Table name
96  * 'wheres' Specify a selection criteria to update required records
97  *
98  * @return boolean
99  */
100  public function update($params = array()) {
101  if(is_array($params)) {
102  if(count($params['names']) == count($params['values']) && !empty($params['table'])) {
103  $valuec = count($params['names']);
104  $i = 1;
105  foreach($params['names'] as $key => $val) {
106  $data[$val] = $params['values'][$key];
107  }
108  foreach($data as $keys => $vals) {
109  if($i == $valuec) {
110  $valyes[] = "`{$keys}` = '{$vals}'";
111  } else {
112  $valyes[] = "`{$keys}` = '{$vals}',";
113  }
114  $i++;
115  }
116  $q = implode('', $valyes);
117  $params['wheres'] = implode(' ', $params['wheres']);
118  $query = "UPDATE {$params['table']} SET {$q} WHERE {$params['wheres']}";
119  $this->statement($query);
120  if($this->execute()) {
121  return true;
122  }
123 
124  }
125  }
126  return false;
127  }
128 
129  /**
130  * Prepare a query to select data from database
131  *
132  * @param array array();
133  * 'from' Names of table
134  * 'params' Names of columns which you want to select
135  * 'wheres' Specify a selection criteria to get required records
136  *
137  * @return boolean
138  */
139  public function select($params, $multi = '') {
140  if(is_array($params)) {
141  if(!isset($params['params'])) {
142  $parameters = '*';
143  } else {
144  $parameters = implode(', ', $params['params']);
145  }
146  $order_by = '';
147  if(!empty($params['order_by'])) {
148  $order_by = "ORDER by {$params['order_by']}";
149  }
150  $group_by = '';
151  if(!empty($params['group_by'])) {
152  $group_by = "GROUP by {$params['group_by']}";
153  }
154  $where = '';
155  if(isset($params['wheres']) && is_array($params['wheres'])) {
156  $where = implode(' ', $params['wheres']);
157  }
158  $wheres = '';
159  if(!empty($params['wheres'])) {
160  $wheres = "WHERE({$where})";
161  }
162  $limit = '';
163  if(!empty($params['limit'])) {
164  $limit = "LIMIT {$params['limit']}";
165  }
166  $joins = '';
167  if(!empty($params['joins']) && !is_array($params['joins'])) {
168  $joins = $params['joins'];
169  } elseif(!empty($params['joins']) && is_array($params['joins'])) {
170  $joins = implode(' ', $params['joins']);
171  }
172  $query = "SELECT {$parameters} FROM {$params['from']} {$joins} {$wheres} {$order_by} {$group_by} {$limit};";
173 
174  $this->statement($query);
175  if($this->execute()) {
176  return $this->fetch($multi);
177  }
178  }
179  return false;
180  }
181 
182  /**
183  * Fetch the data from memory that is stored during execution;
184  *
185  * @param boolean $data Ture if you want to fetch all data , or false if only one row
186  *
187  * @return boolean
188  */
189  public function fetch($data = false) {
190  if(isset($this->exe)) {
191  if($data !== true) {
192  if($fetch = $this->exe) {
193  return arrayObject($fetch->fetch_assoc());
194  }
195  }
196  if($data === true) {
197  if($fetch = $this->exe) {
198  while($all = $fetch->fetch_assoc()) {
199  $alldata[] = arrayObject($all);
200  }
201  }
202  if(isset($alldata) && !empty($alldata)) {
203  return arrayObject($alldata);
204  }
205  }
206  }
207  return false;
208  }
209 
210  /**
211  * Prepare a query to delete data from database
212  *
213  * @param array array();
214  * 'from' Names of table
215  * 'wheres' Specify a selection criteria to get required records
216  *
217  * @return boolean
218  */
219  public function delete($params) {
220  if(is_array($params)) {
221  $where = implode(' ', $params['wheres']);
222  if(!empty($params['wheres'])) {
223  $wheres = "WHERE({$where})";
224  }
225  //don't let any component or query to empty entire table
226  if(empty($params['wheres'])) {
227  return false;
228  }
229  $query = "DELETE FROM `{$params['from']}` {$wheres};";
230  $this->statement($query);
231  if($this->execute()) {
232  return true;
233  }
234  }
235  return false;
236  }
237 
238  /**
239  * Get a guid of newly create entry
240  *
241  * @return integer
242  */
243  public function getLastEntry() {
244  if(!empty($this->last_id)) {
245  return $this->last_id;
246  }
247  }
248  /**
249  * Create a wheres clause for database
250  *
251  * @param array $array A valid array containg wheres clauses;
252  * @param string $operator AND, OR, LIKE
253  *
254  * @return string
255  */
256  public function constructWheres(array $array, $operator = "AND") {
257  if(!empty($array) && !empty($operator)) {
258  $result = implode(" {$operator} ", $array);
259  return $result;
260  }
261  return false;
262  }
263  /**
264  * Generate limit from options
265  *
266  * @param integer $data_limit How much data should be fetched?
267  * @param integer $page_limit Limit of data on one page
268  * @param integer $offset Offset value
269  *
270  * @return string|false
271  */
272  public function generateLimit($data_limit = false, $page_limit = false, $offset = false) {
273  $limit = $data_limit;
274  //get only required result, don't bust your server memory
275  if(isset($offset) && $offset !== false && $page_limit !== false) {
276  $limitfrom = ($offset - 1) * ($page_limit);
277  $limitto = $page_limit;
278 
279  $data_limit = "{$limitfrom}, {$limitto}";
280  if($offset > 1) {
281  if($limit > $limitfrom) {
282  $limitto = $limit - $limitfrom;
283  if($limitto <= $page_limit) {
284  $data_limit = "{$limitfrom}, {$limitto}";
285  }
286  }
287  }
288  if(!empty($limit) && $limit < $page_limit) {
289  $data_limit = $limit;
290  }
291  return $data_limit;
292  }
293  return false;
294  }
295 
296 } //class