Comments (9)
Related: #99 Actually maybe not that related on second read.
from trilogy.
Not the same as mysql2, but temporary_database_connection.query( "SELECT * FROM instruments" )
does return a Trilogy::Result
, which has #fields
available. So as an immediate workaround you could do something like:
result = temporary_database_connection.query( "SELECT * FROM instruments" )
result.rows.map { |row| result.fields.zip(row).to_h }
to get hashes.
from trilogy.
Very true, and that's a great use of zip
!
We use this in quite a few places so it would mean changing a lot of lines, minor as it is.
Is there a desire to have rows
return a Hash
with the fields as keys or is that not going to work? If so, then we'll help with that before moving to trilogy
. If not, then we'll go this workaround route at some point.
Let me know. And thanks again!
from trilogy.
Trilogy isn't a intended as a 1:1 drop in replacement for mysql2, though it is similar due to shared authorship and consumers.
I don't think it's a good idea for us to change the behaviour of rows
as that would break backwards compatibility. I think you can achieve what you want using result.each_hash.first
from trilogy.
Oh right, I totally forgot about each_hash
. Thanks John. Yeah, I agree with closing this.
from trilogy.
Totally fair. I'll give that a shot and go from there. Thanks for all your work on this.
from trilogy.
Just wanted to say, .each_hash.with_index( 1 ) do |row, row_number|
works a treat!
And to get a single expected row, I ended up using .each_hash.sole
. This worked great as it returned a Hash
with the field names but also would raise Enumerable::SoleItemExpectedError: multiple items found
if there were multiple results from the query, which is a nice check to ensure you're getting what you expect.
I didn't know each_hash
existed. Is there a good place to find or add some documentation for that?
Many thanks!
from trilogy.
Our documentation is limited at the moment, but we do have
trilogy/contrib/ruby/README.md
Line 33 in 3c3a386
trilogy/contrib/ruby/lib/trilogy/result.rb
Lines 9 to 25 in 3c3a386
from trilogy.
Yup, perfect.
https://github.com/trilogy-libraries/trilogy/tree/main/contrib/ruby#usage
That would have been super helpful. You even callout that trilogy
returns the row values with .each
, and to use .each_hash
if you want the column names as well:
There is no as query option. Calling Trilogy::Result#each will yield an array of row values. If you want a hash you should use Trilogy::Result#each_hash.
Thanks again.
from trilogy.
Related Issues (20)
- Use memset_s() instead of memset() to clear passwords
- Prepared Statements: Ruby support HOT 5
- Improved Ruby error classes HOT 6
- Compile Ruby in parallel
- Trilogy::SSLError is raised when a vitess vtgate is shut down HOT 2
- Client returns TRILOGY_INVALID_SEQUENCE_ID HOT 1
- CI for old Rubies doesn't work correctly
- failed install on windows 11 with mysys64 HOT 1
- [Ruby] Potential compaction issue in `rb_trilogy_connect`? HOT 2
- `Trilogy.connect` not working in Staging and Production environments. HOT 8
- How did you solve the connection "feature" that translates `localhost` to a local UNIX socket? HOT 1
- Support for cleartext password
- Connection Fails in GitHub Actions HOT 3
- `trilogy` can't connect to remote Rails db, but `mysql2` can with identical `database.yml` HOT 1
- A more native GitHub Actions workflow would be nice HOT 2
- Add configuration option for SSL_OP_IGNORE_UNEXPECTED_EOF? HOT 1
- [Ruby] Add a Pipelining API?
- Make C-extension Ractor-safe HOT 1
- Non-C extension support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from trilogy.