Rubernate 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,11 +23,11 @@ module DBI
23
23
 
24
24
  TimeType = ::DBI::Timestamp
25
25
 
26
- # Represents factory for +Runtime+ implementors.
27
- class RuntimeFactory
28
- # Accepts Runtime impl. class, database url, user name, and user password
29
- def initialize klass, db_url, db_user, db_password
30
- @klass, @db_url, @db_user, @db_password = klass, db_url, db_user, db_password
26
+ # Holds configuration information and serves as factory for Runtime objects.
27
+ class Configuration
28
+ # Accepts Runtime impl class, db initializer class, database url, user name, and user password
29
+ def initialize klass, init, db_url, db_user, db_password
30
+ @klass, @init, @db_url, @db_user, @db_password = klass, init, db_url, db_user, db_password
31
31
  # @params = {'AutoCommit' => false} #TODO: refine
32
32
  end
33
33
 
@@ -43,6 +43,11 @@ module DBI
43
43
  ::DBI.connect(@db_url, @db_user, @db_password, @params) { |dbh| yield dbh }
44
44
  end
45
45
 
46
+ # Intialize databae.
47
+ def init_db
48
+ connect {|dbh| @init.init_db dbh}
49
+ end
50
+
46
51
  def to_s
47
52
  "runtime impl: #{@klass}, db_url: #{@db_url}, db_user: #{@db_user}, db_password: #{@db_password}"
48
53
  end
@@ -54,10 +59,10 @@ module DBI
54
59
  class Runtime < Rubernate::Runtime
55
60
  include DBI
56
61
 
57
- # Parameters are deleted automatically with corresponding r_object records
58
- #DELETE_PARAMS = <<-SQL
59
- # DELETE FROM R_PARAMS WHERE OBJECT_PK = ?
60
- # SQL
62
+ # In some implementations parameters are deleted automatically with corresponding r_object records
63
+ DELETE_PARAMS = <<-SQL
64
+ DELETE FROM R_PARAMS WHERE OBJECT_PK = ?
65
+ SQL
61
66
  DELETE_PARAMS_FOR = <<-SQL
62
67
  DELETE FROM R_PARAMS WHERE OBJECT_PK IN
63
68
  SQL
@@ -23,6 +23,6 @@ module DBI
23
23
  object.peer.dirty = true
24
24
  object.primary_key
25
25
  end
26
- end
26
+ end
27
27
  end
28
28
  end
@@ -9,13 +9,7 @@ module DBI
9
9
  CREATE_PEER = <<-SQL
10
10
  INSERT INTO R_OBJECTS (OBJECT_PK, OBJECT_CLASS) values (?, ?)
11
11
  SQL
12
-
13
- # TODO: make it work
14
- def initialize dbh
15
- super
16
- dbh.do "ALTER session SET nls_date_format = 'YYYY/MM/DD HH24:MI:SS'"
17
- end
18
-
12
+
19
13
  # Creates record in r_objects for specified object
20
14
  def create object
21
15
  object.peer = Rubernate::Peer.new
@@ -31,7 +31,7 @@ module Memory
31
31
  end
32
32
  end
33
33
 
34
- class RuntimeFactory
34
+ class Configuration
35
35
  attr_accessor :database
36
36
  def initialize
37
37
  @pk, @database = 1001, {}
