Problems with loading large clob values via returning clauses
weiznich opened this issue · 2 comments
I've tried the following code:
use oracle::*; // oracle = "0.5.6"
const CREATE_TEST_TABLE: &str = "CREATE TABLE test_table (\
text clob
)";
fn repro(conn: Connection) {
let _ = conn.execute("DROP TABLE test_table", &[]);
conn.execute(CREATE_TEST_TABLE, &[]).unwrap();
let mut stmt = conn
.statement("INSERT INTO test_table(text) VALUES (:in1) RETURNING text INTO :out1")
.build()
.unwrap();
let long_text = std::iter::repeat('a').take(4000).collect::<String>();
stmt.execute_named(&[("in1", &long_text), ("out1", &None::<String>)])
.unwrap();
let s: &String = &stmt.returned_values("out1").unwrap()[0];
assert_eq!(s, &long_text);
}
I expect that this code passes without an error for a valid connection. Instead of that I get the following error message:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: OciError(DbError { code: 22835, offset: 53, message: "ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 4000, maximum: 2000)", fn_name: "dpiStmt_execute", action: "execute" })', src/main.rs:16:10
(where line main.rs:16
refers to the unwrap after the execute_named
)
There are three ways. The second and third ones are usually unacceptable.
- Use
OracleType::CLOB
in place ofNone::<String>
and get the CLOB value asClob
. - Use
OracleType::Varchar2(4000)
in place ofNone::<String>
if the maximum size of CLOB data is 4000. - Use
OracleType::Varchar2(32767)
in place ofNone::<String>
and change the Oracle initialization parameterMAX_STRING_SIZE
toEXTENDED
if the maximum size of CLOB data is 32767. (I have not tested this...)
use oracle::sql_type::{Clob, OracleType};
use oracle::Connection;
use std::io::Read; // for clob.read_to_string
const CREATE_TEST_TABLE: &str = "CREATE TABLE test_table (\
text clob
)";
fn repro(conn: Connection) {
let _ = conn.execute("DROP TABLE test_table", &[]);
conn.execute(CREATE_TEST_TABLE, &[]).unwrap();
let mut stmt = conn
.statement("INSERT INTO test_table(text) VALUES (:in1) RETURNING text INTO :out1")
.build()
.unwrap();
let long_text = std::iter::repeat('a').take(4000).collect::<String>();
stmt.execute_named(&[("in1", &long_text), ("out1", &OracleType::CLOB)])
.unwrap();
// https://docs.rs/oracle/latest/oracle/sql_type/struct.Clob.html
let mut clob: Clob = stmt.returned_values("out1").unwrap().remove(0);
let mut s = String::new();
clob.read_to_string(&mut s).unwrap();
assert_eq!(&s, &long_text);
}
Thanks for the fast answer. For context the real code casing this is coming from diesel-oci, so it's much more generic and cannot assume much about the actual fields. Solution 2 will not work there. Solution 1 works for CLOB
columns, but won't work if the user tries to load a VARCHAR
column (diesel-oci does currently not differentiate between these two types). Using the maximal-size variant sounds like it has some negative implications, is that correct?