Guest User

Untitled

a guest
Mar 1st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. Index: test/simple.rb
  2. ===================================================================
  3. --- test/simple.rb (revision 318)
  4. +++ test/simple.rb (working copy)
  5. @@ -91,6 +91,7 @@
  6. assert_equal ["updated_on"], indexes.first.columns
  7. end
  8. end
  9. +
  10. def test_dumping_schema
  11. require 'active_record/schema_dumper'
  12. @connection.add_index :entries, :title
  13. @@ -106,5 +107,10 @@
  14. test = AutoId.create('value' => '')
  15. assert_nil AutoId.find(test.id).value
  16. end
  17. +
  18. + def test_invalid
  19. + e = Entry.new(:title => @title, :content => @content, :rating => ' ')
  20. + p e.valid?
  21. + end
  22.  
  23. end
  24. Index: lib/active_record/connection_adapters/jdbc_adapter.rb
  25. ===================================================================
  26. --- lib/active_record/connection_adapters/jdbc_adapter.rb (revision 318)
  27. +++ lib/active_record/connection_adapters/jdbc_adapter.rb (working copy)
  28. @@ -94,7 +94,7 @@
  29. lambda {|r| r['type_name'] =~ /^time$/i},
  30. lambda {|r| r['type_name'] =~ /^datetime$/i}],
  31. :date => [ lambda {|r| Jdbc::Types::DATE == r['data_type']},
  32. - lambda {|r| r['type_name'] =~ /^datetime$/i}],
  33. + lambda {|r| r['type_name'] =~ /^date$/i}],
  34. :binary => [ lambda {|r| [Jdbc::Types::LONGVARBINARY,Jdbc::Types::BINARY,Jdbc::Types::BLOB].include?(r['data_type'])},
  35. lambda {|r| r['type_name'] =~ /^blob/i},
  36. lambda {|r| r['type_name'] =~ /sub_type 0$/i}, # For FireBird
  37. @@ -104,8 +104,6 @@
  38. lambda {|r| r['type_name'] =~ /^bool/i},
  39. lambda {|r| r['type_name'] =~ /^tinyint$/i},
  40. lambda {|r| r['type_name'] =~ /^decimal$/i}],
  41. - :decimal => [ lambda {|r| Jdbc::Types::DECIMAL == r['data_type']},
  42. - lambda {|r| r['type_name'] =~ /^decimal$/i}]
  43. }
  44.  
  45. def initialize(types)
  46. @@ -220,7 +218,7 @@
  47. results = metadata.getColumns(nil, nil, table_name, nil)
  48. columns = []
  49. unmarshal_result(results).each do |col|
  50. - columns << ActiveRecord::ConnectionAdapters::JdbcColumn.new(@config,col['column_name'].downcase, col['column_def'],
  51. + columns << ActiveRecord::ConnectionAdapters::JdbcColumn.new(@config,col['column_name'], col['column_def'],
  52. "#{col['type_name']}(#{col['column_size']})", col['is_nullable'] != 'NO')
  53. end
  54. columns
  55. @@ -377,7 +375,7 @@
  56. column_types << metadata.getColumnType(i)
  57. column_scale << metadata.getScale(i)
  58. end
  59. -
  60. +
  61. results = []
  62.  
  63. # take all rows if block not supplied
  64. @@ -389,7 +387,11 @@
  65. if row_filter.call(resultset)
  66. row = {}
  67. 1.upto(column_count) do |i|
  68. + begin
  69. row[column_names[i].downcase] = convert_jdbc_type_to_ruby(i, column_types[i], column_scale[i], resultset)
  70. + rescue
  71. + puts $!, column_types[i]
  72. + end
  73. end
  74. results << row
  75. end
  76. @@ -422,13 +424,20 @@
  77. results
  78. end
  79.  
  80. - def to_ruby_time(java_date)
  81. - if java_date
  82. - tm = java_date.getTime
  83. + def to_ruby_time(java_time)
  84. + if java_time
  85. + tm = java_time.getTime
  86. Time.at(tm / 1000, (tm % 1000) * 1000)
  87. end
  88. end
  89.  
  90. + def to_ruby_date(java_date)
  91. + if java_date
  92. + # FIXME: This should use Calendar
  93. + Date.new(java_date.year+1900, java_date.month, java_date.day)
  94. + end
  95. + end
  96. +
  97. def convert_jdbc_type_to_ruby(row, type, scale, resultset)
  98. if scale != 0 && type != Jdbc::Types::TIMESTAMP
  99. decimal = resultset.getString(row)
  100. @@ -438,9 +447,12 @@
  101. when Jdbc::Types::CHAR, Jdbc::Types::VARCHAR, Jdbc::Types::LONGVARCHAR, Jdbc::Types::CLOB
  102. value = resultset.getString(row)
  103. resultset.wasNull ? nil : value
  104. - when Jdbc::Types::NUMERIC, Jdbc::Types::BIGINT, Jdbc::Types::DECIMAL
  105. - value = resultset.getLong(row)
  106. + when Jdbc::Types::NUMERIC, Jdbc::Types::BIGINT
  107. + value = resultset.getLong(row)
  108. resultset.wasNull ? nil : value
  109. + when Jdbc::Types::DECIMAL
  110. + value = BigDecimal.new(resultset.getBigDecimal(row))
  111. + resultset.wasNull ? nil : value
  112. when Jdbc::Types::SMALLINT, Jdbc::Types::INTEGER
  113. value = resultset.getInt(row)
  114. resultset.wasNull ? nil : value
  115. @@ -451,13 +463,19 @@
  116. value = resultset.getDouble(row)
  117. resultset.wasNull ? nil : value
  118. when Jdbc::Types::TIMESTAMP
  119. - value = to_ruby_time(resultset.getTimestamp(row))
  120. - resultset.wasNull ? nil : value
  121. + # FIXME: This should not be a catchall and it should move this to mysql since it
  122. + # is catching non-existent date 0000-00:00:00
  123. + begin
  124. + value = to_ruby_time(resultset.getTimestamp(row))
  125. + resultset.wasNull ? nil : value
  126. + rescue java.sql.SQLException
  127. + nil
  128. + end
  129. when Jdbc::Types::TIME
  130. value = to_ruby_time(resultset.getTime(row))
  131. resultset.wasNull ? nil : value
  132. when Jdbc::Types::DATE
  133. - value = to_ruby_time(resultset.getDate(row))
  134. + value = to_ruby_date(resultset.getDate(row))
  135. resultset.wasNull ? nil : value
  136. when Jdbc::Types::LONGVARBINARY, Jdbc::Types::BLOB, Jdbc::Types::BINARY, Jdbc::Types::VARBINARY
  137. value = resultset.getString(row)
  138. @@ -590,7 +608,7 @@
  139. end
  140.  
  141. def columns(table_name, name = nil)
  142. - @connection.columns(table_name)
  143. + @connection.columns(table_name.to_s)
  144. end
  145.  
  146. def tables
Add Comment
Please, Sign In to add comment