def get_db_component(request): format_ = {'data': [], 'status': 1, 'message': ''} db = None try: source = json.loads(request.POST.get('source', '{}')) user = User.objects.get(username=request.user) name = None if source['rdbmsMode'] == 'configRdbms': if source['rdbmsType'] != 'jdbc': query_server = rdbms.get_query_server_config(server=source['rdbmsType']) db = rdbms.get(user, query_server=query_server) else: interpreters = get_ordered_interpreters(request.user) options = {} key = [key for key in interpreters if key['name'] == source['rdbmsJdbcDriverName']] if key: options = key[0]['options'] db = Jdbc(driver_name=options['driver'], url=options['url'], username=options['user'], password=options['password']) else: name = source['rdbmsType'] if name != 'jdbc': query_server = { 'server_name': name, 'server_host': source['rdbmsHostname'], 'server_port': int(source['rdbmsPort']), 'username': source['rdbmsUsername'], 'password': source['rdbmsPassword'], 'options': {}, 'alias': name } db = rdbms.get(user, query_server=query_server) else: db = Jdbc(driver_name=source['rdbmsJdbcDriver'], url=source['rdbmsHostname'], username=source['rdbmsUsername'], password=source['rdbmsPassword']) if source['rdbmsType'] != 'jdbc': assist = Assist(db) else: assist = JdbcAssist(db) if not source['rdbmsDatabaseName']: data = assist.get_databases() elif source['rdbmsDatabaseName']: data = assist.get_tables(source['rdbmsDatabaseName']) format_['data'] = [{'name': element, 'value': element} for element in data] format_['status'] = 0 except Exception, e: message = _('Error accessing the database %s: %s') % (name, e) LOG.warn(message) format['message'] = message
def _get_db(request): source = json.loads( request.POST.get('source', request.POST.get('fileFormat', '{}'))) user = User.objects.get(username=request.user) name = None if source['rdbmsMode'] == 'configRdbms': if source['rdbmsType'] != 'jdbc': query_server = rdbms.get_query_server_config( server=source['rdbmsType']) db = rdbms.get(user, query_server=query_server) else: interpreters = get_ordered_interpreters(request.user) options = {} key = [ key for key in interpreters if key['name'] == source['rdbmsJdbcDriverName'] ] if key: options = key[0]['options'] db = Jdbc(driver_name=options['driver'], url=options['url'], username=options['user'], password=options['password']) else: name = source['rdbmsType'] if name != 'jdbc': query_server = { 'server_name': name, 'server_host': source['rdbmsHostname'], 'server_port': int(source['rdbmsPort']), 'username': source['rdbmsUsername'], 'password': source['rdbmsPassword'], 'options': {}, 'alias': name } db = rdbms.get(user, query_server=query_server) else: db = Jdbc(driver_name=source['rdbmsJdbcDriver'], url=source['rdbmsHostname'], username=source['rdbmsUsername'], password=source['rdbmsPassword']) return db
def __init__(self, user, interpreter=None): global API_CACHE Api.__init__(self, user, interpreter=interpreter) self.db = None self.options = interpreter['options'] if self.cache_key in API_CACHE: self.db = API_CACHE[self.cache_key] elif 'password' in self.options: username = self.options.get('user') or user.username self.db = API_CACHE[self.cache_key] = Jdbc(self.options['driver'], self.options['url'], username, self.options['password'])
def __init__(self, user, fs=None, jt=None, options=None): global API_CACHE Api.__init__(self, user, fs=fs, jt=jt, options=options) self.db = None if self.options['user'] in API_CACHE: self.db = API_CACHE[self.options['user']] elif 'password' in self.options: self.db = API_CACHE[self.options['user']] = Jdbc( self.options['driver'], self.options['url'], self.options['user'], self.options['password'])
def execute(self, notebook, snippet): db = Jdbc(self.options['driver'], self.options['url'], self.options['user'], self.options['password']) try: db.connect() curs = db.cursor() try: curs.execute(snippet['statement']) data = curs.fetchmany(100) description = curs.description finally: curs.close() finally: db.close() return { 'sync': True, 'has_result_set': True, 'result': { 'has_more': False, 'data': list(data), 'meta': [{ 'name': col[0], 'type': col[1], 'comment': '' } for col in description], 'type': 'table' } }
def create_session(self, lang=None, properties=None): global API_CACHE props = super(JdbcApi, self).create_session(lang, properties) properties = dict([(p['name'], p['value']) for p in properties]) if properties is not None else {} props['properties'] = {} # We don't store passwords if self.db is None: if 'password' in properties: user = properties.get('user') or self.options.get('user') props['properties'] = {'user': user} self.db = API_CACHE[self.cache_key] = Jdbc(self.options['driver'], self.options['url'], user, properties.pop('password')) if self.db is None: raise AuthenticationRequired() return props