sql_tools 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sql_tools/column_definition.rb +9 -0
- data/lib/sql_tools/constraint.rb +14 -0
- data/lib/sql_tools/index.rb +28 -0
- data/lib/sql_tools/schema.rb +13 -0
- data/lib/sql_tools/schema_visitor.rb +22 -0
- data/lib/sql_tools/version.rb +1 -1
- data/lib/sql_tools.rb +2 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcf9039011a7ccbab22cfb0dc525581dddf1bf64f648ef49e6d0385487833a15
|
4
|
+
data.tar.gz: af79326d174f0a70fd1f53fea96e2858214a0ac1f57254f582cffd24990473db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5417f2bf4e359e91f580fdc941e8101dde4846ec4119ae81b443fea1a011a39ad29795294d76601bc3030029fa07995b73bd216e821167097e90a3c060ae393c
|
7
|
+
data.tar.gz: d87c01b0bf9d724631479af9d8c023f349d06b0937108556682ebae19bac2ee9c31b52ed37e6e0f22d2f14e1db9ff145ccc727e8a4c62f6fd3e65808afccd75a
|
@@ -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,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
|
data/lib/sql_tools/version.rb
CHANGED
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.
|
4
|
+
version: 0.2.0
|
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-
|
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
|