drysql 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,25 +5,43 @@ module ActiveRecord
5
5
  # This is abstract class serves as the template for table constraints from all
6
6
  # databases compatible with the ISO SQL:2003 information schema standard
7
7
  class AbstractTableConstraint
8
+
9
+ @@PRIMARY_KEY_TYPE = "PRIMARY KEY"
10
+ @@FOREIGN_KEY_TYPE = "FOREIGN KEY"
11
+ @@UNIQUE_KEY_TYPE = "UNIQUE"
12
+ @@CHECK_CONSTRAINT_TYPE = "CHECK"
13
+
14
+ attr_reader :constraint_catalog, :constraint_schema, :constraint_name, :constraint_type,
15
+ :table_catalog, :table_schema, :table_name, :column_name,
16
+ :referenced_table_catalog, :referenced_table_schema, :referenced_table_name, :referenced_column_name
17
+
18
+ attr_writer :member_of_composite
8
19
 
9
20
  def raise_subclass_responsibility_error
10
21
  raise NotImplementedError, "AbstractTableConstraint subclass #{self.class} did not implement this method"
11
22
  end
12
23
 
13
- alias :primary_key? :raise_subclass_responsibility_error
14
- alias :foreign_key? :raise_subclass_responsibility_error
15
- alias :unique_key? :raise_subclass_responsibility_error
16
- alias :component_of_unique_key? :raise_subclass_responsibility_error
17
- alias :is_member_of_composite? :raise_subclass_responsibility_error
18
- alias :constraint_schema :raise_subclass_responsibility_error
19
- alias :constraint_name :raise_subclass_responsibility_error
20
- alias :constraint_type :raise_subclass_responsibility_error
21
- alias :table_schema :raise_subclass_responsibility_error
22
- alias :table_name :raise_subclass_responsibility_error
23
- alias :column_name :raise_subclass_responsibility_error
24
- alias :referenced_table_schema :raise_subclass_responsibility_error
25
- alias :referenced_table_name :raise_subclass_responsibility_error
26
- alias :referenced_column_name :raise_subclass_responsibility_error
24
+ alias :initialize :raise_subclass_responsibility_error
25
+
26
+ def primary_key?
27
+ constraint_type == @@PRIMARY_KEY_TYPE
28
+ end
29
+
30
+ def foreign_key?
31
+ constraint_type == @@FOREIGN_KEY_TYPE
32
+ end
33
+
34
+ def component_of_unique_key?
35
+ constraint_type == @@UNIQUE_KEY_TYPE
36
+ end
37
+
38
+ def unique_key?
39
+ component_of_unique_key? and !is_member_of_composite?
40
+ end
41
+
42
+ def is_member_of_composite?
43
+ @member_of_composite ? @member_of_composite : false
44
+ end
27
45
 
28
46
  end
29
47
 
@@ -35,14 +35,6 @@ module ActiveRecord
35
35
  # A representation of a referential constraint or key in MySQL
36
36
  # Specifically: Primary, Foreign, Unique Key definitions
37
37
  class MysqlConstraint < AbstractTableConstraint
38
- @@PRIMARY_KEY_TYPE='PRIMARY KEY'
39
- @@FOREIGN_KEY_TYPE='FOREIGN KEY'
40
- @@UNIQUE_KEY_TYPE='UNIQUE'
41
-
42
- attr_reader :constraint_schema, :constraint_name, :constraint_type, :table_schema, :table_name, :column_name,
43
- :referenced_table_schema, :referenced_table_name, :referenced_column_name
44
-
45
- attr_writer :member_of_composite
46
38
 
47
39
  def initialize(constraint_catalog, constraint_schema, constraint_name, table_schema, table_name, constraint_type,
48
40
  column_name, referenced_table_schema, referenced_table_name, referenced_column_name)
@@ -58,27 +50,6 @@ module ActiveRecord
58
50
  @referenced_column_name = referenced_column_name
59
51
  end
60
52
 
61
- def primary_key?
62
- constraint_type == @@PRIMARY_KEY_TYPE
63
- end
64
-
65
- def foreign_key?
66
- constraint_type == @@FOREIGN_KEY_TYPE
67
- end
68
-
69
- def component_of_unique_key?
70
- constraint_type == @@UNIQUE_KEY_TYPE
71
- end
72
-
73
- def unique_key?
74
- component_of_unique_key? and !is_member_of_composite?
75
- end
76
-
77
- def is_member_of_composite?
78
- @member_of_composite ? @member_of_composite : false
79
- end
80
-
81
-
82
53
  end
