Skip to content

Commit e46ea59

Browse files
committed
Moving setitem to the base class
1 parent 2199bdd commit e46ea59

File tree

3 files changed

+76
-85
lines changed

3 files changed

+76
-85
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444

4545
from qiita_core.exceptions import IncompetentQiitaDeveloperError
4646
from qiita_db.exceptions import (QiitaDBUnknownIDError,
47-
QiitaDBNotImplementedError)
47+
QiitaDBNotImplementedError,
48+
QiitaDBColumnError)
4849
from qiita_db.base import QiitaObject
4950
from qiita_db.sql_connection import SQLConnectionHandler
5051
from qiita_db.util import (exists_table, get_table_cols,
@@ -308,17 +309,79 @@ def handler(x):
308309
" in template %d" %
309310
(key, self._id, self._md_template.id))
310311

311-
def __setitem__(self, key, value):
312-
r"""Sets the metadata value for the category `key`
312+
def __setitem__(self, column, value):
313+
r"""Sets the metadata value for the category `column`
313314
314315
Parameters
315316
----------
316-
key : str
317-
The metadata category
318-
value : obj
319-
The new value for the category
317+
column : str
318+
The column to update
319+
value : str
320+
The value to set. This is expected to be a str on the assumption
321+
that psycopg2 will cast as necessary when updating.
322+
323+
Raises
324+
------
325+
QiitaDBColumnError
326+
If the column does not exist in the table
320327
"""
321-
raise QiitaDBNotImplementedError()
328+
conn_handler = SQLConnectionHandler()
329+
330+
# try dynamic tables
331+
sql = """SELECT EXISTS (
332+
SELECT column_name
333+
FROM information_schema.columns
334+
WHERE table_name=%s
335+
AND table_schema='qiita'
336+
AND column_name=%s)"""
337+
exists_dynamic = conn_handler.execute_fetchone(
338+
sql, (self._dynamic_table, column))[0]
339+
# try required_sample_info
340+
sql = """SELECT EXISTS (
341+
SELECT column_name
342+
FROM information_schema.columns
343+
WHERE table_name=%s
344+
AND table_schema='qiita'
345+
AND column_name=%s)"""
346+
exists_required = conn_handler.execute_fetchone(
347+
sql, (self._table, column))[0]
348+
349+
if exists_dynamic:
350+
# catching error so we can check if the error is due to different
351+
# column type or something else
352+
try:
353+
sql = """UPDATE qiita.{0}
354+
SET {1}=%s
355+
WHERE sample_id=%s""".format(self._dynamic_table,
356+
column)
357+
conn_handler.execute(sql, (value, self._id))
358+
except Exception as e:
359+
column_type = conn_handler.execute_fetchone(
360+
"""SELECT data_type
361+
FROM information_schema.columns
362+
WHERE column_name=%s AND table_schema='qiita'
363+
""", (column,))[0]
364+
value_type = type(value).__name__
365+
366+
if column_type != value_type:
367+
raise ValueError(
368+
'The new value being added to column: "{0}" is "{1}" '
369+
'(type: "{2}"). However, this column in the DB is of '
370+
'type "{3}". Please change the value in your updated '
371+
'template or reprocess your sample template.'.format(
372+
column, value, value_type, column_type))
373+
else:
374+
raise e
375+
elif exists_required:
376+
# here is not required the type check as the required fields have
377+
# an explicit type check
378+
sql = """UPDATE qiita.{0}
379+
SET {1}=%s
380+
WHERE sample_id=%s""".format(self._table, column)
381+
conn_handler.execute(sql, (value, self._id))
382+
else:
383+
raise QiitaDBColumnError("Column %s does not exist in %s" %
384+
(column, self._dynamic_table))
322385

323386
def __delitem__(self, key):
324387
r"""Removes the sample with sample id `key` from the database

qiita_db/metadata_template/sample_template.py

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -63,80 +63,6 @@ def _check_template_class(self, md_template):
6363
if not isinstance(md_template, SampleTemplate):
6464
raise IncompetentQiitaDeveloperError()
6565

66-
def __setitem__(self, column, value):
67-
r"""Sets the metadata value for the category `column`
68-
69-
Parameters
70-
----------
71-
column : str
72-
The column to update
73-
value : str
74-
The value to set. This is expected to be a str on the assumption
75-
that psycopg2 will cast as necessary when updating.
76-
77-
Raises
78-
------
79-
QiitaDBColumnError
80-
If the column does not exist in the table
81-
"""
82-
conn_handler = SQLConnectionHandler()
83-
84-
# try dynamic tables
85-
exists_dynamic = conn_handler.execute_fetchone("""
86-
SELECT EXISTS (
87-
SELECT column_name
88-
FROM information_schema.columns
89-
WHERE table_name='{0}'
90-
AND table_schema='qiita'
91-
AND column_name='{1}')""".format(self._dynamic_table,
92-
column))[0]
93-
# try required_sample_info
94-
exists_required = conn_handler.execute_fetchone("""
95-
SELECT EXISTS (
96-
SELECT column_name
97-
FROM information_schema.columns
98-
WHERE table_name='required_sample_info'
99-
AND table_schema='qiita'
100-
AND column_name='{0}')""".format(column))[0]
101-
102-
if exists_dynamic:
103-
# catching error so we can check if the error is due to different
104-
# column type or something else
105-
try:
106-
conn_handler.execute("""
107-
UPDATE qiita.{0}
108-
SET {1}=%s
109-
WHERE sample_id=%s""".format(self._dynamic_table,
110-
column), (value, self._id))
111-
except Exception as e:
112-
column_type = conn_handler.execute_fetchone("""
113-
SELECT data_type
114-
FROM information_schema.columns
115-
WHERE column_name=%s AND table_schema='qiita'
116-
""", (column,))[0]
117-
value_type = type(value).__name__
118-
119-
if column_type != value_type:
120-
raise ValueError(
121-
'The new value being added to column: "{0}" is "{1}" '
122-
'(type: "{2}"). However, this column in the DB is of '
123-
'type "{3}". Please change the value in your updated '
124-
'template or reprocess your sample template.'.format(
125-
column, value, value_type, column_type))
126-
else:
127-
raise e
128-
elif exists_required:
129-
# here is not required the type check as the required fields have
130-
# an explicit type check
131-
conn_handler.execute("""
132-
UPDATE qiita.required_sample_info
133-
SET {0}=%s
134-
WHERE sample_id=%s
135-
""".format(column), (value, self._id))
136-
else:
137-
raise QiitaDBColumnError("Column %s does not exist in %s" %
138-
(column, self._dynamic_table))
139-
14066

14167
class SampleTemplate(MetadataTemplate):
14268
r"""Represent the SampleTemplate of a study. Provides access to the

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ def test_get_none(self):
220220
class TestPrepSampleReadWrite(BaseTestPrepSample):
221221
"""Tests the PrepSample class"""
222222
def test_setitem(self):
223-
"""setitem raises an error (currently not allowed)"""
224-
with self.assertRaises(QiitaDBNotImplementedError):
225-
self.tester['barcodesequence'] = 'GTCCGCAAGTTA'
223+
with self.assertRaises(QiitaDBColumnError):
224+
self.tester['column that does not exist'] = 0.3
225+
self.assertEqual(self.tester['center_name'], 'ANL')
226+
self.tester['center_name'] = "FOO"
227+
self.assertEqual(self.tester['center_name'], "FOO")
226228

227229
def test_delitem(self):
228230
"""delitem raises an error (currently not allowed)"""

0 commit comments

Comments
 (0)