dbplot 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *.pdf
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jacob Rothstein
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,19 @@
1
+ h1. DBPlot
2
+
3
+ DBPlot is some ruby glue between MySQL and R. It generates and runs R based on a SQL-like DSL. It is still very early in development. The goal is to make it simpler to get common plots out of a database. For more complex examples, there will be an export mode, which will let you save the output R source file for further editing.
4
+
5
+ h2. Installation
6
+
7
+ <pre><code>sudo gem install dbplot</code></pre>
8
+
9
+ h2. Example
10
+
11
+ <pre><code>dbplot -d dbplot -u joe -p password -q "plot yvar vs xvar from tablename"</code></pre>
12
+
13
+ This would generate and execute a MySQL query (@select yvar, xvar from tablename@), pass it to R, and use ggplot2 to generate the appropriate plot (@qplot(xvar, yvar, data=data.from.mysql)@) and save it to a pdf (by default, out.pdf).
14
+
15
+ h2. Project Goals:
16
+
17
+ Support as much of common interactions between a database and ggplot2, including joins, faceting, grouping, coloring, summarization, different geoms, etc.
18
+
19
+ Lock down the security so this can be run in a sandbox through a web interface; users could type in queries and get plots back against a database.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dbplot"
8
+ gem.summary = %Q{dbplot provides a sql-like interface to ggplot2}
9
+ gem.description = %Q{ruby glue between sql and r (ggplot2)}
10
+ gem.require_path = 'lib'
11
+ gem.email = "github@jacobrothstein.com"
12
+ gem.homepage = "http://github.com/jbr/dbplot"
13
+ gem.authors = ["Jacob Rothstein"]
14
+ gem.bindir = "bin"
15
+ gem.executables = %w( dbplot )
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/*_test.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/*_test.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ if File.exist?('VERSION')
50
+ version = File.read('VERSION')
51
+ else
52
+ version = ""
53
+ end
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "dbplot #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/dbplot ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dbplot'
4
+ require 'dbplot/runner'
5
+
6
+ DbPlot::Runner.start
data/dbplot.gemspec ADDED
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dbplot}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jacob Rothstein"]
12
+ s.date = %q{2009-10-27}
13
+ s.default_executable = %q{dbplot}
14
+ s.description = %q{ruby glue between sql and r (ggplot2)}
15
+ s.email = %q{github@jacobrothstein.com}
16
+ s.executables = ["dbplot"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.textile"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.textile",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/dbplot",
29
+ "dbplot.gemspec",
30
+ "lib/dbplot.rb",
31
+ "lib/dbplot/runner.rb",
32
+ "test/dbplot_test.rb",
33
+ "test/test_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/jbr/dbplot}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{dbplot provides a sql-like interface to ggplot2}
40
+ s.test_files = [
41
+ "test/dbplot_test.rb",
42
+ "test/test_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
data/lib/dbplot.rb ADDED
@@ -0,0 +1,103 @@
1
+ require 'rubygems'
2
+ require 'mysql'
3
+ require 'enumerable_proxy'
4
+ require 'readline'
5
+
6
+ class DbPlot
7
+ attr_accessor :settings, :debug, :data, :string
8
+
9
+ def self.version
10
+ "DbPlot v" + File.instance_eval { read(expand_path(join(dirname(__FILE__), '..', "VERSION"))) }
11
+ end
12
+
13
+ def initialize(options = {})
14
+ @settings = {
15
+ :host => 'localhost',
16
+ :username => 'root',
17
+ :password => 'password',
18
+ :database => ''
19
+ }
20
+
21
+ %w(host database username password).map.to_sym.each do |setting|
22
+ settings[setting] = options[setting] if options[setting]
23
+ end
24
+ end
25
+
26
+ def execute
27
+ return unless complete?
28
+
29
+ column_string = @needed_columns.map do |col, col_alias|
30
+ "#{col}#{" AS #{col_alias}" if col_alias}"
31
+ end.join(", ")
32
+
33
+ @query = %{SELECT #{column_string} FROM #{@table}}
34
+
35
+ template = %{
36
+ require(ggplot2);
37
+ require(RMySQL);
38
+
39
+ con <- dbConnect(MySQL(), user="#{settings[:username]}",
40
+ password="#{settings[:password]}", dbname="#{settings[:database]}",
41
+ host="localhost");
42
+
43
+ data <- dbGetQuery(con, "#{@query}")
44
+
45
+ pdf('#{@file}', width = 11, height = 8.5);
46
+
47
+ qplot(#{@qplot.join(", ")}, data=data);
48
+
49
+ dev.off()
50
+ }
51
+ puts template if debug
52
+ unless settings[:dry_run]
53
+ `echo \"#{template.gsub('"', '\\"')}\" | r --no-save #{"2>&1 > /dev/null" unless debug}`
54
+ end
55
+ end
56
+
57
+ def parse
58
+ name_regex = /[a-z_]+/
59
+
60
+ if string =~ /plot (#{name_regex})(?: as (#{name_regex}))? vs (#{name_regex})(?: as (#{name_regex}))? from (#{name_regex})(?: into ([a-z._]+))?/i
61
+ @ordinate, @ordinate_alias, @abscissa, @abscissa_alias, @table, @file = $1, $2, $3, $4, $5, $6
62
+
63
+ @file ||= "out.pdf"
64
+
65
+ @needed_columns = {@ordinate => @ordinate_alias, @abscissa => @abscissa_alias}
66
+
67
+ @qplot = [
68
+ @abscissa_alias || @abscissa,
69
+ @ordinate_alias || @ordinate
70
+ ]
71
+
72
+ if string =~ /color by (#{name_regex})(?: as (#{name_regex}))?/i
73
+ @needed_columns[$1] = $2
74
+ @qplot << "colour = #{$2 || $1}"
75
+ end
76
+ else
77
+ puts "\ncould not parse: \"#{string}\"\n\n"
78
+ @string = nil
79
+ end
80
+
81
+ return self
82
+ end
83
+
84
+ def parse_line(string)
85
+ return self if string.strip == ""
86
+ @string ||= ""
87
+ @string += " #{string}"
88
+ parse if complete?
89
+ return self
90
+ end
91
+
92
+ def complete?
93
+ @string =~ /;/
94
+ end
95
+
96
+ def strip_comments
97
+ @string.gsub /#.+$/, ''
98
+ end
99
+
100
+ def prompt
101
+ @string.nil? ? 'dbplot> ' : ' -> '
102
+ end
103
+ end
@@ -0,0 +1,41 @@
1
+ class DbPlot
2
+ module Runner
3
+ def self.start
4
+ d = DbPlot.new
5
+
6
+ begin
7
+ OptionParser.new do |opts|
8
+ opts.banner = "Usage: #{$0} [options]"
9
+ opts.instance_eval do
10
+ on("--help", "This message") {puts opts; exit}
11
+ on("-v", "--verbose", "Run verbosely") {|v| d.debug = !!v }
12
+ on("-h", "--host HOST", "MySQL Host") {|h| d.settings[:host] = h }
13
+ on("-u", "--user USER", "MySQL User") {|u| d.settings[:username] = u }
14
+ on("-p", "--password PASSWORD", "MySQL Password") {|p| d.settings[:password] = p }
15
+ on("-d", "--database DATABASE", "MySQL Database") {|db| d.settings[:database] = db }
16
+ on("-q", "--query QUERY", "dbplot query") {|q| d.string = q.gsub(/;?$/, ";") }
17
+ on("--version", "Print version info and exit") {puts DbPlot.version;exit}
18
+ on "--dry-run", "Print but do not execute. Implies -v." do |dry|
19
+ d.debug = d.settings[:dry_run] = true
20
+ end
21
+ end
22
+ end.parse!
23
+ rescue => e
24
+ puts e.message
25
+ exit
26
+ end
27
+
28
+ if d.complete?
29
+ d.parse.execute
30
+ else
31
+ while line = Readline.readline(d.prompt, true)
32
+ case line
33
+ when /^(exit|quit)$/ then exit
34
+ when 'help' then puts "help message goes here\n\n "
35
+ else d.parse_line(line)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class DbplotTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'dbplot'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dbplot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jacob Rothstein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-27 00:00:00 -07:00
13
+ default_executable: dbplot
14
+ dependencies: []
15
+
16
+ description: ruby glue between sql and r (ggplot2)
17
+ email: github@jacobrothstein.com
18
+ executables:
19
+ - dbplot
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.textile
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.textile
30
+ - Rakefile
31
+ - VERSION
32
+ - bin/dbplot
33
+ - dbplot.gemspec
34
+ - lib/dbplot.rb
35
+ - lib/dbplot/runner.rb
36
+ - test/dbplot_test.rb
37
+ - test/test_helper.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/jbr/dbplot
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: dbplot provides a sql-like interface to ggplot2
66
+ test_files:
67
+ - test/dbplot_test.rb
68
+ - test/test_helper.rb