11use indexmap:: IndexMap ;
22use serde:: { Deserialize , Serialize } ;
3- use std:: { collections:: HashMap , ops:: Deref , sync:: Arc } ;
3+ use std:: { collections:: HashMap , hash :: Hash , ops:: Deref , sync:: Arc } ;
44
55/// Schema name -> Table name -> Relation
66pub type Relations = HashMap < String , HashMap < String , Relation > > ;
77
8- #[ derive( Debug , Clone , Default , Serialize , Deserialize ) ]
8+ #[ derive( Debug , Clone , Default , Serialize , Deserialize , PartialEq , Hash ) ]
99pub struct Column {
1010 pub table_catalog : String ,
1111 pub table_schema : String ,
@@ -18,7 +18,7 @@ pub struct Column {
1818 pub is_primary_key : bool ,
1919}
2020
21- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
21+ #[ derive( Debug , Clone , Serialize , Deserialize , PartialEq ) ]
2222pub struct Relation {
2323 pub schema : String ,
2424 pub name : String ,
@@ -32,6 +32,23 @@ pub struct Relation {
3232 pub columns : IndexMap < String , Column > ,
3333}
3434
35+ impl Hash for Relation {
36+ fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
37+ self . schema . hash ( state) ;
38+ self . name . hash ( state) ;
39+ self . type_ . hash ( state) ;
40+ self . owner . hash ( state) ;
41+ self . persistence . hash ( state) ;
42+ self . access_method . hash ( state) ;
43+ self . description . hash ( state) ;
44+ self . oid . hash ( state) ;
45+ for ( key, value) in & self . columns {
46+ key. hash ( state) ;
47+ value. hash ( state) ;
48+ }
49+ }
50+ }
51+
3552impl Relation {
3653 /// Get schema where the table is located.
3754 pub fn schema ( & self ) -> & str {
@@ -72,18 +89,41 @@ impl Relation {
7289 }
7390}
7491
75- #[ derive( Debug , Default , Serialize , Deserialize ) ]
92+ #[ derive( Debug , Default , Serialize , Deserialize , PartialEq ) ]
7693pub struct SchemaInner {
7794 pub search_path : Vec < String > ,
7895 pub relations : Relations ,
7996}
8097
98+ impl Hash for SchemaInner {
99+ fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
100+ self . search_path . hash ( state) ;
101+ let mut entries: Vec < _ > = self . relations . iter ( ) . collect ( ) ;
102+ entries. sort_by_key ( |( k, _) | * k) ;
103+ for ( schema, tables) in entries {
104+ schema. hash ( state) ;
105+ let mut table_entries: Vec < _ > = tables. iter ( ) . collect ( ) ;
106+ table_entries. sort_by_key ( |( k, _) | * k) ;
107+ for ( name, relation) in table_entries {
108+ name. hash ( state) ;
109+ relation. hash ( state) ;
110+ }
111+ }
112+ }
113+ }
114+
81115/// Load schema from database.
82- #[ derive( Debug , Clone , Default , Serialize , Deserialize ) ]
116+ #[ derive( Debug , Clone , Default , Serialize , Deserialize , PartialEq ) ]
83117pub struct Schema {
84118 inner : Arc < SchemaInner > ,
85119}
86120
121+ impl Hash for Schema {
122+ fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
123+ self . inner . hash ( state) ;
124+ }
125+ }
126+
87127impl Schema {
88128 pub fn tables ( & self ) -> & Relations {
89129 & self . relations
0 commit comments