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

pattern matching ("like") issue #126

Closed
vrcod opened this Issue May 28, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@vrcod

vrcod commented May 28, 2015

So if I try a query looking something like this: SELECT foo.bar FROM foo WHERE foo.bar like '%$1%' I'm getting an error

thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: DbError(DbError { severity: "ERROR", code: IndeterminateDatatype, message: "could not determine data type of parameter $1", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: "postgres.c", line: 1324, routine: "exec_parse_message" })', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libcore/result.rs:729
An unknown error occurred

Is there a way already to somehow use like %$1%?

@sfackler

This comment has been minimized.

Show comment
Hide comment
@sfackler

sfackler May 28, 2015

Owner

What does foo look like?

Owner

sfackler commented May 28, 2015

What does foo look like?

@sfackler

This comment has been minimized.

Show comment
Hide comment
@sfackler

sfackler Jun 11, 2015

Owner

Ping?

Owner

sfackler commented Jun 11, 2015

Ping?

@vrcod

This comment has been minimized.

Show comment
Hide comment
@vrcod

vrcod Jun 11, 2015

Consider that foo.bar is of type CHARACTER VARYING, the query I've posted is as simple SQL as it gets.

vrcod commented Jun 11, 2015

Consider that foo.bar is of type CHARACTER VARYING, the query I've posted is as simple SQL as it gets.

@sfackler

This comment has been minimized.

Show comment
Hide comment
@sfackler

sfackler Jun 11, 2015

Owner

Postgres doesn't interpret $1 as a parameter in the query you posted:

#[test]
fn adf() {
    let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None).unwrap();
    conn.batch_execute("CREATE TEMPORARY TABLE foo (id VARCHAR);").unwrap();
    let stmt = conn.prepare("SELECT id FROM foo WHERE id LIKE '%$1%'").unwrap();
    assert!(stmt.param_types().is_empty());
}

Using string concatenation to keep the parameter outside of the string literal causes the parameter type to be inferred to TEXT:

#[test]
fn adf() {
    let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None).unwrap();
    conn.batch_execute("CREATE TEMPORARY TABLE foo (id VARCHAR);").unwrap();
    let stmt = conn.prepare("SELECT id FROM foo WHERE id LIKE '%' || $1 || '%'").unwrap();
    assert_eq!(&[Type::Text], stmt.param_types());
}
Owner

sfackler commented Jun 11, 2015

Postgres doesn't interpret $1 as a parameter in the query you posted:

#[test]
fn adf() {
    let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None).unwrap();
    conn.batch_execute("CREATE TEMPORARY TABLE foo (id VARCHAR);").unwrap();
    let stmt = conn.prepare("SELECT id FROM foo WHERE id LIKE '%$1%'").unwrap();
    assert!(stmt.param_types().is_empty());
}

Using string concatenation to keep the parameter outside of the string literal causes the parameter type to be inferred to TEXT:

#[test]
fn adf() {
    let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None).unwrap();
    conn.batch_execute("CREATE TEMPORARY TABLE foo (id VARCHAR);").unwrap();
    let stmt = conn.prepare("SELECT id FROM foo WHERE id LIKE '%' || $1 || '%'").unwrap();
    assert_eq!(&[Type::Text], stmt.param_types());
}
@vrcod

This comment has been minimized.

Show comment
Hide comment
@vrcod

vrcod Jun 11, 2015

Thanks, this is what I was missing.

vrcod commented Jun 11, 2015

Thanks, this is what I was missing.

@vrcod vrcod closed this Jun 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment