| 1056 | def function_get_values_iterator(opts, klass, **kwargs): |
| 1057 | """ |
| 1058 | Return SELECT results as a list of dicts rather than as objects. |
| 1059 | Example use: "SELECT DISTINCT year, month FROM blog ORDER BY year, month" |
| 1060 | """ |
| 1061 | # kwargs['select'] is a dictionary, and dictionaries' key order is |
| 1062 | # undefined, so we convert it to a list of tuples internally. |
| 1063 | kwargs['select'] = kwargs.get('select', {}).items() |
| 1064 | |
| 1065 | cursor = db.db.cursor() |
| 1066 | _, sql, params = function_get_sql_clause(opts, **kwargs) |
| 1067 | # Allow user to specify a specific list of fields to fetch |
| 1068 | fields = kwargs.get('fields') |
| 1069 | if isinstance(fields, basestring): |
| 1070 | # Be forgiving of mistakes: convert to tuple |
| 1071 | fields = (fields,) |
| 1072 | if not fields: |
| 1073 | # Default to all fields |
| 1074 | fields = [f.name for f in opts.fields] |
| 1075 | select = ['%s.%s' % (opts.db_table, f) for f in fields] |
| 1076 | cursor.execute("SELECT " + (kwargs.get('distinct') and "DISTINCT " or "") + ",".join(select) + sql, params) |
| 1077 | while 1: |
| 1078 | rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE) |
| 1079 | if not rows: |
| 1080 | raise StopIteration |
| 1081 | for row in rows: |
| 1082 | yield dict(zip(fields, row)) |
| 1083 | |
| 1084 | def function_get_values_list(opts, klass, **kwargs): |
| 1085 | return list(function_get_values_iterator(opts, klass, **kwargs)) |
| 1086 | |
1120 | | if kwarg in ('order_by', 'limit', 'offset', 'select_related', 'distinct', 'select', 'tables', 'where', 'params'): |
| 1157 | if kwarg in ('order_by', 'limit', 'offset', 'select_related', 'distinct', 'select', 'tables', 'where', 'params', 'fields'): |