-
-
Save ansh/e5d21a6de7e7139e1068cdde05e90147 to your computer and use it in GitHub Desktop.
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
require "granite/adapter/pg" | |
module CompressedString | |
extend self | |
def to_db(value : ::String?) : Granite::Columns::Type | |
bytea : Array(UInt8) | |
compressed = IO::Memory.new | |
Compress::Deflate::Writer.open(compressed) do |deflate| | |
deflate.print value | |
end | |
compressed.seek 0 | |
bytea = compressed.each_byte.to_a | |
compressed.close | |
Slice.new(bytea.to_unsafe, bytea.size) | |
end | |
def from_rs(result : ::DB::ResultSet) : ::String? | |
value = result.read(Bytes?) | |
return nil if value.nil? | |
final = "" | |
string = IO::Memory.new value | |
Compress::Deflate::Reader.open(string) do |deflate| | |
final = deflate.gets_to_end | |
end | |
string.close | |
return final | |
end | |
end | |
# class Granite::AssociationCollection(Owner, Target) | |
# private def query | |
# if through.nil? | |
# "WHERE #{Target.table_name}.#{@foreign_key} = ?" | |
# else | |
# "JOIN #{through} ON #{through}.#{@foreign_key} = #{Target.table_name}.#{Target.primary_name} " \ | |
# "WHERE #{through}.#{@foreign_key} = ?" | |
# end | |
# end | |
# end | |
Granite::Connections << Granite::Adapter::Pg.new( | |
name: "pg", | |
url: ENV["DATABASE_URL"] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment