Skip to content

Commit

Permalink
Return nil instead of 0 as key length for indexes without a key length.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Brito committed Aug 5, 2011
1 parent 107dac6 commit 4817229
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java
Expand Up @@ -39,6 +39,7 @@
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.RubyNil;
import org.jruby.RubyString;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
Expand Down Expand Up @@ -142,9 +143,10 @@ public Object call(Connection connection) throws SQLException {
if (lastIndex != null) {
String columnName = caseConvertIdentifierForRails(metadata, rs.getString("column_name"));
int length = rs.getInt("sub_part");
boolean lengthIsNull = rs.wasNull();

lastIndex.callMethod(context, "columns").callMethod(context, "<<", RubyString.newUnicodeString(runtime, columnName));
lastIndex.callMethod(context, "lengths").callMethod(context, "<<", runtime.newFixnum(length));
lastIndex.callMethod(context, "lengths").callMethod(context, "<<", lengthIsNull ? runtime.getNil() : runtime.newFixnum(length));
}
}

Expand Down
15 changes: 11 additions & 4 deletions test/mysql_index_length_test.rb
Expand Up @@ -5,8 +5,10 @@ class MySQLIndexLengthDBSetup < ActiveRecord::Migration
def self.up
execute <<-SQL
CREATE TABLE index_length_test (
int_column INT,
text_column TEXT,
second_text_column TEXT,
INDEX ix_int (int_column),
INDEX ix_length_text (text_column(255))
)
SQL
Expand All @@ -28,10 +30,8 @@ def teardown
end

def test_index_length
indexes = @connection.indexes('index_length_test')
assert_equal 1, indexes.count

index = indexes.first
index = @connection.indexes('index_length_test').find { |idx| idx.name == 'ix_length_text' }
assert_not_nil index
assert_equal "index_length_test", index.table
assert_equal "ix_length_text", index.name
assert !index.unique
Expand All @@ -48,4 +48,11 @@ def test_add_index
assert_equal ['text_column', 'second_text_column'], index.columns
assert_equal [32, 64], index.lengths
end

def test_index_without_length
index = @connection.indexes('index_length_test').find { |idx| idx.name == 'ix_int' }
assert_not_nil index
assert_equal ['int_column'], index.columns
assert_equal [nil], index.lengths
end
end

0 comments on commit 4817229

Please sign in to comment.