File: prepared_statements_safe.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (81 lines) | stat: -rw-r--r-- 3,063 bytes parent folder | download | duplicates (3)
1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen-string-literal: true

module Sequel
  module Plugins
    # The prepared_statements_safe plugin modifies the model to reduce the number of
    # prepared statements that can be created, by setting as many columns as possible
    # before creating, and by changing +save_changes+ to save all columns instead of
    # just the changed ones.
    #
    # This plugin depends on the +prepared_statements+ plugin.
    # 
    # Usage:
    #
    #   # Make all model subclasses more safe when using prepared statements (called before loading subclasses)
    #   Sequel::Model.plugin :prepared_statements_safe
    #
    #   # Make the Album class more safe when using prepared statements
    #   Album.plugin :prepared_statements_safe
    module PreparedStatementsSafe
      # Depend on the prepared_statements plugin
      def self.apply(model)
        model.plugin(:prepared_statements)
      end

      # Set the column defaults to use when creating on the model.
      def self.configure(model)
        model.send(:set_prepared_statements_column_defaults)
      end

      module ClassMethods
        # A hash with column symbol keys and default values.  Instance
        # values are merged into this hash before creating to reduce the
        # number of free columns (columns that may or may not be present
        # in the INSERT statement), as the number of prepared statements
        # that can be created is 2^N (where N is the number of free columns).
        attr_reader :prepared_statements_column_defaults
        
        Plugins.inherited_instance_variables(self, :@prepared_statements_column_defaults=>:dup)
        Plugins.after_set_dataset(self, :set_prepared_statements_column_defaults)

        # Freeze the prepared statements column defaults when freezing the model class.
        def freeze
          @prepared_statements_column_defaults.freeze if @prepared_statements_column_defaults

          super
        end

        private

        # Set the column defaults based on the database schema.  All columns
        # are set to a default value unless they are a primary key column or
        # they don't have a parseable default.
        def set_prepared_statements_column_defaults
          if db_schema
            h = {}
            db_schema.each do |k, v|
              default = v[:ruby_default]
              h[k] = default if (default || !v[:default]) && !v[:primary_key] && !default.is_a?(Sequel::SQL::Expression)
            end
            @prepared_statements_column_defaults = h
          end
        end
      end

      module InstanceMethods
        # Merge the current values into the default values to reduce the number
        # of free columns.
        def before_create
          @values = model.prepared_statements_column_defaults.merge(@values)
          super
        end

        # Always do a full save of all columns to reduce the number of prepared
        # statements that can be used.
        def save_changes(opts=OPTS)
          save(opts) || false if modified?
        end
      end
    end
  end
end