Skip to content

Feature request: access field names and types even when NO rows are returned - #209

Merged
brianc merged 1 commit into
masterfrom
issues/209
Jun 5, 2013
Merged

Feature request: access field names and types even when NO rows are returned#209
brianc merged 1 commit into
masterfrom
issues/209

Conversation

@brianc

@brianc brianc commented Jun 3, 2013

Copy link
Copy Markdown
Owner

A common trick to get information about the structure of a query result is to run the query with a LIMIT 0 thus only getting the field names and types.

This trick seems to be impossible to do with the node-postgresql module because the only place where field names are available is within a result row, so no rows means no such info.

I hope I'm missing something. If not, this would be a really useful addition.
I've been thinking the information could be passed as part of the .end() event, with information being filed names and data types.

@brianc

brianc commented Oct 25, 2012

Copy link
Copy Markdown
Owner

I like this idea. Do you think you could write a simple failing test w/ the api you imagined? Usually helps me get a head start on new features. Doing this in javascript will be trivial. The trickier part will be having the native driver do the same thing, but should be doable.

@strk

strk commented Oct 26, 2012

Copy link
Copy Markdown
Contributor Author

I was about to look into this when I found a "rowDescription" event
in "simple-query-tests.js". Is a query going to send such event ?
Would seem to be what I was looking for, if it did

@strk

strk commented Oct 26, 2012

Copy link
Copy Markdown
Contributor Author

Actually, since we're at it, it would be also good to get field types too !

strk pushed a commit to CartoDB/node-postgres that referenced this pull request Oct 26, 2012
@strk

strk commented Oct 26, 2012

Copy link
Copy Markdown
Contributor Author

@brianc: check out git://github.com/CartoDB/node-postgres.git types
Haven't added a test for the event-based API, for that one there could be a new event, maybe the "rowDescription" I saw above (but haven't found tested)

@brianc

brianc commented Nov 3, 2012

Copy link
Copy Markdown
Owner

awesome - just saw this update. will check this out asap - been a crazy past week

@brianc

brianc commented Dec 11, 2012

Copy link
Copy Markdown
Owner

this is a pretty beefy change, but I like it. Will work on this when I get a bit more time. Just giving it a little bump. 💡

@whitelynx

Copy link
Copy Markdown
Contributor

👍

This change would make it possible to actually write a generic frontend for Postgres using Node.js, similar to phpPGAdmin.

The same API presented in that failing test can actually be used in conjunction with the row event as well, since the Result object is passed as the second parameter there, though the rowDescription event is also useful for the case where you're not getting any rows back.

@strk

strk commented May 17, 2013

Copy link
Copy Markdown
Contributor Author

Any news on this ?

@brianc

brianc commented May 17, 2013

Copy link
Copy Markdown
Owner

On accessing field names when no rows are returned?

@strk

strk commented May 17, 2013

Copy link
Copy Markdown
Contributor Author

both field names and field types

@jatorre

jatorre commented May 17, 2013

Copy link
Copy Markdown

Cant be more supportive of the idea. Needed for lot of instrospection

@brianc

brianc commented May 17, 2013

Copy link
Copy Markdown
Owner

k - working on it now

@strk

strk commented May 20, 2013

Copy link
Copy Markdown
Contributor Author

@brian let me know when this is ready for test, or if you need help, thank you

@brianc

brianc commented May 20, 2013

Copy link
Copy Markdown
Owner

@strk thanks! I got held up this weekend with a bunch of family coming in to town. This is next on my list for this week. ❤️

@strk

strk commented May 27, 2013

Copy link
Copy Markdown
Contributor Author

@brianc any progress here ?

@strk

strk commented May 30, 2013

Copy link
Copy Markdown
Contributor Author

Ok, I'm giving this a try, within today I should have a pull request ready.

@strk

strk commented May 30, 2013

Copy link
Copy Markdown
Contributor Author

In order to fix the test I produced earlier we should maintain a mapping between oid and type names as type names are not in the protocol (only oid). But now I'm not sure it is ok to maintain such mapping internally. The mapping could be different across different databases (especially for custom types). Maybe the library could expose a function to update the typeName database and provide a basic one...

@strk

strk commented May 30, 2013

Copy link
Copy Markdown
Contributor Author

or (cleaner) we just pass over the type oid and let the calling code do whatever they want with it

@strk

strk commented May 30, 2013

Copy link
Copy Markdown
Contributor Author

For some background info, my development database has ~750 types. Note that any table also becomes a type.

strk pushed a commit to CartoDB/node-postgres that referenced this pull request May 30, 2013
Adds a 'rowDescription' event in query object.
Add test for accessing field info via both event and result object
@whitelynx

Copy link
Copy Markdown
Contributor

Just passing the type's OID back to the user would work fine, as long as we provided a way to look up the type name that corresponds to a given OID... basically, provide lazily-loaded/cached type information whenever the user requests it.

Another possibility would be to provide an option which would cause node-postgres to automatically look up type names for any type OIDs it encounters in query results that it doesn't already have cached, but only if the user requests it. (since in the case of a cache miss, that would incur an extra query's worth of overhead, we wouldn't want that to be default behavior)

@strk

strk commented Jun 3, 2013

Copy link
Copy Markdown
Contributor Author

The cleanest way would be to just return oid and let caller do any lookup/caching...
@brianc : news about your half-working branch ?

@brianc

brianc commented Jun 3, 2013

Copy link
Copy Markdown
Owner

Yeah returning just the OID is the way to go. There's a query which can be run to pull the mapping from type to name out of the database if a client is interested. I'll push it when it's finished.

Closes #209
Native implementation requires significant refactor and so I wont work on this
if/until there is an issue for it
@brianc

brianc commented Jun 3, 2013

Copy link
Copy Markdown
Owner

@strk this should solve your issue, yes?

@strk

strk commented Jun 3, 2013

Copy link
Copy Markdown
Contributor Author

Yes, I think that's good.

@brianc

brianc commented Jun 5, 2013

Copy link
Copy Markdown
Owner

💃 yay! merging & pushing new version now

brianc added a commit that referenced this pull request Jun 5, 2013
Feature request: access field names and types even when NO rows are returned
@brianc
brianc merged commit 4f8cce4 into master Jun 5, 2013
@brianc
brianc deleted the issues/209 branch June 5, 2013 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants