oxigdal-postgis 0.1.5

PostgreSQL/PostGIS integration for OxiGDAL - Spatial database workflows with connection pooling and async operations
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
//! PostGIS writer for inserting features into database
//!
//! This module provides functionality to write features to PostGIS tables.

use crate::connection::ConnectionPool;
use crate::copy_binary::{CopyBinaryEncoder, ewkb_from_wkb};
use crate::error::{QueryError, Result};
use crate::sql::{ColumnName, TableName};
use crate::types::to_postgis;
use crate::wkb::WkbEncoder;
use futures::SinkExt;
use oxigdal_core::vector::feature::Feature;
use tracing::{debug, info, warn};

/// PostGIS feature writer
pub struct PostGisWriter {
    pool: ConnectionPool,
    table_name: String,
    geometry_column: String,
    srid: Option<i32>,
    create_table: bool,
    batch: Vec<Feature>,
    batch_size: usize,
}

impl PostGisWriter {
    /// Creates a new PostGIS writer
    pub fn new(pool: ConnectionPool, table_name: impl Into<String>) -> Self {
        Self {
            pool,
            table_name: table_name.into(),
            geometry_column: "geom".to_string(),
            srid: Some(4326),
            create_table: false,
            batch: Vec::new(),
            batch_size: 1000,
        }
    }

    /// Sets the geometry column name
    pub fn geometry_column(mut self, column: impl Into<String>) -> Self {
        self.geometry_column = column.into();
        self
    }

    /// Sets the SRID
    pub const fn srid(mut self, srid: i32) -> Self {
        self.srid = Some(srid);
        self
    }

    /// Enables automatic table creation
    pub const fn create_table(mut self, create: bool) -> Self {
        self.create_table = create;
        self
    }

    /// Sets the batch size for batch insertions
    pub const fn batch_size(mut self, size: usize) -> Self {
        self.batch_size = size;
        self
    }

    /// Creates the table if it doesn't exist
    pub async fn ensure_table(&self) -> Result<()> {
        let client = self.pool.get().await?;

        let table = TableName::new(&self.table_name)?;
        let geom_col = ColumnName::new(&self.geometry_column)?;

        // Create table
        let create_sql = format!(
            "CREATE TABLE IF NOT EXISTS {} (id SERIAL PRIMARY KEY, {} geometry, properties jsonb)",
            table.qualified(),
            geom_col.quoted()
        );

        debug!("Creating table: {create_sql}");

        client
            .execute(&create_sql, &[])
            .await
            .map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;

        // Add spatial index
        let index_sql = format!(
            "CREATE INDEX IF NOT EXISTS {}_{}_gist ON {} USING GIST ({})",
            self.table_name,
            self.geometry_column,
            table.qualified(),
            geom_col.quoted()
        );

        client
            .execute(&index_sql, &[])
            .await
            .map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;

        info!("Table {} created successfully", self.table_name);

        Ok(())
    }

    /// Inserts a single feature
    pub async fn insert(&mut self, feature: &Feature) -> Result<i64> {
        if self.create_table {
            self.ensure_table().await?;
        }

        let client = self.pool.get().await?;

        let geometry = feature
            .geometry
            .as_ref()
            .ok_or_else(|| QueryError::ExecutionFailed {
                message: "Feature has no geometry".to_string(),
            })?;

        let postgis_geom = to_postgis(geometry.clone(), self.srid);
        let properties =
            serde_json::to_value(&feature.properties).map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;

        let table = TableName::new(&self.table_name)?;
        let geom_col = ColumnName::new(&self.geometry_column)?;

        let sql = format!(
            "INSERT INTO {} ({}, properties) VALUES ($1, $2) RETURNING id",
            table.qualified(),
            geom_col.quoted()
        );

        let row = client
            .query_one(&sql, &[&postgis_geom, &properties])
            .await
            .map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;

        let id: i64 = row.get(0);
        Ok(id)
    }

    /// Adds a feature to the batch
    pub fn add_to_batch(&mut self, feature: Feature) {
        self.batch.push(feature);
    }

