|
107 | 107 | from .util import (check_required_columns, check_table_cols, convert_to_id, |
108 | 108 | get_environmental_packages, get_table_cols, infer_status) |
109 | 109 | from .sql_connection import SQLConnectionHandler |
| 110 | +from .util import exists_table |
110 | 111 |
|
111 | 112 |
|
112 | 113 | class Study(QiitaObject): |
@@ -347,6 +348,58 @@ def create(cls, owner, title, efo, info, investigation=None): |
347 | 348 |
|
348 | 349 | return cls(study_id) |
349 | 350 |
|
| 351 | + @classmethod |
| 352 | + def delete(cls, id_): |
| 353 | + r"""Deletes the study from the database |
| 354 | +
|
| 355 | + Parameters |
| 356 | + ---------- |
| 357 | + id_ : integer |
| 358 | + The object identifier |
| 359 | +
|
| 360 | + Raises |
| 361 | + ------ |
| 362 | + QiitaDBError |
| 363 | + If the sample_(_id) table exists == a sample template exists |
| 364 | + """ |
| 365 | + cls._check_subclass() |
| 366 | + |
| 367 | + study = cls(id_) |
| 368 | + |
| 369 | + conn_handler = SQLConnectionHandler() |
| 370 | + if exists_table('sample_%d' % id_, conn_handler): |
| 371 | + raise QiitaDBError('Study "%s" can not be erased because it has a ' |
| 372 | + 'sample template' % cls(id_).title) |
| 373 | + |
| 374 | + queue = "delete_study_%d" % id_ |
| 375 | + conn_handler.create_queue(queue) |
| 376 | + |
| 377 | + conn_handler.add_to_queue( |
| 378 | + queue, |
| 379 | + "DELETE FROM qiita.study_sample_columns WHERE study_id = %s", |
| 380 | + (id_, )) |
| 381 | + |
| 382 | + conn_handler.add_to_queue( |
| 383 | + queue, |
| 384 | + "DELETE FROM qiita.study_experimental_factor WHERE study_id = %s", |
| 385 | + (id_, )) |
| 386 | + |
| 387 | + conn_handler.add_to_queue( |
| 388 | + queue, |
| 389 | + "DELETE FROM qiita.study_pmid WHERE study_id = %s", (id_, )) |
| 390 | + |
| 391 | + conn_handler.add_to_queue( |
| 392 | + queue, |
| 393 | + "DELETE FROM qiita.study_environmental_package WHERE study_id = " |
| 394 | + "%s", (id_, )) |
| 395 | + |
| 396 | + conn_handler.add_to_queue( |
| 397 | + queue, |
| 398 | + "DELETE FROM qiita.study WHERE study_id = %s", (id_, )) |
| 399 | + |
| 400 | + conn_handler.execute_queue(queue) |
| 401 | + |
| 402 | + |
350 | 403 | # --- Attributes --- |
351 | 404 | @property |
352 | 405 | def title(self): |
|
0 commit comments