-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix prepared statements with auto_encode_arrays
- Loading branch information
Showing
7 changed files
with
122 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require "mini_sql/abstract/prepared_binds" | ||
|
||
module MiniSql | ||
module Postgres | ||
class PreparedBindsAutoArray < ::MiniSql::Abstract::PreparedBinds | ||
|
||
attr_reader :array_encoder | ||
|
||
def initialize(array_encoder) | ||
@array_encoder = array_encoder | ||
end | ||
|
||
def bind_hash(sql, hash) | ||
sql = sql.dup | ||
binds = [] | ||
bind_names = [] | ||
i = 0 | ||
|
||
hash.each do |k, v| | ||
binds << (v.is_a?(Array) ? array_encoder.encode(v) : v) | ||
bind_names << [BindName.new(k)] | ||
bind_outputs = bind_output(i += 1) | ||
|
||
sql.gsub!(":#{k}") do | ||
# ignore ::int and stuff like that | ||
# $` is previous to match | ||
if $` && $`[-1] != ":" | ||
bind_outputs | ||
else | ||
":#{k}" | ||
end | ||
end | ||
end | ||
[sql, binds, bind_names] | ||
end | ||
|
||
def bind_array(sql, array) | ||
sql = sql.dup | ||
param_i = -1 | ||
i = 0 | ||
binds = [] | ||
bind_names = [] | ||
sql.gsub!("?") do | ||
v = array[param_i += 1] | ||
binds << (v.is_a?(Array) ? array_encoder.encode(v) : v) | ||
i += 1 | ||
bind_names << [BindName.new("$#{i}")] | ||
bind_output(i) | ||
end | ||
[sql, binds, bind_names] | ||
end | ||
|
||
def bind_output(i) | ||
"$#{i}" | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters