Created
February 18, 2014 13:00
-
-
Save AlexTalker/9070530 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sequel' | |
DB = "sqlite://application.db" | |
def added_message(name, msg) | |
# added a message in posts database | |
Sequel.connect(DB) do |db| | |
posts = db[:posts] | |
posts.insert(:time => Time.now.to_s, :name => name, :msg => msg) | |
end | |
end | |
def get_posts(range) | |
# return array posts in range | |
Sequel.connect(DB) do |db| | |
posts = db[:posts] | |
array = posts.where(:id => range) | |
array.all | |
end | |
end | |
def get_posts_by_user(name, page = 1) | |
#return user posts | |
Sequel.connect(DB) do |db| | |
# posts = db[:posts].limit(10).where(:msg.like('%@'+name+'%')) | |
# posts = db['select id, time, name, msg from posts where name = ? limit 10 offset ?', name,(page.to_i-1)*10] | |
posts = db[:posts].select(:id, :time, :name, :msg).where(:name => name).limit(10).offset((page.to_i-1)*10).all | |
posts.reverse | |
end | |
end | |
def get_posts_by_tag(tag, page = 1) | |
#return posts included #tag | |
Sequel.connect(DB) do |db| | |
# posts = db['select id, time, name, msg from posts where msg like ? limit 10 offset ?', '%#'+tag+'%',(page.to_i-1)*10] | |
posts = db[:posts].select(:id, :time, :name, :msg).where(Sequel.like(:msg, '%#'+tag+'%')).limit(10).offset((page.to_i-1)*10).all | |
posts.reverse | |
end | |
end | |
def last_tag_post_id(tag) | |
Sequel.connect(DB) do |db| | |
posts = db['select count(msg) from posts where msg like ?', '%#'+tag+'%'] | |
posts[:"count(msg)"][:"count(msg)"].to_i | |
end | |
end | |
def last_user_post_id(name) | |
Sequel.connect(DB) do |db| | |
# posts = db['select count(msg) from posts where name = ?', name] | |
posts = db[:posts].where(:name => name).count | |
posts.to_i | |
end | |
end | |
def last_post_id | |
#return id last post in db | |
Sequel.connect(DB) do |db| | |
db[:posts].max(:id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment