Start adding mysql(mariadb) support

This commit is contained in:
antao 2018-11-21 15:18:41 +08:00
parent c9c5d2b647
commit 21cb1b482a
8 changed files with 172 additions and 16 deletions

View File

@ -1,6 +1,7 @@
cmake_minimum_required (VERSION 3.2)
project (DROGON CXX)
message (STATUS "os:" ${CMAKE_SYSTEM_NAME})
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
EXEC_PROGRAM (gcc ARGS "--version | grep '^gcc'|awk '{print $3}' | sed s'/)//g' | sed s'/-.*//g'" OUTPUT_VARIABLE version)
MESSAGE(STATUS "This is gcc version:: " ${version})
@ -64,13 +65,32 @@ message(STATUS "zlib inc path:" ${ZLIB_INCLUDE_DIR})
#find postgres
find_package(PostgreSQL)
if(PostgreSQL_FOUND)
include_directories(${PostgreSQL_INCLUDE_DIR})
message(STATUS "libpq inc path:" ${PostgreSQL_INCLUDE_DIR})
link_libraries(${PostgreSQL_LIBRARIES})
aux_source_directory(${PROJECT_SOURCE_DIR}/orm_lib/src/postgresql_impl DIR_SRCS)
set(USE_ORM TRUE)
include_directories(${PostgreSQL_INCLUDE_DIR})
message(STATUS "libpq inc path:" ${PostgreSQL_INCLUDE_DIR})
link_libraries(${PostgreSQL_LIBRARIES})
aux_source_directory(${PROJECT_SOURCE_DIR}/orm_lib/src/postgresql_impl DIR_SRCS)
set(USE_ORM TRUE)
endif()
#Find mysql, only mariadb client liberary is supported
find_package(MySQL)
if(MYSQL_FOUND)
message(STATUS "inc:" ${MYSQL_INCLUDE_DIR})
message(STATUS "libs:" ${MYSQL_CLIENT_LIBS})
message(STATUS "version:" ${MYSQL_VERSION_STRING})
if(MYSQL_VERSION_STRING STREQUAL "")
message(STATUS "The mysql in your system is not the mariadb, so we can't use it in drogon")
else()
message(STATUS "Ok! We find the mariadb!")
include_directories(${MYSQL_INCLUDE_DIR})
link_libraries(${MYSQL_CLIENT_LIBS})
aux_source_directory(${PROJECT_SOURCE_DIR}/orm_lib/src/mysql_impl DIR_SRCS)
set(USE_ORM TRUE)
endif()
endif()
message(STATUS ${DIR_SRCS})
if(CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE Release)
endif()
@ -102,12 +122,25 @@ file(WRITE "${CONFIG_HEADER}" "#pragma once\n")
file(APPEND "${CONFIG_HEADER}" "#include <trantor/utils/config.h>\n")
if(PostgreSQL_FOUND)
file(APPEND "${CONFIG_HEADER}" "#define USE_POSTGRESQL 1")
if(USE_ORM)
file(APPEND "${CONFIG_HEADER}" "#define USE_ORM 1\n")
if(PostgreSQL_FOUND)
file(APPEND "${CONFIG_HEADER}" "#define USE_POSTGRESQL 1\n")
else()
file(APPEND "${CONFIG_HEADER}" "#define USE_POSTGRESQL 0\n")
endif()
if(MYSQL_FOUND)
file(APPEND "${CONFIG_HEADER}" "#define USE_MYSQL 1\n")
else()
file(APPEND "${CONFIG_HEADER}" "#define USE_MYSQL 0\n")
endif()
else()
file(APPEND "${CONFIG_HEADER}" "#define USE_POSTGRESQL 0")
file(APPEND "${CONFIG_HEADER}" "#define USE_ORM 0\n")
endif()
string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
if(CMAKE_BUILD_TYPE_LOWER STREQUAL release)
file(APPEND "${CONFIG_HEADER}" "\n" "const char compileFlags[]=\"" ${CMAKE_CXX_FLAGS_RELEASE} "\";")

View File

