Browse Source

ckdb - ensure a DB connection is always initiated before use

master
kanoi 10 years ago
parent
commit
1619b9b159
  1. 47
      src/ckdb.c

47
src/ckdb.c

@ -2024,6 +2024,7 @@ static bool users_add(PGconn *conn, char *username, char *emailaddress, char *pa
tv_t *now, char *by, char *code, char *inet) tv_t *now, char *by, char *code, char *inet)
{ {
ExecStatusType rescode; ExecStatusType rescode;
bool conned = false;
PGresult *res; PGresult *res;
K_ITEM *item; K_ITEM *item;
int n; int n;
@ -2080,6 +2081,11 @@ static bool users_add(PGconn *conn, char *username, char *emailaddress, char *pa
"secondaryuserid" "secondaryuserid"
HISTORYDATECONTROL ") values (" PQPARAM11 ")"; HISTORYDATECONTROL ") values (" PQPARAM11 ")";
if (!conn) {
conn = dbconnect();
conned = true;
}
res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0); res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0);
rescode = PQresultStatus(res); rescode = PQresultStatus(res);
if (!PGOK(rescode)) { if (!PGOK(rescode)) {
@ -2090,6 +2096,8 @@ static bool users_add(PGconn *conn, char *username, char *emailaddress, char *pa
ok = true; ok = true;
unparam: unparam:
PQclear(res); PQclear(res);
if (conned)
PQfinish(conn);
for (n = 0; n < par; n++) for (n = 0; n < par; n++)
free(params[n]); free(params[n]);
unitem: unitem:
@ -2253,6 +2261,7 @@ static K_ITEM *workers_add(PGconn *conn, int64_t userid, char *workername,
char *code, char *inet, tv_t *cd) char *code, char *inet, tv_t *cd)
{ {
ExecStatusType rescode; ExecStatusType rescode;
bool conned = false;
PGresult *res; PGresult *res;
K_ITEM *item, *ret = NULL; K_ITEM *item, *ret = NULL;
int n; int n;
@ -2271,6 +2280,11 @@ static K_ITEM *workers_add(PGconn *conn, int64_t userid, char *workername,
row = DATA_WORKERS(item); row = DATA_WORKERS(item);
if (conn == NULL) {
conn = dbconnect();
conned = true;
}
row->workerid = nextid(conn, "workerid", (int64_t)1, cd, by, code, inet); row->workerid = nextid(conn, "workerid", (int64_t)1, cd, by, code, inet);
if (row->workerid == 0) if (row->workerid == 0)
goto unitem; goto unitem;
@ -2337,6 +2351,8 @@ unparam:
for (n = 0; n < par; n++) for (n = 0; n < par; n++)
free(params[n]); free(params[n]);
unitem: unitem:
if (conned)
PQfinish(conn);
K_WLOCK(workers_free); K_WLOCK(workers_free);
if (!ret) if (!ret)
k_add_head(workers_free, item); k_add_head(workers_free, item);
@ -2354,6 +2370,7 @@ static bool workers_update(PGconn *conn, K_ITEM *item, char *difficultydefault,
char *by, char *code, char *inet, tv_t *cd) char *by, char *code, char *inet, tv_t *cd)
{ {
ExecStatusType rescode; ExecStatusType rescode;
bool conned = false;
PGresult *res; PGresult *res;
int n; int n;
WORKERS *row; WORKERS *row;
@ -2410,6 +2427,11 @@ static bool workers_update(PGconn *conn, K_ITEM *item, char *difficultydefault,
params[par++] = tv_to_buf((tv_t *)&default_expiry, NULL, 0); params[par++] = tv_to_buf((tv_t *)&default_expiry, NULL, 0);
PARCHKVAL(par, 3, params); PARCHKVAL(par, 3, params);
if (conn == NULL) {
conn = dbconnect();
conned = true;
}
res = PQexec(conn, "Begin"); res = PQexec(conn, "Begin");
rescode = PQresultStatus(res); rescode = PQresultStatus(res);
if (!PGOK(rescode)) { if (!PGOK(rescode)) {
@ -2465,6 +2487,8 @@ static bool workers_update(PGconn *conn, K_ITEM *item, char *difficultydefault,
ok = true; ok = true;
unparam: unparam:
PQclear(res); PQclear(res);
if (conned)
PQfinish(conn);
for (n = 0; n < par; n++) for (n = 0; n < par; n++)
free(params[n]); free(params[n]);
early: early:
@ -2941,7 +2965,6 @@ unparam:
PQclear(res); PQclear(res);
if (conned) if (conned)
PQfinish(conn); PQfinish(conn);
for (n = 0; n < par; n++) for (n = 0; n < par; n++)
free(params[n]); free(params[n]);
@ -4061,6 +4084,7 @@ static bool blocks_add(PGconn *conn, char *height, char *blockhash,
bool igndup, char *id) bool igndup, char *id)
{ {
ExecStatusType rescode; ExecStatusType rescode;
bool conned = false;
PGresult *res = NULL; PGresult *res = NULL;
K_TREE_CTX ctx[1]; K_TREE_CTX ctx[1];
K_ITEM *item, *u_item; K_ITEM *item, *u_item;
@ -4130,6 +4154,11 @@ static bool blocks_add(PGconn *conn, char *height, char *blockhash,
"clientid,enonce1,nonce2,nonce,reward,confirmed" "clientid,enonce1,nonce2,nonce,reward,confirmed"
HISTORYDATECONTROL ") values (" PQPARAM16 ")"; HISTORYDATECONTROL ") values (" PQPARAM16 ")";
if (conn == NULL) {
conn = dbconnect();
conned = true;
}
res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0); res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0);
rescode = PQresultStatus(res); rescode = PQresultStatus(res);
if (!PGOK(rescode)) { if (!PGOK(rescode)) {
@ -4146,6 +4175,11 @@ static bool blocks_add(PGconn *conn, char *height, char *blockhash,
params[par++] = tv_to_buf((tv_t *)&default_expiry, NULL, 0); params[par++] = tv_to_buf((tv_t *)&default_expiry, NULL, 0);
PARCHKVAL(par, 3, params); PARCHKVAL(par, 3, params);
if (conn == NULL) {
conn = dbconnect();
conned = true;
}
res = PQexec(conn, "Begin"); res = PQexec(conn, "Begin");
rescode = PQresultStatus(res); rescode = PQresultStatus(res);
if (!PGOK(rescode)) { if (!PGOK(rescode)) {
@ -4205,6 +4239,9 @@ unparam:
for (n = 0; n < par; n++) for (n = 0; n < par; n++)
free(params[n]); free(params[n]);
flail: flail:
if (conned)
PQfinish(conn);
K_WLOCK(blocks_free); K_WLOCK(blocks_free);
if (!ok) if (!ok)
k_add_head(blocks_free, item); k_add_head(blocks_free, item);
@ -4944,6 +4981,7 @@ static cmp_t cmp_userstats_workerstatus(K_ITEM *a, K_ITEM *b)
static bool userstats_add_db(PGconn *conn, USERSTATS *row) static bool userstats_add_db(PGconn *conn, USERSTATS *row)
{ {
ExecStatusType rescode; ExecStatusType rescode;
bool conned = false;
PGresult *res; PGresult *res;
char *ins; char *ins;
bool ok = false; bool ok = false;
@ -4972,6 +5010,11 @@ static bool userstats_add_db(PGconn *conn, USERSTATS *row)
"hashrate24hr,summarylevel,summarycount,statsdate" "hashrate24hr,summarylevel,summarycount,statsdate"
SIMPLEDATECONTROL ") values (" PQPARAM14 ")"; SIMPLEDATECONTROL ") values (" PQPARAM14 ")";
if (conn == NULL) {
conn = dbconnect();
conned = true;
}
res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0); res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0);
rescode = PQresultStatus(res); rescode = PQresultStatus(res);
if (!PGOK(rescode)) { if (!PGOK(rescode)) {
@ -4982,6 +5025,8 @@ static bool userstats_add_db(PGconn *conn, USERSTATS *row)
ok = true; ok = true;
unparam: unparam:
PQclear(res); PQclear(res);
if (conned)
PQfinish(conn);
for (n = 0; n < par; n++) for (n = 0; n < par; n++)
free(params[n]); free(params[n]);

Loading…
Cancel
Save