flask.ext.sqlalchemy.SQLAlchemy.get_engine

Here are the examples of the python api flask.ext.sqlalchemy.SQLAlchemy.get_engine taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

Example 1

Project: ggrc-core Source File: app.py
def _display_sql_queries():
  """Set up display database queries

  This function makes sure we display the sql queries if the record setting is
  enabled.
  """
  report_type = getattr(settings, "SQLALCHEMY_RECORD_QUERIES", False)
  valid_types = ('count', 'slow', 'all')
  if report_type:
    if report_type not in valid_types:
      raise Exception("""Invalid SQLALCHEMY_RECORD_QUERIES value specified.
        Possible options: {}""".format(', '.join(valid_types)))

    # pylint: disable=unused-variable
    @app.after_request
    def display_queries(response):
      """Display database queries

      Prints out SQL queries, EXPLAINs for queries above slow_threshold, and
      a final count of queries after every HTTP request
      """
      slow_threshold = 0.5  # EXPLAIN queries that ran for more than 0.5s
      queries = get_debug_queries()
      logger.info("Total queries: %s", len(queries))
      if report_type == 'count':
        return response
      # We have to copy the queries list below otherwise queries executed
      # in the for loop will be appended causing an endless loop
      for query in queries[:]:
        if report_type == 'slow' and query.duration < slow_threshold:
          continue
        logger.info(
            "%.8f %s\n%s\n%s",
            query.duration,
            query.context,
            query.statement,
            query.parameters)
        is_select = bool(re.match('SELECT', query.statement, re.I))
        if query.duration > slow_threshold and is_select:
          try:
            statement = "EXPLAIN " + query.statement
            engine = SQLAlchemy().get_engine(app)
            result = engine.execute(statement, query.parameters)
            logger.info(tabulate(result.fetchall(), headers=result.keys()))
          except:  # pylint: disable=bare-except
            logger.warning("Statement failed: %s", statement, exc_info=True)
      return response