PageRenderTime 20ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/historical/hive_indra.py

https://bitbucket.org/lindenlab/apiary/
Python | 72 lines | 37 code | 11 blank | 24 comment | 5 complexity | 3cb04cae87b2ec044ae30f0b4b56453a 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
26from optparse import OptionParser
27
28import filtertools
29import hive
30import hive_mysql
31import sqlfilters
32
33class IndraWorker(hive_mysql.MySQLWorker):
34    def __init__(self, options, arguments):
35        hive_mysql.MySQLWorker.__init__(self, options, arguments)
36        self._first_filters = []
37        self._all_filters = []
38        self._at_start = True
39        
40        if options.f_schema:
41            self._first_filters.append(
42                sqlfilters.PrependSchema(options.mysql_db))
43    
44    def start(self):
45        hive_mysql.MySQLWorker.start(self)
46        self._at_start = True
47    
48    def event(self, data):
49        hive_mysql.MySQLWorker.event(self, data)
50        self._at_start = False
51
52    def execute_sql(self, sql):
53        statements = [sql]
54        if self._at_start:
55            statements = filtertools.filterthru(statements, self._first_filters)
56        statements = filtertools.filterthru(statements, self._all_filters)
57        for s in statements:
58            hive_mysql.MySQLWorker.execute_sql(self, s)
59
60
61class IndraHive(hive_mysql.MySQLHive):
62    def __init__(self):
63        hive_mysql.MySQLHive.__init__(self, worker_cls=IndraWorker)
64    
65    def add_options(self, parser):
66        hive_mysql.MySQLHive.add_options(self, parser)
67        parser.add_option('--f-schema',
68                            action='store_true', default=False,
69                            help='infer schema for statements (default: off)')
70
71if __name__ == '__main__':
72    IndraHive().main()