@ -0,0 +1,123 @@
#--------------------------------------------------------
# Copyright (C) 1995-2007 MySQL AB
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# There are special exceptions to the terms and conditions of the GPL
# as it is applied to this software. View the full text of the exception
# in file LICENSE.exceptions in the top-level directory of this software
# distribution.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
# The MySQL Connector/ODBC is licensed under the terms of the
# GPL, like most MySQL Connectors. There are special exceptions
# to the terms and conditions of the GPL as it is applied to
# this software, see the FLOSS License Exception available on
# mysql.com.
##########################################################################
#-------------- FIND MYSQL_INCLUDE_DIR ------------------
FIND_PATH(MYSQL_INCLUDE_DIR mysql.h
/usr/include/mysql
/usr/local/include/mysql
/opt/mysql/mysql/include
/opt/mysql/mysql/include/mysql
/opt/mysql/include
/opt/local/include/mysql5
/usr/local/mysql/include
/usr/local/mysql/include/mysql
$ENV{ProgramFiles}/MySQL/*/include
$ENV{SystemDrive}/MySQL/*/include)
#----------------- FIND MYSQL_LIB_DIR -------------------
IF (WIN32)
# Set lib path suffixes
# dist = for mysql binary distributions
# build = for custom built tree
IF (CMAKE_BUILD_TYPE STREQUAL Debug)
SET(libsuffixDist debug)
SET(libsuffixBuild Debug)
ELSE (CMAKE_BUILD_TYPE STREQUAL Debug)
SET(libsuffixDist opt)
SET(libsuffixBuild Release)
ADD_DEFINITIONS(-DDBUG_OFF)
ENDIF (CMAKE_BUILD_TYPE STREQUAL Debug)
FIND_LIBRARY(MYSQL_LIB NAMES mysqlclient
PATHS
$ENV{MYSQL_DIR}/lib/${libsuffixDist}
$ENV{MYSQL_DIR}/libmysql
$ENV{MYSQL_DIR}/libmysql/${libsuffixBuild}
$ENV{MYSQL_DIR}/client/${libsuffixBuild}
$ENV{MYSQL_DIR}/libmysql/${libsuffixBuild}
$ENV{ProgramFiles}/MySQL/*/lib/${libsuffixDist}
$ENV{SystemDrive}/MySQL/*/lib/${libsuffixDist})
ELSE (WIN32)
FIND_LIBRARY(MYSQL_LIB NAMES mysqlclient_r mysqlclient
PATHS
/usr/lib/mysql
/usr/local/lib/mysql
/usr/local/mysql/lib
/usr/local/mysql/lib/mysql
/opt/local/mysql5/lib
/opt/local/lib/mysql5/mysql
/opt/mysql/mysql/lib/mysql
/opt/mysql/lib/mysql)
ENDIF (WIN32)
IF(MYSQL_LIB)
GET_FILENAME_COMPONENT(MYSQL_LIB_DIR ${MYSQL_LIB} PATH)
ENDIF(MYSQL_LIB)
set(MYSQL_VERSION_STRING "")
if( MYSQL_INCLUDE_DIR AND EXISTS "${MYSQL_INCLUDE_DIR}/mariadb_version.h" )
file( STRINGS "${MYSQL_INCLUDE_DIR}/mariadb_version.h"
MYSQL_VERSION_H REGEX "^#define[ \t]+MARIADB_CLIENT_VERSION_STR[ \t]+\"[^\"]+\".*$" )
string( REGEX REPLACE
"^.*MARIADB_CLIENT_VERSION_STR[ \t]+\"([^\"]+)\".*$" "\\1" MYSQL_VERSION_STRING
"${MYSQL_VERSION_H}" )
endif()
IF (MYSQL_INCLUDE_DIR AND MYSQL_LIB_DIR)
SET(MYSQL_FOUND TRUE)
INCLUDE_DIRECTORIES(${MYSQL_INCLUDE_DIR})
LINK_DIRECTORIES(${MYSQL_LIB_DIR})
FIND_LIBRARY(MYSQL_ZLIB zlib PATHS ${MYSQL_LIB_DIR})
FIND_LIBRARY(MYSQL_TAOCRYPT taocrypt PATHS ${MYSQL_LIB_DIR})
IF (MYSQL_LIB)
SET(MYSQL_CLIENT_LIBS ${MYSQL_LIB})
ELSE()
SET(MYSQL_CLIENT_LIBS mysqlclient_r)
ENDIF()
IF (MYSQL_ZLIB)
SET(MYSQL_CLIENT_LIBS ${MYSQL_CLIENT_LIBS} zlib)
ENDIF (MYSQL_ZLIB)
IF (MYSQL_TAOCRYPT)
SET(MYSQL_CLIENT_LIBS ${MYSQL_CLIENT_LIBS} taocrypt)
ENDIF (MYSQL_TAOCRYPT)
# Added needed mysqlclient dependencies on Windows
IF (WIN32)
SET(MYSQL_CLIENT_LIBS ${MYSQL_CLIENT_LIBS} ws2_32)
ENDIF (WIN32)
MESSAGE(STATUS "MySQL Include dir: ${MYSQL_INCLUDE_DIR} library dir: ${MYSQL_LIB_DIR}")
MESSAGE(STATUS "MySQL client libraries: ${MYSQL_CLIENT_LIBS}")
ELSEIF (MySQL_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Cannot find MySQL. Include dir: ${MYSQL_INCLUDE_DIR} library dir: ${MYSQL_LIB_DIR}")
ENDIF (MYSQL_INCLUDE_DIR AND MYSQL_LIB_DIR)

View File

@ -399,7 +399,7 @@ void create_model::createModel(const std::string &path)
void create_model::handleCommand(std::vector<std::string> &parameters)
{
#if USE_POSTGRESQL
#if USE_ORM
std::cout << "Create model" << std::endl;
if (parameters.size() == 0)
{

View File

@ -15,7 +15,7 @@
#include <drogon/config.h>
#include <json/json.h>
#if USE_POSTGRESQL
#if USE_ORM
#include <drogon/orm/DbClient.h>
using namespace drogon::orm;
#endif

View File

@ -15,7 +15,7 @@
#pragma once
#include <drogon/config.h>
#if USE_POSTGRESQL
#if USE_ORM
#include <drogon/orm/DbClient.h>
#endif
#include <drogon/utils/Utilities.h>
@ -179,7 +179,7 @@ class HttpAppFramework : public trantor::NonCopyable
virtual void setStaticFilesCacheTime(int cacheTime) = 0;
virtual int staticFilesCacheTime() const = 0;
virtual void setIdleConnectionTimeout(size_t timeout) = 0;
#if USE_POSTGRESQL
#if USE_ORM
virtual orm::DbClientPtr getDbClient(const std::string &name = "default") = 0;
virtual void createDbClient(const std::string &dbType,
const std::string &host,

View File

@ -221,7 +221,7 @@ static void loadApp(const Json::Value &app)
}
static void loadDbClients(const Json::Value &dbClients)
{
#if USE_POSTGRESQL
#if USE_ORM
if (!dbClients)
return;
for (auto &client : dbClients)

View File

@ -1235,7 +1235,7 @@ HttpAppFramework::~HttpAppFramework()
{
}
#if USE_POSTGRESQL
#if USE_ORM
orm::DbClientPtr HttpAppFrameworkImpl::getDbClient(const std::string &name)
{
return _dbClientsMap[name];

View File

@ -94,7 +94,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
assert(_loop.isRunning());
_loop.quit();
}
#if USE_POSTGRESQL
#if USE_ORM
virtual orm::DbClientPtr getDbClient(const std::string &name = "default") override;
virtual void createDbClient(const std::string &dbType,
const std::string &host,
@ -221,7 +221,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
int _staticFilesCacheTime = 5;
std::unordered_map<std::string, std::weak_ptr<HttpResponse>> _staticFilesCache;
std::mutex _staticFilesCacheMutex;
#if USE_POSTGRESQL
#if USE_ORM
std::map<std::string, orm::DbClientPtr> _dbClientsMap;
#endif
};