imparcial 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/lib/imparcial/driver/{base → abstract}/expression/base.rb +11 -4
  2. data/lib/imparcial/driver/abstract/expression/column.rb +313 -0
  3. data/lib/imparcial/driver/abstract/expression/constraint.rb +149 -0
  4. data/lib/imparcial/driver/abstract/expression/delete.rb +88 -0
  5. data/lib/imparcial/driver/{base → abstract}/expression/index.rb +103 -96
  6. data/lib/imparcial/driver/abstract/expression/insert.rb +49 -0
  7. data/lib/imparcial/driver/{base → abstract}/expression/lock.rb +1 -1
  8. data/lib/imparcial/driver/{base/expression/util.rb → abstract/expression/record.rb} +8 -12
  9. data/lib/imparcial/driver/{base → abstract}/expression/select.rb +6 -4
  10. data/lib/imparcial/driver/{base → abstract}/expression/sequence.rb +133 -62
  11. data/lib/imparcial/driver/{base → abstract}/expression/statement.rb +1 -1
  12. data/lib/imparcial/driver/abstract/expression/table.rb +416 -0
  13. data/lib/imparcial/driver/abstract/expression/transaction.rb +143 -0
  14. data/lib/imparcial/driver/{base → abstract}/expression/update.rb +20 -3
  15. data/lib/imparcial/driver/abstract/expression.rb +24 -0
  16. data/lib/imparcial/driver/{base → abstract}/result.rb +12 -11
  17. data/lib/imparcial/driver/abstract/sql/column.rb +103 -0
  18. data/lib/imparcial/driver/abstract/sql/constraint.rb +42 -0
  19. data/lib/imparcial/driver/{base → abstract}/sql/delete.rb +1 -1
  20. data/lib/imparcial/driver/{base → abstract}/sql/index.rb +3 -11
  21. data/lib/imparcial/driver/{base → abstract}/sql/insert.rb +1 -1
  22. data/lib/imparcial/driver/abstract/sql/record.rb +19 -0
  23. data/lib/imparcial/driver/{base → abstract}/sql/select.rb +2 -2
  24. data/lib/imparcial/driver/{base → abstract}/sql/sequence.rb +2 -2
  25. data/lib/imparcial/driver/{base/sql/table_operation.rb → abstract/sql/table.rb} +8 -15
  26. data/lib/imparcial/driver/{base → abstract}/sql/transaction.rb +5 -5
  27. data/lib/imparcial/driver/{base → abstract}/sql/update.rb +1 -1
  28. data/lib/imparcial/driver/abstract/sql.rb +21 -0
  29. data/lib/imparcial/driver/{base → abstract}/typemap.rb +43 -89
  30. data/lib/imparcial/driver/{base → abstract}/util.rb +17 -5
  31. data/lib/imparcial/driver/abstract.rb +255 -0
  32. data/lib/imparcial/driver/mysql/expression/table.rb +2 -11
  33. data/lib/imparcial/driver/mysql/expression.rb +4 -4
  34. data/lib/imparcial/driver/mysql/result.rb +1 -1
  35. data/lib/imparcial/driver/mysql/sql/column.rb +59 -0
  36. data/lib/imparcial/driver/mysql/sql/constraint.rb +39 -0
  37. data/lib/imparcial/driver/mysql/sql/index.rb +14 -23
  38. data/lib/imparcial/driver/mysql/sql/sequence.rb +1 -1
  39. data/lib/imparcial/driver/mysql/sql/{table_metadata.rb → table.rb} +29 -5
  40. data/lib/imparcial/driver/mysql/sql.rb +7 -7
  41. data/lib/imparcial/driver/mysql/typemap.rb +1 -1
  42. data/lib/imparcial/driver/mysql/util.rb +1 -1
  43. data/lib/imparcial/driver/mysql.rb +19 -18
  44. data/lib/imparcial/driver/postgre/expression.rb +26 -7
  45. data/lib/imparcial/driver/postgre/result.rb +1 -1
  46. data/lib/imparcial/driver/postgre/sql/column.rb +53 -0
  47. data/lib/imparcial/driver/postgre/sql/constraint.rb +37 -0
  48. data/lib/imparcial/driver/postgre/sql/index.rb +21 -21
  49. data/lib/imparcial/driver/postgre/sql/sequence.rb +6 -4
  50. data/lib/imparcial/driver/postgre/sql/{table_metadata.rb → table.rb} +8 -8
  51. data/lib/imparcial/driver/postgre/sql.rb +7 -7
  52. data/lib/imparcial/driver/postgre/typemap.rb +2 -2
  53. data/lib/imparcial/driver/postgre/util.rb +1 -1
  54. data/lib/imparcial/driver/postgre.rb +11 -11
  55. data/lib/imparcial/driver.rb +1 -1
  56. data/lib/imparcial/exception.rb +28 -18
  57. data/lib/imparcial/initializer.rb +62 -0
  58. data/lib/imparcial.rb +1 -74
  59. metadata +50 -50
  60. data/lib/imparcial/driver/base/expression/delete.rb +0 -72
  61. data/lib/imparcial/driver/base/expression/insert.rb +0 -33
  62. data/lib/imparcial/driver/base/expression/table_diff.rb +0 -154
  63. data/lib/imparcial/driver/base/expression/table_evolution.rb +0 -94
  64. data/lib/imparcial/driver/base/expression/table_metadata.rb +0 -122
  65. data/lib/imparcial/driver/base/expression/table_operation.rb +0 -137
  66. data/lib/imparcial/driver/base/expression/transaction.rb +0 -59
  67. data/lib/imparcial/driver/base/expression.rb +0 -37
  68. data/lib/imparcial/driver/base/sql/table_metadata.rb +0 -29
  69. data/lib/imparcial/driver/base/sql.rb +0 -25
  70. data/lib/imparcial/driver/base.rb +0 -156
  71. data/lib/imparcial/driver/mysql/expression/index.rb +0 -44
  72. data/lib/imparcial/driver/mysql/sql/table_operation.rb +0 -20
  73. data/lib/imparcial/driver/postgre/expression/index.rb +0 -10
  74. data/lib/imparcial/driver/postgre/expression/sequence.rb +0 -9
  75. data/lib/imparcial/driver/postgre/expression/table.rb +0 -20
  76. data/lib/imparcial/driver/postgre/sql/table_operation.rb +0 -9
