| 12345678910111213141516171819202122232425262728293031323334 |
- '''
- Formatter special pour les requetes SQL
- usage:
- Sql = SqlFormatter()
- my_query = Sql.format("SELECT {} FROM {}", "my_field", "tblname")
- @author: olivier.massot
- '''
- import string
- class SqlFormatter(string.Formatter):
- def format_field(self, value, format_spec):
- if value is None:
- return "Null"
- if format_spec == "date":
- return "#{:%Y-%m-%d %H:%M:%S}#".format(value)
- elif format_spec == "text":
- return "'{}'".format(SqlFormatter._escape(str(value)))
- elif format_spec == "float":
- return str(value).replace(",", ".")
- else:
- return SqlFormatter._escape(value.__format__(format_spec))
- @staticmethod
- def _escape(instr):
- return instr.replace("'", "''").replace("\"", "''").replace("\t", " ")
|