Class: ActiveRecord::ConnectionAdapters::DatastoreAdapter::DB
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::DatastoreAdapter::DB
- Defined in:
- lib/active_record/connection_adapters/datastore_adapter.rb
Instance Method Summary collapse
- #add_column(table_name, column_name, type, options = {}) ⇒ Object (also: #change_column)
- #add_index(table_name, column_name, options = {}) ⇒ Object
- #columns(table_name, name = nil) ⇒ Object
- #create_table(tname, fields) ⇒ Object
- #delete_query(q) ⇒ Object
- #drop_table(tname, options = {}) ⇒ Object
-
#initialize(config) ⇒ DB
constructor
A new instance of DB.
- #insert_query(q) ⇒ Object
- #primary_key(tname) ⇒ Object
- #remove_index(table_name, column_name) ⇒ Object
- #rename_table(tname, ntname) ⇒ Object
- #save_indexes ⇒ Object
- #save_schema ⇒ Object
- #select_query(q, options = {}) ⇒ Object
- #tables ⇒ Object
- #update_query(q, values = nil) ⇒ Object
Constructor Details
#initialize(config) ⇒ DB
Returns a new instance of DB.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 138 def initialize( config ) @config = { :database => 'database.yaml', :index => 'index.yaml', :namespace => 'dev' }.merge( config ) if( @config[:database] and File.exist? @config[:database] ) @tables = YAML.load( File.open( @config[:database], "r" ) ) else @tables = {} end if( @config[:index] and File.exist? @config[:index] ) @indexes = YAML.load( File.open( @config[:index], "r" ) ) @indexes = @indexes[:indexes] || [] else @indexes = [] end end |
Instance Method Details
#add_column(table_name, column_name, type, options = {}) ⇒ Object Also known as: change_column
172 173 174 175 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 172 def add_column(table_name, column_name, type, = {}) @tables[table_name][column_name] = { "type" => type.to_s, "default" => [:default], "null" => [:null] } save_schema end |
#add_index(table_name, column_name, options = {}) ⇒ Object
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 179 def add_index(table_name, column_name, = {}) table_name = table_name.to_s column_name = [ column_name ] unless column_name.is_a? Array column_name.map!{|c| c.to_s } inds = @indexes.find_all{|i| i["kind"] == table_name and i["properties"].map{|p| p["name"] } == column_name } if inds.empty? @indexes.push( { "kind" => table_name, "properties" => column_name.map{|c| { "name" => c } } } ) save_indexes end end |
#columns(table_name, name = nil) ⇒ Object
215 216 217 218 219 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 215 def columns( table_name, name = nil ) if tables[table_name] tables[table_name] end end |
#create_table(tname, fields) ⇒ Object
154 155 156 157 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 154 def create_table( tname, fields ) @tables[tname] = fields save_schema end |
#delete_query(q) ⇒ Object
262 263 264 265 266 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 262 def delete_query( q ) keys = [] q.each{|e| keys.push e.key } AppEngine::Datastore.delete keys end |
#drop_table(tname, options = {}) ⇒ Object
159 160 161 162 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 159 def drop_table( tname, = {} ) @tables.delete(tname) save_schema end |
#insert_query(q) ⇒ Object
246 247 248 249 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 246 def insert_query( q ) AppEngine::Datastore.put q q.key.id end |
#primary_key(tname) ⇒ Object
221 222 223 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 221 def primary_key( tname ) 'id' end |
#remove_index(table_name, column_name) ⇒ Object
190 191 192 193 194 195 196 197 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 190 def remove_index(table_name, column_name ) table_name = table_name.to_s column_name = column_name[:column] if column_name.is_a? Hash column_name = [ column_name ] unless column_name.is_a? Array column_name.map!{|c| c.to_s } inds = @indexes.delete_all{|i| i["kind"] == table_name and i["properties"].map{|p| p["name"] } == column_name } save_indexes end |
#rename_table(tname, ntname) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 164 def rename_table( tname, ntname ) value = @tables.delete(tname) if( value ) @tables[ntname] = value save_schema end end |
#save_indexes ⇒ Object
199 200 201 202 203 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 199 def save_indexes f = File.open( @config[:index], 'w' ) f.write( { "indexes" => @indexes }.to_yaml ) f.close end |
#save_schema ⇒ Object
205 206 207 208 209 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 205 def save_schema f = File.open( @config[:database], 'w' ) f.write( @tables.to_yaml ) f.close end |
#select_query(q, options = {}) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 225 def select_query( q, = {} ) output = [] if( [:empty] ) [] elsif( [:count] ) output.push( { "count" => q.count() } ) else t_name = q.kind p_key = primary_key( t_name ) column_list = columns( t_name ) q.fetch().each{|e| h = {} column_list.each{|n,opt| h[n] = ( n == p_key ? e.key.id : e[n] ) } output.push( h ) } end output end |
#tables ⇒ Object
211 212 213 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 211 def tables @tables end |
#update_query(q, values = nil) ⇒ Object
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/active_record/connection_adapters/datastore_adapter.rb', line 251 def update_query( q, values = nil ) if( values and values.size > 0 ) entities = [] q.each{|e| values.each{|k,v| e[k] = v } entities.push(e) } AppEngine::Datastore.put entities end end |