Very close to having the SQL import/export done

svn: r12930
This commit is contained in:
Doug Blank 2009-08-09 04:14:33 +00:00
parent 6c07dfc9dd
commit 78530fae2e
2 changed files with 681 additions and 503 deletions

View File

@ -40,6 +40,9 @@ import ExportOptions
from Utils import create_id from Utils import create_id
def lookup(index, event_ref_list): def lookup(index, event_ref_list):
"""
Get the unserialized event_ref in an list of them and return it.
"""
if index < 0: if index < 0:
return None return None
else: else:
@ -71,9 +74,10 @@ def makeDB(db):
db.query("""drop table lds;""") db.query("""drop table lds;""")
db.query("""drop table media_ref;""") db.query("""drop table media_ref;""")
db.query("""drop table address;""") db.query("""drop table address;""")
db.query("""drop table location;""")
db.query("""drop table attribute;""") db.query("""drop table attribute;""")
db.query("""drop table url;""") db.query("""drop table url;""")
# Completed
db.query("""CREATE TABLE note ( db.query("""CREATE TABLE note (
handle CHARACTER(25), handle CHARACTER(25),
gid CHARACTER(25), gid CHARACTER(25),
@ -85,9 +89,8 @@ def makeDB(db):
marker0 INTEGER, marker0 INTEGER,
marker1 TEXT, marker1 TEXT,
private BOOLEAN);""") private BOOLEAN);""")
db.query("""CREATE TABLE name ( db.query("""CREATE TABLE name (
from_handle CHARACTER(25),
handle CHARACTER(25), handle CHARACTER(25),
primary_name BOOLEAN, primary_name BOOLEAN,
private BOOLEAN, private BOOLEAN,
@ -105,8 +108,7 @@ def makeDB(db):
call TEXT);""") call TEXT);""")
db.query("""CREATE TABLE date ( db.query("""CREATE TABLE date (
from_type CHARACTER(25), handle CHARACTER(25),
from_handle CHARACTER(25),
calendar INTEGER, calendar INTEGER,
modifier INTEGER, modifier INTEGER,
quality INTEGER, quality INTEGER,
@ -149,6 +151,7 @@ def makeDB(db):
handle CHARACTER(25), handle CHARACTER(25),
gid CHARACTER(25), gid CHARACTER(25),
title TEXT, title TEXT,
main_location CHARACTER(25),
long FLOAT, long FLOAT,
lat FLOAT, lat FLOAT,
change INTEGER, change INTEGER,
@ -168,7 +171,6 @@ def makeDB(db):
private BOOLEAN);""") private BOOLEAN);""")
db.query("""CREATE TABLE source ( db.query("""CREATE TABLE source (
from_type CHARACTER(25),
handle CHARACTER(25), handle CHARACTER(25),
gid CHARACTER(25), gid CHARACTER(25),
title TEXT, title TEXT,
@ -202,6 +204,7 @@ def makeDB(db):
marker1 TEXT, marker1 TEXT,
private BOOLEAN);""") private BOOLEAN);""")
# One link to link them all
db.query("""CREATE TABLE link ( db.query("""CREATE TABLE link (
from_type CHARACTER(25), from_type CHARACTER(25),
from_handle CHARACTER(25), from_handle CHARACTER(25),
@ -216,22 +219,18 @@ def makeDB(db):
start_stop_list TEXT);""") start_stop_list TEXT);""")
db.query("""CREATE TABLE event_ref ( db.query("""CREATE TABLE event_ref (
from_type CHARACTER(25), handle CHARACTER(25),
from_handle CHARACTER(25),
ref CHARACTER(25), ref CHARACTER(25),
role0 INTEGER, role0 INTEGER,
role1 TEXT, role1 TEXT,
private BOOLEAN);""") private BOOLEAN);""")
db.query("""CREATE TABLE person_ref ( db.query("""CREATE TABLE person_ref (
from_type CHARACTER(25),
from_handle CHARACTER(25),
handle CHARACTER(25), handle CHARACTER(25),
description TEXT, description TEXT,
private BOOLEAN);""") private BOOLEAN);""")
db.query("""CREATE TABLE source_ref ( db.query("""CREATE TABLE source_ref (
type CHARACTER(25),
handle CHARACTER(25), handle CHARACTER(25),
ref CHARACTER(25), ref CHARACTER(25),
confidence INTEGER, confidence INTEGER,
@ -239,8 +238,7 @@ def makeDB(db):
private BOOLEAN);""") private BOOLEAN);""")
db.query("""CREATE TABLE child_ref ( db.query("""CREATE TABLE child_ref (
from_type CHARACTER(25), handle CHARACTER(25),
from_handle CHARACTER(25),
ref CHARACTER(25), ref CHARACTER(25),
frel0 INTEGER, frel0 INTEGER,
frel1 CHARACTER(25), frel1 CHARACTER(25),
@ -249,11 +247,9 @@ def makeDB(db):
private BOOLEAN);""") private BOOLEAN);""")
db.query("""CREATE TABLE lds ( db.query("""CREATE TABLE lds (
from_type CHARACTER(25),
from_handle CHARACTER(25),
handle CHARACTER(25), handle CHARACTER(25),
type CHARACTER(25), type CHARACTER(25),
place TEXT, place CHARACTER(25),
famc CHARACTER(25), famc CHARACTER(25),
temple TEXT, temple TEXT,
status TEXT, status TEXT,
@ -262,12 +258,15 @@ def makeDB(db):
db.query("""CREATE TABLE media_ref ( db.query("""CREATE TABLE media_ref (
handle CHARACTER(25), handle CHARACTER(25),
ref CHARACTER(25), ref CHARACTER(25),
role TEXT, role0 INTEGER,
role1 TEXT,
private BOOLEAN);""") private BOOLEAN);""")
db.query("""CREATE TABLE address ( db.query("""CREATE TABLE address (
from_type CHARACTER(25), handle CHARACTER(25),
from_handle CHARACTER(25), private BOOLEAN);""")
db.query("""CREATE TABLE location (
handle CHARACTER(25), handle CHARACTER(25),
street TEXT, street TEXT,
city TEXT, city TEXT,
@ -276,22 +275,21 @@ def makeDB(db):
country TEXT, country TEXT,
postal TEXT, postal TEXT,
phone TEXT, phone TEXT,
parish TEXT, parish TEXT);""")
private BOOLEAN);""")
db.query("""CREATE TABLE attribute ( db.query("""CREATE TABLE attribute (
handle CHARACTER(25), handle CHARACTER(25),
from_type CHARACTER(25),
from_handle CHARACTER(25),
the_type0 INTEGER, the_type0 INTEGER,
the_type1 TEXT, the_type1 TEXT,
value TEXT, value TEXT,
private BOOLEAN);""") private BOOLEAN);""")
db.query("""CREATE TABLE url ( db.query("""CREATE TABLE url (
handle CHARACTER(25),
path TEXT, path TEXT,
desc TXT, desc TXT,
type CHARACTER(1), type0 INTEGER,
type1 TEXT,
private BOOLEAN); private BOOLEAN);
""") """)
@ -326,23 +324,31 @@ class Database(object):
self.cursor.close() self.cursor.close()
self.db.close() self.db.close()
def export_place(db, from_type, from_handle, place_handle): def export_location_list(db, from_type, from_handle, locations):
export_link(db, from_type, from_handle, "place", place_handle) for location in locations:
export_location(db, from_type, from_handle, location)
def export_url_list(db, from_type, handle, urls): def export_url_list(db, from_type, from_handle, urls):
for url in urls: for url in urls:
# (False, u'http://www.gramps-project.org/', u'loleach', (0, u'kaabgo')) # (False, u'http://www.gramps-project.org/', u'loleach', (0, u'kaabgo'))
private, path, desc, type = url (private, path, desc, type) = url
handle = create_id()
db.query("""insert INTO url ( db.query("""insert INTO url (
handle,
path, path,
desc, desc,
type, type0,
private) VALUES (?, ?, ?, ?); type1,
private) VALUES (?, ?, ?, ?, ?, ?);
""", """,
handle,
path, path,
desc, desc,
type[0], type[0],
type[1],
private) private)
# finally, link this to parent
export_link(db, from_type, from_handle, "url", handle)
def export_person_ref_list(db, from_type, from_handle, person_ref_list): def export_person_ref_list(db, from_type, from_handle, person_ref_list):
for person_ref in person_ref_list: for person_ref in person_ref_list:
@ -352,76 +358,53 @@ def export_person_ref_list(db, from_type, from_handle, person_ref_list):
handle, handle,
desc) = person_ref desc) = person_ref
db.query("""INSERT INTO person_ref ( db.query("""INSERT INTO person_ref (
from_type,
from_handle,
handle, handle,
description, description,
private) VALUES (?, ?, ?, ?, ?);""", private) VALUES (?, ?, ?);""",
from_type,
from_handle,
handle, handle,
desc, desc,
private private
) )
export_list(db, "person_ref", handle, "note", note_list) export_list(db, "person_ref", handle, "note", note_list)
export_source_list(db, "person_ref", handle, source_list) export_source_ref_list(db, "person_ref", handle, source_list)
# And finally, make a link from parent to new object
export_link(db, from_type, from_handle, "person_ref", handle)
def export_lds(db, from_type, from_handle, data):
def export_event_ref(db, from_type, from_handle, ref, role, private): (lsource_list, lnote_list, date, type, place,
db.query("""insert INTO event_ref ( famc, temple, status, private) = data
from_type, lds_handle = create_id()
from_handle, db.query("""INSERT into lds (handle, type, place, famc, temple, status, private)
ref, VALUES (?,?,?,?,?,?,?);""",
role0, lds_handle, type, place, famc, temple, status, private)
role1, export_link(db, "lds", lds_handle, "place", place)
private) VALUES (?, ?, ?,?,?,?);""", export_list(db, "lds", lds_handle, "note", lnote_list)
from_type, export_date(db, "lds", lds_handle, date)
from_handle, export_source_ref_list(db, "lds", lds_handle, lsource_list)
ref, # And finally, make a link from parent to new object
role[0], export_link(db, from_type, from_handle, "lds", lds_handle)
role[1],
private)
def export_markup(db, handle, markup_code0, markup_code1, value,
start_stop_list):
db.query("""INSERT INTO markup (
handle,
markup0,
markup1,
value,
start_stop_list) VALUES (?,?,?,?,?);""",
handle, markup_code0, markup_code1, value,
start_stop_list)
def export_lds(db, from_type, from_handle, handle, type, place, famc, temple, status, private):
db.query("""INSERT into lds (from_type, from_handle, handle, type, place, famc, temple, status, private)
VALUES (?,?,?,?,?,?,?,?,?);""",
from_type, from_handle, handle, type, place, famc, temple, status, private)
# FIXME: remove place from here?
def export_media_ref(db, handle, ref, role, private): def export_source_ref(db, from_type, from_handle, source):
db.query("""INSERT into media_ref ( (date, private, note_list, confidence, ref, page) = source
handle, handle = create_id()
ref, # handle is source_ref handle
role, # ref is source handle
private) VALUES (?,?,?,?);""",
handle, ref, str(role), private) # FIXME: role with two parts
def export_source_ref(db, from_type, handle, ref, private, confidence, page):
db.query("""INSERT into source_ref ( db.query("""INSERT into source_ref (
type,
handle, handle,
ref, ref,
confidence, confidence,
page, page,
private private
) VALUES (?, ?,?,?,?,?);""", ) VALUES (?,?,?,?,?);""",
from_type,
handle, handle,
ref, ref,
confidence, confidence,
page, page,
private) private)
export_date(db, "source_ref", handle, date)
export_list(db, "source_ref", handle, "note", note_list)
# And finally, make a link from parent to new object
export_link(db, from_type, from_handle, "source_ref", handle)
def export_source(db, handle, gid, title, author, pubinfo, abbrev, change, def export_source(db, handle, gid, title, author, pubinfo, abbrev, change,
marker0, marker1, private): marker0, marker1, private):
@ -448,9 +431,12 @@ def export_source(db, handle, gid, title, author, pubinfo, abbrev, change,
marker1, marker1,
private) private)
def export_note(db, handle, gid, text, format, note_type0, def export_note(db, data):
note_type1, change, marker0, marker1, private): (handle, gid, styled_text, format, note_type,
db.query("""INSERT into note (handle, change, marker, private) = data
text, markup_list = styled_text
db.query("""INSERT into note (
handle,
gid, gid,
text, text,
format, format,
@ -461,18 +447,193 @@ def export_note(db, handle, gid, text, format, note_type0,
marker1, marker1,
private) values (?, ?, ?, ?, ?, private) values (?, ?, ?, ?, ?,
?, ?, ?, ?, ?);""", ?, ?, ?, ?, ?);""",
handle, gid, text, format, note_type0, handle, gid, text, format, note_type[0],
note_type1, change, marker0, marker1, private) note_type[1], change, marker[0], marker[1], private)
for markup in markup_list:
markup_code, value, start_stop_list = markup
export_markup(db, "note", handle, markup_code[0], markup_code[1], value,
str(start_stop_list)) # Not normal form; use eval
def export_name(db, from_handle, handle, primary, data): def export_markup(db, from_type, from_handle, markup_code0, markup_code1, value,
start_stop_list):
markup_handle = create_id()
db.query("""INSERT INTO markup (
handle,
markup0,
markup1,
value,
start_stop_list) VALUES (?,?,?,?,?);""",
markup_handle, markup_code0, markup_code1, value,
start_stop_list)
# And finally, make a link from parent to new object
export_link(db, from_type, from_handle, "markup", markup_handle)
def export_event(db, data):
(handle, gid, the_type, date, description, place_handle,
source_list, note_list, media_list, attribute_list,
change, marker, private) = data
db.query("""INSERT INTO event (
handle,
gid,
the_type0,
the_type1,
description,
change,
marker0,
marker1,
private) VALUES (?,?,?,?,?,?,?,?,?);""",
handle,
gid,
the_type[0],
the_type[1],
description,
change,
marker[0],
marker[1],
private)
export_date(db, "event", handle, date)
export_link(db, "event", handle, "place", place_handle)
export_list(db, "event", handle, "note", note_list)
export_attribute_list(db, "event", handle, attribute_list)
export_media_ref_list(db, "event", handle, media_list)
export_source_ref_list(db, "event", handle, source_list)
def export_event_ref(db, from_type, from_handle, event_ref):
(private, note_list, attribute_list, ref, role) = event_ref
handle = create_id()
db.query("""insert INTO event_ref (
handle,
ref,
role0,
role1,
private) VALUES (?,?,?,?,?);""",
handle,
ref,
role[0],
role[1],
private)
export_list(db, "event_ref", handle, "note", note_list)
export_attribute_list(db, "event_ref", handle, attribute_list)
# finally, link this to parent
export_link(db, from_type, from_handle, "event_ref", handle)
def export_person(db, person):
(handle, # 0
gid, # 1
gender, # 2
primary_name, # 3
alternate_names, # 4
death_ref_index, # 5
birth_ref_index, # 6
event_ref_list, # 7
family_list, # 8
parent_family_list, # 9
media_list, # 10
address_list, # 11
attribute_list, # 12
urls, # 13
lds_ord_list, # 14
psource_list, # 15
pnote_list, # 16
change, # 17
marker, # 18
private, # 19
person_ref_list, # 20
) = person
db.query("""INSERT INTO person (
handle,
gid,
gender,
death_ref_handle,
birth_ref_handle,
change,
marker0,
marker1,
private) values (?, ?, ?, ?, ?, ?, ?, ?, ?);""",
handle,
gid,
gender,
lookup(death_ref_index, event_ref_list),
lookup(birth_ref_index, event_ref_list),
change,
marker[0],
marker[1],
private)
# Event Reference information
for event_ref in event_ref_list:
export_event_ref(db, "person", handle, event_ref)
export_list(db, "person", handle, "family", family_list)
export_list(db, "person", handle, "parent_family", parent_family_list)
export_media_ref_list(db, "person", handle, media_list)
export_list(db, "person", handle, "note", pnote_list)
export_attribute_list(db, "person", handle, attribute_list)
export_url_list(db, "person", handle, urls)
export_person_ref_list(db, "person", handle, person_ref_list)
export_source_ref_list(db, "person", handle, psource_list)
# -------------------------------------
# Address
# -------------------------------------
for address in address_list:
export_address(db, "person", handle, address)
# -------------------------------------
# LDS ord
# -------------------------------------
for ldsord in lds_ord_list:
export_lds(db, "person", handle, ldsord)
# -------------------------------------
# Names
# -------------------------------------
export_name(db, "person", handle, True, primary_name)
map(lambda name: export_name(db, "person", handle, False, name),
alternate_names)
def export_date(db, from_type, from_handle, data):
if data is None: return
(calendar, modifier, quality, dateval, text, sortval, newyear) = data
if len(dateval) == 4:
day1, month1, year1, slash1 = dateval
day2, month2, year2, slash2 = 0, 0, 0, 0
elif len(dateval) == 8:
day1, month1, year1, slash1, day2, month2, year2, slash2 = dateval
else:
raise ("ERROR: date dateval format", dateval)
date_handle = create_id()
db.query("""INSERT INTO date (
handle,
calendar,
modifier,
quality,
day1,
month1,
year1,
slash1,
day2,
month2,
year2,
slash2,
text,
sortval,
newyear) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?);""",
date_handle, calendar, modifier, quality,
day1, month1, year1, slash1,
day2, month2, year2, slash2,
text, sortval, newyear)
# And finally, make a link from parent to new object
export_link(db, from_type, from_handle, "date", date_handle)
def export_name(db, from_type, from_handle, primary, data):
if data: if data:
(private, source_list, note_list, date, (private, source_list, note_list, date,
first_name, surname, suffix, title, first_name, surname, suffix, title,
name_type, prefix, patronymic, name_type, prefix, patronymic,
group_as, sort_as, display_as, call) = data group_as, sort_as, display_as, call) = data
handle = create_id()
db.query("""INSERT into name ( db.query("""INSERT into name (
from_handle,
handle, handle,
primary_name, primary_name,
private, private,
@ -488,98 +649,78 @@ def export_name(db, from_handle, handle, primary, data):
sort_as, sort_as,
display_as, display_as,
call call
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ) values (?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?);""", ?, ?, ?, ?, ?, ?, ?);""",
from_handle, handle, primary, private, first_name, surname, suffix, title, handle, primary, private, first_name, surname, suffix, title,
name_type[0], name_type[1], prefix, patronymic, group_as, name_type[0], name_type[1], prefix, patronymic, group_as,
sort_as, display_as, call) sort_as, display_as, call)
export_date(db, "name", handle, date)
if date:
export_date(db, "name", handle, date)
export_list(db, "name", handle, "note", note_list) export_list(db, "name", handle, "note", note_list)
export_source_list(db, "name", handle, source_list) export_source_ref_list(db, "name", handle, source_list)
# And finally, make a link from parent to new object
export_link(db, from_type, from_handle, "name", handle)
def export_date(db, date_type, handle, date): def export_attribute(db, from_type, from_handle, attribute):
if True: (private, source_list, note_list, the_type, value) = attribute
(calendar, modifier, quality, dateval, text, sortval, newyear) = date handle = create_id()
if len(dateval) == 4:
day1, month1, year1, slash1 = dateval
day2, month2, year2, slash2 = 0, 0, 0, 0
elif len(dateval) == 8:
day1, month1, year1, slash1, day2, month2, year2, slash2 = dateval
else:
raise ("ERROR:", dateval)
db.query("""INSERT INTO date (
from_type,
from_handle,
calendar,
modifier,
quality,
day1,
month1,
year1,
slash1,
day2,
month2,
year2,
slash2,
text,
sortval,
newyear) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?);""",
date_type, handle, calendar, modifier, quality,
day1, month1, year1, slash1,
day2, month2, year2, slash2,
text, sortval, newyear)
def export_attribute(db, from_type, from_handle, attr_handle, the_type, value, private):
db.query("""INSERT INTO attribute ( db.query("""INSERT INTO attribute (
handle, handle,
from_type,
from_handle,
the_type0, the_type0,
the_type1, the_type1,
value, value,
private) VALUES (?,?,?,?,?,?,?);""", private) VALUES (?,?,?,?,?);""",
attr_handle, from_type, from_handle, the_type[0], the_type[1], value, private) handle, the_type[0], the_type[1], value, private)
export_source_ref_list(db, "attribute", handle, source_list)
export_list(db, "attribute", handle, "note", note_list)
# finally, link the parent to the address
export_link(db, from_type, from_handle, "attribute", handle)
def export_source_list(db, from_type, handle, source_list): def export_source_ref_list(db, from_type, from_handle, source_list):
# Event Sources
for source in source_list: for source in source_list:
(date, private, note_list, confidence, ref, page) = source export_source_ref(db, from_type, from_handle, source)
export_source_ref(db, from_type, handle, ref, private, confidence, page)
if date:
export_date(db, "source", ref, date)
export_list(db, "source", ref, "note", note_list)
def export_media_list(db, from_type, from_handle, media_list): def export_media_ref_list(db, from_type, from_handle, media_list):
# Media list
for media in media_list: for media in media_list:
(private, source_list, note_list,attribute_list,ref,role) = media export_media_ref(db, from_type, from_handle, media)
export_media_ref(db, from_handle, ref, role, private)
export_list(db, "media", ref, "note", note_list)
export_attribute_list(db, "media", ref, attribute_list)
def export_attribute_list(db, from_type, from_handle, attribute_list): def export_media_ref(db, from_type, from_handle, media):
for attribute in attribute_list: (private, source_list, note_list, attribute_list, ref, role) = media
(private, source_list, note_list, the_type, value) = attribute # handle is the media_ref handle
attr_handle = create_id() # ref is the media handle
export_attribute(db, from_type, from_handle, attr_handle, the_type, value, private) handle = create_id()
export_list(db, "attribute", attr_handle, "note", note_list) if role is None:
export_source_list(db, "atribute", attr_handle, source_list) role = (-1, '')
db.query("""INSERT into media_ref (
handle,
ref,
role0,
role1,
private) VALUES (?,?,?,?,?);""",
handle, ref, role[0], role[1] ,private)
export_list(db, "media_ref", handle, "note", note_list)
export_attribute_list(db, "media_ref", handle, attribute_list)
# And finally, make a link from parent to new object
export_link(db, from_type, from_handle, "media_ref", handle)
def export_attribute_list(db, from_type, from_handle, attr_list):
for attribute in attr_list:
export_attribute(db, from_type, from_handle, attribute)
def export_child_ref_list(db, from_type, from_handle, to_type, ref_list): def export_child_ref_list(db, from_type, from_handle, to_type, ref_list):
for child_ref in ref_list: for child_ref in ref_list:
# family -> child_ref # family -> child_ref
# (False, [], [], u'b305e96e39652d8f08c', (1, u''), (1, u'')) # (False, [], [], u'b305e96e39652d8f08c', (1, u''), (1, u''))
(private, source_list, note_list, ref, frel, mrel) = child_ref (private, source_list, note_list, ref, frel, mrel) = child_ref
db.query("""INSERT INTO child_ref (from_type, from_handle, handle = create_id()
db.query("""INSERT INTO child_ref (handle,
ref, frel0, frel1, mrel0, mrel1, private) ref, frel0, frel1, mrel0, mrel1, private)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);""", VALUES (?, ?, ?, ?, ?, ?, ?);""",
from_type, from_handle, ref, frel[0], frel[1], handle, ref, frel[0], frel[1],
mrel[0], mrel[1], private) mrel[0], mrel[1], private)
export_source_list(db, from_type, ref, source_list) export_source_ref_list(db, from_type, handle, source_list)
export_list(db, from_type, ref, "note", note_list) export_list(db, from_type, handle, "note", note_list)
# And finally, make a link from parent to new object
export_link(db, from_type, from_handle, "child_ref", handle)
def export_list(db, from_type, from_handle, to_type, handle_list): def export_list(db, from_type, from_handle, to_type, handle_list):
for to_handle in handle_list: for to_handle in handle_list:
@ -593,11 +734,30 @@ 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, handle, street, city, county,
state, country, postal, phone, private, parish=None): def export_address(db, from_type, from_handle, address):
(private, asource_list, anote_list, date, location) = address
addr_handle = create_id()
db.query("""INSERT INTO address ( db.query("""INSERT INTO address (
from_type, handle,
from_handle, private) VALUES (?, ?);""", addr_handle, private)
export_location(db, "address", addr_handle, location)
export_date(db, "address", addr_handle, date)
export_list(db, "address", addr_handle, "note", anote_list)
export_source_ref_list(db, "address", addr_handle, asource_list)
# finally, link the parent to the address
export_link(db, from_type, from_handle, "address", addr_handle)
def export_location(db, from_type, from_handle, location):
if len(location) == 7:
(street, city, county, state, country, postal, phone) = location
parish = None
elif len(location) == 2:
((street, city, county, state, country, postal, phone), parish) = location
else:
print "ERROR: what kind of location is this?", location
handle = create_id()
db.query("""INSERT INTO location (
handle, handle,
street, street,
city, city,
@ -606,10 +766,10 @@ def export_address(db, from_type, from_handle, handle, street, city, county,
country, country,
postal, postal,
phone, phone,
parish, parish) VALUES (?,?,?,?,?,?,?,?,?);""",
private) VALUES (?, ?, ?,?,?,?,?,?,?,?,?,?);""", handle, street, city, county, state, country, postal, phone, parish)
from_type, from_handle, handle, street, city, county, state, country, postal, phone, parish, private) # finally, link the parent to the address
export_link(db, from_type, from_handle, "location", handle)
def exportData(database, filename, option_box=None, callback=None): def exportData(database, filename, option_box=None, callback=None):
if not callable(callback): if not callable(callback):
@ -632,16 +792,8 @@ def exportData(database, filename, option_box=None, callback=None):
# Notes # Notes
# --------------------------------- # ---------------------------------
for note_handle in database.note_map.keys(): for note_handle in database.note_map.keys():
note = database.note_map[note_handle] data = database.note_map[note_handle]
(handle, gid, styled_text, format, note_type, export_note(db, data)
change, marker, private) = note
text, markup_list = styled_text
export_note(db, handle, gid, text, format, note_type[0],
note_type[1], change, marker[0], marker[1], private)
for markup in markup_list:
markup_code, value, start_stop_list = markup
export_markup(db, handle, markup_code[0], markup_code[1], value,
str(start_stop_list)) # Not normal form; use eval
count += 1 count += 1
callback(100 * count/total) callback(100 * count/total)
@ -649,39 +801,8 @@ def exportData(database, filename, option_box=None, callback=None):
# Event # Event
# --------------------------------- # ---------------------------------
for event_handle in database.event_map.keys(): for event_handle in database.event_map.keys():
event = database.event_map[event_handle] data = database.event_map[event_handle]
(handle, gid, the_type, date, description, place, export_event(db, data)
source_list, note_list, media_list, attribute_list,
change, marker, private) = event
if date:
export_date(db, "event", event_handle, date)
db.query("""INSERT INTO event (
handle,
gid,
the_type0,
the_type1,
description,
change,
marker0,
marker1,
private) VALUES (?,?,?,?,?,?,?,?,?);""",
handle,
gid,
the_type[0],
the_type[1],
description,
change,
marker[0],
marker[1],
private)
if place:
export_place(db, "event", handle, place)
export_list(db, "event", handle, "note", note_list)
export_attribute_list(db, "event", handle, attribute_list)
export_media_list(db, "event", handle, media_list)
export_source_list(db, "event", handle, source_list)
count += 1 count += 1
callback(100 * count/total) callback(100 * count/total)
@ -690,96 +811,7 @@ def exportData(database, filename, option_box=None, callback=None):
# --------------------------------- # ---------------------------------
for person_handle in database.person_map.keys(): for person_handle in database.person_map.keys():
person = database.person_map[person_handle] person = database.person_map[person_handle]
(handle, # 0 export_person(db, person)
gid, # 1
gender, # 2
primary_name, # 3
alternate_names, # 4
death_ref_index, # 5
birth_ref_index, # 6
event_ref_list, # 7
family_list, # 8
parent_family_list, # 9
media_list, # 10
address_list, # 11
attribute_list, # 12
urls, # 13
lds_ord_list, # 14
psource_list, # 15
pnote_list, # 16
change, # 17
marker, # 18
private, # 19
person_ref_list, # 20
) = person
db.query("""INSERT INTO person (
handle,
gid,
gender,
death_ref_handle,
birth_ref_handle,
change,
marker0,
marker1,
private) values (?, ?, ?, ?, ?, ?, ?, ?, ?);""",
handle,
gid,
gender,
lookup(death_ref_index, event_ref_list),
lookup(birth_ref_index, event_ref_list),
change,
marker[0],
marker[1],
private)
# Event Reference information
for event_ref in event_ref_list:
(private, note_list, attribute_list, ref, role) = event_ref
export_event_ref(db, "person", handle, ref, role, private)
export_list(db, "event_ref", ref, "note", note_list)
export_attribute_list(db, "event_ref", ref, attribute_list)
export_list(db, "person", handle, "family", family_list)
export_list(db, "person", handle, "parent_family", parent_family_list)
export_media_list(db, "person", handle, media_list)
export_list(db, "person", handle, "note", pnote_list)
export_attribute_list(db, "person", handle, attribute_list)
export_url_list(db, "person", handle, urls)
export_person_ref_list(db, "person", handle, person_ref_list)
export_source_list(db, "person", handle, psource_list)
# -------------------------------------
# Address
# -------------------------------------
for address in address_list:
(private, asource_list, anote_list, date, location) = address
(street, city, county, state, country, postal, phone) = location
addr_handle = create_id()
export_address(db, "person", handle, addr_handle, street, city, county, state, country, postal, phone, private)
if date:
export_date(db, "address", addr_handle, date)
export_list(db, "address", addr_handle, "note", anote_list)
export_source_list(db, "address", addr_handle, source_list)
# -------------------------------------
# LDS ord
# -------------------------------------
for ldsord in lds_ord_list:
(lsource_list, lnote_list, date, type, place,
famc, temple, status, lprivate) = ldsord
lds_handle = create_id()
# FIXME: place?
export_lds(db, "person", handle, lds_handle, type, place, famc, temple, status, lprivate)
if date:
export_date(db, "lds", lds_handle, date)
export_list(db, "lds", lds_handle, "note", lnote_list)
export_source_list(db, "lds", lds_handle, lsource_list)
# -------------------------------------
# Names
# -------------------------------------
export_name(db, handle, create_id(), True, primary_name)
map(lambda name: export_name(db, handle, create_id(), False, name),
alternate_names)
count += 1 count += 1
callback(100 * count/total) callback(100 * count/total)
@ -809,31 +841,20 @@ def exportData(database, filename, option_box=None, callback=None):
private) private)
export_child_ref_list(db, "family", handle, "child_ref", child_ref_list) export_child_ref_list(db, "family", handle, "child_ref", child_ref_list)
export_list(db, "family", handle, "note", pnote_list) export_list(db, "family", handle, "note", note_list)
export_attribute_list(db, "family", handle, attribute_list) export_attribute_list(db, "family", handle, attribute_list)
export_source_list(db, "family", handle, source_list) export_source_ref_list(db, "family", handle, source_list)
export_media_list(db, "family", handle, media_list) export_media_ref_list(db, "family", handle, media_list)
# Event Reference information # Event Reference information
for event_ref in event_ref_list: for event_ref in event_ref_list:
(private, note_list, attribute_list, ref, role) = event_ref export_event_ref(db, "family", handle, event_ref)
export_event_ref(db, "family", handle, ref, role, private)
export_list(db, "event_ref", ref, "note", note_list)
export_attribute_list(db, "event_ref", ref, attribute_list)
# ------------------------------------- # -------------------------------------
# LDS # LDS
# ------------------------------------- # -------------------------------------
for ldsord in lds_seal_list: for ldsord in lds_seal_list:
(lsource_list, lnote_list, date, type, place, export_lds(db, "family", handle, ldsord)
famc, temple, status, lprivate) = ldsord
lds_handle = create_id()
# FIXME: place?
export_lds(db, "family", handle, lds_handle, type, place, famc, temple, status, lprivate)
if date:
export_date(db, "lds", lds_handle, date)
export_list(db, "lds", lds_handle, "note", lnote_list)
export_source_list(db, "lds", lds_handle, lsource_list)
count += 1 count += 1
callback(100 * count/total) callback(100 * count/total)
@ -859,24 +880,17 @@ def exportData(database, filename, option_box=None, callback=None):
handle, gid, the_type[0], the_type[1], handle, gid, the_type[0], the_type[1],
name, change, marker[0], marker[1], private) name, change, marker[0], marker[1], private)
export_list(db, "lds", lds_handle, "note", lnote_list) export_list(db, "repository", handle, "note", note_list)
export_url_list(db, "repository", handle, urls)
for address in address_list: for address in address_list:
(private, asource_list, anote_list, date, location) = address export_address(db, "repository", handle, address)
(street, city, county, state, country, postal, phone) = location
addr_handle = create_id()
export_address(db, "repository", handle, addr_handle, street, city, county, state,
country, postal, phone, private)
if date:
export_date(db, "address", addr_handle, date)
export_list(db, "address", addr_handle, "note", anote_list)
export_source_list(db, "address", addr_handle, asource_list)
count += 1 count += 1
callback(100 * count/total) callback(100 * count/total)
# --------------------------------- # ---------------------------------
# Place # Place
# --------------------------------- # ---------------------------------
for place_handle in database.place_map.keys(): for place_handle in database.place_map.keys():
place = database.place_map[place_handle] place = database.place_map[place_handle]
@ -902,22 +916,16 @@ def exportData(database, filename, option_box=None, callback=None):
change, marker[0], marker[1], private) change, marker[0], marker[1], private)
export_url_list(db, "place", handle, urls) export_url_list(db, "place", handle, urls)
export_media_list(db, "place", handle, media_list) export_media_ref_list(db, "place", handle, media_list)
export_source_list(db, "place", handle, source_list) export_source_ref_list(db, "place", handle, source_list)
export_list(db, "place", handle, "note", note_list) export_list(db, "place", handle, "note", note_list)
# FIX: losing link to places? # Main Location with parish:
for location in alt_location_list: # No need; we have the handle, but ok:
((street, city, county, state, country, postal, phone), parish) = location export_location(db, "place_main", handle, main_loc)
addr_handle = create_id() # But we need to link these:
export_address(db, "place", handle, addr_handle, street, city, county, state, export_location_list(db, "place_alt", handle, alt_location_list)
country, postal, phone, private, parish)
(street, city, county, state, country, postal, phone,
private, parish) = main_loc
export_address(db, "place", handle, create_id(), street, city,
county, state, country, postal, phone, private,
parish)
count += 1 count += 1
callback(100 * count/total) callback(100 * count/total)
@ -938,7 +946,7 @@ def exportData(database, filename, option_box=None, callback=None):
export_source(db, handle, gid, title, author, pubinfo, abbrev, change, export_source(db, handle, gid, title, author, pubinfo, abbrev, change,
marker[0], marker[1], private) marker[0], marker[1], private)
export_list(db, "source", handle, "note", note_list) export_list(db, "source", handle, "note", note_list)
export_media_list(db, "source", handle, media_list) export_media_ref_list(db, "source", handle, media_list)
# FIXME: reporef_list, datamap # FIXME: reporef_list, datamap
#print "FIXME: reporef_list", reporef_list #print "FIXME: reporef_list", reporef_list
#print "FIXME: datamap", datamap #print "FIXME: datamap", datamap
@ -976,10 +984,9 @@ def exportData(database, filename, option_box=None, callback=None):
private) VALUES (?,?,?,?,?,?,?,?,?);""", private) VALUES (?,?,?,?,?,?,?,?,?);""",
handle, gid, path, mime, desc, handle, gid, path, mime, desc,
change, marker[0], marker[1], private) change, marker[0], marker[1], private)
if date: export_date(db, "media", handle, date)
export_date(db, "media", handle, date)
export_list(db, "media", handle, "note", note_list) export_list(db, "media", handle, "note", note_list)
export_source_list(db, "media", handle, source_list) export_source_ref_list(db, "media", handle, source_list)
export_attribute_list(db, "media", handle, attribute_list) export_attribute_list(db, "media", handle, attribute_list)
count += 1 count += 1
callback(100 * count/total) callback(100 * count/total)

