From 0a476e2a3c2139d7466d1e0a75d6a91ba6605da1 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Sun, 9 Aug 2009 17:40:36 +0000 Subject: [PATCH] SQL update: two diffs left, in family and place svn: r12935 --- src/plugins/export/ExportSql.py | 105 ++++++++++++++++++++++++++------ src/plugins/import/ImportSql.py | 64 ++++++++++--------- 2 files changed, 122 insertions(+), 47 deletions(-) diff --git a/src/plugins/export/ExportSql.py b/src/plugins/export/ExportSql.py index 4c8ef75f4..b7ca4ee6f 100644 --- a/src/plugins/export/ExportSql.py +++ b/src/plugins/export/ExportSql.py @@ -1,6 +1,6 @@ # Gramps - a GTK+/GNOME based genealogy program # -# Copyright (C) 2008 Douglas S. Blank +# Copyright (C) 2008 Douglas S. Blank # # 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 @@ -18,27 +18,41 @@ # # $Id$ # + +"Export to SQLite Database" + #------------------------------------------------------------------------ # -# python modules +# Standard Python Modules # #------------------------------------------------------------------------ from gettext import gettext as _ +from gettext import ngettext import sqlite3 as sqlite -import re +import time + +#------------------------------------------------------------------------ +# +# Set up logging +# +#------------------------------------------------------------------------ +import logging +log = logging.getLogger(".ExportSql") #------------------------------------------------------------------------ # # GRAMPS modules # #------------------------------------------------------------------------ -import gen.lib from gen.plug import PluginManager, ExportPlugin -import DateHandler -from gui.utils import ProgressMeter import ExportOptions from Utils import create_id +#------------------------------------------------------------------------- +# +# Export functions +# +#------------------------------------------------------------------------- def lookup(index, event_ref_list): """ Get the unserialized event_ref in an list of them and return it. @@ -60,6 +74,7 @@ def makeDB(db): db.query("""drop table event;""") db.query("""drop table family;""") db.query("""drop table repository;""") + db.query("""drop table repository_ref;""") db.query("""drop table date;""") db.query("""drop table place;""") db.query("""drop table source;""") @@ -77,6 +92,7 @@ def makeDB(db): db.query("""drop table location;""") db.query("""drop table attribute;""") db.query("""drop table url;""") + db.query("""drop table datamap;""") db.query("""CREATE TABLE note ( handle CHARACTER(25), @@ -193,6 +209,14 @@ def makeDB(db): marker1 TEXT, private BOOLEAN);""") + db.query("""CREATE TABLE repository_ref ( + handle CHARACTER(25), + ref CHARACTER(25), + call_number TEXT, + source_media_type0 INTEGER, + source_media_type1 TEXT, + private BOOLEAN);""") + db.query("""CREATE TABLE repository ( handle CHARACTER(25), gid CHARACTER(25), @@ -252,7 +276,7 @@ def makeDB(db): place CHARACTER(25), famc CHARACTER(25), temple TEXT, - status TEXT, + status INTEGER, private BOOLEAN);""") db.query("""CREATE TABLE media_ref ( @@ -295,6 +319,12 @@ def makeDB(db): private BOOLEAN); """) + db.query("""CREATE TABLE datamap ( + handle CHARACTER(25), + key_field TEXT, + value_field TXT); + """) + class Database(object): """ The db connection. @@ -740,6 +770,17 @@ def export_link(db, from_type, from_handle, to_type, to_handle): to_handle) values (?, ?, ?, ?)""", from_type, from_handle, to_type, to_handle) +def export_datamap_dict(db, from_type, from_handle, datamap): + for key_field in datamap: + handle = create_id() + value_field = datamap[key_field] + db.query("""INSERT INTO datamap ( + handle, + key_field, + value_field) values (?, ?, ?)""", + handle, key_field, value_field) + export_link(db, from_type, from_handle, "datamap", handle) + def export_address(db, from_type, from_handle, address): (private, asource_list, anote_list, date, location) = address addr_handle = create_id() @@ -776,10 +817,36 @@ def export_location(db, from_type, from_handle, location): # finally, link the parent to the address export_link(db, from_type, from_handle, "location", handle) +def export_repository_ref_list(db, from_type, from_handle, reporef_list): + for repo in reporef_list: + (note_list, + ref, + call_number, + source_media_type, + private) = repo + handle = create_id() + db.query("""insert INTO repository_ref ( + handle, + ref, + call_number, + source_media_type0, + source_media_type1, + private) VALUES (?,?,?,?,?,?);""", + handle, + ref, + call_number, + source_media_type[0], + source_media_type[1], + private) + export_list(db, "repository_ref", handle, "note", note_list) + # finally, link this to parent + export_link(db, from_type, from_handle, "repository_ref", handle) + def exportData(database, filename, option_box=None, callback=None): if not callable(callback): callback = lambda (percent): None # dummy + start = time.time() total = (len(database.note_map) + len(database.person_map) + len(database.event_map) + @@ -952,14 +1019,8 @@ def exportData(database, filename, option_box=None, callback=None): marker[0], marker[1], private) export_list(db, "source", handle, "note", note_list) export_media_ref_list(db, "source", handle, media_list) - # FIXME: reporef_list, datamap - #print "FIXME: reporef_list", reporef_list - #print "FIXME: datamap", datamap -#FIXME: reporef_list [] -#FIXME: datamap {} -#FIXME: reporef_list [([], u'b2cfa6e37654b308559', '', (2, u''), False)] -#FIXME: datamap {} - + export_datamap_dict(db, "source", handle, datamap) + export_repository_ref_list(db, "source", handle, reporef_list) count += 1 callback(100 * count/total) @@ -996,12 +1057,18 @@ def exportData(database, filename, option_box=None, callback=None): count += 1 callback(100 * count/total) + total_time = time.time() - start + msg = ngettext('Export Complete: %d second','Export Complete: %d seconds', total_time ) % total_time + print msg return True - # Bookmarks - # Header - researcher info - # Name formats - # Namemaps? +# Future ideas +# Also include meta: +# Bookmarks +# Header - researcher info +# Name formats +# Namemaps? +# GRAMPS Version #, date, exporter #------------------------------------------------------------------------- # diff --git a/src/plugins/import/ImportSql.py b/src/plugins/import/ImportSql.py index 544062fc5..24de89223 100644 --- a/src/plugins/import/ImportSql.py +++ b/src/plugins/import/ImportSql.py @@ -19,7 +19,7 @@ # # $Id$ -"Import from SQL Database" +"Import from SQLite Database" #------------------------------------------------------------------------- # @@ -29,7 +29,6 @@ from gettext import gettext as _ from gettext import ngettext import sqlite3 as sqlite -import re import time #------------------------------------------------------------------------ @@ -47,15 +46,12 @@ log = logging.getLogger(".ImportSql") #------------------------------------------------------------------------- import gen.lib from QuestionDialog import ErrorDialog -from DateHandler import parser as _dp from gen.plug import PluginManager, ImportPlugin -from Utils import gender as gender_map -from gui.utils import ProgressMeter from Utils import create_id #------------------------------------------------------------------------- # -# Import function +# Import functions # #------------------------------------------------------------------------- def lookup(handle, event_ref_list): @@ -75,7 +71,7 @@ def lookup(handle, event_ref_list): #------------------------------------------------------------------------- # -# SQL Reader +# SQLite DB Class # #------------------------------------------------------------------------- class Database(object): @@ -109,6 +105,11 @@ class Database(object): self.cursor.close() self.db.close() +#------------------------------------------------------------------------- +# +# SQL Reader +# +#------------------------------------------------------------------------- class SQLReader(object): def __init__(self, db, filename, callback): if not callable(callback): @@ -174,8 +175,17 @@ class SQLReader(object): return retval def get_datamap(self, sql, from_type, from_handle): - # FIXME: - return {} + handles = self.get_links(sql, from_type, from_handle, "datamap") + datamap = {} + for handle in handles: + row = sql.query("select * from datamap where handle = ?;", + handle) + if len(row) == 1: + (handle, key_field, value_field) = row[0] + datamap[key_field] = value_field + else: + print "ERROR: invalid datamap item '%s'" % handle + return datamap def get_event_ref_list(self, sql, from_type, from_handle): results = self.get_links(sql, from_type, from_handle, "event_ref") @@ -238,13 +248,13 @@ class SQLReader(object): def get_note_list(self, sql, from_type, from_handle): return self.get_links(sql, from_type, from_handle, "note") - def get_repository_list(self, sql, from_type, from_handle): - handles = self.get_links(sql, from_type, from_handle, "repository") + def get_repository_ref_list(self, sql, from_type, from_handle): + handles = self.get_links(sql, from_type, from_handle, "repository_ref") results = [] for handle in handles: - results += sql.query("""select * from repository where handle = ?;""", + results += sql.query("""select * from repository_ref where handle = ?;""", handle) - return [self.pack_repository(sql, result) for result in results] + return [self.pack_repository_ref(sql, result) for result in results] def get_source_ref_list(self, sql, from_type, from_handle): handles = self.get_links(sql, from_type, from_handle, "source_ref") @@ -301,21 +311,19 @@ class SQLReader(object): role = (role0, role1, role2, role3) return (private, source_list, note_list, attribute_list, ref, role) - def pack_repository(self, sql, data): + def pack_repository_ref(self, sql, data): (handle, - gid, - the_type0, - the_type1, - name, - change, - marker0, - marker1, + ref, + call_number, + source_media_type0, + source_media_type1, private) = data - note_list = self.get_note_list(sql, "repository", handle) - address_list = self.get_address_list(sql, "repository", handle, with_parish=False) - urls = self.get_url_list(sql, "repository", handle) - return (handle, gid, (the_type0, the_type1), name, note_list, - address_list, urls, change, (marker0, marker1), private) + note_list = self.get_note_list(sql, "repository_ref", handle) + return (note_list, + ref, + call_number, + (source_media_type0, source_media_type1), + private) def pack_url(self, sql, data): (handle, @@ -361,7 +369,7 @@ class SQLReader(object): private) = data note_list = self.get_note_list(sql, "source", handle) media_list = self.get_media_list(sql, "source", handle) - reporef_list = self.get_repository_list(sql, "source", handle) + reporef_list = self.get_repository_ref_list(sql, "source", handle) datamap = {} return (handle, gid, title, author, pubinfo, @@ -768,7 +776,7 @@ class SQLReader(object): note_list = self.get_note_list(sql, "source", handle) media_list = self.get_media_list(sql, "source", handle) datamap = self.get_datamap(sql, "source", handle) - reporef_list = self.get_repository_list(sql, "source", handle) + reporef_list = self.get_repository_ref_list(sql, "source", handle) self.db.source_map[str(handle)] = (str(handle), gid, title, author, pubinfo,