{ "info": { "author": "Dolmen Team", "author_email": "dolmen@list.dolmen-project.org", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python" ], "description": "cromlech.sqlalchemy\n###################\n\n``cromlech.sqlalchemy`` provides utility functions and components to ease\nthe use of `SQLAlchemy`.\n\nTest setup\n===========\n\nWe get a temporary dir to place our databases::\n\n >>> import tempfile\n >>> working_dir = tempfile.mkdtemp('cromlech-sqlalchemy-test')\n\n\nConfiguration & registration\n============================\n\n >>> from cromlech.sqlalchemy import create_engine\n\n >>> user_db = 'sqlite:///%s/users.db' % working_dir\n >>> store_db = 'sqlite:///%s/store.db' % working_dir\n\n >>> users_engine = create_engine(user_db, 'Users')\n >>> store_engine = create_engine(store_db, 'Store')\n\n\nModel\n=====\n\nLet's use declarative extension::\n\n >>> from sqlalchemy.ext.declarative import declarative_base\n\n >>> UserBase = declarative_base()\n >>> users_engine.bind(UserBase)\n\nIn SQLAlchemy you would declare wich engine the database is bound to. In our\ncase the url will come in environ with the first connection so we deffer\nthis to the moment when engine will be created. The package provides a method\nfor that::\n\n\nWe can define our SQLAlchemy model::\n\n >>> from sqlalchemy import Column, Integer, String\n\n >>> class User(UserBase):\n ... __tablename__ = 'test_users'\n ...\n ... id = Column('id', Integer, primary_key=True)\n ... name = Column('name', String(50))\n ...\n ... def __repr__(self):\n ... return \"User(%d, '%s')\" % (self.id, self.name)\n\n\nController\n==========\n\nTo do any operation we will have use SQLAlchemySession context manager\nproviding an engine name::\n\n >>> from cromlech.sqlalchemy import SQLAlchemySession, get_session\n\nTypically first request may create our tables::\n\n >>> with SQLAlchemySession(users_engine) as session:\n ... UserBase.metadata.create_all()\n ... print session.query(User).all()\n []\n\nTransaction of database is ruled by the general transaction package::\n\n >>> import transaction\n >>> transaction.commit()\n\nInside the context you can also fetch the session by its configuration name,\nthis is convenient from anywhere in the code, eg a fonction::\n\n >>> def add_user(id, name):\n ... session = get_session(\"Users\")\n ... session.add(User(id=id, name=name))\n\n >>> with SQLAlchemySession(users_engine) as session:\n ... add_user(1, 'bob')\n ... print session.query(User).all()\n [User(1, 'bob')]\n\n\nTransaction\n===========\n\nFor this chapter let's assume we are inside a with SQLAlchemySession clause::\n\n >>> ctxmanager = SQLAlchemySession(users_engine)\n >>> session = ctxmanager.__enter__()\n\nTransaction are linked to zope transaction using zope.sqlalchemy, all operations\nabove are not yet commited. Let's abort to see bob is not present no more::\n\n >>> transaction.abort()\n \n >>> print session.query(User).all()\n []\n\nNow add it again an commit so that it's ok::\n\n >>> transaction.commit()\n >>> add_user(1, 'bob')\n >>> transaction.commit()\n >>> print session.query(User).all()\n [User(1, 'bob')]\n\nNow we really are in a new transaction::\n\n >>> transaction.abort()\n >>> print session.query(User).all()\n [User(1, 'bob')]\n\nLet's end our session for now ::\n\n >>> ctxmanager.__exit__(None, None, None)\n\n\nMore than one database\n======================\n\nWe can use more than one database, Let's define another\nmodel in another database:\n\n >>> StoreBase = declarative_base()\n >>> store_engine.bind(StoreBase)\n \nWe can define our SQLAlchemy model::\n\n >>> class Product(StoreBase):\n ... __tablename__ = 'test_products'\n ... id = Column('id', Integer, primary_key=True)\n ... name = Column('name', String(50))\n ...\n ... def __repr__(self):\n ... return \"Product(%d, '%s')\" % (self.id, self.name)\n\n\nCreate tables::\n\n >>> from transaction import manager as transaction_manager\n\n >>> with transaction_manager:\n ... with SQLAlchemySession(store_engine) as session:\n ... StoreBase.metadata.create_all()\n\nand an product adder::\n\n >>> def add_product(id, name):\n ... session = get_session('Store')\n ... session.add(Product(id=id, name=name))\n\nWe may do operations in databases using our objects::\n\n >>> with transaction_manager:\n ... with SQLAlchemySession(store_engine) as session:\n ... with SQLAlchemySession(users_engine) as session:\n ... add_user(2, 'juan')\n ... add_product(1, 'table')\n\nIt works :\n\n >>> with SQLAlchemySession(users_engine) as session:\n ... session.query(User).all()\n [User(1, 'bob'), User(2, 'juan')]\n\n >>> with SQLAlchemySession(store_engine) as session:\n ... session.query(Product).all()\n [Product(1, 'table')]\n\n\nMore than one metadata\n======================\n\nNow we may also bind another metadata to same database, and this work\neven if it's already initialised::\n\n >>> GroupBase = declarative_base()\n >>> users_engine.bind(GroupBase)\n\n >>> class Group(GroupBase):\n ... __tablename__ = 'test_groups'\n ... id = Column('id', Integer, primary_key=True)\n ... name = Column('name', String(50))\n ...\n ... def __repr__(self):\n ... return \"Group(%d, '%s')\" % (self.id, self.name)\n\n >>> with transaction_manager:\n ... with SQLAlchemySession(users_engine) as session:\n ... GroupBase.metadata.create_all()\n ... session.add(Group(id=1, name='them'))\n ... session.query(Group).all()\n [Group(1, 'them')]\n\n\nUnique session\n==============\n\nWhenever you know that your application will always associate same db name with\nsame DBName, you can setup session once and only once, removing overhead to\nsingle request. For that use the SharedSQLAlchemySession context manager.\nNote that it is still a scoped session (no sharing between thread), and that\nyou don't have to care when it will be initialized, you just use the context\nmanager ::\n\n >>> from cromlech.sqlalchemy import SharedSQLAlchemySession\n >>> with SharedSQLAlchemySession(store_engine) as session:\n ... the_session = session\n ... session.query(Product).all()\n [Product(1, 'table')]\n\nsession will be reused::\n\n >>> with SharedSQLAlchemySession(store_engine) as session:\n ... print session is the_session\n True\n\n\nto phase or not two phase\n=========================\n\nYou can pass a two_phase boolean argument to activate or not two_phase commit.\nsee zope.sqlalchemy. If you do not pass it, two_phase will be set to true for\nmysql and postgre (known supporting database) and false otherwise.\n\nYou may force it however::\n\n >>> with transaction_manager:\n ... with SQLAlchemySession(store_engine, two_phase=True):\n ... add_product(10, 'chair')\n Traceback (most recent call last):\n ...\n ValueError: SQL Engine 'Store' : 'sqlite' does not support two phase commits\n\nas sqlite does not support it !\n\nChangelog\n=========\n\n0.4 (2017-03-18)\n----------------\n\n- We no longer use the ZCA to register engines. This breaks backward\n compatibility by removing the `create_and_register` function.\n\n\n0.3 (2012-04-29)\n----------------\n\n- Majors changes : we no longer support deferred binding. The context \n managers now take directly the engine, which is now an `Engine` object.\n This makes the implementation much more straightforward.\n\n\n0.2 (2012-01-26)\n----------------\n\n- Fixed testing for py2.7\n\n\n0.2a1 (2011-07-30)\n------------------\n\n- The context manager and util functions no longer depend on an environ.\n This allows us to use this package outside of a WSGI stack.\n\n\n0.1a1\n-----\n\n- Initial release", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://gitweb.dolmen-project.org", "keywords": "SQLAlchemy ORM Cromlech", "license": "ZPL", "maintainer": null, "maintainer_email": null, "name": "cromlech.sqlalchemy", "package_url": "https://pypi.org/project/cromlech.sqlalchemy/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/cromlech.sqlalchemy/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://gitweb.dolmen-project.org" }, "release_url": "https://pypi.org/project/cromlech.sqlalchemy/0.4/", "requires_dist": null, "requires_python": null, "summary": "Cromlech Web Framework utility methods and components for applications based on SQLAlchemy.", "version": "0.4" }, "last_serial": 2811231, "releases": { "0.2a1": [ { "comment_text": "", "digests": { "md5": "5d8068d651988548ba8816aa545c42aa", "sha256": "9f522b22d0cfdb62dfdede07ba6702f08ac42fc973f20a0696a16ec84b927626" }, "downloads": -1, "filename": "cromlech.sqlalchemy-0.2a1.tar.gz", "has_sig": false, "md5_digest": "5d8068d651988548ba8816aa545c42aa", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7818, "upload_time": "2011-07-30T20:25:28", "url": "https://files.pythonhosted.org/packages/39/bf/e178348df3ec6904779e52785ee8327ecc6ec53b2576f123ae4e952954f0/cromlech.sqlalchemy-0.2a1.tar.gz" } ], "0.3b1": [ { "comment_text": "", "digests": { "md5": "dea7629c5c68e06bbac984e4227f2c66", "sha256": "efa1b675648673ecb10e6aecf8f5bb61f63573ac60a84115d3fcabc57d3c2000" }, "downloads": -1, "filename": "cromlech.sqlalchemy-0.3b1.tar.gz", "has_sig": false, "md5_digest": "dea7629c5c68e06bbac984e4227f2c66", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7213, "upload_time": "2017-04-18T13:27:57", "url": "https://files.pythonhosted.org/packages/c8/66/04430e3eb16668db1f1bc2eb858db32236ad45f5da7d8b0cb00a62612ab2/cromlech.sqlalchemy-0.3b1.tar.gz" } ], "0.4": [ { "comment_text": "", "digests": { "md5": "2097769fc29d84c0eed32641edf39d17", "sha256": "1143ed65c2761072ab662065081afdfc1d847cdad4cfc78a80eae0692a118c99" }, "downloads": -1, "filename": "cromlech.sqlalchemy-0.4.tar.gz", "has_sig": false, "md5_digest": "2097769fc29d84c0eed32641edf39d17", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7299, "upload_time": "2017-04-18T13:56:24", "url": "https://files.pythonhosted.org/packages/21/ba/8cca48268c4e33f28f6880a70af193e96f92eb6a491d6599ec32e8f3ab09/cromlech.sqlalchemy-0.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "2097769fc29d84c0eed32641edf39d17", "sha256": "1143ed65c2761072ab662065081afdfc1d847cdad4cfc78a80eae0692a118c99" }, "downloads": -1, "filename": "cromlech.sqlalchemy-0.4.tar.gz", "has_sig": false, "md5_digest": "2097769fc29d84c0eed32641edf39d17", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7299, "upload_time": "2017-04-18T13:56:24", "url": "https://files.pythonhosted.org/packages/21/ba/8cca48268c4e33f28f6880a70af193e96f92eb6a491d6599ec32e8f3ab09/cromlech.sqlalchemy-0.4.tar.gz" } ] }