Sha256: 7e33c41ccf3d5c54faea6052f8adb6ab8473354783ab1c9dace4dbbd8702e8ff

Contents?: true

Size: 813 Bytes

Versions: 2

Compression:

Stored size: 813 Bytes

Contents

# frozen_string_literal: true

module WithAdvisoryLock
  class MySQL < Base
    # See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
    def try_lock
      raise ArgumentError, 'shared locks are not supported on MySQL' if shared
      raise ArgumentError, 'transaction level locks are not supported on MySQL' if transaction

      execute_successful?("GET_LOCK(#{quoted_lock_str}, 0)")
    end

    def release_lock
      execute_successful?("RELEASE_LOCK(#{quoted_lock_str})")
    end

    def execute_successful?(mysql_function)
      sql = "SELECT #{mysql_function} AS #{unique_column_name}"
      connection.select_value(sql).to_i.positive?
    end

    # MySQL wants a string as the lock key.
    def quoted_lock_str
      connection.quote(lock_str)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
with_advisory_lock-5.1.0 lib/with_advisory_lock/mysql.rb
with_advisory_lock-5.0.0 lib/with_advisory_lock/mysql.rb