Comments (4)
Hmm, good question.
Sometimes, you just don't need to do that.
{
auto s =m_db->prepare...;
// use s a number of times and then discard it
// this would be useful for inserts, for instance
}
Otherwise its tough, I think.
Some brainstorming:
With C++14, you could wrap the preparation into a function with
automatic return type deduction. And then you could use that function in
decltype. I am really looking forward to C++14...
Oh, with C++11 you might be able to use a lambda to do the same. This
should work:
namespace
{
auto prepareCountId = [](connection& db) { return db.prepare(...); }
}
int main()
{
connection db;
using CountId = decltype(prepareCountId(db));
CountId countId = prepareCountId(db);
}
As a sidenote:
As of now, the prepared_* have the database and the statement as
template parameters. Especially in case of a select, it would be
possible to have a slightly less specific class by using only those
parts of the select which are actually needed by the prepared select.
But that would still be a typename you would not want to write by hand...
This might be helpful though to be able to have just one
prepared_statement type if result-type and parameters are the same.
from sqlpp11.
Using the lambda to avoid duplication is a good idea.
I think a simpler prepared statement type is also a good idea because storing the prepared statement in a member seems like a pretty common use case to me, and obtaining the prepared statement type is not not really trivial and user friendly at the moment.
For reference, this is my current solution:
namespace
{
// query lambda and type alias
auto childCountQuery = [] () {
return select(count(objects.Id).as(numObjects))
.from(objects)
.where(objects.ParentId == parameter(objects.ParentId));
};
using ChildCountQuery = decltype(childCountQuery());
// ... other queries
// type alias for the prepared statement types
template <typename SelectType>
using PreparedStatement = decltype(((sql::connection*)nullptr)->prepare(*((SelectType*)nullptr)));
}
// member structure containing the prepared statements (used as pimpl data member)
struct Database::PreparedStatements
{
std::unique_ptr<PreparedStatement<ObjectCountQuery>> objectCount;
std::unique_ptr<PreparedStatement<ChildCountQuery>> childCount;
};
// called at construction time to prepare the statements
void Database::prepareStatements()
{
m_statements->objectCount.reset(new PreparedStatement<ObjectCountQuery>(m_db->prepare(objectCountQuery())));
m_statements->childCount.reset(new PreparedStatement<ChildCountQuery>(m_db->prepare(childCountQuery())));
}
It would be nice if the prepared statement would allow default construction which would avoid having to use the unique_ptr, making the code a little more elegant
from sqlpp11.
On 2014-08-10 22:47, dirkvdb wrote:
Using the lambda to avoid duplication is a good idea.
:-)
I think a simpler prepared statement type is also a good idea because
storing the prepared statement in a member seems like a pretty common
use case to me, and obtaining the prepared statement type is not not
really trivial and user friendly at the moment.As stated before, it won't be that much simpler (unless you want to
sacrifice the type safety in setting parameters and obtaining results).For reference, this is my current solution:
Thanks!
It would be nice if the prepared statement would allow default
construction which would avoid having to use the unique_ptr, making
the code a little more elegant
I was conservative about the default constructor because a default
constructed prepared statement is unusable. But yes, that unique_ptr
code is ugly. I therefore added default constructibility for sqlite3
(develop branch). Others will follow soon.
Please note again that you must not use the default-constructed prepared
statement for anything but assigning it a "real" prepared statement.
Best,
Roland
from sqlpp11.
As stated before, it won't be that much simpler (unless you want to
sacrifice the type safety in setting parameters and obtaining results).
You are right, reducing some of the template arguments doesn't help
I therefore added default constructibility for sqlite3
(develop branch). Others will follow soon.
Great, looks much nicer now:
struct Database::PreparedStatements
{
PreparedStatement<ObjectCountQuery> objectCount;
PreparedStatement<ChildCountQuery> childCount;
};
void Database::prepareStatements()
{
m_statements->objectCount = m_db->prepare(objectCountQuery());
m_statements->childCount = m_db->prepare(childCountQuery());
}
from sqlpp11.
Related Issues (20)
- Length of the blob received by "select" HOT 2
- Datetime column breaking insertions into database HOT 9
- sqlpp/connection_pool.h: pooled_connection is inaccessable HOT 1
- SQLite3 connection pool error using HOT 4
- Support for pragma tables HOT 1
- sqlite3_step return code 5 with connection pool HOT 2
- how to get a column by string? HOT 2
- how can I use a in where condition,I always get an error? HOT 4
- mingw64, vcpkg, CMake, Project installation cannot find mariadb ? HOT 5
- sqlite connector: execute() silently ignores statements after the first one HOT 7
- how to use binary equal in where HOT 2
- Is there any opentracing support in sqlpp,like jaeger?Will it be supported in the future? HOT 1
- how to fix update exception? HOT 23
- How to retrieve the row count of a query result? HOT 6
- mysql does not seem to support the TIME type HOT 4
- Multiple table query error HOT 3
- Mysql prepared_statement.h missing _bind_time_of_day_parameter function HOT 3
- How do I get record with count of references from one to many? HOT 4
- Handling of `::sqlpp::tag::enforce_null_result_treatment` does not seem to be implemented, NULL documentation is erroneous HOT 11
- scripts/sqlite2cpp.py requires insert of nullable column HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlpp11.