@@ -0,0 +1,53 @@
1
+ module Imparcial
2
+ module Driver
3
+ module PostgreSQL
4
+ module Column
5
+
6
+ private
7
+
8
+ def sql_for_renaming_column ( options )
9
+
10
+ syntax = ''
11
+
12
+ syntax += 'ALTER TABLE ' + quote(options[:table_name]) + ' '
13
+
14
+ options[:field].each do |old_name, new_name|
15
+
16
+ syntax += "RENAME COLUMN #{old_name} TO #{new_name}"
17
+
18
+ end
19
+
20
+ logger.warn(syntax) if @table_logging
21
+
22
+ syntax
23
+
24
+ end
25
+
26
+ def sql_for_modifying_columns ( options )
27
+
28
+ syntax = ''
29
+
30
+ syntax += 'ALTER TABLE ' + quote(options[:table_name]) + ' '
31
+
32
+ options[:fields].each do |field|
33
+
34
+ column = field_to_column field
35
+
36
+ syntax += "ALTER COLUMN #{column[:name]} TYPE #{column[:type]}"
37
+ syntax += "(#{column[:size]})" if column[:size]
38
+ syntax += ","
39
+
40
+ end
41
+
42
+ syntax.chop!
43
+
44
+ logger.warn(syntax) if @table_logging
45
+
46
+ syntax
47
+
48
+ end
49
+
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ module Imparcial
2
+ module Driver
3
+ module PostgreSQL
4
+ module Constraint
5
+
6
+ private
7
+
8
+ def sql_for_adding_auto_increment ( options )
9
+
10
+ seq_name = 'seq_' + options[:table_name].to_s + '_' + options[:field].to_s
11
+ drop_sequence_if_necessary :sequence_name => seq_name
12
+ create_sequence :sequence_name => seq_name
13
+ sql_for_adding_default_value :table_name => options[:table_name], :fields => {options[:field] => "nextval('#{seq_name}')"}
14
+
15
+ end
16
+
17
+ def sql_for_adding_default_value ( options )
18
+
19
+ syntax = 'ALTER TABLE ' + quote(options[:table_name]) + ' '
20
+
21
+ for field, value in options[:fields]
22
+
23
+ value = quote_value(value) if not value.to_s.match('nextval')
24
+ syntax += "ALTER COLUMN #{quote(field)} SET DEFAULT #{value},"
25
+
26
+ end
27
+
28
+ syntax.chop!
29
+
30
+ syntax
31
+
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,6 +1,6 @@
1
1
  module Imparcial
