sfackler / rust-postgres-array

MIT License
21 stars 10 forks source link

Usage documentation #3

Open Valve opened 9 years ago

Valve commented 9 years ago

Please write a couple of lines of documentation & examples, it's not very clear how to use the library.

shish commented 5 years ago

I think I got at least part of it working - for anybody else who ends up here by google:

First, as of this writing, this crate only works with rust-postgres 0.15 -- 0.16 seems to be a complete rewrite based on top of tokio and is not compatible

With the right versions in place, this is how I run a query which returns a set of (str, array[int]) rows

use postgres::{Connection, TlsMode};
use postgres_array::array::Array;

...

    let client = Connection::connect(dsn, TlsMode::None).expect("Failed to connect");

...

        for row in &client.query("\
            SELECT tag, array_agg(post_id)
            FROM post_tags
            GROUP BY tag
        ", &[]).unwrap() {
            let tag: String = row.get(0);
            let post_ids: Array<i32> = row.get(1);
            let post_ids_vec: Vec<i32> = post_ids.iter().cloned().collect();
            ...
        }