Skip to content

Commit 0ba235d

Browse files
committed
update ActiveRecord documentation
1 parent 9a2711a commit 0ba235d

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

ActiveRecord/doc/ActiveRecordUserGuide.page

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
POCO C++ Libraries ActiveRecord Framework
21
POCO ActiveRecord User Guide
2+
POCO ActiveRecord Framework
33

44
!!!Introduction
55

@@ -9,9 +9,10 @@ to relieve developers from having to write lots of boilerplate database
99
query code for common operations like finding an object by ID, updating an object, deleting
1010
an object or running paged queries. As its name implies, the framework follows
1111
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*>,
1313
or <[arc]>) and uses a convention-over-configuration approach.
1414

15+
1516
!!!Getting Started
1617

1718
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
2122
the respective C++ classes, as well as type handlers for the POCO Data
2223
library.
2324

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
2526
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`).
2728

2829
<project namespace="Sample">
2930
<class name="Employee" table="employees">
@@ -46,14 +47,19 @@ There is a n:1 relationship between `Employee` and `Role` (each employee
4647
has exactly one role). Furthermore, each employee can optionally have
4748
a manager, which is again an `Employee`.
4849

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+
4955
The generated C++ classes will be in the `Sample` namespace, as specified
5056
in the <[project]> element.
5157

5258
The definitions in the XML file correspond to the database schema built
5359
by the following <[CREATE TABLE]> statements:
5460

5561
CREATE TABLE employees (
56-
id CHAR(36),
62+
id CHAR(36) PRIMARY KEY,
5763
name VARCHAR(64),
5864
ssn VARCHAR(32),
5965
role INTEGER,
@@ -67,6 +73,9 @@ by the following <[CREATE TABLE]> statements:
6773
);
6874
----
6975

76+
If the database engine supports it, the `id` column of the `employees` table can be
77+
an UUID as well.
78+
7079
Running the ActiveRecord Compiler with the above XML file (sample.xml) with the
7180
following statement:
7281

@@ -85,8 +94,8 @@ will create the following files in the current working directory:
8594
----
8695

8796
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.
9099

91100
ActiveRecord objects are reference counted, and every generated class contains
92101
a `Ptr` type alias for an appropriate Poco::AutoPtr<>.
@@ -264,6 +273,10 @@ allows some additional flexibility.
264273
const auto result = query.execute();
265274
----
266275

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+
267280
!Relations
268281

269282
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
284297
pEmployee->create(pContext);
285298
----
286299

287-
!Auto-increment Keys and Auto-generated UUIDs on Insert
300+
!Auto-Increment Keys and Auto-Generated UUIDs on Insert
288301

289302
ActiveRecord supports auto-incrementing keys when inserting an ActiveRecord. T
290303
o enable this feature, the `autoIncrementID` attribute in the `class` element needs
@@ -297,7 +310,16 @@ mechanisms.
297310
When inserting an ActiveRecord with an all-null UUID, a random UUID will be generated
298311
before executing the `INSERT` statement.
299312

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
301323

302324
!!Supported Data Types
303325

ActiveRecord/include/Poco/ActiveRecord/Query.h

+33
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ namespace ActiveRecord {
2828

2929
template <typename ActRec>
3030
class Query
31+
/// A Query is used to retrieve ActiveRecord objects from a table.
32+
///
33+
/// As the name implies, Query supports selection of database rows
34+
/// based on a WHERE clause (see where()). Furthermore, results can
35+
/// be sorted (see orderBy()) and filtered based on a lambda expression
36+
/// (see filter()).
37+
///
38+
/// Optional result paging is controlled by offest() and limit().
39+
/// The total number of results is available via totalResults().
3140
{
3241
public:
3342
explicit Query(Context::Ptr pContext):
@@ -43,37 +52,55 @@ class Query
4352
Query& operator = (const Query&) = delete;
4453

4554
Query& where(const std::string& clause)
55+
/// Specify a WHERE clause (without the WHERE keyword)
56+
/// to select only rows matching the clause.
57+
///
58+
/// Placeholders (?) can be used in the clause. For each
59+
/// placeholder, an actual value must be bound before
60+
/// the query is executed (see bind()).
4661
{
4762
_select << " WHERE " << fixPlaceholders(clause);
4863
return *this;
4964
}
5065

5166
Query& orderBy(const std::string& order)
67+
/// Specify a column name and optional direction (ASC, DESC)
68+
/// to order the result by.
5269
{
5370
_select << " ORDER BY " << order;
5471
return *this;
5572
}
5673

5774
template <typename T>
5875
Query& bind(const T& value)
76+
/// Bind a value to a placeholder in the where clause.
5977
{
6078
_select, Poco::Data::Keywords::bind(value);
6179
return *this;
6280
}
6381

6482
Query& offset(std::size_t offset)
83+
/// Specify the index or offset of the first row
84+
/// to return for paging.
6585
{
6686
_offset = offset;
6787
return *this;
6888
}
6989

7090
Query& limit(std::size_t limit)
91+
/// specify the maximum number of rows to return for paging.
7192
{
7293
_limit = limit;
7394
return *this;
7495
}
7596

7697
Query& filter(const std::function<bool(const ActRec&)>& fn)
98+
/// Specify a lambda expression for filtering results.
99+
///
100+
/// The lamda takes a const reference to the ActiveRecord
101+
/// (template argument) as parameter and must return a
102+
/// bool. If the lambda returns true, the respective ActiveRecord
103+
/// is included in the query result.
77104
{
78105
_filter = fn;
79106
return *this;
@@ -86,6 +113,8 @@ class Query
86113
}
87114

88115
std::vector<typename ActRec::Ptr> execute()
116+
/// Execute the query and return a vector with the
117+
/// results.
89118
{
90119
std::vector<typename ActRec::Ptr> result;
91120

@@ -115,11 +144,15 @@ class Query
115144
}
116145

117146
std::size_t totalResults() const
147+
/// In case of a paged query, returns the total number of results
148+
/// that would be returned without paging.
118149
{
119150
return _totalResults;
120151
}
121152

122153
void reset()
154+
/// Resets the query so that it can be executed again, with
155+
/// potentially different parameters.
123156
{
124157
_offset = 0;
125158
_limit = 0;

release/spec/all.release

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ PageCompiler
1111
PageCompiler/File2Page
1212
MongoDB
1313
Redis
14+
ActiveRecord
15+
ActiveRecord/Compiler

0 commit comments

Comments
 (0)