Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. """
  2. Oracle database backend for Django.
  3. Requires cx_Oracle: http://cx-oracle.sourceforge.net/
  4. """
  5. import datetime
  6. import decimal
  7. import os
  8. import platform
  9. from django.conf import settings
  10. from django.core.exceptions import ImproperlyConfigured
  11. from django.db import utils
  12. from django.db.backends.base.base import BaseDatabaseWrapper
  13. from django.utils.encoding import force_bytes, force_text
  14. from django.utils.functional import cached_property
  15. def _setup_environment(environ):
  16. # Cygwin requires some special voodoo to set the environment variables
  17. # properly so that Oracle will see them.
  18. if platform.system().upper().startswith('CYGWIN'):
  19. try:
  20. import ctypes
  21. except ImportError as e:
  22. raise ImproperlyConfigured("Error loading ctypes: %s; "
  23. "the Oracle backend requires ctypes to "
  24. "operate correctly under Cygwin." % e)
  25. kernel32 = ctypes.CDLL('kernel32')
  26. for name, value in environ:
  27. kernel32.SetEnvironmentVariableA(name, value)
  28. else:
  29. os.environ.update(environ)
  30. _setup_environment([
  31. # Oracle takes client-side character set encoding from the environment.
  32. ('NLS_LANG', '.AL32UTF8'),
  33. # This prevents unicode from getting mangled by getting encoded into the
  34. # potentially non-unicode database character set.
  35. ('ORA_NCHAR_LITERAL_REPLACE', 'TRUE'),
  36. ])
  37. try:
  38. import cx_Oracle as Database
  39. except ImportError as e:
  40. raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e)
  41. # Some of these import cx_Oracle, so import them after checking if it's installed.
  42. from .client import DatabaseClient # NOQA isort:skip
  43. from .creation import DatabaseCreation # NOQA isort:skip
  44. from .features import DatabaseFeatures # NOQA isort:skip
  45. from .introspection import DatabaseIntrospection # NOQA isort:skip
  46. from .operations import DatabaseOperations # NOQA isort:skip
  47. from .schema import DatabaseSchemaEditor # NOQA isort:skip
  48. from .utils import Oracle_datetime # NOQA isort:skip
  49. from .validation import DatabaseValidation # NOQA isort:skip
  50. class _UninitializedOperatorsDescriptor:
  51. def __get__(self, instance, cls=None):
  52. # If connection.operators is looked up before a connection has been
  53. # created, transparently initialize connection.operators to avert an
  54. # AttributeError.
  55. if instance is None:
  56. raise AttributeError("operators not available as class attribute")
  57. # Creating a cursor will initialize the operators.
  58. instance.cursor().close()
  59. return instance.__dict__['operators']
  60. class DatabaseWrapper(BaseDatabaseWrapper):
  61. vendor = 'oracle'
  62. display_name = 'Oracle'
  63. # This dictionary maps Field objects to their associated Oracle column
  64. # types, as strings. Column-type strings can contain format strings; they'll
  65. # be interpolated against the values of Field.__dict__ before being output.
  66. # If a column type is set to None, it won't be included in the output.
  67. #
  68. # Any format strings starting with "qn_" are quoted before being used in the
  69. # output (the "qn_" prefix is stripped before the lookup is performed.
  70. data_types = {
  71. 'AutoField': 'NUMBER(11) GENERATED BY DEFAULT ON NULL AS IDENTITY',
  72. 'BigAutoField': 'NUMBER(19) GENERATED BY DEFAULT ON NULL AS IDENTITY',
  73. 'BinaryField': 'BLOB',
  74. 'BooleanField': 'NUMBER(1)',
  75. 'CharField': 'NVARCHAR2(%(max_length)s)',
  76. 'DateField': 'DATE',
  77. 'DateTimeField': 'TIMESTAMP',
  78. 'DecimalField': 'NUMBER(%(max_digits)s, %(decimal_places)s)',
  79. 'DurationField': 'INTERVAL DAY(9) TO SECOND(6)',
  80. 'FileField': 'NVARCHAR2(%(max_length)s)',
  81. 'FilePathField': 'NVARCHAR2(%(max_length)s)',
  82. 'FloatField': 'DOUBLE PRECISION',
  83. 'IntegerField': 'NUMBER(11)',
  84. 'BigIntegerField': 'NUMBER(19)',
  85. 'IPAddressField': 'VARCHAR2(15)',
  86. 'GenericIPAddressField': 'VARCHAR2(39)',
  87. 'NullBooleanField': 'NUMBER(1)',
  88. 'OneToOneField': 'NUMBER(11)',
  89. 'PositiveIntegerField': 'NUMBER(11)',
  90. 'PositiveSmallIntegerField': 'NUMBER(11)',
  91. 'SlugField': 'NVARCHAR2(%(max_length)s)',
  92. 'SmallIntegerField': 'NUMBER(11)',
  93. 'TextField': 'NCLOB',
  94. 'TimeField': 'TIMESTAMP',
  95. 'URLField': 'VARCHAR2(%(max_length)s)',
  96. 'UUIDField': 'VARCHAR2(32)',
  97. }
  98. data_type_check_constraints = {
  99. 'BooleanField': '%(qn_column)s IN (0,1)',
  100. 'NullBooleanField': '%(qn_column)s IN (0,1)',
  101. 'PositiveIntegerField': '%(qn_column)s >= 0',
  102. 'PositiveSmallIntegerField': '%(qn_column)s >= 0',
  103. }
  104. # Oracle doesn't support a database index on these columns.
  105. _limited_data_types = ('clob', 'nclob', 'blob')
  106. operators = _UninitializedOperatorsDescriptor()
  107. _standard_operators = {
  108. 'exact': '= %s',
  109. 'iexact': '= UPPER(%s)',
  110. 'contains': "LIKE TRANSLATE(%s USING NCHAR_CS) ESCAPE TRANSLATE('\\' USING NCHAR_CS)",
  111. 'icontains': "LIKE UPPER(TRANSLATE(%s USING NCHAR_CS)) ESCAPE TRANSLATE('\\' USING NCHAR_CS)",
  112. 'gt': '> %s',
  113. 'gte': '>= %s',
  114. 'lt': '< %s',
  115. 'lte': '<= %s',
  116. 'startswith': "LIKE TRANSLATE(%s USING NCHAR_CS) ESCAPE TRANSLATE('\\' USING NCHAR_CS)",
  117. 'endswith': "LIKE TRANSLATE(%s USING NCHAR_CS) ESCAPE TRANSLATE('\\' USING NCHAR_CS)",
  118. 'istartswith': "LIKE UPPER(TRANSLATE(%s USING NCHAR_CS)) ESCAPE TRANSLATE('\\' USING NCHAR_CS)",
  119. 'iendswith': "LIKE UPPER(TRANSLATE(%s USING NCHAR_CS)) ESCAPE TRANSLATE('\\' USING NCHAR_CS)",
  120. }
  121. _likec_operators = {
  122. **_standard_operators,
  123. 'contains': "LIKEC %s ESCAPE '\\'",
  124. 'icontains': "LIKEC UPPER(%s) ESCAPE '\\'",
  125. 'startswith': "LIKEC %s ESCAPE '\\'",
  126. 'endswith': "LIKEC %s ESCAPE '\\'",
  127. 'istartswith': "LIKEC UPPER(%s) ESCAPE '\\'",
  128. 'iendswith': "LIKEC UPPER(%s) ESCAPE '\\'",
  129. }
  130. # The patterns below are used to generate SQL pattern lookup clauses when
  131. # the right-hand side of the lookup isn't a raw string (it might be an expression
  132. # or the result of a bilateral transformation).
  133. # In those cases, special characters for LIKE operators (e.g. \, *, _) should be
  134. # escaped on database side.
  135. #
  136. # Note: we use str.format() here for readability as '%' is used as a wildcard for
  137. # the LIKE operator.
  138. pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')"
  139. _pattern_ops = {
  140. 'contains': "'%%' || {} || '%%'",
  141. 'icontains': "'%%' || UPPER({}) || '%%'",
  142. 'startswith': "{} || '%%'",
  143. 'istartswith': "UPPER({}) || '%%'",
  144. 'endswith': "'%%' || {}",
  145. 'iendswith': "'%%' || UPPER({})",
  146. }
  147. _standard_pattern_ops = {k: "LIKE TRANSLATE( " + v + " USING NCHAR_CS)"
  148. " ESCAPE TRANSLATE('\\' USING NCHAR_CS)"
  149. for k, v in _pattern_ops.items()}
  150. _likec_pattern_ops = {k: "LIKEC " + v + " ESCAPE '\\'"
  151. for k, v in _pattern_ops.items()}
  152. Database = Database
  153. SchemaEditorClass = DatabaseSchemaEditor
  154. # Classes instantiated in __init__().
  155. client_class = DatabaseClient
  156. creation_class = DatabaseCreation
  157. features_class = DatabaseFeatures
  158. introspection_class = DatabaseIntrospection
  159. ops_class = DatabaseOperations
  160. validation_class = DatabaseValidation
  161. def __init__(self, *args, **kwargs):
  162. super().__init__(*args, **kwargs)
  163. use_returning_into = self.settings_dict["OPTIONS"].get('use_returning_into', True)
  164. self.features.can_return_id_from_insert = use_returning_into
  165. def _dsn(self):
  166. settings_dict = self.settings_dict
  167. if not settings_dict['HOST'].strip():
  168. settings_dict['HOST'] = 'localhost'
  169. if settings_dict['PORT']:
  170. return Database.makedsn(settings_dict['HOST'], int(settings_dict['PORT']), settings_dict['NAME'])
  171. return settings_dict['NAME']
  172. def _connect_string(self):
  173. return '%s/\\"%s\\"@%s' % (self.settings_dict['USER'], self.settings_dict['PASSWORD'], self._dsn())
  174. def get_connection_params(self):
  175. conn_params = self.settings_dict['OPTIONS'].copy()
  176. if 'use_returning_into' in conn_params:
  177. del conn_params['use_returning_into']
  178. return conn_params
  179. def get_new_connection(self, conn_params):
  180. return Database.connect(
  181. user=self.settings_dict['USER'],
  182. password=self.settings_dict['PASSWORD'],
  183. dsn=self._dsn(),
  184. **conn_params,
  185. )
  186. def init_connection_state(self):
  187. cursor = self.create_cursor()
  188. # Set the territory first. The territory overrides NLS_DATE_FORMAT
  189. # and NLS_TIMESTAMP_FORMAT to the territory default. When all of
  190. # these are set in single statement it isn't clear what is supposed
  191. # to happen.
  192. cursor.execute("ALTER SESSION SET NLS_TERRITORY = 'AMERICA'")
  193. # Set Oracle date to ANSI date format. This only needs to execute
  194. # once when we create a new connection. We also set the Territory
  195. # to 'AMERICA' which forces Sunday to evaluate to a '1' in
  196. # TO_CHAR().
  197. cursor.execute(
  198. "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
  199. " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'" +
  200. (" TIME_ZONE = 'UTC'" if settings.USE_TZ else '')
  201. )
  202. cursor.close()
  203. if 'operators' not in self.__dict__:
  204. # Ticket #14149: Check whether our LIKE implementation will
  205. # work for this connection or we need to fall back on LIKEC.
  206. # This check is performed only once per DatabaseWrapper
  207. # instance per thread, since subsequent connections will use
  208. # the same settings.
  209. cursor = self.create_cursor()
  210. try:
  211. cursor.execute("SELECT 1 FROM DUAL WHERE DUMMY %s"
  212. % self._standard_operators['contains'],
  213. ['X'])
  214. except Database.DatabaseError:
  215. self.operators = self._likec_operators
  216. self.pattern_ops = self._likec_pattern_ops
  217. else:
  218. self.operators = self._standard_operators
  219. self.pattern_ops = self._standard_pattern_ops
  220. cursor.close()
  221. self.connection.stmtcachesize = 20
  222. # Ensure all changes are preserved even when AUTOCOMMIT is False.
  223. if not self.get_autocommit():
  224. self.commit()
  225. def create_cursor(self, name=None):
  226. return FormatStylePlaceholderCursor(self.connection)
  227. def _commit(self):
  228. if self.connection is not None:
  229. try:
  230. return self.connection.commit()
  231. except Database.DatabaseError as e:
  232. # cx_Oracle raises a cx_Oracle.DatabaseError exception
  233. # with the following attributes and values:
  234. # code = 2091
  235. # message = 'ORA-02091: transaction rolled back
  236. # 'ORA-02291: integrity constraint (TEST_DJANGOTEST.SYS
  237. # _C00102056) violated - parent key not found'
  238. # We convert that particular case to our IntegrityError exception
  239. x = e.args[0]
  240. if hasattr(x, 'code') and hasattr(x, 'message') \
  241. and x.code == 2091 and 'ORA-02291' in x.message:
  242. raise utils.IntegrityError(*tuple(e.args))
  243. raise
  244. # Oracle doesn't support releasing savepoints. But we fake them when query
  245. # logging is enabled to keep query counts consistent with other backends.
  246. def _savepoint_commit(self, sid):
  247. if self.queries_logged:
  248. self.queries_log.append({
  249. 'sql': '-- RELEASE SAVEPOINT %s (faked)' % self.ops.quote_name(sid),
  250. 'time': '0.000',
  251. })
  252. def _set_autocommit(self, autocommit):
  253. with self.wrap_database_errors:
  254. self.connection.autocommit = autocommit
  255. def check_constraints(self, table_names=None):
  256. """
  257. Check constraints by setting them to immediate. Return them to deferred
  258. afterward.
  259. """
  260. self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
  261. self.cursor().execute('SET CONSTRAINTS ALL DEFERRED')
  262. def is_usable(self):
  263. try:
  264. self.connection.ping()
  265. except Database.Error:
  266. return False
  267. else:
  268. return True
  269. @cached_property
  270. def oracle_version(self):
  271. with self.temporary_connection():
  272. return tuple(int(x) for x in self.connection.version.split('.'))
  273. class OracleParam:
  274. """
  275. Wrapper object for formatting parameters for Oracle. If the string
  276. representation of the value is large enough (greater than 4000 characters)
  277. the input size needs to be set as CLOB. Alternatively, if the parameter
  278. has an `input_size` attribute, then the value of the `input_size` attribute
  279. will be used instead. Otherwise, no input size will be set for the
  280. parameter when executing the query.
  281. """
  282. def __init__(self, param, cursor, strings_only=False):
  283. # With raw SQL queries, datetimes can reach this function
  284. # without being converted by DateTimeField.get_db_prep_value.
  285. if settings.USE_TZ and (isinstance(param, datetime.datetime) and
  286. not isinstance(param, Oracle_datetime)):
  287. param = Oracle_datetime.from_datetime(param)
  288. string_size = 0
  289. # Oracle doesn't recognize True and False correctly.
  290. if param is True:
  291. param = 1
  292. elif param is False:
  293. param = 0
  294. if hasattr(param, 'bind_parameter'):
  295. self.force_bytes = param.bind_parameter(cursor)
  296. elif isinstance(param, (Database.Binary, datetime.timedelta)):
  297. self.force_bytes = param
  298. else:
  299. # To transmit to the database, we need Unicode if supported
  300. # To get size right, we must consider bytes.
  301. self.force_bytes = force_text(param, cursor.charset, strings_only)
  302. if isinstance(self.force_bytes, str):
  303. # We could optimize by only converting up to 4000 bytes here
  304. string_size = len(force_bytes(param, cursor.charset, strings_only))
  305. if hasattr(param, 'input_size'):
  306. # If parameter has `input_size` attribute, use that.
  307. self.input_size = param.input_size
  308. elif string_size > 4000:
  309. # Mark any string param greater than 4000 characters as a CLOB.
  310. self.input_size = Database.CLOB
  311. elif isinstance(param, datetime.datetime):
  312. self.input_size = Database.TIMESTAMP
  313. else:
  314. self.input_size = None
  315. class VariableWrapper:
  316. """
  317. An adapter class for cursor variables that prevents the wrapped object
  318. from being converted into a string when used to instantiate an OracleParam.
  319. This can be used generally for any other object that should be passed into
  320. Cursor.execute as-is.
  321. """
  322. def __init__(self, var):
  323. self.var = var
  324. def bind_parameter(self, cursor):
  325. return self.var
  326. def __getattr__(self, key):
  327. return getattr(self.var, key)
  328. def __setattr__(self, key, value):
  329. if key == 'var':
  330. self.__dict__[key] = value
  331. else:
  332. setattr(self.var, key, value)
  333. class FormatStylePlaceholderCursor:
  334. """
  335. Django uses "format" (e.g. '%s') style placeholders, but Oracle uses ":var"
  336. style. This fixes it -- but note that if you want to use a literal "%s" in
  337. a query, you'll need to use "%%s".
  338. We also do automatic conversion between Unicode on the Python side and
  339. UTF-8 -- for talking to Oracle -- in here.
  340. """
  341. charset = 'utf-8'
  342. def __init__(self, connection):
  343. self.cursor = connection.cursor()
  344. self.cursor.outputtypehandler = self._output_type_handler
  345. # The default for cx_Oracle < 5.3 is 50.
  346. self.cursor.arraysize = 100
  347. @staticmethod
  348. def _output_number_converter(value):
  349. return decimal.Decimal(value) if '.' in value else int(value)
  350. @staticmethod
  351. def _get_decimal_converter(precision, scale):
  352. if scale == 0:
  353. return int
  354. context = decimal.Context(prec=precision)
  355. quantize_value = decimal.Decimal(1).scaleb(-scale)
  356. return lambda v: decimal.Decimal(v).quantize(quantize_value, context=context)
  357. @staticmethod
  358. def _output_type_handler(cursor, name, defaultType, length, precision, scale):
  359. """
  360. Called for each db column fetched from cursors. Return numbers as the
  361. appropriate Python type.
  362. """
  363. if defaultType == Database.NUMBER:
  364. if scale == -127:
  365. if precision == 0:
  366. # NUMBER column: decimal-precision floating point.
  367. # This will normally be an integer from a sequence,
  368. # but it could be a decimal value.
  369. outconverter = FormatStylePlaceholderCursor._output_number_converter
  370. else:
  371. # FLOAT column: binary-precision floating point.
  372. # This comes from FloatField columns.
  373. outconverter = float
  374. elif precision > 0:
  375. # NUMBER(p,s) column: decimal-precision fixed point.
  376. # This comes from IntegerField and DecimalField columns.
  377. outconverter = FormatStylePlaceholderCursor._get_decimal_converter(precision, scale)
  378. else:
  379. # No type information. This normally comes from a
  380. # mathematical expression in the SELECT list. Guess int
  381. # or Decimal based on whether it has a decimal point.
  382. outconverter = FormatStylePlaceholderCursor._output_number_converter
  383. return cursor.var(
  384. Database.STRING,
  385. size=255,
  386. arraysize=cursor.arraysize,
  387. outconverter=outconverter,
  388. )
  389. def _format_params(self, params):
  390. try:
  391. return {k: OracleParam(v, self, True) for k, v in params.items()}
  392. except AttributeError:
  393. return tuple(OracleParam(p, self, True) for p in params)
  394. def _guess_input_sizes(self, params_list):
  395. # Try dict handling; if that fails, treat as sequence
  396. if hasattr(params_list[0], 'keys'):
  397. sizes = {}
  398. for params in params_list:
  399. for k, value in params.items():
  400. if value.input_size:
  401. sizes[k] = value.input_size
  402. self.setinputsizes(**sizes)
  403. else:
  404. # It's not a list of dicts; it's a list of sequences
  405. sizes = [None] * len(params_list[0])
  406. for params in params_list:
  407. for i, value in enumerate(params):
  408. if value.input_size:
  409. sizes[i] = value.input_size
  410. self.setinputsizes(*sizes)
  411. def _param_generator(self, params):
  412. # Try dict handling; if that fails, treat as sequence
  413. if hasattr(params, 'items'):
  414. return {k: v.force_bytes for k, v in params.items()}
  415. else:
  416. return [p.force_bytes for p in params]
  417. def _fix_for_params(self, query, params, unify_by_values=False):
  418. # cx_Oracle wants no trailing ';' for SQL statements. For PL/SQL, it
  419. # it does want a trailing ';' but not a trailing '/'. However, these
  420. # characters must be included in the original query in case the query
  421. # is being passed to SQL*Plus.
  422. if query.endswith(';') or query.endswith('/'):
  423. query = query[:-1]
  424. if params is None:
  425. params = []
  426. elif hasattr(params, 'keys'):
  427. # Handle params as dict
  428. args = {k: ":%s" % k for k in params}
  429. query = query % args
  430. elif unify_by_values and params:
  431. # Handle params as a dict with unified query parameters by their
  432. # values. It can be used only in single query execute() because
  433. # executemany() shares the formatted query with each of the params
  434. # list. e.g. for input params = [0.75, 2, 0.75, 'sth', 0.75]
  435. # params_dict = {0.75: ':arg0', 2: ':arg1', 'sth': ':arg2'}
  436. # args = [':arg0', ':arg1', ':arg0', ':arg2', ':arg0']
  437. # params = {':arg0': 0.75, ':arg1': 2, ':arg2': 'sth'}
  438. params_dict = {param: ':arg%d' % i for i, param in enumerate(set(params))}
  439. args = [params_dict[param] for param in params]
  440. params = {value: key for key, value in params_dict.items()}
  441. query = query % tuple(args)
  442. else:
  443. # Handle params as sequence
  444. args = [(':arg%d' % i) for i in range(len(params))]
  445. query = query % tuple(args)
  446. return query, self._format_params(params)
  447. def execute(self, query, params=None):
  448. query, params = self._fix_for_params(query, params, unify_by_values=True)
  449. self._guess_input_sizes([params])
  450. return self.cursor.execute(query, self._param_generator(params))
  451. def executemany(self, query, params=None):
  452. if not params:
  453. # No params given, nothing to do
  454. return None
  455. # uniform treatment for sequences and iterables
  456. params_iter = iter(params)
  457. query, firstparams = self._fix_for_params(query, next(params_iter))
  458. # we build a list of formatted params; as we're going to traverse it
  459. # more than once, we can't make it lazy by using a generator
  460. formatted = [firstparams] + [self._format_params(p) for p in params_iter]
  461. self._guess_input_sizes(formatted)
  462. return self.cursor.executemany(query, [self._param_generator(p) for p in formatted])
  463. def fetchmany(self, size=None):
  464. if size is None:
  465. size = self.arraysize
  466. return tuple(self.cursor.fetchmany(size))
  467. def fetchall(self):
  468. return tuple(self.cursor.fetchall())
  469. def close(self):
  470. try:
  471. self.cursor.close()
  472. except Database.InterfaceError:
  473. # already closed
  474. pass
  475. def var(self, *args):
  476. return VariableWrapper(self.cursor.var(*args))
  477. def arrayvar(self, *args):
  478. return VariableWrapper(self.cursor.arrayvar(*args))
  479. def __getattr__(self, attr):
  480. return getattr(self.cursor, attr)
  481. def __iter__(self):
  482. return iter(self.cursor)