@@ -205,3 +205,52 @@ def test_single_quotes_with_linebreaks(): # issue118
205205 assert len (p ) == 1
206206 assert p [0 ].ttype is T .String .Single
207207
208+
209+ def test_array_indexed_column ():
210+ # Make sure we still parse sqlite style escapes
211+ p = sqlparse .parse ('[col1],[col2]' )[0 ].tokens
212+ assert (len (p ) == 1
213+ and isinstance (p [0 ], sqlparse .sql .IdentifierList )
214+ and [id .get_name () for id in p [0 ].get_identifiers ()]
215+ == ['[col1]' , '[col2]' ])
216+
217+ p = sqlparse .parse ('[col1]+[col2]' )[0 ]
218+ types = [tok .ttype for tok in p .flatten ()]
219+ assert types == [T .Name , T .Operator , T .Name ]
220+
221+ p = sqlparse .parse ('col[1]' )[0 ].tokens
222+ assert (len (p ) == 1
223+ and tuple (p [0 ].get_array_indices ()) == ('1' ,)
224+ and p [0 ].get_name () == 'col' )
225+
226+ p = sqlparse .parse ('col[1][1:5] as mycol' )[0 ].tokens
227+ assert (len (p ) == 1
228+ and tuple (p [0 ].get_array_indices ()) == ('1' , '1:5' )
229+ and p [0 ].get_name () == 'mycol'
230+ and p [0 ].get_real_name () == 'col' )
231+
232+ p = sqlparse .parse ('col[1][other_col]' )[0 ].tokens
233+ assert len (p ) == 1 and tuple (p [0 ].get_array_indices ()) == ('1' , 'other_col' )
234+
235+ sql = 'SELECT col1, my_1d_array[2] as alias1, my_2d_array[2][5] as alias2'
236+ p = sqlparse .parse (sql )[0 ].tokens
237+ assert len (p ) == 3 and isinstance (p [2 ], sqlparse .sql .IdentifierList )
238+ ids = list (p [2 ].get_identifiers ())
239+ assert (ids [0 ].get_name () == 'col1'
240+ and tuple (ids [0 ].get_array_indices ()) == ()
241+ and ids [1 ].get_name () == 'alias1'
242+ and ids [1 ].get_real_name () == 'my_1d_array'
243+ and tuple (ids [1 ].get_array_indices ()) == ('2' ,)
244+ and ids [2 ].get_name () == 'alias2'
245+ and ids [2 ].get_real_name () == 'my_2d_array'
246+ and tuple (ids [2 ].get_array_indices ()) == ('2' , '5' ))
247+
248+
249+ def test_typed_array_definition ():
250+ # array indices aren't grouped with builtins, but make sure we can extract
251+ # indentifer names
252+ p = sqlparse .parse ('x int, y int[], z int' )[0 ]
253+ names = [x .get_name () for x in p .get_sublists ()]
254+ assert names == ['x' , 'y' , 'z' ]
255+
256+
0 commit comments