async def test_results_support_mapping_interface(database_url): """ Casting results to a dict should work, since the interface defines them as supporting the mapping interface. """ async with Database(database_url) as database: async with database.transaction(force_rollback=True): # execute() query = notes.insert() values = {"text": "example1", "completed": True} await database.execute(query, values) # fetch_all() query = notes.select() results = await database.fetch_all(query=query) results_as_dicts = [dict(item) for item in results] assert len(results[0]) == 3 assert len(results_as_dicts[0]) == 3 assert isinstance(results_as_dicts[0]["id"], int) assert results_as_dicts[0]["text"] == "example1" assert results_as_dicts[0]["completed"] == True
async def sqlalchemy_user_db_oauth( ) -> AsyncGenerator[SQLAlchemyUserDatabase, None]: Base: DeclarativeMeta = declarative_base() class User(SQLAlchemyBaseUserTable, Base): first_name = Column(String, nullable=True) class OAuthAccount(SQLAlchemyBaseOAuthAccountTable, Base): pass DATABASE_URL = "sqlite:///./test-sqlalchemy-user-oauth.db" database = Database(DATABASE_URL) engine = sqlalchemy.create_engine( DATABASE_URL, connect_args={"check_same_thread": False}) Base.metadata.create_all(engine) await database.connect() yield SQLAlchemyUserDatabase(UserDBOAuth, database, User.__table__, OAuthAccount.__table__) Base.metadata.drop_all(engine)
async def test_decimal_field(database_url): """ Test Decimal (NUMERIC) columns, to ensure records are coerced to/from proper Python types. """ async with Database(database_url) as database: async with database.transaction(force_rollback=True): price = decimal.Decimal("0.700000000000001") # execute() query = prices.insert() values = {"price": price} await database.execute(query, values) # fetch_all() query = prices.select() results = await database.fetch_all(query=query) assert len(results) == 1 if database_url.startswith("sqlite"): # aiosqlite does not support native decimals --> a roud-off error is expected assert results[0]["price"] == pytest.approx(price) else: assert results[0]["price"] == price
async def add_resource_history( db: Database, record: typing.Dict[str, typing.Any], first: bool = True ): """ """ data = record.copy() data["txid"] = data.pop("id") data["prev_id"] = None del data["es_id"] if first is False: previous_pk = await db.fetch_val( ( f"SELECT id FROM {ResourceHistoryModel.__tablename__} " "WHERE txid=:txid ORDER BY timestamp DESC LIMIT 1" ), values={"txid": data["txid"]}, column="id", ) if previous_pk: data["prev_id"] = previous_pk async with db.transaction(): query1 = ( f"INSERT INTO {ResourceHistoryModel.__tablename__}" "(txid, resource_id, prev_id, status, resource_type, " "resource_version, resource, fhir_version, timestamp) " "VALUES (:txid, :resource_id, :prev_id, :status, :resource_type, " ":resource_version, :resource, :fhir_version, :timestamp) RETURNING id" ) pk = await db.execute(query1, values=data) if data["prev_id"]: query2 = ( f"UPDATE {ResourceHistoryModel.__tablename__} " "SET next_id=:next_id " "WHERE id=:id" ) await db.execute(query2, values={"next_id": pk, "id": data["prev_id"]})
class DatabaseConnectionLayer: Base: ClassVar = declarative_base( cls=Base, metadata=MetaData( naming_convention={ "ix": "ix_%(column_0_label)s", "uq": "uq_%(table_name)s_%(column_0_name)s", "ck": "ck_%(table_name)s_%(constraint_name)s", "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", "pk": "pk_%(table_name)s", } ), ) engine: ClassVar = create_engine(get_config().database_uri) Session: ClassVar = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=engine) ) async_db: ClassVar[Database] = Database(get_config().database_uri) def __setattr__(self, *args): raise TypeError("Values are immutable!") def __delattr__(self, *args): raise TypeError("Values cannot be deleted!")
def install_bot(): logging.basicConfig(level=logging.INFO) bot = Bot(token=TELEGRAM_API_TOKEN) dp = Dispatcher(bot) database = Database(DB_URL) main_state = AiogramMainRouter(dp) dp.middleware.setup( LocaleMiddleware(f"{os.path.split(dir_path)[0]}/app/localization")) dp.middleware.setup(AntiSpamMiddleware(limit=30, delay=30)) main_state.container.bind_constant(Database, database) main_state.container.bind(SettingsRepository, SettingsRepository) main_state.container.bind(UserRepository, UserRepository) main_state.container.bind(BusStopRepository, BusStopRepository) main_state.container.bind(BusStopService, BusStopService) main_state.include_router(start_state) main_state.include_router(other_state) main_state.include_router(settings_state) main_state.install(default_state_name="start", storage=MemoryStorage()) return dp, database
async def test_iterate_outside_transaction_with_temp_table(database_url): """ Same as test_iterate_outside_transaction_with_values but uses a temporary table instead of a list of values. """ database_url = DatabaseURL(database_url) if database_url.dialect == "sqlite": pytest.skip("SQLite interface does not work with temporary tables.") async with Database(database_url) as database: query = "CREATE TEMPORARY TABLE no_transac(num INTEGER)" await database.execute(query) query = "INSERT INTO no_transac(num) VALUES (1), (2), (3), (4), (5)" await database.execute(query) query = "SELECT * FROM no_transac" iterate_results = [] async for result in database.iterate(query=query): iterate_results.append(result) assert len(iterate_results) == 5
async def migrate(db: Database, src_dir: Path = Path(__file__).absolute().parent / 'migrations', namespace: str = 'default'): """ Apply forward migration on database. :param db: async database connection :param src_dir: source directory with *.sql files, ordered by name (ex: 0001_abc.sql, 0002_def.sql) :param namespace: migration namespace (useful if several projects are using same db) """ logger = getLogger("db-migration") await db.execute(''' CREATE TABLE IF NOT EXISTS _migration ( name TEXT NOT NULL, namespace TEXT NOT NULL, PRIMARY KEY(name,namespace) )''') row = await db.fetch_one( 'SELECT name FROM _migration WHERE namespace = :namespace ORDER BY name DESC LIMIT 1', values={"namespace": namespace}) for file in sorted(src_dir.glob("*.sql")): if row is not None and file.name <= row[0]: logger.info("skipping %s", file.name) continue async with db.transaction(): logger.info("applying migration from %s", file.name) for statement in file.read_text().split(';'): logger.info("applying: %s", statement) await db.execute(statement) await db.execute( 'INSERT INTO _migration(name, namespace) VALUES(:name, :namespace)', values={ 'name': file.name, 'namespace': namespace }) logger.info("migration complete, namespace = %s", namespace)
async def test_add_project(tmpdir): await create_and_populate_test_db(tmpdir) db_path = f"sqlite:///{tmpdir / 'alfie.db'}" app.config['DATABASE'] = db_path client = app.test_client() response = await client.post('/create', form={'name': 'cuckoo'}) page = await response.get_data() assert b'<title>Redirect</title>' in page query = 'SELECT ProjectId FROM Project WHERE Name = "cuckoo"' async with Database(db_path) as db: ret = await db.fetch_val(query) assert ret == 7 response = await client.get('/index') page = (await response.get_data()).decode() assert '<div id="collapse-cuckoo"' in page assert '<div class="card-header" id="heading-cuckoo">' in page assert '<button id="cuckoo_export"' not in page assert '<button id="cuckoo_plus"' in page await client.post('/create', form={'name': 'cuckoo'}) response = await client.get('/index') page = (await response.get_data()).decode() assert 'This project already exists' in page await client.post('/create') response = await client.get('/index') page = (await response.get_data()).decode() assert 'Did not specify a project name' in page
async def get_data(query): db = Database(META_URL) await db.connect() res = await db.fetch_all(query2sql(query)) await db.disconnect() return res
users = Table( "users", metadata, Column( "id", UUID, primary_key=True, default=lambda: uuid4().hex, unique=True, ), Column("username", String), Column("hashed_password", String), Column("is_disabled", Boolean), Column( "created_at", TIMESTAMP(timezone=True), server_default=func.now(), nullable=False, ), Column( "updated_at", TIMESTAMP(timezone=True), server_default=func.now(), nullable=False, onupdate=func.now(), ), ) # databases query builder database = Database(str(DATABASE_URL))
class SQLiteDatasource(object): create_tbl_sub_tasks = """ CREATE TABLE IF NOT EXISTS Tasks ( id INTEGER PRIMARY KEY AUTOINCREMENT, uuid VARCHAR(100) NOT NULL UNIQUE, type VARCHAR(100) NOT NULL, url VARCHAR(1000), status INTEGER, profile VARCHAR(1000), submitted_ts VARCHAR(100) ) """ def __init__(self, db_path: str): self.database = Database(db_path) @staticmethod def _row_to_download_task( row: Optional[Mapping]) -> Optional[DownloadTask]: if row is not None: uuid = row["uuid"] url = row["url"] task_type = row["type"] profile = row["profile"] submitted_ts = row["submitted_ts"] status = TaskStatus(row["status"]) submitted_task = SubmittedTaskModel.create(url, uuid, submitted_ts, status, profile) if task_type == repr(TaskType.YTDL): return YTDLDownloadTask(submitted_task) else: return None else: return None async def connect(self): await self.database.connect() async def initialize(self): await self.database.execute(query=self.create_tbl_sub_tasks) async def disconnect(self): await self.database.disconnect() async def put(self, task: DownloadTask): task_type = repr(task.type) query = """ INSERT INTO Tasks(uuid, type, url, profile, submitted_ts, status) VALUES (:uuid, :type, :url, :profile, :submitted_ts, :status) """ params = { "uuid": task.submitted_task.uuid, "type": task_type, "url": task.submitted_task.url, "submitted_ts": task.submitted_task.submitted_ts, "profile": task.submitted_task.profile, "status": task.submitted_task.status } await self.database.execute(query, params) async def get(self) -> Optional[DownloadTask]: select_query = """ SELECT * FROM Tasks WHERE status = {} ORDER BY id ASC LIMIT 1 """.format(TaskStatus.Created) update_query = """ UPDATE Tasks SET status = {} WHERE id = :id """.format(TaskStatus.Processing) async with self.database.transaction(): row = await self.database.fetch_one(select_query, None) if row is not None: param = {"id": row["id"]} await self.database.execute(update_query, param) task = self._row_to_download_task(row) if task is not None: task.submitted_task.status = TaskStatus.Processing return task else: return None async def retry_processed(self): update_query = """ UPDATE Tasks SET status = {} WHERE status = {} """.format(TaskStatus.Created, TaskStatus.Processing) await self.database.execute(update_query, None) async def retry(self, _uuid: str): update_query = """ UPDATE Tasks SET status = {} WHERE uuid = :uuid """.format(TaskStatus.Created) params = {"uuid": _uuid} await self.database.execute(update_query, params) async def cleanup(self): delete_query = """ DELETE FROM Tasks WHERE status IN ({}, {}, {}) """.format(TaskStatus.Done, TaskStatus.Cancelled, TaskStatus.Failed) await self.database.execute(delete_query, None) async def set_status(self, uuid: str, status: TaskStatus): update_query = """ UPDATE Tasks SET status = :status WHERE uuid = :uuid """ params = {"status": status, "uuid": uuid} await self.database.execute(update_query, params) async def cancel(self, uuid: str): update_query = """ UPDATE Tasks SET status = {} WHERE uuid = :uuid """.format(TaskStatus.Cancelled) params = {"uuid": uuid} await self.database.execute(update_query, params) async def get_by_uuid(self, uuid: str) -> Optional[DownloadTask]: param = {"uuid": uuid} query = """ SELECT * FROM Tasks WHERE uuid = :uuid """ row = await self.database.fetch_one(query, param) return self._row_to_download_task(row) async def _list_by(self, query) -> List[DownloadTask]: list_rows = await self.database.fetch_all(query, None) maybe_tasks: Iterator[Optional[DownloadTask]] = map( self._row_to_download_task, list_rows) return [task for task in maybe_tasks if task is not None] async def list_all(self) -> List[DownloadTask]: query = "SELECT * FROM Tasks" return await self._list_by(query) async def list_running_or_queued(self) -> List[DownloadTask]: query = "SELECT * FROM Tasks WHERE status IN ({}, {})".format( TaskStatus.Processing, TaskStatus.Created) return await self._list_by(query) async def list_running(self) -> List[DownloadTask]: query = "SELECT * FROM Tasks WHERE status = {}".format( TaskStatus.Processing) return await self._list_by(query) async def list_queued(self) -> List[DownloadTask]: query = "SELECT * FROM Tasks WHERE status = {}".format( TaskStatus.Created) return await self._list_by(query)
from typing import List from databases import Database import sqlalchemy from sqlalchemy.ext.declarative import declarative_base DATABASE_URL = "postgresql://*****:*****@127.0.0.1:5432/test" db_instance = Database(DATABASE_URL) metadata = sqlalchemy.MetaData() engine = sqlalchemy.create_engine(DATABASE_URL) LocalSession = sqlalchemy.orm.sessionmaker(autocommit= False, autoflush= False, bind=engine) Base = declarative_base()
def db(database_url): return Database(database_url)
async def export(): """Export the project for download. This url expect the project name as `project`, and an optional `zip` boolean. If `zip` is set, zip the files from the storage path. External resources (ie. link) are then compiled into an `external_resources.txt` file, itself appended to the archive. Its format will be 'resource_name: resource_location'. If `zip` is unset, then all resources in the project will be listed in a text file in the same format as above, and this text file will be served to the user. """ project_name = request.args.get('project') zip_files = request.args.get('zip', default=True) zip_files = True if zip_files in ['True', True] else False async with Database(app.config['DATABASE']) as db: query = 'SELECT ProjectId FROM Project WHERE Name = :name' project_id = await db.fetch_val(query, {'name': project_name}) query = 'SELECT DocumentId FROM ProjectEntry WHERE ProjectId = :pid' entries = await db.fetch_all(query, {'pid': project_id}) query = ('SELECT Name, Location, Description FROM Document ' 'WHERE DocumentId = :did') coros = [db.fetch_one(query, {'did': did}) for did, in entries] ret = await gather(*coros) documents = {name: _ for (name, *_) in ret} attachment_filename = project_name file_ = io.BytesIO() if zip_files: attachment_filename += '.zip' mimetype = 'application/zip' externals = [] with zipfile.ZipFile(file_, 'w') as zf: for filename, (location, description) in documents.items(): if location.startswith('http'): externals.append((filename, location)) continue # Sanitize the file name fname = filename.replace(' ', '_') fname_ext = fname.split('.')[-1] loc_ext = location.split('.')[-1] if fname_ext != loc_ext: fname = f'{fname}.{loc_ext}' # Write the file try: zf.write(location, fname) except FileNotFoundError: continue # TODO: flash a message # Handle external resources if externals: zf.writestr('external_resources.txt', '\n'.join(f'{n}: {uri}' for n, uri in externals)) else: attachment_filename += '.txt' content = '\n'.join([f'{fname}: {loc}' for fname, (loc, _) in documents.items()]) file_.write(content.encode()) mimetype = 'text/csv' file_.seek(0) ret = await send_file(file_, attachment_filename=attachment_filename, mimetype=mimetype, as_attachment=True) return ret
def __init__(self): location = os.path.join(STORAGE_FOLDER, 'sqlite.db') self.database = Database(f"sqlite:///{location}")
import sys sys.path.append('../musicServer') from databases import Database import sqlalchemy from sqlalchemy import MetaData __database_url = "sqlite:///xiaoma.db" __database = Database(__database_url) __metadata = MetaData() # 歌单列表 table_albums = sqlalchemy.Table( "albums", __metadata, sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True), sqlalchemy.Column("name", sqlalchemy.String(length=64), nullable=False), sqlalchemy.Column("desc", sqlalchemy.String(length=128)), sqlalchemy.Column("owner", sqlalchemy.Integer)) # 歌曲表 table_songs = sqlalchemy.Table( "songs", __metadata, sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True), sqlalchemy.Column("title", sqlalchemy.String(length=64), nullable=False), sqlalchemy.Column("singer", sqlalchemy.String(length=64), nullable=False), sqlalchemy.Column("file", sqlalchemy.String(length=128), unique=True), sqlalchemy.Column("duration", sqlalchemy.Integer), sqlalchemy.Column("netease_id", sqlalchemy.Integer, unique=True)) table_songs_album = sqlalchemy.Table( "songs_album", __metadata, sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
import sqlalchemy as sa from databases import Database from conf import settings __all__ = ( 'db', 'metadata', ) assert settings.DATABASE_URL is not None, 'DATABASE_URL must be provided' db = Database(settings.DATABASE_URL) metadata = sa.MetaData()
import pytest from databases import Database from starlette.testclient import TestClient from src.db import DATABASE_URL from src.db import database from src.main import app test_database = Database(DATABASE_URL, force_rollback=True) @pytest.fixture def client(): app.dependency_overrides[database] = test_database with TestClient(app) as client: yield client app.dependency_overrides = {}
def setup_database(): sanic_app.db = Database(env.str("URLDB"), ssl=False, min_size=1, max_size=20) sanic_app.dialect_db_class = DialectDbPostgresql
Integer, MetaData, String, Table, create_engine, ) from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship from sqlalchemy_to_ormar import ormar_model_str_repr, sqlalchemy_to_ormar Base = declarative_base() Database_URL = "sqlite:///test.db" engine = create_engine(Database_URL) database = Database(Database_URL) metadata = MetaData(engine) association_table = Table( "association", Base.metadata, Column("id", Integer, primary_key=True), Column("parent", Integer, ForeignKey("left.id", ondelete="CASCADE")), Column("child", Integer, ForeignKey("right.id", ondelete="CASCADE")), ) class Parent(Base): __tablename__ = "left" id = Column(Integer, primary_key=True) name = Column(String(length=50), nullable=False)
from inspect import signature from typing import List, Optional from alembic import command from alembic.config import Config from asyncpg import CannotConnectNowError from databases import Database from fastapi import FastAPI, Query, status, HTTPException from template_project import core from template_project.api.models import DBRecord from template_project.conf import settings logger = logging.getLogger(__name__) app = FastAPI() database = Database(settings.db_dsn) def _handle_exceptions_helper(status_code, *args): if args: raise HTTPException(status_code=status_code, detail=args[0]) else: raise HTTPException(status_code=status_code) def handle_exceptions(func): signature(func) @wraps(func) async def wrapper(*args, **kwargs): try:
def __init__(self, db_path: str): self.database = Database(db_path)
class SoundBert(commands.Bot): def __init__(self, config: Config): self._ensure_event_loop() super().__init__(command_prefix=SoundBert._get_guild_prefix) self.config = config self.db = Database(config.database_url) self.loop.run_until_complete(self.db.connect()) base_extensions = [ 'soundbert.cogs.soundboard', 'soundbert.cogs.info', 'soundbert.cogs.settings', 'soundbert.cogs.admin' ] log.info('Loading base extensions.') for ext in base_extensions: self.load_extension(ext) log.debug(f'Loaded {ext}') log.info('Loading extra extensions.') for ext in self.config.extra_extensions.split(','): ext = ext.strip() try: self.load_extension(ext) except commands.ExtensionNotFound: log.exception(f'Failed to load {ext}.') else: log.debug(f'Loaded {ext}.') def run(self): super(SoundBert, self).run(self.config.token) @staticmethod def _ensure_event_loop(): """ Allows for subprocessing using asyncio on Windows, and tries to use uvloop on Unix-like systems. """ if platform.system() == 'Windows': loop = asyncio.ProactorEventLoop() asyncio.set_event_loop(loop) else: try: import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) except ImportError: pass async def _get_guild_prefix(self, msg: Message): """ Implementation for command_prefix. :param msg: The message that might have a command. :return: The prefix """ await self._ensure_guild(msg.guild.id) prefix = await self.db.fetch_val( select([guilds.c.prefix]).where(guilds.c.id == msg.guild.id)) return commands.when_mentioned_or(prefix)(self, msg) @alru_cache(maxsize=2048) async def _ensure_guild(self, guild_id: int): """ Ensures that the guild is in the database. Uses an LRU cache to try to not ping the database too much. :param guild_id: The guild id """ log.debug(f'Ensuring guild {guild_id} is in database.') query = guilds.insert().values(id=guild_id, prefix=self.config.default_prefix) try: await self.db.execute(query) except UniqueViolationError: pass async def on_command_error(self, ctx: commands.Context, exception: commands.CommandError): """ Error handling. :param ctx: Command context :param exception: What went wrong """ log_msg = ( f'In guild {ctx.guild.name}, channel {ctx.channel.name}, ' f'{ctx.author.name} executed {ctx.message.content}, but encountered exception: {exception}' ) if isinstance(exception, (commands.UserInputError, commands.CheckFailure, commands.CommandOnCooldown)): log.debug(log_msg) await warn(ctx) else: log.exception(log_msg, exc_info=exception) await err(ctx) if len(exception.args) > 0: try: delay = int(exception.args[1]) except (IndexError, ValueError): delay = 60 await ctx.send(exception.args[0], delete_after=delay) async def on_command(self, ctx: commands.Context): """ Log command execution. :param ctx: Command context """ log.info(f'In guild {ctx.guild.name}, channel {ctx.channel.name}, ' f'{ctx.author.name} executed {ctx.message.content}')
from pydantic import BaseModel from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, Text from sqlalchemy.sql import select class Users(BaseModel): id: int username: str avatar: str message: str received: str db_url = "sqlite:///./users.db" database = Database(db_url) metadata = MetaData() users = Table("users", metadata, Column("id", Integer, primary_key=True), Column("username", String(100), nullable=False), Column("avatar", String(100), nullable=False), Column("message", Text, nullable=False), Column("received", String(100), nullable=False)) engine = create_engine(db_url, echo=False) metadata.bind = engine metadata.create_all(engine) async def insert_message(**kwargs) -> int: if kwargs.get("id"): kwargs.pop("id", None)
def __init__(self): self.database = Database('postgresql://*****:*****@localhost/mydb')
from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from databases import Database from pydantic import BaseModel import aiosqlite database = Database('sqlite:///./app.db') app = FastAPI() @app.on_event("startup") async def database_connect(): await database.connect() @app.on_event("shutdown") async def database_disconnect(): await database.disconnect() @app.get('/') async def fetch_data(): query = "SELECT * FROM product" results = await database.fetch_all(query=query) return results @app.get("/Emi/") async def emi_calculator(product_name: str, tenure: int, interest: int): query = 'SELECT * FROM product' results = await database.fetch_all(query=query) leng = len(results)
def __init__(self, db_path: str): self._db = Database(db_path) self._connected = False
# --- # подключение к базе данных # --- from databases import Database from sqlalchemy import create_engine#, MetaData from sqlalchemy.ext.declarative import declarative_base from ..config.settings import DATABASE_URL # подключение database = Database(DATABASE_URL) engine = create_engine( # только для синхронных запросов к базе DATABASE_URL, ) # --- 1ый способ Base = declarative_base() # --- 2ой способ # metadata = MetaData()
#!/usr/bin/env python # -*- coding: utf-8 -*- # # database.py # @Author : Gustavo Freitas ([email protected]) # @Link : # @Date : 12/13/2019, 12:08:52 PM from databases import Database from authentication.config import SQLALCHEMY_DATABASE_URI, POSTGRES_POOL_SIZE database = Database(SQLALCHEMY_DATABASE_URI, min_size=1, max_size=POSTGRES_POOL_SIZE) # # Execute # query = notes.insert() # values = {"text": "example1", "completed": True} # await database.execute(query=query, values=values) # # Execute many # query = notes.insert() # values = [ # {"text": "example2", "completed": False}, # {"text": "example3", "completed": True}, # ] # await database.execute_many(query=query, values=values)