Created
January 20, 2011 04:12
-
-
Save xaviershay/787378 to your computer and use it in GitHub Desktop.
Bad idea of the week: ARel + DataMapper
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
# Because I can't figure out how the fuck to get SQL out of datamapper itself | |
class PGEngine < Arel::Visitors::PostgreSQL | |
attr_accessor :engine | |
def initialize(engine) | |
super | |
self.engine = engine | |
end | |
def connection_pool | |
self | |
end | |
def connection | |
self | |
end | |
def tables | |
[] | |
end | |
def table_exists?(*args) | |
true | |
end | |
def columns(name, description) | |
model = DataMapper::Model.descendants.detect {|x| name == x.storage_name } || raise("Can't find model for #{name}") | |
model.properties.map {|x| | |
OpenStruct.new({name: x.name, type: x.class.to_s.split("::").last.downcase.to_sym}) | |
} | |
end | |
def with_connection | |
yield(self) | |
end | |
def quote(value, column) | |
"'#{value}'" | |
end | |
def quote_column_name(value) | |
"'#{value}'" | |
end | |
def quote_table_name(value) | |
"'#{value.underscore}'" | |
end | |
end | |
posts = Arel::Table.new(:posts, :engine => PGEngine.new(bogus)) | |
posts.project('*').where(pages[:author_id].eq(1)).to_sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment