Skip to content

Commit

Permalink
Added some convenience methods for adding columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
posulliv committed Nov 23, 2010
1 parent d9a7cca commit 5b70d6c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lib/haru/ffihaildb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ module PureHailDB
attach_function :ib_col_copy_value, [ :pointer, :uint64, :pointer, :uint64 ], :void
attach_function :ib_tuple_write_u32, [ :pointer, :uint64, :uint32 ], DbError
attach_function :ib_tuple_read_u32, [ :pointer, :uint64, :pointer ], DbError
attach_function :ib_tuple_write_float, [ :pointer, :uint64, :float ], DbError
attach_function :ib_tuple_read_float, [ :pointer, :uint64, :pointer ], DbError
attach_function :ib_tuple_write_double, [ :pointer, :uint64, :double ], DbError
attach_function :ib_tuple_read_double, [ :pointer, :uint64, :pointer ], DbError

# miscellaneous functions
attach_function :ib_strerror, [ DbError ], :string
Expand Down
70 changes: 61 additions & 9 deletions lib/haru/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,34 @@ def initialize(col_name, col_type, col_attrs, col_size, col_num)
@num = col_num
end

def raw_type()
if @type == INT
:int
elsif @type == VARCHAR
:string
end
end

def insert_data(tuple_ptr, data)
case @type
when INT
check_return_code(PureHailDB.ib_tuple_write_u32(tuple_ptr,
@num,
data))
when VARCHAR
when FLOAT
check_return_code(PureHailDB.ib_tuple_write_float(tuple_ptr,
@num,
data))
when DOUBLE
check_return_code(PureHailDB.ib_tuple_write_double(tuple_ptr,
@num,
data))
when CHAR
p = FFI::MemoryPointer.from_string(data)
check_return_code(PureHailDB.ib_col_set_value(tuple_ptr,
@num,
p,
@size))
when BLOB
WHEN DECIMAL
when VARCHAR
p = FFI::MemoryPointer.from_string(data)
check_return_code(PureHailDB.ib_col_set_value(tuple_ptr,
@num,
p,
data.size))
end
end

Expand All @@ -45,6 +53,17 @@ def get_data(tuple_ptr)
res_ptr = FFI::MemoryPointer.new :uint32
check_return_code(PureHailDB.ib_tuple_read_u32(tuple_ptr, @num, res_ptr))
res_ptr.read_int()
when FLOAT
res_ptr = FFI::MemoryPointer.new :float
check_return_code(PureHailDB.ib_tuple_read_float(tuple_ptr, @num, res_ptr))
res_ptr.read_float()
when DOUBLE
res_ptr = FFI::MemoryPointer.new :double
check_return_code(PureHailDB.ib_tuple_read_double(tuple_ptr, @num, res_ptr))
res_ptr.read_double()
when CHAR
when BLOB
when DECIMAL
when VARCHAR
res_ptr = PureHailDB.ib_col_get_value(tuple_ptr, @num)
res_ptr.read_string()
Expand Down Expand Up @@ -77,6 +96,39 @@ def add_column(col_name, col_type, col_attrs, col_size)
col_size))
end

def add_integer_column(col_name)
c = Column.new(col_name, INT, UNSIGNED, 4, @columns.size)
@columns[col_name] = c
check_return_code(PureHailDB.ib_table_schema_add_col(@schema_ptr.read_pointer(),
col_name,
PureHailDB::ColumnType[INT],
PureHailDB::ColumnAttr[UNSIGNED],
0,
4))
end

def add_string_column(col_name, col_size)
c = Column.new(col_name, VARCHAR, NONE, col_size, @columns.size)
@columns[col_name] = c
check_return_code(PureHailDB.ib_table_schema_add_col(@schema_ptr.read_pointer(),
col_name,
PureHailDB::ColumnType[VARCHAR],
PureHailDB::ColumnAttr[NONE],
0,
col_size))
end

def add_fixed_size_string_column(col_name, col_size)
c = Column.new(col_name, CHAR, NONE, col_size, @columns.size)
@columns[col_name] = c
check_return_code(PureHailDB.ib_table_schema_add_col(@schema_ptr.read_pointer(),
col_name,
PureHailDB::ColumnType[CHAR],
PureHailDB::ColumnAttr[NONE],
0,
col_size))
end

def add_index()
@idx_ptr = FFI::MemoryPointer.new :pointer
check_return_code(PureHailDB.ib_table_schema_add_index(@schema_ptr.read_pointer(), "PRIMARY", @idx_ptr))
Expand Down
5 changes: 3 additions & 2 deletions test/test_basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ def setup
hail.set_log_file_path(@data_dir)
hail.startup
hail.create_database("padraig")
# create table t1 (c1 int, c2 varchar(32), primary key(c1))
t = Table.new("padraig", "t1")
t.add_column("c1", Haru::INT, Haru::UNSIGNED, 4)
t.add_column("c2", Haru::VARCHAR, Haru::NONE, 32)
t.add_integer_column("c1")
t.add_string_column("c2", 32)
t.add_index
t.add_index_column("c1")
tx = Transaction.new
Expand Down

0 comments on commit 5b70d6c

Please sign in to comment.