本文整理汇总了Python中sqlalchemy.engine.base.Engine方法的典型用法代码示例。如果您正苦于以下问题:Python base.Engine方法的具体用法?Python base.Engine怎么用?Python base.Engine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.engine.base
的用法示例。
在下文中一共展示了base.Engine方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def init(config: Config) -> Engine:
db = sql.create_engine(config["database"])
Base.metadata.bind = db
for table in (DBPlugin, DBClient):
table.bind(db)
if not db.has_table("alembic_version"):
log = logging.getLogger("maubot.db")
if db.has_table("client") and db.has_table("plugin"):
log.warning("alembic_version table not found, but client and plugin tables found. "
"Assuming pre-Alembic database and inserting version.")
db.execute("CREATE TABLE IF NOT EXISTS alembic_version ("
" version_num VARCHAR(32) PRIMARY KEY"
");")
db.execute("INSERT INTO alembic_version VALUES ('d295f8dcfa64');")
else:
log.critical("alembic_version table not found. "
"Did you forget to `alembic upgrade head`?")
sys.exit(10)
return db
示例2: run_migrations_offline
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
context.configure(
url=meta.engine.url, target_metadata=target_metadata,
literal_binds=True)
with context.begin_transaction():
context.run_migrations()
示例3: run_migrations_online
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
# specify here how the engine is acquired
# engine = meta.engine
raise NotImplementedError("Please specify engine connectivity here")
with engine.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
示例4: _get_table
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def _get_table(self, connection, table_name, schema=None):
if isinstance(connection, Engine):
connection = connection.connect()
project, dataset, table_name_prepared = self._split_table_name(table_name)
if dataset is None:
if schema is not None:
dataset = schema
elif self.dataset_id:
dataset = self.dataset_id
table = connection.connection._client.dataset(dataset, project=project).table(table_name_prepared)
try:
t = connection.connection._client.get_table(table)
except NotFound as e:
raise NoSuchTableError(table_name)
return t
示例5: get_table_names
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def get_table_names(self, connection, schema=None, **kw):
if isinstance(connection, Engine):
connection = connection.connect()
datasets = connection.connection._client.list_datasets()
result = []
for d in datasets:
if schema is not None and d.dataset_id != schema:
continue
if self.dataset_id is not None and d.dataset_id != self.dataset_id:
continue
tables = connection.connection._client.list_tables(d.reference)
for t in tables:
if self.dataset_id is None:
table_name = d.dataset_id + '.' + t.table_id
else:
table_name = t.table_id
result.append(table_name)
return result
示例6: _create_database
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def _create_database(engine: "Engine", db: Text):
"""Create database `db` on `engine` if it does not exist."""
import psycopg2
conn = engine.connect()
cursor = conn.connection.cursor()
cursor.execute("COMMIT")
cursor.execute(f"SELECT 1 FROM pg_catalog.pg_database WHERE datname = '{db}'")
exists = cursor.fetchone()
if not exists:
try:
cursor.execute(f"CREATE DATABASE {db}")
except psycopg2.IntegrityError as e:
logger.error(f"Could not create database '{db}': {e}")
cursor.close()
conn.close()
示例7: select_star
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def select_star( # pylint: disable=too-many-arguments
cls,
database: "Database",
table_name: str,
engine: Engine,
schema: Optional[str] = None,
limit: int = 100,
show_cols: bool = False,
indent: bool = True,
latest_partition: bool = True,
cols: Optional[List[Dict[str, Any]]] = None,
) -> str:
return super( # pylint: disable=bad-super-call
PrestoEngineSpec, cls
).select_star(
database,
table_name,
engine,
schema,
limit,
show_cols,
indent,
latest_partition,
cols,
)
示例8: get_db_engine
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def get_db_engine(cls,
dialect: str,
params: dict) -> Engine:
# Fallback to `sqlite` if dialect is None
if dialect is None:
dialect = "sqlite"
if "sqlite" in dialect:
db_path = params.get("db_path")
return create_engine(f"{dialect}:///{db_path}")
else:
username = params.get("db_username")
password = params.get("db_password")
host = params.get("db_host")
port = params.get("db_port")
db_name = params.get("db_name")
return create_engine(f"{dialect}://{username}:{password}@{host}:{port}/{db_name}")
示例9: __init__
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def __init__(self, client: 'MaubotMatrixClient', loop: AbstractEventLoop, http: ClientSession,
instance_id: str, log: Logger, config: Optional['BaseProxyConfig'],
database: Optional[Engine], webapp: Optional['PluginWebApp'],
webapp_url: Optional[str]) -> None:
self.client = client
self.loop = loop
self.http = http
self.id = instance_id
self.log = log
self.config = config
self.database = database
self.webapp = webapp
self.webapp_url = URL(webapp_url) if webapp_url else None
self._handlers_at_startup = []
示例10: create
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def create(self, *args, **kwargs):
"""Given arguments, returns a new Engine instance."""
raise NotImplementedError()
示例11: get_schema_names
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def get_schema_names(self, connection, **kw):
if isinstance(connection, Engine):
connection = connection.connect()
datasets = connection.connection._client.list_datasets()
if self.dataset_id is not None:
return [d.dataset_id for d in datasets if d.dataset_id == self.dataset_id]
else:
return [d.dataset_id for d in datasets]
示例12: list
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def list(self):
"show all alias/engines"
engines = []
for row in self.session.query(self.Engines):
engine = {
'Alias': row.alias,
'Engine': row.engine
}
engines.append(engine)
return pd.DataFrame(engines)
示例13: latest_engine
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def latest_engine(self) -> Engine:
record = self.session.query(self.Engines).order_by(desc(self.Engines.dt)).limit(1).first()
if record:
engine = record.engine
return create_engine(engine)
示例14: get_engine
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def get_engine(self, engine_var: str, session_engine: bool or Engine=False, as_binary: bool=False):
if engine_var:
if engine_var not in self.db_info:
engine = create_engine(engine_var) #new engines
self.add_engine(engine)
else:
engine = create_engine(self.db_info[engine_var]) #engine lookup
else:
engine = session_engine or self.latest_engine
return engine
示例15: add_engine
# 需要导入模块: from sqlalchemy.engine import base [as 别名]
# 或者: from sqlalchemy.engine.base import Engine [as 别名]
def add_engine(self, engine: Engine, alias: str=None) -> None:
if isinstance(engine, str):
engine = make_url(engine)
else:
engine = engine.url
host = engine.host
db = engine.database
engine_str = str(engine)
engine_exists_check = self.session.query(self.Engines).filter_by(db=db, host=host, engine=engine_str).first()
if engine_exists_check: return None
self.session.add(self.Engines(db=db, host=host, engine=engine_str, alias=alias))
self.session.commit()