summaryrefslogtreecommitdiff
path: root/project2/sql/sqlWriter.cpp
blob: 90de82f3270258be8842fe7da2c0f8dd49a34568 (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
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
#include <pch.hpp>
#include "sqlWriter.h"
#include <boost/foreach.hpp>
#include "sqlVariableBinder.h"

DynamicSql::SqlWriter::SqlWriter()
{
}

DynamicSql::SqlWriter::~SqlWriter()
{
}

DynamicSql::SqlCommand::SqlCommand(const xmlpp::Element * N)
{
	BOOST_FOREACH(xmlpp::Node * n, N->get_children()) {
		const xmlpp::TextNode * t = dynamic_cast<const xmlpp::TextNode *>(n);
		if (t) {
			writers.push_back(new SqlText(t));
		}
		const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(n);
		if (e) {
			if (e->get_name() == "filter") {
				SqlFilterPtr f = new SqlFilter(e);
				writers.push_back(f);
				filters.insert(Filters::value_type(f->name, f));
			}
			else if (e->get_name() == "param") {
				writers.push_back(new SqlParameter(e));
			}
		}
	}
}

Glib::ustring
DynamicSql::SqlCommand::getSqlFor(const Glib::ustring & f) const
{
	BOOST_FOREACH(const SqlCommand::Filters::value_type & filter, filters) {
		filter.second->active = (filter.second->name == f);
	}
	Glib::ustring sql;
	writeSql(sql);
	return sql;
}

void
DynamicSql::SqlCommand::writeSql(Glib::ustring & sql) const
{
	BOOST_FOREACH(const SqlWriterPtr & w, writers) {
		w->writeSql(sql);
	}
}

void
DynamicSql::SqlCommand::bindParams(DB::Command * cmd, unsigned int & offset) const
{
	BOOST_FOREACH(const SqlWriterPtr & w, writers) {
		w->bindParams(cmd, offset);
	}
}

DynamicSql::SqlFilter::SqlFilter(const xmlpp::Element * N) :
	name(N->get_attribute_value("name")),
	active(false)
{
	BOOST_FOREACH(xmlpp::Node * n, N->get_children()) {
		const xmlpp::TextNode * t = dynamic_cast<const xmlpp::TextNode *>(n);
		if (t) {
			writers.push_back(new SqlText(t));
		}
		const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(n);
		if (e) {
			if (e->get_name() == "param") {
				writers.push_back(new SqlParameter(e));
			}
		}
	}
}

void
DynamicSql::SqlFilter::writeSql(Glib::ustring & sql) const
{
	if (active) {
		BOOST_FOREACH(const SqlWriterPtr & w, writers) {
			w->writeSql(sql);
		}
	}
}

void
DynamicSql::SqlFilter::bindParams(DB::Command * cmd, unsigned int & offset) const
{
	if (active) {
		BOOST_FOREACH(const SqlWriterPtr & w, writers) {
			w->bindParams(cmd, offset);
		}
	}
}

DynamicSql::SqlParameter::SqlParameter(const xmlpp::Element * n) :
	Variable(n, boost::optional<Glib::ustring>("local"))
{
}

void
DynamicSql::SqlParameter::writeSql(Glib::ustring & sql) const
{
	sql.append("?");
}

void
DynamicSql::SqlParameter::bindParams(DB::Command * cmd, unsigned int & offset) const
{
	boost::apply_visitor<const SqlVariableBinder, const VariableType>(SqlVariableBinder(cmd, offset++), (*this));
}

DynamicSql::SqlText::SqlText(const xmlpp::TextNode * n) :
	text(n->get_content())
{
}

void
DynamicSql::SqlText::writeSql(Glib::ustring & sql) const
{
	sql.append(text);
}

void
DynamicSql::SqlText::bindParams(DB::Command *, unsigned int &) const
{
}