| 67 | |
| 68 | |
| 69 | class QueryGenerator(object): |
| 70 | |
| 71 | def __init__(self, query_profile): |
| 72 | self.profile = query_profile |
| 73 | self.queries_under_construction = list() |
| 74 | self.max_nested_query_count = None |
| 75 | self.cur_id = 0 |
| 76 | |
| 77 | def get_next_id(self): |
| 78 | self.cur_id += 1 |
| 79 | return 'a' + str(self.cur_id) |
| 80 | |
| 81 | @property |
| 82 | def current_query(self): |
| 83 | if self.queries_under_construction: |
| 84 | return self.queries_under_construction[-1] |
| 85 | |
| 86 | @property |
| 87 | def root_query(self): |
| 88 | if self.queries_under_construction: |
| 89 | return self.queries_under_construction[0] |
| 90 | |
| 91 | def clear_state(self): |
| 92 | # This function clears the state from the previous query. |
| 93 | self.cur_id = 0 |
| 94 | self.profile.query = None |
| 95 | self.max_nested_query_count = None |
| 96 | |
| 97 | def generate_statement(self, |
| 98 | table_exprs, |
| 99 | allow_with_clause=True, |
| 100 | allow_union_clause=True, |
| 101 | select_item_data_types=None, |
| 102 | required_select_item_type=None, |
| 103 | required_table_expr_col_type=None, |
| 104 | require_aggregate=None, |
| 105 | dml_table=None, |
| 106 | table_alias_prefix='t'): |
| 107 | '''Create a random query using various language features. |
| 108 | |
| 109 | The initial call to this method should only use tables in the table_exprs |
| 110 | parameter, and not inline views or WITH definitions. The other types of |
| 111 | table exprs may be added as part of the query generation. |
| 112 | |
| 113 | Due to implementation limitations, nested queries are not distributed evenly by |
| 114 | default even if the query profile assigns an equal likelihood of use to each |
| 115 | possible nested query decision. This is because the implementation chooses nested |
| 116 | queries in a fixed order (WITH -> FROM -> WHERE). For example if only one nested |
| 117 | query were allowed and each clause had a 50% chance of using a subquery, the |
| 118 | resulting set of generated queries would contain a subquery in the WITH clause |
| 119 | 50% of the time, in the FROM 25% of the time, and WHERE 12.5% of the time. If the |
| 120 | max nested queries were two, it's most likely that the generated query would |
| 121 | contain a WITH clause which has the second nested query within it.... |
| 122 | |
| 123 | If select_item_data_types is specified it must be a sequence or iterable of |
| 124 | DataType. The generated query.select_clause.select will have data types suitable |
| 125 | for use in a UNION. |
| 126 |
no outgoing calls