2
from SQLObject import *
5
############################################################
6
## Configuration parameters:
7
############################################################
9
user = os.environ.get('SQLOBJECT_USER', 'sqlobject_test')
10
passwd = os.environ.get('SQLOBJECT_PASSWORD', '')
11
database = os.environ.get('SQLOBJECT_DATABASE', 'sqlobject_test')
14
############################################################
16
############################################################
18
print 'Accessing with user %s and password %s' % (user, passwd)
22
__connection__ = MySQLConnection('localhost', database,
23
user, passwd, debug=debug)
26
__connection__ = PostgresConnection('dbname=%s user=%s' %
27
(database, user), debug=debug)
30
############################################################
32
############################################################
34
class Person(SQLObject):
36
_columns = [StringCol('username', length=20, alternateID=True,
38
StringCol('firstName', length=30,
40
StringCol('middleInitial', length=1, default=None),
41
StringCol('lastName', length=50, notNull=1)]
42
_joins = [RelatedJoin('Role'), MultipleJoin('PhoneNumber')]
44
def imageFilename(self):
45
return 'images/person-%s.jpg' % self.id
48
if not os.path.exists(self.imageFilename()):
50
f = open(self.imageFilename())
55
def _set_image(self, value):
56
# assume we get a string for the image
57
f = open(self.imageFilename(), 'w')
61
def _del_image(self, value):
62
# I usually wouldn't include a method like this, but for
63
# instructional purposes...
64
os.unlink(self.imageFilename())
66
_doc_image = 'The headshot for the person'
68
class PhoneNumber(SQLObject):
70
_columns = [KeyCol('personID', foreignKey='Person'),
71
StringCol('phoneNumber', length=14, notNull=1),
73
enumValues=['home', 'work', 'mobile'], notNull=1)]
75
class Role(SQLObject):
77
_columns = [StringCol('name', length=30)]
78
_joins = [RelatedJoin('Person')]
81
tableClasses = [Person, PhoneNumber, Role]
83
############################################################
84
## Process command-line arguments:
85
############################################################
91
for table in tableClasses:
92
table.dropTable(ifExists=True)
95
for table in tableClasses:
96
table.createTable(ifExists=True)
99
for table in tableClasses:
100
table.clearTable(ifExists=True)
103
############################################################
105
############################################################
108
>>> p = Person.new(firstName="John", lastName="Doe", username="johnd")
110
<Person 1 firstName='John' middleInitial=None lastName='Doe'>
111
>>> print p.firstName
113
>>> p.middleInitial = 'Q'
114
>>> print p.middleInitial
116
>>> p2 = Person(p.id)
118
<Person 1 firstName='John' middleInitial='Q' lastName='Doe'>
121
>>> p3 = Person.byUsername("johnd")
123
<Person 1 firstName='John' middleInitial='Q' lastName='Doe'>
127
>>> r = Role.new(name="editor")
128
>>> p = list(Person.select('all'))[-1]
131
[<Role 1 name='editor'>]
133
[<Person 1 firstName='John' middleInitial='Q' lastName='Doe'>]
134
>>> r.removePerson(p)
137
>>> phone = PhoneNumber.new(person=p, phoneNumber='773-555-1023', phoneType='home')
138
>>> print p.phoneNumbers
142
>>> peeps1 = Person.select(Person.q.firstName=="John")
143
>>> print [p.id for p in peeps1]
144
>>> peeps2 = Person.select(AND(PhoneNumber.q.personID == Person.q.id, PhoneNumber.q.phoneNumber.startswith('612')))
145
>>> print [p.id for p in peeps2]
146
>>> peeps3 = Person.select("phone_number.id = person.id AND phone_number.phone_number LIKE '773%'", clauseTables=['phone_number'])[2:5]
147
>>> print [p.id for p in peeps3]
151
############################################################
153
############################################################
156
lines = s.split('\n')
158
if line.startswith('>>> '):