pub trait ToText {
// Required methods
fn write<W: Write>(&self, f: &mut W) -> Result;
fn write_with_type<W: Write>(&self, _ty: &DataType, f: &mut W) -> Result;
// Provided methods
fn to_text_with_type(&self, ty: &DataType) -> String { ... }
fn to_text(&self) -> String { ... }
}
Expand description
Converts ScalarRef
to pgwire “TEXT” format.
§Relationship with casting to varchar
For most types, this is also the implementation for casting to varchar, but there are exceptions.
e.g., The TEXT format for boolean is t
/ f
while they cast to varchar true
/ false
.
- https://github.com/postgres/postgres/blob/REL_16_3/src/include/catalog/pg_cast.dat#L438-L439
- https://www.postgresql.org/docs/16/sql-createcast.html#:~:text=A%20small%20number%20of%20the%20built%2Din%20types%20do%20indeed%20have%20different%20behaviors%20for%20conversions%2C%20mostly%20because%20of%20requirements%20of%20the%20SQL%20standard
§Relationship with ToString
/Display
For some types, the implementation diverge from Rust’s standard ToString
/Display
,
to match PostgreSQL’s representation.
FIXME: ToText
should depend on a lot of other stuff
but we have not implemented them yet: timezone, date style, interval style, bytea output, etc
Required Methods§
Provided Methods§
sourcefn to_text_with_type(&self, ty: &DataType) -> String
fn to_text_with_type(&self, ty: &DataType) -> String
Convert to text according to its data type
sourcefn to_text(&self) -> String
fn to_text(&self) -> String
to_text
is a special version of to_text_with_type
, it convert the scalar to default type
text. E.g. for Int64, it will convert to text as a Int64 type.
We should prefer to use to_text_with_type
because it’s more clear and readable.
Note: currently the DataType
param is actually unnecessary.
Previously, Timestamptz is also represented as int64, and we need the data type to distinguish them.
Now we have 1-1 mapping, and it happens to be the case that PostgreSQL default ToText
format does
not need additional metadata like field names contained in DataType
.