oxigdal-gpkg 0.1.6

Pure Rust GeoPackage (GPKG) reader for OxiGDAL - SQLite format parser without C dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! GeoPackage schema layer.
//!
//! Provides typed representations of the core GeoPackage tables defined by the
//! OGC GeoPackage Encoding Standard v1.3.1.

use crate::btree::{self, CellValue, MasterEntry};
use crate::error::GpkgError;
use crate::filter;
use crate::metadata::{GpkgMetadata, GpkgMetadataReference, MetadataScope, ReferenceScope};
use crate::sqlite_reader::SqliteReader;

/// A full-table scan result: one `(rowid, column_values)` tuple per row.
pub type TableScanRows = Vec<(i64, Vec<CellValue>)>;

/// The content type stored in a GeoPackage table.
#[derive(Debug, Clone, PartialEq)]
pub enum GpkgDataType {
    /// OGC Simple Features vector data.
    Features,
    /// Raster tile pyramid (imagery or elevation).
    Tiles,
    /// Non-spatial attribute data.
    Attributes,
}

impl GpkgDataType {
    /// Parse the `data_type` column value from `gpkg_contents`.
    ///
    /// Unknown strings fall back to [`GpkgDataType::Features`].
    pub fn parse_type(s: &str) -> Self {
        match s {
            "features" => Self::Features,
            "tiles" => Self::Tiles,
            "attributes" => Self::Attributes,
            _ => Self::Features,
        }
    }

    /// Return the canonical string used in `gpkg_contents`.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Features => "features",
            Self::Tiles => "tiles",
            Self::Attributes => "attributes",
        }
    }
}

/// A row from the `gpkg_contents` table.
#[derive(Debug, Clone)]
pub struct GpkgContents {
    /// Name of the user-data table.
    pub table_name: String,
    /// Logical content type.
    pub data_type: GpkgDataType,
    /// Human-readable identifier (may be the same as `table_name`).
    pub identifier: Option<String>,
    /// Human-readable description.
    pub description: Option<String>,
    /// Bounding box — western longitude.
    pub min_x: f64,
    /// Bounding box — southern latitude.
    pub min_y: f64,
    /// Bounding box — eastern longitude.
    pub max_x: f64,
    /// Bounding box — northern latitude.
    pub max_y: f64,
    /// Spatial reference system ID (references `gpkg_spatial_ref_sys`).
    pub srs_id: i32,
}

/// A row from the `gpkg_geometry_columns` table.
#[derive(Debug, Clone)]
pub struct GpkgGeometryColumn {
    /// User-data table that owns this geometry column.
    pub table_name: String,
    /// Name of the geometry column in `table_name`.
    pub column_name: String,
    /// OGC geometry type name, e.g. `"POINT"`, `"MULTIPOLYGON"`.
    pub geometry_type_name: String,
    /// Spatial reference system ID.
    pub srs_id: i32,
    /// Z coordinate rule: 0 = prohibited, 1 = mandatory, 2 = optional.
    pub z: u8,
    /// M coordinate rule: 0 = prohibited, 1 = mandatory, 2 = optional.
    pub m: u8,
}

/// A row from the `gpkg_spatial_ref_sys` table.
#[derive(Debug, Clone)]
pub struct GpkgSrs {
    /// Human-readable name of the SRS.
    pub srs_name: String,
    /// Numeric SRS identifier (primary key).
    pub srs_id: i32,
    /// Defining organisation (e.g. `"EPSG"`).
    pub organization: String,
    /// Organisation-assigned CRS code.
    pub organization_coordsys_id: i32,
    /// WKT definition of the SRS.
    pub definition: String,
    /// Optional human-readable description.
    pub description: Option<String>,
}

/// A parsed GeoPackage file.
///
/// Wraps the underlying [`SqliteReader`] and exposes GeoPackage-specific
/// metadata discovered from the standard system tables.
pub struct GeoPackage {
    /// Low-level SQLite reader.
    pub reader: SqliteReader,
    /// Rows from `gpkg_contents` populated during construction.
    pub contents: Vec<GpkgContents>,
}

