prisma / tiberius

TDS 7.2+ (Microsoft SQL Server) driver for Rust
Apache License 2.0
327 stars 119 forks source link

Binding string parameter adds quotation marks #282

Open delsehi opened 1 year ago

delsehi commented 1 year ago

I cannot get the results back from a stored procedure. It seems like when I bind a string value to a parameter, it adds quotation marks "" around the string.

    let config = get_config();
    let config = Config::from_ado_string(&config.db_connection_string).unwrap();
    let tcp = TcpStream::connect(config.get_addr()).await.unwrap();
    let mut client = Client::connect(config, tcp.compat_write()).await.unwrap();
    let mut query = Query::new("EXEC [app].[get_user] @P1");
    query.bind(user_id);
    let stream = query.query(&mut client).await.unwrap();
    let row = stream.into_row().await.unwrap().unwrap();

    println!("RESULT: {}\n", row.get::<&str, &str>("result").unwrap());
ALTER procedure [app].[get_user]
    (
    @user_id NVARCHAR(MAX)
)
AS BEGIN
    DECLARE @result NVARCHAR(MAX)
    SET @result = (select top 1
        *, @user_id as 'userid'
    from app.[user]
    where [id] = @user_id
    for json auto)

    -- select  @result 'result'
    select isnull(@result, '["id": ' ++ @user_id ++ '') 'result'

END 

This prints RESULT: ["id": "5sXc[.... the rest of the user id ]Ac-E"

And not the actual user which is in the database.

Executing

exec app.get_user '8sdfd[... rest of the user id ]Ac-E'

in the database returns the right result though.

Are there any examples of how to bind a string as a parameter to a stored procedure or is this a bug?

I am using SQL Server in a local Docker container and Tiberius 0.12.1.

delsehi commented 1 year ago

I tried modifying my stored procedure as

ALTER procedure [app].[get_user]
    (
    @user_id NVARCHAR(MAX)
)
AS BEGIN
    DECLARE @result NVARCHAR(MAX)

    DECLARE @user_id_modified NVARCHAR(MAX) 
   -- Remove added "" from Tiberius 
    SET @user_id_modified = REPLACE(@user_id, '"', '')

    SET @result = (select top 1
        *, @user_id as 'userid'
    from app.[user]
    where [id] = @user_id_modified
    for json auto)

    -- select  @result 'result'
    select isnull(@result, '[]') 'result'

END 

And now it worked. But I would prefer not having to strip out quotation marks in my sps in order to use Tiberus... 🥺