    /// Flushes the batch to the database.
    ///
    /// The fast path streams the whole batch through the PostgreSQL binary COPY
    /// protocol (`COPY ... FROM STDIN WITH (FORMAT binary)`), which avoids one
    /// round-trip per feature. If the server rejects the binary COPY for any
    /// reason the writer transparently degrades to the per-row INSERT path via
    /// `flush_via_inserts`, guaranteeing no data
    /// loss. On success the batch is cleared.
    pub async fn flush(&mut self) -> Result<usize> {
        if self.batch.is_empty() {
            return Ok(0);
        }

        if self.create_table {
            self.ensure_table().await?;
        }

        debug!("Flushing batch of {} features", self.batch.len());

        // Fast path: stream the batch through the binary COPY protocol.
        match self.flush_via_binary_copy().await {
            Ok(count) => {
                self.batch.clear();
                Ok(count)
            }
            Err(e) => {
                // Graceful degradation: a server that rejects binary COPY must
                // not cause data loss — replay the batch with per-row INSERTs.
                warn!("binary COPY failed ({e}); falling back to per-row INSERT");
                let count = self.flush_via_inserts().await?;
                self.batch.clear();
                Ok(count)
            }
        }
    }

    /// Streams the current batch to the database using the PostgreSQL binary
    /// COPY protocol.
    ///
    /// Builds a single binary-COPY payload for every feature that carries a
    /// geometry — field 1 is the geometry as EWKB (WKB with the SRID flag set),
    /// field 2 is the feature's properties serialised as a JSON document. The
    /// payload is sent to a `COPY ... FROM STDIN WITH (FORMAT binary)` sink.
    async fn flush_via_binary_copy(&self) -> Result<usize> {
        let client = self.pool.get().await?;

        let table = TableName::new(&self.table_name)?;
        let geom_col = ColumnName::new(&self.geometry_column)?;

        let copy_sql = format!(
            "COPY {} ({}, properties) FROM STDIN WITH (FORMAT binary)",
            table.qualified(),
            geom_col.quoted()
        );

        // Build the full binary-COPY payload for every feature that has a
        // geometry. PostGIS accepts EWKB for a `geometry` column and accepts
        // the UTF-8 text bytes of a JSON document for a `jsonb` column.
        let mut encoder = CopyBinaryEncoder::new();
        let mut count = 0usize;
        for feature in &self.batch {
            let Some(ref geometry) = feature.geometry else {
                continue;
            };

            // `WkbEncoder::new()` emits plain WKB; promote it to EWKB so the
            // SRID travels with the geometry. When `self.srid` is `None` we
            // still emit plain WKB (PostGIS will assume SRID 0).
            let mut wkb_encoder = WkbEncoder::new();
            let wkb = wkb_encoder.encode(geometry)?;
            let geom_field = match self.srid {
                Some(srid) => ewkb_from_wkb(&wkb, srid)?,
                None => wkb,
            };

            let properties = serde_json::to_value(&feature.properties).map_err(|e| {
                QueryError::ExecutionFailed {
                    message: e.to_string(),
                }
            })?;
            let properties_bytes =
                serde_json::to_vec(&properties).map_err(|e| QueryError::ExecutionFailed {
                    message: e.to_string(),
                })?;

            encoder.begin_row(2);
            encoder.write_field_bytes(&geom_field);
            encoder.write_field_bytes(&properties_bytes);
            count += 1;
        }

        let payload = encoder.finish();

        // `CopyInSink` is `!Unpin` — pin it on the stack before driving the
        // `Sink`. `bytes::Bytes` implements `Buf`, the bound `copy_in`
        // requires for the streamed item type.
        let sink = client
            .copy_in::<str, bytes::Bytes>(&copy_sql)
            .await
            .map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;
        tokio::pin!(sink);

        sink.as_mut()
            .send(bytes::Bytes::from(payload))
            .await
            .map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;

        sink.finish()
            .await
            .map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;

        Ok(count)
    }

