1
# -*- test-case-name: twisted.test.test_enterprise -*-
2
# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
3
# See LICENSE for details.
8
from twisted.python.versions import Version, getVersionString
9
from twisted.python.deprecate import deprecated
10
from twisted.enterprise.adbapi import _safe
12
# Common deprecation decorator used for all deprecations.
13
_deprecatedVersion = Version("Twisted", 8, 0, 0)
14
_releasedDeprecation = deprecated(_deprecatedVersion)
17
"twisted.enterprise.util is deprecated since %s." % (
18
getVersionString(_deprecatedVersion),),
19
category=DeprecationWarning)
43
"timestamp": USEQUOTE,
47
class DBError(Exception):
54
def getKeyColumn(rowClass, name):
56
for keyColumn, type in rowClass.rowKeyColumns:
57
if lcname == keyColumn.lower():
60
getKeyColumn = _releasedDeprecation(getKeyColumn)
64
def quote(value, typeCode, string_escaper=_safe):
65
"""Add quotes for text types and no quotes for integer types.
66
NOTE: uses Postgresql type codes.
68
q = dbTypeMap.get(typeCode, None)
70
raise DBError("Type %s not known" % typeCode)
76
if typeCode.startswith('bool'):
81
if typeCode == "bytea":
92
l.append("\\%03o" % i)
95
if not isinstance(value, types.StringType) and \
96
not isinstance(value, types.UnicodeType):
98
return "'%s'" % string_escaper(value)
99
quote = _releasedDeprecation(quote)
104
Make a string safe to include in an SQL statement.
108
safe = _releasedDeprecation(safe)
111
def makeKW(rowClass, args):
112
"""Utility method to construct a dictionary for the attributes
113
of an object from set of args. This also fixes the case of column names.
116
for i in range(0,len(args)):
117
columnName = rowClass.dbColumns[i][0].lower()
118
for attr in rowClass.rowColumns:
119
if attr.lower() == columnName:
123
makeKW = _releasedDeprecation(makeKW)
126
def defaultFactoryMethod(rowClass, data, kw):
127
"""Used by loadObjects to create rowObject instances.
129
newObject = rowClass()
130
newObject.__dict__.update(kw)
132
defaultFactoryMethod = _releasedDeprecation(defaultFactoryMethod)
139
Info about a table/class and it's relationships. Also serves as a container for
142
def __init__(self, rc):
144
self.rowTableName = rc.rowTableName
145
self.rowKeyColumns = rc.rowKeyColumns
146
self.rowColumns = rc.rowColumns
148
if hasattr(rc, "rowForeignKeys"):
149
self.rowForeignKeys = rc.rowForeignKeys
151
self.rowForeignKeys = []
153
if hasattr(rc, "rowFactoryMethod"):
154
if rc.rowFactoryMethod:
155
self.rowFactoryMethod = rc.rowFactoryMethod
157
self.rowFactoryMethod = [defaultFactoryMethod]
159
self.rowFactoryMethod = [defaultFactoryMethod]
161
self.updateSQL = None
162
self.deleteSQL = None
163
self.insertSQL = None
164
self.relationships = []
167
def addForeignKey(self, childColumns, parentColumns, childRowClass, containerMethod, autoLoad):
168
"""This information is attached to the "parent" table
169
childColumns - columns of the "child" table
170
parentColumns - columns of the "parent" table, the one being joined to... the "foreign" table
172
self.relationships.append( _TableRelationship(childColumns, parentColumns,
173
childRowClass, containerMethod, autoLoad) )
175
def getRelationshipFor(self, tableName):
176
for relationship in self.relationships:
177
if relationship.childRowClass.rowTableName == tableName:
181
class _TableRelationship:
184
A foreign key relationship between two tables.
192
self.childColumns = childColumns
193
self.parentColumns = parentColumns
194
self.childRowClass = childRowClass
195
self.containerMethod = containerMethod
196
self.autoLoad = autoLoad
199
__all__ = ['NOQUOTE', 'USEQUOTE', 'dbTypeMap', 'DBError', 'getKeyColumn',