| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | 
| rust:diesel [2024/05/22 21:03]  – [データベース データ型との対応関係のヒント]  ともやん | rust:diesel [2024/06/04 17:41] (現在)  – [データベース データ型との対応関係のヒント]  ともやん | 
|---|
| 本家: [[https://diesel.rs/|Diesel is a Safe, Extensible ORM and Query Builder for Rust]]\\ | 本家: [[https://diesel.rs/|Diesel is a Safe, Extensible ORM and Query Builder for Rust]]\\ | 
| ソースコード: [[git>diesel-rs/diesel|diesel-rs/diesel: A safe, extensible ORM and Query Builder for Rust]]\\ | ソースコード: [[git>diesel-rs/diesel|diesel-rs/diesel: A safe, extensible ORM and Query Builder for Rust]]\\ | 
|  | マイグレーションガイド: [[https://diesel.rs/guides/migration_guide.html|Diesel 2.0 migration guide]]\\ | 
| ===== インストール ===== | ===== インストール ===== | 
| 公式: [[https://diesel.rs/guides/getting-started|Getting Started with Diesel]]\\ | 公式: [[https://diesel.rs/guides/getting-started|Getting Started with Diesel]]\\ | 
|  |  | 
| ===== データベース ===== | ===== データベース ===== | 
| [[linux:podman:piwigo|Piwigo - オープンソースの写真管理 Web アプリ]] を使用する😅\\ | **PostgreSQL**\\ | 
|  | [[linux:podman:postgres|PostgreSQL]] (Podman)\\ | 
|  |  | 
|  | **MariaDB**\\ | 
|  | [[linux:podman:piwigo|Piwigo - オープンソースの写真管理 Web アプリ]] (Podman) を使用する😅\\ | 
|  |  | 
|  | **SQLite**\\ | 
|  | 構築の必要はない\\ | 
|  |  | 
| ===== ワークスペースの作成 ===== | ===== ワークスペースの作成 ===== | 
| return bigint.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | return bigint.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | 
| } | } | 
| </script></html> | </script> | 
|  | <style> | 
|  | th { | 
|  | background-color: #eee; | 
|  | } | 
|  | .xtern_xtrn { | 
|  |  | 
|  | } | 
|  | </style></html> | 
| <WRAP mintbl minfont_12> | <WRAP mintbl minfont_12> | 
| 参考: [[https://dev.mysql.com/doc/refman/8.0/en/integer-types.html|MySQL :: MySQL 8.0 Reference Manual :: 13.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT]]\\ | 参考: [[https://dev.mysql.com/doc/refman/8.0/en/integer-types.html|MySQL :: MySQL 8.0 Reference Manual :: 13.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT]]\\ | 
| </WRAP> | </WRAP> | 
|  |  | 
|  | [[https://gist.github.com/steveh/7c7145409a5eed6b698ee8b609b6d1fc|postgresql-diesel-rust-types.md]]\\ | 
|  | <WRAP mintbl minfont_12> | 
|  | ^ PostgreSQL Type  ^ PostgreSQL Size  ^ Description  ^ Range  ^ Diesel Type  ^ Rust Type  ^ | 
|  | | Nullable Types  |  | nullable  |  | [[http://docs.diesel.rs/diesel/sql_types/struct.Nullable.html|Nullable]]<T>  | [[https://doc.rust-lang.org/std/option/enum.Option.html|Option]]<T>  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-numeric.html|Numeric Types]]  ^^^^^^ | 
|  | | smallint, int2  | 2 bytes  | signed integer  | -32768 to +32767  | [[http://docs.diesel.rs/diesel/sql_types/struct.SmallInt.html|SmallInt]]  | [[https://doc.rust-lang.org/std/primitive.i16.html|i16]]  | | 
|  | | integer, int, int4  | 4 bytes  | signed integer  | -2147483648 to +2147483647  | [[http://docs.diesel.rs/diesel/sql_types/struct.Integer.html|Integer]]  | [[https://doc.rust-lang.org/std/primitive.i32.html|i32]]  | | 
|  | | bigint, int8  | 8 bytes  | signed integer  | -9223372036854775808 to +9223372036854775807  | [[http://docs.diesel.rs/diesel/sql_types/struct.BigInt.html|BigInt]]  | [[https://doc.rust-lang.org/std/primitive.i64.html|i64]]  | | 
|  | | numeric(p, s), decimal(p, s)  | 2 bytes per 4 digits + 3 to 8 bytes  | exact numeric of selectable precision  | 131072.16383 digits  | [[http://docs.diesel.rs/diesel/sql_types/struct.Numeric.html|Numeric]]  | [[http://docs.diesel.rs/bigdecimal/struct.BigDecimal.html|bigdecimal::BigDecimal]]  | | 
|  | | real, float4  | 4 bytes  | single precision floating-point number | 6 digits precision  | [[http://docs.diesel.rs/diesel/sql_types/struct.Float.html|Float]]  | [[https://doc.rust-lang.org/std/primitive.f32.html|f32]]  | | 
|  | | double precision, float8  | 8 bytes  | double precision floating-point number | 15 digits precision  | [[http://docs.diesel.rs/diesel/sql_types/struct.Double.html|Double]]  | [[https://doc.rust-lang.org/std/primitive.f64.html|f64]]  | | 
|  | | smallserial, serial2  | 2 bytes  | autoincrementing integer  | 1 to 32767  | [[http://docs.diesel.rs/diesel/sql_types/struct.SmallInt.html|SmallInt]]  | [[https://doc.rust-lang.org/std/primitive.i16.html|i16]]  | | 
|  | | serial, serial4  | 4 bytes  | autoincrementing integer  | 1 to 2147483647  | [[http://docs.diesel.rs/diesel/sql_types/struct.Integer.html|Integer]]  | [[https://doc.rust-lang.org/std/primitive.i32.html|i32]]  | | 
|  | | bigserial, serial8  | 8 bytes  | autoincrementing integer  | 1 to 9223372036854775807  | [[http://docs.diesel.rs/diesel/sql_types/struct.BigInt.html|BigInt]]  | [[https://doc.rust-lang.org/std/primitive.i64.html|i64]]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-money.html|Monetary Types]]  ^^^^^^ | 
|  | | money  | 8 bytes  | currency amount  | -92233720368547758.08 to +92233720368547758.07  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Money.html|Money]]  | [[http://docs.diesel.rs/diesel/pg/data_types/struct.Cents.html|Cents]]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-character.html|Character Types]]  ^^^^^^ | 
|  | | character varying(n), varchar(n)  | n+1 or n+4 bytes  | variable-length character string  |  | [[http://docs.diesel.rs/diesel/sql_types/struct.Text.html|Text]]  | [[https://doc.rust-lang.org/std/string/struct.String.html|String]], &[[https://doc.rust-lang.org/std/primitive.str.html|str]]  | | 
|  | | character(n), char(n)  | n+1 or n+4 bytes  | fixed-length character string  |  | [[http://docs.diesel.rs/diesel/sql_types/struct.Text.html|Text]]  | [[https://doc.rust-lang.org/std/string/struct.String.html|String]], &[[https://doc.rust-lang.org/std/primitive.str.html|str]]  | | 
|  | | text  | n+1 or n+4 bytes  | variable-length character string  |  | [[http://docs.diesel.rs/diesel/sql_types/struct.Text.html|Text]]  | [[https://doc.rust-lang.org/std/string/struct.String.html|String]], &[[https://doc.rust-lang.org/std/primitive.str.html|str]]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-binary.html|Binary Data Types]]  ^^^^^^ | 
|  | | bytea  | n+1 to n+4 bytes  | binary data ("byte array")  |  | [[http://docs.diesel.rs/diesel/sql_types/struct.Binary.html|Binary]]  | [[https://doc.rust-lang.org/std/vec/struct.Vec.html|Vec]]<[[https://doc.rust-lang.org/std/primitive.u8.html|u8]]>, &[[https://doc.rust-lang.org/std/primitive.u8.html|u8]]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-datetime.html|Date/Time Types]]  ^^^^^^ | 
|  | | timestamp, timestamp(p) without time zone | 8 bytes  | date and time of day  | 4713 BC to 294276 AD, 1 microsecond  | [[http://docs.diesel.rs/diesel/sql_types/struct.Timestamp.html|Timestamp]]  | [[http://docs.diesel.rs/chrono/naive/struct.NaiveDateTime.html|chrono::NaiveDateTime]]  | | 
|  | | timestamptz, timestamp(p) with time zone  | 8 bytes  | date and time of day, with time zone  | 4713 BC to 294276 AD, 1 microsecond  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Timestamptz.html|Timestamptz]]  | [[http://docs.diesel.rs/chrono/struct.DateTime.html|chrono::DateTime]]  | | 
|  | | date  | 4 bytes  | calendar date (year, month, day)  | 4713 BC to 5874897 AD, 1 day  | [[http://docs.diesel.rs/diesel/sql_types/struct.Date.html|Date]]  | [[http://docs.diesel.rs/chrono/naive/struct.NaiveDate.html|chrono::NaiveDate]]  | | 
|  | | time, time(p) without time zone  | 8 bytes  | time of day (no date)  | 00:00:00 to 24:00:00, 1 microsecond  | [[http://docs.diesel.rs/diesel/sql_types/struct.Time.html|Time]]  | [[http://docs.diesel.rs/chrono/naive/struct.NaiveTime.html|chrono::NaiveTime]]  | | 
|  | | timetz, time(p) with time zone  | 12 bytes  | time of day (no date), with time zone  | 00:00:00+1459 to 24:00:00-1459, 1 microsecond  |  | | 
|  | | interval(fields)(p)  | 16 bytes  | time span  | -178000000 years to 178000000 years, 1 microsecond | [[http://docs.diesel.rs/diesel/sql_types/struct.Interval.html|Interval]]  | [[http://docs.diesel.rs/diesel/pg/data_types/struct.PgInterval.html|PgInterval]]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-boolean.html|Boolean Type]]  ^^^^^^ | 
|  | | boolean, bool  | 1 byte  | logical Boolean (true/false)  |  | [[http://docs.diesel.rs/diesel/sql_types/struct.Bool.html|Bool]]  | [[https://doc.rust-lang.org/std/primitive.bool.html|bool]]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-geometric.html|Geometric Types]]  ^^^^^^ | 
|  | | point (x,y)  | 16 bytes  | geometric point on a plane  |  | | 
|  | | line {A,B,C}  | 32 bytes  | infinite line on a plane  |  | | 
|  | | lseg ((x1,y1),(x2,y2))  | 32 bytes  | finite line segment on a plane  |  | | 
|  | | box ((x1,y1),(x2,y2))  | 32 bytes  | rectangular box on a plane  |  | | 
|  | | path ((x1,y1),...)  | 16+16n bytes  | closed geometric path on a plane  |  | | 
|  | | path [(x1,y1),...]  | 16+16n bytes  | open geometric path on a plane  |  | | 
|  | | polygon ((x1,y1),...)  | 40+16n bytes  | closed geometric path on a plane  |  | | 
|  | | circle <(x,y),r\>  | 24 bytes  | circle on a plane  |  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-net-types.html|Network Address Types]]  ^^^^^^ | 
|  | | cidr  | 7 or 19 bytes  | IPv4 or IPv6 network address  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Cidr.html|Cidr]]  | [[http://docs.diesel.rs/ipnetwork/enum.IpNetwork.html|ipnetwork::IpNetwork]]  | | 
|  | | inet  | 7 or 19 bytes  | IPv4 or IPv6 host address  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Inet.html|Inet]]  | [[http://docs.diesel.rs/ipnetwork/enum.IpNetwork.html|ipnetwork::IpNetwork]]  | | 
|  | | macaddr  | 6 bytes  | MAC address  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.MacAddr.html|MacAddr]]  | %%[%%[[https://doc.rust-lang.org/std/primitive.u8.html|u8]]; 6]  | | 
|  | | macaddr8  | 8 bytes  | MAC address (EUI-64 format)  |  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-enum.html|Enumerated Types]]  ^^^^^^ | 
|  | | enum  | 4 bytes  | enumerated value  |  | (user-defined)  | [[https://doc.rust-lang.org/std/string/struct.String.html|String]], [[https://doc.rust-lang.org/book/first-edition/enums.html|enum]]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-bit.html|Bit String Types]]  ^^^^^^ | 
|  | | bit(n)  | 1 byte per 8 bits + 5 or 8 bytes  | fixed-length bit string  |  | | 
|  | | bit varying(n), varbit  | 1 byte per 8 bits + 5 or 8 bytes  | variable-length bit string  |  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-textsearch.html|Text Search Types]]  ^^^^^^ | 
|  | | tsvector  |  | text search document  |  | TsVector  | | 
|  | | tsquery  |  | text search query  |  | TsQuery  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-uuid.html|UUID Type]]  ^^^^^^ | 
|  | | uuid  | 16 bytes  | universally unique identifier  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Uuid.html|Uuid]]  | [[https://docs.rs/uuid/0.5.1/uuid/struct.Uuid.html|uuid::Uuid]]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-xml.html|XML Type]]  ^^^^^^ | 
|  | | xml  |  | XML data  |  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/datatype-json.html|JSON Types]]  ^^^^^^ | 
|  | | json  |  | textual JSON data  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Json.html|Json]]  | [[http://docs.diesel.rs/serde_json/value/enum.Value.html|serde_json::Value]]  | | 
|  | | jsonb  |  | binary JSON data, decomposed  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Jsonb.html|Jsonb]]  | [[http://docs.diesel.rs/serde_json/value/enum.Value.html|serde_json::Value]]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/arrays.html|Arrays]]  ^^^^^^ | 
|  | | t[]  |  | array of values  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Array.html|Array]]<T>  | [[https://doc.rust-lang.org/std/vec/struct.Vec.html|Vec]]<T>, [[https://doc.rust-lang.org/std/vec/struct.Vec.html|Vec]]<[[https://doc.rust-lang.org/std/option/enum.Option.html|Option]]<T>>, &[T], &%%[%%[[https://doc.rust-lang.org/std/option/enum.Option.html|Option]]<T>]  | | 
|  | ^ [[https://www.postgresql.org/docs/current/static/rangetypes.html|Range Types]]  ^^^^^^ | 
|  | | int4range  |  | range of integer  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Range.html|Range]]<[[http://docs.diesel.rs/diesel/sql_types/struct.Integer.html|Integer]]>  | ([[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[https://doc.rust-lang.org/std/primitive.i32.html|i32]]>, [[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[https://doc.rust-lang.org/std/primitive.i32.html|i32]]>) | 
|  | | int8range  |  | range of bigint  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Range.html|Range]]<[[http://docs.diesel.rs/diesel/sql_types/struct.BigInt.html|BigInt]]>  | ([[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[https://doc.rust-lang.org/std/primitive.i64.html|i64]]>, [[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[https://doc.rust-lang.org/std/primitive.i64.html|i64]]>) | 
|  | | numrange  |  | range of numeric  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Range.html|Range]]<[[http://docs.diesel.rs/diesel/sql_types/struct.Numeric.html|Numeric]]>  | ([[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[http://docs.diesel.rs/bigdecimal/struct.BigDecimal.html|bigdecimal::BigDecimal]]>, [[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[http://docs.diesel.rs/bigdecimal/struct.BigDecimal.html|bigdecimal::BigDecimal]]>) | 
|  | | tsrange  |  | range of timestamp  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Range.html|Range]]<[[http://docs.diesel.rs/diesel/sql_types/struct.Timestamp.html|Timestamp]]>  | ([[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[http://docs.diesel.rs/chrono/naive/struct.NaiveDateTime.html|chrono::NaiveDateTime]]>, [[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[http://docs.diesel.rs/chrono/naive/struct.NaiveDateTime.html|chrono::NaiveDateTime]]>) | 
|  | | tstzrange  |  | range of timestamptz  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Range.html|Range]]<[[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Timestamptz.html|Timestamptz]]> | ([[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[http://docs.diesel.rs/chrono/struct.DateTime.html|chrono::DateTime]]>, [[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[http://docs.diesel.rs/chrono/struct.DateTime.html|chrono::DateTime]]>) | 
|  | | daterange  |  | range of date  |  | [[http://docs.diesel.rs/diesel/pg/types/sql_types/struct.Range.html|Range]]<[[http://docs.diesel.rs/diesel/sql_types/struct.Date.html|Date]]>  | ([[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[http://docs.diesel.rs/chrono/naive/struct.NaiveDate.html|chrono::NaiveDate]]>, [[https://doc.rust-lang.org/std/collections/enum.Bound.html|Bound]]<[[http://docs.diesel.rs/chrono/naive/struct.NaiveDate.html|chrono::NaiveDate]]>)  | | 
|  | </WRAP> | 
| === 参考文献 === | === 参考文献 === | 
| [[so>questions/70410103/how-to-make-diesel-auto-generate-models|rust - How to make Diesel auto-generate models? - Stack Overflow]] [[gtr>https://stackoverflow.com/questions/70410103/how-to-make-diesel-auto-generate-models|翻訳]]\\ | [[so>questions/70410103/how-to-make-diesel-auto-generate-models|rust - How to make Diesel auto-generate models? - Stack Overflow]] [[gtr>https://stackoverflow.com/questions/70410103/how-to-make-diesel-auto-generate-models|翻訳]]\\ | 
| [[https://kotiri.com/2018/01/31/postgresql-diesel-rust-types.html|A comprehensive mapping of PostgreSQL, Diesel, and Rust types]]\\ | [[https://kotiri.com/2018/01/31/postgresql-diesel-rust-types.html|A comprehensive mapping of PostgreSQL, Diesel, and Rust types]]\\ | 
|  |  |