Esempio n. 1
0
def insert_rows(logger, rows, commit, one_by_one, precommit_sql):
    if not os.environ.get('SUPPRESS_LOGGING'):
        import logging
        sl = logging.getLogger('sqlalchemy.engine')
        formatter = logging.Formatter("%(message)s")
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(logging.INFO)
        ch.setFormatter(formatter)
        sl.addHandler(ch)
        sl.setLevel(logging.INFO)

    from geoalchemy import WKTSpatialElement
    to_commit = []
    for row in rows:
        j = Jurisdiction(
            name=unicode(row['name']),
            uri=unicode(row['uri']),
            type_uri=unicode(row['type_uri']),
            geom=WKTSpatialElement(row['geom']),
            properties=row.get('properties'),
            )
        if not commit:
            logger.debug('Adding Jurisdiction %r', j)
            session.add_all([j])
        else:
            if one_by_one:
                logger.debug('Immediately committing %r', j)
                session.add_all([j])
                session.commit()
            else:
                logger.debug('Deferring Jurisdiction %r', j)
                to_commit.append(j)


    # Need to add everything and force a flush here so the pre-commit
    # raw SQL expressions run in the right context.
    if to_commit and commit:
        session.add_all(to_commit)
    session.flush()

    from sqlalchemy.sql import text
    for sql_expressions in precommit_sql:
        if isinstance(sql_expressions, basestring):
            sql_expressions = [sql_expressions]
        for sql in sql_expressions:
            logger.notify("Running pre-commit sql expression: %r" % sql)
            session.connection().execute(text(sql))

    if to_commit and commit:
        logger.notify('COMMIT')
        session.commit()
    elif not commit:
        logger.notify('ROLLBACK')
        session.rollback()
Esempio n. 2
0
def reset_types(logger, types):
    for type in types:
        q = session.query(Jurisdiction).filter(
            Jurisdiction.type_uri == unicode(type))
        count = q.count()
        if not count:
            logger.notify('No items with type %s to delete' % type)
        else:
            logger.notify('Deleting all (%s) items with type %s' % (count, type))
            q.delete()
            session.commit()
Esempio n. 3
0
def insert_rows(logger, rows, commit, one_by_one, precommit_sql):
    if not os.environ.get('SUPPRESS_LOGGING'):
        import logging
        sl = logging.getLogger('sqlalchemy.engine')
        formatter = logging.Formatter("%(message)s")
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(logging.INFO)
        ch.setFormatter(formatter)
        sl.addHandler(ch)
        sl.setLevel(logging.INFO)

    from geoalchemy import WKTSpatialElement
    to_commit = []
    for row in rows:
        j = Jurisdiction(
            name=unicode(row['name']),
            uri=unicode(row['uri']),
            type_uri=unicode(row['type_uri']),
            geom=WKTSpatialElement(row['geom']),
            properties=row.get('properties'),
        )
        if not commit:
            logger.debug('Adding Jurisdiction %r', j)
            session.add_all([j])
        else:
            if one_by_one:
                logger.debug('Immediately committing %r', j)
                session.add_all([j])
                session.commit()
            else:
                logger.debug('Deferring Jurisdiction %r', j)
                to_commit.append(j)

    # Need to add everything and force a flush here so the pre-commit
    # raw SQL expressions run in the right context.
    if to_commit and commit:
        session.add_all(to_commit)
    session.flush()

    from sqlalchemy.sql import text
    for sql_expressions in precommit_sql:
        if isinstance(sql_expressions, basestring):
            sql_expressions = [sql_expressions]
        for sql in sql_expressions:
            logger.notify("Running pre-commit sql expression: %r" % sql)
            session.connection().execute(text(sql))

    if to_commit and commit:
        logger.notify('COMMIT')
        session.commit()
    elif not commit:
        logger.notify('ROLLBACK')
        session.rollback()
Esempio n. 4
0
def reset_types(logger, types):
    for type in types:
        q = session.query(Jurisdiction).filter(
            Jurisdiction.type_uri == unicode(type))
        count = q.count()
        if not count:
            logger.notify('No items with type %s to delete' % type)
        else:
            logger.notify('Deleting all (%s) items with type %s' %
                          (count, type))
            q.delete()
            session.commit()