impl GeoPackage {
    /// Open a GeoPackage from its raw file bytes.
    ///
    /// # Errors
    /// Returns an error when the bytes do not represent a valid SQLite file.
    pub fn from_bytes(data: Vec<u8>) -> Result<Self, GpkgError> {
        let reader = SqliteReader::from_bytes(data)?;
        Ok(Self {
            reader,
            contents: Vec::new(),
        })
    }

    /// Open a GeoPackage from a main database file and an optional WAL file.
    pub fn from_files(main: Vec<u8>, wal: Option<Vec<u8>>) -> Result<Self, GpkgError> {
        let data = if let Some(wal_bytes) = wal {
            crate::wal::overlay_wal(&main, &wal_bytes)?
        } else {
            main
        };
        Self::from_bytes(data)
    }

    /// Return `true` when the file appears to be a well-formed GeoPackage.
    ///
    /// Accepts files whose application_id matches `"GPKG"` *or* whose SQLite
    /// structure is valid, to accommodate pre-1.2 files.
    pub fn is_valid_gpkg(&self) -> bool {
        self.reader.header.is_geopackage() || self.reader.is_valid()
    }

    /// Return the page size of the underlying SQLite database.
    pub fn page_size(&self) -> u32 {
        self.reader.header.page_size
    }

    /// Return the total page count of the underlying SQLite database.
    pub fn page_count(&self) -> u32 {
        self.reader.page_count()
    }

    /// Return `true` if the `application_id` field equals the GeoPackage magic.
    pub fn has_gpkg_application_id(&self) -> bool {
        self.reader.header.application_id == 0x4750_4B47
    }

    // ── B-tree / SQLite master scans ────────────────────────────────────────

    /// Scan the `sqlite_master` system table (always rooted at page 1) and
    /// return every entry — tables, indices, views, and triggers.
    ///
    /// # Errors
    /// Returns an error if the underlying SQLite B-tree pages are malformed or
    /// truncated.
    pub fn scan_sqlite_master(&self) -> Result<Vec<MasterEntry>, GpkgError> {
        btree::scan_sqlite_master(
            self.reader.raw_data(),
            self.reader.header.page_size as usize,
        )
    }

    /// Perform a full scan of a table B-tree starting at `root_page`.
    ///
    /// Returns `(rowid, column_values)` for every row, traversing any number of
    /// interior pages to reach leaf pages.
    ///
    /// # Errors
    /// Returns an error if `root_page` is out of range or any B-tree page is
    /// malformed.
    pub fn scan_table(&self, root_page: u32) -> Result<TableScanRows, GpkgError> {
        btree::scan_table(
            self.reader.raw_data(),
            root_page,
            self.reader.header.page_size as usize,
        )
    }

    /// Look up a user table's root page number by name, then scan it.
    ///
    /// Returns `Ok(None)` when no `sqlite_master` entry matches `table_name`.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan or the subsequent table
    /// scan fails.
    pub fn scan_table_by_name(&self, table_name: &str) -> Result<Option<TableScanRows>, GpkgError> {
        let master = self.scan_sqlite_master()?;
        for entry in master {
            if entry.entry_type == "table" && entry.name == table_name {
                if entry.rootpage == 0 {
                    return Ok(Some(Vec::new()));
                }
                return Ok(Some(self.scan_table(entry.rootpage)?));
            }
        }
        Ok(None)
    }

    /// Scan a named table with offset/limit pagination.
    ///
    /// Returns `(rowid, columns)` pairs in B-tree (rowid ascending) order.
    /// Returns `Ok(None)` when the table is not found in `sqlite_master`.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan or the B-tree traversal fails.
    pub fn scan_table_paginated(
        &self,
        table_name: &str,
        offset: usize,
        limit: usize,
    ) -> Result<Option<TableScanRows>, GpkgError> {
        let master = btree::scan_sqlite_master(
            self.reader.raw_data(),
            self.reader.header.page_size as usize,
        )?;
        for entry in master {
            if entry.entry_type == "table" && entry.name == table_name {
                if entry.rootpage == 0 || limit == 0 {
                    return Ok(Some(Vec::new()));
                }
                let rows = btree::scan_table_paginated(
                    self.reader.raw_data(),
                    entry.rootpage,
                    self.reader.header.page_size as usize,
                    offset,
                    limit,
                )?;
                return Ok(Some(rows));
            }
        }
        Ok(None)
    }