2
2
  module Driver
3
- module SQLPostgre
3
+ module PostgreSQL
4
4
  module Index
5
5
 
6
6
  private
@@ -9,19 +9,28 @@ module Imparcial
9
9
 
10
10
  def sql_for_index_exists? ( options )
11
11
 
12
- %{
13
- SELECT 1 FROM pg_class WHERE relkind = 'i' AND
14
- relname = #{quote_value(options[:index_name])}
15
- }
12
+ %{SELECT
13
+ c2.relname as "table",
14
+ (select attname from pg_attribute where attrelid = i.indexrelid AND attnum > 0) as "column" ,
15
+ c.relname as "index"
16
+ FROM pg_class c
17
+ JOIN pg_roles r ON r.oid = c.relowner
18
+ LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
19
+ LEFT JOIN pg_index i ON i.indexrelid = c.oid
20
+ LEFT JOIN pg_class c2 ON i.indrelid = c2.oid
21
+ WHERE c.relkind = 'i'
22
+ AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
23
+ c2.relname = #{quote_value(options[:table_name])} AND
24
+ c.relname = #{quote_value(options[:index_name])}
25
+ }
16
26
 
17
27
  end
18
28
 
19
29
  # Generate SQL statement for retrieving all indexes.
20
30
 
21
- def sql_for_retrieving_indexes
31
+ def sql_for_getting_indexes ( options )
22
32
 
23
- %{
24
- SELECT
33
+ %{SELECT
25
34
  c2.relname as "table",
26
35
  (select attname from pg_attribute where attrelid = i.indexrelid AND attnum > 0) as "column" ,
27
36
  c.relname as "index"
@@ -30,22 +39,13 @@ module Imparcial
30
39
  LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
31
40
  LEFT JOIN pg_index i ON i.indexrelid = c.oid
32
41
  LEFT JOIN pg_class c2 ON i.indrelid = c2.oid
33
- WHERE c.relkind IN ('i','')
34
- AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
35
- }
42
+ WHERE c.relkind = 'i'
43
+ AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND
44
+ c2.relname = #{quote_value(options[:table_name])}
45
+ }
36
46
 
37
47
  end
38
48
 
39
- # Generate SQL statement for retrieving indexes for a specific table.
40
-
41
- def sql_for_retrieving_indexes_for_table ( options )
42
-
43
- sql_for_retrieving_indexes + %{
44
- AND c2.relname = #{quote_value(options[:table_name])}
45
- }
46
-
47
- end
48
-
49
49
 
50
50
  end
51
51
  end
@@ -1,6 +1,6 @@
1
1
  module Imparcial
2
2
  module Driver
3
- module SQLPostgre
3
+ module PostgreSQL
4
4
  module Sequence
5
5
 
6
6
  private
@@ -10,15 +10,17 @@ module Imparcial
10
10
  %{
11
11
  SELECT 1 FROM pg_class WHERE relkind = 'S' AND
12
12
  relname = #{quote_value(options[:sequence_name])}
13
- }
13
+ }
14
14
 
