@@ -87,6 +87,50 @@ public function getBindings(): array
8787 return $ this ->db ->getBindings ();
8888 }
8989
90+ public function callProcedure (array $ dataItem , bool $ query = true ): array
91+ {
92+ if (empty ($ this ->conditionEntity ->getProcedure ())) {
93+ return [];
94+ }
95+ $ count = -1 ;
96+ $ list = []; //默认值
97+
98+ $ procedure = $ this ->conditionEntity ->getProcedure ();
99+ $ rule = '/(?<functionName>.+)\((?<args>.+?)\)/ ' ;
100+ preg_match ($ rule , $ procedure , $ match );
101+ if (empty ($ match ['functionName ' ])) {
102+ return [];
103+ }
104+ $ args = array_map ('trim ' , explode (', ' , $ match ['args ' ]));
105+
106+ $ callArgs = [];
107+ foreach ($ args as $ arg ) {
108+ if (in_array ($ arg , array_keys ($ dataItem ))) {
109+ $ callArgs [] = $ dataItem [$ arg ];
110+ } else if ($ arg == '@limit ' ) {
111+ $ callArgs [] = $ this ->conditionEntity ->getLimit ();
112+ } else if ($ arg == '@offset ' ) {
113+ $ callArgs [] = $ this ->conditionEntity ->getOffset ();
114+ } else {
115+ $ callArgs [] = $ arg ;
116+ }
117+ }
118+
119+ $ sql = sprintf ("CALL %s(%s) " , $ match ['functionName ' ], join (', ' , $ callArgs ));
120+
121+ if ($ query ) {
122+ $ list = Db::select ($ sql );
123+ } else {
124+ $ count = Db::affectingStatement ($ sql );
125+ }
126+
127+ return [
128+ 'count ' => $ count ,
129+ 'update ' => !$ query ,
130+ 'list ' => $ list ,
131+ ];
132+ }
133+
90134 protected function buildQuery (bool $ query = true )
91135 {
92136 if ($ this ->build ) return ;
0 commit comments