void SQLApp::Query() { SqlBool where; SqlSet borrowed = Select(BOOK_ID).From(BORROW_RECORD).Where(IsNull(RETURNED)); if(query.status == 1) where = ID != borrowed; if(query.status == 2) where = ID == borrowed; SqlBool bdate; if(!IsNull(query.borrowed_from)) bdate = BORROWED >= ~query.borrowed_from; if(!IsNull(query.borrowed_to)) bdate = bdate && BORROWED <= ~query.borrowed_to; if(!bdate.IsEmpty()) where = where && ID == Select(BOOK_ID).From(BORROW_RECORD).Where(bdate); book.Query(where); }
void SqlMassInsert::Flush() { const uint64 DONE = (uint64)-1; if(cache.GetCount() == 0) return; if(use_transaction) 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); String insert; int dialect = sql.GetDialect(); if(findarg(dialect, MY_SQL, PGSQL, MSSQL) >= 0) { insert << "insert into " + ~table + '('; for(int i = 0; i < column.GetCount(); i++) { if(i) insert << ", "; insert << column[i]; } insert << ") values "; for(int i = 0; i < cache.GetCount(); i++) { Row& r = cache[i]; if(r.value.GetCount()) { if(i) insert << ", "; insert << "("; for(int i = 0; i < r.value.GetCount(); i++) { if(i) insert << ", "; insert << SqlCompile(dialect, SqlFormat(r.value[i])); } insert << ")"; } } } else for(int ii = 0; ii < cache.GetCount(); ii++) { uint64 nulls = cache[ii].nulls; if(nulls != DONE) { insert << "insert into " + ~table + '('; bool nextcol = false; for(int i = 0; i < column.GetCount(); i++) { if(!(nulls & ((uint64)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.value.GetCount()) { 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 & ((uint64)1 << i))) { if(nextval) insert << ", "; nextval = true; insert << SqlCompile(dialect, SqlFormat(r.value[i])); } if(dialect == ORACLE) insert << " from dual"; } } } } sql.Execute(insert); if(sql.WasError()) { error = true; if(use_transaction) sql.GetSession().Rollback(); } else if(use_transaction) sql.GetSession().Commit(); cache.Clear(); column.Clear(); pos = 0; }
SqlBool operator||(const SqlBool& a, const SqlBool& b) { if(a.IsEmpty() || a.IsFalse()) return b; if(b.IsEmpty() || b.IsFalse()) return a; if(a.IsTrue() || b.IsTrue()) return true; return SqlBool(a, " or ", b, SqlS::LOR); }
SqlBool operator- (const SqlBool& a, const SqlBool& b) { if(a.IsFalse() || b.IsTrue() || b.IsEmpty()) return SqlBool(false); if(b.IsFalse()) return a; return SqlBool(a, " and not ", SqlS(b(SqlS::COMP), SqlS::HIGH), SqlS::LAND); }
SqlBool operator&&(const SqlBool& a, const SqlBool& b) { if(a.IsEmpty() || a.IsTrue()) return b; if(b.IsEmpty() || b.IsTrue()) return a; if(a.IsFalse() || b.IsFalse()) return false; return SqlBool(a, " and ", b, SqlS::LAND); }
SqlBool operator!(const SqlBool& a) { if(a.IsBool()) return !a.AsBool(); return SqlBool(" not " + a(SqlS::UNARY), SqlS::UNARY); }