83
54
 
84
55
  # The purpose of this extension to MysqlAdapter is to exploit the referential constraints offered in MySQL >=5.0, and
@@ -15,17 +15,7 @@ module ActiveRecord
15
15
 
16
16
 
17
17
  class PostgreSQLConstraint < AbstractTableConstraint
18
- @@PRIMARY_KEY_TYPE = "PRIMARY KEY"
19
- @@FOREIGN_KEY_TYPE = "FOREIGN KEY"
20
- @@UNIQUE_KEY_TYPE = "UNIQUE"
21
- @@CHECK_CONSTRAINT_TYPE = "CHECK"
22
-
23
- attr_reader :constraint_catalog, :constraint_schema, :constraint_name, :constraint_type,
24
- :table_catalog, :table_schema, :table_name, :column_name,
25
- :referenced_table_catalog, :referenced_table_schema, :referenced_table_name, :referenced_column_name
26
-
27
- attr_writer :member_of_composite
28
-
18
+
29
19
  def initialize(table_catalog, table_schema, table_name, column_name, constraint_catalog, constraint_schema,
30
20
  constraint_name, referenced_table_catalog, referenced_table_schema, referenced_table_name, constraint_type,
31
21
  referenced_column_name)
@@ -43,30 +33,6 @@ module ActiveRecord
43
33
  @referenced_column_name = referenced_column_name
44
34
  end
45
35
 
46
- def primary_key?
47
- constraint_type == @@PRIMARY_KEY_TYPE
48
- end
49
-
50
- def foreign_key?
51
- constraint_type == @@FOREIGN_KEY_TYPE
52
- end
53
-
54
- def check_constraint?
55
- constraint_type == @@CHECK_CONSTRAINT_TYPE
56
- end
57
-
58
- def component_of_unique_key?
59
- constraint_type == @@UNIQUE_KEY_TYPE
60
- end
61
-
62
- def unique_key?
63
- component_of_unique_key? and !is_member_of_composite?
64
- end
65
-
66
- def is_member_of_composite?
67
- @member_of_composite ? @member_of_composite : false
68
- end
69
-
70
36
  end
71
37
 
72
38
 
