Returns the quoted, escaped string (e.g., "'a bird\'s feathers'") for database entry. Anything that is not a string (e.g., an integer) is converted to string. Booleans are converted to "0" and "1", None is converted to "null". See also: Database.escape()
(value, quote=lambda string: "'%s'" % string.replace("'", "\\'"))
| 277 | return MySQLdb.escape_string(self.data) |
| 278 | |
| 279 | def _escape(value, quote=lambda string: "'%s'" % string.replace("'", "\\'")): |
| 280 | """ Returns the quoted, escaped string (e.g., "'a bird\'s feathers'") for database entry. |
| 281 | Anything that is not a string (e.g., an integer) is converted to string. |
| 282 | Booleans are converted to "0" and "1", None is converted to "null". |
| 283 | See also: Database.escape() |
| 284 | """ |
| 285 | # Note: use Database.escape() for MySQL/SQLITE-specific escape. |
| 286 | if isinstance(value, str): |
| 287 | # Strings are encoded as UTF-8. |
| 288 | try: value = value.encode("utf-8") |
| 289 | except: |
| 290 | pass |
| 291 | if value in ("current_timestamp",): |
| 292 | # Don't quote constants such as current_timestamp. |
| 293 | return value |
| 294 | if isinstance(value, basestring): |
| 295 | # Strings are quoted, single quotes are escaped according to the database engine. |
| 296 | return quote(value) |
| 297 | if isinstance(value, bool): |
| 298 | # Booleans are converted to "0" or "1". |
| 299 | return str(int(value)) |
| 300 | if isinstance(value, (int, long, float)): |
| 301 | # Numbers are converted to string. |
| 302 | return str(value) |
| 303 | if isinstance(value, datetime): |
| 304 | # Dates are formatted as string. |
| 305 | return quote(value.strftime(DEFAULT_DATE_FORMAT)) |
| 306 | if isinstance(value, type(None)): |
| 307 | # None is converted to NULL. |
| 308 | return "null" |
| 309 | if isinstance(value, Query): |
| 310 | # A Query is converted to "("+Query.SQL()+")" (=subquery). |
| 311 | return "(%s)" % value.SQL().rstrip(";") |
| 312 | if isinstance(value, _Binary): |
| 313 | # Binary data is escaped with attention to null bytes. |
| 314 | return "'%s'" % value.escape() |
| 315 | return value |
| 316 | |
| 317 | #### LIST FUNCTIONS ################################################################################ |
| 318 |