activerecord7-redshift-adapter-pennylane 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ module Redshift
6
+ # Value Object to hold a schema qualified name.
7
+ # This is usually the name of a PostgreSQL relation but it can also represent
8
+ # schema qualified type names. +schema+ and +identifier+ are unquoted to prevent
9
+ # double quoting.
10
+ class Name # :nodoc:
11
+ SEPARATOR = '.'
12
+ attr_reader :schema, :identifier
13
+
14
+ def initialize(schema, identifier)
15
+ @schema = unquote(schema)
16
+ @identifier = unquote(identifier)
17
+ end
18
+
19
+ def to_s
20
+ parts.join SEPARATOR
21
+ end
22
+
23
+ def quoted
24
+ if schema
25
+ PG::Connection.quote_ident(schema) << SEPARATOR << PG::Connection.quote_ident(identifier)
26
+ else
27
+ PG::Connection.quote_ident(identifier)
28
+ end
29
+ end
30
+
31
+ def ==(other)
32
+ other.class == self.class && other.parts == parts
33
+ end
34
+ alias eql? ==
35
+
36
+ def hash
37
+ parts.hash
38
+ end
39
+
40
+ protected
41
+
42
+ def unquote(part)
43
+ if part&.start_with?('"')
44
+ part[1..-2]
45
+ else
46
+ part
47
+ end
48
+ end
49
+
50
+ def parts
51
+ @parts ||= [@schema, @identifier].compact
52
+ end
53
+ end
54
+
55
+ module Utils # :nodoc:
56
+ module_function
57
+
58
+ # Returns an instance of <tt>ActiveRecord::ConnectionAdapters::PostgreSQL::Name</tt>
59
+ # extracted from +string+.
60
+ # +schema+ is nil if not specified in +string+.
61
+ # +schema+ and +identifier+ exclude surrounding quotes (regardless of whether provided in +string+)
62
+ # +string+ supports the range of schema/table references understood by PostgreSQL, for example:
63
+ #
64
+ # * <tt>table_name</tt>
65
+ # * <tt>"table.name"</tt>
66
+ # * <tt>schema_name.table_name</tt>
67
+ # * <tt>schema_name."table.name"</tt>
68
+ # * <tt>"schema_name".table_name</tt>
69
+ # * <tt>"schema.name"."table name"</tt>
70
+ def extract_schema_qualified_name(string)
71
+ schema, table = string.scan(/[^".\s]+|"[^"]*"/)
72
+ if table.nil?
73
+ table = schema
74
+ schema = nil
75
+ end
76
+ Redshift::Name.new(schema, table)
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end