@@ -0,0 +1,66 @@
1
+ module ActiveRecord
2
+
3
+ module ConnectionAdapters
4
+
5
+ class SQLServerColumn < Column# :nodoc:
6
+ alias :generated? :identity
7
+
8
+ def default_specified?
9
+ !default.nil?
10
+ end
11
+
12
+ end
13
+
14
+ class SQLServerConstraint < AbstractTableConstraint
15
+
16
+ def initialize(table_catalog, table_schema, table_name, column_name,
17
+ referenced_table_catalog, referenced_table_schema, referenced_table_name, referenced_column_name,
18
+ constraint_catalog, constraint_schema, constraint_name, constraint_type)
19
+ @table_catalog = table_catalog
20
+ @table_schema = table_schema
21
+ @table_name = table_name
22
+ @column_name = column_name
23
+ @referenced_table_catalog = referenced_table_catalog
24
+ @referenced_table_schema = referenced_table_schema
25
+ @referenced_table_name = referenced_table_name
26
+ @referenced_column_name = referenced_column_name
27
+ @constraint_catalog = constraint_catalog
28
+ @constraint_schema = constraint_schema
29
+ @constraint_name = constraint_name
30
+ @constraint_type = constraint_type
31
+ end
32
+
33
+ end
34
+
35
+ class SQLServerAdapter < AbstractAdapter
36
+
37
+ def constraints(table_name, name = nil)#:nodoc:
38
+ constraints = []
39
+ sql = "select t2.*, t1.COLUMN_NAME, t3.UNIQUE_CONSTRAINT_CATALOG, t3.UNIQUE_CONSTRAINT_SCHEMA, t3.UNIQUE_CONSTRAINT_NAME, t4.table_catalog as REFERENCED_TABLE_CATALOG, t4.table_schema as REFERENCED_TABLE_SCHEMA, t4.table_name as REFERENCED_TABLE_NAME, t4.column_name as REFERENCED_COLUMN_NAME from ar.information_schema.constraint_column_usage as t1 inner join
40
+ ar.information_schema.table_constraints as t2 on (t2.constraint_name=t1.constraint_name AND t2.table_catalog=t1.table_catalog AND t2.table_schema=t1.table_schema AND t2.table_name=t1.table_name) left outer join
41
+ ar.information_schema.referential_constraints as t3 on (t3.constraint_name=t2.constraint_name AND t3.constraint_catalog=t2.constraint_catalog AND t3.constraint_schema=t2.constraint_schema) left outer join
42
+ ar.information_schema.constraint_column_usage as t4 on (t4.constraint_name=t3.unique_constraint_name AND t4.constraint_catalog=t3.unique_constraint_catalog AND t4.constraint_schema=t3.unique_constraint_schema)
43
+ where t4.table_name='#{table_name}' or t2.table_name='#{table_name}'"
44
+ results = select_all(sql, name)
45
+ constraint_name_hash = {}
46
+ results.each do |row|
47
+ constraints << SQLServerConstraint.new(row['TABLE_CATALOG'], row['TABLE_SCHEMA'], row['TABLE_NAME'], row['COLUMN_NAME'],
48
+ row['REFERENCED_TABLE_CATALOG'], row['REFERENCED_TABLE_SCHEMA'], row['REFERENCED_TABLE_NAME'], row['REFERENCED_COLUMN_NAME'],
49
+ row['CONSTRAINT_CATALOG'], row['CONSTRAINT_SCHEMA'], row['CONSTRAINT_NAME'], row['CONSTRAINT_TYPE'])
50
+ comparable_constraint_name = row['CONSTRAINT_NAME'].upcase
51
+ constraint_name_count = constraint_name_hash[comparable_constraint_name]
52
+ constraint_name_count ?
53
+ constraint_name_hash[comparable_constraint_name] = constraint_name_count + 1 :
54
+ constraint_name_hash[comparable_constraint_name] = 1
55
+ end
56
+
57
+ constraints.each do |constraint|
58
+ constraint.member_of_composite=(constraint_name_hash[constraint.constraint_name.upcase] > 1)
59
+ end
60
+ constraints
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
data/lib/drysql.rb CHANGED
@@ -6,6 +6,7 @@ end
6
6
  require 'connection_adapters/abstract_adapter'
7
7
  require 'connection_adapters/mysql_adapter'
8
8
  require 'connection_adapters/postgresql_adapter'
9
+ require 'connection_adapters/sqlserver_adapter'
9
10
  require 'connection_adapters/abstract/schema_definitions'
10
11
  require 'associations'
11
12
  require 'validations'
metadata CHANGED
@@ -3,15 +3,15 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: drysql
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2006-11-06 00:00:00 -05:00
8
- summary: Extends ActiveRecord to provide automatic identification of keys, and automatic generation of associations and validations
6
+ version: 0.1.2
7
+ date: 2006-11-16 00:00:00 -05:00
8
+ summary: Dynamic, Reflective, Invisible Object-Relational Mapping for Ruby
9
9
  require_paths:
10
10
  - lib
11
11
  email: bsevans@rubyforge.org
12
12
  homepage: http://drysql.rubyforge.org
13
13
  rubyforge_project: drysql
14
- description: DrySQL uses your DB's information schema to identify keys, and generate associations and validations for ActiveRecord objects. You defined the schema on your DB, don't re-define it in your application code. Legacy DB Tables/Column Names, Ruby Desktop apps, Rails apps...DrySQL's got all your (data)bases covered
14
+ description: DrySQL uses your DB's information schema to identify keys, and generate associations and validations for ActiveRecord objects. You defined the schema on your DB ... don't re-define it in your application code. Legacy DB Tables, Ruby Desktop apps, Rails apps...DrySQL's got all your databases covered
15
15
  autorequire: drysql
16
16
  default_executable:
17
17
  bindir: bin
@@ -36,6 +36,7 @@ files:
36
36
  - lib/helpers/string.rb
37
37
  - lib/connection_adapters/postgresql_adapter.rb
38
38
  - lib/connection_adapters/mysql_adapter.rb
39
+ - lib/connection_adapters/sqlserver_adapter.rb
39
40
  - lib/connection_adapters/abstract_adapter.rb
40
41
  - lib/connection_adapters/abstract/schema_definitions.rb
41
42
  - README