]> git.nothing2do.fr Git - diary-shell/commitdiff
I've added command line switch to print everything or not (default is to not print)
authorNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Mon, 29 Jul 2013 11:23:47 +0000 (13:23 +0200)
committerNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Mon, 29 Jul 2013 11:23:47 +0000 (13:23 +0200)
main.cpp
sqldb.cpp
sqldb.h
test-diary-shell.sh

index ae75c2b64edde56ddffc1384841e9976a6642e3a..e8bc072086ab96e7ace5f7aad53b526ea140bc15 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -12,17 +12,25 @@ string date(string a)//transform any date format (in help) to "YYYY-MM-DD HH:MM:
     ss>>y>>"-">>m>>"-">>d>>" ">>h>>":">>m>>":">>s>>".">>x;
     return ss.str();*/
 }
-int main(int argc, char *argv[])
+int main(int argc, char * argv[])
 {
-    cout << "Début du programme '" << argv[0] << "'."  << endl;
+    cout << "Début du programme '" << argv[0] << "'. argc="<<argc<<endl;
     clog<<"======================start=================================\n";
     sqlpg diary;
     string tmp, rep, shell="? ";
     int hm;
+    bool print=false;
+    string t="p";
+    if (argc>1){
+        if (argv[1]==string("p")){
+            print=true;
+        }
+    };
+    clog<<"print="<<print<<endl;
 
 do{
     clog<<"======================again=================================\n";
-    diary.getvar();// you can update var and it affect the actual session (I know, it's a useless thing)
+    diary.getvar();// you can update var and it affect the actual session (I know, it's a CPU consuming thing)
     hm=diary.hmRecord();
     cout<<hm<<shell;
     getline(cin,rep,'\n');
@@ -38,9 +46,11 @@ do{
     else if (words[0]=="!") {
         string a=rep.substr(2);
         diary.exec(a);
-        //diary.print();
+        if(print)diary.print();
+    }
+    else if (diary.alias(words)){
+        if(print)diary.print();
     }
-    else if (diary.alias(words));
     else {
         /*vector<string>::iterator it;
         it = words.begin();
@@ -48,9 +58,10 @@ do{
         it = words.insert (it, "default");
         diary.alias(words);*/
         diary.insert(words);
+        if(print)diary.print();
     }
     words.clear();
 }while(1);
-clog<<"Fin du programme\n";
+clog<<"=====================Fin du programme===========================\n";
 return 0;
 }
index 6912e0c1451587eb259d2d8a3fa02fd27d9f22bf..b69dbadd647343a69d56f10611496c16d14bd069 100644 (file)
--- a/sqldb.cpp
+++ b/sqldb.cpp
@@ -186,16 +186,13 @@ for (int i=0; i<l; i++){
 
     cout<<endl;
 }
-void sqlpg::exec(string a){
+PGresult* sqlpg::exec(string a){
     clog<<"exec(string["<<a<<"])\n";
     res2=PQexecParams(conn, a.c_str(), 0, NULL, 0, NULL, NULL, 0);
-    if (!((PQresultStatus(res2) == PGRES_COMMAND_OK)||(PQresultStatus(res2) == PGRES_TUPLES_OK)))
-    {
-        cout<<"PQerrorMessage(conn) : "<< PQerrorMessage(conn)<<endl;
-        cout<<"PQresStatus(res2)="<<PQresStatus(PQresultStatus(res2))<<endl;
-    };
+    testSQL(res2);
+    return res2;
 }
-void sqlpg::exec(vector<string> a){
+PGresult* sqlpg::exec(vector<string> a){
     clog<<"exec(vector<string>: a.size()=="<<a.size()<<"\n";
     /*const char *paramValues[1];
     int         paramLengths[1];
@@ -225,14 +222,14 @@ res = PQexecParams(conn,
     }
     res=PQexecParams(conn, a[0].c_str(), argc, NULL, value, length, binary, 0);
     testSQL(res);
-    print();
+    //print();
     clog<<"exec(vector<string>) finish\n";
 }
 inline int sqlpg::testSQL(PGresult* a){
     if (!((PQresultStatus(a) == PGRES_COMMAND_OK)||(PQresultStatus(a) == PGRES_TUPLES_OK)))
     {
-        cout<<"PQerrorMessage(conn): "<< PQerrorMessage(conn)<<endl;
-        cout<<"PQresStatus(res)="<<PQresStatus(PQresultStatus(a))<<endl;
+        cerr<<"PQerrorMessage(conn): "<< PQerrorMessage(conn)<<endl;
+        cerr<<"PQresStatus(res)="<<PQresStatus(PQresultStatus(a))<<endl;
     };
 }
 bool sqlpg::alias(vector<string> a){
@@ -246,7 +243,7 @@ bool sqlpg::alias(vector<string> a){
     res=PQexecPrepared(conn, "selectalias", argca, valuea, lengtha, binarya, 0);
     testSQL(res);
     if (PQntuples(res)!=1){
-        cerr<<"alias end pqntuples(res)="<<PQntuples(res)<<endl;
+        cerr<<"alias end more than one alias found ! (pqntuples(res)="<<PQntuples(res)<<")\n";
         return 0;
     }
     string cmd2;
@@ -292,9 +289,9 @@ bool sqlpg::alias(vector<string> a){
     clog<<"PQprepart done(cmd2="<<cmd2<<" argc="<<argc<<"\n";
     testSQL(res);
     res=PQexecPrepared(conn, "execalias", argc, value, length, binary, 0);
-    clog<<"PQexecPrepared done argc="<<argc<<endl;
+    clog<<"PQexecPrepared done.\n";
     testSQL(res);
-    print(); //I prefer to type 'p' (otherwise, I have to add a column in database)
+    //print();
     exec("DEALLOCATE execalias");
     clog<<"alias() end here\n";
     return 1;
@@ -358,7 +355,7 @@ void sqlpg::connect(string service, string name){
     testSQL(res);
     if (PQntuples(res)!=1){
         cerr<<"alias end pqntuples(res)="<<PQntuples(res)<<endl;
-        return 0;
+        return;
     }
     string cmd2;
     cmd2=PQgetvalue(res, 0, 0);
diff --git a/sqldb.h b/sqldb.h
index e9a230fe6dee53410d41f0c9c9a90333977b989c..2783691cf59c2adb8c88bc3e39d228c18183b009 100644 (file)
--- a/sqldb.h
+++ b/sqldb.h
@@ -23,8 +23,8 @@ public:
     int hmRecord();// How Many records in "raw" table
     void printvar();
     inline int testSQL(PGresult*);// test if all gone good
-    void exec(std::vector<std::string>);// first string is for SQL command, other are to replace %1,%2, etc...
-    void exec(std::string);
+    PGresult* exec(std::vector<std::string>);// first string is for SQL command, other are to replace %1,%2, etc...
+    PGresult* exec(std::string);
     bool alias(std::vector<std::string>);
     void compulse();//populate table words
     void connect(std::string, std::string);//connect with user and service (credential in database)
index 7c70b7d4c99d91a1058a516de096003bca2406ad..9d3af615cae86a0847f62c1fc69e053b10ba5ec3 100755 (executable)
@@ -1 +1,2 @@
-sudo -u bob ./diary-shell 2>>error 3>>log
+
+sudo -u bob ./diary-shell $1 3>>error 2>>log