1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624 |
- <?php
- namespace Illuminate\Tests\Database;
- use BadMethodCallException;
- use Closure;
- use DateTime;
- use Illuminate\Database\ConnectionInterface;
- use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Database\Query\Expression as Raw;
- use Illuminate\Database\Query\Grammars\Grammar;
- use Illuminate\Database\Query\Grammars\MySqlGrammar;
- use Illuminate\Database\Query\Grammars\PostgresGrammar;
- use Illuminate\Database\Query\Grammars\SQLiteGrammar;
- use Illuminate\Database\Query\Grammars\SqlServerGrammar;
- use Illuminate\Database\Query\Processors\MySqlProcessor;
- use Illuminate\Database\Query\Processors\PostgresProcessor;
- use Illuminate\Database\Query\Processors\Processor;
- use Illuminate\Pagination\AbstractPaginator as Paginator;
- use Illuminate\Pagination\Cursor;
- use Illuminate\Pagination\CursorPaginator;
- use Illuminate\Pagination\LengthAwarePaginator;
- use InvalidArgumentException;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- use RuntimeException;
- use stdClass;
- class DatabaseQueryBuilderTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testBasicSelect()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users');
- $this->assertSame('select * from "users"', $builder->toSql());
- }
- public function testBasicSelectWithGetColumns()
- {
- $builder = $this->getBuilder();
- $builder->getProcessor()->shouldReceive('processSelect');
- $builder->getConnection()->shouldReceive('select')->once()->andReturnUsing(function ($sql) {
- $this->assertSame('select * from "users"', $sql);
- });
- $builder->getConnection()->shouldReceive('select')->once()->andReturnUsing(function ($sql) {
- $this->assertSame('select "foo", "bar" from "users"', $sql);
- });
- $builder->getConnection()->shouldReceive('select')->once()->andReturnUsing(function ($sql) {
- $this->assertSame('select "baz" from "users"', $sql);
- });
- $builder->from('users')->get();
- $this->assertNull($builder->columns);
- $builder->from('users')->get(['foo', 'bar']);
- $this->assertNull($builder->columns);
- $builder->from('users')->get('baz');
- $this->assertNull($builder->columns);
- $this->assertSame('select * from "users"', $builder->toSql());
- $this->assertNull($builder->columns);
- }
- public function testBasicSelectUseWritePdo()
- {
- $builder = $this->getMySqlBuilderWithProcessor();
- $builder->getConnection()->shouldReceive('select')->once()
- ->with('select * from `users`', [], false);
- $builder->useWritePdo()->select('*')->from('users')->get();
- $builder = $this->getMySqlBuilderWithProcessor();
- $builder->getConnection()->shouldReceive('select')->once()
- ->with('select * from `users`', [], true);
- $builder->select('*')->from('users')->get();
- }
- public function testBasicTableWrappingProtectsQuotationMarks()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('some"table');
- $this->assertSame('select * from "some""table"', $builder->toSql());
- }
- public function testAliasWrappingAsWholeConstant()
- {
- $builder = $this->getBuilder();
- $builder->select('x.y as foo.bar')->from('baz');
- $this->assertSame('select "x"."y" as "foo.bar" from "baz"', $builder->toSql());
- }
- public function testAliasWrappingWithSpacesInDatabaseName()
- {
- $builder = $this->getBuilder();
- $builder->select('w x.y.z as foo.bar')->from('baz');
- $this->assertSame('select "w x"."y"."z" as "foo.bar" from "baz"', $builder->toSql());
- }
- public function testAddingSelects()
- {
- $builder = $this->getBuilder();
- $builder->select('foo')->addSelect('bar')->addSelect(['baz', 'boom'])->from('users');
- $this->assertSame('select "foo", "bar", "baz", "boom" from "users"', $builder->toSql());
- }
- public function testBasicSelectWithPrefix()
- {
- $builder = $this->getBuilder();
- $builder->getGrammar()->setTablePrefix('prefix_');
- $builder->select('*')->from('users');
- $this->assertSame('select * from "prefix_users"', $builder->toSql());
- }
- public function testBasicSelectDistinct()
- {
- $builder = $this->getBuilder();
- $builder->distinct()->select('foo', 'bar')->from('users');
- $this->assertSame('select distinct "foo", "bar" from "users"', $builder->toSql());
- }
- public function testBasicSelectDistinctOnColumns()
- {
- $builder = $this->getBuilder();
- $builder->distinct('foo')->select('foo', 'bar')->from('users');
- $this->assertSame('select distinct "foo", "bar" from "users"', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->distinct('foo')->select('foo', 'bar')->from('users');
- $this->assertSame('select distinct on ("foo") "foo", "bar" from "users"', $builder->toSql());
- }
- public function testBasicAlias()
- {
- $builder = $this->getBuilder();
- $builder->select('foo as bar')->from('users');
- $this->assertSame('select "foo" as "bar" from "users"', $builder->toSql());
- }
- public function testAliasWithPrefix()
- {
- $builder = $this->getBuilder();
- $builder->getGrammar()->setTablePrefix('prefix_');
- $builder->select('*')->from('users as people');
- $this->assertSame('select * from "prefix_users" as "prefix_people"', $builder->toSql());
- }
- public function testJoinAliasesWithPrefix()
- {
- $builder = $this->getBuilder();
- $builder->getGrammar()->setTablePrefix('prefix_');
- $builder->select('*')->from('services')->join('translations AS t', 't.item_id', '=', 'services.id');
- $this->assertSame('select * from "prefix_services" inner join "prefix_translations" as "prefix_t" on "prefix_t"."item_id" = "prefix_services"."id"', $builder->toSql());
- }
- public function testBasicTableWrapping()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('public.users');
- $this->assertSame('select * from "public"."users"', $builder->toSql());
- }
- public function testWhenCallback()
- {
- $callback = function ($query, $condition) {
- $this->assertTrue($condition);
- $query->where('id', '=', 1);
- };
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->when(true, $callback)->where('email', 'foo');
- $this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->when(false, $callback)->where('email', 'foo');
- $this->assertSame('select * from "users" where "email" = ?', $builder->toSql());
- }
- public function testWhenCallbackWithReturn()
- {
- $callback = function ($query, $condition) {
- $this->assertTrue($condition);
- return $query->where('id', '=', 1);
- };
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->when(true, $callback)->where('email', 'foo');
- $this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->when(false, $callback)->where('email', 'foo');
- $this->assertSame('select * from "users" where "email" = ?', $builder->toSql());
- }
- public function testWhenCallbackWithDefault()
- {
- $callback = function ($query, $condition) {
- $this->assertSame('truthy', $condition);
- $query->where('id', '=', 1);
- };
- $default = function ($query, $condition) {
- $this->assertEquals(0, $condition);
- $query->where('id', '=', 2);
- };
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->when('truthy', $callback, $default)->where('email', 'foo');
- $this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->when(0, $callback, $default)->where('email', 'foo');
- $this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
- $this->assertEquals([0 => 2, 1 => 'foo'], $builder->getBindings());
- }
- public function testUnlessCallback()
- {
- $callback = function ($query, $condition) {
- $this->assertFalse($condition);
- $query->where('id', '=', 1);
- };
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->unless(false, $callback)->where('email', 'foo');
- $this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->unless(true, $callback)->where('email', 'foo');
- $this->assertSame('select * from "users" where "email" = ?', $builder->toSql());
- }
- public function testUnlessCallbackWithReturn()
- {
- $callback = function ($query, $condition) {
- $this->assertFalse($condition);
- return $query->where('id', '=', 1);
- };
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->unless(false, $callback)->where('email', 'foo');
- $this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->unless(true, $callback)->where('email', 'foo');
- $this->assertSame('select * from "users" where "email" = ?', $builder->toSql());
- }
- public function testUnlessCallbackWithDefault()
- {
- $callback = function ($query, $condition) {
- $this->assertEquals(0, $condition);
- $query->where('id', '=', 1);
- };
- $default = function ($query, $condition) {
- $this->assertSame('truthy', $condition);
- $query->where('id', '=', 2);
- };
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->unless(0, $callback, $default)->where('email', 'foo');
- $this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->unless('truthy', $callback, $default)->where('email', 'foo');
- $this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
- $this->assertEquals([0 => 2, 1 => 'foo'], $builder->getBindings());
- }
- public function testTapCallback()
- {
- $callback = function ($query) {
- return $query->where('id', '=', 1);
- };
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->tap($callback)->where('email', 'foo');
- $this->assertSame('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
- }
- public function testBasicWheres()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1);
- $this->assertSame('select * from "users" where "id" = ?', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testWheresWithArrayValue()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', [12]);
- $this->assertSame('select * from "users" where "id" = ?', $builder->toSql());
- $this->assertEquals([0 => 12], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', [12, 30]);
- $this->assertSame('select * from "users" where "id" = ?', $builder->toSql());
- $this->assertEquals([0 => 12], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '!=', [12, 30]);
- $this->assertSame('select * from "users" where "id" != ?', $builder->toSql());
- $this->assertEquals([0 => 12], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '<>', [12, 30]);
- $this->assertSame('select * from "users" where "id" <> ?', $builder->toSql());
- $this->assertEquals([0 => 12], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', [[12, 30]]);
- $this->assertSame('select * from "users" where "id" = ?', $builder->toSql());
- $this->assertEquals([0 => 12], $builder->getBindings());
- }
- public function testMySqlWrappingProtectsQuotationMarks()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->From('some`table');
- $this->assertSame('select * from `some``table`', $builder->toSql());
- }
- public function testDateBasedWheresAcceptsTwoArguments()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', 1);
- $this->assertSame('select * from `users` where date(`created_at`) = ?', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereDay('created_at', 1);
- $this->assertSame('select * from `users` where day(`created_at`) = ?', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereMonth('created_at', 1);
- $this->assertSame('select * from `users` where month(`created_at`) = ?', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereYear('created_at', 1);
- $this->assertSame('select * from `users` where year(`created_at`) = ?', $builder->toSql());
- }
- public function testDateBasedOrWheresAcceptsTwoArguments()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', 1)->orWhereDate('created_at', 1);
- $this->assertSame('select * from `users` where `id` = ? or date(`created_at`) = ?', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', 1)->orWhereDay('created_at', 1);
- $this->assertSame('select * from `users` where `id` = ? or day(`created_at`) = ?', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', 1)->orWhereMonth('created_at', 1);
- $this->assertSame('select * from `users` where `id` = ? or month(`created_at`) = ?', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', 1)->orWhereYear('created_at', 1);
- $this->assertSame('select * from `users` where `id` = ? or year(`created_at`) = ?', $builder->toSql());
- }
- public function testDateBasedWheresExpressionIsNotBound()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', new Raw('NOW()'))->where('admin', true);
- $this->assertEquals([true], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereDay('created_at', new Raw('NOW()'));
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereMonth('created_at', new Raw('NOW()'));
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereYear('created_at', new Raw('NOW()'));
- $this->assertEquals([], $builder->getBindings());
- }
- public function testWhereDateMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', '=', '2015-12-21');
- $this->assertSame('select * from `users` where date(`created_at`) = ?', $builder->toSql());
- $this->assertEquals([0 => '2015-12-21'], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', '=', new Raw('NOW()'));
- $this->assertSame('select * from `users` where date(`created_at`) = NOW()', $builder->toSql());
- }
- public function testWhereDayMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereDay('created_at', '=', 1);
- $this->assertSame('select * from `users` where day(`created_at`) = ?', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testOrWhereDayMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereDay('created_at', '=', 1)->orWhereDay('created_at', '=', 2);
- $this->assertSame('select * from `users` where day(`created_at`) = ? or day(`created_at`) = ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- }
- public function testWhereMonthMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereMonth('created_at', '=', 5);
- $this->assertSame('select * from `users` where month(`created_at`) = ?', $builder->toSql());
- $this->assertEquals([0 => 5], $builder->getBindings());
- }
- public function testOrWhereMonthMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereMonth('created_at', '=', 5)->orWhereMonth('created_at', '=', 6);
- $this->assertSame('select * from `users` where month(`created_at`) = ? or month(`created_at`) = ?', $builder->toSql());
- $this->assertEquals([0 => 5, 1 => 6], $builder->getBindings());
- }
- public function testWhereYearMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereYear('created_at', '=', 2014);
- $this->assertSame('select * from `users` where year(`created_at`) = ?', $builder->toSql());
- $this->assertEquals([0 => 2014], $builder->getBindings());
- }
- public function testOrWhereYearMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereYear('created_at', '=', 2014)->orWhereYear('created_at', '=', 2015);
- $this->assertSame('select * from `users` where year(`created_at`) = ? or year(`created_at`) = ?', $builder->toSql());
- $this->assertEquals([0 => 2014, 1 => 2015], $builder->getBindings());
- }
- public function testWhereTimeMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereTime('created_at', '>=', '22:00');
- $this->assertSame('select * from `users` where time(`created_at`) >= ?', $builder->toSql());
- $this->assertEquals([0 => '22:00'], $builder->getBindings());
- }
- public function testWhereTimeOperatorOptionalMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereTime('created_at', '22:00');
- $this->assertSame('select * from `users` where time(`created_at`) = ?', $builder->toSql());
- $this->assertEquals([0 => '22:00'], $builder->getBindings());
- }
- public function testWhereTimeOperatorOptionalPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereTime('created_at', '22:00');
- $this->assertSame('select * from "users" where "created_at"::time = ?', $builder->toSql());
- $this->assertEquals([0 => '22:00'], $builder->getBindings());
- }
- public function testWhereTimeSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereTime('created_at', '22:00');
- $this->assertSame('select * from [users] where cast([created_at] as time) = ?', $builder->toSql());
- $this->assertEquals([0 => '22:00'], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereTime('created_at', new Raw('NOW()'));
- $this->assertSame('select * from [users] where cast([created_at] as time) = NOW()', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testWhereDatePostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', '=', '2015-12-21');
- $this->assertSame('select * from "users" where "created_at"::date = ?', $builder->toSql());
- $this->assertEquals([0 => '2015-12-21'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', new Raw('NOW()'));
- $this->assertSame('select * from "users" where "created_at"::date = NOW()', $builder->toSql());
- }
- public function testWhereDayPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereDay('created_at', '=', 1);
- $this->assertSame('select * from "users" where extract(day from "created_at") = ?', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testWhereMonthPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereMonth('created_at', '=', 5);
- $this->assertSame('select * from "users" where extract(month from "created_at") = ?', $builder->toSql());
- $this->assertEquals([0 => 5], $builder->getBindings());
- }
- public function testWhereYearPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereYear('created_at', '=', 2014);
- $this->assertSame('select * from "users" where extract(year from "created_at") = ?', $builder->toSql());
- $this->assertEquals([0 => 2014], $builder->getBindings());
- }
- public function testWhereTimePostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereTime('created_at', '>=', '22:00');
- $this->assertSame('select * from "users" where "created_at"::time >= ?', $builder->toSql());
- $this->assertEquals([0 => '22:00'], $builder->getBindings());
- }
- public function testWhereLikePostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', 'like', '1');
- $this->assertSame('select * from "users" where "id"::text like ?', $builder->toSql());
- $this->assertEquals([0 => '1'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', 'LIKE', '1');
- $this->assertSame('select * from "users" where "id"::text LIKE ?', $builder->toSql());
- $this->assertEquals([0 => '1'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', 'ilike', '1');
- $this->assertSame('select * from "users" where "id"::text ilike ?', $builder->toSql());
- $this->assertEquals([0 => '1'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', 'not like', '1');
- $this->assertSame('select * from "users" where "id"::text not like ?', $builder->toSql());
- $this->assertEquals([0 => '1'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', 'not ilike', '1');
- $this->assertSame('select * from "users" where "id"::text not ilike ?', $builder->toSql());
- $this->assertEquals([0 => '1'], $builder->getBindings());
- }
- public function testWhereDateSqlite()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', '=', '2015-12-21');
- $this->assertSame('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = cast(? as text)', $builder->toSql());
- $this->assertEquals([0 => '2015-12-21'], $builder->getBindings());
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', new Raw('NOW()'));
- $this->assertSame('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = cast(NOW() as text)', $builder->toSql());
- }
- public function testWhereDaySqlite()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereDay('created_at', '=', 1);
- $this->assertSame('select * from "users" where strftime(\'%d\', "created_at") = cast(? as text)', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testWhereMonthSqlite()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereMonth('created_at', '=', 5);
- $this->assertSame('select * from "users" where strftime(\'%m\', "created_at") = cast(? as text)', $builder->toSql());
- $this->assertEquals([0 => 5], $builder->getBindings());
- }
- public function testWhereYearSqlite()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereYear('created_at', '=', 2014);
- $this->assertSame('select * from "users" where strftime(\'%Y\', "created_at") = cast(? as text)', $builder->toSql());
- $this->assertEquals([0 => 2014], $builder->getBindings());
- }
- public function testWhereTimeSqlite()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereTime('created_at', '>=', '22:00');
- $this->assertSame('select * from "users" where strftime(\'%H:%M:%S\', "created_at") >= cast(? as text)', $builder->toSql());
- $this->assertEquals([0 => '22:00'], $builder->getBindings());
- }
- public function testWhereTimeOperatorOptionalSqlite()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereTime('created_at', '22:00');
- $this->assertSame('select * from "users" where strftime(\'%H:%M:%S\', "created_at") = cast(? as text)', $builder->toSql());
- $this->assertEquals([0 => '22:00'], $builder->getBindings());
- }
- public function testWhereDateSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', '=', '2015-12-21');
- $this->assertSame('select * from [users] where cast([created_at] as date) = ?', $builder->toSql());
- $this->assertEquals([0 => '2015-12-21'], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', new Raw('NOW()'));
- $this->assertSame('select * from [users] where cast([created_at] as date) = NOW()', $builder->toSql());
- }
- public function testWhereDaySqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereDay('created_at', '=', 1);
- $this->assertSame('select * from [users] where day([created_at]) = ?', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testWhereMonthSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereMonth('created_at', '=', 5);
- $this->assertSame('select * from [users] where month([created_at]) = ?', $builder->toSql());
- $this->assertEquals([0 => 5], $builder->getBindings());
- }
- public function testWhereYearSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereYear('created_at', '=', 2014);
- $this->assertSame('select * from [users] where year([created_at]) = ?', $builder->toSql());
- $this->assertEquals([0 => 2014], $builder->getBindings());
- }
- public function testWhereBetweens()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereBetween('id', [1, 2]);
- $this->assertSame('select * from "users" where "id" between ? and ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereBetween('id', [[1, 2, 3]]);
- $this->assertSame('select * from "users" where "id" between ? and ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereBetween('id', [[1], [2, 3]]);
- $this->assertSame('select * from "users" where "id" between ? and ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereNotBetween('id', [1, 2]);
- $this->assertSame('select * from "users" where "id" not between ? and ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereBetween('id', [new Raw(1), new Raw(2)]);
- $this->assertSame('select * from "users" where "id" between 1 and 2', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testWhereBetweenColumns()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereBetweenColumns('id', ['users.created_at', 'users.updated_at']);
- $this->assertSame('select * from "users" where "id" between "users"."created_at" and "users"."updated_at"', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereNotBetweenColumns('id', ['created_at', 'updated_at']);
- $this->assertSame('select * from "users" where "id" not between "created_at" and "updated_at"', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereBetweenColumns('id', [new Raw(1), new Raw(2)]);
- $this->assertSame('select * from "users" where "id" between 1 and 2', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testBasicOrWheres()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhere('email', '=', 'foo');
- $this->assertSame('select * from "users" where "id" = ? or "email" = ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
- }
- public function testRawWheres()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereRaw('id = ? or email = ?', [1, 'foo']);
- $this->assertSame('select * from "users" where id = ? or email = ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
- }
- public function testRawOrWheres()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereRaw('email = ?', ['foo']);
- $this->assertSame('select * from "users" where "id" = ? or email = ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
- }
- public function testBasicWhereIns()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereIn('id', [1, 2, 3]);
- $this->assertSame('select * from "users" where "id" in (?, ?, ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereIn('id', [1, 2, 3]);
- $this->assertSame('select * from "users" where "id" = ? or "id" in (?, ?, ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 1, 2 => 2, 3 => 3], $builder->getBindings());
- }
- public function testBasicWhereNotIns()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereNotIn('id', [1, 2, 3]);
- $this->assertSame('select * from "users" where "id" not in (?, ?, ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereNotIn('id', [1, 2, 3]);
- $this->assertSame('select * from "users" where "id" = ? or "id" not in (?, ?, ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 1, 2 => 2, 3 => 3], $builder->getBindings());
- }
- public function testRawWhereIns()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereIn('id', [new Raw(1)]);
- $this->assertSame('select * from "users" where "id" in (1)', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereIn('id', [new Raw(1)]);
- $this->assertSame('select * from "users" where "id" = ? or "id" in (1)', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testEmptyWhereIns()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereIn('id', []);
- $this->assertSame('select * from "users" where 0 = 1', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereIn('id', []);
- $this->assertSame('select * from "users" where "id" = ? or 0 = 1', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testEmptyWhereNotIns()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereNotIn('id', []);
- $this->assertSame('select * from "users" where 1 = 1', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereNotIn('id', []);
- $this->assertSame('select * from "users" where "id" = ? or 1 = 1', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testWhereIntegerInRaw()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereIntegerInRaw('id', ['1a', 2]);
- $this->assertSame('select * from "users" where "id" in (1, 2)', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testOrWhereIntegerInRaw()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereIntegerInRaw('id', ['1a', 2]);
- $this->assertSame('select * from "users" where "id" = ? or "id" in (1, 2)', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testWhereIntegerNotInRaw()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereIntegerNotInRaw('id', ['1a', 2]);
- $this->assertSame('select * from "users" where "id" not in (1, 2)', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testOrWhereIntegerNotInRaw()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereIntegerNotInRaw('id', ['1a', 2]);
- $this->assertSame('select * from "users" where "id" = ? or "id" not in (1, 2)', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testEmptyWhereIntegerInRaw()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereIntegerInRaw('id', []);
- $this->assertSame('select * from "users" where 0 = 1', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testEmptyWhereIntegerNotInRaw()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereIntegerNotInRaw('id', []);
- $this->assertSame('select * from "users" where 1 = 1', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testBasicWhereColumn()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereColumn('first_name', 'last_name')->orWhereColumn('first_name', 'middle_name');
- $this->assertSame('select * from "users" where "first_name" = "last_name" or "first_name" = "middle_name"', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereColumn('updated_at', '>', 'created_at');
- $this->assertSame('select * from "users" where "updated_at" > "created_at"', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testArrayWhereColumn()
- {
- $conditions = [
- ['first_name', 'last_name'],
- ['updated_at', '>', 'created_at'],
- ];
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereColumn($conditions);
- $this->assertSame('select * from "users" where ("first_name" = "last_name" and "updated_at" > "created_at")', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testWhereFulltextMySql()
- {
- $builder = $this->getMySqlBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', 'Hello World');
- $this->assertSame('select * from `users` where match (`body`) against (? in natural language mode)', $builder->toSql());
- $this->assertEquals(['Hello World'], $builder->getBindings());
- $builder = $this->getMySqlBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', 'Hello World', ['expanded' => true]);
- $this->assertSame('select * from `users` where match (`body`) against (? in natural language mode with query expansion)', $builder->toSql());
- $this->assertEquals(['Hello World'], $builder->getBindings());
- $builder = $this->getMySqlBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', '+Hello -World', ['mode' => 'boolean']);
- $this->assertSame('select * from `users` where match (`body`) against (? in boolean mode)', $builder->toSql());
- $this->assertEquals(['+Hello -World'], $builder->getBindings());
- $builder = $this->getMySqlBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', '+Hello -World', ['mode' => 'boolean', 'expanded' => true]);
- $this->assertSame('select * from `users` where match (`body`) against (? in boolean mode)', $builder->toSql());
- $this->assertEquals(['+Hello -World'], $builder->getBindings());
- $builder = $this->getMySqlBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext(['body', 'title'], 'Car,Plane');
- $this->assertSame('select * from `users` where match (`body`, `title`) against (? in natural language mode)', $builder->toSql());
- $this->assertEquals(['Car,Plane'], $builder->getBindings());
- }
- public function testWhereFulltextPostgres()
- {
- $builder = $this->getPostgresBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', 'Hello World');
- $this->assertSame('select * from "users" where (to_tsvector(\'english\', "body")) @@ plainto_tsquery(\'english\', ?)', $builder->toSql());
- $this->assertEquals(['Hello World'], $builder->getBindings());
- $builder = $this->getPostgresBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', 'Hello World', ['language' => 'simple']);
- $this->assertSame('select * from "users" where (to_tsvector(\'simple\', "body")) @@ plainto_tsquery(\'simple\', ?)', $builder->toSql());
- $this->assertEquals(['Hello World'], $builder->getBindings());
- $builder = $this->getPostgresBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', 'Hello World', ['mode' => 'plain']);
- $this->assertSame('select * from "users" where (to_tsvector(\'english\', "body")) @@ plainto_tsquery(\'english\', ?)', $builder->toSql());
- $this->assertEquals(['Hello World'], $builder->getBindings());
- $builder = $this->getPostgresBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', 'Hello World', ['mode' => 'phrase']);
- $this->assertSame('select * from "users" where (to_tsvector(\'english\', "body")) @@ phraseto_tsquery(\'english\', ?)', $builder->toSql());
- $this->assertEquals(['Hello World'], $builder->getBindings());
- $builder = $this->getPostgresBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', '+Hello -World', ['mode' => 'websearch']);
- $this->assertSame('select * from "users" where (to_tsvector(\'english\', "body")) @@ websearch_to_tsquery(\'english\', ?)', $builder->toSql());
- $this->assertEquals(['+Hello -World'], $builder->getBindings());
- $builder = $this->getPostgresBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext('body', 'Hello World', ['language' => 'simple', 'mode' => 'plain']);
- $this->assertSame('select * from "users" where (to_tsvector(\'simple\', "body")) @@ plainto_tsquery(\'simple\', ?)', $builder->toSql());
- $this->assertEquals(['Hello World'], $builder->getBindings());
- $builder = $this->getPostgresBuilderWithProcessor();
- $builder->select('*')->from('users')->whereFulltext(['body', 'title'], 'Car Plane');
- $this->assertSame('select * from "users" where (to_tsvector(\'english\', "body") || to_tsvector(\'english\', "title")) @@ plainto_tsquery(\'english\', ?)', $builder->toSql());
- $this->assertEquals(['Car Plane'], $builder->getBindings());
- }
- public function testUnions()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1);
- $builder->union($this->getBuilder()->select('*')->from('users')->where('id', '=', 2));
- $this->assertSame('(select * from "users" where "id" = ?) union (select * from "users" where "id" = ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1);
- $builder->union($this->getMySqlBuilder()->select('*')->from('users')->where('id', '=', 2));
- $this->assertSame('(select * from `users` where `id` = ?) union (select * from `users` where `id` = ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getMysqlBuilder();
- $expectedSql = '(select `a` from `t1` where `a` = ? and `b` = ?) union (select `a` from `t2` where `a` = ? and `b` = ?) order by `a` asc limit 10';
- $union = $this->getMysqlBuilder()->select('a')->from('t2')->where('a', 11)->where('b', 2);
- $builder->select('a')->from('t1')->where('a', 10)->where('b', 1)->union($union)->orderBy('a')->limit(10);
- $this->assertEquals($expectedSql, $builder->toSql());
- $this->assertEquals([0 => 10, 1 => 1, 2 => 11, 3 => 2], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $expectedSql = '(select "name" from "users" where "id" = ?) union (select "name" from "users" where "id" = ?)';
- $builder->select('name')->from('users')->where('id', '=', 1);
- $builder->union($this->getPostgresBuilder()->select('name')->from('users')->where('id', '=', 2));
- $this->assertEquals($expectedSql, $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getSQLiteBuilder();
- $expectedSql = 'select * from (select "name" from "users" where "id" = ?) union select * from (select "name" from "users" where "id" = ?)';
- $builder->select('name')->from('users')->where('id', '=', 1);
- $builder->union($this->getSQLiteBuilder()->select('name')->from('users')->where('id', '=', 2));
- $this->assertEquals($expectedSql, $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $expectedSql = 'select * from (select [name] from [users] where [id] = ?) as [temp_table] union select * from (select [name] from [users] where [id] = ?) as [temp_table]';
- $builder->select('name')->from('users')->where('id', '=', 1);
- $builder->union($this->getSqlServerBuilder()->select('name')->from('users')->where('id', '=', 2));
- $this->assertEquals($expectedSql, $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- }
- public function testUnionAlls()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1);
- $builder->unionAll($this->getBuilder()->select('*')->from('users')->where('id', '=', 2));
- $this->assertSame('(select * from "users" where "id" = ?) union all (select * from "users" where "id" = ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $expectedSql = '(select * from "users" where "id" = ?) union all (select * from "users" where "id" = ?)';
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1);
- $builder->unionAll($this->getBuilder()->select('*')->from('users')->where('id', '=', 2));
- $this->assertEquals($expectedSql, $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- }
- public function testMultipleUnions()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1);
- $builder->union($this->getBuilder()->select('*')->from('users')->where('id', '=', 2));
- $builder->union($this->getBuilder()->select('*')->from('users')->where('id', '=', 3));
- $this->assertSame('(select * from "users" where "id" = ?) union (select * from "users" where "id" = ?) union (select * from "users" where "id" = ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
- }
- public function testMultipleUnionAlls()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1);
- $builder->unionAll($this->getBuilder()->select('*')->from('users')->where('id', '=', 2));
- $builder->unionAll($this->getBuilder()->select('*')->from('users')->where('id', '=', 3));
- $this->assertSame('(select * from "users" where "id" = ?) union all (select * from "users" where "id" = ?) union all (select * from "users" where "id" = ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
- }
- public function testUnionOrderBys()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1);
- $builder->union($this->getBuilder()->select('*')->from('users')->where('id', '=', 2));
- $builder->orderBy('id', 'desc');
- $this->assertSame('(select * from "users" where "id" = ?) union (select * from "users" where "id" = ?) order by "id" desc', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- }
- public function testUnionLimitsAndOffsets()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users');
- $builder->union($this->getBuilder()->select('*')->from('dogs'));
- $builder->skip(5)->take(10);
- $this->assertSame('(select * from "users") union (select * from "dogs") limit 10 offset 5', $builder->toSql());
- $expectedSql = '(select * from "users") union (select * from "dogs") limit 10 offset 5';
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users');
- $builder->union($this->getBuilder()->select('*')->from('dogs'));
- $builder->skip(5)->take(10);
- $this->assertEquals($expectedSql, $builder->toSql());
- $expectedSql = '(select * from "users" limit 11) union (select * from "dogs" limit 22) limit 10 offset 5';
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->limit(11);
- $builder->union($this->getBuilder()->select('*')->from('dogs')->limit(22));
- $builder->skip(5)->take(10);
- $this->assertEquals($expectedSql, $builder->toSql());
- }
- public function testUnionWithJoin()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users');
- $builder->union($this->getBuilder()->select('*')->from('dogs')->join('breeds', function ($join) {
- $join->on('dogs.breed_id', '=', 'breeds.id')
- ->where('breeds.is_native', '=', 1);
- }));
- $this->assertSame('(select * from "users") union (select * from "dogs" inner join "breeds" on "dogs"."breed_id" = "breeds"."id" and "breeds"."is_native" = ?)', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testMySqlUnionOrderBys()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1);
- $builder->union($this->getMySqlBuilder()->select('*')->from('users')->where('id', '=', 2));
- $builder->orderBy('id', 'desc');
- $this->assertSame('(select * from `users` where `id` = ?) union (select * from `users` where `id` = ?) order by `id` desc', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- }
- public function testMySqlUnionLimitsAndOffsets()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users');
- $builder->union($this->getMySqlBuilder()->select('*')->from('dogs'));
- $builder->skip(5)->take(10);
- $this->assertSame('(select * from `users`) union (select * from `dogs`) limit 10 offset 5', $builder->toSql());
- }
- public function testUnionAggregate()
- {
- $expected = 'select count(*) as aggregate from ((select * from `posts`) union (select * from `videos`)) as `temp_table`';
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true);
- $builder->getProcessor()->shouldReceive('processSelect')->once();
- $builder->from('posts')->union($this->getMySqlBuilder()->from('videos'))->count();
- $expected = 'select count(*) as aggregate from ((select `id` from `posts`) union (select `id` from `videos`)) as `temp_table`';
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true);
- $builder->getProcessor()->shouldReceive('processSelect')->once();
- $builder->from('posts')->select('id')->union($this->getMySqlBuilder()->from('videos')->select('id'))->count();
- $expected = 'select count(*) as aggregate from ((select * from "posts") union (select * from "videos")) as "temp_table"';
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true);
- $builder->getProcessor()->shouldReceive('processSelect')->once();
- $builder->from('posts')->union($this->getPostgresBuilder()->from('videos'))->count();
- $expected = 'select count(*) as aggregate from (select * from (select * from "posts") union select * from (select * from "videos")) as "temp_table"';
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true);
- $builder->getProcessor()->shouldReceive('processSelect')->once();
- $builder->from('posts')->union($this->getSQLiteBuilder()->from('videos'))->count();
- $expected = 'select count(*) as aggregate from (select * from (select * from [posts]) as [temp_table] union select * from (select * from [videos]) as [temp_table]) as [temp_table]';
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true);
- $builder->getProcessor()->shouldReceive('processSelect')->once();
- $builder->from('posts')->union($this->getSqlServerBuilder()->from('videos'))->count();
- }
- public function testHavingAggregate()
- {
- $expected = 'select count(*) as aggregate from (select (select `count(*)` from `videos` where `posts`.`id` = `videos`.`post_id`) as `videos_count` from `posts` having `videos_count` > ?) as `temp_table`';
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('getDatabaseName');
- $builder->getConnection()->shouldReceive('select')->once()->with($expected, [0 => 1], true)->andReturn([['aggregate' => 1]]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $builder->from('posts')->selectSub(function ($query) {
- $query->from('videos')->select('count(*)')->whereColumn('posts.id', '=', 'videos.post_id');
- }, 'videos_count')->having('videos_count', '>', 1);
- $builder->count();
- }
- public function testSubSelectWhereIns()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereIn('id', function ($q) {
- $q->select('id')->from('users')->where('age', '>', 25)->take(3);
- });
- $this->assertSame('select * from "users" where "id" in (select "id" from "users" where "age" > ? limit 3)', $builder->toSql());
- $this->assertEquals([25], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereNotIn('id', function ($q) {
- $q->select('id')->from('users')->where('age', '>', 25)->take(3);
- });
- $this->assertSame('select * from "users" where "id" not in (select "id" from "users" where "age" > ? limit 3)', $builder->toSql());
- $this->assertEquals([25], $builder->getBindings());
- }
- public function testBasicWhereNulls()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereNull('id');
- $this->assertSame('select * from "users" where "id" is null', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereNull('id');
- $this->assertSame('select * from "users" where "id" = ? or "id" is null', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testJsonWhereNullMysql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereNull('items->id');
- $this->assertSame('select * from `users` where (json_extract(`items`, \'$."id"\') is null OR json_type(json_extract(`items`, \'$."id"\')) = \'NULL\')', $builder->toSql());
- }
- public function testJsonWhereNotNullMysql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereNotNull('items->id');
- $this->assertSame('select * from `users` where (json_extract(`items`, \'$."id"\') is not null AND json_type(json_extract(`items`, \'$."id"\')) != \'NULL\')', $builder->toSql());
- }
- public function testArrayWhereNulls()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereNull(['id', 'expires_at']);
- $this->assertSame('select * from "users" where "id" is null and "expires_at" is null', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereNull(['id', 'expires_at']);
- $this->assertSame('select * from "users" where "id" = ? or "id" is null or "expires_at" is null', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testBasicWhereNotNulls()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereNotNull('id');
- $this->assertSame('select * from "users" where "id" is not null', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '>', 1)->orWhereNotNull('id');
- $this->assertSame('select * from "users" where "id" > ? or "id" is not null', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testArrayWhereNotNulls()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->whereNotNull(['id', 'expires_at']);
- $this->assertSame('select * from "users" where "id" is not null and "expires_at" is not null', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', '>', 1)->orWhereNotNull(['id', 'expires_at']);
- $this->assertSame('select * from "users" where "id" > ? or "id" is not null or "expires_at" is not null', $builder->toSql());
- $this->assertEquals([0 => 1], $builder->getBindings());
- }
- public function testGroupBys()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->groupBy('email');
- $this->assertSame('select * from "users" group by "email"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->groupBy('id', 'email');
- $this->assertSame('select * from "users" group by "id", "email"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->groupBy(['id', 'email']);
- $this->assertSame('select * from "users" group by "id", "email"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->groupBy(new Raw('DATE(created_at)'));
- $this->assertSame('select * from "users" group by DATE(created_at)', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->groupByRaw('DATE(created_at), ? DESC', ['foo']);
- $this->assertSame('select * from "users" group by DATE(created_at), ? DESC', $builder->toSql());
- $this->assertEquals(['foo'], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->havingRaw('?', ['havingRawBinding'])->groupByRaw('?', ['groupByRawBinding'])->whereRaw('?', ['whereRawBinding']);
- $this->assertEquals(['whereRawBinding', 'groupByRawBinding', 'havingRawBinding'], $builder->getBindings());
- }
- public function testOrderBys()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->orderBy('email')->orderBy('age', 'desc');
- $this->assertSame('select * from "users" order by "email" asc, "age" desc', $builder->toSql());
- $builder->orders = null;
- $this->assertSame('select * from "users"', $builder->toSql());
- $builder->orders = [];
- $this->assertSame('select * from "users"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->orderBy('email')->orderByRaw('"age" ? desc', ['foo']);
- $this->assertSame('select * from "users" order by "email" asc, "age" ? desc', $builder->toSql());
- $this->assertEquals(['foo'], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->orderByDesc('name');
- $this->assertSame('select * from "users" order by "name" desc', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('posts')->where('public', 1)
- ->unionAll($this->getBuilder()->select('*')->from('videos')->where('public', 1))
- ->orderByRaw('field(category, ?, ?) asc', ['news', 'opinion']);
- $this->assertSame('(select * from "posts" where "public" = ?) union all (select * from "videos" where "public" = ?) order by field(category, ?, ?) asc', $builder->toSql());
- $this->assertEquals([1, 1, 'news', 'opinion'], $builder->getBindings());
- }
- public function testOrderBysSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->orderBy('email')->orderBy('age', 'desc');
- $this->assertSame('select * from [users] order by [email] asc, [age] desc', $builder->toSql());
- $builder->orders = null;
- $this->assertSame('select * from [users]', $builder->toSql());
- $builder->orders = [];
- $this->assertSame('select * from [users]', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->orderBy('email');
- $this->assertSame('select * from [users] order by [email] asc', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->orderByDesc('name');
- $this->assertSame('select * from [users] order by [name] desc', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->orderByRaw('[age] asc');
- $this->assertSame('select * from [users] order by [age] asc', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->orderBy('email')->orderByRaw('[age] ? desc', ['foo']);
- $this->assertSame('select * from [users] order by [email] asc, [age] ? desc', $builder->toSql());
- $this->assertEquals(['foo'], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->skip(25)->take(10)->orderByRaw('[email] desc');
- $this->assertSame('select * from [users] order by [email] desc offset 25 rows fetch next 10 rows only', $builder->toSql());
- }
- public function testReorder()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->orderBy('name');
- $this->assertSame('select * from "users" order by "name" asc', $builder->toSql());
- $builder->reorder();
- $this->assertSame('select * from "users"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->orderBy('name');
- $this->assertSame('select * from "users" order by "name" asc', $builder->toSql());
- $builder->reorder('email', 'desc');
- $this->assertSame('select * from "users" order by "email" desc', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('first');
- $builder->union($this->getBuilder()->select('*')->from('second'));
- $builder->orderBy('name');
- $this->assertSame('(select * from "first") union (select * from "second") order by "name" asc', $builder->toSql());
- $builder->reorder();
- $this->assertSame('(select * from "first") union (select * from "second")', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->orderByRaw('?', [true]);
- $this->assertEquals([true], $builder->getBindings());
- $builder->reorder();
- $this->assertEquals([], $builder->getBindings());
- }
- public function testOrderBySubQueries()
- {
- $expected = 'select * from "users" order by (select "created_at" from "logins" where "user_id" = "users"."id" limit 1)';
- $subQuery = function ($query) {
- return $query->select('created_at')->from('logins')->whereColumn('user_id', 'users.id')->limit(1);
- };
- $builder = $this->getBuilder()->select('*')->from('users')->orderBy($subQuery);
- $this->assertSame("$expected asc", $builder->toSql());
- $builder = $this->getBuilder()->select('*')->from('users')->orderBy($subQuery, 'desc');
- $this->assertSame("$expected desc", $builder->toSql());
- $builder = $this->getBuilder()->select('*')->from('users')->orderByDesc($subQuery);
- $this->assertSame("$expected desc", $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('posts')->where('public', 1)
- ->unionAll($this->getBuilder()->select('*')->from('videos')->where('public', 1))
- ->orderBy($this->getBuilder()->selectRaw('field(category, ?, ?)', ['news', 'opinion']));
- $this->assertSame('(select * from "posts" where "public" = ?) union all (select * from "videos" where "public" = ?) order by (select field(category, ?, ?)) asc', $builder->toSql());
- $this->assertEquals([1, 1, 'news', 'opinion'], $builder->getBindings());
- }
- public function testOrderByInvalidDirectionParam()
- {
- $this->expectException(InvalidArgumentException::class);
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->orderBy('age', 'asec');
- }
- public function testHavings()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->having('email', '>', 1);
- $this->assertSame('select * from "users" having "email" > ?', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')
- ->orHaving('email', '=', 'test@example.com')
- ->orHaving('email', '=', 'test2@example.com');
- $this->assertSame('select * from "users" having "email" = ? or "email" = ?', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->groupBy('email')->having('email', '>', 1);
- $this->assertSame('select * from "users" group by "email" having "email" > ?', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('email as foo_email')->from('users')->having('foo_email', '>', 1);
- $this->assertSame('select "email" as "foo_email" from "users" having "foo_email" > ?', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select(['category', new Raw('count(*) as "total"')])->from('item')->where('department', '=', 'popular')->groupBy('category')->having('total', '>', new Raw('3'));
- $this->assertSame('select "category", count(*) as "total" from "item" where "department" = ? group by "category" having "total" > 3', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select(['category', new Raw('count(*) as "total"')])->from('item')->where('department', '=', 'popular')->groupBy('category')->having('total', '>', 3);
- $this->assertSame('select "category", count(*) as "total" from "item" where "department" = ? group by "category" having "total" > ?', $builder->toSql());
- }
- public function testHavingBetweens()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->havingBetween('id', [1, 2, 3]);
- $this->assertSame('select * from "users" having "id" between ? and ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->havingBetween('id', [[1, 2], [3, 4]]);
- $this->assertSame('select * from "users" having "id" between ? and ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- }
- public function testHavingShortcut()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->having('email', 1)->orHaving('email', 2);
- $this->assertSame('select * from "users" having "email" = ? or "email" = ?', $builder->toSql());
- }
- public function testHavingFollowedBySelectGet()
- {
- $builder = $this->getBuilder();
- $query = 'select "category", count(*) as "total" from "item" where "department" = ? group by "category" having "total" > ?';
- $builder->getConnection()->shouldReceive('select')->once()->with($query, ['popular', 3], true)->andReturn([['category' => 'rock', 'total' => 5]]);
- $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $builder->from('item');
- $result = $builder->select(['category', new Raw('count(*) as "total"')])->where('department', '=', 'popular')->groupBy('category')->having('total', '>', 3)->get();
- $this->assertEquals([['category' => 'rock', 'total' => 5]], $result->all());
- // Using \Raw value
- $builder = $this->getBuilder();
- $query = 'select "category", count(*) as "total" from "item" where "department" = ? group by "category" having "total" > 3';
- $builder->getConnection()->shouldReceive('select')->once()->with($query, ['popular'], true)->andReturn([['category' => 'rock', 'total' => 5]]);
- $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $builder->from('item');
- $result = $builder->select(['category', new Raw('count(*) as "total"')])->where('department', '=', 'popular')->groupBy('category')->having('total', '>', new Raw('3'))->get();
- $this->assertEquals([['category' => 'rock', 'total' => 5]], $result->all());
- }
- public function testRawHavings()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->havingRaw('user_foo < user_bar');
- $this->assertSame('select * from "users" having user_foo < user_bar', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->having('baz', '=', 1)->orHavingRaw('user_foo < user_bar');
- $this->assertSame('select * from "users" having "baz" = ? or user_foo < user_bar', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->havingBetween('last_login_date', ['2018-11-16', '2018-12-16'])->orHavingRaw('user_foo < user_bar');
- $this->assertSame('select * from "users" having "last_login_date" between ? and ? or user_foo < user_bar', $builder->toSql());
- }
- public function testLimitsAndOffsets()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->offset(5)->limit(10);
- $this->assertSame('select * from "users" limit 10 offset 5', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->limit(null);
- $this->assertSame('select * from "users"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->limit(0);
- $this->assertSame('select * from "users" limit 0', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->skip(5)->take(10);
- $this->assertSame('select * from "users" limit 10 offset 5', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->skip(0)->take(0);
- $this->assertSame('select * from "users" limit 0 offset 0', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->skip(-5)->take(-10);
- $this->assertSame('select * from "users" offset 0', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->skip(null)->take(null);
- $this->assertSame('select * from "users" offset 0', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->skip(5)->take(null);
- $this->assertSame('select * from "users" offset 5', $builder->toSql());
- }
- public function testForPage()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->forPage(2, 15);
- $this->assertSame('select * from "users" limit 15 offset 15', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->forPage(0, 15);
- $this->assertSame('select * from "users" limit 15 offset 0', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->forPage(-2, 15);
- $this->assertSame('select * from "users" limit 15 offset 0', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->forPage(2, 0);
- $this->assertSame('select * from "users" limit 0 offset 0', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->forPage(0, 0);
- $this->assertSame('select * from "users" limit 0 offset 0', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->forPage(-2, 0);
- $this->assertSame('select * from "users" limit 0 offset 0', $builder->toSql());
- }
- public function testGetCountForPaginationWithBindings()
- {
- $builder = $this->getBuilder();
- $builder->from('users')->selectSub(function ($q) {
- $q->select('body')->from('posts')->where('id', 4);
- }, 'post');
- $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $count = $builder->getCountForPagination();
- $this->assertEquals(1, $count);
- $this->assertEquals([4], $builder->getBindings());
- }
- public function testGetCountForPaginationWithColumnAliases()
- {
- $builder = $this->getBuilder();
- $columns = ['body as post_body', 'teaser', 'posts.created as published'];
- $builder->from('posts')->select($columns);
- $builder->getConnection()->shouldReceive('select')->once()->with('select count("body", "teaser", "posts"."created") as aggregate from "posts"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $count = $builder->getCountForPagination($columns);
- $this->assertEquals(1, $count);
- }
- public function testGetCountForPaginationWithUnion()
- {
- $builder = $this->getBuilder();
- $builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'));
- $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $count = $builder->getCountForPagination();
- $this->assertEquals(1, $count);
- }
- public function testWhereShortcut()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('id', 1)->orWhere('name', 'foo');
- $this->assertSame('select * from "users" where "id" = ? or "name" = ?', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
- }
- public function testWhereWithArrayConditions()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where([['foo', 1], ['bar', 2]]);
- $this->assertSame('select * from "users" where ("foo" = ? and "bar" = ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where(['foo' => 1, 'bar' => 2]);
- $this->assertSame('select * from "users" where ("foo" = ? and "bar" = ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where([['foo', 1], ['bar', '<', 2]]);
- $this->assertSame('select * from "users" where ("foo" = ? and "bar" < ?)', $builder->toSql());
- $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
- }
- public function testNestedWheres()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('email', '=', 'foo')->orWhere(function ($q) {
- $q->where('name', '=', 'bar')->where('age', '=', 25);
- });
- $this->assertSame('select * from "users" where "email" = ? or ("name" = ? and "age" = ?)', $builder->toSql());
- $this->assertEquals([0 => 'foo', 1 => 'bar', 2 => 25], $builder->getBindings());
- }
- public function testNestedWhereBindings()
- {
- $builder = $this->getBuilder();
- $builder->where('email', '=', 'foo')->where(function ($q) {
- $q->selectRaw('?', ['ignore'])->where('name', '=', 'bar');
- });
- $this->assertEquals([0 => 'foo', 1 => 'bar'], $builder->getBindings());
- }
- public function testFullSubSelects()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('email', '=', 'foo')->orWhere('id', '=', function ($q) {
- $q->select(new Raw('max(id)'))->from('users')->where('email', '=', 'bar');
- });
- $this->assertSame('select * from "users" where "email" = ? or "id" = (select max(id) from "users" where "email" = ?)', $builder->toSql());
- $this->assertEquals([0 => 'foo', 1 => 'bar'], $builder->getBindings());
- }
- public function testWhereExists()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('orders')->whereExists(function ($q) {
- $q->select('*')->from('products')->where('products.id', '=', new Raw('"orders"."id"'));
- });
- $this->assertSame('select * from "orders" where exists (select * from "products" where "products"."id" = "orders"."id")', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('orders')->whereNotExists(function ($q) {
- $q->select('*')->from('products')->where('products.id', '=', new Raw('"orders"."id"'));
- });
- $this->assertSame('select * from "orders" where not exists (select * from "products" where "products"."id" = "orders"."id")', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('orders')->where('id', '=', 1)->orWhereExists(function ($q) {
- $q->select('*')->from('products')->where('products.id', '=', new Raw('"orders"."id"'));
- });
- $this->assertSame('select * from "orders" where "id" = ? or exists (select * from "products" where "products"."id" = "orders"."id")', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('orders')->where('id', '=', 1)->orWhereNotExists(function ($q) {
- $q->select('*')->from('products')->where('products.id', '=', new Raw('"orders"."id"'));
- });
- $this->assertSame('select * from "orders" where "id" = ? or not exists (select * from "products" where "products"."id" = "orders"."id")', $builder->toSql());
- }
- public function testBasicJoins()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', 'users.id', 'contacts.id');
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->leftJoin('photos', 'users.id', '=', 'photos.id');
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" left join "photos" on "users"."id" = "photos"."id"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->leftJoinWhere('photos', 'users.id', '=', 'bar')->joinWhere('photos', 'users.id', '=', 'foo');
- $this->assertSame('select * from "users" left join "photos" on "users"."id" = ? inner join "photos" on "users"."id" = ?', $builder->toSql());
- $this->assertEquals(['bar', 'foo'], $builder->getBindings());
- }
- public function testCrossJoins()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('sizes')->crossJoin('colors');
- $this->assertSame('select * from "sizes" cross join "colors"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('tableB')->join('tableA', 'tableA.column1', '=', 'tableB.column2', 'cross');
- $this->assertSame('select * from "tableB" cross join "tableA" on "tableA"."column1" = "tableB"."column2"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('tableB')->crossJoin('tableA', 'tableA.column1', '=', 'tableB.column2');
- $this->assertSame('select * from "tableB" cross join "tableA" on "tableA"."column1" = "tableB"."column2"', $builder->toSql());
- }
- public function testCrossJoinSubs()
- {
- $builder = $this->getBuilder();
- $builder->selectRaw('(sale / overall.sales) * 100 AS percent_of_total')->from('sales')->crossJoinSub($this->getBuilder()->selectRaw('SUM(sale) AS sales')->from('sales'), 'overall');
- $this->assertSame('select (sale / overall.sales) * 100 AS percent_of_total from "sales" cross join (select SUM(sale) AS sales from "sales") as "overall"', $builder->toSql());
- }
- public function testComplexJoin()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->orOn('users.name', '=', 'contacts.name');
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" or "users"."name" = "contacts"."name"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->where('users.id', '=', 'foo')->orWhere('users.name', '=', 'bar');
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = ? or "users"."name" = ?', $builder->toSql());
- $this->assertEquals(['foo', 'bar'], $builder->getBindings());
- // Run the assertions again
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = ? or "users"."name" = ?', $builder->toSql());
- $this->assertEquals(['foo', 'bar'], $builder->getBindings());
- }
- public function testJoinWhereNull()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->whereNull('contacts.deleted_at');
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" and "contacts"."deleted_at" is null', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->orWhereNull('contacts.deleted_at');
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" or "contacts"."deleted_at" is null', $builder->toSql());
- }
- public function testJoinWhereNotNull()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->whereNotNull('contacts.deleted_at');
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" and "contacts"."deleted_at" is not null', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->orWhereNotNull('contacts.deleted_at');
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" or "contacts"."deleted_at" is not null', $builder->toSql());
- }
- public function testJoinWhereIn()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->whereIn('contacts.name', [48, 'baz', null]);
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" and "contacts"."name" in (?, ?, ?)', $builder->toSql());
- $this->assertEquals([48, 'baz', null], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->orWhereIn('contacts.name', [48, 'baz', null]);
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" or "contacts"."name" in (?, ?, ?)', $builder->toSql());
- $this->assertEquals([48, 'baz', null], $builder->getBindings());
- }
- public function testJoinWhereInSubquery()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $q = $this->getBuilder();
- $q->select('name')->from('contacts')->where('name', 'baz');
- $j->on('users.id', '=', 'contacts.id')->whereIn('contacts.name', $q);
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" and "contacts"."name" in (select "name" from "contacts" where "name" = ?)', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $q = $this->getBuilder();
- $q->select('name')->from('contacts')->where('name', 'baz');
- $j->on('users.id', '=', 'contacts.id')->orWhereIn('contacts.name', $q);
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" or "contacts"."name" in (select "name" from "contacts" where "name" = ?)', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- }
- public function testJoinWhereNotIn()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->whereNotIn('contacts.name', [48, 'baz', null]);
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" and "contacts"."name" not in (?, ?, ?)', $builder->toSql());
- $this->assertEquals([48, 'baz', null], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->orWhereNotIn('contacts.name', [48, 'baz', null]);
- });
- $this->assertSame('select * from "users" inner join "contacts" on "users"."id" = "contacts"."id" or "contacts"."name" not in (?, ?, ?)', $builder->toSql());
- $this->assertEquals([48, 'baz', null], $builder->getBindings());
- }
- public function testJoinsWithNestedConditions()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->leftJoin('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->where(function ($j) {
- $j->where('contacts.country', '=', 'US')->orWhere('contacts.is_partner', '=', 1);
- });
- });
- $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and ("contacts"."country" = ? or "contacts"."is_partner" = ?)', $builder->toSql());
- $this->assertEquals(['US', 1], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->leftJoin('contacts', function ($j) {
- $j->on('users.id', '=', 'contacts.id')->where('contacts.is_active', '=', 1)->orOn(function ($j) {
- $j->orWhere(function ($j) {
- $j->where('contacts.country', '=', 'UK')->orOn('contacts.type', '=', 'users.type');
- })->where(function ($j) {
- $j->where('contacts.country', '=', 'US')->orWhereNull('contacts.is_partner');
- });
- });
- });
- $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and "contacts"."is_active" = ? or (("contacts"."country" = ? or "contacts"."type" = "users"."type") and ("contacts"."country" = ? or "contacts"."is_partner" is null))', $builder->toSql());
- $this->assertEquals([1, 'UK', 'US'], $builder->getBindings());
- }
- public function testJoinsWithAdvancedConditions()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->leftJoin('contacts', function ($j) {
- $j->on('users.id', 'contacts.id')->where(function ($j) {
- $j->whereRole('admin')
- ->orWhereNull('contacts.disabled')
- ->orWhereRaw('year(contacts.created_at) = 2016');
- });
- });
- $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and ("role" = ? or "contacts"."disabled" is null or year(contacts.created_at) = 2016)', $builder->toSql());
- $this->assertEquals(['admin'], $builder->getBindings());
- }
- public function testJoinsWithSubqueryCondition()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->leftJoin('contacts', function ($j) {
- $j->on('users.id', 'contacts.id')->whereIn('contact_type_id', function ($q) {
- $q->select('id')->from('contact_types')
- ->where('category_id', '1')
- ->whereNull('deleted_at');
- });
- });
- $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and "contact_type_id" in (select "id" from "contact_types" where "category_id" = ? and "deleted_at" is null)', $builder->toSql());
- $this->assertEquals(['1'], $builder->getBindings());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->leftJoin('contacts', function ($j) {
- $j->on('users.id', 'contacts.id')->whereExists(function ($q) {
- $q->selectRaw('1')->from('contact_types')
- ->whereRaw('contact_types.id = contacts.contact_type_id')
- ->where('category_id', '1')
- ->whereNull('deleted_at');
- });
- });
- $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where contact_types.id = contacts.contact_type_id and "category_id" = ? and "deleted_at" is null)', $builder->toSql());
- $this->assertEquals(['1'], $builder->getBindings());
- }
- public function testJoinsWithAdvancedSubqueryCondition()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->leftJoin('contacts', function ($j) {
- $j->on('users.id', 'contacts.id')->whereExists(function ($q) {
- $q->selectRaw('1')->from('contact_types')
- ->whereRaw('contact_types.id = contacts.contact_type_id')
- ->where('category_id', '1')
- ->whereNull('deleted_at')
- ->whereIn('level_id', function ($q) {
- $q->select('id')->from('levels')
- ->where('is_active', true);
- });
- });
- });
- $this->assertSame('select * from "users" left join "contacts" on "users"."id" = "contacts"."id" and exists (select 1 from "contact_types" where contact_types.id = contacts.contact_type_id and "category_id" = ? and "deleted_at" is null and "level_id" in (select "id" from "levels" where "is_active" = ?))', $builder->toSql());
- $this->assertEquals(['1', true], $builder->getBindings());
- }
- public function testJoinsWithNestedJoins()
- {
- $builder = $this->getBuilder();
- $builder->select('users.id', 'contacts.id', 'contact_types.id')->from('users')->leftJoin('contacts', function ($j) {
- $j->on('users.id', 'contacts.id')->join('contact_types', 'contacts.contact_type_id', '=', 'contact_types.id');
- });
- $this->assertSame('select "users"."id", "contacts"."id", "contact_types"."id" from "users" left join ("contacts" inner join "contact_types" on "contacts"."contact_type_id" = "contact_types"."id") on "users"."id" = "contacts"."id"', $builder->toSql());
- }
- public function testJoinsWithMultipleNestedJoins()
- {
- $builder = $this->getBuilder();
- $builder->select('users.id', 'contacts.id', 'contact_types.id', 'countrys.id', 'planets.id')->from('users')->leftJoin('contacts', function ($j) {
- $j->on('users.id', 'contacts.id')
- ->join('contact_types', 'contacts.contact_type_id', '=', 'contact_types.id')
- ->leftJoin('countrys', function ($q) {
- $q->on('contacts.country', '=', 'countrys.country')
- ->join('planets', function ($q) {
- $q->on('countrys.planet_id', '=', 'planet.id')
- ->where('planet.is_settled', '=', 1)
- ->where('planet.population', '>=', 10000);
- });
- });
- });
- $this->assertSame('select "users"."id", "contacts"."id", "contact_types"."id", "countrys"."id", "planets"."id" from "users" left join ("contacts" inner join "contact_types" on "contacts"."contact_type_id" = "contact_types"."id" left join ("countrys" inner join "planets" on "countrys"."planet_id" = "planet"."id" and "planet"."is_settled" = ? and "planet"."population" >= ?) on "contacts"."country" = "countrys"."country") on "users"."id" = "contacts"."id"', $builder->toSql());
- $this->assertEquals(['1', 10000], $builder->getBindings());
- }
- public function testJoinsWithNestedJoinWithAdvancedSubqueryCondition()
- {
- $builder = $this->getBuilder();
- $builder->select('users.id', 'contacts.id', 'contact_types.id')->from('users')->leftJoin('contacts', function ($j) {
- $j->on('users.id', 'contacts.id')
- ->join('contact_types', 'contacts.contact_type_id', '=', 'contact_types.id')
- ->whereExists(function ($q) {
- $q->select('*')->from('countrys')
- ->whereColumn('contacts.country', '=', 'countrys.country')
- ->join('planets', function ($q) {
- $q->on('countrys.planet_id', '=', 'planet.id')
- ->where('planet.is_settled', '=', 1);
- })
- ->where('planet.population', '>=', 10000);
- });
- });
- $this->assertSame('select "users"."id", "contacts"."id", "contact_types"."id" from "users" left join ("contacts" inner join "contact_types" on "contacts"."contact_type_id" = "contact_types"."id") on "users"."id" = "contacts"."id" and exists (select * from "countrys" inner join "planets" on "countrys"."planet_id" = "planet"."id" and "planet"."is_settled" = ? where "contacts"."country" = "countrys"."country" and "planet"."population" >= ?)', $builder->toSql());
- $this->assertEquals(['1', 10000], $builder->getBindings());
- }
- public function testJoinSub()
- {
- $builder = $this->getBuilder();
- $builder->from('users')->joinSub('select * from "contacts"', 'sub', 'users.id', '=', 'sub.id');
- $this->assertSame('select * from "users" inner join (select * from "contacts") as "sub" on "users"."id" = "sub"."id"', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->from('users')->joinSub(function ($q) {
- $q->from('contacts');
- }, 'sub', 'users.id', '=', 'sub.id');
- $this->assertSame('select * from "users" inner join (select * from "contacts") as "sub" on "users"."id" = "sub"."id"', $builder->toSql());
- $builder = $this->getBuilder();
- $eloquentBuilder = new EloquentBuilder($this->getBuilder()->from('contacts'));
- $builder->from('users')->joinSub($eloquentBuilder, 'sub', 'users.id', '=', 'sub.id');
- $this->assertSame('select * from "users" inner join (select * from "contacts") as "sub" on "users"."id" = "sub"."id"', $builder->toSql());
- $builder = $this->getBuilder();
- $sub1 = $this->getBuilder()->from('contacts')->where('name', 'foo');
- $sub2 = $this->getBuilder()->from('contacts')->where('name', 'bar');
- $builder->from('users')
- ->joinSub($sub1, 'sub1', 'users.id', '=', 1, 'inner', true)
- ->joinSub($sub2, 'sub2', 'users.id', '=', 'sub2.user_id');
- $expected = 'select * from "users" ';
- $expected .= 'inner join (select * from "contacts" where "name" = ?) as "sub1" on "users"."id" = ? ';
- $expected .= 'inner join (select * from "contacts" where "name" = ?) as "sub2" on "users"."id" = "sub2"."user_id"';
- $this->assertEquals($expected, $builder->toSql());
- $this->assertEquals(['foo', 1, 'bar'], $builder->getRawBindings()['join']);
- $this->expectException(InvalidArgumentException::class);
- $builder = $this->getBuilder();
- $builder->from('users')->joinSub(['foo'], 'sub', 'users.id', '=', 'sub.id');
- }
- public function testJoinSubWithPrefix()
- {
- $builder = $this->getBuilder();
- $builder->getGrammar()->setTablePrefix('prefix_');
- $builder->from('users')->joinSub('select * from "contacts"', 'sub', 'users.id', '=', 'sub.id');
- $this->assertSame('select * from "prefix_users" inner join (select * from "contacts") as "prefix_sub" on "prefix_users"."id" = "prefix_sub"."id"', $builder->toSql());
- }
- public function testLeftJoinSub()
- {
- $builder = $this->getBuilder();
- $builder->from('users')->leftJoinSub($this->getBuilder()->from('contacts'), 'sub', 'users.id', '=', 'sub.id');
- $this->assertSame('select * from "users" left join (select * from "contacts") as "sub" on "users"."id" = "sub"."id"', $builder->toSql());
- $this->expectException(InvalidArgumentException::class);
- $builder = $this->getBuilder();
- $builder->from('users')->leftJoinSub(['foo'], 'sub', 'users.id', '=', 'sub.id');
- }
- public function testRightJoinSub()
- {
- $builder = $this->getBuilder();
- $builder->from('users')->rightJoinSub($this->getBuilder()->from('contacts'), 'sub', 'users.id', '=', 'sub.id');
- $this->assertSame('select * from "users" right join (select * from "contacts") as "sub" on "users"."id" = "sub"."id"', $builder->toSql());
- $this->expectException(InvalidArgumentException::class);
- $builder = $this->getBuilder();
- $builder->from('users')->rightJoinSub(['foo'], 'sub', 'users.id', '=', 'sub.id');
- }
- public function testRawExpressionsInSelect()
- {
- $builder = $this->getBuilder();
- $builder->select(new Raw('substr(foo, 6)'))->from('users');
- $this->assertSame('select substr(foo, 6) from "users"', $builder->toSql());
- }
- public function testFindReturnsFirstResultByID()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([['foo' => 'bar']]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) {
- return $results;
- });
- $results = $builder->from('users')->find(1);
- $this->assertEquals(['foo' => 'bar'], $results);
- }
- public function testFirstMethodReturnsFirstResult()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([['foo' => 'bar']]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) {
- return $results;
- });
- $results = $builder->from('users')->where('id', '=', 1)->first();
- $this->assertEquals(['foo' => 'bar'], $results);
- }
- public function testPluckMethodGetsCollectionOfColumnValues()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->andReturn([['foo' => 'bar'], ['foo' => 'baz']]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar'], ['foo' => 'baz']])->andReturnUsing(function ($query, $results) {
- return $results;
- });
- $results = $builder->from('users')->where('id', '=', 1)->pluck('foo');
- $this->assertEquals(['bar', 'baz'], $results->all());
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->andReturn([['id' => 1, 'foo' => 'bar'], ['id' => 10, 'foo' => 'baz']]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['id' => 1, 'foo' => 'bar'], ['id' => 10, 'foo' => 'baz']])->andReturnUsing(function ($query, $results) {
- return $results;
- });
- $results = $builder->from('users')->where('id', '=', 1)->pluck('foo', 'id');
- $this->assertEquals([1 => 'bar', 10 => 'baz'], $results->all());
- }
- public function testImplode()
- {
- // Test without glue.
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->andReturn([['foo' => 'bar'], ['foo' => 'baz']]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar'], ['foo' => 'baz']])->andReturnUsing(function ($query, $results) {
- return $results;
- });
- $results = $builder->from('users')->where('id', '=', 1)->implode('foo');
- $this->assertSame('barbaz', $results);
- // Test with glue.
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->andReturn([['foo' => 'bar'], ['foo' => 'baz']]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar'], ['foo' => 'baz']])->andReturnUsing(function ($query, $results) {
- return $results;
- });
- $results = $builder->from('users')->where('id', '=', 1)->implode('foo', ',');
- $this->assertSame('bar,baz', $results);
- }
- public function testValueMethodReturnsSingleColumn()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select "foo" from "users" where "id" = ? limit 1', [1], true)->andReturn([['foo' => 'bar']]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturn([['foo' => 'bar']]);
- $results = $builder->from('users')->where('id', '=', 1)->value('foo');
- $this->assertSame('bar', $results);
- }
- public function testAggregateFunctions()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $results = $builder->from('users')->count();
- $this->assertEquals(1, $results);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select exists(select * from "users") as "exists"', [], true)->andReturn([['exists' => 1]]);
- $results = $builder->from('users')->exists();
- $this->assertTrue($results);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select exists(select * from "users") as "exists"', [], true)->andReturn([['exists' => 0]]);
- $results = $builder->from('users')->doesntExist();
- $this->assertTrue($results);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select max("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $results = $builder->from('users')->max('id');
- $this->assertEquals(1, $results);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select min("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $results = $builder->from('users')->min('id');
- $this->assertEquals(1, $results);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select sum("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $results = $builder->from('users')->sum('id');
- $this->assertEquals(1, $results);
- }
- public function testSqlServerExists()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select top 1 1 [exists] from [users]', [], true)->andReturn([['exists' => 1]]);
- $results = $builder->from('users')->exists();
- $this->assertTrue($results);
- }
- public function testExistsOr()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->andReturn([['exists' => 1]]);
- $results = $builder->from('users')->doesntExistOr(function () {
- return 123;
- });
- $this->assertSame(123, $results);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->andReturn([['exists' => 0]]);
- $results = $builder->from('users')->doesntExistOr(function () {
- throw new RuntimeException;
- });
- $this->assertTrue($results);
- }
- public function testDoesntExistsOr()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->andReturn([['exists' => 0]]);
- $results = $builder->from('users')->existsOr(function () {
- return 123;
- });
- $this->assertSame(123, $results);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->andReturn([['exists' => 1]]);
- $results = $builder->from('users')->existsOr(function () {
- throw new RuntimeException;
- });
- $this->assertTrue($results);
- }
- public function testAggregateResetFollowedByGet()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getConnection()->shouldReceive('select')->once()->with('select sum("id") as aggregate from "users"', [], true)->andReturn([['aggregate' => 2]]);
- $builder->getConnection()->shouldReceive('select')->once()->with('select "column1", "column2" from "users"', [], true)->andReturn([['column1' => 'foo', 'column2' => 'bar']]);
- $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $builder->from('users')->select('column1', 'column2');
- $count = $builder->count();
- $this->assertEquals(1, $count);
- $sum = $builder->sum('id');
- $this->assertEquals(2, $sum);
- $result = $builder->get();
- $this->assertEquals([['column1' => 'foo', 'column2' => 'bar']], $result->all());
- }
- public function testAggregateResetFollowedBySelectGet()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select count("column1") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getConnection()->shouldReceive('select')->once()->with('select "column2", "column3" from "users"', [], true)->andReturn([['column2' => 'foo', 'column3' => 'bar']]);
- $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $builder->from('users');
- $count = $builder->count('column1');
- $this->assertEquals(1, $count);
- $result = $builder->select('column2', 'column3')->get();
- $this->assertEquals([['column2' => 'foo', 'column3' => 'bar']], $result->all());
- }
- public function testAggregateResetFollowedByGetWithColumns()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select count("column1") as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getConnection()->shouldReceive('select')->once()->with('select "column2", "column3" from "users"', [], true)->andReturn([['column2' => 'foo', 'column3' => 'bar']]);
- $builder->getProcessor()->shouldReceive('processSelect')->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $builder->from('users');
- $count = $builder->count('column1');
- $this->assertEquals(1, $count);
- $result = $builder->get(['column2', 'column3']);
- $this->assertEquals([['column2' => 'foo', 'column3' => 'bar']], $result->all());
- }
- public function testAggregateWithSubSelect()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]);
- $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
- return $results;
- });
- $builder->from('users')->selectSub(function ($query) {
- $query->from('posts')->select('foo', 'bar')->where('title', 'foo');
- }, 'post');
- $count = $builder->count();
- $this->assertEquals(1, $count);
- $this->assertSame('(select "foo", "bar" from "posts" where "title" = ?) as "post"', $builder->columns[0]->getValue());
- $this->assertEquals(['foo'], $builder->getBindings());
- }
- public function testSubqueriesBindings()
- {
- $builder = $this->getBuilder();
- $second = $this->getBuilder()->select('*')->from('users')->orderByRaw('id = ?', 2);
- $third = $this->getBuilder()->select('*')->from('users')->where('id', 3)->groupBy('id')->having('id', '!=', 4);
- $builder->groupBy('a')->having('a', '=', 1)->union($second)->union($third);
- $this->assertEquals([0 => 1, 1 => 2, 2 => 3, 3 => 4], $builder->getBindings());
- $builder = $this->getBuilder()->select('*')->from('users')->where('email', '=', function ($q) {
- $q->select(new Raw('max(id)'))
- ->from('users')->where('email', '=', 'bar')
- ->orderByRaw('email like ?', '%.com')
- ->groupBy('id')->having('id', '=', 4);
- })->orWhere('id', '=', 'foo')->groupBy('id')->having('id', '=', 5);
- $this->assertEquals([0 => 'bar', 1 => 4, 2 => '%.com', 3 => 'foo', 4 => 5], $builder->getBindings());
- }
- public function testInsertMethod()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('insert')->once()->with('insert into "users" ("email") values (?)', ['foo'])->andReturn(true);
- $result = $builder->from('users')->insert(['email' => 'foo']);
- $this->assertTrue($result);
- }
- public function testInsertUsingMethod()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into "table1" ("foo") select "bar" from "table2" where "foreign_id" = ?', [5])->andReturn(1);
- $result = $builder->from('table1')->insertUsing(
- ['foo'],
- function (Builder $query) {
- $query->select(['bar'])->from('table2')->where('foreign_id', '=', 5);
- }
- );
- $this->assertEquals(1, $result);
- }
- public function testInsertUsingInvalidSubquery()
- {
- $this->expectException(InvalidArgumentException::class);
- $builder = $this->getBuilder();
- $builder->from('table1')->insertUsing(['foo'], ['bar']);
- }
- public function testInsertOrIgnoreMethod()
- {
- $this->expectException(RuntimeException::class);
- $this->expectExceptionMessage('does not support');
- $builder = $this->getBuilder();
- $builder->from('users')->insertOrIgnore(['email' => 'foo']);
- }
- public function testMySqlInsertOrIgnoreMethod()
- {
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert ignore into `users` (`email`) values (?)', ['foo'])->andReturn(1);
- $result = $builder->from('users')->insertOrIgnore(['email' => 'foo']);
- $this->assertEquals(1, $result);
- }
- public function testPostgresInsertOrIgnoreMethod()
- {
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into "users" ("email") values (?) on conflict do nothing', ['foo'])->andReturn(1);
- $result = $builder->from('users')->insertOrIgnore(['email' => 'foo']);
- $this->assertEquals(1, $result);
- }
- public function testSQLiteInsertOrIgnoreMethod()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert or ignore into "users" ("email") values (?)', ['foo'])->andReturn(1);
- $result = $builder->from('users')->insertOrIgnore(['email' => 'foo']);
- $this->assertEquals(1, $result);
- }
- public function testSqlServerInsertOrIgnoreMethod()
- {
- $this->expectException(RuntimeException::class);
- $this->expectExceptionMessage('does not support');
- $builder = $this->getSqlServerBuilder();
- $builder->from('users')->insertOrIgnore(['email' => 'foo']);
- }
- public function testInsertGetIdMethod()
- {
- $builder = $this->getBuilder();
- $builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into "users" ("email") values (?)', ['foo'], 'id')->andReturn(1);
- $result = $builder->from('users')->insertGetId(['email' => 'foo'], 'id');
- $this->assertEquals(1, $result);
- }
- public function testInsertGetIdMethodRemovesExpressions()
- {
- $builder = $this->getBuilder();
- $builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into "users" ("email", "bar") values (?, bar)', ['foo'], 'id')->andReturn(1);
- $result = $builder->from('users')->insertGetId(['email' => 'foo', 'bar' => new Raw('bar')], 'id');
- $this->assertEquals(1, $result);
- }
- public function testInsertGetIdWithEmptyValues()
- {
- $builder = $this->getMySqlBuilder();
- $builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into `users` () values ()', [], null);
- $builder->from('users')->insertGetId([]);
- $builder = $this->getPostgresBuilder();
- $builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into "users" default values returning "id"', [], null);
- $builder->from('users')->insertGetId([]);
- $builder = $this->getSQLiteBuilder();
- $builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into "users" default values', [], null);
- $builder->from('users')->insertGetId([]);
- $builder = $this->getSqlServerBuilder();
- $builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into [users] default values', [], null);
- $builder->from('users')->insertGetId([]);
- }
- public function testInsertMethodRespectsRawBindings()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('insert')->once()->with('insert into "users" ("email") values (CURRENT TIMESTAMP)', [])->andReturn(true);
- $result = $builder->from('users')->insert(['email' => new Raw('CURRENT TIMESTAMP')]);
- $this->assertTrue($result);
- }
- public function testMultipleInsertsWithExpressionValues()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('insert')->once()->with('insert into "users" ("email") values (UPPER(\'Foo\')), (LOWER(\'Foo\'))', [])->andReturn(true);
- $result = $builder->from('users')->insert([['email' => new Raw("UPPER('Foo')")], ['email' => new Raw("LOWER('Foo')")]]);
- $this->assertTrue($result);
- }
- public function testUpdateMethod()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "id" = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->where('id', '=', 1)->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update `users` set `email` = ?, `name` = ? where `id` = ? order by `foo` desc limit 5', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->where('id', '=', 1)->orderBy('foo', 'desc')->limit(5)->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpsertMethod()
- {
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into `users` (`email`, `name`) values (?, ?), (?, ?) on duplicate key update `email` = values(`email`), `name` = values(`name`)', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
- $result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email');
- $this->assertEquals(2, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into "users" ("email", "name") values (?, ?), (?, ?) on conflict ("email") do update set "email" = "excluded"."email", "name" = "excluded"."name"', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
- $result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email');
- $this->assertEquals(2, $result);
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into "users" ("email", "name") values (?, ?), (?, ?) on conflict ("email") do update set "email" = "excluded"."email", "name" = "excluded"."name"', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
- $result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email');
- $this->assertEquals(2, $result);
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('merge [users] using (values (?, ?), (?, ?)) [laravel_source] ([email], [name]) on [laravel_source].[email] = [users].[email] when matched then update set [email] = [laravel_source].[email], [name] = [laravel_source].[name] when not matched then insert ([email], [name]) values ([email], [name]);', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
- $result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email');
- $this->assertEquals(2, $result);
- }
- public function testUpsertMethodWithUpdateColumns()
- {
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into `users` (`email`, `name`) values (?, ?), (?, ?) on duplicate key update `name` = values(`name`)', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
- $result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email', ['name']);
- $this->assertEquals(2, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into "users" ("email", "name") values (?, ?), (?, ?) on conflict ("email") do update set "name" = "excluded"."name"', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
- $result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email', ['name']);
- $this->assertEquals(2, $result);
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into "users" ("email", "name") values (?, ?), (?, ?) on conflict ("email") do update set "name" = "excluded"."name"', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
- $result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email', ['name']);
- $this->assertEquals(2, $result);
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('merge [users] using (values (?, ?), (?, ?)) [laravel_source] ([email], [name]) on [laravel_source].[email] = [users].[email] when matched then update set [name] = [laravel_source].[name] when not matched then insert ([email], [name]) values ([email], [name]);', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
- $result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email', ['name']);
- $this->assertEquals(2, $result);
- }
- public function testUpdateMethodWithJoins()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" inner join "orders" on "users"."id" = "orders"."user_id" set "email" = ?, "name" = ? where "users"."id" = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', 'users.id', '=', 'orders.user_id')->where('users.id', '=', 1)->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" inner join "orders" on "users"."id" = "orders"."user_id" and "users"."id" = ? set "email" = ?, "name" = ?', [1, 'foo', 'bar'])->andReturn(1);
- $result = $builder->from('users')->join('orders', function ($join) {
- $join->on('users.id', '=', 'orders.user_id')
- ->where('users.id', '=', 1);
- })->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpdateMethodWithJoinsOnSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update [users] set [email] = ?, [name] = ? from [users] inner join [orders] on [users].[id] = [orders].[user_id] where [users].[id] = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', 'users.id', '=', 'orders.user_id')->where('users.id', '=', 1)->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update [users] set [email] = ?, [name] = ? from [users] inner join [orders] on [users].[id] = [orders].[user_id] and [users].[id] = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', function ($join) {
- $join->on('users.id', '=', 'orders.user_id')
- ->where('users.id', '=', 1);
- })->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpdateMethodWithJoinsOnMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update `users` inner join `orders` on `users`.`id` = `orders`.`user_id` set `email` = ?, `name` = ? where `users`.`id` = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', 'users.id', '=', 'orders.user_id')->where('users.id', '=', 1)->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update `users` inner join `orders` on `users`.`id` = `orders`.`user_id` and `users`.`id` = ? set `email` = ?, `name` = ?', [1, 'foo', 'bar'])->andReturn(1);
- $result = $builder->from('users')->join('orders', function ($join) {
- $join->on('users.id', '=', 'orders.user_id')
- ->where('users.id', '=', 1);
- })->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpdateMethodWithJoinsOnSQLite()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "rowid" in (select "users"."rowid" from "users" where "users"."id" > ? order by "id" asc limit 3)', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->where('users.id', '>', 1)->limit(3)->oldest('id')->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "rowid" in (select "users"."rowid" from "users" inner join "orders" on "users"."id" = "orders"."user_id" where "users"."id" = ?)', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', 'users.id', '=', 'orders.user_id')->where('users.id', '=', 1)->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "rowid" in (select "users"."rowid" from "users" inner join "orders" on "users"."id" = "orders"."user_id" and "users"."id" = ?)', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', function ($join) {
- $join->on('users.id', '=', 'orders.user_id')
- ->where('users.id', '=', 1);
- })->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" as "u" set "email" = ?, "name" = ? where "rowid" in (select "u"."rowid" from "users" as "u" inner join "orders" as "o" on "u"."id" = "o"."user_id")', ['foo', 'bar'])->andReturn(1);
- $result = $builder->from('users as u')->join('orders as o', 'u.id', '=', 'o.user_id')->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpdateMethodWithJoinsAndAliasesOnSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update [u] set [email] = ?, [name] = ? from [users] as [u] inner join [orders] on [u].[id] = [orders].[user_id] where [u].[id] = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users as u')->join('orders', 'u.id', '=', 'orders.user_id')->where('u.id', '=', 1)->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpdateMethodWithoutJoinsOnPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "id" = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->where('id', '=', 1)->update(['users.email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "id" = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->where('id', '=', 1)->selectRaw('?', ['ignore'])->update(['users.email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users"."users" set "email" = ?, "name" = ? where "id" = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users.users')->where('id', '=', 1)->selectRaw('?', ['ignore'])->update(['users.users.email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpdateMethodWithJoinsOnPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "ctid" in (select "users"."ctid" from "users" inner join "orders" on "users"."id" = "orders"."user_id" where "users"."id" = ?)', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', 'users.id', '=', 'orders.user_id')->where('users.id', '=', 1)->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "ctid" in (select "users"."ctid" from "users" inner join "orders" on "users"."id" = "orders"."user_id" and "users"."id" = ?)', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', function ($join) {
- $join->on('users.id', '=', 'orders.user_id')
- ->where('users.id', '=', 1);
- })->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? where "ctid" in (select "users"."ctid" from "users" inner join "orders" on "users"."id" = "orders"."user_id" and "users"."id" = ? where "name" = ?)', ['foo', 'bar', 1, 'baz'])->andReturn(1);
- $result = $builder->from('users')
- ->join('orders', function ($join) {
- $join->on('users.id', '=', 'orders.user_id')
- ->where('users.id', '=', 1);
- })->where('name', 'baz')
- ->update(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpdateFromMethodWithJoinsOnPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? from "orders" where "users"."id" = ? and "users"."id" = "orders"."user_id"', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', 'users.id', '=', 'orders.user_id')->where('users.id', '=', 1)->updateFrom(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? from "orders" where "users"."id" = "orders"."user_id" and "users"."id" = ?', ['foo', 'bar', 1])->andReturn(1);
- $result = $builder->from('users')->join('orders', function ($join) {
- $join->on('users.id', '=', 'orders.user_id')
- ->where('users.id', '=', 1);
- })->updateFrom(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ?, "name" = ? from "orders" where "name" = ? and "users"."id" = "orders"."user_id" and "users"."id" = ?', ['foo', 'bar', 'baz', 1])->andReturn(1);
- $result = $builder->from('users')
- ->join('orders', function ($join) {
- $join->on('users.id', '=', 'orders.user_id')
- ->where('users.id', '=', 1);
- })->where('name', 'baz')
- ->updateFrom(['email' => 'foo', 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpdateMethodRespectsRaw()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = foo, "name" = ? where "id" = ?', ['bar', 1])->andReturn(1);
- $result = $builder->from('users')->where('id', '=', 1)->update(['email' => new Raw('foo'), 'name' => 'bar']);
- $this->assertEquals(1, $result);
- }
- public function testUpdateOrInsertMethod()
- {
- $builder = m::mock(Builder::class.'[where,exists,insert]', [
- m::mock(ConnectionInterface::class),
- new Grammar,
- m::mock(Processor::class),
- ]);
- $builder->shouldReceive('where')->once()->with(['email' => 'foo'])->andReturn(m::self());
- $builder->shouldReceive('exists')->once()->andReturn(false);
- $builder->shouldReceive('insert')->once()->with(['email' => 'foo', 'name' => 'bar'])->andReturn(true);
- $this->assertTrue($builder->updateOrInsert(['email' => 'foo'], ['name' => 'bar']));
- $builder = m::mock(Builder::class.'[where,exists,update]', [
- m::mock(ConnectionInterface::class),
- new Grammar,
- m::mock(Processor::class),
- ]);
- $builder->shouldReceive('where')->once()->with(['email' => 'foo'])->andReturn(m::self());
- $builder->shouldReceive('exists')->once()->andReturn(true);
- $builder->shouldReceive('take')->andReturnSelf();
- $builder->shouldReceive('update')->once()->with(['name' => 'bar'])->andReturn(1);
- $this->assertTrue($builder->updateOrInsert(['email' => 'foo'], ['name' => 'bar']));
- }
- public function testUpdateOrInsertMethodWorksWithEmptyUpdateValues()
- {
- $builder = m::spy(Builder::class.'[where,exists,update]', [
- m::mock(ConnectionInterface::class),
- new Grammar,
- m::mock(Processor::class),
- ]);
- $builder->shouldReceive('where')->once()->with(['email' => 'foo'])->andReturn(m::self());
- $builder->shouldReceive('exists')->once()->andReturn(true);
- $this->assertTrue($builder->updateOrInsert(['email' => 'foo']));
- $builder->shouldNotHaveReceived('update');
- }
- public function testDeleteMethod()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "email" = ?', ['foo'])->andReturn(1);
- $result = $builder->from('users')->where('email', '=', 'foo')->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "users"."id" = ?', [1])->andReturn(1);
- $result = $builder->from('users')->delete(1);
- $this->assertEquals(1, $result);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "users"."id" = ?', [1])->andReturn(1);
- $result = $builder->from('users')->selectRaw('?', ['ignore'])->delete(1);
- $this->assertEquals(1, $result);
- $builder = $this->getSqliteBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "rowid" in (select "users"."rowid" from "users" where "email" = ? order by "id" asc limit 1)', ['foo'])->andReturn(1);
- $result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from `users` where `email` = ? order by `id` asc limit 1', ['foo'])->andReturn(1);
- $result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from [users] where [email] = ?', ['foo'])->andReturn(1);
- $result = $builder->from('users')->where('email', '=', 'foo')->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete top (1) from [users] where [email] = ?', ['foo'])->andReturn(1);
- $result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();
- $this->assertEquals(1, $result);
- }
- public function testDeleteWithJoinMethod()
- {
- $builder = $this->getSqliteBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "rowid" in (select "users"."rowid" from "users" inner join "contacts" on "users"."id" = "contacts"."id" where "users"."email" = ? order by "users"."id" asc limit 1)', ['foo'])->andReturn(1);
- $result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->where('users.email', '=', 'foo')->orderBy('users.id')->limit(1)->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getSqliteBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" as "u" where "rowid" in (select "u"."rowid" from "users" as "u" inner join "contacts" as "c" on "u"."id" = "c"."id")', [])->andReturn(1);
- $result = $builder->from('users as u')->join('contacts as c', 'u.id', '=', 'c.id')->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete `users` from `users` inner join `contacts` on `users`.`id` = `contacts`.`id` where `email` = ?', ['foo'])->andReturn(1);
- $result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete `a` from `users` as `a` inner join `users` as `b` on `a`.`id` = `b`.`user_id` where `email` = ?', ['foo'])->andReturn(1);
- $result = $builder->from('users AS a')->join('users AS b', 'a.id', '=', 'b.user_id')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete `users` from `users` inner join `contacts` on `users`.`id` = `contacts`.`id` where `users`.`id` = ?', [1])->andReturn(1);
- $result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->orderBy('id')->take(1)->delete(1);
- $this->assertEquals(1, $result);
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete [users] from [users] inner join [contacts] on [users].[id] = [contacts].[id] where [email] = ?', ['foo'])->andReturn(1);
- $result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->where('email', '=', 'foo')->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete [a] from [users] as [a] inner join [users] as [b] on [a].[id] = [b].[user_id] where [email] = ?', ['foo'])->andReturn(1);
- $result = $builder->from('users AS a')->join('users AS b', 'a.id', '=', 'b.user_id')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getSqlServerBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete [users] from [users] inner join [contacts] on [users].[id] = [contacts].[id] where [users].[id] = ?', [1])->andReturn(1);
- $result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->delete(1);
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "ctid" in (select "users"."ctid" from "users" inner join "contacts" on "users"."id" = "contacts"."id" where "users"."email" = ?)', ['foo'])->andReturn(1);
- $result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->where('users.email', '=', 'foo')->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" as "a" where "ctid" in (select "a"."ctid" from "users" as "a" inner join "users" as "b" on "a"."id" = "b"."user_id" where "email" = ? order by "id" asc limit 1)', ['foo'])->andReturn(1);
- $result = $builder->from('users AS a')->join('users AS b', 'a.id', '=', 'b.user_id')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "ctid" in (select "users"."ctid" from "users" inner join "contacts" on "users"."id" = "contacts"."id" where "users"."id" = ? order by "id" asc limit 1)', [1])->andReturn(1);
- $result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->orderBy('id')->take(1)->delete(1);
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "ctid" in (select "users"."ctid" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id" and "users"."id" = ? where "name" = ?)', [1, 'baz'])->andReturn(1);
- $result = $builder->from('users')
- ->join('contacts', function ($join) {
- $join->on('users.id', '=', 'contacts.user_id')
- ->where('users.id', '=', 1);
- })->where('name', 'baz')
- ->delete();
- $this->assertEquals(1, $result);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "ctid" in (select "users"."ctid" from "users" inner join "contacts" on "users"."id" = "contacts"."id")', [])->andReturn(1);
- $result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->delete();
- $this->assertEquals(1, $result);
- }
- public function testTruncateMethod()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('statement')->once()->with('truncate table "users"', []);
- $builder->from('users')->truncate();
- $sqlite = new SQLiteGrammar;
- $builder = $this->getBuilder();
- $builder->from('users');
- $this->assertEquals([
- 'delete from sqlite_sequence where name = ?' => ['users'],
- 'delete from "users"' => [],
- ], $sqlite->compileTruncate($builder));
- }
- public function testPreserveAddsClosureToArray()
- {
- $builder = $this->getBuilder();
- $builder->beforeQuery(function () {
- });
- $this->assertCount(1, $builder->beforeQueryCallbacks);
- $this->assertInstanceOf(Closure::class, $builder->beforeQueryCallbacks[0]);
- }
- public function testApplyPreserveCleansArray()
- {
- $builder = $this->getBuilder();
- $builder->beforeQuery(function () {
- });
- $this->assertCount(1, $builder->beforeQueryCallbacks);
- $builder->applyBeforeQueryCallbacks();
- $this->assertCount(0, $builder->beforeQueryCallbacks);
- }
- public function testPreservedAreAppliedByToSql()
- {
- $builder = $this->getBuilder();
- $builder->beforeQuery(function ($builder) {
- $builder->where('foo', 'bar');
- });
- $this->assertSame('select * where "foo" = ?', $builder->toSql());
- $this->assertEquals(['bar'], $builder->getBindings());
- }
- public function testPreservedAreAppliedByInsert()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('insert')->once()->with('insert into "users" ("email") values (?)', ['foo']);
- $builder->beforeQuery(function ($builder) {
- $builder->from('users');
- });
- $builder->insert(['email' => 'foo']);
- }
- public function testPreservedAreAppliedByInsertGetId()
- {
- $this->called = false;
- $builder = $this->getBuilder();
- $builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into "users" ("email") values (?)', ['foo'], 'id');
- $builder->beforeQuery(function ($builder) {
- $builder->from('users');
- });
- $builder->insertGetId(['email' => 'foo'], 'id');
- }
- public function testPreservedAreAppliedByInsertUsing()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into "users" () select *', []);
- $builder->beforeQuery(function ($builder) {
- $builder->from('users');
- });
- $builder->insertUsing([], $this->getBuilder());
- }
- public function testPreservedAreAppliedByUpsert()
- {
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into `users` (`email`) values (?) on duplicate key update `email` = values(`email`)', ['foo']);
- $builder->beforeQuery(function ($builder) {
- $builder->from('users');
- });
- $builder->upsert(['email' => 'foo'], 'id');
- }
- public function testPreservedAreAppliedByUpdate()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "email" = ? where "id" = ?', ['foo', 1]);
- $builder->from('users')->beforeQuery(function ($builder) {
- $builder->where('id', 1);
- });
- $builder->update(['email' => 'foo']);
- }
- public function testPreservedAreAppliedByDelete()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users"', []);
- $builder->beforeQuery(function ($builder) {
- $builder->from('users');
- });
- $builder->delete();
- }
- public function testPreservedAreAppliedByTruncate()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('statement')->once()->with('truncate table "users"', []);
- $builder->beforeQuery(function ($builder) {
- $builder->from('users');
- });
- $builder->truncate();
- }
- public function testPreservedAreAppliedByExists()
- {
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select')->once()->with('select exists(select * from "users") as "exists"', [], true);
- $builder->beforeQuery(function ($builder) {
- $builder->from('users');
- });
- $builder->exists();
- }
- public function testPostgresInsertGetId()
- {
- $builder = $this->getPostgresBuilder();
- $builder->getProcessor()->shouldReceive('processInsertGetId')->once()->with($builder, 'insert into "users" ("email") values (?) returning "id"', ['foo'], 'id')->andReturn(1);
- $result = $builder->from('users')->insertGetId(['email' => 'foo'], 'id');
- $this->assertEquals(1, $result);
- }
- public function testMySqlWrapping()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users');
- $this->assertSame('select * from `users`', $builder->toSql());
- }
- public function testMySqlUpdateWrappingJson()
- {
- $grammar = new MySqlGrammar;
- $processor = m::mock(Processor::class);
- $connection = $this->createMock(ConnectionInterface::class);
- $connection->expects($this->once())
- ->method('update')
- ->with(
- 'update `users` set `name` = json_set(`name`, \'$."first_name"\', ?), `name` = json_set(`name`, \'$."last_name"\', ?) where `active` = ?',
- ['John', 'Doe', 1]
- );
- $builder = new Builder($connection, $grammar, $processor);
- $builder->from('users')->where('active', '=', 1)->update(['name->first_name' => 'John', 'name->last_name' => 'Doe']);
- }
- public function testMySqlUpdateWrappingNestedJson()
- {
- $grammar = new MySqlGrammar;
- $processor = m::mock(Processor::class);
- $connection = $this->createMock(ConnectionInterface::class);
- $connection->expects($this->once())
- ->method('update')
- ->with(
- 'update `users` set `meta` = json_set(`meta`, \'$."name"."first_name"\', ?), `meta` = json_set(`meta`, \'$."name"."last_name"\', ?) where `active` = ?',
- ['John', 'Doe', 1]
- );
- $builder = new Builder($connection, $grammar, $processor);
- $builder->from('users')->where('active', '=', 1)->update(['meta->name->first_name' => 'John', 'meta->name->last_name' => 'Doe']);
- }
- public function testMySqlUpdateWrappingJsonArray()
- {
- $grammar = new MySqlGrammar;
- $processor = m::mock(Processor::class);
- $connection = $this->createMock(ConnectionInterface::class);
- $connection->expects($this->once())
- ->method('update')
- ->with(
- 'update `users` set `options` = ?, `meta` = json_set(`meta`, \'$."tags"\', cast(? as json)), `group_id` = 45, `created_at` = ? where `active` = ?',
- [
- json_encode(['2fa' => false, 'presets' => ['laravel', 'vue']]),
- json_encode(['white', 'large']),
- new DateTime('2019-08-06'),
- 1,
- ]
- );
- $builder = new Builder($connection, $grammar, $processor);
- $builder->from('users')->where('active', 1)->update([
- 'options' => ['2fa' => false, 'presets' => ['laravel', 'vue']],
- 'meta->tags' => ['white', 'large'],
- 'group_id' => new Raw('45'),
- 'created_at' => new DateTime('2019-08-06'),
- ]);
- }
- public function testMySqlUpdateWithJsonPreparesBindingsCorrectly()
- {
- $grammar = new MySqlGrammar;
- $processor = m::mock(Processor::class);
- $connection = m::mock(ConnectionInterface::class);
- $connection->shouldReceive('update')
- ->once()
- ->with(
- 'update `users` set `options` = json_set(`options`, \'$."enable"\', false), `updated_at` = ? where `id` = ?',
- ['2015-05-26 22:02:06', 0]
- );
- $builder = new Builder($connection, $grammar, $processor);
- $builder->from('users')->where('id', '=', 0)->update(['options->enable' => false, 'updated_at' => '2015-05-26 22:02:06']);
- $connection->shouldReceive('update')
- ->once()
- ->with(
- 'update `users` set `options` = json_set(`options`, \'$."size"\', ?), `updated_at` = ? where `id` = ?',
- [45, '2015-05-26 22:02:06', 0]
- );
- $builder = new Builder($connection, $grammar, $processor);
- $builder->from('users')->where('id', '=', 0)->update(['options->size' => 45, 'updated_at' => '2015-05-26 22:02:06']);
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update `users` set `options` = json_set(`options`, \'$."size"\', ?)', [null]);
- $builder->from('users')->update(['options->size' => null]);
- $builder = $this->getMySqlBuilder();
- $builder->getConnection()->shouldReceive('update')->once()->with('update `users` set `options` = json_set(`options`, \'$."size"\', 45)', []);
- $builder->from('users')->update(['options->size' => new Raw('45')]);
- }
- public function testPostgresUpdateWrappingJson()
- {
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')
- ->with('update "users" set "options" = jsonb_set("options"::jsonb, \'{"name","first_name"}\', ?)', ['"John"']);
- $builder->from('users')->update(['users.options->name->first_name' => 'John']);
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')
- ->with('update "users" set "options" = jsonb_set("options"::jsonb, \'{"language"}\', \'null\')', []);
- $builder->from('users')->update(['options->language' => new Raw("'null'")]);
- }
- public function testPostgresUpdateWrappingJsonArray()
- {
- $builder = $this->getPostgresBuilder();
- $builder->getConnection()->shouldReceive('update')
- ->with('update "users" set "options" = ?, "meta" = jsonb_set("meta"::jsonb, \'{"tags"}\', ?), "group_id" = 45, "created_at" = ?', [
- json_encode(['2fa' => false, 'presets' => ['laravel', 'vue']]),
- json_encode(['white', 'large']),
- new DateTime('2019-08-06'),
- ]);
- $builder->from('users')->update([
- 'options' => ['2fa' => false, 'presets' => ['laravel', 'vue']],
- 'meta->tags' => ['white', 'large'],
- 'group_id' => new Raw('45'),
- 'created_at' => new DateTime('2019-08-06'),
- ]);
- }
- public function testSQLiteUpdateWrappingJsonArray()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('update')
- ->with('update "users" set "options" = ?, "group_id" = 45, "created_at" = ?', [
- json_encode(['2fa' => false, 'presets' => ['laravel', 'vue']]),
- new DateTime('2019-08-06'),
- ]);
- $builder->from('users')->update([
- 'options' => ['2fa' => false, 'presets' => ['laravel', 'vue']],
- 'group_id' => new Raw('45'),
- 'created_at' => new DateTime('2019-08-06'),
- ]);
- }
- public function testSQLiteUpdateWrappingNestedJsonArray()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->getConnection()->shouldReceive('update')
- ->with('update "users" set "group_id" = 45, "created_at" = ?, "options" = json_patch(ifnull("options", json(\'{}\')), json(?))', [
- new DateTime('2019-08-06'),
- json_encode(['name' => 'Taylor', 'security' => ['2fa' => false, 'presets' => ['laravel', 'vue']], 'sharing' => ['twitter' => 'username']]),
- ]);
- $builder->from('users')->update([
- 'options->name' => 'Taylor',
- 'group_id' => new Raw('45'),
- 'options->security' => ['2fa' => false, 'presets' => ['laravel', 'vue']],
- 'options->sharing->twitter' => 'username',
- 'created_at' => new DateTime('2019-08-06'),
- ]);
- }
- public function testMySqlWrappingJsonWithString()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('items->sku', '=', 'foo-bar');
- $this->assertSame('select * from `users` where json_unquote(json_extract(`items`, \'$."sku"\')) = ?', $builder->toSql());
- $this->assertCount(1, $builder->getRawBindings()['where']);
- $this->assertSame('foo-bar', $builder->getRawBindings()['where'][0]);
- }
- public function testMySqlWrappingJsonWithInteger()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('items->price', '=', 1);
- $this->assertSame('select * from `users` where json_unquote(json_extract(`items`, \'$."price"\')) = ?', $builder->toSql());
- }
- public function testMySqlWrappingJsonWithDouble()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('items->price', '=', 1.5);
- $this->assertSame('select * from `users` where json_unquote(json_extract(`items`, \'$."price"\')) = ?', $builder->toSql());
- }
- public function testMySqlWrappingJsonWithBoolean()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('items->available', '=', true);
- $this->assertSame('select * from `users` where json_extract(`items`, \'$."available"\') = true', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where(new Raw("items->'$.available'"), '=', true);
- $this->assertSame("select * from `users` where items->'$.available' = true", $builder->toSql());
- }
- public function testMySqlWrappingJsonWithBooleanAndIntegerThatLooksLikeOne()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('items->available', '=', true)->where('items->active', '=', false)->where('items->number_available', '=', 0);
- $this->assertSame('select * from `users` where json_extract(`items`, \'$."available"\') = true and json_extract(`items`, \'$."active"\') = false and json_unquote(json_extract(`items`, \'$."number_available"\')) = ?', $builder->toSql());
- }
- public function testJsonPathEscaping()
- {
- $expectedWithJsonEscaped = <<<'SQL'
- select json_unquote(json_extract(`json`, '$."''))#"'))
- SQL;
- $builder = $this->getMySqlBuilder();
- $builder->select("json->'))#");
- $this->assertEquals($expectedWithJsonEscaped, $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select("json->\'))#");
- $this->assertEquals($expectedWithJsonEscaped, $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select("json->\\'))#");
- $this->assertEquals($expectedWithJsonEscaped, $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select("json->\\\'))#");
- $this->assertEquals($expectedWithJsonEscaped, $builder->toSql());
- }
- public function testMySqlWrappingJson()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereRaw('items->\'$."price"\' = 1');
- $this->assertSame('select * from `users` where items->\'$."price"\' = 1', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('items->price')->from('users')->where('users.items->price', '=', 1)->orderBy('items->price');
- $this->assertSame('select json_unquote(json_extract(`items`, \'$."price"\')) from `users` where json_unquote(json_extract(`users`.`items`, \'$."price"\')) = ? order by json_unquote(json_extract(`items`, \'$."price"\')) asc', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('items->price->in_usd', '=', 1);
- $this->assertSame('select * from `users` where json_unquote(json_extract(`items`, \'$."price"."in_usd"\')) = ?', $builder->toSql());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('items->price->in_usd', '=', 1)->where('items->age', '=', 2);
- $this->assertSame('select * from `users` where json_unquote(json_extract(`items`, \'$."price"."in_usd"\')) = ? and json_unquote(json_extract(`items`, \'$."age"\')) = ?', $builder->toSql());
- }
- public function testPostgresWrappingJson()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('items->price')->from('users')->where('users.items->price', '=', 1)->orderBy('items->price');
- $this->assertSame('select "items"->>\'price\' from "users" where "users"."items"->>\'price\' = ? order by "items"->>\'price\' asc', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('items->price->in_usd', '=', 1);
- $this->assertSame('select * from "users" where "items"->\'price\'->>\'in_usd\' = ?', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('items->price->in_usd', '=', 1)->where('items->age', '=', 2);
- $this->assertSame('select * from "users" where "items"->\'price\'->>\'in_usd\' = ? and "items"->>\'age\' = ?', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('items->prices->0', '=', 1)->where('items->age', '=', 2);
- $this->assertSame('select * from "users" where "items"->\'prices\'->>0 = ? and "items"->>\'age\' = ?', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('items->available', '=', true);
- $this->assertSame('select * from "users" where ("items"->\'available\')::jsonb = \'true\'::jsonb', $builder->toSql());
- }
- public function testSqlServerWrappingJson()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('items->price')->from('users')->where('users.items->price', '=', 1)->orderBy('items->price');
- $this->assertSame('select json_value([items], \'$."price"\') from [users] where json_value([users].[items], \'$."price"\') = ? order by json_value([items], \'$."price"\') asc', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->where('items->price->in_usd', '=', 1);
- $this->assertSame('select * from [users] where json_value([items], \'$."price"."in_usd"\') = ?', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->where('items->price->in_usd', '=', 1)->where('items->age', '=', 2);
- $this->assertSame('select * from [users] where json_value([items], \'$."price"."in_usd"\') = ? and json_value([items], \'$."age"\') = ?', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->where('items->available', '=', true);
- $this->assertSame('select * from [users] where json_value([items], \'$."available"\') = \'true\'', $builder->toSql());
- }
- public function testSqliteWrappingJson()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->select('items->price')->from('users')->where('users.items->price', '=', 1)->orderBy('items->price');
- $this->assertSame('select json_extract("items", \'$."price"\') from "users" where json_extract("users"."items", \'$."price"\') = ? order by json_extract("items", \'$."price"\') asc', $builder->toSql());
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->where('items->price->in_usd', '=', 1);
- $this->assertSame('select * from "users" where json_extract("items", \'$."price"."in_usd"\') = ?', $builder->toSql());
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->where('items->price->in_usd', '=', 1)->where('items->age', '=', 2);
- $this->assertSame('select * from "users" where json_extract("items", \'$."price"."in_usd"\') = ? and json_extract("items", \'$."age"\') = ?', $builder->toSql());
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->where('items->available', '=', true);
- $this->assertSame('select * from "users" where json_extract("items", \'$."available"\') = true', $builder->toSql());
- }
- public function testSQLiteOrderBy()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->orderBy('email', 'desc');
- $this->assertSame('select * from "users" order by "email" desc', $builder->toSql());
- }
- public function testSqlServerLimitsAndOffsets()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->take(10);
- $this->assertSame('select top 10 * from [users]', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->skip(10);
- $this->assertSame('select * from (select *, row_number() over (order by (select 0)) as row_num from [users]) as temp_table where row_num >= 11 order by row_num', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->skip(10)->take(10);
- $this->assertSame('select * from (select *, row_number() over (order by (select 0)) as row_num from [users]) as temp_table where row_num between 11 and 20 order by row_num', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->skip(11)->take(10)->orderBy('email', 'desc');
- $this->assertSame('select * from [users] order by [email] desc offset 11 rows fetch next 10 rows only', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $subQueryBuilder = $this->getSqlServerBuilder();
- $subQuery = function ($query) {
- return $query->select('created_at')->from('logins')->where('users.name', 'nameBinding')->whereColumn('user_id', 'users.id')->limit(1);
- };
- $builder->select('*')->from('users')->where('email', 'emailBinding')->orderBy($subQuery)->skip(10)->take(10);
- $this->assertSame('select * from [users] where [email] = ? order by (select top 1 [created_at] from [logins] where [users].[name] = ? and [user_id] = [users].[id]) asc offset 10 rows fetch next 10 rows only', $builder->toSql());
- $this->assertEquals(['emailBinding', 'nameBinding'], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->take('foo');
- $this->assertSame('select * from [users]', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->take('foo')->offset('bar');
- $this->assertSame('select * from [users]', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->offset('bar');
- $this->assertSame('select * from [users]', $builder->toSql());
- }
- public function testMySqlSoundsLikeOperator()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('name', 'sounds like', 'John Doe');
- $this->assertSame('select * from `users` where `name` sounds like ?', $builder->toSql());
- $this->assertEquals(['John Doe'], $builder->getBindings());
- }
- public function testBitwiseOperators()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('bar', '&', 1);
- $this->assertSame('select * from "users" where "bar" & ?', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('bar', '#', 1);
- $this->assertSame('select * from "users" where ("bar" # ?)::bool', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('range', '>>', '[2022-01-08 00:00:00,2022-01-09 00:00:00)');
- $this->assertSame('select * from "users" where ("range" >> ?)::bool', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->where('bar', '&', 1);
- $this->assertSame('select * from [users] where ([bar] & ?) != 0', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->having('bar', '&', 1);
- $this->assertSame('select * from "users" having "bar" & ?', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->having('bar', '#', 1);
- $this->assertSame('select * from "users" having ("bar" # ?)::bool', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->having('range', '>>', '[2022-01-08 00:00:00,2022-01-09 00:00:00)');
- $this->assertSame('select * from "users" having ("range" >> ?)::bool', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->having('bar', '&', 1);
- $this->assertSame('select * from [users] having ([bar] & ?) != 0', $builder->toSql());
- }
- public function testMergeWheresCanMergeWheresAndBindings()
- {
- $builder = $this->getBuilder();
- $builder->wheres = ['foo'];
- $builder->mergeWheres(['wheres'], [12 => 'foo', 13 => 'bar']);
- $this->assertEquals(['foo', 'wheres'], $builder->wheres);
- $this->assertEquals(['foo', 'bar'], $builder->getBindings());
- }
- public function testProvidingNullWithOperatorsBuildsCorrectly()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('foo', null);
- $this->assertSame('select * from "users" where "foo" is null', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('foo', '=', null);
- $this->assertSame('select * from "users" where "foo" is null', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('foo', '!=', null);
- $this->assertSame('select * from "users" where "foo" is not null', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('foo', '<>', null);
- $this->assertSame('select * from "users" where "foo" is not null', $builder->toSql());
- }
- public function testDynamicWhere()
- {
- $method = 'whereFooBarAndBazOrQux';
- $parameters = ['corge', 'waldo', 'fred'];
- $builder = m::mock(Builder::class)->makePartial();
- $builder->shouldReceive('where')->with('foo_bar', '=', $parameters[0], 'and')->once()->andReturnSelf();
- $builder->shouldReceive('where')->with('baz', '=', $parameters[1], 'and')->once()->andReturnSelf();
- $builder->shouldReceive('where')->with('qux', '=', $parameters[2], 'or')->once()->andReturnSelf();
- $this->assertEquals($builder, $builder->dynamicWhere($method, $parameters));
- }
- public function testDynamicWhereIsNotGreedy()
- {
- $method = 'whereIosVersionAndAndroidVersionOrOrientation';
- $parameters = ['6.1', '4.2', 'Vertical'];
- $builder = m::mock(Builder::class)->makePartial();
- $builder->shouldReceive('where')->with('ios_version', '=', '6.1', 'and')->once()->andReturnSelf();
- $builder->shouldReceive('where')->with('android_version', '=', '4.2', 'and')->once()->andReturnSelf();
- $builder->shouldReceive('where')->with('orientation', '=', 'Vertical', 'or')->once()->andReturnSelf();
- $builder->dynamicWhere($method, $parameters);
- }
- public function testCallTriggersDynamicWhere()
- {
- $builder = $this->getBuilder();
- $this->assertEquals($builder, $builder->whereFooAndBar('baz', 'qux'));
- $this->assertCount(2, $builder->wheres);
- }
- public function testBuilderThrowsExpectedExceptionWithUndefinedMethod()
- {
- $this->expectException(BadMethodCallException::class);
- $builder = $this->getBuilder();
- $builder->getConnection()->shouldReceive('select');
- $builder->getProcessor()->shouldReceive('processSelect')->andReturn([]);
- $builder->noValidMethodHere();
- }
- public function testMySqlLock()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock();
- $this->assertSame('select * from `foo` where `bar` = ? for update', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock(false);
- $this->assertSame('select * from `foo` where `bar` = ? lock in share mode', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock('lock in share mode');
- $this->assertSame('select * from `foo` where `bar` = ? lock in share mode', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- }
- public function testPostgresLock()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock();
- $this->assertSame('select * from "foo" where "bar" = ? for update', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock(false);
- $this->assertSame('select * from "foo" where "bar" = ? for share', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock('for key share');
- $this->assertSame('select * from "foo" where "bar" = ? for key share', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- }
- public function testSqlServerLock()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock();
- $this->assertSame('select * from [foo] with(rowlock,updlock,holdlock) where [bar] = ?', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock(false);
- $this->assertSame('select * from [foo] with(rowlock,holdlock) where [bar] = ?', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock('with(holdlock)');
- $this->assertSame('select * from [foo] with(holdlock) where [bar] = ?', $builder->toSql());
- $this->assertEquals(['baz'], $builder->getBindings());
- }
- public function testSelectWithLockUsesWritePdo()
- {
- $builder = $this->getMySqlBuilderWithProcessor();
- $builder->getConnection()->shouldReceive('select')->once()
- ->with(m::any(), m::any(), false);
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock()->get();
- $builder = $this->getMySqlBuilderWithProcessor();
- $builder->getConnection()->shouldReceive('select')->once()
- ->with(m::any(), m::any(), false);
- $builder->select('*')->from('foo')->where('bar', '=', 'baz')->lock(false)->get();
- }
- public function testBindingOrder()
- {
- $expectedSql = 'select * from "users" inner join "othertable" on "bar" = ? where "registered" = ? group by "city" having "population" > ? order by match ("foo") against(?)';
- $expectedBindings = ['foo', 1, 3, 'bar'];
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->join('othertable', function ($join) {
- $join->where('bar', '=', 'foo');
- })->where('registered', 1)->groupBy('city')->having('population', '>', 3)->orderByRaw('match ("foo") against(?)', ['bar']);
- $this->assertEquals($expectedSql, $builder->toSql());
- $this->assertEquals($expectedBindings, $builder->getBindings());
- // order of statements reversed
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->orderByRaw('match ("foo") against(?)', ['bar'])->having('population', '>', 3)->groupBy('city')->where('registered', 1)->join('othertable', function ($join) {
- $join->where('bar', '=', 'foo');
- });
- $this->assertEquals($expectedSql, $builder->toSql());
- $this->assertEquals($expectedBindings, $builder->getBindings());
- }
- public function testAddBindingWithArrayMergesBindings()
- {
- $builder = $this->getBuilder();
- $builder->addBinding(['foo', 'bar']);
- $builder->addBinding(['baz']);
- $this->assertEquals(['foo', 'bar', 'baz'], $builder->getBindings());
- }
- public function testAddBindingWithArrayMergesBindingsInCorrectOrder()
- {
- $builder = $this->getBuilder();
- $builder->addBinding(['bar', 'baz'], 'having');
- $builder->addBinding(['foo'], 'where');
- $this->assertEquals(['foo', 'bar', 'baz'], $builder->getBindings());
- }
- public function testMergeBuilders()
- {
- $builder = $this->getBuilder();
- $builder->addBinding(['foo', 'bar']);
- $otherBuilder = $this->getBuilder();
- $otherBuilder->addBinding(['baz']);
- $builder->mergeBindings($otherBuilder);
- $this->assertEquals(['foo', 'bar', 'baz'], $builder->getBindings());
- }
- public function testMergeBuildersBindingOrder()
- {
- $builder = $this->getBuilder();
- $builder->addBinding('foo', 'where');
- $builder->addBinding('baz', 'having');
- $otherBuilder = $this->getBuilder();
- $otherBuilder->addBinding('bar', 'where');
- $builder->mergeBindings($otherBuilder);
- $this->assertEquals(['foo', 'bar', 'baz'], $builder->getBindings());
- }
- public function testSubSelect()
- {
- $expectedSql = 'select "foo", "bar", (select "baz" from "two" where "subkey" = ?) as "sub" from "one" where "key" = ?';
- $expectedBindings = ['subval', 'val'];
- $builder = $this->getPostgresBuilder();
- $builder->from('one')->select(['foo', 'bar'])->where('key', '=', 'val');
- $builder->selectSub(function ($query) {
- $query->from('two')->select('baz')->where('subkey', '=', 'subval');
- }, 'sub');
- $this->assertEquals($expectedSql, $builder->toSql());
- $this->assertEquals($expectedBindings, $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->from('one')->select(['foo', 'bar'])->where('key', '=', 'val');
- $subBuilder = $this->getPostgresBuilder();
- $subBuilder->from('two')->select('baz')->where('subkey', '=', 'subval');
- $builder->selectSub($subBuilder, 'sub');
- $this->assertEquals($expectedSql, $builder->toSql());
- $this->assertEquals($expectedBindings, $builder->getBindings());
- $this->expectException(InvalidArgumentException::class);
- $builder = $this->getPostgresBuilder();
- $builder->selectSub(['foo'], 'sub');
- }
- public function testSubSelectResetBindings()
- {
- $builder = $this->getPostgresBuilder();
- $builder->from('one')->selectSub(function ($query) {
- $query->from('two')->select('baz')->where('subkey', '=', 'subval');
- }, 'sub');
- $this->assertSame('select (select "baz" from "two" where "subkey" = ?) as "sub" from "one"', $builder->toSql());
- $this->assertEquals(['subval'], $builder->getBindings());
- $builder->select('*');
- $this->assertSame('select * from "one"', $builder->toSql());
- $this->assertEquals([], $builder->getBindings());
- }
- public function testSqlServerWhereDate()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereDate('created_at', '=', '2015-09-23');
- $this->assertSame('select * from [users] where cast([created_at] as date) = ?', $builder->toSql());
- $this->assertEquals([0 => '2015-09-23'], $builder->getBindings());
- }
- public function testUppercaseLeadingBooleansAreRemoved()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('name', '=', 'Taylor', 'AND');
- $this->assertSame('select * from "users" where "name" = ?', $builder->toSql());
- }
- public function testLowercaseLeadingBooleansAreRemoved()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('name', '=', 'Taylor', 'and');
- $this->assertSame('select * from "users" where "name" = ?', $builder->toSql());
- }
- public function testCaseInsensitiveLeadingBooleansAreRemoved()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('name', '=', 'Taylor', 'And');
- $this->assertSame('select * from "users" where "name" = ?', $builder->toSql());
- }
- public function testTableValuedFunctionAsTableInSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users()');
- $this->assertSame('select * from [users]()', $builder->toSql());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users(1,2)');
- $this->assertSame('select * from [users](1,2)', $builder->toSql());
- }
- public function testChunkWithLastChunkComplete()
- {
- $builder = $this->getMockQueryBuilder();
- $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];
- $chunk1 = collect(['foo1', 'foo2']);
- $chunk2 = collect(['foo3', 'foo4']);
- $chunk3 = collect([]);
- $builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
- $builder->shouldReceive('forPage')->once()->with(2, 2)->andReturnSelf();
- $builder->shouldReceive('forPage')->once()->with(3, 2)->andReturnSelf();
- $builder->shouldReceive('get')->times(3)->andReturn($chunk1, $chunk2, $chunk3);
- $callbackAssertor = m::mock(stdClass::class);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);
- $callbackAssertor->shouldReceive('doSomething')->never()->with($chunk3);
- $builder->chunk(2, function ($results) use ($callbackAssertor) {
- $callbackAssertor->doSomething($results);
- });
- }
- public function testChunkWithLastChunkPartial()
- {
- $builder = $this->getMockQueryBuilder();
- $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];
- $chunk1 = collect(['foo1', 'foo2']);
- $chunk2 = collect(['foo3']);
- $builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
- $builder->shouldReceive('forPage')->once()->with(2, 2)->andReturnSelf();
- $builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);
- $callbackAssertor = m::mock(stdClass::class);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);
- $builder->chunk(2, function ($results) use ($callbackAssertor) {
- $callbackAssertor->doSomething($results);
- });
- }
- public function testChunkCanBeStoppedByReturningFalse()
- {
- $builder = $this->getMockQueryBuilder();
- $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];
- $chunk1 = collect(['foo1', 'foo2']);
- $chunk2 = collect(['foo3']);
- $builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
- $builder->shouldReceive('forPage')->never()->with(2, 2);
- $builder->shouldReceive('get')->times(1)->andReturn($chunk1);
- $callbackAssertor = m::mock(stdClass::class);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
- $callbackAssertor->shouldReceive('doSomething')->never()->with($chunk2);
- $builder->chunk(2, function ($results) use ($callbackAssertor) {
- $callbackAssertor->doSomething($results);
- return false;
- });
- }
- public function testChunkWithCountZero()
- {
- $builder = $this->getMockQueryBuilder();
- $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];
- $chunk = collect([]);
- $builder->shouldReceive('forPage')->once()->with(1, 0)->andReturnSelf();
- $builder->shouldReceive('get')->times(1)->andReturn($chunk);
- $callbackAssertor = m::mock(stdClass::class);
- $callbackAssertor->shouldReceive('doSomething')->never();
- $builder->chunk(0, function ($results) use ($callbackAssertor) {
- $callbackAssertor->doSomething($results);
- });
- }
- public function testChunkPaginatesUsingIdWithLastChunkComplete()
- {
- $builder = $this->getMockQueryBuilder();
- $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];
- $chunk1 = collect([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
- $chunk2 = collect([(object) ['someIdField' => 10], (object) ['someIdField' => 11]]);
- $chunk3 = collect([]);
- $builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
- $builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf();
- $builder->shouldReceive('forPageAfterId')->once()->with(2, 11, 'someIdField')->andReturnSelf();
- $builder->shouldReceive('get')->times(3)->andReturn($chunk1, $chunk2, $chunk3);
- $callbackAssertor = m::mock(stdClass::class);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);
- $callbackAssertor->shouldReceive('doSomething')->never()->with($chunk3);
- $builder->chunkById(2, function ($results) use ($callbackAssertor) {
- $callbackAssertor->doSomething($results);
- }, 'someIdField');
- }
- public function testChunkPaginatesUsingIdWithLastChunkPartial()
- {
- $builder = $this->getMockQueryBuilder();
- $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];
- $chunk1 = collect([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
- $chunk2 = collect([(object) ['someIdField' => 10]]);
- $builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
- $builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf();
- $builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);
- $callbackAssertor = m::mock(stdClass::class);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);
- $builder->chunkById(2, function ($results) use ($callbackAssertor) {
- $callbackAssertor->doSomething($results);
- }, 'someIdField');
- }
- public function testChunkPaginatesUsingIdWithCountZero()
- {
- $builder = $this->getMockQueryBuilder();
- $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];
- $chunk = collect([]);
- $builder->shouldReceive('forPageAfterId')->once()->with(0, 0, 'someIdField')->andReturnSelf();
- $builder->shouldReceive('get')->times(1)->andReturn($chunk);
- $callbackAssertor = m::mock(stdClass::class);
- $callbackAssertor->shouldReceive('doSomething')->never();
- $builder->chunkById(0, function ($results) use ($callbackAssertor) {
- $callbackAssertor->doSomething($results);
- }, 'someIdField');
- }
- public function testChunkPaginatesUsingIdWithAlias()
- {
- $builder = $this->getMockQueryBuilder();
- $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc'];
- $chunk1 = collect([(object) ['table_id' => 1], (object) ['table_id' => 10]]);
- $chunk2 = collect([]);
- $builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'table.id')->andReturnSelf();
- $builder->shouldReceive('forPageAfterId')->once()->with(2, 10, 'table.id')->andReturnSelf();
- $builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);
- $callbackAssertor = m::mock(stdClass::class);
- $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
- $callbackAssertor->shouldReceive('doSomething')->never()->with($chunk2);
- $builder->chunkById(2, function ($results) use ($callbackAssertor) {
- $callbackAssertor->doSomething($results);
- }, 'table.id', 'table_id');
- }
- public function testPaginate()
- {
- $perPage = 16;
- $columns = ['test'];
- $pageName = 'page-name';
- $page = 1;
- $builder = $this->getMockQueryBuilder();
- $path = 'http://foo.bar?page=3';
- $results = collect([['test' => 'foo'], ['test' => 'bar']]);
- $builder->shouldReceive('getCountForPagination')->once()->andReturn(2);
- $builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
- $builder->shouldReceive('get')->once()->andReturn($results);
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->paginate($perPage, $columns, $pageName, $page);
- $this->assertEquals(new LengthAwarePaginator($results, 2, $perPage, $page, [
- 'path' => $path,
- 'pageName' => $pageName,
- ]), $result);
- }
- public function testPaginateWithDefaultArguments()
- {
- $perPage = 15;
- $pageName = 'page';
- $page = 1;
- $builder = $this->getMockQueryBuilder();
- $path = 'http://foo.bar?page=3';
- $results = collect([['test' => 'foo'], ['test' => 'bar']]);
- $builder->shouldReceive('getCountForPagination')->once()->andReturn(2);
- $builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
- $builder->shouldReceive('get')->once()->andReturn($results);
- Paginator::currentPageResolver(function () {
- return 1;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->paginate();
- $this->assertEquals(new LengthAwarePaginator($results, 2, $perPage, $page, [
- 'path' => $path,
- 'pageName' => $pageName,
- ]), $result);
- }
- public function testPaginateWhenNoResults()
- {
- $perPage = 15;
- $pageName = 'page';
- $page = 1;
- $builder = $this->getMockQueryBuilder();
- $path = 'http://foo.bar?page=3';
- $results = [];
- $builder->shouldReceive('getCountForPagination')->once()->andReturn(0);
- $builder->shouldNotReceive('forPage');
- $builder->shouldNotReceive('get');
- Paginator::currentPageResolver(function () {
- return 1;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->paginate();
- $this->assertEquals(new LengthAwarePaginator($results, 0, $perPage, $page, [
- 'path' => $path,
- 'pageName' => $pageName,
- ]), $result);
- }
- public function testPaginateWithSpecificColumns()
- {
- $perPage = 16;
- $columns = ['id', 'name'];
- $pageName = 'page-name';
- $page = 1;
- $builder = $this->getMockQueryBuilder();
- $path = 'http://foo.bar?page=3';
- $results = collect([['id' => 3, 'name' => 'Taylor'], ['id' => 5, 'name' => 'Mohamed']]);
- $builder->shouldReceive('getCountForPagination')->once()->andReturn(2);
- $builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
- $builder->shouldReceive('get')->once()->andReturn($results);
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->paginate($perPage, $columns, $pageName, $page);
- $this->assertEquals(new LengthAwarePaginator($results, 2, $perPage, $page, [
- 'path' => $path,
- 'pageName' => $pageName,
- ]), $result);
- }
- public function testCursorPaginate()
- {
- $perPage = 16;
- $columns = ['test'];
- $cursorName = 'cursor-name';
- $cursor = new Cursor(['test' => 'bar']);
- $builder = $this->getMockQueryBuilder();
- $builder->from('foobar')->orderBy('test');
- $builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
- return new Builder($builder->connection, $builder->grammar, $builder->processor);
- });
- $path = 'http://foo.bar?cursor='.$cursor->encode();
- $results = collect([['test' => 'foo'], ['test' => 'bar']]);
- $builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results) {
- $this->assertEquals(
- 'select * from "foobar" where ("test" > ?) order by "test" asc limit 17',
- $builder->toSql());
- $this->assertEquals(['bar'], $builder->bindings['where']);
- return $results;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate($perPage, $columns, $cursorName, $cursor);
- $this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['test'],
- ]), $result);
- }
- public function testCursorPaginateMultipleOrderColumns()
- {
- $perPage = 16;
- $columns = ['test', 'another'];
- $cursorName = 'cursor-name';
- $cursor = new Cursor(['test' => 'bar', 'another' => 'foo']);
- $builder = $this->getMockQueryBuilder();
- $builder->from('foobar')->orderBy('test')->orderBy('another');
- $builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
- return new Builder($builder->connection, $builder->grammar, $builder->processor);
- });
- $path = 'http://foo.bar?cursor='.$cursor->encode();
- $results = collect([['test' => 'foo', 'another' => 1], ['test' => 'bar', 'another' => 2]]);
- $builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results) {
- $this->assertEquals(
- 'select * from "foobar" where ("test" > ? or ("test" = ? and ("another" > ?))) order by "test" asc, "another" asc limit 17',
- $builder->toSql()
- );
- $this->assertEquals(['bar', 'bar', 'foo'], $builder->bindings['where']);
- return $results;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate($perPage, $columns, $cursorName, $cursor);
- $this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['test', 'another'],
- ]), $result);
- }
- public function testCursorPaginateWithDefaultArguments()
- {
- $perPage = 15;
- $cursorName = 'cursor';
- $cursor = new Cursor(['test' => 'bar']);
- $builder = $this->getMockQueryBuilder();
- $builder->from('foobar')->orderBy('test');
- $builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
- return new Builder($builder->connection, $builder->grammar, $builder->processor);
- });
- $path = 'http://foo.bar?cursor='.$cursor->encode();
- $results = collect([['test' => 'foo'], ['test' => 'bar']]);
- $builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results) {
- $this->assertEquals(
- 'select * from "foobar" where ("test" > ?) order by "test" asc limit 16',
- $builder->toSql());
- $this->assertEquals(['bar'], $builder->bindings['where']);
- return $results;
- });
- CursorPaginator::currentCursorResolver(function () use ($cursor) {
- return $cursor;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate();
- $this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['test'],
- ]), $result);
- }
- public function testCursorPaginateWhenNoResults()
- {
- $perPage = 15;
- $cursorName = 'cursor';
- $builder = $this->getMockQueryBuilder()->orderBy('test');
- $path = 'http://foo.bar?cursor=3';
- $results = [];
- $builder->shouldReceive('get')->once()->andReturn($results);
- CursorPaginator::currentCursorResolver(function () {
- return null;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate();
- $this->assertEquals(new CursorPaginator($results, $perPage, null, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['test'],
- ]), $result);
- }
- public function testCursorPaginateWithSpecificColumns()
- {
- $perPage = 16;
- $columns = ['id', 'name'];
- $cursorName = 'cursor-name';
- $cursor = new Cursor(['id' => 2]);
- $builder = $this->getMockQueryBuilder();
- $builder->from('foobar')->orderBy('id');
- $builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
- return new Builder($builder->connection, $builder->grammar, $builder->processor);
- });
- $path = 'http://foo.bar?cursor=3';
- $results = collect([['id' => 3, 'name' => 'Taylor'], ['id' => 5, 'name' => 'Mohamed']]);
- $builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results) {
- $this->assertEquals(
- 'select * from "foobar" where ("id" > ?) order by "id" asc limit 17',
- $builder->toSql());
- $this->assertEquals([2], $builder->bindings['where']);
- return $results;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate($perPage, $columns, $cursorName, $cursor);
- $this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['id'],
- ]), $result);
- }
- public function testCursorPaginateWithMixedOrders()
- {
- $perPage = 16;
- $columns = ['foo', 'bar', 'baz'];
- $cursorName = 'cursor-name';
- $cursor = new Cursor(['foo' => 1, 'bar' => 2, 'baz' => 3]);
- $builder = $this->getMockQueryBuilder();
- $builder->from('foobar')->orderBy('foo')->orderByDesc('bar')->orderBy('baz');
- $builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
- return new Builder($builder->connection, $builder->grammar, $builder->processor);
- });
- $path = 'http://foo.bar?cursor='.$cursor->encode();
- $results = collect([['foo' => 1, 'bar' => 2, 'baz' => 4], ['foo' => 1, 'bar' => 1, 'baz' => 1]]);
- $builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results) {
- $this->assertEquals(
- 'select * from "foobar" where ("foo" > ? or ("foo" = ? and ("bar" < ? or ("bar" = ? and ("baz" > ?))))) order by "foo" asc, "bar" desc, "baz" asc limit 17',
- $builder->toSql()
- );
- $this->assertEquals([1, 1, 2, 2, 3], $builder->bindings['where']);
- return $results;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate($perPage, $columns, $cursorName, $cursor);
- $this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['foo', 'bar', 'baz'],
- ]), $result);
- }
- public function testCursorPaginateWithUnionWheres()
- {
- $ts = now()->toDateTimeString();
- $perPage = 16;
- $columns = ['test'];
- $cursorName = 'cursor-name';
- $cursor = new Cursor(['created_at' => $ts]);
- $builder = $this->getMockQueryBuilder();
- $builder->select('id', 'start_time as created_at')->selectRaw("'video' as type")->from('videos');
- $builder->union($this->getBuilder()->select('id', 'created_at')->selectRaw("'news' as type")->from('news'));
- $builder->orderBy('created_at');
- $builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
- return new Builder($builder->connection, $builder->grammar, $builder->processor);
- });
- $path = 'http://foo.bar?cursor='.$cursor->encode();
- $results = collect([
- ['id' => 1, 'created_at' => now(), 'type' => 'video'],
- ['id' => 2, 'created_at' => now(), 'type' => 'news'],
- ]);
- $builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results, $ts) {
- $this->assertEquals(
- '(select "id", "start_time" as "created_at", \'video\' as type from "videos" where ("start_time" > ?)) union (select "id", "created_at", \'news\' as type from "news" where ("start_time" > ?)) order by "created_at" asc limit 17',
- $builder->toSql());
- $this->assertEquals([$ts], $builder->bindings['where']);
- $this->assertEquals([$ts], $builder->bindings['union']);
- return $results;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate($perPage, $columns, $cursorName, $cursor);
- $this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['created_at'],
- ]), $result);
- }
- public function testCursorPaginateWithUnionWheresWithRawOrderExpression()
- {
- $ts = now()->toDateTimeString();
- $perPage = 16;
- $columns = ['test'];
- $cursorName = 'cursor-name';
- $cursor = new Cursor(['created_at' => $ts]);
- $builder = $this->getMockQueryBuilder();
- $builder->select('id', 'is_published', 'start_time as created_at')->selectRaw("'video' as type")->where('is_published', true)->from('videos');
- $builder->union($this->getBuilder()->select('id', 'is_published', 'created_at')->selectRaw("'news' as type")->where('is_published', true)->from('news'));
- $builder->orderByRaw('case when (id = 3 and type="news" then 0 else 1 end)')->orderBy('created_at');
- $builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
- return new Builder($builder->connection, $builder->grammar, $builder->processor);
- });
- $path = 'http://foo.bar?cursor='.$cursor->encode();
- $results = collect([
- ['id' => 1, 'created_at' => now(), 'type' => 'video', 'is_published' => true],
- ['id' => 2, 'created_at' => now(), 'type' => 'news', 'is_published' => true],
- ]);
- $builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results, $ts) {
- $this->assertEquals(
- '(select "id", "is_published", "start_time" as "created_at", \'video\' as type from "videos" where "is_published" = ? and ("start_time" > ?)) union (select "id", "is_published", "created_at", \'news\' as type from "news" where "is_published" = ? and ("start_time" > ?)) order by case when (id = 3 and type="news" then 0 else 1 end), "created_at" asc limit 17',
- $builder->toSql());
- $this->assertEquals([true, $ts], $builder->bindings['where']);
- $this->assertEquals([true, $ts], $builder->bindings['union']);
- return $results;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate($perPage, $columns, $cursorName, $cursor);
- $this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['created_at'],
- ]), $result);
- }
- public function testCursorPaginateWithUnionWheresReverseOrder()
- {
- $ts = now()->toDateTimeString();
- $perPage = 16;
- $columns = ['test'];
- $cursorName = 'cursor-name';
- $cursor = new Cursor(['created_at' => $ts], false);
- $builder = $this->getMockQueryBuilder();
- $builder->select('id', 'start_time as created_at')->selectRaw("'video' as type")->from('videos');
- $builder->union($this->getBuilder()->select('id', 'created_at')->selectRaw("'news' as type")->from('news'));
- $builder->orderBy('created_at');
- $builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
- return new Builder($builder->connection, $builder->grammar, $builder->processor);
- });
- $path = 'http://foo.bar?cursor='.$cursor->encode();
- $results = collect([
- ['id' => 1, 'created_at' => now(), 'type' => 'video'],
- ['id' => 2, 'created_at' => now(), 'type' => 'news'],
- ]);
- $builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results, $ts) {
- $this->assertEquals(
- '(select "id", "start_time" as "created_at", \'video\' as type from "videos" where ("start_time" < ?)) union (select "id", "created_at", \'news\' as type from "news" where ("start_time" < ?)) order by "created_at" asc limit 17',
- $builder->toSql());
- $this->assertEquals([$ts], $builder->bindings['where']);
- $this->assertEquals([$ts], $builder->bindings['union']);
- return $results;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate($perPage, $columns, $cursorName, $cursor);
- $this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['created_at'],
- ]), $result);
- }
- public function testCursorPaginateWithUnionWheresMultipleOrders()
- {
- $ts = now()->toDateTimeString();
- $perPage = 16;
- $columns = ['test'];
- $cursorName = 'cursor-name';
- $cursor = new Cursor(['created_at' => $ts, 'id' => 1]);
- $builder = $this->getMockQueryBuilder();
- $builder->select('id', 'start_time as created_at')->selectRaw("'video' as type")->from('videos');
- $builder->union($this->getBuilder()->select('id', 'created_at')->selectRaw("'news' as type")->from('news'));
- $builder->orderByDesc('created_at')->orderBy('id');
- $builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
- return new Builder($builder->connection, $builder->grammar, $builder->processor);
- });
- $path = 'http://foo.bar?cursor='.$cursor->encode();
- $results = collect([
- ['id' => 1, 'created_at' => now(), 'type' => 'video'],
- ['id' => 2, 'created_at' => now(), 'type' => 'news'],
- ]);
- $builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results, $ts) {
- $this->assertEquals(
- '(select "id", "start_time" as "created_at", \'video\' as type from "videos" where ("start_time" < ? or ("start_time" = ? and ("id" > ?)))) union (select "id", "created_at", \'news\' as type from "news" where ("start_time" < ? or ("start_time" = ? and ("id" > ?)))) order by "created_at" desc, "id" asc limit 17',
- $builder->toSql());
- $this->assertEquals([$ts, $ts, 1], $builder->bindings['where']);
- $this->assertEquals([$ts, $ts, 1], $builder->bindings['union']);
- return $results;
- });
- Paginator::currentPathResolver(function () use ($path) {
- return $path;
- });
- $result = $builder->cursorPaginate($perPage, $columns, $cursorName, $cursor);
- $this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
- 'path' => $path,
- 'cursorName' => $cursorName,
- 'parameters' => ['created_at', 'id'],
- ]), $result);
- }
- public function testWhereRowValues()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('orders')->whereRowValues(['last_update', 'order_number'], '<', [1, 2]);
- $this->assertSame('select * from "orders" where ("last_update", "order_number") < (?, ?)', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('orders')->where('company_id', 1)->orWhereRowValues(['last_update', 'order_number'], '<', [1, 2]);
- $this->assertSame('select * from "orders" where "company_id" = ? or ("last_update", "order_number") < (?, ?)', $builder->toSql());
- $builder = $this->getBuilder();
- $builder->select('*')->from('orders')->whereRowValues(['last_update', 'order_number'], '<', [1, new Raw('2')]);
- $this->assertSame('select * from "orders" where ("last_update", "order_number") < (?, 2)', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereRowValuesArityMismatch()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The number of columns must match the number of values');
- $builder = $this->getBuilder();
- $builder->select('*')->from('orders')->whereRowValues(['last_update'], '<', [1, 2]);
- }
- public function testWhereJsonContainsMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereJsonContains('options', ['en']);
- $this->assertSame('select * from `users` where json_contains(`options`, ?)', $builder->toSql());
- $this->assertEquals(['["en"]'], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereJsonContains('users.options->languages', ['en']);
- $this->assertSame('select * from `users` where json_contains(`users`.`options`, ?, \'$."languages"\')', $builder->toSql());
- $this->assertEquals(['["en"]'], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonContains('options->languages', new Raw("'[\"en\"]'"));
- $this->assertSame('select * from `users` where `id` = ? or json_contains(`options`, \'["en"]\', \'$."languages"\')', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereJsonContainsPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereJsonContains('options', ['en']);
- $this->assertSame('select * from "users" where ("options")::jsonb @> ?', $builder->toSql());
- $this->assertEquals(['["en"]'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereJsonContains('users.options->languages', ['en']);
- $this->assertSame('select * from "users" where ("users"."options"->\'languages\')::jsonb @> ?', $builder->toSql());
- $this->assertEquals(['["en"]'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonContains('options->languages', new Raw("'[\"en\"]'"));
- $this->assertSame('select * from "users" where "id" = ? or ("options"->\'languages\')::jsonb @> \'["en"]\'', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereJsonContainsSqlite()
- {
- $this->expectException(RuntimeException::class);
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereJsonContains('options->languages', ['en'])->toSql();
- }
- public function testWhereJsonContainsSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereJsonContains('options', true);
- $this->assertSame('select * from [users] where ? in (select [value] from openjson([options]))', $builder->toSql());
- $this->assertEquals(['true'], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereJsonContains('users.options->languages', 'en');
- $this->assertSame('select * from [users] where ? in (select [value] from openjson([users].[options], \'$."languages"\'))', $builder->toSql());
- $this->assertEquals(['en'], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonContains('options->languages', new Raw("'en'"));
- $this->assertSame('select * from [users] where [id] = ? or \'en\' in (select [value] from openjson([options], \'$."languages"\'))', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereJsonDoesntContainMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereJsonDoesntContain('options->languages', ['en']);
- $this->assertSame('select * from `users` where not json_contains(`options`, ?, \'$."languages"\')', $builder->toSql());
- $this->assertEquals(['["en"]'], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonDoesntContain('options->languages', new Raw("'[\"en\"]'"));
- $this->assertSame('select * from `users` where `id` = ? or not json_contains(`options`, \'["en"]\', \'$."languages"\')', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereJsonDoesntContainPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereJsonDoesntContain('options->languages', ['en']);
- $this->assertSame('select * from "users" where not ("options"->\'languages\')::jsonb @> ?', $builder->toSql());
- $this->assertEquals(['["en"]'], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonDoesntContain('options->languages', new Raw("'[\"en\"]'"));
- $this->assertSame('select * from "users" where "id" = ? or not ("options"->\'languages\')::jsonb @> \'["en"]\'', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereJsonDoesntContainSqlite()
- {
- $this->expectException(RuntimeException::class);
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereJsonDoesntContain('options->languages', ['en'])->toSql();
- }
- public function testWhereJsonDoesntContainSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereJsonDoesntContain('options->languages', 'en');
- $this->assertSame('select * from [users] where not ? in (select [value] from openjson([options], \'$."languages"\'))', $builder->toSql());
- $this->assertEquals(['en'], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonDoesntContain('options->languages', new Raw("'en'"));
- $this->assertSame('select * from [users] where [id] = ? or not \'en\' in (select [value] from openjson([options], \'$."languages"\'))', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereJsonLengthMySql()
- {
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereJsonLength('options', 0);
- $this->assertSame('select * from `users` where json_length(`options`) = ?', $builder->toSql());
- $this->assertEquals([0], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->whereJsonLength('users.options->languages', '>', 0);
- $this->assertSame('select * from `users` where json_length(`users`.`options`, \'$."languages"\') > ?', $builder->toSql());
- $this->assertEquals([0], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', new Raw('0'));
- $this->assertSame('select * from `users` where `id` = ? or json_length(`options`, \'$."languages"\') = 0', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- $builder = $this->getMySqlBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', '>', new Raw('0'));
- $this->assertSame('select * from `users` where `id` = ? or json_length(`options`, \'$."languages"\') > 0', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereJsonLengthPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereJsonLength('options', 0);
- $this->assertSame('select * from "users" where json_array_length(("options")::json) = ?', $builder->toSql());
- $this->assertEquals([0], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->whereJsonLength('users.options->languages', '>', 0);
- $this->assertSame('select * from "users" where json_array_length(("users"."options"->\'languages\')::json) > ?', $builder->toSql());
- $this->assertEquals([0], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', new Raw('0'));
- $this->assertSame('select * from "users" where "id" = ? or json_array_length(("options"->\'languages\')::json) = 0', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', '>', new Raw('0'));
- $this->assertSame('select * from "users" where "id" = ? or json_array_length(("options"->\'languages\')::json) > 0', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereJsonLengthSqlite()
- {
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereJsonLength('options', 0);
- $this->assertSame('select * from "users" where json_array_length("options") = ?', $builder->toSql());
- $this->assertEquals([0], $builder->getBindings());
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->whereJsonLength('users.options->languages', '>', 0);
- $this->assertSame('select * from "users" where json_array_length("users"."options", \'$."languages"\') > ?', $builder->toSql());
- $this->assertEquals([0], $builder->getBindings());
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', new Raw('0'));
- $this->assertSame('select * from "users" where "id" = ? or json_array_length("options", \'$."languages"\') = 0', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- $builder = $this->getSQLiteBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', '>', new Raw('0'));
- $this->assertSame('select * from "users" where "id" = ? or json_array_length("options", \'$."languages"\') > 0', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testWhereJsonLengthSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereJsonLength('options', 0);
- $this->assertSame('select * from [users] where (select count(*) from openjson([options])) = ?', $builder->toSql());
- $this->assertEquals([0], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->whereJsonLength('users.options->languages', '>', 0);
- $this->assertSame('select * from [users] where (select count(*) from openjson([users].[options], \'$."languages"\')) > ?', $builder->toSql());
- $this->assertEquals([0], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', new Raw('0'));
- $this->assertSame('select * from [users] where [id] = ? or (select count(*) from openjson([options], \'$."languages"\')) = 0', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- $builder = $this->getSqlServerBuilder();
- $builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonLength('options->languages', '>', new Raw('0'));
- $this->assertSame('select * from [users] where [id] = ? or (select count(*) from openjson([options], \'$."languages"\')) > 0', $builder->toSql());
- $this->assertEquals([1], $builder->getBindings());
- }
- public function testFromSub()
- {
- $builder = $this->getBuilder();
- $builder->fromSub(function ($query) {
- $query->select(new Raw('max(last_seen_at) as last_seen_at'))->from('user_sessions')->where('foo', '=', '1');
- }, 'sessions')->where('bar', '<', '10');
- $this->assertSame('select * from (select max(last_seen_at) as last_seen_at from "user_sessions" where "foo" = ?) as "sessions" where "bar" < ?', $builder->toSql());
- $this->assertEquals(['1', '10'], $builder->getBindings());
- $this->expectException(InvalidArgumentException::class);
- $builder = $this->getBuilder();
- $builder->fromSub(['invalid'], 'sessions')->where('bar', '<', '10');
- }
- public function testFromSubWithPrefix()
- {
- $builder = $this->getBuilder();
- $builder->getGrammar()->setTablePrefix('prefix_');
- $builder->fromSub(function ($query) {
- $query->select(new Raw('max(last_seen_at) as last_seen_at'))->from('user_sessions')->where('foo', '=', '1');
- }, 'sessions')->where('bar', '<', '10');
- $this->assertSame('select * from (select max(last_seen_at) as last_seen_at from "prefix_user_sessions" where "foo" = ?) as "prefix_sessions" where "bar" < ?', $builder->toSql());
- $this->assertEquals(['1', '10'], $builder->getBindings());
- }
- public function testFromSubWithoutBindings()
- {
- $builder = $this->getBuilder();
- $builder->fromSub(function ($query) {
- $query->select(new Raw('max(last_seen_at) as last_seen_at'))->from('user_sessions');
- }, 'sessions');
- $this->assertSame('select * from (select max(last_seen_at) as last_seen_at from "user_sessions") as "sessions"', $builder->toSql());
- $this->expectException(InvalidArgumentException::class);
- $builder = $this->getBuilder();
- $builder->fromSub(['invalid'], 'sessions');
- }
- public function testFromRaw()
- {
- $builder = $this->getBuilder();
- $builder->fromRaw(new Raw('(select max(last_seen_at) as last_seen_at from "user_sessions") as "sessions"'));
- $this->assertSame('select * from (select max(last_seen_at) as last_seen_at from "user_sessions") as "sessions"', $builder->toSql());
- }
- public function testFromRawOnSqlServer()
- {
- $builder = $this->getSqlServerBuilder();
- $builder->fromRaw('dbo.[SomeNameWithRoundBrackets (test)]');
- $this->assertSame('select * from dbo.[SomeNameWithRoundBrackets (test)]', $builder->toSql());
- }
- public function testFromRawWithWhereOnTheMainQuery()
- {
- $builder = $this->getBuilder();
- $builder->fromRaw(new Raw('(select max(last_seen_at) as last_seen_at from "sessions") as "last_seen_at"'))->where('last_seen_at', '>', '1520652582');
- $this->assertSame('select * from (select max(last_seen_at) as last_seen_at from "sessions") as "last_seen_at" where "last_seen_at" > ?', $builder->toSql());
- $this->assertEquals(['1520652582'], $builder->getBindings());
- }
- public function testFromQuestionMarkOperatorOnPostgres()
- {
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('roles', '?', 'superuser');
- $this->assertSame('select * from "users" where "roles" ?? ?', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('roles', '?|', 'superuser');
- $this->assertSame('select * from "users" where "roles" ??| ?', $builder->toSql());
- $builder = $this->getPostgresBuilder();
- $builder->select('*')->from('users')->where('roles', '?&', 'superuser');
- $this->assertSame('select * from "users" where "roles" ??& ?', $builder->toSql());
- }
- public function testClone()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users');
- $clone = $builder->clone()->where('email', 'foo');
- $this->assertNotSame($builder, $clone);
- $this->assertSame('select * from "users"', $builder->toSql());
- $this->assertSame('select * from "users" where "email" = ?', $clone->toSql());
- }
- public function testCloneWithout()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('email', 'foo')->orderBy('email');
- $clone = $builder->cloneWithout(['orders']);
- $this->assertSame('select * from "users" where "email" = ? order by "email" asc', $builder->toSql());
- $this->assertSame('select * from "users" where "email" = ?', $clone->toSql());
- }
- public function testCloneWithoutBindings()
- {
- $builder = $this->getBuilder();
- $builder->select('*')->from('users')->where('email', 'foo')->orderBy('email');
- $clone = $builder->cloneWithout(['wheres'])->cloneWithoutBindings(['where']);
- $this->assertSame('select * from "users" where "email" = ? order by "email" asc', $builder->toSql());
- $this->assertEquals([0 => 'foo'], $builder->getBindings());
- $this->assertSame('select * from "users" order by "email" asc', $clone->toSql());
- $this->assertEquals([], $clone->getBindings());
- }
- protected function getConnection()
- {
- $connection = m::mock(ConnectionInterface::class);
- $connection->shouldReceive('getDatabaseName')->andReturn('database');
- return $connection;
- }
- protected function getBuilder()
- {
- $grammar = new Grammar;
- $processor = m::mock(Processor::class);
- return new Builder($this->getConnection(), $grammar, $processor);
- }
- protected function getPostgresBuilder()
- {
- $grammar = new PostgresGrammar;
- $processor = m::mock(Processor::class);
- return new Builder($this->getConnection(), $grammar, $processor);
- }
- protected function getMySqlBuilder()
- {
- $grammar = new MySqlGrammar;
- $processor = m::mock(Processor::class);
- return new Builder(m::mock(ConnectionInterface::class), $grammar, $processor);
- }
- protected function getSQLiteBuilder()
- {
- $grammar = new SQLiteGrammar;
- $processor = m::mock(Processor::class);
- return new Builder(m::mock(ConnectionInterface::class), $grammar, $processor);
- }
- protected function getSqlServerBuilder()
- {
- $grammar = new SqlServerGrammar;
- $processor = m::mock(Processor::class);
- return new Builder($this->getConnection(), $grammar, $processor);
- }
- protected function getMySqlBuilderWithProcessor()
- {
- $grammar = new MySqlGrammar;
- $processor = new MySqlProcessor;
- return new Builder(m::mock(ConnectionInterface::class), $grammar, $processor);
- }
- protected function getPostgresBuilderWithProcessor()
- {
- $grammar = new PostgresGrammar;
- $processor = new PostgresProcessor;
- return new Builder(m::mock(ConnectionInterface::class), $grammar, $processor);
- }
- /**
- * @return \Mockery\MockInterface|\Illuminate\Database\Query\Builder
- */
- protected function getMockQueryBuilder()
- {
- return m::mock(Builder::class, [
- m::mock(ConnectionInterface::class),
- new Grammar,
- m::mock(Processor::class),
- ])->makePartial();
- }
- }
|