You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Converters from <ahref="sqlobject/converters.py.html" class="reference external">sqlobject/converters.py</a> aren't visible to the user. They
85
-
are used behind the scene to convert object to SQL strings. The most
86
-
elaborated converter there is StringConverters. Yes, it converts strings
87
-
to strings. It converts python strings to SQL strings using
88
-
backend-specific quoting rules.</p>
88
+
SQLObject's metaclass. The two classes are usually named <ttclass="docutils literal">Col</tt> and
89
+
<ttclass="docutils literal">SOCol</tt>; for example, <ttclass="docutils literal">BoolCol</tt> and <ttclass="docutils literal">SOBoolCol</tt>. User-visible
90
+
classes, descendants of <ttclass="docutils literal">Col</tt>, seldom contain any code; the main code
91
+
for a column is in <ttclass="docutils literal">SOCol</tt> descendants and in validators.</p>
92
+
<p>Every column has a list of validators. Validators validate input data
93
+
and convert input data to python data and back. Every validator must
94
+
have methods <ttclass="docutils literal">from_python</tt> and <ttclass="docutils literal">to_python</tt>. The former converts data
95
+
from python to internal representation that will be converted by
96
+
converters to SQL strings. The latter converts data from SQL data to
97
+
python. Also please bear in mind that validators can receive <ttclass="docutils literal">None</tt>
98
+
(for SQL <ttclass="docutils literal">NULL</tt>) and <ttclass="docutils literal">SQLExpression</tt> (an object that represents
99
+
SQLObject expressions); both objects must be passed unchanged by
100
+
validators.</p>
101
+
<p>Converters from <ahref="sqlobject/converters.py.html" class="reference external">sqlobject/converters.py</a> aren't visible to users. They
102
+
are used behind the scene to convert objects returned by validators to
103
+
backend-specific SQL strings. The most elaborated converter is
104
+
<ttclass="docutils literal">StringLikeConverter</tt>. Yes, it converts strings to strings. It
105
+
converts python strings to SQL strings using backend-specific quoting
106
+
rules.</p>
107
+
<p>Let look into <ttclass="docutils literal">BoolCol</tt> as an example. The very <ttclass="docutils literal">BoolCol</tt> doesn't
108
+
have any code. <ttclass="docutils literal">SOBoolCol</tt> has a method to create <ttclass="docutils literal">BoolValidator</tt>
109
+
and methods to create backend-specific column type. <ttclass="docutils literal">BoolValidator</tt>
110
+
has identical methods <ttclass="docutils literal">from_python</tt> and <ttclass="docutils literal">to_python</tt>; the method
111
+
passes <ttclass="docutils literal">None</tt>, <ttclass="docutils literal">SQLExpression</tt> and bool values unchanged; int and
112
+
objects that have method <ttclass="docutils literal">__nonzero__</tt> are converted to bool; other
113
+
objects trigger validation error. Bool values that are returned by call
114
+
to <ttclass="docutils literal">from_python</tt> will be converted to SQL strings by
115
+
<ttclass="docutils literal">BoolConverter</tt>; bool values from <ttclass="docutils literal">to_python</tt> (is is supposed they
116
+
are originated from the backend via DB API driver) are passed to the
117
+
application.</p>
118
+
<p>Objects that are returned from <ttclass="docutils literal">from_python</tt> must be registered with
119
+
converters. Another approach for <ttclass="docutils literal">from_python</tt> is to return an object
120
+
that has <ttclass="docutils literal">__sqlrepr__</tt> method. Such objects convert to SQL strings
0 commit comments