summaryrefslogtreecommitdiff
path: root/libdbpp/tablepatch.cpp
blob: 40a1b4181cc29e05d9f9abd6f7543d2287148005 (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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "connection.h"
#include "modifycommand.h"
#include "selectcommand.h"
#include "tablepatch.h"
#include "sqlWriter.h"
#include <buffer.h>
#include <safeMapFind.h>
#include <boost/algorithm/string/join.hpp>

DB::TablePatch::TablePatch() :
	srcExpr(nullptr),
	insteadOfDelete(nullptr),
	where(nullptr),
	order(nullptr),
	doDeletes(true),
	doUpdates(true),
	doInserts(true)
{
}

DB::PatchResult
DB::Connection::patchTable(TablePatch * tp)
{
	if (tp->pk.empty()) {
		throw PatchCheckFailure();
	}
	if (!inTx()) {
		throw TransactionRequired();
	}
	TransactionScope tx(this);
	bool ownedExpr = false;
	if (!tp->srcExpr && !tp->src.empty()) {
		tp->srcExpr = new DB::StaticSqlWriter(tp->src);
		ownedExpr = true;
	}
	DB::PatchResult r {
		tp->doDeletes ? patchDeletes(tp) : 0,
		tp->doUpdates ? patchUpdates(tp) : 0,
		tp->doInserts ? patchInserts(tp) : 0
	};
	if (ownedExpr) {
		delete tp->srcExpr;
		tp->srcExpr = nullptr;
	}
	return r;
}

template<typename Container>
static inline void
push(const boost::format &, typename Container::const_iterator &)
{
}

template<typename Container, typename Value, typename ... Values>
static inline void
push(boost::format & f, typename Container::const_iterator & i, const Value & v, const Values & ... vs)
{
	f % v(i);
	push<Container>(f, i, vs...);
}

template<typename Separator, typename Container, typename ... Ps>
static inline unsigned int
appendIf(AdHoc::Buffer & buf, const Container & c, const boost::function<bool(const typename Container::const_iterator)> & sel, const Separator & sep, const std::string & fmts, const Ps & ... ps)
{
	auto fmt = AdHoc::Buffer::getFormat(fmts);
	unsigned int x = 0;
	for (typename Container::const_iterator i = c.begin(); i != c.end(); ++i) {
		if (sel(i)) {
			if (x > 0) {
				buf.appendbf("%s", sep);
			}
			push<Container>(*fmt, i, ps...);
			buf.append(fmt->str());
			x += 1;
		}
	}
	return x;
}

template<typename Separator, typename Container, typename ... Ps>
static inline unsigned int
append(AdHoc::Buffer & buf, const Container & c, const Separator & sep, const std::string & fmts, const Ps & ... ps)
{
	return appendIf(buf, c, [](auto){ return true; }, sep, fmts, ps...);
}

template <typename Container>
static inline typename Container::key_type
self(const typename Container::const_iterator & i)
{
	return *i;
}
#define selfCols self<decltype(DB::TablePatch::cols)>
#define isKey(tp) [tp](auto i){ return AdHoc::containerContains(tp->pk, *i); }
#define isNotKey(tp) [tp](auto i){ return !AdHoc::containerContains(tp->pk, *i); }

static
void
patchDeletesSelect(AdHoc::Buffer & toDelSql, DB::TablePatch * tp)
{
	toDelSql.append(" WHERE ");
	append(toDelSql, tp->pk, " AND ", " b.%s IS NULL", selfCols);
	if (tp->where) {
		toDelSql.append(" AND ");
		tp->where->writeSql(toDelSql);
	}
	if (tp->order) {
		toDelSql.append(" ORDER BY ");
		tp->order->writeSql(toDelSql);
	}
}

unsigned int
DB::Connection::patchDeletes(TablePatch * tp)
{
	if (tp->beforeDelete) {
		AdHoc::Buffer toDelSql;
		toDelSql.append("SELECT ");
		append(toDelSql, tp->cols, ", ", "a.%s", selfCols);
		toDelSql.appendbf(" FROM %s a LEFT OUTER JOIN ", tp->dest);
		tp->srcExpr->writeSql(toDelSql);
		toDelSql.append(" b ON ");
		append(toDelSql, tp->pk, " AND ", " a.%s = b.%s", selfCols, selfCols);
		patchDeletesSelect(toDelSql, tp);
		auto del = select(toDelSql);
		unsigned int offset = 0;
		tp->srcExpr->bindParams(del.get(), offset);
		if (tp->where) {
			tp->where->bindParams(del.get(), offset);
		}
		if (tp->order) {
			tp->order->bindParams(del.get(), offset);
		}
		tp->beforeDelete(del);
	}
	AdHoc::Buffer toDelSql;
	switch (bulkDeleteStyle()) {
		case BulkDeleteUsingSubSelect:
			{
				// -----------------------------------------------------------------
				// Build SQL to delete keys ----------------------------------------
				// -----------------------------------------------------------------
				if (tp->insteadOfDelete) {
					toDelSql.appendbf("UPDATE %s SET ",
							tp->dest);
					tp->insteadOfDelete->writeSql(toDelSql);
					toDelSql.append(" WHERE (");
				}
				else {
					toDelSql.appendbf("DELETE FROM %s WHERE (",
							tp->dest);
				}
				append(toDelSql, tp->pk, ", ", "%s.%s", [tp](auto){ return tp->dest; }, selfCols);
				// -----------------------------------------------------------------
				// Build SQL to select keys to delete ------------------------------
				// -----------------------------------------------------------------
				toDelSql.append(") IN (SELECT ");
				append(toDelSql, tp->pk, ", ", "a.%s", selfCols);
				toDelSql.appendbf(" FROM %s a LEFT OUTER JOIN ",
						tp->dest);
				tp->srcExpr->writeSql(toDelSql);
				toDelSql.append(" b ON ");
				append(toDelSql, tp->pk, " AND ", " a.%s = b.%s", selfCols, selfCols);
				patchDeletesSelect(toDelSql, tp);
				toDelSql.append(")");
				break;
			}
		case BulkDeleteUsingUsingAlias:
		case BulkDeleteUsingUsing:
			{
				if (tp->insteadOfDelete) {
					toDelSql.appendbf("UPDATE %s a ",
							tp->dest);
				}
				else {
					toDelSql.appendbf("DELETE FROM %s USING %s a ",
							(bulkDeleteStyle() == BulkDeleteUsingUsingAlias ? "a" : tp->dest),
							tp->dest);
				}
				toDelSql.append(" LEFT OUTER JOIN ");
				tp->srcExpr->writeSql(toDelSql);
				toDelSql.append(" b ON ");
				append(toDelSql, tp->pk, " AND ", " a.%s = b.%s ", selfCols, selfCols);
				if (tp->insteadOfDelete) {
					tp->insteadOfDelete->writeSql(toDelSql);
				}
				patchDeletesSelect(toDelSql, tp);
				break;
			}
	}
	auto del = ModifyCommandPtr(newModifyCommand(toDelSql));
	unsigned int offset = 0;
	tp->srcExpr->bindParams(del.get(), offset);
	if (tp->insteadOfDelete) {
		tp->insteadOfDelete->bindParams(del.get(), offset);
	}
	if (tp->where) {
		tp->where->bindParams(del.get(), offset);
	}
	if (tp->order) {
		tp->order->bindParams(del.get(), offset);
	}
	return del->execute();
}

static
void
patchUpdatesSelect(AdHoc::Buffer & updSql, DB::TablePatch * tp)
{
	updSql.append(" WHERE ");
	append(updSql, tp->pk, " AND ", " a.%s = b.%s ", selfCols, selfCols);
	updSql.append(" AND (");
	appendIf(updSql, tp->cols, isNotKey(tp), " OR ",
			" (((CASE WHEN (a.%s IS NULL AND b.%s IS NULL) THEN 1 ELSE 0 END) \
			+ (CASE WHEN(a.%s = b.%s) THEN 1 ELSE 0 END)) = 0)",
			selfCols, selfCols, selfCols, selfCols);
	updSql.append(")");
	if (tp->where) {
		updSql.append(" AND ");
		tp->where->writeSql(updSql);
	}
}

unsigned int
DB::Connection::patchUpdates(TablePatch * tp)
{
	if (tp->cols.size() == tp->pk.size()) {
		// Can't "change" anything... it's all part of the key
		return 0;
	}
	if (tp->beforeUpdate) {
		AdHoc::Buffer updSql;
		updSql.append("SELECT ");
		append(updSql, tp->pk, ", ", "a.%s", selfCols);
		appendIf(updSql, tp->cols, isNotKey(tp), "", ", a.%1% old_%1%", selfCols);
		appendIf(updSql, tp->cols, isNotKey(tp), "", ", b.%1% new_%1%", selfCols);
		updSql.appendbf(" FROM %s a, ", tp->dest);
		tp->srcExpr->writeSql(updSql);
		updSql.append(" b ");
		patchUpdatesSelect(updSql, tp);
		if (tp->order) {
			updSql.append(" ORDER BY ");
			tp->order->writeSql(updSql);
		}
		auto upd = select(updSql);
		unsigned int offset = 0;
		tp->srcExpr->bindParams(upd.get(), offset);
		if (tp->where) {
			tp->where->bindParams(upd.get(), offset);
		}
		if (tp->order) {
			tp->order->bindParams(upd.get(), offset);
		}
		tp->beforeUpdate(upd);
	}
	switch (bulkUpdateStyle()) {
		case BulkUpdateUsingFromSrc:
			{
				// -----------------------------------------------------------------
				// Build SQL for list of updates to perform ------------------------
				// -----------------------------------------------------------------
				AdHoc::Buffer updSql;
				updSql.appendbf("UPDATE %s a SET ",
						tp->dest);
				appendIf(updSql, tp->cols, isNotKey(tp), ", ", " %s = b.%s ", selfCols, selfCols);
				updSql.append(" FROM ");
				tp->srcExpr->writeSql(updSql);
				updSql.append(" b ");
				patchUpdatesSelect(updSql, tp);
				// -----------------------------------------------------------------
				// Execute the bulk update command ---------------------------------
				// -----------------------------------------------------------------
				auto upd = ModifyCommandPtr(newModifyCommand(updSql));
				unsigned int offset = 0;
				tp->srcExpr->bindParams(upd.get(), offset);
				if (tp->where) {
					tp->where->bindParams(upd.get(), offset);
				}
				return upd->execute(true);
			}
			break;
		case BulkUpdateUsingJoin:
			{
				// -----------------------------------------------------------------
				// Build SQL for list of updates to perform ------------------------
				// -----------------------------------------------------------------
				AdHoc::Buffer updSql;
				updSql.appendbf("UPDATE %s a, ",
						tp->dest);
				tp->srcExpr->writeSql(updSql);
				updSql.append(" b SET ");
				appendIf(updSql, tp->cols, isNotKey(tp), ", ", " a.%s = b.%s ", selfCols, selfCols);
				patchUpdatesSelect(updSql, tp);
				if (tp->order) {
					updSql.append(" ORDER BY ");
					tp->order->writeSql(updSql);
				}
				// -----------------------------------------------------------------
				// Execute the bulk update command ---------------------------------
				// -----------------------------------------------------------------
				auto upd = ModifyCommandPtr(newModifyCommand(updSql));
				unsigned int offset = 0;
				tp->where->bindParams(upd.get(), offset);
				if (tp->where) {
					tp->where->bindParams(upd.get(), offset);
				}
				if (tp->order) {
					tp->order->bindParams(upd.get(), offset);
				}
				return upd->execute(true);
			}
		default:
			return 0;
	}
}

static
void
patchInsertsSelect(AdHoc::Buffer & toInsSql, DB::TablePatch * tp)
{
	toInsSql.append("SELECT ");
	append(toInsSql, tp->cols, ", ", "b.%s", selfCols);
	toInsSql.append(" FROM  ");
	tp->srcExpr->writeSql(toInsSql);
	toInsSql.appendbf(" b LEFT OUTER JOIN %s a ON ",
			tp->dest);
	append(toInsSql, tp->pk, " AND ", " a.%s = b.%s", selfCols, selfCols);
	toInsSql.append(" WHERE ");
	append(toInsSql, tp->pk, " AND ", " a.%s IS NULL", selfCols);
	if (tp->order) {
		toInsSql.appendf(" ORDER BY ");
		tp->order->writeSql(toInsSql);
	}
}

unsigned int
DB::Connection::patchInserts(TablePatch * tp)
{
	if (tp->beforeInsert) {
		AdHoc::Buffer toInsSql;
		patchInsertsSelect(toInsSql, tp);
		auto ins = select(toInsSql);
		unsigned int offset = 0;
		tp->srcExpr->bindParams(ins.get(), offset);
		if (tp->order) {
			tp->order->bindParams(ins.get(), offset);
		}
		tp->beforeInsert(ins);
	}
	// -----------------------------------------------------------------
	// Build SQL for copying new records -------------------------------
	// -----------------------------------------------------------------
	AdHoc::Buffer toInsSql;
	toInsSql.appendbf("INSERT INTO %s(",
			tp->dest);
	append(toInsSql, tp->cols, ", ", "%s", selfCols);
	toInsSql.append(")\n");
	patchInsertsSelect(toInsSql, tp);
	auto ins = ModifyCommandPtr(newModifyCommand(toInsSql));
	unsigned int offset = 0;
	tp->srcExpr->bindParams(ins.get(), offset);
	if (tp->order) {
		tp->order->bindParams(ins.get(), offset);
	}
	return ins->execute();
}

std::string
DB::PatchCheckFailure::message() const throw()
{
	return "Santiy checks failed: check table names and keys";
}