MCPcopy Create free account
hub / github.com/erans/pgsqlite / store_create_table_metadata

Method store_create_table_metadata

src/session/db_handler.rs:2750–2829  ·  view source on GitHub ↗

Store CREATE TABLE metadata including type mappings and numeric constraints

(
        &self,
        conn: &rusqlite::Connection,
        table_name: &str,
        type_mappings: &std::collections::HashMap<String, crate::metadata::TypeMapping>
    )

Source from the content-addressed store, hash-verified

2748
2749 /// Store CREATE TABLE metadata including type mappings and numeric constraints
2750 fn store_create_table_metadata(
2751 &self,
2752 conn: &rusqlite::Connection,
2753 table_name: &str,
2754 type_mappings: &std::collections::HashMap<String, crate::metadata::TypeMapping>
2755 ) -> Result<(), rusqlite::Error> {
2756 debug!("Storing CREATE TABLE metadata: {} type mappings found", type_mappings.len());
2757
2758 // Initialize the metadata table if it doesn't exist
2759 let init_query = "CREATE TABLE IF NOT EXISTS __pgsqlite_schema (
2760 table_name TEXT NOT NULL,
2761 column_name TEXT NOT NULL,
2762 pg_type TEXT NOT NULL,
2763 sqlite_type TEXT NOT NULL,
2764 PRIMARY KEY (table_name, column_name)
2765 )";
2766
2767 conn.execute(init_query, [])?;
2768
2769 // Initialize numeric constraints table if it doesn't exist
2770 let init_numeric_query = "CREATE TABLE IF NOT EXISTS __pgsqlite_numeric_constraints (
2771 table_name TEXT NOT NULL,
2772 column_name TEXT NOT NULL,
2773 precision INTEGER NOT NULL,
2774 scale INTEGER NOT NULL,
2775 PRIMARY KEY (table_name, column_name)
2776 )";
2777
2778 conn.execute(init_numeric_query, [])?;
2779
2780 // Store each type mapping
2781 for (full_column, type_mapping) in type_mappings {
2782 // Split table.column format
2783 let parts: Vec<&str> = full_column.split('.').collect();
2784 if parts.len() == 2 && parts[0] == table_name {
2785 let insert_query = format!(
2786 "INSERT OR REPLACE INTO __pgsqlite_schema (table_name, column_name, pg_type, sqlite_type) VALUES ('{}', '{}', '{}', '{}')",
2787 table_name, parts[1], type_mapping.pg_type, type_mapping.sqlite_type
2788 );
2789
2790 if let Err(e) = conn.execute(&insert_query, []) {
2791 debug!("Failed to store metadata for {}.{}: {}", table_name, parts[1], e);
2792 } else {
2793 debug!("Stored metadata: {}.{} -> {} ({})", table_name, parts[1], type_mapping.pg_type, type_mapping.sqlite_type);
2794 }
2795
2796 // Store numeric constraints if present
2797 debug!("Checking for numeric constraints: pg_type={}, modifier={:?}", type_mapping.pg_type, type_mapping.type_modifier);
2798 if let Some(modifier) = type_mapping.type_modifier {
2799 // Extract base type without parameters
2800 let base_type = if let Some(paren_pos) = type_mapping.pg_type.find('(') {
2801 type_mapping.pg_type[..paren_pos].trim()
2802 } else {
2803 &type_mapping.pg_type
2804 };
2805 let pg_type_lower = base_type.to_lowercase();
2806
2807 if pg_type_lower == "numeric" || pg_type_lower == "decimal" {

Callers 1

Calls 2

executeMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected