Skip to content

Commit e311fb7

Browse files
committed
removing conditional db connections
1 parent ef7955c commit e311fb7

File tree

7 files changed

+38
-41
lines changed

7 files changed

+38
-41
lines changed

qiita_db/analysis.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ def build_files(self, rarefaction_depth=None):
663663

664664
def _get_samples(self, conn_handler=None):
665665
"""Retrieves dict of samples to proc_data_id for the analysis"""
666-
conn_handler = conn_handler if conn_handler is not None \
667-
else SQLConnectionHandler()
666+
conn_handler = SQLConnectionHandler()
668667
sql = ("SELECT processed_data_id, array_agg(sample_id ORDER BY "
669668
"sample_id) FROM qiita.analysis_sample WHERE analysis_id = %s "
670669
"GROUP BY processed_data_id")
@@ -701,8 +700,7 @@ def _build_biom_tables(self, samples, rarefaction_depth,
701700
new_tables[data_type] = new_tables[data_type].merge(table)
702701

703702
# add the new tables to the analysis
704-
conn_handler = conn_handler if conn_handler is not None \
705-
else SQLConnectionHandler()
703+
conn_handler = SQLConnectionHandler()
706704
_, base_fp = get_mountpoint(self._table)[0]
707705
for dt, biom_table in viewitems(new_tables):
708706
# rarefy, if specified
@@ -719,9 +717,7 @@ def _build_biom_tables(self, samples, rarefaction_depth,
719717
def _build_mapping_file(self, samples, conn_handler=None):
720718
"""Builds the combined mapping file for all samples
721719
Code modified slightly from qiime.util.MetadataMap.__add__"""
722-
conn_handler = conn_handler if conn_handler is not None \
723-
else SQLConnectionHandler()
724-
720+
conn_handler = SQLConnectionHandler()
725721
all_sample_ids = set()
726722
sql = """SELECT filepath_id, filepath
727723
FROM qiita.filepath
@@ -791,8 +787,8 @@ def _add_file(self, filename, filetype, data_type=None, conn_handler=None):
791787
data_type : str, optional
792788
conn_handler : SQLConnectionHandler object, optional
793789
"""
794-
conn_handler = conn_handler if conn_handler is not None \
795-
else SQLConnectionHandler()
790+
conn_handler = SQLConnectionHandler() \
791+
796792

797793
filetype_id = convert_to_id(filetype, 'filepath_type', conn_handler)
798794
_, mp = get_mountpoint('analysis', conn_handler)[0]

qiita_db/base.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def _check_id(self, id_, conn_handler=None):
132132
"""
133133
self._check_subclass()
134134

135-
conn_handler = (conn_handler if conn_handler is not None
136-
else SQLConnectionHandler())
135+
conn_handler = SQLConnectionHandler()
137136

138137
return conn_handler.execute_fetchone(
139138
"SELECT EXISTS(SELECT * FROM qiita.{0} WHERE "
@@ -265,8 +264,8 @@ def check_status(self, status, exclude=False, conn_handler=None):
265264
self._check_subclass()
266265

267266
# Get all available statuses
268-
conn_handler = (conn_handler if conn_handler is not None
269-
else SQLConnectionHandler())
267+
conn_handler = SQLConnectionHandler()
268+
270269
statuses = [x[0] for x in conn_handler.execute_fetchall(
271270
"SELECT DISTINCT status FROM qiita.{0}_status".format(self._table),
272271
(self._id, ))]

qiita_db/data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def add_filepaths(self, filepaths, conn_handler=None):
147147

148148
# Check if the connection handler has been provided. Create a new
149149
# one if not.
150-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
150+
conn_handler = SQLConnectionHandler()
151151

152152
# Update the status of the current object
153153
self._set_link_filepaths_status("linking")
@@ -478,7 +478,7 @@ def _is_preprocessed(self, conn_handler=None):
478478
bool
479479
whether the RawData has been preprocessed or not
480480
"""
481-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
481+
conn_handler = SQLConnectionHandler()
482482
return conn_handler.execute_fetchone(
483483
"SELECT EXISTS(SELECT * FROM qiita.prep_template_preprocessed_data"
484484
" PTPD JOIN qiita.prep_template PT ON PT.prep_template_id = "

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,9 @@ class MetadataTemplate(QiitaObject):
492492
def _check_id(self, id_, conn_handler=None):
493493
r"""Checks that the MetadataTemplate id_ exists on the database"""
494494
self._check_subclass()
495-
conn_handler = (conn_handler if conn_handler is not None
496-
else SQLConnectionHandler())
495+
496+
conn_handler = SQLConnectionHandler()
497+
497498
return conn_handler.execute_fetchone(
498499
"SELECT EXISTS(SELECT * FROM qiita.{0} WHERE "
499500
"{1}=%s)".format(self._table, self._id_column),
@@ -1032,7 +1033,7 @@ def add_filepath(self, filepath, conn_handler=None, fp_id=None):
10321033

10331034
# Check if the connection handler has been provided. Create a new
10341035
# one if not.
1035-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
1036+
conn_handler = SQLConnectionHandler()
10361037
fp_id = self._fp_id if fp_id is None else fp_id
10371038

10381039
try:
@@ -1056,7 +1057,7 @@ def get_filepaths(self, conn_handler=None):
10561057

10571058
# Check if the connection handler has been provided. Create a new
10581059
# one if not.
1059-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
1060+
conn_handler = SQLConnectionHandler()
10601061

10611062
try:
10621063
filepath_ids = conn_handler.execute_fetchall(

qiita_db/parameters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def _check_id(self, id_, conn_handler=None):
133133
"""
134134
self._check_subclass()
135135

136-
conn_handler = (conn_handler if conn_handler is not None
137-
else SQLConnectionHandler())
136+
conn_handler = SQLConnectionHandler()
137+
138138
return conn_handler.execute_fetchone(
139139
"SELECT EXISTS(SELECT * FROM qiita.{0} WHERE {1} = %s)".format(
140140
self._table, self._column_id),

qiita_db/user.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def _check_id(self, id_, conn_handler=None):
8282
"""
8383
self._check_subclass()
8484

85-
conn_handler = (conn_handler if conn_handler is not None
86-
else SQLConnectionHandler())
85+
conn_handler = SQLConnectionHandler()
86+
8787
return conn_handler.execute_fetchone(
8888
"SELECT EXISTS(SELECT * FROM qiita.qiita_user WHERE "
8989
"email = %s)", (id_, ))[0]
@@ -454,7 +454,7 @@ def _change_pass(self, newpass, conn_handler=None):
454454

455455
sql = ("UPDATE qiita.{0} SET password=%s, pass_reset_code=NULL WHERE "
456456
"email = %s".format(self._table))
457-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
457+
conn_handler = SQLConnectionHandler()
458458
conn_handler.execute(sql, (hash_password(newpass), self._id))
459459

460460

qiita_db/util.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def get_table_cols(table, conn_handler=None):
341341
list of str
342342
The column headers of `table`
343343
"""
344-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
344+
conn_handler = SQLConnectionHandler()
345345
headers = conn_handler.execute_fetchall(
346346
"SELECT column_name FROM information_schema.columns WHERE "
347347
"table_name=%s AND table_schema='qiita'", (table, ))
@@ -363,7 +363,7 @@ def get_table_cols_w_type(table, conn_handler=None):
363363
list of tuples of (str, str)
364364
The column headers and data type of `table`
365365
"""
366-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
366+
conn_handler = SQLConnectionHandler()
367367
return conn_handler.execute_fetchall(
368368
"SELECT column_name, data_type FROM information_schema.columns WHERE "
369369
"table_name=%s", (table,))
@@ -412,8 +412,8 @@ def get_db_files_base_dir(conn_handler=None):
412412
str
413413
The path to the base directory of all db files
414414
"""
415-
conn_handler = (conn_handler if conn_handler is not None
416-
else SQLConnectionHandler())
415+
conn_handler = SQLConnectionHandler()
416+
417417
return conn_handler.execute_fetchone(
418418
"SELECT base_data_dir FROM settings")[0]
419419

@@ -426,8 +426,8 @@ def get_work_base_dir(conn_handler=None):
426426
str
427427
The path to the base directory of all db files
428428
"""
429-
conn_handler = (conn_handler if conn_handler is not None
430-
else SQLConnectionHandler())
429+
conn_handler = SQLConnectionHandler()
430+
431431
return conn_handler.execute_fetchone(
432432
"SELECT base_work_dir FROM settings")[0]
433433

@@ -555,8 +555,8 @@ def get_mountpoint(mount_type, conn_handler=None, retrieve_all=False):
555555
list
556556
List of tuple, where: [(id_mountpoint, filepath_of_mountpoint)]
557557
"""
558-
conn_handler = (conn_handler if conn_handler is not None
559-
else SQLConnectionHandler())
558+
conn_handler = SQLConnectionHandler()
559+
560560
if retrieve_all:
561561
result = conn_handler.execute_fetchall(
562562
"SELECT data_directory_id, mountpoint, subdirectory FROM "
@@ -586,7 +586,7 @@ def get_mountpoint_path_by_id(mount_id, conn_handler=None):
586586
str
587587
The mountpoint path
588588
"""
589-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
589+
conn_handler = SQLConnectionHandler()
590590
mountpoint, subdirectory = conn_handler.execute_fetchone(
591591
"""SELECT mountpoint, subdirectory FROM qiita.data_directory
592592
WHERE data_directory_id=%s""", (mount_id,))
@@ -676,7 +676,7 @@ def purge_filepaths(conn_handler=None):
676676
conn_handler : SQLConnectionHandler, optional
677677
The connection handler object connected to the DB
678678
"""
679-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
679+
conn_handler = SQLConnectionHandler()
680680

681681
# Get all the (table, column) pairs that reference to the filepath table
682682
# Code adapted from http://stackoverflow.com/q/5347050/3746629
@@ -733,7 +733,7 @@ def move_filepaths_to_upload_folder(study_id, filepaths, conn_handler=None):
733733
conn_handler : SQLConnectionHandler, optional
734734
The connection handler object connected to the DB
735735
"""
736-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
736+
conn_handler = SQLConnectionHandler()
737737
uploads_fp = join(get_mountpoint("uploads")[0][1], str(study_id))
738738

739739
# We can now go over and remove all the filepaths
@@ -848,7 +848,7 @@ def convert_to_id(value, table, conn_handler=None):
848848
IncompetentQiitaDeveloperError
849849
The passed string has no associated id
850850
"""
851-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
851+
conn_handler = SQLConnectionHandler()
852852
sql = "SELECT {0}_id FROM qiita.{0} WHERE {0} = %s".format(table)
853853
_id = conn_handler.execute_fetchone(sql, (value, ))
854854
if _id is None:
@@ -879,7 +879,7 @@ def convert_from_id(value, table, conn_handler=None):
879879
ValueError
880880
The passed id has no associated string
881881
"""
882-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
882+
conn_handler = SQLConnectionHandler()
883883
string = conn_handler.execute_fetchone(
884884
"SELECT {0} FROM qiita.{0} WHERE {0}_id = %s".format(table),
885885
(value, ))
@@ -989,8 +989,9 @@ def get_environmental_packages(conn_handler=None):
989989
environmental package name and the second string is the table where
990990
the metadata for the environmental package is stored
991991
"""
992-
conn = conn_handler if conn_handler else SQLConnectionHandler()
993-
return conn.execute_fetchall("SELECT * FROM qiita.environmental_package")
992+
conn_handler = SQLConnectionHandler()
993+
return conn_handler.execute_fetchall(
994+
"SELECT * FROM qiita.environmental_package")
994995

995996

996997
def get_timeseries_types(conn_handler=None):
@@ -1007,8 +1008,8 @@ def get_timeseries_types(conn_handler=None):
10071008
The available timeseries types. Each timeseries type is defined by the
10081009
tuple (timeseries_id, timeseries_type, intervention_type)
10091010
"""
1010-
conn = conn_handler if conn_handler else SQLConnectionHandler()
1011-
return conn.execute_fetchall(
1011+
conn_handler = SQLConnectionHandler()
1012+
return conn_handler.execute_fetchall(
10121013
"SELECT * FROM qiita.timeseries_type ORDER BY timeseries_type_id")
10131014

10141015

0 commit comments

Comments
 (0)