15
15
  end
16
16
 
17
17
  # Generate SQL statement for retrieving sequences.
18
18
 
19
- def sql_for_retrieving_sequences
19
+ def sql_for_getting_sequences
20
20
 
21
- "SELECT relname FROM pg_class WHERE relkind = 'S'"
21
+ %{
22
+ SELECT relname FROM pg_class WHERE relkind = 'S'
23
+ }
22
24
 
23
25
  end
24
26
 
@@ -1,27 +1,27 @@
1
1
  module Imparcial
2
2
  module Driver
3
- module SQLPostgre
4
- module TableMetadata
3
+ module PostgreSQL
4
+ module Table
5
5
 
6
6
  private
7
7
 
8
8
  # Generate SQL statement for retrieving tables.
9
9
 
10
- def sql_for_retrieving_tables
10
+ def sql_for_getting_tables
11
11
 
12
12
  %{
13
13
  SELECT table_name FROM information_schema.tables
14
14
  WHERE table_schema = 'public'
15
- }
15
+ }
16
16
 
17
17
  end
18
18
 
19
19
  # Generate SQL statement for retrieving columns.
20
-
20
+ =begin
21
21
  def sql_for_retrieving_columns ( options )
22
22
 
23
23
  %{
24
- SELECT
24
+ SELECT
25
25
  A.column_name, A.data_type, A.character_maximum_length,
26
26
  (A.is_nullable = 'YES') as allow_null,
27
27
  (A.column_name = B.column_name) as pk,
@@ -36,10 +36,10 @@ module Imparcial
36
36
  FROM information_schema.columns A
37
37
  LEFT JOIN information_schema.key_column_usage B ON A.table_name = B.table_name
38
38
  WHERE A.table_name = #{quote_value(options[:table_name])};
39
- }
39
+ }
40
40
 
41
41
  end
42
-
42
+ =end
43
43
  end
44
44
  end
45
45
  end
@@ -1,15 +1,15 @@
1
- require 'imparcial/driver/postgre/sql/table_operation'
2
- require 'imparcial/driver/postgre/sql/table_metadata'
3
- require 'imparcial/driver/postgre/sql/sequence'
4
- require 'imparcial/driver/postgre/sql/index'
1
+ FileList[File.dirname(__FILE__) + '/sql/*.rb'].each do |f|
2
+ require f
3
+ end
5
4
 
6
5
  module Imparcial
7
6
  module Driver
8
- module SQLPostgre
9
- include TableOperation
10
- include TableMetadata
7
+ module PostgreSQL
8
+ include Table
11
9
  include Sequence
12
10
  include Index
11
+ include Constraint
12
+ include Column
13
13
  end
14
14
  end
15
15
  end
@@ -1,10 +1,10 @@
1
1
  module Imparcial
2
2
  module Driver
3
- module TypemapPostgre
3
+ module PostgreTypemap
4
4
 
5
5
  def sql_types
6
6
 
7
- super.dup.merge!('CHARACTER VARYING' => :string)
7
+ super.dup.merge!('CHARACTER VARYING' => :string, 'DOUBLE PRECISION' => :float)
8
8
 
9
9
  end
10
10
 
@@ -1,6 +1,6 @@
1
1
  module Imparcial
2
2
  module Driver
3
- module UtilPostgre
3
+ module PostgreUtil
4
4
 
5
5
  def quote_value ( val )
6
6
 
@@ -1,28 +1,29 @@
1
1
  require 'postgres'
2
+
2
3
  require 'imparcial/driver/postgre/util'
3
- require 'imparcial/driver/postgre/typemap'
4
4
  require 'imparcial/driver/postgre/result'
5
+ require 'imparcial/driver/postgre/typemap'
5
6
  require 'imparcial/driver/postgre/sql'
6
7
  require 'imparcial/driver/postgre/expression'
7
8
 
8
9
  module Imparcial
