79 lines
1.8 KiB
C
79 lines
1.8 KiB
C
/* $Id$ */
|
||
/*
|
||
* Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv>
|
||
*
|
||
* Permission to use, copy, modify, and distribute this software for any
|
||
* purpose with or without fee is hereby granted, provided that the above
|
||
* copyright notice and this permission notice appear in all copies.
|
||
*
|
||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||
*/
|
||
#include <stdarg.h>
|
||
#include <stdint.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include <time.h>
|
||
#include <unistd.h>
|
||
|
||
#include <kcgi.h>
|
||
#include <kcgijson.h>
|
||
#include <ksql.h>
|
||
|
||
#include "extern.h"
|
||
|
||
#define STMT_PAGE " LIMIT ? OFFSET ?"
|
||
|
||
char *
|
||
strornull(struct ksqlstmt *stmt, int col)
|
||
{
|
||
if (ksql_stmt_isnull(stmt, col))
|
||
return(NULL);
|
||
return(kstrdup(ksql_stmt_str(stmt, col)));
|
||
}
|
||
|
||
/*
|
||
* Open the database and stash the resulting handle in the d
|
||
*/
|
||
int
|
||
db_open(struct kreq *r, const char *file)
|
||
{
|
||
struct ksqlcfg cfg;
|
||
struct ksql *sql;
|
||
|
||
/* Configure normal database except with foreign keys. */
|
||
|
||
memset(&cfg, 0, sizeof(struct ksqlcfg));
|
||
cfg.flags = KSQL_EXIT_ON_ERR |
|
||
KSQL_FOREIGN_KEYS |
|
||
KSQL_SAFE_EXIT;
|
||
cfg.err = ksqlitemsg;
|
||
cfg.dberr = ksqlitedbmsg;
|
||
|
||
/* Allocate database. */
|
||
|
||
if (NULL == (sql = ksql_alloc(&cfg)))
|
||
return(0);
|
||
|
||
ksql_open(sql, file);
|
||
r->arg = sql;
|
||
return(1);
|
||
}
|
||
|
||
/*
|
||
* Close the database stashed in the kreq's argument.
|
||
*/
|
||
void
|
||
db_close(struct kreq *r)
|
||
{
|
||
|
||
ksql_free(r->arg);
|
||
r->arg = NULL;
|
||
}
|