rsql 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/rsql CHANGED
@@ -59,7 +59,7 @@ else
59
59
  rc_fn = File.join(ENV['HOME'], ".#{bn}rc")
60
60
  end
61
61
 
62
- eval_context.load(rc_fn, false) if File.exists?(rc_fn)
62
+ eval_context.load(rc_fn, :skip_init_registrations) if File.exists?(rc_fn)
63
63
 
64
64
  def get_password(prompt)
65
65
  STDOUT.print(prompt)
@@ -2,7 +2,7 @@
2
2
  # Commands using an EvalContext for handling recipes.
3
3
  #
4
4
  module RSQL
5
- VERSION = '0.2.3'
5
+ VERSION = '0.2.4'
6
6
 
7
7
  require 'rsql/mysql_results'
8
8
  require 'rsql/eval_context'
@@ -29,7 +29,8 @@ module RSQL
29
29
  #
30
30
  class EvalContext
31
31
 
32
- Registration = Struct.new(:name, :args, :bangs, :block, :usage, :desc, :source)
32
+ Registration = Struct.new(:name, :args, :bangs, :block, :usage,
33
+ :desc, :source, :source_fn)
33
34
 
34
35
  HEXSTR_LIMIT = 32
35
36
 
@@ -80,7 +81,17 @@ module RSQL
80
81
  end
81
82
  end
82
83
 
83
- def load(fn, init=true)
84
+ def load(fn, opt=nil)
85
+ # this should only be done after we have established a
86
+ # mysql connection, so this option allows rsql to load the
87
+ # init file immediately and then later make the init
88
+ # registration calls--we set this as an instance variable
89
+ # to allow for loaded files to call load again and yet
90
+ # still maintain the skip logic
91
+ if opt == :skip_init_registrations
92
+ reset_skipping = @skipping_init_registrations = true
93
+ end
94
+
84
95
  ret = Thread.new {
85
96
  begin
86
97
  eval(File.read(fn), binding, fn)
@@ -91,12 +102,22 @@ module RSQL
91
102
  }.value
92
103
 
93
104
  if Exception === ret
94
- bt = ret.backtrace.collect{|line| line.start_with?(fn) ? line : nil}.compact
95
- $stderr.puts("#{ret.class}: #{ret.message}", bt, '')
105
+ if @verbose
106
+ $stderr.puts("#{ex.class}: #{ex.message}", ex.backtrace)
107
+ else
108
+ bt = ret.backtrace.collect{|line| line.start_with?(fn) ? line : nil}.compact
109
+ $stderr.puts("#{ret.class}: #{ret.message}", bt, '')
110
+ end
111
+ ret = false
96
112
  else
97
113
  @loaded_fns << fn unless @loaded_fns.include?(fn)
98
- call_init_registrations if init
114
+ call_init_registrations unless @skipping_init_registrations
115
+ ret = true
99
116
  end
117
+
118
+ @skipping_init_registrations = false if reset_skipping
119
+
120
+ return ret
100
121
  end
101
122
 
102
123
  def reload
@@ -316,6 +337,10 @@ module RSQL
316
337
  return params
317
338
  end
318
339
 
340
+ # Similiar to the MySQL "desc" command, show the content
341
+ # of nearly any registered recipe including where it was
342
+ # sourced (e.g. what file:line it came from).
343
+ #
319
344
  def desc(sym)
320
345
  unless Symbol === sym
321
346
  $stderr.puts("must provide a Symbol--try prefixing it with a colon (:)")
@@ -346,6 +371,8 @@ module RSQL
346
371
  return
347
372
  end
348
373
 
374
+ reg.source_fn = "#{fn}:#{lineno}"
375
+
349
376
  File.open(fn) do |f|
350
377
  source = ''
351
378
  i = 0
@@ -380,7 +407,7 @@ module RSQL
380
407
  end
381
408
 
382
409
  if reg.source && !reg.source.empty?
