Skip to content

Fails to read SQL with GDALVectorTranslate #638

Description

@latot

Hi! I has been trying to run GDALVectorTranslate, I do not know almost nothing of C/C++! so @lnicola has been helping me figure out how to run everything, the next code is not perfect, but there is a very weird error, the tables.

This works:

ogrinfo "PG:dbname=postgres host=localhost port=5432 user=postgres password=postgres" -sql "select * from communes"

Here the code:

use gdal::DriverManager;
use std::ptr::{null, null_mut};

use std::ffi::{CString, c_int};

fn main() -> Result<(), Box<dyn std::error::Error>> {
        let path = std::path::Path::new("tmp.gpkg");
        let _ = std::fs::remove_file(path);
        let drv = DriverManager::get_driver_by_name("GPKG").unwrap();
        let mut ds = drv.create_vector_only(path.to_str().unwrap()).unwrap();
        let org_path = "PG:\"dbname='postgres' host='localhost' port='5432' user='postgres' password='postgres'\"";
        let mut org = gdal::Dataset::open(org_path).unwrap();
        //This do not make any difference on the error
        let opts = gdal::DatasetOptions {
                open_options: Some(&[
                        "DBNAME=postgres",
                        "PORT=4326",
                        "HOST=localhost",
                        "USER=postgres",
                        "PASSWORD=postgres",
                ]),
                ..Default::default()
        };
        let org2 = gdal::Dataset::open_ex(org_path, opts).unwrap();
        let mut org_vc = [org2.c_dataset()];
        let mut pb_usage_error: c_int = 0;
        let mut opts = [
                CString::new("-sql").unwrap(),
                CString::new("select * from some_table").unwrap(),
        ];
        let mut ref_opts = [
                opts[0].clone().into_raw(),
                opts[1].clone().into_raw(),
                null_mut(),
        ];
        unsafe {
                let b = gdal_sys::GDALVectorTranslateOptionsNew(ref_opts.as_mut_ptr(), null_mut());
                gdal_sys::GDALVectorTranslate(
                        null(),
                        ds.c_dataset(),
                        1,
                        org_vc.as_mut_ptr(),
                        b,
                        &mut pb_usage_error,
                );
        }
        Ok(())
}

I have checked in postgres, when this code is executed this is the query that reach the server:

select
  pg_namespace.nspname as schema,
  pg_class.relname as table,
  pg_attribute.attname as column
from pg_class, pg_namespace,pg_attribute, pg_type
where
  pg_class.relnamespace = pg_namespace.oid and
    pg_class.oid = pg_attribute.attrelid and
    pg_attribute.atttypid = pg_type.oid and
    pg_type.typname = 'raster'

Thx!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions