22
22
QiitaDBDuplicateHeaderError )
23
23
from qiita_db .base import QiitaObject
24
24
from qiita_db .sql_connection import SQLConnectionHandler
25
- from qiita_db .util import (exists_table , get_table_cols , convert_to_id ,
26
- get_mountpoint , insert_filepaths )
25
+ from qiita_db .util import (exists_table , get_table_cols , get_mountpoint ,
26
+ insert_filepaths )
27
27
from qiita_db .logger import LogEntry
28
28
from .util import get_invalid_sample_names , prefix_sample_names_with_id
29
29
@@ -425,6 +425,7 @@ class MetadataTemplate(QiitaObject):
425
425
_id_column = None
426
426
_sample_cls = None
427
427
_filepath_table = None
428
+ _filepath_type = None
428
429
429
430
def _check_id (self , id_ , conn_handler = None ):
430
431
r"""Checks that the MetadataTemplate id_ exists on the database"""
@@ -839,28 +840,19 @@ def add_filepath(self, filepath, conn_handler=None):
839
840
# one if not.
840
841
conn_handler = conn_handler if conn_handler else SQLConnectionHandler ()
841
842
842
- if self ._table == 'required_sample_info' :
843
- fp_id = convert_to_id ("sample_template" , "filepath_type" ,
844
- conn_handler )
845
- table = 'sample_template_filepath'
846
- column = 'study_id'
847
- elif self ._table == 'common_prep_info' :
848
- fp_id = convert_to_id ("prep_template" , "filepath_type" ,
849
- conn_handler )
850
- table = 'prep_template_filepath'
851
- column = 'prep_template_id'
852
- else :
853
- raise QiitaDBNotImplementedError (
854
- 'add_filepath for %s' % self ._table )
855
-
856
843
try :
857
- fpp_id = insert_filepaths ([(filepath , fp_id )], None , "templates" ,
858
- "filepath" , conn_handler ,
844
+ fpp_id = insert_filepaths ([(filepath , self . _filepath_type )], None ,
845
+ "templates" , " filepath" , conn_handler ,
859
846
move_files = False )[0 ]
860
847
values = (self ._id , fpp_id )
861
- conn_handler .execute (
862
- "INSERT INTO qiita.{0} ({1}, filepath_id) "
863
- "VALUES (%s, %s)" .format (table , column ), values )
848
+ sql = """INSERT INTO qiita.{0} ({1}, filepath_id)
849
+ VALUES (%s, %s)""" .format (self ._filepath_table ,
850
+ self ._id_column )
851
+ # If this call fails, filepaths will have been added to the DB,
852
+ # but they're not linked to anything. However, this is not a
853
+ # problem, as any subsequent call to purge_filepaths will remove
854
+ # those filepaths
855
+ conn_handler .execute (sql , values )
864
856
except Exception as e :
865
857
LogEntry .create ('Runtime' , str (e ),
866
858
info = {self .__class__ .__name__ : self .id })
@@ -875,22 +867,16 @@ def get_filepaths(self, conn_handler=None):
875
867
# one if not.
876
868
conn_handler = conn_handler if conn_handler else SQLConnectionHandler ()
877
869
878
- if self ._table == 'required_sample_info' :
879
- table = 'sample_template_filepath'
880
- column = 'study_id'
881
- elif self ._table == 'common_prep_info' :
882
- table = 'prep_template_filepath'
883
- column = 'prep_template_id'
884
- else :
885
- raise QiitaDBNotImplementedError (
886
- 'get_filepath for %s' % self ._table )
887
-
870
+ sql = """SELECT filepath_id, filepath
871
+ FROM qiita.filepath
872
+ WHERE filepath_id IN (
873
+ SELECT filepath_id
874
+ FROM qiita.{0}
875
+ WHERE {1}=%s)
876
+ ORDER BY filepath_id DESC""" .format (self ._filepath_table ,
877
+ self ._id_column )
888
878
try :
889
- filepath_ids = conn_handler .execute_fetchall (
890
- "SELECT filepath_id, filepath FROM qiita.filepath WHERE "
891
- "filepath_id IN (SELECT filepath_id FROM qiita.{0} WHERE "
892
- "{1}=%s) ORDER BY filepath_id DESC" .format (table , column ),
893
- (self .id , ))
879
+ filepath_ids = conn_handler .execute_fetchall (sql , (self .id , ))
894
880
except Exception as e :
895
881
LogEntry .create ('Runtime' , str (e ),
896
882
info = {self .__class__ .__name__ : self .id })
0 commit comments