sql_tools 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2ba309601d1b52dd6da36b4a8bb360a36d626887c422670c8ab9baeac3613b1
4
- data.tar.gz: aed2136db1548ecebf953dade273f0827eaebc5f9669b3739fae7a4f243e53f3
3
+ metadata.gz: f3b0a5d22da6c9be49114af27655dcffcfb1c0d6e2a045507f81947068dc4b84
4
+ data.tar.gz: 52003a39612fbb1cb6f778514235957e1024aa95ffd30f50b0f9bee48bba6ad5
5
5
  SHA512:
6
- metadata.gz: 5659b85ea66765857217a48c74c115e24e30a57d292fa035c45f51565b7a0a478ce76e7750785ad11b1643eda23a40a05f683a83051b105eeaab14dbc133927d
7
- data.tar.gz: 12b1b67b6273fb448ed28eeb74d4a755cab78edaa026cefe617426367b1a53657cc355dbbee1c012febded7c2b562879e01e5ef482e8620ab279053090604c73
6
+ metadata.gz: 94688c70de849c18bc490b236ad9242518cd659140a5978d6a20d05d619f0b3932d88299e831531a6b8f45d65ee91c1ef3cc6689bf1725bfc1f693f1240aed32
7
+ data.tar.gz: d825b27eea4c4ee7e96f64c6e4fa4a5cdc6e0fa65f7f46f4d17cbef7aa99c1c1f6b4d58db3df890b12a6a31d7f0ccbc8be3d386a6f94157d59883778cb6dc039
@@ -0,0 +1,9 @@
1
+ module SqlTools
2
+ class ColumnDefinition
3
+ def initialize(node)
4
+ @node = node
5
+ end
6
+
7
+ def name = @node.name.text
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module SqlTools
2
+ class Constraint
3
+ class << self
4
+ def from(node)
5
+ if node.children.any? { |child| child.type == :keyword_key }
6
+ Index.new(node)
7
+ else
8
+ binding.b
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,28 @@
1
+ module SqlTools
2
+ class Index
3
+ def initialize(node)
4
+ @node = node
5
+ end
6
+
7
+ def columns
8
+ @node.query(<<~QUERY).map do |match|
9
+ (ordered_columns (column name: (identifier) @column_name))
10
+ QUERY
11
+ match["column_name"].text
12
+ end
13
+ end
14
+
15
+ def primary_key?
16
+ @node.any? { |child| child.type == :keyword_primary } &&
17
+ @node.any? { |child| child.type == :keyword_key }
18
+ end
19
+
20
+ def name
21
+ return :primary_key if primary_key?
22
+
23
+ @node.name.text.delete("`")
24
+ end
25
+ end
26
+ end
27
+
28
+
@@ -0,0 +1,13 @@
1
+ module SqlTools
2
+ class Schema
3
+ attr_accessor :create_table
4
+ attr_reader :columns, :constraints
5
+
6
+ def initialize
7
+ @columns = []
8
+ @constraints = []
9
+ end
10
+
11
+ def indices = @constraints.select { |c| c.is_a?(Index) }
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ module SqlTools
2
+ class SchemaVisitor < TreeStand::Visitor
3
+ attr_reader :schema
4
+
5
+ def initialize(node)
6
+ super(node)
7
+ @schema = Schema.new
8
+ end
9
+
10
+ def on_create_table(node)
11
+ @schema.create_table = node
12
+ end
13
+
14
+ def on_column_definition(node)
15
+ @schema.columns << ColumnDefinition.new(node)
16
+ end
17
+
18
+ def on_constraint(node)
19
+ @schema.constraints << Constraint.from(node)
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SqlTools
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/sql_tools.rb CHANGED
@@ -10,6 +10,8 @@ module SqlTools
10
10
  class Error < StandardError; end
11
11
 
12
12
  class << self
13
+ def schema_from_sql(sql) = schema_from_tree(tree_from_sql(sql))
14
+ def schema_from_tree(tree) = SqlTools::SchemaVisitor.new(tree.root_node).visit.schema
13
15
  def query_from_sql(sql) = query_from_tree(tree_from_sql(sql))
14
16
  def query_from_tree(tree) = SqlTools::QueryVisitor.new(tree.root_node).visit.query
15
17
  def tree_from_sql(sql) = parser.parse_string(sql)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - derekstride
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-03 00:00:00.000000000 Z
11
+ date: 2024-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby_tree_sitter
@@ -52,7 +52,10 @@ files:
52
52
  - Rakefile
53
53
  - exe/install_tree_sitter_sql
54
54
  - lib/sql_tools.rb
55
+ - lib/sql_tools/column_definition.rb
55
56
  - lib/sql_tools/common_table_expression.rb
57
+ - lib/sql_tools/constraint.rb
58
+ - lib/sql_tools/index.rb
56
59
  - lib/sql_tools/inner_join.rb
57
60
  - lib/sql_tools/left_join.rb
58
61
  - lib/sql_tools/predicate.rb
@@ -61,6 +64,8 @@ files:
61
64
  - lib/sql_tools/query.rb
62
65
  - lib/sql_tools/query_visitor.rb
63
66
  - lib/sql_tools/relation.rb
67
+ - lib/sql_tools/schema.rb
68
+ - lib/sql_tools/schema_visitor.rb
64
69
  - lib/sql_tools/selection.rb
65
70
  - lib/sql_tools/version.rb
66
71
  - rakelib/treesitter.rake