9
10
  module Driver
10
- class AdapterPostgre < AdapterBase
11
+ class PostgreAdapter < AbstractAdapter
11
12
 
13
+ include PostgreUtil
14
+ include PostgreTypemap
15
+ include PostgreSQL
16
+ include PostgreExpression
17
+
12
18
  def adapter_specific_exception
13
19
 
14
20
  PGError
15
21
 
16
22
  end
17
23
 
18
- include UtilPostgre
19
- include TypemapPostgre
20
- include SQLPostgre
21
- include ExpressionPostgre
22
-
23
24
  def connect
24
25
 
25
- @conn = PGconn.connect @host, @port, '', '', @database, @user, @pass
26
+ @conn = PGconn.connect @host, @port, '', '', @database, @username, @password
26
27
 
27
28
  rescue adapter_specific_exception => ex
28
29
 
@@ -33,11 +34,10 @@ module Imparcial
33
34
  def query ( sql )
34
35
 
35
36
  result = conn.exec sql
36
- @result = ResultPostgre.new result
37
+ @result = PostgreResult.new result
37
38
 
38
39
  end
39
-
40
-
40
+
41
41
  end
42
42
  end
43
43
  end
@@ -1 +1 @@
1
- require 'imparcial/driver/base'
1
+ require 'imparcial/driver/abstract'
@@ -6,31 +6,37 @@ module Imparcial
6
6
 
7
7
  # Adapter
8
8
 
9
- class AdapterError < ImparcialError; end
10
- class AdapterNotFound < AdapterError; end
11
- class AdapterConfigError < AdapterError; end
9
+ class AdapterError < ImparcialError; end
10
+ class AdapterNotFound < AdapterError; end
12
11
  class AdapterConnectionError < AdapterError; end
13
-
14
- # Result
15
-
16
- class ResultError < ImparcialError; end
17
12
 
18
13
  # Expression
19
14
 
20
15
  class ExpressionError < ImparcialError; end
21
16
 
22
17
  class FeatureNotFound < ExpressionError; end
18
+ class ConditionError < ExpressionError; end
19
+ class OptionError < ExpressionError; end
20
+ class ResultError < ExpressionError; end
23
21
 
24
22
  # Table
25
23
 
26
- class TableError < ExpressionError; end
24
+ class TableError < ExpressionError; end
27
25
  class TableCreateError < TableError; end
28
- class TableDropError < TableError; end
29
- class TableRetrieveError < TableError; end
30
-
31
- class ConditionError < ExpressionError; end
32
- class OptionError < ExpressionError; end
33
-
26
+ class TableDropError < TableError; end
27
+ class TableListError < TableError; end
28
+ class TableUpdateError < TableError; end
29
+
30
+ # Column
31
+
32
+ class ColumnError < ExpressionError; end
33
+ class ColumnListError < ColumnError; end
34
+ class ColumnUpdateError < ColumnError; end
35
+ class ColumnCreateError < ColumnError; end
36
+ class ColumnDropError < ColumnError; end
37
+ class ColumnConstraintError < ColumnError; end
38
+ class ColumnRenameError < ColumnError; end
39
+
34
40
  # Crud
35
41
 
36
42
  class SelectError < ExpressionError; end
@@ -40,9 +46,11 @@ module Imparcial
40
46
 
41
47
  # Sequence
42
48
 
43
- class SequenceError < ExpressionError; end
49
+ class SequenceError < ExpressionError; end
44
50
  class SequenceCreateError < SequenceError; end
45
- class SequenceDropError < SequenceError; end
51
+ class SequenceDropError < SequenceError; end
52
+ class SequenceListError < SequenceError; end
53
+ class SequenceUpdateError < SequenceError; end
46
54
 
47
55
  # Transaction
48
56
 
@@ -54,8 +62,10 @@ module Imparcial
54
62
 
55
63
  # Index
56
64
 
57
- class IndexError < ExpressionError; end
65
+ class IndexError < ExpressionError; end
58
66
  class IndexCreateError < IndexError; end
