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

How get the field type for a null field? #494

Closed
mamcx opened this issue Mar 19, 2019 · 12 comments
Closed

How get the field type for a null field? #494

mamcx opened this issue Mar 19, 2019 · 12 comments

Comments

@mamcx
Copy link

mamcx commented Mar 19, 2019

Related to https://github.com/jgallagher/rusqlite/issues/372.

I can query a Value for the type, but if the field allow null, and the data return null, the original type is lost.

I can get the column names, but not the types.

@thomcc
Copy link
Member

thomcc commented Mar 19, 2019

I'm not sure this makes sense. NULL values in SQLite don't have a type besides NULL. SQLite's type system can be a bit strange, so you might want to read over https://www.sqlite.org/datatype3.html.

@mamcx
Copy link
Author

mamcx commented Mar 19, 2019

Yeah, I know.

But the field have:

CREATE TABLE config (
    config_id INTEGER PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    value TEXT
);

Now If I do:

SELECT * FROM config;

I need to know that value is Text.

BTW: And the rest of the info too, like if is PK, Unique, etc...

@gwenn
Copy link
Collaborator

gwenn commented Mar 19, 2019

Currently, there is no helper to access metadata:

@gwenn
Copy link
Collaborator

gwenn commented Mar 19, 2019

In postgres crate:

@mamcx
Copy link
Author

mamcx commented Mar 19, 2019

Yes, that is what is expected.

gwenn added a commit to gwenn/rusqlite that referenced this issue Mar 19, 2019
Return Columns name and type. (rusqlite#494)
@gwenn
Copy link
Collaborator

gwenn commented Apr 24, 2019

Fixed by 0.18 release.

@gwenn gwenn closed this as completed Apr 24, 2019
@ghost
Copy link

ghost commented Dec 2, 2019

Is it currently possible to get more metadata, such as which columns are nullable? columns seems to only give declared type without that kind of extra information.

@gwenn
Copy link
Collaborator

gwenn commented Dec 3, 2019

If you want such information, you have to use:

  1. sqlite3_column_origin_name => col_name or null for expression
  2. sqlite3_column_table_name => tbl_name or null for expression
  3. sqlite3_column_database_name => db_name or null for expression
    And then:
sqlite> SELECT "notnull" FROM <db_name>.pragma_table_info('<tbl_name>') WHERE name like '<col_name>';

Currently, there is no such feature in rusqlite. We can add such information behind a new 'metadata' feature.

@ghost
Copy link

ghost commented Dec 8, 2019

So is it just impossible right now (without directly using the ffi)? I've been trying to use queries but it looks like they basically only return rows. I can't figure out how to run something like sqlite> SELECT "notnull" FROM <db_name>.pragma_table_info('<tbl_name>') WHERE name like '<col_name>'; and get its result.

@gwenn
Copy link
Collaborator

gwenn commented Dec 8, 2019

@billop There is already this test which works fine !

@ghost
Copy link

ghost commented Dec 9, 2019

Thanks for the help, I basically got it working:

    fn get_nullable_stats(self: Self) -> Result<Vec<i64>, Error> {
        let conn = self.connect()?;
        let mut table_info =
            conn.prepare(r#"SELECT "notnull" FROM books.pragma_table_info("books")"#)?;
        let mut columns = Vec::new();
        let mut rows = table_info.query(NO_PARAMS)?;

        dbg!(rows.columns());

        while let Some(row) = rows.next()? {
            let row = row;
            let column: i64 = row.get(0)?;
            columns.push(column);
        }

        Ok(columns)
    }

@gwenn
Copy link
Collaborator

gwenn commented Dec 10, 2019

@billop You can also return booleans:

fn get_nullable_stats(self: Self) -> Result<Vec<bool>, Error> {
...
            let column: bool = row.get(0)?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants