Register PostgreSQL catalog-related functions
(conn: &Connection)
| 3 | |
| 4 | /// Register PostgreSQL catalog-related functions |
| 5 | pub fn register_catalog_functions(conn: &Connection) -> Result<()> { |
| 6 | debug!("Registering catalog functions"); |
| 7 | |
| 8 | // pg_table_is_visible(oid) - checks if table is in search path |
| 9 | // For SQLite, all tables are visible |
| 10 | conn.create_scalar_function( |
| 11 | "pg_table_is_visible", |
| 12 | 1, |
| 13 | FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC, |
| 14 | |ctx| { |
| 15 | // Accept either integer or text OID |
| 16 | // Try to get as i64 first, if that fails try as string and parse |
| 17 | let _oid = match ctx.get::<i64>(0) { |
| 18 | Ok(oid) => oid, |
| 19 | Err(_) => { |
| 20 | // Try as string |
| 21 | let oid_str: String = ctx.get(0)?; |
| 22 | oid_str.parse::<i64>().unwrap_or(0) |
| 23 | } |
| 24 | }; |
| 25 | // In SQLite, all tables are visible |
| 26 | // Return 1 for true (SQLite boolean convention) |
| 27 | Ok(1i32) |
| 28 | }, |
| 29 | )?; |
| 30 | |
| 31 | // Note: SQLite doesn't support schema-qualified function names, |
| 32 | // so we handle pg_catalog.pg_table_is_visible through query rewriting |
| 33 | |
| 34 | // regclass type cast function |
| 35 | conn.create_scalar_function( |
| 36 | "regclass", |
| 37 | 1, |
| 38 | FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC, |
| 39 | |ctx| { |
| 40 | let table_name: String = ctx.get(0)?; |
| 41 | |
| 42 | // Look up table OID from pg_class view |
| 43 | // For now, just generate a consistent OID |
| 44 | let oid = generate_table_oid(&table_name); |
| 45 | Ok(oid) |
| 46 | }, |
| 47 | )?; |
| 48 | |
| 49 | debug!("Catalog functions registered successfully"); |
| 50 | Ok(()) |
| 51 | } |
| 52 | |
| 53 | // Generate a stable OID from table name |
| 54 | fn generate_table_oid(name: &str) -> i32 { |