|
1 | | -# PD\SQL |
| 1 | +# MySQLPool |
2 | 2 |
|
3 | | -> PD\SQL is a PDO-based SQL query builder that provides an elegant and secure way to construct and execute database queries in PHP. |
| 3 | +> A MySQL connection pool solution designed specifically for PHP, featuring intuitive chaining syntax that integrates query builder capabilities with read-write separation. |
4 | 4 |
|
5 | | - |
6 | | -<br> |
7 | | - |
8 | | - |
| 5 | +[](https://packagist.org/packages/pardnchiu/mysql-pool) |
9 | 6 |
|
10 | 7 | ## Features |
11 | | -- Fluent interface for building SQL queries |
12 | | -- Safe parameter binding to prevent SQL injection |
13 | | -- Support for complex JOIN operations (INNER, LEFT, RIGHT) |
14 | | -- Dynamic WHERE clause construction |
15 | | -- Ordering and pagination support |
16 | | -- Transaction handling |
17 | | -- Query execution time monitoring |
18 | | -- Environment-based configuration |
19 | | -- Automatic connection management |
20 | | - |
21 | | -## functions |
22 | | - |
23 | | -- Table selection with `table()` |
24 | | -- Custom field selection with `select()` |
25 | | -- Conditional filtering with `where()` |
26 | | -- Join operations with `join()`, `left_join()`, `right_join()` |
27 | | -- Result ordering with `order_by()` |
28 | | -- Pagination with `limit()` and `offset()` |
29 | | -- Record creation with `insertGetId()` |
30 | | -- Record updates with `update()` |
31 | | -- Total row count with `total()` |
32 | | -- Raw query execution with `query()` for complex custom queries |
33 | | - |
34 | | -## How to Use |
35 | | - |
36 | | -### Install |
| 8 | + |
| 9 | +- **Dual Connection Pools**: Separate read and write database connections |
| 10 | +- **Query Builder**: Fluent interface for building SQL queries |
| 11 | +- **Environment Configuration**: Easy setup using environment variables |
| 12 | +- **Connection Management**: Automatic connection pooling and cleanup |
| 13 | +- **Slow Query Detection**: Automatic logging of queries taking over 20ms |
| 14 | +- **JOIN Operations**: Support for INNER, LEFT, and RIGHT joins |
| 15 | +- **CRUD Operations**: Complete Create, Read, Update, Delete functionality |
| 16 | +- **UPSERT Support**: Insert or update on duplicate key |
| 17 | + |
| 18 | +## Installation |
37 | 19 |
|
38 | 20 | ```shell |
39 | | -composer require pardnchiu/sql |
| 21 | +composer require pardnchiu/mysql-pool |
| 22 | +``` |
| 23 | + |
| 24 | +## Environment Configuration |
| 25 | + |
| 26 | +Set up your database connections using environment variables: |
| 27 | + |
| 28 | +### Read Database (Optional) |
| 29 | +```env |
| 30 | +DB_READ_HOST=localhost |
| 31 | +DB_READ_PORT=3306 |
| 32 | +DB_READ_USER=read_user |
| 33 | +DB_READ_PASSWORD=read_password |
| 34 | +DB_READ_DATABASE=your_database |
| 35 | +DB_READ_CHARSET=utf8mb4 |
| 36 | +DB_READ_CONNECTION=8 |
40 | 37 | ``` |
41 | 38 |
|
| 39 | +### Write Database (Required for write operations) |
| 40 | +```env |
| 41 | +DB_WRITE_HOST=localhost |
| 42 | +DB_WRITE_PORT=3306 |
| 43 | +DB_WRITE_USER=write_user |
| 44 | +DB_WRITE_PASSWORD=write_password |
| 45 | +DB_WRITE_DATABASE=your_database |
| 46 | +DB_WRITE_CHARSET=utf8mb4 |
| 47 | +DB_WRITE_CONNECTION=4 |
| 48 | +``` |
| 49 | + |
| 50 | +## Quick Start |
| 51 | + |
42 | 52 | ```php |
43 | 53 | <?php |
44 | 54 |
|
45 | | -use PD\SQL; |
| 55 | +use pardnchiu\MySQLPool as SQL; |
46 | 56 |
|
47 | | -$result_user_0 = SQL::table('users') |
| 57 | +// simple query |
| 58 | +$result_user = SQL::table("users") |
48 | 59 | ->where("status", "active") |
49 | | - ->where("age", ">", 18) |
50 | 60 | ->get(); |
| 61 | +``` |
51 | 62 |
|
52 | | -$result_order = SQL::table("orders") |
53 | | - ->select("orders.*", "users.name") |
54 | | - ->join("users", "orders.user_id", "users.id") |
55 | | - ->where("orders.status", "pending") |
56 | | - ->get(); |
| 63 | +## API Reference |
57 | 64 |
|
58 | | -$result_product = SQL::table("products") |
59 | | - ->total() |
60 | | - ->limit(10) |
61 | | - ->offset(0) |
62 | | - ->order_by("created_at", "DESC") |
63 | | - ->get(); |
| 65 | +### Query Builder Methods |
| 66 | + |
| 67 | +#### `table($table, $target = "READ" | "WRITE")` |
| 68 | +Set the target table and target pool |
| 69 | + |
| 70 | +```php |
| 71 | +<?php |
64 | 72 |
|
65 | | -$result_user_1 = SQL::query( |
66 | | - "SELECT * FROM users WHERE status = ? AND role = ?", |
67 | | - ["active", "admin"] |
68 | | -); |
| 73 | +SQL::table("users") // use read pool |
| 74 | +SQL::table("users", "WRITE") // use write pool |
| 75 | +``` |
| 76 | + |
| 77 | +#### `select($fields)` |
| 78 | +Specify columns to select. |
| 79 | + |
| 80 | +```php |
| 81 | +<?php |
| 82 | + |
| 83 | +SQL::table("users") |
| 84 | + ->select("id", "name", "email"); |
| 85 | +SQL::table("users") |
| 86 | + ->select("COUNT(*) as total"); |
| 87 | +``` |
| 88 | + |
| 89 | +### `where($column, $operator, $value)` |
| 90 | +Add WHERE conditions. |
| 91 | + |
| 92 | +```php |
| 93 | +<?php |
| 94 | + |
| 95 | +// Basic where |
| 96 | +SQL::table("users") |
| 97 | + ->where("id", 1); |
| 98 | +SQL::table("users") |
| 99 | + ->where("age", ">", 18); |
| 100 | + |
| 101 | +// LIKE operator (automatically adds % wildcards) |
| 102 | +SQL::table("users") |
| 103 | + ->where("name", "LIKE", "John"); |
| 104 | +``` |
| 105 | + |
| 106 | +### JOIN Operations |
| 107 | + |
| 108 | +```php |
| 109 | +<?php |
| 110 | + |
| 111 | +// INNER JOIN |
| 112 | +SQL::table("users") |
| 113 | + ->join("profiles", "users.id", "profiles.user_id"); |
| 114 | + |
| 115 | +// LEFT JOIN |
| 116 | +SQL::table("users") |
| 117 | + ->left_join("orders", "users.id", "orders.user_id"); |
| 118 | + |
| 119 | +// RIGHT JOIN with custom operator |
| 120 | +SQL::table("users") |
| 121 | + ->right_join("posts", "users.id", "!=", "posts.author_id"); |
| 122 | +``` |
| 123 | + |
| 124 | +### Error Handling |
| 125 | +```php |
| 126 | +<?php |
69 | 127 |
|
70 | 128 | try { |
71 | 129 | $result = SQL::table("users") |
|
0 commit comments