Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add change_db option to switch between databases, closes #86 #90

Merged
merged 1 commit into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions easyaccess/config_ea.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,17 @@ def get_desconfig(desfile, db, verbose=True, user=None, pw1=None):
if not config.has_option(db, 'server'): configwrite = True;config.set(db, 'server', server_n)
if not config.has_option(db, 'port'): configwrite = True;config.set(db, 'port', port_n)

if not config.has_section(db_alter):
user = config.get(db, 'user')
pw1 = config.get(db, 'passwd')
db = db_alter
config.add_section(db)
if not config.has_option(db, 'user'): configwrite = True;config.set(db, 'user', user)
if not config.has_option(db, 'passwd'): configwrite = True;config.set(db, 'passwd', pw1)
if not config.has_option(db, 'name'): configwrite = True;config.set(db, 'name', db[3:])
if not config.has_option(db, 'server'): configwrite = True;config.set(db, 'server', server_n)
if not config.has_option(db, 'port'): configwrite = True;config.set(db, 'port', port_n)
if db_alter is not None:
if not config.has_section(db_alter):
user = config.get(db, 'user')
pw1 = config.get(db, 'passwd')
db = db_alter
config.add_section(db)
if not config.has_option(db, 'user'): configwrite = True;config.set(db, 'user', user)
if not config.has_option(db, 'passwd'): configwrite = True;config.set(db, 'passwd', pw1)
if not config.has_option(db, 'name'): configwrite = True;config.set(db, 'name', db[3:])
if not config.has_option(db, 'server'): configwrite = True;config.set(db, 'server', server_n)
if not config.has_option(db, 'port'): configwrite = True;config.set(db, 'port', port_n)

check = True
if configwrite == True:
Expand Down
56 changes: 56 additions & 0 deletions easyaccess/easyaccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,62 @@ def do_show_db(self, arg):
""" % (self.user.upper())
self.query_and_print(query, print_time=False, extra=lines, clear=True)

def do_change_db(self, line):
"""
DB: Change to another database, namely dessci, desoper or destest

Usage:
change_db DB # Changes to DB, it does not refresh metadata, e.g.: change_db desoper

"""
if line == '': return self.do_help('change_db')
line = " ".join(line.split())
key_db = line.split()[0]
if key_db in ('dessci', 'desoper', 'destest'):
if key_db == self.dbname:
print(colored("Already connected to : %s" % key_db, "green"))
return
self.dbname = key_db
# connect to db
try:
self.user = self.desconfig.get('db-' + self.dbname, 'user')
self.password = self.desconfig.get('db-' + self.dbname, 'passwd')
except:
print(colored("DB {} does not exist in your desservices file".format(key_db), "red"))
return
kwargs = {'host': self.dbhost, 'port': self.port, 'service_name': self.dbname}
self.dsn = cx_Oracle.makedsn(**kwargs)
if not self.quiet: print('Connecting to DB ** %s ** ...' % self.dbname)
self.con.close()
connected = False
for tries in range(3):
try:
self.con = cx_Oracle.connect(self.user, self.password, dsn=self.dsn)
if self.autocommit: self.con.autocommit = True
connected = True
break
except Exception as e:
lasterr = str(e).strip()
print(colored("Error when trying to connect to database: %s" % lasterr, "red"))
print("\n Retrying...\n")
time.sleep(5)
if not connected:
print('\n ** Could not successfully connect to DB. Try again later. Aborting. ** \n')
os._exit(0)
self.cur = self.con.cursor()
self.cur.arraysize = int(self.prefetch)
return
else:
print(colored("DB {} does not exist or you don't have access to it".format(key_db), "red"))
return

def complete_change_db(self, text, line, start_index, end_index):
options_db = ['desoper','dessci','destest']
if text:
return [option for option in options_db if option.startswith(text.lower())]
else:
return options_db

def do_whoami(self, arg):
"""
DB:Print information about the user's details.
Expand Down