1
- POCO C++ Libraries ActiveRecord Framework
2
1
POCO ActiveRecord User Guide
2
+ POCO ActiveRecord Framework
3
3
4
4
!!!Introduction
5
5
@@ -9,9 +9,10 @@ to relieve developers from having to write lots of boilerplate database
9
9
query code for common operations like finding an object by ID, updating an object, deleting
10
10
an object or running paged queries. As its name implies, the framework follows
11
11
the well-known [[https://en.wikipedia.org/wiki/Active_record_pattern Active Record]]
12
- architectural pattern. It's based on a code generator (named *Active Record Compiler*,
12
+ architectural pattern. It's based on a code generator (named <*ActiveRecord Compiler*> ,
13
13
or <[arc]>) and uses a convention-over-configuration approach.
14
14
15
+
15
16
!!!Getting Started
16
17
17
18
The starting point for using the ActiveRecord framework is an XML file.
@@ -21,9 +22,9 @@ generates corresponding header and source files defining and implementing
21
22
the respective C++ classes, as well as type handlers for the POCO Data
22
23
library.
23
24
24
- Following is a first example for such an XML file. The file defines two
25
+ Following is an example for such an XML file. The file defines two
25
26
classes, an `Employee` class (mapped to a table named `employees`), and
26
- a `Role` class (mapped to a table names `roles`).
27
+ a `Role` class (mapped to a table named `roles`).
27
28
28
29
<project namespace="Sample">
29
30
<class name="Employee" table="employees">
@@ -46,14 +47,19 @@ There is a n:1 relationship between `Employee` and `Role` (each employee
46
47
has exactly one role). Furthermore, each employee can optionally have
47
48
a manager, which is again an `Employee`.
48
49
50
+ Properties named `id` are considered to be primary keys, unless a different
51
+ property has been designated the primary key (with the `key` attribute in
52
+ the `class` element). It's also possible to have objects without a primary key
53
+ or ID column (keyless active records).
54
+
49
55
The generated C++ classes will be in the `Sample` namespace, as specified
50
56
in the <[project]> element.
51
57
52
58
The definitions in the XML file correspond to the database schema built
53
59
by the following <[CREATE TABLE]> statements:
54
60
55
61
CREATE TABLE employees (
56
- id CHAR(36),
62
+ id CHAR(36) PRIMARY KEY ,
57
63
name VARCHAR(64),
58
64
ssn VARCHAR(32),
59
65
role INTEGER,
@@ -67,6 +73,9 @@ by the following <[CREATE TABLE]> statements:
67
73
);
68
74
----
69
75
76
+ If the database engine supports it, the `id` column of the `employees` table can be
77
+ an UUID as well.
78
+
70
79
Running the ActiveRecord Compiler with the above XML file (sample.xml) with the
71
80
following statement:
72
81
@@ -85,8 +94,8 @@ will create the following files in the current working directory:
85
94
----
86
95
87
96
The generated classes are derived from the Poco::ActiveRecord::ActiveRecord class
88
- template and have accessor methods for all properties, as well as methods
89
- for creating, updating and deleting instances in the database.
97
+ template and have accessor methods for all properties defined in the XML file,
98
+ as well as methods for creating, updating and deleting instances in the database.
90
99
91
100
ActiveRecord objects are reference counted, and every generated class contains
92
101
a `Ptr` type alias for an appropriate Poco::AutoPtr<>.
@@ -264,6 +273,10 @@ allows some additional flexibility.
264
273
const auto result = query.execute();
265
274
----
266
275
276
+ The lambda expression is passed a const reference to an ActiveRecord object and
277
+ must return a `bool`. If `true` is returned, the object is included in the result,
278
+ otherwise not.
279
+
267
280
!Relations
268
281
269
282
Relations (defined in the XML file as properties with a `references` attribute)
@@ -284,7 +297,7 @@ In the following sample, the `role` property is set with the key value, whereas
284
297
pEmployee->create(pContext);
285
298
----
286
299
287
- !Auto-increment Keys and Auto-generated UUIDs on Insert
300
+ !Auto-Increment Keys and Auto-Generated UUIDs on Insert
288
301
289
302
ActiveRecord supports auto-incrementing keys when inserting an ActiveRecord. T
290
303
o enable this feature, the `autoIncrementID` attribute in the `class` element needs
@@ -297,7 +310,16 @@ mechanisms.
297
310
When inserting an ActiveRecord with an all-null UUID, a random UUID will be generated
298
311
before executing the `INSERT` statement.
299
312
300
- !!!XML Reference
313
+ !Keyless Active Records
314
+
315
+ It is possible to define classes without an ID or primary key property. For these objects,
316
+ no `find()` method will be generated, and updating these objects is also not possible
317
+ (`update()` will throw a Poco::NotImplementedException).
318
+
319
+ Keyless ActiveRecord objects can be retrieved by executing a Poco::ActiveRecord::Query.
320
+
321
+
322
+ !!!Compiler XML Reference
301
323
302
324
!!Supported Data Types
303
325
0 commit comments