activerecord-jdbc-adapter 0.9.3-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/History.txt +248 -0
  2. data/LICENSE.txt +21 -0
  3. data/Manifest.txt +125 -0
  4. data/README.txt +218 -0
  5. data/Rakefile +10 -0
  6. data/lib/active_record/connection_adapters/cachedb_adapter.rb +1 -0
  7. data/lib/active_record/connection_adapters/derby_adapter.rb +13 -0
  8. data/lib/active_record/connection_adapters/h2_adapter.rb +13 -0
  9. data/lib/active_record/connection_adapters/hsqldb_adapter.rb +13 -0
  10. data/lib/active_record/connection_adapters/informix_adapter.rb +1 -0
  11. data/lib/active_record/connection_adapters/jdbc_adapter.rb +640 -0
  12. data/lib/active_record/connection_adapters/jdbc_adapter_spec.rb +26 -0
  13. data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -0
  14. data/lib/active_record/connection_adapters/mysql_adapter.rb +13 -0
  15. data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -0
  16. data/lib/active_record/connection_adapters/postgresql_adapter.rb +13 -0
  17. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +13 -0
  18. data/lib/generators/jdbc/jdbc_generator.rb +9 -0
  19. data/lib/jdbc_adapter.rb +27 -0
  20. data/lib/jdbc_adapter/jdbc.rake +121 -0
  21. data/lib/jdbc_adapter/jdbc_adapter_internal.jar +0 -0
  22. data/lib/jdbc_adapter/jdbc_cachedb.rb +33 -0
  23. data/lib/jdbc_adapter/jdbc_db2.rb +203 -0
  24. data/lib/jdbc_adapter/jdbc_derby.rb +430 -0
  25. data/lib/jdbc_adapter/jdbc_firebird.rb +109 -0
  26. data/lib/jdbc_adapter/jdbc_hsqldb.rb +218 -0
  27. data/lib/jdbc_adapter/jdbc_informix.rb +147 -0
  28. data/lib/jdbc_adapter/jdbc_mimer.rb +141 -0
  29. data/lib/jdbc_adapter/jdbc_mssql.rb +337 -0
  30. data/lib/jdbc_adapter/jdbc_mysql.rb +236 -0
  31. data/lib/jdbc_adapter/jdbc_oracle.rb +377 -0
  32. data/lib/jdbc_adapter/jdbc_postgre.rb +498 -0
  33. data/lib/jdbc_adapter/jdbc_sqlite3.rb +384 -0
  34. data/lib/jdbc_adapter/jdbc_sybase.rb +50 -0
  35. data/lib/jdbc_adapter/missing_functionality_helper.rb +87 -0
  36. data/lib/jdbc_adapter/rake_tasks.rb +10 -0
  37. data/lib/jdbc_adapter/tsql_helper.rb +60 -0
  38. data/lib/jdbc_adapter/version.rb +5 -0
  39. data/lib/pg.rb +4 -0
  40. data/rails_generators/jdbc_generator.rb +15 -0
  41. data/rails_generators/templates/config/initializers/jdbc.rb +7 -0
  42. data/rails_generators/templates/lib/tasks/jdbc.rake +8 -0
  43. data/rakelib/compile.rake +23 -0
  44. data/rakelib/package.rake +90 -0
  45. data/rakelib/rails.rake +41 -0
  46. data/rakelib/test.rake +76 -0
  47. data/src/java/jdbc_adapter/JdbcAdapterInternalService.java +53 -0
  48. data/src/java/jdbc_adapter/JdbcConnectionFactory.java +36 -0
  49. data/src/java/jdbc_adapter/JdbcDerbySpec.java +293 -0
  50. data/src/java/jdbc_adapter/JdbcMySQLSpec.java +134 -0
  51. data/src/java/jdbc_adapter/MssqlRubyJdbcConnection.java +71 -0
  52. data/src/java/jdbc_adapter/PostgresRubyJdbcConnection.java +55 -0
  53. data/src/java/jdbc_adapter/RubyJdbcConnection.java +1162 -0
  54. data/src/java/jdbc_adapter/SQLBlock.java +27 -0
  55. data/src/java/jdbc_adapter/Sqlite3RubyJdbcConnection.java +41 -0
  56. data/test/abstract_db_create.rb +107 -0
  57. data/test/activerecord/connection_adapters/type_conversion_test.rb +31 -0
  58. data/test/activerecord/connections/native_jdbc_mysql/connection.rb +25 -0
  59. data/test/cachedb_simple_test.rb +6 -0
  60. data/test/db/cachedb.rb +9 -0
  61. data/test/db/db2.rb +9 -0
  62. data/test/db/derby.rb +14 -0
  63. data/test/db/h2.rb +11 -0
  64. data/test/db/hsqldb.rb +12 -0
  65. data/test/db/informix.rb +11 -0
  66. data/test/db/jdbc.rb +11 -0
  67. data/test/db/jndi_config.rb +30 -0
  68. data/test/db/logger.rb +3 -0
  69. data/test/db/mssql.rb +9 -0
  70. data/test/db/mysql.rb +10 -0
  71. data/test/db/oracle.rb +34 -0
  72. data/test/db/postgres.rb +9 -0
  73. data/test/db/sqlite3.rb +15 -0
  74. data/test/db2_simple_test.rb +10 -0
  75. data/test/derby_migration_test.rb +21 -0
  76. data/test/derby_multibyte_test.rb +12 -0
  77. data/test/derby_simple_test.rb +21 -0
  78. data/test/generic_jdbc_connection_test.rb +9 -0
  79. data/test/h2_simple_test.rb +6 -0
  80. data/test/has_many_through.rb +79 -0
  81. data/test/helper.rb +5 -0
  82. data/test/hsqldb_simple_test.rb +6 -0
  83. data/test/informix_simple_test.rb +48 -0
  84. data/test/jdbc_adapter/jdbc_db2_test.rb +26 -0
  85. data/test/jdbc_adapter/jdbc_sybase_test.rb +33 -0
  86. data/test/jdbc_common.rb +25 -0
  87. data/test/jndi_callbacks_test.rb +38 -0
  88. data/test/jndi_test.rb +35 -0
  89. data/test/manualTestDatabase.rb +191 -0
  90. data/test/minirunit.rb +109 -0
  91. data/test/minirunit/testConnect.rb +14 -0
  92. data/test/minirunit/testH2.rb +73 -0
  93. data/test/minirunit/testHsqldb.rb +73 -0
  94. data/test/minirunit/testLoadActiveRecord.rb +3 -0
  95. data/test/minirunit/testMysql.rb +83 -0
  96. data/test/minirunit/testRawSelect.rb +24 -0
  97. data/test/models/add_not_null_column_to_table.rb +12 -0
  98. data/test/models/auto_id.rb +18 -0
  99. data/test/models/data_types.rb +28 -0
  100. data/test/models/entry.rb +23 -0
  101. data/test/models/mixed_case.rb +20 -0
  102. data/test/models/reserved_word.rb +18 -0
  103. data/test/models/string_id.rb +18 -0
  104. data/test/models/validates_uniqueness_of_string.rb +19 -0
  105. data/test/mssql_simple_test.rb +6 -0
  106. data/test/mysql_db_create_test.rb +25 -0
  107. data/test/mysql_multibyte_test.rb +10 -0
  108. data/test/mysql_nonstandard_primary_key_test.rb +42 -0
  109. data/test/mysql_simple_test.rb +32 -0
  110. data/test/oracle_simple_test.rb +29 -0
  111. data/test/pick_rails_version.rb +3 -0
  112. data/test/postgres_db_create_test.rb +21 -0
  113. data/test/postgres_mixed_case_test.rb +19 -0
  114. data/test/postgres_nonseq_pkey_test.rb +40 -0
  115. data/test/postgres_reserved_test.rb +22 -0
  116. data/test/postgres_schema_search_path_test.rb +46 -0
  117. data/test/postgres_simple_test.rb +13 -0
  118. data/test/simple.rb +475 -0
  119. data/test/sqlite3_simple_test.rb +233 -0
  120. data/test/sybase_jtds_simple_test.rb +6 -0
  121. metadata +188 -0
