Update UDT doc examples for frozen keyword.

This commit is contained in:
Adam Holmberg
2014-09-09 17:00:15 +00:00
parent 3245b89e48
commit db5ec3e523

View File

@@ -21,7 +21,7 @@ instance through :meth:`.Cluster.register_user_type`:
session = cluster.connect() session = cluster.connect()
session.set_keyspace('mykeyspace') session.set_keyspace('mykeyspace')
session.execute("CREATE TYPE address (street text, zipcode int)") session.execute("CREATE TYPE address (street text, zipcode int)")
session.execute("CREATE TABLE users (id int PRIMARY KEY, location address)") session.execute("CREATE TABLE users (id int PRIMARY KEY, location frozen<address>)")
# create a class to map to the "address" UDT # create a class to map to the "address" UDT
class Address(object): class Address(object):
@@ -58,7 +58,7 @@ for the UDT:
session = cluster.connect() session = cluster.connect()
session.set_keyspace('mykeyspace') session.set_keyspace('mykeyspace')
session.execute("CREATE TYPE address (street text, zipcode int)") session.execute("CREATE TYPE address (street text, zipcode int)")
session.execute("CREATE TABLE users (id int PRIMARY KEY, location address)") session.execute("CREATE TABLE users (id int PRIMARY KEY, location frozen<address>)")
class Foo(object): class Foo(object):
@@ -72,13 +72,13 @@ for the UDT:
# since we're using a prepared statement, we don't *have* to register # since we're using a prepared statement, we don't *have* to register
# a class to map to the UDT to insert data. The object just needs to have # a class to map to the UDT to insert data. The object just needs to have
# "street" and "zipcode" attributes (which Foo does): # "street" and "zipcode" attributes (which Foo does):
session.execute(insert_statement, [0, Foo("123 Main St.", 78723, "some other stuff")] session.execute(insert_statement, [0, Foo("123 Main St.", 78723, "some other stuff")])
# when we query data, UDT columns that don't have a class registered # when we query data, UDT columns that don't have a class registered
# will be returned as namedtuples: # will be returned as namedtuples:
results = session.execute("SELECT * FROM users") results = session.execute("SELECT * FROM users")
first_row = results[0] first_row = results[0]
address = first_row.address address = first_row.location
print address # prints "Address(street='123 Main St.', zipcode=78723)" print address # prints "Address(street='123 Main St.', zipcode=78723)"
street = address.street street = address.street
zipcode = address.street zipcode = address.street