| 1 | use sqlx_core::bytes::Buf; |
| 2 | |
| 3 | use crate::decode::Decode; |
| 4 | use crate::encode::Encode; |
| 5 | use crate::error::{mismatched_types, BoxDynError}; |
| 6 | use crate::type_info::TypeInfo; |
| 7 | use crate::type_info::{PgType, PgTypeKind}; |
| 8 | use crate::types::Oid; |
| 9 | use crate::types::Type; |
| 10 | use crate::{PgArgumentBuffer, PgTypeInfo, PgValueFormat, PgValueRef, Postgres}; |
| 11 | |
| 12 | #[doc(hidden)] |
| 13 | pub struct PgRecordEncoder<'a> { |
| 14 | buf: &'a mut PgArgumentBuffer, |
| 15 | off: usize, |
| 16 | num: u32, |
| 17 | } |
| 18 | |
| 19 | impl<'a> PgRecordEncoder<'a> { |
| 20 | #[doc(hidden)] |
| 21 | pub fn new(buf: &'a mut PgArgumentBuffer) -> Self { |
| 22 | let off = buf.len(); |
| 23 | |
| 24 | // reserve space for a field count |
| 25 | buf.extend(&(0_u32).to_be_bytes()); |
| 26 | |
| 27 | Self { buf, off, num: 0 } |
| 28 | } |
| 29 | |
| 30 | #[doc(hidden)] |
| 31 | pub fn finish(&mut self) { |
| 32 | // fill in the record length |
| 33 | self.buf[self.off..(self.off + 4)].copy_from_slice(&self.num.to_be_bytes()); |
| 34 | } |
| 35 | |
| 36 | #[doc(hidden)] |
| 37 | pub fn encode<'q, T>(&mut self, value: T) -> Result<&mut Self, BoxDynError> |
| 38 | where |
| 39 | 'a: 'q, |
| 40 | T: Encode<'q, Postgres> + Type<Postgres>, |
| 41 | { |
| 42 | let ty = value.produces().unwrap_or_else(T::type_info); |
| 43 | |
| 44 | if let PgType::DeclareWithName(name) = ty.0 { |
| 45 | // push a hole for this type ID |
| 46 | // to be filled in on query execution |
| 47 | self.buf.patch_type_by_name(&name); |
| 48 | } else { |
| 49 | // write type id |
| 50 | self.buf.extend(&ty.0.oid().0.to_be_bytes()); |
| 51 | } |
| 52 | |
| 53 | self.buf.encode(value)?; |
| 54 | self.num += 1; |
| 55 | |
| 56 | Ok(self) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[doc(hidden)] |
| 61 | pub struct PgRecordDecoder<'r> { |
| 62 | buf: &'r [u8], |
| 63 | typ: PgTypeInfo, |
| 64 | fmt: PgValueFormat, |
| 65 | ind: usize, |
| 66 | } |
| 67 | |
| 68 | impl<'r> PgRecordDecoder<'r> { |
| 69 | #[doc(hidden)] |
| 70 | pub fn new(value: PgValueRef<'r>) -> Result<Self, BoxDynError> { |
| 71 | let fmt = value.format(); |
| 72 | let mut buf = value.as_bytes()?; |
| 73 | let typ = value.type_info; |
| 74 | |
| 75 | match fmt { |
| 76 | PgValueFormat::Binary => { |
| 77 | let _len = buf.get_u32(); |
| 78 | } |
| 79 | |
| 80 | PgValueFormat::Text => { |
| 81 | // remove the enclosing `(` .. `)` |
| 82 | buf = &buf[1..(buf.len() - 1)]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | Ok(Self { |
| 87 | buf, |
| 88 | fmt, |
| 89 | typ, |
| 90 | ind: 0, |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | #[doc(hidden)] |
| 95 | pub fn try_decode<T>(&mut self) -> Result<T, BoxDynError> |
| 96 | where |
| 97 | T: for<'a> Decode<'a, Postgres> + Type<Postgres>, |
| 98 | { |
| 99 | if self.buf.is_empty() { |
| 100 | return Err(format!("no field `{0}` found on record", self.ind).into()); |
| 101 | } |
| 102 | |
| 103 | match self.fmt { |
| 104 | PgValueFormat::Binary => { |
| 105 | let element_type_oid = Oid(self.buf.get_u32()); |
| 106 | let element_type_opt = match self.typ.0.kind() { |
| 107 | PgTypeKind::Simple if self.typ.0 == PgType::Record => { |
| 108 | PgTypeInfo::try_from_oid(element_type_oid) |
| 109 | } |
| 110 | |
| 111 | PgTypeKind::Composite(fields) => { |
| 112 | let ty = fields[self.ind].1.clone(); |
| 113 | if ty.0.oid() != element_type_oid { |
| 114 | return Err("unexpected mismatch of composite type information".into()); |
| 115 | } |
| 116 | |
| 117 | Some(ty) |
| 118 | } |
| 119 | |
| 120 | _ => { |
| 121 | return Err( |
| 122 | "unexpected non-composite type being decoded as a composite type" |
| 123 | .into(), |
| 124 | ); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | if let Some(ty) = &element_type_opt { |
| 129 | if !ty.is_null() && !T::compatible(ty) { |
| 130 | return Err(mismatched_types::<Postgres, T>(ty)); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | let element_type = |
| 135 | element_type_opt |
| 136 | .ok_or_else(|| BoxDynError::from(format!("custom types in records are not fully supported yet: failed to retrieve type info for field {} with type oid {}", self.ind, element_type_oid.0)))?; |
| 137 | |
| 138 | self.ind += 1; |
| 139 | |
| 140 | T::decode(PgValueRef::get(&mut self.buf, self.fmt, element_type)?) |
| 141 | } |
| 142 | |
| 143 | PgValueFormat::Text => { |
| 144 | let mut element = String::new(); |
| 145 | let mut quoted = false; |
| 146 | let mut in_quotes = false; |
| 147 | let mut in_escape = false; |
| 148 | let mut prev_ch = '\0'; |
| 149 | |
| 150 | while !self.buf.is_empty() { |
| 151 | let ch = self.buf.get_u8() as char; |
| 152 | match ch { |
| 153 | _ if in_escape => { |
| 154 | element.push(ch); |
| 155 | in_escape = false; |
| 156 | } |
| 157 | |
| 158 | '"' if in_quotes => { |
| 159 | in_quotes = false; |
| 160 | } |
| 161 | |
| 162 | '"' => { |
| 163 | in_quotes = true; |
| 164 | quoted = true; |
| 165 | |
| 166 | if prev_ch == '"' { |
| 167 | element.push('"') |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | '\\' if !in_escape => { |
| 172 | in_escape = true; |
| 173 | } |
| 174 | |
| 175 | ',' if !in_quotes => break, |
| 176 | |
| 177 | _ => { |
| 178 | element.push(ch); |
| 179 | } |
| 180 | } |
| 181 | prev_ch = ch; |
| 182 | } |
| 183 | |
| 184 | let buf = if element.is_empty() && !quoted { |
| 185 | // completely empty input means NULL |
| 186 | None |
| 187 | } else { |
| 188 | Some(element.as_bytes()) |
| 189 | }; |
| 190 | |
| 191 | // NOTE: we do not call [`accepts`] or give a chance to from a user as |
| 192 | // TEXT sequences are not strongly typed |
| 193 | |
| 194 | T::decode(PgValueRef { |
| 195 | // NOTE: We pass `0` as the type ID because we don't have a reasonable value |
| 196 | // we could use. |
| 197 | type_info: PgTypeInfo::with_oid(Oid(0)), |
| 198 | format: self.fmt, |
| 199 | value: buf, |
| 200 | row: None, |
| 201 | }) |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |