import os
import re
import sys
from ctypes import cdll, c_void_p, c_int, c_char_p, string_at
sys.path.append(sys.path[0] + "/../../")
from base_utils.os.env_util import EnvUtil
class SqlResult(object):
"""
Class for storing search result from database
"""
def __init__(self, result):
"""
Constructor
"""
self.resCount = 0
self.resSet = []
self.result = result
def parseResult(self):
"""
function : get resCount and resSet from result
input:NA
output:NA
"""
libpq_path = EnvUtil.get_libpgso55_env_path()
libc = cdll.LoadLibrary(libpq_path)
libc.PQntuples.argtypes = [c_void_p]
libc.PQntuples.restype = c_int
libc.PQnfields.argtypes = [c_void_p]
libc.PQnfields.restype = c_int
libc.PQgetvalue.restype = c_char_p
ntups = libc.PQntuples(self.result)
nfields = libc.PQnfields(self.result)
libc.PQgetvalue.argtypes = [c_void_p, c_int, c_int]
self.resCount = ntups
for i_index in range(ntups):
tmp_string = []
for j_index in range(nfields):
paramValue = libc.PQgetvalue(self.result, i_index, j_index)
if paramValue is not None:
tmp_string.append(string_at(paramValue))
else:
tmp_string.append("")
self.resSet.append(tmp_string)
@staticmethod
def findErrorInSql(output):
"""
function : Find error in sql
input : String
output : boolean
"""
ERROR_MSG_FLAG = "(ERROR|FATAL|PANIC)"
ERROR_PATTERN = "^%s:.*" % ERROR_MSG_FLAG
pattern = re.compile(ERROR_PATTERN)
for line in output.split("\n"):
line = line.strip()
result = pattern.match(line)
if result is not None:
return True
return False