14.2.37. camcops_server.cc_modules.cc_db

camcops_server/cc_modules/cc_db.py


Copyright (C) 2012-2019 Rudolf Cardinal (rudolf@pobox.com).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <http://www.gnu.org/licenses/>.


Common database code, e.g. mixins for tables that are uploaded from the client.

class camcops_server.cc_modules.cc_db.GenericTabletRecordMixin[source]

Mixin for all tables that are uploaded from the client, representing the fields that the server adds at the point of upload.

From the server’s perspective, _pk is the unique primary key.

However, records are defined also in their tablet context, for which an individual tablet (defined by the combination of _device_id and _era) sees its own PK, id.

create_fresh(req: CamcopsRequest, device_id: int, era: str, group_id: int) → None[source]

Used to create a record from scratch.

delete_with_dependants(req: CamcopsRequest) → None[source]

Deletes (completely from the database) this record and any dependant records.

gen_ancillary_instances() → Generator[[_ForwardRef('GenericTabletRecordMixin'), NoneType], NoneType][source]

Generates all _current ancillary objects of this object.

gen_ancillary_instances_even_noncurrent() → Generator[[_ForwardRef('GenericTabletRecordMixin'), NoneType], NoneType][source]

Generates all ancillary objects of this object, even non-current ones.

gen_attrname_ancillary_pairs() → Generator[[Tuple[str, _ForwardRef('GenericTabletRecordMixin')], NoneType], NoneType][source]

Iterates through and yields all _current “ancillary” objects (typically: records of subtables).

Yields tuples of (attrname, related_record).

gen_blobs() → Generator[[_ForwardRef('Blob'), NoneType], NoneType][source]

Generate all _current BLOBs owned by this object.

gen_blobs_even_noncurrent() → Generator[[_ForwardRef('Blob'), NoneType], NoneType][source]

Generates all BLOBs owned by this object, even non-current ones.

get_device_id() → Union[int, NoneType][source]

Returns the client device ID of this record.

get_era() → Union[str, NoneType][source]

Returns the era of this record (a text representation of the date/time of the point of record finalization, or NOW if the record is still present on the client device).

get_group_id() → Union[int, NoneType][source]

Returns the group ID of this record.

get_lineage() → List[_ForwardRef('GenericTabletRecordMixin')][source]

Returns all records that are part of the same “lineage”, that is:

  • of the same class;
  • matching on id/device_id/era;
  • including both current and any historical non-current versions.
get_pk() → Union[int, NoneType][source]

Returns the (server) primary key of this record.

get_summaries(req: CamcopsRequest) → List[_ForwardRef('SummaryElement')][source]

Return a list of SummaryElement objects, for this database object (not any dependent classes/tables).

Note that this is implemented on GenericTabletRecordMixin, not camcops_server.cc_modules.cc_task.Task, so that ancillary objects can also provide summaries.

get_summary_names(req: CamcopsRequest) → List[str][source]

Returns a list of summary field names.

manually_erase_with_dependants(req: CamcopsRequest) → None[source]

Manually erases a standard record and marks it so erased. Iterates through any dependants and does likewise to them.

The object remains _current (if it was), as a placeholder, but its contents are wiped.

WRITES TO THE DATABASE.

mark_as_deleted(req: CamcopsRequest) → None[source]

Ends the history chain and marks this record as non-current.

set_predecessor(req: CamcopsRequest, predecessor: GenericTabletRecordMixin) → None[source]

Used for some unusual server-side manipulations (e.g. editing patient details).

Amends this object so the “self” object replaces the predecessor, so:

  • “self” becomes current and refers back to “predecessor”;
  • “predecessor” becomes non-current and refers forward to “self”.
camcops_server.cc_modules.cc_db.add_multiple_columns(cls: Type, prefix: str, start: int, end: int, coltype=<class 'sqlalchemy.sql.sqltypes.Integer'>, colkwargs: Dict[str, Any] = None, comment_fmt: str = None, comment_strings: List[str] = None, minimum: Union[int, float] = None, maximum: Union[int, float] = None, pv: List[Any] = None) → None[source]

Add a sequence of SQLAlchemy columns to a class.

Called from a metaclass. Used to make task creation a bit easier.

Parameters:
  • cls – class to which to add columns
  • prefix – Fieldname will be prefix + str(n), where n is defined as below.
  • start – Start of range.
  • end – End of range. Thus: i will range from 0 to (end - start) inclusive; n will range from start to end inclusive.
  • coltype – SQLAlchemy column type, in either of these formats: (a) Integer (of general type Type[TypeEngine]?); (b) Integer() (of general type TypeEngine).
  • colkwargs – SQLAlchemy column arguments, as in Column(name, coltype, **colkwargs)
  • comment_fmt

    Format string defining field comments. Substitutable values are:

    • {n}: field number (from range).
    • {s}: comment_strings[i], where i is a zero-based index as defined as above, or “” if out of range.
  • comment_strings – see comment_fmt
  • minimum – minimum permitted value, or None
  • maximum – maximum permitted value, or None
  • pv – list of permitted values, or None
camcops_server.cc_modules.cc_db.ancillary_relationship(parent_class_name: str, ancillary_class_name: str, ancillary_fk_to_parent_attr_name: str, ancillary_order_by_attr_name: str = None, read_only: bool = True) → sqlalchemy.orm.relationships.RelationshipProperty[source]

Implements a one-to-many relationship, i.e. one parent to many ancillaries.