@@ -0,0 +1,71 @@
1
+ require 'erb'
2
+
3
+ module Rubernate
4
+ module DBI
5
+ class MySqlInit
6
+ CREATE_R_OBJECTS = %q{
7
+ CREATE TABLE R_OBJECTS (
8
+ OBJECT_PK INTEGER(20) PRIMARY KEY AUTO_INCREMENT,
9
+ OBJECT_CLASS VARCHAR(100) NOT NULL) ENGINE=InnoDB;
10
+ }.gsub(/^ /, '')
11
+
12
+ CREATE_R_PARAMS = %q{
13
+ CREATE TABLE R_PARAMS (
14
+ OBJECT_PK INTEGER(20) NOT NULL,
15
+ NAME VARCHAR(100) NOT NULL,
16
+ FLAGS INTEGER(5) NOT NULL,
17
+ INT_VALUE INTEGER(20),
18
+ FLT_VALUE FLOAT,
19
+ STR_VALUE VARCHAR(255),
20
+ DAT_VALUE DATETIME,
21
+ REF_VALUE INTEGER(20),
22
+ CONSTRAINT R_PARAM_FK FOREIGN KEY (OBJECT_PK) REFERENCES R_OBJECTS(OBJECT_PK) ON DELETE CASCADE,
23
+ CONSTRAINT R_REF_FK FOREIGN KEY (REF_VALUE) REFERENCES R_OBJECTS(OBJECT_PK) ON DELETE CASCADE) ENGINE=InnoDB;
24
+ }.gsub(/^ /, '')
25
+ CREATE_INDEX_O_PK_CLASS = %q{
26
+ CREATE INDEX R_O_PK_CLASS ON R_OBJECTS (OBJECT_PK ASC, OBJECT_CLASS);
27
+ }.gsub(/^ /, '')
28
+ CREATE_INDEX_P_PK_NAME = %q{
29
+ CREATE INDEX R_P_PK_NAME ON R_PARAMS (OBJECT_PK ASC, NAME);
30
+ }.gsub(/^ /, '')
31
+
32
+ TEMPLATE = %q{
33
+ #
34
+ # Creates Rubernate tables for MySQL database.
35
+ # Copyright (C) 2006 Andrey Ryabov <andrey_ryabov@bk.ru>
36
+ #
37
+
38
+ # Uncomment following if you want to create database named rubernate_db
39
+ # CREATE DATABASE RUBERNATE_DB;
40
+ # USE RUBERNATE_DB;
41
+
42
+ # Create r_objects table <%= CREATE_R_OBJECTS %>
43
+ # Create r_params table <%= CREATE_R_PARAMS %>
44
+ # Create index on r_objects <%= CREATE_INDEX_O_PK_CLASS %>
45
+ # Create index on r_params <%= CREATE_INDEX_P_PK_NAME %>
46
+ #
47
+ # End
48
+ #
49
+ }.gsub(/^ /, '')
50
+
51
+ # Prints initialization script and tryes to initialize databaes database if dbh is given.
52
+ def self.init_db dbh=nil
53
+ puts ERB.new(TEMPLATE).result(binding)
54
+ if dbh
55
+ puts "\nInitializing database ... "
56
+ puts "Creation of r_objects - #{exec_ddl dbh, CREATE_R_OBJECTS}"
57
+ puts "Creation of r_params - #{exec_ddl dbh, CREATE_R_PARAMS}"
58
+ puts "Creation of index on r_objects - #{exec_ddl dbh, CREATE_INDEX_O_PK_CLASS}"
59
+ puts "Creation of index on r_params - #{exec_ddl dbh, CREATE_INDEX_P_PK_NAME}"
60
+ end
61
+ end
62
+
63
+ def self.exec_ddl dbh, stat
64
+ dbh.do stat
65
+ 'done'
66
+ rescue Exception => e
67
+ "failed: #{e}"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,88 @@
1
+ require 'erb'
2
+
3
+ module Rubernate
4
+ module DBI
5
+ class OracleInit
6
+ CREATE_R_OBJECTS = %q{
7
+ CREATE TABLE R_OBJECTS (
8
+ OBJECT_PK NUMBER(20) PRIMARY KEY,
9
+ OBJECT_CLASS VARCHAR2(100) NOT NULL)
10
+ }.gsub(/^ /, '')
11
+
12
+ CREATE_R_PARAMS = %q{
13
+ CREATE TABLE R_PARAMS (
14
+ OBJECT_PK NUMBER(20) NOT NULL,
15
+ NAME VARCHAR2(100) NOT NULL,
16
+ FLAGS NUMBER(5) NOT NULL,
17
+ INT_VALUE NUMBER(20),
18
+ FLT_VALUE FLOAT,
19
+ STR_VALUE VARCHAR2(1000),
20
+ DAT_VALUE DATE,
21
+ REF_VALUE NUMBER(20),
22
+ CONSTRAINT R_PARAM_FK FOREIGN KEY (OBJECT_PK) REFERENCES R_OBJECTS(OBJECT_PK) ON DELETE CASCADE,
23
+ CONSTRAINT R_REF_FK FOREIGN KEY (REF_VALUE) REFERENCES R_OBJECTS(OBJECT_PK) ON DELETE CASCADE)
24
+ }.gsub(/^ /, '')
25
+ CREATE_INDEX_O_PK_CLASS = %q{
26
+ CREATE INDEX R_O_PK_CLASS ON R_OBJECTS (OBJECT_PK ASC, OBJECT_CLASS)
27
+ }.gsub(/^ /, '')
28
+ CREATE_INDEX_P_PK_NAME = %q{
29
+ CREATE INDEX R_P_PK_NAME ON R_PARAMS (OBJECT_PK ASC, NAME)
30
+ }.gsub(/^ /, '')
31
+ CREATE_R_PK_SEQUENCE = %q{
32
+ CREATE SEQUENCE R_PK_SEQUENCE START WITH 1001 INCREMENT BY 1
33
+ }.gsub(/^ /, '')
34
+
35
+ TEMPLATE = %q{
36
+ /**
37
+ * Creates Rubernate tables for Oracle database.
38
+ * Copyright (C) 2006 Andrey Ryabov <andrey_ryabov@bk.ru>
39
+ */
40
+
41
+ /**
42
+ * Create r_objects table
43
+ */ <%= CREATE_R_OBJECTS %>/
44
+
45
+ /**
46
+ * Create r_params table
47
+ */ <%= CREATE_R_PARAMS %>/
48
+
49
+ /**
50
+ * Create index on r_objects
51
+ */ <%= CREATE_INDEX_O_PK_CLASS %>/
52
+
53
+ /**
54
+ * Create index on r_params
55
+ */ <%= CREATE_INDEX_P_PK_NAME %>/
56
+
57
+ /**
58
+ * Create primary key sequence
59
+ */ <%= CREATE_R_PK_SEQUENCE %>/
60
+
61
+
62
+ /**
63
+ * End
64
+ */
65
+ }.gsub(/^ /, '')
66
+
67
+ # Prints initialization script and tryes to initialize databaes database if dbh is given.
68
+ def self.init_db dbh=nil
69
+ puts ERB.new(TEMPLATE).result(binding)
70
+ if dbh
71
+ puts "\nInitializing database ... "
72
+ puts "Creation of r_objects - #{exec_ddl dbh, CREATE_R_OBJECTS}"
73
+ puts "Creation of r_params - #{exec_ddl dbh, CREATE_R_PARAMS}"
74
+ puts "Creation of index on r_objects - #{exec_ddl dbh, CREATE_INDEX_O_PK_CLASS}"
75
+ puts "Creation of index on r_params - #{exec_ddl dbh, CREATE_INDEX_P_PK_NAME}"
76
+ puts "Creation of pk sequence - #{exec_ddl dbh, CREATE_R_PK_SEQUENCE}"
77
+ end
78
+ end
79
+
80
+ def self.exec_ddl dbh, stat
81
+ dbh.do stat
82
+ 'done'
83
+ rescue Exception => e
84
+ "failed: #{e}"
85
+ end
86
+ end
87
+ end
88
+ end
data/lib/rubernate.rb CHANGED
@@ -33,6 +33,8 @@ require 'rubernate/queries'
33
33
  require 'rubernate/impl/memory'
34
34
  require 'rubernate/impl/dbimysql'
35
35
  require 'rubernate/impl/dbioracle'
36
+ require 'rubernate/init/init_mysql'
37
+ require 'rubernate/init/init_oracle'
36
38
 
37
39
 
38
40
 
@@ -44,11 +46,11 @@ module Rubernate
44
46
 
45
47
  # Rubernate core singelton methods.
46
48
  class << self
47
- # Allows you setup Rubernate by yours implementaion of RuntimeFactory.
49
+ # Allows you setup Rubernate by yours implementaion of Configuration.
48
50
  # See also method config
49
- def runtime_factory= factory
50
- Log.info "Rubernate configured by #{factory}"
51
- @runtime_factory = factory
51
+ def configuration= config
52
+ Log.info "Rubernate configured by #{config}"
53
+ @factory = config
52
54
  end
53
55
 
54
56
  # Performs configuration of Rubernate.
@@ -57,12 +59,17 @@ module Rubernate
57
59
  # * user - databases user name
58
60
  # * password - databases users password
59
61
  def config db, url, user, password
60
- impl = case db
61
- when :mysql: DBI::MySqlRuntime
62
- when :oracle: DBI::OracleRuntime
62
+ impl, init = case db
63
+ when :mysql: [DBI::MySqlRuntime, DBI::MySqlInit]
64
+ when :oracle: [DBI::OracleRuntime, DBI::OracleInit]
63
65
  else raise "Database #{db} not supported"
