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
mod attribute;
mod error_field;

pub use attribute::*;
pub use error_field::*;

#[derive(Clone)]
pub struct Result {
    result: *mut pq_sys::PGresult,
}

impl Result {
    /**
     * Constructs an empty `Result` object with the given status.
     *
     * See
     * [PQmakeEmptyPGresult](https://www.postgresql.org/docs/current/libpq-misc.html#LIBPQ-PQmakeEmptyPGresult).
     */
    pub fn new(conn: &crate::Connection, status: crate::Status) -> Self {
        let result = unsafe { pq_sys::PQmakeEmptyPGresult(conn.into(), status.into()) };

        result.into()
    }

    /**
     * Returns the result status of the command.
     *
     * See [PQresultStatus](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQRESULTSTATUS).
     */
    pub fn status(&self) -> crate::Status {
        unsafe { pq_sys::PQresultStatus(self.into()) }.into()
    }

    /**
     * Returns the error message associated with the command, or an empty string if there was no error.
     *
     * See [PQresultErrorMessage](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQRESULTERRORMESSAGE).
     */
    pub fn error_message(&self) -> Option<String> {
        crate::ffi::to_option_string(unsafe { pq_sys::PQresultErrorMessage(self.into()) })
    }

    /**
     * Returns a reformatted version of the error message associated with a `libpq::Result` object.
     *
     * See [PQresultErrorField](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQRESULTERRORFIELD).
     */
    pub fn error_field(&self, field: crate::result::ErrorField) -> Option<&'static str> {
        unsafe {
            let ptr = pq_sys::PQresultErrorField(self.into(), field.into());

            if ptr.is_null() {
                return None;
            }

            crate::ffi::to_option_str(ptr)
        }
    }

    /**
     * Returns the number of rows (tuples) in the query result.
     *
     * See [PQntuples](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQNTUPLES).
     */
    pub fn ntuples(&self) -> usize {
        unsafe { pq_sys::PQntuples(self.into()) as usize }
    }

    /**
     * Returns the number of columns (fields) in each row of the query result.
     *
     * See [PQnfields](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQNFIELDS).
     */
    pub fn nfields(&self) -> usize {
        unsafe { pq_sys::PQnfields(self.into()) as usize }
    }

    /**
     * Returns the column name associated with the given column number.
     *
     * See [PQfname](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQFNAME).
     */
    pub fn field_name(&self, number: usize) -> Option<String> {
        let raw = unsafe { pq_sys::PQfname(self.into(), number as i32) };

        if raw.is_null() {
            None
        } else {
            Some(crate::ffi::to_string(raw))
        }
    }

    /**
     * Returns the column number associated with the given column name.
     *
     * See [PQfnumber](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQFNUMBER).
     */
    pub fn field_number(&self, name: &str) -> Option<usize> {
        let c_name = crate::ffi::to_cstr(name);
        let number = unsafe { pq_sys::PQfnumber(self.into(), c_name.as_ptr()) };

        if number == -1 {
            None
        } else {
            Some(number as usize)
        }
    }

    /**
     * Returns the OID of the table from which the given column was fetched.
     *
     * See [PQftable](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQFTABLE).
     */
    pub fn field_table(&self, column: usize) -> Option<crate::Oid> {
        let oid = unsafe { pq_sys::PQftable(self.into(), column as i32) };

        if oid == crate::oid::INVALID {
            None
        } else {
            Some(oid)
        }
    }

    /**
     * Returns the column number (within its table) of the column making up the specified query
     * result column.
     *
     * See [PQftablecol](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQFTABLECOL).
     */
    pub fn field_tablecol(&self, column: usize) -> usize {
        unsafe { pq_sys::PQftablecol(self.into(), column as i32) as usize }
    }

    /**
     * Returns the format code indicating the format of the given column.
     *
     * See [PQfformat](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQFFORMAT).
     */
    pub fn field_format(&self, column: usize) -> crate::Format {
        unsafe { pq_sys::PQfformat(self.into(), column as i32) }.into()
    }

    /**
     * Returns the data type associated with the given column number.
     *
     * See [PQftype](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQFTYPE).
     */
    pub fn field_type(&self, column: usize) -> crate::Oid {
        unsafe { pq_sys::PQftype(self.into(), column as i32) }
    }

    /**
     * Returns the type modifier of the column associated with the given column number.
     *
     * See [PQfmod](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQFMOD).
     */
    pub fn field_mod(&self, column: usize) -> Option<i32> {
        let raw = unsafe { pq_sys::PQfmod(self.into(), column as i32) };

        if raw < 0 {
            None
        } else {
            Some(raw)
        }
    }

    /**
     * Returns the size in bytes of the column associated with the given column number.
     *
     * `None` indicates the data type is variable-length.
     *
     * See [PQfsize](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQFSIZE).
     */
    pub fn field_size(&self, column: usize) -> Option<usize> {
        let raw = unsafe { pq_sys::PQfsize(self.into(), column as i32) };

        if raw < 0 {
            None
        } else {
            Some(raw as usize)
        }
    }

    /**
     * Returns `true` if the `Result` contains binary data and `false` if it contains text data.
     *
     * See
     * [PQbinaryTuples](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQBINARYTUPLES).
     */
    pub fn binary_tuples(&self) -> bool {
        unsafe { pq_sys::PQbinaryTuples(self.into()) == 1 }
    }

    /**
     * Returns a single field value of one row of a `Result`.
     *
     * See [PQgetvalue](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQGETVALUE).
     */
    pub fn value(&self, row: usize, column: usize) -> Option<&[u8]> {
        if self.is_null(row, column) {
            None
        } else {
            let slice = unsafe {
                let raw = pq_sys::PQgetvalue(self.into(), row as i32, column as i32) as *const u8;
                let length = self.length(row, column);

                std::slice::from_raw_parts(raw, length)
            };

            Some(slice)
        }
    }

    /**
     * Tests a field for a null value.
     *
     * See [PQgetisnull](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQGETISNULL).
     */
    pub fn is_null(&self, row: usize, column: usize) -> bool {
        unsafe { pq_sys::PQgetisnull(self.into(), row as i32, column as i32) == 1 }
    }

    /**
     * Returns the actual length of a field value in bytes.
     *
     * See [PQgetlength](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQGETLENGTH).
     */
    pub fn length(&self, row: usize, column: usize) -> usize {
        unsafe { pq_sys::PQgetlength(self.into(), row as i32, column as i32) as usize }
    }

    /**
     * Returns the number of parameters of a prepared statement.
     *
     * See [PQnparams](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQNPARAMS).
     */
    pub fn nparams(&self) -> usize {
        unsafe { pq_sys::PQnparams(self.into()) as usize }
    }

    /**
     * Returns the data type of the indicated statement parameter.
     *
     * See [PQparamtype](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQPARAMTYPE).
     */
    pub fn param_type(&self, param: usize) -> Option<crate::Oid> {
        let oid = unsafe { pq_sys::PQparamtype(self.into(), param as i32) };

        if oid == crate::oid::INVALID {
            None
        } else {
            Some(oid)
        }
    }

    /**
     * Prints out all the rows and, optionally, the column names to the specified output stream.
     *
     * See [PQprint](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQPRINT).
     */
    #[cfg(unix)]
    pub fn print(&self, output: &dyn std::os::unix::io::AsRawFd, option: &crate::print::Options) {
        let c_mode = crate::ffi::to_cstr("w");

        let (_c_field_name, ptr_field_name) = crate::ffi::vec_to_nta(&option.field_name);

        let c_field_sep = crate::ffi::to_cstr(&option.field_sep);
        let c_table_opt = crate::ffi::to_cstr(&option.table_opt);
        let c_caption = crate::ffi::to_cstr(&option.caption);

        let c_option = pq_sys::_PQprintOpt {
            header: option.header as pq_sys::pqbool,
            align: option.align as pq_sys::pqbool,
            standard: option.standard as pq_sys::pqbool,
            html3: option.html3 as pq_sys::pqbool,
            expanded: option.expanded as pq_sys::pqbool,
            pager: option.pager as pq_sys::pqbool,
            fieldSep: c_field_sep.as_ptr() as *mut libc::c_char,
            tableOpt: c_table_opt.as_ptr() as *mut libc::c_char,
            caption: c_caption.as_ptr() as *mut libc::c_char,
            fieldName: ptr_field_name.as_ptr() as *mut *mut libc::c_char,
        };

        unsafe {
            let stream = libc::fdopen(output.as_raw_fd(), c_mode.as_ptr());

            pq_sys::PQprint(stream as *mut _, self.into(), &c_option);
        }
    }

    /**
     * Returns the command status tag from the SQL command that generated the `Result`.
     *
     * See [PQcmdStatus](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQCMDSTATUS).
     */
    pub fn cmd_status(&self) -> Option<String> {
        crate::ffi::to_option_string(unsafe { pq_sys::PQcmdStatus(self.into()) })
    }

    /**
     * Returns the number of rows affected by the SQL command.
     *
     * See [PQcmdTuples](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQCMDTUPLES).
     */
    pub fn cmd_tuples(&self) -> usize {
        let ntuples = crate::ffi::to_string(unsafe { pq_sys::PQcmdTuples(self.into()) });

        ntuples.parse().unwrap_or_default()
    }

    /**
     * Returns the OID of the inserted row.
     *
     * See [PQoidValue](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQOIDVALUE).
     */
    pub fn oid_value(&self) -> Option<crate::Oid> {
        let oid = unsafe { pq_sys::PQoidValue(self.into()) };

        if oid == crate::oid::INVALID {
            None
        } else {
            Some(oid)
        }
    }

    /**
     * See [PQoidStatus](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQOIDSTATUS).
     */
    #[deprecated(
        note = "This function is deprecated in favor of `libpq::Result::oid_value` and is not thread-safe."
    )]
    pub fn oid_status(&self) -> Option<String> {
        crate::ffi::to_option_string(unsafe { pq_sys::PQoidStatus(self.into()) })
    }

    /**
     * Makes a copy of a `Result` object.
     *
     * See
     * [PQcopyResult](https://www.postgresql.org/docs/current/libpq-misc.html#LIBPQ-PQCOPYRESULT).
     */
    pub fn copy(&self, flags: i32) -> std::result::Result<Self, ()> {
        let raw = unsafe { pq_sys::PQcopyResult(self.into(), flags) };

        if raw.is_null() {
            Err(())
        } else {
            Ok(raw.into())
        }
    }

    /**
     * Sets the attributes of a PGresult object.
     *
     * See
     * [PQsetResultAttrs](https://www.postgresql.org/docs/current/libpq-misc.html#LIBPQ-PQSETRESULTATTRS).
     */
    pub fn set_attrs(
        &mut self,
        attributes: &[&crate::result::Attribute],
    ) -> std::result::Result<(), ()> {
        let mut attr = attributes.iter().map(|x| x.into()).collect::<Vec<_>>();

        let success = unsafe {
            pq_sys::PQsetResultAttrs(self.into(), attributes.len() as i32, attr.as_mut_ptr())
        };

        if success == 0 {
            Err(())
        } else {
            Ok(())
        }
    }

    /**
     * Sets a tuple field value of a `Result` object.
     *
     * See [PQsetvalue](https://www.postgresql.org/docs/current/libpq-misc.html#LIBPQ-PQSETVALUE).
     */
    pub fn set_value(
        &mut self,
        tuple: usize,
        field: usize,
        value: Option<&str>,
    ) -> std::result::Result<(), ()> {
        let (v, len) = if let Some(v) = value {
            let cstring = std::ffi::CString::new(v).unwrap();
            (cstring.into_raw(), v.len() as i32)
        } else {
            (std::ptr::null_mut(), -1)
        };

        let success =
            unsafe { pq_sys::PQsetvalue(self.into(), tuple as i32, field as i32, v, len as i32) };

        if success == 0 {
            Err(())
        } else {
            Ok(())
        }
    }

    /**
     * Allocate subsidiary storage for a `Result` object.
     *
     * See
     * [PQresultAlloc](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQRESULTALLOC).
     *
     * # Safety
     *
     * This function return a `void*` pointer.
     */
    pub unsafe fn alloc(
        &mut self,
        nbytes: usize,
    ) -> std::result::Result<*mut core::ffi::c_void, ()> {
        let space = pq_sys::PQresultAlloc(self.into(), nbytes as pq_sys::size_t);

        if space.is_null() {
            Err(())
        } else {
            Ok(space)
        }
    }

    /**
     * Retrieves the number of bytes allocated for a `Result` object.
     *
     * See [PQresultMemorySize](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQRESULTMEMORYSIZE)
     */
    #[cfg(feature = "v12")]
    pub fn memory_size(&self) -> u64 {
        unsafe { pq_sys::PQresultMemorySize(self.into()) }
    }

    /**
     * Really old printing routines.
     */
    #[cfg(unix)]
    pub fn display_tuples(
        &self,
        file: std::fs::File,
        fill_align: bool,
        field_sep: Option<&str>,
        print_header: bool,
        quiet: bool,
    ) {
        use std::os::unix::io::IntoRawFd;

        let c_mode = crate::ffi::to_cstr("w");

        unsafe {
            let fp = libc::fdopen(file.into_raw_fd(), c_mode.as_ptr());

            let c_sep = field_sep.map(crate::ffi::to_cstr);
            let sep = if let Some(c_sep) = c_sep {
                c_sep.as_ptr()
            } else {
                std::ptr::null()
            };

            pq_sys::PQdisplayTuples(
                self.into(),
                fp as *mut _,
                fill_align as i32,
                sep,
                print_header as i32,
                quiet as i32,
            );
        }
    }
}