View File

@ -54,6 +54,9 @@ from gui.utils import ProgressMeter
from Utils import create_id from Utils import create_id
def lookup(handle, event_ref_list): def lookup(handle, event_ref_list):
"""
Find the handle in a unserialized event_ref_list and return code.
"""
if handle is None: if handle is None:
return -1 return -1
else: else:
@ -125,37 +128,44 @@ class SQLReader(object):
# ----------------------------------------------- # -----------------------------------------------
def get_address_list(self, sql, from_type, from_handle): def get_address_list(self, sql, from_type, from_handle):
# FIXME results = self.get_links(sql, from_type, from_handle, "address")
return [] retval = []
for handle in results:
result = sql.query("select * from address where handle = ?;",
handle)
retval.append(self.pack_address(sql, result[0]))
return retval
def get_attribute_list(self, sql, from_type, from_handle): def get_attribute_list(self, sql, from_type, from_handle):
rows = sql.query("select * from attribute where from_type = ? and from_handle = ?;", handles = self.get_links(sql, from_type, from_handle, "attribute")
from_type, from_handle)
retval = [] retval = []
for row in rows: for handle in handles:
(handle, rows = sql.query("select * from attribute where handle = ?;",
from_type, handle)
from_handle, for row in rows:
the_type0, (handle,
the_type1, the_type0,
value, the_type1,
private) = row value,
source_list = self.get_source_list(sql, "attribute", handle) private) = row
note_list = self.get_note_list(sql, "attribute", handle) source_list = self.get_source_ref_list(sql, "attribute", handle)
retval.append((private, source_list, note_list, note_list = self.get_note_list(sql, "attribute", handle)
(the_type0, the_type1), value)) retval.append((private, source_list, note_list,
(the_type0, the_type1), value))
return retval return retval
def get_child_ref_list(self, sql, from_type, from_handle): def get_child_ref_list(self, sql, from_type, from_handle):
rows = sql.query("select * from child_ref where from_type = ? and from_handle = ?;", results = self.get_links(sql, from_type, from_handle, "child_ref")
from_type, from_handle)
retval = [] retval = []
for row in rows: for handle in results:
(rtype, rhandle, ref, frel0, frel1, mrel0, mrel1, private) = row rows = sql.query("select * from child_ref where handle = ?;",
source_list = self.get_source_list(sql, "child_ref", rhandle) handle)
note_list = self.get_note_list(sql, "child_ref", rhandle) for row in rows:
retval.append((private, source_list, note_list, ref, (handle, ref, frel0, frel1, mrel0, mrel1, private) = row
(frel0, frel1), (mrel0, mrel1))) source_list = self.get_source_ref_list(sql, "child_ref", handle)
note_list = self.get_note_list(sql, "child_ref", handle)
retval.append((private, source_list, note_list, ref,
(frel0, frel1), (mrel0, mrel1)))
return retval return retval
def get_datamap(self, sql, from_type, from_handle): def get_datamap(self, sql, from_type, from_handle):
@ -163,79 +173,169 @@ class SQLReader(object):
return {} return {}
def get_event_ref_list(self, sql, from_type, from_handle): def get_event_ref_list(self, sql, from_type, from_handle):
results = sql.query("select * from event_ref where from_type = ? and from_handle = ?;", results = self.get_links(sql, from_type, from_handle, "event_ref")
from_type, retval = []
from_handle) for handle in results:
return [self.pack_event_ref(sql, result) for result in results] result = sql.query("select * from event_ref where handle = ?;",
handle)
retval.append(self.pack_event_ref(sql, result[0]))
return retval
def get_family_list(self, sql, from_type, from_handle): def get_family_list(self, sql, from_type, from_handle):
return self.get_list(sql, from_type, from_handle, "family") return self.get_links(sql, from_type, from_handle, "family")
def get_parent_family_list(self, sql, from_type, from_handle): def get_parent_family_list(self, sql, from_type, from_handle):
return self.get_list(sql, from_type, from_handle, "parent_family") return self.get_links(sql, from_type, from_handle, "parent_family")
def get_person_ref_list(self, sql, from_type, from_handle): def get_person_ref_list(self, sql, from_type, from_handle):
rows = sql.query("select * from person_ref where from_type = ? and from_handle = ?;", handles = self.get_links(sql, from_type, from_handle, "person_ref")
from_type, from_handle)
retval = [] retval = []
for row in rows: for ref_handle in handles:
(from_type, rows = sql.query("select * from person_ref where handle = ?;",
from_handle, ref_handle)
handle, for row in rows:
description, (handle,
private) = row description,
source_list = self.get_source_list(sql, "person_ref", handle) private) = row
note_list = self.get_note_list(sql, "person_ref", handle) source_list = self.get_source_ref_list(sql, "person_ref", handle)
retval.append((private, note_list = self.get_note_list(sql, "person_ref", handle)
source_list, retval.append((private,
note_list, source_list,
handle, note_list,
description)) handle,
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):
# FIXME handles = self.get_links(sql, from_type, from_handle, "location")
return [] results = []
for handle in handles:
results += sql.query("""select * from location where handle = ?;""",
handle)
return [self.pack_location(sql, result, with_parish=True) for result in results]
def get_lds_list(self, sql, from_type, from_handle): def get_lds_list(self, sql, from_type, from_handle):
# FIXME handles = self.get_links(sql, from_type, from_handle, "lds")
return [] results = []
for handle in handles:
results += sql.query("""select * from lds where handle = ?;""",
handle)
return [self.pack_lds(sql, result) for result in results]
def get_media_list(self, sql, from_type, from_handle): def get_media_list(self, sql, from_type, from_handle):
# FIXME handles = self.get_links(sql, from_type, from_handle, "media_ref")
return [] results = []
for handle in handles:
results += sql.query("""select * from media_ref where handle = ?;""",
handle)
return [self.pack_media_ref(sql, result) for result in results]
def get_note_list(self, sql, from_type, from_handle): def get_note_list(self, sql, from_type, from_handle):
return self.get_list(sql, from_type, from_handle, "note") return self.get_links(sql, from_type, from_handle, "note")
def get_repository_ref_list(self, sql, from_type, from_handle): def get_repository_list(self, sql, from_type, from_handle):
# FIXME handles = self.get_links(sql, from_type, from_handle, "repository")
return [] results = []
for handle in handles:
results += sql.query("""select * from repository where handle = ?;""",
handle)
return [self.pack_repository(sql, result) for result in results]
def get_source_list(self, sql, from_type, from_handle): def get_source_ref_list(self, sql, from_type, from_handle):
results = sql.query("""select * from source where from_type = ? and handle = ?;""", handles = self.get_links(sql, from_type, from_handle, "source_ref")
from_type, from_handle) results = []
return [self.pack_source(sql, result) for result in results] for handle in handles:
results += sql.query("""select * from source_ref where handle = ?;""",
handle)
return [self.pack_source_ref(sql, result) for result in results]
def get_url_list(self, sql, from_type, from_handle): def get_url_list(self, sql, from_type, from_handle):
# FIXME handles = self.get_links(sql, from_type, from_handle, "url")
return [] results = []
for handle in handles:
results += sql.query("""select * from url where handle = ?;""",
handle)
return [self.pack_url(sql, result) for result in results]
# --------------------------------- # ---------------------------------
# Helpers # Helpers
# --------------------------------- # ---------------------------------
def pack_event_ref(self, sql, data): def pack_address(self, sql, data):
(from_type, (handle, private) = data
from_handle, source_list = self.get_source_ref_list(sql, "address", handle)
date_handle = self.get_link(sql, "address", handle, "date")
date = self.get_date(sql, date_handle)
note_list = self.get_note_list(sql, "address", handle)
location = self.get_location(sql, "address", handle)
return (private, source_list, note_list, date, location)
def pack_lds(self, sql, data):
(handle, type, place, famc, temple, status, private) = data
source_list = self.get_source_ref_list(sql, "lds", handle)
note_list = self.get_note_list(sql, "lds", handle)
date_handle = self.get_link(sql, "lds", handle, "date")
date = self.get_date(sql, date_handle)
return (source_list, note_list, date, type, place,
famc, temple, status, private)
def pack_media_ref(self, sql, data):
(handle,
ref, ref,
role0, role0,
role1, role1,
private) = data private) = data
note_list = self.get_note_list(sql, "event_ref", from_handle) source_list = self.get_source_ref_list(sql, "media_ref", handle)
attribute_list = self.get_attribute_list(sql, "event_ref", from_handle) note_list = self.get_note_list(sql, "media_ref", handle)
attribute_list = self.get_attribute_list(sql, "event_ref", handle)
return (private, source_list, note_list, attribute_list, ref, (role0, role1))
def pack_repository(self, sql, data):
(handle,
gid,
the_type0,
the_type1,
name,
change,
marker0,
marker1,
private) = data
note_list = self.get_note_list(sql, "repository", handle)
address_list = self.get_address_list(sql, "repository", handle)
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)
def pack_url(self, sql, data):
(handle,
path,
desc,
type0,
type1,
private) = data
return (private, path, desc, (type0, type1))
def pack_event_ref(self, sql, data):
(handle,
ref,
role0,
role1,
private) = data
note_list = self.get_note_list(sql, "event_ref", handle)
attribute_list = self.get_attribute_list(sql, "event_ref", handle)
return (private, note_list, attribute_list, ref, (role0, role1)) return (private, note_list, attribute_list, ref, (role0, role1))
def pack_source_ref(self, sql, data):
(handle,
ref,
confidence,
page,
private) = data
date_handle = self.get_link(sql, "source_ref", handle, "date")
date = self.get_date(sql, date_handle)
note_list = self.get_note_list(sql, "source_ref", handle)
return (date, private, note_list, confidence, ref, page)
def pack_source(self, sql, data): def pack_source(self, sql, data):
(handle, (handle,
gid, gid,
@ -247,12 +347,10 @@ class SQLReader(object):
marker0, marker0,
marker1, marker1,
private) = data private) = data
note_list = self.get_note_list(sql, "source", handle) note_list = self.get_note_list(sql, "source", handle)
media_list = self.get_media_list(sql, "source", handle) media_list = self.get_media_list(sql, "source", handle)
reporef_list = self.get_repository_ref_list(sql, "source", handle) reporef_list = self.get_repository_list(sql, "source", handle)
datamap = None datamap = {}
return (handle, gid, title, return (handle, gid, title,
author, pubinfo, author, pubinfo,
note_list, note_list,
@ -262,29 +360,36 @@ class SQLReader(object):
reporef_list, reporef_list,
(marker0, marker1), private) (marker0, marker1), private)
def get_list(self, sql, from_type, from_handle, to_type): def get_location(self, sql, from_type, from_handle):
results = sql.query("""select to_handle from link where from_type = ? and from_handle = ? and to_type = ?;""", handle = self.get_link(sql, from_type, from_handle, "location")
from_type, from_handle, to_type) if handle:
return [str(result) for result in results] results = sql.query("""select * from location where handle = ?;""",
handle)
if len(results) == 1:
return self.pack_location(sql, results[0], with_parish=True)
def get_names(self, sql, handle, primary): def get_names(self, sql, from_type, from_handle, primary):
names = sql.query("""select * from name where from_handle = ? and primary_name = ?;""", handles = self.get_links(sql, from_type, from_handle, "name")
handle, primary) names = []
for handle in handles:
results = sql.query("""select * from name where handle = ? and primary_name = ?;""",
handle, primary)
if len(results) > 0:
names += results
result = [self.pack_name(sql, name) for name in names] result = [self.pack_name(sql, name) for name in names]
if primary: if primary:
if len(result) == 1: if len(result) == 1:
return result[0] return result[0]
elif len(result) == 0: elif len(result) == 0:
return None return gen.lib.Name().serialize()
else: else:
print Exception("too many primary names") raise Exception("too many primary names")
else: else:
return result return result
def pack_name(self, sql, data): def pack_name(self, sql, data):
# unpack name from SQL table: # unpack name from SQL table:
(from_handle, (handle,
handle,
primary_name, primary_name,
private, private,
first_name, first_name,
@ -300,47 +405,101 @@ class SQLReader(object):
display_as, display_as,
call) = data call) = data
source_list = self.get_source_list(sql, "name", from_handle) source_list = self.get_source_ref_list(sql, "name", handle)
note_list = self.get_note_list(sql, "name", from_handle) note_list = self.get_note_list(sql, "name", handle)
date = self.get_date(sql, "name", from_handle) date_handle = self.get_link(sql, "name", handle, "date")
date = self.get_date(sql, date_handle)
return (private, source_list, note_list, date, return (private, source_list, note_list, date,
first_name, surname, suffix, title, first_name, surname, suffix, title,
(name_type0, name_type1), prefix, patronymic, (name_type0, name_type1), prefix, patronymic,
group_as, sort_as, display_as, call) group_as, sort_as, display_as, call)
def get_place(self, sql, from_type, handle): def pack_location(self, sql, data, with_parish):
row = self.get_list(sql, from_type, handle, "place") (handle,
if len(row) == 1: street,
return row[0] city,
county,
state,
country,
postal,
phone,
parish) = data
if with_parish:
return ((street, city, county, state, country, postal, phone), parish)
else: else:
print AttributeError("invalid place '%s' '%s'" % (from_type, handle)) return (street, city, county, state, country, postal, phone)
def get_date(self, sql, from_type, from_handle): def get_place_from_handle(self, sql, ref_handle):
rows = sql.query("select * from date where from_type = ? and from_handle = ?;", if ref_handle:
from_type, from_handle) place_row = sql.query("select * from place where handle = ?;",
ref_handle)
if len(place_row) == 1:
# return raw DB data here:
return place_row[0]
elif len(place_row) == 0:
print "ERROR: get_place_from_handle('%s'), no such handle." % (ref_handle, )
else:
print "ERROR: get_place_from_handle('%s') should be unique; returned %d records." % (ref_handle, len(place_row))
return None
def get_location_from_handle(self, sql, ref_handle, with_parish=False):
if ref_handle:
place_row = sql.query("select * from location where handle = ?;",
ref_handle)
if len(place_row) == 1:
return self.pack_location(sql, place_row[0], with_parish)
elif len(place_row) == 0:
print "ERROR: get_location_from_handle('%s'), no such handle." % (ref_handle, )
else:
print "ERROR: get_location_from_handle('%s') should be unique; returned %d records." % (ref_handle, len(place_row))
return gen.lib.Location().serialize()
def get_link(self, sql, from_type, from_handle, to_link):
"""
Return a link, and return 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)
if len(rows) == 1: if len(rows) == 1:
(from_type, return rows[0]
from_handle, elif len(rows) > 1:
calendar, print "ERROR: too many links %s:%s -> %s (%d)" % (from_type, from_handle, to_link, len(rows))
modifier, return None
quality,
day1, def get_links(self, sql, from_type, from_handle, to_link):
month1, """
year1, Return a list of handles (possibly none).
slash1, """
day2, results = sql.query("""select to_handle from link where from_type = ? and from_handle = ? and to_type = ?;""",
month2, from_type, from_handle, to_link)
year2, return [result[0] for result in results]
slash2,
text, def get_date(self, sql, handle):
sortval, assert type(handle) in [unicode, str, type(None)], "handle is wrong type: %s" % handle
newyear) = rows[0] if handle:
dateval = day1, month1, year1, slash1, day2, month2, year2, slash2 rows = sql.query("select * from date where handle = ?;", handle)
return (calendar, modifier, quality, dateval, text, sortval, newyear) if len(rows) == 1:
elif len(rows) == 0: (handle,
return None calendar,
else: modifier,
print Exception("ERROR, wrong number of dates: %s" % rows) quality,
day1,
month1,
year1,
slash1,
day2,
month2,
year2,
slash2,
text,
sortval,
newyear) = rows[0]
dateval = day1, month1, year1, slash1, day2, month2, year2, slash2
return (calendar, modifier, quality, dateval, text, sortval, newyear)
elif len(rows) == 0:
return None
else:
print Exception("ERROR, wrong number of dates: %s" % rows)
def process(self): def process(self):
sql = self.openSQL() sql = self.openSQL()
@ -355,7 +514,7 @@ class SQLReader(object):
self.trans = self.db.transaction_begin("",batch=True) self.trans = self.db.transaction_begin("",batch=True)
self.db.disable_signals() self.db.disable_signals()
count = 0.0 count = 0.0
t = time.time() self.t = time.time()
# --------------------------------- # ---------------------------------
# Process note # Process note
# --------------------------------- # ---------------------------------
@ -372,6 +531,7 @@ class SQLReader(object):
marker1, marker1,
private) = note private) = note
styled_text = [text, []] styled_text = [text, []]
# direct connection with note handle
markups = sql.query("""select * from markup where handle = ?""", handle) markups = sql.query("""select * from markup where handle = ?""", handle)
for markup in markups: for markup in markups:
(mhandle, (mhandle,
@ -386,6 +546,42 @@ class SQLReader(object):
(marker0, marker1), private) (marker0, marker1), private)
count += 1 count += 1
self.callback(100 * count/total) self.callback(100 * count/total)
# ---------------------------------
# Process event
# ---------------------------------
events = sql.query("""select * from event;""")
for event in events:
(handle,
gid,
the_type0,
the_type1,
description,
change,
marker0,
marker1,
private) = event
note_list = self.get_note_list(sql, "event", handle)
source_list = self.get_source_ref_list(sql, "event", handle)
media_list = self.get_media_list(sql, "event", handle)
attribute_list = self.get_attribute_list(sql, "event", handle)
date_handle = self.get_link(sql, "event", handle, "date")
date = self.get_date(sql, date_handle)
place_handle = self.get_link(sql, "event", handle, "place")
place = self.get_place_from_handle(sql, place_handle)
data = (str(handle), gid, (the_type0, the_type1), date, description, place,
source_list, note_list, media_list, attribute_list,
change, (marker0, marker1), private)
self.db.event_map[str(handle)] = data
count += 1
self.callback(100 * count/total)
# --------------------------------- # ---------------------------------
# Process person # Process person
# --------------------------------- # ---------------------------------
@ -403,8 +599,8 @@ class SQLReader(object):
marker1, # 18 marker1, # 18
private, # 19 private, # 19
) = person ) = person
primary_name = self.get_names(sql, handle, True) # one primary_name = self.get_names(sql, "person", handle, True) # one
alternate_names = self.get_names(sql, handle, False) # list alternate_names = self.get_names(sql, "person", handle, False) # list
event_ref_list = self.get_event_ref_list(sql, "person", handle) event_ref_list = self.get_event_ref_list(sql, "person", handle)
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)
@ -413,7 +609,7 @@ class SQLReader(object):
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)
psource_list = self.get_source_list(sql, "person", handle) psource_list = self.get_source_ref_list(sql, "person", handle)
pnote_list = self.get_note_list(sql, "person", handle) pnote_list = self.get_note_list(sql, "person", handle)
person_ref_list = self.get_person_ref_list(sql, "person", handle) person_ref_list = self.get_person_ref_list(sql, "person", handle)
death_ref_index = lookup(death_ref_handle, event_ref_list) death_ref_index = lookup(death_ref_handle, event_ref_list)
@ -443,37 +639,6 @@ class SQLReader(object):
count += 1 count += 1
self.callback(100 * count/total) self.callback(100 * count/total)
# --------------------------------- # ---------------------------------
# Process event
# ---------------------------------
events = sql.query("""select * from event;""")
for event in events:
(handle,
gid,
the_type0,
the_type1,
description,
change,
marker0,
marker1,
private) = event
source_list = self.get_source_list(sql, "event", handle)
note_list = self.get_note_list(sql, "event", handle)
media_list = self.get_media_list(sql, "event", handle)
attribute_list = self.get_attribute_list(sql, "event", handle)
date = self.get_date(sql, "event", handle)
place = self.get_place(sql, "event", handle)
data = (str(handle), gid, (the_type0, the_type1), date, description, place,
source_list, note_list, media_list, attribute_list,
change, (marker0, marker1), private)
self.db.event_map[str(handle)] = data
count += 1
self.callback(100 * count/total)
# ---------------------------------
# Process family # Process family
# --------------------------------- # ---------------------------------
families = sql.query("""select * from family;""") families = sql.query("""select * from family;""")
@ -494,7 +659,7 @@ class SQLReader(object):
media_list = self.get_media_list(sql, "family", handle) media_list = self.get_media_list(sql, "family", handle)
attribute_list = self.get_attribute_list(sql, "family", handle) attribute_list = self.get_attribute_list(sql, "family", handle)
lds_seal_list = self.get_lds_list(sql, "family", handle) lds_seal_list = self.get_lds_list(sql, "family", handle)
source_list = self.get_source_list(sql, "family", handle) source_list = self.get_source_ref_list(sql, "family", handle)
note_list = self.get_note_list(sql, "family", handle) note_list = self.get_note_list(sql, "family", handle)
self.db.family_map[str(handle)] = (str(handle), gid, self.db.family_map[str(handle)] = (str(handle), gid,
@ -542,6 +707,7 @@ class SQLReader(object):
(handle, (handle,
gid, gid,
title, title,
main_loc,
long, long,
lat, lat,
change, change,
@ -549,11 +715,13 @@ class SQLReader(object):
marker1, marker1,
private) = place private) = place
main_loc = None # FIXME # We could look this up by "place_main", but we have the handle:
alt_location_list = self.get_location_list(sql, "place", handle) main_loc = self.get_location_from_handle(sql, main_loc,
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_list(sql, "place", handle) source_list = self.get_source_ref_list(sql, "place", handle)
note_list = self.get_note_list(sql, "place", handle) note_list = self.get_note_list(sql, "place", handle)
self.db.place_map[str(handle)] = (str(handle), gid, title, long, lat, self.db.place_map[str(handle)] = (str(handle), gid, title, long, lat,
@ -570,8 +738,7 @@ class SQLReader(object):
# --------------------------------- # ---------------------------------
sources = sql.query("""select * from source;""") sources = sql.query("""select * from source;""")
for source in sources: for source in sources:
(from_type, (handle,
handle,
gid, gid,
title, title,
author, author,
@ -584,7 +751,7 @@ class SQLReader(object):
note_list = self.get_note_list(sql, "source", handle) note_list = self.get_note_list(sql, "source", handle)
media_list = self.get_media_list(sql, "source", handle) media_list = self.get_media_list(sql, "source", handle)
datamap = self.get_datamap(sql, "source", handle) datamap = self.get_datamap(sql, "source", handle)
reporef_list = self.get_repository_ref_list(sql, "source", handle) reporef_list = self.get_repository_list(sql, "source", handle)
self.db.source_map[str(handle)] = (str(handle), gid, title, self.db.source_map[str(handle)] = (str(handle), gid, title,
author, pubinfo, author, pubinfo,
@ -612,10 +779,11 @@ class SQLReader(object):
private) = med private) = med
attribute_list = self.get_attribute_list(sql, "media", handle) attribute_list = self.get_attribute_list(sql, "media", handle)
source_list = self.get_source_list(sql, "media", handle) source_list = self.get_source_ref_list(sql, "media", handle)
note_list = self.get_note_list(sql, "media", handle) note_list = self.get_note_list(sql, "media", handle)
date = self.get_date(sql, "media", handle) date_handle = self.get_link(sql, "media", handle, "date")
date = self.get_date(sql, date_handle)
self.db.media_map[str(handle)] = (str(handle), gid, path, mime, desc, self.db.media_map[str(handle)] = (str(handle), gid, path, mime, desc,
attribute_list, attribute_list,
@ -627,18 +795,21 @@ class SQLReader(object):
private) private)
count += 1 count += 1
self.callback(100 * count/total) self.callback(100 * count/total)
return None
t = time.time() - t def cleanup(self):
msg = ngettext('Import Complete: %d second','Import Complete: %d seconds', t ) % t self.t = time.time() - self.t
msg = ngettext('Import Complete: %d second','Import Complete: %d seconds', self.t ) % self.t
self.db.transaction_commit(self.trans,_("SQL import")) self.db.transaction_commit(self.trans,_("SQL import"))
self.db.enable_signals() self.db.enable_signals()
self.db.request_rebuild() self.db.request_rebuild()
print msg print msg
return None
def importData(db, filename, callback=None): def importData(db, filename, callback=None):
g = SQLReader(db, filename, callback) g = SQLReader(db, filename, callback)
g.process() g.process()
g.cleanup()
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #