def save_notification_policy(self, workspace_id, user_id, notification_policy): # Connect to database if needed. self.db_connect() # Build ANP arguments list. am = kanp.ANP_msg() am.add_u64(int(workspace_id)) am.add_u32(int(user_id)) am.add_u32(notification_policy) # Do the query. query = "SELECT do_notif_mgt(E'%s')" % ( pgdb.escape_bytea(am.get_payload()) ) cur = kpg.exec_pg_query_rb_on_except(self.db_conn, query) row = cur.fetchone() self.db_conn.commit() return 0
def workspace_bound_request(self, stored_proc_name, args_payload): # Connect or reuse DB connection. self.db_connect() # Do the query. query = "SELECT %s('%s')" % (stored_proc_name, pgdb.escape_bytea(args_payload)) cur = kpg.exec_pg_query_rb_on_except(self.db_conn, query) row = cur.fetchone() self.db_conn.commit() # Handle result. am = kanp.ANP_msg() am.parse(row[0].value) # Get generic error status. error_status = am.get_u32() if error_status == -1: # Generic error. err_msg = am.get_str() raise Exception("Error: '%s'." % (err_msg)) else: # No generic error... continue parsing. # Get result type. res_type = am.get_u32() res_buf = am.get_bin() res_error = am.get_u32() if res_type == kanp.KANP_RES_OK: # Success ext_buf_am = am res_buf_am = kanp.ANP_msg() res_buf_am.parse(res_buf) return ext_buf_am, res_buf_am else: # Specific error am = kanp.ANP_msg() am.parse(res_buf) err_code = am.get_u32() err_msg = am.get_str() raise Exception("Error: '%s' stored procedure failed: error: %i ('%s')" % \ ( stored_proc_name, err_code, err_msg ) )
def workspace_bound_request(self, stored_proc_name, args_payload): # Connect or reuse DB connection. self.db_connect() # Do the query. query = "SELECT %s('%s')" % ( stored_proc_name, pgdb.escape_bytea(args_payload) ) cur = kpg.exec_pg_query_rb_on_except(self.db_conn, query) row = cur.fetchone() self.db_conn.commit() # Handle result. am = kanp.ANP_msg() am.parse(row[0].value) # Get generic error status. error_status = am.get_u32() if error_status == -1: # Generic error. err_msg = am.get_str() raise Exception("Error: '%s'." % ( err_msg ) ) else: # No generic error... continue parsing. # Get result type. res_type = am.get_u32() res_buf = am.get_bin() res_error = am.get_u32() if res_type == kanp.KANP_RES_OK: # Success ext_buf_am = am res_buf_am = kanp.ANP_msg() res_buf_am.parse(res_buf) return ext_buf_am, res_buf_am else: # Specific error am = kanp.ANP_msg() am.parse(res_buf) err_code = am.get_u32() err_msg = am.get_str() raise Exception("Error: '%s' stored procedure failed: error: %i ('%s')" % \ ( stored_proc_name, err_code, err_msg ) )
def save_notification_policy(self, workspace_id, user_id, notification_policy): # Connect to database if needed. self.db_connect() # Build ANP arguments list. am = kanp.ANP_msg() am.add_u64(int(workspace_id)) am.add_u32(int(user_id)) am.add_u32(notification_policy) # Do the query. query = "SELECT do_notif_mgt(E'%s')" % (pgdb.escape_bytea( am.get_payload())) cur = kpg.exec_pg_query_rb_on_except(self.db_conn, query) row = cur.fetchone() self.db_conn.commit() return 0
def escape_pg_bytea(bytea): return "E'" + pgdb.escape_bytea(bytea) + "'"
def escape_pg_bytea(bytea): return "'" + pgdb.escape_bytea(bytea) + "'"
def __pg_repr__(self): return "E'%s'::bytea" % pgdb.escape_bytea(self.__data)
def formatInsertValuesList(row, starelid, inclHLL): """ @return rowVals """ rowVals = ["\t%s" % (starelid)] types = ['smallint', # staattnum 'boolean', 'real', 'integer', 'real', 'smallint', # stakind1 'smallint', 'smallint', 'smallint', 'smallint', 'oid', # staop1 'oid', 'oid', 'oid', 'oid', 'oid', # stacoll1 'oid', 'oid', 'oid', 'oid', 'real[]', # stanumbers1 'real[]', 'real[]', 'real[]', 'real[]' ] i = 0 hll = False # Populate types for stavaluesN: infer the type from pg_type.typname if row[3][0] == '_': rowTypes = types + [row[3]] * 5 else: rowTypes = types + [row[3] + '[]'] * 5 for val, typ in zip(row[5:], rowTypes): i = i + 1 # Check stakind1 to see if slot is a hll slot or a full hll slot if i == 10 and (val == 98 or val == 99): if inclHLL == False: val = 0 hll = True elif val is None: val = 'NULL' # Format stavalues5 for an hll slot elif i == 30 and hll: if inclHLL: val = '\'{"%s"}\'' % pgdb.escape_bytea(val[0]) rowVals.append('\t{0}::{1}'.format(val, 'bytea[]')) else: rowVals.append('\t{0}'.format('NULL::int4[]')) continue # Postgres array types are adapted to Python lists by pgdb # We have to transform these lists to Postgres array literals in the # output file. elif isinstance(val, list): val = format_array_literal(val) rowVals.append('\t{0}::{1}'.format(val, typ)) return rowVals
def formatInsertValuesList(row, starelid, inclHLL): """ @return rowVals """ rowVals = ["\t%s" % (starelid)] # the types of the columns in the pg_statistic table, except for starelid and stavalues[1-5] types = ['smallint', # staattnum 'boolean', 'real', 'integer', 'real', 'smallint', # stakind1 'smallint', 'smallint', 'smallint', 'smallint', 'oid', # staop1 'oid', 'oid', 'oid', 'oid', 'oid', # stacoll1 'oid', 'oid', 'oid', 'oid', 'real[]', # stanumbers1 'real[]', 'real[]', 'real[]', 'real[]' ] i = 0 hll = False typeschema = row[3] typename = row[4] if typeschema != "pg_catalog": # a type that is not built-in, qualify it with its schema name # and play it safe by double-quoting the identifiers typename = '"%s"."%s"' % (typeschema, typename) # Populate types for stavaluesN: infer the type from pg_type.typname if row[4][0] == '_': # column is an array type, use as is rowTypes = types + [typename] * 5 else: # non-array type, make an array type out of it rowTypes = types + [typename + '[]'] * 5 for val, typ in zip(row[6:], rowTypes): i = i + 1 # Check stakind1 to see if slot is a hll slot or a full hll slot if i == 10 and (val == 98 or val == 99): if inclHLL == False: val = 0 hll = True elif val is None: val = 'NULL' # Format stavalues5 for an hll slot elif i == 30 and hll: if inclHLL: val = '\'{"%s"}\'' % pgdb.escape_bytea(val[0]) rowVals.append('\t{0}::{1}'.format(val, 'bytea[]')) else: rowVals.append('\t{0}'.format('NULL::int4[]')) continue # Postgres array types are adapted to Python lists by pgdb # We have to transform these lists to Postgres array literals in the # output file. elif isinstance(val, list): val = format_array_literal(val) rowVals.append('\t{0}::{1}'.format(val, typ)) return rowVals