I am writing my own wrapper for PostgreSQL (but it can be used for other engines too).
I am using this desgin:
PSQLPreparedStatement st("SELECT ? FROM ? WHERE name = '? xx' AND surname = 'xx ? xx';");
st.SetValue(0, "*");
st.SetValue(1, "test_table");
st.Prepare();
st.SetValue(0, "name");
st.Prepare();
PostgreSQLWrapper psql;
psql.Connect();
PSQLInsert insert{ "name", "surname" };
insert.SetTable("test_table");
insert.AddRow({ { "name", "xx" }, { "surname", "x" } });
insert.AddRow({ { "name", "xyyyx" }});
std::unordered_map<MyStringAnsi, MyStringAnsi> data;
psql.RunQuery("UPDATE test_table SET name='John'");
//============
// Example 1
PSQLSelect select;
select.SetBaseTable("test_table");
select.AddColumn("name");
select.AddColumn("surname");
select.AddColumn("id");
auto lambda = [&](std::unordered_map<COL_NAME, char *> & data, int row, int totalRows) -> void
{
printf("%s %s %s\n", data["name"], data["surname"], data["id"]);
};
psql.Select(select, lambda);
//===============
// Example 2
psql.Select("SELECT name, surname, id FROM test_table", [&](std::unordered_map<COL_NAME, char *> & data, int row, int totalRows) -> void
{
printf("%s %s %s\n", data["name"], data["surname"], data["id"]);
}
);
In the background, the classes create SQL queries through string processing. I cache almost everyhing to speed things up. I am also using my string class with some search/replace substring modifications (like the KMP algorithm).
It is written to be readable code with easy to add new functionality and easy to read source code.
Is this an OK solution? Is it worthwile to add some more functions and made this an open library project?