Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Out of my head:</p> <p>I define two example domains, <code>User</code> and <code>Animal</code>, an base storage class <code>Store</code> and two specialized Storage classes <code>UserStore</code> and <code>AnimalStore</code>. Use of context manager closes db connection (for simplicity I use sqlite in this example):</p> <pre><code>import sqlite3 def get_connection(): return sqlite3.connect('test.sqlite') class StoreException(Exception): def __init__(self, message, *errors): Exception.__init__(self, message) self.errors = errors # domains class User(): def __init__(self, name): self.name = name class Animal(): def __init__(self, name): self.name = name # base store class class Store(): def __init__(self): try: self.conn = get_connection() except Exception as e: raise StoreException(*e.args, **e.kwargs) self._complete = False def __enter__(self): return self def __exit__(self, type_, value, traceback): # can test for type and handle different situations self.close() def complete(self): self._complete = True def close(self): if self.conn: try: if self._complete: self.conn.commit() else: self.conn.rollback() except Exception as e: raise StoreException(*e.args) finally: try: self.conn.close() except Exception as e: raise StoreException(*e.args) # store for User obects class UserStore(Store): def add_user(self, user): try: c = self.conn.cursor() # this needs an appropriate table c.execute('INSERT INTO user (name) VALUES(?)', (user.name,)) except Exception as e: raise StoreException('error storing user') # store for Animal obects class AnimalStore(Store): def add_animal(self, animal): try: c = self.conn.cursor() # this needs an appropriate table c.execute('INSERT INTO animal (name) VALUES(?)', (animal.name,)) except Exception as e: raise StoreException('error storing animal') # do something try: with UserStore() as user_store: user_store.add_user(User('John')) user_store.complete() with AnimalStore() as animal_store: animal_store.add_animal(Animal('Dog')) animal_store.add_animal(Animal('Pig')) animal_store.add_animal(Animal('Cat')) animal_store.add_animal(Animal('Wolf')) animal_store.complete() except StoreException as e: # exception handling here print(e) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload