Skip to content

Commit 24df59f

Browse files
committed
chore: update README
1 parent 3e9077b commit 24df59f

File tree

3 files changed

+173
-119
lines changed

3 files changed

+173
-119
lines changed

README.md

Lines changed: 109 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,129 @@
1-
# PD\SQL
1+
# MySQLPool
22

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.
44
5-
![tag](https://img.shields.io/badge/tag-PHP%20Library-bb4444)
6-
![size](https://img.shields.io/github/size/pardnchiu/PHP-SQL/src/SQL.php)<br>
7-
![version](https://img.shields.io/packagist/v/pardnchiu/sql)
8-
![download](https://img.shields.io/packagist/dm/pardnchiu/sql)
5+
[![packagist](https://img.shields.io/packagist/v/pardnchiu/mysql-pool)](https://packagist.org/packages/pardnchiu/mysql-pool)
96

107
## 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
3719

3820
```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
4037
```
4138

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+
4252
```php
4353
<?php
4454

45-
use PD\SQL;
55+
use pardnchiu\MySQLPool as SQL;
4656

47-
$result_user_0 = SQL::table('users')
57+
// simple query
58+
$result_user = SQL::table("users")
4859
->where("status", "active")
49-
->where("age", ">", 18)
5060
->get();
61+
```
5162

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
5764

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
6472

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
69127

70128
try {
71129
$result = SQL::table("users")

composer.json

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,64 @@
11
{
2-
"name": "pardnchiu/php-mysql-pool",
3-
"description": "PD\\SQL is a PDO-based SQL query builder that provides an elegant and secure way to construct and execute database queries in PHP.",
4-
"type": "library",
5-
"keywords": [
6-
"php",
7-
"sql",
8-
"mysql",
9-
"database",
10-
"query-builder",
11-
"pdo",
12-
"orm",
13-
"mysql-pdo",
14-
"pardnchiu",
15-
"邱敬幃"
16-
],
17-
"homepage": "https://pardn.io",
18-
"license": "MIT",
19-
"authors": [
20-
{
21-
"name": "邱敬幃 Pardn Chiu",
22-
"email": "dev@pardn.io",
23-
"homepage": "https://pardn.io",
24-
"role": "Developer"
25-
}
26-
],
27-
"support": {
28-
"email": "dev@pardn.io",
29-
"issues": "https://github.com/pardnchiu/php-mysql-pool/issues",
30-
"source": "https://github.com/pardnchiu/php-mysql-pool",
31-
"docs": "https://github.com/pardnchiu/php-mysql-pool/blob/main/README.md"
32-
},
33-
"minimum-stability": "stable",
34-
"prefer-stable": true,
35-
"require": {
36-
"php": ">=8.0"
37-
},
38-
"require-dev": {},
39-
"autoload": {
40-
"psr-4": {
41-
"PD\\": "src/"
42-
},
43-
"exclude-from-classmap": [
44-
".gitignore",
45-
"*.md",
46-
"composer.json"
47-
]
48-
},
49-
"autoload-dev": {
50-
"psr-4": {}
51-
},
52-
"scripts": {},
53-
"extra": {
54-
"branch-alias": {}
55-
},
56-
"config": {
57-
"sort-packages": true,
58-
"optimize-autoloader": true,
59-
"preferred-install": "dist"
60-
},
61-
"archive": {
62-
"exclude": [
63-
"/.git",
64-
"/*.md",
65-
"/.gitignore"
66-
]
2+
"name": "pardnchiu/mysql-pool",
3+
"description": "A MySQL connection pool solution designed specifically for PHP, featuring intuitive chaining syntax that integrates query builder capabilities with read-write separation.",
4+
"type": "library",
5+
"keywords": [
6+
"php",
7+
"PDO",
8+
"chain-syntax",
9+
"mysql",
10+
"邱敬幃",
11+
"pardnchiu"
12+
],
13+
"homepage": "https://pardn.io",
14+
"license": "MIT",
15+
"authors": [
16+
{
17+
"name": "邱敬幃 Pardn Chiu",
18+
"email": "dev@pardn.io",
19+
"homepage": "https://pardn.io",
20+
"role": "Developer"
6721
}
68-
}
22+
],
23+
"support": {
24+
"email": "dev@pardn.io",
25+
"issues": "https://github.com/pardnchiu/php-mysql-pool/issues",
26+
"source": "https://github.com/pardnchiu/php-mysql-pool",
27+
"docs": "https://github.com/pardnchiu/php-mysql-pool/blob/main/README.md"
28+
},
29+
"minimum-stability": "stable",
30+
"prefer-stable": true,
31+
"require": {
32+
"php": ">=8.0"
33+
},
34+
"require-dev": {},
35+
"autoload": {
36+
"psr-4": {
37+
"pardnchiu\\": "src/"
38+
},
39+
"exclude-from-classmap": [
40+
".gitignore",
41+
"*.md",
42+
"composer.json"
43+
]
44+
},
45+
"autoload-dev": {
46+
"psr-4": {}
47+
},
48+
"scripts": {},
49+
"extra": {
50+
"branch-alias": {}
51+
},
52+
"config": {
53+
"sort-packages": true,
54+
"optimize-autoloader": true,
55+
"preferred-install": "dist"
56+
},
57+
"archive": {
58+
"exclude": [
59+
"/.git",
60+
"/*.md",
61+
"/.gitignore"
62+
]
63+
}
64+
}

src/SQL.php renamed to src/MySQLPool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace PD;
3+
namespace pardnchiu;
44

5-
class SQL
5+
class MySQLPool
66
{
77
private static $client;
88
private static $table;

0 commit comments

Comments
 (0)