~openerp/openobject-server/web-dashboard

« back to all changes in this revision

Viewing changes to bin/sql_db.py

  • Committer: HDA(OpenERP)
  • Date: 2010-03-03 07:22:55 UTC
  • mfrom: (2089.1.16)
  • Revision ID: hda@tinyerp.com-20100303072255-5opkt86b0kqlagj9
Merged

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
__all__ = ['db_connect', 'close_db']
23
23
 
 
24
import logging
24
25
import netsvc
25
26
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE
26
27
from psycopg2.psycopg1 import cursor as psycopg1cursor
60
61
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
61
62
re_into = re.compile('.* into "?([a-zA-Z_0-9]+)"? .*$');
62
63
 
63
 
 
64
 
def log(msg, lvl=netsvc.LOG_DEBUG2):
65
 
    logger = netsvc.Logger()
66
 
    logger.notifyChannel('sql', lvl, msg)
67
 
 
68
64
sql_counter = 0
69
65
 
70
66
class Cursor(object):
71
67
    IN_MAX = 1000
 
68
    __logger = logging.getLogger('db.cursor')
72
69
 
73
70
    def check(f):
74
71
        @wraps(f)
102
99
            # pool, preventing some operation on the database like dropping it.
103
100
            # This can also lead to a server overload.
104
101
            msg = "Cursor not closed explicitly\n"  \
105
 
                  "Cursor was created at %s:%s" % self.__caller
106
 
            log(msg, netsvc.LOG_WARNING)
 
102
                  "Cursor was created at %s:%s"
 
103
            self.__logger.warn(msg, *self.__caller)
107
104
            self.close()
108
105
 
109
106
    @check
110
107
    def execute(self, query, params=None):
111
108
        if '%d' in query or '%f' in query:
112
 
            log(query, netsvc.LOG_WARNING)
113
 
            log("SQL queries cannot contain %d or %f anymore. Use only %s", netsvc.LOG_WARNING)
 
109
            self.__logger.warn(query)
 
110
            self.__logger.warn("SQL queries cannot contain %d or %f anymore. "
 
111
                               "Use only %s")
114
112
            if params:
115
113
                query = query.replace('%d', '%s').replace('%f', '%s')
116
114
 
124
122
            logger= netsvc.Logger()
125
123
            logger.notifyChannel('sql_db', netsvc.LOG_ERROR, "Programming error: %s, in query %s" % (pe, query))
126
124
            raise
127
 
        except Exception, e:
128
 
            log("bad query: %s" % self._obj.query)
129
 
            log(e)
 
125
        except Exception:
 
126
            self.__logger.exception("bad query: %s", self._obj.query)
130
127
            raise
131
128
 
132
129
        if self.sql_log:
133
130
            delay = mdt.now() - now
134
131
            delay = delay.seconds * 1E6 + delay.microseconds
135
132
 
136
 
            log("query: %s" % self._obj.query)
 
133
            self.__logger.debug("query: %s", self._obj.query)
137
134
            self.sql_log_count+=1
138
135
            res_from = re_from.match(query.lower())
139
136
            if res_from:
158
155
            if sqllogs[type]:
159
156
                sqllogitems = sqllogs[type].items()
160
157
                sqllogitems.sort(key=lambda k: k[1][1])
161
 
                log("SQL LOG %s:" % (type,))
 
158
                self.__logger.debug("SQL LOG %s:", type)
162
159
                for r in sqllogitems:
163
160
                    delay = timedelta(microseconds=r[1][1])
164
 
                    log("table: %s: %s/%s" %(r[0], str(delay), r[1][0]))
 
161
                    self.__logger.debug("table: %s: %s/%s",
 
162
                                        r[0], delay, r[1][0])
165
163
                    sum+= r[1][1]
166
164
                sqllogs[type].clear()
167
165
            sum = timedelta(microseconds=sum)
168
 
            log("SUM %s:%s/%d [%d]" % (type, str(sum), self.sql_log_count, sql_counter))
 
166
            self.__logger.debug("SUM %s:%s/%d [%d]",
 
167
                                type, sum, self.sql_log_count, sql_counter)
169
168
            sqllogs[type].clear()
170
169
        process('from')
171
170
        process('into')