    /// Scan a named table, returning only rows that satisfy a [`filter::FilterExpr`].
    ///
    /// The filter is evaluated against each decoded row after the leaf-page is
    /// parsed; non-matching rows are discarded before being included in the
    /// result.  Rows are returned in B-tree (rowid ascending) order.
    ///
    /// Returns `Ok(None)` when the table is not found in `sqlite_master`.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan or the B-tree traversal fails.
    pub fn scan_table_filtered(
        &self,
        table_name: &str,
        expr: &filter::FilterExpr,
    ) -> Result<Option<TableScanRows>, GpkgError> {
        let master = btree::scan_sqlite_master(
            self.reader.raw_data(),
            self.reader.header.page_size as usize,
        )?;
        for entry in master {
            if entry.entry_type == "table" && entry.name == table_name {
                if entry.rootpage == 0 {
                    return Ok(Some(Vec::new()));
                }
                let rows = btree::scan_table_filtered(
                    self.reader.raw_data(),
                    entry.rootpage,
                    self.reader.header.page_size as usize,
                    expr,
                )?;
                return Ok(Some(rows));
            }
        }
        Ok(None)
    }

    /// Scan a named table with a filter and post-filter offset/limit pagination.
    ///
    /// Rows are first matched against `expr`, then `offset` matching rows are
    /// skipped, and at most `limit` matching rows are returned.  Both `offset`
    /// and `limit` are relative to the post-filter row stream.
    ///
    /// Returns `Ok(None)` when the table is not found in `sqlite_master`.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan or the B-tree traversal fails.
    pub fn scan_table_filtered_paginated(
        &self,
        table_name: &str,
        expr: &filter::FilterExpr,
        offset: usize,
        limit: usize,
    ) -> Result<Option<TableScanRows>, GpkgError> {
        let master = btree::scan_sqlite_master(
            self.reader.raw_data(),
            self.reader.header.page_size as usize,
        )?;
        for entry in master {
            if entry.entry_type == "table" && entry.name == table_name {
                if entry.rootpage == 0 || limit == 0 {
                    return Ok(Some(Vec::new()));
                }
                let rows = btree::scan_table_filtered_paginated(
                    self.reader.raw_data(),
                    entry.rootpage,
                    self.reader.header.page_size as usize,
                    expr,
                    offset,
                    limit,
                )?;
                return Ok(Some(rows));
            }
        }
        Ok(None)
    }

    /// Count total rows in a named table.
    ///
    /// Returns `Ok(None)` when the table is not found in `sqlite_master`.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan or the B-tree traversal fails.
    pub fn count_table_rows(&self, table_name: &str) -> Result<Option<u64>, GpkgError> {
        let master = btree::scan_sqlite_master(
            self.reader.raw_data(),
            self.reader.header.page_size as usize,
        )?;
        for entry in master {
            if entry.entry_type == "table" && entry.name == table_name {
                if entry.rootpage == 0 {
                    return Ok(Some(0));
                }
                let count = btree::count_table_rows(
                    self.reader.raw_data(),
                    entry.rootpage,
                    self.reader.header.page_size as usize,
                )?;
                return Ok(Some(count));
            }
        }
        Ok(None)
    }

    /// Populate `self.contents` by scanning the `gpkg_contents` system table.
    ///
    /// The canonical column layout of `gpkg_contents` is:
    ///
    /// | # | column         | type      |
    /// |---|----------------|-----------|
    /// | 0 | `table_name`   | TEXT      |
    /// | 1 | `data_type`    | TEXT      |
    /// | 2 | `identifier`   | TEXT      |
    /// | 3 | `description`  | TEXT      |
    /// | 4 | `last_change`  | DATETIME  |
    /// | 5 | `min_x`        | REAL      |
    /// | 6 | `min_y`        | REAL      |
    /// | 7 | `max_x`        | REAL      |
    /// | 8 | `max_y`        | REAL      |
    /// | 9 | `srs_id`       | INTEGER   |
    ///
    /// Returns the number of rows loaded.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan fails or the
    /// `gpkg_contents` table is not present and cannot be scanned.
    pub fn load_contents(&mut self) -> Result<usize, GpkgError> {
        let rows = self
            .scan_table_by_name("gpkg_contents")?
            .unwrap_or_default();

        let mut contents = Vec::with_capacity(rows.len());
        for (_rowid, values) in rows {
            if values.len() < 10 {
                continue; // skip malformed row
            }
            let table_name = cell_to_string(&values[0]);
            let data_type = GpkgDataType::parse_type(&cell_to_string(&values[1]));
            let identifier = cell_to_optional_string(&values[2]);
            let description = cell_to_optional_string(&values[3]);
            // values[4] is last_change (TEXT/datetime) — skipped
            let min_x = cell_to_f64(&values[5]);
            let min_y = cell_to_f64(&values[6]);
            let max_x = cell_to_f64(&values[7]);
            let max_y = cell_to_f64(&values[8]);
            let srs_id = cell_to_i32(&values[9]);

            contents.push(GpkgContents {
                table_name,
                data_type,
                identifier,
                description,
                min_x,
                min_y,
                max_x,
                max_y,
                srs_id,
            });
        }

        let count = contents.len();
        self.contents = contents;
        Ok(count)
    }

