use of org.tmatesoft.sqljet.core.table.ISqlJetTable in project BibleMultiConverter by schierlm.
the class SQLiteDump method run.
@Override
public void run(String... args) throws Exception {
SqlJetDb db = SqlJetDb.open(new File(args[0]), false);
db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), StandardCharsets.UTF_8))) {
ISqlJetOptions options = db.getOptions();
bw.write("FLAG\tAutovacuum\t" + options.isAutovacuum());
bw.newLine();
bw.write("FLAG\tLegacyFileFormat\t" + options.isLegacyFileFormat());
bw.newLine();
bw.write("FLAG\tIncrementalVacuum\t" + options.isIncrementalVacuum());
bw.newLine();
bw.write("OPTION\tCacheSize\t" + options.getCacheSize());
bw.newLine();
bw.write("OPTION\tEncoding\t" + options.getEncoding());
bw.newLine();
bw.write("OPTION\tFileFormat\t" + options.getFileFormat());
bw.newLine();
bw.write("OPTION\tSchemaVersion\t" + options.getSchemaVersion());
bw.newLine();
bw.write("OPTION\tUserVersion\t" + options.getUserVersion());
bw.newLine();
ISqlJetSchema schema = db.getSchema();
List<String> triggers = new ArrayList<>(schema.getTriggerNames());
Collections.sort(triggers);
for (String trigger : triggers) {
bw.write("TRG\t" + trigger + "\t" + schema.getTrigger(trigger).toSQL());
bw.newLine();
}
List<String> indexes = new ArrayList<>(schema.getIndexNames());
Collections.sort(indexes);
for (String index : indexes) {
bw.write("IDX\t" + index + "\t" + schema.getIndex(index).toSQL());
bw.newLine();
}
List<String> views = new ArrayList<>(schema.getViewNames());
Collections.sort(views);
for (String view : views) {
bw.write("VIEW\t" + view + "\t" + schema.getView(view).toSQL());
bw.newLine();
}
List<String> vTables = new ArrayList<>(schema.getVirtualTableNames());
Collections.sort(vTables);
for (String vtbl : vTables) {
bw.write("VTBL\t" + vtbl + "\t" + schema.getVirtualTable(vtbl).toSQL());
bw.newLine();
}
List<String> tables = new ArrayList<>(schema.getTableNames());
Collections.sort(tables);
for (String table : tables) {
bw.write("TABLE\t" + table + "\t" + schema.getTable(table).toSQL());
bw.newLine();
ISqlJetTable tbl = db.getTable(table);
String primaryIndex = tbl.getPrimaryKeyIndexName();
ISqlJetCursor cursor = primaryIndex == null ? tbl.open() : tbl.order(primaryIndex);
int count = cursor.getFieldsCount();
while (!cursor.eof()) {
for (int i = 0; i < count; i++) {
bw.write(" " + cursor.getFieldType(i).name().substring(0, 1) + "\t" + escape("" + cursor.getValue(i)));
bw.newLine();
}
bw.write(" -----");
bw.newLine();
cursor.next();
}
cursor.close();
}
}
db.commit();
db.close();
}
use of org.tmatesoft.sqljet.core.table.ISqlJetTable in project BibleMultiConverter by schierlm.
the class MyBibleZone method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String outfile = exportArgs[0];
if (!outfile.endsWith(".SQLite3"))
outfile += ".SQLite3";
boolean hasFootnotes = false, hasStrongs = false;
for (Book bk : bible.getBooks()) {
for (Chapter ch : bk.getChapters()) {
for (Verse vv : ch.getVerses()) {
String elementTypes = vv.getElementTypes(Integer.MAX_VALUE);
if (elementTypes.contains("f")) {
hasFootnotes = true;
}
if (elementTypes.contains("g")) {
hasStrongs = true;
}
}
}
}
new File(outfile).delete();
SqlJetDb db = SqlJetDb.open(new File(outfile), true);
db.getOptions().setAutovacuum(true);
db.beginTransaction(SqlJetTransactionMode.WRITE);
db.getOptions().setUserVersion(0);
db.createTable("CREATE TABLE info (name TEXT, value TEXT)");
db.createTable("CREATE TABLE books (book_number NUMERIC, book_color TEXT, short_name TEXT, long_name TEXT)");
db.createTable("CREATE TABLE introductions (book_number NUMERIC, introduction TEXT)");
db.createIndex("CREATE UNIQUE INDEX introductions_index on introductions(book_number)");
db.createTable("CREATE TABLE verses (book_number INTEGER, chapter INTEGER, verse INTEGER, text TEXT)");
db.createIndex("CREATE UNIQUE INDEX verses_index on verses (book_number, chapter, verse)");
db.createTable("CREATE TABLE stories (book_number NUMERIC, chapter NUMERIC, verse NUMERIC, order_if_several NUMERIC, title TEXT)");
db.createIndex("CREATE UNIQUE INDEX stories_index on stories(book_number, chapter, verse, order_if_several)");
Map<String, String> infoValues = new LinkedHashMap<>();
MetadataBook mb = bible.getMetadataBook();
if (mb == null)
mb = new MetadataBook();
infoValues.put("language", "xx");
infoValues.put("description", bible.getName());
infoValues.put("detailed_info", "");
infoValues.put("russian_numbering", "false");
infoValues.put("chapter_string", "Chapter");
infoValues.put("introduction_string", "Introduction");
infoValues.put("strong_numbers", hasStrongs ? "true" : "false");
infoValues.put("right_to_left", "false");
infoValues.put("digits0-9", "0123456789");
infoValues.put("swaps_non_localized_words_in_mixed_language_line", "false");
infoValues.put("localized_book_abbreviations", "false");
infoValues.put("font_scale", "1.0");
infoValues.put("contains_accents", "true");
for (String mbkey : mb.getKeys()) {
if (mbkey.startsWith("MyBible.zone@")) {
infoValues.put(mbkey.substring(13).replace('.', '_'), mb.getValue(mbkey));
} else {
infoValues.put("detailed_info", infoValues.get("detailed_info") + "\r\n<br><b>" + mbkey + ":</b>" + mb.getValue(mbkey));
}
}
String bibleIntro = null, singleFootnoteMarker = null, singleXrefMarker = null;
if (exportArgs.length > 1) {
Properties props = new Properties();
FileInputStream in = new FileInputStream(exportArgs[1]);
props.load(in);
in.close();
bibleIntro = (String) props.remove("__INTRODUCTION__");
singleFootnoteMarker = (String) props.remove("__FOOTNOTE_MARKER__");
singleXrefMarker = (String) props.remove("__XREF_MARKER__");
for (Object key : props.keySet()) {
String template = props.getProperty(key.toString());
template = template.replace("${name}", bible.getName());
for (String mbkey : mb.getKeys()) template = template.replace("${" + mbkey + "}", mb.getValue(mbkey));
infoValues.put(key.toString(), template);
}
}
ISqlJetTable infoTable = db.getTable("info");
ISqlJetTable booksTable = db.getTable("books");
ISqlJetTable introductionsTable = db.getTable("introductions");
ISqlJetTable versesTable = db.getTable("verses");
ISqlJetTable storiesTable = db.getTable("stories");
for (Map.Entry<String, String> entry : infoValues.entrySet()) {
infoTable.insert(entry.getKey(), entry.getValue());
}
SqlJetDb cdb = null;
ISqlJetTable footnotesTable = null;
if (hasFootnotes) {
String commentaryfile = outfile.replace(".SQLite3", ".commentaries.SQLite3");
new File(commentaryfile).delete();
cdb = SqlJetDb.open(new File(commentaryfile), true);
cdb.getOptions().setAutovacuum(true);
cdb.beginTransaction(SqlJetTransactionMode.WRITE);
cdb.getOptions().setUserVersion(0);
cdb.createTable("CREATE TABLE info (name TEXT, value TEXT)");
cdb.createTable("CREATE TABLE commentaries (book_number NUMERIC, chapter_number_from NUMERIC, verse_number_from NUMERIC, chapter_number_to NUMERIC, verse_number_to NUMERIC, marker TEXT, text TEXT )");
cdb.createIndex("CREATE INDEX commentaries_index on commentaries(book_number, chapter_number_from, verse_number_from)");
ISqlJetTable cInfoTable = cdb.getTable("info");
for (String key : Arrays.asList("language", "description", "russian_numbering")) {
cInfoTable.insert(key, infoValues.get(key));
}
cInfoTable.insert("is_footnotes", "true");
footnotesTable = cdb.getTable("commentaries");
}
final Set<String> unsupportedFeatures = new HashSet<>();
FormattedText introProlog = null;
for (Book bk : bible.getBooks()) {
if (bk.getId() == BookID.INTRODUCTION || bk.getId() == BookID.INTRODUCTION_OT || bk.getId() == BookID.INTRODUCTION_NT || bk.getId() == BookID.APPENDIX) {
if (introProlog == null)
introProlog = new FormattedText();
introProlog.getAppendVisitor().visitHeadline(1).visitText(bk.getLongName());
bk.getChapters().get(0).getProlog().accept(introProlog.getAppendVisitor());
continue;
}
MyBibleZoneBook info = null;
for (MyBibleZoneBook bi : BOOK_INFO) {
if (bi.bookID == bk.getId())
info = bi;
}
if (info == null) {
System.out.println("WARNING: Skipping unsupported book " + bk.getId());
continue;
}
booksTable.insert(info.bookNumber, info.bookColor, bk.getAbbr(), bk.getShortName());
FormattedText prologs = null;
for (int cn = 1; cn <= bk.getChapters().size(); cn++) {
Chapter ch = bk.getChapters().get(cn - 1);
if (ch.getProlog() != null) {
if (prologs == null)
prologs = new FormattedText();
prologs.getAppendVisitor().visitHeadline(1).visitText(cn == 1 ? bk.getLongName() : "" + cn);
ch.getProlog().accept(prologs.getAppendVisitor());
}
int vn = 0;
for (VirtualVerse vv : ch.createVirtualVerses()) {
vn++;
while (vn < vv.getNumber()) versesTable.insert(info.bookNumber, cn, vn++, "");
if (vn != vv.getNumber())
throw new RuntimeException(vn + " != " + vv.getNumber());
for (int hl = 0; hl < vv.getHeadlines().size(); hl++) {
final StringBuilder sb = new StringBuilder();
final Map<StringBuilder, String> xrefTags = new HashMap<>();
vv.getHeadlines().get(hl).accept(new VisitorAdapter<RuntimeException>(null) {
@Override
protected Visitor<RuntimeException> wrapChildVisitor(Visitor<RuntimeException> childVisitor) throws RuntimeException {
return this;
}
@Override
protected void beforeVisit() throws RuntimeException {
unsupportedFeatures.add("markup in headline");
}
@Override
public void visitText(String text) throws RuntimeException {
sb.append(text.replace('<', '〈').replace('>', '〉'));
}
@Override
public Visitor<RuntimeException> visitFootnote() throws RuntimeException {
// handle this separately; we do not like
// footnote text inside the headline!
unsupportedFeatures.add("footnote in headline");
return new VisitorAdapter<RuntimeException>(null) {
@Override
protected Visitor<RuntimeException> wrapChildVisitor(Visitor<RuntimeException> childVisitor) throws RuntimeException {
return this;
}
@Override
public Visitor<RuntimeException> visitCrossReference(String bookAbbr, BookID book, int firstChapter, String firstVerse, int lastChapter, String lastVerse) throws RuntimeException {
if (!BOOK_NUMBERS.containsKey(book))
return null;
final StringBuilder innerBuilder = new StringBuilder();
String endVerse = firstChapter != lastChapter ? "-" + lastChapter + ":" + lastVerse : !firstVerse.equals(lastVerse) ? "-" + lastVerse : "";
xrefTags.put(innerBuilder, "<x>" + BOOK_NUMBERS.get(book) + " " + firstChapter + ":" + firstVerse + endVerse + "</x>");
return new VisitorAdapter<RuntimeException>(null) {
@Override
protected void beforeVisit() throws RuntimeException {
throw new RuntimeException("Unsupported content inside headline xref");
}
@Override
public void visitText(String text) throws RuntimeException {
innerBuilder.append(text.replace('<', '〈').replace('>', '〉'));
}
};
}
};
}
@Override
public Visitor<RuntimeException> visitExtraAttribute(ExtraAttributePriority prio, String category, String key, String value) throws RuntimeException {
unsupportedFeatures.add("extra atrribute in headline");
return prio.handleVisitor(category, this);
}
});
String headline = sb.toString();
for (Map.Entry<StringBuilder, String> xrefTag : xrefTags.entrySet()) {
headline = headline.replace(xrefTag.getKey().toString(), xrefTag.getValue());
}
storiesTable.insert(info.bookNumber, cn, vn, hl, headline);
}
StringBuilder vb = new StringBuilder();
Map<String, MyBibleHTMLVisitor> footnotes = new HashMap<>();
MyBibleVerseVisitor mbvv = new MyBibleVerseVisitor(vb, footnotes, unsupportedFeatures);
for (Verse v : vv.getVerses()) {
if (!v.getNumber().equals("" + vv.getNumber())) {
vb.append(" <e>(" + v.getNumber() + ")</e> ");
}
mbvv.reset();
v.accept(mbvv);
}
if (singleXrefMarker != null || singleFootnoteMarker != null) {
String singleXref = null, singleFootnote = null;
for (Map.Entry<String, MyBibleHTMLVisitor> fn : footnotes.entrySet()) {
if (!fn.getKey().matches("\\[[0-9]+\\]"))
continue;
if (fn.getValue().getResult().startsWith(FormattedText.XREF_MARKER) && singleXrefMarker != null) {
if (singleXref == null) {
singleXref = fn.getKey();
} else {
System.out.println("WARNING: More than one XREF footnote in verse " + info.bookID + " " + cn + ":" + vn);
singleXref = "-";
}
} else if (singleFootnoteMarker != null) {
if (singleFootnote == null) {
singleFootnote = fn.getKey();
} else {
System.out.println("WARNING: More than one normal footnote in verse " + info.bookID + " " + cn + ":" + vn);
singleFootnote = "-";
}
}
}
if (singleXref != null && !singleXref.equals("-")) {
MyBibleHTMLVisitor xfn = footnotes.remove(singleXref);
if (xfn == null)
throw new RuntimeException();
footnotes.put(singleXrefMarker, xfn);
String verse = vb.toString();
vb.setLength(0);
vb.append(verse.replace("<f>" + singleXref + "</f>", "<f>" + singleXrefMarker + "</f>"));
}
if (singleFootnote != null && !singleFootnote.equals("-")) {
MyBibleHTMLVisitor sfn = footnotes.remove(singleFootnote);
if (sfn == null)
throw new RuntimeException();
footnotes.put(singleFootnoteMarker, sfn);
String verse = vb.toString();
vb.setLength(0);
vb.append(verse.replace("<f>" + singleFootnote + "</f>", "<f>" + singleFootnoteMarker + "</f>"));
}
}
for (Map.Entry<String, MyBibleHTMLVisitor> fn : footnotes.entrySet()) {
footnotesTable.insert(info.bookNumber, cn, vn, cn, vn, fn.getKey(), fn.getValue().getResult());
}
versesTable.insert(info.bookNumber, cn, vn, vb.toString().trim());
}
}
if (prologs != null) {
MyBibleHTMLVisitor v = new MyBibleHTMLVisitor(unsupportedFeatures, "in introduction");
prologs.accept(v);
introductionsTable.insert(info.bookNumber, v.getResult());
}
}
if (bibleIntro != null) {
introductionsTable.insert(0, bibleIntro);
} else if (introProlog != null) {
MyBibleHTMLVisitor v = new MyBibleHTMLVisitor(unsupportedFeatures, "in introduction");
introProlog.accept(v);
introductionsTable.insert(0, v.getResult());
}
if (!unsupportedFeatures.isEmpty()) {
System.out.println("WARNING: Skipped unsupported features: " + unsupportedFeatures);
}
db.commit();
db.close();
if (cdb != null) {
cdb.commit();
cdb.close();
}
}
use of org.tmatesoft.sqljet.core.table.ISqlJetTable in project BibleMultiConverter by schierlm.
the class MyBibleZoneCrossreferences method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String outfile = exportArgs[0];
if (!outfile.endsWith(".crossreferences.SQLite3"))
outfile += ".crossreferences.SQLite3";
new File(outfile).delete();
SqlJetDb db = SqlJetDb.open(new File(outfile), true);
db.getOptions().setAutovacuum(true);
db.beginTransaction(SqlJetTransactionMode.WRITE);
db.getOptions().setUserVersion(0);
db.createTable("CREATE TABLE info (name TEXT, value TEXT)");
db.createTable("CREATE TABLE cross_references (book NUMERIC, chapter NUMERIC, verse NUMERIC, verse_end NUMERIC, book_to NUMERIC, chapter_to NUMERIC, verse_to_start NUMERIC, verse_to_end NUMERIC, votes NUMERIC)");
db.createIndex("CREATE INDEX book_and_chapter ON cross_references(book, chapter)");
Map<String, String> infoValues = new LinkedHashMap<>();
MetadataBook mb = bible.getMetadataBook();
if (mb == null)
mb = new MetadataBook();
infoValues.put("language", "xx");
infoValues.put("description", bible.getName());
infoValues.put("detailed_info", "");
infoValues.put("russian_numbering", "false");
infoValues.put("requires_reverse_processing", "false");
for (String mbkey : mb.getKeys()) {
if (mbkey.startsWith("MyBible.zone@")) {
infoValues.put(mbkey.substring(13).replace('.', '_'), mb.getValue(mbkey));
} else {
infoValues.put("detailed_info", infoValues.get("detailed_info") + "\r\n<br><b>" + mbkey + ":</b>" + mb.getValue(mbkey));
}
}
if (exportArgs.length > 1) {
Properties props = new Properties();
FileInputStream in = new FileInputStream(exportArgs[1]);
props.load(in);
in.close();
for (Object key : props.keySet()) {
String template = props.getProperty(key.toString());
template = template.replace("${name}", bible.getName());
for (String mbkey : mb.getKeys()) template = template.replace("${" + mbkey + "}", mb.getValue(mbkey));
infoValues.put(key.toString(), template);
}
}
ISqlJetTable infoTable = db.getTable("info");
ISqlJetTable crossReferencesTable = db.getTable("cross_references");
for (Map.Entry<String, String> entry : infoValues.entrySet()) {
infoTable.insert(entry.getKey(), entry.getValue());
}
for (Book bk : bible.getBooks()) {
if (!MyBibleZone.BOOK_NUMBERS.containsKey(bk.getId()))
continue;
int bn = MyBibleZone.BOOK_NUMBERS.get(bk.getId());
for (int cn = 1; cn <= bk.getChapters().size(); cn++) {
Chapter ch = bk.getChapters().get(cn - 1);
for (Verse v : ch.getVerses()) {
int vn = numericVerse(v.getNumber());
v.accept(new XrefVerseVisitor(new int[] { bn, cn, vn, 0 }, bible, crossReferencesTable));
}
}
}
db.commit();
db.close();
}
use of org.tmatesoft.sqljet.core.table.ISqlJetTable in project BibleMultiConverter by schierlm.
the class MyBibleZoneDictionary method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String outfile = exportArgs[0];
String dictionaryName = exportArgs[1];
if (!outfile.endsWith(".dictionary.SQLite3"))
outfile += ".dictionary.SQLite3";
new File(outfile).delete();
SqlJetDb db = SqlJetDb.open(new File(outfile), true);
db.getOptions().setAutovacuum(true);
db.beginTransaction(SqlJetTransactionMode.WRITE);
db.getOptions().setUserVersion(0);
db.createTable("CREATE TABLE info (name TEXT, value TEXT)");
db.createTable("CREATE TABLE dictionary (topic TEXT, definition TEXT)");
db.createIndex("CREATE UNIQUE INDEX dictionary_topic ON dictionary(topic ASC)");
Map<String, String> infoValues = new LinkedHashMap<>();
MetadataBook mb = bible.getMetadataBook();
if (mb == null)
mb = new MetadataBook();
infoValues.put("language", "xx");
infoValues.put("description", bible.getName());
infoValues.put("detailed_info", "");
infoValues.put("russian_numbering", "false");
infoValues.put("is_strong", dictionaryName.equals("strong") ? "true" : "false");
infoValues.put("is_word_forms", "false");
infoValues.put("morphology_topic_reference", "");
for (String mbkey : mb.getKeys()) {
if (mbkey.startsWith("MyBible.zone@")) {
infoValues.put(mbkey.substring(13).replace('.', '_'), mb.getValue(mbkey));
} else {
infoValues.put("detailed_info", infoValues.get("detailed_info") + "\r\n<br><b>" + mbkey + ":</b>" + mb.getValue(mbkey));
}
}
if (exportArgs.length > 2) {
Properties props = new Properties();
FileInputStream in = new FileInputStream(exportArgs[2]);
props.load(in);
in.close();
for (Object key : props.keySet()) {
String template = props.getProperty(key.toString());
template = template.replace("${name}", bible.getName());
for (String mbkey : mb.getKeys()) template = template.replace("${" + mbkey + "}", mb.getValue(mbkey));
infoValues.put(key.toString(), template);
}
}
ISqlJetTable infoTable = db.getTable("info");
ISqlJetTable dictionaryTable = db.getTable("dictionary");
for (Map.Entry<String, String> entry : infoValues.entrySet()) {
infoTable.insert(entry.getKey(), entry.getValue());
}
Set<String> usedTopics = new HashSet<>();
final Set<String> unsupportedFeatures = new HashSet<>();
for (Book bk : bible.getBooks()) {
if (bk.getId() != BookID.DICTIONARY_ENTRY) {
System.out.println("WARNING: Skipping book " + bk.getAbbr());
continue;
}
String topicName = bk.getShortName();
if (usedTopics.contains(topicName) && !dictionaryName.equals("strong")) {
for (int i = 2; ; i++) {
if (!usedTopics.contains(topicName + " (" + i + ")")) {
topicName += " (" + i + ")";
break;
}
}
}
if (usedTopics.contains(topicName)) {
System.out.println("WARNING: Skipping duplicate topic " + topicName);
continue;
}
if (dictionaryName.equals("strong")) {
if (!topicName.matches("[GH][1-9][0-9]*")) {
System.out.println("WARNING: Skipping invalid Strong number " + topicName);
continue;
}
} else if (!dictionaryName.equals("-")) {
dictionaryTable.insert("[" + dictionaryName + "]" + bk.getAbbr(), "\u2197 <a href=\"S:" + topicName + "\">" + bk.getShortName() + "</a>");
}
MyBibleHTMLVisitor v = new MyBibleHTMLVisitor(unsupportedFeatures, "in dictionary entry");
bk.getChapters().get(0).getProlog().accept(v);
dictionaryTable.insert(topicName, v.getResult());
}
if (!unsupportedFeatures.isEmpty()) {
System.out.println("WARNING: Skipped unsupported features: " + unsupportedFeatures);
}
db.commit();
db.close();
}
Aggregations