PageRenderTime 152ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/historical/sqlfilters.py

https://bitbucket.org/lindenlab/apiary/
Python | 124 lines | 77 code | 23 blank | 24 comment | 19 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. import re
  26. import sys
  27. import time
  28. from sqllog import *
  29. def build_matchers(path='indra-tables'):
  30. schemas = {}
  31. tableRE = re.compile(r'(\w+)\.(\w+)')
  32. for line in file(path):
  33. m = tableRE.match(line)
  34. if m:
  35. schema, table = m.groups()
  36. if schema not in schemas:
  37. schemas[schema] = []
  38. schemas[schema].append(table)
  39. matchers = {}
  40. for schema,table_list in schemas.iteritems():
  41. expr = r'[^`.\w]`?(?:' + '|'.join(table_list) + r')`?(?:[^`.\w]|$)'
  42. matchers[schema] = re.compile(expr)
  43. return matchers
  44. class PrependSchema(object):
  45. def __init__(self, default_schema=None):
  46. self._matchers = build_matchers()
  47. self._default_schema = default_schema
  48. def __call__(self, sql):
  49. n = 0
  50. s = []
  51. for schema,matcher in self._matchers.iteritems():
  52. if matcher.search(sql):
  53. n += 1
  54. s = schema
  55. if n == 1 and s != self._default_schema:
  56. return ["USE " + s, sql]
  57. return [sql]
  58. class FindMissingSchemas(FollowSequences):
  59. def __init__(self):
  60. FollowSequences.__init__(self)
  61. self._matchers = build_matchers()
  62. self._num_full_spec = 0
  63. self._num_add_schema = {}
  64. self._num_add_multiple = 0
  65. def notingEvent(self, s, e):
  66. if e.state != Event.Query:
  67. return
  68. n = 0
  69. s = []
  70. for schema,matcher in self._matchers.iteritems():
  71. if matcher.search(e.body):
  72. n += 1
  73. s.append(schema)
  74. if n == 0:
  75. self._num_full_spec += 1
  76. elif n == 1:
  77. self._num_add_schema[s[0]] = self._num_add_schema.get(s[0], 0) + 1
  78. else:
  79. self._num_add_multiple += 1
  80. if True and n > 0:
  81. if n > 1:
  82. print "*** TWO MATCHES ***"
  83. print "USE", ','.join(s)
  84. print e.body
  85. print '----------------------------------'
  86. def report(self):
  87. print "%30s: %8d" % ("_num_full_spec", self._num_full_spec)
  88. schemas = self._num_add_schema.keys()
  89. schemas.sort()
  90. for s in schemas:
  91. print "%20s %9s: %8d" % ("_num_add_schema", s, self._num_add_schema[s])
  92. print "%30s: %8d" % ("_num_add_multiple", self._num_add_multiple)
  93. if __name__ == '__main__':
  94. f = FindMissingSchemas()
  95. t = - time.time()
  96. c = - time.clock()
  97. f.replay(input_events(sys.argv[1:]))
  98. c += time.clock()
  99. t += time.time()
  100. print ("Timing: %f process clock, %f wall clock" % (c, t))
  101. f.report()