64
66
  end
65
- self.runtime_factory = DBI::RuntimeFactory.new impl, url, user, password
67
+ self.configuration = DBI::Configuration.new impl, init, url, user, password
68
+ end
69
+
70
+ # Intializes database - creates necessary tables, sequences and indices.
71
+ def init_db
72
+ @factory.init_db
66
73
  end
67
74
 
68
75
  # Returns Runtime object associated with current session
@@ -78,7 +85,7 @@ module Rubernate
78
85
 
79
86
  # Begins new Rubernate session
80
87
  def session
81
- runtime = @runtime_factory.create
88
+ runtime = @factory.create
82
89
  Thread.current[:Rubernate] = runtime
83
90
  runtime.begin
84
91
  return runtime unless block_given?
data/tests/config.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # Set to true to run tests or Oracle
2
- $run_oracle_tests = false
2
+ $run_oracle_tests = true
3
3
 
4
4
  ORA_RUNTIME_CLASS = Rubernate::DBI::OracleRuntime
5
5
  ORA_DB_URL = 'dbi:OCI8:dbg91'
@@ -7,7 +7,7 @@ ORA_DB_USER = 'netcracker65'
7
7
  ORA_DB_PWD = 'netcracker65'
8
8
 
9
9
  # Set to true to run tests on MySQL
10
- $run_mysql_tests = false
10
+ $run_mysql_tests = true
11
11
 
12
12
  MYSQL_RUNTIME_CLASS = Rubernate::DBI::MySqlRuntime
13
13
  MYSQL_DB_URL = 'dbi:Mysql:rubernate_db:localhost'
@@ -68,9 +68,9 @@ module Rubernate
68
68
  include FixtureClasses
69
69
 
70
70
  def setup
71
- @factory = Rubernate::Memory::RuntimeFactory.new
71
+ @factory = Rubernate::Memory::Configuration.new
72
72
  @runtime = @factory.create
73
- Rubernate.runtime_factory = @factory
73
+ Rubernate.configuration = @factory
74
74
  Callbacks.clear
75
75
  end
76
76
 
@@ -13,7 +13,7 @@ module DBI::GenericTests
13
13
  include FixtureClasses
14
14
 
15
15
  def init rt_class, db_url, db_user, db_password
