Sql: MassInsert now has remove option

git-svn-id: svn://ultimatepp.org/upp/trunk@4443 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-01-19 14:39:30 +00:00
parent b5930ad68e
commit 03618e002c
2 changed files with 26 additions and 4 deletions

View file

@ -26,8 +26,9 @@ SqlMassInsert& SqlMassInsert::operator()(SqlId col, const Value& val)
return *this;
}
SqlMassInsert& SqlMassInsert::EndRow()
SqlMassInsert& SqlMassInsert::EndRow(SqlBool remove)
{
cache.Top().remove = remove;
if(cache.GetCount() && cache[0].value.GetCount() * cache.GetCount() > 5000)
Flush();
ASSERT(column.GetCount() == pos);
@ -40,6 +41,18 @@ 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(SqlId(table)).Where(remove);
for(int ii = 0; ii < cache.GetCount(); ii++) {
dword nulls = cache[ii].nulls;
if(nulls != DONE) {
@ -79,6 +92,12 @@ void SqlMassInsert::Flush()
sql.Execute(insert);
}
}
if(sql.WasError()) {
error = true;
sql.GetSession().Rollback();
}
else
sql.GetSession().Commit();
cache.Clear();
column.Clear();
pos = 0;

View file

@ -429,6 +429,7 @@ class SqlMassInsert {
struct Row : Moveable<Row> {
dword nulls;
Vector <Value> value;
SqlBool remove;
};
Sql& sql;
@ -436,17 +437,19 @@ class SqlMassInsert {
Vector<String> column;
Vector<Row> cache;
int pos;
bool error;
void NewRow();
public:
SqlMassInsert& operator()(SqlId col, const Value& val);
SqlMassInsert& EndRow();
SqlMassInsert& EndRow(SqlBool remove = SqlBool());
void Flush();
bool IsError() const { return error; }
SqlMassInsert(Sql& sql, SqlId table) : sql(sql), table(~table) { pos = 0; }
SqlMassInsert(Sql& sql, SqlId table) : sql(sql), table(~table) { pos = 0; error = false; }
#ifndef NOAPPSQL
SqlMassInsert(SqlId table) : sql(SQL), table(~table) { pos = 0; }
SqlMassInsert(SqlId table) : sql(SQL), table(~table) { pos = 0; error = false; }
#endif
~SqlMassInsert();
};