summaryrefslogtreecommitdiff
path: root/project2/sql/sqlRows.cpp
blob: 867d5cf00fb0a3f50fe7a953a6c86f2c3c3fbed8 (plain)
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
#include "pch.hpp"
#include "sqlRows.h"
#include "sqlHandleAsVariableType.h"
#include "rowProcessor.h"
#include "selectcommand.h"
#include "rdbmsDataSource.h"
#include "column.h"
#include <string.h>
#include "scriptLoader.h"
#include "commonObjects.h"
#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/foreach.hpp>

DECLARE_LOADER("sqlrows", SqlRows);

SqlRows::SqlRows(ScriptNodePtr p) :
	RowSet(p),
	dataSource(p, "datasource"),
	sqlCommand(p->child("sql")),
	db(NULL)
{
}

SqlRows::~SqlRows()
{
}

void
SqlRows::loadComplete(const CommonObjects * co)
{
	db = co->dataSource<RdbmsDataSource>(dataSource());
}

SqlRows::SqlState::SqlState(SelectPtr s) :
	query(s)
{
}

const Columns &
SqlRows::SqlState::getColumns() const
{
	if (columns.empty()) {
		for (unsigned int c = 0; c < query->columnCount(); c++) {
			columns.insert(new Column(c, (*query)[c].name));
		}
	}
	return columns;
}

void
SqlRows::execute(const Glib::ustring & filter, const RowProcessor * rp) const
{
	unsigned int offset = 0;
	SqlState ss(SelectPtr(db->getReadonly().newSelectCommand(sqlCommand.getSqlFor(filter))));
	sqlCommand.bindParams(ss.query.get(), offset);
	while (ss.query->fetch()) {
		HandleAsVariableType h;
		if (ss.fields.empty()) {
			ss.fields.resize(ss.query->columnCount());
		}
		for (unsigned int c = 0; c < ss.query->columnCount(); c++) {
			const DB::Column & col = (*ss.query)[c];
			col.apply(h);
			ss.fields[c] = h.variable;
		}
		ss.process(rp);
	}
}