Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Implement simple query API #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 19, 2018
Merged

Conversation

jwilm
Copy link
Contributor

@jwilm jwilm commented Nov 21, 2017

The simple query API is a more robust version of batch_execute. Like
that method, simple_query allows passing a &str of semicolon
delimited queries. Divergence from batch_execute is in the return
type; instead of nothing, a Vec<TextRows> is returned. Each entry in
this Vec is the result set of one query in the query string. Thus if
there are two semicolon delimited queries, there will be two entries in
this Vec.

The TextRows and TextRow types returned from simple_query closely
mirror existing Rows and Row types with one major difference: only
string values can be retrieved from them.

There are a few TODOs in the code:

  • Are text values in this case guaranteed to be utf-8 encoded?
  • unwrap call in simple_query which assumes RowDescription is always
    sent. Is this valid?
  • documentation (denoted with either STUB or TODO)
  • tests

One other question that came up was whether get and get_opt should handle null specially. TextRow::get_inner currently returns an Option<Option<&str>>, but perhaps an Option<&str> can be used with an empty "" value when the field is null.

This is the API discussed in #303. For reference, this is what was discussed/implemented:

pub trait GenericConnection {
    // ...

    /// New method on GenericConnection; sends a simple "Query" frame
    ///
    /// Returns `Vec<TextRows>` since there may be multiple, semicolon delimited
    /// queries in the `query` string. From a protocol level, each query yields T, D, C
    /// (row description, data row(s), command completion), and finally a "ready
    /// for query" frame Z is returned.
    fn simple_query(&self, query: &str) -> Result<Vec<TextRows>>;
}

/// Result of one query sent via the `simple_query` API.
struct TextRows {
    /// Result of parsing Row description for query
    columns: Vec<Column>,
    /// Data rows returned in text format
    rows: Vec<RowData>,
}

/// One Row whose data is only available in string format
struct TextRow {
    columns: &[Column],
    raw: &RowData,
}

// Mirror existing Rows/Row APIs where it makes sense
impl IntoIterator for &TextRows {}
impl TextRows {
    fn columns(&self) -> &[Column];
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;
    fn get(&self, idx) -> TextRow;
    fn iter(&self) -> TextRowIter;
}

impl TextRow {
    fn columns(&self) -> &[Column];
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;
    fn get<I: RowIndex>(&self, idx: I) -> &str;
    fn get_opt<I: RowIndex>(&self, idx: I) -> Option<Result<&str>>;
}

@jwilm jwilm changed the title Implement simple query API [WIP] Implement simple query API Nov 21, 2017
@jwilm jwilm mentioned this pull request Nov 21, 2017
@jwilm
Copy link
Contributor Author

jwilm commented Nov 21, 2017

@sfackler, no need to review yet, but your input on first two TODOs would be helpful at this point.

@sfackler
Copy link
Owner

You can assume strings are UTF8. You can also assume that the backend will send you the data it's documented to, but I'd return a bad_message error rather than panicking.

@jwilm jwilm force-pushed the simple-query-api branch from 957566a to 9417a71 Compare April 28, 2018 00:09
jwilm added 6 commits April 27, 2018 17:34
The simple query API is a more robust version of `batch_execute`. Like
that method, `simple_query` allows passing a `&str` of semicolon
delimited queries. Divergence from `batch_execute` is in the return
type; instead of nothing, a `Vec<TextRows>` is returned. Each entry in
this `Vec` is the result set of one query in the query string. Thus if
there are two semicolon delimited queries, there will be two entries in
this `Vec`.

The `TextRows` and `TextRow` types returned from `simple_query` closely
mirror existing `Rows` and `Row` types with one major difference: only
string values can be retrieved from them.

There are a few TODOs in the code:

* Are text values in this case guaranteed to be utf-8 encoded?
* unwrap call in simple_query which assumes RowDescription is always
  sent
* documentation (denoted with either STUB or TODO)
Should always be utf8 in practice, but there's no point in panicking.
Also fixes the return type to be consistent with regular "Rows" API.
@jwilm jwilm force-pushed the simple-query-api branch from 9417a71 to a1d759a Compare April 28, 2018 00:34
@jwilm jwilm changed the title [WIP] Implement simple query API Implement simple query API Apr 28, 2018
@jwilm
Copy link
Contributor Author

jwilm commented Apr 28, 2018

Hey @sfackler, I finally got around to polishing this up, and it's ready to review! Thanks for letting this sit idle for so long in the PR queue.

self.columns
}

/// stub
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to finish the docs here.

@sfackler
Copy link
Owner

sfackler commented May 3, 2018

This seems pretty reasonable to me. Seems like we might want to deprecate/remove batch_execute since this basically replaces it, right?

result.push(TextRows::new(cols, mem::replace(&mut rows, Vec::new())));
}
}
_ => {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other messages we'd expect to get here? Ideally we'd be a bit more defensive and return an unexpected message error like we do elsewhere.

@jwilm
Copy link
Contributor Author

jwilm commented May 3, 2018

Both of the review items have been resolved. For batch_execute, I think it should be deprecated with a message that points to simple_query instead. Would you like me to push a commit for that?

@jwilm
Copy link
Contributor Author

jwilm commented May 17, 2018

Friendly ping in case this slipped past your radar

@sfackler
Copy link
Owner

Sorry for the delay! Yeah, let's deprecate batch_execute.

While you're here, I'd be interested for your thoughts on #346 :)

@jwilm
Copy link
Contributor Author

jwilm commented May 18, 2018

The deprecation was added in the most recent commit.

@jwilm
Copy link
Contributor Author

jwilm commented May 19, 2018

Oops, I guess I should fix the tests as well!

It's being replaced by the more capable `simple_query` API.
@jwilm jwilm force-pushed the simple-query-api branch from 502d3d0 to 39a4cdf Compare May 19, 2018 00:33
@jwilm
Copy link
Contributor Author

jwilm commented May 19, 2018

Fixed; tests are green now.

@sfackler sfackler merged commit b33fdcd into sfackler:master May 19, 2018
@sfackler
Copy link
Owner

Thanks!

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.

2 participants