Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fixes https://code.djangoproject.com/ticket/18465 #149

Closed
wants to merge 2 commits into from

2 participants

@jarshwah
Collaborator

Correctly configure NLS_SESSION_PARAMETERS to format Date and DateTime with
respect to the Oracle backend.

jarshwah added some commits
@jarshwah jarshwah fixes https://code.djangoproject.com/ticket/18465
Correctly configure NLS_SESSION_PARAMETERS to format Date and DateTime with
respect to the Oracle backend.
e729c57
@jarshwah jarshwah regression test added to Oracle Backend for issue #18465 ae73f89
@akaariai
Collaborator

I have forgotten to close this pull requests. Merged in manually in fa182e8

@akaariai akaariai closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 12, 2012
  1. @jarshwah

    fixes https://code.djangoproject.com/ticket/18465

    jarshwah authored
    Correctly configure NLS_SESSION_PARAMETERS to format Date and DateTime with
    respect to the Oracle backend.
  2. @jarshwah
This page is out of date. Refresh to see the latest.
View
6 django/db/backends/oracle/base.py
@@ -482,9 +482,13 @@ def _cursor(self):
# Set oracle date to ansi date format. This only needs to execute
# once when we create a new connection. We also set the Territory
# to 'AMERICA' which forces Sunday to evaluate to a '1' in TO_CHAR().
+ cursor.execute("ALTER SESSION SET NLS_TERRITORY = 'AMERICA'")
+ # We set the territory first, which overrides NLS_DATE_FORMAT and
+ # NLS_TIMESTAMP_FORMAT to the territory default, and then set
+ # the formats for predictable behaviour.
+ # see http://docs.oracle.com/cd/E11882_01/server.112/e10729/ch3globenv.htm#autoId14
cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
" NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
- " NLS_TERRITORY = 'AMERICA'"
+ (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
if 'operators' not in self.__dict__:
View
21 tests/regressiontests/backends/tests.py
@@ -65,6 +65,27 @@ def test_client_encoding(self):
connection.cursor() # Ensure the connection is initialized.
self.assertEqual(connection.connection.encoding, "UTF-8")
self.assertEqual(connection.connection.nencoding, "UTF-8")
+
+ @unittest.skipUnless(connection.vendor == 'oracle',
+ "No need to check Oracle connection semantics")
+ def test_order_of_nls_parameters(self):
+ # an 'almost right' datetime should work with configured
+ # NLS parameters as per #18465.
+ c = connection.cursor()
+ query = "select 1 from dual where '1936-12-29 00:00' < sysdate"
+ c.execute(query)
+ try:
+ # should change the NLS_DATE_FORMAT and NLS_TIMESTAMP_FORMAT
+ c.execute("ALTER SESSION SET NLS_TERRITORY = 'AMERICA'")
+ with self.assertRaises(DatabaseError):
+ c.execute(query)
+ finally:
+ try:
+ c.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
+ " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
+ + (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
+ except:
+ pass
class MySQLTests(TestCase):
@unittest.skipUnless(connection.vendor == 'mysql',
Something went wrong with that request. Please try again.