Skip to content
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

Example of an NTEXT bulk insert? #352

Open
RandomGHUser opened this issue Sep 12, 2024 · 0 comments
Open

Example of an NTEXT bulk insert? #352

RandomGHUser opened this issue Sep 12, 2024 · 0 comments

Comments

@RandomGHUser
Copy link

Tiberius 0.12.3
I am probably missing something simple, but, given a modified example in attempt to bulk insert an NTEXT value:

use indicatif::ProgressBar;
use once_cell::sync::Lazy;
use std::env;
use tiberius::{Client, Config, IntoRow};
use tokio::net::TcpStream;
use tokio_util::compat::TokioAsyncWriteCompatExt;
use tracing::log::info;

static CONN_STR: Lazy<String> = Lazy::new(|| {
    env::var("TIBERIUS_TEST_CONNECTION_STRING").unwrap_or_else(|_| {
        "server=tcp:localhost,1433;IntegratedSecurity=true;TrustServerCertificate=true".to_owned()
    })
});

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    env_logger::init();

    let config = Config::from_ado_string(&CONN_STR)?;

    let tcp = TcpStream::connect(config.get_addr()).await?;
    tcp.set_nodelay(true)?;

    let mut client = Client::connect(config, tcp.compat_write()).await?;

    client
        .execute("DROP TABLE IF EXISTS bulk_test1", &[])
        .await?;
    info!("drop table");
    client
        .execute(
            r#"CREATE TABLE bulk_test1 (
                        id INT IDENTITY PRIMARY KEY,
                        text NTEXT NULL, 
            )"#,
            &[],
        )
        .await?;
    info!("create table done");

    let mut req = client.bulk_insert("bulk_test1").await?;

    let count = 1000i32;

    let pb = ProgressBar::new(count as u64);

    info!("start loading data");
    for i in 0..1000 {
        let ntext_col = [Some("this doesn't work"), None][i % 2];

        let row = (ntext_col).into_row();

        req.send(row).await.expect("err");
        pb.inc(1);
    }

    pb.finish_with_message("waiting...");

    let res = req.finalize().await.expect("err");

    info!("{:?}", res);

    Ok(())
}

I get this result:

err: BulkInput("invalid data type, expecting Some(VarLenSized(VarLenContext { type: NText, len: 2147483646, collation: Some(Collation { info: 15729673, sort_id: 0 }) })) but found String(Some(\"this doesn't work\"))")

Based on the to_sql.rs comments, it seems that a string field should be able to write to an ntext field:

...All string
/// types can also be used with `ntext`, `text`, `varchar`, `nchar` and `char`
/// columns.

Any idea what I may be doing wrong? I assume I need to encode/parse this into the applicable type, but am lost as to how to do so for this scenario.

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

No branches or pull requests

1 participant