@@ -2153,6 +2153,29 @@ public function testMySqlUpdateWrappingNestedJson()
21532153 ]);
21542154 }
21552155
2156+ public function testMySqlUpdateWrappingJsonPathArrayIndex ()
2157+ {
2158+ $ grammar = new MySqlGrammar ();
2159+ $ processor = Mockery::mock (Processor::class);
2160+
2161+ $ connection = $ this ->createMock (ConnectionInterface::class);
2162+ $ connection ->expects ($ this ->once ())
2163+ ->method ('update ' )
2164+ ->with (
2165+ 'update `users` set `options` = json_set(`options`, \'$[1]."2fa" \', false), `meta` = json_set(`meta`, \'$."tags"[0][2] \', ?) where `active` = ? ' ,
2166+ [
2167+ 'large ' ,
2168+ 1 ,
2169+ ]
2170+ );
2171+
2172+ $ builder = new Builder ($ connection , $ grammar , $ processor );
2173+ $ builder ->from ('users ' )->where ('active ' , 1 )->update ([
2174+ 'options->[1]->2fa ' => false ,
2175+ 'meta->tags[0][2] ' => 'large ' ,
2176+ ]);
2177+ }
2178+
21562179 public function testMySqlUpdateWithJsonPreparesBindingsCorrectly ()
21572180 {
21582181 $ grammar = new MySqlGrammar ();
@@ -2257,6 +2280,29 @@ public function testMySqlWrappingJson()
22572280 $ this ->assertEquals ('select * from `users` where json_unquote(json_extract(`items`, \'$."price"."in_usd" \')) = ? and json_unquote(json_extract(`items`, \'$."age" \')) = ? ' , $ builder ->toSql ());
22582281 }
22592282
2283+ public function testJsonPathEscaping ()
2284+ {
2285+ $ expectedWithJsonEscaped = <<<'SQL'
2286+ select json_unquote(json_extract(`json`, '$."''))#"'))
2287+ SQL;
2288+
2289+ $ builder = $ this ->getMySqlBuilder ();
2290+ $ builder ->select ("json->'))# " );
2291+ $ this ->assertEquals ($ expectedWithJsonEscaped , $ builder ->toSql ());
2292+
2293+ $ builder = $ this ->getMySqlBuilder ();
2294+ $ builder ->select ("json-> \\'))# " );
2295+ $ this ->assertEquals ($ expectedWithJsonEscaped , $ builder ->toSql ());
2296+
2297+ $ builder = $ this ->getMySqlBuilder ();
2298+ $ builder ->select ("json-> \\'))# " );
2299+ $ this ->assertEquals ($ expectedWithJsonEscaped , $ builder ->toSql ());
2300+
2301+ $ builder = $ this ->getMySqlBuilder ();
2302+ $ builder ->select ("json-> \\\\'))# " );
2303+ $ this ->assertEquals ($ expectedWithJsonEscaped , $ builder ->toSql ());
2304+ }
2305+
22602306 public function testMySqlSoundsLikeOperator ()
22612307 {
22622308 $ builder = $ this ->getMySqlBuilder ();
@@ -2971,6 +3017,40 @@ public function testWhereJsonDoesntContainMySql()
29713017 $ this ->assertEquals ([1 ], $ builder ->getBindings ());
29723018 }
29733019
3020+ public function testWhereJsonContainsKeyMySql ()
3021+ {
3022+ $ builder = $ this ->getMySqlBuilder ();
3023+ $ builder ->select ('* ' )->from ('users ' )->whereJsonContainsKey ('users.options->languages ' );
3024+ $ this ->assertSame ('select * from `users` where ifnull(json_contains_path(`users`.`options`, \'one \', \'$."languages" \'), 0) ' , $ builder ->toSql ());
3025+
3026+ $ builder = $ this ->getMySqlBuilder ();
3027+ $ builder ->select ('* ' )->from ('users ' )->whereJsonContainsKey ('options->language->primary ' );
3028+ $ this ->assertSame ('select * from `users` where ifnull(json_contains_path(`options`, \'one \', \'$."language"."primary" \'), 0) ' , $ builder ->toSql ());
3029+
3030+ $ builder = $ this ->getMySqlBuilder ();
3031+ $ builder ->select ('* ' )->from ('users ' )->where ('id ' , '= ' , 1 )->orWhereJsonContainsKey ('options->languages ' );
3032+ $ this ->assertSame ('select * from `users` where `id` = ? or ifnull(json_contains_path(`options`, \'one \', \'$."languages" \'), 0) ' , $ builder ->toSql ());
3033+
3034+ $ builder = $ this ->getMySqlBuilder ();
3035+ $ builder ->select ('* ' )->from ('users ' )->whereJsonContainsKey ('options->languages[0][1] ' );
3036+ $ this ->assertSame ('select * from `users` where ifnull(json_contains_path(`options`, \'one \', \'$."languages"[0][1] \'), 0) ' , $ builder ->toSql ());
3037+ }
3038+
3039+ public function testWhereJsonDoesntContainKeyMySql ()
3040+ {
3041+ $ builder = $ this ->getMySqlBuilder ();
3042+ $ builder ->select ('* ' )->from ('users ' )->whereJsonDoesntContainKey ('options->languages ' );
3043+ $ this ->assertSame ('select * from `users` where not ifnull(json_contains_path(`options`, \'one \', \'$."languages" \'), 0) ' , $ builder ->toSql ());
3044+
3045+ $ builder = $ this ->getMySqlBuilder ();
3046+ $ builder ->select ('* ' )->from ('users ' )->where ('id ' , '= ' , 1 )->orWhereJsonDoesntContainKey ('options->languages ' );
3047+ $ this ->assertSame ('select * from `users` where `id` = ? or not ifnull(json_contains_path(`options`, \'one \', \'$."languages" \'), 0) ' , $ builder ->toSql ());
3048+
3049+ $ builder = $ this ->getMySqlBuilder ();
3050+ $ builder ->select ('* ' )->from ('users ' )->whereJsonDoesntContainKey ('options->languages[0][1] ' );
3051+ $ this ->assertSame ('select * from `users` where not ifnull(json_contains_path(`options`, \'one \', \'$."languages"[0][1] \'), 0) ' , $ builder ->toSql ());
3052+ }
3053+
29743054 public function testWhereJsonLengthMySql ()
29753055 {
29763056 $ builder = $ this ->getMySqlBuilder ();
0 commit comments