-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.boo
218 lines (174 loc) · 6.69 KB
/
Database.boo
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* This file is a part of Flickroff
*
* Copyright (C) 2008:
*
* Authors:
* Michael Dominic K. <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace Flickroff
import System
import System.IO
import Mono.Data.SqliteClient;
static class Database ():
_connection as SqliteConnection
_locker as Object
_candidatesList as List
def Initialize ():
_candidatesList = []
_locker = Object ()
_connection = SqliteConnection (GetAndCreateDatabaseUri ())
_connection.Open ()
CreateTables () if not CheckDatabaseVersion ()
def SyncToStorage ():
removalList = []
Messenger.PushMessage ("Looking at downloaded photos...")
currentPhotosDir = Config.PreviousPhotosDirectory
cmd = _connection.CreateCommand ()
cmd.CommandText = ("SELECT * FROM photos ")
lock _locker:
reader = cmd.ExecuteReader ()
# For each photo in the db...
while reader.Read ():
photoPath = Path.Combine (currentPhotosDir, reader [3])
removalList.Add (reader [0]) if not System.IO.File.Exists (photoPath)
# Now remove all the elements from the removal list
for photoId as int in removalList:
cmd = _connection.CreateCommand ()
cmd.CommandText = ("DELETE FROM photos " +
"WHERE id = :id")
p1 = cmd.CreateParameter ()
p1.ParameterName = ":id"
p1.Value = photoId
cmd.Parameters.Add (p1)
cmd.ExecuteNonQuery ()
def CopyPhotosToNewLocation (newDir):
Messenger.PushMessage ("Copying photo files to a new location...")
# FIXME Need some error handling in this function
if not System.IO.Directory.Exists (newDir):
raise ReplaceMeException ("Can't find target directory")
currentPhotosDir = Config.PreviousPhotosDirectory
cmd = _connection.CreateCommand ()
cmd.CommandText = ("SELECT * FROM photos ")
lock _locker:
reader = cmd.ExecuteReader ()
# Copy all the photos...
while reader.Read ():
oldPhotoPath = Path.Combine (currentPhotosDir, reader [3])
newPhotoPath = Path.Combine (newDir, reader [3])
subDir = System.IO.Path.GetDirectoryName (newPhotoPath)
System.IO.Directory.CreateDirectory (subDir) if not System.IO.Directory.Exists (subDir)
System.IO.File.Copy (oldPhotoPath, newPhotoPath, true)
def HasLocation (location):
cmd = _connection.CreateCommand ()
cmd.CommandText = ("SELECT * FROM photos " +
"WHERE location = :location")
p1 = cmd.CreateParameter ()
p1.ParameterName = ":location"
p1.Value = location
cmd.Parameters.Add (p1)
lock _locker:
reader = cmd.ExecuteReader ()
return true if reader.Read ()
for item as DownloadItem in _candidatesList:
return true if item.ShortPath == location
return false
def AddCandidate (candidate):
lock _locker:
_candidatesList.Add (candidate)
def HasPhoto (photoid, setid):
# Hmm, I wish I new a little more about System.Data
# FIXME Execute as non-query?
# FIXME The parameter creation should be moved to separate helper funcs
cmd = _connection.CreateCommand ()
cmd.CommandText = ("SELECT * FROM photos " +
"WHERE photoid = :photoid AND " +
"setid = :setid")
p1 = cmd.CreateParameter ()
p1.ParameterName = ":photoid"
p1.Value = photoid
cmd.Parameters.Add (p1)
p2 = cmd.CreateParameter ()
p2.ParameterName = ":setid"
p2.Value = setid
cmd.Parameters.Add (p2)
lock _locker:
reader = cmd.ExecuteReader ()
return reader.Read ()
def Reset ():
c = _connection.CreateCommand ()
c.CommandText = ("DELETE FROM photos")
lock _locker:
c.ExecuteNonQuery ()
def AddPhoto (photo as DownloadItem):
c = _connection.CreateCommand ()
c.CommandText = ("INSERT INTO photos VALUES " +
"(null, :photoid, :setid, :location)")
p1 = c.CreateParameter ()
p1.ParameterName = ":photoid"
p1.Value = photo.PhotoId
c.Parameters.Add (p1)
p2 = c.CreateParameter ()
p2.ParameterName = ":setid"
p2.Value = photo.PhotosetId
c.Parameters.Add (p2)
p3 = c.CreateParameter ()
p3.ParameterName = ":location"
p3.Value = photo.ShortPath
c.Parameters.Add (p3)
lock _locker:
c.ExecuteNonQuery ()
_candidatesList.Remove (photo) if _candidatesList.Contains (photo)
def GetPhotoCount () as int:
# FIXME Smarter way to do this without dumbly fetching all results?
count = 0
cmd = _connection.CreateCommand ()
cmd.CommandText = ("SELECT * FROM photos ")
reader = cmd.ExecuteReader ()
while reader.Read ():
count++
return count
private def GetAndCreateDatabaseUri () as string:
userdir = Environment.GetEnvironmentVariable ('HOME')
dbdir = Path.Combine (userdir, Path.Combine (".gnome2", "flickroff"))
Directory.CreateDirectory (dbdir) if not Directory.Exists (dbdir)
file = Path.Combine (dbdir, "photos.db")
return String.Format ("URI=file:{0}", file)
private def CheckDatabaseVersion () as bool:
try:
cmd = _connection.CreateCommand ()
cmd.CommandText = "SELECT * FROM info"
reader = cmd.ExecuteReader ()
reader.Read ()
raise IncompatibleDatabaseException (reader [0]) if reader [0] != '1'
return true
except e as IncompatibleDatabaseException:
raise e
except e as Exception:
return false
private def CreateTables ():
ExecuteCreateCommand ("CREATE TABLE info (version INTEGER)")
ExecuteCreateCommand ("CREATE TABLE photos (" +
"id INTEGER PRIMARY KEY," +
"photoid VARCHAR," +
"setid VARCHAR," +
"location VARCHAR)")
# Put standard version number in the database
ExecuteCreateCommand ("INSERT INTO info VALUES ('1')")
private def ExecuteCreateCommand (c):
cmd = _connection.CreateCommand ()
cmd.CommandText = c
cmd.ExecuteNonQuery ()