コード例 #1
0
ファイル: parser_test.py プロジェクト: cjfuller/tinyquery
 def test_multi_clause_case(self):
     self.assert_parsed_select(
         'SELECT CASE WHEN x = 4 THEN 16 WHEN x = 5 THEN 25 END',
         tq_ast.Select(
             [
                 tq_ast.SelectField(
                     tq_ast.CaseExpression([
                         tq_ast.CaseClause(
                             tq_ast.BinaryOperator('=',
                                                   tq_ast.ColumnId('x'),
                                                   tq_ast.Literal(4)),
                             tq_ast.Literal(16)
                         ),
                         tq_ast.CaseClause(
                             tq_ast.BinaryOperator('=',
                                                   tq_ast.ColumnId('x'),
                                                   tq_ast.Literal(5)),
                             tq_ast.Literal(25)
                         ),
                     ]),
                     None
                 )
             ],
             None, None, None, None, None, None
         )
     )
コード例 #2
0
 def test_multi_part_join(self):
     self.assert_parsed_select(
         'SELECT t1.foo, t2.bar, t3.baz '
         'FROM table1 t1 LEFT OUTER JOIN EACH table2 t2 ON t1.id = t2.id '
         'JOIN table3 t3 ON t3.id = t1.id',
         tq_ast.Select(
             [
                 tq_ast.SelectField(tq_ast.ColumnId('t1.foo'), None, None),
                 tq_ast.SelectField(tq_ast.ColumnId('t2.bar'), None, None),
                 tq_ast.SelectField(tq_ast.ColumnId('t3.baz'), None, None)
             ],
             tq_ast.Join(tq_ast.TableId('table1', 't1'), [
                 tq_ast.PartialJoin(
                     tq_ast.TableId('table2', 't2'),
                     tq_ast.JoinType.LEFT_OUTER,
                     tq_ast.BinaryOperator('=', tq_ast.ColumnId('t1.id'),
                                           tq_ast.ColumnId('t2.id')),
                 ),
                 tq_ast.PartialJoin(
                     tq_ast.TableId('table3', 't3'),
                     tq_ast.JoinType.INNER,
                     tq_ast.BinaryOperator('=', tq_ast.ColumnId('t3.id'),
                                           tq_ast.ColumnId('t1.id')),
                 ),
             ]), None, None, None, None, None, None))
コード例 #3
0
ファイル: parser_test.py プロジェクト: nawalgupta/tinyquery
 def test_parens(self):
     self.assert_parsed_select(
         'SELECT 2 + (3 * 4)',
         tq_ast.Select([
             tq_ast.SelectField(
                 tq_ast.BinaryOperator(
                     '+', literal(2),
                     tq_ast.BinaryOperator('*', literal(3), literal(4))),
                 None)
         ], None, None, None, None, None, None))
コード例 #4
0
ファイル: parser_test.py プロジェクト: nawalgupta/tinyquery
 def test_where(self):
     self.assert_parsed_select(
         'SELECT foo + 2 FROM bar WHERE foo > 3',
         tq_ast.Select([
             tq_ast.SelectField(
                 tq_ast.BinaryOperator('+', tq_ast.ColumnId('foo'),
                                       tq_ast.Literal(2)), None)
         ], tq_ast.TableId('bar', None),
                       tq_ast.BinaryOperator('>', tq_ast.ColumnId('foo'),
                                             tq_ast.Literal(3)), None, None,
                       None, None))
コード例 #5
0
ファイル: parser_test.py プロジェクト: nawalgupta/tinyquery
 def test_arithmetic_operator_parsing(self):
     self.assert_parsed_select(
         'SELECT 1 * 2 + 3 / 4',
         tq_ast.Select([
             tq_ast.SelectField(
                 tq_ast.BinaryOperator(
                     '+', tq_ast.BinaryOperator('*', literal(1),
                                                literal(2)),
                     tq_ast.BinaryOperator('/', literal(3), literal(4))),
                 None)
         ], None, None, None, None, None, None))
コード例 #6
0
ファイル: parser_test.py プロジェクト: nawalgupta/tinyquery
 def test_operator_precedence(self):
     self.assert_parsed_select(
         'SELECT 2 + 3 * 4 + 5',
         tq_ast.Select([
             tq_ast.SelectField(
                 tq_ast.BinaryOperator(
                     '+',
                     tq_ast.BinaryOperator(
                         '+', literal(2),
                         tq_ast.BinaryOperator('*',
                                               literal(3), literal(4))),
                     literal(5)), None)
         ], None, None, None, None, None, None))
