From 2fb6777eb8b9fa95f27d5f220c2cc3ea1720b774 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 22 Aug 2017 09:03:44 +0100 Subject: [PATCH] Create tables on postgres if they don't exist. --- .gitmodules | 3 + CMakeLists.txt | 1 + cmake/binary_resource | 1 + src/backends/postgresql/CMakeLists.txt | 30 ++++++++- .../postgresql/backend_postgresql.cpp | 7 ++ src/backends/postgresql/create_tables.cpp | 66 +++++++++++++++++++ src/backends/postgresql/create_tables.hpp | 29 ++++++++ src/backends/postgresql/create_tables_query.h | 24 +++++++ 8 files changed, 160 insertions(+), 1 deletion(-) create mode 160000 cmake/binary_resource create mode 100644 src/backends/postgresql/create_tables.cpp create mode 100644 src/backends/postgresql/create_tables.hpp create mode 100644 src/backends/postgresql/create_tables_query.h diff --git a/.gitmodules b/.gitmodules index 4d7e430..891aa13 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "lib/incredis"] path = lib/incredis url = ../incredis.git +[submodule "cmake/binary_resource"] + path = cmake/binary_resource + url = https://github.com/KingDuckZ/binary_resource.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a55068..bca955c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.3 FATAL_ERROR) set(bare_name "dindexer") project("${bare_name}-if" VERSION 0.1.5 LANGUAGES CXX C) list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules) +list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/binary_resource) set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A file indexing program to help you keep track of your backed up files") set(CPACK_PACKAGE_VENDOR "King_DuckZ") diff --git a/cmake/binary_resource b/cmake/binary_resource new file mode 160000 index 0000000..38719ba --- /dev/null +++ b/cmake/binary_resource @@ -0,0 +1 @@ +Subproject commit 38719ba8f513611232cd300ed4614c047391b956 diff --git a/src/backends/postgresql/CMakeLists.txt b/src/backends/postgresql/CMakeLists.txt index 486d5b0..d2bffeb 100644 --- a/src/backends/postgresql/CMakeLists.txt +++ b/src/backends/postgresql/CMakeLists.txt @@ -1,4 +1,18 @@ -project(${bare_name}-backend-postgresql CXX) +project(${bare_name}-backend-postgresql CXX C) + +find_package(ZLIB) + +include(binary_resource) + +if (ZLIB_FOUND) + set(gzip GZIP) +else() + set(gzip "") +endif() +make_binary_resource(${gzip} + INPUT ${CMAKE_BINARY_DIR}/dindexer.sql + ARRAY_NAME create_tables_query +) add_library(${PROJECT_NAME} SHARED tag.cpp @@ -7,8 +21,13 @@ add_library(${PROJECT_NAME} SHARED scan.cpp navigate.cpp backend_postgresql.cpp + ${CMAKE_CURRENT_BINARY_DIR}/create_tables_query.c + create_tables.cpp ) +target_include_directories(${PROJECT_NAME} + PRIVATE ${CMAKE_CURRENT_BINARY_DIR} +) target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${Boost_INCLUDE_DIRS} ) @@ -17,10 +36,19 @@ target_link_libraries(${PROJECT_NAME} PRIVATE ${bare_name}-inc PRIVATE ${bare_name}-pq ) +if (ZLIB_FOUND) + target_link_libraries(${PROJECT_NAME} PRIVATE ZLIB::ZLIB) +endif() install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION lib RUNTIME DESTINATION bin ARCHIVE DESTINATION lib/static ) + +configure_file( + backend_postgresql_config.h.in + ${CMAKE_CURRENT_BINARY_DIR}/backend_postgresql_config.h +) + ln_backend(${PROJECT_NAME}) diff --git a/src/backends/postgresql/backend_postgresql.cpp b/src/backends/postgresql/backend_postgresql.cpp index 7dd3d45..03df171 100644 --- a/src/backends/postgresql/backend_postgresql.cpp +++ b/src/backends/postgresql/backend_postgresql.cpp @@ -18,6 +18,7 @@ #include "backend_postgresql.hpp" #include "backends/exposed_functions.hpp" #include "backends/backend_version.hpp" +#include "create_tables.hpp" #include "tag.hpp" #include "delete.hpp" #include "scan.hpp" @@ -83,6 +84,12 @@ namespace dindb { void BackendPostgreSql::connect() { m_conn->connect(); + if (m_conn->is_connected()) { + pq::ResultSet res = m_conn->query("SELECT EXISTS(SELECT 1 FROM pg_tables WHERE tablename = 'files'), EXISTS(SELECT 1 FROM pg_tables WHERE tablename = 'sets');"); + if (res.size() == 1 and res[0].size() == 2 and (res[0][0] == "f" or res[0][1] == "f")) { + create_tables(*m_conn); + } + } } void BackendPostgreSql::disconnect() { diff --git a/src/backends/postgresql/create_tables.cpp b/src/backends/postgresql/create_tables.cpp new file mode 100644 index 0000000..68cec41 --- /dev/null +++ b/src/backends/postgresql/create_tables.cpp @@ -0,0 +1,66 @@ +/* Copyright 2015, 2016, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" 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 "dindexer". If not, see . + */ + +#include "create_tables.hpp" +#include "create_tables_query.h" +#include "pq/connection.hpp" +#include "backend_postgresql_config.h" +#if defined(ZLIB_FOUND) +# include +# include +# include +# include +# include +# include +# include +#endif +#include +#include + +namespace dindb { + void create_tables (pq::Connection& parConn) { + assert(parConn.is_connected()); + +#if defined(ZLIB_FOUND) + using boost::iostreams::filtering_istream; + using boost::iostreams::stream_buffer; + using boost::iostreams::array_source; + using boost::iostreams::gzip_decompressor; + using std::istream_iterator; + using std::string; + + filtering_istream fs; + stream_buffer text_stream(reinterpret_cast(create_tables_query), create_tables_query_len); + fs.push(gzip_decompressor{}); + fs.push(text_stream); + + string query_str; + query_str.reserve(create_tables_query_len); + std::copy( + istream_iterator(fs >> std::noskipws), + istream_iterator(), + std::back_inserter(query_str) + ); + +#else + std::string query_str(reinterpret_cast(create_tables_query), create_tables_query_len); +#endif + std::cout << query_str << std::endl; + + parConn.query(query_str); + } +} //namespace dindb diff --git a/src/backends/postgresql/create_tables.hpp b/src/backends/postgresql/create_tables.hpp new file mode 100644 index 0000000..abcb926 --- /dev/null +++ b/src/backends/postgresql/create_tables.hpp @@ -0,0 +1,29 @@ +/* Copyright 2015, 2016, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" 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 "dindexer". If not, see . + */ + +#ifndef id16D167FB279D4A9097EDB94FB43BD3B6 +#define id16D167FB279D4A9097EDB94FB43BD3B6 + +namespace pq { + class Connection; +} //namespace pq + +namespace dindb { + void create_tables (pq::Connection& parConn); +} //namespace dindb + +#endif diff --git a/src/backends/postgresql/create_tables_query.h b/src/backends/postgresql/create_tables_query.h new file mode 100644 index 0000000..466cc47 --- /dev/null +++ b/src/backends/postgresql/create_tables_query.h @@ -0,0 +1,24 @@ +/* Copyright 2015, 2016, Michele Santullo + * This file is part of "dindexer". + * + * "dindexer" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "dindexer" 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 "dindexer". If not, see . + */ + +#ifndef id3885F59E78B044ABA51EBB161746354D +#define id3885F59E78B044ABA51EBB161746354D + +extern const unsigned char create_tables_query[]; +extern const unsigned int create_tables_query_len; + +#endif