Skip to main content

export

Function export 

Source
pub fn export(conn: &Connection, filename: &str, lobj_id: Oid) -> Result
Expand description

Exporting a Large Object.

See lo_export

Examples found in repository?
examples/testlo.rs (line 45)
7fn main() -> libpq::errors::Result {
8    let mut args = std::env::args();
9
10    if args.len() < 4 {
11        panic!(
12            "usage: {} database_name in_filename out_filename",
13            args.next().unwrap()
14        );
15    }
16
17    let database = args.nth(1).unwrap();
18    let in_filename = args.next().unwrap();
19    let out_filename = args.next().unwrap();
20
21    /*
22     * set up the connection
23     */
24    let conn = libpq::Connection::set_db(None, None, None, None, Some(&database))?;
25
26    /* Set always-secure search path, so malicious users can't take control. */
27    let res = conn.exec("SELECT pg_catalog.set_config('search_path', '', false)");
28    if res.status() != libpq::Status::TuplesOk {
29        panic!("SET failed: {:?}", conn.error_message());
30    }
31
32    conn.exec("begin");
33    println!("importing file \"{in_filename}\" ...");
34    let lobj_oid = libpq::lo::import(&conn, &in_filename);
35
36    println!("\tas large object {lobj_oid}.");
37
38    println!("picking out bytes 1000-2000 of the large object");
39    pickout(&conn, lobj_oid, 1_000, 1_000)?;
40
41    println!("overwriting bytes 1000-2000 of the large object with X's");
42    overwrite(&conn, lobj_oid, 1_000, 1_000)?;
43
44    println!("exporting large object to file \"{out_filename}\" ...");
45    libpq::lo::export(&conn, &out_filename, lobj_oid)?;
46
47    conn.exec("end");
48
49    Ok(())
50}
More examples
Hide additional examples
examples/testlo64.rs (line 47)
7fn main() -> libpq::errors::Result {
8    let mut args = std::env::args();
9
10    if args.len() < 5 {
11        panic!(
12            "usage: {} database_name in_filename out_filename out_filename2",
13            args.next().unwrap()
14        );
15    }
16
17    let database = args.nth(1).unwrap();
18    let in_filename = args.next().unwrap();
19    let out_filename = args.next().unwrap();
20    let out_filename2 = args.next().unwrap();
21
22    /*
23     * set up the connection
24     */
25    let conn = libpq::Connection::set_db(None, None, None, None, Some(&database))?;
26
27    /* Set always-secure search path, so malicious users can't take control. */
28    let res = conn.exec("SELECT pg_catalog.set_config('search_path', '', false)");
29    if res.status() != libpq::Status::TuplesOk {
30        panic!("SET failed: {:?}", conn.error_message());
31    }
32
33    conn.exec("begin");
34
35    println!("importing file \"{in_filename}\" ...");
36    let lobj_oid = libpq::lo::import(&conn, &in_filename);
37
38    println!("\tas large object {lobj_oid}.");
39
40    println!("picking out bytes 4294967000-4294968000 of the large object");
41    pickout(&conn, lobj_oid, 4_294_967_000, 1_000)?;
42
43    println!("overwriting bytes 4294967000-4294968000 of the large object with X's");
44    overwrite(&conn, lobj_oid, 4_294_967_000, 1_000)?;
45
46    println!("exporting large object to file \"{out_filename}\" ...");
47    libpq::lo::export(&conn, &out_filename, lobj_oid)?;
48
49    println!("truncating to 3294968000 bytes");
50    my_truncate(&conn, lobj_oid, 3_294_968_000)?;
51
52    println!("exporting truncated large object to file \"{out_filename2}\" ...");
53    libpq::lo::export(&conn, &out_filename2, lobj_oid)?;
54
55    conn.exec("end");
56
57    Ok(())
58}