-insert into alias (nom, cmd, argc, help) values ('tables', 'select tablename from pg_tables where tableowner=current_user', 0, 'list table (every table...)');
+insert into alias (nom, cmd, argc, help) values ('tables', 'select tablename from pg_tables where tableowner=current_user;', 0, 'list table (every table...)');
INSERT INTO alias (nom, cmd, argc, help) VALUES ('ls', 'select id,texte from raw order by date asc;', 0, 'list id,text of every records');
insert into alias (nom, cmd, argc, help) values ('ls', 'select * from raw where id=$1 order by date asc;', 1, 'print id,date,text of one record');
insert into alias (nom, cmd, argc, help) values ('ls', 'select * from raw where date between $1 and $2 order by date asc;', 2, 'liste record with date between $1 and $2');
INSERT INTO alias (nom, cmd, argc, help) VALUES ('search', 'select id,texte from raw where texte ~ $1 order by date asc;', 1, 'search record who contains your arg (only one arg)');
INSERT INTO alias (nom, cmd, argc, help) VALUES ('rm', 'DELETE FROM raw WHERE id =$1;', 1, 'delete record by ID');
INSERT INTO alias (nom, cmd, argc, help) VALUES ('rmm', 'DELETE FROM memo WHERE id =$1;', 1, 'delete record by ID');
-insert into alias (nom, cmd, argc, help) values ('var', 'select nom from var', 0, 'print avery var');
-insert into alias (nom, cmd, argc, help) values ('var', 'select value from var where nom=$1', 1, 'print named var content');
-insert into alias (nom, cmd, argc, inf, help) values ('var', 'insert into var (nom, value) values ($1, $2)', 2, '1', 'add new var and it''s content');
+insert into alias (nom, cmd, argc, help) values ('var', 'select nom from var;', 0, 'print avery var');
+insert into alias (nom, cmd, argc, help) values ('var', 'select value from var where nom=$1;', 1, 'print named var content');
+insert into alias (nom, cmd, argc, help) values ('var', 'insert into var (nom, value) values ($1, $2);', 2, 'add new var and it''s content');
+insert into alias (nom, cmd, argc, help) values ('uvar', 'update var set value=$2 where nom=$1;', 2, 'update var''s content');
insert into alias (nom, cmd, argc, inf, help) values ('memo', 'insert into memo (word, rem) values ($1, $2);', 2, '1', 'insert new memo');
-insert into alias (nom, cmd, argc, help) values ('memo', 'select distinct word from memo', 0, 'list all word with memo attached');
-insert into alias (nom, cmd, argc, help) values ('memo', 'select id,rem from memo where word=$1 order by id', 1, 'print memo content');
-insert into alias (nom, cmd, argc, help) values ('help', 'select distinct nom from alias', 0, 'help itself (add "cmd" to get specific help)');
-insert into alias (nom, cmd, argc, help) values ('help', 'select argc from alias where nom=$1 order by argc', 1, 'print argc of named alias by ID');
-insert into alias (nom, cmd, argc, help) values ('help', 'select help from alias where nom=$1 and argc=$2', 2, 'print help for only one alias (as "nom" and "argc" is a key)');
-insert into alias (nom, cmd, argc, help) values ('print', 'select cmd,inf from alias where nom=$1 and argc=$2', 2, 'print SQL command of an alias (...)');
+insert into alias (nom, cmd, argc, help) values ('memo', 'select distinct word from memo;', 0, 'list all word with memo attached');
+insert into alias (nom, cmd, argc, help) values ('memo', 'select id,rem from memo where word=$1 order by id;', 1, 'print memo content');
+insert into alias (nom, cmd, argc, help) values ('help', 'select distinct nom from alias;', 0, 'help itself (add "cmd" to get specific help)');
+insert into alias (nom, cmd, argc, help) values ('help', 'select argc from alias where nom=$1 order by argc;', 1, 'print argc of named alias by ID');
+insert into alias (nom, cmd, argc, help) values ('help', 'select help from alias where nom=$1 and argc=$2;', 2, 'print help for only one alias (as "nom" and "argc" is a key)');
+insert into alias (nom, cmd, argc, help) values ('print', 'select cmd,inf from alias where nom=$1 and argc=$2;', 2, 'print SQL command of an alias (...)');
insert into alias (nom, cmd, argc, inf, help) values ('default', 'INSERT INTO raw (date, texte) VALUES ($1::timestamp, $2::text);', 2 , '1', 'default command');
insert into alias (nom, cmd, argc, inf, help) values ('aliasadd', 'insert into alias (nom, argc, cmd) values ( $1, $2, $3 );', 3, '1', 'add alias (with form : aliasadd "name" "argc" "SQL")');
+insert into alias (nom, cmd, argc, help) values ('getpass', 'select username,password from credential where service=$1;', 1, 'command to retrieve username and password with a service name (perhaps more than one)');
+insert into alias (nom, cmd, argc, help) values ('getpass', 'select distinct service from credential;', 0, 'get a list of service you have credential for');
+insert into alias (nom, cmd, argc, help) values ('getpass', 'select password from credential where service=$1 and username=$2;', 2, 'retrieve one saved password (as service and user has unique constraint)');
sqlpg::sqlpg() {
//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";
+ strconnect = "dbname=bob user=bob password=pass connect_timeout=10";
//conn = PQconnectStart(var.c_str());
- conn = PQconnectdb(connect.c_str());
+ conn = PQconnectdb(strconnect.c_str());
clog<<"prepare SQL statement\n";
string cmd="select cmd,argc,inf from alias where ((nom = $1) and (argc = $2)) OR ((nom = $1) and (inf<>0) and (argc < $2));";
- clog<<"SQL statement="<<cmd<<endl;
+ clog<<"SQL selectalias="<<cmd<<endl;
res=PQprepare(conn, "selectalias", cmd.c_str(), 2, NULL);
testSQL(res);
}
int sqlpg::hmRecord(){
string command;
command="select date from raw;";
- //res=PQexec(conn, command.c_str());
exec(command);
- a=PQntuples(res);
- //clog<<"hmRecord()="<<a<<endl;
+ a=PQntuples(res2);
+ clog<<"hmRecord()->a="<<a<<endl;
return a;
}
void sqlpg::printvar(){
- cout<<"date="<<date<<", chaîne de connection: " + connect << endl;
+ cout<<"date="<<date<<", chaîne de connection: " + strconnect << endl;
for(map<string, string>::iterator i=value.begin(); i!=value.end(); ++i){
cout<<"value["<<i->first<<"]="<<i->second<<endl;
}
}
void sqlpg::exec(string a){
clog<<"exec(string["<<a<<"])\n";
- res=PQexecParams(conn, a.c_str(), 0, NULL, 0, NULL, NULL, 0);
- if (!((PQresultStatus(res) == PGRES_COMMAND_OK)||(PQresultStatus(res) == PGRES_TUPLES_OK)))
+ 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(res)="<<PQresStatus(PQresultStatus(res))<<endl;
+ cout<<"PQresStatus(res2)="<<PQresStatus(PQresultStatus(res2))<<endl;
};
}
void sqlpg::exec(vector<string> a){
bool sqlpg::alias(vector<string> a){
clog<<"alias start here\n";
string nom=a[0], as=toString(a.size()-1), i;
- int argca=2;
+ int argca=2;//it won't work if I use argca in place of 2 :-(
bool inf=0;
const char *valuea[2]={nom.c_str(), as.c_str()};
int lengtha[2]={nom.size(), as.size()};
res=PQexecPrepared(conn, "execalias", argc, value, length, binary, 0);
clog<<"PQexecPrepared done argc="<<argc<<endl;
testSQL(res);
- print();
+ print(); //I prefer to type 'p' (otherwise, I have to add a column in database)
exec("DEALLOCATE execalias");
clog<<"alias() end here\n";
return 1;
};
void sqlpg::getvar(){
clog<<"getvar\n";
+ value.clear();
exec("select nom,value from var;");
- int l=PQntuples(res);
+ int l=PQntuples(res2);
for (int i=0; i<l; i++){
string a, b;
- a=PQgetvalue(res, i, 0);
- b=PQgetvalue(res, i, 1);
+ a=PQgetvalue(res2, i, 0);
+ b=PQgetvalue(res2, i, 1);
value.insert(pair<string, string> (a, b) );
};
clog<<"value.size="<<value.size()<<endl;
};
+void sqlpg::connect(string service, string name){
+ clog<<"connect start here\n";
+ string nom="connect", as=toString(2), i;
+ int argca=2;//it won't work if I use argca in place of 2 :-(
+ bool inf=0;
+ const char *valuea[2]={nom.c_str(), as.c_str()};
+ int lengtha[2]={nom.size(), as.size()};
+ int binarya[2]={0, 0};
+ res=PQexecPrepared(conn, "selectalias", argca, valuea, lengtha, binarya, 0);
+ testSQL(res);
+ if (PQntuples(res)!=1){
+ cerr<<"alias end pqntuples(res)="<<PQntuples(res)<<endl;
+ return 0;
+ }
+ string cmd2;
+ cmd2=PQgetvalue(res, 0, 0);
+ clog<<"alias()->commande : "<<cmd2<<endl;
+//res=PQprepare(conn, "pass", )
+
+}
#endif