summaryrefslogtreecommitdiff
path: root/db.py
blob: 1c76ad2e9f2b98cc35abe614c18d54296e8d14f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text, Index, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relation, backref
import settings

engine = create_engine(settings.db_path)

Base = declarative_base(engine = engine)

class Paste(Base):
	__tablename__ = 'paste'

	id = Column(Integer, primary_key = True)
	hash = Column(String, unique = True, index = True)
	nick = Column(String)
	date = Column(DateTime, nullable = False)
	syntax = Column(String)
	title = Column(String)
	text = Column(Text, nullable = False)

	def __init__(self, hash, nick, date, syntax, title, text):
		self.nick = nick
		self.hash = hash
		self.date = date
		self.syntax = syntax
		self.title = title
		self.text = text

	def __repr__(self):
		return '<Paste(%d, "%s", "%s", "%s", "%s")>' % (self.id, self.hash, self.nick, self.date.ctime(), self.title)

class Cache(Base):
	__tablename__ = 'cache'

	id = Column(Integer, primary_key = True)
	paste_hash = Column(String, ForeignKey('paste.hash'), index = True)
	paste = relation(Paste, primaryjoin = paste_hash == Paste.hash, lazy = False)
	syntax_name = Column(String)
	text = Column(Text, nullable = False)

	def __init__(self, hash, syntax_name, text):
		self.paste_hash = hash
		self.syntax_name = syntax_name
		self.text = text

	def __repr__(self):
		return '<Cache(%d, "%s")>' % (self.id, self.paste_hash)

Base.metadata.create_all()
Session = sessionmaker(bind = engine, autoflush = True, autocommit = False)