def _executemany(self, q, l): try: self.cursor.executemany(q, l) except psycopg2.OperationalError: Speaker.log_raise('Connection is broken...', BrokenConnection) except Exception, ss: Speaker.log_raise('%s', ERR_COL(ss), QueryError)
def _connect(self): Speaker.log_insane('Initializing postgres connection...') try: s= _PostgresInfo._connect_str() self.connection= psycopg2.connect(s) self.cursor= self.connection.cursor() except Exception, ss: Speaker.log_raise('Unable to establish a connection ' 'with the database: %s', ERR_COL(ss), BrokenConnection)
def or_query(s, filter= None): """ One Record Query: returns a dict.""" res= DbOne.mr_query(s, filter) if len(res)<> 1: Speaker.log_raise( "Query returned %s records instead of 1." % ERR_COL(len(res)), QueryError) return res[0]
def ov_query(s, filter): """ One Value Query: returns a unique value.""" res= DbOne.or_query(s, filter) res= res.values() if len(res)<> 1: Speaker.log_raise( "Query returned %s values instead of 1." % ERR_COL(len(res)), QueryError) return res[0]
def _connect(self): Speaker.log_insane('Initializing sqlite connection (db = %s)...', INF_COL(_SqliteInfo.dbfile)) dbfile= _SqliteInfo.dbfile try: self.connection= apsw.Connection(dbfile) self.cursor= self.connection.cursor() except Exception, ss: Speaker.log_raise('Unable to establish a connection ' 'with the database: %s', ERR_COL(ss), BrokenConnection)
def _safe_execute(execattr, s, d): try: return execattr(s, d) except BrokenConnection, e: Speaker.log_info('Connection to DB seems broken, ' 'now reconnecting...') try: DbOne._connect() except: Speaker.log_raise('Cannot re-establish connection', BrokenConnection) # if we are inside a trans, we have to signal # the broken transaction -> exception. # otherwise, the query is tried once more if DbOne.trans_level: Speaker.log_raise('Connection re-established: transaction is lost.', ConnectionError) return execattr(s, d)
def add_filter(self, filter, tbl= None, store= None): if not filter: return def t(tp): return isinstance(filter, tp) if store: self.locals.update(store) if t(str): if 'JOIN' in filter: self.pre_filter.append(filter) else: self.strfilter.append(filter) return afrm= self._arg_format res= [] if t(dict): for k, v in filter.iteritems(): if v== SQL_DEFAULT: if tbl: res.append('%s.%s= DEFAULT' % (tbl, k)) else: res.append('%s= DEFAULT' % k) elif v == SQL_NULL: if tbl: res.append('%s.%s IS NULL' % (tbl, k)) else: res.append('%s IS NULL' % k) else: if tbl: res.append('%s.%s= %s' % (tbl, k, afrm(k))) else: res.append('%s= %s' % (k, afrm(k))) self.strfilter.append(_A(res)) self.locals.update(filter) return # ToDo: just assume that filter is iterable? if t(list) or t(set) or t(frozenset): if not store: Speaker.log_raise('Invalid Filter: store not provided ' 'for iterator filter.', InvalidFilter) for k in filter: try: v= store[k] self.locals[k]= v except: Speaker.log_raise('Invalid Filter: key %s not in store', ERR_COL(k), InvalidFilter) if v== SQL_DEFAULT: if tbl: res.append('%s.%s= DEFAULT' % (tbl, k)) else: res.append('%s= DEFAULT' % k) elif v == SQL_DEFAULT: if tbl: res.append('%s.%s IS NULL' % (tbl, k)) else: res.append('%s IS NULL' % k) else: if tbl: res.append('%s.%s= %s' % (tbl, k, afrm(k))) else: res.append('%s= %s' % (k, afrm(k))) self.strfilter.append(_A(res)) return Speaker.log_raise('Invalid Filter: type %s not allowed.', ERR_COL(type(filter)), InvalidFilter)
def _execute(self, q, d): d= d or {} try: self.cursor.execute(q, d) except Exception, ss: Speaker.log_raise('%s', ERR_COL(ss), QueryError)
def _executemany(self, q, l): try: self.cursor.executemany(q, l) except Exception, ss: Speaker.log_raise('%s', ERR_COL(ss), QueryError)