Permalink
Browse files

added primary_key

  • Loading branch information...
1 parent c2d60e8 commit 7ee3351b7ba014466bcb4f8614bc4f7e2af6f82b @klarrimore committed Mar 27, 2012
@@ -8,17 +8,25 @@ def initialize(args = {})
def create_table!(table_template)
@connection.create_table! table_template.name.to_sym do
+ primary_key table_template.primary_key if table_template.primary_key
table_template.columns.each do |column|
column column.name.to_sym, column.type
end
+ table_template.indexes.each do |idx|
+ index idx.to_sym
+ end
end
end
def create_table(table_template)
@connection.create_table? table_template.name.to_sym do
+ primary_key table_template.primary_key if table_template.primary_key
table_template.columns.each do |column|
column column.name.to_sym, column.type
end
+ table_template.indexes.each do |idx|
+ index idx.to_sym
+ end
end
end
@@ -27,4 +35,4 @@ def table_exists?(name)
end
end
-end
+end
@@ -1,16 +1,25 @@
module DisposableDB
class TableTemplate
- attr_accessor :name, :columns
+ attr_accessor :name, :columns, :primary_key
def initialize(name, columns = [])
@name = name
@columns = columns
+ @indexes = indexes
end
def add_column(column)
@columns << column
end
+ def set_primary_key(pk)
+ @primary_key = pk
+ end
+
+ def indexes
+ @columns.select{ |c| c.indexed == true }.collect{ |c| c.name }
+ end
+
class Column
attr_accessor :name, :type, :indexed
@@ -31,4 +40,4 @@ def initialize(name, type, options = {})
end
end
-end
+end
@@ -1,3 +1,3 @@
module DisposableDB
- VERSION = '0.0.2' unless defined? DisposableDB::VERSION
+ VERSION = '0.0.3' unless defined? DisposableDB::VERSION
end
View
@@ -20,5 +20,16 @@
d.create_table tt
d.table_exists?(tt.name).should be_true
end
+
+ it "can create table with primary_key" do
+ tt = TableTemplate.new('foo2', [TableTemplate::Column.new('t1', Integer)])
+ tt.set_primary_key :id
+
+ d = Database.new
+ d.table_exists?(tt.name).should be_false
+ d.create_table tt
+ d.table_exists?(tt.name).should be_true
+ d.connection[:foo2].columns.include?(:id).should be_true
+ end
end
@@ -19,5 +19,13 @@
tt.columns.length.should == columns.length
end
+
+ it "can set primary key" do
+ tt = TableTemplate.new('foo')
+
+ tt.set_primary_key :id
+
+ tt.primary_key == :id
+ end
end

0 comments on commit 7ee3351

Please sign in to comment.