def _cursor( self, connection, kwargs ): if not connection: self.connectionFactory = kwargs.get( 'options' ) and kwargs.get( 'options' ).get( 'CONNECTION_FACTORY' ) or None if self.connectionFactory: con = self.connectionFactory.getConnection() connection = PyConnection( con ) else: host = kwargs.get( 'host' ) or 'localhost' port = kwargs.get( 'port' ) and ( ':%s' % kwargs.get( 'port' )) or '' if not ( host.lower() == 'localhost' ): conn_string = "jdbc:db2://%s%s/%s" % ( host, port, kwargs.get( 'database' ) ) else: conn_string = "jdbc:db2:%s" % ( kwargs.get( 'database' ) ) # for Setting default AUTO COMMIT off on connection. autocommit = False if kwargs.get( 'options' ) and kwargs.get( 'options' ).keys().__contains__( 'autocommit' ): autocommit = kwargs.get( 'options' ).get( 'autocommit' ) del kwargs.get( 'options' )['autocommit'] connection = zxJDBC.connect( conn_string, kwargs.get( 'user' ), kwargs.get( 'password' ), 'com.ibm.db2.jcc.DB2Driver', kwargs.get( 'options' ) ) # To prevent dirty reads self.__prevent_dirty_reads( connection ) connection.__connection__.setAutoCommit( autocommit ) return connection, DB2CursorWrapper( connection.cursor() ) else: return DB2CursorWrapper( connection.cursor() )
def __init__(self, jConnection): """ @type: java.sql.Connection or com.hp.ucmdb.discovery.probe.util.DiscoveryProbeDbConnection """ assert jConnection, 'Connection should not be None' wrapper = self.__getJdbcConnection(jConnection) connection = PyConnection(wrapper) self._cursor = connection.cursor() self.queryCount = 0
def get_new_connection(self, kwargs): self.connectionFactory = kwargs.get( 'options' ) and kwargs.get( 'options' ).get( 'CONNECTION_FACTORY' ) or None if self.connectionFactory: con = self.connectionFactory.getConnection() connection = PyConnection( con ) else: host = kwargs.get( 'host' ) or 'localhost' port = kwargs.get( 'port' ) and ( ':%s' % kwargs.get( 'port' )) or '' DriverType = 4 kwargsKeys = kwargs.keys() if kwargsKeys.__contains__( 'DriverType' ): DriverType = kwargs['DriverType'] if DriverType == 4: conn_string = "jdbc:db2://%s%s/%s" % ( host, port, kwargs.get( 'database' ) ) elif DriverType == 2: conn_string = "jdbc:db2:%s" % ( kwargs.get( 'database' ) ) else: raise ImproperlyConfigured( "Wrong Driver type" ) # for Setting default AUTO COMMIT off on connection. autocommit = False if kwargs.get( 'options' ) and kwargs.get( 'options' ).keys().__contains__( 'autocommit' ): autocommit = kwargs.get( 'options' ).get( 'autocommit' ) del kwargs.get( 'options' )['autocommit'] connection = zxJDBC.connect( conn_string, kwargs.get( 'user' ), kwargs.get( 'password' ), 'com.ibm.db2.jcc.DB2Driver', kwargs.get( 'options' ) ) # To prevent dirty reads self.__prevent_dirty_reads( connection ) connection.__connection__.setAutoCommit( autocommit ) return connection
def __init__(self, url, options): classpath = os.getenv('CLASSPATH') if getattr(options, 'classpath', None) is not None: if classpath: classpath = ':'.join([classpath, options.classpath]) else: classpath = options.classpath if classpath: for path in classpath.split(':'): if not path in sys.path: sys.path.append(path) try: import importlib Driver = importlib.import_module(options.driver) except ImportError: raise DriverNotFound('Cannot import %s, please check CLASSPATH' % options.driver) props = java.util.Properties() for attr in ('user', 'password'): if hasattr(options, attr): props.put(attr, getattr(options, attr)) if hasattr(options, 'dsn'): props.put('serverDataSource', options.dsn) db = Driver().connect(url, props) self.__conn = PyConnection(db)
def getDBConnection(userName, password, driverName, connectionURL): """ Return PyConnection to the database (see Jython's zxJDBC description) @param userName: the username to connect to DB with @param password: the password to connect to DB with @param driverName: name of the driver to connect through @return: com.ziclix.python.sql.PyConnection """ jdbcDriver = Class.forName( driverName, 1, Thread.currentThread().getContextClassLoader()).newInstance() props = Properties() props.put('user', userName) props.put('password', password) return PyConnection(jdbcDriver.connect(connectionURL, props))
class JDBCConnection(object): def __init__(self, url, options): classpath = os.getenv('CLASSPATH') if getattr(options, 'classpath', None) is not None: if classpath: classpath = ':'.join([classpath, options.classpath]) else: classpath = options.classpath if classpath: for path in classpath.split(':'): if not path in sys.path: sys.path.append(path) try: import importlib Driver = importlib.import_module(options.driver) except ImportError: raise DriverNotFound('Cannot import %s, please check CLASSPATH' % options.driver) props = java.util.Properties() for attr in ('user', 'password'): if hasattr(options, attr): props.put(attr, getattr(options, attr)) if hasattr(options, 'dsn'): props.put('serverDataSource', options.dsn) db = Driver().connect(url, props) self.__conn = PyConnection(db) def cursor(self): return self.__conn.cursor() def get_tables(self, catalog, schema='%', table='%'): cursor = self.cursor() cursor.tables(catalog, schema, table, ['TABLE']) return cursor.fetchall() def get_columns(self, catalog, schema='%', table='%', column='%'): cursor = self.cursor() cursor.columns(catalog, schema, table, column) return cursor.fetchall()
def process(self, millis): conn = PyConnection(self.ds.getConnection()) c = conn.cursor() try: c.execute("insert into dataservice.py_script_heartbeat(beat,demo) values (%d,'%s')" % (millis, 'nonse')) conn.commit() finally: if c: c.close() if conn: conn.close() #self.log.info(self.name + ': success') return 'success'
def tick(self): for phrase in self.sql: self.startQueryCycle(phrase["id"]) preparedStatementParams = [] preparedStatementParamsDict = {} logger.info(phrase["statement"]) numrows = 0 conn = PyConnection(self.datasource.getConnection()) try: with conn.cursor(1) as cursor: #TODO: review, index out of range very possible if "parameter" in phrase: logger.debug("Dude, got parameters!!") for parameter in phrase["parameter"]: strlist = parameter.split("$cycle.") if len(strlist) > 0: if strlist[1] in ["qend","qstart","qlaststart","qenddt","qstartdt","qlaststartdt","qelapsed"]: value = self.getQueryParameter(phrase["id"], strlist[1]) if value == None: logger.debug("Dude, couldn't fetch local parameter %s = %s, going global", parameter, value) value = self.getCycleProperty(strlist[1][1:]) preparedStatementParams.append(value) preparedStatementParamsDict[parameter] = value logger.debug("Dude, got local parameter %s = %s", parameter, value) else: value = self.getCycleProperty(strlist[1]) if value == None: value = phrase[strlist[1]] preparedStatementParams.append(value) preparedStatementParamsDict[parameter] = value logger.debug("Dude, got global parameter %s = %s", parameter, value) logger.debug(strlist[1]) logger.debug("Dude, preparing statement: %s", phrase["id"]) query = cursor.prepare(phrase["statement"]) logger.debug("Dude, executing statement: %s", phrase["id"]) cursor.execute(query, preparedStatementParams) row = None logger.debug("Dude, starting fetch for statement: %s", phrase["id"]) if cursor.description != None: fields = [i[0] for i in cursor.description] row = cursor.fetchone() else: fields = [] conn.commit() self.processData({ '@timestamp':self.getQueryParameter(phrase["id"], "qstart"), "statement": phrase["statement"], "parameters": preparedStatementParamsDict } ) #TODO: process data with commit timestamp and whatnot while row is not None: idx = 0 out = {} rowDict = {} metrics = {} terms = {} for field in fields: if isinstance(row[idx], str): if self.getInputProperty("ignoreFieldIfEmptyString") and len(row[idx]) == 0: logger.warning("Ignoring key %s due to empty value", field) else: rowDict[field] = row[idx] elif isinstance(row[idx], unicode): if self.getInputProperty("ignoreFieldIfEmptyString") and len(row[idx]) == 0: logger.warning("Ignoring key %s due to empty value", field) else: rowDict[field] = row[idx] else: rowDict[field] = row[idx] idx = idx + 1 for key in rowDict: if key in self.metrics: metrics[key] = rowDict[key] elif key in self.terms: terms[key] = rowDict[key] else: out[key] = rowDict[key] self.processData(out) for key in metrics: try: out["metric"] = key if self.getInputProperty("decimalMark"): metrics[key] = metrics[key].replace(self.getInputProperty("decimalMark"), ".") out["value"] = float(metrics[key]) self.processData(out) except Exception, ex: logger.warning("Failure to parse %s as float for metric %s", key, metrics[key]) #self.processData(out) if 'value' in out: del out['value'] for key in terms: out["metric"] = key out["term"] = str(terms[key]) self.processData(out) row = cursor.fetchone() query.close() assert query.closed except Exception, ex: logger.debug("\n _ _ _ \n __| |_ _ __| | ___| |\n / _` | | | |/ _` |/ _ \ |\n| (_| | |_| | (_| | __/_|\n \__,_|\__,_|\__,_|\___(_)\n") logger.debug(ex) raise finally: