Class: ActiveRecord::ConnectionAdapters::AbstractTokyoCabinetAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/abstract_tokyocabinet_adapter.rb

Direct Known Subclasses

TokyoCabinetAdapter, TokyoTyrantAdapter

Instance Method Summary collapse

Constructor Details

#initialize(connection, logger, query_klass) ⇒ AbstractTokyoCabinetAdapter

Returns a new instance of AbstractTokyoCabinetAdapter.



45
46
47
48
# File 'lib/active_record/connection_adapters/abstract_tokyocabinet_adapter.rb', line 45

def initialize(connection, logger, query_klass)
  super(connection, logger)
  @query_klass = query_klass
end

Instance Method Details

#delete_sql(sql, name = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/active_record/connection_adapters/abstract_tokyocabinet_adapter.rb', line 132

def delete_sql(sql, name = nil)
  log(sql, name) do
    parsed_sql = ActiveTokyoCabinet::SQLParser.new(sql).parse

    tdbopen(parsed_sql[:table]) do |tdb|
      condition = parsed_sql[:condition] || []

      unless cond?(condition)
        [condition].flatten.each do |rkey| 
          unless tdb.out(rkey)
            ecode = tdb.ecode
            raise '%s: %s' % [tdb.errmsg(ecode), sql]
          end
        end
      else
        unless query(tdb, parsed_sql).searchout
          ecode = tdb.ecode
          raise '%s: %s' % [tdb.errmsg(ecode), sql]
        end
      end
    end
  end

  # XXX:
  return 1
end

#insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_record/connection_adapters/abstract_tokyocabinet_adapter.rb', line 76

def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
  pkey = nil

  log(sql, name) do
    parsed_sql = ActiveTokyoCabinet::SQLParser.new(sql).parse

    tdbopen(parsed_sql[:table]) do |tdb|
      pkey = tdb.genuid
      keys = parsed_sql[:column_list]
      vals = parsed_sql[:value_list]
      cols = {}

      keys.zip(vals).each do |k, v|
        k.split('.').last
        cols[k] = v.to_s
      end

      unless tdb.put(pkey, cols)
        ecode = tdb.ecode
        raise '%s: %s' % [tdb.errmsg(ecode), sql]
      end
    end
  end
  return pkey
end

#select(sql, name = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_record/connection_adapters/abstract_tokyocabinet_adapter.rb', line 54

def select(sql, name = nil)
  rows = nil

  log(sql, name) do
    parsed_sql = ActiveTokyoCabinet::SQLParser.new(sql).parse

    tdbopen(parsed_sql[:table], true) do |tdb|
      if (count = parsed_sql[:count])
        rows = [{count => rnum(tdb, parsed_sql)}]
      elsif block_given?
        rows = search(tdb, parsed_sql) do |_tdb, rkey, rcols|
          yield([_tdb, rkey, rcols])
        end
      else
        rows = search(tdb, parsed_sql)
      end
    end
  end

  return rows
end

#supports_count_distinct?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/active_record/connection_adapters/abstract_tokyocabinet_adapter.rb', line 50

def supports_count_distinct?
  false
end

#update_sql(sql, name = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/active_record/connection_adapters/abstract_tokyocabinet_adapter.rb', line 102

def update_sql(sql, name = nil)
  rownum = 0

  log(sql, name) do
    parsed_sql = ActiveTokyoCabinet::SQLParser.new(sql).parse

    tdbopen(parsed_sql[:table]) do |tdb|
      set_clause_list = parsed_sql[:set_clause_list]

      rkeys(tdb, parsed_sql).each do |rkey|
        rcols = tdb.get(rkey)

        set_clause_list.each do |k, v|
          k.split('.').last
          rcols[k] = v.to_s
        end

        rownum += 1

        unless tdb.put(rkey, rcols)
          ecode = tdb.ecode
          raise '%s: %s' % [tdb.errmsg(ecode), sql]
        end
      end
    end
  end

  return rownum
end