def connect(name, type='extract_from_name', create_indices=True, use_lock_file=False): if type == 'extract_from_name': if name is None: type = None elif name.startswith('postgres://'): type = 'postgres' else: type = os.path.splitext(name)[1][1:] if type is None: return NoDatabase() if type == 'json': from ase.db.json 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 == 'postgres': from ase.db.postgresql import PostgreSQLDatabase as DB return PostgreSQLDatabase(name, create_indices) raise ValueError('Unknown database type: ' + type)
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)
def _initialize(self, con): if self.initialized: return import ase from ase.db.sqlite import SQLite3Database SQLite3Database()._initialize(con) self._metadata = {} cur = con.execute( 'SELECT COUNT(*) FROM sqlite_master WHERE name="catapp"') if cur.fetchone()[0] > 0: self.update_names(con) cur = con.execute( 'SELECT COUNT(*) FROM sqlite_master WHERE name="reaction"') if cur.fetchone()[0] == 0: for init_command in init_commands: con.execute(init_command) self.id = 1 self.pid = 1 con.commit() self.initialized = True
def _initialize(self, con): """Set up tables in SQL""" if self.initialized: return SQLite3Database()._initialize(con) # ASE db initialization cur = con.execute( 'SELECT COUNT(*) FROM sqlite_master WHERE name="reaction"') if cur.fetchone()[0] == 0: # no reaction table for init_command in init_commands: con.execute(init_command) # Create tables con.commit() self.initialized = True
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)
def _initialize(self, con): if self.initialized: return SQLite3Database()._initialize(con) self._metadata = {} cur = con.execute( 'SELECT COUNT(*) FROM sqlite_master WHERE name="reaction"') if cur.fetchone()[0] == 0: for init_command in init_commands: con.execute(init_command) self.id = 1 self.pid = 1 con.commit() if self.id is None: self.id = self.get_last_id(cur) + 1 self.pid = self.get_last_pub_id(cur) + 1 self.initialized = True