/* 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 "backend_postgresql.hpp" #include "backends/exposed_functions.hpp" #include "pq/connection.hpp" #include #include #include #include namespace dindb { namespace { struct PostgreConnectionSettings { std::string address; std::string username; std::string password; std::string dbname; uint16_t port; }; } //unnamed namespace } //namespace dindb namespace YAML { template<> struct convert { static Node encode (const dindb::PostgreConnectionSettings& parSettings) { Node node; node["address"] = parSettings.address; node["username"] = parSettings.username; node["password"] = parSettings.password; node["port"] = parSettings.port; node["dbname"] = parSettings.dbname; return node; } static bool decode (const Node& parNode, dindb::PostgreConnectionSettings& parSettings) { if (not parNode.IsMap() or parNode.size() != 5) { return false; } parSettings.address = parNode["address"].as(); parSettings.username = parNode["username"].as(); parSettings.password = parNode["password"].as(); parSettings.dbname = parNode["dbname"].as(); parSettings.port = parNode["port"].as(); return true; } }; } //namespace YAML namespace dindb { BackendPostgreSql::BackendPostgreSql (std::string&& parUser, std::string&& parPass, std::string&& parDB, std::string&& parAddr, uint16_t parPort) : m_conn(new pq::Connection(std::move(parUser), std::move(parPass), std::move(parDB), std::move(parAddr), parPort)) { assert(m_conn); m_conn->connect(); } BackendPostgreSql::~BackendPostgreSql() noexcept { m_conn->disconnect(); } void BackendPostgreSql::tag_files (const std::vector& parFiles, const std::vector& parTags, GroupIDType parSet) const { if (InvalidGroupID != parSet) { const std::string query = "UPDATE \"files\" SET \"tags\" = ARRAY(SELECT DISTINCT UNNEST(\"tags\" || $1) ORDER BY 1) WHERE \"id\"=ANY($2) AND \"group_id\"=$3;"; m_conn->query(query, parTags, parFiles, parSet); } else { const std::string query = "UPDATE \"files\" SET \"tags\" = ARRAY(SELECT DISTINCT UNNEST(\"tags\" || $1) ORDER BY 1) WHERE \"id\"=ANY($2);"; m_conn->query(query, parTags, parFiles); } } void BackendPostgreSql::tag_files (const std::vector& parRegexes, const std::vector& parTags, GroupIDType parSet) const { } void BackendPostgreSql::delete_tags (const std::vector& parFiles, const std::vector& parTags, GroupIDType parSet) const { } void BackendPostgreSql::delete_tags (const std::vector& parRegexes, const std::vector& parTags, GroupIDType parSet) const { } void BackendPostgreSql::delete_all_tags (const std::vector& parFiles, GroupIDType parSet) const { } void BackendPostgreSql::delete_all_tags (const std::vector& parRegexes, GroupIDType parSet) const { } } //namespace dindb extern "C" dindb::Backend* dindexer_create_backend (const YAML::Node* parConfig) { if (not parConfig) return nullptr; auto config = parConfig->as(); return new dindb::BackendPostgreSql( std::move(config.username), std::move(config.password), std::move(config.dbname), std::move(config.address), config.port ); } extern "C" void dindexer_destroy_backend (dindb::Backend* parDele) { if (parDele) delete parDele; } extern "C" const char* dindexer_backend_name() { return "postgresql"; }