Skip to content

Commit

Permalink
Sqlite update for GEPS 045
Browse files Browse the repository at this point in the history
  • Loading branch information
prculley committed Dec 31, 2019
1 parent 50b3253 commit c23f658
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 57 deletions.
106 changes: 67 additions & 39 deletions Sqlite/ExportSql.py
Expand Up @@ -52,6 +52,7 @@
#
#------------------------------------------------------------------------
from gramps.gen.utils.id import create_id
from gramps.gen.lib import PlaceType
from gramps.gen.const import GRAMPS_LOCALE as glocale
from gramps.gui.plug.export import WriterOptionBox # don't remove, used!!!
try:
Expand Down Expand Up @@ -186,13 +187,8 @@ def makeDB(db, callback):
handle CHARACTER(25) PRIMARY KEY,
gid CHARACTER(25),
title TEXT,
value TEXT,
the_type0 INTEGER,
the_type1 TEXT,
code TEXT,
long TEXT,
lat TEXT,
lang TEXT,
change INTEGER,
private BOOLEAN);""")
count += 1
Expand All @@ -202,7 +198,9 @@ def makeDB(db, callback):
db.query("""CREATE TABLE place_ref (
handle CHARACTER(25) PRIMARY KEY,
from_place_handle CHARACTER(25),
to_place_handle CHARACTER(25));""")
to_place_handle CHARACTER(25),
h_type0 INTEGER,
h_type1 TEXT);""")
count += 1
callback(100 * count / total)

Expand All @@ -215,6 +213,25 @@ def makeDB(db, callback):
count += 1
callback(100 * count / total)

db.query("""drop table place_abbrev;""")
db.query("""CREATE TABLE place_abbrev (
handle CHARACTER(25) PRIMARY KEY,
from_handle CHARACTER(25),
value INTEGER,
abbr_type0 INTEGER,
abbr_type1 TEXT);""")
count += 1
callback(100 * count / total)

db.query("""drop table place_type;""")
db.query("""CREATE TABLE place_type (
handle CHARACTER(25) PRIMARY KEY,
from_handle CHARACTER(25),
value INTEGER,
t_str TEXT);""")
count += 1
callback(100 * count / total)

db.query("""drop table event;""")
db.query("""CREATE TABLE event (
handle CHARACTER(25) PRIMARY KEY,
Expand Down Expand Up @@ -470,18 +487,41 @@ def close(self):
self.db.close()


def export_alt_place_name_list(db, handle, alt_place_name_list):
for place_name in alt_place_name_list:
def export_place_name_list(db, handle, place_name_list):
for place_name in place_name_list:
export_place_name(db, handle, place_name)


def export_place_name(db, handle, place_name):
# alt_place_name_list = [('Ohio', None, ''), ...] [(value, date, lang)...]
(value, date, lang) = place_name
(value, date, lang, abbr_list, citation_list) = place_name
ref_handle = create_id()
db.query("insert into place_name (handle, from_handle, value, lang)"
" VALUES (?, ?, ?, ?);", ref_handle, handle, value, lang)
export_date(db, "place_name", ref_handle, date)
export_citation_list(db, "place_name", ref_handle, citation_list)
export_place_abbr_list(db, ref_handle, abbr_list)


def export_place_abbr_list(db, handle, place_abbr_list):
for place_abbr in place_abbr_list:
(value, a_type) = place_abbr
ref_handle = create_id()
db.query("insert into place_abbrev (handle, from_handle, value,"
" abbr_type0, abbr_type1)"
" VALUES (?, ?, ?, ?, ?);",
ref_handle, handle, value, a_type[0], a_type[1])


def export_place_type_list(db, handle, place_type_list):
for place_type in place_type_list:
(value, date, citation_list) = place_type
ref_handle = create_id()
db.query("insert into place_type (handle, from_handle, value, t_str)"
" VALUES (?, ?, ?, ?);",
ref_handle, handle, value, PlaceType(value).xml_str())
export_date(db, "place_type", ref_handle, date)
export_citation_list(db, "place_type", ref_handle, citation_list)


def export_place_ref_list(db, handle, place_ref_list):
Expand All @@ -492,12 +532,14 @@ def export_place_ref_list(db, handle, place_ref_list):


def export_place_ref(db, handle, place_ref):
(to_place_handle, date) = place_ref
(to_place_handle, date, citation_list, htype) = place_ref
ref_handle = create_id()
db.query("insert into place_ref"
" (handle, from_place_handle, to_place_handle) VALUES (?, ?, ?);",
ref_handle, handle, to_place_handle)
" (handle, from_place_handle, to_place_handle, h_type0, h_type1)"
" VALUES (?, ?, ?, ?, ?);",
ref_handle, handle, to_place_handle, htype[0], htype[1])
export_date(db, "place_ref", ref_handle, date)
export_citation_list(db, "place_ref", ref_handle, citation_list)


def export_location_list(db, from_type, from_handle, locations):
Expand Down Expand Up @@ -1159,56 +1201,42 @@ def exportData(database, filename, user, option_box):
continue
(handle, gid, title, long, lat,
place_ref_list,
place_name,
alt_place_name_list,
place_type,
code,
name_list,
type_list,
eventref_list,
alt_location_list,
urls,
media_list,
citation_list,
note_list,
change, tag_list, private) = place.serialize()

value, date, lang = place_name
change, tag_list, private,
attribute_list) = place.serialize()

db.query("""INSERT INTO place (
handle,
gid,
title,
value,
the_type0,
the_type1,
code,
long,
lat,
lang,
change,
private) values (?,?,?,?,?,?,?,?,?,?,?,?);""",
handle, gid, title, value,
place_type[0], place_type[1],
code,
long, lat,
lang,
change, private)
private) values (?,?,?,?,?,?,?);""",
handle, gid, title, long, lat, change, private)

