liberty_code / sql
Requires
- php: ~7 || ~8
- liberty_code/authentication: ^1.0.
- liberty_code/di: ^1.0.
- liberty_code/item_browser: ^1.0.
- liberty_code/library: ^1.0.
- liberty_code/model: ^1.0.
- liberty_code/register: ^1.0.
- liberty_code/validation: ^1.0.
This package is auto-updated.
Last update: 2024-09-11 00:10:25 UTC
README
描述
该库包含SQL组件,用于SQL数据存储类型。
要求
- 脚本语言:PHP:版本 7 || 8
安装
有多种可能的安装方式
Composer
要求
它需要安装Composer。更多信息: https://getcomposer.org.cn
命令:移动到项目根路径
cd "<project_root_path>"
命令:安装
php composer.phar require liberty_code/sql ["<version>"]
注意
包含供应商
如果项目使用Composer,则必须包含供应商
require_once('<project_root_path>/vendor/autoload.php');
配置
安装命令允许在composer文件中添加
{ "require": { "liberty_code/sql": "<version>" } }
包含
下载
- 下载以下存储库。
- 将其放在存储库根路径上。
包含源代码
require_once('<repository_root_path>/include/Include.php');
使用
数据库连接
连接允许设计SQL数据库连接,从指定配置连接和请求特定SQL数据存储。
元素
连接
允许设计一个连接,它是一个包含所有信息的项目,允许连接和管理数据。
PdoConnection
扩展连接功能。允许使用特定的PDO标准设计SQL数据库连接。
示例
// Define new PDO connection
use liberty_code\sql\database\connection\library\ConstConnection;
use liberty_code\sql\database\connection\pdo\library\ConstPdoConnection;
use liberty_code\sql\database\connection\pdo\model\PdoConnection;
class MysqlPdoConnection extends PdoConnection
{
public function getStrConfigDsn()
{
$tabConfig = $this->getTabConfig();
return sprintf(
'host=%1$s;port=%2$s;dbname=%3$s;charset=%4$s',
$tabConfig[ConstConnection::TAB_CONFIG_KEY_HOST],
$tabConfig[ConstConnection::TAB_CONFIG_KEY_PORT],
$tabConfig[ConstConnection::TAB_CONFIG_KEY_DB_NAME],
$tabConfig[ConstConnection::TAB_CONFIG_KEY_CHARSET]
)
}
public function setConfig(array $tabConfig, $boolConnect = true)
{
// Set MySQL driver
$tabConfig[ConstPdoConnection::TAB_CONFIG_KEY_DRIVER] = 'mysql';
parent::setConfig($tabConfig, $boolConnect);
}
}
...
// Get connection
$connection = new MysqlPdoConnection(array(
ConstConnection::TAB_CONFIG_KEY_HOST => 'host',
ConstConnection::TAB_CONFIG_KEY_DB_NAME => 'db_name'
ConstConnection::TAB_CONFIG_KEY_CHARSET => 'utf8',
ConstConnection::TAB_CONFIG_KEY_LOGIN => 'login',
ConstConnection::TAB_CONFIG_KEY_PASSWORD => 'password'
));
...
// Execute SQL command
$connection->execute('...SQL string command');
...
数据库语句
语句允许设计预编译查询,从指定的查询参数执行SQL命令,在特定的SQL数据存储上。
元素
语句
允许设计一个语句,代表预编译查询,可以使用指定的参数执行。
PdoStatement
扩展语句功能。允许使用特定的PDO标准设计SQL数据库语句。
示例
...
// Get statement from connection
$statement = $connection->getObjStatement('...SQL string command');
...
// Execute statement
$statement->execute(array(
'param_1' => 'Value 1',
...,
'param_N' => 'Value N'
));
...
数据库结果
结果允许设计查询结果,读取和获取查询结果,在特定的SQL数据存储上。
元素
结果
允许设计查询结果,以读取和获取查询结果。
PdoStatement
扩展结果功能。允许使用特定的PDO标准设计SQL数据库结果。
示例
...
// Get SQL result from connection
$result = $connection->executeResult('...SQL string command');
...
// Or get SQL result from statement
$statement = $connection->getObjStatement('...SQL string command');
$result = $statement->executeResult(array(
'param_1' => 'Value 1',
...,
'param_N' => 'Value N'
));
...
if($result !== false) {
while(($data = $result->getFetchData()) !== false) {
var_dump($data);
}
/**
* Show:
* row 1: array('column name 1' => 'value 1', ..., 'column name N' => 'value N')
* ...
* row N: array('column name 1' => 'value 1', ..., 'column name N' => 'value N')
*/
...
// Close result
$result->close();
}
...
数据库命令
命令允许设计查询构建器,从指定的配置获取SQL字符串命令,在特定的SQL数据存储上使用。
元素
命令
允许设计查询构建器,从指定的配置获取SQL字符串命令。
DbUseCommand
扩展命令功能。允许设计用于SQL数据库字符串命令的查询构建器。
StandardDbUseCommand
扩展数据库使用命令功能。使用标准SQL。
DbShowCommand
扩展命令功能。允许设计用于SQL数据库显示字符串命令的查询构建器。
StandardDbShowCommand
扩展数据库显示命令功能。使用标准SQL。
DbCreateCommand
扩展命令功能。允许设计用于SQL数据库创建字符串命令的查询构建器。
StandardDbCreateCommand
扩展数据库创建命令功能。使用标准SQL。
DbAlterCommand
扩展命令功能。允许设计用于SQL数据库更改字符串命令的查询构建器。
StandardDbAlterCommand
扩展数据库更改命令功能。使用标准SQL。
DbDropCommand
扩展命令功能。允许设计用于SQL数据库删除字符串命令的查询构建器。
StandardDbDropCommand
扩展数据库删除命令功能。使用标准SQL。
TableShowCommand
扩展命令功能。允许设计用于SQL表显示字符串命令的查询构建器。
StandardTableShowCommand
扩展表显示命令功能。使用标准SQL。
TableDropCommand
扩展命令功能。允许设计用于SQL表删除字符串命令的查询构建器。
StandardTableDropCommand
扩展表删除命令功能。使用标准SQL。
SelectCommand
扩展命令功能。允许设计用于SQL选择字符串命令的查询构建器。
StandardSelectCommand
扩展选择命令功能。使用标准SQL。
InsertCommand
扩展命令功能。允许设计用于SQL插入字符串命令的查询构建器。
StandardInsertCommand
扩展插入命令功能。使用标准SQL。
UpdateCommand
扩展命令功能。允许设计用于SQL更新字符串命令的查询构建器。
StandardUpdateCommand
扩展更新命令功能。使用标准SQL。
DeleteCommand
扩展命令功能。允许设计用于SQL删除字符串命令的查询构建器。
StandardDeleteCommand
扩展删除命令功能。使用标准SQL。
CommandFactory
允许设计查询构建器工厂,从指定的SQL数据库连接提供新的命令对象。
StandardCommandFactory
扩展命令工厂功能。使用标准SQL提供命令对象。
示例
...
// Get command factory
use liberty_code\sql\database\command\factory\standard\model\StandardCommandFactory;
$commandFacto = new StandardCommandFactory($connection);
...
// Get SQL select command
$command = $commandFacto->getObjSelectCommand(array(
... SQL select command configuration format
));
...
// Get SQL result
$result = $command->executeResult();
...
if($result !== false) {
while(($data = $result->getFetchData()) !== false) {
var_dump($data);
}
/**
* Show:
* row 1: array('column name 1' => 'value 1', ..., 'column name N' => 'value N')
* ...
* row N: array('column name 1' => 'value 1', ..., 'column name N' => 'value N')
*/
...
// Close result
$result->close();
}
...
Register
使用特定的SQL数据存储作为存储支持进行注册。
元素
Register (SQL默认)
扩展默认注册功能。允许使用特定SQL数据存储管理项目。
TableRegister
扩展SQL默认注册功能。每个键被视为列名。将每个项目存储在行中,与正确的列(键)关联。
DataRegister
扩展SQL默认注册功能。将键存储在同一特定列中。将项目存储在同一特定列中。
示例
...
// Get register
use liberty_code\sql\register\library\ConstRegister;
use liberty_code\sql\register\data\library\ConstDataRegister;
use liberty_code\sql\register\data\model\DataRegister;
$register = new DataRegister(
array(
ConstRegister::TAB_CONFIG_KEY_TABLE_NAME => 'table name',
ConstDataRegister::TAB_CONFIG_KEY_COLUMN_NAME_KEY => 'column name for keys',
ConstDataRegister::TAB_CONFIG_KEY_COLUMN_NAME_ITEM => 'column name for items',
ConstDataRegister::TAB_CONFIG_KEY_COLUMN_NAME_EXPIRE_TIMEOUT_DATETIME => 'column name for expiration datetimes'
),
$commandFacto
);
...
$register->putItem('key_1', '...'); // Register specified item for key 1
$register->putItem('key_N', '...'); // Register specified item for key N
...
foreach($register->getTabKey() as $key) {
var_dump($register->getItem($key));
}
/**
* Show:
* item for key 1
* item for key N
*/
...
Rule
SQL规则允许使用SQL数据存储检查指定数据验证。
元素
SqlRule
扩展规则功能。允许使用SQL数据存储检查指定数据验证。
ExistSqlRule
扩展SQL规则功能。允许检查指定数据在指定SQL数据存储中是否存在。
Persistence
使用特定SQL数据存储作为存储支持进行持久化。
元素
Persistor (SQL默认)
扩展默认持久化功能。允许设计实体在特定SQL数据存储上的保存引擎。
TablePersistor
扩展SQL默认持久化功能。每个属性名被视为列名。将每个实体属性值存储在同一行中,与正确的列(属性名)关联。
DataPersistor
扩展SQL默认持久化功能。将属性名存储在同一特定列中。将属性值存储在同一特定列中。
示例
...
// Get persistor
use liberty_code\sql\persistence\library\ConstPersistor;
use liberty_code\sql\persistence\table\model\TablePersistor;
$persistor = new TablePersistor($commandFacto);
$config = array(
ConstPersistor::TAB_CONFIG_KEY_TABLE_NAME => 'table name',
ConstPersistor::TAB_CONFIG_KEY_COLUMN_NAME_ID => 'column name for ids',
);
...
// Select entities attributes from specified ids
$tabData = $persistor->getTabData(
array(
'entity id 1',
...,
'entity id N'
),
$config
);
...
if ($tabData !== false) {
foreach($tabData as $data) {
var_dump($data);
}
/**
* Show:
* entity 1 data: array('attribute name 1' => 'value 1', ..., 'attribute name N' => 'value N')
* ...
* entity N data: array('attribute name 1' => 'value 1', ..., 'attribute name N' => 'value N')
*/
}
...
Item browser
使用特定SQL数据存储提供项目的浏览器。
元素
Browser (SQL默认)
扩展条件浏览器功能。允许从特定SQL数据存储提供项目。
TableBrowser
扩展SQL默认浏览器功能。每个项目键被视为列名。每个项目值被视为存储在同一行中,与正确的列(项目键)关联。
DataBrowser
扩展SQL默认浏览器功能。每个项目键被视为存储在同一特定列中。每个项目值被视为存储在同一特定列中。
示例
...
// Get SQL table browser
use liberty_code\register\register\memory\model\MemoryRegister;
use liberty_code\item_browser\operation\model\OperatorData;
use liberty_code\item_browser\browser\library\ConstBrowser
use liberty_code\sql\browser\table\model\TableBrowser;
$config = array(
ConstBrowser::TAB_CONFIG_KEY_QUERY => ... SQL select command configuration format
);
$operatorData = new OperatorData();
$register = new MemoryRegister();
$browser = new TableBrowser(
$operatorData,
$commandFacto,
$register,
$config,
array(),
array()
);
...
// Set number items per page
$browser->setItemCountPerPage(10);
...
$pageCount = $browser->getIntPageCount(); // Get number of pages
for($cpt = 0; $cpt < count($pageCount); $cpt++) {
$browser->setActivePageIndex($cpt);
var_dump($browser->getTabItem());
}
/**
* Show:
* index array of items, on page 1
* ...
* index array of items, on page N
*/
...
Authenticator
使用特定SQL数据存储进行身份验证和身份检查的SQL身份验证器。
元素
SqlAuthenticator
扩展身份验证器功能。允许使用SQL数据存储检查身份验证和身份。
示例
...
// Get SQL authenticator
use liberty_code\sql\authentication\authenticator\library\ConstSqlAuthenticator;
use liberty_code\sql\authentication\authenticator\model\SqlAuthenticator;
$config = array(
ConstSqlAuthenticator::TAB_CONFIG_KEY_QUERY => ... SQL select command configuration format,
ConstSqlAuthenticator::TAB_CONFIG_KEY_DEFAULT_ID_DATA_COLUMN_NAME => ... default identification column name,
ConstSqlAuthenticator::TAB_CONFIG_KEY_DEFAULT_AUTH_DATA_COLUMN_NAME => ... default authentication column name
);
$authenticator = new SqlAuthenticator(
$config,
$commandFacto
);
...
// Check if specified authentication is identified
var_dump($authenticator->checkIsIdentified(...authentication object));
...
// Check if specified authentication is authenticated
var_dump($authenticator->checkIsAuthenticated(...authentication object));
...