Kotchasan Framework Documentation
SqlBuilder Classes
SqlBuilder Classes
โฟลเดอร์ Kotchasan/QueryBuilder/SqlBuilder/ มี SQL builders สำหรับแต่ละ database
โครงสร้าง
QueryBuilder/SqlBuilder/
├── SqlBuilderInterface.php # Interface
├── AbstractSqlBuilder.php # Base class
├── MySqlSqlBuilder.php # MySQL
├── PostgreSqlSqlBuilder.php # PostgreSQL
├── SqliteSqlBuilder.php # SQLite
└── SqlServerSqlBuilder.php # SQL ServerSqlBuilderInterface
Interface สำหรับ SQL builders
interface SqlBuilderInterface
{
public function buildSelect(array $columns, string $table, array $joins, ...): string;
public function buildInsert(string $table, array $columns, array $values): string;
public function buildUpdate(string $table, array $sets, array $where): string;
public function buildDelete(string $table, array $where): string;
public function quoteIdentifier(string $identifier): string;
public function quoteValue($value): string;
}Database Differences
Identifier Quoting
| Database | Quote Character | Example |
|---|---|---|
| MySQL | ` (backtick) | `table`.`column` |
|
| PostgreSQL | " (double quote) |
"table"."column" |
| SQLite | " (double quote) |
"table"."column" |
| SQL Server | [] (brackets) |
[table].[column] |
LIMIT/OFFSET
| Database | Syntax |
|---|---|
| MySQL | LIMIT 10 OFFSET 20 |
| PostgreSQL | LIMIT 10 OFFSET 20 |
| SQLite | LIMIT 10 OFFSET 20 |
| SQL Server | OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY |
Boolean Values
| Database | True | False |
|---|---|---|
| MySQL | 1 |
0 |
| PostgreSQL | TRUE |
FALSE |
| SQLite | 1 |
0 |
| SQL Server | 1 |
0 |
Auto Increment
| Database | Syntax |
|---|---|
| MySQL | AUTO_INCREMENT |
| PostgreSQL | SERIAL or GENERATED ALWAYS AS IDENTITY |
| SQLite | AUTOINCREMENT |
| SQL Server | IDENTITY(1,1) |
AbstractSqlBuilder
Base class พร้อม default implementations
abstract class AbstractSqlBuilder implements SqlBuilderInterface
{
protected string $quoteChar = '"';
public function quoteIdentifier(string $identifier): string
{
return $this->quoteChar . $identifier . $this->quoteChar;
}
// Default implementations for common SQL patterns
public function buildSelect(...): string { ... }
public function buildInsert(...): string { ... }
public function buildUpdate(...): string { ... }
public function buildDelete(...): string { ... }
}MySqlSqlBuilder
MySQL specific:
class MySqlSqlBuilder extends AbstractSqlBuilder
{
protected string $quoteChar = '`';
// MySQL-specific implementations
public function buildInsertIgnore(...): string { ... }
public function buildReplace(...): string { ... }
public function buildOnDuplicateKey(...): string { ... }
}การใช้งาน
Framework เลือก SqlBuilder ให้อัตโนมัติ:
// ไม่ต้องเรียกใช้โดยตรง
// QueryBuilder ใช้ SqlBuilder ภายใน
$query = Database::create()->select('*')
->from('user')
->where([['id', 1]])
->execute();
// SQL ที่สร้างจะถูกต้องตาม database driver
// MySQL: SELECT * FROM `user` WHERE `id` = 1
// PostgreSQL: SELECT * FROM "user" WHERE "id" = 1คลาสที่เกี่ยวข้อง
- QueryBuilder - ใช้ SqlBuilder ภายใน
- Functions - SQL functions
- Connection - Database connections
เพิ่มเติม
ลิงก์ไปยังหัวข้ออื่นที่เกี่ยวข้องในเอกสารนี้