Skip to content

Commit

Permalink
Fix bugs and slightly change the API
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitigr committed Dec 10, 2022
1 parent 7fa7444 commit 164594c
Show file tree
Hide file tree
Showing 21 changed files with 269 additions and 71 deletions.
6 changes: 3 additions & 3 deletions cmake/dmitigr_fs.cmake → cmake/dmitigr_fsx.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
# Info
# ------------------------------------------------------------------------------

dmitigr_libs_set_library_info(fs 0 0 0 "Standard filesystem extensions")
dmitigr_libs_set_library_info(fsx 0 0 0 "Standard filesystem extensions")

# ------------------------------------------------------------------------------
# Sources
# ------------------------------------------------------------------------------

set(dmitigr_fs_headers
set(dmitigr_fsx_headers
filesystem.hpp
misc.hpp
)
Expand All @@ -33,4 +33,4 @@ set(dmitigr_fs_headers
# Dependencies
# ------------------------------------------------------------------------------

dmitigr_append_cppfs(dmitigr_fs_target_link_libraries_interface)
dmitigr_append_cppfs(dmitigr_fsx_target_link_libraries_interface)
24 changes: 12 additions & 12 deletions cmake/dmitigr_libs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ set(dmitigr_libs
# Level 0 (base level)
base
# Level 1
algo dt fs hsh ipc math os que rnd str tpl util uv
algo dt fsx hsh ipc math os que rnd str tpl util uv
# Level 2
log mulf net rajson sqlixx url
# Level 3
Expand All @@ -53,7 +53,7 @@ set(dmitigr_libs_3rdparty_uwebsockets_deps 3rdparty_usockets)
# Level 1
set(dmitigr_libs_algo_deps base)
set(dmitigr_libs_dt_deps base)
set(dmitigr_libs_fs_deps)
set(dmitigr_libs_fsx_deps)
set(dmitigr_libs_hsh_deps)
set(dmitigr_libs_ipc_deps base)
set(dmitigr_libs_math_deps base)
Expand All @@ -65,27 +65,27 @@ set(dmitigr_libs_tpl_deps base)
set(dmitigr_libs_util_deps base)
set(dmitigr_libs_uv_deps base)
# Level 2
set(dmitigr_libs_log_deps base fs os str)
set(dmitigr_libs_log_deps base fsx os str)
set(dmitigr_libs_mulf_deps base str)
set(dmitigr_libs_net_deps base fs os)
set(dmitigr_libs_rajson_deps base fs 3rdparty_rapidjson)
set(dmitigr_libs_sqlixx_deps base fs)
set(dmitigr_libs_net_deps base fsx os)
set(dmitigr_libs_rajson_deps base fsx 3rdparty_rapidjson)
set(dmitigr_libs_sqlixx_deps base fsx)
set(dmitigr_libs_url_deps base str)
# Level 3
set(dmitigr_libs_concur_deps base log)
set(dmitigr_libs_fcgi_deps base fs math net)
set(dmitigr_libs_fcgi_deps base fsx math net)
set(dmitigr_libs_http_deps base dt net str)
set(dmitigr_libs_jrpc_deps base algo math rajson str)
set(dmitigr_libs_msg_deps base fs ipc os rajson sqlixx)
set(dmitigr_libs_pgfe_deps base fs net str util)
set(dmitigr_libs_prg_deps base fs log os)
set(dmitigr_libs_ws_deps base fs http net 3rdparty_uwebsockets)
set(dmitigr_libs_msg_deps base fsx ipc os rajson sqlixx)
set(dmitigr_libs_pgfe_deps base fsx net str util)
set(dmitigr_libs_prg_deps base fsx log os)
set(dmitigr_libs_ws_deps base fsx http net 3rdparty_uwebsockets)
set(dmitigr_libs_wscl_deps base net 3rdparty_uwsc)
# Level 4
set(dmitigr_libs_lisp_deps base)
set(dmitigr_libs_pgfex_deps pgfe rajson)
# Level 5
set(dmitigr_libs_web_deps base concur fs http jrpc lisp log rajson str tpl url ws)
set(dmitigr_libs_web_deps base concur fsx http jrpc lisp log rajson str tpl url ws)

# ------------------------------------------------------------------------------
# Source type list
Expand Down
2 changes: 1 addition & 1 deletion cmake/dmitigr_libs_package.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set(dmitigr_libs_package "base;fs;os;str;util;net;pgfe")
set(dmitigr_libs_package "base;fsx;os;str;util;net;pgfe")
4 changes: 2 additions & 2 deletions cmake/dmitigr_util.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ dmitigr_libs_set_library_info(util 0 0 0 "Utilities")
# ------------------------------------------------------------------------------

set(dmitigr_util_headers
autostack.hpp
contract.hpp
diagnostic.hpp
endianness.hpp
enum_bitmask.hpp
memory.hpp
)

Expand Down
6 changes: 3 additions & 3 deletions src/fs/filesystem.hpp → src/fsx/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef DMITIGR_FS_FILESYSTEM_HPP
#define DMITIGR_FS_FILESYSTEM_HPP
#ifndef DMITIGR_FSX_FILESYSTEM_HPP
#define DMITIGR_FSX_FILESYSTEM_HPP

#if (defined(__clang__) && (__clang_major__ < 7)) || \
(defined(__GNUG__) && (__GNUC__ < 8) && !defined (__clang__))
Expand All @@ -27,4 +27,4 @@
#include <filesystem>
#endif

#endif // DMITIGR_FS_FILESYSTEM_HPP
#endif // DMITIGR_FSX_FILESYSTEM_HPP
6 changes: 3 additions & 3 deletions src/fs/fs.hpp → src/fsx/fsx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef DMITIGR_FS_FS_HPP
#define DMITIGR_FS_FS_HPP
#ifndef DMITIGR_FSX_FSX_HPP
#define DMITIGR_FSX_FSX_HPP

#include "filesystem.hpp"
#include "misc.hpp"

#endif // DMITIGR_FS_FS_HPP
#endif // DMITIGR_FSX_FSX_HPP
24 changes: 11 additions & 13 deletions src/fs/misc.hpp → src/fsx/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef DMITIGR_FS_MISC_HPP
#define DMITIGR_FS_MISC_HPP
#ifndef DMITIGR_FSX_MISC_HPP
#define DMITIGR_FSX_MISC_HPP

#include "filesystem.hpp"

#include <optional>
#include <vector>

namespace dmitigr::fs {
namespace dmitigr::fsx {

/**
* @returns The vector of the paths.
Expand Down Expand Up @@ -72,26 +72,24 @@ file_paths_by_extension(const std::filesystem::path& root,
}

/**
* @brief Searches for the `dir` directory starting from the current working
* directory and up to the root directory.
* @brief Searches for the `dir` directory starting from `path` up to the root.
*
* @returns The first path found to the `dir` directory, or
* `std::nullopt` if no specified directory found.
* @returns A first path to a directory in which `dir` directory found, or
* `std::nullopt` if no such a directory found.
*/
inline std::optional<std::filesystem::path>
parent_directory_path(const std::filesystem::path& dir)
first_parent(std::filesystem::path path, const std::filesystem::path& child)
{
auto path = std::filesystem::current_path();
while (true) {
if (is_directory(path / dir))
if (exists(path / child))
return path;
else if (path.has_relative_path())
else if (path.has_parent_path())
path = path.parent_path();
else
return std::nullopt;
}
}

} // namespace dmitigr::fs
} // namespace dmitigr::fsx

#endif // DMITIGR_FS_MISC_HPP
#endif // DMITIGR_FSX_MISC_HPP
2 changes: 1 addition & 1 deletion src/net/address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define DMITIGR_NET_ADDRESS_HPP

#include "../base/assert.hpp"
#include "../fs/filesystem.hpp"
#include "../fsx/filesystem.hpp"
#include "exceptions.hpp"

#include <algorithm>
Expand Down
2 changes: 1 addition & 1 deletion src/net/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define DMITIGR_NET_ENDPOINT_HPP

#include "../base/assert.hpp"
#include "../fs/filesystem.hpp"
#include "../fsx/filesystem.hpp"
#include "basics.hpp"

#include <optional>
Expand Down
2 changes: 1 addition & 1 deletion src/net/listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define DMITIGR_NET_LISTENER_HPP

#include "../base/assert.hpp"
#include "../fs/filesystem.hpp"
#include "../fsx/filesystem.hpp"
#include "address.hpp"
#include "descriptor.hpp"
#include "endpoint.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/pgfe/connection_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef DMITIGR_PGFE_CONNECTION_OPTIONS_HPP
#define DMITIGR_PGFE_CONNECTION_OPTIONS_HPP

#include "../fs/filesystem.hpp"
#include "../fsx/filesystem.hpp"
#include "basics.hpp"
#include "dll.hpp"

Expand Down
11 changes: 9 additions & 2 deletions src/pgfe/prepared_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,17 @@ DMITIGR_PGFE_INLINE Prepared_statement::Prepared_statement(
init_connection__(std::move(state));
state_->preparsed_ = static_cast<bool>(preparsed);
if (state_->preparsed_) {
std::size_t bound_params_count{};
const std::size_t pc = preparsed->parameter_count();
parameters_.resize(pc);
for (std::size_t i = preparsed->positional_parameter_count(); i < pc; ++i)
parameters_[i].name = preparsed->parameter_name(i);
for (std::size_t i = preparsed->positional_parameter_count(); i < pc; ++i) {
const auto name = preparsed->parameter_name(i);
if (preparsed->bound(name))
++bound_params_count;
else
parameters_[i - bound_params_count].name = name;
}
parameters_.resize(pc - bound_params_count);
} else
parameters_.reserve(8);

Expand Down
33 changes: 31 additions & 2 deletions src/pgfe/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Statement::parameter_name(const std::size_t index) const
{
if (!((positional_parameter_count() <= index) && (index < parameter_count())))
throw Client_exception{"cannot get Statement parameter name"};
return (named_parameters_[index - positional_parameter_count()])->str;
return named_parameters_[index - positional_parameter_count()]->str;
}

DMITIGR_PGFE_INLINE std::size_t
Expand Down Expand Up @@ -232,7 +232,7 @@ DMITIGR_PGFE_INLINE void Statement::append(const Statement& appendix)
assert(is_invariant_ok());
}

DMITIGR_PGFE_INLINE void
DMITIGR_PGFE_INLINE Statement&
Statement::bind(const std::string_view name,
const std::optional<std::string>& value)
{
Expand All @@ -243,6 +243,7 @@ Statement::bind(const std::string_view name,
fragment.value = value;
}
assert(is_invariant_ok());
return *this;
}

DMITIGR_PGFE_INLINE const std::optional<std::string>&
Expand All @@ -257,6 +258,34 @@ Statement::bound(const std::string_view name) const
DMITIGR_ASSERT(false);
}

DMITIGR_PGFE_INLINE std::size_t
Statement::bound_parameter_count() const noexcept
{
return count_if(cbegin(fragments_), cend(fragments_),
[counted = std::vector<std::string>{}](const auto& fragment)mutable -> bool
{
if (fragment.is_named_parameter()) {
const bool is_uncounted{none_of(cbegin(counted), cend(counted),
[&fragment](const auto& name){return name == fragment.str;})};
if (is_uncounted) {
counted.push_back(fragment.str);
return static_cast<bool>(fragment.value);
}
}
return false;
});
}

DMITIGR_PGFE_INLINE bool
Statement::has_bound_parameters() const noexcept
{
const auto e = cend(fragments_);
return find_if(cbegin(fragments_), e, [](const auto& fragment)
{
return fragment.is_named_parameter() && fragment.value;
}) != e;
}

DMITIGR_PGFE_INLINE void
Statement::replace_parameter(const std::string_view name,
const Statement& replacement)
Expand Down
22 changes: 20 additions & 2 deletions src/pgfe/statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ class Statement final : public Parameterizable {
/**
* @brief Binds the parameter named by the `name` with the specified `value`.
*
* @returns `*this`.
*
* @par Requires
* `has_parameter(name)`.
*
Expand All @@ -228,20 +230,36 @@ class Statement final : public Parameterizable {
* @par Exception safety guarantee
* Basic.
*
* @see has_parameter(), replace_parameter().
* @see has_parameter(), replace_parameter(), bound().
*/
DMITIGR_PGFE_API void
DMITIGR_PGFE_API Statement&
bind(const std::string_view name, const std::optional<std::string>& value);

/**
* @returns The value bound to parameter.
*
* @par Requires
* `has_parameter(name)`.
*
* @see bind().
*/
DMITIGR_PGFE_API const std::optional<std::string>&
bound(const std::string_view name) const;

/**
* @returns The number of bound parameters.
*
* @see has_bound_parameters(), bound().
*/
DMITIGR_PGFE_API std::size_t bound_parameter_count() const noexcept;

/**
* @returns `true` if `bound_parameter_count() > 0`.
*
* @see bound_parameter_count(), bound().
*/
DMITIGR_PGFE_API bool has_bound_parameters() const noexcept;

/**
* @brief Replaces the parameter named by the `name` with the specified
* `replacement`.
Expand Down
19 changes: 11 additions & 8 deletions src/pgfe/transaction_guard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Transaction_guard final {
if (savepoint_.empty())
savepoint_ = "pgfe_savepoint";
rollback_stmt_ = R"(rollback to savepoint :"s")";
rollback_stmt_.replace_parameter("s", savepoint_);
rollback_stmt_.bind("s", savepoint_);
} else
rollback_stmt_ = "rollback";

Expand Down Expand Up @@ -128,29 +128,32 @@ class Transaction_guard final {
*/
void rollback()
{
if (conn_.is_transaction_uncommitted())
if (conn_.is_transaction_uncommitted() && !is_subtransaction_committed_)
conn_.execute(rollback_stmt_);
}

private:
Connection& conn_;
bool is_subtransaction_{};
bool is_subtransaction_committed_{};
std::string savepoint_;
Statement rollback_stmt_;

Statement savepoint_stmt__(const std::string_view input) const
{
DMITIGR_ASSERT(!savepoint_.empty());
Statement stmt{input};
stmt.replace_parameter("s", savepoint_);
return stmt;
return Statement{input}.bind("s", savepoint_);
}

void commit__(const Statement commit_query)
{
if (conn_.is_transaction_uncommitted())
conn_.execute(is_subtransaction_ ?
savepoint_stmt__(R"(release :"s")") : commit_query);
if (conn_.is_transaction_uncommitted()) {
if (is_subtransaction_) {
conn_.execute(savepoint_stmt__(R"(release :"s")"));
is_subtransaction_committed_ = true;
} else
conn_.execute(commit_query);
}
}
};

Expand Down
Loading

0 comments on commit 164594c

Please sign in to comment.