My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
#include "Sql.h"

NAMESPACE_UPP

SqlMassInsert::~SqlMassInsert()
{
Flush();
}

SqlMassInsert& SqlMassInsert::operator()(SqlId col, const Value& val)
{
if(pos == 0) {
cache.Add().nulls = 0;
cache.Top();
}
if(cache.GetCount() == 1)
column.Add(~col);
else
ASSERT(column[pos] == col.Quoted() || column[pos] == ~col);
Row& r = cache.Top();
r.value.Add(val);
if(IsNull(val))
r.nulls |= (1 << pos);
pos++;
ASSERT(pos < 30);
return *this;
}

SqlMassInsert& SqlMassInsert::EndRow(SqlBool remove)
{
cache.Top().remove = remove;
if(cache.GetCount() && cache[0].value.GetCount() * cache.GetCount() > 5000)
Flush();
ASSERT(column.GetCount() == pos);
pos = 0;
return *this;
}

void SqlMassInsert::Flush()
{
const dword DONE = 0xffffffff;
if(cache.GetCount() == 0)
return;
sql.GetSession().Begin();
SqlBool remove;
bool doremove = false;
for(int ii = 0; ii < cache.GetCount(); ii++) {
SqlBool rm = cache[ii].remove;
if(!rm.IsEmpty()) {
doremove = true;
remove = remove || rm;
}
}
if(doremove)
sql * Delete(table).Where(remove);
for(int ii = 0; ii < cache.GetCount(); ii++) {
dword nulls = cache[ii].nulls;
if(nulls != DONE) {
String insert;
insert << "insert into " + ~table + '(';
bool nextcol = false;
for(int i = 0; i < column.GetCount(); i++) {
if(!(nulls & (1 << i))) {
if(nextcol)
insert << ", ";
nextcol = true;
insert << column[i];
}
}
insert << ')';
bool nextsel = false;
for(int i = ii; i < cache.GetCount(); i++) {
Row& r = cache[i];
if(r.nulls == nulls) {
r.nulls = DONE;
if(nextsel)
insert << " union all";
nextsel = true;
insert << " select ";
bool nextval = false;
for(int i = 0; i < r.value.GetCount(); i++)
if(!(nulls & (1 << i))) {
if(nextval)
insert << ", ";
nextval = true;
insert << SqlCompile(sql.GetDialect(), SqlFormat(r.value[i]));
}
if(sql.GetDialect() == ORACLE)
insert << " from dual";
}
}
sql.Execute(insert);
}
}
if(sql.WasError()) {
error = true;
sql.GetSession().Rollback();
}
else
sql.GetSession().Commit();
cache.Clear();
column.Clear();
pos = 0;
}

END_UPP_NAMESPACE

Change log

r4607 by cxl on Feb 21, 2012   Diff
Sql: Fixed ASSERT in SqlMassInsert
Go to: 
Project members, sign in to write a code review

Older revisions

r4561 by cxl on Feb 8, 2012   Diff
*Sql: fixed MassInsert quoted SqlId
issue(s)
r4526 by cxl on Feb 3, 2012   Diff
Sql: SqlIds now quoted
r4443 by cxl on Jan 19, 2012   Diff
Sql: MassInsert now has remove option
All revisions of this file

File info

Size: 2326 bytes, 107 lines
Powered by Google Project Hosting