Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question
    
libpq wrapper, I presume? What's your reason for not improving/extending/maintaining libpqxx instead? More C++-native API? –  Craig Ringer Jul 26 at 1:27
    
@CraigRinger Universal for PostreSQL (with some PostGIS Utils), MySQL etc. –  Martin Perry Jul 26 at 10:15
    
Using libdbi? Or raw client libraries? –  Craig Ringer Jul 26 at 10:21
    
Most of those libraries are OK, but AFAIK dont have automatic query building as I have been using in PSQLSelect or PSQLInsert. They usually have prepared statements. Plus I have not found anything like PostGIS helper class, so I dont have to write the same functions over and over again (mostly during selects) –  Martin Perry Jul 26 at 14:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.