├── .gitignore ├── .travis.yml ├── DOCS.md ├── LICENCE.md ├── README.md ├── composer.json ├── example.php ├── src ├── Cache.php ├── Pdox.php └── PdoxInterface.php └── tests ├── DeleteTest.php ├── InsertTest.php ├── JoinTest.php ├── SelectTest.php └── UpdateTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.git 2 | /.DS_Store 3 | /composer.lock 4 | /vendor 5 | /index.php 6 | /.htaccess 7 | /.idea 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - "7.4" 4 | - "7.3" 5 | - "7.2" 6 | - "7.1" 7 | - "7.0" 8 | - "5.6" 9 | - "5.5" -------------------------------------------------------------------------------- /DOCS.md: -------------------------------------------------------------------------------- 1 | # PDOx Documentation 2 | 3 | ## Install 4 | `composer.json` file: 5 | ```json 6 | { 7 | "require": { 8 | "izniburak/pdox": "^1" 9 | } 10 | } 11 | ``` 12 | after run the install command. 13 | ``` 14 | $ composer install 15 | ``` 16 | 17 | OR run the following command directly. 18 | 19 | ``` 20 | $ composer require izniburak/pdox 21 | ``` 22 | 23 | ## Quick Usage 24 | ```php 25 | require 'vendor/autoload.php'; 26 | 27 | $config = [ 28 | 'driver' => 'mysql', 29 | 'host' => 'localhost', 30 | 'database' => 'test', 31 | 'username' => 'root', 32 | 'password' => '', 33 | 'charset' => 'utf8', 34 | 'collation' => 'utf8_general_ci', 35 | 'prefix' => '', 36 | 'options' => [ ] 37 | ]; 38 | 39 | $db = new \Buki\Pdox($config); 40 | ``` 41 | 42 | Congratulations! Now, you can use PDOx. 43 | 44 | If you have a problem, you can [contact me][support-url]. 45 | 46 | # Detailed Usage and Methods 47 | 48 | ## config 49 | ```php 50 | $config = [ 51 | # Database Driver Type (optional) 52 | # default value: mysql 53 | # values: mysql, pgsql, sqlite, oracle 54 | 'driver' => 'mysql', 55 | 56 | # Host name or IP Address (optional) 57 | # hostname:port (for Port Usage. Example: 127.0.0.1:1010) 58 | # default value: localhost 59 | 'host' => 'localhost', 60 | 61 | # IP Address for Database Host (optional) 62 | # default value: null 63 | 'port' => 3306, 64 | 65 | # Database Name (required) 66 | 'database' => 'test', 67 | 68 | # Database User Name (required) 69 | 'username' => 'root', 70 | 71 | # Database User Password (required) 72 | 'password' => '', 73 | 74 | # Database Charset (optional) 75 | # default value: utf8 76 | 'charset' => 'utf8', 77 | 78 | # Database Charset Collation (optional) 79 | # default value: utf8_general_ci 80 | 'collation' => 'utf8_general_ci', 81 | 82 | # Database Prefix (optional) 83 | # default value: null 84 | 'prefix' => '', 85 | 86 | # Cache Directory of the Sql Result (optional) 87 | # default value: __DIR__ . '/cache/' 88 | 'cachedir' => __DIR__ . '/cache/sql/', 89 | 90 | # Connection options (Things like SSL certificates, etc) 91 | 'options' => [ ] 92 | ]; 93 | 94 | $db = new \Buki\Pdox($config); 95 | ``` 96 | 97 | ## Contents 98 | 99 | * [select](#select) 100 | * [select functions (min, max, sum, avg, count)](#select-functions-min-max-sum-avg-count) 101 | * [table](#table) 102 | * [get AND getAll](#get-and-getall) 103 | * [join](#join) 104 | * [where](#where) 105 | * [grouped](#grouped) 106 | * [in](#in) 107 | * [findInSet](#findinset) 108 | * [between](#between) 109 | * [like](#like) 110 | * [groupBy](#groupby) 111 | * [having](#having) 112 | * [orderBy](#orderby) 113 | * [limit - offset](#limit---offset) 114 | * [pagination](#pagination) 115 | * [insert](#insert) 116 | * [update](#update) 117 | * [delete](#delete) 118 | * [analyze](#analyze) - [check](#check) - [checksum](#checksum) - [optimize](#optimize) - [repair](#repair) 119 | * [query](#query) 120 | * [insertId](#insertid) 121 | * [numRows](#numrows) 122 | * [cache](#cache) 123 | * [transaction](#transaction) - [commit](#transaction) - [rollBack](#transaction) 124 | * [error](#error) 125 | * [queryCount](#querycount) 126 | * [getQuery](#getquery) 127 | 128 | ## Methods 129 | 130 | ### select 131 | ```php 132 | # Usage 1: string parameter 133 | $db->select('title, content')->table('test')->getAll(); 134 | # Output: "SELECT title, content FROM test" 135 | 136 | $db->select('title AS t, content AS c')->table('test')->getAll(); 137 | # Output: "SELECT title AS t, content AS c FROM test" 138 | ``` 139 | ```php 140 | # Usage2: array parameter 141 | $db->select(['title', 'content'])->table('test')->getAll(); 142 | # Output: "SELECT title, content FROM test" 143 | 144 | $db->select(['title AS t', 'content AS c'])->table('test')->getAll(); 145 | # Output: "SELECT title AS t, content AS c FROM test" 146 | ``` 147 | 148 | ### select functions (min, max, sum, avg, count) 149 | ```php 150 | # Usage 1: 151 | $db->table('test')->max('price')->get(); 152 | # Output: "SELECT MAX(price) FROM test" 153 | 154 | # Usage 2: 155 | $db->table('test')->count('id', 'total_row')->get(); 156 | # Output: "SELECT COUNT(id) AS total_row FROM test" 157 | ``` 158 | 159 | ### table 160 | ```php 161 | ### Usage 1: string parameter 162 | $db->table('table'); 163 | # Output: "SELECT * FROM table" 164 | 165 | $db->table('table1, table2'); 166 | # Output: "SELECT * FROM table1, table2" 167 | 168 | $db->table('table1 AS t1, table2 AS t2'); 169 | # Output: "SELECT * FROM table1 AS t1, table2 AS t2" 170 | ``` 171 | ```php 172 | ### Usage 2: array parameter 173 | $db->table(['table1', 'table2']); 174 | # Output: "SELECT * FROM table1, table2" 175 | 176 | $db->table(['table1 AS t1', 'table2 AS t2']); 177 | # Output: "SELECT * FROM table1 AS t1, table2 AS t2" 178 | ``` 179 | 180 | ### get AND getAll 181 | ```php 182 | # get(): return 1 record. 183 | # getAll(): return multiple records. 184 | 185 | $db->table('test')->getAll(); 186 | # Output: "SELECT * FROM test" 187 | 188 | $db->select('username')->table('users')->where('status', 1)->getAll(); 189 | # Output: "SELECT username FROM users WHERE status='1'" 190 | 191 | $db->select('title')->table('pages')->where('id', 17)->get(); 192 | # Output: "SELECT title FROM pages WHERE id='17' LIMIT 1" 193 | ``` 194 | 195 | ### join 196 | ```php 197 | $db->table('test as t')->join('foo as f', 't.id', 'f.t_id')->where('t.status', 1)->getAll(); 198 | # Output: "SELECT * FROM test as t JOIN foo as f ON t.id=f.t_id WHERE t.status='1'" 199 | ``` 200 | You can use this method in 7 ways. These; 201 | - join 202 | - left_join 203 | - right_join 204 | - inner_join 205 | - full_outer_join 206 | - left_outer_join 207 | - right_outer_join 208 | 209 | Examples: 210 | ```php 211 | $db->table('test as t')->leftJoin('foo as f', 't.id', 'f.t_id')->getAll(); 212 | # Output: "SELECT * FROM test as t LEFT JOIN foo as f ON t.id=f.t_id" 213 | ``` 214 | 215 | ```php 216 | $db->table('test as t')->fullOuterJoin('foo as f', 't.id', 'f.t_id')->getAll(); 217 | # Output: "SELECT * FROM test as t FULL OUTER JOIN foo as f ON t.id=f.t_id" 218 | ``` 219 | 220 | ### where 221 | ```php 222 | $where = [ 223 | 'name' => 'Burak', 224 | 'age' => 23, 225 | 'status' => 1 226 | ]; 227 | $db->table('test')->where($where)->get(); 228 | # Output: "SELECT * FROM test WHERE name='Burak' AND age='23' AND status='1' LIMIT 1" 229 | 230 | # OR 231 | 232 | $db->table('test')->where('active', 1)->getAll(); 233 | # Output: "SELECT * FROM test WHERE active='1'" 234 | 235 | # OR 236 | 237 | $db->table('test')->where('age', '>=', 18)->getAll(); 238 | # Output: "SELECT * FROM test WHERE age>='18'" 239 | 240 | # OR 241 | 242 | $db->table('test')->where('age = ? OR age = ?', [18, 20])->getAll(); 243 | # Output: "SELECT * FROM test WHERE age='18' OR age='20'" 244 | ``` 245 | 246 | You can use this method in 4 ways. These; 247 | 248 | - where 249 | - orWhere 250 | - notWhere 251 | - orNotWhere 252 | - whereNull 253 | - whereNotNull 254 | 255 | Example: 256 | ```php 257 | $db->table('test')->where('active', 1)->notWhere('auth', 1)->getAll(); 258 | # Output: "SELECT * FROM test WHERE active = '1' AND NOT auth = '1'" 259 | 260 | # OR 261 | 262 | $db->table('test')->where('age', 20)->orWhere('age', '>', 25)->getAll(); 263 | # Output: "SELECT * FROM test WHERE age = '20' OR age > '25'" 264 | 265 | $db->table('test')->whereNotNull('email')->getAll(); 266 | # Output: "SELECT * FROM test WHERE email IS NOT NULL" 267 | ``` 268 | 269 | ### grouped 270 | ```php 271 | $db->table('users') 272 | ->grouped(function($q) { 273 | $q->where('country', 'TURKEY')->orWhere('country', 'ENGLAND'); 274 | }) 275 | ->where('status', 1) 276 | ->getAll(); 277 | # Ouput: "SELECT * FROM users WHERE (country='TURKEY' OR country='ENGLAND') AND status ='1'" 278 | ``` 279 | 280 | ### in 281 | ```php 282 | $db->table('test')->where('active', 1)->in('id', [1, 2, 3])->getAll(); 283 | # Output: "SELECT * FROM test WHERE active = '1' AND id IN ('1', '2', '3')" 284 | ``` 285 | 286 | You can use this method in 4 ways. These; 287 | 288 | - in 289 | - orIn 290 | - notIn 291 | - orNotIn 292 | 293 | Example: 294 | ```php 295 | $db->table('test')->where('active', 1)->notIn('id', [1, 2, 3])->getAll(); 296 | # Output: "SELECT * FROM test WHERE active = '1' AND id NOT IN ('1', '2', '3')" 297 | 298 | # OR 299 | 300 | $db->table('test')->where('active', 1)->orIn('id', [1, 2, 3])->getAll(); 301 | # Output: "SELECT * FROM test WHERE active = '1' OR id IN ('1', '2', '3')" 302 | ``` 303 | 304 | ### findInSet 305 | ```php 306 | $db->table('test')->where('active', 1)->findInSet('selected_ids', 1)->getAll(); 307 | # Output: "SELECT * FROM test WHERE active = '1' AND FIND_IN_SET (1, selected_ids)" 308 | ``` 309 | 310 | You can use this method in 4 ways. These; 311 | 312 | - findInSet 313 | - orFindInSet 314 | - notFindInSet 315 | - orNotFindInSet 316 | 317 | Example: 318 | ```php 319 | $db->table('test')->where('active', 1)->notFindInSet('selected_ids', 1)->getAll(); 320 | # Output: "SELECT * FROM test WHERE active = '1' AND NOT FIND_IN_SET (1, selected_ids)" 321 | 322 | # OR 323 | 324 | $db->table('test')->where('active', 1)->orFindInSet('selected_ids', 1)->getAll(); 325 | # Output: "SELECT * FROM test WHERE active = '1' OR FIND_IN_SET (1, selected_ids)" 326 | ``` 327 | 328 | ### between 329 | ```php 330 | $db->table('test')->where('active', 1)->between('age', 18, 25)->getAll(); 331 | # Output: "SELECT * FROM test WHERE active = '1' AND age BETWEEN '18' AND '25'" 332 | ``` 333 | 334 | You can use this method in 4 ways. These; 335 | 336 | - between 337 | - orBetween 338 | - notBetween 339 | - orNotBetween 340 | 341 | Example: 342 | ```php 343 | $db->table('test')->where('active', 1)->notBetween('age', 18, 25)->getAll(); 344 | # Output: "SELECT * FROM test WHERE active = '1' AND age NOT BETWEEN '18' AND '25'" 345 | 346 | # OR 347 | 348 | $db->table('test')->where('active', 1)->orBetween('age', 18, 25)->getAll(); 349 | # Output: "SELECT * FROM test WHERE active = '1' OR age BETWEEN '18' AND '25'" 350 | ``` 351 | 352 | ### like 353 | ```php 354 | $db->table('test')->like('title', "%php%")->getAll(); 355 | # Output: "SELECT * FROM test WHERE title LIKE '%php%'" 356 | ``` 357 | 358 | You can use this method in 4 ways. These; 359 | 360 | - like 361 | - orLike 362 | - notLike 363 | - orNotLike 364 | 365 | Example: 366 | ```php 367 | $db->table('test')->where('active', 1)->notLike('tags', '%dot-net%')->getAll(); 368 | # Output: "SELECT * FROM test WHERE active = '1' AND tags NOT LIKE '%dot-net%'" 369 | 370 | # OR 371 | 372 | $db->table('test')->like('bio', '%php%')->orLike('bio', '%java%')->getAll(); 373 | # Output: "SELECT * FROM test WHERE bio LIKE '%php%' OR bio LIKE '%java%'" 374 | ``` 375 | 376 | ### groupBy 377 | ```php 378 | # Usage 1: One parameter 379 | $db->table('test')->where('status', 1)->groupBy('cat_id')->getAll(); 380 | # Output: "SELECT * FROM test WHERE status = '1' GROUP BY cat_id" 381 | ``` 382 | 383 | ```php 384 | # Usage 1: Array parameter 385 | $db->table('test')->where('status', 1)->groupBy(['cat_id', 'user_id'])->getAll(); 386 | # Output: "SELECT * FROM test WHERE status = '1' GROUP BY cat_id, user_id" 387 | ``` 388 | 389 | ### having 390 | ```php 391 | $db->table('test')->where('status', 1)->groupBy('city')->having('COUNT(person)', 100)->getAll(); 392 | # Output: "SELECT * FROM test WHERE status='1' GROUP BY city HAVING COUNT(person) > '100'" 393 | 394 | # OR 395 | 396 | $db->table('test')->where('active', 1)->groupBy('department_id')->having('AVG(salary)', '<=', 500)->getAll(); 397 | # Output: "SELECT * FROM test WHERE active='1' GROUP BY department_id HAVING AVG(salary) <= '500'" 398 | 399 | # OR 400 | 401 | $db->table('test')->where('active', 1)->groupBy('department_id')->having('AVG(salary) > ? AND MAX(salary) < ?', [250, 1000])->getAll(); 402 | # Output: "SELECT * FROM test WHERE active='1' GROUP BY department_id HAVING AVG(salary) > '250' AND MAX(salary) < '1000'" 403 | ``` 404 | 405 | ### orderBy 406 | ```php 407 | # Usage 1: One parameter 408 | $db->table('test')->where('status', 1)->orderBy('id')->getAll(); 409 | # Output: "SELECT * FROM test WHERE status='1' ORDER BY id ASC" 410 | 411 | ### OR 412 | 413 | $db->table('test')->where('status', 1)->orderBy('id desc')->getAll(); 414 | # Output: "SELECT * FROM test WHERE status='1' ORDER BY id desc" 415 | ``` 416 | 417 | ```php 418 | # Usage 1: Two parameters 419 | $db->table('test')->where('status', 1)->orderBy('id', 'desc')->getAll(); 420 | # Output: "SELECT * FROM test WHERE status='1' ORDER BY id DESC" 421 | ``` 422 | 423 | ```php 424 | # Usage 3: Rand() 425 | $db->table('test')->where('status', 1)->orderBy('rand()')->limit(10)->getAll(); 426 | # Output: "SELECT * FROM test WHERE status='1' ORDER BY rand() LIMIT 10" 427 | ``` 428 | 429 | ### limit - offset 430 | ```php 431 | # Usage 1: One parameter 432 | $db->table('test')->limit(10)->getAll(); 433 | # Output: "SELECT * FROM test LIMIT 10" 434 | ``` 435 | ```php 436 | # Usage 2: Two parameters 437 | $db->table('test')->limit(10, 20)->getAll(); 438 | # Output: "SELECT * FROM test LIMIT 10, 20" 439 | 440 | # Usage 3: with offset method 441 | $db->table('test')->limit(10)->offset(10)->getAll(); 442 | # Output: "SELECT * FROM test LIMIT 10 OFFSET 10" 443 | ``` 444 | 445 | ### pagination 446 | ```php 447 | # First parameter: Data count of per page 448 | # Second parameter: Active page 449 | 450 | $db->table('test')->pagination(15, 1)->getAll(); 451 | # Output: "SELECT * FROM test LIMIT 15 OFFSET 0" 452 | 453 | $db->table('test')->pagination(15, 2)->getAll(); 454 | # Output: "SELECT * FROM test LIMIT 15 OFFSET 15" 455 | ``` 456 | 457 | ### insert 458 | ```php 459 | $data = [ 460 | 'title' => 'test', 461 | 'content' => 'Lorem ipsum dolor sit amet...', 462 | 'time' => '2017-05-19 19:05:00', 463 | 'status' => 1 464 | ]; 465 | 466 | $db->table('pages')->insert($data); 467 | # Output: "INSERT INTO test (title, content, time, status) VALUES ('test', 'Lorem ipsum dolor sit amet...', '2017-05-19 19:05:00', '1')" 468 | ``` 469 | 470 | ### update 471 | ```php 472 | $data = [ 473 | 'username' => 'izniburak', 474 | 'password' => 'pass', 475 | 'activation' => 1, 476 | 'status' => 1 477 | ]; 478 | 479 | $db->table('users')->where('id', 10)->update($data); 480 | # Output: "UPDATE users SET username='izniburak', password='pass', activation='1', status='1' WHERE id='10'" 481 | ``` 482 | 483 | ### delete 484 | ```php 485 | $db->table('test')->where("id", 17)->delete(); 486 | # Output: "DELETE FROM test WHERE id = '17'" 487 | 488 | # OR 489 | 490 | $db->table('test')->delete(); 491 | # Output: "TRUNCATE TABLE delete" 492 | ``` 493 | 494 | ### transaction 495 | ```php 496 | $db->transaction(); 497 | 498 | $data = [ 499 | 'title' => 'new title', 500 | 'status' => 2 501 | ]; 502 | $db->table('test')->where('id', 10)->update($data); 503 | 504 | $db->commit(); 505 | # OR 506 | $db->rollBack(); 507 | ``` 508 | 509 | ### analyze 510 | ```php 511 | $db->table('users')->analyze(); 512 | # Output: "ANALYZE TABLE users" 513 | ``` 514 | 515 | ### check 516 | ```php 517 | $db->table(['users', 'pages'])->check(); 518 | # Output: "CHECK TABLE users, pages" 519 | ``` 520 | 521 | ### checksum 522 | ```php 523 | $db->table(['users', 'pages'])->checksum(); 524 | # Output: "CHECKSUM TABLE users, pages" 525 | ``` 526 | 527 | ### optimize 528 | ```php 529 | $db->table(['users', 'pages'])->optimize(); 530 | # Output: "OPTIMIZE TABLE users, pages" 531 | ``` 532 | 533 | ### repair 534 | ```php 535 | $db->table(['users', 'pages'])->repair(); 536 | # Output: "REPAIR TABLE users, pages" 537 | ``` 538 | 539 | ### query 540 | ```php 541 | # Usage 1: Select all records 542 | $db->query('SELECT * FROM test WHERE id=? AND status=?', [10, 1])->fetchAll(); 543 | 544 | # Usage 2: Select one record 545 | $db->query('SELECT * FROM test WHERE id=? AND status=?', [10, 1])->fetch(); 546 | 547 | # Usage 3: Other queries like Update, Insert, Delete etc... 548 | $db->query('DELETE FROM test WHERE id=?', [10])->exec(); 549 | ``` 550 | 551 | ### insertId 552 | ```php 553 | $data = [ 554 | 'title' => 'test', 555 | 'content' => 'Lorem ipsum dolor sit amet...', 556 | 'time' => time(), 557 | 'status' => 1 558 | ]; 559 | $db->table('pages')->insert($data); 560 | 561 | var_dump($db->insertId()); 562 | ``` 563 | 564 | ### numRows 565 | ```php 566 | $db->select('id, title')->table('test')->where('status', 1)->orWhere('status', 2)->getAll(); 567 | 568 | var_dump($db->numRows()); 569 | ``` 570 | 571 | ### error 572 | ```php 573 | $db->error(); 574 | ``` 575 | 576 | ### cache 577 | ```php 578 | # Usage: ...->cache($time)->... 579 | $db->table('pages')->where('slug', 'example-page')->cache(60)->get(); 580 | # cache time: 60 seconds 581 | ``` 582 | 583 | ### queryCount 584 | ```php 585 | $db->queryCount(); 586 | # The number of all SQL queries on the page until the end of the beginning. 587 | ``` 588 | 589 | ### getQuery 590 | ```php 591 | $db->getQuery(); 592 | # Last SQL Query. 593 | ``` 594 | 595 | [support-url]: https://github.com/izniburak/PDOx#support 596 | -------------------------------------------------------------------------------- /LICENCE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015, İzni Burak Demirtaş 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## PDOx 2 | ``` 3 | _____ _____ ____ 4 | | __ \| __ \ / __ \ 5 | | |__) | | | | | | |_ __ 6 | | ___/| | | | | | \ \/ / 7 | | | | |__| | |__| |> < 8 | |_| |_____/ \____//_/\_\ 9 | ``` 10 | Fast, efficient and useful Query Builder and PDO Class for #PHP 11 | 12 | [![Total Downloads](https://poser.pugx.org/izniburak/pdox/d/total.svg)](https://packagist.org/packages/izniburak/pdox) 13 | [![Latest Stable Version](https://poser.pugx.org/izniburak/pdox/v/stable.svg)](https://packagist.org/packages/izniburak/pdox) 14 | [![Latest Unstable Version](https://poser.pugx.org/izniburak/pdox/v/unstable.svg)](https://packagist.org/packages/izniburak/pdox) 15 | [![License](https://poser.pugx.org/izniburak/pdox/license.svg)](https://packagist.org/packages/izniburak/pdox) 16 | 17 | ## Install 18 | 19 | composer.json file: 20 | ```json 21 | { 22 | "require": { 23 | "izniburak/pdox": "^1" 24 | } 25 | } 26 | ``` 27 | after run the install command. 28 | ``` 29 | $ composer install 30 | ``` 31 | 32 | OR run the following command directly. 33 | 34 | ``` 35 | $ composer require izniburak/pdox 36 | ``` 37 | 38 | ## Example Usage 39 | ```php 40 | require 'vendor/autoload.php'; 41 | 42 | $config = [ 43 | 'host' => 'localhost', 44 | 'driver' => 'mysql', 45 | 'database' => 'test', 46 | 'username' => 'root', 47 | 'password' => '', 48 | 'charset' => 'utf8', 49 | 'collation' => 'utf8_general_ci', 50 | 'prefix' => '' 51 | ]; 52 | 53 | $db = new \Buki\Pdox($config); 54 | 55 | $records = $db->table('users') 56 | ->select('id, name, surname, age') 57 | ->where('age', '>', 18) 58 | ->orderBy('id', 'desc') 59 | ->limit(20) 60 | ->getAll(); 61 | 62 | var_dump($records); 63 | ``` 64 | 65 | ## Docs 66 | Documentation page: [PDOx Docs][doc-url] 67 | 68 | ## Support 69 | [izniburak's homepage][author-url] 70 | 71 | [izniburak's twitter][twitter-url] 72 | 73 | ## Licence 74 | [MIT Licence][mit-url] 75 | 76 | ## Contributing 77 | 78 | 1. Fork it ( https://github.com/izniburak/pdox/fork ) 79 | 2. Create your feature branch (git checkout -b my-new-feature) 80 | 3. Commit your changes (git commit -am 'Add some feature') 81 | 4. Push to the branch (git push origin my-new-feature) 82 | 5. Create a new Pull Request 83 | 84 | ## Contributors 85 | 86 | - [izniburak](https://github.com/izniburak) İzni Burak Demirtaş - creator, maintainer 87 | - [Others](https://github.com/izniburak/pdox/graphs/contributors) 88 | 89 | [pdox-img]: http://burakdemirtas.org/uploads/images/20140610210255_pdox_pdo_class_for_php.jpg 90 | [paypal-donate-url]: http://burakdemirtas.org 91 | [mit-url]: http://opensource.org/licenses/MIT 92 | [doc-url]: https://github.com/izniburak/PDOx/blob/master/DOCS.md 93 | [author-url]: http://burakdemirtas.org 94 | [twitter-url]: https://twitter.com/izniburak 95 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "izniburak/pdox", 3 | "type": "library", 4 | "description": "Useful Query Builder, PDO Class for PHP. A simple access to your SQL records.", 5 | "keywords": ["php","pdo","sql","query","builder"], 6 | "homepage": "https://github.com/izniburak/PDOx", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "İzni Burak Demirtaş", 11 | "email": "info@burakdemirtas.org", 12 | "homepage": "http://burakdemirtas.org" 13 | } 14 | ], 15 | "require": { 16 | "php": ">=5.5", 17 | "ext-pdo": "*", 18 | "ext-json": "*" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "Buki\\": "src" 23 | } 24 | }, 25 | "minimum-stability": "dev" 26 | } 27 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | 6 | * @ Web: http://burakdemirtas.org 7 | * @ URL: https://github.com/izniburak/PDOx 8 | * @ Licence: The MIT License (MIT) - Copyright (c) - http://opensource.org/licenses/MIT 9 | * 10 | */ 11 | 12 | require 'vendor/autoload.php'; 13 | 14 | // database config 15 | $config = [ 16 | 'host' => 'localhost', 17 | 'driver' => 'mysql', 18 | 'database' => 'test', 19 | 'username' => 'root', 20 | 'password' => '', 21 | 'charset' => 'utf8', 22 | 'collation' => 'utf8_general_ci', 23 | 'prefix' => '' 24 | ]; 25 | 26 | // start PDOx 27 | $db = new \Buki\Pdox($config); 28 | 29 | // Select Records 30 | $records = $db->table('pages') 31 | ->where('age', '>', 18) 32 | ->orderBy('id', 'desc') 33 | ->limit(10) 34 | ->getAll(); 35 | 36 | var_dump($records); 37 | -------------------------------------------------------------------------------- /src/Cache.php: -------------------------------------------------------------------------------- 1 | 7 | * @web 8 | * @url 9 | * @license The MIT License (MIT) - 10 | */ 11 | 12 | namespace Buki; 13 | 14 | class Cache 15 | { 16 | protected $cacheDir = null; 17 | protected $cache = null; 18 | protected $finish = null; 19 | 20 | /** 21 | * Cache constructor. 22 | * 23 | * @param null $dir 24 | * @param int $time 25 | */ 26 | function __construct($dir = null, $time = 0) 27 | { 28 | if (! file_exists($dir)) { 29 | mkdir($dir, 0755); 30 | } 31 | 32 | $this->cacheDir = $dir; 33 | $this->cache = $time; 34 | $this->finish = time() + $time; 35 | } 36 | 37 | /** 38 | * @param $sql 39 | * @param bool $array 40 | * 41 | * @return bool|void 42 | */ 43 | public function getCache($sql, $array = false) 44 | { 45 | if (is_null($this->cache)) { 46 | return false; 47 | } 48 | 49 | $cacheFile = $this->cacheDir . $this->fileName($sql) . '.cache'; 50 | if (file_exists($cacheFile)) { 51 | $cache = json_decode(file_get_contents($cacheFile), $array); 52 | 53 | if (($array ? $cache['finish'] : $cache->finish) < time()) { 54 | unlink($cacheFile); 55 | return; 56 | } 57 | 58 | return ($array ? $cache['data'] : $cache->data); 59 | } 60 | 61 | return false; 62 | } 63 | 64 | /** 65 | * @param $sql 66 | * @param $result 67 | * 68 | * @return bool|void 69 | */ 70 | public function setCache($sql, $result) 71 | { 72 | if (is_null($this->cache)) { 73 | return false; 74 | } 75 | 76 | $cacheFile = $this->cacheDir . $this->fileName($sql) . '.cache'; 77 | $cacheFile = fopen($cacheFile, 'w'); 78 | 79 | if ($cacheFile) { 80 | fputs($cacheFile, json_encode(['data' => $result, 'finish' => $this->finish])); 81 | } 82 | 83 | return; 84 | } 85 | 86 | /** 87 | * @param $name 88 | * 89 | * @return string 90 | */ 91 | protected function fileName($name) 92 | { 93 | return md5($name); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/Pdox.php: -------------------------------------------------------------------------------- 1 | 14 | * @url 15 | * @license The MIT License (MIT) - 16 | */ 17 | class Pdox implements PdoxInterface 18 | { 19 | /** 20 | * PDOx Version 21 | * 22 | * @var string 23 | */ 24 | const VERSION = '1.6.0'; 25 | 26 | /** 27 | * @var PDO|null 28 | */ 29 | public $pdo = null; 30 | 31 | /** 32 | * @var mixed Query variables 33 | */ 34 | protected $select = '*'; 35 | protected $from = null; 36 | protected $where = null; 37 | protected $limit = null; 38 | protected $offset = null; 39 | protected $join = null; 40 | protected $orderBy = null; 41 | protected $groupBy = null; 42 | protected $having = null; 43 | protected $grouped = false; 44 | protected $numRows = 0; 45 | protected $insertId = null; 46 | protected $query = null; 47 | protected $error = null; 48 | protected $result = []; 49 | protected $prefix = null; 50 | 51 | /** 52 | * @var array SQL operators 53 | */ 54 | protected $operators = ['=', '!=', '<', '>', '<=', '>=', '<>']; 55 | 56 | /** 57 | * @var Cache|null 58 | */ 59 | protected $cache = null; 60 | 61 | /** 62 | * @var string|null Cache Directory 63 | */ 64 | protected $cacheDir = null; 65 | 66 | /** 67 | * @var int Total query count 68 | */ 69 | protected $queryCount = 0; 70 | 71 | /** 72 | * @var bool 73 | */ 74 | protected $debug = true; 75 | 76 | /** 77 | * @var int Total transaction count 78 | */ 79 | protected $transactionCount = 0; 80 | 81 | /** 82 | * Pdox constructor. 83 | * 84 | * @param array $config 85 | */ 86 | public function __construct(array $config) 87 | { 88 | $config['driver'] = isset($config['driver']) ? $config['driver'] : 'mysql'; 89 | $config['host'] = isset($config['host']) ? $config['host'] : 'localhost'; 90 | $config['charset'] = isset($config['charset']) ? $config['charset'] : 'utf8mb4'; 91 | $config['collation'] = isset($config['collation']) ? $config['collation'] : 'utf8mb4_general_ci'; 92 | $config['port'] = isset($config['port']) 93 | ? $config['port'] 94 | : (strstr($config['host'], ':') ? explode(':', $config['host'])[1] : ''); 95 | $this->prefix = isset($config['prefix']) ? $config['prefix'] : ''; 96 | $this->cacheDir = isset($config['cachedir']) ? $config['cachedir'] : __DIR__ . '/cache/'; 97 | $this->debug = isset($config['debug']) ? $config['debug'] : true; 98 | 99 | $dsn = ''; 100 | if (in_array($config['driver'], ['', 'mysql', 'pgsql'])) { 101 | $dsn = $config['driver'] . ':host=' . str_replace(':' . $config['port'], '', $config['host']) . ';' 102 | . ($config['port'] !== '' ? 'port=' . $config['port'] . ';' : '') 103 | . 'dbname=' . $config['database']; 104 | } elseif ($config['driver'] === 'sqlite') { 105 | $dsn = 'sqlite:' . $config['database']; 106 | } elseif ($config['driver'] === 'oracle') { 107 | $dsn = 'oci:dbname=' . $config['host'] . '/' . $config['database']; 108 | } 109 | 110 | try { 111 | $this->pdo = new PDO($dsn, $config['username'], $config['password'], isset($config['options']) ? $config['options'] : null); 112 | $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'"); 113 | $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'"); 114 | $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); 115 | } catch (PDOException $e) { 116 | die('Cannot the connect to Database with PDO. ' . $e->getMessage()); 117 | } 118 | 119 | return $this->pdo; 120 | } 121 | 122 | /** 123 | * @param $table 124 | * 125 | * @return $this 126 | */ 127 | public function table($table) 128 | { 129 | if (is_array($table)) { 130 | $from = ''; 131 | foreach ($table as $key) { 132 | $from .= $this->prefix . $key . ', '; 133 | } 134 | $this->from = rtrim($from, ', '); 135 | } else { 136 | if (strpos($table, ',') > 0) { 137 | $tables = explode(',', $table); 138 | foreach ($tables as $key => &$value) { 139 | $value = $this->prefix . ltrim($value); 140 | } 141 | $this->from = implode(', ', $tables); 142 | } else { 143 | $this->from = $this->prefix . $table; 144 | } 145 | } 146 | 147 | return $this; 148 | } 149 | 150 | /** 151 | * @param array|string $fields 152 | * 153 | * @return $this 154 | */ 155 | public function select($fields) 156 | { 157 | $select = is_array($fields) ? implode(', ', $fields) : $fields; 158 | $this->optimizeSelect($select); 159 | 160 | return $this; 161 | } 162 | 163 | /** 164 | * @param string $field 165 | * @param string|null $name 166 | * 167 | * @return $this 168 | */ 169 | public function max($field, $name = null) 170 | { 171 | $column = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); 172 | $this->optimizeSelect($column); 173 | 174 | return $this; 175 | } 176 | 177 | /** 178 | * @param string $field 179 | * @param string|null $name 180 | * 181 | * @return $this 182 | */ 183 | public function min($field, $name = null) 184 | { 185 | $column = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); 186 | $this->optimizeSelect($column); 187 | 188 | return $this; 189 | } 190 | 191 | /** 192 | * @param string $field 193 | * @param string|null $name 194 | * 195 | * @return $this 196 | */ 197 | public function sum($field, $name = null) 198 | { 199 | $column = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); 200 | $this->optimizeSelect($column); 201 | 202 | return $this; 203 | } 204 | 205 | /** 206 | * @param string $field 207 | * @param string|null $name 208 | * 209 | * @return $this 210 | */ 211 | public function count($field, $name = null) 212 | { 213 | $column = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); 214 | $this->optimizeSelect($column); 215 | 216 | return $this; 217 | } 218 | 219 | /** 220 | * @param string $field 221 | * @param string|null $name 222 | * 223 | * @return $this 224 | */ 225 | public function avg($field, $name = null) 226 | { 227 | $column = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : ''); 228 | $this->optimizeSelect($column); 229 | 230 | return $this; 231 | } 232 | 233 | /** 234 | * @param string $table 235 | * @param string|null $field1 236 | * @param string|null $operator 237 | * @param string|null $field2 238 | * @param string $type 239 | * 240 | * @return $this 241 | */ 242 | public function join($table, $field1 = null, $operator = null, $field2 = null, $type = '') 243 | { 244 | $on = $field1; 245 | $table = $this->prefix . $table; 246 | 247 | if (!is_null($operator)) { 248 | $on = !in_array($operator, $this->operators) 249 | ? $field1 . ' = ' . $operator . (!is_null($field2) ? ' ' . $field2 : '') 250 | : $field1 . ' ' . $operator . ' ' . $field2; 251 | } 252 | 253 | $this->join = (is_null($this->join)) 254 | ? ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on 255 | : $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on; 256 | 257 | return $this; 258 | } 259 | 260 | /** 261 | * @param string $table 262 | * @param string $field1 263 | * @param string $operator 264 | * @param string $field2 265 | * 266 | * @return $this 267 | */ 268 | public function innerJoin($table, $field1, $operator = '', $field2 = '') 269 | { 270 | return $this->join($table, $field1, $operator, $field2, 'INNER '); 271 | } 272 | 273 | /** 274 | * @param string $table 275 | * @param string $field1 276 | * @param string $operator 277 | * @param string $field2 278 | * 279 | * @return $this 280 | */ 281 | public function leftJoin($table, $field1, $operator = '', $field2 = '') 282 | { 283 | return $this->join($table, $field1, $operator, $field2, 'LEFT '); 284 | } 285 | 286 | /** 287 | * @param string $table 288 | * @param string $field1 289 | * @param string $operator 290 | * @param string $field2 291 | * 292 | * @return $this 293 | */ 294 | public function rightJoin($table, $field1, $operator = '', $field2 = '') 295 | { 296 | return $this->join($table, $field1, $operator, $field2, 'RIGHT '); 297 | } 298 | 299 | /** 300 | * @param string $table 301 | * @param string $field1 302 | * @param string $operator 303 | * @param string $field2 304 | * 305 | * @return $this 306 | */ 307 | public function fullOuterJoin($table, $field1, $operator = '', $field2 = '') 308 | { 309 | return $this->join($table, $field1, $operator, $field2, 'FULL OUTER '); 310 | } 311 | 312 | /** 313 | * @param string $table 314 | * @param string $field1 315 | * @param string $operator 316 | * @param string $field2 317 | * 318 | * @return $this 319 | */ 320 | public function leftOuterJoin($table, $field1, $operator = '', $field2 = '') 321 | { 322 | return $this->join($table, $field1, $operator, $field2, 'LEFT OUTER '); 323 | } 324 | 325 | /** 326 | * @param string $table 327 | * @param string $field1 328 | * @param string $operator 329 | * @param string $field2 330 | * 331 | * @return $this 332 | */ 333 | public function rightOuterJoin($table, $field1, $operator = '', $field2 = '') 334 | { 335 | return $this->join($table, $field1, $operator, $field2, 'RIGHT OUTER '); 336 | } 337 | 338 | /** 339 | * @param array|string $where 340 | * @param string $operator 341 | * @param string $val 342 | * @param string $type 343 | * @param string $andOr 344 | * 345 | * @return $this 346 | */ 347 | public function where($where, $operator = null, $val = null, $type = '', $andOr = 'AND') 348 | { 349 | if (is_array($where) && !empty($where)) { 350 | $_where = []; 351 | foreach ($where as $column => $data) { 352 | $_where[] = $type . $column . '=' . $this->escape($data); 353 | } 354 | $where = implode(' ' . $andOr . ' ', $_where); 355 | } else { 356 | if (is_null($where) || empty($where)) { 357 | return $this; 358 | } 359 | 360 | if (is_array($operator)) { 361 | $params = explode('?', $where); 362 | $_where = ''; 363 | foreach ($params as $key => $value) { 364 | if (!empty($value)) { 365 | $_where .= $type . $value . (isset($operator[$key]) ? $this->escape($operator[$key]) : ''); 366 | } 367 | } 368 | $where = $_where; 369 | } elseif (!in_array($operator, $this->operators) || $operator == false) { 370 | $where = $type . $where . ' = ' . $this->escape($operator); 371 | } else { 372 | $where = $type . $where . ' ' . $operator . ' ' . $this->escape($val); 373 | } 374 | } 375 | 376 | if ($this->grouped) { 377 | $where = '(' . $where; 378 | $this->grouped = false; 379 | } 380 | 381 | $this->where = is_null($this->where) 382 | ? $where 383 | : $this->where . ' ' . $andOr . ' ' . $where; 384 | 385 | return $this; 386 | } 387 | 388 | /** 389 | * @param array|string $where 390 | * @param string|null $operator 391 | * @param string|null $val 392 | * 393 | * @return $this 394 | */ 395 | public function orWhere($where, $operator = null, $val = null) 396 | { 397 | return $this->where($where, $operator, $val, '', 'OR'); 398 | } 399 | 400 | /** 401 | * @param array|string $where 402 | * @param string|null $operator 403 | * @param string|null $val 404 | * 405 | * @return $this 406 | */ 407 | public function notWhere($where, $operator = null, $val = null) 408 | { 409 | return $this->where($where, $operator, $val, 'NOT ', 'AND'); 410 | } 411 | 412 | /** 413 | * @param array|string $where 414 | * @param string|null $operator 415 | * @param string|null $val 416 | * 417 | * @return $this 418 | */ 419 | public function orNotWhere($where, $operator = null, $val = null) 420 | { 421 | return $this->where($where, $operator, $val, 'NOT ', 'OR'); 422 | } 423 | 424 | /** 425 | * @param string $where 426 | * @param bool $not 427 | * 428 | * @return $this 429 | */ 430 | public function whereNull($where, $not = false) 431 | { 432 | $where = $where . ' IS ' . ($not ? 'NOT' : '') . ' NULL'; 433 | $this->where = is_null($this->where) ? $where : $this->where . ' ' . 'AND ' . $where; 434 | 435 | return $this; 436 | } 437 | 438 | /** 439 | * @param string $where 440 | * 441 | * @return $this 442 | */ 443 | public function whereNotNull($where) 444 | { 445 | return $this->whereNull($where, true); 446 | } 447 | 448 | /** 449 | * @param Closure $obj 450 | * 451 | * @return $this 452 | */ 453 | public function grouped(Closure $obj) 454 | { 455 | $this->grouped = true; 456 | call_user_func_array($obj, [$this]); 457 | $this->where .= ')'; 458 | 459 | return $this; 460 | } 461 | 462 | /** 463 | * @param string $field 464 | * @param array $keys 465 | * @param string $type 466 | * @param string $andOr 467 | * 468 | * @return $this 469 | */ 470 | public function in($field, array $keys, $type = '', $andOr = 'AND') 471 | { 472 | if (is_array($keys)) { 473 | $_keys = []; 474 | foreach ($keys as $k => $v) { 475 | $_keys[] = is_numeric($v) ? $v : $this->escape($v); 476 | } 477 | $where = $field . ' ' . $type . 'IN (' . implode(', ', $_keys) . ')'; 478 | 479 | if ($this->grouped) { 480 | $where = '(' . $where; 481 | $this->grouped = false; 482 | } 483 | 484 | $this->where = is_null($this->where) 485 | ? $where 486 | : $this->where . ' ' . $andOr . ' ' . $where; 487 | } 488 | 489 | return $this; 490 | } 491 | 492 | /** 493 | * @param string $field 494 | * @param array $keys 495 | * 496 | * @return $this 497 | */ 498 | public function notIn($field, array $keys) 499 | { 500 | return $this->in($field, $keys, 'NOT ', 'AND'); 501 | } 502 | 503 | /** 504 | * @param string $field 505 | * @param array $keys 506 | * 507 | * @return $this 508 | */ 509 | public function orIn($field, array $keys) 510 | { 511 | return $this->in($field, $keys, '', 'OR'); 512 | } 513 | 514 | /** 515 | * @param string $field 516 | * @param array $keys 517 | * 518 | * @return $this 519 | */ 520 | public function orNotIn($field, array $keys) 521 | { 522 | return $this->in($field, $keys, 'NOT ', 'OR'); 523 | } 524 | 525 | /** 526 | * @param string $field 527 | * @param string|integer $key 528 | * @param string $type 529 | * @param string $andOr 530 | * 531 | * @return $this 532 | */ 533 | public function findInSet($field, $key, $type = '', $andOr = 'AND') 534 | { 535 | $key = is_numeric($key) ? $key : $this->escape($key); 536 | $where = $type . 'FIND_IN_SET (' . $key . ', '.$field.')'; 537 | 538 | if ($this->grouped) { 539 | $where = '(' . $where; 540 | $this->grouped = false; 541 | } 542 | 543 | $this->where = is_null($this->where) 544 | ? $where 545 | : $this->where . ' ' . $andOr . ' ' . $where; 546 | 547 | return $this; 548 | } 549 | 550 | /** 551 | * @param string $field 552 | * @param string $key 553 | * 554 | * @return $this 555 | */ 556 | public function notFindInSet($field, $key) 557 | { 558 | return $this->findInSet($field, $key, 'NOT '); 559 | } 560 | 561 | /** 562 | * @param string $field 563 | * @param string $key 564 | * 565 | * @return $this 566 | */ 567 | public function orFindInSet($field, $key) 568 | { 569 | return $this->findInSet($field, $key, '', 'OR'); 570 | } 571 | 572 | /** 573 | * @param string $field 574 | * @param string $key 575 | * 576 | * @return $this 577 | */ 578 | public function orNotFindInSet($field, $key) 579 | { 580 | return $this->findInSet($field, $key, 'NOT ', 'OR'); 581 | } 582 | 583 | /** 584 | * @param string $field 585 | * @param string|int $value1 586 | * @param string|int $value2 587 | * @param string $type 588 | * @param string $andOr 589 | * 590 | * @return $this 591 | */ 592 | public function between($field, $value1, $value2, $type = '', $andOr = 'AND') 593 | { 594 | $where = '(' . $field . ' ' . $type . 'BETWEEN ' . ($this->escape($value1) . ' AND ' . $this->escape($value2)) . ')'; 595 | if ($this->grouped) { 596 | $where = '(' . $where; 597 | $this->grouped = false; 598 | } 599 | 600 | $this->where = is_null($this->where) 601 | ? $where 602 | : $this->where . ' ' . $andOr . ' ' . $where; 603 | 604 | return $this; 605 | } 606 | 607 | /** 608 | * @param string $field 609 | * @param string|int $value1 610 | * @param string|int $value2 611 | * 612 | * @return $this 613 | */ 614 | public function notBetween($field, $value1, $value2) 615 | { 616 | return $this->between($field, $value1, $value2, 'NOT ', 'AND'); 617 | } 618 | 619 | /** 620 | * @param string $field 621 | * @param string|int $value1 622 | * @param string|int $value2 623 | * 624 | * @return $this 625 | */ 626 | public function orBetween($field, $value1, $value2) 627 | { 628 | return $this->between($field, $value1, $value2, '', 'OR'); 629 | } 630 | 631 | /** 632 | * @param string $field 633 | * @param string|int $value1 634 | * @param string|int $value2 635 | * 636 | * @return $this 637 | */ 638 | public function orNotBetween($field, $value1, $value2) 639 | { 640 | return $this->between($field, $value1, $value2, 'NOT ', 'OR'); 641 | } 642 | 643 | /** 644 | * @param string $field 645 | * @param string $data 646 | * @param string $type 647 | * @param string $andOr 648 | * 649 | * @return $this 650 | */ 651 | public function like($field, $data, $type = '', $andOr = 'AND') 652 | { 653 | $like = $this->escape($data); 654 | $where = $field . ' ' . $type . 'LIKE ' . $like; 655 | 656 | if ($this->grouped) { 657 | $where = '(' . $where; 658 | $this->grouped = false; 659 | } 660 | 661 | $this->where = is_null($this->where) 662 | ? $where 663 | : $this->where . ' ' . $andOr . ' ' . $where; 664 | 665 | return $this; 666 | } 667 | 668 | /** 669 | * @param string $field 670 | * @param string $data 671 | * 672 | * @return $this 673 | */ 674 | public function orLike($field, $data) 675 | { 676 | return $this->like($field, $data, '', 'OR'); 677 | } 678 | 679 | /** 680 | * @param string $field 681 | * @param string $data 682 | * 683 | * @return $this 684 | */ 685 | public function notLike($field, $data) 686 | { 687 | return $this->like($field, $data, 'NOT ', 'AND'); 688 | } 689 | 690 | /** 691 | * @param string $field 692 | * @param string $data 693 | * 694 | * @return $this 695 | */ 696 | public function orNotLike($field, $data) 697 | { 698 | return $this->like($field, $data, 'NOT ', 'OR'); 699 | } 700 | 701 | /** 702 | * @param int $limit 703 | * @param int|null $limitEnd 704 | * 705 | * @return $this 706 | */ 707 | public function limit($limit, $limitEnd = null) 708 | { 709 | $this->limit = !is_null($limitEnd) 710 | ? $limit . ', ' . $limitEnd 711 | : $limit; 712 | 713 | return $this; 714 | } 715 | 716 | /** 717 | * @param int $offset 718 | * 719 | * @return $this 720 | */ 721 | public function offset($offset) 722 | { 723 | $this->offset = $offset; 724 | 725 | return $this; 726 | } 727 | 728 | /** 729 | * @param int $perPage 730 | * @param int $page 731 | * 732 | * @return $this 733 | */ 734 | public function pagination($perPage, $page) 735 | { 736 | $this->limit = $perPage; 737 | $this->offset = (($page > 0 ? $page : 1) - 1) * $perPage; 738 | 739 | return $this; 740 | } 741 | 742 | /** 743 | * @param string $orderBy 744 | * @param string|null $orderDir 745 | * 746 | * @return $this 747 | */ 748 | public function orderBy($orderBy, $orderDir = null) 749 | { 750 | if (!is_null($orderDir)) { 751 | $this->orderBy = $orderBy . ' ' . strtoupper($orderDir); 752 | } else { 753 | $this->orderBy = stristr($orderBy, ' ') || strtolower($orderBy) === 'rand()' 754 | ? $orderBy 755 | : $orderBy . ' ASC'; 756 | } 757 | 758 | return $this; 759 | } 760 | 761 | /** 762 | * @param string|array $groupBy 763 | * 764 | * @return $this 765 | */ 766 | public function groupBy($groupBy) 767 | { 768 | $this->groupBy = is_array($groupBy) ? implode(', ', $groupBy) : $groupBy; 769 | 770 | return $this; 771 | } 772 | 773 | /** 774 | * @param string $field 775 | * @param string|array|null $operator 776 | * @param string|null $val 777 | * 778 | * @return $this 779 | */ 780 | public function having($field, $operator = null, $val = null) 781 | { 782 | if (is_array($operator)) { 783 | $fields = explode('?', $field); 784 | $where = ''; 785 | foreach ($fields as $key => $value) { 786 | if (!empty($value)) { 787 | $where .= $value . (isset($operator[$key]) ? $this->escape($operator[$key]) : ''); 788 | } 789 | } 790 | $this->having = $where; 791 | } elseif (!in_array($operator, $this->operators)) { 792 | $this->having = $field . ' > ' . $this->escape($operator); 793 | } else { 794 | $this->having = $field . ' ' . $operator . ' ' . $this->escape($val); 795 | } 796 | 797 | return $this; 798 | } 799 | 800 | /** 801 | * @return int 802 | */ 803 | public function numRows() 804 | { 805 | return $this->numRows; 806 | } 807 | 808 | /** 809 | * @return int|null 810 | */ 811 | public function insertId() 812 | { 813 | return $this->insertId; 814 | } 815 | 816 | /** 817 | * @throw PDOException 818 | */ 819 | public function error() 820 | { 821 | if ($this->debug === true) { 822 | if (php_sapi_name() === 'cli') { 823 | die("Query: " . $this->query . PHP_EOL . "Error: " . $this->error . PHP_EOL); 824 | } 825 | 826 | $msg = '

