SQL import/export complete except for source repos, and datamap

svn: r12932
This commit is contained in:
Doug Blank 2009-08-09 08:01:51 +00:00
parent 18048ccfab
commit 778cc935e9
2 changed files with 54 additions and 32 deletions

View File

@ -248,7 +248,7 @@ def makeDB(db):
db.query("""CREATE TABLE lds ( db.query("""CREATE TABLE lds (
handle CHARACTER(25), handle CHARACTER(25),
type CHARACTER(25), type INTEGER,
place CHARACTER(25), place CHARACTER(25),
famc CHARACTER(25), famc CHARACTER(25),
temple TEXT, temple TEXT,
@ -259,7 +259,9 @@ def makeDB(db):
handle CHARACTER(25), handle CHARACTER(25),
ref CHARACTER(25), ref CHARACTER(25),
role0 INTEGER, role0 INTEGER,
role1 TEXT, role1 INTEGER,
role2 INTEGER,
role3 INTEGER,
private BOOLEAN);""") private BOOLEAN);""")
db.query("""CREATE TABLE address ( db.query("""CREATE TABLE address (
@ -689,16 +691,19 @@ def export_media_ref(db, from_type, from_handle, media):
# ref is the media handle # ref is the media handle
handle = create_id() handle = create_id()
if role is None: if role is None:
role = (-1, '') role = (-1, -1, -1, -1)
db.query("""INSERT into media_ref ( db.query("""INSERT into media_ref (
handle, handle,
ref, ref,
role0, role0,
role1, role1,
private) VALUES (?,?,?,?,?);""", role2,
handle, ref, role[0], role[1] ,private) role3,
private) VALUES (?,?,?,?,?,?,?);""",
handle, ref, role[0], role[1], role[2], role[3], private)
export_list(db, "media_ref", handle, "note", note_list) export_list(db, "media_ref", handle, "note", note_list)
export_attribute_list(db, "media_ref", handle, attribute_list) export_attribute_list(db, "media_ref", handle, attribute_list)
export_source_ref_list(db, "media_ref", handle, source_list)
# And finally, make a link from parent to new object # And finally, make a link from parent to new object
export_link(db, from_type, from_handle, "media_ref", handle) export_link(db, from_type, from_handle, "media_ref", handle)
@ -727,6 +732,7 @@ def export_list(db, from_type, from_handle, to_type, handle_list):
export_link(db, from_type, from_handle, to_type, to_handle) export_link(db, from_type, from_handle, to_type, to_handle)
def export_link(db, from_type, from_handle, to_type, to_handle): def export_link(db, from_type, from_handle, to_type, to_handle):
if to_handle:
db.query("""insert into link ( db.query("""insert into link (
from_type, from_type,
from_handle, from_handle,
@ -734,7 +740,6 @@ def export_link(db, from_type, from_handle, to_type, to_handle):
to_handle) values (?, ?, ?, ?)""", to_handle) values (?, ?, ?, ?)""",
from_type, from_handle, to_type, to_handle) from_type, from_handle, to_type, to_handle)
def export_address(db, from_type, from_handle, address): def export_address(db, from_type, from_handle, address):
(private, asource_list, anote_list, date, location) = address (private, asource_list, anote_list, date, location) = address
addr_handle = create_id() addr_handle = create_id()

View File

@ -53,6 +53,11 @@ from Utils import gender as gender_map
from gui.utils import ProgressMeter from gui.utils import ProgressMeter
from Utils import create_id from Utils import create_id
#-------------------------------------------------------------------------
#
# Import function
#
#-------------------------------------------------------------------------
def lookup(handle, event_ref_list): def lookup(handle, event_ref_list):
""" """
Find the handle in a unserialized event_ref_list and return code. Find the handle in a unserialized event_ref_list and return code.
@ -127,13 +132,13 @@ class SQLReader(object):
# Get methods to retrieve data from the tables # Get methods to retrieve data from the tables
# ----------------------------------------------- # -----------------------------------------------
def get_address_list(self, sql, from_type, from_handle): def get_address_list(self, sql, from_type, from_handle, with_parish):
results = self.get_links(sql, from_type, from_handle, "address") results = self.get_links(sql, from_type, from_handle, "address")
retval = [] retval = []
for handle in results: for handle in results:
result = sql.query("select * from address where handle = ?;", result = sql.query("select * from address where handle = ?;",
handle) handle)
retval.append(self.pack_address(sql, result[0])) retval.append(self.pack_address(sql, result[0], with_parish))
return retval return retval
def get_attribute_list(self, sql, from_type, from_handle): def get_attribute_list(self, sql, from_type, from_handle):
@ -206,13 +211,13 @@ class SQLReader(object):
description)) description))
return retval return retval
def get_location_list(self, sql, from_type, from_handle): def get_location_list(self, sql, from_type, from_handle, with_parish):
handles = self.get_links(sql, from_type, from_handle, "location") handles = self.get_links(sql, from_type, from_handle, "location")
results = [] results = []
for handle in handles: for handle in handles:
results += sql.query("""select * from location where handle = ?;""", results += sql.query("""select * from location where handle = ?;""",
handle) handle)
return [self.pack_location(sql, result, with_parish=True) for result in results] return [self.pack_location(sql, result, with_parish) for result in results]
def get_lds_list(self, sql, from_type, from_handle): def get_lds_list(self, sql, from_type, from_handle):
handles = self.get_links(sql, from_type, from_handle, "lds") handles = self.get_links(sql, from_type, from_handle, "lds")
@ -261,13 +266,13 @@ class SQLReader(object):
# Helpers # Helpers
# --------------------------------- # ---------------------------------
def pack_address(self, sql, data): def pack_address(self, sql, data, with_parish):
(handle, private) = data (handle, private) = data
source_list = self.get_source_ref_list(sql, "address", handle) source_list = self.get_source_ref_list(sql, "address", handle)
date_handle = self.get_link(sql, "address", handle, "date") date_handle = self.get_link(sql, "address", handle, "date")
date = self.get_date(sql, date_handle) date = self.get_date(sql, date_handle)
note_list = self.get_note_list(sql, "address", handle) note_list = self.get_note_list(sql, "address", handle)
location = self.get_location(sql, "address", handle) location = self.get_location(sql, "address", handle, with_parish)
return (private, source_list, note_list, date, location) return (private, source_list, note_list, date, location)
def pack_lds(self, sql, data): def pack_lds(self, sql, data):
@ -284,11 +289,17 @@ class SQLReader(object):
ref, ref,
role0, role0,
role1, role1,
role2,
role3,
private) = data private) = data
source_list = self.get_source_ref_list(sql, "media_ref", handle) source_list = self.get_source_ref_list(sql, "media_ref", handle)
note_list = self.get_note_list(sql, "media_ref", handle) note_list = self.get_note_list(sql, "media_ref", handle)
attribute_list = self.get_attribute_list(sql, "event_ref", handle) attribute_list = self.get_attribute_list(sql, "media_ref", handle)
return (private, source_list, note_list, attribute_list, ref, (role0, role1)) if role0 == role1 == role2 == role3 == -1:
role = None
else:
role = (role0, role1, role2, role3)
return (private, source_list, note_list, attribute_list, ref, role)
def pack_repository(self, sql, data): def pack_repository(self, sql, data):
(handle, (handle,
@ -301,7 +312,7 @@ class SQLReader(object):
marker1, marker1,
private) = data private) = data
note_list = self.get_note_list(sql, "repository", handle) note_list = self.get_note_list(sql, "repository", handle)
address_list = self.get_address_list(sql, "repository", handle) address_list = self.get_address_list(sql, "repository", handle, with_parish=False)
urls = self.get_url_list(sql, "repository", handle) urls = self.get_url_list(sql, "repository", handle)
return (handle, gid, (the_type0, the_type1), name, note_list, return (handle, gid, (the_type0, the_type1), name, note_list,
address_list, urls, change, (marker0, marker1), private) address_list, urls, change, (marker0, marker1), private)
@ -323,7 +334,8 @@ class SQLReader(object):
private) = data private) = data
note_list = self.get_note_list(sql, "event_ref", handle) note_list = self.get_note_list(sql, "event_ref", handle)
attribute_list = self.get_attribute_list(sql, "event_ref", handle) attribute_list = self.get_attribute_list(sql, "event_ref", handle)
return (private, note_list, attribute_list, ref, (role0, role1)) role = (role0, role1)
return (private, note_list, attribute_list, ref, role)
def pack_source_ref(self, sql, data): def pack_source_ref(self, sql, data):
(handle, (handle,
@ -360,13 +372,13 @@ class SQLReader(object):
reporef_list, reporef_list,
(marker0, marker1), private) (marker0, marker1), private)
def get_location(self, sql, from_type, from_handle): def get_location(self, sql, from_type, from_handle, with_parish):
handle = self.get_link(sql, from_type, from_handle, "location") handle = self.get_link(sql, from_type, from_handle, "location")
if handle: if handle:
results = sql.query("""select * from location where handle = ?;""", results = sql.query("""select * from location where handle = ?;""",
handle) handle)
if len(results) == 1: if len(results) == 1:
return self.pack_location(sql, results[0], with_parish=True) return self.pack_location(sql, results[0], with_parish)
def get_names(self, sql, from_type, from_handle, primary): def get_names(self, sql, from_type, from_handle, primary):
handles = self.get_links(sql, from_type, from_handle, "name") handles = self.get_links(sql, from_type, from_handle, "name")
@ -404,7 +416,7 @@ class SQLReader(object):
sort_as, sort_as,
display_as, display_as,
call) = data call) = data
# build up a GRAMPS object:
source_list = self.get_source_ref_list(sql, "name", handle) source_list = self.get_source_ref_list(sql, "name", handle)
note_list = self.get_note_list(sql, "name", handle) note_list = self.get_note_list(sql, "name", handle)
date_handle = self.get_link(sql, "name", handle, "date") date_handle = self.get_link(sql, "name", handle, "date")
@ -434,30 +446,32 @@ class SQLReader(object):
place_row = sql.query("select * from place where handle = ?;", place_row = sql.query("select * from place where handle = ?;",
ref_handle) ref_handle)
if len(place_row) == 1: if len(place_row) == 1:
# return raw DB data here: # return just the handle here:
return place_row[0] return place_row[0][0]
elif len(place_row) == 0: elif len(place_row) == 0:
print "ERROR: get_place_from_handle('%s'), no such handle." % (ref_handle, ) print "ERROR: get_place_from_handle('%s'), no such handle." % (ref_handle, )
else: else:
print "ERROR: get_place_from_handle('%s') should be unique; returned %d records." % (ref_handle, len(place_row)) print "ERROR: get_place_from_handle('%s') should be unique; returned %d records." % (ref_handle, len(place_row))
return None return ''
def get_location_from_handle(self, sql, ref_handle, with_parish=False): def get_main_location(self, sql, from_handle, with_parish):
ref_handle = self.get_link(sql, "place_main", from_handle, "location")
if ref_handle: if ref_handle:
place_row = sql.query("select * from location where handle = ?;", place_row = sql.query("select * from location where handle = ?;",
ref_handle) ref_handle)
if len(place_row) == 1: if len(place_row) == 1:
return self.pack_location(sql, place_row[0], with_parish) return self.pack_location(sql, place_row[0], with_parish)
elif len(place_row) == 0: elif len(place_row) == 0:
print "ERROR: get_location_from_handle('%s'), no such handle." % (ref_handle, ) print "ERROR: get_main_location('%s'), no such handle." % (ref_handle, )
else: else:
print "ERROR: get_location_from_handle('%s') should be unique; returned %d records." % (ref_handle, len(place_row)) print "ERROR: get_main_location('%s') should be unique; returned %d records." % (ref_handle, len(place_row))
return gen.lib.Location().serialize() return gen.lib.Location().serialize()
def get_link(self, sql, from_type, from_handle, to_link): def get_link(self, sql, from_type, from_handle, to_link):
""" """
Return a link, and return handle. Return a link, and return handle.
""" """
if from_handle is None: return
assert type(from_handle) in [unicode, str], "from_handle is wrong type: %s is %s" % (from_handle, type(from_handle)) assert type(from_handle) in [unicode, str], "from_handle is wrong type: %s is %s" % (from_handle, type(from_handle))
rows = self.get_links(sql, from_type, from_handle, to_link) rows = self.get_links(sql, from_type, from_handle, to_link)
if len(rows) == 1: if len(rows) == 1:
@ -495,6 +509,8 @@ class SQLReader(object):
sortval, sortval,
newyear) = rows[0] newyear) = rows[0]
dateval = day1, month1, year1, slash1, day2, month2, year2, slash2 dateval = day1, month1, year1, slash1, day2, month2, year2, slash2
if slash1 == day2 == month2 == year2 == slash2 == 0:
dateval = day1, month1, year1, slash1
return (calendar, modifier, quality, dateval, text, sortval, newyear) return (calendar, modifier, quality, dateval, text, sortval, newyear)
elif len(rows) == 0: elif len(rows) == 0:
return None return None
@ -605,7 +621,7 @@ class SQLReader(object):
family_list = self.get_family_list(sql, "person", handle) family_list = self.get_family_list(sql, "person", handle)
parent_family_list = self.get_parent_family_list(sql, "person", handle) parent_family_list = self.get_parent_family_list(sql, "person", handle)
media_list = self.get_media_list(sql, "person", handle) media_list = self.get_media_list(sql, "person", handle)
address_list = self.get_address_list(sql, "person", handle) address_list = self.get_address_list(sql, "person", handle, with_parish=False)
attribute_list = self.get_attribute_list(sql, "person", handle) attribute_list = self.get_attribute_list(sql, "person", handle)
urls = self.get_url_list(sql, "person", handle) urls = self.get_url_list(sql, "person", handle)
lds_ord_list = self.get_lds_list(sql, "person", handle) lds_ord_list = self.get_lds_list(sql, "person", handle)
@ -688,7 +704,7 @@ class SQLReader(object):
private) = repo private) = repo
note_list = self.get_note_list(sql, "repository", handle) note_list = self.get_note_list(sql, "repository", handle)
address_list = self.get_address_list(sql, "repository", handle) address_list = self.get_address_list(sql, "repository", handle, with_parish=False)
urls = self.get_url_list(sql, "repository", handle) urls = self.get_url_list(sql, "repository", handle)
self.db.repository_map[str(handle)] = (str(handle), gid, self.db.repository_map[str(handle)] = (str(handle), gid,
@ -716,9 +732,10 @@ class SQLReader(object):
private) = place private) = place
# We could look this up by "place_main", but we have the handle: # We could look this up by "place_main", but we have the handle:
main_loc = self.get_location_from_handle(sql, main_loc, main_loc = self.get_main_location(sql, main_loc,
with_parish=True)
alt_location_list = self.get_location_list(sql, "place_alt", handle,
with_parish=True) with_parish=True)
alt_location_list = self.get_location_list(sql, "place_alt", handle)
urls = self.get_url_list(sql, "place", handle) urls = self.get_url_list(sql, "place", handle)
media_list = self.get_media_list(sql, "place", handle) media_list = self.get_media_list(sql, "place", handle)
source_list = self.get_source_ref_list(sql, "place", handle) source_list = self.get_source_ref_list(sql, "place", handle)