PageRenderTime 664ms CodeModel.GetById 162ms app.highlight 138ms RepoModel.GetById 248ms app.codeStats 0ms

/historical/sqlfilters.py

https://bitbucket.org/lindenlab/apiary/
Python | 124 lines | 77 code | 23 blank | 24 comment | 24 complexity | 080db846b1aa4b61d8d853bb85005ee7 MD5 | raw file
  1#
  2# $LicenseInfo:firstyear=2010&license=mit$
  3# 
  4# Copyright (c) 2010, Linden Research, Inc.
  5# 
  6# Permission is hereby granted, free of charge, to any person obtaining a copy
  7# of this software and associated documentation files (the "Software"), to deal
  8# in the Software without restriction, including without limitation the rights
  9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10# copies of the Software, and to permit persons to whom the Software is
 11# furnished to do so, subject to the following conditions:
 12# 
 13# The above copyright notice and this permission notice shall be included in
 14# all copies or substantial portions of the Software.
 15# 
 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22# THE SOFTWARE.
 23# $/LicenseInfo$
 24#
 25
 26import re
 27import sys
 28import time
 29
 30from sqllog import *
 31
 32def build_matchers(path='indra-tables'):
 33    schemas = {}
 34    tableRE = re.compile(r'(\w+)\.(\w+)')
 35    for line in file(path):
 36        m = tableRE.match(line)
 37        if m:
 38            schema, table = m.groups()
 39            if schema not in schemas:
 40                schemas[schema] = []
 41            schemas[schema].append(table)
 42    matchers = {}
 43    for schema,table_list in schemas.iteritems():
 44        expr = r'[^`.\w]`?(?:' + '|'.join(table_list) + r')`?(?:[^`.\w]|$)'
 45        matchers[schema] = re.compile(expr)
 46    return matchers
 47
 48
 49
 50class PrependSchema(object):
 51    def __init__(self, default_schema=None):
 52        self._matchers = build_matchers()
 53        self._default_schema = default_schema
 54
 55    def __call__(self, sql):
 56        n = 0
 57        s = []
 58        for schema,matcher in self._matchers.iteritems():
 59            if matcher.search(sql):
 60                n += 1
 61                s = schema
 62                
 63        if n == 1 and s != self._default_schema:
 64            return ["USE " + s, sql]
 65        return [sql]
 66
 67
 68
 69
 70
 71class FindMissingSchemas(FollowSequences):
 72    def __init__(self):
 73        FollowSequences.__init__(self)
 74        self._matchers = build_matchers()
 75        self._num_full_spec = 0
 76        self._num_add_schema = {}
 77        self._num_add_multiple = 0
 78    
 79    def notingEvent(self, s, e):
 80        if e.state != Event.Query:
 81            return
 82        n = 0
 83        s = []
 84        for schema,matcher in self._matchers.iteritems():
 85            if matcher.search(e.body):
 86                n += 1
 87                s.append(schema)
 88                
 89        if n == 0:
 90            self._num_full_spec += 1
 91        elif n == 1:
 92            self._num_add_schema[s[0]] = self._num_add_schema.get(s[0], 0) + 1
 93        else:
 94            self._num_add_multiple += 1
 95            
 96        if True and n > 0:
 97            if n > 1:
 98                print "*** TWO MATCHES ***"
 99            print "USE", ','.join(s)
100            print e.body
101            print '----------------------------------'
102
103    def report(self):
104        print "%30s:   %8d" % ("_num_full_spec", self._num_full_spec)
105        schemas = self._num_add_schema.keys()
106        schemas.sort()
107        for s in schemas:
108            print "%20s %9s:   %8d" % ("_num_add_schema", s, self._num_add_schema[s])
109        print "%30s:   %8d" % ("_num_add_multiple", self._num_add_multiple)
110        
111        
112        
113if __name__ == '__main__':
114    f = FindMissingSchemas()
115
116    t = - time.time()
117    c = - time.clock()
118    f.replay(input_events(sys.argv[1:]))
119    c += time.clock()
120    t += time.time()
121
122    print ("Timing: %f process clock, %f wall clock" % (c, t))
123    f.report()
124