80 lines
2.1 KiB
Python
80 lines
2.1 KiB
Python
import logging
|
|
import os
|
|
from model import *
|
|
import sqlite3
|
|
|
|
log = logging.getLogger("storage")
|
|
|
|
DB_FRAGMENTS = ""
|
|
|
|
# Commands
|
|
|
|
# birth location - general area, not exact location (i.e. Transylvania)
|
|
# birth origin - rural or urban
|
|
# studies - masters, bachelors, high school, middle school, primary school
|
|
# occupation - comma separated if there are multiple
|
|
# studiesAbroad - foreign cities where author studied (comma separated)
|
|
COMMAND_CREATE_AUTHORS = """CREATE TABLE Authors (
|
|
name TEXT PRIMARY KEY,
|
|
birthYear INTEGER,
|
|
birthLocation TEXT,
|
|
birthOrigin TEXT,
|
|
studies TEXT,
|
|
occupations TEXT,
|
|
studiesAbroad TEXT
|
|
)"""
|
|
|
|
# genre - short story (nuvela), novel (roman), poem etc
|
|
# movement - literary movement (submovements separated by /) (i.e. realism/naturalism)
|
|
# tags - other relevant information (i.e. psychological)
|
|
COMMAND_CREATE_FRAGMENTS = """CREATE TABLE Fragments (
|
|
id INTEGER PRIMARY KEY,
|
|
title TEXT,
|
|
year INTEGER,
|
|
author TEXT REFERENCES Authors(name),
|
|
genre TEXT,
|
|
movement TEXT,
|
|
tags TEXT
|
|
)"""
|
|
|
|
# contains the actual text
|
|
COMMAND_CREATE_FRAGMENTS_CONTENT = """CREATE TABLE FragmentsContent (
|
|
id INTEGER REFERENCES Fragments(id),
|
|
content TEXT
|
|
)"""
|
|
|
|
# Initialize databases
|
|
def initializeFragmentDatabase(dbFile):
|
|
global DB_FRAGMENTS
|
|
DB_FRAGMENTS = dbFile
|
|
|
|
if not os.path.exists(dbFile):
|
|
log.info("Text database %s not found. Will create database.", dbFile)
|
|
con = sqlite3.connect(dbFile)
|
|
c = con.cursor()
|
|
c.execute(COMMAND_CREATE_AUTHORS)
|
|
c.execute(COMMAND_CREATE_FRAGMENTS)
|
|
c.execute(COMMAND_CREATE_FRAGMENTS_CONTENT)
|
|
con.commit()
|
|
con.close()
|
|
log.info("Database created!")
|
|
|
|
def getTextCount():
|
|
con = sqlite3.connect(DB_FRAGMENTS)
|
|
c = con.cursor()
|
|
c.execute("SELECT COUNT(*) FROM Fragments")
|
|
item = c.fetchone()
|
|
c.close()
|
|
con.close()
|
|
return item[0]
|
|
|
|
def getAllTexts():
|
|
con = sqlite3.connect(DB_FRAGMENTS)
|
|
c = con.cursor()
|
|
c.execute("SELECT id, content FROM FragmentsContent")
|
|
|
|
items = c.fetchall()
|
|
|
|
c.close()
|
|
con.close()
|
|
return items |