def items(self): '''Returns items on the current page.''' if isinstance(self.collection, Query): query = self.collection if sqav.startswith("0.4"): attribute = getattr(query.table.c, self.alpha_attribute) elif sqav.startswith("0.5"): attribute = getattr(query._entity_zero().selectable.c, self.alpha_attribute) else: entity = getattr(query.column_descriptions[0]['expr'], self.alpha_attribute) query = query.add_columns(entity) column = dropwhile(lambda x: x['name'] != \ self.alpha_attribute, query.column_descriptions) attribute = column.next()['expr'] if self.item_count >= self.paging_threshold: if self.page != self.other_text: query = query.filter(attribute.ilike(u'%s%%' % self.page)) else: # regexp search query = query.filter(attribute.op('~')(u'^[^a-zA-Z].*')) query.order_by(attribute) return query.all() else: raise NotImplementedError
def test_column_create(self): if sqav.startswith("0.6") or sqav.startswith("0.7"): ## FIXME??? ## This test does not appear to test anything that is ## VDM specific. It also fails with SQLAlchemy 0.6 ## Does this test really belong here? return tabxyz = Table('xyz', metadata, Column('id', Integer, primary_key=True) ) col = Column('abc', Integer, ForeignKey('xyz.id')) # FK does *not* work assert len(col.foreign_keys) == 0 tab = Table('tab', metadata, Column('abc', Integer, ForeignKey('xyz.id')), ) col0 = tab.c['abc'] fk0 = col0.foreign_keys[0] assert len(col0.foreign_keys) == 1 assert fk0.parent is not None tab2 = Table('tab', metadata) tab2.append_column( Column('abc', Integer, ForeignKey('xyz.id')) ) assert len(tab2.c['abc'].foreign_keys) == 1 assert tab2.c['abc'].foreign_keys[0].parent is not None tab3 = Table('tab', metadata) col3 = col0.copy() col3.foreign_keys.add(tab.c['abc'].foreign_keys[0].copy()) tab3.append_column(col3) assert len(tab3.c['abc'].foreign_keys) == 1 # fails # assert tab3.c['abc'].foreign_keys[0].parent tab3 = Table('tab', metadata) col3 = col0.copy() tab3.append_column(col3) col3.foreign_keys.add(ForeignKey(col0.key)) assert len(tab3.c['abc'].foreign_keys) == 1 # fails # assert tab3.c['abc'].foreign_keys[0].parent tab4 = Table('tab', metadata) tab4.append_column( col0.copy() ) tab4.c[col0.key].append_foreign_key(fk0.copy()) assert len(tab4.c['abc'].foreign_keys) == 1 assert tab4.c['abc'].foreign_keys[0].parent is not None
def items(self): '''Returns items on the current page.''' if isinstance(self.collection, Query): query = self.collection if sqav.startswith("0.4"): attribute = getattr(query.table.c, self.alpha_attribute) elif sqav.startswith("0.5"): attribute = getattr(query._entity_zero().selectable.c, self.alpha_attribute) else: entity = getattr(query.column_descriptions[0]['expr'], self.alpha_attribute) query = query.add_columns(entity) column = dropwhile(lambda x: x['name'] != \ self.alpha_attribute, query.column_descriptions) attribute = column.next()['expr'] if self.item_count >= self.paging_threshold: if self.page != self.other_text: query = query.filter(attribute.ilike(u'%s%%' % self.page)) else: # regexp search query = query.filter(attribute.op('~')(u'^[^a-zA-Z].*')) query.order_by(attribute) return query.all() elif isinstance(self.collection,list): if self.item_count >= self.paging_threshold: if self.page != self.other_text: print '\ndict es '+str(dict) if isinstance(self.collection[0], dict): items = [x for x in self.collection if x[self.alpha_attribute][0:1].lower() == self.page.lower()] elif isinstance(self.collection[0], unicode): items = [x for x in self.collection if x[0:1].lower() == self.page.lower()] else: items = [x for x in self.collection if getattr(x,self.alpha_attribute)[0:1].lower() == self.page.lower()] else: # regexp search if isinstance(self.collection[0], dict): items = [x for x in self.collection if re.match('^[^a-zA-Z].*',x[self.alpha_attribute])] else: items = [x for x in self.collection if re.match('^[^a-zA-Z].*',x)] items.sort() else: items = self.collection return items else: raise NotImplementedError
def test_03_reorder_resources(self): rev = model.repo.new_revision() pkg = model.Package.by_name(self.pkgname) rg = pkg.resource_groups[0] res0 = rg.resources[0] del rg.resources[0] rg.resources.append(res0) # this assert will fail # assert pkg.resources[1].position == 1 # Why? According to docs for ordering list it does not reorder appended # elements by default (see # http://www.sqlalchemy.org/trac/browser/lib/sqlalchemy/ext/orderinglist.py#L197) # so we have to call reorder directly in supported versions # of sqlalchemy and set position to None in older ones. if sqav.startswith('0.4'): rg.resources[1].position = None else: rg.resources.target.reorder() model.repo.commit_and_remove() pkg = model.Package.by_name(self.pkgname) assert len(rg.resources) == 3, rg.resources lastres = rg.resources[2] assert lastres.position == 2, lastres assert lastres.url == self.urls[0], lastres.url
def _has_property(cls, name): """Check if the related mapper has the given property""" mapper = cls.__mapper__ if sa_version.startswith('0.4'): return mapper.get_property(name, raiseerr=False) is not None else: return mapper.has_property(name)
def delete_all(self): '''Delete all data from all tables.''' self.session.remove() ## use raw connection for performance connection = self.session.connection() if sqav.startswith("0.4"): tables = self.metadata.table_iterator() else: tables = reversed(self.metadata.sorted_tables) for table in tables: connection.execute('delete from "%s"' % table.name) self.session.commit()
def check_real_change(self, instance, mapper, connection): # check each attribute to see if they have been changed logger.debug('check_real_change: %s', instance) if sqav.startswith("0.4"): state = instance._state else: state = instance for key in instance.revisioned_fields(): (added, unchanged, deleted) = get_history(state, key, passive=PASSIVE_OFF) if added or deleted: logger.debug('check_real_change: True') return True logger.debug('check_real_change: False') return False
def check_real_change(self, instance, mapper, connection): # check each attribute to see if they have been changed logger.debug('check_real_change: %s', instance) if sqav.startswith("0.4"): state = instance._state else: state = instance for key in instance.revisioned_fields(): (added, unchanged, deleted) = get_history(state, key, passive = PASSIVE_OFF) if added or deleted: logger.debug('check_real_change: True') return True logger.debug('check_real_change: False') return False
def smc_sa10_workaround(prop): def add_event_handlers(): # We have the relationship from the child to the parent but we # need the inverse. This is only defined after the mapper has # been configured. That's why this is inside a listener for the # "after_configured" event. parent_prop = getattr(prop.mapper.class_, prop.back_populates) def cb_on_init(target, coll, adapter): coll.on_init() event.listen(parent_prop, "init_collection", cb_on_init) def cb_on_dispose(target, coll, adapter): coll.on_dispose() event.listen(parent_prop, "dispose_collection", cb_on_dispose) if sa_version.startswith("1."): event.listen(mapper, "after_configured", add_event_handlers) return prop
def initialize_defered_mappers(metadata): # Execute __declare__last__ for sqlalchemy 0.4 ignored_tables = [] for mapper in declaratives_mappers(metadata): # http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/declarative.html#declare-last if verify_mapper(mapper, metadata) is False: continue if mapper._create_table is False: tname = '.'.join([e for e in [mapper.__table__.schema, mapper.__table__.name] if e]) ignored_tables.append(tname) if sa_version.startswith('0.4') and hasattr(mapper, '__declare_last__') is True: mapper.__declare_last__() if hasattr(mapper, '_create_relations'): mapper._create_relations() # Fix defaults for tests if metadata.bind.name == 'sqlite': if sqlalchemy.__version__.startswith('0.4'): set_sqlite_defaults(metadata) # Avoid an error with the immutabledict from SA 0.8 tables = dict(metadata.tables) for tname in ignored_tables: del tables[tname] metadata.tables = tables
print "SqlAlchemy was not found, please install it !" sys.exit(1) def usage(argv): print >> sys.stderr, 'Usage: %s db_conn_string [--id entity_id|--name entity_name]' % argv[ 0] print >> sys.stderr, 'Where db_conn_string is a SQLAlchemy connection string, e.g. mysql://user:password@host/database' return 1 if __name__ == "__main__": if len(sys.argv) != 2 and len(sys.argv) != 4: sys.exit(usage(sys.argv)) if not __version__.startswith('0.4'): print "Wrong version of SqlAlchemy found, please install a 0.4 version !" sys.exit(1) get_entity = False if len(sys.argv) == 4: if sys.argv[2] == '--id': id_entity = sys.argv[3] get_entity = True elif sys.argv[2] == '--name': id_entity = -1 name_entity = sys.argv[3] get_entity = True else: print "dont know this option : %s" % sys.argv[2] sys.exit(1)
# -*- encoding: utf-8 -*- import os parentpath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")) from web.utils import Storage from sqlalchemy import __version__ as sqlalchemy_version sitepath = None if sqlalchemy_version.startswith("0.5"): db = "postgres:///soc" else: db = "postgresql:///soc" init = "socmembers.xml" instance = u"soc" secret = u"..." cookie = "soclogin" maps_key = Storage(http="ABQIAAAAnfs7bKE82qgb3Zc2YyS-oBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSySz_REpPq-4WZA27OwgbtyR3VcA") pdflatex = "/usr/bin/pdflatex" xsltproc = "/usr/bin/xsltproc" tmppath = "/tmp/socmanager" photopath = os.path.join(parentpath, "photos") thumbpath = os.path.join(parentpath, "var", "spool", "socmanager", "thumb") midpath = os.path.join(parentpath, "var", "spool", "socmanager", "mid") formats = [Storage(processor="tex", name="full", description=u"Komplettliste mit allen Informationen (pdf)", cls="full.cls", merge=None, hide_private=False), Storage(processor="tex", name="project", description=u"Projektliste zum Verteilen (pdf)", cls="project.cls", merge=None, hide_private=True), Storage(processor="tex", name="chorverband", description=u"Mitgliederliste für Chorverband (pdf)", cls="chorverband.cls", merge=None, hide_private=False), Storage(processor="tex", name="address_sticker", description=u"Addressettikten (pdf)", cls="address_sticker.cls", merge="address", hide_private=True), Storage(processor="tex", name="address_panel", description=u"Addressaufdruck für Fensterumschläge (pdf)", cls="address_panel.cls", merge="address", hide_private=True), Storage(processor="tex", name="badge", description=u"Namensschilder (pdf)", cls="badge.cls", merge=None, hide_private=True), Storage(processor="tex", name="voucher", description=u"Gutschein (pdf)", cls="voucher.cls", merge=None, hide_private=True), Storage(processor="xml", name="kml", description=u"Google Earth Datei (kml)", xslt="kml.xslt", merge="address", mime="application/vnd.google-earth.kml+xml", extension="kml", hide_private=True),
# http://blog.pythonisito.com/2008/01/cascading-drop-table-with-sqlalchemy.html from sqlalchemy import __version__ as sqav if sqav[:3] in ("0.4", "0.5"): from sqlalchemy.databases import postgres class CascadeSchemaDropper(postgres.PGSchemaDropper): def visit_table(self, table): for column in table.columns: if column.default is not None: self.traverse_single(column.default) self.append("\nDROP TABLE " + self.preparer.format_table(table) + " CASCADE") self.execute() postgres.dialect.schemadropper = CascadeSchemaDropper elif sqav.startswith("0.6") or sqav.startswith("0.7") or sqav.startswith( "0.8"): from sqlalchemy.dialects.postgresql import base def visit_drop_table(self, drop): return "\nDROP TABLE " + \ self.preparer.format_table(drop.element) + \ " CASCADE" base.dialect.ddl_compiler.visit_drop_table = visit_drop_table else: raise ValueError( "VDM only works with SQLAlchemy versions 0.4 through 0.8, not: %s" % sqav) from sqlalchemy import create_engine
class SaMappedClassAlias(pyamf.ClassAlias): KEY_ATTR = 'sa_key' LAZY_ATTR = 'sa_lazy' EXCLUDED_ATTRS = [ '_entity_name', '_instance_key', '_sa_adapter', '_sa_appender', '_sa_class_manager', '_sa_initiator', '_sa_instance_state', '_sa_instrumented', '_sa_iterator', '_sa_remover', '_sa_session_id', '_state' ] STATE_ATTR = '_sa_instance_state' if __version__.startswith('0.4'): STATE_ATTR = '_state' def getCustomProperties(self): self.mapper = class_mapper(self.klass) self.exclude_attrs.update(self.EXCLUDED_ATTRS) self.properties = [] for prop in self.mapper.iterate_properties: self.properties.append(prop.key) self.encodable_properties.update(self.properties) self.decodable_properties.update(self.properties) self.exclude_sa_key = self.KEY_ATTR in self.exclude_attrs self.exclude_sa_lazy = self.LAZY_ATTR in self.exclude_attrs def getEncodableAttributes(self, obj, **kwargs): """ Returns a C{tuple} containing a dict of static and dynamic attributes for C{obj}. """ attrs = pyamf.ClassAlias.getEncodableAttributes(self, obj, **kwargs) if not self.exclude_sa_key: # primary_key_from_instance actually changes obj.__dict__ if # primary key properties do not already exist in obj.__dict__ attrs[self.KEY_ATTR] = self.mapper.primary_key_from_instance(obj) if not self.exclude_sa_lazy: lazy_attrs = [] for attr in self.properties: if attr not in obj.__dict__: lazy_attrs.append(attr) attrs[self.LAZY_ATTR] = lazy_attrs return attrs def getDecodableAttributes(self, obj, attrs, **kwargs): """ """ attrs = pyamf.ClassAlias.getDecodableAttributes( self, obj, attrs, **kwargs) # Delete lazy-loaded attrs. # # Doing it this way ensures that lazy-loaded attributes are not # attached to the object, even if there is a default value specified # in the __init__ method. # # This is the correct behavior, because SQLAlchemy ignores __init__. # So, an object retreived from a DB with SQLAlchemy will not have a # lazy-loaded value, even if __init__ specifies a default value. if self.LAZY_ATTR in attrs: obj_state = None if hasattr(orm.attributes, 'instance_state'): obj_state = orm.attributes.instance_state(obj) for lazy_attr in attrs[self.LAZY_ATTR]: if lazy_attr in obj.__dict__: # Delete directly from the dict, so # SA callbacks are not triggered. del obj.__dict__[lazy_attr] # Delete from committed_state so SA thinks this attribute was # never modified. # # If the attribute was set in the __init__ method, # SA will think it is modified and will try to update # it in the database. if obj_state is not None: if lazy_attr in obj_state.committed_state: del obj_state.committed_state[lazy_attr] if lazy_attr in obj_state.dict: del obj_state.dict[lazy_attr] if lazy_attr in attrs: del attrs[lazy_attr] del attrs[self.LAZY_ATTR] if self.KEY_ATTR in attrs: del attrs[self.KEY_ATTR] return attrs def createInstance(self, *args, **kwargs): self.compile() return self.mapper.class_manager.new_instance()
# -*- encoding: utf-8 -*- import os parentpath = os.path.normpath( os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")) from web.utils import Storage from sqlalchemy import __version__ as sqlalchemy_version sitepath = None if sqlalchemy_version.startswith("0.5"): db = "postgres:///soc" else: db = "postgresql:///soc" init = "socmembers.xml" instance = u"soc" secret = u"..." cookie = "soclogin" pdflatex = "/usr/bin/pdflatex" xsltproc = "/usr/bin/xsltproc" tmppath = "/tmp/socmanager" photopath = os.path.join(parentpath, "photos") thumbpath = os.path.join(parentpath, "var", "spool", "socmanager", "thumb") midpath = os.path.join(parentpath, "var", "spool", "socmanager", "mid") formats = [ Storage(processor="tex", name="full", description=u"Komplettliste mit allen Informationen (pdf)", cls="full.cls", merge=None, hide_private=False), Storage(processor="tex",
# Copyright 2011 Lincoln de Sousa <*****@*****.**>. # Copyright 2012, 2013, 2014, 2015, 2016 Jeffrey Finkelstein # <*****@*****.**> and contributors. # # This file is part of Flask-Restless. # # Flask-Restless is distributed under both the GNU Affero General Public # License version 3 and under the 3-clause BSD license. For more # information, see LICENSE.AGPL and LICENSE.BSD. """Helper functions for view classes.""" from sqlalchemy.exc import OperationalError from sqlalchemy.inspection import inspect as sqlalchemy_inspect from sqlalchemy.sql import func from sqlalchemy import __version__ as SQLALCHEMY_VERSION SQLALCHEMY_14 = SQLALCHEMY_VERSION.startswith('1.4.') def upper_keys(dictionary): """Returns a new dictionary with the keys of ``dictionary`` converted to upper case and the values left unchanged. """ return {k.upper(): v for k, v in dictionary.items()} def evaluate_functions(session, model, functions): """Executes each of the SQLAlchemy functions specified in ``functions``, a list of dictionaries of the form described below, on the given model and returns a dictionary mapping function name (slightly modified, see below) to result of evaluation of that function.
) def after_commit(self, session): if hasattr(session, '_object_cache'): del session._object_cache def after_rollback(self, session): if hasattr(session, '_object_cache'): del session._object_cache # __all__ = ['Session', 'engine', 'metadata', 'mapper'] # SQLAlchemy database engine. Updated by model.init_model() engine = None if sqav.startswith("0.4"): # SQLAlchemy session manager. Updated by model.init_model() Session = scoped_session(sessionmaker( autoflush=False, transactional=True, extension=[CkanSessionExtension(), extension.PluginSessionExtension()], )) else: Session = scoped_session(sessionmaker( autoflush=False, autocommit=False, expire_on_commit=False, extension=[CkanSessionExtension(), extension.PluginSessionExtension()], ))
# fix up table dropping on postgres # http://blog.pythonisito.com/2008/01/cascading-drop-table-with-sqlalchemy.html from sqlalchemy import __version__ as sqav if sqav[:3] in ("0.4", "0.5"): from sqlalchemy.databases import postgres class CascadeSchemaDropper(postgres.PGSchemaDropper): def visit_table(self, table): for column in table.columns: if column.default is not None: self.traverse_single(column.default) self.append("\nDROP TABLE " + self.preparer.format_table(table) + " CASCADE") self.execute() postgres.dialect.schemadropper = CascadeSchemaDropper elif sqav.startswith("0.6") or sqav.startswith("0.7") or sqav.startswith("0.8"): from sqlalchemy.dialects.postgresql import base def visit_drop_table(self, drop): return "\nDROP TABLE " + \ self.preparer.format_table(drop.element) + \ " CASCADE" base.dialect.ddl_compiler.visit_drop_table = visit_drop_table else: raise ValueError("VDM only works with SQLAlchemy versions 0.4 through 0.7, not: %s" % sqav) from sqlalchemy import create_engine try: from sqlalchemy.orm import ScopedSession as scoped_session except ImportError: from sqlalchemy.orm import scoped_session
from mongosql.handlers import MongoJoin from tests.models import * # Import both MongoSQL packages try: from tests.benchmarks.mongosql_v1.mongosql import MongoQuery as MongoQuery_v1 except ImportError: print('Please install MongoSQL 1.5: ') print('$ bash tests/benchmarks/mongosql_v1_checkout.sh') exit(1) from mongosql import MongoQuery as MongoQuery_v2 # Check SqlAlchemy version from sqlalchemy import __version__ as SA_VERSION assert SA_VERSION.startswith('1.2.'), 'Only works with SqlAlchemy 1.2.x' # Init DB: choose one engine, Session = get_working_db_for_tests() # Prepare N_REPEATS = 1000 MongoJoin.ENABLED_EXPERIMENTAL_SELECTINQUERY = False ssn = Session() # Tests def test_v1(n): """ Test MongoSQL v1 """ for i in range(n): q = MongoQuery_v1(User, query=ssn.query(User)).query(
from datetime import datetime from functools import wraps from typing import Any from sqlalchemy import text # type: ignore from sqlalchemy.orm import Query # type: ignore from sqlalchemy import __version__ SQLALCHEMY_VERSION = __version__ IS_SQLALCHEMY_1_4 = __version__.startswith("1.4") class Empty: pass def get_sqlalchemy_version(): return def is_none(value: Any) -> bool: return isinstance(value, type(None)) def empty_sql(): return text("") def get_already_joined_tables(query: Query) -> list: if IS_SQLALCHEMY_1_4:
try: from sqlalchemy import create_engine, MetaData, Table, select, and_, __version__ except ImportError: print "SqlAlchemy was not found, please install it !" sys.exit(1) def usage(argv): print >> sys.stderr, 'Usage: %s db_conn_string [--id entity_id|--name entity_name]' % argv[0] print >> sys.stderr, 'Where db_conn_string is a SQLAlchemy connection string, e.g. mysql://user:password@host/database' return 1 if __name__ == "__main__": if len(sys.argv) != 2 and len(sys.argv) != 4: sys.exit(usage(sys.argv)) if not __version__.startswith('0.4'): print "Wrong version of SqlAlchemy found, please install a 0.4 version !" sys.exit(1) get_entity = False if len(sys.argv) == 4: if sys.argv[2] == '--id': id_entity = sys.argv[3] get_entity = True elif sys.argv[2] == '--name': id_entity = -1 name_entity = sys.argv[3] get_entity = True else: print "dont know this option : %s" % sys.argv[2] sys.exit(1)
metadata.create_all(engine) from sqlalchemy.orm import scoped_session, sessionmaker, create_session from sqlalchemy.orm import relation, backref SessionObject = scoped_session(create_session) session = SessionObject() from sqlalchemy.orm import mapper #mapper = SessionObject.mapper from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.orm.collections import attribute_mapped_collection from sqlalchemy import __version__ as sqav if sqav.startswith("0.4"): _clear = session.clear else: _clear = session.expunge_all class BaseObject(object): def __repr__(self): return '<%s %s>' % (self.__class__.__name__, self.id) def delete(st): st.state = 'deleted' def undelete(st):
filter: { age: { $gte: 18 } }, // filter: age >= 18 join: ['user_profile'], // load related `user_profile` limit: 10, // limit to 10 rows })) ``` Tired of adding query parameters for pagination, filtering, sorting? Here is the ultimate solution. NOTE: currently, only tested with PostgreSQL. """ # SqlAlchemy versions from sqlalchemy import __version__ as SA_VERSION SA_12 = SA_VERSION.startswith('1.2') SA_13 = SA_VERSION.startswith('1.3') # Exceptions that are used here and there from .exc import * # MongoSQL needs a lot of information about the properties of your models. # All this is handled by the following class: from .bag import ModelPropertyBags, CombinedBag # The heart of MongoSql are the handlers: # that's where your JSON objects are converted to actual SqlAlchemy queries! from . import handlers # MongoQuery is the man that parses your QueryObject and applies the methods from MongoModel that # implement individual fields.