Class: Qreport::ReportRun

Inherits:
Object
  • Object
show all
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

Attributes included from Model

#conn

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Initialization

#initialize, #initialize_from_hash!

Instance Attribute Details

#additional_columnsObject

Returns the value of attribute additional_columns.



10
11
12
# File 'lib/qreport/report_run.rb', line 10

def additional_columns
  @additional_columns
end

#argumentsObject

Returns the value of attribute arguments.



12
13
14
# File 'lib/qreport/report_run.rb', line 12

def arguments
  @arguments
end

#base_columnsObject

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_signatureObject

Returns the value of attribute column_signature.



15
16
17
# File 'lib/qreport/report_run.rb', line 15

def column_signature
  @column_signature
end

#columnsObject

Returns the value of attribute columns.



15
16
17
# File 'lib/qreport/report_run.rb', line 15

def columns
  @columns
end

#created_atObject

Returns the value of attribute created_at.



18
19
20
# File 'lib/qreport/report_run.rb', line 18

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/qreport/report_run.rb', line 11

def description
  @description
end

#finished_atObject

Returns the value of attribute finished_at.



18
19
20
# File 'lib/qreport/report_run.rb', line 18

def finished_at
  @finished_at
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/qreport/report_run.rb', line 9

def id
  @id
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/qreport/report_run.rb', line 10

def name
  @name
end

#nrowsObject

Returns the value of attribute nrows.



17
18
19
# File 'lib/qreport/report_run.rb', line 17

def nrows
  @nrows
end

#raw_sqlObject

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_idObject

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_sqlObject

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_tableObject

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

#sqlObject

Returns the value of attribute sql.



10
11
12
# File 'lib/qreport/report_run.rb', line 10

def sql
  @sql
end

#started_atObject

Returns the value of attribute started_at.



18
19
20
# File 'lib/qreport/report_run.rb', line 18

def started_at
  @started_at
end

#variantObject

Returns the value of attribute variant.



10
11
12
# File 'lib/qreport/report_run.rb', line 10

def variant
  @variant
end

#verboseObject

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, options = { }
  result = conn.run <<"END", options.merge(:capture_error => true) # , :verbose => true
CREATE SEQUENCE qr_report_runs_pkey;
CREATE TABLE -- IF NOT EXISTS
qr_report_runs (
id           INTEGER PRIMARY KEY DEFAULT nextval('qr_report_runs_pkey')
  , name         VARCHAR(255) NOT NULL
  , variant      VARCHAR(255)
  , sql          TEXT NOT NULL
  , description  TEXT NOT NULL
  , arguments    TEXT NOT NULL
  , base_columns TEXT NOT NULL
  , additional_columns TEXT NOT NULL
  , report_table VARCHAR(255) NOT NULL
  , error        TEXT
  , created_at   TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
  , started_at   TIMESTAMP WITH TIME ZONE
  , finished_at  TIMESTAMP WITH TIME ZONE
  , nrows        INTEGER
);
CREATE INDEX qr_report_runs__name ON qr_report_runs (name);
CREATE INDEX qr_report_runs__variant ON qr_report_runs (variant);
CREATE INDEX qr_report_runs__report_table ON qr_report_runs (report_table);
CREATE INDEX qr_report_runs__created_at ON qr_report_runs (created_at);
END
end

Instance Method Details

#_options(options) ⇒ Object



143
144
145
146
147
148
# File 'lib/qreport/report_run.rb', line 143

def _options options
  options ||= EMPTY_Hash
  arguments = options[:arguments] || EMPTY_Hash
  arguments = arguments.merge(:qr_run_id => id)
  options.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 options = nil
  options = _options options
  columns = options[:COLUMNS] || '*'
  columns = conn.safe_sql(columns)
  order_by = conn.safe_sql(options[:order_by] || '')
  options[: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?", options
end

#dataObject

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! options = nil
  truncate!
  options = _options options
  options.update(:capture_error => true)
  conn.run "DELETE FROM qr_report_runs WHERE id = :qr_run_id", options # .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! options = nil
  conn.run "DROP TABLE #{report_table}", :capture_error => true  # , :verbose => true
end

#errorObject



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.message }
  else
    raise TypeError
  end
end

#error_objectObject



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 options = nil
  options = _options options
  _select({:order_by => 'ORDER BY qr_run_row'}.merge(options))
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! options = nil
  options = _options options
  options.update(:capture_error => true)
  conn.run "DELETE FROM #{report_table} WHERE qr_run_id = :qr_run_id :WHERE?", options
  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! options = nil
  options = _options options
  values = options[:values] || EMPTY_Hash
  if Array === values
    h = { }
    values.each { | k | h[k] = send(k) }
    values = h
  end
  options[:arguments].update(:names_and_values => values)
  # options.update :verbose => true, :verbose_result => true # , :dry_run => true
  conn.run <<"END", options
UPDATE qr_report_runs
SET :SET_VALUES
WHERE id = :qr_run_id
:WHERE?
END
end