1 """GnuMed database object business class.
2
3 Overview
4 --------
5 This class wraps a source relation (table, view) which
6 represents an entity that makes immediate business sense
7 such as a vaccination or a medical document. In many if
8 not most cases this source relation is a denormalizing
9 view. The data in that view will in most cases, however,
10 originate from several normalized tables. One instance
11 of this class represents one row of said source relation.
12
13 Note, however, that this class does not *always* simply
14 wrap a single table or view. It can also encompass several
15 relations (views, tables, sequences etc) that taken together
16 form an object meaningful to *business* logic.
17
18 Initialization
19 --------------
20 There are two ways to initialize an instance with values.
21 One way is to pass a "primary key equivalent" object into
22 __init__(). Refetch_payload() will then pull the data from
23 the backend. Another way would be to fetch the data outside
24 the instance and pass it in via the <row> argument. In that
25 case the instance will not initially connect to the databse
26 which may offer a great boost to performance.
27
28 Values API
29 ----------
30 Field values are cached for later access. They can be accessed
31 by a dictionary API, eg:
32
33 old_value = object['field']
34 object['field'] = new_value
35
36 The field names correspond to the respective column names
37 in the "main" source relation. Accessing non-existant field
38 names will raise an error, so does trying to set fields not
39 listed in self.__class__._updatable_fields. To actually
40 store updated values in the database one must explicitly
41 call save_payload().
42
43 The class will in many cases be enhanced by accessors to
44 related data that is not directly part of the business
45 object itself but are closely related, such as codes
46 linked to a clinical narrative entry (eg a diagnosis). Such
47 accessors in most cases start with get_*. Related setters
48 start with set_*. The values can be accessed via the
49 object['field'] syntax, too, but they will be cached
50 independantly.
51
52 Concurrency handling
53 --------------------
54 GnuMed connections always run transactions in isolation level
55 "serializable". This prevents transactions happening at the
56 *very same time* to overwrite each other's data. All but one
57 of them will abort with a concurrency error (eg if a
58 transaction runs a select-for-update later than another one
59 it will hang until the first transaction ends. Then it will
60 succeed or fail depending on what the first transaction
61 did). This is standard transactional behaviour.
62
63 However, another transaction may have updated our row
64 between the time we first fetched the data and the time we
65 start the update transaction. This is noticed by getting the
66 XMIN system column for the row when initially fetching the
67 data and using that value as a where condition value when
68 updating the row later. If the row had been updated (xmin
69 changed) or deleted (primary key disappeared) in the
70 meantime the update will touch zero rows (as no row with
71 both PK and XMIN matching is found) even if the query itself
72 syntactically succeeds.
73
74 When detecting a change in a row due to XMIN being different
75 one needs to be careful how to represent that to the user.
76 The row may simply have changed but it also might have been
77 deleted and a completely new and unrelated row which happens
78 to have the same primary key might have been created ! This
79 row might relate to a totally different context (eg. patient,
80 episode, encounter).
81
82 One can offer all the data to the user:
83
84 self.original_payload
85 - contains the data at the last successful refetch
86
87 self.modified_payload
88 - contains the modified payload just before the last
89 failure of save_payload() - IOW what is currently
90 in the database
91
92 self._payload
93 - contains the currently active payload which may or
94 may not contain changes
95
96 For discussion on this see the thread starting at:
97
98 http://archives.postgresql.org/pgsql-general/2004-10/msg01352.php
99
100 and here
101
102 http://groups.google.com/group/pgsql.general/browse_thread/thread/e3566ba76173d0bf/6cf3c243a86d9233
103 (google for "XMIN semantic at peril")
104
105 Problem cases with XMIN:
106
107 1) not unlikely
108 - a very old row is read with XMIN
109 - vacuum comes along and sets XMIN to FrozenTransactionId
110 - now XMIN changed but the row actually didn't !
111 - an update with "... where xmin = old_xmin ..." fails
112 although there is no need to fail
113
114 2) quite unlikely
115 - a row is read with XMIN
116 - a long time passes
117 - the original XMIN gets frozen to FrozenTransactionId
118 - another writer comes along and changes the row
119 - incidentally the exact same old row gets the old XMIN *again*
120 - now XMIN is (again) the same but the data changed !
121 - a later update fails to detect the concurrent change !!
122
123 TODO:
124 The solution is to use our own column for optimistic locking
125 which gets updated by an AFTER UPDATE trigger.
126 """
127
128
129
130 __version__ = "$Revision: 1.60 $"
131 __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
132 __license__ = "GPL"
133
134 import sys, copy, types, inspect, logging
135
136
137 if __name__ == '__main__':
138 sys.path.insert(0, '../../')
139 from Gnumed.pycommon import gmExceptions, gmPG2
140
141
142 _log = logging.getLogger('gm.db')
143 _log.info(__version__)
144
146 """Represents business objects in the database.
147
148 Rules:
149 - instances ARE ASSUMED TO EXIST in the database
150 - PK construction (aPK_obj): DOES verify its existence on instantiation
151 (fetching data fails)
152 - Row construction (row): allowed by using a dict of pairs
153 field name: field value (PERFORMANCE improvement)
154 - does NOT verify FK target existence
155 - does NOT create new entries in the database
156 - does NOT lazy-fetch fields on access
157
158 Class scope SQL commands and variables:
159
160 <_cmd_fetch_payload>
161 - must return exactly one row
162 - where clause argument values are expected
163 in self.pk_obj (taken from __init__(aPK_obj))
164 - must return xmin of all rows that _cmds_store_payload
165 will be updating, so views must support the xmin columns
166 of their underlying tables
167
168 <_cmds_store_payload>
169 - one or multiple "update ... set ... where xmin_* = ..." statements
170 which actually update the database from the data in self._payload,
171 - the last query must refetch the XMIN values needed to detect
172 concurrent updates, their field names had better be the same as
173 in _cmd_fetch_payload
174
175 <_updatable_fields>
176 - a list of fields available for update via object['field']
177
178 """
179
180 - def __init__(self, aPK_obj=None, row=None):
181 """Init business object.
182 """
183
184
185
186 self.pk_obj = '<uninitialized>'
187 self._idx = {}
188 self._payload = []
189 self._ext_cache = {}
190 self._is_modified = False
191
192
193 self.__class__._cmd_fetch_payload
194 self.__class__._cmds_store_payload
195 self.__class__._updatable_fields
196
197 if aPK_obj is not None:
198 self.__init_from_pk(aPK_obj=aPK_obj)
199 else:
200 self._init_from_row_data(row=row)
201
202 self._is_modified = False
203
205 """Creates a new clinical item instance by its PK.
206
207 aPK_obj can be:
208 - a simple value
209 * the primary key WHERE condition must be
210 a simple column
211 - a dictionary of values
212 * the primary key where condition must be a
213 subselect consuming the dict and producing
214 the single-value primary key
215 """
216 self.pk_obj = aPK_obj
217 result = self.refetch_payload()
218 if result is True:
219 self.original_payload = {}
220 for field in self._idx.keys():
221 self.original_payload[field] = self._payload[self._idx[field]]
222 return True
223
224 if result is False:
225 raise gmExceptions.ConstructorError, "[%s:%s]: error loading instance" % (self.__class__.__name__, self.pk_obj)
226
228 """Creates a new clinical item instance given its fields.
229
230 row must be a dict with the fields:
231 - pk_field: the name of the primary key field
232 - idx: a dict mapping field names to position
233 - data: the field values in a list (as returned by
234 cursor.fetchone() in the DB-API)
235 """
236 try:
237 self._idx = row['idx']
238 self._payload = row['data']
239 self.pk_obj = self._payload[self._idx[row['pk_field']]]
240 except:
241 _log.exception('faulty <row> argument structure: %s' % row)
242 raise gmExceptions.ConstructorError, "[%s:??]: error loading instance from row data" % self.__class__.__name__
243
244 if len(self._idx.keys()) != len(self._payload):
245 _log.critical('field index vs. payload length mismatch: %s field names vs. %s fields' % (len(self._idx.keys()), len(self._payload)))
246 _log.critical('faulty <row> argument structure: %s' % row)
247 raise gmExceptions.ConstructorError, "[%s:??]: error loading instance from row data" % self.__class__.__name__
248
249 self.original_payload = {}
250 for field in self._idx.keys():
251 self.original_payload[field] = self._payload[self._idx[field]]
252
254 if self.__dict__.has_key('_is_modified'):
255 if self._is_modified:
256 _log.critical('[%s:%s]: loosing payload changes' % (self.__class__.__name__, self.pk_obj))
257 _log.debug('original: %s' % self.original_payload)
258 _log.debug('modified: %s' % self._payload)
259
261 tmp = []
262 try:
263 for attr in self._idx.keys():
264 if self._payload[self._idx[attr]] is None:
265 tmp.append(u'%s: NULL' % attr)
266 else:
267 tmp.append('%s: >>%s<<' % (attr, self._payload[self._idx[attr]]))
268 return '[%s:%s]: %s' % (self.__class__.__name__, self.pk_obj, str(tmp))
269 except:
270 return 'nascent [%s @ %s], cannot show payload and primary key' %(self.__class__.__name__, id(self))
271
273
274
275
276 try:
277 return self._payload[self._idx[attribute]]
278 except KeyError:
279 pass
280
281
282 getter = getattr(self, 'get_%s' % attribute, None)
283 if not callable(getter):
284 _log.warning('[%s]: no attribute [%s]' % (self.__class__.__name__, attribute))
285 _log.warning('[%s]: valid attributes: %s' % (self.__class__.__name__, str(self._idx.keys())))
286 _log.warning('[%s]: no getter method [get_%s]' % (self.__class__.__name__, attribute))
287 methods = filter(lambda x: x[0].startswith('get_'), inspect.getmembers(self, inspect.ismethod))
288 _log.warning('[%s]: valid getter methods: %s' % (self.__class__.__name__, str(methods)))
289 raise gmExceptions.NoSuchBusinessObjectAttributeError, '[%s]: cannot access [%s]' % (self.__class__.__name__, attribute)
290
291 self._ext_cache[attribute] = getter()
292 return self._ext_cache[attribute]
293
295
296
297 if attribute in self.__class__._updatable_fields:
298 try:
299 if self._payload[self._idx[attribute]] != value:
300 self._payload[self._idx[attribute]] = value
301 self._is_modified = True
302 return
303 except KeyError:
304 _log.warning('[%s]: cannot set attribute <%s> despite marked settable' % (self.__class__.__name__, attribute))
305 _log.warning('[%s]: supposedly settable attributes: %s' % (self.__class__.__name__, str(self.__class__._updatable_fields)))
306 raise gmExceptions.NoSuchBusinessObjectAttributeError, '[%s]: cannot access [%s]' % (self.__class__.__name__, attribute)
307
308
309 if hasattr(self, 'set_%s' % attribute):
310 setter = getattr(self, "set_%s" % attribute)
311 if not callable(setter):
312 raise gmExceptions.NoSuchBusinessObjectAttributeError, '[%s] setter [set_%s] not callable' % (self.__class__.__name__, attribute)
313 try:
314 del self._ext_cache[attribute]
315 except KeyError:
316 pass
317 if type(value) is types.TupleType:
318 if setter(*value):
319 self._is_modified = True
320 return
321 raise gmExceptions.BusinessObjectAttributeNotSettableError, '[%s]: setter [%s] failed for [%s]' % (self.__class__.__name__, setter, value)
322 if setter(value):
323 self._is_modified = True
324 return
325
326
327 _log.error('[%s]: cannot find attribute <%s> or setter method [set_%s]' % (self.__class__.__name__, attribute, attribute))
328 _log.warning('[%s]: settable attributes: %s' % (self.__class__.__name__, str(self.__class__._updatable_fields)))
329 methods = filter(lambda x: x[0].startswith('set_'), inspect.getmembers(self, inspect.ismethod))
330 _log.warning('[%s]: valid setter methods: %s' % (self.__class__.__name__, str(methods)))
331 raise gmExceptions.BusinessObjectAttributeNotSettableError, '[%s]: cannot set [%s]' % (self.__class__.__name__, attribute)
332
333
334
336 raise NotImplementedError('comparison between [%s] and [%s] not implemented' % (self, another_object))
337
339 return self._is_modified
340
342 return self._idx.keys()
343
346
348 _log.error('[%s:%s]: forgot to override get_patient()' % (self.__class__.__name__, self.pk_obj))
349 return None
350
352 """Fetch field values from backend.
353 """
354 if self._is_modified:
355 if ignore_changes:
356 _log.critical('[%s:%s]: loosing payload changes' % (self.__class__.__name__, self.pk_obj))
357 _log.debug('original: %s' % self.original_payload)
358 _log.debug('modified: %s' % self._payload)
359 else:
360 _log.critical('[%s:%s]: cannot reload, payload changed' % (self.__class__.__name__, self.pk_obj))
361 return False
362
363 if type(self.pk_obj) == types.DictType:
364 arg = self.pk_obj
365 else:
366 arg = [self.pk_obj]
367 rows, self._idx = gmPG2.run_ro_queries (
368 queries = [{'cmd': self.__class__._cmd_fetch_payload, 'args': arg}],
369 get_col_idx = True
370 )
371 if len(rows) == 0:
372 _log.error('[%s:%s]: no such instance' % (self.__class__.__name__, self.pk_obj))
373 return False
374 self._payload = rows[0]
375 return True
376
379
380 - def save(self, conn=None):
382
384 """Store updated values (if any) in database.
385
386 Optionally accepts a pre-existing connection
387 - returns a tuple (<True|False>, <data>)
388 - True: success
389 - False: an error occurred
390 * data is (error, message)
391 * for error meanings see gmPG2.run_rw_queries()
392 """
393 if not self._is_modified:
394 return (True, None)
395
396 args = {}
397 for field in self._idx.keys():
398 args[field] = self._payload[self._idx[field]]
399 self.modified_payload = args
400
401 close_conn = self.__noop
402 if conn is None:
403 conn = gmPG2.get_connection(readonly=False)
404 close_conn = conn.close
405
406
407
408
409
410
411 queries = []
412 for query in self.__class__._cmds_store_payload:
413 queries.append({'cmd': query, 'args': args})
414 rows, idx = gmPG2.run_rw_queries (
415 link_obj = conn,
416 queries = queries,
417 return_data = True,
418 get_col_idx = True
419 )
420
421
422 row = rows[0]
423 for key in idx:
424 try:
425 self._payload[self._idx[key]] = row[idx[key]]
426 except KeyError:
427 conn.rollback()
428 close_conn()
429 _log.error('[%s:%s]: cannot update instance, XMIN refetch key mismatch on [%s]' % (self.__class__.__name__, self.pk_obj, key))
430 _log.error('payload keys: %s' % str(self._idx))
431 _log.error('XMIN refetch keys: %s' % str(idx))
432 _log.error(args)
433 raise
434
435 conn.commit()
436 close_conn()
437
438 self._is_modified = False
439
440 self.original_payload = {}
441 for field in self._idx.keys():
442 self.original_payload[field] = self._payload[self._idx[field]]
443
444 return (True, None)
445
446 if __name__ == '__main__':
447
458
459 if len(sys.argv) > 1 and sys.argv[1] == u'test':
460
461 from Gnumed.pycommon import gmI18N
462 gmI18N.activate_locale()
463 gmI18N.install_domain()
464
465 data = {
466 'pk_field': 'bogus_pk',
467 'idx': {'bogus_pk': 0, 'bogus_field': 1},
468 'data': [-1, 'bogus_data']
469 }
470 obj = cTestObj(row=data)
471
472 obj['wrong_field'] = 1
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763