Project

General

Profile

« Previous | Next » 

Revision 1889

sql.py: DbConn: Added run_query(). run_raw_query(): Use new DbConn.run_query().

View differences:

sql.py
6 6
import warnings
7 7

  
8 8
import exc
9
from Proxy import Proxy
9 10
import rand
10 11
import strings
11 12
import util
......
74 75
    return db_config['engine']+' database '+db_config['database']
75 76

  
76 77
class DbConn:
77
    def __init__(self, db_config, serializable=True):
78
    def __init__(self, db_config, serializable=True, debug=False):
78 79
        self.db_config = db_config
79 80
        self.serializable = serializable
81
        self.debug = debug
80 82
        
81 83
        self.__db = None
82 84
        self.pkeys = {}
83 85
        self.index_cols = {}
86
        self.query_results = {}
84 87
    
85 88
    def __getattr__(self, name):
86 89
        if name == '__dict__': raise Exception('getting __dict__')
......
111 114
                'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE')
112 115
        
113 116
        return self.__db
117
    
118
    class Cursor(Proxy):
119
        def __init__(self, inner):
120
            Proxy.__init__(self, inner)
121
            self.result = None
122
        
123
        def rows(cur): return iter(lambda: cur.fetchone(), None)
124
    
125
    def run_query(self, query, params=None):
126
        cur = self.db.cursor()
127
        try: cur.execute(query, params)
128
        except Exception, e:
129
            _add_cursor_info(e, cur)
130
            raise
131
        if self.debug:
132
            sys.stderr.write(strings.one_line(get_cur_query(cur))+'\n')
133
        return cur
114 134

  
115 135
connect = DbConn
116 136

  
117 137
##### Querying
118 138

  
119
def run_raw_query(db, query, params=None):
120
    cur = db.db.cursor()
121
    try: cur.execute(query, params)
122
    except Exception, e:
123
        _add_cursor_info(e, cur)
124
        raise
125
    if run_raw_query.debug:
126
        sys.stderr.write(strings.one_line(get_cur_query(cur))+'\n')
127
    return cur
139
def run_raw_query(db, query, params=None): return db.run_query(query, params)
128 140

  
129 141
##### Recoverable querying
130 142

  

Also available in: Unified diff