22
33use pg_query:: protobuf:: { ParseResult , RawStmt } ;
44
5- use super :: { output:: RewriteActionKind , stats:: RewriteStats , Error , RewriteAction , StepOutput } ;
5+ use super :: {
6+ output:: RewriteActionKind , stats:: RewriteStats , Error , RewriteAction , RewritePlan , StepOutput ,
7+ } ;
68use crate :: net:: { Bind , Parse , ProtocolMessage , Query } ;
79
810#[ derive( Debug , Clone ) ]
@@ -12,30 +14,23 @@ pub struct Context<'a> {
1214 original : & ' a ParseResult ,
1315 // If an in-place rewrite was done, the statement is saved here.
1416 rewrite : Option < ParseResult > ,
15- /// Original bind message, if any.
16- bind : Option < & ' a Bind > ,
17- /// Bind rewritten.
18- rewrite_bind : Option < Bind > ,
1917 /// Additional messages to add to the request.
2018 result : Vec < RewriteAction > ,
2119 /// Extended protocol.
2220 parse : Option < & ' a Parse > ,
21+ /// Rewrite plan.
22+ plan : RewritePlan ,
2323}
2424
2525impl < ' a > Context < ' a > {
2626 /// Create new input.
27- pub ( super ) fn new (
28- original : & ' a ParseResult ,
29- bind : Option < & ' a Bind > ,
30- parse : Option < & ' a Parse > ,
31- ) -> Self {
27+ pub ( super ) fn new ( original : & ' a ParseResult , parse : Option < & ' a Parse > ) -> Self {
3228 Self {
3329 original,
34- bind,
3530 rewrite : None ,
36- rewrite_bind : None ,
3731 result : vec ! [ ] ,
3832 parse,
33+ plan : RewritePlan :: default ( ) ,
3934 }
4035 }
4136
@@ -46,32 +41,12 @@ impl<'a> Context<'a> {
4641
4742 /// We are rewriting an extended protocol request.
4843 pub fn extended ( & self ) -> bool {
49- self . parse ( ) . is_some ( ) || self . bind ( ) . is_some ( )
44+ self . parse ( ) . is_some ( )
5045 }
5146
52- /// Get the Bind message, if set.
53- pub fn bind ( & ' a self ) -> Option < & ' a Bind > {
54- if let Some ( ref rewrite_bind) = self . rewrite_bind {
55- Some ( rewrite_bind)
56- } else {
57- self . bind
58- }
59- }
60-
61- /// Take the Bind message for modification.
62- /// Don't forget to return it.
63- #[ must_use]
64- pub fn bind_take ( & mut self ) -> Option < Bind > {
65- if self . rewrite_bind . is_none ( ) {
66- self . rewrite_bind = self . bind . cloned ( ) ;
67- }
68-
69- self . rewrite_bind . take ( )
70- }
71-
72- /// Put the bind message back.
73- pub fn bind_put ( & mut self , bind : Option < Bind > ) {
74- self . rewrite_bind = bind;
47+ /// Get reference to rewrite plan for modification.
48+ pub fn plan ( & mut self ) -> & mut RewritePlan {
49+ & mut self . plan
7550 }
7651
7752 /// Get the original (or modified) statement.
@@ -121,31 +96,19 @@ impl<'a> Context<'a> {
12196 Ok ( StepOutput :: NoOp )
12297 } else {
12398 let mut stats = RewriteStats :: default ( ) ;
124- let bind = self . rewrite_bind . take ( ) ;
12599 let ast = self . rewrite . take ( ) . ok_or ( Error :: NoRewrite ) ?;
126100 let stmt = ast. deparse ( ) ?;
127- let extended = self . extended ( ) ;
128101 let mut parse = self . parse ( ) . cloned ( ) ;
129102
130103 let mut actions = self . result ;
131104
132- if extended {
133- if let Some ( mut parse) = parse. take ( ) {
134- parse. set_query ( & stmt) ;
135- actions. push ( RewriteAction {
136- message : parse. into ( ) ,
137- action : RewriteActionKind :: Replace ,
138- } ) ;
139- stats. parse += 1 ;
140- }
141-
142- if let Some ( bind) = bind {
143- actions. push ( RewriteAction {
144- message : bind. into ( ) ,
145- action : RewriteActionKind :: Replace ,
146- } ) ;
147- stats. bind += 1 ;
148- }
105+ if let Some ( mut parse) = parse. take ( ) {
106+ parse. set_query ( & stmt) ;
107+ actions. push ( RewriteAction {
108+ message : parse. into ( ) ,
109+ action : RewriteActionKind :: Replace ,
110+ } ) ;
111+ stats. parse += 1 ;
149112 } else {
150113 actions. push ( RewriteAction {
151114 message : Query :: new ( stmt. clone ( ) ) . into ( ) ,
@@ -159,6 +122,7 @@ impl<'a> Context<'a> {
159122 ast,
160123 actions,
161124 stats,
125+ plan : self . plan . freeze ( ) ,
162126 } )
163127 }
164128 }
0 commit comments