-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruleTable.cpp
More file actions
229 lines (207 loc) · 6.92 KB
/
ruleTable.cpp
File metadata and controls
229 lines (207 loc) · 6.92 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdlib.h>
#include <string>
#include <iostream>
#include <regex>
#include <pqxx/pqxx>
#include <structuredLogger.h>
#include "ruleTable.h"
/// \version $Revision: $
/// \date $Date: $
/// \file genericTable.cpp
/// \class genericTable
///\fn ruleTable::ruleTable
///\arg none
///<< constructor no args
///<< connect to rule table to fill in the fields
///
/**
** fields
** id big serial
** rule_name var char
** regex var char
** rule_set foreign key
** severity enum notification warn error fatal debug (db type)
**/
ruleTable::ruleTable() : genericTable()
{
m_tableName = "rule"; //ctor
setCusorCallBack(std::bind(&ruleTable::readCusorHandler,this,std::placeholders::_1));
}
///\fn ruleTable::ruleTable
///\arg std::string connection
///<< dbname=[databaseName] user=[database user] password=[md5 encypted password] hostadd=[ip or localhost] port=[5432]
///<< constructor string for database connection
///<< connect to rule table to fill in the fields
///
ruleTable::ruleTable(std::string &dbconnection) : genericTable(dbconnection)
{
// file in field and type string vectors
std::string tableName("rule");
m_tableName = "rule";
retriveTableMetadata(tableName);
mp_readCusorHandler = std::bind(&ruleTable::readCusorHandler,this,std::placeholders::_1);
}
ruleTable::~ruleTable()
{
//dtor
}
///\fn readCusorHandler
///\arg void * point to the instance
///\arg const pqxx::result::tuple &row
///\brief static funtion in the class ruleTable to handle the call back when walking through
///\brief all the rules
///
void ruleTable::readCusorHandler( const pqxx::result &row)
//void ruleTable::readCusorHandler(void *classInstPtr, const pqxx::result::tuple &row)
{
/** debug
for (pqxx::result::tuple::iterator it=row.begin();
it != row.end() ; it++)
std::cout << it.c_str() << " ";
std::cout << std::endl;
*/
}
///\fn ruleTable::readCusor
///\arg pqxx::conection * dbc
///\arg std::vector<std::string> fieldsToRead ,
///\arg std::condtion
///\brief read the specified fields from a table and rows selected by the condition supplied
///\brief could also be the result of a union ???
///\brief for reads of extremly large return sets (how much memory do you have ???)
///\brief virtual
///
pqxx::result ruleTable::readCusor(pqxx::connection *dbc ,
std::vector<std::string> &fieldsToRead,
std::string &condition,
void * instancePtr,
long cursorSize)
{
pqxx::result r;
// get record count before we begin
size_t countRecords=getRecordCount(condition);
if(dbc == nullptr)
{
dbc = mp_dbc;
}
std::string sqlquery= "SELECT ";
for (std::vector<std::string>::iterator it =fieldsToRead.begin() ;
it != fieldsToRead.end(); it++)
sqlquery.append(*it + ",");
sqlquery.erase(sqlquery.length() - 1,1) ; // remove extra comma
sqlquery.append(" FROM " + m_tableName + " ");
if (!condition.empty()) {
sqlquery.append(condition);
}
sqlquery.append(";");
try {
pqxx::work W(*dbc);
pqxx::stateless_cursor<pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
cursor(W, sqlquery, "mycursor", true);
/* Assume you know total number of records returned */
/* todo detrmine the total number of records */
/* see getRecordCount */
/** Walk through all the records/rows one at a time.
* This could be changed to the number of rows the handler wants
**/
for (size_t idx = 0; idx < countRecords; ) {
pqxx::result r = cursor.retrieve(idx,cursorSize);
/**
* pass just tuple
for (size_t rownum = 0; rownum < r.size(); rownum++) {
const pqxx::result::tuple row = r[rownum];
if (mp_readCusorHandler != nullptr) {
(*mp_readCusorHandler)(instancePtr,row);
}
//used mapping of data type to tuple for converting to binary data types
//vid1 = row[0].as<int>();
//vid2 = row[1].as<int>();
//vid3 = row[2].as<int>();
}
**/
/**
* pass set of rows
*/
if (mp_readCusorHandler != nullptr && r.size() > 0 ) {
///(*mp_readCusorHandler)(instancePtr, r);
mp_readCusorHandler(r);
}
idx += cursorSize;
}
}
catch (const pqxx::sql_error &e)
{
dumpPqxxException(e);
}
catch (const std::exception &e) {
dumpException(e);
}
return r;
}
///\fn ruleTable::readFields
///\arg pqxx::conection * dbc
///\arg std::vector<std::string> fieldsToRead ,
///\arg std::string condtiion
///\brief read the specified fields from a table and rows selected by the condition supplied
///\brief could also be the result of a union ???
///\brief virtual
pqxx::result ruleTable::readFields(pqxx::connection *dbc ,
std::vector<std::string> &fieldsToRead,
std::string &condition)
{
pqxx::result r;
try
{
pqxx::connection *wdbc = (dbc != nullptr) ? dbc : mp_dbc;
pqxx::work txn (*wdbc);
std::vector<std::string> columnsToRead = (! fieldsToRead.empty() ) ? fieldsToRead : m_fields;
// reads full table no conditions
//pqxx::tablereader trd (txn,m_tableName,columnsToRead.begin(),columnsToRead.end());
// or
std::string query("SELECT ");
//
// walk thru vector to see what we want
//
std::vector<std::string>::iterator sit = columnsToRead.begin();
while (sit != columnsToRead.end() )
{
query.append(*sit);
sit++;
if (sit != columnsToRead.end())
{
query.append(" , ");
}
}
if ( ! condition.empty())
query.append( " FROM " + m_tableName + " WHERE " + condition );
else
query.append( " FROM " + m_tableName);
/* todo get the number of records */
r = txn.exec(query);
}
catch (const pqxx::sql_error &e)
{
dumpPqxxException(e);
}
catch(const std::exception &e)
{
dumpException(e,condition);
// option to rethrow
//rethrow(e);
}
return r;
}
///\fn ruleTable::getRecordCount
///\arg [in] std::string condition
///\return [out] long
///\brief get the record count using a default if no condition is set
///
long ruleTable::getRecordCount(std::string & condition)
{
std::string defaultSql ("SELECT count(*) from " + m_tableName + " ");
if (!condition.empty() ) {
defaultSql.append("WHERE ");
defaultSql.append(condition );
}
defaultSql.append(" ;");
return genericTable::getRecordCount(defaultSql);
}