unsafe impl Send for Result {}

unsafe impl Sync for Result {}

impl Drop for Result {
    fn drop(&mut self) {
        unsafe { pq_sys::PQclear(self.into()) };
    }
}

#[doc(hidden)]
impl From<*mut pq_sys::PGresult> for Result {
    fn from(result: *mut pq_sys::PGresult) -> Self {
        Result { result }
    }
}

#[doc(hidden)]
impl From<&Result> for *mut pq_sys::PGresult {
    fn from(result: &Result) -> *mut pq_sys::PGresult {
        result.result
    }
}

#[doc(hidden)]
impl From<&mut Result> for *mut pq_sys::PGresult {
    fn from(result: &mut Result) -> *mut pq_sys::PGresult {
        result.result
    }
}

#[doc(hidden)]
impl From<&Result> for *const pq_sys::PGresult {
    fn from(result: &Result) -> *const pq_sys::PGresult {
        result.result
    }
}

impl std::fmt::Debug for Result {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Result")
            .field("inner", &self.result)
            .field("status", &self.status())
            .field("error_message", &self.error_message())
            .field("ntuples", &self.ntuples())
            .field("nfields", &self.nfields())
            .field("cmd_status", &self.cmd_status())
            .field("cmd_tuples", &self.cmd_tuples())
            .field("oid_value", &self.oid_value())
            .field("nparams", &self.nparams())
            .finish()
    }
}