Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ("Select", "SimpleQuery") 

25 

26from typing import ( 

27 Any, 

28 List, 

29 Optional, 

30 Union, 

31 Type, 

32 TypeVar, 

33) 

34 

35import sqlalchemy 

36 

37 

38T = TypeVar("T") 

39 

40 

41class Select: 

42 """Tag class used to indicate that a field should be returned in 

43 a SELECT query. 

44 """ 

45 pass 

46 

47 

48Select.Or = Union[T, Type[Select]] 

49"""A type annotation for arguments that can take the `Select` type or some 

50other value. 

51""" 

52 

53 

54class SimpleQuery: 

55 """A struct that combines SQLAlchemy objects representing SELECT, FROM, 

56 and WHERE clauses. 

57 """ 

58 

59 def __init__(self): 

60 self.columns = [] 

61 self.where = [] 

62 self._from = None 

63 

64 def join(self, table: sqlalchemy.sql.FromClause, *, 

65 onclause: Optional[sqlalchemy.sql.ColumnElement] = None, 

66 isouter: bool = False, 

67 full: bool = False, 

68 **kwargs: Select.Or[Any]): 

69 """Add a table or subquery join to the query, possibly adding 

70 SELECT columns or WHERE expressions at the same time. 

71 

72 Parameters 

73 ---------- 

74 table : `sqlalchemy.sql.FromClause` 

75 Table or subquery to include. 

76 onclause : `sqlalchemy.sql.ColumnElement`, optional 

77 Expression used to join the new table or subquery to those already 

78 present. Passed directly to `sqlalchemy.sql.FromClause.join`, but 

79 ignored if this is the first call to `SimpleQuery.join`. 

80 isouter : `bool`, optional 

81 If `True`, make this an LEFT OUTER JOIN. Passed directly to 

82 `sqlalchemy.sql.FromClause.join`. 

83 full : `bool`, optional 

84 If `True`, make this a FULL OUTER JOIN. Passed directly to 

85 `sqlalchemy.sql.FromClause.join`. 

86 **kwargs 

87 Additional keyword arguments correspond to columns in the joined 

88 table or subquery. Values may be: 

89 

90 - `Select` (a special tag type) to indicate that this column 

91 should be added to the SELECT clause as a query result; 

92 - `None` to do nothing (equivalent to no keyword argument); 

93 - Any other value to add an equality constraint to the WHERE 

94 clause that constrains this column to the given value. Note 

95 that this cannot be used to add ``IS NULL`` constraints, because 

96 the previous condition for `None` is checked first. 

97 """ 

98 if self._from is None: 

99 self._from = table 

100 else: 

101 self._from = self._from.join(table, onclause=onclause, isouter=isouter, full=full) 

102 for name, arg in kwargs.items(): 

103 if arg is Select: 

104 self.columns.append(table.columns[name].label(name)) 

105 elif arg is not None: 

106 self.where.append(table.columns[name] == arg) 

107 

108 def combine(self) -> sqlalchemy.sql.Select: 

109 """Combine all terms into a single query object. 

110 

111 Returns 

112 ------- 

113 sql : `sqlalchemy.sql.Select` 

114 A SQLAlchemy object representing the full query. 

115 """ 

116 result = sqlalchemy.sql.select(self.columns) 

117 if self._from is not None: 

118 result = result.select_from(self._from) 

119 if self.where: 

120 result = result.where(sqlalchemy.sql.and_(*self.where)) 

121 return result 

122 

123 @property 

124 def from_(self) -> sqlalchemy.sql.FromClause: 

125 """The FROM clause of the query (`sqlalchemy.sql.FromClause`). 

126 

127 This property cannot be set. To add tables to the FROM clause, call 

128 `join`. 

129 """ 

130 return self._from 

131 

132 columns: List[sqlalchemy.sql.ColumnElement] 

133 """The columns in the SELECT clause 

134 (`list` [ `sqlalchemy.sql.ColumnElement` ]). 

135 """ 

136 

137 where: List[sqlalchemy.sql.ColumnElement] 

138 """Boolean expressions that will be combined with AND to form the WHERE 

139 clause (`list` [ `sqlalchemy.sql.ColumnElement` ]). 

140 """