/* $Id: drydb.c,v 1.17 2009/05/07 21:22:52 imil Exp $ */ /* * Copyright (c) 2009 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Emile "iMil" Heitor . * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include #include "pkg_dry.h" static sqlite3 *ddb; static char *ddberr = NULL; static int ddbres = 0; static FILE *fp; static const char *pragmaopts[] = { "cache_size = 1000000", "locking_mode = EXCLUSIVE", "empty_result_callbacks = 1", NULL }; /* "synchronous = OFF", */ static void ddb_err(const char *errmsg) { warn("%s: %s", errmsg, sqlite3_errmsg(ddb)); sqlite3_close(ddb); exit(EXIT_FAILURE); } /* * unused: optional parameter given as 4th argument of sqlite3_exec * argc : row number * argv : row * col : column * * col[0] col[1] col[argc] * ______________________________________ * | argv[0] | argv[1] | ... | argv[argc] | * * WARNING: callbacl is called on every line */ static int drydb_simple_callback(void *param, int argc, char **argv, char **colname) { ddbres = argc; return DDB_OK; } /* sqlite callback, record a single value */ int ddb_get_value(void *param, int argc, char **argv, char **colname) { char *value = (char *)param; if (argv != NULL) { XSTRCPY(value, argv[0]); return DDB_OK; } return DDB_ERR; } int drydb_doquery(const char *query, int (*drydb_callback)(void *, int, char **, char **), void *param) { if (sqlite3_exec(ddb, query, drydb_callback, param, &ddberr) != SQLITE_OK) { fprintf(fp, "SQL error: %s in query %s\n", ddberr, query); sqlite3_free(ddberr); return DDB_ERR; } return DDB_OK; } void drydb_close() { sqlite3_close(ddb); fclose(fp); } void drydb_init() { int i; char buf[BUFSIZ]; if ((fp = fopen(PKG_DRY_LOG, "w")) == NULL) errx(EXIT_FAILURE, "Can't access logfile %s", PKG_DRY_LOG); if (sqlite3_open(DDB, &ddb) != SQLITE_OK) ddb_err("Can't open database"); /* generic query in order to check tables existence */ if (drydb_doquery("select * from sqlite_master;", drydb_simple_callback, NULL) != DDB_OK) ddb_err("Can't access database: %s"); /* apply PRAGMA properties */ for (i = 0; pragmaopts[i] != NULL; i++) { snprintf(buf, BUFSIZ, "PRAGMA %s;", pragmaopts[i]); drydb_doquery(buf, NULL, NULL); } }