lib/each_sql.rb in each_sql-0.1.0 vs lib/each_sql.rb in each_sql-0.2.0
- old
+ new
@@ -18,70 +18,96 @@
# - :oracle: Parsing rules for Oracle scripts. Removes trailing slashes after begin-end blocks.
Defaults = {
:default => {
:delimiter => /;+/,
:blocks => {
+ /`/ => /`/,
+ /"/ => /"/,
/'/ => /'/,
/\/\*[^+]/ => /\*\//,
/--+/ => $/,
},
:nesting_blocks => {
- /\bdeclare\b/i => /\bbegin\b/i,
- /\bbegin\b/i => /\bend\b/i
+ /\bdeclare.*?;\s*?begin\b/im => /;\s*?end\b/i,
+ /\bbegin\b/i => /;\s*?end\b/i,
},
+ :nesting_context => [
+ /\A\s*(begin|declare|create\b[^;]+?\b(procedure|function|trigger|package))\b/im
+ ],
:callbacks => {},
:ignore => [],
:replace => {},
# Let's assume we don't change delimiters within usual sql scripts
:strip_delimiter => lambda { |obj, stmt| stmt.chomp ';' }
},
:mysql => {
:delimiter => /;+|delimiter\s+\S+/i,
:blocks => {
+ /`/ => /`/,
+ /"/ => /"/,
/'/ => /'/,
/\/\*[^+]/ => /\*\//,
/--+/ => $/,
},
:nesting_blocks => {
/\bbegin\b/i => /\bend\b/i
},
+ :nesting_context => [
+ /\A\s*(begin|create\b[^;]+?\b(procedure|function|trigger))\b/im
+ ],
# We need to change delimiter on `delimiter' command
:callbacks => {
/^\s*delimiter\s+(\S+)/i => lambda { |obj, stmt, md|
new_delimiter = Regexp.new(Regexp.escape md[1])
- obj.delimiter = /#{new_delimiter}+|delimiter\s+\S+/i
+ obj.delimiter = /(#{new_delimiter})+|delimiter\s+\S+/i
obj.delimiter_string = md[1]
}
},
:ignore => [
/^delimiter\s+\S+$/i
],
:replace => {},
:strip_delimiter => lambda { |obj, stmt|
- stmt.chomp(obj.delimiter_string || ';')
+ stmt.gsub(/(#{Regexp.escape(obj.delimiter_string || ';')})+\Z/, '')
}
},
:oracle => {
:delimiter => /;+/,
:blocks => {
+ /`/ => /`/,
+ /"/ => /"/,
/'/ => /'/,
/\/\*[^+]/ => /\*\//,
/--+/ => $/,
},
:nesting_blocks => {
/\bbegin\b/i => /\bend\b/i,
- /\bcreate[^;]*\b(procedure|function|trigger|package)\b/im => {
+ /\bdeclare.*?;\s*?begin\b/im => {
:closer => %r{;\s*/}m,
- :delimiter => /;\s*\//
+ # Stops immediately
+ :pop => true
+ },
+ /\bcreate[^;]+?\b(procedure|function|trigger|package)\b/im => {
+ :closer => %r{;\s*/}m,
+ # Stops immediately
+ :pop => true
}
},
- :callbacks => {},
+ :nesting_context => [
+ /\A\s*(begin|declare|create\b[^;]+?\b(procedure|function|trigger|package))\b/im
+ ],
+ :callbacks => {
+ /\Abegin\b/ => lambda { |obj, stmt, md|
+ # Oracle needs this
+ stmt << ';' if stmt !~ /;\Z/
+ }
+ },
:ignore => [],
- :replace => {},
+ :replace => { %r[\A/] => '' },
:strip_delimiter => lambda { |obj, stmt| obj
- stmt.chomp( stmt =~ /;\s*\// ? '/' : ';' )
+ stmt.gsub(/(#{stmt =~ /;\s*\// ? '/' : ';'})+\Z/, '')
}
}
}
Defaults[:plsql] = Defaults[:oracle] # alias