summaryrefslogtreecommitdiff
path: root/odb
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-06-21 21:15:36 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-06-21 21:15:36 +0200
commit441e6f1b5901a5bde169f94930c6b42f6a2d5e05 (patch)
tree1efb68cb8fbca900d68ca2fe4296436a42f26c8b /odb
parent3f37eff838e00274e20acc89f58fc36fb3531e94 (diff)
Distinguish between normal and auto-id params in persist statement
This can be necessary to handle auto-id assignment in some databases, such as PostgreSQL.
Diffstat (limited to 'odb')
-rw-r--r--odb/relational/source.hxx54
1 files changed, 43 insertions, 11 deletions
diff --git a/odb/relational/source.hxx b/odb/relational/source.hxx
index 2cf11c2..4d2f10a 100644
--- a/odb/relational/source.hxx
+++ b/odb/relational/source.hxx
@@ -56,7 +56,9 @@ namespace relational
// Query parameter generator. A new instance is created for each
// query, so the customized version can have a counter to implement,
- // for example, numbered parameters (e.g., $1, $2, etc).
+ // for example, numbered parameters (e.g., $1, $2, etc). The auto_id()
+ // function is called instead of next() for the automatically-assigned
+ // object id member when generating the persist statement.
//
struct query_parameters: virtual context
{
@@ -65,6 +67,12 @@ namespace relational
{
return "?";
}
+
+ virtual string
+ auto_id ()
+ {
+ return next ();
+ }
};
struct object_columns: object_columns_base, virtual context
@@ -1753,6 +1761,36 @@ namespace relational
string obj_prefix_;
};
+ // Output a list of parameters for the persist statement.
+ //
+ struct persist_statement_params: object_members_base, virtual context
+ {
+ persist_statement_params (string& params)
+ : params_ (params), count_ (0)
+ {
+ }
+
+ virtual void
+ simple (semantics::data_member& m)
+ {
+ if (!inverse (m))
+ {
+ if (count_++ != 0)
+ params_ += ',';
+
+ if (m.count ("id") && m.count ("auto"))
+ params_ += qp->auto_id ();
+ else
+ params_ += qp->next ();
+ }
+ }
+
+ private:
+ string& params_;
+ size_t count_;
+ instance<query_parameters> qp;
+ };
+
//
//
struct class_: traversal::class_, virtual context
@@ -2028,18 +2066,12 @@ namespace relational
"=" << endl
<< strlit ("INSERT INTO " + table + " (") << endl;
- instance<object_columns> t (false);
- t->traverse (c);
+ instance<object_columns> ct (false);
+ ct->traverse (c);
string values;
- instance<query_parameters> qp;
- for (size_t i (0), n (in_column_count (c)); i < n; ++i)
- {
- if (i != 0)
- values += ',';
-
- values += qp->next ();
- }
+ instance<persist_statement_params> pt (values);
+ pt->traverse (c);
os << strlit (") VALUES (" + values + ")") << ";"
<< endl;