Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clang-format #291

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
BasedOnStyle: Google
Language: Cpp
TabWidth: 8
IndentWidth: 2
UseTab: Never
IndentCaseLabels: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AccessModifierOffset: -4
IncludeCategories:
- Regex: '^((<|")(pch.h))'
Priority: -1
1 change: 1 addition & 0 deletions .gitpod.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN sudo apt install -y \
r-base \
ccache \
cmake \
clang-format \
mariadb-server mariadb-client \
# Install dependencies for devtools package
libharfbuzz-dev libfribidi-dev
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
# quote(cynkrathis::use_cmakelists())

cmake_minimum_required(VERSION 3.14)

project(RMariaDB VERSION 0.1.0)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
include(Misc)

add_subdirectory(src)
24 changes: 24 additions & 0 deletions cmake/Misc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# clang-format support
function(add_clang_format_target)
set(options)
set(oneValueArgs)
set(multiValueArgs SOURCES)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
if(NOT ${PROJECT_NAME}_CLANG_FORMAT_BINARY)
find_program(${PROJECT_NAME}_CLANG_FORMAT_BINARY clang-format)
endif()

message(STATUS ${ARG_SOURCES})

if(${PROJECT_NAME}_CLANG_FORMAT_BINARY)
add_custom_target(clang-format
COMMAND ${${PROJECT_NAME}_CLANG_FORMAT_BINARY} --verbose
-i ${ARG_SOURCES}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

message(STATUS "Format the project using the `clang-format` target (i.e: cmake --build build --target clang-format).\n")
endif()
endfunction()
94 changes: 46 additions & 48 deletions src/DbConnection.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "pch.h"

#include "DbConnection.h"

#include "DbResult.h"

DbConnection::DbConnection() :
pConn_(NULL),
pCurrentResult_(NULL),
transacting_(false)
{
DbConnection::DbConnection()
: pConn_(NULL), pCurrentResult_(NULL), transacting_(false) {
LOG_VERBOSE;
}

Expand All @@ -19,15 +18,16 @@ DbConnection::~DbConnection() {
}
}

void DbConnection::connect(const Nullable<std::string>& host, const Nullable<std::string>& user,
const Nullable<std::string>& password, const Nullable<std::string>& db,
unsigned int port, const Nullable<std::string>& unix_socket,
unsigned long client_flag, const Nullable<std::string>& groups,
const Nullable<std::string>& default_file,
const Nullable<std::string>& ssl_key, const Nullable<std::string>& ssl_cert,
const Nullable<std::string>& ssl_ca, const Nullable<std::string>& ssl_capath,
const Nullable<std::string>& ssl_cipher,
int timeout, bool reconnect) {
void DbConnection::connect(
const Nullable<std::string>& host, const Nullable<std::string>& user,
const Nullable<std::string>& password, const Nullable<std::string>& db,
unsigned int port, const Nullable<std::string>& unix_socket,
unsigned long client_flag, const Nullable<std::string>& groups,
const Nullable<std::string>& default_file,
const Nullable<std::string>& ssl_key, const Nullable<std::string>& ssl_cert,
const Nullable<std::string>& ssl_ca,
const Nullable<std::string>& ssl_capath,
const Nullable<std::string>& ssl_cipher, int timeout, bool reconnect) {
LOG_VERBOSE;

this->pConn_ = mysql_init(NULL);
Expand All @@ -46,33 +46,30 @@ void DbConnection::connect(const Nullable<std::string>& host, const Nullable<std
if (!ssl_key.isNull() || !ssl_cert.isNull() || !ssl_ca.isNull() ||
!ssl_capath.isNull() || !ssl_cipher.isNull()) {
mysql_ssl_set(
this->pConn_,
ssl_key.isNull() ? NULL : as<std::string>(ssl_key).c_str(),
ssl_cert.isNull() ? NULL : as<std::string>(ssl_cert).c_str(),
ssl_ca.isNull() ? NULL : as<std::string>(ssl_ca).c_str(),
ssl_capath.isNull() ? NULL : as<std::string>(ssl_capath).c_str(),
ssl_cipher.isNull() ? NULL : as<std::string>(ssl_cipher).c_str()
);
this->pConn_,
ssl_key.isNull() ? NULL : as<std::string>(ssl_key).c_str(),
ssl_cert.isNull() ? NULL : as<std::string>(ssl_cert).c_str(),
ssl_ca.isNull() ? NULL : as<std::string>(ssl_ca).c_str(),
ssl_capath.isNull() ? NULL : as<std::string>(ssl_capath).c_str(),
ssl_cipher.isNull() ? NULL : as<std::string>(ssl_cipher).c_str());
}
if (timeout > 0) {
mysql_options(this->pConn_, MYSQL_OPT_CONNECT_TIMEOUT,
&timeout);
mysql_options(this->pConn_, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);
}
if (reconnect) {
my_bool reconnect_ = 1;
mysql_options(this->pConn_, MYSQL_OPT_RECONNECT, (void *)&reconnect_);
mysql_options(this->pConn_, MYSQL_OPT_RECONNECT, (void*)&reconnect_);
}

LOG_VERBOSE;

if (!mysql_real_connect(this->pConn_,
host.isNull() ? NULL : as<std::string>(host).c_str(),
user.isNull() ? NULL : as<std::string>(user).c_str(),
password.isNull() ? NULL : as<std::string>(password).c_str(),
db.isNull() ? NULL : as<std::string>(db).c_str(),
port,
unix_socket.isNull() ? NULL : as<std::string>(unix_socket).c_str(),
client_flag)) {
if (!mysql_real_connect(
this->pConn_, host.isNull() ? NULL : as<std::string>(host).c_str(),
user.isNull() ? NULL : as<std::string>(user).c_str(),
password.isNull() ? NULL : as<std::string>(password).c_str(),
db.isNull() ? NULL : as<std::string>(db).c_str(), port,
unix_socket.isNull() ? NULL : as<std::string>(unix_socket).c_str(),
client_flag)) {
std::string error = mysql_error(this->pConn_);
mysql_close(this->pConn_);
this->pConn_ = NULL;
Expand All @@ -82,19 +79,18 @@ void DbConnection::connect(const Nullable<std::string>& host, const Nullable<std
}

void DbConnection::disconnect() {
if (!is_valid()) return;
if (!is_valid())
return;

if (has_query()) {
warning(
"%s\n%s",
"There is a result object still in use.",
"The connection will be automatically released when it is closed"
);
warning("%s\n%s", "There is a result object still in use.",
"The connection will be automatically released when it is closed");
}

try {
mysql_close(get_conn());
} catch (...) {};
} catch (...) {
};

pConn_ = NULL;
}
Expand All @@ -110,17 +106,15 @@ void DbConnection::check_connection() {
}

List DbConnection::info() {
return
List::create(
return List::create(
_["host"] = std::string(pConn_->host),
_["username"] = std::string(pConn_->user),
_["dbname"] = std::string(pConn_->db ? pConn_->db : ""),
_["con.type"] = std::string(mysql_get_host_info(pConn_)),
_["db.version"] = std::string(mysql_get_server_info(pConn_)),
_["port"] = NA_INTEGER,
_["protocol.version"] = (int) mysql_get_proto_info(pConn_),
_["thread.id"] = (int) mysql_thread_id(pConn_)
);
_["protocol.version"] = (int)mysql_get_proto_info(pConn_),
_["thread.id"] = (int)mysql_thread_id(pConn_));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move the closing paren ); to the next line?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature will be added to clang-format. Seems now doesn't exists.

}

MYSQL* DbConnection::get_conn() {
Expand All @@ -138,7 +132,8 @@ SEXP DbConnection::quote_string(const String& input) {
std::string output = "'";
output.resize(input_len * 2 + 3);

size_t end = mysql_real_escape_string(pConn_, &output[1], input_cstr, input_len);
size_t end =
mysql_real_escape_string(pConn_, &output[1], input_cstr, input_len);

output.resize(end + 1);
output.append("'");
Expand Down Expand Up @@ -196,22 +191,25 @@ bool DbConnection::exec(const std::string& sql) {
}

void DbConnection::begin_transaction() {
if (is_transacting()) stop("Nested transactions not supported.");
if (is_transacting())
stop("Nested transactions not supported.");
check_connection();

transacting_ = true;
}

void DbConnection::commit() {
if (!is_transacting()) stop("Call dbBegin() to start a transaction.");
if (!is_transacting())
stop("Call dbBegin() to start a transaction.");
check_connection();

mysql_commit(get_conn());
transacting_ = false;
}

void DbConnection::rollback() {
if (!is_transacting()) stop("Call dbBegin() to start a transaction.");
if (!is_transacting())
stop("Call dbBegin() to start a transaction.");
check_connection();

mysql_rollback(get_conn());
Expand Down
26 changes: 15 additions & 11 deletions src/DbConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ class DbConnection : boost::noncopyable {
DbResult* pCurrentResult_;
bool transacting_;

public:

public:
DbConnection();
~DbConnection();

public:
void
connect(const Nullable<std::string>& host, const Nullable<std::string>& user, const Nullable<std::string>& password,
const Nullable<std::string>& db, unsigned int port, const Nullable<std::string>& unix_socket,
unsigned long client_flag, const Nullable<std::string>& groups, const Nullable<std::string>& default_file,
const Nullable<std::string>& ssl_key, const Nullable<std::string>& ssl_cert,
const Nullable<std::string>& ssl_ca, const Nullable<std::string>& ssl_capath,
const Nullable<std::string>& ssl_cipher,
int timeout, bool reconnect);
public:
void connect(const Nullable<std::string>& host,
const Nullable<std::string>& user,
const Nullable<std::string>& password,
const Nullable<std::string>& db, unsigned int port,
const Nullable<std::string>& unix_socket,
unsigned long client_flag, const Nullable<std::string>& groups,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we ensure that each argument gets its own line in these cases?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think now it's not possible. Clang-format add new line if arguments are to long, but it doesn't check the context of this lines.

const Nullable<std::string>& default_file,
const Nullable<std::string>& ssl_key,
const Nullable<std::string>& ssl_cert,
const Nullable<std::string>& ssl_ca,
const Nullable<std::string>& ssl_capath,
const Nullable<std::string>& ssl_cipher, int timeout,
bool reconnect);
void disconnect();
bool is_valid();
void check_connection();
Expand Down
15 changes: 7 additions & 8 deletions src/DbResult.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include "pch.h"

#include "DbResult.h"

#include "DbConnection.h"
#include "DbResultImpl.h"



// Construction ////////////////////////////////////////////////////////////////

DbResult::DbResult(const DbConnectionPtr& pConn) :
pConn_(pConn)
{
DbResult::DbResult(const DbConnectionPtr& pConn) : pConn_(pConn) {
pConn_->check_connection();

// subclass constructor can throw, the destructor will remove the
Expand All @@ -22,10 +20,10 @@ DbResult::~DbResult() {
if (is_active()) {
pConn_->reset_current_result(this);
}
} catch (...) {}
} catch (...) {
}
}


// Publics /////////////////////////////////////////////////////////////////////

bool DbResult::complete() const {
Expand Down Expand Up @@ -67,7 +65,8 @@ List DbResult::get_column_info() {

void DbResult::close() {
// Called from destructor
if (impl) impl->close();
if (impl)
impl->close();
}

// Privates ///////////////////////////////////////////////////////////////////
Expand Down
15 changes: 7 additions & 8 deletions src/DbResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#define __RDBI_DB_RESULT__

#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>

#include "DbResultImplDecl.h"


class DbConnection;
typedef boost::shared_ptr<DbConnection> DbConnectionPtr;

Expand All @@ -16,16 +15,16 @@ typedef boost::shared_ptr<DbConnection> DbConnectionPtr;
class DbResult : boost::noncopyable {
DbConnectionPtr pConn_;

protected:
protected:
boost::scoped_ptr<DbResultImpl> impl;

protected:
protected:
DbResult(const DbConnectionPtr& pConn);

public:
public:
~DbResult();

public:
public:
void close();

bool complete() const;
Expand All @@ -38,8 +37,8 @@ class DbResult : boost::noncopyable {

List get_column_info();

private:
private:
void validate_params(const List& params) const;
};

#endif // __RDBI_DB_RESULT__
#endif // __RDBI_DB_RESULT__
Loading