简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM