]> git.nothing2do.fr Git - diary-shell.git/commitdiff
translate '$$' into all word enter by user
authorNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Wed, 10 Jul 2013 07:22:01 +0000 (09:22 +0200)
committerNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Wed, 10 Jul 2013 07:22:01 +0000 (09:22 +0200)
alias.sql
main.cpp
sqldb.cpp
tables1.sql

index 8ca746624cd9842d576ae782eee331e626ee4fdd..085ca833102a9a0e76829ae5c5be28832ec3518e 100644 (file)
--- a/alias.sql
+++ b/alias.sql
@@ -1,8 +1,13 @@
 INSERT INTO alias (nom, command, argc) VALUES ('ls', 'select id,texte from raw order by date asc;', 0);
+insert into alias (nom, command, argc) values ('ls', 'select * from raw where id=$1 order by date asc;', 1);
+insert into alias (nom, command, argc) values ('ls', 'select * from raw where date between $1 and $2 order by date asc;', 2);
 INSERT INTO alias (nom, command, argc) VALUES ('search', 'select id,texte from raw where texte ~ $1 order by date asc;', 1);
 INSERT INTO alias (nom, command, argc) VALUES ('rm', 'DELETE FROM raw WHERE id =$1;', 1);
 INSERT INTO alias (nom, command, argc) VALUES ('alias', 'select nom from alias;', 0);
 insert into alias (nom, command, argc) values ('memo', 'select word from memo;', 0);
