no need of global vars, prefer self.var

This commit is contained in:
Yann Leboulanger 2005-12-05 17:22:50 +00:00
parent faa46882ec
commit d3a6163729
1 changed files with 26 additions and 28 deletions

View File

@ -83,18 +83,17 @@ class Logger:
# db is not created here but in src/common/checks_paths.py # db is not created here but in src/common/checks_paths.py
return return
global con, cur
# if locked, wait up to 20 sec to unlock # if locked, wait up to 20 sec to unlock
# before raise (hopefully should be enough) # before raise (hopefully should be enough)
con = sqlite.connect(LOG_DB_PATH, timeout = 20.0, self.con = sqlite.connect(LOG_DB_PATH, timeout = 20.0,
isolation_level = 'IMMEDIATE') isolation_level = 'IMMEDIATE')
cur = con.cursor() self.cur = self.con.cursor()
self.get_jids_already_in_db() self.get_jids_already_in_db()
def get_jids_already_in_db(self): def get_jids_already_in_db(self):
cur.execute('SELECT jid FROM jids') self.cur.execute('SELECT jid FROM jids')
rows = cur.fetchall() # list of tupples: (u'aaa@bbb',), (u'cc@dd',)] rows = self.cur.fetchall() # list of tupples: (u'aaa@bbb',), (u'cc@dd',)]
for row in rows: for row in rows:
# row[0] is first item of row (the only result here, the jid) # row[0] is first item of row (the only result here, the jid)
self.jids_already_in.append(row[0]) self.jids_already_in.append(row[0])
@ -110,9 +109,9 @@ class Logger:
possible_room_jid, possible_nick = jid.split('/', 1) possible_room_jid, possible_nick = jid.split('/', 1)
cur.execute('SELECT jid_id FROM jids WHERE jid="%s" AND type=%d' %\ self.cur.execute('SELECT jid_id FROM jids WHERE jid="%s" AND type=%d' %\
(possible_room_jid, constants.JID_ROOM_TYPE)) (possible_room_jid, constants.JID_ROOM_TYPE))
row = cur.fetchone() row = self.cur.fetchone()
if row is not None: if row is not None:
return True return True
else: else:
@ -129,19 +128,19 @@ class Logger:
if not jid_is_from_pm: # it's normal jid with resource if not jid_is_from_pm: # it's normal jid with resource
jid = jid.split('/', 1)[0] # remove the resource jid = jid.split('/', 1)[0] # remove the resource
if jid in self.jids_already_in: # we already have jids in DB if jid in self.jids_already_in: # we already have jids in DB
cur.execute('SELECT jid_id FROM jids WHERE jid="%s"' % jid) self.cur.execute('SELECT jid_id FROM jids WHERE jid="%s"' % jid)
jid_id = cur.fetchone()[0] jid_id = self.cur.fetchone()[0]
else: # oh! a new jid :), we add it now else: # oh! a new jid :), we add it now
if typestr == 'ROOM': if typestr == 'ROOM':
typ = constants.JID_ROOM_TYPE typ = constants.JID_ROOM_TYPE
else: else:
typ = constants.JID_NORMAL_TYPE typ = constants.JID_NORMAL_TYPE
cur.execute('INSERT INTO jids (jid, type) VALUES (?, ?)', (jid, typ)) self.cur.execute('INSERT INTO jids (jid, type) VALUES (?, ?)', (jid, typ))
try: try:
con.commit() self.con.commit()
except sqlite.OperationalError, e: except sqlite.OperationalError, e:
print >> sys.stderr, str(e) print >> sys.stderr, str(e)
jid_id = cur.lastrowid jid_id = self.cur.lastrowid
self.jids_already_in.append(jid) self.jids_already_in.append(jid)
return jid_id return jid_id
@ -185,9 +184,9 @@ class Logger:
def commit_to_db(self, values): def commit_to_db(self, values):
#print 'saving', values #print 'saving', values
sql = 'INSERT INTO logs (jid_id, contact_name, time, kind, show, message, subject) VALUES (?, ?, ?, ?, ?, ?, ?)' sql = 'INSERT INTO logs (jid_id, contact_name, time, kind, show, message, subject) VALUES (?, ?, ?, ?, ?, ?, ?)'
cur.execute(sql, values) self.cur.execute(sql, values)
try: try:
con.commit() self.con.commit()
except sqlite.OperationalError, e: except sqlite.OperationalError, e:
print >> sys.stderr, str(e) print >> sys.stderr, str(e)
@ -203,10 +202,9 @@ class Logger:
ROOM_JID/nick if pm-related.''' ROOM_JID/nick if pm-related.'''
if self.jids_already_in == []: # only happens if we just created the db if self.jids_already_in == []: # only happens if we just created the db
global con, cur self.con = sqlite.connect(LOG_DB_PATH, timeout = 20.0,
con = sqlite.connect(LOG_DB_PATH, timeout = 20.0,
isolation_level = 'IMMEDIATE') isolation_level = 'IMMEDIATE')
cur = con.cursor() self.cur = self.con.cursor()
jid = jid.lower() jid = jid.lower()
contact_name_col = None # holds nickname for kinds gcstatus, gc_msg contact_name_col = None # holds nickname for kinds gcstatus, gc_msg
@ -269,7 +267,7 @@ class Logger:
timed_out = now - (timeout * 60) # before that they are too old timed_out = now - (timeout * 60) # before that they are too old
# so if we ask last 5 lines and we have 2 pending we get # so if we ask last 5 lines and we have 2 pending we get
# 3 - 8 (we avoid the last 2 lines but we still return 5 asked) # 3 - 8 (we avoid the last 2 lines but we still return 5 asked)
cur.execute(''' self.cur.execute('''
SELECT time, kind, message FROM logs SELECT time, kind, message FROM logs
WHERE jid_id = %d AND kind IN (%d, %d, %d, %d) AND time > %d WHERE jid_id = %d AND kind IN (%d, %d, %d, %d) AND time > %d
ORDER BY time DESC LIMIT %d OFFSET %d ORDER BY time DESC LIMIT %d OFFSET %d
@ -278,7 +276,7 @@ class Logger:
timed_out, restore_how_many_rows, pending_how_many) timed_out, restore_how_many_rows, pending_how_many)
) )
results = cur.fetchall() results = self.cur.fetchall()
results.reverse() results.reverse()
return results return results
@ -303,14 +301,14 @@ class Logger:
seconds_in_a_day = 86400 # 60 * 60 * 24 seconds_in_a_day = 86400 # 60 * 60 * 24
last_second_of_day = start_of_day + seconds_in_a_day - 1 last_second_of_day = start_of_day + seconds_in_a_day - 1
cur.execute(''' self.cur.execute('''
SELECT contact_name, time, kind, show, message FROM logs SELECT contact_name, time, kind, show, message FROM logs
WHERE jid_id = %d WHERE jid_id = %d
AND time BETWEEN %d AND %d AND time BETWEEN %d AND %d
ORDER BY time ORDER BY time
''' % (jid_id, start_of_day, last_second_of_day)) ''' % (jid_id, start_of_day, last_second_of_day))
results = cur.fetchall() results = self.cur.fetchall()
return results return results
def get_search_results_for_query(self, jid, query): def get_search_results_for_query(self, jid, query):
@ -321,20 +319,20 @@ class Logger:
jid_id = self.get_jid_id(jid) jid_id = self.get_jid_id(jid)
if False: #query.startswith('SELECT '): # it's SQL query if False: #query.startswith('SELECT '): # it's SQL query
try: try:
cur.execute(query) self.cur.execute(query)
except sqlite.OperationalError, e: except sqlite.OperationalError, e:
results = [('', '', '', '', str(e))] results = [('', '', '', '', str(e))]
return results return results
else: # user just typed something, we search in message column else: # user just typed something, we search in message column
like_sql = '%' + query + '%' like_sql = '%' + query + '%'
cur.execute(''' self.cur.execute('''
SELECT contact_name, time, kind, show, message, subject FROM logs SELECT contact_name, time, kind, show, message, subject FROM logs
WHERE jid_id = ? AND message LIKE ? WHERE jid_id = ? AND message LIKE ?
ORDER BY time ORDER BY time
''', (jid_id, like_sql)) ''', (jid_id, like_sql))
results = cur.fetchall() results = self.cur.fetchall()
return results return results
def date_has_logs(self, jid, year, month, day): def date_has_logs(self, jid, year, month, day):
@ -348,7 +346,7 @@ class Logger:
last_second_of_day = start_of_day + seconds_in_a_day - 1 last_second_of_day = start_of_day + seconds_in_a_day - 1
# just ask one row to see if we have sth for this date # just ask one row to see if we have sth for this date
cur.execute(''' self.cur.execute('''
SELECT log_line_id FROM logs SELECT log_line_id FROM logs
WHERE jid_id = %d WHERE jid_id = %d
AND time BETWEEN %d AND %d AND time BETWEEN %d AND %d
@ -356,7 +354,7 @@ class Logger:
LIMIT 1 LIMIT 1
''' % (jid_id, start_of_day, last_second_of_day, ''' % (jid_id, start_of_day, last_second_of_day,
constants.KIND_STATUS, constants.KIND_GCSTATUS)) constants.KIND_STATUS, constants.KIND_GCSTATUS))
result = cur.fetchone() result = self.cur.fetchone()
if result: if result:
return True return True
else: else:
@ -367,11 +365,11 @@ class Logger:
we had logs (excluding statuses)''' we had logs (excluding statuses)'''
jid = jid.lower() jid = jid.lower()
jid_id = self.get_jid_id(jid) jid_id = self.get_jid_id(jid)
cur.execute(''' self.cur.execute('''
SELECT time FROM logs SELECT time FROM logs
WHERE jid_id = ? WHERE jid_id = ?
AND kind NOT IN (?, ?) AND kind NOT IN (?, ?)
ORDER BY time DESC LIMIT 1 ORDER BY time DESC LIMIT 1
''', (jid_id, constants.KIND_STATUS, constants.KIND_GCSTATUS)) ''', (jid_id, constants.KIND_STATUS, constants.KIND_GCSTATUS))
result = cur.fetchone() result = self.cur.fetchone()
return result return result