Metadata-Version: 2.1
Name: classic-sql-storage
Version: 0.0.1
Summary: Provides primitives for contextual transactions processing with SQLALchemy and base for repository class
Home-page: https://github.com/variasov/classic_sql_storage
Author: Sergei Variasov
Author-email: variasov@gmail.com
License: UNKNOWN
Project-URL: Bug Tracker, https://github.com/variasov/classic_sql_storage/issues
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3.7
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 4 - Beta
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Provides-Extra: dev
License-File: LICENSE

# Classic SQL Storage

This package provides contextual transactions processing for SQLAlchemy and 
base for pattern "Repository".

Part of project "Classic".

Usage:

```python
from classic.sql_storage import TransactionContext
from sqlalchemy import create_engine, text


engine = create_engine('sqlite:///')

transaction = TransactionContext(bind=engine)


# As context manager:
with transaction:
    transaction.current_session.execute(
        text('SELECT 1')
    )


# As decorator:
@transaction
def some_work():
    transaction.current_session.execute(
        text('SELECT 1')
    )


# Propagation:
@transaction
def complex_function():
    """Doing complex work with db.
    Session will be commited only after finish of complex_function call.
    TransactionContext will count all calls, and will commit or rollback session
    only in last call.
    """
    some_work()
    some_work()
    some_work()
    
    with transaction:
        transaction.current_session.execute(
            text('SELECT 1')
        )


# Automatic rollback
@transaction
def function_with_error():
    """Changes, made by some_work, will be cancelled after raising error"""
    some_work()
    raise ValueError()


```