@@ -0,0 +1,10 @@
1
+ require 'jdbc_common'
2
+ require 'db/db2'
3
+
4
+ class DB2SimpleTest < Test::Unit::TestCase
5
+ include SimpleTestMethods
6
+ end
7
+
8
+ class DB2HasManyThroughTest < Test::Unit::TestCase
9
+ include HasManyThroughMethods
10
+ end
@@ -0,0 +1,21 @@
1
+ require 'jdbc_common'
2
+ require 'db/derby'
3
+
4
+ class CreateDummies < ActiveRecord::Migration
5
+ def self.up
6
+ create_table :dummies, :force => true do |t|
7
+ t.string :year, :default => "", :null => false
8
+ end
9
+ add_index :dummies, :year, :unique => true
10
+ end
11
+
12
+ end
13
+
14
+ class DerbyQuotingTest < Test::Unit::TestCase
15
+ include FixtureSetup
16
+
17
+ def test_create_table_column_quoting_vs_keywords
18
+ CreateDummies.up
19
+ end
20
+
21
+ end
@@ -0,0 +1,12 @@
1
+ # To run this script, run the following in a mysql instance:
2
+ #
3
+ # drop database if exists weblog_development;
4
+ # create database weblog_development;
5
+ # grant all on weblog_development.* to blog@localhost;
6
+
7
+ require 'jdbc_common'
8
+ require 'db/derby'
9
+
10
+ class DerbyMultibyteTest < Test::Unit::TestCase
11
+ include MultibyteTestMethods
12
+ end
@@ -0,0 +1,21 @@
1
+ # To run this script, run the following in a mysql instance:
2
+ #
3
+ # drop database if exists weblog_development;
4
+ # create database weblog_development;
5
+ # grant all on weblog_development.* to blog@localhost;
6
+
7
+ require 'jdbc_common'
8
+ require 'db/derby'
9
+
10
+ class DerbySimpleTest < Test::Unit::TestCase
11
+ include SimpleTestMethods
12
+
13
+ # Check that a table-less VALUES(xxx) query (like SELECT works.
14
+ def test_values
15
+ value = nil
16
+ assert_nothing_raised do
17
+ value = ActiveRecord::Base.connection.send(:select_rows, "VALUES('ur', 'doin', 'it', 'right')")
18
+ end
19
+ assert_equal [['ur', 'doin', 'it', 'right']], value
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ require 'jdbc_common'
2
+ require 'db/jdbc'
3
+
4
+ class GenericJdbcConnectionTest < Test::Unit::TestCase
5
+ def test_connection_available_through_jdbc_adapter
6
+ ActiveRecord::Base.connection.execute("show databases");
7
+ assert ActiveRecord::Base.connected?
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ require 'jdbc_common'
2
+ require 'db/h2'
3
+
4
+ class H2SimpleTest < Test::Unit::TestCase
5
+ include SimpleTestMethods
6
+ end
@@ -0,0 +1,79 @@
1
+ class CreateRbac < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :role_assignments do |t|
4
+ t.column :role_id, :integer
5
+ t.column :user_id, :integer
6
+ end
7
+
8
+ create_table :roles do |t|
9
+ t.column :name, :string
10
+ t.column :description, :string
11
+ end
12
+
13
+ create_table :permission_groups do |t|
14
+ t.column :right_id, :integer
15
+ t.column :role_id, :integer
16
+ end
17
+
18
+ create_table :rights do |t|
19
+ t.column :name, :string
20
+ t.column :controller_name, :string
21
+ t.column :actions, :string
22
+ t.column :hours, :float, :null => false
23
+ end
24
+ end
25
+
26
+ def self.down
27
+ drop_table :role_assignments
28
+ drop_table :roles
29
+ drop_table :permission_groups
30
+ drop_table :rights
31
+ end
32
+ end
33
+
34
+ class Right < ActiveRecord::Base
35
+ has_many :permission_groups, :dependent => :destroy
36
+ has_many :roles, :through => :permission_groups
37
+ end
38
+
39
+ class Role < ActiveRecord::Base
40
+ has_many :permission_groups, :dependent => :destroy
41
+ has_many :rights, :through => :permission_groups
42
+ has_many :role_assignments, :dependent => :destroy
43
+ end
44
+
45
+ class PermissionGroup < ActiveRecord::Base
46
+ belongs_to :right
47
+ belongs_to :role
48
+ end
49
+
50
+ class RoleAssignment < ActiveRecord::Base
51
+ belongs_to :user
52
+ belongs_to :role
53
+ end
54
+
55
+ module HasManyThroughMethods
56
+ def setup
57
+ CreateRbac.up
58
+ end
59
+
60
+ def teardown
61
+ CreateRbac.down
62
+ end
63
+
64
+ def test_has_many_through
65
+ admin_role = Role.create( {:name => "Administrator", :description => "System defined super user - access to right and role management."} )
66
+ admin_role.save
67
+
68
+ assert_equal(0, admin_role.rights.sum(:hours))
69
+
70
+ role_rights = Right.create( {:name => "Administrator - Full Access To Roles", :actions => "*", :controller_name => "Admin::RolesController", :hours => 0} )
71
+ right_rights = Right.create( {:name => "Administrator - Full Access To Rights", :actions => "*", :controller_name => "Admin::RightsController", :hours => 1.5} )
72
+
73
+ admin_role.rights << role_rights
74
+ admin_role.rights << right_rights
75
+ admin_role.save
76
+
77
+ assert_equal(1.5, admin_role.rights.sum(:hours))
78
+ end
79
+ end
@@ -0,0 +1,5 @@
1
+ module Kernel
2
+ def find_executable?(name)
3
+ ENV['PATH'].split(File::PATH_SEPARATOR).detect {|p| File.executable?(File.join(p, name))}
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'jdbc_common'
2
+ require 'db/hsqldb'
3
+
4
+ class HsqldbSimpleTest < Test::Unit::TestCase
5
+ include SimpleTestMethods
6
+ end
@@ -0,0 +1,48 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # To run this script, run the following:
4
+ #
5
+ # CREATE DATABASE weblog_development;
6
+ #
7
+ # TODO: Finish the explanation.
8
+
9
+ require 'jdbc_common'
10
+ require 'db/informix'
11
+
12
+ class InformixSimpleTest < Test::Unit::TestCase
13
+ include SimpleTestMethods
14
+
15
+ # Informix does not like "= NULL".
16
+ def test_equals_null
17
+ Entry.create!(:title => "Foo")
18
+ entry = Entry.find(:first, :conditions => ["content = NULL"])
19
+ assert_equal "Foo", entry.title
20
+ end
21
+
22
+ # Informix does not like "!= NULL" or "<> NULL".
23
+ def test_not_equals_null
24
+ Entry.create!(:title => "Foo", :content => "Bar")
25
+ entry = Entry.find_by_title("Foo", :conditions => ["content != NULL"])
26
+ assert_equal "Foo", entry.title
27
+ entry = Entry.find_by_title("Foo", :conditions => ["content <> NULL"])
28
+ assert_equal "Foo", entry.title
29
+ end
30
+ end
31
+
32
+ class InformixMultibyteTest < Test::Unit::TestCase
33
+ include MultibyteTestMethods
34
+
35
+ # Overriding the included test since we can't create text fields via a
36
+ # simple insert in Informix.
37
+ def test_select_multibyte_string
38
+ Entry.create!(:title => 'テスト', :content => '本文')
39
+ entry = Entry.find(:first)
40
+ assert_equal "テスト", entry.title
41
+ assert_equal "本文", entry.content
42
+ assert_equal entry, Entry.find_by_title("テスト")
43
+ end
44
+ end
45
+
46
+ class InformixHasManyThroughTest < Test::Unit::TestCase
47
+ include HasManyThroughMethods
48
+ end
@@ -0,0 +1,26 @@
1
+ require 'java'
2
+ require 'lib/jdbc_adapter/jdbc_db2'
3
+ require 'test/unit'
4
+
5
+ class JdbcSpec::DB2Test < Test::Unit::TestCase
6
+ def setup
7
+ @inst = Object.new
8
+ @inst.extend JdbcSpec::DB2
9
+ @column = Object.new
10
+ class <<@column
11
+ attr_accessor :type
12
+ end
13
+ end
14
+
15
+ def test_quote_decimal
16
+ assert_equal %q{'123.45'}, @inst.quote("123.45")
17
+ @column.type = :decimal
18
+ assert_equal %q{123.45}, @inst.quote("123.45", @column), "decimal columns should not have quotes"
19
+ end
20
+
21
+ def test_primary_key_generation
22
+ @column.type = :primary_key
23
+ assert_equal 'int not null generated by default as identity (start with 1) primary key', @inst.modify_types({:string => {}, :integer => {}, :boolean => {}})[:primary_key]
24
+ end
25
+
26
+ end
@@ -0,0 +1,33 @@
1
+ require 'jdbc_common'
2
+ require 'jdbc_adapter'
3
+
4
+ class MockConnection
5
+
6
+ def adapter=( adapt )
7
+ end
8
+
9
+ end
10
+
11
+ module ActiveRecord
12
+ module ConnectionAdapters
13
+
14
+ class SybaseAdapterSelectionTest < Test::Unit::TestCase
15
+
16
+ def testJtdsSelectionUsingDialect()
17
+ config = {
18
+ :driver => 'net.sourceforge.jtds.Driver',
19
+ :dialect => 'sybase'
20
+ }
21
+ adapt = JdbcAdapter.new(MockConnection.new, nil, config)
22
+ assert adapt.kind_of?(JdbcSpec::Sybase), "Should be a sybase adapter"
23
+ end
24
+
25
+ def testJtdsSelectionNotUsingDialect
26
+ config = { :driver => 'net.sourceforge.jtds.Driver' }
27
+ adapt = JdbcAdapter.new(MockConnection.new, nil, config)
28
+ assert adapt.kind_of?(JdbcSpec::MsSQL), "Should be a MsSQL apdater"
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ # Simple method to reduce the boilerplate
2
+ def jruby?
3
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
4
+ end
5
+
6
+ require 'rubygems'
7
+ require 'pick_rails_version'
8
+ require 'jdbc_adapter' if jruby?
9
+ puts "Using activerecord version #{ActiveRecord::VERSION::STRING}"
10
+ puts "Specify version with AR_VERSION=={version} or RUBYLIB={path}"
11
+ require 'models/auto_id'
12
+ require 'models/entry'
13
+ require 'models/data_types'
14
+ require 'models/add_not_null_column_to_table'
15
+ require 'models/validates_uniqueness_of_string'
16
+ require 'models/string_id'
17
+ require 'simple'
18
+ require 'has_many_through'
19
+ require 'helper'
20
+ require 'test/unit'
21
+
22
+ # Comment/uncomment to enable logging to be loaded for any of the database adapters
23
+ # require 'db/logger'
24
+
25
+
@@ -0,0 +1,38 @@
1
+ require 'jdbc_common'
2
+
3
+ begin
4
+ require 'mocha'
5
+
6
+ class JndiConnectionPoolCallbacksTest < Test::Unit::TestCase
7
+ def setup
8
+ @connection = mock "JdbcConnection"
9
+ @connection.stubs(:jndi_connection?).returns(true)
10
+ @connection.stubs(:adapter=)
11
+ @logger = mock "logger"
12
+ @config = { :jndi => "jdbc/some_pool", :adapter => "mysql" }
13
+ Entry.connection_pool.disconnect!
14
+ assert !Entry.connection_pool.connected?
15
+ class << Entry.connection_pool; public :instance_variable_set; end
16
+ end
17
+
18
+ def teardown
19
+ @connection.stubs(:disconnect!)
20
+ Entry.connection_pool.disconnect!
21
+ end
22
+
23
+ def test_should_call_hooks_on_checkout_and_checkin
24
+ @connection.expects(:disconnect!)
25
+ @adapter = ActiveRecord::ConnectionAdapters::JdbcAdapter.new @connection, @logger, @config
26
+ Entry.connection_pool.instance_variable_set "@connections", [@adapter]
27
+
28
+ @connection.expects(:reconnect!)
29
+ Entry.connection_pool.checkout
30
+
31
+ @connection.expects(:disconnect!)
32
+ Entry.connection_pool.checkin @adapter
33
+ end
34
+ end
35
+
36
+ rescue LoadError
37
+ warn "mocha not found, disabling mocha-based tests"
38
+ end if ActiveRecord::Base.respond_to?(:connection_pool)
@@ -0,0 +1,35 @@
1
+ # In order to run these tests, you need to have a few things on your
2
+ # classpath. First, you're going to need the Sun File system
3
+ # context. You can get that here:
4
+ #
5
+ # http://java.sun.com/products/jndi/serviceproviders.html.
6
+ #
7
+ # Make sure that you put both the fscontext.jar and the
8
+ # providerutil.jar on your classpath.
9
+ #
10
+ # To support the connection pooling in the test, you'll need
11
+ # commons-dbcp, commons-pool, and commons-collections.
12
+ #
13
+ # Finally, you'll need the jdbc driver, which is derby, for this test.
14
+
15
+ require 'jdbc_common'
16
+ require 'db/jndi_config'
17
+
18
+ class DerbyJndiTest < Test::Unit::TestCase
19
+ include SimpleTestMethods
20
+ alias_method :setup_simple, :setup
21
+ def setup
22
+ ActiveRecord::Base.establish_connection({
23
+ :jndi => 'jdbc/derbydb',
24
+ :adapter => 'jdbc'})
25
+ logger = Logger.new('jndi_test.log')
26
+ logger.level = Logger::DEBUG
27
+ ActiveRecord::Base.logger = logger
28
+ setup_simple
29
+ end
30
+ end
31
+
32
+ at_exit {
33
+ require 'fileutils'
34
+ FileUtils.rm_rf 'derby-testdb'
35
+ }
@@ -0,0 +1,191 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ if ARGV.length < 2
4
+ $stderr.puts "syntax: #{__FILE__} [filename] [configuration-name]"
5
+ $stderr.puts " where filename points to a YAML database configuration file"
6
+ $stderr.puts " and the configuration name is in this file"
7
+ exit
8
+ end
9
+
10
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
11
+
12
+ require 'yaml'
13
+ require 'rubygems'
14
+ RAILS_CONNECTION_ADAPTERS = ['mysql', 'jdbc']
15
+ require 'active_record'
16
+
17
+ cfg = (File.open(ARGV[0]) {|f| YAML.load(f) })[ARGV[1]]
18
+
19
+ ActiveRecord::Base.establish_connection(cfg)
20
+
21
+ ActiveRecord::Schema.define do
22
+ drop_table :authors rescue nil
23
+ drop_table :author rescue nil
24
+
25
+ create_table :author, :force => true do |t|
26
+ t.column :name, :string, :null => false
27
+ end
28
+
29
+ # Exercise all types, and add_column
30
+ add_column :author, :description, :text
31
+ add_column :author, :descr, :string, :limit => 50
32
+ add_column :author, :age, :integer, :null => false, :default => 17
33
+ add_column :author, :weight, :float
34
+ add_column :author, :born, :datetime
35
+ add_column :author, :died, :timestamp
36
+ add_column :author, :wakeup_time, :time
37
+ add_column :author, :birth_date, :date
38
+ add_column :author, :private_key, :binary
39
+ add_column :author, :female, :boolean, :default => true
40
+
41
+ change_column :author, :descr, :string, :limit => 100 if /db2|derby/ !~ ARGV[1]
42
+ change_column_default :author, :female, false if /db2|derby|mssql|firebird/ !~ ARGV[1]
43
+ remove_column :author, :died if /db2|derby/ !~ ARGV[1]
44
+ rename_column :author, :wakeup_time, :waking_time if /db2|derby|mimer/ !~ ARGV[1]
45
+
46
+ add_index :author, :name, :unique if /db2/ !~ ARGV[1]
47
+ add_index :author, [:age,:female], :name => :is_age_female if /db2/ !~ ARGV[1]
48
+
49
+ remove_index :author, :name if /db2/ !~ ARGV[1]
50
+ remove_index :author, :name => :is_age_female if /db2/ !~ ARGV[1]
51
+
52
+ rename_table :author, :authors if /db2|firebird|mimer/ !~ ARGV[1]
53
+
54
+
55
+ create_table :products, :force => true do |t|
56
+ t.column :title, :string
57
+ t.column :description, :text
58
+ t.column :image_url, :string
59
+ end
60
+ add_column :products, :price, :float, :default => 0.0
61
+ create_table :orders, :force => true do |t|
62
+ t.column :name, :string
63
+ t.column :address, :text
64
+ t.column :email, :string
65
+ t.column :pay_type, :string, :limit => 10
66
+ end
67
+ create_table :line_items, :force => true do |t|
68
+ t.column :product_id, :integer, :null => false
69
+ t.column :order_id, :integer, :null => false
70
+ t.column :quantity, :integer, :null => false
71
+ t.column :total_price, :float, :null => false
72
+ end
73
+ end
74
+
75
+ class Author < ActiveRecord::Base;
76
+ set_table_name "author" if /db2|firebird|mimer/ =~ ARGV[1]
77
+ end
78
+
79
+ class Order < ActiveRecord::Base
80
+ has_many :line_items
81
+ end
82
+
83
+ class Product < ActiveRecord::Base
84
+ has_many :orders, :through => :line_items
85
+ has_many :line_items
86
+
87
+ def self.find_products_for_sale
88
+ find(:all, :order => "title")
89
+ end
90
+ end
91
+
92
+ class LineItem < ActiveRecord::Base
93
+ belongs_to :order
94
+ belongs_to :product
95
+ end
96
+
97
+ Product.create(:title => 'Pragmatic Project Automation',
98
+ :description =>
99
+ %{<p>
100
+ <em>Pragmatic Project Automation</em> shows you how to improve the
101
+ consistency and repeatability of your project's procedures using
102
+ automation to reduce risk and errors.
103
+ </p>
104
+ <p>
105
+ Simply put, we're going to put this thing called a computer to work
106
+ for you doing the mundane (but important) project stuff. That means
107
+ you'll have more time and energy to do the really
108
+ exciting---and difficult---stuff, like writing quality code.
109
+ </p>},
110
+ :image_url => '/images/auto.jpg',
111
+ :price => 29.95)
112
+
113
+
114
+ Product.create(:title => 'Pragmatic Version Control',
115
+ :description =>
116
+ %{<p>
117
+ This book is a recipe-based approach to using Subversion that will
118
+ get you up and
119
+ running quickly---and correctly. All projects need version control:
120
+ it's a foundational piece of any project's infrastructure. Yet half
121
+ of all project teams in the U.S. don't use any version control at all.
122
+ Many others don't use it well, and end up experiencing time-consuming problems.
123
+ </p>},
124
+ :image_url => '/images/svn.jpg',
125
+ :price => 28.50)
126
+
127
+ # . . .
128
+
129
+
130
+ Product.create(:title => 'Pragmatic Unit Testing (C#)',
131
+ :description =>
132
+ %{<p>
133
+ Pragmatic programmers use feedback to drive their development and
134
+ personal processes. The most valuable feedback you can get while
135
+ coding comes from unit testing.
136
+ </p>
137
+ <p>
138
+ Without good tests in place, coding can become a frustrating game of
139
+ "whack-a-mole." That's the carnival game where the player strikes at a
140
+ mechanical mole; it retreats and another mole pops up on the opposite side
141
+ of the field. The moles pop up and down so fast that you end up flailing
142
+ your mallet helplessly as the moles continue to pop up where you least
143
+ expect them.
144
+ </p>},
145
+ :image_url => '/images/utc.jpg',
146
+ :price => 27.75)
147
+
148
+
149
+
150
+
151
+ 1.times do
152
+ $stderr.print '.'
153
+ Author.destroy_all
154
+ Author.create(:name => "Arne Svensson", :age => 30)
155
+ if /db2|derby|mimer/ !~ ARGV[1]
156
+ Author.create(:name => "Pelle Gogolsson", :age => 15, :waking_time => Time.now, :private_key => "afbafddsfgsdfg")
157
+ else
158
+ Author.create(:name => "Pelle Gogolsson", :age => 15, :wakeup_time => Time.now, :private_key => "afbafddsfgsdfg")
159
+ end
160
+ Author.find(:first)
161
+ Author.find(:all)
162
+ arne = Author.find(:first)
163
+ arne.destroy
164
+
165
+ pelle = Author.find(:first)
166
+ pelle.name = "Pelle Sweitchon"
167
+ pelle.description = "dfsssdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
168
+ pelle.descr = "adsfasdf"
169
+ pelle.age = 79
170
+ pelle.weight = 57.6
171
+ pelle.born = Time.gm(1982,8,13,10,15,3,0)
172
+ pelle.female = false
173
+ pelle.save
174
+
175
+ prods = Product.find :all
176
+ order = Order.new(:name => "Dalai Lama", :address => "Great Road 32", :email => "abc@dot.com", :pay_type => "cash")
177
+ order.line_items << LineItem.new(:product => prods[0], :quantity => 3, :total_price => (prods[0].price * 3))
178
+ order.line_items << LineItem.new(:product => prods[2], :quantity => 1, :total_price => (prods[2].price))
179
+ order.save
180
+
181
+ puts "order: #{order.line_items.inspect}, with id: #{order.id} and name: #{order.name}"
182
+ end
183
+
184
+ ActiveRecord::Schema.define do
185
+ drop_table :line_items
186
+ drop_table :orders
187
+ drop_table :products
188
+
189
+
190
+ drop_table((/db2|firebird|mimer/=~ARGV[1]? :author : :authors ))
191
+ end