Database Error

'; 827 | $msg .= '

Query: "' . $this->query . '"

'; 828 | $msg .= '

Error: ' . $this->error . '

'; 829 | die($msg); 830 | } 831 | 832 | throw new PDOException($this->error . '. (' . $this->query . ')'); 833 | } 834 | 835 | /** 836 | * @param string|bool $type 837 | * @param string|null $argument 838 | * 839 | * @return mixed 840 | */ 841 | public function get($type = null, $argument = null) 842 | { 843 | $this->limit = 1; 844 | $query = $this->getAll(true); 845 | return $type === true ? $query : $this->query($query, false, $type, $argument); 846 | } 847 | 848 | /** 849 | * @param bool|string $type 850 | * @param string|null $argument 851 | * 852 | * @return mixed 853 | */ 854 | public function getAll($type = null, $argument = null) 855 | { 856 | $query = 'SELECT ' . $this->select . ' FROM ' . $this->from; 857 | 858 | if (!is_null($this->join)) { 859 | $query .= $this->join; 860 | } 861 | 862 | if (!is_null($this->where)) { 863 | $query .= ' WHERE ' . $this->where; 864 | } 865 | 866 | if (!is_null($this->groupBy)) { 867 | $query .= ' GROUP BY ' . $this->groupBy; 868 | } 869 | 870 | if (!is_null($this->having)) { 871 | $query .= ' HAVING ' . $this->having; 872 | } 873 | 874 | if (!is_null($this->orderBy)) { 875 | $query .= ' ORDER BY ' . $this->orderBy; 876 | } 877 | 878 | if (!is_null($this->limit)) { 879 | $query .= ' LIMIT ' . $this->limit; 880 | } 881 | 882 | if (!is_null($this->offset)) { 883 | $query .= ' OFFSET ' . $this->offset; 884 | } 885 | 886 | return $type === true ? $query : $this->query($query, true, $type, $argument); 887 | } 888 | 889 | /** 890 | * @param array $data 891 | * @param bool $type 892 | * 893 | * @return bool|string|int|null 894 | */ 895 | public function insert(array $data, $type = false) 896 | { 897 | $query = 'INSERT INTO ' . $this->from; 898 | 899 | $values = array_values($data); 900 | if (isset($values[0]) && is_array($values[0])) { 901 | $column = implode(', ', array_keys($values[0])); 902 | $query .= ' (' . $column . ') VALUES '; 903 | foreach ($values as $value) { 904 | $val = implode(', ', array_map([$this, 'escape'], $value)); 905 | $query .= '(' . $val . '), '; 906 | } 907 | $query = trim($query, ', '); 908 | } else { 909 | $column = implode(', ', array_keys($data)); 910 | $val = implode(', ', array_map([$this, 'escape'], $data)); 911 | $query .= ' (' . $column . ') VALUES (' . $val . ')'; 912 | } 913 | 914 | if ($type === true) { 915 | return $query; 916 | } 917 | 918 | if ($this->query($query, false)) { 919 | $this->insertId = $this->pdo->lastInsertId(); 920 | return $this->insertId(); 921 | } 922 | 923 | return false; 924 | } 925 | 926 | /** 927 | * @param array $data 928 | * @param bool $type 929 | * 930 | * @return mixed|string 931 | */ 932 | public function update(array $data, $type = false) 933 | { 934 | $query = 'UPDATE ' . $this->from . ' SET '; 935 | $values = []; 936 | 937 | foreach ($data as $column => $val) { 938 | $values[] = $column . '=' . $this->escape($val); 939 | } 940 | $query .= implode(',', $values); 941 | 942 | if (!is_null($this->where)) { 943 | $query .= ' WHERE ' . $this->where; 944 | } 945 | 946 | if (!is_null($this->orderBy)) { 947 | $query .= ' ORDER BY ' . $this->orderBy; 948 | } 949 | 950 | if (!is_null($this->limit)) { 951 | $query .= ' LIMIT ' . $this->limit; 952 | } 953 | 954 | return $type === true ? $query : $this->query($query, false); 955 | } 956 | 957 | /** 958 | * @param bool $type 959 | * 960 | * @return mixed|string 961 | */ 962 | public function delete($type = false) 963 | { 964 | $query = 'DELETE FROM ' . $this->from; 965 | 966 | if (!is_null($this->where)) { 967 | $query .= ' WHERE ' . $this->where; 968 | } 969 | 970 | if (!is_null($this->orderBy)) { 971 | $query .= ' ORDER BY ' . $this->orderBy; 972 | } 973 | 974 | if (!is_null($this->limit)) { 975 | $query .= ' LIMIT ' . $this->limit; 976 | } 977 | 978 | if ($query === 'DELETE FROM ' . $this->from) { 979 | $query = 'TRUNCATE TABLE ' . $this->from; 980 | } 981 | 982 | return $type === true ? $query : $this->query($query, false); 983 | } 984 | 985 | /** 986 | * @return mixed 987 | */ 988 | public function analyze() 989 | { 990 | return $this->query('ANALYZE TABLE ' . $this->from, false); 991 | } 992 | 993 | /** 994 | * @return mixed 995 | */ 996 | public function check() 997 | { 998 | return $this->query('CHECK TABLE ' . $this->from, false); 999 | } 1000 | 1001 | /** 1002 | * @return mixed 1003 | */ 1004 | public function checksum() 1005 | { 1006 | return $this->query('CHECKSUM TABLE ' . $this->from, false); 1007 | } 1008 | 1009 | /** 1010 | * @return mixed 1011 | */ 1012 | public function optimize() 1013 | { 1014 | return $this->query('OPTIMIZE TABLE ' . $this->from, false); 1015 | } 1016 | 1017 | /** 1018 | * @return mixed 1019 | */ 1020 | public function repair() 1021 | { 1022 | return $this->query('REPAIR TABLE ' . $this->from, false); 1023 | } 1024 | 1025 | /** 1026 | * @return bool 1027 | */ 1028 | public function transaction() 1029 | { 1030 | if (!$this->transactionCount++) { 1031 | return $this->pdo->beginTransaction(); 1032 | } 1033 | 1034 | $this->pdo->exec('SAVEPOINT trans' . $this->transactionCount); 1035 | return $this->transactionCount >= 0; 1036 | } 1037 | 1038 | /** 1039 | * @return bool 1040 | */ 1041 | public function commit() 1042 | { 1043 | if (!--$this->transactionCount) { 1044 | return $this->pdo->commit(); 1045 | } 1046 | 1047 | return $this->transactionCount >= 0; 1048 | } 1049 | 1050 | /** 1051 | * @return bool 1052 | */ 1053 | public function rollBack() 1054 | { 1055 | if (--$this->transactionCount) { 1056 | $this->pdo->exec('ROLLBACK TO trans' . ($this->transactionCount + 1)); 1057 | return true; 1058 | } 1059 | 1060 | return $this->pdo->rollBack(); 1061 | } 1062 | 1063 | /** 1064 | * @return mixed 1065 | */ 1066 | public function exec() 1067 | { 1068 | if (is_null($this->query)) { 1069 | return null; 1070 | } 1071 | 1072 | $query = $this->pdo->exec($this->query); 1073 | if ($query === false) { 1074 | $this->error = $this->pdo->errorInfo()[2]; 1075 | $this->error(); 1076 | } 1077 | 1078 | return $query; 1079 | } 1080 | 1081 | /** 1082 | * @param string $type 1083 | * @param string $argument 1084 | * @param bool $all 1085 | * 1086 | * @return mixed 1087 | */ 1088 | public function fetch($type = null, $argument = null, $all = false) 1089 | { 1090 | if (is_null($this->query)) { 1091 | return null; 1092 | } 1093 | 1094 | $query = $this->pdo->query($this->query); 1095 | if (!$query) { 1096 | $this->error = $this->pdo->errorInfo()[2]; 1097 | $this->error(); 1098 | } 1099 | 1100 | $type = $this->getFetchType($type); 1101 | if ($type === PDO::FETCH_CLASS) { 1102 | $query->setFetchMode($type, $argument); 1103 | } else { 1104 | $query->setFetchMode($type); 1105 | } 1106 | 1107 | $result = $all ? $query->fetchAll() : $query->fetch(); 1108 | $this->numRows = is_array($result) ? count($result) : 1; 1109 | return $result; 1110 | } 1111 | 1112 | /** 1113 | * @param string $type 1114 | * @param string $argument 1115 | * 1116 | * @return mixed 1117 | */ 1118 | public function fetchAll($type = null, $argument = null) 1119 | { 1120 | return $this->fetch($type, $argument, true); 1121 | } 1122 | 1123 | /** 1124 | * @param string $query 1125 | * @param array|bool $all 1126 | * @param string $type 1127 | * @param string $argument 1128 | * 1129 | * @return $this|mixed 1130 | */ 1131 | public function query($query, $all = true, $type = null, $argument = null) 1132 | { 1133 | $this->reset(); 1134 | 1135 | if (is_array($all) || func_num_args() === 1) { 1136 | $params = explode('?', $query); 1137 | $newQuery = ''; 1138 | foreach ($params as $key => $value) { 1139 | if (!empty($value)) { 1140 | $newQuery .= $value . (isset($all[$key]) ? $this->escape($all[$key]) : ''); 1141 | } 1142 | } 1143 | $this->query = $newQuery; 1144 | return $this; 1145 | } 1146 | 1147 | $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query)); 1148 | $str = false; 1149 | foreach (['select', 'optimize', 'check', 'repair', 'checksum', 'analyze'] as $value) { 1150 | if (stripos($this->query, $value) === 0) { 1151 | $str = true; 1152 | break; 1153 | } 1154 | } 1155 | 1156 | $type = $this->getFetchType($type); 1157 | $cache = false; 1158 | if (!is_null($this->cache) && $type !== PDO::FETCH_CLASS) { 1159 | $cache = $this->cache->getCache($this->query, $type === PDO::FETCH_ASSOC); 1160 | } 1161 | 1162 | if (!$cache && $str) { 1163 | $sql = $this->pdo->query($this->query); 1164 | if ($sql) { 1165 | $this->numRows = $sql->rowCount(); 1166 | if ($this->numRows > 0) { 1167 | if ($type === PDO::FETCH_CLASS) { 1168 | $sql->setFetchMode($type, $argument); 1169 | } else { 1170 | $sql->setFetchMode($type); 1171 | } 1172 | $this->result = $all ? $sql->fetchAll() : $sql->fetch(); 1173 | } 1174 | 1175 | if (!is_null($this->cache) && $type !== PDO::FETCH_CLASS) { 1176 | $this->cache->setCache($this->query, $this->result); 1177 | } 1178 | $this->cache = null; 1179 | } else { 1180 | $this->cache = null; 1181 | $this->error = $this->pdo->errorInfo()[2]; 1182 | $this->error(); 1183 | } 1184 | } elseif ((!$cache && !$str) || ($cache && !$str)) { 1185 | $this->cache = null; 1186 | $this->result = $this->pdo->exec($this->query); 1187 | 1188 | if ($this->result === false) { 1189 | $this->error = $this->pdo->errorInfo()[2]; 1190 | $this->error(); 1191 | } 1192 | } else { 1193 | $this->cache = null; 1194 | $this->result = $cache; 1195 | $this->numRows = is_array($this->result) ? count($this->result) : ($this->result === '' ? 0 : 1); 1196 | } 1197 | 1198 | $this->queryCount++; 1199 | return $this->result; 1200 | } 1201 | 1202 | /** 1203 | * @param $data 1204 | * 1205 | * @return string 1206 | */ 1207 | public function escape($data) 1208 | { 1209 | return $data === null ? 'NULL' : ( 1210 | is_int($data) || is_float($data) ? $data : $this->pdo->quote($data) 1211 | ); 1212 | } 1213 | 1214 | /** 1215 | * @param $time 1216 | * 1217 | * @return $this 1218 | */ 1219 | public function cache($time) 1220 | { 1221 | $this->cache = new Cache($this->cacheDir, $time); 1222 | 1223 | return $this; 1224 | } 1225 | 1226 | /** 1227 | * @return int 1228 | */ 1229 | public function queryCount() 1230 | { 1231 | return $this->queryCount; 1232 | } 1233 | 1234 | /** 1235 | * @return string|null 1236 | */ 1237 | public function getQuery() 1238 | { 1239 | return $this->query; 1240 | } 1241 | 1242 | /** 1243 | * @return void 1244 | */ 1245 | public function __destruct() 1246 | { 1247 | $this->pdo = null; 1248 | } 1249 | 1250 | /** 1251 | * @return void 1252 | */ 1253 | protected function reset() 1254 | { 1255 | $this->select = '*'; 1256 | $this->from = null; 1257 | $this->where = null; 1258 | $this->limit = null; 1259 | $this->offset = null; 1260 | $this->orderBy = null; 1261 | $this->groupBy = null; 1262 | $this->having = null; 1263 | $this->join = null; 1264 | $this->grouped = false; 1265 | $this->numRows = 0; 1266 | $this->insertId = null; 1267 | $this->query = null; 1268 | $this->error = null; 1269 | $this->result = []; 1270 | $this->transactionCount = 0; 1271 | } 1272 | 1273 | /** 1274 | * @param $type 1275 | * 1276 | * @return int 1277 | */ 1278 | protected function getFetchType($type) 1279 | { 1280 | return $type === 'class' 1281 | ? PDO::FETCH_CLASS 1282 | : ($type === 'array' 1283 | ? PDO::FETCH_ASSOC 1284 | : PDO::FETCH_OBJ); 1285 | } 1286 | 1287 | /** 1288 | * Optimize Selected fields for the query 1289 | * 1290 | * @param string $fields 1291 | * 1292 | * @return void 1293 | */ 1294 | private function optimizeSelect($fields) 1295 | { 1296 | $this->select = $this->select === '*' 1297 | ? $fields 1298 | : $this->select . ', ' . $fields; 1299 | } 1300 | } 1301 | -------------------------------------------------------------------------------- /src/PdoxInterface.php: -------------------------------------------------------------------------------- 1 | 'localhost', 7 | 'driver' => 'mysql', 8 | 'database' => 'test', 9 | 'username' => 'root', 10 | 'password' => '', 11 | 'charset' => 'utf8', 12 | 'collation' => 'utf8_general_ci', 13 | 'prefix' => '' 14 | ]; 15 | 16 | $db = new \Buki\Pdox($config); 17 | 18 | 19 | $query = $db->table('pages')->where('year', 2014)->where('status', 0)->delete(); 20 | 21 | if($query) 22 | { 23 | echo 'Record(s) deleted!'; 24 | } -------------------------------------------------------------------------------- /tests/InsertTest.php: -------------------------------------------------------------------------------- 1 | 'localhost', 7 | 'driver' => 'mysql', 8 | 'database' => 'test', 9 | 'username' => 'root', 10 | 'password' => '', 11 | 'charset' => 'utf8', 12 | 'collation' => 'utf8_general_ci', 13 | 'prefix' => '' 14 | ]; 15 | 16 | $db = new \Buki\Pdox($config); 17 | 18 | $data = [ 19 | 'name' => 'Burak', 20 | 'surname' => 'Demirtaş', 21 | 'age' => '24', 22 | 'country' => 'Turkey', 23 | 'city' => 'Ankara', 24 | 'status' => 1 25 | ]; 26 | 27 | $query = $db->table('users')->insert($data); 28 | 29 | if($query) 30 | { 31 | echo 'Record added!
' . 32 | 'InsertID: ' . $db->insertId(); 33 | } -------------------------------------------------------------------------------- /tests/JoinTest.php: -------------------------------------------------------------------------------- 1 | 'localhost', 7 | 'driver' => 'mysql', 8 | 'database' => 'test', 9 | 'username' => 'root', 10 | 'password' => '', 11 | 'charset' => 'utf8', 12 | 'collation' => 'utf8_general_ci', 13 | 'prefix' => '' 14 | ]; 15 | 16 | $db = new \Buki\Pdox($config); 17 | 18 | $records = $db->table('authors') 19 | ->select('authors.name, articles.title, articles.slug') 20 | ->join('articles', 'users.id', 'articles.user_id') 21 | ->where('users.status', 1) 22 | ->where('articles.status', 1) 23 | ->orderBy('articles.created_at', 'desc') 24 | ->limit(10) 25 | ->getAll(); 26 | 27 | foreach($records as $record) 28 | { 29 | echo $record->name . '
' . 30 | $record->title . '
' . 31 | $record->slug; 32 | } -------------------------------------------------------------------------------- /tests/SelectTest.php: -------------------------------------------------------------------------------- 1 | 'localhost', 7 | 'driver' => 'mysql', 8 | 'database' => 'test', 9 | 'username' => 'root', 10 | 'password' => '', 11 | 'charset' => 'utf8', 12 | 'collation' => 'utf8_general_ci', 13 | 'prefix' => '' 14 | ]; 15 | 16 | $db = new \Buki\Pdox($config); 17 | 18 | $records = $db->table('users') 19 | ->select('name, surname, age') 20 | ->where('age', '>', 18) 21 | ->orderBy('id', 'desc') 22 | ->limit(20) 23 | ->getAll(); 24 | 25 | foreach($records as $record) 26 | { 27 | echo $record->name . '
' . 28 | $record->surname . '
' . 29 | $record->age; 30 | } -------------------------------------------------------------------------------- /tests/UpdateTest.php: -------------------------------------------------------------------------------- 1 | 'localhost', 7 | 'driver' => 'mysql', 8 | 'database' => 'test', 9 | 'username' => 'root', 10 | 'password' => '', 11 | 'charset' => 'utf8', 12 | 'collation' => 'utf8_general_ci', 13 | 'prefix' => '' 14 | ]; 15 | 16 | $db = new \Buki\Pdox($config); 17 | 18 | $data = [ 19 | 'username' => 'new-user-name', 20 | 'password' => md5('new-password'), 21 | 'status' => 1 22 | ]; 23 | 24 | $query = $db->table('users')->where('id', 17)->update($data); 25 | 26 | if($query) 27 | { 28 | echo 'Record updated!'; 29 | } --------------------------------------------------------------------------------