forked from ibmdb/python-ibmdb-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
executable file
·77 lines (66 loc) · 3.38 KB
/
client.py
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
# +--------------------------------------------------------------------------+
# | Licensed Materials - Property of IBM |
# | |
# | (C) Copyright IBM Corporation 2009-2021. |
# +--------------------------------------------------------------------------+
# | This module complies with Django 1.0 and is |
# | Licensed under the Apache License, Version 2.0 (the "License"); |
# | you may not use this file except in compliance with the License. |
# | You may obtain a copy of the License at |
# | http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable |
# | law or agreed to in writing, software distributed under the License is |
# | distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
# | KIND, either express or implied. See the License for the specific |
# | language governing permissions and limitations under the License. |
# +--------------------------------------------------------------------------+
# | Authors: Ambrish Bhargava, Tarun Pasrija, Rahul Priyadarshi |
# +--------------------------------------------------------------------------+
"""
This module implements command line interface for DB2 through Django.
"""
try:
from django.db.backends import BaseDatabaseClient
except ImportError:
from django.db.backends.base.client import BaseDatabaseClient
from django import VERSION as djangoVersion
import types
import os
import sys
class DatabaseClient( BaseDatabaseClient ):
#Over-riding base method to provide shell support for DB2 through Django.
def runshell( self, parameters ):
if ( djangoVersion[0:2] <= ( 1, 0 ) ):
from django.conf import settings
database_name = settings.DATABASE_NAME
database_user = settings.DATABASE_USER
database_password = settings.DATABASE_PASSWORD
elif ( djangoVersion[0:2] <= ( 1, 1 ) ):
settings_dict = self.connection.settings_dict
database_name = settings_dict['DATABASE_NAME']
database_user = settings_dict['DATABASE_USER']
database_password = settings_dict['DATABASE_PASSWORD']
else:
settings_dict = self.connection.settings_dict
database_name = settings_dict['NAME']
database_user = settings_dict['USER']
database_password = settings_dict['PASSWORD']
cmdArgs = ["db2"]
if ( os.name == 'nt' ):
cmdArgs += ["db2 connect to %s" % database_name]
else:
cmdArgs += ["connect to %s" % database_name]
if sys.version_info.major >= 3:
strvar = str
else:
strvar = str
if ( isinstance( database_user, strvar ) and
( database_user != '' ) ):
cmdArgs += ["user %s" % database_user]
if ( isinstance( database_password, strvar ) and
( database_password != '' ) ):
cmdArgs += ["using %s" % database_password]
# db2cmd is the shell which is required to run db2 commands on windows.
if ( os.name == 'nt' ):
os.execvp( 'db2cmd', cmdArgs )
else:
os.execvp( 'db2', cmdArgs )