sequel-mysql_generated_columns 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cebd0e3d382ad56563cc96ab8f07ce0f4936455c
4
- data.tar.gz: fa6d22d714976ed109e45a474936427141f6d06e
3
+ metadata.gz: f89990321a4c7b4523b5e3065df8106095d85a75
4
+ data.tar.gz: bba7a2bb5d8309a441d4c78bbc01b6eefda23b69
5
5
  SHA512:
6
- metadata.gz: 00a490e3c86c400f023612c92e54b4927ade888017d7be3c5773d719405ec6424d9b33b6040901475f7acd723e49bae4af1c96ae8ca9b6cc615ca91eae8b0d43
7
- data.tar.gz: a8ce7cf5f97015e61f92d6bf2b581c60eaee234a24b70b08d47677ca3c71f64d47d240b6cdb5bcd06ed775e6f221b1f9524a886f1aff0707296425cfd49b16f4
6
+ metadata.gz: 5315b306cb5247eb13602a9a41c7295c9e6e419ea43203a25556804ba508404df6a2c533f12267b90148acd2806851e14b80736ac646758550471ac891c3be81
7
+ data.tar.gz: ade24da7fcb5a53687d224a8f766be398622993b4c7acbb856da39a941eefa15333a32c9be7301e2ea96099f3e052d83dd2856cfe40fe992b80db1997870e45d
data/README.md CHANGED
@@ -3,18 +3,22 @@
3
3
  Sequel extension that adds support for MySQL generated columns (added first on
4
4
  MySQL 5.7.5).
5
5
 
6
- When enabled, use `#generated_column` method on `DB#create_table` blocks, and
7
- `#add_generated_column` method on `DB#alter_table` blocks.
6
+ When enabled, it allows passing an SQL expression with the option `:as` to
7
+ `#column` and `#add_column` methods, inside `DB#create_table` and
8
+ `DB#alter_table` blocks.
8
9
 
9
10
  ## Example
10
11
 
12
+ When used in a `create_table` block:
13
+
11
14
  ```ruby
12
15
  create_table(:triangles) do
13
- Integer :a
14
- Integer :b
15
- generated_column :c, Integer, :sqrt.sql_function(:a*:a + :b*:b)
16
+ integer :a
17
+ integer :b
18
+ integer :c, :as => :sqrt.sql_function(:a*:a + :b*:b)
16
19
  end
17
20
  ```
21
+ This will generate the following SQL statement:
18
22
 
19
23
  ```sql
20
24
  CREATE TABLE `triangles` (
@@ -24,16 +28,24 @@ CREATE TABLE `triangles` (
24
28
  )
25
29
  ```
26
30
 
31
+ In this example, after creating a table with a single JSON column, the next
32
+ `alter_table` block:
33
+
27
34
  ```ruby
28
35
  create_table(:docs) do
29
36
  json :doc
30
37
  end
31
38
 
32
39
  alter_table(:docs) do
33
- add_generated_column :id, Integer, :json_extract.sql_function(:doc, '$.id'), primary_key: true, stored: true
40
+ add_column :id, Integer, :as => :json_extract.sql_function(:doc, '$.id'),
41
+ :stored => true,
42
+ :primary_key => true
34
43
  end
35
44
  ```
36
45
 
46
+ will add a stored generated column with a primary key over the JSON object
47
+ property `id` in from the JSON document stored in column `doc`:
48
+
37
49
  ```sql
38
50
  CREATE TABLE `documents` (`doc` json)
39
51
  ALTER TABLE `documents` ADD COLUMN `id` integer AS (json_extract(`doc`, '$.id')) STORED PRIMARY KEY
@@ -9,55 +9,12 @@ module Sequel
9
9
  NOT_NULL = ' NOT NULL'.freeze
10
10
  PRIMARY_KEY = ' PRIMARY KEY'.freeze
11
11
 
12
- # Additional methods for the create_table generator to support constraint validations.
13
- module CreateTableGeneratorMethods
14
- def generated_column(name, type, expr, opts={})
15
- index_opts = opts.delete(:index)
16
- columns << {:name => name, :type => type, :expr => expr, :gen => true}.merge!(opts)
17
- if index_opts
18
- index(name, index_opts.is_a?(Hash) ? index_opts : {})
19
- end
20
- end
21
- end
22
-
23
- # Additional methods for the alter_table generator to support constraint validations,
24
- # used to give it a more similar API to the create_table generator.
25
- module AlterTableGeneratorMethods
26
- include CreateTableGeneratorMethods
27
-
28
- # Add a generated column with the given name, type, and opts to the DDL for the table.
29
- #
30
- # add_generated_column(:name, String, Sequel.function(:sum, :a, :b)) # ADD COLUMN name varchar(255) AS (SUM(a, b))
31
- def add_generated_column(name, type, expr, opts={})
32
- @operations << {:op => :add_column, :name => name, :type => type, :expr => expr, :gen => true}.merge!(opts)
33
- end
34
- end
35
-
36
- # Modify the default create_table generator to include
37
- # the generated columns methods.
38
- def create_table_generator(&block)
39
- super do
40
- extend CreateTableGeneratorMethods
41
- @generated_columns = []
42
- instance_eval(&block) if block
43
- end
44
- end
45
-
46
- # Modify the default alter_table generator to include
47
- # the generated columns methods.
48
- def alter_table_generator(&block)
49
- super do
50
- extend AlterTableGeneratorMethods
51
- instance_eval(&block) if block
52
- end
53
- end
54
-
55
12
  # Modify column definition generator method to support generated columns
56
13
  def column_definition_sql(column)
57
- if column[:gen]
14
+ if expr = column[:as]
58
15
  sql = String.new
59
16
  sql << "#{quote_identifier(column[:name])} #{type_literal(column)}"
60
- sql << " AS (#{literal(column[:expr])})"
17
+ sql << " AS (#{literal(expr)})"
61
18
  generated_column_definition_order.each{|m| send(:"generated_column_definition_#{m}_sql", sql, column)}
62
19
  sql
63
20
  else
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "sequel-mysql_generated_columns"
7
- spec.version = "0.1.0"
7
+ spec.version = "0.2.0"
8
8
  spec.authors = ["Damián Silvani"]
9
9
  spec.email = ["munshkr@gmail.com"]
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-mysql_generated_columns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damián Silvani