mirror of
https://github.com/django/django.git
synced 2025-04-25 09:44:36 +00:00
Fixed #18465 -- Set date formats correctly on Oracle
Correctly configure NLS_SESSION_PARAMETERS to format Date and DateTime on Oracle backend. Thanks to Josh Smeaton for report & patch.
This commit is contained in:
parent
05d333ba3b
commit
fa182e8ae8
@ -479,12 +479,18 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
|||||||
del conn_params['use_returning_into']
|
del conn_params['use_returning_into']
|
||||||
self.connection = Database.connect(conn_string, **conn_params)
|
self.connection = Database.connect(conn_string, **conn_params)
|
||||||
cursor = FormatStylePlaceholderCursor(self.connection)
|
cursor = FormatStylePlaceholderCursor(self.connection)
|
||||||
|
# Set the territory first. The territory overrides NLS_DATE_FORMAT
|
||||||
|
# and NLS_TIMESTAMP_FORMAT to the territory default. When all of
|
||||||
|
# these are set in single statement it isn't clear what is supposed
|
||||||
|
# to happen.
|
||||||
|
cursor.execute("ALTER SESSION SET NLS_TERRITORY = 'AMERICA'")
|
||||||
# Set oracle date to ansi date format. This only needs to execute
|
# Set oracle date to ansi date format. This only needs to execute
|
||||||
# once when we create a new connection. We also set the Territory
|
# 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().
|
# to 'AMERICA' which forces Sunday to evaluate to a '1' in
|
||||||
cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
|
# TO_CHAR().
|
||||||
|
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_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
|
||||||
" NLS_TERRITORY = 'AMERICA'"
|
|
||||||
+ (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
|
+ (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
|
||||||
|
|
||||||
if 'operators' not in self.__dict__:
|
if 'operators' not in self.__dict__:
|
||||||
|
@ -66,6 +66,18 @@ class OracleChecks(unittest.TestCase):
|
|||||||
self.assertEqual(connection.connection.encoding, "UTF-8")
|
self.assertEqual(connection.connection.encoding, "UTF-8")
|
||||||
self.assertEqual(connection.connection.nencoding, "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"
|
||||||
|
# Test that the query succeeds without errors - pre #18465 this
|
||||||
|
# wasn't the case.
|
||||||
|
c.execute(query)
|
||||||
|
self.assertEqual(c.fetchone()[0], 1)
|
||||||
|
|
||||||
class MySQLTests(TestCase):
|
class MySQLTests(TestCase):
|
||||||
@unittest.skipUnless(connection.vendor == 'mysql',
|
@unittest.skipUnless(connection.vendor == 'mysql',
|
||||||
"Test valid only for MySQL")
|
"Test valid only for MySQL")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user