Take a sequence of N fields and a sequence of M rows of values, and generate placeholder SQL and parameters for each field and value. Return a pair containing: * a sequence of M rows of N SQL placeholder strings, and * a sequence of M rows of corresponding
(self, fields, value_rows)
| 1755 | return field.pre_save(obj, add=True) |
| 1756 | |
| 1757 | def assemble_as_sql(self, fields, value_rows): |
| 1758 | """ |
| 1759 | Take a sequence of N fields and a sequence of M rows of values, and |
| 1760 | generate placeholder SQL and parameters for each field and value. |
| 1761 | Return a pair containing: |
| 1762 | * a sequence of M rows of N SQL placeholder strings, and |
| 1763 | * a sequence of M rows of corresponding parameter values. |
| 1764 | |
| 1765 | Each placeholder string may contain any number of '%s' interpolation |
| 1766 | strings, and each parameter row will contain exactly as many params |
| 1767 | as the total number of '%s's in the corresponding placeholder row. |
| 1768 | """ |
| 1769 | if not value_rows: |
| 1770 | return [], [] |
| 1771 | |
| 1772 | # list of (sql, [params]) tuples for each object to be saved |
| 1773 | # Shape: [n_objs][n_fields][2] |
| 1774 | get_placeholder_sqls = [ |
| 1775 | getattr(field, "get_placeholder_sql", None) for field in fields |
| 1776 | ] |
| 1777 | rows_of_fields_as_sql = ( |
| 1778 | ( |
| 1779 | self.field_as_sql(field, get_placeholder_sql, value) |
| 1780 | for field, get_placeholder_sql, value in zip( |
| 1781 | fields, get_placeholder_sqls, row |
| 1782 | ) |
| 1783 | ) |
| 1784 | for row in value_rows |
| 1785 | ) |
| 1786 | |
| 1787 | # tuple like ([sqls], [[params]s]) for each object to be saved |
| 1788 | # Shape: [n_objs][2][n_fields] |
| 1789 | sql_and_param_pair_rows = (zip(*row) for row in rows_of_fields_as_sql) |
| 1790 | |
| 1791 | # Extract separate lists for placeholders and params. |
| 1792 | # Each of these has shape [n_objs][n_fields] |
| 1793 | placeholder_rows, param_rows = zip(*sql_and_param_pair_rows) |
| 1794 | |
| 1795 | # Params for each field are still lists, and need to be flattened. |
| 1796 | param_rows = [[p for ps in row for p in ps] for row in param_rows] |
| 1797 | |
| 1798 | return placeholder_rows, param_rows |
| 1799 | |
| 1800 | def as_sql(self): |
| 1801 | qn = self.quote_name |