def _get_cursor(self): try: if self.connection is None: self.connection = mysql.connect(**self.connection_params) else: self.connection.ping(reconnect=True) cur = self.connection.cursor() cur.execute('SELECT VERSION()') if not cur.fetchall(): raise BackendError("_get_cursor: Connection Failed") cur.reset() return cur except Exception as ex: raise BackendError("_get_cursor: "+str(ex))
def __init__(self, connection_params): connection_params["server"] = connection_params.pop("host", None) for key in list(connection_params): if connection_params[key] is None: del connection_params[key] super().__init__(connection_params) try: self.connection = pymssql.connect(login_timeout=5, **connection_params) except pymssql.Error as ex: raise BackendError(str(ex)) from ex except ValueError: # ValueError is raised when 'server' contains "\\" raise BackendError("Incorrect format of connection details")
def execute_sql_query(self, query, params=()): try: with self.connection.cursor() as cur: cur.execute(query, *params) yield cur except pymssql.Error as ex: raise BackendError(str(ex)) from ex
def count_approx(self, query): with self.connection.cursor() as cur: try: cur.execute("SET SHOWPLAN_XML ON") try: cur.execute(query) result = cur.fetchone() match = self.EST_ROWS_RE.search(result[0]) if not match: # Either StatementEstRows was not found or # a float is received. # If it is a float then it is most probable # that the server's statistics are out of date # and the result is false. In that case # it is preferable to return None so # an exact count be used. return None return int(match.group(1)) finally: cur.execute("SET SHOWPLAN_XML OFF") except pymssql.Error as ex: if "SHOWPLAN permission denied" in str(ex): warnings.warn( "SHOWPLAN permission denied, count approximates will not be used" ) return None raise BackendError(str(ex)) from ex
def count_approx(self, query): with self.connection.cursor() as cur: try: cur.execute("SET SHOWPLAN_XML ON") try: cur.execute(query) result = cur.fetchone() return int(self.EST_ROWS_RE.search(result[0]).group(1)) except AttributeError: # This is to catch a bug from SQL Server 2012 # (the StatementEstRows=float instead of an int) pass finally: cur.execute("SET SHOWPLAN_XML OFF") except pymssql.Error as ex: if "SHOWPLAN permission denied" in str(ex): warnings.warn( "SHOWPLAN permission denied, count approximates will not be used" ) return None raise BackendError(str(ex)) from ex # In case of an AttributeError, give a second chance: # Use the long method for counting (or upgrade SQL Server version :) ) cur.execute("SELECT count(*) FROM ( {} ) x".format(query)) result = cur.fetchone() return result[0]
def __init__(self, connection_params): connection_params["server"] = connection_params.pop("host", None) for key in list(connection_params): if connection_params[key] is None: del connection_params[key] super().__init__(connection_params) try: self.connection = pymssql.connect(**connection_params) except pymssql.Error as ex: raise BackendError(str(ex)) from ex
def __init__(self, connection_params): super().__init__(connection_params) connection_params.pop('port') self.connection_params = connection_params try: cursor = self._get_cursor() cursor.execute('SELECT VERSION()') if cursor.fetchall() is None: raise BackendError cursor.close() except Exception as e: raise BackendError(str(e))
def execute_sql_query(self, query, params=None): connection = self.connection_pool.getconn() cur = connection.cursor() try: utfquery = cur.mogrify(query, params).decode('utf-8') log.debug("Executing: %s", utfquery) t = time() cur.execute(query, params) yield cur log.info("%.2f ms: %s", 1000 * (time() - t), utfquery) except (Error, ProgrammingError) as ex: raise BackendError(str(ex)) from ex finally: connection.commit() self.connection_pool.putconn(connection)
def count_approx(self, query): try: with self.connection.cursor() as cur: cur.execute("SET SHOWPLAN_XML ON") try: cur.execute(query) result = cur.fetchone() return int(self.EST_ROWS_RE.search(result[0]).group(1)) finally: cur.execute("SET SHOWPLAN_XML OFF") except pymssql.Error as ex: if "SHOWPLAN permission denied" in str(ex): warnings.warn("SHOWPLAN permission denied, count approximates will not be used") return None raise BackendError(str(ex)) from ex
def __init__(self, connection_params): super().__init__(connection_params) raise BackendError("Error connecting to DB.")
def _create_connection_pool(self): try: self.connection_pool = ThreadedConnectionPool( 1, 16, **self.connection_params) except Error as ex: raise BackendError(str(ex)) from ex