383
- puts reg.source
410
+ puts '', "[#{reg.source_fn}]", '', reg.source
384
411
  else
385
412
  $stderr.puts "unable to locate body for #{sym}"
386
413
  end
@@ -401,6 +428,9 @@ module RSQL
401
428
  MySQLResults.query(content, self, *args)
402
429
  end
403
430
 
431
+ # Show the most recent queries made to the MySQL server in
432
+ # this session. Default is to show the last one.
433
+ #
404
434
  def history(cnt=1)
405
435
  if h = MySQLResults.history(cnt)
406
436
  h.each{|q| puts '', q}
@@ -422,6 +452,10 @@ module RSQL
422
452
  # sql with variable interpolation.
423
453
  #
424
454
  def register(sym, *args, &block) # :doc:
455
+ if m = caller.first.match(/^([^:]+:\d+)/)
456
+ source_fn = m[1]
457
+ end
458
+
425
459
  name = usage = sym.to_s
426
460
 
427
461
  if Hash === args.last
@@ -434,7 +468,7 @@ module RSQL
434
468
  desc = '' unless desc
435
469
 
436
470
  if block.nil?
437
- source = args.pop
471
+ source = args.pop.strip
438
472
  sql = squeeze!(source.dup)
439
473
 
440
474
  argstr = args.join(',')
@@ -448,7 +482,8 @@ module RSQL
448
482
  usage << params(name, block)
449
483
  end
450
484
 
451
- @registrations[sym] = Registration.new(name, args, bangs, block, usage, desc, source)
485
+ @registrations[sym] = Registration.new(name, args, bangs, block, usage,
486
+ desc, source, source_fn)
452
487
  end
453
488
 
454
489
  # Convert a list of values into a comma-delimited string,
@@ -27,9 +27,11 @@ class TestEvalContext < Test::Unit::TestCase
27
27
  @ctx.load(File.join(File.dirname(__FILE__),'..','example.rsqlrc'))
28
28
  end
29
29
 
30
- def test_load
30
+ def test_reload
31
31
  orig = $stdout
32
32
  $stdout = out = StringIO.new
33
+ @conn.expects(:query).with(instance_of(String)).returns(nil).times(2)
34
+ @conn.expects(:affected_rows).returns(0).times(2)
33
35
  @ctx.safe_eval('reload', nil, out)
34
36
  assert_match(/loaded: .+?example.rsqlrc/, out.string)
35
37
  ensure
@@ -100,15 +102,18 @@ class TestEvalContext < Test::Unit::TestCase
100
102
  out.string = ''
101
103
  val = @ctx.safe_eval('desc :cleanup_example', nil, out)
102
104
  assert_equal('', err.string)
103
- assert_equal('DROP TEMPORARY TABLE IF EXISTS #{@rsql_table}', out.string.strip)
105
+ assert_match(
106
+ /^\s*\[.+\/example.rsqlrc:\d+\]\s+DROP TEMPORARY TABLE IF EXISTS \#\{@rsql_table\}\s*$/,
107
+ out.string)
104
108
 
105
109
  out.string = ''
106
110
  val = @ctx.safe_eval('desc :to_report', nil, out)
107
- lines = out.string.split($/)
108
- assert_match(/^register .+ do$/, lines[0])
111
+ lines = out.string.split($/).select{|l|l.any?}
112
+ assert_match(/^\[.+\/example.rsqlrc:\d+\]$/, lines[0])
113
+ assert_match(/^register .+ do$/, lines[1])
109
114
  assert_match(/^\s+puts/, lines[-2])
110
115
  assert_match(/^end$/, lines[-1])
111
- assert_equal(12, lines.size)
116
+ assert_equal(13, lines.size)
112
117
  end
113
118
 
114
119
  def test_complete
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsql
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 3
10
- version: 0.2.3
9
+ - 4
10
+ version: 0.2.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brad Robel-Forrest
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-28 00:00:00 Z
18
+ date: 2011-10-09 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: net-ssh