コード例 #7
0
ファイル: parser_test.py プロジェクト: cjfuller/tinyquery
 def test_left_outer_join(self):
     self.assert_parsed_select(
         'SELECT t1.foo, t2.bar '
         'FROM table1 t1 LEFT OUTER JOIN EACH table2 t2 ON t1.id = t2.id',
         tq_ast.Select([
             tq_ast.SelectField(tq_ast.ColumnId('t1.foo'), None),
             tq_ast.SelectField(tq_ast.ColumnId('t2.bar'), None)],
             tq_ast.Join(
                 tq_ast.TableId('table1', 't1'),
                 tq_ast.TableId('table2', 't2'),
                 tq_ast.BinaryOperator(
                     '=',
                     tq_ast.ColumnId('t1.id'),
                     tq_ast.ColumnId('t2.id')
                 ),
                 is_left_outer=True
             ),
             None,
             None,
             None,
             None,
             None
         )
     )
     self.assert_parsed_select(
         'SELECT t1.foo, t2.bar '
         'FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id',
         tq_ast.Select([
             tq_ast.SelectField(tq_ast.ColumnId('t1.foo'), None),
             tq_ast.SelectField(tq_ast.ColumnId('t2.bar'), None)],
             tq_ast.Join(
                 tq_ast.TableId('table1', 't1'),
                 tq_ast.TableId('table2', 't2'),
                 tq_ast.BinaryOperator(
                     '=',
                     tq_ast.ColumnId('t1.id'),
                     tq_ast.ColumnId('t2.id')
                 ),
                 is_left_outer=True
             ),
             None,
             None,
             None,
             None,
             None
         )
     )
コード例 #8
0
ファイル: parser_test.py プロジェクト: nawalgupta/tinyquery
 def test_select_comparison(self):
     self.assert_parsed_select(
         'SELECT foo = bar FROM baz',
         tq_ast.Select([
             tq_ast.SelectField(
                 tq_ast.BinaryOperator('=', tq_ast.ColumnId('foo'),
                                       tq_ast.ColumnId('bar')), None)
         ], tq_ast.TableId('baz', None), None, None, None, None, None))
コード例 #9
0
ファイル: parser_test.py プロジェクト: nawalgupta/tinyquery
 def test_join_each(self):
     self.assert_parsed_select(
         'SELECT 0 FROM table1 t1 JOIN EACH table2 t2 ON t1.foo = t2.bar',
         tq_ast.Select([tq_ast.SelectField(tq_ast.Literal(0), None)],
                       tq_ast.Join(tq_ast.TableId('table1', 't1'),
                                   tq_ast.TableId('table2', 't2'),
                                   tq_ast.BinaryOperator(
                                       '=', tq_ast.ColumnId('t1.foo'),
                                       tq_ast.ColumnId('t2.bar')),
                                   is_left_outer=False), None, None, None,
                       None, None))
コード例 #10
0
ファイル: parser_test.py プロジェクト: nawalgupta/tinyquery
 def test_multiple_select(self):
     self.assert_parsed_select(
         'SELECT a AS foo, b bar, a + 1 baz FROM test_table',
         tq_ast.Select([
             tq_ast.SelectField(tq_ast.ColumnId('a'), 'foo'),
             tq_ast.SelectField(tq_ast.ColumnId('b'), 'bar'),
             tq_ast.SelectField(
                 tq_ast.BinaryOperator('+', tq_ast.ColumnId('a'),
                                       tq_ast.Literal(1)), 'baz')
         ], tq_ast.TableId('test_table', None), None, None, None, None,
                       None))
コード例 #11
0
    def test_operator_precedence(self):
        self.assert_parsed_select(
            'SELECT 2 + 3 * 4 + 5',
            tq_ast.Select([
                tq_ast.SelectField(
                    tq_ast.BinaryOperator(
                        '+',
                        tq_ast.BinaryOperator(
                            '+', literal(2),
                            tq_ast.BinaryOperator('*',
                                                  literal(3), literal(4))),
                        literal(5)), None, None)
            ], None, None, None, None, None, None, None))

        self.assert_parsed_select(
            'SELECT FALSE OR "xyz" CONTAINS "y"',
            tq_ast.Select([
                tq_ast.SelectField(
                    tq_ast.BinaryOperator(
                        'or', literal(False),
                        tq_ast.BinaryOperator('contains', literal('xyz'),
                                              literal('y'))), None, None)
            ], None, None, None, None, None, None, None))
コード例 #12
0
def p_expression_binary(p):
    """expression : expression PLUS expression
                  | expression MINUS expression
                  | expression STAR expression
                  | expression DIVIDED_BY expression
                  | expression MOD expression
                  | expression EQUALS expression
                  | expression NOT_EQUAL expression
                  | expression GREATER_THAN expression
                  | expression LESS_THAN expression
                  | expression GREATER_THAN_OR_EQUAL expression
                  | expression LESS_THAN_OR_EQUAL expression
                  | expression AND expression
                  | expression OR expression
    """
    p[0] = tq_ast.BinaryOperator(p[2], p[1], p[3])
コード例 #13
0
 def test_join(self):
     self.assert_parsed_select(
         'SELECT t1.foo, t2.bar '
         'FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id',
         tq_ast.Select(
             [
                 tq_ast.SelectField(tq_ast.ColumnId('t1.foo'), None, None),
                 tq_ast.SelectField(tq_ast.ColumnId('t2.bar'), None, None)
             ],
             tq_ast.Join(tq_ast.TableId('table1', 't1'), [
                 tq_ast.PartialJoin(
                     tq_ast.TableId('table2', 't2'),
                     tq_ast.JoinType.INNER,
                     tq_ast.BinaryOperator('=', tq_ast.ColumnId('t1.id'),
                                           tq_ast.ColumnId('t2.id')),
                 ),
             ]), None, None, None, None, None, None))