    /// Flushes the current batch with one parameterised `INSERT` per feature.
    ///
    /// This is the graceful-degradation fallback used by [`flush`](Self::flush)
    /// when the binary COPY path fails. It does not clear `self.batch`; the
    /// caller is responsible for that.
    async fn flush_via_inserts(&self) -> Result<usize> {
        let client = self.pool.get().await?;

        let table = TableName::new(&self.table_name)?;
        let geom_col = ColumnName::new(&self.geometry_column)?;

        let mut count = 0;
        for feature in &self.batch {
            if let Some(ref geometry) = feature.geometry {
                let postgis_geom = to_postgis(geometry.clone(), self.srid);
                let properties = serde_json::to_value(&feature.properties).map_err(|e| {
                    QueryError::ExecutionFailed {
                        message: e.to_string(),
                    }
                })?;

                let sql = format!(
                    "INSERT INTO {} ({}, properties) VALUES ($1, $2)",
                    table.qualified(),
                    geom_col.quoted()
                );

                client
                    .execute(&sql, &[&postgis_geom, &properties])
                    .await
                    .map_err(|e| QueryError::ExecutionFailed {
                        message: e.to_string(),
                    })?;

                count += 1;
            }
        }

        Ok(count)
    }

    /// Updates a feature by ID
    pub async fn update(&self, id: i64, feature: &Feature) -> Result<u64> {
        let client = self.pool.get().await?;

        let geometry = feature
            .geometry
            .as_ref()
            .ok_or_else(|| QueryError::ExecutionFailed {
                message: "Feature has no geometry".to_string(),
            })?;

        let postgis_geom = to_postgis(geometry.clone(), self.srid);
        let properties =
            serde_json::to_value(&feature.properties).map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;

        let table = TableName::new(&self.table_name)?;
        let geom_col = ColumnName::new(&self.geometry_column)?;

        let sql = format!(
            "UPDATE {} SET {} = $1, properties = $2 WHERE id = $3",
            table.qualified(),
            geom_col.quoted()
        );

        let rows_affected = client
            .execute(&sql, &[&postgis_geom, &properties, &id])
            .await
            .map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;

        Ok(rows_affected)
    }

    /// Deletes a feature by ID
    pub async fn delete(&self, id: i64) -> Result<u64> {
        let client = self.pool.get().await?;

        let table = TableName::new(&self.table_name)?;

        let sql = format!("DELETE FROM {} WHERE id = $1", table.qualified());

        let rows_affected =
            client
                .execute(&sql, &[&id])
                .await
                .map_err(|e| QueryError::ExecutionFailed {
                    message: e.to_string(),
                })?;

        Ok(rows_affected)
    }

    /// Truncates the table
    pub async fn truncate(&self) -> Result<()> {
        let client = self.pool.get().await?;

        let table = TableName::new(&self.table_name)?;

        let sql = format!("TRUNCATE TABLE {}", table.qualified());

        client
            .execute(&sql, &[])
            .await
            .map_err(|e| QueryError::ExecutionFailed {
                message: e.to_string(),
            })?;

        info!("Table {} truncated", self.table_name);

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::connection::ConnectionConfig;

    #[test]
    fn test_writer_creation() {
        let config = ConnectionConfig::default();
        let pool = ConnectionPool::new(config).ok();
        assert!(pool.is_some());

        let pool = pool.expect("pool creation failed");
        let writer = PostGisWriter::new(pool, "test_table");

        assert_eq!(writer.table_name, "test_table");
        assert_eq!(writer.geometry_column, "geom");
        assert_eq!(writer.srid, Some(4326));
    }

    #[test]
    fn test_writer_configuration() {
        let config = ConnectionConfig::default();
        let pool = ConnectionPool::new(config).ok();
        assert!(pool.is_some());

        let pool = pool.expect("pool creation failed");
        let writer = PostGisWriter::new(pool, "test_table")
            .geometry_column("the_geom")
            .srid(3857)
            .create_table(true)
            .batch_size(500);

        assert_eq!(writer.geometry_column, "the_geom");
        assert_eq!(writer.srid, Some(3857));
        assert!(writer.create_table);
        assert_eq!(writer.batch_size, 500);
    }
}