Kotchasan Framework Documentation

Kotchasan Framework Documentation

SqlBuilder Classes

EN 05 Feb 2026 06:23

SqlBuilder Classes

The Kotchasan/QueryBuilder/SqlBuilder/ folder contains SQL builders for each database.

Structure

QueryBuilder/SqlBuilder/
├── SqlBuilderInterface.php      # Interface
├── AbstractSqlBuilder.php       # Base class
├── MySqlSqlBuilder.php          # MySQL
├── PostgreSqlSqlBuilder.php     # PostgreSQL
├── SqliteSqlBuilder.php         # SQLite
└── SqlServerSqlBuilder.php      # SQL Server

SqlBuilderInterface

Interface for 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 with 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 { ... }
}

Usage

Framework automatically selects SqlBuilder:

// No need to call directly
// QueryBuilder uses SqlBuilder internally

$query = Database::create()->select('*')
    ->from('user')
    ->where([['id', 1]])
    ->execute();

// Generated SQL will be correct for the database driver
// MySQL: SELECT * FROM `user` WHERE `id` = 1
// PostgreSQL: SELECT * FROM "user" WHERE "id" = 1

Additional Resources

Related guides and references within this documentation set.