Exemple #1
0
 def save(self, *args, **kwargs):
     if self.session is not None and self.session.school_id != self.school_id:
         raise IntegrityError(
             '%s: session should belong to the same school, but %s != %s' %
             (self.__class__.__name__, self.session.school, self.school))
     if self.parallel is not None and self.parallel.school_id != self.school_id:
         raise IntegrityError(
             '%s: parallel should belong to the same school, but %s != %s' %
             (self.__class__.__name__, self.parallel.school, self.school))
     if (self.session is not None and self.parallel is not None
             and not self.parallel.sessions.filter(
                 id=self.session_id).exists()):
         raise IntegrityError(
             '%s: parallel %s doesn\'t exist in session %s' %
             (self.__class__.__name__, self.parallel, self.school))
     super().save(*args, **kwargs)
Exemple #2
0
    def _pending_to_persistent(session: Session, instance: Any) -> None:
        """Called when a SQLAlchemy object transitions to a persistent object. If this function throws, the session
        will be rolled back and that object will not be committed."""
        if not isinstance(instance, DirectIngestIngestFileMetadata):
            return

        results = (session.query(DirectIngestIngestFileMetadata).filter_by(
            is_invalidated=False,
            is_file_split=False,
            region_code=instance.region_code,
            file_tag=instance.file_tag,
            ingest_database_name=instance.ingest_database_name,
            datetimes_contained_lower_bound_exclusive=instance.
            datetimes_contained_lower_bound_exclusive,
            datetimes_contained_upper_bound_inclusive=instance.
            datetimes_contained_upper_bound_inclusive,
        ).all())

        if len(results) > 1:
            raise IntegrityError(
                f"Attempting to commit repeated non-file split DirectIngestIngestFileMetadata row for "
                f"region_code={instance.region_code}, file_tag={instance.file_tag}, "
                f"ingest_database_name={instance.ingest_database_name}",
                f"datetimes_contained_lower_bound_exclusive={instance.datetimes_contained_lower_bound_exclusive}, "
                f"datetimes_contained_upper_bound_inclusive={instance.datetimes_contained_upper_bound_inclusive}",
            )
Exemple #3
0
    def validateForeignKeys(self) -> None:
        """
        Validate foreign key constraints.
        Raise :exc:`IntegrityError` if there is a constraint violation.
        """
        valid = True

        for referent, rowid, referred, constraint in self.execute(
            "pragma foreign_key_check"
        ):
            row = self.execute(
                f"select * from {referent} where ROWID=:rowid",
                dict(rowid=rowid),
            ).fetchone()
            self._log.critical(
                "Foreign key constraint {constraint} violated by "
                "table {referent}, row {rowid} to table {referred}\n"
                "Row: {row}",
                referent=referent,
                rowid=rowid,
                referred=referred,
                constraint=constraint,
                row={k: row[k] for k in row.keys()},
            )

            valid = False

        if not valid:
            raise IntegrityError("Foreign key constraints violated")
Exemple #4
0
    def change_metadata(self, c, new_slug, new_title, unlisted):
        new_slug = new_slug.strip()
        if not is_valid_slug(new_slug):
            raise IntegrityError('Invalid slug')

        new_title = new_title.strip()
        if not new_title:
            raise IntegrityError('You need a title')

        unlisted = 1 if unlisted else 0

        c.execute(
            """
        UPDATE toc SET slug = ?, title = ?, mtime = ?, unlisted = ?
        WHERE id = ?
        """, (new_slug, new_title, int(time.time()), unlisted, self.page_id))
 def fetch_row_id(self,
                  *,
                  table: str,
                  column: str = 'name',
                  value: Any) -> int:
     result_cursor: Cursor = self._execute(f"FETCH_ROW_ID_{table.upper()}",
                                           value)
     result_list: list = result_cursor.fetchall()
     if not len(result_list):
         raise IntegrityError(
             f"No value for '{value}' in column '{column}' of table '{table}' found!"
         )
     if len(result_list) == 1:
         return result_list[0][0]
     else:
         raise IntegrityError(
             f"Multiple values for '{value}' in column '{column}' of table '{table}' found!"
         )
Exemple #6
0
 def run(self, *args, **kwargs):
     if self.closed:
         raise DatabaseException('Connection closed', '')
     with global_lock:
         if self.proxy.unhandled_error[0] is not None:
             raise DatabaseException(*self.proxy.unhandled_error)
         self.proxy.requests.put((fn.__name__, args, kwargs))
         ok, res = self.proxy.results.get()
         if not ok:
             if isinstance(res[0], IntegrityError):
                 raise IntegrityError(unicode_type(res[0]))
             raise DatabaseException(*res)
         return res
Exemple #7
0
    def _check_is_front(self, c, paragraph_id):
        c.execute(
            """
        SELECT first_content_id FROM toc
        WHERE id = ? AND
        (SELECT parent_id FROM content where id = ?) = ?
        """, (self.page_id, paragraph_id, self.page_id))
        row = c.fetchone()
        # also checks if row exists, and the if the parent is correct
        if not row:
            raise IntegrityError('Row DNE')

        return row[0] == paragraph_id
Exemple #8
0
    def _insert(self, c, after_content_id, content):
        # check if id exists
        c.execute('SELECT parent_id, next_id FROM content WHERE id = ?',
                  (after_content_id, ))
        row = c.fetchone()
        if not row:
            raise IntegrityError('after_content_id not found')

        c.execute(
            """
        INSERT INTO content (parent_id, next_id, content) VALUES
        (?, ?, ?)
        """, (row[0], row[1], content))

        c.execute(
            """
        UPDATE content SET next_id = last_insert_rowid()
        WHERE id = ?
        """, (after_content_id, ))