export_date(db, "place", handle, date)
export_place_name_list(db, handle, name_list)
export_place_type_list(db, handle, type_list)
export_url_list(db, "place", handle, urls)
export_media_ref_list(db, "place", handle, media_list)
export_citation_list(db, "place", handle, citation_list)
export_list(db, "place", handle, "note", note_list)
export_list(db, "place", handle, "tag", tag_list)

#1. alt_place_name_list = [('Ohio', None, ''), ...]
# [(value, date, lang)...]
#2. place_ref_list = Enclosed by: [('4ECKQCWCLO5YIHXEXC', None)]
# [(handle, date)...]

export_alt_place_name_list(db, handle, alt_place_name_list)
export_place_ref_list(db, handle, place_ref_list)
export_attribute_list(db, "place", handle, attribute_list)

# But we need to link these:
export_location_list(db, "place_alt", handle, alt_location_list)
# Event Reference information
for event_ref in eventref_list:
export_event_ref(db, "place", handle, event_ref)

count += 1
callback(100 * count / total)
Expand Down
84 changes: 66 additions & 18 deletions Sqlite/ImportSql.py
Expand Up @@ -45,7 +45,8 @@
#-------------------------------------------------------------------------
from gramps.gen.lib import (Person, Family, Note, Media, Place, Citation,
Source, Tag, Event, Repository, Name, Location,
PlaceName)
PlaceType)
from gramps.gen.lib.placetype import DM_NAME
from gramps.gen.db import DbTxn
from gramps.gen.const import GRAMPS_LOCALE as glocale
try:
Expand Down Expand Up @@ -187,7 +188,7 @@ def get_child_ref_list(self, sql, from_type, from_handle):
(frel0, frel1), (mrel0, mrel1)))
return retval

def get_datamap_list(self, sql, from_type, from_handle):
def get_datamap_list(self, sql, _from_type, from_handle):
datamap = []
rows = sql.query("select * from datamap where from_handle = ?;",
from_handle)
Expand Down Expand Up @@ -312,7 +313,7 @@ def pack_lds(self, sql, data):
return (citation_list, note_list, date, type_, place,
famc, temple, status, bool(private))

def pack_surnames(self, sql, data):
def pack_surnames(self, _sql, data):
(_handle,
surname,
prefix,
Expand Down Expand Up @@ -355,7 +356,7 @@ def pack_repository_ref(self, sql, data):
(source_media_type0, source_media_type1),
bool(private))

def pack_url(self, sql, data):
def pack_url(self, _sql, data):
(_handle,
path,
desc,
Expand Down Expand Up @@ -451,7 +452,7 @@ def pack_name(self, sql, data):
(name_type0, name_type1),
group_as, sort_as, display_as, call, nick, famnick)

def pack_location(self, sql, data, with_parish):
def pack_location(self, _sql, data, with_parish):
(_handle, street, locality, city, county, state, country, postal,
phone, parish) = data
if with_parish:
Expand All @@ -476,15 +477,27 @@ def get_place_from_handle(self, sql, ref_handle):
" returned %d records." % (ref_handle, len(place_row)))
return ''

