Puppet Function: postgresql::postgresql_escape

Defined in:
lib/puppet/functions/postgresql/postgresql_escape.rb
Function type:
Ruby 4.x API

Summary

This function escapes a string using [Dollar Quoting](https://www.postgresql.org/docs/12/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING) using a randomly generated tag if required.

Overview

postgresql::postgresql_escape(String[1] $input_string)String

Parameters:

  • input_string (String[1])

    The unescaped string you want to escape using ‘dollar quoting`

Returns:

  • (String)

    A ‘Dollar Quoted` string



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puppet/functions/postgresql/postgresql_escape.rb', line 6

Puppet::Functions.create_function(:'postgresql::postgresql_escape') do
  # @param input_string
  #   The unescaped string you want to escape using `dollar quoting`
  #
  # @return [String]
  #   A `Dollar Quoted` string
  dispatch :default_impl do
    param 'String[1]', :input_string
  end

  def default_impl(input_string)
    # Where allowed, just return the original string wrapped in `$$`
    return "$$#{input_string}$$" unless tag_needed?(input_string)

    # Keep generating possible values for tag until we find one that doesn't appear in the input string
    tag = Digest::MD5.hexdigest(input_string)[0..5].gsub(%r{\d}, '')
    tag = Digest::MD5.hexdigest(tag)[0..5].gsub(%r{\d}, '') until input_string !~ %r{#{tag}}

    "$#{tag}$#{input_string}$#{tag}$"
  end

  def tag_needed?(input_string)
    input_string.include?('$$') || input_string.end_with?('$')
  end
end