88use InvalidArgumentException ;
99use Psr \Http \Message \RequestFactoryInterface ;
1010use Psr \Http \Message \RequestInterface ;
11+ use Psr \Http \Message \StreamFactoryInterface ;
1112use Psr \Http \Message \UriFactoryInterface ;
1213use Psr \Http \Message \UriInterface ;
1314use SimPod \ClickHouseClient \Exception \UnsupportedParamType ;
@@ -31,6 +32,7 @@ final class RequestFactory
3132 public function __construct (
3233 private ParamValueConverterRegistry $ paramValueConverterRegistry ,
3334 private RequestFactoryInterface $ requestFactory ,
35+ private StreamFactoryInterface $ streamFactory ,
3436 UriFactoryInterface |null $ uriFactory = null ,
3537 UriInterface |string $ uri = '' ,
3638 ) {
@@ -71,37 +73,39 @@ public function prepareRequest(RequestOptions $requestOptions): RequestInterface
7173 $ request = $ this ->requestFactory ->createRequest ('POST ' , $ uri );
7274
7375 preg_match_all ('~\{([a-zA-Z\d]+):([a-zA-Z\d ]+(\(.+\))?)}~ ' , $ requestOptions ->sql , $ matches );
74-
75- $ typeToParam = array_reduce (
76- array_keys ($ matches [1 ]),
77- static function (array $ acc , string |int $ k ) use ($ matches ) {
78- $ acc [$ matches [1 ][$ k ]] = Type::fromString ($ matches [2 ][$ k ]);
79-
80- return $ acc ;
81- },
82- [],
83- );
84-
85- $ streamElements = [['name ' => 'query ' , 'contents ' => $ requestOptions ->sql ]];
86- foreach ($ requestOptions ->params as $ name => $ value ) {
87- $ type = $ typeToParam [$ name ] ?? null ;
88- if ($ type === null ) {
89- continue ;
76+ if ($ matches === []) {
77+ $ body = $ this ->streamFactory ->createStream ($ requestOptions ->sql );
78+ } else {
79+ $ typeToParam = array_reduce (
80+ array_keys ($ matches [1 ]),
81+ static function (array $ acc , string |int $ k ) use ($ matches ) {
82+ $ acc [$ matches [1 ][$ k ]] = Type::fromString ($ matches [2 ][$ k ]);
83+
84+ return $ acc ;
85+ },
86+ [],
87+ );
88+
89+ $ streamElements = [['name ' => 'query ' , 'contents ' => $ requestOptions ->sql ]];
90+ foreach ($ requestOptions ->params as $ name => $ value ) {
91+ $ type = $ typeToParam [$ name ] ?? null ;
92+ if ($ type === null ) {
93+ continue ;
94+ }
95+
96+ $ streamElements [] = [
97+ 'name ' => 'param_ ' . $ name ,
98+ 'contents ' => $ this ->paramValueConverterRegistry ->get ($ type )($ value , $ type , false ),
99+ ];
90100 }
91101
92- $ streamElements [] = [
93- 'name ' => 'param_ ' . $ name ,
94- 'contents ' => $ this ->paramValueConverterRegistry ->get ($ type )($ value , $ type , false ),
95- ];
96- }
97-
98- try {
99- $ body = new MultipartStream ($ streamElements );
100- $ request = $ request
101- ->withHeader ('Content-Type ' , 'multipart/form-data; boundary= ' . $ body ->getBoundary ())
102- ->withBody ($ body );
103- } catch (InvalidArgumentException ) {
104- absurd ();
102+ try {
103+ $ body = new MultipartStream ($ streamElements );
104+ $ request = $ request ->withBody ($ body )
105+ ->withHeader ('Content-Type ' , 'multipart/form-data; boundary= ' . $ body ->getBoundary ());
106+ } catch (InvalidArgumentException ) {
107+ absurd ();
108+ }
105109 }
106110
107111 return $ request ;
0 commit comments