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
pub(crate) fn literal(conn: &crate::Connection, str: &str) -> std::result::Result<String, String> {
    let c_str = crate::ffi::to_cstr(str);
    unsafe {
        let raw = pq_sys::PQescapeLiteral(conn.into(), c_str.as_ptr(), str.len() as u64);

        if raw.is_null() {
            return Err(conn
                .error_message()
                .unwrap_or_else(|| "Unknow error".to_string()));
        }

        let escaped = crate::ffi::to_string(raw);
        pq_sys::PQfreemem(raw as *mut std::ffi::c_void);

        Ok(escaped)
    }
}

/**
 * Escape a string for use as an SQL identifier, such as a table, column, or function name.
 *
 * See [PQescapeIdentifier](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQESCAPEIDENTIFIER).
 */
pub fn identifier(conn: &crate::Connection, str: &str) -> std::result::Result<String, String> {
    let c_str = crate::ffi::to_cstr(str);
    unsafe {
        let raw = pq_sys::PQescapeIdentifier(conn.into(), c_str.as_ptr(), str.len() as u64);

        if raw.is_null() {
            return Err(conn
                .error_message()
                .unwrap_or_else(|| "Unknow error".to_string()));
        }

        let escaped = crate::ffi::to_string(raw);
        pq_sys::PQfreemem(raw as *mut std::ffi::c_void);

        Ok(escaped)
    }
}

pub(crate) fn string_conn(
    conn: &crate::Connection,
    from: &str,
) -> std::result::Result<String, String> {
    let mut error = 0;

    // @see https://github.com/postgres/postgres/blob/REL_12_2/src/interfaces/libpq/fe-exec.c#L3329
    let cstring = crate::ffi::new_cstring(2 * from.len() + 1);
    let raw = cstring.into_raw();

    let c_from = crate::ffi::to_cstr(from);

    unsafe {
        pq_sys::PQescapeStringConn(
            conn.into(),
            raw,
            c_from.as_ptr(),
            from.len() as u64,
            &mut error,
        );

        if error != 0 {
            return Err(conn
                .error_message()
                .unwrap_or_else(|| "Unknow error".to_string()));
        }
    };

    let to = crate::ffi::from_raw(raw);

    Ok(to)
}

#[deprecated(note = "Use libpq::Connection::escape_string instead")]
pub fn string(from: &str) -> String {
    let c_from = crate::ffi::to_cstr(from);
    // @see https://github.com/postgres/postgres/blob/REL_12_2/src/interfaces/libpq/fe-exec.c#L3329
    let cstring = crate::ffi::new_cstring(2 * from.len() + 1);
    let raw = cstring.into_raw();

    unsafe {
        pq_sys::PQescapeString(raw, c_from.as_ptr(), from.len() as u64);
    };

    crate::ffi::from_raw(raw)
}

pub(crate) fn bytea_conn(
    conn: &crate::Connection,
    from: &[u8],
) -> std::result::Result<Vec<u8>, String> {
    let to = unsafe {
        let mut len = 0;
        let tmp =
            pq_sys::PQescapeByteaConn(conn.into(), from.as_ptr(), from.len() as u64, &mut len);
        if tmp.is_null() {
            return Err(conn
                .error_message()
                .unwrap_or_else(|| "Unknow error".to_string()));
        }
        let to = std::slice::from_raw_parts(tmp, len as usize - 1).to_vec();
        pq_sys::PQfreemem(tmp as *mut std::ffi::c_void);

        to
    };

    Ok(to)
}

/**
 * See [PQescapeBytea](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQESCAPEBYTEA).
 */
#[deprecated(note = "Use libpq::Connection::escape_bytea instead")]
pub fn bytea(from: &[u8]) -> std::result::Result<Vec<u8>, String> {
    let to = unsafe {
        let mut len = 0;
        let tmp = pq_sys::PQescapeBytea(from.as_ptr(), from.len() as u64, &mut len);
        let to = std::slice::from_raw_parts(tmp, len as usize - 1).to_vec();
        pq_sys::PQfreemem(tmp as *mut std::ffi::c_void);

        to
    };

    Ok(to)
}

/**
 * Converts a string representation of binary data into binary data — the reverse of
 * `libpq::Connection::escape_bytea`.
 *
 * See
 * [PQunescapeBytea](https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQUNESCAPEBYTEA).
 */
pub fn unescape_bytea(from: &[u8]) -> std::result::Result<Vec<u8>, ()> {
    let to = unsafe {
        let mut len = 0;
        let tmp = pq_sys::PQunescapeBytea(from.as_ptr(), &mut len);
        if tmp.is_null() {
            return Err(());
        }
        let to = std::slice::from_raw_parts(tmp, len as usize).to_vec();
        pq_sys::PQfreemem(tmp as *mut std::ffi::c_void);

        to
    };

    Ok(to)
}

#[cfg(test)]
mod test {
    #[test]
    fn literal() {
        let conn = crate::test::new_conn();

        assert_eq!(
            crate::escape::literal(&conn, "foo"),
            Ok("'foo'".to_string())
        );
    }

    #[test]
    fn identifier() {
        let conn = crate::test::new_conn();

        assert_eq!(
            crate::escape::identifier(&conn, "foo"),
            Ok("\"foo\"".to_string())
        );
    }

    #[test]
    fn string_conn() {
        let conn = crate::test::new_conn();

        assert_eq!(
            crate::escape::string_conn(&conn, "'foo'"),
            Ok("''foo''".to_string())
        );
    }

    #[test]
    fn string() {
        #![allow(deprecated)]
        assert_eq!(crate::escape::string("'foo'"), "''foo''".to_string());
    }

    #[test]
    fn bytea_conn() {
        let conn = crate::test::new_conn();

        assert_eq!(
            crate::escape::bytea_conn(&conn, b"\0"),
            Ok(b"\\x00".to_vec())
        );
    }

    #[test]
    fn bytea() {
        #![allow(deprecated)]
        assert_eq!(crate::escape::bytea(b"'foo'"), Ok(b"''foo''".to_vec()));
    }

    #[test]
    fn unescape_bytea() {
        #![allow(deprecated)]
        assert_eq!(crate::escape::bytea(b"'foo'"), Ok(b"''foo''".to_vec()));
    }
}