    /// Load all rows from the `gpkg_extensions` system table.
    ///
    /// Returns an empty vector when the table is absent.
    pub fn load_extensions(&mut self) -> Result<Vec<crate::extensions::GpkgExtension>, GpkgError> {
        let rows = match self.scan_table_by_name("gpkg_extensions")? {
            Some(r) => r,
            None => return Ok(Vec::new()),
        };

        let mut exts = Vec::with_capacity(rows.len());
        for (_rowid, values) in rows {
            if values.len() < 5 {
                continue;
            }
            let table_name = cell_to_optional_string(&values[0]);
            let column_name = cell_to_optional_string(&values[1]);
            let extension_name = cell_to_string(&values[2]);
            let definition = cell_to_string(&values[3]);
            let scope_str = cell_to_string(&values[4]);
            let scope = scope_str
                .parse::<crate::extensions::ExtensionScope>()
                .unwrap_or(crate::extensions::ExtensionScope::ReadWrite);
            exts.push(crate::extensions::GpkgExtension {
                table_name,
                column_name,
                extension_name,
                definition,
                scope,
            });
        }
        Ok(exts)
    }

    /// Load all rows from the `gpkg_metadata` system table (OGC §10.8, Table 16).
    ///
    /// Returns an empty vector when the `gpkg_metadata` table is absent from
    /// the GeoPackage (the table is optional per the OGC specification).
    ///
    /// Column layout expected in the B-tree (positions are 0-based):
    ///
    /// | # | Column            | SQLite type |
    /// |---|-------------------|-------------|
    /// | 0 | `id`              | INTEGER     |
    /// | 1 | `md_scope`        | TEXT        |
    /// | 2 | `md_standard_uri` | TEXT        |
    /// | 3 | `mime_type`       | TEXT        |
    /// | 4 | `metadata`        | TEXT        |
    ///
    /// Rows with fewer than 5 columns are silently skipped to defend against
    /// schema version mismatches.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan or the B-tree traversal
    /// of `gpkg_metadata` fails for reasons other than a missing table.
    pub fn load_metadata(&self) -> Result<Vec<GpkgMetadata>, GpkgError> {
        let rows = match self.scan_table_by_name("gpkg_metadata")? {
            Some(r) => r,
            None => return Ok(Vec::new()),
        };

        let mut result = Vec::with_capacity(rows.len());
        for (_rowid, values) in rows {
            if values.len() < 5 {
                // Malformed or schema-version-mismatched row — skip gracefully.
                continue;
            }

            let id = cell_to_i64(&values[0]);
            let md_scope = cell_to_string(&values[1])
                .parse::<MetadataScope>()
                .unwrap_or(MetadataScope::Undefined);
            let md_standard_uri = cell_to_string(&values[2]);
            let mime_type = cell_to_string(&values[3]);
            let metadata = cell_to_string(&values[4]);

            result.push(GpkgMetadata {
                id,
                md_scope,
                md_standard_uri,
                mime_type,
                metadata,
            });
        }

        Ok(result)
    }