Exemple #9
0
def _set_error(rc, errmsg=None):
    throw()

    if rc == SQLITE_OK:
        return
    if errmsg is None:
        errmsg = sqlite3_errstr(rc).decode()

    if rc in (
            SQLITE_INTERNAL,
            SQLITE_NOTFOUND):
        raise InteralError(errmsg)
    elif rc == SQLITE_NOMEM:
        raise MemoryError()
    elif rc in (
            SQLITE_ERROR,
            SQLITE_PERM,
            SQLITE_ABORT,
            SQLITE_BUSY,
            SQLITE_LOCKED,
            SQLITE_READONLY,
            SQLITE_INTERRUPT,
            SQLITE_IOERR,
            SQLITE_FULL,
            SQLITE_CANTOPEN,
            SQLITE_PROTOCOL,
            SQLITE_EMPTY,
            SQLITE_SCHEMA,
    ):
        raise OperationalError(errmsg)
    elif rc == SQLITE_CORRUPT:
        raise DatabaseError(errmsg)
    elif rc == SQLITE_TOOBIG:
        raise DataError(errmsg)
    elif rc in (SQLITE_CONSTRAINT, SQLITE_MISMATCH):
        raise IntegrityError(errmsg)
    elif rc == SQLITE_MISUSE:
        raise ProgrammingError(errmsg)
    else:
        raise DatabaseError(errmsg)
Exemple #10
0
    def validateForeignKeys(self) -> None:
        valid = True

        for referent, rowid, referred, constraint in (
                self.execute("pragma foreign_key_check")):
            row = self.execute(
                "select * from {} where ROWID=:rowid".format(referent),
                dict(rowid=rowid)).fetchone()
            self._log.critical(
                "Foreign key constraint {constraint} violated by "
                "table {referent}, row {rowid} to table {referred}\n"
                "Row: {row}",
                referent=referent,
                rowid=rowid,
                referred=referred,
                constraint=constraint,
                row={k: row[k]
                     for k in row.keys()},
            )

            valid = False

        if not valid:
            raise IntegrityError("Foreign key constraints violated")
Exemple #11
0
 def execute(self, foo, bar):
     raise IntegrityError('123456')
Exemple #12
0
def add_metadata(xml_fh, db_fp):
    '''Add to the database the metadata of records from the UniProt xml file.

    Parameters
    ----------
    xml_fh : file object
        The file of either UniProtKB Swiss-Prot or TrEMBLE.
    db_fp : str
        The output database file. See ``Notes``.

    Returns
    -------
    int
        The number of records processed.

    '''
    logger.info('Adding UniProt metadata to db from %r' % xml_fh)

    # this is the namespace for uniprot xml files.
    ns_map = {
        'xmlns': 'http://uniprot.org/uniprot',
        'xsi': 'http://WWW.w3.org/2001/XMLSchema-instance'
    }
    entry_tag = '{{{ns}}}{tag}'.format(ns=ns_map['xmlns'], tag='entry')
    paths = {
        'EC_number': './/xmlns:ecNumber',  # E.C. number
        'GO': './xmlns:dbReference[@type="GO"]',  # GO
        'KEGG': './xmlns:dbReference[@type="KEGG"]',  # KEGG,
        'Pfam': './xmlns:dbReference[@type="Pfam"]',
        'eggNOG': './xmlns:dbReference[@type="eggNOG"]',
        'TIGRFAM': './xmlns:dbReference[@type="TIGRFAMs"]'
    }
    inserts = {}
    with connect(db_fp) as conn:
        c = conn.cursor()
        # The INTEGER PRIMARY KEY column created is simply an
        # alias for ROWID or _ROWID_ or OID.
        # You can't ignore this column because ROWID can't server
        # as foreign key.
        c.execute('CREATE TABLE IF NOT EXISTS UniProt ('
                  ' id   INTEGER PRIMARY KEY,'
                  ' accn TEXT  UNIQUE,'
                  ' name TEXT  NOT NULL);')
        insert = 'INSERT OR IGNORE INTO UniProt (id, accn, name) VALUES (?,?,?);'

        for other_table in paths:
            ct, it, clt, ilt = _cross_ref_table(other_table)
            c.execute(ct)
            c.execute(clt)
            inserts[other_table] = [it, ilt]
        for n, entry in enumerate(_parse_xml(xml_fh, entry_tag), 1):
            try:
                # get the primary accession number
                accn = entry.find('./xmlns:accession', ns_map).text
                # get the protein product name
                name = entry.find('.//xmlns:fullName', ns_map).text
            except AttributeError as e:
                # customize with more informative error msg
                raise AttributeError(
                    'failed to get accession and name for record %d' %
                    n) from e

            try:
                # `None` to increment the id column automatically
                c.execute(insert, (None, accn, name))
            except IntegrityError as e:
                raise IntegrityError('failed to insert {}'.format(
                    (accn, name))) from e

            # get the ID for UniProt entry just inserted into the table
            uniprot_id = c.execute('SELECT id FROM UniProt WHERE accn = ?;',
                                   (accn, )).fetchone()[0]

            for other_table, path in paths.items():
                it, ilt = inserts[other_table]
                select = 'SELECT id FROM %s WHERE accn = ?;' % other_table
                for elem in entry.findall(path, ns_map):
                    if other_table == 'EC_number':
                        other_accn = elem.text
                    else:
                        other_accn = elem.attrib['id']
                    c.execute(it, (None, other_accn, None))
                    other_table_id = c.execute(select,
                                               (other_accn, )).fetchone()[0]
                    c.execute(ilt, (uniprot_id, other_table_id))

        conn.commit()
    return n
Exemple #13
0
def raise_sql_error(_req):
    """Raise a sql error"""
    raise IntegrityError(ERROR_MESSAGE_2)