Skip to content

Commit

Permalink
Refactoring sqlite3 backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
koichiro committed Dec 28, 2009
1 parent 0f48cc7 commit e468703
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
17 changes: 7 additions & 10 deletions lib/plugins/storage.rb
@@ -1,19 +1,16 @@
# -*- coding: utf-8 -*-

require 'pp'
require 'time'

require File.dirname(__FILE__) + '/storage/status'
require File.dirname(__FILE__) + '/storage/db'

module Termtter::Client
public_storage[:log] = []

@db = Termtter::Storage::DB.new
register_hook(
:name => :storage,
:points => [:pre_filter],
:exec_proc => lambda {|statuses, event|
statuses.each do |s|
Termtter::Storage::Status.insert(
@db.update(
:post_id => s.id,
:created_at => Time.parse(s.created_at).to_i,
:in_reply_to_status_id => s.in_reply_to_status_id,
Expand All @@ -31,8 +28,8 @@ module Termtter::Client
:aliases => [:ss],
:exec_proc => lambda {|arg|
unless arg.strip.empty?
key = arg.strip
statuses = Termtter::Storage::Status.search({:text => key})
text = arg.strip
statuses = @db.find_text(text)
output(statuses, :search)
end
},
Expand All @@ -44,8 +41,8 @@ module Termtter::Client
:aliases => [:ssu],
:exec_proc => lambda {|arg|
unless arg.strip.empty?
key = arg.strip.gsub(/^@/, '')
statuses = Termtter::Storage::Status.search_user({:user => key})
user = arg.strip.gsub(/^@/, '')
statuses = @db.find_user(user)
output(statuses, :search)
end
},
Expand Down
36 changes: 29 additions & 7 deletions lib/plugins/storage/sqlite3.rb
Expand Up @@ -37,8 +37,33 @@ def create_table
end

def update(status)
return nil if find_id(status[:post_id])
insert(status)
@db.transaction
begin
return nil if find_id(status[:post_id])
insert(status)
ensure
@db.commit
end
end

def update_user(user_id, screen_name)
return nil if find_user_id(user_id)
@db.execute(
"insert into user values(?,?)",
status[:user_id],
status[:screen_name])
end

FIND_USER_ID = <<-EOS
select id, screen_name
from user where id = ?
EOS
def find_user_id(user_id)
result = nil
@db.execute(FIND_USER_ID, user_id) do |id, screen_name|
result = { :id => id, :screen_name => screen_name}
end
result
end

def insert(status)
Expand All @@ -51,10 +76,7 @@ def insert(status)
status[:in_reply_to_user_id],
status[:text],
status[:user_id])
@db.execute(
"insert into user values(?,?)",
status[:user_id],
status[:screen_name])
update_user(status[:user_id], status[:screen_name])
end

FIND_ID = <<-EOS
Expand All @@ -63,7 +85,7 @@ def insert(status)
EOS
def find_id(id)
result = nil
@db.execute(FIND, id) do |created_at, screen_name, post_text, in_reply_to_status_id, post_id, user_id|
@db.execute(FIND_ID, id) do |created_at, screen_name, post_text, in_reply_to_status_id, post_id, user_id|
result = Termtter::ActiveRubytter.new({
:id => post_id,
:created_at => created_at,
Expand Down
4 changes: 4 additions & 0 deletions lib/termtter/active_rubytter.rb
Expand Up @@ -43,5 +43,9 @@ def to_hash
memo
end
end

def retweeted_status
nil
end
end
end
3 changes: 2 additions & 1 deletion spec/plugins/storage/sqlite3_spec.rb
@@ -1,9 +1,10 @@
require 'tmpdir'
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/../../../lib/plugins/storage/sqlite3'

module Termtter::Storage
describe "sqlite3" do
DB_FILE = '/tmp/test.db'
DB_FILE = File.join(Dir.tmpdir, 'test.db')
before(:each) do
File.delete(DB_FILE) if File.exists?(DB_FILE)
@db = Termtter::Storage::SQLite3.new(DB_FILE)
Expand Down

0 comments on commit e468703

Please sign in to comment.