    /// Load all rows from `gpkg_relations` (OGC GPKG-RTE §2.4).
    ///
    /// Returns an empty vector when the `gpkg_relations` table is absent from
    /// the GeoPackage (the table is present only when the Related Tables
    /// Extension is in use).
    ///
    /// Column layout expected in the B-tree (positions are 0-based):
    ///
    /// | # | Column                   | SQLite type |
    /// |---|--------------------------|-------------|
    /// | 0 | `id`                     | INTEGER     |
    /// | 1 | `base_table_name`        | TEXT        |
    /// | 2 | `base_primary_column`    | TEXT        |
    /// | 3 | `related_table_name`     | TEXT        |
    /// | 4 | `related_primary_column` | TEXT        |
    /// | 5 | `relation_name`          | TEXT        |
    /// | 6 | `mapping_table_name`     | TEXT        |
    ///
    /// Rows with fewer than 6 columns are silently skipped to defend against
    /// schema version mismatches.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan or the B-tree traversal
    /// of `gpkg_relations` fails for reasons other than a missing table.
    pub fn load_relations(&self) -> Result<Vec<crate::related_tables::GpkgRelation>, GpkgError> {
        let rows = match self.scan_table_by_name("gpkg_relations")? {
            Some(r) => r,
            None => return Ok(Vec::new()),
        };

        let mut out = Vec::with_capacity(rows.len());
        for (rowid, values) in rows {
            // We need at least 6 columns; with the INTEGER `id` as column 0
            // that means 7 values total.  With only 6 values the `id` column
            // must have been omitted (non-standard) so we fall back to the
            // rowid from the B-tree.
            let has_id_col = values.len() >= 7;

            // Need at least 6 meaningful text columns.
            if values.len() < 6 {
                continue;
            }

            let id = if has_id_col {
                cell_to_i64(&values[0])
            } else {
                rowid
            };
            let base_off: usize = if has_id_col { 1 } else { 0 };

            let base_table_name = cell_to_string(&values[base_off]);
            let base_primary_column = cell_to_string(&values[base_off + 1]);
            let related_table_name = cell_to_string(&values[base_off + 2]);
            let related_primary_column = cell_to_string(&values[base_off + 3]);
            // FromStr for RelationType is infallible (Err = Infallible).
            let relation_name = cell_to_string(&values[base_off + 4])
                .parse::<crate::related_tables::RelationType>()
                .unwrap_or_else(|e| match e {});
            let mapping_table_name = cell_to_string(&values[base_off + 5]);

            out.push(crate::related_tables::GpkgRelation {
                id,
                base_table_name,
                base_primary_column,
                related_table_name,
                related_primary_column,
                relation_name,
                mapping_table_name,
            });
        }
        Ok(out)
    }

    /// Load all rows from a GPKG-RTE mapping table.
    ///
    /// Mapping tables store `(base_id, related_id)` pairs that implement the
    /// many-to-many join described by a [`crate::related_tables::GpkgRelation`].
    /// The table name to pass is the `mapping_table_name` field of the relevant
    /// relation row.
    ///
    /// Returns an empty vector when the named table is absent from the
    /// GeoPackage.
    ///
    /// Expected column layout (0-based):
    ///
    /// | # | Column       | SQLite type |
    /// |---|--------------|-------------|
    /// | 0 | `id`         | INTEGER     |
    /// | 1 | `base_id`    | INTEGER     |
    /// | 2 | `related_id` | INTEGER     |
    ///
    /// When only two columns are found the B-tree rowid is used as the `id`,
    /// and the two columns are treated as `base_id` and `related_id`.
    /// Rows with fewer than 2 columns are silently skipped.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan or B-tree traversal fails
    /// for reasons other than a missing table.
    pub fn load_mapping_table(
        &self,
        table_name: &str,
    ) -> Result<Vec<crate::related_tables::MappingRow>, GpkgError> {
        let rows = match self.scan_table_by_name(table_name)? {
            Some(r) => r,
            None => return Ok(Vec::new()),
        };

        let mut out = Vec::with_capacity(rows.len());
        for (rowid, values) in rows {
            let (id, base_id, related_id) = if values.len() >= 3 {
                (
                    cell_to_i64(&values[0]),
                    cell_to_i64(&values[1]),
                    cell_to_i64(&values[2]),
                )
            } else if values.len() == 2 {
                (rowid, cell_to_i64(&values[0]), cell_to_i64(&values[1]))
            } else {
                // Fewer than 2 value columns — cannot form a valid mapping row.
                continue;
            };
            out.push(crate::related_tables::MappingRow {
                id,
                base_id,
                related_id,
            });
        }
        Ok(out)
    }

