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
use raw;
/**
* PqBytes is used as smart pointer to `std::ffi::c_void` pointer that was allocated by libpq.
*
* It frees the memory using libpq.PQfreemem when it is dropped.
*
* This struct is not cloneable since it has a raw pointer.
* If you need to clone it, you must convert it to a owned one by calling [`to_vec()`](#method.to_vec)
* or use [`std::rc::Rc`].
*
* # Examples
*
* ```
* // Connect to postgres
* let dsn = "host=localhost";
* # let dsn = std::env::var("PQ_DSN").unwrap_or_else(|_| "host=localhost".to_string());
* let conn = libpq::Connection::new(&dsn).expect("Failed to connect to postgres");
*
* // Create temporary table
* conn.exec("CREATE TEMPORARY TABLE tmp (id INTEGER);");
*
* // Create some data
* conn.exec("COPY tmp (id) FROM STDIN;");
* conn.put_copy_data(b"1\n2\n3\n4\n").expect("Error while sending data");
* conn.put_copy_end(None).expect("Error while sending end of data indication");
*
* // Read the data
* conn.exec("COPY tmp TO STDOUT;");
*
* // PqBytes implements Deref<Target = [u8]]>, so it is coerced to &[u8] slice ...
* let buffer = conn.copy_data(false).expect("Error while reading data");
* assert_eq!(&*buffer, b"1\n");
*
* // ... having all the same methods from &[u8] slice ...
* let buffer = conn.copy_data(false).expect("Error while reading data");
* assert_eq!(buffer.to_vec(), vec![b'2', b'\n']);
* assert_eq!(buffer.len(), 2);
* assert_eq!(buffer.last(), Some(&b'\n'));
* // ... and traits like Index ...
* assert_eq!(buffer[0], b'2');
*
* // ... or being used in any function that accepts &[u8] slice ...
* let buffer = conn.copy_data(false).expect("Error while reading data");
* fn work_on_u8_slice(b: &[u8]) {
* assert_eq!(b, b"3\n");
* }
* work_on_u8_slice(&buffer);
*
* // ... like String::from_utf8_lossy which requires a &[u8]
* let buffer = conn.copy_data(false).expect("Error while reading data");
* assert_eq!(String::from_utf8_lossy(&buffer), "4\n");
* ```
*
* See [`String::from_utf8_lossy`], [`crate::Connection::copy_data`] and [`slice`].
*/
/**
* PqString is used as smart pointer to `std::os::raw::c_char` pointer that was allocated by libpq.
*
* It frees the memory using libpq.PQfreemem when it is dropped.
*
* This struct is not cloneable since it has a raw pointer.
* If you need to clone it, you must convert to a owned one by calling
* [`to_owned()`](std::ffi::CStr::to_owned) or use [`std::rc::Rc`].
* # Examples
*
* ```
* # // Since there isn't a public method to create PqString, let's
* # // create a CString and transmute it to PqString.
* # let cstring = std::ffi::CString::new("Something returned by postgres").unwrap();
* # let s = unsafe {std::mem::transmute::<_, libpq::connection::PqString>(cstring.as_ptr())};
* // let s = /* ... "Something returned by postgres" ... */
*
* // PqString implements Deref<Target = CStr>, so it is coerced
* // to &CStr and it has all the same methods ...
* assert_eq!(s.to_bytes(), b"Something returned by postgres");
* assert_eq!(s.to_string_lossy(), "Something returned by postgres");
* // ... and traits like ToOwned
* assert_eq!(s.to_owned(), std::ffi::CString::new("Something returned by postgres").unwrap());
*
* // We can use to_str() to safe convert it a rust &str slice ...
* assert_eq!(s.to_str().ok(), Some("Something returned by postgres"));
* // .. and use to_string() on it to create a owned Rust String.
* assert_eq!(s.to_str().unwrap().to_string(), String::from("Something returned by postgres"));
*
* // Since PqString implements AsRef<[u8]>, is can be used in any method that requires &[u8] ...
* fn work_on_bytes<T: AsRef<[u8]>>(input: &T) {
* assert_eq!(input.as_ref(), b"Something returned by postgres");
* }
* work_on_bytes(&s);
* // .. including String::from_utf8_lossy
* assert_eq!(String::from_utf8_lossy(s.as_ref()), "Something returned by postgres");
*
* # // We don't want to libpq to free the memory that does not belong to it, so we must use ManuallyDrop.
* # std::mem::ManuallyDrop::new(s);
* ```
*
* See [`std::ffi::CStr`].
*/