02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * Copyright (C) 2007, 2016 Internet Systems Consortium, Inc. ("ISC")
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt/* $Id: sqlitedb.c,v 1.2 2011/10/11 00:09:02 each Exp $ */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * A simple database driver that interfaces to a SQLite database.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * The table must contain the fields "name", "rdtype", and "rdata", and
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * is expected to contain a properly constructed zone. The program "zonetodb"
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * creates such a table.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic dns_sdbimplementation_t *sqlitedb = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (sqlite3_open(dbi->filename, &dbi->db) == SQLITE_OK) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews /* a connection is returned even if the open fails */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_lookup_cb(void *p, int cc, char **cv, char **cn)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews /* FIXME - check these(num/names); I'm assuming a mapping for now */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews parm->result = dns_sdb_putrr(parm->lookup, type, ttl, data);
793814f80703afdd69b59ade91e63efa81ae4178Evan Huntsqlitedb_lookup(const char *zone, const char *name, void *dbdata,
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt dns_sdblookup_t *lookup, dns_clientinfomethods_t *methods,
793814f80703afdd69b59ade91e63efa81ae4178Evan Huntsqlitedb_lookup(const char *zone, const char *name, void *dbdata,
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#endif /* DNS_CLIENTINFO_VERSION */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * synchronous absolute name lookup
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews lookup_parm_t parm = { 0, lookup, ISC_R_SUCCESS };
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt#endif /* DNS_CLIENTINFO_VERSION */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews "SELECT TTL,RDTYPE,RDATA FROM \"%q\" WHERE "
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews "lower(NAME) = lower('%q')",
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_allnodes_cb(void *p, int cc, char **cv, char **cn)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews /* FIXME - check these(num/names); I'm assuming a mapping for now */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews parm->result = dns_sdb_putnamedrr(parm->allnodes, name, type, ttl, data);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews allnodes_parm_t parm = { 0, allnodes, ISC_R_SUCCESS };
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews "SELECT TTL,NAME,RDTYPE,RDATA FROM \"%q\" ORDER BY NAME",
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewssqlitedb_destroy(const char *zone, void *driverdata, void **dbdata)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews } while (0);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * Create a connection to the database and save any necessary information
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * in dbdata.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * argv[0] is the name of the database file
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * argv[1] is the name of the table
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi = isc_mem_get(ns_g_mctx, sizeof(dbinfo_t));
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlitedb_destroy(zone, driverdata, (void **)&dbi);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * Since the SQL database corresponds to a zone, the authority data should
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * be returned by the lookup() function. Therefore the authority() function
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * Wrapper around dns_sdb_register().
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (dns_sdb_register("sqlite", &sqlitedb_methods, NULL, flags,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * Wrapper around dns_sdb_unregister().