-
Notifications
You must be signed in to change notification settings - Fork 7
[#1013] Implement the Database Adapter #1025
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
Open
marcocapozzoli
wants to merge
20
commits into
master
Choose a base branch
from
masc/1013-database-adapter-cpp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2abe834
WIP
marcocapozzoli 5b5bbbf
WIP - Add a simple test
marcocapozzoli e6f4e2b
WIP
marcocapozzoli e1ef57a
WIP
marcocapozzoli a29786d
WIP
marcocapozzoli db82fd8
WIP
marcocapozzoli 8d82723
WIP
marcocapozzoli 889167e
WIP
marcocapozzoli 125c0b2
WIP
marcocapozzoli 479638f
WIP
marcocapozzoli 09dface
WIP
marcocapozzoli f00ec73
WIP
marcocapozzoli 95292fb
WIP
marcocapozzoli d559a32
WIP
marcocapozzoli 76ca880
WIP - doc
marcocapozzoli 38906cb
WIP
marcocapozzoli 73376df
WIP-tests
marcocapozzoli 052de67
change log level
marcocapozzoli 6cf4df6
add exists() in HandleTrie
marcocapozzoli 4530a64
Merge branch 'master' into masc/1013-database-adapter-cpp
marcocapozzoli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| load("@rules_cc//cc:cc_library.bzl", "cc_library") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| cc_library( | ||
| name = "db_adapter_lib", | ||
| includes = ["."], | ||
| deps = [ | ||
| ":data_mapper", | ||
| ":data_types", | ||
| ":db_wrapper", | ||
| "//db_adapter/postgres:postgres_lib", | ||
| ], | ||
| ) | ||
|
|
||
| cc_library( | ||
| name = "data_mapper", | ||
| srcs = ["DataMapper.cc"], | ||
| hdrs = ["DataMapper.h"], | ||
| includes = ["."], | ||
| deps = [ | ||
| "//commons:commons_lib", | ||
| "//commons/atoms:atoms_lib", | ||
| ], | ||
| ) | ||
|
|
||
| cc_library( | ||
| name = "data_types", | ||
| hdrs = ["DataTypes.h"], | ||
| includes = ["."], | ||
| deps = [ | ||
| "//commons:commons_lib", | ||
| "//commons/atoms:atoms_lib", | ||
| ], | ||
| ) | ||
|
|
||
| cc_library( | ||
| name = "db_wrapper", | ||
| hdrs = ["DBWrapper.h"], | ||
| includes = ["."], | ||
| deps = [ | ||
| ":data_types", | ||
| "//commons:commons_lib", | ||
| "//commons/atoms:atoms_lib", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "DataMapper.h" | ||
|
|
||
| using namespace std; | ||
| using namespace db_adapter; | ||
|
|
||
| namespace db_adapter { | ||
|
|
||
| /** | ||
| * @class DatabaseWrapper | ||
| * @brief Generic interface for a database connection wrapper. | ||
| * | ||
| * @tparam ConnT The underlying connection object type (e.g., pqxx::connection). | ||
| */ | ||
| template <typename ConnT> | ||
| class DatabaseWrapper { | ||
| public: | ||
| explicit DatabaseWrapper(shared_ptr<Mapper> mapper, MAPPER_TYPE mapper_type) | ||
| : mapper(move(mapper)), mapper_type(mapper_type) {} | ||
| virtual ~DatabaseWrapper() = default; | ||
|
|
||
| /** | ||
| * @brief Closes the connection. | ||
| */ | ||
| virtual void disconnect() = 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method will disappear when you implement Processor |
||
|
|
||
| unsigned int mapper_handle_trie_size() { return mapper->handle_trie_size(); } | ||
|
|
||
| protected: | ||
| /** | ||
| * @brief Establishes connection to the database. | ||
| */ | ||
| virtual unique_ptr<ConnT> connect() = 0; | ||
|
|
||
| unique_ptr<ConnT> db_client; | ||
| shared_ptr<Mapper> mapper; | ||
| MAPPER_TYPE mapper_type; | ||
| }; | ||
|
|
||
| /** | ||
| * @class SQLWrapper | ||
| * @brief Specialization of DatabaseWrapper for SQL-based databases. | ||
| */ | ||
| template <typename ConnT> | ||
| class SQLWrapper : public DatabaseWrapper<ConnT> { | ||
| public: | ||
| explicit SQLWrapper(MAPPER_TYPE mapper_type) | ||
| : DatabaseWrapper<ConnT>(create_mapper(mapper_type), mapper_type) {} | ||
| virtual ~SQLWrapper() = default; | ||
|
|
||
| /** | ||
| * @brief Retrieves schema information for a specific table. | ||
| */ | ||
| virtual Table get_table(const string& name) = 0; | ||
|
|
||
| /** | ||
| * @brief Lists all tables in the database. | ||
| */ | ||
| virtual vector<Table> list_tables() = 0; | ||
|
|
||
| /** | ||
| * @brief Maps a table's data to the target format. | ||
| * | ||
| * @param table The table metadata. | ||
| * @param clauses Additional SQL WHERE clauses. | ||
| * @param skip_columns Columns to exclude from mapping. | ||
| * @param second_level Boolean flag for recursive/depth mapping logic. | ||
| */ | ||
| virtual void map_table(const Table& table, | ||
| const vector<string>& clauses, | ||
| const vector<string>& skip_columns, | ||
| bool second_level) = 0; | ||
|
|
||
| /** | ||
| * @brief Executes a raw SQL query and maps the result. | ||
| */ | ||
| virtual void map_sql_query(const string& virtual_name, const string& raw_query) = 0; | ||
|
|
||
| private: | ||
| // Factory method for creating the specific mapper strategy | ||
| static shared_ptr<Mapper> create_mapper(MAPPER_TYPE mapper_type) { | ||
| switch (mapper_type) { | ||
| case MAPPER_TYPE::SQL2METTA: | ||
| return make_shared<SQL2MettaMapper>(); | ||
| case MAPPER_TYPE::SQL2ATOMS: | ||
| return make_shared<SQL2AtomsMapper>(); | ||
| default: | ||
| throw invalid_argument("Unknown mapper type"); | ||
| } | ||
| } | ||
| }; | ||
| } // namespace db_adapter | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eddiebrissow @marcocapozzoli
Please make this class implement commons/processor/Processor.h. Basically it means you need to implement
virtual void setup();
virtual void start();
virtual void stop();
setup() Is supposed to make proper initialization without starting any threads, connections etc. It's OK if it's empty.
start() is supposed to start any resources like threads, connections, file openings etc
stop() is supposed to gracefully stop the use of any resource. Stopping/synching threads, closing connections, closing open files etc.
The idea is that relevant resources like threads, connections etc become Processors as well. You can connect them (by calling add_subprocessor()) so when you setup/start/stop Processors they are setup/started/stopped in the proper order to avoid crashing.
We already have DedicatedThread.h and ThreadPool.h in the Processor package. The former is for when you need to spawn a single thread. The later is for when you want a thread pool of workers.
You may need to add a PostgresDBConnection (which inherits from Processor as well) in your package and probably you'll pass this class as the template argument instead of connection type.