From 732c467efd5edfce70480ae3ae4d17cd180dc90b Mon Sep 17 00:00:00 2001 From: Roman Tsisyk <roman@tsisyk.com> Date: Tue, 28 Apr 2015 14:42:43 +0300 Subject: [PATCH] Extract mysql and pg modules into separate repositories - mysql moved to https://github.com/tarantool/mysql - pg moved to https://github.com/tarantool/pg Please consult documentation on GitHub for more details. --- cmake/FindMySQL.cmake | 24 - cmake/FindPostgreSQL.cmake | 154 ------ debian/changelog | 6 + debian/control | 86 +--- debian/rules | 5 +- debian/tarantool-modules.install | 1 - debian/tarantool-mysql-module.install | 1 - debian/tarantool-postgresql-module.install | 1 - extra/rpm/tarantool-scl.rpm.spec | 3 - extra/rpm/tarantool.rpm.spec.in | 74 +-- src/module/CMakeLists.txt | 3 - src/module/mysql/CMakeLists.txt | 12 - src/module/mysql/mysql.cc | 472 ------------------ src/module/pg/CMakeLists.txt | 13 - src/module/pg/pg.cc | 527 --------------------- src/module/sql/CMakeLists.txt | 3 - src/module/sql/sql.lua | 227 --------- test/module/net_sql.common.result | 13 - test/module/net_sql.common.test.lua | 6 - test/module/net_sql.mysql.result | 136 ------ test/module/net_sql.mysql.skipcond | 9 - test/module/net_sql.mysql.test.lua | 55 --- test/module/net_sql.pg.result | 190 -------- test/module/net_sql.pg.skipcond | 9 - test/module/net_sql.pg.test.lua | 64 --- 25 files changed, 10 insertions(+), 2084 deletions(-) delete mode 100644 cmake/FindMySQL.cmake delete mode 100644 cmake/FindPostgreSQL.cmake delete mode 100644 debian/tarantool-modules.install delete mode 100644 debian/tarantool-mysql-module.install delete mode 100644 debian/tarantool-postgresql-module.install delete mode 100644 src/module/mysql/CMakeLists.txt delete mode 100644 src/module/mysql/mysql.cc delete mode 100644 src/module/pg/CMakeLists.txt delete mode 100644 src/module/pg/pg.cc delete mode 100644 src/module/sql/CMakeLists.txt delete mode 100644 src/module/sql/sql.lua delete mode 100644 test/module/net_sql.common.result delete mode 100644 test/module/net_sql.common.test.lua delete mode 100644 test/module/net_sql.mysql.result delete mode 100644 test/module/net_sql.mysql.skipcond delete mode 100644 test/module/net_sql.mysql.test.lua delete mode 100644 test/module/net_sql.pg.result delete mode 100644 test/module/net_sql.pg.skipcond delete mode 100644 test/module/net_sql.pg.test.lua diff --git a/cmake/FindMySQL.cmake b/cmake/FindMySQL.cmake deleted file mode 100644 index 815f44c391..0000000000 --- a/cmake/FindMySQL.cmake +++ /dev/null @@ -1,24 +0,0 @@ -find_path(MYSQL_INCLUDE_DIR - NAMES mysql.h - PATH_SUFFIXES mysql -) -find_library(MYSQL_LIBRARIES - NAMES mysqlclient_r - PATH_SUFFIXES mysql -) - -if(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES) - set(MYSQL_FOUND ON) -endif(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES) - -if(MYSQL_FOUND) - if (NOT MYSQL_FIND_QUIETLY) - message(STATUS "Found MySQL includes: ${MYSQL_INCLUDE_DIR}/mysql.h") - message(STATUS "Found MySQL library: ${MYSQL_LIBRARIES}") - endif (NOT MYSQL_FIND_QUIETLY) - set(MYSQL_INCLUDE_DIRS ${MYSQL_INCLUDE_DIR}) -else(MYSQL_FOUND) - if (MYSQL_FIND_REQUIRED) - message(FATAL_ERROR "Could not find mysql development files") - endif (MYSQL_FIND_REQUIRED) -endif (MYSQL_FOUND) diff --git a/cmake/FindPostgreSQL.cmake b/cmake/FindPostgreSQL.cmake deleted file mode 100644 index b741305b9f..0000000000 --- a/cmake/FindPostgreSQL.cmake +++ /dev/null @@ -1,154 +0,0 @@ -# - Find the PostgreSQL installation. -# In Windows, we make the assumption that, if the PostgreSQL files are installed, the default directory -# will be C:\Program Files\PostgreSQL. -# -# This module defines -# PostgreSQL_LIBRARIES - the PostgreSQL libraries needed for linking -# PostgreSQL_INCLUDE_DIRS - the directories of the PostgreSQL headers -# PostgreSQL_VERSION_STRING - the version of PostgreSQL found (since CMake 2.8.8) - -#============================================================================= -# Copyright 2004-2009 Kitware, Inc. -# -# Distributed under the OSI-approved BSD License (the "License"); -# see accompanying file Copyright.txt for details. -# -# This software is distributed WITHOUT ANY WARRANTY; without even the -# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -# See the License for more information. -#============================================================================= -# (To distribute this file outside of CMake, substitute the full -# License text for the above reference.) - -# ---------------------------------------------------------------------------- -# History: -# This module is derived from the module originally found in the VTK source tree. -# -# ---------------------------------------------------------------------------- -# Note: -# PostgreSQL_ADDITIONAL_VERSIONS is a variable that can be used to set the -# version mumber of the implementation of PostgreSQL. -# In Windows the default installation of PostgreSQL uses that as part of the path. -# E.g C:\Program Files\PostgreSQL\8.4. -# Currently, the following version numbers are known to this module: -# "9.1" "9.0" "8.4" "8.3" "8.2" "8.1" "8.0" -# -# To use this variable just do something like this: -# set(PostgreSQL_ADDITIONAL_VERSIONS "9.2" "8.4.4") -# before calling find_package(PostgreSQL) in your CMakeLists.txt file. -# This will mean that the versions you set here will be found first in the order -# specified before the default ones are searched. -# -# ---------------------------------------------------------------------------- -# You may need to manually set: -# PostgreSQL_INCLUDE_DIR - the path to where the PostgreSQL include files are. -# PostgreSQL_LIBRARY_DIR - The path to where the PostgreSQL library files are. -# If FindPostgreSQL.cmake cannot find the include files or the library files. -# -# ---------------------------------------------------------------------------- -# The following variables are set if PostgreSQL is found: -# PostgreSQL_FOUND - Set to true when PostgreSQL is found. -# PostgreSQL_INCLUDE_DIRS - Include directories for PostgreSQL -# PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries -# PostgreSQL_LIBRARIES - The PostgreSQL libraries. -# -# ---------------------------------------------------------------------------- -# If you have installed PostgreSQL in a non-standard location. -# (Please note that in the following comments, it is assumed that <Your Path> -# points to the root directory of the include directory of PostgreSQL.) -# Then you have three options. -# 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and -# PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is -# 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path() -# to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file -# set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include") -# 3) Set an environment variable called ${PostgreSQL_ROOT} that points to the root of where you have -# installed PostgreSQL, e.g. <Your Path>. -# -# ---------------------------------------------------------------------------- - -set(PostgreSQL_INCLUDE_PATH_DESCRIPTION "top-level directory containing the PostgreSQL include directories. E.g /usr/local/include/PostgreSQL/8.4 or C:/Program Files/PostgreSQL/8.4/include") -set(PostgreSQL_INCLUDE_DIR_MESSAGE "Set the PostgreSQL_INCLUDE_DIR cmake cache entry to the ${PostgreSQL_INCLUDE_PATH_DESCRIPTION}") -set(PostgreSQL_LIBRARY_PATH_DESCRIPTION "top-level directory containing the PostgreSQL libraries.") -set(PostgreSQL_LIBRARY_DIR_MESSAGE "Set the PostgreSQL_LIBRARY_DIR cmake cache entry to the ${PostgreSQL_LIBRARY_PATH_DESCRIPTION}") -set(PostgreSQL_ROOT_DIR_MESSAGE "Set the PostgreSQL_ROOT system variable to where PostgreSQL is found on the machine E.g C:/Program Files/PostgreSQL/8.4") - -set(PostgreSQL_KNOWN_ROOTS "/usr/include" "/usr/include/postgres" - "/usr/include/pgsql" "/usr/include/postgresql") -set(PostgreSQL_KNOWN_VERSIONS "9.3" "9.2" "9.1" "9.0") - -set(PostgreSQL_ROOT_DIRECTORIES) -foreach (root ${PostgreSQL_KNOWN_ROOTS} ) - list(APPEND PostgreSQL_ROOT_DIRECTORIES "${root}") - foreach (version ${PostgreSQL_KNOWN_VERSIONS} ) - list(APPEND PostgreSQL_ROOT_DIRECTORIES "${root}/${version}" ) - endforeach() -endforeach() - -foreach (version ${PostgreSQL_KNOWN_VERSIONS} ) - list(APPEND PostgreSQL_ROOT_DIRECTORIES "/usr/pgsql-${version}" ) -endforeach() - -# -# Look for an installation. -# -find_path(PostgreSQL_INCLUDE_DIR - NAMES libpq-fe.h - PATHS - # Look in other places. - ${PostgreSQL_ROOT_DIRECTORIES} - PATH_SUFFIXES - pgsql - postgresql - include - # Help the user find it if we cannot. - DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}" -) - -# The PostgreSQL library. -set (PostgreSQL_LIBRARY_TO_FIND pq) -# Setting some more prefixes for the library -set (PostgreSQL_LIB_PREFIX "") -if ( WIN32 ) - set (PostgreSQL_LIB_PREFIX ${PostgreSQL_LIB_PREFIX} "lib") - set ( PostgreSQL_LIBRARY_TO_FIND ${PostgreSQL_LIB_PREFIX}${PostgreSQL_LIBRARY_TO_FIND}) -endif() - -find_library( PostgreSQL_LIBRARY - NAMES ${PostgreSQL_LIBRARY_TO_FIND} - PATHS - ${PostgreSQL_ROOT_DIRECTORIES} - PATH_SUFFIXES - lib -) -get_filename_component(PostgreSQL_LIBRARY_DIR ${PostgreSQL_LIBRARY} PATH) - -if (PostgreSQL_INCLUDE_DIR AND EXISTS "${PostgreSQL_INCLUDE_DIR}/pg_config.h") - file(STRINGS "${PostgreSQL_INCLUDE_DIR}/pg_config.h" pgsql_version_str - REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"") - - string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*" "\\1" - PostgreSQL_VERSION_STRING "${pgsql_version_str}") - unset(pgsql_version_str) -endif() - -# Did we find anything? -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(PostgreSQL - REQUIRED_VARS PostgreSQL_LIBRARY PostgreSQL_INCLUDE_DIR - VERSION_VAR PostgreSQL_VERSION_STRING) -set( PostgreSQL_FOUND ${POSTGRESQL_FOUND}) - -# Now try to get the include and library path. -if(PostgreSQL_FOUND) - - set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR} ) - set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR} ) - set(PostgreSQL_LIBRARIES ${PostgreSQL_LIBRARY_TO_FIND}) - - #message("Final PostgreSQL include dir: ${PostgreSQL_INCLUDE_DIRS}") - #message("Final PostgreSQL library dir: ${PostgreSQL_LIBRARY_DIRS}") - #message("Final PostgreSQL libraries: ${PostgreSQL_LIBRARIES}") -endif() - -mark_as_advanced(PostgreSQL_INCLUDE_DIR PostgreSQL_LIBRARY ) diff --git a/debian/changelog b/debian/changelog index f17f760209..582b13db5a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +tarantool (1.6.5.20.g51b2e0d-1) unstable; urgency=medium + + * Remove tarantool-mysql-module and tarantool-pg-module. + + -- Roman Tsisyk <roman@tarantool.org> Tue, 28 Apr 2015 14:34:17 +0300 + tarantool (1.6.3.539.g211672f-1) unstable; urgency=medium * Init infrastructure for tarantool 1.6. diff --git a/debian/control b/debian/control index 324f2aae90..01285666fe 100644 --- a/debian/control +++ b/debian/control @@ -6,9 +6,7 @@ Build-Depends: cdbs, debhelper (>= 8), libreadline-dev, libncurses5-dev, libiberty-dev | binutils-dev, - libbfd-dev | binutils-dev, - libmysqlclient-dev, - libpq-dev + libbfd-dev | binutils-dev Section: database Standards-Version: 3.9.6 Homepage: http://tarantool.org/ @@ -75,88 +73,6 @@ Description: In-memory database with Lua application server . This package provides Tarantool server. -Package: tarantool-modules -Conflicts: tarantool-plugins (<< 1.5.3), - tarantool-lts-common, - tarantool-lts, - tarantool-lts-modules, - tarantool-lts-postgresql-module, - tarantool-lts-mysql-module, - tarantool-lts-dev, - tarantool-dbg (<< 1.5.2), - tarantool-common (<< 1.6~), - tarantool-client (<< 1.6~), - tarantool-client-dbg (<< 1.6~), - tarantool-plugins (<< 1.6~), - tarantool-mysql-plugin (<< 1.6~), - tarantool-postgresql-plugin (<< 1.6~), - libtarantool-dev (<< 1.6~) -Replaces: tarantool-plugins (<< 1.5.3), tarantool-lts-modules -Architecture: i386 amd64 kfreebsd-i386 kfreebsd-amd64 hurd-i386 -Priority: optional -Depends: - ${shlibs:Depends}, - ${misc:Depends}, - tarantool (= ${binary:Version}) -Description: Tarantool in-memory database - all plug-ins bundle - Tarantool is an in-memory database and Lua application server. - This package provides (or depends on) all Tarantool additional - modules. - -Package: tarantool-postgresql-module -Conflicts: tarantool-postgresql-plugin (<< 1.5.3), - tarantool-lts-common, - tarantool-lts, - tarantool-lts-modules, - tarantool-lts-postgresql-module, - tarantool-lts-mysql-module, - tarantool-lts-dev, - tarantool-dbg (<< 1.5.2), - tarantool-common (<< 1.6~), - tarantool-client (<< 1.6~), - tarantool-client-dbg (<< 1.6~), - tarantool-plugins (<< 1.6~), - tarantool-mysql-plugin (<< 1.6~), - tarantool-postgresql-plugin (<< 1.6~), - libtarantool-dev (<< 1.6~) -Replaces: tarantool-postgresql-plugin, tarantool-lts-postgresql-module -Architecture: i386 amd64 kfreebsd-i386 kfreebsd-amd64 hurd-i386 -Priority: optional -Depends: ${shlibs:Depends}, ${misc:Depends}, - tarantool-modules (= ${binary:Version}), - tarantool (= ${binary:Version}) -Description: Tarantool in-memory database - PostgreSQL connector - Tarantool is an in-memory database and Lua application server. - This package contains a connector to PostgreSQL database for - Tarantool Lua stored procedures. - -Package: tarantool-mysql-module -Conflicts: tarantool-mysql-plugin (<< 1.5.3), - tarantool-lts-common, - tarantool-lts, - tarantool-lts-modules, - tarantool-lts-postgresql-module, - tarantool-lts-mysql-module, - tarantool-lts-dev, - tarantool-dbg (<< 1.5.2), - tarantool-common (<< 1.6~), - tarantool-client (<< 1.6~), - tarantool-client-dbg (<< 1.6~), - tarantool-plugins (<< 1.6~), - tarantool-mysql-plugin (<< 1.6~), - tarantool-postgresql-plugin (<< 1.6~), - libtarantool-dev (<< 1.6~) -Replaces: tarantool-mysql-plugin, tarantool-lts-mysql-module -Architecture: i386 amd64 kfreebsd-i386 kfreebsd-amd64 hurd-i386 -Priority: optional -Depends: ${shlibs:Depends}, ${misc:Depends}, - tarantool-modules (= ${binary:Version}), - tarantool (= ${binary:Version}) -Description: Tarantool in-memory database - MySQL connector - Tarantool is an in-memory database and Lua application server. - This package contains a connector to MySQL database for Tarantool - Lua stored procedures. - Package: tarantool-dev Architecture: i386 amd64 kfreebsd-i386 kfreebsd-amd64 hurd-i386 Priority: optional diff --git a/debian/rules b/debian/rules index e647e764ac..47c4285bae 100755 --- a/debian/rules +++ b/debian/rules @@ -7,10 +7,7 @@ VERSION := $(shell dpkg-parsechangelog|grep ^Version|awk '{print $$2}') UVERSION := $(shell echo $(VERSION)|sed 's/-[[:digit:]]\+$$//') DEB_CMAKE_EXTRA_FLAGS := \ - -DCMAKE_BUILD_TYPE=RelWithDebugInfo \ - -DWITH_MYSQL=ON \ - -DWITH_POSTGRESQL=ON -# -DENABLE_CLIENT=true \ + -DCMAKE_BUILD_TYPE=RelWithDebugInfo DEB_DH_INSTALLINIT_ARGS := --name=tarantool diff --git a/debian/tarantool-modules.install b/debian/tarantool-modules.install deleted file mode 100644 index 0bf50083b8..0000000000 --- a/debian/tarantool-modules.install +++ /dev/null @@ -1 +0,0 @@ -usr/share/tarantool/box/net/sql.lua diff --git a/debian/tarantool-mysql-module.install b/debian/tarantool-mysql-module.install deleted file mode 100644 index ce433b6764..0000000000 --- a/debian/tarantool-mysql-module.install +++ /dev/null @@ -1 +0,0 @@ -usr/lib/tarantool/box/net/mysql.so diff --git a/debian/tarantool-postgresql-module.install b/debian/tarantool-postgresql-module.install deleted file mode 100644 index e8ad91381a..0000000000 --- a/debian/tarantool-postgresql-module.install +++ /dev/null @@ -1 +0,0 @@ -usr/lib/tarantool/box/net/pg.so diff --git a/extra/rpm/tarantool-scl.rpm.spec b/extra/rpm/tarantool-scl.rpm.spec index 4c08ae6215..0c8255ff3b 100644 --- a/extra/rpm/tarantool-scl.rpm.spec +++ b/extra/rpm/tarantool-scl.rpm.spec @@ -21,9 +21,6 @@ License: BSD Group: Applications/File Requires: %{scl_prefix}tarantool Requires: %{scl_prefix}tarantool-dev -Requires: %{scl_prefix}tarantool-sql-module -Requires: %{scl_prefix}tarantool-pg-module -Requires: %{scl_prefix}tarantool-mysql-module Requires: scl-utils %description diff --git a/extra/rpm/tarantool.rpm.spec.in b/extra/rpm/tarantool.rpm.spec.in index 0b883ddda1..6669f181e6 100644 --- a/extra/rpm/tarantool.rpm.spec.in +++ b/extra/rpm/tarantool.rpm.spec.in @@ -17,8 +17,6 @@ %global _with_systemd 1 %endif -%bcond_without postgresql -%bcond_without mysql %bcond_with systemd BuildRequires: readline-devel @@ -102,53 +100,6 @@ It supports replication, online backup, stored procedures in Lua. This package provides common files -# Tarantool sql module -%package sql-module -Summary: Tarantool common sql interface -Vendor: tarantool.org -Group: Applications/Databases -Requires: %{?scl_prefix}tarantool = @RPM_PACKAGE_VERSION@-@RPM_PACKAGE_RELEASE@ -%description sql-module -Tarantool is a high performance in-memory NoSQL database. -It supports replication, online backup, stored procedures in Lua. - -This package provides a common sql interface to use with -tarantool-pg-module or by tarantool-mysql-module. - -%if %{with postgresql} -%package pg-module -Summary: Tarantool PostgreSQL interface -Vendor: tarantool.org -Group: Applications/Databases -Provides: %{?scl_prefix}tarantool-pg-module -BuildRequires: postgresql-devel >= 9.0 -Requires: postgresql-libs >= 9.0 -Requires: %{?scl_prefix}tarantool = @RPM_PACKAGE_VERSION@-@RPM_PACKAGE_RELEASE@ -%description pg-module -Tarantool is a high performance in-memory NoSQL database. -It supports replication, online backup, stored procedures in Lua. - -This package provides a PostgreSQL interface to use with -tarantool-sql-module. -%endif - -%if %{with mysql} -%package mysql-module -Summary: Tarantool MySQL interface -Vendor: tarantool.org -Group: Applications/Databases -Provides: %{?scl_prefix}tarantool-mysql-module -BuildRequires: mysql-devel >= 5.0 -Requires: mysql-libs >= 5.0 -Requires: %{?scl_prefix}tarantool = @RPM_PACKAGE_VERSION@-@RPM_PACKAGE_RELEASE@ -%description mysql-module -Tarantool is a high performance in-memory NoSQL database. -It supports replication, online backup, stored procedures in Lua. - -This package provides a MySQL interface to use with -tarantool-sql-module. -%endif - ################################################################## %prep @@ -207,8 +158,6 @@ tarantool-sql-module. .. ' %{!?scl:-DCMAKE_INSTALL_SYSCONFDIR=%{_sysconfdir}}' .. ' %{!?scl:-DENABLE_RPM=ON}' .. ' %{?scl:-DENABLE_RPM_SCL=ON}' - .. dev_with('postgresql', 'WITH_POSTGRESQL') - .. dev_with('mysql', 'WITH_MYSQL') .. dev_with('systemd', 'WITH_SYSTEMD') .. dev_with_kv('systemd', 'SYSTEMD_SERVICES_INSTALL_DIR', '%{_unitdir}') @@ -269,27 +218,6 @@ chkconfig --del tarantool "%{_mandir}/man1/tarantool.1.gz" -%files sql-module -%defattr(-,root,root,-) -%dir "%{_datadir}/tarantool" -%dir "%{_datadir}/tarantool/box" -%dir "%{_datadir}/tarantool/box/net" -"%{_datadir}/tarantool/box/net/sql.lua" - -%if %{with postgresql} -%files pg-module -%defattr(-,root,root,-) -%dir "%{_libdir}/tarantool/box" -"%{_libdir}/tarantool/box/net/pg.so" -%endif - -%if %{with mysql} -%files mysql-module -%defattr(-,root,root,-) -%dir "%{_libdir}/tarantool/box" -"%{_libdir}/tarantool/box/net/mysql.so" -%endif - %files dev %defattr(-,root,root,-) %dir "%{_includedir}/tarantool" @@ -315,6 +243,8 @@ chkconfig --del tarantool %endif %changelog +* Tue Apr 28 2015 roman@tarantool.org <roman@tarantool.org> 1.0-3 +- Remove sql-module, pg-module, mysql-module * Fri Jun 06 2014 Eugine Blikh <bigbes@tarantool.org> 1.0-2 - Add SCL support - Add --with support diff --git a/src/module/CMakeLists.txt b/src/module/CMakeLists.txt index 3929198f70..841d7cc277 100644 --- a/src/module/CMakeLists.txt +++ b/src/module/CMakeLists.txt @@ -1,4 +1 @@ include_directories(${CMAKE_BINARY_DIR}/src/trivia/) -add_subdirectory(sql) -add_subdirectory(pg) -add_subdirectory(mysql) diff --git a/src/module/mysql/CMakeLists.txt b/src/module/mysql/CMakeLists.txt deleted file mode 100644 index 28a155f733..0000000000 --- a/src/module/mysql/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -find_optional_package(MySQL) - -if (MYSQL_FOUND) - include_directories(${MYSQL_INCLUDE_DIRS}) - add_library(mysql SHARED mysql.cc) - target_link_libraries(mysql ${MYSQL_LIBRARIES} -rdynamic) - set_target_properties(mysql PROPERTIES PREFIX "") - add_dependencies(mysql generate_module_api) - install(TARGETS mysql LIBRARY DESTINATION ${MODULE_LIBDIR}/box/net) -else() - message(STATUS "MySQL client not found, box.net.sql(mysql) disabled") -endif() diff --git a/src/module/mysql/mysql.cc b/src/module/mysql/mysql.cc deleted file mode 100644 index ef012974c0..0000000000 --- a/src/module/mysql/mysql.cc +++ /dev/null @@ -1,472 +0,0 @@ -/* - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <tarantool.h> - -#include <stddef.h> - -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} - -#include <stdio.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> - -#include <mysql.h> - -/** - * gets MYSQL connector from lua stack (or object) - */ -static MYSQL * -lua_check_mysql(struct lua_State *L, int index) -{ - int pop = 0; - if (lua_istable(L, index)) { - if (index < 0) - index--; - lua_pushstring(L, "raw"); - lua_rawget(L, index); - - pop = 1; - index = -1; - } - - if (!lua_isuserdata(L, index)) - luaL_error(L, "Can't extract userdata from lua-stack"); - - MYSQL *mysql = *(MYSQL **)lua_touserdata(L, index); - if (pop) - lua_pop(L, pop); - return mysql; -} - - -/** - * connect to MySQL server - */ -static ssize_t -connect_mysql(va_list ap) -{ - MYSQL *mysql = va_arg(ap, MYSQL*); - const char *host = va_arg(ap, const char*); - const char *port = va_arg(ap, const char*); - const char *user = va_arg(ap, const char*); - const char *password = va_arg(ap, const char*); - const char *db = va_arg(ap, const char*); - - - int iport = 0; - const char *usocket = 0; - - if (strcmp(host, "unix/") == 0) { - usocket = port; - host = NULL; - } else { - iport = atoi(port); - if (iport == 0) - iport = 3306; - } - - mysql_real_connect(mysql, host, user, password, db, iport, usocket, - CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS); - return 0; -} - - -/** - * returns self.field as C-string - */ -static const char * -self_field(struct lua_State *L, const char *name, int index) -{ - lua_pushstring(L, name); - if (index < 0) - index--; - lua_rawget(L, index); - const char *res; - if (lua_isnil(L, -1)) - res = NULL; - else - res = lua_tostring(L, -1); - lua_pop(L, 1); - return res; -} - - -/** - * execute request - */ -static ssize_t -exec_mysql(va_list ap) -{ - MYSQL *mysql = va_arg(ap, MYSQL*); - const char *sql = va_arg(ap, const char*); - size_t len = va_arg(ap, size_t); - - int res = mysql_real_query(mysql, sql, len); - if (res == 0) { - return 0; - } - return -2; -} - -/** - * fetch one result from socket - */ -static ssize_t -fetch_result(va_list ap) -{ - MYSQL *mysql = va_arg(ap, MYSQL*); - MYSQL_RES **result = va_arg(ap, MYSQL_RES**); - int resno = va_arg(ap, int); - if (resno) { - if (mysql_next_result(mysql) > 0) - return -2; - } - *result = mysql_store_result(mysql); - return 0; -} - -/** - * push results to lua stack - */ -static int -lua_mysql_pushresult(struct lua_State *L, MYSQL *mysql, - MYSQL_RES *result, int resno) -{ - int tidx; - - if (resno > 0) { - /* previous push is in already on stack */ - tidx = lua_gettop(L) - 1; - } else { - lua_newtable(L); - tidx = lua_gettop(L); - lua_pushnumber(L, 0); - } - - if (!result) { - if (mysql_field_count(mysql) == 0) { - double v = lua_tonumber(L, -1); - v += mysql_affected_rows(mysql); - lua_pop(L, 1); - lua_pushnumber(L, v); - return 2; - } - mysql_free_result(result); - luaL_error(L, "%s", mysql_error(mysql)); - } - - - MYSQL_ROW row; - MYSQL_FIELD * fields = mysql_fetch_fields(result); - while((row = mysql_fetch_row(result))) { - lua_pushnumber(L, luaL_getn(L, tidx) + 1); - lua_newtable(L); - - unsigned long *len = mysql_fetch_lengths(result); - - for (int i = 0; i < mysql_num_fields(result); i++) { - lua_pushstring(L, fields[i].name); - - - switch(fields[i].type) { - case MYSQL_TYPE_TINY: - case MYSQL_TYPE_SHORT: - case MYSQL_TYPE_LONG: - case MYSQL_TYPE_FLOAT: - case MYSQL_TYPE_INT24: - case MYSQL_TYPE_DOUBLE: { - lua_pushlstring(L, row[i], len[i]); - double v = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pushnumber(L, v); - break; - } - - case MYSQL_TYPE_NULL: - lua_pushnil(L); - break; - - - case MYSQL_TYPE_LONGLONG: - case MYSQL_TYPE_TIMESTAMP: { - long long v = atoll(row[i]); - luaL_pushuint64(L, v); - break; - } - - /* AS string */ - case MYSQL_TYPE_NEWDECIMAL: - case MYSQL_TYPE_DECIMAL: - default: - lua_pushlstring(L, row[i], len[i]); - break; - - } - lua_settable(L, -3); - } - - - lua_settable(L, tidx); - } - - /* sum(affected_rows) */ - double v = lua_tonumber(L, -1); - v += mysql_affected_rows(mysql); - lua_pop(L, 1); - lua_pushnumber(L, v); - mysql_free_result(result); - return 2; -} - -/** - * mysql:execute() method - */ -int -lua_mysql_execute(struct lua_State *L) -{ - MYSQL *mysql = lua_check_mysql(L, 1); - size_t len; - const char *sql = lua_tolstring(L, 2, &len); - - luaL_Buffer b; - luaL_buffinit(L, &b); - int idx = 3; - - char *q = NULL; - /* process placeholders */ - for (size_t i = 0; i < len; i++) { - if (sql[i] != '?') { - luaL_addchar(&b, sql[i]); - continue; - } - - if (lua_gettop(L) < idx) { - free(q); - luaL_error(L, - "Can't find value for %d placeholder", idx); - } - - if (lua_isboolean(L, idx)) { - int v = lua_toboolean(L, idx++); - luaL_addstring(&b, v ? "TRUE" : "FALSE"); - continue; - } - - if (lua_isnil(L, idx)) { - idx++; - luaL_addstring(&b, "NULL"); - continue; - } - - if (lua_isnumber(L, idx)) { - const char *s = lua_tostring(L, idx++); - luaL_addstring(&b, s); - continue; - } - - size_t l; - const char *s = lua_tolstring(L, idx++, &l); - char *nq = (char *)realloc(q, l * 2 + 1); - if (!nq) { - free(q); - luaL_error(L, "Can't allocate memory for variable"); - } - q = nq; - l = mysql_real_escape_string(mysql, q, s, l); - luaL_addchar(&b, '\''); - luaL_addlstring(&b, q, l); - luaL_addchar(&b, '\''); - } - free(q); - - luaL_pushresult(&b); - - sql = lua_tolstring(L, -1, &len); - - - int res = coio_call(exec_mysql, mysql, sql, len); - lua_pop(L, 1); - if (res == -1) - luaL_error(L, "%s", strerror(errno)); - - if (res != 0) - luaL_error(L, "%s", mysql_error(mysql)); - - int resno = 0; - do { - MYSQL_RES *result = NULL; - res = coio_call(fetch_result, mysql, &result, resno); - if (res == -1) - luaL_error(L, "%s", strerror(errno)); - - lua_mysql_pushresult(L, mysql, result, resno++); - - } while(mysql_more_results(mysql)); - - return 2; -} - - -/** - * collect MYSQL object - */ -int -lua_mysql_gc(struct lua_State *L) -{ - MYSQL *mysql = lua_check_mysql(L, 1); - mysql_close(mysql); - return 0; -} - -/** - * quote variable - */ -int -lua_mysql_quote(struct lua_State *L) -{ - MYSQL *mysql = lua_check_mysql(L, 1); - if (lua_gettop(L) < 2) { - lua_pushnil(L); - return 1; - } - - size_t len; - const char *s = lua_tolstring(L, -1, &len); - char *sout = (char*)malloc(len * 2 + 1); - if (!sout) { - luaL_error(L, "Can't allocate memory for variable"); - } - - len = mysql_real_escape_string(mysql, sout, s, len); - lua_pushlstring(L, sout, len); - free(sout); - return 1; -} - -/** - * connect to MYSQL server (lua binding) - */ -int -lbox_net_mysql_connect(struct lua_State *L) -{ - MYSQL *mysql = mysql_init(NULL); - if (!mysql) - luaL_error(L, "Can not allocate memory for connector"); - - const char *host = self_field(L, "host", 1); - const char *port = self_field(L, "port", 1); - const char *user = self_field(L, "user", 1); - const char *pass = self_field(L, "password", 1); - const char *db = self_field(L, "db", 1); - - - if (!host || (!port) || (!user) || (!pass) || (!db)) { - luaL_error(L, - "Usage: box.net.sql.connect" - "('mysql', host, port, user, password, db, ...)" - ); - } - - - if (coio_call(connect_mysql, mysql, host, port, user, pass, db) == -1) { - mysql_close(mysql); - luaL_error(L, "%s", strerror(errno)); - } - - if (*mysql_error(mysql)) { - luaL_Buffer b; - luaL_buffinit(L, &b); - luaL_addstring(&b, mysql_error(mysql)); - luaL_pushresult(&b); - mysql_close(mysql); - lua_error(L); - } - - lua_pushstring(L, "raw"); - MYSQL **ptr = (MYSQL **)lua_newuserdata(L, sizeof(mysql)); - *ptr = mysql; - - lua_newtable(L); - lua_pushstring(L, "__index"); - - lua_newtable(L); - - static const struct luaL_reg meta [] = { - {"execute", lua_mysql_execute}, - {"quote", lua_mysql_quote}, - {NULL, NULL} - }; - luaL_register(L, NULL, meta); - lua_settable(L, -3); - - lua_pushstring(L, "__gc"); - lua_pushcfunction(L, lua_mysql_gc); - lua_settable(L, -3); - - - lua_setmetatable(L, -2); - lua_rawset(L, 1); - - /* return self */ - lua_pushvalue(L, 1); - return 1; -} - -extern "C" { - int LUA_API luaopen_box_net_mysql(lua_State*); -} - -int LUA_API luaopen_box_net_mysql(lua_State *L) -{ - lua_getfield(L, LUA_GLOBALSINDEX, "box"); /* stack: box */ - - lua_pushstring(L, "net"); - lua_rawget(L, -2); /* stack: box.net */ - - lua_pushstring(L, "sql"); - lua_rawget(L, -2); /* stack: box.net.sql */ - - lua_pushstring(L, "connectors"); - lua_rawget(L, -2); /* stack: box.net.sql.connectors */ - - /* stack: box, box.net.sql.connectors */ - lua_pushstring(L, "mysql"); - lua_pushcfunction(L, lbox_net_mysql_connect); - lua_rawset(L, -3); - - /* cleanup stack */ - lua_pop(L, 4); - return 0; -} diff --git a/src/module/pg/CMakeLists.txt b/src/module/pg/CMakeLists.txt deleted file mode 100644 index 24aca28de8..0000000000 --- a/src/module/pg/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -find_optional_package(PostgreSQL) - -if (PostgreSQL_FOUND) - include_directories(${PostgreSQL_INCLUDE_DIRS}) - link_directories(${PostgreSQL_LIBRARY_DIRS}) - add_library(pg SHARED pg.cc) - target_link_libraries(pg ${PostgreSQL_LIBRARIES} -rdynamic) - set_target_properties(pg PROPERTIES PREFIX "") - add_dependencies(pg generate_module_api) - install(TARGETS pg LIBRARY DESTINATION ${MODULE_LIBDIR}/box/net) -else() - message(STATUS "PostgreSQL client not found, box.net.sql(pg) disabled") -endif() diff --git a/src/module/pg/pg.cc b/src/module/pg/pg.cc deleted file mode 100644 index 68efc0a40c..0000000000 --- a/src/module/pg/pg.cc +++ /dev/null @@ -1,527 +0,0 @@ -/* - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include <tarantool.h> - -extern "C" { - #include <libpq-fe.h> - #undef PACKAGE_VERSION -} - -/* PostgreSQL types (see catalog/pg_type.h) */ -#define INT2OID 21 -#define INT4OID 23 -#define INT8OID 20 -#define NUMERICOID 1700 -#define BOOLOID 16 -#define TEXTOID 25 - -#include <stddef.h> - -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} - -#include <stdio.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> - - -static PGconn * -lua_check_pgconn(struct lua_State *L, int index) -{ - int pop = 0; - if (lua_istable(L, index)) { - if (index < 0) - index--; - lua_pushstring(L, "raw"); - lua_rawget(L, index); - - pop = 1; - index = -1; - } - - if (!lua_isuserdata(L, index)) - luaL_error(L, "Can't extract userdata from lua-stack"); - - PGconn *conn = *(PGconn **)lua_touserdata(L, index); - if (pop) - lua_pop(L, pop); - return conn; -} - -/** do execute request (is run in the other thread) */ -static ssize_t -pg_exec(va_list ap) -{ - PGconn *conn = va_arg(ap, PGconn*); - const char *sql = va_arg(ap, const char*); - int count = va_arg(ap, int); - Oid *paramTypes = va_arg(ap, Oid*); - const char **paramValues = va_arg(ap, const char**); - const int *paramLengths = va_arg(ap, int*); - const int *paramFormats = va_arg(ap, int*); - PGresult **res = va_arg(ap, PGresult**); - - *res = PQexecParams(conn, sql, - count, paramTypes, paramValues, paramLengths, paramFormats, 0); - return 0; -} - - -/** push query result into lua stack */ -static int -lua_pg_pushresult(struct lua_State *L, PGresult *r) -{ - if (!r) - luaL_error(L, "PG internal error: zero rults"); - - switch(PQresultStatus(r)) { - case PGRES_COMMAND_OK: - lua_newtable(L); - if (*PQcmdTuples(r) == 0) { - lua_pushnumber(L, 0); - } else { - lua_pushstring(L, PQcmdTuples(r)); - double v = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pushnumber(L, v); - } - lua_pushstring(L, PQcmdStatus(r)); - PQclear(r); - return 3; - - case PGRES_TUPLES_OK: - break; - - case PGRES_BAD_RESPONSE: - PQclear(r); - luaL_error(L, "Broken postgresql response"); - return 0; - - case PGRES_FATAL_ERROR: - case PGRES_NONFATAL_ERROR: - case PGRES_EMPTY_QUERY: - lua_pushstring(L, PQresultErrorMessage(r)); - PQclear(r); - luaL_error(L, "%s", lua_tostring(L, -1)); - return 0; - - default: - luaL_error(L, "pg: unsupported result type"); - return 0; - } - - lua_newtable(L); - int count = PQntuples(r); - int cols = PQnfields(r); - for (int i = 0; i < count; i++) { - lua_pushnumber(L, i + 1); - lua_newtable(L); - - for (int j = 0; j < cols; j++) { - if (PQgetisnull(r, i, j)) - continue; - - lua_pushstring(L, PQfname(r, j)); - const char *s = PQgetvalue(r, i, j); - int len = PQgetlength(r, i, j); - - switch (PQftype(r, j)) { - case INT2OID: - case INT4OID: - case INT8OID: - case NUMERICOID: { - lua_pushlstring(L, s, len); - double v = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pushnumber(L, v); - break; - } - case BOOLOID: - if (*s == 't' || *s == 'T') - lua_pushboolean(L, 1); - else - lua_pushboolean(L, 0); - break; - - default: - lua_pushlstring(L, s, len); - break; - - } - - - lua_settable(L, -3); - } - - lua_settable(L, -3); - } - - if (*PQcmdTuples(r) == 0) { - lua_pushnumber(L, 0); - } else { - lua_pushstring(L, PQcmdTuples(r)); - double v = lua_tonumber(L, -1); - lua_pop(L, 1); - lua_pushnumber(L, v); - } - lua_pushstring(L, PQcmdStatus(r)); - PQclear(r); - return 3; -} - - -/** execute method */ -static int -lua_pg_execute(struct lua_State *L) -{ - PGconn *conn = lua_check_pgconn(L, 1); - const char *sql = lua_tostring(L, 2); - - int count = lua_gettop(L) - 2; - const char **paramValues = NULL; - int *paramLengths = NULL; - int *paramFormats = NULL; - Oid *paramTypes = NULL; - - if (count > 0) { - /* Allocate memory for params using lua_newuserdata */ - char *buf = (char *) lua_newuserdata(L, count * - (sizeof(*paramValues) + sizeof(*paramLengths) + - sizeof(*paramFormats) + sizeof(*paramTypes))); - - paramValues = (const char **) buf; - buf += count * sizeof(*paramValues); - paramLengths = (int *) buf; - buf += count * sizeof(*paramLengths); - paramFormats = (int *) buf; - buf += count * sizeof(*paramFormats); - paramTypes = (Oid *) buf; - buf += count * sizeof(*paramTypes); - - for(int i = 0, idx = 3; i < count; i++, idx++) { - if (lua_isnil(L, idx)) { - paramValues[i] = NULL; - paramLengths[i] = 0; - paramFormats[i] = 0; - paramTypes[i] = 0; - continue; - } - - if (lua_isboolean(L, idx)) { - int v = lua_toboolean(L, idx); - static const char pg_true[] = "t"; - static const char pg_false[] = "f"; - paramValues[i] = v ? pg_true : pg_false; - paramLengths[i] = 1; - paramFormats[i] = 0; - paramTypes[i] = BOOLOID; - continue; - } - - size_t len; - const char *s = lua_tolstring(L, idx, &len); - - if (lua_isnumber(L, idx)) { - paramTypes[i] = NUMERICOID; - paramValues[i] = s; - paramLengths[i] = len; - paramFormats[i] = 0; - continue; - } - - - paramValues[i] = s; - paramLengths[i] = len; - paramFormats[i] = 0; - paramTypes[i] = TEXTOID; - - } - - /* transform sql placeholders */ - luaL_Buffer b; - luaL_buffinit(L, &b); - char num[10]; - for (int i = 0, j = 1; sql[i]; i++) { - if (sql[i] != '?') { - luaL_addchar(&b, sql[i]); - continue; - } - luaL_addchar(&b, '$'); - - snprintf(num, 10, "%d", j++); - luaL_addstring(&b, num); - } - luaL_pushresult(&b); - sql = lua_tostring(L, -1); - } - - PGresult *res = NULL; - if (coio_call(pg_exec, conn, - sql, count, paramTypes, paramValues, - paramLengths, paramFormats, &res) == -1) { - - luaL_error(L, "Can't execute sql: %s", - strerror(errno)); - } - - lua_settop(L, 0); - return lua_pg_pushresult(L, res); -} - -/** - * collect connection - */ -static int -lua_pg_gc(struct lua_State *L) -{ - PGconn *conn = lua_check_pgconn(L, 1); - PQfinish(conn); - return 0; -} - -/** - * prints warnings from Postgresql into tarantool log - */ -static void -pg_notice(void *arg, const char *message) -{ - say_info("Postgresql: %s", message); - (void)arg; -} - -/** - * do connect to postgresql (is run in the other thread) - */ -static ssize_t -pg_connect(va_list ap) -{ - const char *constr = va_arg(ap, const char*); - PGconn **conn = va_arg(ap, PGconn**); - *conn = PQconnectdb(constr); - if (*conn) - PQsetNoticeProcessor(*conn, pg_notice, NULL); - return 0; -} - -/** - * returns self.field as C-string - */ -static const char * -self_field(struct lua_State *L, const char *name, int index) -{ - lua_pushstring(L, name); - if (index < 0) - index--; - lua_rawget(L, index); - const char *res; - if (lua_isnil(L, -1)) - res = NULL; - else - res = lua_tostring(L, -1); - lua_pop(L, 1); - return res; -} - - -/** - * quote variable - */ -static int -lua_pg_quote(struct lua_State *L) -{ - if (lua_gettop(L) < 2) { - lua_pushnil(L); - return 1; - } - PGconn *conn = lua_check_pgconn(L, 1); - size_t len; - const char *s = lua_tolstring(L, -1, &len); - - s = PQescapeLiteral(conn, s, len); - - if (!s) - luaL_error(L, "Can't allocate memory"); - lua_pushstring(L, s); - free((void *)s); - return 1; -} - - -/** - * quote identifier - */ -static int -lua_pg_quote_ident(struct lua_State *L) -{ - if (lua_gettop(L) < 2) { - lua_pushnil(L); - return 1; - } - PGconn *conn = lua_check_pgconn(L, 1); - size_t len; - const char *s = lua_tolstring(L, -1, &len); - - s = PQescapeIdentifier(conn, s, len); - - if (!s) - luaL_error(L, "Can't allocate memory"); - lua_pushstring(L, s); - free((void *)s); - return 1; -} - - -/** - * connect to postgresql - */ -static int -lbox_net_pg_connect(struct lua_State *L) -{ - const char *host = self_field(L, "host", 1); - const char *port = self_field(L, "port", 1); - const char *user = self_field(L, "user", 1); - const char *pass = self_field(L, "password", 1); - const char *db = self_field(L, "db", 1); - - - if (!host || (!port) || (!user) || (!pass) || (!db)) { - luaL_error(L, - "Usage: box.net.sql.connect" - "('pg', host, port, user, password, db, ...)" - ); - } - - PGconn *conn = NULL; - - luaL_Buffer b; - luaL_buffinit(L, &b); - luaL_addstring(&b, "host='"); - luaL_addstring(&b, host); - - luaL_addstring(&b, "' port='"); - luaL_addstring(&b, port); - - luaL_addstring(&b, "' user='"); - luaL_addstring(&b, user); - - luaL_addstring(&b, "' password='"); - luaL_addstring(&b, pass); - - luaL_addstring(&b, "' dbname='"); - luaL_addstring(&b, db); - - luaL_addchar(&b, '\''); - luaL_pushresult(&b); - - const char *constr = lua_tostring(L, -1); - - if (coio_call(pg_connect, constr, &conn) == -1) { - luaL_error(L, "Can't connect to postgresql: %s", - strerror(errno)); - } - - /* cleanup stack */ - lua_pop(L, 1); - - if (PQstatus(conn) != CONNECTION_OK) { - luaL_Buffer b; - luaL_buffinit(L, &b); - luaL_addstring(&b, PQerrorMessage(conn)); - luaL_pushresult(&b); - PQfinish(conn); - lua_error(L); - } - - lua_pushstring(L, "raw"); - PGconn **ptr = (PGconn **)lua_newuserdata(L, sizeof(conn)); - *ptr = conn; - - lua_newtable(L); - lua_pushstring(L, "__index"); - - lua_newtable(L); - - static const struct luaL_reg meta [] = { - {"execute", lua_pg_execute}, - {"quote", lua_pg_quote}, - {"quote_ident", lua_pg_quote_ident}, - {NULL, NULL} - }; - luaL_register(L, NULL, meta); - lua_settable(L, -3); - - lua_pushstring(L, "__gc"); - lua_pushcfunction(L, lua_pg_gc); - lua_settable(L, -3); - - - lua_setmetatable(L, -2); - lua_rawset(L, 1); - - /* return self */ - lua_pushvalue(L, 1); - return 1; -} - -extern "C" { - int LUA_API luaopen_box_net_pg(lua_State*); -} - -int LUA_API luaopen_box_net_pg(lua_State *L) -{ - lua_getfield(L, LUA_GLOBALSINDEX, "box"); /* stack: box */ - - lua_pushstring(L, "net"); - lua_rawget(L, -2); /* stack: box.net */ - - lua_pushstring(L, "sql"); - lua_rawget(L, -2); /* stack: box.net.sql */ - - lua_pushstring(L, "connectors"); - lua_rawget(L, -2); /* stack: box.net.sql.connectors */ - - /* stack: box, box.net.sql.connectors */ - lua_pushstring(L, "pg"); - lua_pushcfunction(L, lbox_net_pg_connect); - lua_rawset(L, -3); - - /* alias for driver */ - lua_pushstring(L, "postgresql"); - lua_pushcfunction(L, lbox_net_pg_connect); - lua_rawset(L, -3); - - /* cleanup stack */ - lua_pop(L, 4); - return 0; -} diff --git a/src/module/sql/CMakeLists.txt b/src/module/sql/CMakeLists.txt deleted file mode 100644 index 2c9f501601..0000000000 --- a/src/module/sql/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ - -install(FILES ${PROJECT_SOURCE_DIR}/src/module/sql/sql.lua - DESTINATION ${MODULE_LUADIR}/box/net) diff --git a/src/module/sql/sql.lua b/src/module/sql/sql.lua deleted file mode 100644 index 65df14268e..0000000000 --- a/src/module/sql/sql.lua +++ /dev/null @@ -1,227 +0,0 @@ --- sql.lua (internal file) - -local fiber = require('fiber') - -if box.net == nil then - box.net = {} -end - -box.net.sql = { - -- constructor - -- box.net.sql.connect( - -- 'pg', -- @driver ('pg' or 'mysql') - -- 'my.host', -- @host - -- 5432, -- @port - -- 'user', -- @username - -- 'SECRET', -- @password - -- 'DB', -- @database name - -- { raise = false }, -- @config options - -- { sql1, var, ... var }, -- @startup SQL statements - -- ... - -- ) - -- - -- @return connector to database or throw error - -- if option raise set in 'false' and an error will be happened - -- the function will return 'nil' as the first variable and - -- text of error as the second - - connect = function(driver, host, port, user, password, db, cfg, ...) - - if type(driver) == 'table' then - driver = driver.driver - end - - if type(box.net.sql.connectors[driver]) ~= 'function' then - error(string.format("Unknown driver '%s'", driver)) - end - - local self = { - -- connection variables - driver = driver, - host = host, - port = port, - user = user, - password = password, - db = db, - - -- private variables - queue = {}, - processing = false, - - -- throw exception if error - raise = true - } - - -- config parameters - if type(cfg) == 'table' then - if type(cfg.raise) == 'boolean' then - self.raise = cfg.raise - end - end - - local init = { ... } - - setmetatable(self, box.net.sql) - - -- it add 'raw' field in the table - local s, c = pcall(box.net.sql.connectors[driver], self) - if not s then - if self.raise then - error(c) - end - return nil, c - end - - -- perform init statements - for i, s in pairs(init) do - c:execute(unpack(s)) - end - return c - end, - - connectors = { }, - - __index = { - -- base method - -- example: - -- local tuples, arows, txtst = db:execute(sql, args) - -- tuples - a table of tuples (tables) - -- arows - count of affected rows - -- txtst - text status (Postgresql specific) - - -- the method throws exception by default. - -- user can change the behaviour by set 'connection.raise' - -- attribute to 'false' - -- in the case it will return negative arows if error and - -- txtst will contain text of error - - execute = function(self, sql, ...) - -- waits until connection will be free - while self.processing do - self.queue[ fiber.id() ] = fiber.channel() - self.queue[ fiber.id() ]:get() - self.queue[ fiber.id() ] = nil - end - self.processing = true - - local res = { pcall(self.raw.execute, self, sql, ...) } - self.processing = false - if not res[1] then - if self.raise then - error(res[2]) - end - return {}, -1, res[2] - end - - -- wakeup one waiter - for fid, ch in pairs(self.queue) do - ch:put(true, 0) - self.queue[ fid ] = nil - break - end - table.remove(res, 1) - return unpack(res) - end, - - - -- pings database - -- returns true if success. doesn't throw any errors - ping = function(self) - local pf = function() - local res = self:execute('SELECT 1 AS code') - if type(res) ~= 'table' then - return false - end - if type(res[1]) ~= 'table' then - return false - end - - return res[1].code == 1 - end - - local res, code = pcall(pf) - if res == true then - return code - else - return false - end - end, - - - -- select rows - -- returns table of rows - select = function(self, sql, ...) - local res = self:execute(sql, ...) - return res - end, - - -- select one row - single = function(self, sql, ...) - local res = self:execute(sql, ...) - if #res > 1 then - error("SQL request returned multiply rows") - end - return res[1] - end, - - -- perform request. returns count of affected rows - perform = function(self, sql, ...) - local res, affected, status = self:execute(sql, ...) - return affected - end, - - - -- quote variable - quote = function(self, variable) - return self.raw:quote(variable) - end, - - -- quote identifier - quote_ident = function(self, variable) - return self.raw:quote_ident(variable) - end, - - - -- begin transaction - begin_work = function(self) - return self:perform('BEGIN') - end, - - -- commit transaction - commit = function(self) - return self:perform('COMMIT') - end, - - -- rollback transaction - rollback = function(self) - return self:perform('ROLLBACK') - end, - - -- transaction - txn = function(self, proc) - local raise = self.raise - self.raise = true - self:begin_work() - local res = { pcall(proc, self) } - - -- commit transaction - if res[1] then - res = { pcall(function() self:commit() end) } - self.raise = raise - if res[1] then - return true - end - return res[1], res[2] - end - - - local res_txn = { pcall(function() self:rollback() end) } - - if not res_txn[1] then - res[2] = res[2] .. "\n" .. res_txn[2] - end - self.raise = raise - return res[1], res[2] - end - } -} diff --git a/test/module/net_sql.common.result b/test/module/net_sql.common.result deleted file mode 100644 index fcb0f13763..0000000000 --- a/test/module/net_sql.common.result +++ /dev/null @@ -1,13 +0,0 @@ -package.path = os.getenv("TARANTOOL_SRC_DIR").."/src/module/sql/?.lua" ---- -... -package.cpath = "?.so" ---- -... -require("sql") ---- -- true -... -if type(box.net.sql) ~= "table" then error("net.sql load failed") end ---- -... diff --git a/test/module/net_sql.common.test.lua b/test/module/net_sql.common.test.lua deleted file mode 100644 index 8a730b54ee..0000000000 --- a/test/module/net_sql.common.test.lua +++ /dev/null @@ -1,6 +0,0 @@ -package.path = os.getenv("TARANTOOL_SRC_DIR").."/src/module/sql/?.lua" -package.cpath = "?.so" - -require("sql") -if type(box.net.sql) ~= "table" then error("net.sql load failed") end - diff --git a/test/module/net_sql.mysql.result b/test/module/net_sql.mysql.result deleted file mode 100644 index cfe1f7bd3f..0000000000 --- a/test/module/net_sql.mysql.result +++ /dev/null @@ -1,136 +0,0 @@ -package.path = os.getenv("TARANTOOL_SRC_DIR").."/src/module/sql/?.lua" ---- -... -package.cpath = "?.so" ---- -... -require("sql") ---- -- true -... -if type(box.net.sql) ~= "table" then error("net.sql load failed") end ---- -... -os.execute("mkdir -p box/net/") ---- -- 0 -... -os.execute("cp ../../src/module/mysql/mysql.so box/net/") ---- -- 0 -... -require("box.net.mysql") ---- -- true -... ---# setopt delimiter ';' -do - stat, err = pcall(box.net.sql.connect, 'abcd') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:35: Unknown driver \'abcd\'' -end; ---- -- true -... -do - stat, err = pcall(box.net.sql.connect, 'mysql') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:70: Usage: box.net.sql.connect(\'mysql\', host, port, user, password, db, ...)' -end; ---- -- true -... ---# setopt delimiter '' -function dump(v) return require('json').encode(v) end ---- -... -connect = {} ---- -... -for tk in string.gmatch(os.getenv('MYSQL')..':', '(.-):') do table.insert(connect, tk) end ---- -... --- mysql -c = box.net.sql.connect('mysql', unpack(connect)) ---- -... -for k, v in pairs(c) do print(k, ': ', type(v)) end ---- -... ---# setopt delimiter ';' -do - stat, err = pcall(c.execute, c, 'SEL ECT 1') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:111: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'SEL ECT 1\' at line 1' -end; ---- -- true -... ---# setopt delimiter '' -c:execute('SELECT ? AS bool1, ? AS bool2, ? AS nil, ? AS num, ? AS str', true, false, nil, 123, 'abc') ---- -- - bool2: 0 - str: abc - num: 123 - bool1: 1 -- 1 -... -c:execute('SELECT * FROM (SELECT ?) t WHERE 1 = 0', 2) ---- -- [] -- 0 -... -c:execute('CREATE PROCEDURE p1() BEGIN SELECT 1 AS One; SELECT 2 AS Two, 3 AS Three; END') ---- -- [] -- 0 -... -c:execute('CALL p1') ---- -- - One: 1 - - Three: 3 - Two: 2 -- 2 -... -c:execute('DROP PROCEDURE p1') ---- -- [] -- 0 -... -c:execute('SELECT 1 AS one UNION ALL SELECT 2') ---- -- - one: 1 - - one: 2 -- 2 -... -c:execute('SELECT 1 AS one UNION ALL SELECT 2; SELECT ? AS two', 'abc') ---- -- - one: 1 - - one: 2 - - two: abc -- 3 -... -c:quote('test \"abc\" test') ---- -- test \"abc\" test -... -c:begin_work() ---- -- 0 -... -c:rollback() ---- -- 0 -... -c:begin_work() ---- -- 0 -... -c:commit() ---- -- 0 -... -os.execute("rm -rf box/net/") ---- -- 0 -... diff --git a/test/module/net_sql.mysql.skipcond b/test/module/net_sql.mysql.skipcond deleted file mode 100644 index cb8e83fee7..0000000000 --- a/test/module/net_sql.mysql.skipcond +++ /dev/null @@ -1,9 +0,0 @@ -import os -import os.path - -try: - (host, port, user, password, db) = os.getenv('MYSQL').split(':') - if not os.path.exists(os.path.join(self.args.builddir, 'src/module/mysql/mysql.so')): - raise RuntimeError('No MySQL module found') -except (RuntimeError, TypeError, NameError, AttributeError, ValueError): - self.skip = 1 diff --git a/test/module/net_sql.mysql.test.lua b/test/module/net_sql.mysql.test.lua deleted file mode 100644 index a22226a658..0000000000 --- a/test/module/net_sql.mysql.test.lua +++ /dev/null @@ -1,55 +0,0 @@ -package.path = os.getenv("TARANTOOL_SRC_DIR").."/src/module/sql/?.lua" -package.cpath = "?.so" - -require("sql") -if type(box.net.sql) ~= "table" then error("net.sql load failed") end - -os.execute("mkdir -p box/net/") -os.execute("cp ../../src/module/mysql/mysql.so box/net/") - -require("box.net.mysql") ---# setopt delimiter ';' -do - stat, err = pcall(box.net.sql.connect, 'abcd') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:35: Unknown driver \'abcd\'' -end; -do - stat, err = pcall(box.net.sql.connect, 'mysql') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:70: Usage: box.net.sql.connect(\'mysql\', host, port, user, password, db, ...)' -end; ---# setopt delimiter '' -function dump(v) return require('json').encode(v) end - -connect = {} -for tk in string.gmatch(os.getenv('MYSQL')..':', '(.-):') do table.insert(connect, tk) end - --- mysql -c = box.net.sql.connect('mysql', unpack(connect)) -for k, v in pairs(c) do print(k, ': ', type(v)) end - ---# setopt delimiter ';' -do - stat, err = pcall(c.execute, c, 'SEL ECT 1') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:111: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'SEL ECT 1\' at line 1' -end; ---# setopt delimiter '' -c:execute('SELECT ? AS bool1, ? AS bool2, ? AS nil, ? AS num, ? AS str', true, false, nil, 123, 'abc') - -c:execute('SELECT * FROM (SELECT ?) t WHERE 1 = 0', 2) -c:execute('CREATE PROCEDURE p1() BEGIN SELECT 1 AS One; SELECT 2 AS Two, 3 AS Three; END') -c:execute('CALL p1') -c:execute('DROP PROCEDURE p1') -c:execute('SELECT 1 AS one UNION ALL SELECT 2') -c:execute('SELECT 1 AS one UNION ALL SELECT 2; SELECT ? AS two', 'abc') - -c:quote('test \"abc\" test') - -c:begin_work() -c:rollback() -c:begin_work() -c:commit() - -os.execute("rm -rf box/net/") diff --git a/test/module/net_sql.pg.result b/test/module/net_sql.pg.result deleted file mode 100644 index c29fd65f87..0000000000 --- a/test/module/net_sql.pg.result +++ /dev/null @@ -1,190 +0,0 @@ -package.path = os.getenv("TARANTOOL_SRC_DIR").."/src/module/sql/?.lua" ---- -... -package.cpath = "?.so" ---- -... -require("sql") ---- -- true -... -if type(box.net.sql) ~= "table" then error("net.sql load failed") end ---- -... -os.execute("mkdir -p box/net/") ---- -- 0 -... -os.execute("cp ../../src/module/pg/pg.so box/net/") ---- -- 0 -... -require("box.net.pg") ---- -- true -... ---# setopt delimiter ';' -do - stat, err = pcall(box.net.sql.connect, 'abcd') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:35: Unknown driver \'abcd\'' -end; ---- -- true -... ---# setopt delimiter '' -function dump(v) return require('json').encode(v) end ---- -... -connect = {} ---- -... -for tk in string.gmatch(os.getenv('PG')..':', '(.-):') do table.insert(connect, tk) end ---- -... --- postgresql -c = box.net.sql.connect('pg', unpack(connect)) ---- -... -c:execute('SELECT 123::text AS bla, 345') ---- -- - ?column?: 345 - bla: '123' -- 1 -- SELECT 1 -... -c:execute('SELECT -1 AS neg, NULL AS abc') ---- -- - neg: -1 -- 1 -- SELECT 1 -... -c:execute('SELECT -1.1 AS neg, 1.2 AS pos') ---- -- - neg: -1.1 - pos: 1.2 -- 1 -- SELECT 1 -... -c:execute('SELECT ARRAY[1,2] AS neg, 1.2 AS pos') ---- -- - neg: '{1,2}' - pos: 1.2 -- 1 -- SELECT 1 -... -c:execute('SELECT ? AS val', 'abc') ---- -- - val: abc -- 1 -- SELECT 1 -... -c:execute('SELECT ? AS val', 123) ---- -- - val: 123 -- 1 -- SELECT 1 -... -c:execute('SELECT ? AS val', true) ---- -- - val: true -- 1 -- SELECT 1 -... -c:execute('SELECT ? AS val', false) ---- -- - val: false -- 1 -- SELECT 1 -... -c:execute('SELECT ? AS val, ? AS num, ? AS str', false, 123, 'abc') ---- -- - str: abc - num: 123 - val: false -- 1 -- SELECT 1 -... -c:execute('DROP TABLE IF EXISTS unknown_table') ---- -- [] -- 0 -- DROP TABLE -... -c:execute('SELECT * FROM (VALUES (1,2), (2,3)) t') ---- -- - column1: 1 - column2: 2 - - column1: 2 - column2: 3 -- 2 -- SELECT 2 -... -c:ping() ---- -- true -... -c:select('SELECT * FROM (VALUES (1,2), (2,3)) t') ---- -- - column1: 1 - column2: 2 - - column1: 2 - column2: 3 -... ---# setopt delimiter ';' -do - stat, err = pcall(c.single, c, 'SELECT * FROM (VALUES (1,2), (2,3)) t') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:162: SQL request returned multiply rows' -end; ---- -- true -... ---# setopt delimiter '' -c:single('SELECT * FROM (VALUES (1,2)) t') ---- -- column1: 1 - column2: 2 -... -c:perform('SELECT * FROM (VALUES (1,2), (2,3)) t') ---- -- 2 -... ---# setopt delimiter ';' -do - stat, err = pcall(c.execute, c, 'SELEC T') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:111: ERROR: syntax error at or near "SELEC"\nLINE 1: SELEC T\n ^\n' -end; ---- -- true -... ---# setopt delimiter '' -c:quote('abc\"cde\"def') ---- -- '''abc"cde"def''' -... -c:begin_work() ---- -- 0 -... -c:rollback() ---- -- 0 -... -c:begin_work() ---- -- 0 -... -c:commit() ---- -- 0 -... -c:txn(function(dbi) dbi:single('SELECT 1') end) ---- -- true -... -os.execute("rm -rf box/net/") ---- -- 0 -... diff --git a/test/module/net_sql.pg.skipcond b/test/module/net_sql.pg.skipcond deleted file mode 100644 index dbd8835687..0000000000 --- a/test/module/net_sql.pg.skipcond +++ /dev/null @@ -1,9 +0,0 @@ -import os -import os.path - -try: - (host, port, user, password, db) = os.getenv('PG').split(':') - if not os.path.exists(os.path.join(self.args.builddir, 'src/module/pg/pg.so')): - raise RuntimeError('No PG module found') -except (RuntimeError, TypeError, NameError, AttributeError, ValueError): - self.skip = 1 diff --git a/test/module/net_sql.pg.test.lua b/test/module/net_sql.pg.test.lua deleted file mode 100644 index 14e0f5b2f6..0000000000 --- a/test/module/net_sql.pg.test.lua +++ /dev/null @@ -1,64 +0,0 @@ -package.path = os.getenv("TARANTOOL_SRC_DIR").."/src/module/sql/?.lua" -package.cpath = "?.so" - -require("sql") -if type(box.net.sql) ~= "table" then error("net.sql load failed") end - -os.execute("mkdir -p box/net/") -os.execute("cp ../../src/module/pg/pg.so box/net/") - -require("box.net.pg") ---# setopt delimiter ';' -do - stat, err = pcall(box.net.sql.connect, 'abcd') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:35: Unknown driver \'abcd\'' -end; ---# setopt delimiter '' -function dump(v) return require('json').encode(v) end - -connect = {} -for tk in string.gmatch(os.getenv('PG')..':', '(.-):') do table.insert(connect, tk) end - --- postgresql -c = box.net.sql.connect('pg', unpack(connect)) -c:execute('SELECT 123::text AS bla, 345') -c:execute('SELECT -1 AS neg, NULL AS abc') -c:execute('SELECT -1.1 AS neg, 1.2 AS pos') -c:execute('SELECT ARRAY[1,2] AS neg, 1.2 AS pos') -c:execute('SELECT ? AS val', 'abc') -c:execute('SELECT ? AS val', 123) -c:execute('SELECT ? AS val', true) -c:execute('SELECT ? AS val', false) -c:execute('SELECT ? AS val, ? AS num, ? AS str', false, 123, 'abc') -c:execute('DROP TABLE IF EXISTS unknown_table') -c:execute('SELECT * FROM (VALUES (1,2), (2,3)) t') -c:ping() -c:select('SELECT * FROM (VALUES (1,2), (2,3)) t') ---# setopt delimiter ';' -do - stat, err = pcall(c.single, c, 'SELECT * FROM (VALUES (1,2), (2,3)) t') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:162: SQL request returned multiply rows' -end; ---# setopt delimiter '' -c:single('SELECT * FROM (VALUES (1,2)) t') -c:perform('SELECT * FROM (VALUES (1,2), (2,3)) t') ---# setopt delimiter ';' -do - stat, err = pcall(c.execute, c, 'SELEC T') - err, _ = err:gsub('.*/src/module/sql/sql.lua', 'error: src/module/sql/sql.lua') - return err == 'error: src/module/sql/sql.lua:111: ERROR: syntax error at or near "SELEC"\nLINE 1: SELEC T\n ^\n' -end; ---# setopt delimiter '' - -c:quote('abc\"cde\"def') - -c:begin_work() -c:rollback() -c:begin_work() -c:commit() - -c:txn(function(dbi) dbi:single('SELECT 1') end) - -os.execute("rm -rf box/net/") -- GitLab