59
- class IndexDropError < IndexError; end
67
+ class IndexDropError < IndexError; end
68
+ class IndexListError < IndexError; end
69
+ class IndexUpdateError < IndexError; end
60
70
 
61
71
  end
@@ -0,0 +1,62 @@
1
+ module Imparcial
2
+
3
+ # This is what happens in a nutshell:
4
+ # Firstly, we get some configs about the selected adapter. Things
5
+ # like user, pass and database.
6
+ # Secondly and not less important, based on those configs, we load
7
+ # the appropriate adapter.
8
+ # Finally, we copy those configs into new adapter object.
9
+ # Now, those configs will also be avaliable in the adapter's instance.
10
+
11
+ class Initializer
12
+
13
+ attr_accessor :host, :username, :password, :database, :socket, :adapter, :logger, :port
14
+
15
+ # Factory method.
16
+
17
+ def self.run
18
+
19
+ instance = new
20
+
21
+ yield instance
22
+
23
+ instance.setup_adapter
24
+
25
+ end
26
+
27
+ def host
28
+
29
+ @host || 'localhost'
30
+
31
+ end
32
+
33
+ # Load adapter and copy instance vars.
34
+
35
+ def setup_adapter
36
+
37
+ require 'imparcial/driver/' + @adapter
38
+
39
+ # Get an adapter const.
40
+
41
+ adapter_const = ('Imparcial::Driver::' + @adapter.camelize + 'Adapter').to_const
42
+
43
+ adapter = adapter_const.new
44
+
45
+ # Copy these instance variables into new adapter.
46
+
47
+ instance_variables.each do |v|
48
+
49
+ adapter.instance_variable_set(v,send(v.gsub('@','')))
50
+
51
+ end
52
+
53
+ adapter
54
+
55
+ rescue LoadError
56
+
57
+ raise AdapterNotFound.new(driver + ' cannot be found')
58
+
59
+ end
60
+
61
+ end
62
+ end
data/lib/imparcial.rb CHANGED
@@ -6,77 +6,4 @@ require 'facets/core/symbol/to_const'
6
6
 
7
7
  require 'imparcial/exception'
8
8
  require 'imparcial/driver'
9
-
10
- module Imparcial
11
- class Initializer
12
-
13
- attr_accessor :host, :driver, :port, :user, :pass, :socket, :database
14
-
15
- alias_method :adapter=, :driver=
16
- alias_method :username=, :user=
17
- alias_method :password=, :pass=
18
- alias_method :sock=, :socket=
19
- alias_method :db=, :database=
20
- alias_method :hostname=, :host=
21
-
22
- alias_method :adapter, :driver
23
- alias_method :username, :user
24
- alias_method :password, :pass
25
- alias_method :sock, :socket
26
- alias_method :db, :database
27
- alias_method :hostname, :host
28
-
29
- # Default is localhost.
30
-
31
- def host
32
-
33
- @host || 'localhost'
34
-
35
- end
36
-
37
- # Default is Mysql
38
-
39
- def driver
40
-
41
- @driver || 'mysql'
42
-
43
- end
44
-
45
- # Default is STDOUT
46
-
47
- def logger
48
-
49
- @logger || Logger.new( STDERR )
50
-
51
- end
52
-
53
- def self.run
54
-
55
- instance = new
56
-
57
- yield instance
58
-
59
- instance.start_me
60
-
61
- end
62
-
63
- def start_me
64
-
65
- require 'imparcial/driver/' + driver
66
-
67
- # Get an adapter const.
68
-
69
- adapter_const = ('Imparcial::Driver::Adapter' + driver.camelize).to_const
70
-
71
- adapter = adapter_const.new self
72
-
73
- adapter
74
-
75
- rescue LoadError
76
-
77
- raise AdapterNotFound.new(driver + ' cannot be found')
78
-
79
- end
80
-
81
- end
82
- end
9
+ require 'imparcial/initializer'