class LiveJournal::Database
An interface for an SQLite database dump.
Constants
- EXPECTED_DATABASE_VERSION
- SCHEMA
Attributes
db[R]
The underlying SQLite3 database.
Public Class Methods
new(filename, create_if_necessary=false)
click to toggle source
# File lib/livejournal/database.rb, line 82 def initialize(filename, create_if_necessary=false) exists = FileTest::exists? filename raise Errno::ENOENT if not create_if_necessary and not exists @db = SQLite3::Database.new(filename) # We'd like to use type translation, but it unfortunately fails on MAX() # queries. # @db.type_translation = true if exists # Existing database! version = self.version unless version == EXPECTED_DATABASE_VERSION raise Error, "Database version mismatch -- db has #{version.inspect}, expected #{EXPECTED_DATABASE_VERSION.inspect}" end end if create_if_necessary and not exists # New database! Initialize it. transaction do @db.execute_batch(SCHEMA) end self.version = EXPECTED_DATABASE_VERSION end end
Public Instance Methods
close()
click to toggle source
Close the underlying database. (Is this necessary? Not sure.)
# File lib/livejournal/database.rb, line 115 def close @db.close end
each_entry(where=nil, &block)
click to toggle source
Yield a set of entries, ordered by ascending itemid (first to last).
# File lib/livejournal/database.rb, line 167 def each_entry(where=nil, &block) sql = 'SELECT * FROM entry' sql += " WHERE #{where}" if where sql += ' ORDER BY itemid ASC' query_entries sql, &block end
get_entry(itemid)
click to toggle source
Fetch a specific itemid.
# File lib/livejournal/database.rb, line 149 def get_entry(itemid) query_entry("select * from entry where itemid=?", itemid) end
journal()
click to toggle source
The the actual journal stored by this Database
. (This is different than simply the username when usejournal is specified.)
# File lib/livejournal/database.rb, line 137 def journal usejournal || username end
last_comment_full()
click to toggle source
Used for Sync::Comments
.
# File lib/livejournal/database.rb, line 191 def last_comment_full Database::optional_to_i( @db.get_first_value('SELECT MAX(commentid) FROM comment ' + 'WHERE body IS NOT NULL')) end
last_comment_meta()
click to toggle source
Used for Sync::Comments
.
# File lib/livejournal/database.rb, line 186 def last_comment_meta Database::optional_to_i( @db.get_first_value('SELECT MAX(commentid) FROM comment')) end
query_entries(sql, *sqlargs) { |entry| ... }
click to toggle source
Given SQL that selects some entries, yield each Entry
.
# File lib/livejournal/database.rb, line 160 def query_entries(sql, *sqlargs) # :yields: entry @db.execute(sql, *sqlargs) do |row| yield Entry.new.load_from_database_row(row) end end
query_entry(sql, *sqlargs)
click to toggle source
Given SQL that selects an entry, return that Entry
.
# File lib/livejournal/database.rb, line 154 def query_entry(sql, *sqlargs) row = @db.get_first_row(sql, *sqlargs) return Entry.new.load_from_database_row(row) end
store_comments_full(comments)
click to toggle source
Used for Sync::Comments
.
# File lib/livejournal/database.rb, line 202 def store_comments_full(comments) store_comments(comments, false) end
store_comments_meta(comments)
click to toggle source
Used for Sync::Comments
.
# File lib/livejournal/database.rb, line 198 def store_comments_meta(comments) store_comments(comments, true) end
store_entry(entry)
click to toggle source
Store an Entry
.
# File lib/livejournal/database.rb, line 180 def store_entry entry sql = 'INSERT OR REPLACE INTO entry VALUES (' + ("?, " * 17) + '?)' @db.execute(sql, *entry.to_database_row) end
store_usermap(usermap)
click to toggle source
Used for Sync::Comments
.
# File lib/livejournal/database.rb, line 207 def store_usermap(usermap) transaction do sql = "INSERT OR REPLACE INTO users VALUES (?, ?)" @db.prepare(sql) do |stmt| usermap.each do |id, user| stmt.execute(id, user) end end end end
total_entry_count()
click to toggle source
Return the total number of entries.
# File lib/livejournal/database.rb, line 175 def total_entry_count @db.get_first_value('SELECT COUNT(*) FROM entry').to_i end
trace!()
click to toggle source
Turn tracing on. Mostly useful for debugging.
# File lib/livejournal/database.rb, line 142 def trace! @db.trace() do |data, sql| puts "SQL> #{sql.inspect}" end end
transaction() { || ... }
click to toggle source
Run a block within a single database transaction. Useful for bulk inserts.
# File lib/livejournal/database.rb, line 110 def transaction @db.transaction { yield } end
Private Instance Methods
store_comments(comments, meta_only=true)
click to toggle source
# File lib/livejournal/database.rb, line 219 def store_comments(comments, meta_only=true) transaction do sql = "INSERT OR REPLACE INTO comment " if meta_only sql += "(commentid, posterid, state) VALUES (?, ?, ?)" else sql += "VALUES (?, ?, ?, ?, ?, ?, ?, ?)" end @db.prepare(sql) do |stmt| comments.each do |id, comment| if meta_only stmt.execute(comment.commentid, comment.posterid, LiveJournal::Comment::state_to_string(comment.state)) else stmt.execute(*comment.to_database_row) end end end end end