Execute SQL query `sql_query` using dictionary `vars` to interpolate it. If `processed=True`, `vars` is a `reparam`-style list to use instead of interpolating. >>> db = DB(None, {}) >>> db.query("SELECT * FROM foo", _test=True) <sql: 'SEL
(self, sql_query, vars=None, processed=False, _test=False)
| 791 | return None |
| 792 | |
| 793 | def query(self, sql_query, vars=None, processed=False, _test=False): |
| 794 | """ |
| 795 | Execute SQL query `sql_query` using dictionary `vars` to interpolate it. |
| 796 | If `processed=True`, `vars` is a `reparam`-style list to use |
| 797 | instead of interpolating. |
| 798 | |
| 799 | >>> db = DB(None, {}) |
| 800 | >>> db.query("SELECT * FROM foo", _test=True) |
| 801 | <sql: 'SELECT * FROM foo'> |
| 802 | >>> db.query("SELECT * FROM foo WHERE x = $x", vars=dict(x='f'), _test=True) |
| 803 | <sql: "SELECT * FROM foo WHERE x = 'f'"> |
| 804 | >>> db.query("SELECT * FROM foo WHERE x = " + sqlquote('f'), _test=True) |
| 805 | <sql: "SELECT * FROM foo WHERE x = 'f'"> |
| 806 | """ |
| 807 | if vars is None: |
| 808 | vars = {} |
| 809 | |
| 810 | if not processed and not isinstance(sql_query, SQLQuery): |
| 811 | sql_query = reparam(sql_query, vars) |
| 812 | |
| 813 | if _test: |
| 814 | return sql_query |
| 815 | |
| 816 | db_cursor = self._db_cursor() |
| 817 | self._db_execute(db_cursor, sql_query) |
| 818 | |
| 819 | if db_cursor.description: |
| 820 | out = self.create_result_set(db_cursor) |
| 821 | else: |
| 822 | out = db_cursor.rowcount |
| 823 | |
| 824 | if not self.ctx.transactions: |
| 825 | self.ctx.commit() |
| 826 | return out |
| 827 | |
| 828 | def create_result_set(self, cursor): |
| 829 | return ResultSet(cursor) |