# -*- coding: utf-8 -*-

from sqlobject import *

########## database stuff ##########

sqlhub.processConnection = connectionForURI('sqlite:/var/tmp/articledb.sqlite')
#db_conn = connectionForURI('postgres://test:testtest@farruca:5433/codes-et-lois')
#sqlhub.processConnection = db_conn

class DBCodeTable(SQLObject):
    url = StringCol()
    title = UnicodeCol()
    urlIndex = DatabaseIndex('url')

DBCodeTable.createTable(ifNotExists=True)

class DBArticleTable(SQLObject):
    url = StringCol()
    title = UnicodeCol()
    normalizedTitle = StringCol()
    code = ForeignKey('DBCodeTable')
    previous = SingleJoin('DBArticleNext', joinColumn='next_article_id')
    next = SingleJoin('DBArticleNext', joinColumn='article_id')
    references = SQLMultipleJoin('DBArticleTable', 
                                 joinColumn='from_article_id')
    backReferences = SQLMultipleJoin('DBArticleTable', 
                                      joinColumn='to_article_id')
    normalizedTitleIndex = DatabaseIndex('normalizedTitle')

DBArticleTable.createTable(ifNotExists=True)

class DBArticleReferences(SQLObject):
    fromArticle = ForeignKey('DBArticleTable')
    toArticle = ForeignKey('DBArticleTable')
    fromToIndex = DatabaseIndex('fromArticle', 'toArticle', unique=True)

DBArticleReferences.createTable(ifNotExists=True)

class DBArticleNext(SQLObject):
    article = ForeignKey('DBArticleTable')
    nextArticle = ForeignKey('DBArticleTable')

DBArticleNext.createTable(ifNotExists=True)

