31
31
32
32
_whitespace = re .compile ('\s+' )
33
33
34
+
34
35
def squash_schema (sqls ):
35
36
l = []
36
37
for sql in sqls :
@@ -47,12 +48,13 @@ def sqlite_schema(conn):
47
48
48
49
class SQLiteDatabase (BaseDatabase ):
49
50
DDL = [
50
- """CREATE TABLE generation (inverter_serial INTEGER,
51
- timestamp INTEGER,
52
- total_yield INTEGER,
53
- PRIMARY KEY (inverter_serial, timestamp))""" ,
54
- """CREATE TABLE pvoutput (sid STRING,
55
- last_datetime_uploaded INTEGER)""" ,
51
+ """CREATE TABLE generation (inverter_serial INTEGER,
52
+ timestamp INTEGER,
53
+ total_yield INTEGER,
54
+ PRIMARY KEY (inverter_serial,
55
+ timestamp))""" ,
56
+ """CREATE TABLE pvoutput (sid STRING,
57
+ last_datetime_uploaded INTEGER)""" ,
56
58
]
57
59
58
60
def __init__ (self , filename ):
@@ -69,9 +71,9 @@ def commit(self):
69
71
70
72
def add_historic (self , serial , timestamp , total_yield ):
71
73
c = self .conn .cursor ()
72
- c .execute ("INSERT INTO generation"
73
- + " (inverter_serial, timestamp, total_yield)"
74
- + " VALUES (?, ?, ?);" ,
74
+ c .execute ("INSERT INTO generation" +
75
+ " (inverter_serial, timestamp, total_yield)" +
76
+ " VALUES (?, ?, ?);" ,
75
77
(serial , timestamp , total_yield ))
76
78
77
79
def get_one_historic (self , serial , timestamp ):
@@ -104,11 +106,12 @@ def get_aggregate_one_historic(self, ts, ids):
104
106
105
107
def get_aggregate_historic (self , from_ts , to_ts , ids ):
106
108
c = self .conn .cursor ()
107
- c .execute ("SELECT timestamp, sum(total_yield) FROM generation"
108
- " WHERE inverter_serial IN (" + "," .join ("?" * len (ids )) + ")"
109
- + " AND timestamp >= ? AND timestamp < ?"
110
- + " GROUP BY timestamp ORDER BY timestamp ASC" ,
111
- tuple (ids ) + (from_ts , to_ts ))
109
+ template = ("SELECT timestamp, sum(total_yield) FROM generation" +
110
+ " WHERE inverter_serial IN (" +
111
+ "," .join ("?" * len (ids )) +
112
+ ") AND timestamp >= ? AND timestamp < ?" +
113
+ " GROUP BY timestamp ORDER BY timestamp ASC" )
114
+ c .execute (template , tuple (ids ) + (from_ts , to_ts ))
112
115
return c .fetchall ()
113
116
114
117
# return midnights for each day in the database
@@ -117,11 +120,12 @@ def get_aggregate_historic(self, from_ts, to_ts, ids):
117
120
def midnights (self , inverters ):
118
121
c = self .conn .cursor ()
119
122
serials = ',' .join (x .serial for x in inverters )
120
- c .execute ("SELECT distinct(timestamp) "
121
- "FROM generation "
122
- "WHERE inverter_serial in ( ? ) "
123
- "AND timestamp % 86400 = 0 "
124
- "ORDER BY timestamp ASC" , (serials ,))
123
+ template = """SELECT distinct(timestamp)
124
+ FROM generation
125
+ WHERE inverter_serial in ( ? )
126
+ AND timestamp % 86400 = 0
127
+ ORDER BY timestamp ASC"""
128
+ c .execute (template , (serials ,))
125
129
r = c .fetchall ()
126
130
r = map (lambda x : datetime .datetime .utcfromtimestamp (x [0 ]), r )
127
131
return r
@@ -221,11 +225,11 @@ def create_from_empty(conn):
221
225
222
226
223
227
SCHEMA_NOPVO = squash_schema ((
224
- """CREATE TABLE generation (inverter_serial INTEGER,
225
- timestamp INTEGER,
226
- total_yield INTEGER,
227
- PRIMARY KEY (inverter_serial, timestamp))""" ,
228
- """CREATE TABLE schema (magic INTEGER, version INTEGER)""" ))
228
+ """CREATE TABLE generation (inverter_serial INTEGER,
229
+ timestamp INTEGER,
230
+ total_yield INTEGER,
231
+ PRIMARY KEY (inverter_serial, timestamp))""" ,
232
+ """CREATE TABLE schema (magic INTEGER, version INTEGER)""" ))
229
233
230
234
231
235
def update_nopvo (conn ):
@@ -236,13 +240,13 @@ def update_nopvo(conn):
236
240
237
241
238
242
SCHEMA_V0 = squash_schema ((
239
- """CREATE TABLE generation (inverter_serial INTEGER,
240
- timestamp INTEGER,
241
- total_yield INTEGER,
242
- PRIMARY KEY (inverter_serial, timestamp))""" ,
243
- """CREATE TABLE schema (magic INTEGER, version INTEGER)""" ,
244
- """CREATE TABLE pvoutput (sid STRING,
245
- last_datetime_uploaded INTEGER)""" ))
243
+ """CREATE TABLE generation (inverter_serial INTEGER,
244
+ timestamp INTEGER,
245
+ total_yield INTEGER,
246
+ PRIMARY KEY (inverter_serial, timestamp))""" ,
247
+ """CREATE TABLE schema (magic INTEGER, version INTEGER)""" ,
248
+ """CREATE TABLE pvoutput (sid STRING,
249
+ last_datetime_uploaded INTEGER)""" ))
246
250
247
251
248
252
def update_v0 (conn ):
@@ -258,6 +262,7 @@ def update_v0(conn):
258
262
SCHEMA_NOPVO : update_nopvo ,
259
263
}
260
264
265
+
261
266
def try_open (filename ):
262
267
try :
263
268
db = SQLiteDatabase (filename )
0 commit comments