Class: Sequel::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/async.rb

Constant Summary collapse

STOCK_COUNT_OPTS =
{:select => ["COUNT(*)".lit], :order => nil}

Instance Method Summary collapse

Instance Method Details

#async_allObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/sequel/async.rb', line 121

def async_all
  async_fetch_rows(sql, :all) do |rows|
    if row_proc = @row_proc
      yield(rows.map{|r| row_proc.call(r) })
    else
      yield(rows)
    end
  end
  nil
end

#async_count(&cb) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/sequel/async.rb', line 132

def async_count &cb
  if options_overlap(COUNT_FROM_SELF_OPTS)
    from_self.async_count(&cb)
  else
    clone(STOCK_COUNT_OPTS).async_each{|r|
      yield r.is_a?(Hash) ? r.values.first.to_i : r.values.values.first.to_i
    }
  end
  nil
end

#async_delete(&cb) ⇒ Object



73
74
75
76
# File 'lib/sequel/async.rb', line 73

def async_delete &cb
  em_mysq.execute delete_sql, &cb
  nil
end

#async_eachObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/sequel/async.rb', line 110

def async_each
  async_fetch_rows(select_sql, :each) do |r|
    if row_proc = @row_proc
      yield row_proc.call(r)
    else
      yield r
    end
  end
  nil
end

#async_fetch_rows(sql, iter = :each) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sequel/async.rb', line 88

def async_fetch_rows sql, iter = :each
  em_mysq.raw(sql) do |m|
    r = m.result

    i = -1
    cols = r.fetch_fields.map{|f| [output_identifier(f.name), Sequel::MySQL::MYSQL_TYPES[f.type], i+=1]}
    @columns = cols.map{|c| c.first}
    rows = []
    while row = r.fetch_row
      h = {}
      cols.each{|n, p, i| v = row[i]; h[n] = (v && p) ? p.call(v) : v}
      if iter == :each
        yield h
      else
        rows << h
      end
    end
    yield rows if iter == :all
  end
  nil
end

#async_insert(*args, &cb) ⇒ Object



58
59
60
61
# File 'lib/sequel/async.rb', line 58

def async_insert *args, &cb
  em_mysq.insert insert_sql(*args), &cb
  nil
end

#async_insert_ignore(*args, &cb) ⇒ Object



63
64
65
66
# File 'lib/sequel/async.rb', line 63

def async_insert_ignore *args, &cb
  em_mysq.insert insert_ignore.insert_sql(*args), &cb
  nil
end

#async_multi_insert(*args, &cb) ⇒ Object



78
79
80
81
# File 'lib/sequel/async.rb', line 78

def async_multi_insert *args, &cb
  em_mysq.execute multi_insert_sql(*args).first, &cb
  nil
end

#async_multi_insert_ignore(*args, &cb) ⇒ Object



83
84
85
86
# File 'lib/sequel/async.rb', line 83

def async_multi_insert_ignore *args, &cb
  em_mysq.execute insert_ignore.multi_insert_sql(*args).first, &cb
  nil
end

#async_update(*args, &cb) ⇒ Object



68
69
70
71
# File 'lib/sequel/async.rb', line 68

def async_update *args, &cb
  em_mysq.update update_sql(*args), &cb
  nil
end

#em_mysqObject



54
55
56
# File 'lib/sequel/async.rb', line 54

def em_mysq
  EventedMysql.databases[db] || EventedMysql
end