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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

from __future__ import annotations 

 

__all__ = ["OracleDatabase"] 

 

from contextlib import closing, contextmanager 

import copy 

from typing import Optional 

 

import sqlalchemy 

 

from ..interfaces import Database, ReadOnlyDatabaseError 

from .. import ddl 

from ..nameShrinker import NameShrinker 

 

 

class _Merge(sqlalchemy.sql.expression.Executable, sqlalchemy.sql.ClauseElement): 

"""A SQLAlchemy query that compiles to a MERGE invocation that is the 

equivalent of PostgreSQL and SQLite's INSERT ... ON CONFLICT REPLACE on the 

primary key constraint for the table. 

""" 

 

def __init__(self, table): 

super().__init__() 

self.table = table 

 

 

@sqlalchemy.ext.compiler.compiles(_Merge, "oracle") 

def _merge(merge, compiler, **kw): 

"""Generate MERGE query for inserting or updating records. 

""" 

table = merge.table 

preparer = compiler.preparer 

 

allColumns = [col.name for col in table.columns] 

pkColumns = [col.name for col in table.primary_key] 

nonPkColumns = [col for col in allColumns if col not in pkColumns] 

 

# To properly support type decorators defined in core/schema.py we need 

# to pass column type to `bindparam`. 

selectColumns = [sqlalchemy.sql.bindparam(col.name, type_=col.type).label(col.name) 

for col in table.columns] 

selectClause = sqlalchemy.sql.select(selectColumns) 

 

tableAlias = table.alias("t") 

tableAliasText = compiler.process(tableAlias, asfrom=True, **kw) 

selectAlias = selectClause.alias("d") 

selectAliasText = compiler.process(selectAlias, asfrom=True, **kw) 

 

condition = sqlalchemy.sql.and_( 

*[tableAlias.columns[col] == selectAlias.columns[col] for col in pkColumns] 

) 

conditionText = compiler.process(condition, **kw) 

 

query = f"MERGE INTO {tableAliasText}" \ 

f"\nUSING {selectAliasText}" \ 

f"\nON ({conditionText})" 

updates = [] 

for col in nonPkColumns: 

src = compiler.process(selectAlias.columns[col], **kw) 

dst = compiler.process(tableAlias.columns[col], **kw) 

updates.append(f"{dst} = {src}") 

updates = ", ".join(updates) 

query += f"\nWHEN MATCHED THEN UPDATE SET {updates}" 

 

insertColumns = ", ".join([preparer.format_column(col) for col in table.columns]) 

insertValues = ", ".join([compiler.process(selectAlias.columns[col], **kw) for col in allColumns]) 

 

query += f"\nWHEN NOT MATCHED THEN INSERT ({insertColumns}) VALUES ({insertValues})" 

return query 

 

 

class OracleDatabase(Database): 

"""An implementation of the `Database` interface for Oracle. 

 

Parameters 

---------- 

connection : `sqlalchemy.engine.Connection` 

An existing connection created by a previous call to `connect`. 

origin : `int` 

An integer ID that should be used as the default for any datasets, 

quanta, or other entities that use a (autoincrement, origin) compound 

primary key. 

namespace : `str`, optional 

The namespace (schema) this database is associated with. If `None`, 

the default schema for the connection is used (which may be `None`). 

writeable : `bool`, optional 

If `True`, allow write operations on the database, including 

``CREATE TABLE``. 

prefix : `str`, optional 

Prefix to add to all table names, effectively defining a virtual 

schema that can coexist with others within the same actual database 

schema. This prefix must not be used in the un-prefixed names of 

tables. 

""" 

 

def __init__(self, *, connection: sqlalchemy.engine.Connection, origin: int, 

namespace: Optional[str] = None, writeable: bool = True, prefix: Optional[str] = None): 

# Get the schema that was included/implicit in the URI we used to 

# connect. 

dbapi = connection.engine.raw_connection() 

namespace = dbapi.current_schema 

super().__init__(connection=connection, origin=origin, namespace=namespace) 

self._writeable = writeable 

self.dsn = dbapi.dsn 

self.prefix = prefix 

self._shrinker = NameShrinker(connection.engine.dialect.max_identifier_length) 

 

@classmethod 

def connect(cls, uri: str, *, writeable: bool = True) -> sqlalchemy.engine.Connection: 

connection = sqlalchemy.engine.create_engine(uri, pool_size=1).connect() 

# Work around SQLAlchemy assuming that the Oracle limit on identifier 

# names is even shorter than it is after 12.2. 

oracle_ver = connection.engine.dialect._get_server_version_info(connection) 

if oracle_ver < (12, 2): 

raise RuntimeError("Oracle server version >= 12.2 required.") 

connection.engine.dialect.max_identifier_length = 128 

return connection 

 

@classmethod 

def fromConnection(cls, connection: sqlalchemy.engine.Connection, *, origin: int, 

namespace: Optional[str] = None, writeable: bool = True) -> Database: 

return cls(connection=connection, origin=origin, writeable=writeable, namespace=namespace) 

 

@contextmanager 

def transaction(self, *, interrupting: bool = False) -> None: 

with super().transaction(interrupting=interrupting): 

if not self.isWriteable(): 

with closing(self._connection.connection.cursor()) as cursor: 

cursor.execute("SET TRANSACTION READ ONLY") 

yield 

 

def isWriteable(self) -> bool: 

return self._writeable 

 

def __str__(self) -> str: 

if self.namespace is None: 

name = self.dsn 

else: 

name = f"{self.dsn:self.namespace}" 

return f"Oracle@{name}" 

 

def shrinkDatabaseEntityName(self, original: str) -> str: 

return self._shrinker.shrink(original) 

 

def expandDatabaseEntityName(self, shrunk: str) -> str: 

return self._shrinker.expand(shrunk) 

 

def _convertForeignKeySpec(self, table: str, spec: ddl.ForeignKeySpec, metadata: sqlalchemy.MetaData, 

**kwds) -> sqlalchemy.schema.ForeignKeyConstraint: 

if self.prefix is not None: 

spec = copy.copy(spec) 

spec.table = self.prefix + spec.table 

return super()._convertForeignKeySpec(table, spec, metadata, **kwds) 

 

def _convertTableSpec(self, name: str, spec: ddl.TableSpec, metadata: sqlalchemy.MetaData, 

**kwds) -> sqlalchemy.schema.Table: 

if self.prefix is not None and not name.startswith(self.prefix): 

name = self.prefix + name 

return super()._convertTableSpec(name, spec, metadata, **kwds) 

 

def getExistingTable(self, name: str, spec: ddl.TableSpec) -> Optional[sqlalchemy.schema.Table]: 

if self.prefix is not None and not name.startswith(self.prefix): 

name = self.prefix + name 

return super().getExistingTable(name, spec) 

 

def replace(self, table: sqlalchemy.schema.Table, *rows: dict): 

if not self.isWriteable(): 

raise ReadOnlyDatabaseError(f"Attempt to replace into read-only database '{self}'.") 

self._connection.execute(_Merge(table), *rows) 

 

prefix: Optional[str] 

"""A prefix included in all table names to simulate a database namespace 

(`str` or `None`). 

""" 

 

dsn: str 

"""The TNS entry of the database this instance is connected to (`str`). 

"""