-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashbuiltin_psql.c
64 lines (58 loc) · 2.22 KB
/
bashbuiltin_psql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/////////////////////////////////////////////////////////////////
/// Author: Christoph Gille ///
/// Licence: GNU ///
/// Bash-builtin Postgresql ///
/////////////////////////////////////////////////////////////////
#include <libpq-fe.h>
#define TYPE_DB_CON PGconn
#define NAME cg_psql
#include "bashbuiltin_databases.h"
#define DOC_DEPENDENCIES "libpq-dev"
#define DOC_STAND_ALONE_PROGRAM "psql"
#define DOC_DB_TEST "dbname=my_db"
#define DOC_OPTION_D "Example: 'dbname=my_db user=x password=xxx host=localhost port=5432'"
#define DOC_DB_NAME_OR_FILE "database connection string"
static void cg_db_connect(const struct struct_parameters *p, struct struct_variables *v){
v->connection=PQconnectdb(p->db);
char *s;
#define C(c) case c: s=#c;break;
switch(PQstatus(v->connection)){
case CONNECTION_OK: return;
C(CONNECTION_BAD);
C(CONNECTION_STARTED);
C(CONNECTION_MADE);
C(CONNECTION_AWAITING_RESPONSE);
C(CONNECTION_AUTH_OK);
C(CONNECTION_SSL_STARTUP);
C(CONNECTION_SETENV);
default: s="Unknown";
}
#undef C
PRINT_ERROR("PQconnectdb %s\n",s);
v->connection=NULL;
}
static void cg_process_sql(const struct struct_parameters *p, struct struct_variables *v){
FOR(j,0,p->SQLs_l){
PGresult *result=PQexec(v->connection,p->SQLs[j]);
if (!cg_starts_with_select(p,p->SQLs[j])) continue;
ExecStatusType resStatus=PQresultStatus(result);
if (resStatus!=PGRES_TUPLES_OK) {
PRINT_ERROR("Error while executing the query: %s\n", PQerrorMessage(v->connection));
}else{
const int rows=PQntuples(result), cols=PQnfields(result);
FOR(row,(p->is_header?-1:0),rows){
if (!cg_result_reset(p,v)) break;
FOR(col,0,cols){
const char *s=row<0?PQfname(result,col):PQgetvalue(result,row,col);
if (!cg_result_append_column(col,s,-1,p,v)){ v->res=EXECUTION_FAILURE;PQclear(result);return;}
}/*col*/
if (cg_result_apply(row,p,v)) break;
}/*row*/
}/*PGRES_TUPLES_OK*/
if (result) PQclear(result);
}/*SQLs*/
}
static void cg_db_connection_unload(TYPE_DB_CON *connection){
PQfinish(connection);
}
#include "bashbuiltin_databases.c"