Skip to content

Commit

Permalink
Support Sqlite (#5063)
Browse files Browse the repository at this point in the history
* hook sqlite

* fix test

* fix error

* fix test and add SQLITE_CONFIG_SERIALIZED

* fix test and optimize code

* fix test and optimize code

* add test

* config.m4
  • Loading branch information
NathanFreeman authored Jun 5, 2023
1 parent 9db19c5 commit d8c2b55
Show file tree
Hide file tree
Showing 81 changed files with 5,474 additions and 12 deletions.
43 changes: 41 additions & 2 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,38 @@ EOF
fi
dnl SWOOLE_ORACLE stop

dnl sqlite start
PHP_ARG_ENABLE([swoole-sqlite],
[for sqlite 3 support for PDO],
[AS_HELP_STRING([--enable-swoole-sqlite],
[PDO: sqlite 3 support.])], [no], [no])

if test "$PHP_SWOOLE_SQLITE" != "no"; then

if test "$PHP_PDO" = "no" && test "$ext_shared" = "no"; then
AC_MSG_ERROR([PDO is not enabled! Add --enable-pdo to your configure line.])
fi

PHP_CHECK_PDO_INCLUDES

PKG_CHECK_MODULES([SQLITE], [sqlite3 >= 3.7.7])

PHP_EVAL_INCLINE($SQLITE_CFLAGS)
PHP_EVAL_LIBLINE($SQLITE_LIBS, SWOOLE_SHARED_LIBADD)
AC_DEFINE(HAVE_SW_PDO_SQLITELIB, 1, [Define to 1 if you have the pdo_sqlite extension enabled.])

PHP_CHECK_LIBRARY(sqlite3, sqlite3_close_v2, [
AC_DEFINE(HAVE_SW_SQLITE3_CLOSE_V2, 1, [have sqlite3_close_v2])
], [], [$SWOOLE_SHARED_LIBADD])

PHP_CHECK_LIBRARY(sqlite3, sqlite3_column_table_name, [
AC_DEFINE(HAVE_SW_SQLITE3_COLUMN_TABLE_NAME, 1, [have sqlite3_column_table_name])
], [], [$SWOOLE_SHARED_LIBADD])

AC_DEFINE(SW_USE_SQLITE, 1, [do we enable sqlite coro support])
fi
dnl sqlite stop

AC_CHECK_LIB(z, gzgets, [
AC_DEFINE(SW_HAVE_COMPRESSION, 1, [have compression])
AC_DEFINE(SW_HAVE_ZLIB, 1, [have zlib])
Expand Down Expand Up @@ -989,6 +1021,7 @@ EOF
ext-src/swoole_pgsql.cc \
ext-src/swoole_odbc.cc \
ext-src/swoole_oracle.cc \
ext-src/swoole_sqlite.cc \
ext-src/swoole_process.cc \
ext-src/swoole_process_pool.cc \
ext-src/swoole_redis_coro.cc \
Expand Down Expand Up @@ -1121,15 +1154,21 @@ EOF
thirdparty/php81/pdo_oci/oci_statement.c"
fi



if test "$PHP_PDO_ODBC" != "no"; then
swoole_source_file="$swoole_source_file \
thirdparty/php80/pdo_odbc/odbc_driver.c \
thirdparty/php80/pdo_odbc/odbc_stmt.c \
thirdparty/php81/pdo_odbc/odbc_driver.c \
thirdparty/php81/pdo_odbc/odbc_stmt.c"
fi

if test "$PHP_SWOOLE_SQLITE" != "no"; then
swoole_source_file="$swoole_source_file \
thirdparty/php80/pdo_sqlite/sqlite_driver.c \
thirdparty/php80/pdo_sqlite/sqlite_statement.c \
thirdparty/php81/pdo_sqlite/sqlite_driver.c \
thirdparty/php81/pdo_sqlite/sqlite_statement.c"
fi

SW_ASM_DIR="thirdparty/boost/asm/"
SW_USE_ASM_CONTEXT="yes"
Expand Down
15 changes: 15 additions & 0 deletions examples/runtime/sqlite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

Co::set(['hook_flags'=> SWOOLE_HOOK_PDO_SQLITE]);

run(function() {
$db = new PDO('sqlite::memory:');
for ($i = 0; $i < 10; $i++) {
go(function() use($i, $db) {
$db->query('select randomblob(99999999)');
var_dump($i);
});
}
});
15 changes: 13 additions & 2 deletions ext-src/php_swoole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static const zend_module_dep swoole_deps[] = {
#ifdef SW_USE_CURL
ZEND_MOD_REQUIRED("curl")
#endif
#if defined(SW_USE_PGSQL) || defined(SW_USE_ORACLE)
#if defined(SW_USE_PGSQL) || defined(SW_USE_ORACLE) || defined(SW_USE_SQLITE)
ZEND_MOD_REQUIRED("pdo")
#endif

Expand Down Expand Up @@ -748,6 +748,10 @@ PHP_MINIT_FUNCTION(swoole) {
php_swoole_oracle_minit(module_number);
#endif

#ifdef SW_USE_SQLITE
php_swoole_sqlite_minit(module_number);
#endif

SwooleG.fatal_error = fatal_error;
Socket::default_buffer_size = SWOOLE_G(socket_buffer_size);
SwooleG.dns_cache_refresh_time = 60;
Expand Down Expand Up @@ -792,6 +796,10 @@ PHP_MSHUTDOWN_FUNCTION(swoole) {
php_swoole_oracle_mshutdown();
#endif

#ifdef SW_USE_SQLITE
php_swoole_sqlite_mshutdown();
#endif

swoole_clean();

return SUCCESS;
Expand Down Expand Up @@ -906,7 +914,10 @@ PHP_MINFO_FUNCTION(swoole) {
php_info_print_table_row(2, "coroutine_odbc", "enabled");
#endif
#ifdef SW_USE_ORACLE
php_info_print_table_row(2, "coroutine_oracle", "enabled");
php_info_print_table_row(2, "coroutine_oracle", "enabled");
#endif
#ifdef SW_USE_SQLITR
php_info_print_table_row(2, "coroutine_sqlite", "enabled");
#endif
php_info_print_table_end();

Expand Down
1 change: 1 addition & 0 deletions ext-src/php_swoole_coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class PHPCoroutine {
HOOK_PDO_PGSQL = 1u << 16,
HOOK_PDO_ODBC = 1u << 17,
HOOK_PDO_ORACLE = 1u << 18,
HOOK_PDO_SQLITE = 1u << 19,
#ifdef SW_USE_CURL
HOOK_ALL = 0x7fffffff ^ HOOK_CURL,
#else
Expand Down
2 changes: 1 addition & 1 deletion ext-src/php_swoole_oracle.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
BEGIN_EXTERN_C()

#include "ext/pdo/php_pdo_driver.h"
#if PHP_VERSION_ID > 80100
#if PHP_VERSION_ID >= 80100
#include "thirdparty/php81/pdo_oci/php_pdo_oci_int.h"
#else
#include "thirdparty/php80/pdo_oci/php_pdo_oci_int.h"
Expand Down
6 changes: 6 additions & 0 deletions ext-src/php_swoole_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ int php_swoole_odbc_minit(int module_id);
#ifdef SW_USE_ORACLE
void php_swoole_oracle_minit(int module_number);
#endif
#ifdef SW_USE_SQLITE
void php_swoole_sqlite_minit(int module_number);
#endif
// server
void php_swoole_server_minit(int module_number);
void php_swoole_server_port_minit(int module_number);
Expand Down Expand Up @@ -308,6 +311,9 @@ void php_swoole_pgsql_mshutdown();
#ifdef SW_USE_ORACLE
void php_swoole_oracle_mshutdown();
#endif
#ifdef SW_USE_SQLITE
void php_swoole_sqlite_mshutdown();
#endif

static sw_inline zend_bool php_swoole_websocket_frame_is_object(zval *zdata) {
return Z_TYPE_P(zdata) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zdata), swoole_websocket_frame_ce);
Expand Down
54 changes: 54 additions & 0 deletions ext-src/php_swoole_sqlite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| Copyright (c) 2012-2018 The Swoole Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: NathanFreeman <mariasocute@163.com> |
+----------------------------------------------------------------------+
*/
#ifndef SWOOLE_SRC_PHP_SWOOLE_SQLITE_H
#define SWOOLE_SRC_PHP_SWOOLE_SQLITE_H
#include "php_swoole.h"

#ifdef SW_USE_SQLITE

BEGIN_EXTERN_C()

#include "ext/pdo/php_pdo_driver.h"
#if PHP_VERSION_ID >= 80100
#include "thirdparty/php81/pdo_sqlite/php_pdo_sqlite_int.h"
#else
#include "thirdparty/php80/pdo_sqlite/php_pdo_sqlite_int.h"
#endif

extern const pdo_driver_t swoole_pdo_sqlite_driver;
void swoole_sqlite_set_blocking(bool blocking);

int swoole_sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs);
int swoole_sqlite3_prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **ppStmt, const char **pzTail);
int swoole_sqlite3_exec(
sqlite3 *, const char *sql, int (*callback)(void *, int, char **, char **), void *, char **errmsg);
int swoole_sqlite3_close(sqlite3 *db);
int swoole_sqlite3_close_v2(sqlite3 *db);
int swoole_sqlite3_step(sqlite3_stmt *stmt);

#ifdef SW_USE_SQLITE_HOOK
#define sqlite3_open_v2 swoole_sqlite3_open_v2
#define sqlite3_prepare_v2 swoole_sqlite3_prepare_v2
#define sqlite3_exec swoole_sqlite3_exec
#define sqlite3_close swoole_sqlite3_close
#define sqlite3_close_v2 swoole_sqlite3_close_v2
#define sqlite3_step swoole_sqlite3_step
#endif
END_EXTERN_C()
#endif
#endif
9 changes: 9 additions & 0 deletions ext-src/swoole_oracle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ static bool async(const std::function<void(void)> &fn) {
}

sword swoole_oci_session_begin(OCISvcCtx *svchp, OCIError *errhp, OCISession *usrhp, ub4 credt, ub4 mode) {
swoole_trace_log(SW_TRACE_CO_ORACLE, "oci_session_begin");
sword result = 0;
async([&]() { result = OCISessionBegin(svchp, errhp, usrhp, credt, mode); });

return result;
}

sword swoole_oci_server_detach(OCIServer *srvhp, OCIError *errhp, ub4 mode) {
swoole_trace_log(SW_TRACE_CO_ORACLE, "oci_server_detach");
sword result = 0;
async([&]() { result = OCIServerDetach(srvhp, errhp, mode); });

Expand All @@ -53,6 +55,7 @@ sword swoole_oci_server_detach(OCIServer *srvhp, OCIError *errhp, ub4 mode) {

sword swoole_oci_stmt_prepare(
OCIStmt *stmtp, OCIError *errhp, const OraText *stmt, ub4 stmt_len, ub4 language, ub4 mode) {
swoole_trace_log(SW_TRACE_CO_ORACLE, "oci_stmt_prepare");
sword result = 0;
async([&]() { result = OCIStmtPrepare(stmtp, errhp, stmt, stmt_len, language, mode); });

Expand All @@ -67,41 +70,47 @@ sword swoole_oci_stmt_execute(OCISvcCtx *svchp,
const OCISnapshot *snap_in,
OCISnapshot *snap_out,
ub4 mode) {
swoole_trace_log(SW_TRACE_CO_ORACLE, "oci_stmt_execute");
sword result = 0;
async([&]() { result = OCIStmtExecute(svchp, stmtp, errhp, iters, rowoff, snap_in, snap_out, mode); });

return result;
}

sword swoole_oci_stmt_fetch(OCIStmt *stmtp, OCIError *errhp, ub4 nrows, ub2 orientation, ub4 mode) {
swoole_trace_log(SW_TRACE_CO_ORACLE, "oci_stmt_fetch");
sword result = 0;
async([&]() { result = OCIStmtFetch(stmtp, errhp, nrows, orientation, mode); });

return result;
}

sword swoole_oci_stmt_fetch2(OCIStmt *stmtp, OCIError *errhp, ub4 nrows, ub2 orientation, sb4 scrollOffset, ub4 mode) {
swoole_trace_log(SW_TRACE_CO_ORACLE, "oci_stmt_fetch2");
sword result = 0;
async([&]() { result = OCIStmtFetch2(stmtp, errhp, nrows, orientation, scrollOffset, mode); });

return result;
}

sword swoole_oci_trans_commit(OCISvcCtx *svchp, OCIError *errhp, ub4 flags) {
swoole_trace_log(SW_TRACE_CO_ORACLE, "oci_trans_commit");
sword result = 0;
async([&]() { result = OCITransCommit(svchp, errhp, flags); });

return result;
}

sword swoole_oci_trans_rollback(OCISvcCtx *svchp, OCIError *errhp, ub4 flags) {
swoole_trace_log(SW_TRACE_CO_ORACLE, "oci_trans_rollback");
sword result = 0;
async([&]() { result = OCITransRollback(svchp, errhp, flags); });

return result;
}

sword swoole_oci_ping(OCISvcCtx *svchp, OCIError *errhp, ub4 mode) {
swoole_trace_log(SW_TRACE_CO_ORACLE, "oci_ping");
sword result = 0;
async([&]() { result = OCIPing(svchp, errhp, mode); });

Expand Down
20 changes: 19 additions & 1 deletion ext-src/swoole_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ extern void swoole_odbc_set_blocking(bool blocking);
#ifdef SW_USE_ORACLE
extern void swoole_oracle_set_blocking(bool blocking);
#endif

#ifdef SW_USE_SQLITE
extern void swoole_sqlite_set_blocking(bool blocking);
#endif
END_EXTERN_C()

/* openssl */
Expand Down Expand Up @@ -205,6 +209,9 @@ void php_swoole_runtime_minit(int module_number) {
#endif
#ifdef SW_USE_ORACLE
SW_REGISTER_LONG_CONSTANT("SWOOLE_HOOK_PDO_ORACLE", PHPCoroutine::HOOK_PDO_ORACLE);
#endif
#ifdef SW_USE_SQLITE
SW_REGISTER_LONG_CONSTANT("SWOOLE_HOOK_PDO_SQLITE", PHPCoroutine::HOOK_PDO_SQLITE);
#endif
SW_REGISTER_LONG_CONSTANT("SWOOLE_HOOK_ALL", PHPCoroutine::HOOK_ALL);
#ifdef SW_USE_CURL
Expand Down Expand Up @@ -1280,7 +1287,7 @@ bool PHPCoroutine::enable_hook(uint32_t flags) {
}
#endif
#ifdef SW_USE_ORACLE
if (flags & PHPCoroutine::HOOK_PDO_ORACLE) {
if (flags & PHPCoroutine::HOOK_PDO_ORACLE) {
if (!(runtime_hook_flags & PHPCoroutine::HOOK_PDO_ORACLE)) {
swoole_oracle_set_blocking(0);
}
Expand All @@ -1289,6 +1296,17 @@ bool PHPCoroutine::enable_hook(uint32_t flags) {
swoole_oracle_set_blocking(1);
}
}
#endif
#ifdef SW_USE_SQLITE
if (flags & PHPCoroutine::HOOK_PDO_SQLITE) {
if (!(runtime_hook_flags & PHPCoroutine::HOOK_PDO_SQLITE)) {
swoole_sqlite_set_blocking(0);
}
} else {
if (runtime_hook_flags & PHPCoroutine::HOOK_PDO_SQLITE) {
swoole_sqlite_set_blocking(1);
}
}
#endif
if (flags & PHPCoroutine::HOOK_STREAM_FUNCTION) {
if (!(runtime_hook_flags & PHPCoroutine::HOOK_STREAM_FUNCTION)) {
Expand Down
Loading

0 comments on commit d8c2b55

Please sign in to comment.