Coverage for src/lib/python/isc/datasrc/sqlite3_ds : 65%
        
        
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
| 
 # Copyright (C) 2010 Internet Systems Consortium. # # 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 INTERNET SYSTEMS CONSORTIUM # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL # INTERNET SYSTEMS CONSORTIUM 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. 
 
 
 #define the index of different part of one record 
 # Current major and minor versions of schema 
 """ Define exceptions.""" 
 """ Set up schema for a newly created zones/records database. 
 Arguments: cur - sqlite3 cursor. """ # We are creating the database because it apparently had not been at # the time we tried to read from it. However, another process may have # had the same idea, resulting in a potential race condition. # Therefore, we obtain an exclusive lock before we create anything # When we have it, we check *again* whether the database has been # initialized. If not, we do so. 
 # If the database is perpetually locked, it'll time out automatically # and we just let it fail. row = cur.fetchone() minor INTEGER NOT NULL DEFAULT 0)""") str(SCHEMA_MAJOR_VERSION) + ", " + str(SCHEMA_MINOR_VERSION) + ")") name TEXT NOT NULL COLLATE NOCASE, rdclass TEXT NOT NULL COLLATE NOCASE DEFAULT 'IN', dnssec BOOLEAN NOT NULL DEFAULT 0)""") zone_id INTEGER NOT NULL, name TEXT NOT NULL COLLATE NOCASE, rname TEXT NOT NULL COLLATE NOCASE, ttl INTEGER NOT NULL, rdtype TEXT NOT NULL COLLATE NOCASE, sigtype TEXT COLLATE NOCASE, rdata TEXT NOT NULL)""") (rdtype, rname)""") zone_id INTEGER NOT NULL, hash TEXT NOT NULL COLLATE NOCASE, owner TEXT NOT NULL COLLATE NOCASE, ttl INTEGER NOT NULL, rdtype TEXT NOT NULL COLLATE NOCASE, rdata TEXT NOT NULL)""") zone_id INTEGER NOT NULL, version INTEGER NOT NULL, operation INTEGER NOT NULL, name TEXT NOT NULL COLLATE NOCASE, rrtype TEXT NOT NULL COLLATE NOCASE, ttl INTEGER NOT NULL, rdata TEXT NOT NULL)""") 
 """ Open a database, if the database is not yet set up, call create to do so. It may raise Sqlite3DSError if failed to open sqlite3 database file or find bad database schema version in the database. 
 Arguments: dbfile - the filename for the sqlite3 database. connect_timeout - timeout for opening the database or acquiring locks defaults to sqlite3 module's default of 5.0 seconds 
 Return sqlite3 connection, sqlite3 cursor. """ except Exception as e: fail = "Failed to open " + dbfile + ": " + e.args[0] raise Sqlite3DSError(fail) 
 # Does the database exist yet? If not, create it. # temporarily disable automatic transactions so # we can do our own 
 
 
 
 """ A generator function producing an iterable set of the records in the zone with the given zone name. 
 Arguments: zonename - the zone's origin name. dbfile - the filename for the sqlite3 database. """ conn, cur = open(dbfile) zone_id = get_zoneid(zonename, cur) 
 cur.execute("SELECT * FROM records WHERE zone_id = ?", [zone_id]) record = cur.fetchone() while record: yield record record = cur.fetchone() 
 cur.close() conn.close() 
 
 """Return the soa record of the zone with the given zone name. If the zone doesn't exist, return None. 
 Arguments: zonename - the zone's origin name. dbfile - the filename for the sqlite3 database. """ 
 
 
 """Return the rrset of the zone with the given zone name, rrset name and given RR type. If the zone doesn't exist or RR type doesn't exist, return an empty list. 
 Arguments: zonename - the zone's origin name. rr_name - rr name. rdtype - RR type. dbfile - the filename for the sqlite3 database. """ conn, cur = open(dbfile) id = get_zoneid(zonename, cur) cur.execute("SELECT * FROM records WHERE name = ? and zone_id = ? and rdtype = ?", [rr_name, id, rdtype]) datas = cur.fetchall() cur.close() conn.close() return datas 
 
 """ Return all the zones' information in the database. 
 Arguments: dbfile - the filename for the sqlite3 database. """ 
 
 
 """ Get the zone_id for a given zone name. 
 Arguments: zonename - the zone's origin name. cur - sqlite3 cursor. 
 Return zone id for the given zone name, or an empty string if the zone is not found. """ return row[0] else: 
 
 """ Search for the zone with the given zone name in databse. This method may throw a Sqlite3DSError exception because its underlying method open() can throw that exception. 
 Arguments: zonename - the zone's origin name. dbfile - the filename for the sqlite3 database. 
 Return True if the zone is found, otherwise False. """ conn, cur = open(dbfile) zoneid = get_zoneid(zonename, cur) cur.close() conn.close() if zoneid: return True return False 
 
 """Reverse the labels of a domain name; for example, given 'www.example.org.', return 'org.example.www.' This is needed for DNSSEC sort order. 
 Arguments: name - the DNS name will be reversed. """ new = name.split('.') new.reverse() if new[0] == '': new.pop(0) return '.'.join(new)+'.' 
 
 """ Load a zone into the SQL database. 
 Arguments: dbfile - the sqlite3 database filename zone - the zone origin reader - a generator function producing an iterable set of name/ttl/class/rrtype/rdata-text tuples. """ # if the zone name doesn't contain the trailing dot, automatically add it. zone += '.' 
 
 
 sigtype = '' if rdtype.lower() == 'rrsig': sigtype = rdata.split()[0] 
 if rdtype.lower() == 'nsec3' or sigtype.lower() == 'nsec3': hash = name.split('.')[0] cur.execute("""INSERT INTO nsec3 (zone_id, hash, owner, ttl, rdtype, rdata) VALUES (?, ?, ?, ?, ?, ?)""", [new_zone_id, hash, name, ttl, rdtype, rdata]) elif rdtype.lower() == 'rrsig': cur.execute("""INSERT INTO records (zone_id, name, rname, ttl, rdtype, sigtype, rdata) VALUES (?, ?, ?, ?, ?, ?, ?)""", [new_zone_id, name, reverse_name(name), ttl, rdtype, sigtype, rdata]) else: cur.execute("""INSERT INTO records (zone_id, name, rname, ttl, rdtype, rdata) VALUES (?, ?, ?, ?, ?, ?)""", [new_zone_id, name, reverse_name(name), ttl, rdtype, rdata]) 
 cur.execute("DELETE FROM zones WHERE id=?", [old_zone_id]) cur.execute("UPDATE zones SET name=? WHERE id=?", [zone, new_zone_id]) conn.commit() cur.execute("DELETE FROM records WHERE zone_id=?", [old_zone_id]) cur.execute("DELETE FROM nsec3 WHERE zone_id=?", [old_zone_id]) conn.commit() else: except Exception as e: fail = "Error while loading " + zone + ": " + e.args[0] raise Sqlite3DSError(fail) finally:  |