    /// Load all rows from the `gpkg_metadata_reference` system table
    /// (OGC §10.8.5, Table 18).
    ///
    /// Returns an empty vector when the table is absent (the table is optional
    /// per the OGC specification).
    ///
    /// Column layout expected in the B-tree (positions are 0-based):
    ///
    /// | # | Column            | SQLite type |
    /// |---|-------------------|-------------|
    /// | 0 | `reference_scope` | TEXT        |
    /// | 1 | `table_name`      | TEXT NULL   |
    /// | 2 | `column_name`     | TEXT NULL   |
    /// | 3 | `row_id_value`    | INTEGER NULL|
    /// | 4 | `timestamp`       | TEXT        |
    /// | 5 | `md_file_id`      | INTEGER     |
    /// | 6 | `md_parent_id`    | INTEGER NULL|
    ///
    /// Rows with fewer than 7 columns are silently skipped.
    ///
    /// # Errors
    /// Returns an error if the `sqlite_master` scan or the B-tree traversal
    /// of `gpkg_metadata_reference` fails for reasons other than a missing table.
    pub fn load_metadata_references(&self) -> Result<Vec<GpkgMetadataReference>, GpkgError> {
        let rows = match self.scan_table_by_name("gpkg_metadata_reference")? {
            Some(r) => r,
            None => return Ok(Vec::new()),
        };

        let mut result = Vec::with_capacity(rows.len());
        for (_rowid, values) in rows {
            if values.len() < 7 {
                // Malformed or schema-version-mismatched row — skip gracefully.
                continue;
            }

            let reference_scope = cell_to_string(&values[0])
                .parse::<ReferenceScope>()
                .unwrap_or(ReferenceScope::GeoPackage);
            let table_name = cell_to_optional_string(&values[1]);
            let column_name = cell_to_optional_string(&values[2]);
            let row_id_value = cell_to_optional_i64(&values[3]);
            let timestamp = cell_to_string(&values[4]);
            let md_file_id = cell_to_i64(&values[5]);
            let md_parent_id = cell_to_optional_i64(&values[6]);

            result.push(GpkgMetadataReference {
                reference_scope,
                table_name,
                column_name,
                row_id_value,
                timestamp,
                md_file_id,
                md_parent_id,
            });
        }

        Ok(result)
    }
}

// ── Cell-value coercion helpers ─────────────────────────────────────────────

fn cell_to_string(v: &CellValue) -> String {
    match v {
        CellValue::Text(s) => s.clone(),
        CellValue::Integer(i) => i.to_string(),
        CellValue::Float(f) => f.to_string(),
        CellValue::Blob(b) => String::from_utf8_lossy(b).into_owned(),
        CellValue::Null => String::new(),
    }
}

fn cell_to_optional_string(v: &CellValue) -> Option<String> {
    match v {
        CellValue::Null => None,
        CellValue::Text(s) if s.is_empty() => None,
        other => Some(cell_to_string(other)),
    }
}

fn cell_to_f64(v: &CellValue) -> f64 {
    match v {
        CellValue::Float(f) => *f,
        CellValue::Integer(i) => *i as f64,
        _ => 0.0,
    }
}

fn cell_to_i32(v: &CellValue) -> i32 {
    match v {
        CellValue::Integer(i) => {
            if *i > i32::MAX as i64 {
                i32::MAX
            } else if *i < i32::MIN as i64 {
                i32::MIN
            } else {
                *i as i32
            }
        }
        _ => 0,
    }
}

/// Coerce a [`CellValue`] to `i64`, returning 0 for non-integer types.
pub(crate) fn cell_to_i64(v: &CellValue) -> i64 {
    match v {
        CellValue::Integer(i) => *i,
        CellValue::Float(f) => *f as i64,
        _ => 0,
    }
}

/// Coerce a [`CellValue`] to `Option<i64>`, returning `None` for SQL NULL.
pub(crate) fn cell_to_optional_i64(v: &CellValue) -> Option<i64> {
    match v {
        CellValue::Null => None,
        CellValue::Integer(i) => Some(*i),
        CellValue::Float(f) => Some(*f as i64),
        _ => None,
    }
}