In 5.1
the version, support for a variety of database coroutine clients has been added, and all PDO
are provided in the form of interfaces. Old business codes can be switched to coroutine mode with one click without any changes, and can be executed concurrently asynchronously and non-blockingly.
include:
pdo_pgsql
pdo_odbc
pdo_sqlite
pdo_oci
(Oracle
database)
Contents
Open method
Added 4
compilation parameters and Runtime Hook
options to enable these coroutine clients.
compile options
--with-swoole-odbc
,relyunixodbc-dev
--with-swoole-pgsql
,relylibpq-dev
--with-swoole-sqlite
,relylibsqlite3-dev
--with-swoole-oracle
,relyoracle-instantclient
Runtime Hook
Options
SWOOLE_HOOK_PDO_PGSQL
SWOOLE_HOOK_PDO_ODBC
SWOOLE_HOOK_PDO_SQLITE
SWOOLE_HOOK_PDO_ORACLE
These options are included in SWOOLE_HOOK_ALL
, or one of the clients can be enabled individually.
PDO_PGSQL
In the previous version, Swoole\Coroutine\PostgreSQL
the client was provided. Since it is brand new API
, users need to be compatible PHP-FPM
with and Swoole
, and it is not widely used.
go (function () { $pg = new Swoole\Coroutine\PostgreSql (); $conn = $pg -> connect ( "host=127.0.0.1 port=5432 dbname=test user=test password=" ); $result = $pg -> query ( $conn , 'SELECT * FROM test;' ); $arr = $pg -> fetchAll ( $result ); var_dump ( $arr ); });
5.1
Added in the version Runtime Hook PDO_PGSQL
, you can directly use FPM
the historical code under .
go (function () { $pdo = new PDO ( "pgsql:host= {$host} ;port= {$port} ;dbname= {$dbname} " , $user , $password ); $statement = $pdo - > query ( 'select * from user where id =1 limit 1' ); var_dump ( $statement -> fetch (PDO:: FETCH_ASSOC )); });
PDO_ODBC
ODBC
( Open Database Connectivity
) is a standardized technology that provides a unified interface for multiple database management systems. It was proposed by Microsoft to achieve heterogeneity and enable applications to access multiple different types of data sources. ODBC
The standard defines what applications make requests API
, and what drivers provide in response to requests API
. ODBC
It is an open, cross-platform technology that can be used in a variety of operating systems and programming languages.
Most relational databases support ODBC
drivers, including:
Microsoft SQL Server
Oracle
IBM DB2
MySQL
PostgreSQL
SQLite
Teradata
Microsoft Access
SAP Sybase Adaptive Server Enterprise (ASE)
Informix
These databases use ODBC
drivers to provide access interfaces, allowing developers to use the same code to access data when using different databases.
5.1
This version supports PDO_ODBC
coroutine support, and ODBC
can support almost all databases, so that these databases can also run in Swoole
coroutine mode.
Linux
You need to install the library under unixodbc
to supportodbc
sudo apt install unixodbc-dev
Unlike other database drivers, odbc
you need to use odbcinst
the tool to register the database first. The configuration file needs to be implemented Linux
below /etc/odbcinst.ini
.
odbcinst.ini
[mysql] Driver =libmaodbc.so Description =MariaDB Connector/ODBC(Unicode) Threading = 0 UsageCount = 1
The configuration file is placed in /etc/odbcinst.ini
, Driver=libmaodbc.so
which tells odbc
to use this dynamic link library as the driver layer.
To support other more database types, you need to install ODBC
the driver of the database. For example, MySQL ODBC
the driver can useodbc-mariadb
sudo apt install odbc-mariadb
After successful execution, it will be installed libmaodbc.so
in the system library directory.
mysql_odbc.ini
To register a database ODBC
, you need to write a configuration file, including the database’s host address, user name, password and other information.
[mysql-test] Description = MySQL test database Trace = On TraceFile = stderr Driver = mysql SERVER = 127.0 . 0.1 USER = root PASSWORD = root PORT = 3306 DATABASE = test
Then use odbcinst
the tool to register the database:
odbcinst -i -d -f ~/mysql-odbc.ini
Can be isql
executed using toolsSQL
htf@swoole-12:/etc$ isql mysql-test +---------------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------------+ SQL> show tables +------------------------------------------------- ----------------+ | Tables_in_test | +------------------------------------------------- ----------------+ | ckl | | custom | | firmware | | numbers | | userinfo | +------------------------------------------------- ----------------+ SQLRowCount returns 5 5 rows fetched SQL>
code
Co\ run (function () { $pdo = new PDO ( 'odbc:mysql-test' ); $statement = $pdo -> prepare ( 'show tables' ); $statement -> execute (); var_dump ( count ( $statement -> fetchAll (PDO:: FETCH_COLUMN )), 1 ); });
PDO_SQLITE
SQLite
A lightweight, open source, embedded relational database management system. Its main advantages are that it is small, fast, has support for transactions, and is portable. Here are SQLite
some of the advantages:
- Occupies few resources:
SQLite
It is an embedded database that does not require installation or maintenance. It is stored in the file system of the local operating system in the form of a file and does not need to run a database server process independently. Therefore, it takes up very little resources and can run on resource-constrained embedded devices and mobile devices. - Fast:
SQLite
It is a very fast relational database that can easily handle small and medium-sized data sets. Compared with other traditional relational databases, its read and write speeds are relatively fast. - Support for transactions:
SQLite
The support for transactions is very good compared to other lightweight databases, and supports the Acid attribute to prevent data loss or corruption. - Portability: SQLite’s file format is operating system independent, which enables users to migrate their database files from one operating system to another.
Consider using it in the following situations SQLite
:
- Local data storage:
SQLite
suitable for storing local data in applications, such as user personal data, local settings and cache in mobile applications. - Small and medium-sized data storage:
SQLite
suitable for storing small and medium-sized data sets, such as the data of small enterprises or single users, rather than situations where the application scenario is complex or the data set is very large. - Offline data read and write operations are required: Since
SQLite
the file format of is independent of the operating system, data can be stored as files and read and written offline. - Rapid prototyping: Because it
SQLite
is easy to use and manage, it is suitable for rapid prototyping to test and optimize the application in real production.
5.1
The version provides sqlite
coroutine client support for embedded databases, and also uses PDO
as the interface layer.
compile options
./configure --enable-swoole-sqlite
need to depend on
libsqlite3-dev
Code example
go (function() { $db = new PDO ( 'sqlite::memory:' ); $db -> exec ( 'create table test (id int)' ); $stmt = $db -> prepare ( 'insert into test values(?)' ); $i = 2024 ; $stmt -> execute ([ $i ]); $stmt = $db -> prepare ( 'select id from test where id = ?' ); $stmt -> execute ([ $i ]); var_dump ( $stmt -> fetch (PDO:: FETCH_ASSOC )[ 'id' ] == $i ); });
PDO_OCI
Oracle
The database is a large-scale relational database developed by Oracle Corporation in the United States. It is used by many large enterprises around the world Oracle
. Databases are used in the core systems of many commercial projects such as e-commerce platforms, banks, finance, and telecommunications Oracle
.
The following are some features and functions of Oracle database:
- High performance:
Oracle
The database can handle a large number of concurrent user requests and quickly process large-scale data. - Scale:
Oracle
The database supports large-scale database clusters and complex aggregation operations, making it very popular in large enterprises and business-critical applications. - Redundant backup:
Oracle
The database provides powerful redundant backup and recovery functions, supporting online backup and recovery, incremental backup and recovery, point-in-time recovery, etc. - Security:
Oracle
The database provides features for encryption, data masking, access control, and authentication of the database. - Flexibility:
Oracle
The database supports multiple data types and programming languages, and provides powerful storage, indexing and query functions. - High availability:
Oracle
The database has high availability and supports master-slave synchronization, automatic failover and fault tolerance functions. These functions can ensure the continuity and stability of the system. - Performance optimization:
Oracle
The database provides rich performance optimization functions and supports various tuning technologies to ensure that the database runs stably under high load.
5.1
Version provides native pdo_oci
coroutine client implementation. The database can be connected and operated concurrently in a coroutine environment Oracle
.
compile options
You need to add --with-swoole-oracle=instantclient,/path/to/instant/client/lib
parameters to enable PDO_OCI
the coroutine client.
Oracle
You can download the client driver from the official website
apt install -y libaio-dev apt install -y libaio1 wget -nv https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linuxx64.zip unzip instantclient-basiclite-linuxx64.zip && rm instantclient-basiclite-linuxx64.zip wget -nv https://download.oracle.com/otn_software/linux/instantclient/instantclient-sdk-linuxx64.zip unzip instantclient-sdk-linuxx64.zip && rm instantclient-sdk-linuxx64.zip mv instantclient_*_* ./instantclient rm ./instantclient/sdk/include/ldap.h # fix debug build warning: zend_signal: handler was replaced for signal (2) after startup echo DISABLE_INTERRUPT=on > ./instantclient/network/admin/sqlnet.ora mv ./instantclient /usr/local/ echo '/usr/local/instantclient' > /etc/ld.so.conf.d/oracle-instantclient.conf ldconfig
Code example
Co\ run (function () { $db = new PDO ( 'oci:dbname=127.0.0.1:' . $ORACLE_PORT . '/' . $ORACLE_SERVICE_NAME . ';charset=AL32UTF8' , $ORACLE_USER , $ORACLE_PASSWORD ); $ db -> setAttribute (PDO:: ATTR_CASE , PDO:: CASE_LOWER ); $db -> exec ( "create table test (id int)" ); for ( $i = 0 ; $i < 10 ; $i ++) { go (function () use ($ db , $ i ){ $ stmt = $ db -> prepare (" insert into test values (?)"); $stmt -> execute ([ $i ]); $stmt = $db -> prepare ( "select id from test where id = ? " ); $stmt -> execute ([ $i ]); var_dump ( $stmt -> fetch (PDO:: FETCH_ASSOC )[ 'id' ] == $i ); }); } });
Conclusion
In 5.1
the version, support for more database coroutine clients has been added, allowing Swoole
multiple databases to be supported in coroutine mode.
Use is selected on the interface PDO API
to maintain PHP-FPM
compatibility with . There is no need to modify the code and there is no additional learning cost.
The implementation uses native API
+ swoole::coroutine::async
, based on scalable AIO
thread pool and coroutine scheduling API
.
This method is more secure and robust and can be directly used in production environments.