Skip to content
Draft
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build*/
include/strata/version.hpp
examples/
complogs.txt
include/strata/db_config.hpp
7 changes: 0 additions & 7 deletions CHANGELOG.md

This file was deleted.

18 changes: 13 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ project(StrataORM VERSION 0.2.0 LANGUAGES CXX)
#set CXX standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set (CMAKE_CXX_FLAGS_DEBUG "-std=c++20 -Wall -Wextra -Wpedantic -g")

set (CMAKE_CXX_FLAGS_RELEASE "-std=c++20 -O3")

set(SOURCES
src/datatypes.cpp
src/models.cpp
src/utils.cpp
)
#Build options
option(DB_ENGINE "Target database engine(only 'PSQL' supported now)" "PSQL")
option(DB_ENGINE "Target database engine." "PSQL")

#Database Engine Macro setup
if(DB_ENGINE STREQUAL "PSQL")
set(DB_MACRO "#define PSQL")
file(GLOB DB_SRC src/psql/*.cpp)
list(APPEND SOURCES ${DB_SRC})
set(DB_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/strata/psql")
elseif(DB_ENGINE STREQUAL "MARIADB")
set(DB_MACRO "#define MARIADB")
file(GLOB DB_SRC src/mariadb/*.cpp)
list(APPEND SOURCES ${DB_SRC})
set(DB_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/strata/mariadb")
else()
message(FATAL_ERROR "Unsupported database engine chosen: ${DB_ENGINE}")
endif()
Expand All @@ -40,6 +44,9 @@ set_target_properties(strata_shared PROPERTIES OUTPUT_NAME "strata")
add_library(strata_static STATIC ${SOURCES})
set_target_properties(strata_static PROPERTIES OUTPUT_NAME "strata")

# target_link_libraries(strata_shared PUBLIC ${DEP_LIBS})
# target_link_libraries(strata_static PUBLIC ${DEP_LIBS})

#generate version.hpp from version.hpp.in in include/strata/
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/include/strata/version.hpp.in
Expand All @@ -66,15 +73,16 @@ target_include_directories(strata_static
#Install both targets
install(TARGETS strata_shared strata_static
EXPORT strata-targets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/strata
LIBRARY DESTINATION lib/strata
)

#Install top-level headers
install(DIRECTORY include/strata
DESTINATION include
FILES_MATCHING PATTERN "*.hpp"
PATTERN "psql" EXCLUDE
PATTERN "mariadb" EXCLUDE
)

#Install database-specific headers
Expand Down
140 changes: 41 additions & 99 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ An ORM built on C++20 that is inspired by Django's ORM, built to support multipl

[![C++20](https://img.shields.io/badge/C%2B%2B-20-blue)](https://en.cppreference.com/w/cpp/20.html)
[![CMake](https://img.shields.io/badge/Build-CMake%203.16%2B-brightblue)](https://cmake.org)
[![PostgreSQL](https://img.shields.io/badge/Database-PostgreSQL-blueviolet)](https://www.postgresql.org)
[![libpqxx](https://img.shields.io/badge/Dependency-libpqxx--dev-pink)](https://github.com/jtv/libpqxx)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-darkred)](https://www.gnu.org/licenses/gpl-3.0.en.html)

Strata provides an easy-to-use, intuitive API to interact with databases. As of now, it only
supports PostgreSQL but plans to add support for more database engines are in mind.
Strata provides an easy-to-use, intuitive API to interact with databases.
Supports the following databases:
- PostgreSQL
- MariaDB

Documentation can be found at the [WIKI](https://github.com/bitflaw/strataorm/wiki).

Expand All @@ -23,109 +23,63 @@ Documentation can be found at the [WIKI](https://github.com/bitflaw/strataorm/wi
- [X] Clean abstraction over raw, basic SQL datatypes using classes.
- [X] Support for performing fetches, filters(limited) and joins.
- [X] Environmental variables set in-program for db connections.
- [X] Support for user-defined datatypes.
- [X] Support for more database engines ie. postgres and Mariadb
- [ ] Support for nullable values.
- [ ] Support for user-defined datatypes.
- [ ] Support for more database engines eg MySQL, SQLite, MSSQL etc.

## Dependencies
Since this library supports only PostgreSQL now, dependencies are:
- A C++ compiler supporting [```-std=C++20```](https://en.cppreference.com/w/cpp/compiler_support/20).
- [CMake](https://cmake.org/download/) (at least version 3.16).
- [PostgreSQL](https://www.postgresql.org/download/) library.
**If using PostgreSQL**
- [libpqxx](https://github.com/jtv/libpqxx) -> Official C++ client library for postgres.


>[!Warning]
> This library depends on a feature from libpqxx that is only available on the latest development version(not released yet).
> Options are to build from source or wait for the upcoming [```8.0```](https://github.com/jtv/libpqxx/pull/914) release.

- [PostgreSQL](https://postgresql.org/download) -> PostgreSQL database.
**If using MariaDB**
- [mdbcxx](https://github.com/bitflaw/mdbcxx) -> A MariaDB C++ Connector.
- [MariaDB's C connector](https://github.com/mariadb-corporation/mariadb-connector-c) -> C connector for MariaDB which is a dependency for `mdbcxx`
- [MariaDB](https://mariadb.com/downloads) -> MariaDB database

## Build & Installation

### Step 1: Clone the repository
```bash
$ git clone [email protected]:bitflaw/strataorm.git
$ cd strataorm
```
### Step 2: Build the library
Since we are using CMake, I recommend building in a dedicated build directory:
```bash
$ mkdir build
$ cmake -B ${BUILD_DIR} -S . -DDB_ENGINE=PSQL
$ cmake --build ${BUILD_DIR}
```

- ```-DDB_ENGINE=PSQL``` to specify the database you want to use the ORM with.
This flag only takes ```PSQL``` for now since only postgres is supported for now.
- Note that both static and dynamic libraries will be built for both use cases, avoiding rebuilding just to
use a desired one.


### Step 3: Install to System
To install to the default location specified by CMake, run:
```bash
$ cmake --install ${BUILD_DIR}
```
To install to a specified location, do:
```bash
$ cmake --install ${BUILD_DIR} --prefix ${DESTINATION}
```
> [!NOTE]
> You might need sudo/admin privileges to run this command.


If it's a CMake project, add this in your CMakeLists.txt file immediately after cloning the repo:
```
add_subdirectory(strata)
target_link_libraries(my_project PRIVATE strata)
git clone [email protected]:bitflaw/strataorm.git
cd strataorm
cmake -B ${BUILD_DIR} -S . -DDB_ENGINE={PSQL or MARIADB}
cmake --build ${BUILD_DIR}
cmake --install ${BUILD_DIR} #installs to /usr/local/
```

## Examples
Examples can be found under the ```examples``` directory in the source tree.

**Model usage example**
```cpp
#include <memory>
#include <strata/models.hpp>
#include <strata/db_adapters.hpp>

class users : public Model{
public:
users(){
col_map["username"] = std::make_shared<CharField>(CharField("varchar", 24, true, true));
col_map["email"] = std::make_shared<CharField>(CharField("varchar", 50, true, true));
col_map["pin"] = std::make_shared<IntegerField>(IntegerField("integer", false, true));
col_map["username"] = db::Field::CharField("varchar", 24, true, false);
col_map["email"] = db::Field::CharField("varchar", 51, false, true);
col_map["pin"] = db::Field::IntegerField("tinyint", false, true);
}
};REGISTER_MODEL(users);

class message : public Model{
public:
message(){
col_map["sender"] = std::make_shared<ForeignKey>(ForeignKey("sender", "users", "users_id", std::nullopt, "CASCADE", "CASCADE"));
col_map["receiver"] = std::make_shared<ForeignKey>(ForeignKey("receiver", "users", "users_id", std::nullopt, "CASCADE", "CASCADE"));
col_map["content"] = std::make_shared<CharField>(CharField("varchar", 256, true));
}
};REGISTER_MODEL(message);

int main(){
Utils::dbenvars vars = {
//insert your db credentials here
{"DBUSER", ""},
{"DBPASS", ""},
{"DBNAME", ""},
{"DBHOST", ""},
{"DBPORT", ""}
};
Utils::set_dbenvars(vars);
//can remove if u don't plan to apply the changes to the actual db.
//this is only relevant when there is a db in play.
// Utils::dbenvars vars = {
// {"DBUSER", ""},
// {"DBPASS", ""},
// {"DBNAME", ""},
// {"DBHOST", ""},
// {"DBPORT", ""}
// };
// Utils::set_dbenvars(vars);

Model model {};
nlohmann::json mrm {};
nlohmann::json frm {};
std::string sql_filename {"migrations.sql"};

model.make_migrations(mrm, frm, sql_filename);

db_adapter::opt_result_t result = db_adapter::execute_sql(sql_filename);
// db::opt_result_t result = db::execute_sql(sql_filename);
return 0;
}
```
Expand All @@ -148,22 +102,14 @@ int main(){
Utils::set_dbenvars(vars);

users user {};
message m {};

using params = std::vector<pqxx::params>;
params user_rows = user.parse_json_rows();
params message_rows = m.parse_json_rows();

pqxx::connection cxn = db_adapter::prepare_insert<users>();
pqxx::connection cxn = db::prepare_insert<users>();
for(pqxx::params& user_row : user_rows){
db_adapter::exec_insert(cxn, user_row);
db::exec_insert(cxn, user_row);
}

cxn = db_adapter::prepare_insert<message>();
for(pqxx::params& message_r : message_rows){
db_adapter::exec_insert(cxn, message_r);
}

return 0;
}
```
Expand All @@ -185,16 +131,16 @@ int main(){

users user {};

db_adapter::query::fetch_all(user, "*");
//db_adapter::query::get(user, "username", "berna");
db::query::fetch_all(user, "*");
//db::query::get(user, "username", "berna");
filters filters = {
{"email", OP::CONTAINS, "gmail"},
{"username", OP::STARTSWITH, "b"}
};

db_adapter::query::filter(user, "or", filters);
db::query::filter(user, "or", filters);

std::vector<users> my_users = db_adapter::query::to_instances(user);
std::vector<users> my_users = db::query::to_instances(user);

return 0;
}
Expand All @@ -216,7 +162,7 @@ int main(){
Utils::set_dbenvars(vars);

users user {};
db_adapter::query::JoinBuilder JB {user};
db::query::JoinBuilder JB {user};
pqxx::result result = JB.select("username, email")
.inner_join("message")
.on("and", "users.users_id = message.sender")
Expand Down Expand Up @@ -244,7 +190,7 @@ int main(){

users user {};

db_adapter::Update<users> user_update {};
db::Update<users> user_update {};
Utils::filters filters = {
{"username", OP::EQ, "janedoe"}
};
Expand All @@ -254,7 +200,7 @@ int main(){
.where("and", filters)
.commit();

db_adapter::query::get(user, "username", "'janny'");
db::query::get(user, "username", "'janny'");
return 0;
}
```
Expand All @@ -274,13 +220,11 @@ int main(){
};
Utils::set_dbenvars(vars);


users user {};

Utils::filters filters = {
{"users_id", OP::EQ, 3}
};
db_adapter::delete_row<users>("and", filters);
db::delete_row<users>("and", filters);

return 0;
}
Expand All @@ -289,5 +233,3 @@ int main(){
> [!NOTE]
> Tests have not been implemented yet but will be soon.

## Contributing
All contributions are welcome. Please open an issue or submit a pull request for contributions to the library.
24 changes: 0 additions & 24 deletions examples/deletes/CMakeLists.txt

This file was deleted.

48 changes: 0 additions & 48 deletions examples/deletes/main.cpp

This file was deleted.

Loading