16
- @factory = RuntimeFactory.new rt_class, db_url, db_user, db_password
16
+ @factory = Configuration.new rt_class, nil, db_url, db_user, db_password
17
17
  @factory.connect {|dbh|
18
18
  dbh.do 'delete from r_params'
19
19
  dbh.do 'delete from r_objects'
@@ -403,13 +403,13 @@ module GenericRuntimeTests
403
403
  include FixtureClasses
404
404
 
405
405
  def init rt_class, db_url, db_user, db_password
406
- @factory = RuntimeFactory.new rt_class, db_url, db_user, db_password
406
+ @factory = Configuration.new rt_class, nil, db_url, db_user, db_password
407
407
  @factory.connect {|dbh|
408
408
  dbh.do 'delete from r_params'
409
409
  dbh.do 'delete from r_objects'
410
410
  dbh.commit
411
411
  }
412
- Rubernate.runtime_factory = @factory
412
+ Rubernate.configuration = @factory
413
413
  Thread.current[:on_save] = Thread.current[:on_load] = nil
414
414
  end
415
415
 
@@ -9,9 +9,9 @@ class Rubernate::Memory::RuntimeTest < Test::Unit::TestCase
9
9
  include FixtureClasses
10
10
 
11
11
  def setup
12
- @factory = RuntimeFactory.new
12
+ @factory = Configuration.new
13
13
  @runtime = @factory.create
14
- Rubernate.runtime_factory = @factory
14
+ Rubernate.configuration = @factory
15
15
  end
16
16
 
17
17
  # Runtime Interface Tests
@@ -14,9 +14,9 @@ class Rubernate::RubernateTest < Test::Unit::TestCase
14
14
  include Rubernate::DBI
15
15
 
16
16
  def setup
17
- @factory = Rubernate::Memory::RuntimeFactory.new
17
+ @factory = Rubernate::Memory::Configuration.new
18
18
  @runtime = @factory.create
19
- Rubernate.runtime_factory = @factory
19
+ Rubernate.configuration = @factory
20
20
  Thread.current[:on_save] = Thread.current[:on_load] = nil
21
21
  end
22
22
 
@@ -244,9 +244,9 @@ class Rubernate::BaseRuntimeTest < Test::Unit::TestCase
244
244
  include FixtureClasses
245
245
 
246
246
  def setup
247
- @factory = Rubernate::Memory::RuntimeFactory.new
247
+ @factory = Rubernate::Memory::Configuration.new
248
248
  @runtime = @factory.create
249
- Rubernate.runtime_factory = @factory
249
+ Rubernate.configuration = @factory
250
250
  Thread.current[:on_save] = Thread.current[:on_load] = nil
251
251
  end
252
252
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: Rubernate
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2006-03-07 00:00:00 +03:00
6
+ version: 0.1.2
7
+ date: 2006-03-10 00:00:00 +03:00
8
8
  summary: Object-oriented storage for Ruby objects based on relational database model
9
9
  require_paths:
10
10
  - lib
@@ -28,13 +28,12 @@ cert_chain:
28
28
  authors:
29
29
  - Andrey Ryabov
30
30
  files:
31
- - db/mysql.sql
32
- - db/oracle.sql
33
31
  - lib/rubernate
34
32
  - lib/rubernate.rb
35
33
  - lib/rubernate/callbacks.rb
36
34
  - lib/rubernate/entity.rb
37
35
  - lib/rubernate/impl
36
+ - lib/rubernate/init
38
37
  - lib/rubernate/mixins.rb
39
38
  - lib/rubernate/peer.rb
40
39
  - lib/rubernate/queries.rb
@@ -43,6 +42,8 @@ files:
43
42
  - lib/rubernate/impl/dbimysql.rb
44
43
  - lib/rubernate/impl/dbioracle.rb
45
44
  - lib/rubernate/impl/memory.rb
45
+ - lib/rubernate/init/init_mysql.rb
46
+ - lib/rubernate/init/init_oracle.rb
46
47
  - tests/all_tests.rb
47
48
  - tests/config.rb
48
49
  - tests/README
data/db/mysql.sql DELETED
@@ -1,36 +0,0 @@
1
- /**
2
- * Creates Rubernate tables for MySQL database.
3
- * Copyright (C) 2006 Andrey Ryabov <andrey_ryabov@bk.ru>
4
- */
5
-
6
- # Creates database named rubernate_db change name and uncomment if necessary
7
- # CREATE DATABASE RUBERNATE_DB;
8
- # USE RUBERNATE_DB;
9
-
10
-
11
- DROP TABLE IF EXISTS R_PARAMS;
12
- DROP TABLE IF EXISTS R_OBJECTS;
13
-
14
- CREATE TABLE R_OBJECTS (
15
- OBJECT_PK INTEGER(20) PRIMARY KEY AUTO_INCREMENT,
16
- OBJECT_CLASS VARCHAR(100) NOT NULL) ENGINE=InnoDB;
17
-
18
- CREATE TABLE R_PARAMS (
19
- OBJECT_PK INTEGER(20) NOT NULL,
20
- NAME VARCHAR(100) NOT NULL,
21
- FLAGS INTEGER(5) NOT NULL,
22
- INT_VALUE INTEGER(20),
23
- FLT_VALUE FLOAT,
24
- STR_VALUE VARCHAR(255),
25
- DAT_VALUE DATETIME,
26
- REF_VALUE INTEGER(20),
27
- CONSTRAINT R_PARAM_FK FOREIGN KEY (OBJECT_PK) REFERENCES R_OBJECTS(OBJECT_PK) ON DELETE CASCADE,
28
- CONSTRAINT R_REF_FK FOREIGN KEY (REF_VALUE) REFERENCES R_OBJECTS(OBJECT_PK) ON DELETE CASCADE) ENGINE=InnoDB;
29
-
30
- CREATE INDEX R_O_PK_CLASS ON R_OBJECTS (OBJECT_PK ASC, OBJECT_CLASS);
31
- CREATE INDEX R_P_PK_NAME ON R_PARAMS (OBJECT_PK ASC, NAME);
32
-
33
-
34
- /**
35
- * End
36
- */
data/db/oracle.sql DELETED
@@ -1,30 +0,0 @@
1
- /**
2
- * Creates Rubernate tables for Oracle database.
3
- * Copyright (C) 2006 Andrey Ryabov <andrey_ryabov@bk.ru>
4
- */
5
-
6
- CREATE TABLE R_OBJECTS (
7
- OBJECT_PK NUMBER(20) PRIMARY KEY,
8
- OBJECT_CLASS VARCHAR2(100) NOT NULL)
9
- /
10
- CREATE TABLE R_PARAMS (
11
- OBJECT_PK NUMBER(20) NOT NULL,
12
- NAME VARCHAR2(100) NOT NULL,
13
- FLAGS NUMBER(5) NOT NULL,
14
- INT_VALUE NUMBER(20),
15
- FLT_VALUE FLOAT,
16
- STR_VALUE VARCHAR2(1000),
17
- DAT_VALUE DATE,
18
- REF_VALUE NUMBER(20),
19
- CONSTRAINT R_PARAM_FK FOREIGN KEY (OBJECT_PK) REFERENCES R_OBJECTS(OBJECT_PK) ON DELETE CASCADE,
20
- CONSTRAINT R_REF_FK FOREIGN KEY (REF_VALUE) REFERENCES R_OBJECTS(OBJECT_PK) ON DELETE CASCADE)
21
- /
22
- CREATE INDEX R_O_PK_CLASS ON R_OBJECTS (OBJECT_PK ASC, OBJECT_CLASS)
23
- /
24
- CREATE INDEX R_P_PK_NAME ON R_PARAMS (OBJECT_PK ASC, NAME)
25
- /
26
- CREATE SEQUENCE R_PK_SEQUENCE START WITH 1001 INCREMENT BY 1
27
- /
28
- /**
29
- * End
30
- */