def get_alt_place_name_list(self, sql, handle):
def get_place_name_list(self, sql, handle):
place_name_list = sql.query(
"""select * from place_name where from_handle = ?;""", handle)
retval = []
for place_name_data in place_name_list:
ref_handle, handle, value, lang = place_name_data
date_handle = self.get_link(sql, "place_name", ref_handle, "date")
date = self.get_date(sql, date_handle)
retval.append((value, date, lang))
abbr_list = self.get_place_abbr_list(sql, ref_handle)
citation_list = self.get_citation_list(sql, "place_name",
ref_handle)
retval.append((value, date, lang, abbr_list, citation_list))
return retval

def get_place_abbr_list(self, sql, handle):
place_abbr_list = sql.query(
"""select * from place_abbrev where from_handle = ?;""", handle)
retval = []
for place_abbr_data in place_abbr_list:
_r_handle, handle, value, abbr_type0, abbr_type1 = place_abbr_data
retval.append((value, (abbr_type0, abbr_type1)))
return retval

def get_place_ref_list(self, sql, handle):
Expand All @@ -493,11 +506,45 @@ def get_place_ref_list(self, sql, handle):
place_ref_list = sql.query(
"""select * from place_ref where from_place_handle = ?;""", handle)
retval = []
for place_ref_data in place_ref_list:
ref_handle, handle, to_place_handle = place_ref_data
for ref_data in place_ref_list:
ref_handle, handle, to_place_handle, h_type0, h_type1 = ref_data
date_handle = self.get_link(sql, "place_ref", ref_handle, "date")
date = self.get_date(sql, date_handle)
retval.append((to_place_handle, date))
citation_list = self.get_citation_list(sql, "place_ref",
ref_handle)
retval.append((to_place_handle, date, citation_list,
(h_type0, h_type1)))
return retval

def get_place_type_list(self, sql, handle):
place_type_list = sql.query(
"""select * from place_type where from_handle = ?;""",
handle)
retval = []
for place_type_data in place_type_list:
ref_handle, handle, type0, type1 = place_type_data
date_handle = self.get_link(sql, "place_type", ref_handle, "date")
date = self.get_date(sql, date_handle)
citation_list = self.get_citation_list(sql, "place_type",
ref_handle)
retval.append((type0, date, citation_list))
if type0 in PlaceType.DATAMAP:
# if the number is already there, we are done
continue
if type0 < PlaceType.CUSTOM:
# number is not definitive, check for already there by name
for tup in PlaceType.DATAMAP.values():
if type1.lower() == tup[DM_NAME].lower():
break
else:
PlaceType.DATAMAP[type0] = (type1,
PlaceType.G_PLACE, # groups
True) # visible
else:
# not found, so store the new definition
PlaceType.DATAMAP[type0] = (type1,
PlaceType.G_PLACE, # groups
True) # visible
return retval

def get_main_location(self, sql, from_handle, with_parish):
Expand Down Expand Up @@ -784,8 +831,7 @@ def _process(self, count, total, sql):
places = sql.query("""select * from place;""")
for place in places:
count += 1
(handle, gid, title, value, the_type0, the_type1, code, long, lat,
lang, change, private) = place
(handle, gid, title, long, lat, change, private) = place

# We could look this up by "place_main", but we have the handle:
#main_loc = self.get_main_location(sql, handle, with_parish=True)
Expand All @@ -796,14 +842,15 @@ def _process(self, count, total, sql):
citation_list = self.get_citation_list(sql, "place", handle)
note_list = self.get_note_list(sql, "place", handle)
tags = self.get_links(sql, "place", handle, "tag")
place_type = (the_type0, the_type1)
alt_place_name_list = self.get_alt_place_name_list(sql, handle)
place_type_list = self.get_place_type_list(sql, handle)
place_name_list = self.get_place_name_list(sql, handle)
place_ref_list = self.get_place_ref_list(sql, handle)
eventref_list = self.get_event_ref_list(sql, "place", handle)
attr_list = self.get_attribute_list(sql, "place", handle)
data = (handle, gid, title, long, lat, place_ref_list,
PlaceName(value=value, lang=lang).serialize(),
alt_place_name_list, place_type, code, alt_loc_list,
urls, media_list, citation_list, note_list,
change, tags, private)
place_name_list, place_type_list, eventref_list,
alt_loc_list, urls, media_list, citation_list, note_list,
change, tags, private, attr_list)
g_plac = Place()
g_plac.unserialize(data)
self.db.commit_place(g_plac, self.trans)
Expand Down Expand Up @@ -900,3 +947,4 @@ def importData(db, filename, user):
g = SQLReader(db, filename, user)
g.process()
g.cleanup()
return _("Import finished...")

0 comments on commit c23f658

Please sign in to comment.