-insert into alias (nom, command, argc) values ('memo', 'select id, word, rem where nom=$1 from memo;', 1);
-insert into alias (nom, command, argc) values ('rdv',
-insert into alias (nom, command, argc) values ('var', 'select id, nom, value from var', 0);
+insert into alias (nom, command, argc) values ('memo', 'select id, word, rem from memo where nom=$1;', 1);
+insert into alias (nom, command, argc) values ('var', 'select nom from var', 0);
+insert into alias (nom, command, argc) values ('var', 'select id,nom,value from var where nom=$1', 1);
+insert into alias (command, argc, nom, inf) values ('insert into memo (word, rem) values ($1, ''$$'');', 1, 'memo', '1');
+insert into alias (nom, command, argc) values ('memo', 'select word from memo', 0);
+insert into alias (nom, command, argc) values ('memo', 'select rem from memo where word=$1', 1);
index d39f3e0ea0f41d9cc7eb8d7b25ecf13e50bdb77f..cdf1f22ec7c50d0ea88c4b00d7fd3da560a87d1c 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -15,8 +15,8 @@ string date(string a)//transform any date format (in help) to "YYYY-MM-DD HH:MM:
 void help()
 {
 
-    cout<<"command : var, now, ls, rm {n°}, rm date1 date2, status, help, alias (-/+list/+add),\n";
-    cout<<"select SQL command, \"SQL select\"\n";
+    cout<<"command in c++ : value, print, status, help, alias (-/+list/+add), ! SQL...\n";
+    cout<<"command in SQL : \n";
 }
 
 
index 30f7c19e2426c026290db252b10d833bc1b84f3d..4a5c5e7f7d53b7ae85ee997e7416f5a6c38fbf58 100644 (file)
--- a/sqldb.cpp
+++ b/sqldb.cpp
@@ -15,9 +15,9 @@ sqlpg::sqlpg() {
     connect = "dbname=bob user=bob password=pass connect_timeout=10";
     //conn =  PQconnectStart(var.c_str());
     conn =  PQconnectdb(connect.c_str());
-    clog<<"prepare SQL statement";
-    string cmd="select command,argc from alias where nom=$1 and argc = $2;";
-    clog<<"cmd="<<cmd<<endl;
+    clog<<"prepare SQL statement\n";
+    string cmd="select command,argc,inf from alias where ((nom=$1) and (argc = $2)) OR ((nom=$1) and (argc<$2) and (inf!='0'));";
+    clog<<"SQL statement prepared"<<endl;
     res=PQprepare(conn, "selectalias", cmd.c_str(), 2, NULL);
     testSQL(res);
 
@@ -112,7 +112,7 @@ int sqlpg::insert(vector<string> text) {
 
     }
     else {
-        clog<<"text.size()<2 L111\n";
+        clog<<"text.size()<2\n";
         tmp=text[0];
         string a="now";
         value[0]=a.c_str();
@@ -184,7 +184,7 @@ for (int i=0; i<PQntuples(res); i++){
     for (int j=0; j<lim-1; j++){
             cout<<PQgetvalue(res, i, j)<<"+";
         };
-        cout<<PQgetvalue(res, i, lim-1)<<"\n";
+        cout<<PQgetvalue(res, i, lim-1)<<"|";
     };
 
 cout<<endl;
@@ -224,12 +224,12 @@ res = PQexecParams(conn,
         value[i]=a[i+1].c_str();
         length[i]=a[i+1].size();
         binary[i]=0;
-        cerr<<"i="<<i<<" value="<<value[i]<<" length="<<length[i]<<"\n";
+        clog<<"i="<<i<<" value="<<value[i]<<" length="<<length[i]<<"\n";
     }
     res=PQexecParams(conn, a[0].c_str(), argc, NULL, value, length, binary, 0);
     testSQL(res);
     print();
-    clog<<"exec() finish\n";
+    clog<<"exec(vector<string>) finish\n";
 }
 inline int sqlpg::testSQL(PGresult* a){
     if (!((PQresultStatus(a) == PGRES_COMMAND_OK)||(PQresultStatus(a) == PGRES_TUPLES_OK)))
@@ -241,7 +241,7 @@ inline int sqlpg::testSQL(PGresult* a){
 bool sqlpg::alias(vector<string> a){
     clog<<"alias start here\n";
     string nom=a[0], as=toString(a.size()-1).c_str();
-    int argc=2;
+    int argc=2, inf;
     const char *value[2]={nom.c_str(), as.c_str()};
     int length[2]={nom.size(), as.size()};
     int binary[2]={0, 0};
@@ -253,13 +253,25 @@ bool sqlpg::alias(vector<string> a){
     }
     string cmd2;
     cmd2=PQgetvalue(res, 0, 0);
-    clog<<"cmd2="<<cmd2<<endl;
+    inf=PQgetvalue(res, 0, 2);
+    clog<<"cmd2="<<cmd2<<" inf="<<inf<<endl;
     int pos=0;
     argc=atoi(PQgetvalue(res, 0, 1));
     clog<<"argc="<<argc<<endl;
     res=PQprepare(conn, "execalias",cmd2.c_str(), argc, NULL);
     testSQL(res);
-
+    clog<<"bcl qui tue, inf="<<inf<<"\n";
+    if(1){
+        string b=a[argc+1];
+        int pos=cmd2.find("$$");
+        clog<<"pos="<<pos<<" cmd2="<<cmd2<<"\n";
+        int asize=a.size();
+        for(int i=argc+2;i<asize;i++){
+            b=b+" "+a[i];
+        }
+        clog<<"b="<<b;
+        cmd2.replace(pos, 2, b, 0, string::npos);
+    }
     clog<<"for argc="<<argc<<"\n";
     for(int i=0; i<argc; i++){
         value[i]=a[i+1].c_str();
@@ -279,10 +291,10 @@ void sqlpg::compulse(){
     exec(cmd);
     clog<<"exec1("<<cmd<<") PQntuples(res)="<<PQntuples(res)<<endl;
     int tuples=PQntuples(res);
-    for (int i=0; i<tuples;i++){
+    for (int i=0; i<tuples;i++){// run this for every tuple where analyzed=0
         string buf, txt=PQgetvalue(res, i, PQfnumber(res, "texte"));
         stringstream ss(txt);
-        while (ss >> buf) {//exec for each words in tuples
+        while (ss >> buf) {//exec for each words in tuple
             string a="select time,txt from words where txt='"+buf+"';", command;
             res2=PQexec(conn, a.c_str());
             testSQL(res2);
index a29dfcf637484098ee03860b9763363b1ad7e0d9..bd8583b7fa4cddb73c0eb1a7372396185d6e9496 100755 (executable)
@@ -11,6 +11,7 @@ id serial primary key,
 cmd text,
 argc integer,
 nom text,
+inf bit,
 CONSTRAINT uc_aliasID UNIQUE (nom, argc)
 );