1+ import sqlite3
2+
3+ class SQLite3Storage :
4+ def __init__ (self , location = 'pastebin.db' ):
5+ self .connection = sqlite3 .connect (location )
6+
7+ def initialize_tables (self ):
8+ self .connection .execute (
9+ '''
10+ CREATE TABLE IF NOT EXISTS paste (
11+ paste_key CHAR(8) PRIMARY KEY,
12+ timestamp TIMESTAMP,
13+ size INT,
14+ expires TIMESTAMP,
15+ title TEXT,
16+ syntax TEXT,
17+ user TEXT NULL
18+ );
19+ '''
20+ )
21+
22+ self .connection .execute (
23+ '''
24+ CREATE TABLE IF NOT EXISTS paste_content (
25+ paste_key CHAR(8) PRIMARY KEY,
26+ raw_content TEXT
27+ );
28+ '''
29+ )
30+
31+ def has_paste_content (self , key ):
32+ cursor = self .connection .cursor ()
33+
34+ cursor .execute ('SELECT COUNT(*) FROM paste_content WHERE paste_key = ?' , (key ,))
35+
36+ paste_content_count = cursor .fetchone ()[0 ]
37+
38+ return paste_content_count > 0
39+
40+ def save_paste_reference (self , key , size , timestamp , expires , title , syntax , user ):
41+ self .connection .execute (
42+ '''
43+ INSERT OR REPLACE INTO paste
44+ (paste_key, timestamp, size, expires, title, syntax, user)
45+ VALUES
46+ (?, ?, ?, ?, ?, ?, ?)
47+ ''' ,
48+ (
49+ key ,
50+ timestamp ,
51+ size ,
52+ expires ,
53+ title ,
54+ syntax ,
55+ user ,
56+ )
57+ )
58+
59+ self .connection .commit ()
60+
61+ def save_paste_content (self , key , content ):
62+ self .connection .execute (
63+ '''
64+ INSERT OR REPLACE INTO paste_content
65+ (paste_key, raw_content)
66+ VALUES
67+ (?, ?)
68+ ''' ,
69+ (
70+ key ,
71+ content ,
72+ )
73+ )
0 commit comments