Skip to content

Instantly share code, notes, and snippets.

@davidhooey
Created March 26, 2014 13:56
Show Gist options
  • Save davidhooey/9783634 to your computer and use it in GitHub Desktop.
Save davidhooey/9783634 to your computer and use it in GitHub Desktop.
Oracle utilities for stmt_to_sqlid, stmt_to_hash and sqlid_to_hash.
require 'digest'
module OracleStmtUtils
def stmt_to_sqlid(stmt)
d1, d2, msb, lsb = Digest::MD5.digest(stmt + "\x00").unpack('IIII')
sqln = msb * (2 ** 32) + lsb
stop = Math.log(sqln, Math::E) / Math.log(32, Math::E) + 1
sqlid = String.new
alphabet = '0123456789abcdfghjkmnpqrstuvwxyz'
(0..stop-1).each {|i| sqlid = alphabet[(sqln / (32 ** i)) % 32] + sqlid}
return sqlid
end
def stmt_to_hash(stmt)
return Digest::MD5.digest(stmt + "\x00").unpack('IIII')[3]
end
def sqlid_to_hash(sqlid)
sum = 0
i = 1
alphabet = '0123456789abcdfghjkmnpqrstuvwxyz'
sqlid.each_char do |ch|
sum += alphabet.index(ch) * (32**(sqlid.length - i))
i += 1
end
return sum % (2 ** 32)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment