synestergates788 /
OctaORM
| 1 | <?php |
||
| 2 | class OctaORM{ |
||
| 3 | |||
| 4 | protected $redbean; |
||
| 5 | protected $db_field; |
||
| 6 | |||
| 7 | var $last_id; |
||
| 8 | var $last_query; |
||
| 9 | |||
| 10 | var $get; |
||
| 11 | var $select; |
||
| 12 | var $where; |
||
| 13 | var $or_where; |
||
| 14 | var $order_by; |
||
| 15 | var $join; |
||
| 16 | var $group_by; |
||
| 17 | var $table; |
||
| 18 | var $limit; |
||
| 19 | var $offset; |
||
| 20 | var $where_in; |
||
| 21 | var $or_where_in; |
||
| 22 | var $where_not_in; |
||
| 23 | var $or_where_not_in; |
||
| 24 | var $like; |
||
| 25 | var $or_like; |
||
| 26 | var $not_like; |
||
| 27 | var $or_not_like; |
||
| 28 | var $row; |
||
| 29 | var $num_rows; |
||
| 30 | var $result; |
||
| 31 | |||
| 32 | public function __construct($db){ |
||
| 33 | include_once('RedBean.php'); |
||
| 34 | $this->redbean = new R(); |
||
| 35 | $this->db_field = $db; |
||
| 36 | |||
| 37 | if(!$this->redbean->testConnection()){ |
||
| 38 | $conn = $this->initiate_database_connection($db); |
||
| 39 | $this->redbean->setup($conn); |
||
| 40 | $this->redbean->useFeatureSet( 'novice/latest' ); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | public function initiate_database_connection($db){ |
||
| 45 | $DB_HOST = $db['hostname']; |
||
| 46 | $DB_USERNAME = $db['username']; |
||
| 47 | $DB_PASSWORD = $db['password']; |
||
| 48 | $DB_NAME = $db['database']; |
||
| 49 | |||
| 50 | $DB_con = new PDO("mysql:host=$DB_HOST", $DB_USERNAME, $DB_PASSWORD); |
||
| 51 | $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 52 | |||
| 53 | $DB_con->exec("CREATE DATABASE IF NOT EXISTS $DB_NAME;"); |
||
| 54 | |||
| 55 | $DB_con = new PDO("mysql:host={$DB_HOST};dbname={$DB_NAME}",$DB_USERNAME,$DB_PASSWORD); |
||
| 56 | $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 57 | return $DB_con; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function insert_id(){ |
||
| 61 | return $this->last_id; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function last_query(){ |
||
| 65 | return $this->last_query; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function insert($array,$table){ |
||
| 69 | $data = $this->redbean->dispense($table); |
||
| 70 | |||
| 71 | if($array){ |
||
| 72 | foreach($array as $key=>$row){ |
||
| 73 | $data->$key = $row; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->last_id = $this->redbean->store($data); |
||
| 78 | return ($this->last_id) ? true : false; |
||
| 79 | } |
||
| 80 | |||
| 81 | public function insert_batch($array,$table){ |
||
| 82 | $data = array(); |
||
| 83 | |||
| 84 | if($array){ |
||
| 85 | foreach($array as $key_ib=>$row_ib){ |
||
| 86 | |||
| 87 | $data[$key_ib]=$this->redbean->dispense($table); |
||
| 88 | $table_fields = []; |
||
| 89 | $table_fields_val = []; |
||
| 90 | |||
| 91 | if($row_ib){ |
||
| 92 | foreach($row_ib as $row=>$val){ |
||
| 93 | $table_fields[] = $row; |
||
| 94 | $table_fields_val[] = $val; |
||
| 95 | } |
||
| 96 | |||
| 97 | if($table_fields && $table_fields_val){ |
||
| 98 | foreach($table_fields as $key=>$row){ |
||
| 99 | $data[$key_ib]->$row = $table_fields_val[$key]; |
||
| 100 | } |
||
| 101 | |||
| 102 | }else{ |
||
| 103 | return false; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | $result = $this->redbean->storeAll($data); |
||
| 110 | return ($result) ? true : false; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function update($table,$array,$id){ |
||
| 114 | $data = $this->redbean->load($table, $id); |
||
| 115 | |||
| 116 | if($array){ |
||
| 117 | foreach($array as $key=>$row){ |
||
| 118 | $data->$key = $row; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | $result = $this->redbean->store($data); |
||
| 123 | return ($result) ? true : false; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function update_batch($table,$array,$where){ |
||
| 127 | if($array){ |
||
| 128 | foreach($array as $key_ub=>$row_ub){ |
||
| 129 | $where_key = ''; |
||
| 130 | $table_fields = []; |
||
| 131 | $table_fields_val = []; |
||
| 132 | |||
| 133 | if($row_ub){ |
||
| 134 | foreach($row_ub as $row=>$val){ |
||
| 135 | |||
| 136 | if($row === $where){ |
||
| 137 | $where_key = $val; |
||
| 138 | } |
||
| 139 | |||
| 140 | if($row !== $where){ |
||
| 141 | $table_fields[] = $row; |
||
| 142 | $table_fields_val[] = $val; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | $data = $this->redbean->load($table, $where_key); |
||
| 147 | if($table_fields && $table_fields_val){ |
||
| 148 | foreach($table_fields as $key=>$row){ |
||
| 149 | $data->$row = $table_fields_val[$key]; |
||
| 150 | } |
||
| 151 | |||
| 152 | }else{ |
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->redbean->store($data); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | return true; |
||
| 162 | } |
||
| 163 | |||
| 164 | public function delete($table,$id){ |
||
| 165 | if(is_array($id)){ |
||
| 166 | if($id){ |
||
| 167 | foreach($id as $key=>$row){ |
||
| 168 | $this->redbean->trash($table,$row); |
||
| 169 | } |
||
| 170 | |||
| 171 | return true; |
||
| 172 | } |
||
| 173 | |||
| 174 | }else{ |
||
| 175 | $this->redbean->trash($table,$id); |
||
| 176 | return true; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | public function delete_all($table){ |
||
| 181 | $result = $this->redbean->wipe($table); |
||
| 182 | return ($result) ? true : false; |
||
| 183 | } |
||
| 184 | |||
| 185 | public function select($data){ |
||
| 186 | $reset = []; |
||
| 187 | if($data){ |
||
| 188 | foreach($data as $key=>$row){ |
||
| 189 | array_push($reset,$row); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | $this->select = $reset; |
||
| 194 | return $this->select; |
||
| 195 | } |
||
| 196 | |||
| 197 | public function where($data=null,$match=null){ |
||
| 198 | $tmp_where = ''; |
||
| 199 | $arr_check = false; |
||
| 200 | if($data){ |
||
| 201 | if(is_array($data)){ |
||
| 202 | end($data); |
||
| 203 | $last_element = key($data); |
||
| 204 | |||
| 205 | if($data){ |
||
| 206 | $arr_check = true; |
||
| 207 | foreach($data as $key=>$row){ |
||
| 208 | if($key == $last_element){ |
||
| 209 | $tmp_where .= $key."='".$row."'"; |
||
| 210 | }else{ |
||
| 211 | $tmp_where .= $key."='".$row."' AND "; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | }else{ |
||
| 216 | $arr_check = false; |
||
| 217 | $tmp_where = "WHERE ".$data."='".$match."'"; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | $this->where = ($arr_check) ? "WHERE ".$tmp_where : $tmp_where; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function or_where($data=null,$match=null){ |
||
| 225 | $tmp_or_where = ''; |
||
| 226 | $arr_check = false; |
||
| 227 | if($data){ |
||
| 228 | if(is_array($data)){ |
||
| 229 | end($data); |
||
| 230 | $last_element = key($data); |
||
| 231 | $arr_check = true; |
||
| 232 | |||
| 233 | if($data){ |
||
| 234 | foreach($data as $key=>$row){ |
||
| 235 | if($key == $last_element){ |
||
| 236 | $tmp_or_where .= $key."='".$row."'"; |
||
| 237 | }else{ |
||
| 238 | $tmp_or_where .= $key."='".$row."' AND "; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | }else{ |
||
| 243 | $arr_check = false; |
||
| 244 | $tmp_or_where = "OR ".$data."='".$match."'"; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | $this->or_where = ($arr_check) ? "OR ".$tmp_or_where : $tmp_or_where; |
||
| 249 | } |
||
| 250 | |||
| 251 | public function where_in($field,$data){ |
||
| 252 | $where_in_fields = ''; |
||
| 253 | $last_key = end(array_keys($data)); |
||
| 254 | if($data){ |
||
| 255 | foreach($data as $key=>$row){ |
||
| 256 | if($key == $last_key){ |
||
| 257 | $where_in_fields .= $row; |
||
| 258 | }else{ |
||
| 259 | $where_in_fields .= $row.","; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | $this->where_in = 'WHERE '.$field.' IN ('.$where_in_fields.')'; |
||
| 265 | } |
||
| 266 | |||
| 267 | public function or_where_in($field,$data){ |
||
| 268 | $where_in_fields = ''; |
||
| 269 | $last_key = end(array_keys($data)); |
||
| 270 | if($data){ |
||
| 271 | foreach($data as $key=>$row){ |
||
| 272 | if($key == $last_key){ |
||
| 273 | $where_in_fields .= $row; |
||
| 274 | }else{ |
||
| 275 | $where_in_fields .= $row.","; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | $this->or_where_in = 'OR '.$field.' IN ('.$where_in_fields.')'; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function where_not_in($field,$data){ |
||
| 284 | $where_in_fields = ''; |
||
| 285 | $last_key = end(array_keys($data)); |
||
| 286 | if($data){ |
||
| 287 | foreach($data as $key=>$row){ |
||
| 288 | if($key == $last_key){ |
||
| 289 | $where_in_fields .= $row; |
||
| 290 | }else{ |
||
| 291 | $where_in_fields .= $row.","; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | $this->where_not_in = 'WHERE '.$field.' NOT IN ('.$where_in_fields.')'; |
||
| 297 | } |
||
| 298 | |||
| 299 | public function or_where_not_in($field,$data){ |
||
| 300 | $where_in_fields = ''; |
||
| 301 | $last_key = end(array_keys($data)); |
||
| 302 | if($data){ |
||
| 303 | foreach($data as $key=>$row){ |
||
| 304 | if($key == $last_key){ |
||
| 305 | $where_in_fields .= $row; |
||
| 306 | }else{ |
||
| 307 | $where_in_fields .= $row.","; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | $this->or_where_not_in = 'OR '.$field.' NOT IN ('.$where_in_fields.')'; |
||
| 313 | } |
||
| 314 | |||
| 315 | public function like($data=null,$match=null){ |
||
| 316 | $tmp_like = ''; |
||
| 317 | $arr_check = false; |
||
| 318 | if($data){ |
||
| 319 | if(is_array($data)){ |
||
| 320 | end($data); |
||
| 321 | $last_element = key($data); |
||
| 322 | |||
| 323 | if($data){ |
||
| 324 | $arr_check = true; |
||
| 325 | foreach($data as $key=>$row){ |
||
| 326 | if($key == $last_element){ |
||
| 327 | $tmp_like .= $key." LIKE '%".$row."%'"; |
||
| 328 | }else{ |
||
| 329 | $tmp_like .= $key." LIKE '%".$row."%' AND "; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | }else{ |
||
| 334 | $arr_check = false; |
||
| 335 | $tmp_like = "WHERE ".$data." LIKE '%".$match."%'"; |
||
| 336 | } |
||
| 337 | |||
| 338 | } |
||
| 339 | |||
| 340 | $this->like = ($arr_check == false) ? "WHERE ".$tmp_like : $tmp_like; |
||
|
0 ignored issues
–
show
|
|||
| 341 | } |
||
| 342 | |||
| 343 | public function or_like($data=null,$match=null){ |
||
| 344 | $tmp_or_like = ''; |
||
| 345 | if($data){ |
||
| 346 | if(is_array($data)){ |
||
| 347 | end($data); |
||
| 348 | $last_element = key($data); |
||
| 349 | |||
| 350 | if($data){ |
||
| 351 | foreach($data as $key=>$row){ |
||
| 352 | if($key == $last_element){ |
||
| 353 | $tmp_or_like .= "OR ".$key." LIKE '%".$row."%'"; |
||
| 354 | }else{ |
||
| 355 | $tmp_or_like .= "OR ".$key." LIKE '%".$row."%' AND "; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | } |
||
| 359 | }else{ |
||
| 360 | $tmp_or_like = "OR ".$data." LIKE '%".$match."%'"; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | $this->or_like = $tmp_or_like; |
||
| 365 | } |
||
| 366 | |||
| 367 | public function not_like($data=null,$match=null){ |
||
| 368 | $tmp_like = ''; |
||
| 369 | $arr_check = false; |
||
| 370 | if($data){ |
||
| 371 | if(is_array($data)){ |
||
| 372 | end($data); |
||
| 373 | $last_element = key($data); |
||
| 374 | |||
| 375 | if($data){ |
||
| 376 | $arr_check = true; |
||
| 377 | foreach($data as $key=>$row){ |
||
| 378 | if($key == $last_element){ |
||
| 379 | $tmp_like .= $key." NOT LIKE '%".$row."%'"; |
||
| 380 | }else{ |
||
| 381 | $tmp_like .= $key." NOT LIKE '%".$row."%' AND "; |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 385 | }else{ |
||
| 386 | $arr_check = false; |
||
| 387 | $tmp_like = "WHERE ".$data." NOT LIKE '%".$match."%'"; |
||
| 388 | } |
||
| 389 | |||
| 390 | } |
||
| 391 | |||
| 392 | $this->like = ($arr_check) ? "WHERE ".$tmp_like : $tmp_like; |
||
| 393 | } |
||
| 394 | |||
| 395 | public function or_not_like($data=null,$match=null){ |
||
| 396 | $tmp_or_like = ''; |
||
| 397 | if($data){ |
||
| 398 | if(is_array($data)){ |
||
| 399 | end($data); |
||
| 400 | $last_element = key($data); |
||
| 401 | |||
| 402 | if($data){ |
||
| 403 | foreach($data as $key=>$row){ |
||
| 404 | if($key == $last_element){ |
||
| 405 | $tmp_or_like .= "OR ".$key." NOT LIKE '%".$row."%'"; |
||
| 406 | }else{ |
||
| 407 | $tmp_or_like .= "OR ".$key." NOT LIKE '%".$row."%' AND "; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } |
||
| 411 | }else{ |
||
| 412 | $tmp_or_like = "OR ".$data." NOT LIKE '%".$match."%'"; |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | $this->or_like = $tmp_or_like; |
||
| 417 | } |
||
| 418 | |||
| 419 | public function order_by($field,$sort){ |
||
| 420 | $this->order_by = $field.' '.$sort; |
||
| 421 | } |
||
| 422 | |||
| 423 | public function join($table,$joint,$join_type="INNER"){ |
||
| 424 | $this->join = $join_type.' JOIN '.$table.' ON '.$joint; |
||
| 425 | } |
||
| 426 | |||
| 427 | public function group_by($field){ |
||
| 428 | $this->group_by = 'GROUP BY '.$field; |
||
| 429 | } |
||
| 430 | |||
| 431 | public function get($table=null,$limit=null,$offset=null){ |
||
| 432 | $this->table = $table; |
||
| 433 | $this->limit = $limit; |
||
| 434 | $this->offset = $offset; |
||
| 435 | } |
||
| 436 | |||
| 437 | public function clear_global_var(){ |
||
| 438 | $this->select = ''; |
||
| 439 | $this->where = ''; |
||
| 440 | $this->or_where = ''; |
||
| 441 | $this->order_by = ''; |
||
| 442 | $this->join = ''; |
||
| 443 | $this->group_by = ''; |
||
| 444 | $this->limit = ''; |
||
| 445 | $this->table = ''; |
||
| 446 | $this->offset = ''; |
||
| 447 | $this->where_in = ''; |
||
| 448 | $this->or_where_in = ''; |
||
| 449 | $this->where_not_in = ''; |
||
| 450 | $this->or_where_not_in = ''; |
||
| 451 | $this->like = ''; |
||
| 452 | $this->or_like = ''; |
||
| 453 | $this->not_like = ''; |
||
| 454 | $this->or_not_like = ''; |
||
| 455 | $this->row = ''; |
||
| 456 | $this->num_rows = ''; |
||
| 457 | $this->result = ''; |
||
| 458 | } |
||
| 459 | |||
| 460 | public function row(){ |
||
| 461 | /*init var*/ |
||
| 462 | $result = []; |
||
| 463 | $select = ($this->select) ? implode(',',$this->select) : '*'; |
||
| 464 | $order_by = ($this->order_by) ? 'ORDER BY '.$this->order_by : ''; |
||
| 465 | $join = ($this->join) ? $this->join : ''; |
||
| 466 | $group_by = ($this->group_by) ? $this->group_by : ''; |
||
| 467 | $limit = ($this->limit) ? 'LIMIT '.$this->limit : ''; |
||
| 468 | $offset = ($this->offset) ? 'OFFSET '.$this->offset : ''; |
||
| 469 | $table = ($this->table) ? $this->table : ''; |
||
| 470 | |||
| 471 | $where = ($this->where) ? $this->where : ''; |
||
| 472 | $or_where = ($this->or_where) ? $this->or_where : ''; |
||
| 473 | $where_in = ($this->where_in) ? $this->where_in : ''; |
||
| 474 | $or_where_in = ($this->or_where_in) ? $this->or_where_in : ''; |
||
| 475 | $where_not_in = ($this->where_not_in) ? $this->where_not_in : ''; |
||
| 476 | $or_where_not_in = ($this->or_where_not_in) ? $this->or_where_not_in : ''; |
||
| 477 | |||
| 478 | $like = ($this->like) ? $this->like : ''; |
||
| 479 | $or_like = ($this->or_like) ? $this->or_like : ''; |
||
| 480 | $not_like = ($this->not_like) ? $this->not_like : ''; |
||
| 481 | $or_not_like = ($this->or_not_like) ? $this->or_not_like : ''; |
||
| 482 | /*end init var*/ |
||
| 483 | |||
| 484 | $this->row = "SELECT {$select} FROM {$table} {$join} {$where} {$or_where} {$where_in} {$or_where_in} {$where_not_in} {$or_where_not_in} {$like} {$or_like} {$not_like} {$or_not_like} {$group_by} {$order_by} {$limit} {$offset}"; |
||
| 485 | $this->last_query = $this->row; |
||
| 486 | $data = $this->redbean->getRow($this->row); |
||
| 487 | if($data){ |
||
| 488 | foreach($data as $key=>$row){ |
||
| 489 | $result[$key] = $row; |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | /*clear global variables*/ |
||
| 494 | $this->clear_global_var(); |
||
| 495 | /*end clearing global variables*/ |
||
| 496 | |||
| 497 | return $result; |
||
| 498 | } |
||
| 499 | |||
| 500 | public function num_rows(){ |
||
| 501 | /*init var*/ |
||
| 502 | $result = 0;#array(); |
||
| 503 | $select = ($this->select) ? implode(',',$this->select) : '*'; |
||
| 504 | $order_by = ($this->order_by) ? 'ORDER BY '.$this->order_by : ''; |
||
| 505 | $join = ($this->join) ? $this->join : ''; |
||
| 506 | $group_by = ($this->group_by) ? $this->group_by : ''; |
||
| 507 | $limit = ($this->limit) ? 'LIMIT '.$this->limit : ''; |
||
| 508 | $offset = ($this->offset) ? 'OFFSET '.$this->offset : ''; |
||
| 509 | $table = ($this->table) ? $this->table : ''; |
||
| 510 | |||
| 511 | $where = ($this->where) ? $this->where : ''; |
||
| 512 | $or_where = ($this->or_where) ? $this->or_where : ''; |
||
| 513 | $where_in = ($this->where_in) ? $this->where_in : ''; |
||
| 514 | $or_where_in = ($this->or_where_in) ? $this->or_where_in : ''; |
||
| 515 | $where_not_in = ($this->where_not_in) ? $this->where_not_in : ''; |
||
| 516 | $or_where_not_in = ($this->or_where_not_in) ? $this->or_where_not_in : ''; |
||
| 517 | |||
| 518 | $like = ($this->like) ? $this->like : ''; |
||
| 519 | $or_like = ($this->or_like) ? $this->or_like : ''; |
||
| 520 | $not_like = ($this->not_like) ? $this->not_like : ''; |
||
| 521 | $or_not_like = ($this->or_not_like) ? $this->or_not_like : ''; |
||
| 522 | /*end init var*/ |
||
| 523 | |||
| 524 | $this->num_rows = "SELECT {$select} FROM {$table} {$join} {$where} {$or_where} {$where_in} {$or_where_in} {$where_not_in} {$or_where_not_in} {$like} {$or_like} {$not_like} {$or_not_like} {$group_by} {$order_by} {$limit} {$offset}"; |
||
| 525 | $this->last_query = $this->num_rows; |
||
| 526 | $data = $this->redbean->exec($this->num_rows); |
||
| 527 | $result = $result + $data; |
||
| 528 | |||
| 529 | /*clear global variables*/ |
||
| 530 | $this->clear_global_var(); |
||
| 531 | /*end clearing global variables*/ |
||
| 532 | |||
| 533 | return $result; |
||
| 534 | } |
||
| 535 | |||
| 536 | public function result(){ |
||
| 537 | /*init var*/ |
||
| 538 | $result = array(); |
||
| 539 | $select = ($this->select) ? implode(',',$this->select) : '*'; |
||
| 540 | $order_by = ($this->order_by) ? 'ORDER BY '.$this->order_by : ''; |
||
| 541 | $join = ($this->join) ? $this->join : ''; |
||
| 542 | $group_by = ($this->group_by) ? $this->group_by : ''; |
||
| 543 | $limit = ($this->limit) ? 'LIMIT '.$this->limit : ''; |
||
| 544 | $offset = ($this->offset) ? 'OFFSET '.$this->offset : ''; |
||
| 545 | $table = ($this->table) ? $this->table : ''; |
||
| 546 | |||
| 547 | $where = ($this->where) ? $this->where : ''; |
||
| 548 | $or_where = ($this->or_where) ? $this->or_where : ''; |
||
| 549 | $where_in = ($this->where_in) ? $this->where_in : ''; |
||
| 550 | $or_where_in = ($this->or_where_in) ? $this->or_where_in : ''; |
||
| 551 | $where_not_in = ($this->where_not_in) ? $this->where_not_in : ''; |
||
| 552 | $or_where_not_in = ($this->or_where_not_in) ? $this->or_where_not_in : ''; |
||
| 553 | |||
| 554 | $like = ($this->like) ? $this->like : ''; |
||
| 555 | $or_like = ($this->or_like) ? $this->or_like : ''; |
||
| 556 | $not_like = ($this->not_like) ? $this->not_like : ''; |
||
| 557 | $or_not_like = ($this->or_not_like) ? $this->or_not_like : ''; |
||
| 558 | /*end init var*/ |
||
| 559 | |||
| 560 | #$this->get($table,$limit,$offset); |
||
| 561 | |||
| 562 | $this->result = "SELECT {$select} FROM {$table} {$join} {$where} {$or_where} {$where_in} {$or_where_in} {$where_not_in} {$or_where_not_in} {$like} {$or_like} {$not_like} {$or_not_like} {$group_by} {$order_by} {$limit} {$offset}"; |
||
| 563 | $this->last_query = $this->result; |
||
| 564 | $data = $this->redbean->getAll($this->result); |
||
| 565 | if($data){ |
||
| 566 | foreach($data as $key=>$row){ |
||
| 567 | $result[$key] = (object)$row; |
||
| 568 | } |
||
| 569 | } |
||
| 570 | |||
| 571 | /*clear global variables*/ |
||
| 572 | $this->clear_global_var(); |
||
| 573 | /*end clearing global variables*/ |
||
| 574 | |||
| 575 | return $result; |
||
| 576 | } |
||
| 577 | |||
| 578 | /*Database*/ |
||
| 579 | public function wipe($beanType){ |
||
| 580 | $result = $this->redbean->wipe($beanType); |
||
| 581 | return ($result) ? $result : false; |
||
| 582 | } |
||
| 583 | |||
| 584 | public function dispense($data){ |
||
| 585 | $result = $this->redbean->dispense($data); |
||
| 586 | return ($result) ? true : false; |
||
| 587 | } |
||
| 588 | |||
| 589 | public function store($data){ |
||
| 590 | $result = $this->redbean->store($data); |
||
| 591 | return ($result) ? true : false; |
||
| 592 | } |
||
| 593 | |||
| 594 | public function inspect($data){ |
||
| 595 | if($data){ |
||
| 596 | if(is_array($data)){ |
||
| 597 | |||
| 598 | $result = array(); |
||
| 599 | foreach($data as $key=>$row){ |
||
| 600 | $result[$key]=$this->redbean->inspect($row); |
||
| 601 | } |
||
| 602 | |||
| 603 | return ($result) ? $result : false; |
||
| 604 | |||
| 605 | }else{ |
||
| 606 | $this->redbean->inspect($data); |
||
| 607 | return true; |
||
| 608 | } |
||
| 609 | } |
||
| 610 | } |
||
| 611 | |||
| 612 | public function get_all_tables(){ |
||
| 613 | $result = $this->redbean->inspect(); |
||
| 614 | return ($result) ? $result : false; |
||
| 615 | } |
||
| 616 | |||
| 617 | public function select_database($db_name){ |
||
| 618 | $result = $this->redbean->selectDatabase($db_name); |
||
| 619 | return ($result) ? true : false; |
||
| 620 | } |
||
| 621 | |||
| 622 | public function set_database(){ |
||
| 623 | $result = $this->redbean->selectDatabase('default'); |
||
| 624 | return ($result) ? true : false; |
||
| 625 | } |
||
| 626 | |||
| 627 | public function begin(){ |
||
| 628 | $result = $this->redbean->begin(); |
||
| 629 | return ($result) ? true : false; |
||
| 630 | } |
||
| 631 | |||
| 632 | public function commit(){ |
||
| 633 | $result = $this->redbean->commit(); |
||
| 634 | return ($result) ? true : false; |
||
| 635 | } |
||
| 636 | |||
| 637 | public function roll_back(){ |
||
| 638 | $result = $this->redbean->rollback(); |
||
| 639 | return ($result) ? true : false; |
||
| 640 | } |
||
| 641 | |||
| 642 | public function get_query_count(){ |
||
| 643 | $this->redbean->getQueryCount(); |
||
| 644 | return true; |
||
| 645 | } |
||
| 646 | |||
| 647 | public function get_logs(){ |
||
| 648 | $this->redbean->getLogs(); |
||
| 649 | return true; |
||
| 650 | } |
||
| 651 | |||
| 652 | /*querying*/ |
||
| 653 | public function exec($data){ |
||
| 654 | $result = $this->redbean->exec($data); |
||
| 655 | return ($result) ? $result : false; |
||
| 656 | } |
||
| 657 | |||
| 658 | public function get_all($table){ |
||
| 659 | $result = $this->redbean->getAll($table); |
||
| 660 | return ($result) ? $result : false; |
||
| 661 | } |
||
| 662 | |||
| 663 | public function get_row($data){ |
||
| 664 | $result = $this->redbean->getRow($data); |
||
| 665 | return ($result) ? $result : false; |
||
| 666 | } |
||
| 667 | |||
| 668 | public function get_column($data){ |
||
| 669 | $result = $this->redbean->getCol($data); |
||
| 670 | return ($result) ? $result : false; |
||
| 671 | } |
||
| 672 | |||
| 673 | public function get_cell($data){ |
||
| 674 | $result = $this->redbean->getCell($data); |
||
| 675 | return ($result) ? $result : false; |
||
| 676 | } |
||
| 677 | |||
| 678 | public function get_assoc($data){ |
||
| 679 | $result = $this->redbean->getAssoc($data); |
||
| 680 | return ($result) ? $result : false; |
||
| 681 | } |
||
| 682 | |||
| 683 | public function get_inserted_id(){ |
||
| 684 | $result = $this->redbean->getInsertID(); |
||
| 685 | return ($result) ? $result : false; |
||
| 686 | } |
||
| 687 | |||
| 688 | public function convert_to_beans($type,$rows,$metamask=null){ |
||
| 689 | $result = $this->redbean->convertToBeans( $type, $rows, $metamask ); |
||
| 690 | return ($result) ? $result : false; |
||
| 691 | } |
||
| 692 | |||
| 693 | /*Data Tools*/ |
||
| 694 | public function match_up($type, $sql, $bindings = array(), $onFoundDo = NULL, $onNotFoundDo = NULL, &$bean = NULL){ |
||
| 695 | $result = $this->redbean->matchUp($type, $sql, $bindings, $onFoundDo, $onNotFoundDo, $bean); |
||
| 696 | return ($result) ? $result : false; |
||
| 697 | } |
||
| 698 | |||
| 699 | public function find_all($table){ |
||
| 700 | $this->redbean->findAll($table); |
||
| 701 | return true; |
||
| 702 | } |
||
| 703 | |||
| 704 | public function find($type,$sql,$bindings){ |
||
| 705 | $result = $this->redbean->find($type,$sql,$bindings); |
||
| 706 | return ($result) ? $result : false; |
||
| 707 | } |
||
| 708 | |||
| 709 | /*Fluid and Frozen*/ |
||
| 710 | public function freeze($data=null){ |
||
| 711 | if($data){ |
||
| 712 | $data = array(); |
||
| 713 | foreach($data as $key=>$row){ |
||
| 714 | $data[$key] = $row; |
||
| 715 | } |
||
| 716 | |||
| 717 | $this->redbean->freeze($data); |
||
| 718 | }else{ |
||
| 719 | $this->redbean->freeze(TRUE); |
||
| 720 | } |
||
| 721 | return true; |
||
| 722 | } |
||
| 723 | |||
| 724 | /*Debugging*/ |
||
| 725 | /*value "true", "false"*/ |
||
| 726 | public function debug($tf = TRUE, $mode = 0){ |
||
| 727 | $result = $this->redbean->debug($tf,$mode); |
||
| 728 | return ($result) ? $result : false; |
||
| 729 | } |
||
| 730 | |||
| 731 | public function dump($data){ |
||
| 732 | $this->redbean->dump($data); |
||
| 733 | return true; |
||
| 734 | } |
||
| 735 | |||
| 736 | public function test_connection(){ |
||
| 737 | $result = $this->redbean->testConnection(); |
||
| 738 | return ($result) ? $result : false; |
||
| 739 | } |
||
| 740 | |||
| 741 | /*Aliases*/ |
||
| 742 | public function load($oodb, $types, $id){ |
||
| 743 | $result = $this->redbean->load($oodb, $types, $id); |
||
| 744 | return ($result) ? $result : false; |
||
| 745 | } |
||
| 746 | |||
| 747 | /*Count*/ |
||
| 748 | public function count($type, $addSQL = '', $bindings = array()){ |
||
| 749 | $result = $this->redbean->count($type,$addSQL,$bindings); |
||
| 750 | return ($result) ? $result : false; |
||
| 751 | } |
||
| 752 | |||
| 753 | public function count_shared($type){ |
||
| 754 | $result = $this->redbean->countOwn($type); |
||
| 755 | return ($result) ? $result : false; |
||
| 756 | } |
||
| 757 | |||
| 758 | /*Labels, Enums, Tags*/ |
||
| 759 | public function dispense_labels($type,$labels){ |
||
| 760 | $result = $this->redbean->dispenseLabels($type,$labels); |
||
| 761 | return ($result) ? $result : false; |
||
| 762 | } |
||
| 763 | |||
| 764 | public function gather_labels($beans){ |
||
| 765 | $result = $this->redbean->gatherLabels($beans); |
||
| 766 | return ($result) ? $result : false; |
||
| 767 | } |
||
| 768 | |||
| 769 | public function enum($enum){ |
||
| 770 | $result = $this->redbean->enum($enum); |
||
| 771 | return ($result) ? $result : false; |
||
| 772 | } |
||
| 773 | |||
| 774 | public function tag($bean, $tagList){ |
||
| 775 | $result = $this->redbean->tag($bean, $tagList); |
||
| 776 | return ($result) ? $result : false; |
||
| 777 | } |
||
| 778 | } |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.