Kotchasan

Driver extends Query
in package

Kotchasan Database driver Class (base class)

Tags
see
https://www.kotchasan.com/

Table of Contents

$cache  : DbCache
$cache_item  : CacheItem
$cfg  : object
Config class instance.
$connection  : object
$db  : Driver
Database connection.
$debugger  : int
1 แสดง Query ข้อความอย่างเดียวสำหรับใส่ลงใน console 2 แสดง Query ด้านล่างจอภาพ
$error_message  : string
$query_count  : int
$request  : Request
Server request class instance.
$result_id  : resource|object
$sqls  : array<string|int, mixed>
ตัวแปรเก็บคำสั่ง SQL
__construct()  : mixed
Class constructor
cacheGetAction()  : int
Get the cache action.
cacheOn()  : static
Enable caching.
cacheSave()  : bool
Save cache data.
close()  : mixed
Close the database connection.
connection()  : resource
Get the current database connection.
createQuery()  : QueryBuilder
Create a new query builder instance.
customQuery()  : array<string|int, mixed>
Process an SQL query command to retrieve data.
databaseExists()  : bool
Checks if a database exists.
db()  : Driver
Get the database connection.
debug()  : mixed
คำสั่งสำหรับแสดง Query ออกทางหน้าจอ ใช้ในการ debug Query 1 แสดง Query ข้อความอย่างเดียวสำหรับใส่ลงใน console 2 แสดง Query ด้านล่างจอภาพ
delete()  : bool
Deletes records from a table based on the given condition.
emptyTable()  : bool
Empties a table by deleting all its records.
execQuery()  : mixed
Executes one or multiple SQL queries and returns the result.
fieldCount()  : int
Returns the number of fields in the query result.
fieldExists()  : bool
Checks if a column exists in the table.
find()  : array<string|int, mixed>
Queries data and returns all items matching the condition.
first()  : mixed
Queries data and returns the first item matching the condition.
getAllSettings()  : object
Get all database settings.
getError()  : string
Returns the error message of the database.
getFields()  : array<string|int, mixed>
Returns the list of all fields from the query result.
getFullTableName()  : string
ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table ครอบชื่อตารางด้วย ``
getNextId()  : int
Gets the next ID for the specified table based on the primary key.
getSetting()  : mixed
Get the value of a database setting.
getTableName()  : string
ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table
indexExists()  : bool
Check if an index exists in a table.
insert()  : int|bool
Insert new data into a table.
insertOrUpdate()  : int
Insert new data into a table or update existing data if a unique key constraint is violated.
makeQuery()  : string
Generate an SQL query command.
optimizeTable()  : bool
Optimize a table.
query()  : bool
Execute an SQL query that does not require a result, such as CREATE, INSERT, or UPDATE.
queryCount()  : int
Get the total count of executed SQL queries.
repairTable()  : bool
Repair a table.
select()  : array<string|int, mixed>
Retrieve data from a table.
selectDB()  : bool
Selects a database.
tableExists()  : bool
Check if a table exists.
text()  : string
ฟังก์ชั่นสร้างคำสั่ง SQL เป็นข้อความ
update()  : bool
Updates data in the specified table.
updateAll()  : bool
Update data for all records in a table.
aliasName()  : string
ฟังก์ชั่นสร้างคีย์ สำหรับการ execute
buildGroup()  : string
สร้าง query สำหรับ GROUP BY
buildJoin()  : string
สร้างคำสั่ง JOIN ถ้าไม่มี alias คืนค่าว่าง
buildOrder()  : string
สร้าง query เรียงลำดับ
buildSelect()  : string
ฟังก์ชั่นสร้าง query string สำหรับคำสั่ง SELECT
buildValue()  : string
แปลงข้อมูลรูปแบบ SQL รูปแบบ array('field1', 'condition', 'field2') ไม่ระบุ condition หมายถึง = หรือ IN
buildWhere()  : string|array<string|int, mixed>
ฟังก์ชั่นสร้างคำสั่ง WHERE คืนค่า string สำหรับคำสั่ง WHERE หรือคืนค่า array(where, values) สำหรับใช้กับการ bind
buildWhereValues()  : array<string|int, mixed>
ฟังก์ชั่นสร้างคำสั่ง WHERE และ values ไม่ใส่ alias ให้กับชื่อฟิลด์ คืนค่า ($condition, $values)
doCustomQuery()  : array<string|int, mixed>|bool
Execute an SQL query for retrieving data.
doQuery()  : int|bool
Execute an SQL query that does not require a result, such as CREATE, INSERT, or UPDATE.
fieldName()  : string
แปลงข้อความสำหรับชื่อฟิลด์หรือชื่อตาราง
fieldValue()  : string
แปลงข้อความสำหรับ value
groupAnd()  : Sql
ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย AND
groupOr()  : Sql
ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย OR
log()  : mixed
Log the SQL query.
quoteTableName()  : string
ฟังก์ชั่นอ่านชื่อตารางและชื่อรอง และใส่ ` ครอบชื่อตารางด้วย
whereValue()  : array<string|int, mixed>|string
สร้างคำสั่ง WHERE

Properties

$cfg

Config class instance.

protected static object $cfg

$connection

protected object $connection = null

database connection

$debugger

1 แสดง Query ข้อความอย่างเดียวสำหรับใส่ลงใน console 2 แสดง Query ด้านล่างจอภาพ

protected int $debugger = 0

$error_message

protected string $error_message = ''

database error message

$query_count

protected static int $query_count = 0

number of queries

$request

Server request class instance.

protected static Request $request

$result_id

protected resource|object $result_id

result object from query

$sqls

ตัวแปรเก็บคำสั่ง SQL

protected array<string|int, mixed> $sqls

query statements for execution

Methods

__construct()

Class constructor

public __construct() : mixed
Return values
mixed

cacheGetAction()

Get the cache action.

public cacheGetAction() : int

Returns the cache action status:

  • 0: Cache is not used.
  • 1: Load and automatically save cache.
  • 2: Load data from cache, but do not automatically save cache.
Return values
int

The cache action status

cacheOn()

Enable caching.

public cacheOn([bool $auto_save = true ]) : static
Parameters
$auto_save : bool = true

(optional) Whether to automatically save the cache results (default: true)

Return values
static

cacheSave()

Save cache data.

public cacheSave(array<string|int, mixed> $datas) : bool
Parameters
$datas : array<string|int, mixed>

The data to be saved

Return values
bool

True if the cache is saved successfully, false otherwise

close()

Close the database connection.

public close() : mixed
Return values
mixed

connection()

Get the current database connection.

public connection() : resource
Return values
resource

The database connection

customQuery()

Process an SQL query command to retrieve data.

public customQuery(string $sql[, bool $toArray = false ][, array<string|int, mixed> $values = [] ]) : array<string|int, mixed>

Returns an array of records that match the conditions. If no records are found, an empty array is returned.

Parameters
$sql : string

The query string

$toArray : bool = false

Optional. Default is false. Set to true to return results as an array, otherwise returns results as objects.

$values : array<string|int, mixed> = []

Optional. If specified, prepares the query using these values instead of executing the query directly.

Return values
array<string|int, mixed>

An array of records that match the conditions

databaseExists()

Checks if a database exists.

public databaseExists(string $database) : bool
Parameters
$database : string

The name of the database to check

Return values
bool

Returns true if the database exists, false otherwise

db()

Get the database connection.

public db() : Driver
Return values
Driver

The database connection.

debug()

คำสั่งสำหรับแสดง Query ออกทางหน้าจอ ใช้ในการ debug Query 1 แสดง Query ข้อความอย่างเดียวสำหรับใส่ลงใน console 2 แสดง Query ด้านล่างจอภาพ

public debug([int $value = 1 ]) : mixed
Parameters
$value : int = 1
Return values
mixed

delete()

Deletes records from a table based on the given condition.

public delete(string $table_name, array<string|int, mixed>|string $condition[, int $limit = 1 ][, string $operator = 'AND' ]) : bool
Parameters
$table_name : string

The name of the table to delete records from

$condition : array<string|int, mixed>|string

The condition for deleting records (can be an array or a string)

$limit : int = 1

Optional. The maximum number of records to delete (default: 1)

$operator : string = 'AND'

Optional. The operator used to combine multiple conditions (default: 'AND')

Return values
bool

Returns true if the delete operation is successful, false otherwise

emptyTable()

Empties a table by deleting all its records.

public emptyTable(string $table_name) : bool
Parameters
$table_name : string

The name of the table to empty

Return values
bool

Returns true if the table is successfully emptied, false otherwise

execQuery()

Executes one or multiple SQL queries and returns the result.

public execQuery(mixed $sqls[, array<string|int, mixed> $values = [] ], int $debugger) : mixed
Parameters
$sqls : mixed

The SQL query or an array of SQL queries to execute

$values : array<string|int, mixed> = []

An array of parameter values to bind to the query (optional)

$debugger : int

Debugging mode: 0 - disabled, 1 - echo debug info, 2 - collect debug info (optional)

Return values
mixed

Returns the result of the executed query/queries

fieldCount()

Returns the number of fields in the query result.

public abstract fieldCount() : int
Return values
int

The number of fields

fieldExists()

Checks if a column exists in the table.

public fieldExists(string $table_name, string $column_name) : bool
Parameters
$table_name : string

The table name

$column_name : string

The column name

Return values
bool

True if the column exists, false otherwise

find()

Queries data and returns all items matching the condition.

public find(string $table_name, mixed $condition[, array<string|int, mixed> $sort = [] ]) : array<string|int, mixed>
Parameters
$table_name : string

The table name

$condition : mixed

The query WHERE condition

$sort : array<string|int, mixed> = []

Sorting options

Return values
array<string|int, mixed>

An array of objects representing the retrieved data, or an empty array if not found

first()

Queries data and returns the first item matching the condition.

public first(string $table_name, mixed $condition) : mixed
Parameters
$table_name : string

The table name

$condition : mixed

The query WHERE condition

Return values
mixed

An object representing the retrieved data, or false if not found

getAllSettings()

Get all database settings.

public getAllSettings() : object
Return values
object

The database settings object.

getError()

Returns the error message of the database.

public getError() : string
Return values
string

The error message

getFields()

Returns the list of all fields from the query result.

public abstract getFields() : array<string|int, mixed>
Return values
array<string|int, mixed>

An array containing the names of all fields

getFullTableName()

ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table ครอบชื่อตารางด้วย ``

public getFullTableName(string $table) : string
Parameters
$table : string

ชื่อตารางตามที่กำหนดใน settings/datasbase.php

Return values
string

getNextId()

Gets the next ID for the specified table based on the primary key.

public getNextId(string $table_name[, array<string|int, mixed> $condition = [] ][, string $operator = 'AND' ][, string $primary_key = 'id' ]) : int
Parameters
$table_name : string

The name of the table.

$condition : array<string|int, mixed> = []

An array of conditions for the query (default is empty).

$operator : string = 'AND'

The logical operator for combining conditions (default is 'AND').

$primary_key : string = 'id'

The primary key column name (default is 'id').

Return values
int

The next ID for the specified table.

getSetting()

Get the value of a database setting.

public getSetting(string $key) : mixed
Parameters
$key : string

The setting key.

Return values
mixed

The value of the setting.

getTableName()

ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table

public getTableName(string $table) : string
Parameters
$table : string

ชื่อตารางตามที่กำหนดใน settings/datasbase.php

Return values
string

indexExists()

Check if an index exists in a table.

public indexExists(string $database_name, string $table_name, string $index) : bool
Parameters
$database_name : string

The database name.

$table_name : string

The table name.

$index : string

The index name.

Return values
bool

Returns true if the index exists, false otherwise.

insert()

Insert new data into a table.

public abstract insert(string $table_name, array<string|int, mixed> $save) : int|bool
Parameters
$table_name : string

The table name.

$save : array<string|int, mixed>

The data to be saved.

Return values
int|bool

Returns the ID of the inserted data if successful, or false if an error occurs.

insertOrUpdate()

Insert new data into a table or update existing data if a unique key constraint is violated.

public abstract insertOrUpdate(string $table_name, array<string|int, mixed>|object $save) : int
Parameters
$table_name : string

The table name.

$save : array<string|int, mixed>|object

The data to be saved in the format array('key1'=>'value1', 'key2'=>'value2', ...).

Tags
throws
Exception

if there's an error executing the query.

Return values
int

Returns the ID of the inserted data, 0 if an update occurred, or null if an error occurs.

makeQuery()

Generate an SQL query command.

public abstract makeQuery(array<string|int, mixed> $sqls) : string
Parameters
$sqls : array<string|int, mixed>

The SQL commands from the query builder.

Return values
string

Returns the SQL command.

optimizeTable()

Optimize a table.

public optimizeTable(string $table_name) : bool
Parameters
$table_name : string

The table name.

Return values
bool

Returns true if successful.

query()

Execute an SQL query that does not require a result, such as CREATE, INSERT, or UPDATE.

public query(string $sql[, array<string|int, mixed> $values = [] ]) : bool
Parameters
$sql : string

The query string.

$values : array<string|int, mixed> = []

If specified, it will use prepared statements instead of directly querying the database.

Return values
bool

Returns true if successful, or false if an error occurs.

queryCount()

Get the total count of executed SQL queries.

public static queryCount() : int
Return values
int

repairTable()

Repair a table.

public repairTable(string $table_name) : bool
Parameters
$table_name : string

The table name.

Return values
bool

Returns true if successful.

select()

Retrieve data from a table.

public abstract select(string $table_name[, mixed $condition = [] ][, array<string|int, mixed> $sort = [] ], int $limit) : array<string|int, mixed>
Parameters
$table_name : string

The table name.

$condition : mixed = []

The query WHERE condition.

$sort : array<string|int, mixed> = []

The sorting criteria.

$limit : int

The number of data to retrieve.

Return values
array<string|int, mixed>

The resulting data in array format. Returns an empty array if unsuccessful.

selectDB()

Selects a database.

public abstract selectDB(string $database) : bool
Parameters
$database : string

The name of the database.

Return values
bool

Returns true on success, false on failure.

tableExists()

Check if a table exists.

public tableExists(string $table_name) : bool
Parameters
$table_name : string

The table name.

Return values
bool

Returns true if the table exists, false otherwise.

text()

ฟังก์ชั่นสร้างคำสั่ง SQL เป็นข้อความ

public text() : string
Return values
string

update()

Updates data in the specified table.

public abstract update(string $table_name, mixed $condition, array<string|int, mixed>|object $save) : bool
Parameters
$table_name : string

The table name.

$condition : mixed

The query WHERE condition.

$save : array<string|int, mixed>|object

The data to be saved in the format array('key1'=>'value1', 'key2'=>'value2', ...)

Return values
bool

Returns true on success, false on failure.

updateAll()

Update data for all records in a table.

public updateAll(string $table_name, array<string|int, mixed> $save) : bool

Returns true if successful, false otherwise.

Parameters
$table_name : string

The table name.

$save : array<string|int, mixed>

The data to be saved in the format array('key1'=>'value1', 'key2'=>'value2', ...).

Return values
bool

aliasName()

ฟังก์ชั่นสร้างคีย์ สำหรับการ execute

protected aliasName(string $name[, string $prefix = '' ]) : string
Parameters
$name : string

ชื่อฟิลด์

$prefix : string = ''

คำนำหน้าชื่อฟิลด์ ใช้เพื่อป้องกันการใช้ตัวแปรซ้ำ

Return values
string

buildGroup()

สร้าง query สำหรับ GROUP BY

protected buildGroup(array<string|int, mixed>|string $fields) : string
Parameters
$fields : array<string|int, mixed>|string

array('U.id', 'U.username') หรือ string U.id

Return values
string

buildJoin()

สร้างคำสั่ง JOIN ถ้าไม่มี alias คืนค่าว่าง

protected buildJoin(string|array<string|int, mixed> $table, string $type, mixed $on) : string
Parameters
$table : string|array<string|int, mixed>

ชื่อตารางต้องมี alias ด้วย หรือ (QueryBuilder, alias)

$type : string

เข่น INNER OUTER LEFT RIGHT

$on : mixed

query string หรือ array

Return values
string

buildOrder()

สร้าง query เรียงลำดับ

protected buildOrder(array<string|int, mixed>|string $fields) : string
Parameters
$fields : array<string|int, mixed>|string

array('field ASC','field DESC') หรือ 'field ASC', 'field DESC', ...

Return values
string

buildSelect()

ฟังก์ชั่นสร้าง query string สำหรับคำสั่ง SELECT

protected buildSelect(string|array<string|int, mixed>|QueryBuilder $fields, mixed $alias) : string
Parameters
$fields : string|array<string|int, mixed>|QueryBuilder
$alias : mixed
Return values
string

buildValue()

แปลงข้อมูลรูปแบบ SQL รูปแบบ array('field1', 'condition', 'field2') ไม่ระบุ condition หมายถึง = หรือ IN

protected buildValue(array<string|int, mixed> $params) : string
Parameters
$params : array<string|int, mixed>
Return values
string

buildWhere()

ฟังก์ชั่นสร้างคำสั่ง WHERE คืนค่า string สำหรับคำสั่ง WHERE หรือคืนค่า array(where, values) สำหรับใช้กับการ bind

protected buildWhere(mixed $condition[, string $operator = 'AND' ][, string $id = 'id' ]) : string|array<string|int, mixed>
Parameters
$condition : mixed
$operator : string = 'AND'

(optional) เช่น AND หรือ OR

$id : string = 'id'

(optional )ชื่อฟิลด์ที่เป็น key

Return values
string|array<string|int, mixed>

buildWhereValues()

ฟังก์ชั่นสร้างคำสั่ง WHERE และ values ไม่ใส่ alias ให้กับชื่อฟิลด์ คืนค่า ($condition, $values)

protected buildWhereValues(mixed $condition[, string $operator = 'AND' ][, string $id = 'id' ]) : array<string|int, mixed>
Parameters
$condition : mixed
$operator : string = 'AND'

(optional) เช่น AND หรือ OR

$id : string = 'id'

(optional )ชื่อฟิลด์ที่เป็น key

Return values
array<string|int, mixed>

doCustomQuery()

Execute an SQL query for retrieving data.

protected abstract doCustomQuery(string $sql[, array<string|int, mixed> $values = [] ]) : array<string|int, mixed>|bool
Parameters
$sql : string

The SQL query string.

$values : array<string|int, mixed> = []

If specified, it will use prepared statements instead of direct query execution.

Return values
array<string|int, mixed>|bool

Returns an array of records that match the condition on success, or false on failure.

doQuery()

Execute an SQL query that does not require a result, such as CREATE, INSERT, or UPDATE.

protected abstract doQuery(string $sql[, array<string|int, mixed> $values = [] ]) : int|bool
Parameters
$sql : string

The SQL query string.

$values : array<string|int, mixed> = []

If specified, it will use prepared statements instead of direct query execution.

Return values
int|bool

Returns the number of affected rows on success, or false on failure.

fieldName()

แปลงข้อความสำหรับชื่อฟิลด์หรือชื่อตาราง

protected fieldName(string $name) : string
Parameters
$name : string
Return values
string

fieldValue()

แปลงข้อความสำหรับ value

protected fieldValue(string $value) : string
Parameters
$value : string
Return values
string

groupAnd()

ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย AND

protected groupAnd(array<string|int, mixed> $params) : Sql
Parameters
$params : array<string|int, mixed>

คำสั่ง รูปแบบ array('field1', 'condition', 'field2')

Return values
Sql

groupOr()

ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย OR

protected groupOr(array<string|int, mixed> $params) : Sql
Parameters
$params : array<string|int, mixed>

คำสั่ง รูปแบบ array('field1', 'condition', 'field2')

Return values
Sql

log()

Log the SQL query.

protected log(string $type, string $sql[, array<string|int, mixed> $values = [] ]) : mixed
Parameters
$type : string

The type of query.

$sql : string

The SQL query string.

$values : array<string|int, mixed> = []

(Optional) The values for prepared statements.

Return values
mixed

quoteTableName()

ฟังก์ชั่นอ่านชื่อตารางและชื่อรอง และใส่ ` ครอบชื่อตารางด้วย

protected quoteTableName(string $table) : string
Parameters
$table : string

ชื่อตารางตามที่กำหนดใน settings/datasbase.php

Return values
string

whereValue()

สร้างคำสั่ง WHERE

private whereValue(array<string|int, mixed> $params[, int|null $i = null ]) : array<string|int, mixed>|string
Parameters
$params : array<string|int, mixed>
$i : int|null = null
Return values
array<string|int, mixed>|string

Search results