Esempio n. 1
0
    def _initialize(self, con):
        if self.initialized:
            return
        cur = con.cursor()

        self.stdout.write("_initialize start\n")

        set_schema = 'ALTER ROLE {0} SET search_path TO {1};'\
                     .format(self.user, self.schema)
        cur.execute(set_schema)
        con.commit()

        self.stdout.write(
            "_initialize set schema to {self.schema}\n".format(**locals()))

        PostgreSQLDatabase()._initialize(con)

        cur.execute("""SELECT to_regclass('publication');""")
        if cur.fetchone()[0] is None:  # publication doesn't exist
            self.stdout.write("_initialize create tables:\n")
            for init_command in init_commands:
                self.stdout.write(init_command + '\n')
                cur.execute(init_command)
            self.stdout.write("_initialize create indexes:\n")
            for statement in index_statements:
                self.stdout.write(statement + '\n')
                cur.execute(statement)
            self.stdout.write("_initialize create text search columns:\n")
            for statement in tsvector_statements:
                self.stdout.write(statement + '\n')
                cur.execute(statement)
            con.commit()
        self.initialized = True
        return self
Esempio n. 2
0
    def _initialize(self, con):
        if self.initialized:
            return
        cur = con.cursor()

        self.stdout.write("_initialize start\n")

        set_schema = 'SET search_path = {0};'.format(self.schema)
        cur.execute(set_schema)

        self.stdout.write(
            "_initialize set schema to {self.schema}\n".format(**locals()))

        from ase.db.postgresql import PostgreSQLDatabase
        PostgreSQLDatabase()._initialize(con)

        cur.execute("""SELECT to_regclass('publication');""")
        if cur.fetchone()[0] is None:  # publication doesn't exist
            for init_command in init_commands:
                self.stdout.write(init_command + '\n')
                cur.execute(init_command)
            for statement in index_statements:
                self.stdout.write(statement + '\n')
                cur.execute(statement)
            for statement in tsvector_statements:
                self.stdout(statement + '\n')
                cur.execute(statement)
            con.commit()
        self.initialized = True
        return self
Esempio n. 3
0
def connect(name,
            type='extract_from_name',
            create_indices=True,
            use_lock_file=True,
            append=True,
            serial=False):
    """Create connection to database.

    name: str
        Filename or address of database.
    type: str
        One of 'json', 'db', 'postgresql',
        (JSON, SQLite, PostgreSQL).
        Default is 'extract_from_name', which will guess the type
        from the name.
    use_lock_file: bool
        You can turn this off if you know what you are doing ...
    append: bool
        Use append=False to start a new database.
    """

    if isinstance(name, PurePath):
        name = str(name)

    if type == 'extract_from_name':
        if name is None:
            type = None
        elif not isinstance(name, basestring):
            type = 'json'
        elif (name.startswith('postgresql://')
              or name.startswith('postgres://')):
            type = 'postgresql'
        else:
            type = os.path.splitext(name)[1][1:]
            if type == '':
                raise ValueError('No file extension or database type given')

    if type is None:
        return Database()

    if not append and world.rank == 0:
        if isinstance(name, str) and os.path.isfile(name):
            os.remove(name)

    if type != 'postgresql' and isinstance(name, basestring):
        name = os.path.abspath(name)

    if type == 'json':
        from ase.db.jsondb import JSONDatabase
        return JSONDatabase(name, use_lock_file=use_lock_file, serial=serial)
    if type == 'db':
        from ase.db.sqlite import SQLite3Database
        return SQLite3Database(name,
                               create_indices,
                               use_lock_file,
                               serial=serial)
    if type == 'postgresql':
        from ase.db.postgresql import PostgreSQLDatabase
        return PostgreSQLDatabase(name)
    raise ValueError('Unknown database type: ' + type)
Esempio n. 4
0
def connect(name,
            type='extract_from_name',
            create_indices=True,
            use_lock_file=True):
    """Create connection to database.
    
    name: str
        Filename or address of database.
    type: str
        One of 'json', 'db', 'postgresql', 'mysql'
        (JSON, SQLite, PostgreSQL, MySQL/MariaDB).
        Default is 'extract_from_name', which will ... guess the type
        from the name.
    use_lock_file: bool
        You can turn this off if you know what you are doing ...
        """

    if type == 'extract_from_name':
        if name is None:
            type = None
        elif name.startswith('postgresql://'):
            type = 'postgresql'
        elif name.startswith('mysql://'):
            type = 'mysql'
        else:
            type = os.path.splitext(name)[1][1:]

    if type is None:
        return Database()

    if type == 'json':
        from ase.db.jsondb import JSONDatabase
        return JSONDatabase(name, use_lock_file=use_lock_file)
    if type == 'db':
        from ase.db.sqlite import SQLite3Database
        return SQLite3Database(name, create_indices, use_lock_file)
    if type == 'postgresql':
        from ase.db.postgresql import PostgreSQLDatabase
        return PostgreSQLDatabase(name[5:])
    if type == 'mysql':
        from ase.db.mysql import MySQLDatabase
        return MySQLDatabase(name[5:])
    raise ValueError('Unknown database type: ' + type)