Class | Ruport::Query |
In: |
lib/ruport/query.rb
lib/ruport/query/sql_split.rb |
Parent: | Object |
Query offers a way to interact with databases via RubyDBI. It supports returning result sets in either Ruport‘s Data::Table, or in their raw form as DBI::Rows.
Query allows you to treat your result sets as an Enumerable data structure that plays well with the rest of Ruport.
If you are using ActiveRecord, you might prefer our acts_as_reportable extension.
raw_data | [RW] | |
sql | [R] | The original SQL for the Query object |
Allows you to add a labeled DBI source configuration.
Query objects will use the source labeled :default, unless another source is specified.
Examples:
# a connection to a MySQL database foo with user root, pass chunkybacon Query.add_source :default, :dsn => "dbi:mysql:foo", :user => "root", :password => "chunkybacon" # a second connection to a MySQL database bar Query.add_source :test, :dsn => "dbi:mysql:bar", :user => "tester", :password => "blinky"
Ruport::Query provides an interface for dealing with raw SQL queries. The SQL can be single or multistatement, but the resulting Data::Table will consist only of the result of the last statement.
Available options:
:source: | A source specified in Ruport::Query.sources, defaults to :default. |
:dsn: | If specifed, the Query object will manually override Ruport::Query. |
:user: | If a DSN is specified, the user can be set with this option. |
:password: | If a DSN is specified, the password can be set with this option. |
:row_type: | When set to :raw, DBI::Rows will be returned instead of a Data::Table |
Examples:
# uses Ruport::Query's default source Ruport::Query.new("select * from fo") # uses the Ruport::Query's source labeled :my_source Ruport::Query.new("select * from fo", :source => :my_source) # uses a manually entered source Ruport::Query.new("select * from fo", :dsn => "dbi:mysql:my_db", :user => "greg", :password => "chunky_bacon" ) # uses a SQL file stored on disk Ruport::Query.new("my_query.sql") # explicitly use a file, even if it doesn't end in .sql Ruport::Query.new(:file => "foo")
This will set the dsn, username, and password to one specified by a source in Ruport::Query.