Esempio n. 1
0
def install():
    Base.metadata.create_all(Session().bind)

    data = [
        ('Chicago', 'United States', ('60601', '60602', '60603', '60604')),
        ('Montreal', 'Canada', ('H2S 3K9', 'H2B 1V4', 'H7G 2T8')),
        ('Edmonton', 'Canada', ('T5J 1R9', 'T5J 1Z4', 'T5H 1P6')),
        ('New York', 'United States',
                        ('10001', '10002', '10003', '10004', '10005', '10006')),
        ('San Francisco', 'United States',
                        ('94102', '94103', '94104', '94105', '94107', '94108'))
    ]

    countries = {}
    all_post_codes = []
    for city, country, postcodes in data:
        try:
            country = countries[country]
        except KeyError:
            countries[country] = country = Country(country)

        city = City(city, country)
        pc = [PostalCode(code, city) for code in postcodes]
        Session.add_all(pc)
        all_post_codes.extend(pc)

    for i in xrange(1, 51):
        person = Person(
                    "person %.2d" % i,
                    Address(
                        street="street %.2d" % i,
                        postal_code=all_post_codes[
                                random.randint(0, len(all_post_codes) - 1)]
                    )
                )
        Session.add(person)

    Session.commit()

    # start the demo fresh
    Session.remove()
Esempio n. 2
0
def install():
    Base.metadata.create_all(Session().bind)

    data = [('Chicago', 'United States', ('60601', '60602', '60603', '60604')),
            ('Montreal', 'Canada', ('H2S 3K9', 'H2B 1V4', 'H7G 2T8')),
            ('Edmonton', 'Canada', ('T5J 1R9', 'T5J 1Z4', 'T5H 1P6')),
            ('New York', 'United States', ('10001', '10002', '10003', '10004',
                                           '10005', '10006')),
            ('San Francisco', 'United States', ('94102', '94103', '94104',
                                                '94105', '94107', '94108'))]

    countries = {}
    all_post_codes = []
    for city, country, postcodes in data:
        try:
            country = countries[country]
        except KeyError:
            countries[country] = country = Country(country)

        city = City(city, country)
        pc = [PostalCode(code, city) for code in postcodes]
        Session.add_all(pc)
        all_post_codes.extend(pc)

    for i in xrange(1, 51):
        person = Person(
            "person %.2d" % i,
            Address(street="street %.2d" % i,
                    postal_code=all_post_codes[random.randint(
                        0,
                        len(all_post_codes) - 1)]))
        Session.add(person)

    Session.commit()

    # start the demo fresh
    Session.remove()
Esempio n. 3
0
    
    # query to load Person by name, with criterion
    # of "person 10"
    q = Session.query(Person).\
                    options(FromCache("local_session", "by_name")).\
                    filter(Person.name=="person 10")
                    
    # load from DB
    person10 = q.one()
    
    # next call, the query is cached.
    person10 = q.one()

    # clear out the Session.  The "_beaker_cache" dictionary
    # disappears with it.
    Session.remove()
    
    # query calls from DB again
    person10 = q.one()
    
    # identity is preserved - person10 is the *same* object that's
    # ultimately inside the cache.   So it is safe to manipulate
    # the not-queried-for attributes of objects when using such a 
    # cache without the need to invalidate - however, any change 
    # that would change the results of a cached query, such as 
    # inserts, deletes, or modification to attributes that are 
    # part of query criterion, still require careful invalidation.
    from caching_query import _get_cache_parameters
    cache, key = _get_cache_parameters(q)
    assert person10 is cache.get(key)[0]
Illustrate how to load some data, and cache the results.

"""

import pudb; pu.db
from environment import Session
from model import Person
from caching_query import FromCache

# load Person objects.  cache the result in the "default" cache region
print("loading people....")
people = Session.query(Person).options(FromCache("default")).all()

# remove the Session.  next query starts from scratch.
Session.remove()

# load again, using the same FromCache option. now they're cached,
# so no SQL is emitted.
print("loading people....again!")
people = Session.query(Person).options(FromCache("default")).all()

# Specifying a different query produces a different cache key, so
# these results are independently cached.
print("loading people two through twelve")
people_two_through_twelve = Session.query(Person).\
                            options(FromCache("default")).\
                            filter(Person.name.between("person 02", "person 12")).\
                            all()

# the data is cached under string structure of the SQL statement, *plus*