]>
git.ipfire.org Git - oddments/ddns.git/blob - src/ddns/database.py
2 ###############################################################################
4 # ddns - A dynamic DNS client for IPFire #
5 # Copyright (C) 2014 IPFire development team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
26 # Initialize the logger.
28 logger
= logging
.getLogger("ddns.database")
31 class DDNSDatabase(object):
32 def __init__(self
, core
, path
):
35 # Open the database file
36 self
._db
= self
._open
_database
(path
)
39 self
._close
_database
()
41 def _open_database(self
, path
):
42 logger
.debug("Opening database %s" % path
)
44 exists
= os
.path
.exists(path
)
46 conn
= sqlite3
.connect(path
, detect_types
=sqlite3
.PARSE_DECLTYPES|sqlite3
.PARSE_COLNAMES
)
47 conn
.isolation_level
= None
50 logger
.debug("Initialising database layout")
53 CREATE TABLE updates (
54 hostname TEXT NOT NULL,
57 timestamp timestamp NOT NULL
60 CREATE TABLE settings (
65 c
.execute("INSERT INTO settings(k, v) VALUES(?, ?)", ("version", "1"))
69 def _close_database(self
):
74 def _execute(self
, query
, *parameters
):
77 c
.execute(query
, parameters
)
81 def add_update(self
, hostname
, status
, message
=None):
82 self
._execute
("INSERT INTO updates(hostname, status, message, timestamp) \
83 VALUES(?, ?, ?, ?)", hostname
, status
, message
, datetime
.datetime
.utcnow())
85 def log_success(self
, hostname
):
86 logger
.debug("Logging successful update for %s" % hostname
)
88 return self
.add_update(hostname
, "success")
90 def log_failure(self
, hostname
, exception
):
92 message
= "%s: %s" % (exception
.__class
__.__name
__, exception
.reason
)
96 logger
.debug("Logging failed update for %s: %s" % (hostname
, message
or ""))
98 return self
.add_update(hostname
, "failure", message
=message
)
100 def last_update(self
, hostname
, status
="success"):
101 c
= self
._db
.cursor()
104 c
.execute("SELECT timestamp FROM updates WHERE hostname = ? AND status = ? \
105 ORDER BY timestamp DESC LIMIT 1", (hostname
, status
))