def _execute(self, sql, opt=None, callno = <NUM_LIT:0>):
self._logger.debug("<STR_LIT>" + sql + "<STR_LIT>" + unicode(opt))<EOL>try:<EOL><INDENT>self._curs_pg.execute(sql, opt)<EOL><DEDENT>except psycopg2.InternalError as exc:<EOL><INDENT>self._con_pg.rollback()<EOL>if len(unicode(exc).split("<STR_LIT::>")) < <NUM_LIT:2>:<EOL><INDENT>raise NipapError(exc)<EOL><DEDENT>code = unicode(exc).split("<STR_LIT::>", <NUM_LIT:1>)[<NUM_LIT:0>]<EOL>try:<EOL><INDENT>int(code)<EOL><DEDENT>except:<EOL><INDENT>raise NipapError(exc)<EOL><DEDENT>text = unicode(exc).splitlines()[<NUM_LIT:0>].split("<STR_LIT::>", <NUM_LIT:1>)[<NUM_LIT:1>]<EOL>if code == '<STR_LIT>':<EOL><INDENT>raise NipapValueError(text)<EOL><DEDENT>estr = "<STR_LIT>" % exc<EOL>self._logger.error(estr)<EOL>raise NipapError(unicode(exc))<EOL><DEDENT>except psycopg2.IntegrityError as exc:<EOL><INDENT>self._con_pg.rollback()<EOL>if exc.pgcode == "<STR_LIT>":<EOL><INDENT>m = re.match(r'<STR_LIT>', exc.pgerror)<EOL>if m is None:<EOL><INDENT>raise NipapDuplicateError("<STR_LIT>")<EOL><DEDENT>cursor = self._con_pg.cursor()<EOL>cursor.execute("""<STR_LIT>""",<EOL>{ '<STR_LIT>': m.group(<NUM_LIT:1>) })<EOL>column_desc = '<STR_LIT>'<EOL>for desc in cursor:<EOL><INDENT>column_desc = unicode(desc[<NUM_LIT:0>])<EOL><DEDENT>column_value = None<EOL>try:<EOL><INDENT>m = re.match(r'<STR_LIT>', exc.pgerror.splitlines()[<NUM_LIT:1>])<EOL>if m is not None:<EOL><INDENT>column_value = m.group(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>raise NipapDuplicateError("<STR_LIT>" +<EOL>unicode(column_desc) + "<STR_LIT>" +<EOL>unicode(column_value) + "<STR_LIT>")<EOL><DEDENT>raise NipapDuplicateError("<STR_LIT>" +<EOL>unicode(column_desc) +<EOL>"<STR_LIT>")<EOL><DEDENT>self._logger.exception("<STR_LIT>")<EOL>raise NipapError("<STR_LIT>")<EOL><DEDENT>except psycopg2.DataError as exc:<EOL><INDENT>self._con_pg.rollback()<EOL>m = re.search('<STR_LIT>', exc.pgerror)<EOL>if m is not None:<EOL><INDENT>strict_prefix = unicode(IPy.IP(m.group(<NUM_LIT:1>), make_net = True))<EOL>estr = "<STR_LIT>" % (m.group(<NUM_LIT:1>), strict_prefix)<EOL>raise NipapValueError(estr)<EOL><DEDENT>m = re.search('<STR_LIT>', exc.pgerror)<EOL>if m is not None:<EOL><INDENT>if m.group(<NUM_LIT:1>) in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>estr = "<STR_LIT>" % m.group(<NUM_LIT:2>)<EOL><DEDENT>else:<EOL><INDENT>estr = "<STR_LIT>" % (m.group(<NUM_LIT:1>), m.group(<NUM_LIT:2>))<EOL><DEDENT>raise NipapValueError(estr)<EOL><DEDENT>self._logger.exception("<STR_LIT>")<EOL>raise NipapError("<STR_LIT>")<EOL><DEDENT>except psycopg2.Error as exc:<EOL><INDENT>try:<EOL><INDENT>self._con_pg.rollback()<EOL><DEDENT>except psycopg2.Error:<EOL><INDENT>pass<EOL><DEDENT>estr = "<STR_LIT>" % exc<EOL>self._logger.error(estr)<EOL>if callno > <NUM_LIT:0>:<EOL><INDENT>self._logger.error(estr)<EOL>raise NipapError(estr)<EOL><DEDENT>self._logger.info("<STR_LIT>")<EOL>self._connect_db()<EOL>return self._execute(sql, opt, callno + <NUM_LIT:1>)<EOL><DEDENT>except psycopg2.Warning as warn:<EOL><INDENT>self._logger.warning(unicode(warn))<EOL><DEDENT>
Execute query, catch and log errors.