forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONRowFormatter.h
More file actions
159 lines (125 loc) · 3.8 KB
/
JSONRowFormatter.h
File metadata and controls
159 lines (125 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// JSONRowFormatter.h
//
// Library: Data
// Package: DataCore
// Module: JSONRowFormatter
//
// Definition of the JSONRowFormatter class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef SQL_JSONRowFormatter_INCLUDED
#define SQL_JSONRowFormatter_INCLUDED
#include "Poco/SQL/RowFormatter.h"
namespace Poco {
namespace SQL {
class Poco_SQL_API JSONRowFormatter: public Poco::SQL::RowFormatter
/// Class for JSON formatting of data rows.
///
/// Formatter can be configured to operate in four modes (and
/// certain combinations thereof) :
///
/// - small (condensed mode, only array of values)
///
/// Example:
/// {
/// [["Simpson", "Bart", "Springfield", 12],
/// ["Simpson", "Lisa", "Springfield", 10]]
/// }
///
/// - row count (total row count provided)
///
/// Example:
/// {
/// "count":2,
/// [["Simpson", "Bart", "Springfield", 12],
/// ["Simpson", "Lisa", "Springfield", 10]]
/// }
///
/// - column names (column names provided as a string array)
///
/// Example:
/// {
/// "names":["LastName", "FirstName", "Address", "Age"],
/// [["Simpson", "Bart", "Springfield", 12],
/// ["Simpson", "Lisa", "Springfield", 10]]
/// }
///
/// - full (total row count, column names provided in every row of data)
///
/// Example:
/// {
/// "count":2,
/// [
/// {"LastName": "Simpson", "FirstName": "Bart", "Address": "Springfield", "Age": 12},
/// {"LastName": "Simpson", "FirstName": "Lisa", "Address": "Springfield", "Age": 10}
/// ]
/// }
///
/// Total row count will be specified by the Poco::SQLRecordSet. Note, however, that this is
/// not possible to do accurately in case of result set paging. For those cases, there is
/// setTotalRowCount() member function, which allows to explicitly set the total row count.
/// If the total row count is preset on the formatter, the Data framework shall not interfere.
{
public:
static const int JSON_FMT_MODE_SMALL = 1;
static const int JSON_FMT_MODE_ROW_COUNT = 2;
static const int JSON_FMT_MODE_COLUMN_NAMES = 4;
static const int JSON_FMT_MODE_FULL = 8;
JSONRowFormatter(int mode = (JSON_FMT_MODE_COLUMN_NAMES | JSON_FMT_MODE_SMALL));
/// Creates a new JSONRowFormatter.
~JSONRowFormatter();
/// Destroys the JSONRowFormatter.
std::string& formatNames(const NameVecPtr pNames, std::string& formattedNames);
/// Formats names.
std::string& formatValues(const ValueVec& vals, std::string& formattedValues);
// Formats values.
void setJSONMode(int mode);
/// Sets the mode. Valid mode values are:
/// JSON_FMT_MODE_SMALL
/// JSON_FMT_MODE_ROW_COUNT
/// JSON_FMT_MODE_COLUMN_NAMES
/// JSON_FMT_MODE_FULL
bool printRowCount();
/// Returns true if row count printing is enabled,
/// false otherwise.
bool printColumnNames();
/// Returns true if column names printing is enabled,
/// false otherwise.
bool isSmall();
/// Returns true if compact mode formatting is enabled,
/// false otherwise.
bool isFull();
/// Returns true if full mode formatting is enabled,
/// false otherwise.
private:
void adjustPrefix();
NameVecPtr _pNames;
int _mode;
bool _firstTime;
};
//
// inlines
//
inline bool JSONRowFormatter::printRowCount()
{
return (_mode & JSON_FMT_MODE_ROW_COUNT) != 0;
}
inline bool JSONRowFormatter::printColumnNames()
{
return (_mode & JSON_FMT_MODE_COLUMN_NAMES) != 0;
}
inline bool JSONRowFormatter::isSmall()
{
return (_mode & JSON_FMT_MODE_SMALL) != 0;
}
inline bool JSONRowFormatter::isFull()
{
return (_mode & JSON_FMT_MODE_FULL) != 0;
}
} } // namespace Poco::SQL
#endif // Data_JSONRowFormatter_INCLUDED