module SybaseDefinitions def view( view_name ) return View.new( view_name ) end def table( table_name ) return Table.new( table_name ) end def int_column( column_name ) return ColumnDefinition.new( column_name, "int" ) end def varchar_column( column_name ) return ColumnDefinition.new( column_name, "varchar" ) end def char_column( column_name ) return ColumnDefinition.new( column_name, "char" ) end def datetime_column( column_name ) return ColumnDefinition.new( column_name, "datetime" ) end def numeric_column( column_name ) return ColumnDefinition.new( column_name, "numeric" ) end def bit_column( column_name ) return ColumnDefinition.new( column_name, "bit" ) end # helper classes class View def initialize( view_name ) @view_name = view_name #puts $connection.columns( @table_name ).columns.each_hash{ |key,value| puts "Key: #{key}, value: #{value}"} end def exists? fail( "View #{@view_name} does not exist" ) unless get_user_views.include? @view_name end end class Table def initialize( table_name ) @table_name = table_name #puts $connection.columns( @table_name ).columns.each_hash{ |key,value| puts "Key: #{key}, value: #{value}"} end def exists? fail( "Table #{@table_name} does not exist" ) unless get_user_tables.include? @table_name end def contains_data?( map ) if( count( @table_name, map ) == 0 ) fail( "#{@table_name} does not contain data" ) end return true end def does_not_contain_data?( map ) if( count( @table_name, map ) != 0 ) fail( "#{@table_name} does contain data" ) end return true end def contains_columns?( list_of_expected_columns_definitions ) list_of_expected_columns_definitions.each do |expected_column| contains_column?( expected_column.to_hash ) end end def has_index?( index_name ) $db.indexes( @table_name ).each_hash do |map,value| return true if map["INDEX_NAME"].eql?( index_name ) end fail "#{@table_name} does not have index '#{index_name}'" end def has_dropped_index?( column_name ) $db.indexes( @table_name ).each_hash do |map,value| fail "#{@table_name} does contain index '#{column_name}'" if map["INDEX_NAME"].eql?( column_name ) end return true end def has_primary_key_on?( column_name ) $db.primary_keys( @table_name ).each_hash do |map,value| return true if map["COLUMN_NAME"].eql?( column_name ) end fail "#{@table_name} does not have a primary key on '#{column_name}'" end def is_referenced_by_foreign_key?( referring_table, column_name ) $db.foreign_keys( @table_name ).each_hash do |map,value| #p map return true if (map["FKCOLUMN_NAME"].eql?( column_name ) && map["FKTABLE_NAME"].eql?( referring_table ) ) end fail "#{@table_name} does not have a foreign key on '#{column_name}'" end def has_row_count?( expected_count ) actual_count = count( @table_name ) if( expected_count != count( @table_name ) ) fail( "Number of colums are not equal,\nExpected: #{expected_count}\nActual: #{actual_count}") end return self end private def contains_column?( expected_column_details ) actual_column = column( expected_column_details[:name] ) type_match?(expected_column_details, actual_column ) nullability_match?(expected_column_details, actual_column ) column_size_match?(expected_column_details, actual_column ) return !actual_column.exists? end def type_match?( expected_column, actual_column) return do_they_match?( expected_column[:type], actual_column.type, "Type", expected_column[:name]) end def nullability_match?( expected_column, actual_column) return do_they_match?( expected_column[:nullable], actual_column.nullable?, "Nullability", expected_column[:name]) end def column_size_match?(expected_column, actual_column) return do_they_match?( expected_column[:column_size], actual_column.column_size, "Column size", expected_column[:name]) end def column( column_name ) $db.columns( @table_name ).each_hash do |map,value| return Column.new( map ) if map["COLUMN_NAME"].eql?( column_name ) end end def do_they_match?( expected, actual, error_type, column_name ) if( !expected.nil? ) if( !expected.eql?( actual ) ) fail( "#{error_type} mismatch for column #{column_name}, \nExpected: #{expected}\nActual: #{actual}") end end return true end end class Column def initialize( column_map ) @map = column_map end def type return @map["TYPE_NAME"] end def nullable? return @map["IS_NULLABLE"].eql?( "YES" ) end def column_size return @map["COLUMN_SIZE"] end def exists? return !@map.nil? end end class ColumnDefinition attr_accessor :name, :type def initialize( name, type ) @name, @type = name, type end def to_hash result = {} result[:name] = @name unless @name.nil? result[:type] = @type unless @type.nil? result[:nullable] = @nullable unless @nullable.nil? result[:column_size] = @size unless @size.nil? return result end def with_size( size ) @size = size return self end def is_nullable @nullable = true return self end def is_not_nullable @nullable = false return self end def with_identity @type += " identity"; return self end end end