]> git.nothing2do.fr Git - diary-shell.git/commitdiff
now ' character don't make anymore crash
authorNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Sat, 6 Jul 2013 11:10:23 +0000 (13:10 +0200)
committerNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Sat, 6 Jul 2013 11:10:23 +0000 (13:10 +0200)
TODO : table var must be usefully populated

alias.sql
main.cpp
sqldb.cpp
tables1.sql
test-diary-shell.sh

index 8b426c9a6618e9a97e74bbcae58fef78f26e4ffa..8ca746624cd9842d576ae782eee331e626ee4fdd 100644 (file)
--- a/alias.sql
+++ b/alias.sql
@@ -1,7 +1,8 @@
 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 ('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 ('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 from memo;"', 1);
+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);
index 03763b163c97df29eb3e0315f10cdd51811b8ceb..d39f3e0ea0f41d9cc7eb8d7b25ecf13e50bdb77f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -15,7 +15,7 @@ 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, date {arg}, help, alias (-/+list/+add),\n";
+    cout<<"command : var, now, ls, rm {n°}, rm date1 date2, status, help, alias (-/+list/+add),\n";
     cout<<"select SQL command, \"SQL select\"\n";
 }
 
@@ -24,6 +24,7 @@ void help()
 int main(int argc, char *argv[])
 {
     cout << "Début du programme '" << argv[0] << "'. Nombre de paramètres reçus : " << argc  << endl;
+    cerr<<"===start===\n";
     sqlpg diary;
     string tmp, rep, shell="? ";
     int hm;
@@ -42,11 +43,9 @@ do{
     else if (words[0]=="help") help();
     else if (words[0]=="status") diary.status();
 
-    else if (words[0]=="var") diary.var();
-    //else if (words[0]=="now") cout<<diary.now()<<endl;
-    else if (words[0]=="alias") {
-        if (words.size()==1) {diary.exec("select nom from alias;");diary.print();}
-        else if (words[1]=="add") {
+    else if (words[0]=="value") diary.var();
+    else if ((words[0]=="alias")&&(words.size()>1)) {
+        if (words[1]=="add") {
             string cmd="";
             for (int a=3; a<words.size()-1; a++){
                 cmd=cmd+words[a]+" ";
index f921ef21a84fc6d13c8273a60393e9213ff4bd42..27c87fbfd5d9cbe6f95259dbecf1b395e821e443 100644 (file)
--- a/sqldb.cpp
+++ b/sqldb.cpp
@@ -11,10 +11,16 @@ using namespace std;
 
 sqlpg::sqlpg() {
 
-    //var = "host=postgresql1.alwaysdata.com port=5432 dbname=nothing2do.eu_diary user=nothing2do.eu password=x connect_timeout=10";
+    //connect = "host=postgresql1.alwaysdata.com port=5432 dbname=nothing2do.eu_diary user=nothing2do.eu password=x connect_timeout=10";
     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;
+    res=PQprepare(conn, "selectalias", cmd.c_str(), 2, NULL);
+    testSQL(res);
+
 }
 
 int sqlpg::status(){
@@ -42,17 +48,18 @@ template<typename T> string sqlpg::toString( const T & Value ){
     return oss.str();
 }
 int sqlpg::insert(vector<string> text) {
-    cerr<<"insert(vector<string>) begin\n";
-    string command="INSERT INTO raw (date, texte) VALUES ($1::timestamp, $2);";
+    clog<<"insert(vector<string>) begin\n";
+    string command="INSERT INTO raw (date, texte) VALUES ($1::timestamp, $2::text);";
     string tmp="";
     int argc=2;
     const char * value[2];
     int length[2];
     int binary[2];
+    PQprepare(conn, "insert", command.c_str(), 2, NULL);
 
     if (text.size()>2){
         if ((isdate(text[0]))+(isdate(text[1]))==3){
-            cerr<<"text[0]+text[1]==3(L55)\n";
+            clog<<"text[0]+text[1]==3(L55)\n";
             date=text[0];
             string a=date+" "+text[1];
             value[0]=a.c_str();
@@ -69,7 +76,7 @@ int sqlpg::insert(vector<string> text) {
 
         }
         else if((text[0]=="-")&&(isdate(text[1])==2)){
-            cerr<<"date partielle(L72)\n";
+            clog<<"date partielle(L72)\n";
             tmp=text[2];
             string a=date+" "+text[1];
             value[0]=a.c_str();
@@ -86,7 +93,7 @@ int sqlpg::insert(vector<string> text) {
 
         }
         else{
-            cerr<<"text.size()>2; text[0] & text[1] isn't date. L89";
+            clog<<"text.size()>2 && text[0] & text[1] isn't date. L89";
             tmp=text[0];
             string a="1971-11-11 22:22:22";
             value[0]=a.c_str();
@@ -105,11 +112,9 @@ int sqlpg::insert(vector<string> text) {
 
     }
     else {
-        cerr<<"text.size()<2 L111\n";
+        clog<<"text.size()<2 L111\n";
         tmp=text[0];
-        cerr<<"113\n";
         string a="now";
-        cerr<<"115\n";
         value[0]=a.c_str();
         length[0]=a.size();
         binary[0]=0;
@@ -122,11 +127,12 @@ int sqlpg::insert(vector<string> text) {
         //value[1]=now().c_str();
         length[1]=19;
         binary[1]=0;
-        cerr<<"text.size()<2 finish\n";
+        clog<<"text.size()<2 finish\n";
     };
-    res=PQexecParams(conn, command.c_str(), argc, NULL, value, length, binary, 0);
+    //res=PQexecParams(conn, command.c_str(), argc, NULL, value, length, binary, 0);
+    res=PQexecPrepared(conn, "insert", argc, value, length, binary, 0);
     testSQL(res);
-    cerr<<"insert(vector<string>) end\n";
+    clog<<"insert(vector<string>) end\n";
     //exec(command);
     return 0;
 }
@@ -162,6 +168,7 @@ int sqlpg::hmRecord(){
     //res=PQexec(conn, command.c_str());
     exec(command);
     a=PQntuples(res);
+    clog<<"hmRecord()="<<a<<endl;
     return a;
 }
 void sqlpg::var(){
@@ -183,18 +190,16 @@ for (int i=0; i<PQntuples(res); i++){
 cout<<endl;
 }
 void sqlpg::exec(string a){
-    cerr<<"exec(string "<<a<<")\n";
+    clog<<"exec(string["<<a<<"])\n";
     res=PQexecParams(conn, a.c_str(), 0, NULL, 0, NULL, NULL, 0);
-    cerr<<"exec:1/2\n";
     if (!((PQresultStatus(res) == PGRES_COMMAND_OK)||(PQresultStatus(res) == PGRES_TUPLES_OK)))
     {
         cout<<"PQerrorMessage(conn) : "<< PQerrorMessage(conn)<<endl;
         cout<<"PQresStatus(res)="<<PQresStatus(PQresultStatus(res))<<endl;
     };
-    cerr<<"exec() finish\n";
 }
 void sqlpg::exec(vector<string> a){
-    cerr<<"exec(vector<string>: a.size()=="<<a.size()<<"\n";
+    clog<<"exec(vector<string>: a.size()=="<<a.size()<<"\n";
     /*const char *paramValues[1];
     int         paramLengths[1];
     int         paramFormats[1];
@@ -211,7 +216,7 @@ res = PQexecParams(conn,
 
 */
     int argc=a.size()-1;
-    cerr<<"argc="<<argc<<endl;
+    clog<<"argc="<<argc<<endl;
     const char *value[argc];
     int length[argc];
     int binary[argc];
@@ -224,7 +229,7 @@ res = PQexecParams(conn,
     res=PQexecParams(conn, a[0].c_str(), argc, NULL, value, length, binary, 0);
     testSQL(res);
     print();
-    cerr<<"exec() finish\n";
+    clog<<"exec() finish\n";
 }
 inline int sqlpg::testSQL(PGresult* a){
     if (!((PQresultStatus(a) == PGRES_COMMAND_OK)||(PQresultStatus(a) == PGRES_TUPLES_OK)))
@@ -234,42 +239,45 @@ inline int sqlpg::testSQL(PGresult* a){
     };
 }
 bool sqlpg::alias(vector<string> a){
-    cerr<<"alias start here\n";
-    string cmd;
-
-    cmd="select command,argc from alias where nom='"+a[0]+"' and argc = '"+toString(a.size()-1)+"';";// I need to select a function with argc too !
-    cerr<<"cmd="<<cmd<<endl;
-    exec(cmd);
+    clog<<"alias start here\n";
+    string nom=a[0], as=toString(a.size()-1).c_str();
+    int argc=2;
+    const char *value[2]={nom.c_str(), as.c_str()};
+    int length[2]={nom.size(), as.size()};
+    int binary[2]={0, 0};
+    res=PQexecPrepared(conn, "selectalias", argc, value, length, binary, 0);
+    testSQL(res);
     if (PQntuples(res)<1){
         cerr<<"alias end pqntuples(res)="<<PQntuples(res)<<endl;
         return 0;
     }
     string cmd2;
     cmd2=PQgetvalue(res, 0, 0);
-    cerr<<"cmd2="<<cmd2<<endl;
+    clog<<"cmd2="<<cmd2<<endl;
     int pos=0;
-    int argc=atoi(PQgetvalue(res, 0, 1));
-    cerr<<"argc="<<argc<<endl;
-    const char *value[argc];
-    int length[argc];
-    int binary[argc];
-    cerr<<"for argc="<<argc<<"\n";
+    argc=atoi(PQgetvalue(res, 0, 1));
+    clog<<"argc="<<argc<<endl;
+    res=PQprepare(conn, "execalias",cmd2.c_str(), argc, NULL);
+    //testSQL(res);
+
+    clog<<"for argc="<<argc<<"\n";
     for(int i=0; i<argc; i++){
         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";
     }
-    res=PQexecParams(conn, cmd2.c_str(), argc, NULL, value, length, binary, 0);
+    res=PQexecPrepared(conn, "execalias", argc, value, length, binary, 0);
     testSQL(res);
     print();
-    cerr<<"alias() end here\n";
+    testSQL(exec("DEALLOCATE execalias"));
+    clog<<"alias() end here\n";
     return 1;
 }
 void sqlpg::compulse(){
     string cmd="select * from raw where analyzed='0' order by date asc;";
     exec(cmd);
-    cerr<<"exec1("<<cmd<<") PQntuples(res)="<<PQntuples(res)<<endl;
+    clog<<"exec1("<<cmd<<") PQntuples(res)="<<PQntuples(res)<<endl;
     int tuples=PQntuples(res);
     for (int i=0; i<tuples;i++){
         string buf, txt=PQgetvalue(res, i, PQfnumber(res, "texte"));
@@ -294,7 +302,7 @@ void sqlpg::compulse(){
         };
         stringstream y;
         y<<"update raw set analyzed='1' where date='"<<(PQgetvalue(res, i, PQfnumber(res, "date")))<<"';";
-        cerr<<"y="<<y<<endl;
+        clog<<"y="<<y<<endl;
         testSQL(PQexec(conn, y.str().c_str()));
         //ex ec(y.str()); it don't works and i don't know why
     }
index 91c2c61db9b179242150bc9be5ae5b0cc049d79c..a29dfcf637484098ee03860b9763363b1ad7e0d9 100755 (executable)
@@ -1,34 +1,35 @@
---drop database if exists bob;
 --CREATE DATABASE bob OWNER bob;
 
-create table words (
+create table IF NOT EXISTS words (
 id serial primary key,
 time integer,
 word text
 );
 
-create table alias (
+create table IF NOT EXISTS alias(
 id serial primary key,
 cmd text,
 argc integer,
-nom text
+nom text,
+CONSTRAINT uc_aliasID UNIQUE (nom, argc)
 );
 
-create table raw (
+create table IF NOT EXISTS raw (
 id serial primary key,
 date timestamp,
 analyzed bit(1),
 texte text
 );
 
-create table memo (
+create table IF NOT EXISTS memo (
 id serial primary key,
 word text,
 rem text
 );
 
-create table var (
+create table IF NOT EXISTS var (
 id serial primary key,
 nom text,
-value text
+value text,
+CONSTRAINT uc_varID UNIQUE (nom)
 );
index 61801e0542471c9bd26fe1e3d2dc19b70a1b820a..7c70b7d4c99d91a1058a516de096003bca2406ad 100755 (executable)
@@ -1 +1 @@
-sudo -u bob ./diary-shell
+sudo -u bob ./diary-shell 2>>error 3>>log