1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-25 00:53:43 +00:00

Create tables on postgres if they don't exist.

This commit is contained in:
King_DuckZ 2017-08-22 09:03:44 +01:00
parent 6df2a4db43
commit 2fb6777eb8
8 changed files with 160 additions and 1 deletions

3
.gitmodules vendored
View file

@ -13,3 +13,6 @@
[submodule "lib/incredis"] [submodule "lib/incredis"]
path = lib/incredis path = lib/incredis
url = ../incredis.git url = ../incredis.git
[submodule "cmake/binary_resource"]
path = cmake/binary_resource
url = https://github.com/KingDuckZ/binary_resource.git

View file

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
set(bare_name "dindexer") set(bare_name "dindexer")
project("${bare_name}-if" VERSION 0.1.5 LANGUAGES CXX C) 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/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_DESCRIPTION_SUMMARY "A file indexing program to help you keep track of your backed up files")
set(CPACK_PACKAGE_VENDOR "King_DuckZ") set(CPACK_PACKAGE_VENDOR "King_DuckZ")

1
cmake/binary_resource Submodule

@ -0,0 +1 @@
Subproject commit 38719ba8f513611232cd300ed4614c047391b956

View file

@ -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 add_library(${PROJECT_NAME} SHARED
tag.cpp tag.cpp
@ -7,8 +21,13 @@ add_library(${PROJECT_NAME} SHARED
scan.cpp scan.cpp
navigate.cpp navigate.cpp
backend_postgresql.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 target_include_directories(${PROJECT_NAME} SYSTEM
PUBLIC ${Boost_INCLUDE_DIRS} PUBLIC ${Boost_INCLUDE_DIRS}
) )
@ -17,10 +36,19 @@ target_link_libraries(${PROJECT_NAME}
PRIVATE ${bare_name}-inc PRIVATE ${bare_name}-inc
PRIVATE ${bare_name}-pq PRIVATE ${bare_name}-pq
) )
if (ZLIB_FOUND)
target_link_libraries(${PROJECT_NAME} PRIVATE ZLIB::ZLIB)
endif()
install(TARGETS ${PROJECT_NAME} install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib LIBRARY DESTINATION lib
RUNTIME DESTINATION bin RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib/static ARCHIVE DESTINATION lib/static
) )
configure_file(
backend_postgresql_config.h.in
${CMAKE_CURRENT_BINARY_DIR}/backend_postgresql_config.h
)
ln_backend(${PROJECT_NAME}) ln_backend(${PROJECT_NAME})

View file

@ -18,6 +18,7 @@
#include "backend_postgresql.hpp" #include "backend_postgresql.hpp"
#include "backends/exposed_functions.hpp" #include "backends/exposed_functions.hpp"
#include "backends/backend_version.hpp" #include "backends/backend_version.hpp"
#include "create_tables.hpp"
#include "tag.hpp" #include "tag.hpp"
#include "delete.hpp" #include "delete.hpp"
#include "scan.hpp" #include "scan.hpp"
@ -83,6 +84,12 @@ namespace dindb {
void BackendPostgreSql::connect() { void BackendPostgreSql::connect() {
m_conn->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() { void BackendPostgreSql::disconnect() {

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "create_tables.hpp"
#include "create_tables_query.h"
#include "pq/connection.hpp"
#include "backend_postgresql_config.h"
#if defined(ZLIB_FOUND)
# include <boost/iostreams/device/array.hpp>
# include <boost/iostreams/stream_buffer.hpp>
# include <boost/iostreams/filter/gzip.hpp>
# include <boost/iostreams/filtering_stream.hpp>
# include <algorithm>
# include <iterator>
# include <iostream>
#endif
#include <string>
#include <cassert>
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<array_source> text_stream(reinterpret_cast<const char*>(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<unsigned char>(fs >> std::noskipws),
istream_iterator<unsigned char>(),
std::back_inserter(query_str)
);
#else
std::string query_str(reinterpret_cast<const char*>(create_tables_query), create_tables_query_len);
#endif
std::cout << query_str << std::endl;
parConn.query(query_str);
}
} //namespace dindb

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef id16D167FB279D4A9097EDB94FB43BD3B6
#define id16D167FB279D4A9097EDB94FB43BD3B6
namespace pq {
class Connection;
} //namespace pq
namespace dindb {
void create_tables (pq::Connection& parConn);
} //namespace dindb
#endif

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef id3885F59E78B044ABA51EBB161746354D
#define id3885F59E78B044ABA51EBB161746354D
extern const unsigned char create_tables_query[];
extern const unsigned int create_tables_query_len;
#endif