OKlibrary  0.2.1.6
Delete_rows.cpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 1.7.2002 (Swansea)
00002 /* Copyright 2002 - 2007 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00008 // Call:
00009 
00010 // Delete_rows deletion_table subtraction_table test_field
00011 
00012 /* deletes all rows from deletion_table where the field test_field
00013 is in subtraction_table;
00014 */
00015 
00016 #include <string>
00017 #include <algorithm>
00018 #include <memory>
00019 #include <iostream>
00020 
00021 #include <OKlib/General/DatabaseHandler01.hpp>
00022 
00023 using namespace std;
00024 
00025 const string database = "OKRandGen";
00026 
00027 using namespace DatabaseHandler01;
00028 
00029 class eliminate_row {
00030 public :
00031   eliminate_row(const Connection& C, const string& t, const string& f)
00032     : Comm(new Command(C)), table(t), field(f) {}
00033   eliminate_row(const eliminate_row& er)
00034     : Comm(er.Comm), table(er.table), field(er.field) {}
00035   void operator() (const string& value) {
00036     Comm -> issue("delete from " + table + " where " + field + " = " + value + ";");
00037   }
00038 private :
00039   mutable auto_ptr<Command> Comm; // ich glaube, es ist ein Fehler von g++
00040   // (3.0.4), der keine nicht-konstanten Kopier-Konstruktoren zulaesst;
00041   const string& table;
00042   const string& field;
00043 };
00044 
00045 int main(const int argc, const char* const argv[]) {
00046 
00047   if (argc < 4) {
00048     cerr << "ERROR[Delete_rows] Three arguments are needed (the table from which rows are to be deleted, the table containing the rows to be eliminated, and the test field).\n";
00049     return 1;
00050   }
00051   const string deletion_table(argv[1]);
00052   const string subtraction_table(argv[2]);
00053   const string test_field(argv[3]);
00054 
00055   Connection Conn(database);
00056 
00057   Select S(Conn);
00058   S.issue("select " + test_field + " from " + subtraction_table + ";");
00059   const int test_column = S.get_Attribute(test_field).column;
00060   
00061   for_each(RowIterator(S, test_column), RowIterator(S, test_column) + S.number_rows(), eliminate_row(Conn, deletion_table, test_field));
00062 }
00063