2007-06-04 08:22:49 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000-2007 Donald N. Allingham
|
|
|
|
#
|
|
|
|
# This program 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 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
2011-10-23 08:43:50 +05:30
|
|
|
# gen/db/cursor.py
|
|
|
|
# $Id$
|
|
|
|
#
|
2007-06-04 08:22:49 +05:30
|
|
|
|
2009-06-12 21:47:07 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-09-24 01:37:58 +05:30
|
|
|
from cPickle import dumps, loads
|
2011-01-24 02:55:51 +05:30
|
|
|
|
|
|
|
import config
|
|
|
|
if config.get('preferences.use-bsddb3'):
|
|
|
|
from bsddb3 import db
|
|
|
|
else:
|
|
|
|
from bsddb import db
|
2009-06-12 21:47:07 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2010-08-09 03:36:54 +05:30
|
|
|
# BsddbBaseCursor class
|
2009-06-12 21:47:07 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
2010-08-09 03:36:54 +05:30
|
|
|
class BsddbBaseCursor(object):
|
2007-06-04 08:22:49 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Provide a basic iterator that allows the user to cycle through
|
2008-02-23 14:08:55 +05:30
|
|
|
the elements in a particular map.
|
|
|
|
|
|
|
|
A cursor should never be directly instantiated. Instead, in should be
|
|
|
|
created by the database class.
|
2007-06-04 08:22:49 +05:30
|
|
|
|
|
|
|
A cursor should only be used for a single pass through the
|
|
|
|
database. If multiple passes are needed, multiple cursors
|
|
|
|
should be used.
|
|
|
|
"""
|
2009-06-12 21:47:07 +05:30
|
|
|
|
2009-09-24 01:37:58 +05:30
|
|
|
def __init__(self, txn=None, update=False, commit=False):
|
2009-06-12 21:47:07 +05:30
|
|
|
"""
|
|
|
|
Instantiate the object. Note, this method should be overridden in
|
|
|
|
derived classes that properly set self.cursor and self.source
|
|
|
|
"""
|
|
|
|
self.cursor = self.source = None
|
2009-09-24 01:37:58 +05:30
|
|
|
self.txn = txn
|
|
|
|
self._update = update
|
|
|
|
self.commit = commit
|
2007-06-04 08:22:49 +05:30
|
|
|
|
2009-08-19 22:44:32 +05:30
|
|
|
def __getattr__(self, name):
|
|
|
|
"""
|
|
|
|
Return a method from the underlying cursor object, if it exists
|
|
|
|
"""
|
|
|
|
return getattr(self.cursor, name)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
"""
|
|
|
|
Context manager enter method
|
|
|
|
"""
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
"""
|
|
|
|
Context manager exit method
|
|
|
|
"""
|
|
|
|
self.close()
|
2009-09-24 01:37:58 +05:30
|
|
|
if self.txn and self.commit:
|
|
|
|
self.txn.commit()
|
2009-08-19 22:44:32 +05:30
|
|
|
return exc_type is None
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""
|
|
|
|
Iterator
|
|
|
|
"""
|
|
|
|
|
|
|
|
data = self.first()
|
2009-09-24 01:37:58 +05:30
|
|
|
_n = self.next # Saved attribute lookup in the loop
|
2009-08-19 22:44:32 +05:30
|
|
|
while data:
|
|
|
|
yield data
|
2009-09-24 01:37:58 +05:30
|
|
|
data = _n()
|
2009-08-19 22:44:32 +05:30
|
|
|
|
2009-09-24 01:37:58 +05:30
|
|
|
def _get(_flags=0):
|
|
|
|
""" Closure that returns a cursor get function """
|
2007-06-04 08:22:49 +05:30
|
|
|
|
2009-09-24 01:37:58 +05:30
|
|
|
def get(self, flags=0, **kwargs):
|
|
|
|
"""
|
|
|
|
Issue DBCursor get call (with DB_RMW flag if update requested)
|
|
|
|
Return results to caller
|
|
|
|
"""
|
|
|
|
data = self.cursor.get(
|
|
|
|
_flags | flags | (db.DB_RMW if self._update else 0),
|
|
|
|
**kwargs)
|
2007-06-04 08:22:49 +05:30
|
|
|
|
2009-09-24 01:37:58 +05:30
|
|
|
return (data[0], loads(data[1])) if data else None
|
2007-06-04 08:22:49 +05:30
|
|
|
|
2009-09-24 01:37:58 +05:30
|
|
|
return get
|
2007-06-04 08:22:49 +05:30
|
|
|
|
2009-09-24 01:37:58 +05:30
|
|
|
# Use closure to define access methods
|
2009-08-07 14:41:13 +05:30
|
|
|
|
2009-09-24 01:37:58 +05:30
|
|
|
current = _get(db.DB_CURRENT)
|
|
|
|
first = _get(db.DB_FIRST)
|
|
|
|
next = _get(db.DB_NEXT)
|
|
|
|
last = _get(db.DB_LAST)
|
|
|
|
prev = _get(db.DB_PREV)
|
2009-08-19 22:44:32 +05:30
|
|
|
|
2009-09-24 01:37:58 +05:30
|
|
|
def update(self, key, data, flags=0, **kwargs):
|
2009-06-12 21:47:07 +05:30
|
|
|
"""
|
2009-09-24 01:37:58 +05:30
|
|
|
Write the current key, data pair to the database.
|
2009-06-12 21:47:07 +05:30
|
|
|
"""
|
2009-09-24 01:37:58 +05:30
|
|
|
self.cursor.put(key, dumps(data), flags=flags | db.DB_CURRENT,
|
|
|
|
**kwargs)
|