The KirbyBase adapter does not need a "db driver", as KirbyBase is a pure-ruby DBMS. This adapter defines all the required functionality by executing direct method calls on a KirbyBase DB object.
Options (for database.yml):
- :connection_type — type of connection (local or client). Defaults to :local
- :host — If using KirbyBase in a client/server mode
- :port — If using KirbyBase in a client/server mode
- :path — Path to DB storage area. Defaults to /db/data
Note that Ackbar/KirbyBase support migrations/schema but not transactions.
- adapter_name
- add_column
- add_index
- change_column
- change_column_default
- columns
- create_table
- drop_table
- execute
- get_table
- indexes
- initialize_schema_information
- native_database_types
- new
- primary_key
- remove_column
- remove_index
- rename_column
- rename_table
- supports_migrations?
- tables
- update
| VERSION | = | '0.1.1' |
| Ackbar’s own version - i.e. the adapter version, not KirbyBase or Rails. | ||
| PRIMARY_KEY_TYPE | = | { :Calculated => 'recno', :DataType => :Integer } |
| [RW] | db |
[ show source ]
# File kirbybase_adapter.rb, line 101
101: def initialize(connect_type, host, port, path)
102: if connect_type == :local
103: FileUtils.mkdir_p(path) unless File.exists?(path)
104: end
105: @db = KirbyBase.new(connect_type, host, port, path)
106: end
[ show source ]
# File kirbybase_adapter.rb, line 108
108: def adapter_name
109: 'KirbyBase'
110: end
[ show source ]
# File kirbybase_adapter.rb, line 243
243: def add_column(table_name, column_name, type, options = {})
244: type = type.is_a?(Hash)? type : native_database_types[type]
245: type.merge!({:Required => true}) if options[:null] == false
246: type.merge!({:Default => options[:default]}) if options.has_key?(:default)
247: if type[:DataType] == :Boolean && type.has_key?(:Default)
248: type[:Default] = case type[:Default]
249: when true, false, nil then type[:Default]
250: when String then type[:Default] == 't' ? true : false
251: when Integer then type[:Default] == 1 ? true : false
252: end
253: end
254: db.get_table(table_name.to_sym).add_column(column_name.to_sym, type)
255: end
[ show source ]
# File kirbybase_adapter.rb, line 231
231: def add_index(table_name, column_name, options = {})
232: db.get_table(table_name.to_sym).add_index( *Array(column_name).map{|c| c.to_sym} )
233: end
[ show source ]
# File kirbybase_adapter.rb, line 274
274: def change_column(table_name, column_name, type, options = {})
275: column_name = column_name.to_sym
276: tbl = db.get_table(table_name.to_sym)
277: tbl.change_column_type(column_name, native_database_types[type][:DataType])
278: tbl.change_column_required(column_name, options[:null] == false)
279: if options.has_key?(:default)
280: change_column_default(table_name, column_name, options[:default])
281: end
282: end
[ show source ]
# File kirbybase_adapter.rb, line 261
261: def change_column_default(table_name, column_name, default)
262: column_name = column_name.to_sym
263: tbl = db.get_table(table_name.to_sym)
264: if columns(table_name.to_sym).detect{|col| col.name.to_sym == column_name}.type == :boolean
265: default = case default
266: when true, false, nil then default
267: when String then default == 't' ? true : false
268: when Integer then default == 1 ? true : false
269: end
270: end
271: tbl.change_column_default_value(column_name.to_sym, default)
272: end
[ show source ]
# File kirbybase_adapter.rb, line 207
207: def columns(table_name, name=nil)
208: tbl = db.get_table(table_name.to_sym)
209: tbl.field_names.zip(tbl.field_defaults, tbl.field_types, tbl.field_requireds).map do |fname, fdefault, ftype, frequired|
210: KirbyBaseColumn.new(fname.to_s, fdefault, ftype.to_s.downcase, !frequired)
211: end
212: end
[ show source ]
# File kirbybase_adapter.rb, line 157
157: def create_table(name, options = {})
158: table_definition = TableDefinition.new(self)
159: table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false
160:
161: yield table_definition
162:
163: if options[:force]
164: drop_table(name) rescue nil
165: end
166:
167: # Todo: Handle temporary tables (options[:temporary]), creation options (options[:options])
168: defns = table_definition.columns.inject([]) do |defns, col|
169: if col.type == PRIMARY_KEY_TYPE
170: defns
171: else
172: kb_col_options = native_database_types[col.type]
173: kb_col_options = kb_col_options.merge({ :Required => true }) if not col.null.nil? and not col.null
174: kb_col_options = kb_col_options.merge({ :Default => col.default }) unless col.default.nil?
175: kb_col_options[:Default] = true if kb_col_options[:DataType] == :Boolean && kb_col_options[:Default]
176: # the :limit option is ignored - meaningless considering the ruby types and KB storage
177: defns << [col.name.to_sym, kb_col_options]
178: end
179: end
180: begin
181: db.create_table(name.to_sym, *defns.flatten)
182: rescue => detail
183: raise "Create table '#{name}' failed: #{detail}"
184: end
185: end
[ show source ]
# File kirbybase_adapter.rb, line 187
187: def drop_table(table_name)
188: db.drop_table(table_name.to_sym)
189: end
NOT SUPPORTED !!!
[ show source ]
# File kirbybase_adapter.rb, line 139
139: def execute(*params)
140: raise ArgumentError, "SQL not supported! (#{params.inspect})" unless block_given?
141: yield db
142: end
Returns a handle on a KBTable object
[ show source ]
# File kirbybase_adapter.rb, line 151
151: def get_table(table_name)
152: db.get_table(table_name.to_sym)
153: end
[ show source ]
# File kirbybase_adapter.rb, line 214
214: def indexes(table_name, name = nil)
215: table = db.get_table(table_name.to_sym)
216: indices = table.field_names.zip(table.field_indexes)
217: indices_to_columns = indices.inject(Hash.new{|h,k| h[k] = Array.new}) {|hsh, (fn, ind)| hsh[ind] << fn.to_s unless ind.nil?; hsh}
218: indices_to_columns.map do |ind, cols|
219: # we're not keeping the names anywhere (KB doesn't store them), so we
220: # just give the default name
221: IndexDefinition.new(table_name, "#{table_name}_#{cols[0]}_index", false, cols)
222: end
223: end
[ show source ]
# File kirbybase_adapter.rb, line 191
191: def initialize_schema_information
192: begin
193: schema_info_table = create_table(ActiveRecord::Migrator.schema_info_table_name.to_sym) do |t|
194: t.column :version, :integer
195: end
196: schema_info_table.insert(0)
197: rescue ActiveRecord::StatementInvalid, RuntimeError
198: # RuntimeError is raised by KB if the table already exists
199: # Schema has been intialized
200: end
201: end
Translates all the ActiveRecord simplified SQL types to KirbyBase (Ruby) Types. Also allows KB specific types like :YAML.
[ show source ]
# File kirbybase_adapter.rb, line 121
121: def native_database_types #:nodoc
122: {
123: :primary_key => PRIMARY_KEY_TYPE,
124: :string => { :DataType => :String },
125: :text => { :DataType => :String }, # are KBMemos better?
126: :integer => { :DataType => :Integer },
127: :float => { :DataType => :Float },
128: :datetime => { :DataType => :Time },
129: :timestamp => { :DataType => :Time },
130: :time => { :DataType => :Time },
131: :date => { :DataType => :Date },
132: :binary => { :DataType => :String }, # are KBBlobs better?
133: :boolean => { :DataType => :Boolean },
134: :yaml => { :DataType => :YAML }
135: }
136: end
[ show source ]
# File kirbybase_adapter.rb, line 225
225: def primary_key(table_name)
226: raise ArgumentError, "#primary_key called"
227: column = table_structure(table_name).find {|field| field['pk'].to_i == 1}
228: column ? column['name'] : nil
229: end
[ show source ]
# File kirbybase_adapter.rb, line 257
257: def remove_column(table_name, column_name)
258: db.get_table(table_name.to_sym).drop_column(column_name.to_sym)
259: end
[ show source ]
# File kirbybase_adapter.rb, line 235
235: def remove_index(table_name, options={})
236: db.get_table(table_name.to_sym).drop_index(options) rescue nil
237: end
[ show source ]
# File kirbybase_adapter.rb, line 284
284: def rename_column(table_name, column_name, new_column_name)
285: db.get_table(table_name.to_sym).rename_column(column_name.to_sym, new_column_name.to_sym)
286: end
[ show source ]
# File kirbybase_adapter.rb, line 239
239: def rename_table(name, new_name)
240: db.rename_table(name.to_sym, new_name.to_sym)
241: end
[ show source ]
# File kirbybase_adapter.rb, line 112
112: def supports_migrations?
113: true
114: end
[ show source ]
# File kirbybase_adapter.rb, line 203
203: def tables(name = nil)
204: db.tables.map {|t| t.to_s}
205: end
NOT SUPPORTED !!!
[ show source ]
# File kirbybase_adapter.rb, line 145
145: def update(*params)
146: raise ArgumentError, "SQL not supported! (#{params.inspect})" unless block_given?
147: yield db
148: end