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 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 distinct word from memo', 0);
insert into alias (nom, command, argc) values ('memo', 'select rem from memo where word=$1', 1);
#include <sys/types.h>
#include <stdlib.h>
#include <cstring>
+
using namespace std;
sqlpg::sqlpg() {
//conn = PQconnectStart(var.c_str());
conn = PQconnectdb(connect.c_str());
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;
+ string cmd="select command,argc,inf from alias where ((nom = $1) and (argc = $2)) OR ((nom = $1) and (inf='1') and (argc < $2)) OR ((argc=$2) and (nom=$1) and (inf<>'1'));";
+//
+ clog<<"SQL statement="<<cmd<<endl;
res=PQprepare(conn, "selectalias", cmd.c_str(), 2, NULL);
testSQL(res);
else if (day==2)sprintf(date,"%d-%d-%d", 1900+ltm->tm_year, 1+ltm->tm_mon, ltm->tm_mday);
else {
cout<<"error in date";
- date="0000-00-00 00;00;00";
+ date="0000-00-00 00:00:00";
}
string d(date);
}
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, inf;
+ string nom=a[0], as=toString(a.size()-1).c_str(), i;
+ 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){
+ if (PQntuples(res)!=1){
cerr<<"alias end pqntuples(res)="<<PQntuples(res)<<endl;
return 0;
}
string cmd2;
cmd2=PQgetvalue(res, 0, 0);
- inf=PQgetvalue(res, 0, 2);
- clog<<"cmd2="<<cmd2<<" inf="<<inf<<endl;
+ i=PQgetvalue(res, 0, 2);
+
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];
+ //clog<<"bcl qui tue, i="<<i<<"\n";
+ if(i=="1"){
+ string b=a[argc];
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<<"pos="<<pos<<" cmd2="<<cmd2<<" asize="<<asize<<" argc="<<argc<<"\n";
+ b=a[argc+1];
+ for(int j=argc+2;j<asize;j++){
+ b=b+" "+a[j];
}
- clog<<"b="<<b;
cmd2.replace(pos, 2, b, 0, string::npos);
+ clog<<"bcl qui tue, cmd2="<<cmd2<<" b="<<b<<"\n";
+
}
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";
+ clog<<"i="<<i<<" value="<<value[i]<<" length="<<length[i]<<"\n";
}
+ clog<<"argc="<<argc<<" cmd2="<<cmd2<<" i="<<i<<endl;
+ res=PQprepare(conn, "execalias",cmd2.c_str(), argc, NULL);
+ testSQL(res);
res=PQexecPrepared(conn, "execalias", argc, value, length, binary, 0);
testSQL(res);
print();