Class: Birdwatcher::Commands::Query

Inherits:
Birdwatcher::Command show all
Defined in:
lib/birdwatcher/commands/query.rb

Constant Summary

Constants inherited from Birdwatcher::Command

Birdwatcher::Command::ARGUMENT_SEPARATOR

Constants included from Birdwatcher::Concerns::Concurrency

Birdwatcher::Concerns::Concurrency::DEFAULT_THREAD_POOL_SIZE

Constants included from Birdwatcher::Concerns::Core

Birdwatcher::Concerns::Core::DATA_DIRECTORY

Instance Attribute Summary

Attributes inherited from Birdwatcher::Command

#arguments

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Birdwatcher::Command

auto_completion, auto_completion_strings, descendants, #execute, has_name?, meta, meta=

Methods included from Birdwatcher::Concerns::Concurrency

included, #thread_pool

Methods included from Birdwatcher::Concerns::Persistence

included, #save_status, #save_user

Methods included from Birdwatcher::Concerns::Presentation

included, #make_status_summary_output, #make_url_summary_output, #make_user_details_output, #make_user_summary_output, #output_status_summary, #output_user_details, #output_user_summary, #page_text

Methods included from Birdwatcher::Concerns::Outputting

#confirm, #error, #fatal, included, #info, #line_separator, #newline, #output, #output_formatted, #task, #warn

Methods included from Birdwatcher::Concerns::Util

#escape_html, #excerpt, included, #parse_time, #pluralize, #strip_control_characters, #strip_html, #suppress_output, #suppress_warnings, #time_ago_in_words, #unescape_html

Methods included from Birdwatcher::Concerns::Core

#console, #current_workspace, #current_workspace=, #database, included, #klout_client, #read_data_file, #twitter_client

Class Method Details

.detailed_usageObject



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
# File 'lib/birdwatcher/commands/query.rb', line 10

def self.detailed_usage
<<-USAGE
The #{'query'.bold} command can be used to execute raw SQL queries against the
underlying database for Birdwatcher. The query results will be shown in a formatted
table.

#{'IMPORTANT:'.bold} The query command does not automatically isolate the data
to the current workspace so queries will need to handle that on their own.
Most tables will have a column called #{'workspace_id'.bold} which will contain
the numeric ID of the workspace the object belongs to.

For a more machine-parsable query result, please see the #{'query_csv'.bold} command.

#{'USAGE EXAMPLES:'.bold}

#{'See current workspaces:'.bold}
  query SELECT * from workspaces ORDER BY name

#{'See geo coordinates for all statuses in a workspace:'.bold}
  query SELECT longitude,latitude FROM statuses WHERE geo IS TRUE AND workspace_id = 1

#{'See statuses containing the word "password":'.bold}
  query SELECT u.screen_name, s.text, s.posted_at FROM users AS u JOIN statuses AS s ON s.user_id = u.id WHERE s.text LIKE '%password%'

#{'See status geographic places by frequency:'.bold}
  query SELECT COUNT(*) AS count, place_name FROM statuses WHERE place_name IS NOT NULL GROUP BY place_name ORDER BY count DESC
USAGE
end

Instance Method Details

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/birdwatcher/commands/query.rb', line 39

def run
  if !arguments?
    error("You must provide an SQL query to execute")
    return false
  end

  query  = arguments.join(" ")
  result = database[query]
  rows   = result.map { |r| r.to_hash.values }
  table  = Terminal::Table.new(
    :headings => result.columns.map { |c| c.to_s.bold },
    :rows     => rows
  ).to_s
  page_text(table)
rescue Sequel::DatabaseError => e
  error("Syntax error: #{e.message}")
end