Class: Qreport::ReportRun
- Inherits:
-
Object
- Object
- Qreport::ReportRun
- Includes:
- Initialization, Model
- Defined in:
- lib/qreport/report_run.rb,
lib/qreport/report_run/data.rb
Defined Under Namespace
Classes: Data
Instance Attribute Summary collapse
-
#additional_columns ⇒ Object
Returns the value of attribute additional_columns.
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#base_columns ⇒ Object
Returns the value of attribute base_columns.
-
#column_signature ⇒ Object
Returns the value of attribute column_signature.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#description ⇒ Object
Returns the value of attribute description.
-
#finished_at ⇒ Object
Returns the value of attribute finished_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#nrows ⇒ Object
Returns the value of attribute nrows.
-
#raw_sql ⇒ Object
Returns the value of attribute raw_sql.
-
#report_id ⇒ Object
Returns the value of attribute report_id.
-
#report_sql ⇒ Object
Returns the value of attribute report_sql.
-
#report_table ⇒ Object
Construct report_table name from column names and types.
-
#sql ⇒ Object
Returns the value of attribute sql.
-
#started_at ⇒ Object
Returns the value of attribute started_at.
-
#variant ⇒ Object
Returns the value of attribute variant.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Attributes included from Model
Class Method Summary collapse
Instance Method Summary collapse
- #_options(options) ⇒ Object
- #_select(options = nil) ⇒ Object
-
#data ⇒ Object
Return rows from this report run’s report table.
-
#delete!(options = nil) ⇒ Object
Deletes this report and its rows.
- #drop_table!(options = nil) ⇒ Object
- #error ⇒ Object
- #error=(x) ⇒ Object
- #error_object ⇒ Object
- #insert! ⇒ Object
- #reload! ⇒ Object
- #run!(conn) ⇒ Object
- #select(options = nil) ⇒ Object
-
#truncate!(options = nil) ⇒ Object
Deletes the actual rows for this report run.
- #update!(options = nil) ⇒ Object
Methods included from Initialization
#initialize, #initialize_from_hash!
Instance Attribute Details
#additional_columns ⇒ Object
Returns the value of attribute additional_columns.
10 11 12 |
# File 'lib/qreport/report_run.rb', line 10 def additional_columns @additional_columns end |
#arguments ⇒ Object
Returns the value of attribute arguments.
12 13 14 |
# File 'lib/qreport/report_run.rb', line 12 def arguments @arguments end |
#base_columns ⇒ Object
Returns the value of attribute base_columns.
15 16 17 |
# File 'lib/qreport/report_run.rb', line 15 def base_columns @base_columns end |
#column_signature ⇒ Object
Returns the value of attribute column_signature.
15 16 17 |
# File 'lib/qreport/report_run.rb', line 15 def column_signature @column_signature end |
#columns ⇒ Object
Returns the value of attribute columns.
15 16 17 |
# File 'lib/qreport/report_run.rb', line 15 def columns @columns end |
#created_at ⇒ Object
Returns the value of attribute created_at.
18 19 20 |
# File 'lib/qreport/report_run.rb', line 18 def created_at @created_at end |
#description ⇒ Object
Returns the value of attribute description.
11 12 13 |
# File 'lib/qreport/report_run.rb', line 11 def description @description end |
#finished_at ⇒ Object
Returns the value of attribute finished_at.
18 19 20 |
# File 'lib/qreport/report_run.rb', line 18 def finished_at @finished_at end |
#id ⇒ Object
Returns the value of attribute id.
9 10 11 |
# File 'lib/qreport/report_run.rb', line 9 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/qreport/report_run.rb', line 10 def name @name end |
#nrows ⇒ Object
Returns the value of attribute nrows.
17 18 19 |
# File 'lib/qreport/report_run.rb', line 17 def nrows @nrows end |
#raw_sql ⇒ Object
Returns the value of attribute raw_sql.
14 15 16 |
# File 'lib/qreport/report_run.rb', line 14 def raw_sql @raw_sql end |
#report_id ⇒ Object
Returns the value of attribute report_id.
13 14 15 |
# File 'lib/qreport/report_run.rb', line 13 def report_id @report_id end |
#report_sql ⇒ Object
Returns the value of attribute report_sql.
14 15 16 |
# File 'lib/qreport/report_run.rb', line 14 def report_sql @report_sql end |
#report_table ⇒ Object
Construct report_table name from column names and types.
22 23 24 |
# File 'lib/qreport/report_run.rb', line 22 def report_table @report_table end |
#sql ⇒ Object
Returns the value of attribute sql.
10 11 12 |
# File 'lib/qreport/report_run.rb', line 10 def sql @sql end |
#started_at ⇒ Object
Returns the value of attribute started_at.
18 19 20 |
# File 'lib/qreport/report_run.rb', line 18 def started_at @started_at end |
#variant ⇒ Object
Returns the value of attribute variant.
10 11 12 |
# File 'lib/qreport/report_run.rb', line 10 def variant @variant end |
#verbose ⇒ Object
Returns the value of attribute verbose.
19 20 21 |
# File 'lib/qreport/report_run.rb', line 19 def verbose @verbose end |
Class Method Details
.find(id) ⇒ Object
168 169 170 171 172 |
# File 'lib/qreport/report_run.rb', line 168 def self.find id obj = new obj.id = id obj.reload! end |
.schema!(conn, options = { }) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/qreport/report_run.rb', line 92 def self.schema! conn, = { } result = conn.run "CREATE SEQUENCE qr_report_runs_pkey;\nCREATE TABLE -- IF NOT EXISTS\nqr_report_runs (\nid INTEGER PRIMARY KEY DEFAULT nextval('qr_report_runs_pkey')\n , name VARCHAR(255) NOT NULL\n , variant VARCHAR(255)\n , sql TEXT NOT NULL\n , description TEXT NOT NULL\n , arguments TEXT NOT NULL\n , base_columns TEXT NOT NULL\n , additional_columns TEXT NOT NULL\n , report_table VARCHAR(255) NOT NULL\n , error TEXT\n , created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()\n , started_at TIMESTAMP WITH TIME ZONE\n , finished_at TIMESTAMP WITH TIME ZONE\n , nrows INTEGER\n);\nCREATE INDEX qr_report_runs__name ON qr_report_runs (name);\nCREATE INDEX qr_report_runs__variant ON qr_report_runs (variant);\nCREATE INDEX qr_report_runs__report_table ON qr_report_runs (report_table);\nCREATE INDEX qr_report_runs__created_at ON qr_report_runs (created_at);\n", .merge(:capture_error => true) # , :verbose => true end |
Instance Method Details
#_options(options) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/qreport/report_run.rb', line 143 def ||= EMPTY_Hash arguments = [:arguments] || EMPTY_Hash arguments = arguments.merge(:qr_run_id => id) .merge(:arguments => arguments) end |
#_select(options = nil) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/qreport/report_run.rb', line 193 def _select = nil = columns = [:COLUMNS] || '*' columns = conn.safe_sql(columns) order_by = conn.safe_sql([:order_by] || '') [:arguments].update( :COLUMNS => columns, :ORDER_BY => order_by) conn.run "SELECT :COLUMNS FROM #{report_table} WHERE qr_run_id = :qr_run_id :WHERE? :ORDER_BY?", end |
#data ⇒ Object
Return rows from this report run’s report table.
184 185 186 |
# File 'lib/qreport/report_run.rb', line 184 def data @data ||= ReportRun::Data.new(self) end |
#delete!(options = nil) ⇒ Object
Deletes this report and its rows.
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/qreport/report_run.rb', line 205 def delete! = nil truncate! = .update(:capture_error => true) conn.run "DELETE FROM qr_report_runs WHERE id = :qr_run_id", # .merge(:verbose => true) result = conn.run "SELECT COUNT(*) AS \"count\" from qr_report_runs WHERE report_table = :report_table", :arguments => { :report_table => report_table }, :capture_error => true # , :verbose => true if result.rows[0]["count"] <= 0 # drop_table! end end |
#drop_table!(options = nil) ⇒ Object
218 219 220 |
# File 'lib/qreport/report_run.rb', line 218 def drop_table! = nil conn.run "DROP TABLE #{report_table}", :capture_error => true # , :verbose => true end |
#error ⇒ Object
71 72 73 74 |
# File 'lib/qreport/report_run.rb', line 71 def error self.error = @error if String === @error @error end |
#error=(x) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/qreport/report_run.rb', line 76 def error= x case x when nil, Hash @error = x when String @error = JSON.parse(x) when Exception @error_object = x @error = { :error_class => x.class.name, :error_message => x. } else raise TypeError end end |
#error_object ⇒ Object
90 |
# File 'lib/qreport/report_run.rb', line 90 def error_object; @error_object || @error; end |
#insert! ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/qreport/report_run.rb', line 119 def insert! values = { :name => name, :variant => variant, :sql => sql, :description => description, :arguments => (arguments || { }), :base_columns => base_columns, :additional_columns => additional_columns, :report_table => report_table, :error => error, :created_at => created_at, :started_at => started_at, :finished_at => finished_at, :nrows => nrows, } result = conn.run 'INSERT INTO qr_report_runs ( :NAMES ) VALUES ( :VALUES ) RETURNING id', :arguments => { :names_and_values => values } # , :verbose => true, :verbose_arguments => true self.id = result.rows[0]["id"] or raise "no id" self end |
#reload! ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/qreport/report_run.rb', line 174 def reload! result = conn.run("SELECT * FROM qr_report_runs WHERE id = :id LIMIT 1", :arguments => { :id => id }) result = result.rows.first initialize_from_hash! result @base_columns = JSON.parse(@base_columns) @data = nil self end |
#run!(conn) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/qreport/report_run.rb', line 64 def run! conn runner = Qreport::ReportRunner.new runner.connection = conn runner.run!(self) self end |
#select(options = nil) ⇒ Object
188 189 190 191 |
# File 'lib/qreport/report_run.rb', line 188 def select = nil = _select({:order_by => 'ORDER BY qr_run_row'}.merge()) end |
#truncate!(options = nil) ⇒ Object
Deletes the actual rows for this report run.
223 224 225 226 227 228 |
# File 'lib/qreport/report_run.rb', line 223 def truncate! = nil = .update(:capture_error => true) conn.run "DELETE FROM #{report_table} WHERE qr_run_id = :qr_run_id :WHERE?", self end |
#update!(options = nil) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/qreport/report_run.rb', line 150 def update! = nil = values = [:values] || EMPTY_Hash if Array === values h = { } values.each { | k | h[k] = send(k) } values = h end [:arguments].update(:names_and_values => values) # options.update :verbose => true, :verbose_result => true # , :dry_run => true conn.run "UPDATE qr_report_runs\nSET :SET_VALUES\nWHERE id = :qr_run_id\n:WHERE?\n", end |