You can pass this sort of thing as a clause in any db function. Otherwise, you can pass a dictionary to the keyword argument `vars` and the function will call reparam for you. Internally, consists of `items`, which is a list of strings and SQLParams, which get concatenated to p
| 129 | |
| 130 | |
| 131 | class SQLQuery: |
| 132 | """ |
| 133 | You can pass this sort of thing as a clause in any db function. |
| 134 | Otherwise, you can pass a dictionary to the keyword argument `vars` |
| 135 | and the function will call reparam for you. |
| 136 | |
| 137 | Internally, consists of `items`, which is a list of strings and |
| 138 | SQLParams, which get concatenated to produce the actual query. |
| 139 | """ |
| 140 | |
| 141 | __slots__ = ["items"] |
| 142 | |
| 143 | # tested in sqlquote's docstring |
| 144 | def __init__(self, items=None): |
| 145 | r"""Creates a new SQLQuery. |
| 146 | |
| 147 | >>> SQLQuery("x") |
| 148 | <sql: 'x'> |
| 149 | >>> q = SQLQuery(['SELECT * FROM ', 'test', ' WHERE x=', SQLParam(1)]) |
| 150 | >>> q |
| 151 | <sql: 'SELECT * FROM test WHERE x=1'> |
| 152 | >>> q.query(), q.values() |
| 153 | ('SELECT * FROM test WHERE x=%s', [1]) |
| 154 | >>> SQLQuery(SQLParam(1)) |
| 155 | <sql: '1'> |
| 156 | """ |
| 157 | if items is None: |
| 158 | self.items = [] |
| 159 | elif isinstance(items, list): |
| 160 | self.items = items |
| 161 | elif isinstance(items, SQLParam): |
| 162 | self.items = [items] |
| 163 | elif isinstance(items, SQLQuery): |
| 164 | self.items = list(items.items) |
| 165 | else: |
| 166 | self.items = [items] |
| 167 | |
| 168 | # Take care of SQLLiterals |
| 169 | for i, item in enumerate(self.items): |
| 170 | if isinstance(item, SQLParam) and isinstance(item.value, SQLLiteral): |
| 171 | self.items[i] = item.value.v |
| 172 | |
| 173 | def append(self, value): |
| 174 | self.items.append(value) |
| 175 | |
| 176 | def __add__(self, other): |
| 177 | if isinstance(other, str): |
| 178 | items = [other] |
| 179 | elif isinstance(other, SQLQuery): |
| 180 | items = other.items |
| 181 | else: |
| 182 | return NotImplemented |
| 183 | return SQLQuery(self.items + items) |
| 184 | |
| 185 | def __radd__(self, other): |
| 186 | if isinstance(other, str): |
| 187 | items = [other] |
| 188 | elif isinstance(other, SQLQuery): |
no outgoing calls
no test coverage detected