def find_values_by_company_name(self, company_name):
data_row = self._pg_util.query_one_sql(
self.s_b.find_by_value(column_name=self.model.COMPANY_NAME, column_value=company_name)
)
def find_by_value(self, column_name, column_value, is_array=False, limit_num=None):
"""
注释见同名私有方法
"""
return self.__find_column_by_value('*', column_name, column_value, is_array, limit_num)
def __find_column_by_value(self, select_column_name, column_name, column_value, is_array=False,
limit_num=None):
"""
构造查找列名值等于输入值的sql
:param column_name: 列名
:param column_value: 列值,这里会做安全过滤处理
:param is_array: 列值是否为一个数组列
:param limit_num: 最大获取数量
"""
condition = ' WHERE '
if is_array:
condition += column_name + ' @>array[%s]'
else:
condition += column_name + '= %s'
if limit_num is not None:
condition += ' limit ' + str(limit_num)
return 'SELECT ' + select_column_name + ' from ' + self._table_name + \
condition, [column_value]
def query_one_sql(self, sql_and_params):
"""
查询返回行
"""
sql, params = sql_and_params
conn = self.get_conn()
cur = conn.cursor(cursor_factory=DictCursor)
try:
cur.execute(sql, params)
row = cur.fetchone()
return dict(row) if row else {}
except Exception, e:
self.__logging_error_message(sql, params)
raise e
finally:
self.__close_conn(cur, conn)