def ExecuteQuery()

in framework/resources/Functional/odbcTest.py [0:0]


def ExecuteQuery(queryDir, queryFile, outputFileExt, resultFileCreated):
  queryFileNameRegex = re.compile("(.*)\.(.*)$")
  queryFileName = queryFileNameRegex.search(queryFile).group(1)

  queryStrings = subprocess.Popen("cat %s/%s" % (queryDir, queryFile), shell=True, stdout=subprocess.PIPE).stdout.read()

  # if there are multiple SQL statements, there is only one main SQL query.
  # the first few are used to setup the main query.
  # the last few are used to restore the state of the system after the query is run.
  # If a query starts with "--@test", it is considered the main SQL query.
  # Otherwise, the number of statements before and after the main SQL query is the same.
  # thus we expect an odd number of SQL statements.
  if queryStrings.count(';') > 1:
    # find the main query
    numberStatements = queryStrings.count(';')
    statements = queryStrings.split(';')
    mainStatementIndex = -1
    for index,statement in enumerate(statements):
      statement = statement.lstrip()
      if statement.startswith("--@test"):
        mainStatementIndex = index
        break
    if mainStatementIndex == -1:
      mainStatementIndex = numberStatements / 2

    # execute the setup statements
    for index in range(0,mainStatementIndex):
      queryString = statements[index]
      queryString = queryString.rstrip('\n;')
      try:
        value = cursor.execute(queryString)
      except:
        print "ERROR - generic execute error for setup statements"
    mainStatement = statements[mainStatementIndex]
    multipleStatements = True
  else:
    mainStatement = queryStrings
    multipleStatements = False

  queryString = mainStatement.rstrip(';\n\r\t ')

  try:
    value = cursor.execute(queryString)
  except Exception as inst:
    print "ERROR - generic execute error for main statement: %s" % inst
    failedTests.append(queryFile)

  rows = cursor.fetchall()
  SaveData (rows, queryDir, queryFileName, outputFileExt, resultFileCreated)

  # if there are statements after the main statement, execute them
  # to restore the system
  if multipleStatements:
    for index in range(mainStatementIndex+1, numberStatements):
      queryString = statements[index]
      queryString = queryString.rstrip('\n;')
      try:
        value = cursor.execute(queryString)
      except:
        print "ERROR - generic execute error for cleanup statements"