Jack Owels
Jack Owels

Reputation: 169

How to send PG binary data from the Rails controller?

I have a Postgres database with PostGIS extension and some geo-data in it. I need to generate tiles with custom SQL queries (using ST_... functions), that returns binary data (bytea, like \x1a320a0474657374121d12020000180322150946ec3f1a14453b0a09280f091413121e09091e0f1a026331220228012880207802). And I need to send this data from my Rails controller to the requester, with the headers like 'Content-Type: x-application/protobuf'.

So, my question is:

1) How to get the binary data from the Database (Sequel, ActiveRecord::Base.connection.execute ?) 2) How to return this data in the controller action? (send_data ..., tyle: '' ?)

Thanks!

Upvotes: 1

Views: 616

Answers (1)

Wonyoung So
Wonyoung So

Reputation: 31

I just figured out the solution - hope it helps, although it's a long time ago..

records_array = ActiveRecord::Base.connection.execute(query)
result = records_array[0]['st_asmvt']
encoded = ActiveRecord::Base.connection.unescape_bytea(result)

ActiveRecord::Base.connection.unescape_bytea is the answer.

Upvotes: 3

Related Questions