doctrine-migrations.phar 864 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. Phar::mapPhar();
  20. require_once 'phar://'.__FILE__.'/Doctrine/Common/ClassLoader.php';
  21. $classLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', 'phar://'.__FILE__);
  22. $classLoader->register();
  23. $classLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', 'phar://'.__FILE__);
  24. $classLoader->register();
  25. $classLoader = new \Doctrine\Common\ClassLoader('Symfony', 'phar://'.__FILE__);
  26. $classLoader->register();
  27. $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
  28. 'dialog' => new \Symfony\Component\Console\Helper\DialogHelper(),
  29. ));
  30. $cli = new \Symfony\Component\Console\Application('Doctrine Migrations', \Doctrine\DBAL\Migrations\MigrationsVersion::VERSION);
  31. $cli->setCatchExceptions(true);
  32. $cli->setHelperSet($helperSet);
  33. $cli->addCommands(array(
  34. // Migrations Commands
  35. new \Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand(),
  36. new \Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand(),
  37. new \Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand(),
  38. new \Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand(),
  39. new \Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand(),
  40. new \Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand()
  41. ));
  42. $input = file_exists('migrations-input.php')
  43. ? include('migrations-input.php')
  44. : null;
  45. $output = file_exists('migrations-output.php')
  46. ? include('migrations-output.php')
  47. : null;
  48. $cli->run($input, $output);
  49. __HALT_COMPILER(); ?>
  50. ƒ,��¤���������������;���Doctrine/DBAL/Migrations/IrreversibleMigrationException.php¨��™ÈÏL¨��‚üÊܶ������4���Doctrine/DBAL/Migrations/AbortMigrationException.phpm���™ÈÏLm���°ßœ¶������3���Doctrine/DBAL/Migrations/SkipMigrationException.phpl���™ÈÏLl���TŶ������.���Doctrine/DBAL/Migrations/AbstractMigration.phpå��™ÈÏLå��¢�é`¶������/���Doctrine/DBAL/Migrations/MigrationException.phpÈ ��™ÈÏLÈ ��(Shz¶������&���Doctrine/DBAL/Migrations/Migration.php ��™ÈÏL ��Í~®«¶������8���Doctrine/DBAL/Migrations/Configuration/Configuration.php"7��™ÈÏL"7��pë�¶������;���Doctrine/DBAL/Migrations/Configuration/XmlConfiguration.php› ��™ÈÏL› ��ëZ÷¶������<���Doctrine/DBAL/Migrations/Configuration/YamlConfiguration.php¢ ��™ÈÏL¢ ��>x7¾¶������D���Doctrine/DBAL/Migrations/Configuration/AbstractFileConfiguration.php­ ��™ÈÏL­ ��ŸMÿ¶������)���Doctrine/DBAL/Migrations/OutputWriter.php��™ÈÏL��4—£4¶������$���Doctrine/DBAL/Migrations/Version.phpÏ&��™ÈÏLÏ&��vŒM}¶������.���Doctrine/DBAL/Migrations/MigrationsVersion.php8��™ÈÏL8��¢R£¶������>���Doctrine/DBAL/Migrations/Tools/Console/Command/DiffCommand.phpÌ��™ÈÏLÌ��£4&¶������B���Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.phpâ��™ÈÏLâ��*T\j¶������B���Doctrine/DBAL/Migrations/Tools/Console/Command/GenerateCommand.phpã��™ÈÏLã��~Ò¶������A���Doctrine/DBAL/Migrations/Tools/Console/Command/VersionCommand.php��™ÈÏL��í±§¶������A���Doctrine/DBAL/Migrations/Tools/Console/Command/ExecuteCommand.php–��™ÈÏL–��UüI¶������@���Doctrine/DBAL/Migrations/Tools/Console/Command/StatusCommand.php÷��™ÈÏL÷��ñe�-¶������A���Doctrine/DBAL/Migrations/Tools/Console/Command/MigrateCommand.php7��™ÈÏL7��a±é¹¶���������Doctrine/DBAL/DBALException.php
  51. ��™ÈÏL
  52. ��Œ^Ñ<¶������)���Doctrine/DBAL/Driver/PDOOracle/Driver.php¤
  53. ��™ÈÏL¤
  54. ��ýós&¶������-���Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php ��™ÈÏL ��<t–�¶������,���Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php®.��™ÈÏL®.��Á¥3¶������,���Doctrine/DBAL/Driver/IBMDB2/DB2Exception.php7��™ÈÏL7��Ç~Þò¶������)���Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php®��™ÈÏL®��ühƒ$¶������%���Doctrine/DBAL/Driver/PDOStatement.php ��™ÈÏL ��‡FÁ¶������(���Doctrine/DBAL/Driver/PDOMsSql/Driver.phpÒ ��™ÈÏLÒ ���kX)¶������,���Doctrine/DBAL/Driver/PDOMsSql/Connection.php|��™ÈÏL|��P{1¶������"���Doctrine/DBAL/Driver/Statement.php›%��™ÈÏL›%��,¿i€¶������(���Doctrine/DBAL/Driver/PDOPgSql/Driver.phpŒ��™ÈÏLŒ��§œZ¶������&���Doctrine/DBAL/Driver/PDOConnection.php��™ÈÏL��Q0Ô¶������&���Doctrine/DBAL/Driver/PDOIbm/Driver.phpN��™ÈÏLN��SPuo¶������(���Doctrine/DBAL/Driver/PDOMySql/Driver.php? ��™ÈÏL? ��£°¤¿¶������)���Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php¬ ��™ÈÏL¬ ��·ܪ¶������)���Doctrine/DBAL/Driver/PDOSqlite/Driver.php§��™ÈÏL§��ÓÖV€¶������+���Doctrine/DBAL/Driver/OCI8/OCI8Exception.php«��™ÈÏL«��J¢Ù®¶������$���Doctrine/DBAL/Driver/OCI8/Driver.php? ��™ÈÏL? ��óÀ»¶������+���Doctrine/DBAL/Driver/OCI8/OCI8Statement.php��™ÈÏL��µR¦¶������,���Doctrine/DBAL/Driver/OCI8/OCI8Connection.php��™ÈÏL��Ï父������#���Doctrine/DBAL/Driver/Connection.phpé��™ÈÏLé���kV«¶���������Doctrine/DBAL/Events.phpù��™ÈÏLù��Â�p¶���������Doctrine/DBAL/DriverManager.phpË��™ÈÏLË��dÄ< ¶���������Doctrine/DBAL/Configuration.phpp��™ÈÏLp��¦êMo¶���������Doctrine/DBAL/Statement.php��™ÈÏL��uÒËÛ¶������+���Doctrine/DBAL/Schema/MySqlSchemaManager.php:��™ÈÏL:��#÷r€¶������0���Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php—$��™ÈÏL—$��ÔÛ‹ó¶���������Doctrine/DBAL/Schema/View.phpF��™ÈÏLF���.ú±¶������)���Doctrine/DBAL/Schema/DB2SchemaManager.php=��™ÈÏL=��Ó“»¶������,���Doctrine/DBAL/Schema/SqliteSchemaManager.php÷��™ÈÏL÷��U´¶������#���Doctrine/DBAL/Schema/Comparator.phpÁ3��™ÈÏLÁ3��ŽÖÜ
  55. ¶���������Doctrine/DBAL/Schema/Index.php€��™ÈÏL€��Ô&o!¶������.���Doctrine/DBAL/Schema/AbstractSchemaManager.phpKV��™ÈÏLKV��,:‰¶������&���Doctrine/DBAL/Schema/AbstractAsset.phpþ ��™ÈÏLþ �� TK¶������%���Doctrine/DBAL/Schema/SchemaConfig.phpC��™ÈÏLC��Ù>­Æ¶���������Doctrine/DBAL/Schema/Column.php§��™ÈÏL§��+WŠ’¶������#���Doctrine/DBAL/Schema/Constraint.phpO��™ÈÏLO��‚:‡¦¶������#���Doctrine/DBAL/Schema/SchemaDiff.phpÿ��™ÈÏLÿ��~'š×¶���������Doctrine/DBAL/Schema/Schema.php3"��™ÈÏL3"��"$V¶������+���Doctrine/DBAL/Schema/MsSqlSchemaManager.php!��™ÈÏL!��…@á„¶������(���Doctrine/DBAL/Schema/Visitor/Visitor.php* ��™ÈÏL* ��D�i&¶������9���Doctrine/DBAL/Schema/Visitor/CreateSchemaSqlCollector.phpj��™ÈÏLj��à¶¶������7���Doctrine/DBAL/Schema/Visitor/DropSchemaSqlCollector.php¡��™ÈÏL¡��0ýé¶������-���Doctrine/DBAL/Schema/ForeignKeyConstraint.phpÚ��™ÈÏLÚ��}�W¼¶���������Doctrine/DBAL/Schema/Table.php`C��™ÈÏL`C��ï»P¶������"���Doctrine/DBAL/Schema/TableDiff.php^��™ÈÏL^��Ê­¶������,���Doctrine/DBAL/Schema/OracleSchemaManager.php6$��™ÈÏL6$��äÍG¶������!���Doctrine/DBAL/Schema/Sequence.php’��™ÈÏL’��LöÜk¶������#���Doctrine/DBAL/Schema/ColumnDiff.php��™ÈÏL��ûÏ'‹¶������(���Doctrine/DBAL/Schema/SchemaException.phpt��™ÈÏLt��ή!†¶���������Doctrine/DBAL/LockMode.phpä��™ÈÏLä��jWW̶���������Doctrine/DBAL/Driver.php=
  56. ��™ÈÏL=
  57. ��1ô]¶������.���Doctrine/DBAL/Platforms/PostgreSqlPlatform.php Y��™ÈÏL Y��®Œv¡¶������)���Doctrine/DBAL/Platforms/MsSqlPlatform.php—M��™ÈÏL—M��¼å>0¶������*���Doctrine/DBAL/Platforms/OraclePlatform.phpƒV��™ÈÏLƒV��|ª�¶������'���Doctrine/DBAL/Platforms/DB2Platform.phpA��™ÈÏLA��\E1ß¶������)���Doctrine/DBAL/Platforms/MySqlPlatform.php�M��™ÈÏL�M��¥ “6¶������,���Doctrine/DBAL/Platforms/AbstractPlatform.phpýî��™ÈÏLýî��Xšòˆ¶������*���Doctrine/DBAL/Platforms/SqlitePlatform.phpF8��™ÈÏLF8��E T¶���������Doctrine/DBAL/README.markdown����™ÈÏL��������¶���������Doctrine/DBAL/Version.phpó��™ÈÏLó��|!’¶������%���Doctrine/DBAL/ConnectionException.php���™ÈÏL���¾óíC¶���������Doctrine/DBAL/Connection.php£{��™ÈÏL£{��÷ªË¶������'���Doctrine/DBAL/Logging/EchoSQLLogger.phpN��™ÈÏLN��¾N¬¶������$���Doctrine/DBAL/Logging/DebugStack.phpŸ��™ÈÏLŸ��}ª¶������#���Doctrine/DBAL/Logging/SQLLogger.php§��™ÈÏL§��5õ� ¶������5���Doctrine/DBAL/Tools/Console/Command/ImportCommand.php��™ÈÏL��G:�¨¶������5���Doctrine/DBAL/Tools/Console/Command/RunSqlCommand.php/ ��™ÈÏL/ ��÷Ï9R¶������7���Doctrine/DBAL/Tools/Console/Helper/ConnectionHelper.phpv��™ÈÏLv��4ás¶������'���Doctrine/DBAL/Types/VarDateTimeType.phpÔ��™ÈÏLÔ��wá3ì¶������#���Doctrine/DBAL/Types/IntegerType.phpª��™ÈÏLª�� l6’¶������ ���Doctrine/DBAL/Types/DateType.phpÊ��™ÈÏLÊ��òEÇe¶���������Doctrine/DBAL/Types/Type.php��™ÈÏL��Â,do¶������!���Doctrine/DBAL/Types/FloatType.phpz��™ÈÏLz��~[e‘¶������$���Doctrine/DBAL/Types/SmallIntType.php…��™ÈÏL…��G9#k¶������"���Doctrine/DBAL/Types/BigIntType.phpû��™ÈÏLû��>Ÿõ¶������"���Doctrine/DBAL/Types/StringType.phpY��™ÈÏLY��(¤.G¶������"���Doctrine/DBAL/Types/ObjectType.phpñ��™ÈÏLñ��M(¶������&���Doctrine/DBAL/Types/DateTimeTzType.php ��™ÈÏL ��–ç}¶������+���Doctrine/DBAL/Types/ConversionException.php±��™ÈÏL±��¢Gf[¶������$���Doctrine/DBAL/Types/DateTimeType.phpë��™ÈÏLë��ìÝ�¶������#���Doctrine/DBAL/Types/DecimalType.php��™ÈÏL��œµ‘¶������ ���Doctrine/DBAL/Types/TextType.php��™ÈÏL��ªúæ@¶������!���Doctrine/DBAL/Types/ArrayType.phpÊ��™ÈÏLÊ��§´ùï¶������#���Doctrine/DBAL/Types/BooleanType.php��™ÈÏL��Ÿ<4‰¶������ ���Doctrine/DBAL/Types/TimeType.php5��™ÈÏL5��­Ù»¶������+���Doctrine/DBAL/Event/ConnectionEventArgs.phpõ��™ÈÏLõ��±5�¶������2���Doctrine/DBAL/Event/Listeners/MysqlSessionInit.phpO ��™ÈÏLO ��¤â¿9¶������3���Doctrine/DBAL/Event/Listeners/OracleSessionInit.php˜ ��™ÈÏL˜ ��1?‘¶���������Doctrine/Common/EventArgs.php#
  58. ��™ÈÏL#
  59. ��to·¶������#���Doctrine/Common/CommonException.phpq��™ÈÏLq��(|k¶������0���Doctrine/Common/Annotations/AnnotationReader.php"��™ÈÏL"��
  60. 6ÿ¶������3���Doctrine/Common/Annotations/AnnotationException.php���™ÈÏL���ø=?‹¶������&���Doctrine/Common/Annotations/Parser.php£=��™ÈÏL£=��3¾šS¶������%���Doctrine/Common/Annotations/Lexer.phpª��™ÈÏLª��Snôÿ¶������*���Doctrine/Common/Annotations/Annotation.phpö ��™ÈÏLö ��@ãÔ¶���������Doctrine/Common/ClassLoader.php"��™ÈÏL"��›:à@¶������/���Doctrine/Common/Collections/ArrayCollection.php .��™ÈÏL .��ùjë¶������*���Doctrine/Common/Collections/Collection.phpZ ��™ÈÏLZ ��Ÿ´€€¶���������Doctrine/Common/Version.phpõ��™ÈÏLõ��¸Ó/\¶������%���Doctrine/Common/Cache/XcacheCache.phpç ��™ÈÏLç ��&2Ͷ������$���Doctrine/Common/Cache/ArrayCache.php' ��™ÈÏL' ��ï::b¶������'���Doctrine/Common/Cache/MemcacheCache.phpû ��™ÈÏLû ��rü6€¶���������Doctrine/Common/Cache/Cache.php
  61. ��™ÈÏL
  62. ��±Ó\O¶������"���Doctrine/Common/Cache/ApcCache.phpA ��™ÈÏLA ��Y„Á*¶������'���Doctrine/Common/Cache/AbstractCache.php1��™ÈÏL1��L»�0¶���������Doctrine/Common/Util/Debug.php��™ÈÏL��Í;º`¶������"���Doctrine/Common/Util/Inflector.php;
  63. ��™ÈÏL;
  64. ��;%ë�¶������)���Doctrine/Common/NotifyPropertyChanged.phpª��™ÈÏLª��ÃnF¶������+���Doctrine/Common/PropertyChangedListener.php‰��™ÈÏL‰��©ï†î¶������ ���Doctrine/Common/EventManager.php,��™ÈÏL,��Ñg]F¶���������Doctrine/Common/Lexer.phpK��™ÈÏLK��NÁã¶������#���Doctrine/Common/EventSubscriber.php��™ÈÏL��iJÛ¶���������Symfony/Component/Yaml/Yaml.phpž ��™ÈÏLž ��!ˆ¯¶������!���Symfony/Component/Yaml/Dumper.php��™ÈÏL��Âßê¶������*���Symfony/Component/Yaml/ParserException.php��™ÈÏL��§³À9¶������$���Symfony/Component/Yaml/Exception.php
  65. ��™ÈÏL
  66. ��WGQ¶������!���Symfony/Component/Yaml/Inline.php *��™ÈÏL *��:¬Ú¶������!���Symfony/Component/Yaml/Parser.php�?��™ÈÏL�?��í|#ɶ������1���Symfony/Component/Console/Command/ListCommand.php¢��™ÈÏL¢��ý[¯¤¶������1���Symfony/Component/Console/Command/HelpCommand.phpi��™ÈÏLi��LÇUí¶������-���Symfony/Component/Console/Command/Command.phpR8��™ÈÏLR8��š¾Dj¶������2���Symfony/Component/Console/Output/ConsoleOutput.phpi��™ÈÏLi��Üw*ë¶������4���Symfony/Component/Console/Output/OutputInterface.php¶��™ÈÏL¶��ïÛw ¶������1���Symfony/Component/Console/Output/StreamOutput.php ��™ÈÏL ��¿�ÐJ¶������+���Symfony/Component/Console/Output/Output.phpí��™ÈÏLí��Èvs ¶������/���Symfony/Component/Console/Output/NullOutput.phpÕ��™ÈÏLÕ��æ1Éê¶������#���Symfony/Component/Console/Shell.php!��™ÈÏL!��É–ž¶������4���Symfony/Component/Console/Helper/FormatterHelper.php ��™ÈÏL ��g ø¶������.���Symfony/Component/Console/Helper/HelperSet.php ��™ÈÏL ��ïÑǶ������+���Symfony/Component/Console/Helper/Helper.php¿��™ÈÏL¿��³Êܶ������4���Symfony/Component/Console/Helper/HelperInterface.php±��™ÈÏL±��×ñf ¶������1���Symfony/Component/Console/Helper/DialogHelper.php” ��™ÈÏL” ��3Ü.϶������)���Symfony/Component/Console/Application.php‰[��™ÈÏL‰[��¸Nªâ¶������6���Symfony/Component/Console/Tester/ApplicationTester.phpb
  67. ��™ÈÏLb
  68. ��ôé*¶������2���Symfony/Component/Console/Tester/CommandTester.php8
  69. ��™ÈÏL8
  70. ��µÕ¥®¶������)���Symfony/Component/Console/Input/Input.php~��™ÈÏL~��)ª—[¶������.���Symfony/Component/Console/Input/ArrayInput.php ��™ÈÏL ��¿glt¶������/���Symfony/Component/Console/Input/InputOption.phpA��™ÈÏLA��ÁÁÇ6¶������3���Symfony/Component/Console/Input/InputDefinition.phpE<��™ÈÏLE<��„.ú¶������2���Symfony/Component/Console/Input/InputInterface.php¸��™ÈÏL¸��\…)-¶������-���Symfony/Component/Console/Input/ArgvInput.php‰��™ÈÏL‰��¥M°¶������/���Symfony/Component/Console/Input/StringInput.phpÍ ��™ÈÏLÍ ��g¸kd¶������1���Symfony/Component/Console/Input/InputArgument.phpË ��™ÈÏLË ��]×TP¶������<?php
  71. /*
  72. * $Id$
  73. *
  74. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  75. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  76. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  77. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  78. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  79. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  80. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  81. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  82. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  83. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  84. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  85. *
  86. * This software consists of voluntary contributions made by many individuals
  87. * and is licensed under the LGPL. For more information, see
  88. * <http://www.doctrine-project.org>.
  89. */
  90. namespace Doctrine\DBAL\Migrations;
  91. /**
  92. * Exception to be thrown in the down() methods of migrations that signifies it
  93. * is an irreversible migration and stops execution.
  94. *
  95. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  96. * @link www.doctrine-project.org
  97. * @since 2.0
  98. * @version $Revision$
  99. * @author Jonathan H. Wage <jonwage@gmail.com>
  100. */
  101. class IrreversibleMigrationException extends \Exception
  102. {
  103. }<?php
  104. namespace Doctrine\DBAL\Migrations;
  105. class AbortMigrationException extends MigrationException
  106. {
  107. }<?php
  108. namespace Doctrine\DBAL\Migrations;
  109. class SkipMigrationException extends MigrationException
  110. {
  111. }<?php
  112. /*
  113. * $Id$
  114. *
  115. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  116. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  117. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  118. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  119. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  120. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  121. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  122. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  123. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  124. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  125. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  126. *
  127. * This software consists of voluntary contributions made by many individuals
  128. * and is licensed under the LGPL. For more information, see
  129. * <http://www.doctrine-project.org>.
  130. */
  131. namespace Doctrine\DBAL\Migrations;
  132. use Doctrine\DBAL\Schema\Schema,
  133. Doctrine\DBAL\Migrations\Configuration\Configuration,
  134. Doctrine\DBAL\Migrations\Version;
  135. /**
  136. * Abstract class for individual migrations to extend from.
  137. *
  138. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  139. * @link www.doctrine-project.org
  140. * @since 2.0
  141. * @version $Revision$
  142. * @author Jonathan H. Wage <jonwage@gmail.com>
  143. */
  144. abstract class AbstractMigration
  145. {
  146. /** The Migrations Configuration instance for this migration */
  147. protected $_configuration;
  148. /** The OutputWriter object instance used for outputting information */
  149. protected $_outputWriter;
  150. /** The Doctrine\DBAL\Connection instance we are migrating */
  151. protected $_connection;
  152. /** Reference to the SchemaManager instance referened by $_connection */
  153. protected $_sm;
  154. /** Reference to the DatabasePlatform instance referenced by $_conection */
  155. protected $_platform;
  156. /** Reference to the Version instance representing this migration */
  157. protected $_version;
  158. public function __construct(Version $version)
  159. {
  160. $this->_configuration = $version->getConfiguration();
  161. $this->_outputWriter = $this->_configuration->getOutputWriter();
  162. $this->_connection = $this->_configuration->getConnection();
  163. $this->_sm = $this->_connection->getSchemaManager();
  164. $this->_platform = $this->_connection->getDatabasePlatform();
  165. $this->_version = $version;
  166. }
  167. abstract public function up(Schema $schema);
  168. abstract public function down(Schema $schema);
  169. protected function _addSql($sql)
  170. {
  171. return $this->_version->addSql($sql);
  172. }
  173. protected function _write($message)
  174. {
  175. $this->_outputWriter->write($message);
  176. }
  177. protected function _throwIrreversibleMigrationException($message = null)
  178. {
  179. if ($message === null) {
  180. $message = 'This migration is irreversible and cannot be reverted.';
  181. }
  182. throw new IrreversibleMigrationException($message);
  183. }
  184. /**
  185. * Print a warning message if the condition evalutes to TRUE.
  186. *
  187. * @param bool $condition
  188. * @param string $message
  189. */
  190. public function warnIf($condition, $message = '')
  191. {
  192. $message = (strlen($message)) ? $message : 'Unknown Reason';
  193. if ($condition === true) {
  194. $this->_outputWriter->write(' <warning>Warning during ' . $this->_version->getExecutionState() . ': ' . $message . '</warning>');
  195. }
  196. }
  197. /**
  198. * Abort the migration if the condition evalutes to TRUE.
  199. *
  200. * @param bool $condition
  201. * @param string $message
  202. */
  203. public function abortIf($condition, $message = '')
  204. {
  205. $message = (strlen($message)) ? $message : 'Unknown Reason';
  206. if ($condition === true) {
  207. throw new AbortMigrationException($message);
  208. }
  209. }
  210. /**
  211. * Skip this migration (but not the next ones) if condition evalutes to TRUE.
  212. *
  213. * @param bool $condition
  214. * @param string $message
  215. */
  216. public function skipIf($condition, $message = '')
  217. {
  218. $message = (strlen($message)) ? $message : 'Unknown Reason';
  219. if ($condition === true) {
  220. throw new SkipMigrationException($message);
  221. }
  222. }
  223. public function preUp(Schema $schema)
  224. {
  225. }
  226. public function postUp(Schema $schema)
  227. {
  228. }
  229. public function preDown(Schema $schema)
  230. {
  231. }
  232. public function postDown(Schema $schema)
  233. {
  234. }
  235. }<?php
  236. /*
  237. * $Id$
  238. *
  239. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  240. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  241. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  242. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  243. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  244. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  245. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  246. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  247. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  248. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  249. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  250. *
  251. * This software consists of voluntary contributions made by many individuals
  252. * and is licensed under the LGPL. For more information, see
  253. * <http://www.doctrine-project.org>.
  254. */
  255. namespace Doctrine\DBAL\Migrations;
  256. /**
  257. * Class for Migrations specific exceptions
  258. *
  259. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  260. * @link www.doctrine-project.org
  261. * @since 2.0
  262. * @version $Revision$
  263. * @author Jonathan H. Wage <jonwage@gmail.com>
  264. */
  265. class MigrationException extends \Exception
  266. {
  267. public static function migrationsNamespaceRequired()
  268. {
  269. return new self('Migrations namespace must be configured in order to use Doctrine migrations.', 2);
  270. }
  271. public static function migrationsDirectoryRequired()
  272. {
  273. return new self('Migrations directory must be configured in order to use Doctrine migrations.', 3);
  274. }
  275. public static function noMigrationsToExecute()
  276. {
  277. return new self('Could not find any migrations to execute.', 4);
  278. }
  279. public static function unknownMigrationVersion($version)
  280. {
  281. return new self(sprintf('Could not find migration version %s', $version), 5);
  282. }
  283. public static function alreadyAtVersion($version)
  284. {
  285. return new self(sprintf('Database is already at version %s', $version), 6);
  286. }
  287. public static function duplicateMigrationVersion($version, $class)
  288. {
  289. return new self(sprintf('Migration version %s already registered with class %s', $version, $class), 7);
  290. }
  291. public static function configurationFileAlreadyLoaded()
  292. {
  293. return new self(sprintf('Migrations configuration file already loaded'), 8);
  294. }
  295. }
  296. <?php
  297. /*
  298. * $Id$
  299. *
  300. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  301. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  302. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  303. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  304. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  305. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  306. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  307. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  308. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  309. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  310. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  311. *
  312. * This software consists of voluntary contributions made by many individuals
  313. * and is licensed under the LGPL. For more information, see
  314. * <http://www.doctrine-project.org>.
  315. */
  316. namespace Doctrine\DBAL\Migrations;
  317. use Doctrine\DBAL\Migrations\Configuration\Configuration,
  318. Doctrine\DBAL\Schema\Schema;
  319. /**
  320. * Class for running migrations to the current version or a manually specified version.
  321. *
  322. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  323. * @link www.doctrine-project.org
  324. * @since 2.0
  325. * @version $Revision$
  326. * @author Jonathan H. Wage <jonwage@gmail.com>
  327. */
  328. class Migration
  329. {
  330. /** The Doctrine\DBAL\Connection instance we are migrating */
  331. private $_connection;
  332. /** The OutputWriter object instance used for outputting information */
  333. private $_outputWriter;
  334. /**
  335. * Construct a Migration instance
  336. *
  337. * @param Configuration $configuration A migration Configuration instance
  338. */
  339. public function __construct(Configuration $configuration)
  340. {
  341. $this->_configuration = $configuration;
  342. $this->_outputWriter = $configuration->getOutputWriter();
  343. }
  344. /**
  345. * Get the array of versions and SQL queries that would be executed for
  346. * each version but do not execute anything.
  347. *
  348. * @param string $to The version to migrate to.
  349. * @return array $sql The array of SQL queries.
  350. */
  351. public function getSql($to = null)
  352. {
  353. return $this->migrate($to, true);
  354. }
  355. /**
  356. * Write a migration SQL file to the given path
  357. *
  358. * @param string $path The path to write the migration SQL file.
  359. * @param string $to The version to migrate to.
  360. * @return bool $written
  361. */
  362. public function writeSqlFile($path, $to = null)
  363. {
  364. $sql = $this->getSql($to);
  365. $from = $this->_configuration->getCurrentVersion();
  366. if ($to === null) {
  367. $to = $this->_configuration->getLatestVersion();
  368. }
  369. $string = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:m:s'));
  370. $string .= sprintf("# Migrating from %s to %s\n", $from, $to);
  371. foreach ($sql as $version => $queries) {
  372. $string .= "\n# Version " . $version . "\n";
  373. foreach ($queries as $query) {
  374. $string .= $query . ";\n";
  375. }
  376. }
  377. if (is_dir($path)) {
  378. $path = realpath($path);
  379. $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
  380. }
  381. $this->_outputWriter->write("\n".sprintf('Writing migration file to "<info>%s</info>"', $path));
  382. return file_put_contents($path, $string);
  383. }
  384. /**
  385. * Run a migration to the current version or the given target version.
  386. *
  387. * @param string $to The version to migrate to.
  388. * @param string $dryRun Whether or not to make this a dry run and not execute anything.
  389. * @return array $sql The array of migration sql statements
  390. * @throws MigrationException
  391. */
  392. public function migrate($to = null, $dryRun = false)
  393. {
  394. if ($to === null) {
  395. $to = $this->_configuration->getLatestVersion();
  396. }
  397. $from = $this->_configuration->getCurrentVersion();
  398. $from = (string) $from;
  399. $to = (string) $to;
  400. $migrations = $this->_configuration->getMigrations();
  401. if ( ! isset($migrations[$to]) && $to > 0) {
  402. throw MigrationException::unknownMigrationVersion($to);
  403. }
  404. if ($from === $to) {
  405. throw MigrationException::alreadyAtVersion($to);
  406. }
  407. $direction = $from > $to ? 'down' : 'up';
  408. $migrations = $this->_configuration->getMigrationsToExecute($direction, $to);
  409. if ($dryRun === false) {
  410. $this->_outputWriter->write(sprintf('Migrating <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from));
  411. } else {
  412. $this->_outputWriter->write(sprintf('Executing dry run of migration <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from));
  413. }
  414. if (empty($migrations)) {
  415. throw MigrationException::noMigrationsToExecute();
  416. }
  417. $sql = array();
  418. $time = 0;
  419. foreach ($migrations as $version) {
  420. $versionSql = $version->execute($direction, $dryRun);
  421. $sql[$version->getVersion()] = $versionSql;
  422. $time += $version->getTime();
  423. }
  424. $this->_outputWriter->write("\n <comment>------------------------</comment>\n");
  425. $this->_outputWriter->write(sprintf(" <info>++</info> finished in %s", $time));
  426. $this->_outputWriter->write(sprintf(" <info>++</info> %s migrations executed", count($migrations)));
  427. $this->_outputWriter->write(sprintf(" <info>++</info> %s sql queries", count($sql, true) - count($sql)));
  428. return $sql;
  429. }
  430. }
  431. <?php
  432. /*
  433. * $Id$
  434. *
  435. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  436. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  437. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  438. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  439. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  440. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  441. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  442. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  443. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  444. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  445. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  446. *
  447. * This software consists of voluntary contributions made by many individuals
  448. * and is licensed under the LGPL. For more information, see
  449. * <http://www.doctrine-project.org>.
  450. */
  451. namespace Doctrine\DBAL\Migrations\Configuration;
  452. use Doctrine\DBAL\Connection,
  453. Doctrine\DBAL\Migrations\MigrationException,
  454. Doctrine\DBAL\Migrations\Version,
  455. Doctrine\DBAL\Migrations\OutputWriter,
  456. Doctrine\DBAL\Schema\Table,
  457. Doctrine\DBAL\Schema\Column,
  458. Doctrine\DBAL\Types\Type;
  459. /**
  460. * Default Migration Configurtion object used for configuring an instance of
  461. * the Migration class. Set the connection, version table name, register migration
  462. * classes/versions, etc.
  463. *
  464. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  465. * @link www.doctrine-project.org
  466. * @since 2.0
  467. * @version $Revision$
  468. * @author Jonathan H. Wage <jonwage@gmail.com>
  469. */
  470. class Configuration
  471. {
  472. /** Name of this set of migrations */
  473. private $_name;
  474. /** Flag for whether or not the migration table has been created */
  475. private $_migrationTableCreated = false;
  476. /** Connection instance to use for migrations */
  477. private $_connection;
  478. /** OutputWriter instance for writing output during migrations */
  479. private $_outputWriter;
  480. /** The migration table name to track versions in */
  481. private $_migrationsTableName = 'doctrine_migration_versions';
  482. /** The path to a directory where new migration classes will be written */
  483. private $_migrationsDirectory;
  484. /** Namespace the migration classes live in */
  485. private $_migrationsNamespace;
  486. /** Array of the registered migrations */
  487. private $_migrations = array();
  488. /**
  489. * Construct a migration configuration object.
  490. *
  491. * @param Connection $connection A Connection instance
  492. * @param OutputWriter $outputWriter A OutputWriter instance
  493. */
  494. public function __construct(Connection $connection, OutputWriter $outputWriter = null)
  495. {
  496. $this->_connection = $connection;
  497. if ($outputWriter === null) {
  498. $outputWriter = new OutputWriter();
  499. }
  500. $this->_outputWriter = $outputWriter;
  501. }
  502. /**
  503. * Validation that this instance has all the required properties configured
  504. *
  505. * @return void
  506. * @throws MigrationException
  507. */
  508. public function validate()
  509. {
  510. if ( ! $this->_migrationsNamespace) {
  511. throw MigrationException::migrationsNamespaceRequired();
  512. }
  513. if ( ! $this->_migrationsDirectory) {
  514. throw MigrationException::migrationsDirectoryRequired();
  515. }
  516. }
  517. /**
  518. * Set the name of this set of migrations
  519. *
  520. * @param string $name The name of this set of migrations
  521. */
  522. public function setName($name)
  523. {
  524. $this->_name = $name;
  525. }
  526. /**
  527. * Returns the name of this set of migrations
  528. *
  529. * @return string $name The name of this set of migrations
  530. */
  531. public function getName()
  532. {
  533. return $this->_name;
  534. }
  535. /**
  536. * Returns the OutputWriter instance
  537. *
  538. * @return OutputWriter $outputWriter The OutputWriter instance
  539. */
  540. public function getOutputWriter()
  541. {
  542. return $this->_outputWriter;
  543. }
  544. /**
  545. * Returns a timestamp version as a formatted date
  546. *
  547. * @param string $version
  548. * @return string $formattedVersion The formatted version
  549. */
  550. public function formatVersion($version)
  551. {
  552. return sprintf('%s-%s-%s %s:%s:%s',
  553. substr($version, 0, 4),
  554. substr($version, 4, 2),
  555. substr($version, 6, 2),
  556. substr($version, 8, 2),
  557. substr($version, 10, 2),
  558. substr($version, 12, 2)
  559. );
  560. }
  561. /**
  562. * Returns the Connection instance
  563. *
  564. * @return Connection $connection The Connection instance
  565. */
  566. public function getConnection()
  567. {
  568. return $this->_connection;
  569. }
  570. /**
  571. * Set the migration table name
  572. *
  573. * @param string $tableName The migration table name
  574. */
  575. public function setMigrationsTableName($tableName)
  576. {
  577. $this->_migrationsTableName = $tableName;
  578. }
  579. /**
  580. * Returns the migration table name
  581. *
  582. * @return string $migrationsTableName The migration table name
  583. */
  584. public function getMigrationsTableName()
  585. {
  586. return $this->_migrationsTableName;
  587. }
  588. /**
  589. * Set the new migrations directory where new migration classes are generated
  590. *
  591. * @param string $migrationsDirectory The new migrations directory
  592. */
  593. public function setMigrationsDirectory($migrationsDirectory)
  594. {
  595. $this->_migrationsDirectory = $migrationsDirectory;
  596. }
  597. /**
  598. * Returns the new migrations directory where new migration classes are generated
  599. *
  600. * @return string $migrationsDirectory The new migrations directory
  601. */
  602. public function getMigrationsDirectory()
  603. {
  604. return $this->_migrationsDirectory;
  605. }
  606. /**
  607. * Set the migrations namespace
  608. *
  609. * @param string $migrationsNamespace The migrations namespace
  610. */
  611. public function setMigrationsNamespace($migrationsNamespace)
  612. {
  613. $this->_migrationsNamespace = $migrationsNamespace;
  614. }
  615. /**
  616. * Returns the migrations namespace
  617. *
  618. * @return string $migrationsNamespace The migrations namespace
  619. */
  620. public function getMigrationsNamespace()
  621. {
  622. return $this->_migrationsNamespace;
  623. }
  624. /**
  625. * Register migrations from a given directory. Recursively finds all files
  626. * with the pattern VersionYYYYMMDDHHMMSS.php as the filename and registers
  627. * them as migrations.
  628. *
  629. * @param string $path The root directory to where some migration classes live.
  630. * @return $migrations The array of migrations registered.
  631. */
  632. public function registerMigrationsFromDirectory($path)
  633. {
  634. $path = realpath($path);
  635. $path = rtrim($path, '/');
  636. $files = glob($path . '/Version*.php');
  637. $versions = array();
  638. foreach ($files as $file) {
  639. require_once($file);
  640. $info = pathinfo($file);
  641. $version = substr($info['filename'], 7);
  642. $class = $this->_migrationsNamespace . '\\' . $info['filename'];
  643. $versions[] = $this->registerMigration($version, $class);
  644. }
  645. return $versions;
  646. }
  647. /**
  648. * Register a single migration version to be executed by a AbstractMigration
  649. * class.
  650. *
  651. * @param string $version The version of the migration in the format YYYYMMDDHHMMSS.
  652. * @param string $class The migration class to execute for the version.
  653. */
  654. public function registerMigration($version, $class)
  655. {
  656. $version = (string) $version;
  657. $class = (string) $class;
  658. if (isset($this->_migrations[$version])) {
  659. throw MigrationException::duplicateMigrationVersion($version, get_class($this->_migrations[$version]));
  660. }
  661. $version = new Version($this, $version, $class);
  662. $this->_migrations[$version->getVersion()] = $version;
  663. ksort($this->_migrations);
  664. return $version;
  665. }
  666. /**
  667. * Register an array of migrations. Each key of the array is the version and
  668. * the value is the migration class name.
  669. *
  670. *
  671. * @param array $migrations
  672. * @return void
  673. */
  674. public function registerMigrations(array $migrations)
  675. {
  676. $versions = array();
  677. foreach ($migrations as $version => $class) {
  678. $versions[] = $this->registerMigration($version, $class);
  679. }
  680. return $versions;
  681. }
  682. /**
  683. * Get the array of registered migration versions.
  684. *
  685. * @return array $migrations
  686. */
  687. public function getMigrations()
  688. {
  689. return $this->_migrations;
  690. }
  691. /**
  692. * Returns the Version instance for a given version in the format YYYYMMDDHHMMSS.
  693. *
  694. * @param string $version The version string in the format YYYYMMDDHHMMSS.
  695. * @return Version $version
  696. * @throws MigrationException $exception Throws exception if migration version does not exist.
  697. */
  698. public function getVersion($version)
  699. {
  700. if ( ! isset($this->_migrations[$version])) {
  701. MigrationException::unknownMigrationVersion($version);
  702. }
  703. return $this->_migrations[$version];
  704. }
  705. /**
  706. * Check if a version exists.
  707. *
  708. * @param string $version
  709. * @return bool $exists
  710. */
  711. public function hasVersion($version)
  712. {
  713. return isset($this->_migrations[$version]) ? true : false;
  714. }
  715. /**
  716. * Check if a version has been migrated or not yet
  717. *
  718. * @param Version $version
  719. * @return bool $migrated
  720. */
  721. public function hasVersionMigrated(Version $version)
  722. {
  723. $this->createMigrationTable();
  724. $version = $this->_connection->fetchColumn("SELECT version FROM " . $this->_migrationsTableName . " WHERE version = '" . $version->getVersion() . "'");
  725. return $version !== false ? true : false;
  726. }
  727. /**
  728. * Returns the current migrated version from the versions table.
  729. *
  730. * @return bool $currentVersion
  731. */
  732. public function getCurrentVersion()
  733. {
  734. $this->createMigrationTable();
  735. $result = $this->_connection->fetchColumn("SELECT version FROM " . $this->_migrationsTableName . " ORDER BY version DESC LIMIT 1");
  736. return $result !== false ? (string) $result : '0';
  737. }
  738. /**
  739. * Returns the total number of executed migration versions
  740. *
  741. * @return integer $count
  742. */
  743. public function getNumberOfExecutedMigrations()
  744. {
  745. $this->createMigrationTable();
  746. $result = $this->_connection->fetchColumn("SELECT COUNT(version) FROM " . $this->_migrationsTableName);
  747. return $result !== false ? $result : 0;
  748. }
  749. /**
  750. * Returns the total number of available migration versions
  751. *
  752. * @return integer $count
  753. */
  754. public function getNumberOfAvailableMigrations()
  755. {
  756. return count($this->_migrations);
  757. }
  758. /**
  759. * Returns the latest available migration version.
  760. *
  761. * @return string $version The version string in the format YYYYMMDDHHMMSS.
  762. */
  763. public function getLatestVersion()
  764. {
  765. $versions = array_keys($this->_migrations);
  766. $latest = end($versions);
  767. return $latest !== false ? (string) $latest : '0';
  768. }
  769. /**
  770. * Create the migration table to track migrations with.
  771. *
  772. * @return bool $created Whether or not the table was created.
  773. */
  774. public function createMigrationTable()
  775. {
  776. $this->validate();
  777. if ($this->_migrationTableCreated) {
  778. return false;
  779. }
  780. $schema = $this->_connection->getSchemaManager()->createSchema();
  781. if ( ! $schema->hasTable($this->_migrationsTableName)) {
  782. $columns = array(
  783. 'version' => new Column('version', Type::getType('string'), array('length' => 14)),
  784. );
  785. $table = new Table($this->_migrationsTableName, $columns);
  786. $table->setPrimaryKey(array('version'));
  787. $this->_connection->getSchemaManager()->createTable($table);
  788. $this->_migrationTableCreated = true;
  789. return true;
  790. }
  791. return false;
  792. }
  793. /**
  794. * Returns the array of migrations to executed based on the given direction
  795. * and target version number.
  796. *
  797. * @param string $direction The direction we are migrating.
  798. * @param string $to The version to migrate to.
  799. * @return array $migrations The array of migrations we can execute.
  800. */
  801. public function getMigrationsToExecute($direction, $to)
  802. {
  803. if ($direction === 'down') {
  804. $allVersions = array_reverse(array_keys($this->_migrations));
  805. $classes = array_reverse(array_values($this->_migrations));
  806. $allVersions = array_combine($allVersions, $classes);
  807. } else {
  808. $allVersions = $this->_migrations;
  809. }
  810. $versions = array();
  811. foreach ($allVersions as $version) {
  812. if ($this->_shouldExecuteMigration($direction, $version, $to)) {
  813. $versions[$version->getVersion()] = $version;
  814. }
  815. }
  816. return $versions;
  817. }
  818. /**
  819. * Check if we should execute a migration for a given direction and target
  820. * migration version.
  821. *
  822. * @param string $direction The direction we are migrating.
  823. * @param Version $version The Version instance to check.
  824. * @param string $to The version we are migrating to.
  825. * @return void
  826. */
  827. private function _shouldExecuteMigration($direction, Version $version, $to)
  828. {
  829. if ($direction === 'down') {
  830. if ( ! $this->hasVersionMigrated($version)) {
  831. return false;
  832. }
  833. return $version->getVersion() > $to ? true : false;
  834. } else if ($direction === 'up') {
  835. if ($this->hasVersionMigrated($version)) {
  836. return false;
  837. }
  838. return $version->getVersion() <= $to ? true : false;
  839. }
  840. }
  841. }<?php
  842. /*
  843. * $Id$
  844. *
  845. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  846. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  847. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  848. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  849. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  850. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  851. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  852. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  853. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  854. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  855. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  856. *
  857. * This software consists of voluntary contributions made by many individuals
  858. * and is licensed under the LGPL. For more information, see
  859. * <http://www.doctrine-project.org>.
  860. */
  861. namespace Doctrine\DBAL\Migrations\Configuration;
  862. /**
  863. * Load migration configuration information from a XML configuration file.
  864. *
  865. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  866. * @link www.doctrine-project.org
  867. * @since 2.0
  868. * @version $Revision$
  869. * @author Jonathan H. Wage <jonwage@gmail.com>
  870. */
  871. class XmlConfiguration extends AbstractFileConfiguration
  872. {
  873. /**
  874. * @inheritdoc
  875. */
  876. protected function _load($file)
  877. {
  878. $xml = simplexml_load_file($file);
  879. if (isset($xml->name)) {
  880. $this->setName((string) $xml->name);
  881. }
  882. if (isset($xml->table['name'])) {
  883. $this->setMigrationsTableName((string) $xml->table['name']);
  884. }
  885. if (isset($xml->{'migrations-namespace'})) {
  886. $this->setMigrationsNamespace((string) $xml->{'migrations-namespace'});
  887. }
  888. if (isset($xml->{'migrations-directory'})) {
  889. $migrationsDirectory = $this->_getDirectoryRelativeToFile($file, (string) $xml->{'migrations-directory'});
  890. $this->setMigrationsDirectory($migrationsDirectory);
  891. $this->registerMigrationsFromDirectory($migrationsDirectory);
  892. }
  893. if (isset($xml->migrations->migration)) {
  894. foreach ($xml->migrations->migration as $migration) {
  895. $this->registerMigration((string) $migration['version'], (string) $migration['class']);
  896. }
  897. }
  898. }
  899. }<?php
  900. /*
  901. * $Id$
  902. *
  903. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  904. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  905. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  906. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  907. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  908. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  909. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  910. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  911. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  912. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  913. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  914. *
  915. * This software consists of voluntary contributions made by many individuals
  916. * and is licensed under the LGPL. For more information, see
  917. * <http://www.doctrine-project.org>.
  918. */
  919. namespace Doctrine\DBAL\Migrations\Configuration;
  920. use Symfony\Component\Yaml\Yaml;
  921. /**
  922. * Load migration configuration information from a YAML configuration file.
  923. *
  924. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  925. * @link www.doctrine-project.org
  926. * @since 2.0
  927. * @version $Revision$
  928. * @author Jonathan H. Wage <jonwage@gmail.com>
  929. */
  930. class YamlConfiguration extends AbstractFileConfiguration
  931. {
  932. /**
  933. * @inheritdoc
  934. */
  935. protected function _load($file)
  936. {
  937. $array = Yaml::load($file);
  938. if (isset($array['name'])) {
  939. $this->setName($array['name']);
  940. }
  941. if (isset($array['table_name'])) {
  942. $this->setMigrationsTableName($array['table_name']);
  943. }
  944. if (isset($array['migrations_namespace'])) {
  945. $this->setMigrationsNamespace($array['migrations_namespace']);
  946. }
  947. if (isset($array['migrations_directory'])) {
  948. $migrationsDirectory = $this->_getDirectoryRelativeToFile($file, $array['migrations_directory']);
  949. $this->setMigrationsDirectory($migrationsDirectory);
  950. $this->registerMigrationsFromDirectory($migrationsDirectory);
  951. }
  952. if (isset($array['migrations']) && is_array($array['migrations'])) {
  953. foreach ($array['migrations'] as $migration) {
  954. $this->registerMigration($migration['version'], $migration['class']);
  955. }
  956. }
  957. }
  958. }
  959. <?php
  960. /*
  961. * $Id$
  962. *
  963. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  964. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  965. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  966. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  967. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  968. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  969. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  970. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  971. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  972. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  973. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  974. *
  975. * This software consists of voluntary contributions made by many individuals
  976. * and is licensed under the LGPL. For more information, see
  977. * <http://www.doctrine-project.org>.
  978. */
  979. namespace Doctrine\DBAL\Migrations\Configuration;
  980. use Doctrine\DBAL\Migrations\MigrationsException;
  981. /**
  982. * Abstract Migration Configuration class for loading configuration information
  983. * from a configuration file (xml or yml).
  984. *
  985. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  986. * @link www.doctrine-project.org
  987. * @since 2.0
  988. * @version $Revision$
  989. * @author Jonathan H. Wage <jonwage@gmail.com>
  990. */
  991. abstract class AbstractFileConfiguration extends Configuration
  992. {
  993. /** The configuration file used to load configuration information */
  994. private $_file;
  995. /** Whether or not the configuration file has been loaded yet or not */
  996. private $_loaded = false;
  997. /**
  998. * Load the information from the passed configuration file
  999. *
  1000. * @param string $file The path to the configuration file
  1001. * @return void
  1002. * @throws MigrationException $exception Throws exception if configuration file was already loaded
  1003. */
  1004. public function load($file)
  1005. {
  1006. if ($this->_loaded) {
  1007. throw MigrationsException::configurationFileAlreadyLoaded();
  1008. }
  1009. if (file_exists($path = getcwd() . '/' . $file)) {
  1010. $file = $path;
  1011. }
  1012. $this->_file = $file;
  1013. $this->_load($file);
  1014. $this->_loaded = true;
  1015. }
  1016. protected function _getDirectoryRelativeToFile($file, $input)
  1017. {
  1018. $path = realpath(dirname($file) . '/' . $input);
  1019. if ($path !== false) {
  1020. $directory = $path;
  1021. } else {
  1022. $directory = $input;
  1023. }
  1024. return $directory;
  1025. }
  1026. public function getFile()
  1027. {
  1028. return $this->_file;
  1029. }
  1030. /**
  1031. * Abstract method that each file configuration driver must implement to
  1032. * load the given configuration file whether it be xml, yaml, etc. or something
  1033. * else.
  1034. *
  1035. * @param string $file The path to a configuration file.
  1036. */
  1037. abstract protected function _load($file);
  1038. }<?php
  1039. /*
  1040. * $Id$
  1041. *
  1042. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1043. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1044. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1045. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1046. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1047. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1048. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1049. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1050. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1051. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1052. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1053. *
  1054. * This software consists of voluntary contributions made by many individuals
  1055. * and is licensed under the LGPL. For more information, see
  1056. * <http://www.doctrine-project.org>.
  1057. */
  1058. namespace Doctrine\DBAL\Migrations;
  1059. /**
  1060. * Simple class for outputting information from migrations.
  1061. *
  1062. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1063. * @link www.doctrine-project.org
  1064. * @since 2.0
  1065. * @version $Revision$
  1066. * @author Jonathan H. Wage <jonwage@gmail.com>
  1067. */
  1068. class OutputWriter
  1069. {
  1070. private $_closure;
  1071. public function __construct(\Closure $closure = null)
  1072. {
  1073. if ($closure === null) {
  1074. $closure = function($message) {};
  1075. }
  1076. $this->_closure = $closure;
  1077. }
  1078. /**
  1079. * Write output using the configured closure.
  1080. *
  1081. * @param string $message The message to write.
  1082. */
  1083. public function write($message)
  1084. {
  1085. $closure = $this->_closure;
  1086. $closure($message);
  1087. }
  1088. }<?php
  1089. /*
  1090. * $Id$
  1091. *
  1092. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1093. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1094. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1095. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1096. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1097. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1098. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1099. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1100. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1101. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1102. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1103. *
  1104. * This software consists of voluntary contributions made by many individuals
  1105. * and is licensed under the LGPL. For more information, see
  1106. * <http://www.doctrine-project.org>.
  1107. */
  1108. namespace Doctrine\DBAL\Migrations;
  1109. use Doctrine\DBAL\Migrations\Configuration\Configuration,
  1110. Doctrine\DBAL\Schema\Schema;
  1111. /**
  1112. * Class which wraps a migration version and allows execution of the
  1113. * individual migration version up or down method.
  1114. *
  1115. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1116. * @link www.doctrine-project.org
  1117. * @since 2.0
  1118. * @version $Revision$
  1119. * @author Jonathan H. Wage <jonwage@gmail.com>
  1120. */
  1121. class Version
  1122. {
  1123. const STATE_NONE = 0;
  1124. const STATE_PRE = 1;
  1125. const STATE_EXEC = 2;
  1126. const STATE_POST = 3;
  1127. /**
  1128. * The Migrations Configuration instance for this migration
  1129. *
  1130. * @var Configuration
  1131. */
  1132. private $_configuration;
  1133. /**
  1134. * The OutputWriter object instance used for outputting information
  1135. *
  1136. * @var OutputWriter
  1137. */
  1138. private $_outputWriter;
  1139. /**
  1140. * The version in timestamp format (YYYYMMDDHHMMSS)
  1141. *
  1142. * @param int
  1143. */
  1144. private $_version;
  1145. /**
  1146. * @var AbstractSchemaManager
  1147. */
  1148. private $_sm;
  1149. /**
  1150. * @var AbstractPlatform
  1151. */
  1152. private $_platform;
  1153. /**
  1154. * The migration instance for this version
  1155. *
  1156. * @var AbstractMigration
  1157. */
  1158. private $_migration;
  1159. /**
  1160. * @var Connection
  1161. */
  1162. private $_connection;
  1163. /**
  1164. * @var string
  1165. */
  1166. private $_class;
  1167. /** The array of collected SQL statements for this version */
  1168. private $_sql = array();
  1169. /** The time in seconds that this migration version took to execute */
  1170. private $_time;
  1171. /**
  1172. * @var int
  1173. */
  1174. private $_state = self::STATE_NONE;
  1175. public function __construct(Configuration $configuration, $version, $class)
  1176. {
  1177. $this->_configuration = $configuration;
  1178. $this->_outputWriter = $configuration->getOutputWriter();
  1179. $this->_version = $version;
  1180. $this->_class = $class;
  1181. $this->_connection = $configuration->getConnection();
  1182. $this->_sm = $this->_connection->getSchemaManager();
  1183. $this->_platform = $this->_connection->getDatabasePlatform();
  1184. $this->_migration = new $class($this);
  1185. }
  1186. /**
  1187. * Returns the string version in the format YYYYMMDDHHMMSS
  1188. *
  1189. * @return string $version
  1190. */
  1191. public function getVersion()
  1192. {
  1193. return $this->_version;
  1194. }
  1195. /**
  1196. * Returns the Migrations Configuration object instance
  1197. *
  1198. * @return Configuration $configuration
  1199. */
  1200. public function getConfiguration()
  1201. {
  1202. return $this->_configuration;
  1203. }
  1204. /**
  1205. * Check if this version has been migrated or not.
  1206. *
  1207. * @param bool $bool
  1208. * @return mixed
  1209. */
  1210. public function isMigrated()
  1211. {
  1212. return $this->_configuration->hasVersionMigrated($this);
  1213. }
  1214. public function markMigrated()
  1215. {
  1216. $this->_configuration->createMigrationTable();
  1217. $this->_connection->executeQuery("INSERT INTO " . $this->_configuration->getMigrationsTableName() . " (version) VALUES (?)", array($this->_version));
  1218. }
  1219. public function markNotMigrated()
  1220. {
  1221. $this->_configuration->createMigrationTable();
  1222. $this->_connection->executeQuery("DELETE FROM " . $this->_configuration->getMigrationsTableName() . " WHERE version = '$this->_version'");
  1223. }
  1224. /**
  1225. * Add some SQL queries to this versions migration
  1226. *
  1227. * @param mixed $sql
  1228. * @return void
  1229. */
  1230. public function addSql($sql)
  1231. {
  1232. if (is_array($sql)) {
  1233. foreach ($sql as $query) {
  1234. $this->_sql[] = $query;
  1235. }
  1236. } else {
  1237. $this->_sql[] = $sql;
  1238. }
  1239. }
  1240. /**
  1241. * Write a migration SQL file to the given path
  1242. *
  1243. * @param string $path The path to write the migration SQL file.
  1244. * @param string $direction The direction to execute.
  1245. * @return bool $written
  1246. */
  1247. public function writeSqlFile($path, $direction = 'up')
  1248. {
  1249. $queries = $this->execute($direction, true);
  1250. $string = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:m:s'));
  1251. $string .= "\n# Version " . $this->_version . "\n";
  1252. foreach ($queries as $query) {
  1253. $string .= $query . ";\n";
  1254. }
  1255. if (is_dir($path)) {
  1256. $path = realpath($path);
  1257. $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
  1258. }
  1259. $this->_outputWriter->write("\n".sprintf('Writing migration file to "<info>%s</info>"', $path));
  1260. return file_put_contents($path, $string);
  1261. }
  1262. /**
  1263. * Execute this migration version up or down and and return the SQL.
  1264. *
  1265. * @param string $direction The direction to execute the migration.
  1266. * @param string $dryRun Whether to not actually execute the migration SQL and just do a dry run.
  1267. * @return array $sql
  1268. * @throws Exception when migration fails
  1269. */
  1270. public function execute($direction, $dryRun = false)
  1271. {
  1272. $this->_sql = array();
  1273. $this->_connection->beginTransaction();
  1274. try {
  1275. $start = microtime(true);
  1276. $this->_state = self::STATE_PRE;
  1277. $fromSchema = $this->_sm->createSchema();
  1278. $this->_migration->{'pre' . ucfirst($direction)}($fromSchema);
  1279. if ($direction === 'up') {
  1280. $this->_outputWriter->write("\n" . sprintf(' <info>++</info> migrating <comment>%s</comment>', $this->_version) . "\n");
  1281. } else {
  1282. $this->_outputWriter->write("\n" . sprintf(' <info>--</info> reverting <comment>%s</comment>', $this->_version) . "\n");
  1283. }
  1284. $this->_state = self::STATE_EXEC;
  1285. $toSchema = clone $fromSchema;
  1286. $this->_migration->$direction($toSchema);
  1287. $this->addSql($fromSchema->getMigrateToSql($toSchema, $this->_platform));
  1288. if ($dryRun === false) {
  1289. if ($this->_sql) {
  1290. $count = count($this->_sql);
  1291. foreach ($this->_sql as $query) {
  1292. $this->_outputWriter->write(' <comment>-></comment> ' . $query);
  1293. $this->_connection->executeQuery($query);
  1294. }
  1295. if ($direction === 'up') {
  1296. $this->markMigrated();
  1297. } else {
  1298. $this->markNotMigrated();
  1299. }
  1300. } else {
  1301. $this->_outputWriter->write(sprintf('<error>Migration %s was executed but did not result in any SQL statements.</error>', $this->_version));
  1302. }
  1303. } else {
  1304. foreach ($this->_sql as $query) {
  1305. $this->_outputWriter->write(' <comment>-></comment> ' . $query);
  1306. }
  1307. }
  1308. $this->_state = self::STATE_POST;
  1309. $this->_migration->{'post' . ucfirst($direction)}($toSchema);
  1310. $end = microtime(true);
  1311. $this->_time = round($end - $start, 2);
  1312. if ($direction === 'up') {
  1313. $this->_outputWriter->write(sprintf("\n <info>++</info> migrated (%ss)", $this->_time));
  1314. } else {
  1315. $this->_outputWriter->write(sprintf("\n <info>--</info> reverted (%ss)", $this->_time));
  1316. }
  1317. $this->_connection->commit();
  1318. return $this->_sql;
  1319. } catch(SkipMigrationException $e) {
  1320. $this->_connection->rollback();
  1321. // now mark it as migrated
  1322. if ($direction === 'up') {
  1323. $this->markMigrated();
  1324. } else {
  1325. $this->markNotMigrated();
  1326. }
  1327. $this->_outputWriter->write(sprintf("\n <info>SS</info> skipped (Reason: %s)", $e->getMessage()));
  1328. } catch (\Exception $e) {
  1329. $this->_outputWriter->write(sprintf(
  1330. '<error>Migration %s failed during %s. Error %s</error>',
  1331. $this->_version, $this->getExecutionState(), $e->getMessage()
  1332. ));
  1333. $this->_connection->rollback();
  1334. $this->_state = self::STATE_NONE;
  1335. throw $e;
  1336. }
  1337. $this->_state = self::STATE_NONE;
  1338. }
  1339. public function getExecutionState()
  1340. {
  1341. switch($this->_state) {
  1342. case self::STATE_PRE:
  1343. return 'Pre-Checks';
  1344. case self::STATE_POST:
  1345. return 'Post-Checks';
  1346. case self::STATE_EXEC:
  1347. return 'Execution';
  1348. default:
  1349. return 'No State';
  1350. }
  1351. }
  1352. /**
  1353. * Returns the time this migration version took to execute
  1354. *
  1355. * @return integer $time The time this migration version took to execute
  1356. */
  1357. public function getTime()
  1358. {
  1359. return $this->_time;
  1360. }
  1361. public function __toString()
  1362. {
  1363. return $this->_version;
  1364. }
  1365. }
  1366. <?php
  1367. /*
  1368. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1369. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1370. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1371. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1372. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1373. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1374. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1375. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1376. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1377. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1378. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1379. *
  1380. * This software consists of voluntary contributions made by many individuals
  1381. * and is licensed under the LGPL. For more information, see
  1382. * <http://www.doctrine-project.org>.
  1383. */
  1384. namespace Doctrine\DBAL\Migrations;
  1385. class MigrationsVersion
  1386. {
  1387. const VERSION = '2.0.0-DEV';
  1388. }<?php
  1389. /*
  1390. * $Id$
  1391. *
  1392. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1393. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1394. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1395. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1396. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1397. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1398. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1399. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1400. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1401. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1402. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1403. *
  1404. * This software consists of voluntary contributions made by many individuals
  1405. * and is licensed under the LGPL. For more information, see
  1406. * <http://www.doctrine-project.org>.
  1407. */
  1408. namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
  1409. use Symfony\Component\Console\Input\InputInterface,
  1410. Symfony\Component\Console\Output\OutputInterface,
  1411. Symfony\Component\Console\Input\InputArgument,
  1412. Symfony\Component\Console\Input\InputOption,
  1413. Doctrine\ORM\Tools\SchemaTool,
  1414. Doctrine\DBAL\Migrations\Configuration\Configuration;
  1415. /**
  1416. * Command for generate migration classes by comparing your current database schema
  1417. * to your mapping information.
  1418. *
  1419. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1420. * @link www.doctrine-project.org
  1421. * @since 2.0
  1422. * @version $Revision$
  1423. * @author Jonathan Wage <jonwage@gmail.com>
  1424. */
  1425. class DiffCommand extends GenerateCommand
  1426. {
  1427. protected function configure()
  1428. {
  1429. parent::configure();
  1430. $this
  1431. ->setName('migrations:diff')
  1432. ->setDescription('Generate a migration by comparing your current database to your mapping information.')
  1433. ->setHelp(<<<EOT
  1434. The <info>%command.name%</info> command generates a migration by comparing your current database to your mapping information:
  1435. <info>%command.full_name%</info>
  1436. You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
  1437. <info>%command.full_name% --editor-cmd=mate</info>
  1438. EOT
  1439. );
  1440. }
  1441. public function execute(InputInterface $input, OutputInterface $output)
  1442. {
  1443. $configuration = $this->_getMigrationConfiguration($input, $output);
  1444. $em = $this->getHelper('em')->getEntityManager();
  1445. $conn = $em->getConnection();
  1446. $platform = $conn->getDatabasePlatform();
  1447. $metadata = $em->getMetadataFactory()->getAllMetadata();
  1448. if (empty($metadata)) {
  1449. $output->writeln('No mapping information to process.', 'ERROR');
  1450. return;
  1451. }
  1452. $tool = new SchemaTool($em);
  1453. $fromSchema = $conn->getSchemaManager()->createSchema();
  1454. $toSchema = $tool->getSchemaFromMetadata($metadata);
  1455. $up = $this->_buildCodeFromSql($configuration, $fromSchema->getMigrateToSql($toSchema, $platform));
  1456. $down = $this->_buildCodeFromSql($configuration, $fromSchema->getMigrateFromSql($toSchema, $platform));
  1457. if ( ! $up && ! $down) {
  1458. $output->writeln('No changes detected in your mapping information.', 'ERROR');
  1459. return;
  1460. }
  1461. $version = date('YmdHis');
  1462. $path = $this->_generateMigration($configuration, $input, $version, $up, $down);
  1463. $output->writeln(sprintf('Generated new migration class to "<info>%s</info>" from schema differences.', $path));
  1464. }
  1465. private function _buildCodeFromSql(Configuration $configuration, array $sql)
  1466. {
  1467. $code = array();
  1468. foreach ($sql as $query) {
  1469. if (strpos($query, $configuration->getMigrationsTableName()) !== false) {
  1470. continue;
  1471. }
  1472. $code[] = "\$this->_addSql('" . $query . "');";
  1473. }
  1474. return implode("\n", $code);
  1475. }
  1476. }
  1477. <?php
  1478. /*
  1479. * $Id$
  1480. *
  1481. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1482. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1483. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1484. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1485. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1486. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1487. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1488. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1489. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1490. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1491. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1492. *
  1493. * This software consists of voluntary contributions made by many individuals
  1494. * and is licensed under the LGPL. For more information, see
  1495. * <http://www.doctrine-project.org>.
  1496. */
  1497. namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
  1498. use Symfony\Component\Console\Command\Command,
  1499. Symfony\Component\Console\Input\InputInterface,
  1500. Symfony\Component\Console\Output\OutputInterface,
  1501. Symfony\Component\Console\Input\InputOption,
  1502. Doctrine\DBAL\Migrations\Migration,
  1503. Doctrine\DBAL\Migrations\MigrationException,
  1504. Doctrine\DBAL\Migrations\OutputWriter,
  1505. Doctrine\DBAL\Migrations\Configuration\Configuration,
  1506. Doctrine\DBAL\Migrations\Configuration\YamlConfiguration,
  1507. Doctrine\DBAL\Migrations\Configuration\XmlConfiguration;
  1508. /**
  1509. * CLI Command for adding and deleting migration versions from the version table.
  1510. *
  1511. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1512. * @link www.doctrine-project.org
  1513. * @since 2.0
  1514. * @version $Revision$
  1515. * @author Jonathan Wage <jonwage@gmail.com>
  1516. */
  1517. abstract class AbstractCommand extends Command
  1518. {
  1519. protected $_configuration;
  1520. protected function configure()
  1521. {
  1522. $this->addOption('configuration', null, InputOption::PARAMETER_OPTIONAL, 'The path to a migrations configuration file.');
  1523. $this->addOption('db-configuration', null, InputOption::PARAMETER_OPTIONAL, 'The path to a database connection configuration file.');
  1524. }
  1525. protected function _outputHeader(Configuration $configuration, OutputInterface $output)
  1526. {
  1527. $name = $configuration->getName();
  1528. $name = $name ? $name : 'Doctrine Database Migrations';
  1529. $name = str_repeat(' ', 20) . $name . str_repeat(' ', 20);
  1530. $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
  1531. $output->writeln('<question>' . $name . '</question>');
  1532. $output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>');
  1533. $output->writeln('');
  1534. }
  1535. /**
  1536. * @param InputInterface $input
  1537. * @param OutputInterface $output
  1538. * @return Configuration
  1539. */
  1540. protected function _getMigrationConfiguration(InputInterface $input, OutputInterface $output)
  1541. {
  1542. if ( ! $this->_configuration) {
  1543. $outputWriter = new OutputWriter(function($message) use ($output) {
  1544. return $output->writeln($message);
  1545. });
  1546. if ($this->application->getHelperSet()->has('db')) {
  1547. $conn = $this->getHelper('db')->getConnection();
  1548. } else if($input->getOption('db-configuration')) {
  1549. if (!file_exists($input->getOption('db-configuration'))) {
  1550. throw new \InvalidArgumentException("The specified connection file is a valid file.");
  1551. }
  1552. $params = include($input->getOption('db-configuration'));
  1553. if (!is_array($params)) {
  1554. throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.');
  1555. }
  1556. $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
  1557. } else if (file_exists('migrations-db.php')) {
  1558. $params = include("migrations-db.php");
  1559. if (!is_array($params)) {
  1560. throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.');
  1561. }
  1562. $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
  1563. } else {
  1564. throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.');
  1565. }
  1566. if ($input->getOption('configuration')) {
  1567. $info = pathinfo($input->getOption('configuration'));
  1568. $class = $info['extension'] === 'xml' ? 'Doctrine\DBAL\Migrations\Configuration\XmlConfiguration' : 'Doctrine\DBAL\Migrations\Configuration\YamlConfiguration';
  1569. $configuration = new $class($conn, $outputWriter);
  1570. $configuration->load($input->getOption('configuration'));
  1571. } else if (file_exists('migrations.xml')) {
  1572. $configuration = new XmlConfiguration($conn, $outputWriter);
  1573. $configuration->load('migrations.xml');
  1574. } else if (file_exists('migrations.yml')) {
  1575. $configuration = new YamlConfiguration($conn, $outputWriter);
  1576. $configuration->load('migrations.yml');
  1577. } else {
  1578. $configuration = new Configuration($conn, $outputWriter);
  1579. }
  1580. $this->_configuration = $configuration;
  1581. }
  1582. return $this->_configuration;
  1583. }
  1584. }
  1585. <?php
  1586. /*
  1587. * $Id$
  1588. *
  1589. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1590. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1591. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1592. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1593. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1594. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1595. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1596. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1597. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1598. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1599. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1600. *
  1601. * This software consists of voluntary contributions made by many individuals
  1602. * and is licensed under the LGPL. For more information, see
  1603. * <http://www.doctrine-project.org>.
  1604. */
  1605. namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
  1606. use Symfony\Component\Console\Input\InputInterface,
  1607. Symfony\Component\Console\Output\OutputInterface,
  1608. Symfony\Component\Console\Input\InputArgument,
  1609. Symfony\Component\Console\Input\InputOption,
  1610. Doctrine\DBAL\Migrations\MigrationException,
  1611. Doctrine\DBAL\Migrations\Configuration\Configuration;
  1612. /**
  1613. * Command for generating new blank migration classes
  1614. *
  1615. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1616. * @link www.doctrine-project.org
  1617. * @since 2.0
  1618. * @version $Revision$
  1619. * @author Jonathan Wage <jonwage@gmail.com>
  1620. */
  1621. class GenerateCommand extends AbstractCommand
  1622. {
  1623. private static $_template =
  1624. '<?php
  1625. namespace <namespace>;
  1626. use Doctrine\DBAL\Migrations\AbstractMigration,
  1627. Doctrine\DBAL\Schema\Schema;
  1628. class Version<version> extends AbstractMigration
  1629. {
  1630. public function up(Schema $schema)
  1631. {
  1632. <up>
  1633. }
  1634. public function down(Schema $schema)
  1635. {
  1636. <down>
  1637. }
  1638. }';
  1639. protected function configure()
  1640. {
  1641. $this
  1642. ->setName('migrations:generate')
  1643. ->setDescription('Generate a blank migration class.')
  1644. ->addOption('editor-cmd', null, InputOption::PARAMETER_OPTIONAL, 'Open file with this command upon creation.')
  1645. ->setHelp(<<<EOT
  1646. The <info>%command.name%</info> command generates a blank migration class:
  1647. <info>%command.full_name%</info>
  1648. You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
  1649. <info>%command.full_name% --editor-cmd=mate</info>
  1650. EOT
  1651. );
  1652. parent::configure();
  1653. }
  1654. public function execute(InputInterface $input, OutputInterface $output)
  1655. {
  1656. $configuration = $this->_getMigrationConfiguration($input, $output);
  1657. $version = date('YmdHis');
  1658. $path = $this->_generateMigration($configuration, $input, $version);
  1659. $output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path));
  1660. }
  1661. protected function _generateMigration(Configuration $configuration, InputInterface $input, $version, $up = null, $down = null)
  1662. {
  1663. $placeHolders = array(
  1664. '<namespace>',
  1665. '<version>',
  1666. '<up>',
  1667. '<down>'
  1668. );
  1669. $replacements = array(
  1670. $configuration->getMigrationsNamespace(),
  1671. $version,
  1672. $up ? " " . implode("\n ", explode("\n", $up)) : null,
  1673. $down ? " " . implode("\n ", explode("\n", $down)) : null
  1674. );
  1675. $code = str_replace($placeHolders, $replacements, self::$_template);
  1676. $dir = $configuration->getMigrationsDirectory();
  1677. $dir = $dir ? $dir : getcwd();
  1678. $dir = rtrim($dir, '/');
  1679. $path = $dir . '/Version' . $version . '.php';
  1680. file_put_contents($path, $code);
  1681. if ($editorCmd = $input->getOption('editor-cmd'))
  1682. {
  1683. shell_exec($editorCmd . ' ' . escapeshellarg($path));
  1684. }
  1685. return $path;
  1686. }
  1687. }
  1688. <?php
  1689. /*
  1690. * $Id$
  1691. *
  1692. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1693. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1694. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1695. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1696. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1697. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1698. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1699. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1700. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1701. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1702. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1703. *
  1704. * This software consists of voluntary contributions made by many individuals
  1705. * and is licensed under the LGPL. For more information, see
  1706. * <http://www.doctrine-project.org>.
  1707. */
  1708. namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
  1709. use Symfony\Component\Console\Input\InputInterface,
  1710. Symfony\Component\Console\Output\OutputInterface,
  1711. Symfony\Component\Console\Input\InputArgument,
  1712. Symfony\Component\Console\Input\InputOption,
  1713. Doctrine\DBAL\Migrations\Migration,
  1714. Doctrine\DBAL\Migrations\MigrationException,
  1715. Doctrine\DBAL\Migrations\Configuration\Configuration,
  1716. Doctrine\DBAL\Migrations\Configuration\YamlConfiguration,
  1717. Doctrine\DBAL\Migrations\Configuration\XmlConfiguration;
  1718. /**
  1719. * Command for manually adding and deleting migration versions from the version table.
  1720. *
  1721. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1722. * @link www.doctrine-project.org
  1723. * @since 2.0
  1724. * @version $Revision$
  1725. * @author Jonathan Wage <jonwage@gmail.com>
  1726. */
  1727. class VersionCommand extends AbstractCommand
  1728. {
  1729. protected function configure()
  1730. {
  1731. $this
  1732. ->setName('migrations:version')
  1733. ->setDescription('Manually add and delete migration versions from the version table.')
  1734. ->addArgument('version', InputArgument::REQUIRED, 'The version to add or delete.', null)
  1735. ->addOption('add', null, InputOption::PARAMETER_NONE, 'Add the specified version.')
  1736. ->addOption('delete', null, InputOption::PARAMETER_NONE, 'Delete the specified version.')
  1737. ->setHelp(<<<EOT
  1738. The <info>%command.name%</info> command allows you to manually add and delete migration versions from the version table:
  1739. <info>%command.full_name% YYYYMMDDHHMMSS --add</info>
  1740. If you want to delete a version you can use the <comment>--delete</comment> option:
  1741. <info>%command.full_name% YYYYMMDDHHMMSS --delete</info>
  1742. EOT
  1743. );
  1744. parent::configure();
  1745. }
  1746. public function execute(InputInterface $input, OutputInterface $output)
  1747. {
  1748. $configuration = $this->_getMigrationConfiguration($input, $output);
  1749. $migration = new Migration($configuration);
  1750. if ($input->getOption('add') === false && $input->getOption('delete') === false) {
  1751. throw new \InvalidArgumentException('You must specify whether you want to --add or --delete the specified version.');
  1752. }
  1753. $version = $input->getArgument('version');
  1754. $markMigrated = $input->getOption('add') ? true : false;
  1755. if ( ! $configuration->hasVersion($version)) {
  1756. throw MigrationException::unknownMigrationVersion($version);
  1757. }
  1758. $version = $configuration->getVersion($version);
  1759. if ($markMigrated && $configuration->hasVersionMigrated($version)) {
  1760. throw new \InvalidArgumentException(sprintf('The version "%s" already exists in the version table.', $version));
  1761. }
  1762. if ( ! $markMigrated && ! $configuration->hasVersionMigrated($version)) {
  1763. throw new \InvalidArgumentException(sprintf('The version "%s" does not exists in the version table.', $version));
  1764. }
  1765. if ($markMigrated) {
  1766. $version->markMigrated();
  1767. } else {
  1768. $version->markNotMigrated();
  1769. }
  1770. }
  1771. }
  1772. <?php
  1773. /*
  1774. * $Id$
  1775. *
  1776. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1777. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1778. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1779. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1780. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1781. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1782. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1783. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1784. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1785. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1786. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1787. *
  1788. * This software consists of voluntary contributions made by many individuals
  1789. * and is licensed under the LGPL. For more information, see
  1790. * <http://www.doctrine-project.org>.
  1791. */
  1792. namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
  1793. use Symfony\Component\Console\Input\InputInterface,
  1794. Symfony\Component\Console\Output\OutputInterface,
  1795. Symfony\Component\Console\Input\InputArgument,
  1796. Symfony\Component\Console\Input\InputOption,
  1797. Doctrine\DBAL\Migrations\Migration,
  1798. Doctrine\DBAL\Migrations\Configuration\Configuration,
  1799. Doctrine\DBAL\Migrations\Configuration\YamlConfiguration,
  1800. Doctrine\DBAL\Migrations\Configuration\XmlConfiguration;
  1801. /**
  1802. * Command for executing single migrations up or down manually.
  1803. *
  1804. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1805. * @link www.doctrine-project.org
  1806. * @since 2.0
  1807. * @version $Revision$
  1808. * @author Jonathan Wage <jonwage@gmail.com>
  1809. */
  1810. class ExecuteCommand extends AbstractCommand
  1811. {
  1812. protected function configure()
  1813. {
  1814. $this
  1815. ->setName('migrations:execute')
  1816. ->setDescription('Execute a single migration version up or down manually.')
  1817. ->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null)
  1818. ->addOption('write-sql', null, InputOption::PARAMETER_NONE, 'The path to output the migration SQL file instead of executing it.')
  1819. ->addOption('dry-run', null, InputOption::PARAMETER_NONE, 'Execute the migration as a dry run.')
  1820. ->addOption('up', null, InputOption::PARAMETER_NONE, 'Execute the migration down.')
  1821. ->addOption('down', null, InputOption::PARAMETER_NONE, 'Execute the migration down.')
  1822. ->setHelp(<<<EOT
  1823. The <info>%command.name%</info> command executes a single migration version up or down manually:
  1824. <info>%command.full_name% YYYYMMDDHHMMSS</info>
  1825. If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up:
  1826. <info>%command.full_name% YYYYMMDDHHMMSS --down</info>
  1827. You can also execute the migration as a <comment>--dry-run</comment>:
  1828. <info>%command.full_name% YYYYMMDDHHMMSS --dry-run</info>
  1829. Or you can output the would be executed SQL statements to a file with <comment>--write-sql</comment>:
  1830. <info>%command.full_name% YYYYMMDDHHMMSS --write-sql</info>
  1831. EOT
  1832. );
  1833. parent::configure();
  1834. }
  1835. public function execute(InputInterface $input, OutputInterface $output)
  1836. {
  1837. $version = $input->getArgument('version');
  1838. $direction = $input->getOption('down') ? 'down' : 'up';
  1839. $configuration = $this->_getMigrationConfiguration($input, $output);
  1840. $version = $configuration->getVersion($version);
  1841. if ($path = $input->getOption('write-sql')) {
  1842. $path = is_bool($path) ? getcwd() : $path;
  1843. $version->writeSqlFile($path, $direction);
  1844. } else {
  1845. $confirmation = $this->getHelper('dialog')->askConfirmation($output, '<question>WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)</question>', 'y');
  1846. if ($confirmation === true) {
  1847. $version->execute($direction, $input->getOption('dry-run') ? true : false);
  1848. } else {
  1849. $output->writeln('<error>Migration cancelled!</error>');
  1850. }
  1851. }
  1852. }
  1853. }
  1854. <?php
  1855. /*
  1856. * $Id$
  1857. *
  1858. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1859. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1860. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1861. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1862. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1863. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1864. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1865. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1866. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1867. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1868. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1869. *
  1870. * This software consists of voluntary contributions made by many individuals
  1871. * and is licensed under the LGPL. For more information, see
  1872. * <http://www.doctrine-project.org>.
  1873. */
  1874. namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
  1875. use Symfony\Component\Console\Input\InputInterface,
  1876. Symfony\Component\Console\Output\OutputInterface,
  1877. Symfony\Component\Console\Input\InputArgument,
  1878. Symfony\Component\Console\Input\InputOption,
  1879. Doctrine\DBAL\Migrations\Migration,
  1880. Doctrine\DBAL\Migrations\MigrationException,
  1881. Doctrine\DBAL\Migrations\Configuration\Configuration,
  1882. Doctrine\DBAL\Migrations\Configuration\YamlConfiguration,
  1883. Doctrine\DBAL\Migrations\Configuration\XmlConfiguration;
  1884. /**
  1885. * Command to view the status of a set of migrations.
  1886. *
  1887. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1888. * @link www.doctrine-project.org
  1889. * @since 2.0
  1890. * @version $Revision$
  1891. * @author Jonathan Wage <jonwage@gmail.com>
  1892. */
  1893. class StatusCommand extends AbstractCommand
  1894. {
  1895. protected function configure()
  1896. {
  1897. $this
  1898. ->setName('migrations:status')
  1899. ->setDescription('View the status of a set of migrations.')
  1900. ->addOption('show-versions', null, InputOption::PARAMETER_NONE, 'This will display a list of all available migrations and their status')
  1901. ->setHelp(<<<EOT
  1902. The <info>%command.name%</info> command outputs the status of a set of migrations:
  1903. <info>%command.full_name%</info>
  1904. You can output a list of all available migrations and their status with <comment>--show-versions</comment>:
  1905. <info>%command.full_name% --show-versions</info>
  1906. EOT
  1907. );
  1908. parent::configure();
  1909. }
  1910. public function execute(InputInterface $input, OutputInterface $output)
  1911. {
  1912. $configuration = $this->_getMigrationConfiguration($input, $output);
  1913. $currentVersion = $configuration->getCurrentVersion();
  1914. if ($currentVersion) {
  1915. $currentVersionFormatted = $configuration->formatVersion($currentVersion) . ' (<comment>'.$currentVersion.'</comment>)';
  1916. } else {
  1917. $currentVersionFormatted = 0;
  1918. }
  1919. $latestVersion = $configuration->getLatestVersion();
  1920. if ($latestVersion) {
  1921. $latestVersionFormatted = $configuration->formatVersion($latestVersion) . ' (<comment>'.$latestVersion.'</comment>)';
  1922. } else {
  1923. $latestVersionFormatted = 0;
  1924. }
  1925. $executedMigrations = $configuration->getNumberOfExecutedMigrations();
  1926. $availableMigrations = $configuration->getNumberOfAvailableMigrations();
  1927. $newMigrations = $availableMigrations - $executedMigrations;
  1928. $output->writeln("\n <info>==</info> Configuration\n");
  1929. $info = array(
  1930. 'Name' => $configuration->getName() ? $configuration->getName() : 'Doctrine Database Migrations',
  1931. 'Database Driver' => $configuration->getConnection()->getDriver()->getName(),
  1932. 'Database Name' => $configuration->getConnection()->getDatabase(),
  1933. 'Configuration Source' => $configuration instanceof \Doctrine\DBAL\Migrations\Configuration\AbstractFileConfiguration ? $configuration->getFile() : 'manually configured',
  1934. 'Version Table Name' => $configuration->getMigrationsTableName(),
  1935. 'Migrations Namespace' => $configuration->getMigrationsNamespace(),
  1936. 'Migrations Directory' => $configuration->getMigrationsDirectory(),
  1937. 'Current Version' => $currentVersionFormatted,
  1938. 'Latest Version' => $latestVersionFormatted,
  1939. 'Executed Migrations' => $executedMigrations,
  1940. 'Available Migrations' => $availableMigrations,
  1941. 'New Migrations' => $newMigrations > 0 ? '<question>' . $newMigrations . '</question>' : $newMigrations
  1942. );
  1943. foreach ($info as $name => $value) {
  1944. $output->writeln(' <comment>>></comment> ' . $name . ': ' . str_repeat(' ', 50 - strlen($name)) . $value);
  1945. }
  1946. $showVersions = $input->getOption('show-versions') ? true : false;
  1947. if ($showVersions === true) {
  1948. if ($migrations = $configuration->getMigrations()) {
  1949. $output->writeln("\n <info>==</info> Migration Versions\n");
  1950. foreach ($migrations as $version) {
  1951. $isMigrated = $version->isMigrated();
  1952. $status = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
  1953. $output->writeln(' <comment>>></comment> ' . $configuration->formatVersion($version->getVersion()) . ' (<comment>' . $version->getVersion() . '</comment>)' . str_repeat(' ', 30 - strlen($name)) . $status);
  1954. }
  1955. }
  1956. }
  1957. }
  1958. }
  1959. <?php
  1960. /*
  1961. * $Id$
  1962. *
  1963. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1964. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1965. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1966. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1967. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1968. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1969. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1970. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1971. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1972. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  1973. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1974. *
  1975. * This software consists of voluntary contributions made by many individuals
  1976. * and is licensed under the LGPL. For more information, see
  1977. * <http://www.doctrine-project.org>.
  1978. */
  1979. namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
  1980. use Symfony\Component\Console\Input\InputInterface,
  1981. Symfony\Component\Console\Output\OutputInterface,
  1982. Symfony\Component\Console\Input\InputArgument,
  1983. Symfony\Component\Console\Input\InputOption,
  1984. Doctrine\DBAL\Migrations\Migration,
  1985. Doctrine\DBAL\Migrations\Configuration\Configuration,
  1986. Doctrine\DBAL\Migrations\Configuration\YamlConfiguration,
  1987. Doctrine\DBAL\Migrations\Configuration\XmlConfiguration;
  1988. /**
  1989. * Command for executing a migration to a specified version or the latest available version.
  1990. *
  1991. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1992. * @link www.doctrine-project.org
  1993. * @since 2.0
  1994. * @version $Revision$
  1995. * @author Jonathan Wage <jonwage@gmail.com>
  1996. */
  1997. class MigrateCommand extends AbstractCommand
  1998. {
  1999. protected function configure()
  2000. {
  2001. $this
  2002. ->setName('migrations:migrate')
  2003. ->setDescription('Execute a migration to a specified version or the latest available version.')
  2004. ->addArgument('version', InputArgument::OPTIONAL, 'The version to migrate to.', null)
  2005. ->addOption('write-sql', null, InputOption::PARAMETER_NONE, 'The path to output the migration SQL file instead of executing it.')
  2006. ->addOption('dry-run', null, InputOption::PARAMETER_NONE, 'Execute the migration as a dry run.')
  2007. ->setHelp(<<<EOT
  2008. The <info>%command.name%</info> command executes a migration to a specified version or the latest available version:
  2009. <info>%command.full_name%</info>
  2010. You can optionally manually specify the version you wish to migrate to:
  2011. <info>%command.full_name% YYYYMMDDHHMMSS</info>
  2012. You can also execute the migration as a <comment>--dry-run</comment>:
  2013. <info>%command.full_name% YYYYMMDDHHMMSS --dry-run</info>
  2014. Or you can output the would be executed SQL statements to a file with <comment>--write-sql</comment>:
  2015. <info>%command.full_name% YYYYMMDDHHMMSS --write-sql</info>
  2016. You can also execute the migration without a warning message wich you need to interact with:
  2017. <info>%command.full_name% --no-interaction</info>
  2018. EOT
  2019. );
  2020. parent::configure();
  2021. }
  2022. public function execute(InputInterface $input, OutputInterface $output)
  2023. {
  2024. $version = $input->getArgument('version');
  2025. $configuration = $this->_getMigrationConfiguration($input, $output);
  2026. $migration = new Migration($configuration);
  2027. $this->_outputHeader($configuration, $output);
  2028. if ($path = $input->getOption('write-sql')) {
  2029. $path = is_bool($path) ? getcwd() : $path;
  2030. $migration->writeSqlFile($path, $version);
  2031. } else {
  2032. $dryRun = $input->getOption('dry-run') ? true : false;
  2033. if ($dryRun === true) {
  2034. $migration->migrate($version, true);
  2035. } else {
  2036. $noInteraction = $input->getOption('no-interaction') ? true : false;
  2037. if ($noInteraction === true) {
  2038. $migration->migrate($version, $dryRun);
  2039. } else {
  2040. $confirmation = $this->getHelper('dialog')->askConfirmation($output, '<question>WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)</question>', 'y');
  2041. if ($confirmation === true) {
  2042. $migration->migrate($version, $dryRun);
  2043. } else {
  2044. $output->writeln('<error>Migration cancelled!</error>');
  2045. }
  2046. }
  2047. }
  2048. }
  2049. }
  2050. }
  2051. <?php
  2052. namespace Doctrine\DBAL;
  2053. class DBALException extends \Exception
  2054. {
  2055. public static function notSupported($method)
  2056. {
  2057. return new self("Operation '$method' is not supported by platform.");
  2058. }
  2059. public static function invalidPlatformSpecified()
  2060. {
  2061. return new self(
  2062. "Invalid 'platform' option specified, need to give an instance of ".
  2063. "\Doctrine\DBAL\Platforms\AbstractPlatform.");
  2064. }
  2065. public static function invalidPdoInstance()
  2066. {
  2067. return new self(
  2068. "The 'pdo' option was used in DriverManager::getConnection() but no ".
  2069. "instance of PDO was given."
  2070. );
  2071. }
  2072. public static function driverRequired()
  2073. {
  2074. return new self("The options 'driver' or 'driverClass' are mandatory if no PDO ".
  2075. "instance is given to DriverManager::getConnection().");
  2076. }
  2077. public static function unknownDriver($unknownDriverName, array $knownDrivers)
  2078. {
  2079. return new self("The given 'driver' ".$unknownDriverName." is unknown, ".
  2080. "Doctrine currently supports only the following drivers: ".implode(", ", $knownDrivers));
  2081. }
  2082. public static function invalidWrapperClass($wrapperClass)
  2083. {
  2084. return new self("The given 'wrapperClass' ".$wrapperClass." has to be a ".
  2085. "subtype of \Doctrine\DBAL\Connection.");
  2086. }
  2087. public static function invalidDriverClass($driverClass)
  2088. {
  2089. return new self("The given 'driverClass' ".$driverClass." has to implement the ".
  2090. "\Doctrine\DBAL\Driver interface.");
  2091. }
  2092. /**
  2093. * @param string $tableName
  2094. * @return DBALException
  2095. */
  2096. public static function invalidTableName($tableName)
  2097. {
  2098. return new self("Invalid table name specified: ".$tableName);
  2099. }
  2100. /**
  2101. * @param string $tableName
  2102. * @return DBALException
  2103. */
  2104. public static function noColumnsSpecifiedForTable($tableName)
  2105. {
  2106. return new self("No columns specified for table ".$tableName);
  2107. }
  2108. public static function limitOffsetInvalid()
  2109. {
  2110. return new self("Invalid Offset in Limit Query, it has to be larger or equal to 0.");
  2111. }
  2112. public static function typeExists($name)
  2113. {
  2114. return new self('Type '.$name.' already exists.');
  2115. }
  2116. public static function unknownColumnType($name)
  2117. {
  2118. return new self('Unknown column type '.$name.' requested.');
  2119. }
  2120. public static function typeNotFound($name)
  2121. {
  2122. return new self('Type to be overwritten '.$name.' does not exist.');
  2123. }
  2124. }<?php
  2125. /*
  2126. * $Id$
  2127. *
  2128. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2129. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2130. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2131. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2132. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2133. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2134. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2135. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2136. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2137. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2138. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2139. *
  2140. * This software consists of voluntary contributions made by many individuals
  2141. * and is licensed under the LGPL. For more information, see
  2142. * <http://www.doctrine-project.org>.
  2143. */
  2144. namespace Doctrine\DBAL\Driver\PDOOracle;
  2145. use Doctrine\DBAL\Platforms;
  2146. class Driver implements \Doctrine\DBAL\Driver
  2147. {
  2148. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  2149. {
  2150. return new \Doctrine\DBAL\Driver\PDOConnection(
  2151. $this->_constructPdoDsn($params),
  2152. $username,
  2153. $password,
  2154. $driverOptions
  2155. );
  2156. }
  2157. /**
  2158. * Constructs the Oracle PDO DSN.
  2159. *
  2160. * @return string The DSN.
  2161. */
  2162. private function _constructPdoDsn(array $params)
  2163. {
  2164. $dsn = 'oci:';
  2165. if (isset($params['host'])) {
  2166. $dsn .= 'dbname=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)' .
  2167. '(HOST=' . $params['host'] . ')';
  2168. if (isset($params['port'])) {
  2169. $dsn .= '(PORT=' . $params['port'] . ')';
  2170. } else {
  2171. $dsn .= '(PORT=1521)';
  2172. }
  2173. $dsn .= '))(CONNECT_DATA=(SID=' . $params['dbname'] . ')))';
  2174. } else {
  2175. $dsn .= 'dbname=' . $params['dbname'];
  2176. }
  2177. if (isset($params['charset'])) {
  2178. $dsn .= ';charset=' . $params['charset'];
  2179. }
  2180. return $dsn;
  2181. }
  2182. public function getDatabasePlatform()
  2183. {
  2184. return new \Doctrine\DBAL\Platforms\OraclePlatform();
  2185. }
  2186. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  2187. {
  2188. return new \Doctrine\DBAL\Schema\OracleSchemaManager($conn);
  2189. }
  2190. public function getName()
  2191. {
  2192. return 'pdo_oracle';
  2193. }
  2194. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  2195. {
  2196. $params = $conn->getParams();
  2197. return $params['user'];
  2198. }
  2199. }<?php
  2200. /*
  2201. * $Id$
  2202. *
  2203. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2204. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2205. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2206. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2207. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2208. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2209. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2210. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2211. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2212. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2213. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2214. *
  2215. * This software consists of voluntary contributions made by many individuals
  2216. * and is licensed under the LGPL. For more information, see
  2217. * <http://www.doctrine-project.org>.
  2218. */
  2219. namespace Doctrine\DBAL\Driver\IBMDB2;
  2220. class DB2Connection implements \Doctrine\DBAL\Driver\Connection
  2221. {
  2222. private $_conn = null;
  2223. public function __construct(array $params, $username, $password, $driverOptions = array())
  2224. {
  2225. $isPersistant = (isset($params['persistent']) && $params['persistent'] == true);
  2226. if ($isPersistant) {
  2227. $this->_conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
  2228. } else {
  2229. $this->_conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
  2230. }
  2231. if (!$this->_conn) {
  2232. throw new DB2Exception(db2_conn_errormsg());
  2233. }
  2234. }
  2235. function prepare($sql)
  2236. {
  2237. $stmt = @db2_prepare($this->_conn, $sql);
  2238. if (!$stmt) {
  2239. throw new DB2Exception(db2_stmt_errormsg());
  2240. }
  2241. return new DB2Statement($stmt);
  2242. }
  2243. function query()
  2244. {
  2245. $args = func_get_args();
  2246. $sql = $args[0];
  2247. $stmt = $this->prepare($sql);
  2248. $stmt->execute();
  2249. return $stmt;
  2250. }
  2251. function quote($input, $type=\PDO::PARAM_STR)
  2252. {
  2253. $input = db2_escape_string($input);
  2254. if ($type == \PDO::PARAM_INT ) {
  2255. return $input;
  2256. } else {
  2257. return "'".$input."'";
  2258. }
  2259. }
  2260. function exec($statement)
  2261. {
  2262. $stmt = $this->prepare($statement);
  2263. $stmt->execute();
  2264. return $stmt->rowCount();
  2265. }
  2266. function lastInsertId($name = null)
  2267. {
  2268. return db2_last_insert_id($this->_conn);
  2269. }
  2270. function beginTransaction()
  2271. {
  2272. db2_autocommit($this->_conn, DB2_AUTOCOMMIT_OFF);
  2273. }
  2274. function commit()
  2275. {
  2276. if (!db2_commit($this->_conn)) {
  2277. throw new DB2Exception(db2_conn_errormsg($this->_conn));
  2278. }
  2279. db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON);
  2280. }
  2281. function rollBack()
  2282. {
  2283. if (!db2_rollback($this->_conn)) {
  2284. throw new DB2Exception(db2_conn_errormsg($this->_conn));
  2285. }
  2286. db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON);
  2287. }
  2288. function errorCode()
  2289. {
  2290. return db2_conn_error($this->_conn);
  2291. }
  2292. function errorInfo()
  2293. {
  2294. return array(
  2295. 0 => db2_conn_errormsg($this->_conn),
  2296. 1 => $this->errorCode(),
  2297. );
  2298. }
  2299. }<?php
  2300. /*
  2301. * $Id$
  2302. *
  2303. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2304. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2305. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2306. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2307. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2308. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2309. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2310. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2311. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2312. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2313. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2314. *
  2315. * This software consists of voluntary contributions made by many individuals
  2316. * and is licensed under the LGPL. For more information, see
  2317. * <http://www.doctrine-project.org>.
  2318. */
  2319. namespace Doctrine\DBAL\Driver\IBMDB2;
  2320. class DB2Statement implements \Doctrine\DBAL\Driver\Statement
  2321. {
  2322. private $_stmt = null;
  2323. private $_bindParam = array();
  2324. /**
  2325. * DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG
  2326. * @var <type>
  2327. */
  2328. static private $_typeMap = array(
  2329. \PDO::PARAM_INT => DB2_LONG,
  2330. \PDO::PARAM_STR => DB2_CHAR,
  2331. );
  2332. public function __construct($stmt)
  2333. {
  2334. $this->_stmt = $stmt;
  2335. }
  2336. /**
  2337. * Binds a value to a corresponding named or positional
  2338. * placeholder in the SQL statement that was used to prepare the statement.
  2339. *
  2340. * @param mixed $param Parameter identifier. For a prepared statement using named placeholders,
  2341. * this will be a parameter name of the form :name. For a prepared statement
  2342. * using question mark placeholders, this will be the 1-indexed position of the parameter
  2343. *
  2344. * @param mixed $value The value to bind to the parameter.
  2345. * @param integer $type Explicit data type for the parameter using the PDO::PARAM_* constants.
  2346. *
  2347. * @return boolean Returns TRUE on success or FALSE on failure.
  2348. */
  2349. function bindValue($param, $value, $type = null)
  2350. {
  2351. return $this->bindParam($param, $value, $type);
  2352. }
  2353. /**
  2354. * Binds a PHP variable to a corresponding named or question mark placeholder in the
  2355. * SQL statement that was use to prepare the statement. Unlike PDOStatement->bindValue(),
  2356. * the variable is bound as a reference and will only be evaluated at the time
  2357. * that PDOStatement->execute() is called.
  2358. *
  2359. * Most parameters are input parameters, that is, parameters that are
  2360. * used in a read-only fashion to build up the query. Some drivers support the invocation
  2361. * of stored procedures that return data as output parameters, and some also as input/output
  2362. * parameters that both send in data and are updated to receive it.
  2363. *
  2364. * @param mixed $param Parameter identifier. For a prepared statement using named placeholders,
  2365. * this will be a parameter name of the form :name. For a prepared statement
  2366. * using question mark placeholders, this will be the 1-indexed position of the parameter
  2367. *
  2368. * @param mixed $variable Name of the PHP variable to bind to the SQL statement parameter.
  2369. *
  2370. * @param integer $type Explicit data type for the parameter using the PDO::PARAM_* constants. To return
  2371. * an INOUT parameter from a stored procedure, use the bitwise OR operator to set the
  2372. * PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter.
  2373. * @return boolean Returns TRUE on success or FALSE on failure.
  2374. */
  2375. function bindParam($column, &$variable, $type = null)
  2376. {
  2377. $this->_bindParam[$column] =& $variable;
  2378. if ($type && isset(self::$_typeMap[$type])) {
  2379. $type = self::$_typeMap[$type];
  2380. } else {
  2381. $type = DB2_CHAR;
  2382. }
  2383. if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) {
  2384. throw new DB2Exception(db2_stmt_errormsg());
  2385. }
  2386. return true;
  2387. }
  2388. /**
  2389. * Closes the cursor, enabling the statement to be executed again.
  2390. *
  2391. * @return boolean Returns TRUE on success or FALSE on failure.
  2392. */
  2393. function closeCursor()
  2394. {
  2395. if (!$this->_stmt) {
  2396. return false;
  2397. }
  2398. $this->_bindParam = array();
  2399. db2_free_result($this->_stmt);
  2400. $ret = db2_free_stmt($this->_stmt);
  2401. $this->_stmt = false;
  2402. return $ret;
  2403. }
  2404. /**
  2405. * columnCount
  2406. * Returns the number of columns in the result set
  2407. *
  2408. * @return integer Returns the number of columns in the result set represented
  2409. * by the PDOStatement object. If there is no result set,
  2410. * this method should return 0.
  2411. */
  2412. function columnCount()
  2413. {
  2414. if (!$this->_stmt) {
  2415. return false;
  2416. }
  2417. return db2_num_fields($this->_stmt);
  2418. }
  2419. /**
  2420. * errorCode
  2421. * Fetch the SQLSTATE associated with the last operation on the statement handle
  2422. *
  2423. * @see Doctrine_Adapter_Interface::errorCode()
  2424. * @return string error code string
  2425. */
  2426. function errorCode()
  2427. {
  2428. return db2_stmt_error();
  2429. }
  2430. /**
  2431. * errorInfo
  2432. * Fetch extended error information associated with the last operation on the statement handle
  2433. *
  2434. * @see Doctrine_Adapter_Interface::errorInfo()
  2435. * @return array error info array
  2436. */
  2437. function errorInfo()
  2438. {
  2439. return array(
  2440. 0 => db2_stmt_errormsg(),
  2441. 1 => db2_stmt_error(),
  2442. );
  2443. }
  2444. /**
  2445. * Executes a prepared statement
  2446. *
  2447. * If the prepared statement included parameter markers, you must either:
  2448. * call PDOStatement->bindParam() to bind PHP variables to the parameter markers:
  2449. * bound variables pass their value as input and receive the output value,
  2450. * if any, of their associated parameter markers or pass an array of input-only
  2451. * parameter values
  2452. *
  2453. *
  2454. * @param array $params An array of values with as many elements as there are
  2455. * bound parameters in the SQL statement being executed.
  2456. * @return boolean Returns TRUE on success or FALSE on failure.
  2457. */
  2458. function execute($params = null)
  2459. {
  2460. if (!$this->_stmt) {
  2461. return false;
  2462. }
  2463. /*$retval = true;
  2464. if ($params !== null) {
  2465. $retval = @db2_execute($this->_stmt, $params);
  2466. } else {
  2467. $retval = @db2_execute($this->_stmt);
  2468. }*/
  2469. if ($params === null) {
  2470. ksort($this->_bindParam);
  2471. $params = array_values($this->_bindParam);
  2472. }
  2473. $retval = @db2_execute($this->_stmt, $params);
  2474. if ($retval === false) {
  2475. throw new DB2Exception(db2_stmt_errormsg());
  2476. }
  2477. return $retval;
  2478. }
  2479. /**
  2480. * fetch
  2481. *
  2482. * @see Query::HYDRATE_* constants
  2483. * @param integer $fetchStyle Controls how the next row will be returned to the caller.
  2484. * This value must be one of the Query::HYDRATE_* constants,
  2485. * defaulting to Query::HYDRATE_BOTH
  2486. *
  2487. * @param integer $cursorOrientation For a PDOStatement object representing a scrollable cursor,
  2488. * this value determines which row will be returned to the caller.
  2489. * This value must be one of the Query::HYDRATE_ORI_* constants, defaulting to
  2490. * Query::HYDRATE_ORI_NEXT. To request a scrollable cursor for your
  2491. * PDOStatement object,
  2492. * you must set the PDO::ATTR_CURSOR attribute to Doctrine::CURSOR_SCROLL when you
  2493. * prepare the SQL statement with Doctrine_Adapter_Interface->prepare().
  2494. *
  2495. * @param integer $cursorOffset For a PDOStatement object representing a scrollable cursor for which the
  2496. * $cursorOrientation parameter is set to Query::HYDRATE_ORI_ABS, this value specifies
  2497. * the absolute number of the row in the result set that shall be fetched.
  2498. *
  2499. * For a PDOStatement object representing a scrollable cursor for
  2500. * which the $cursorOrientation parameter is set to Query::HYDRATE_ORI_REL, this value
  2501. * specifies the row to fetch relative to the cursor position before
  2502. * PDOStatement->fetch() was called.
  2503. *
  2504. * @return mixed
  2505. */
  2506. function fetch($fetchStyle = \PDO::FETCH_BOTH)
  2507. {
  2508. switch ($fetchStyle) {
  2509. case \PDO::FETCH_BOTH:
  2510. return db2_fetch_both($this->_stmt);
  2511. case \PDO::FETCH_ASSOC:
  2512. return db2_fetch_assoc($this->_stmt);
  2513. case \PDO::FETCH_NUM:
  2514. return db2_fetch_array($this->_stmt);
  2515. default:
  2516. throw new DB2Exception("Given Fetch-Style " . $fetchStyle . " is not supported.");
  2517. }
  2518. }
  2519. /**
  2520. * Returns an array containing all of the result set rows
  2521. *
  2522. * @param integer $fetchStyle Controls how the next row will be returned to the caller.
  2523. * This value must be one of the Query::HYDRATE_* constants,
  2524. * defaulting to Query::HYDRATE_BOTH
  2525. *
  2526. * @param integer $columnIndex Returns the indicated 0-indexed column when the value of $fetchStyle is
  2527. * Query::HYDRATE_COLUMN. Defaults to 0.
  2528. *
  2529. * @return array
  2530. */
  2531. function fetchAll($fetchStyle = \PDO::FETCH_BOTH)
  2532. {
  2533. $rows = array();
  2534. while ($row = $this->fetch($fetchStyle)) {
  2535. $rows[] = $row;
  2536. }
  2537. return $rows;
  2538. }
  2539. /**
  2540. * fetchColumn
  2541. * Returns a single column from the next row of a
  2542. * result set or FALSE if there are no more rows.
  2543. *
  2544. * @param integer $columnIndex 0-indexed number of the column you wish to retrieve from the row. If no
  2545. * value is supplied, PDOStatement->fetchColumn()
  2546. * fetches the first column.
  2547. *
  2548. * @return string returns a single column in the next row of a result set.
  2549. */
  2550. function fetchColumn($columnIndex = 0)
  2551. {
  2552. $row = $this->fetch(\PDO::FETCH_NUM);
  2553. if ($row && isset($row[$columnIndex])) {
  2554. return $row[$columnIndex];
  2555. }
  2556. return false;
  2557. }
  2558. /**
  2559. * rowCount
  2560. * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
  2561. * executed by the corresponding object.
  2562. *
  2563. * If the last SQL statement executed by the associated Statement object was a SELECT statement,
  2564. * some databases may return the number of rows returned by that statement. However,
  2565. * this behaviour is not guaranteed for all databases and should not be
  2566. * relied on for portable applications.
  2567. *
  2568. * @return integer Returns the number of rows.
  2569. */
  2570. function rowCount()
  2571. {
  2572. return (@db2_num_rows($this->_stmt))?:0;
  2573. }
  2574. }
  2575. <?php
  2576. /*
  2577. * $Id$
  2578. *
  2579. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2580. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2581. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2582. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2583. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2584. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2585. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2586. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2587. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2588. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2589. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2590. *
  2591. * This software consists of voluntary contributions made by many individuals
  2592. * and is licensed under the LGPL. For more information, see
  2593. * <http://www.doctrine-project.org>.
  2594. */
  2595. namespace Doctrine\DBAL\Driver\IBMDB2;
  2596. class DB2Exception extends \Exception
  2597. {
  2598. }<?php
  2599. /*
  2600. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2601. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2602. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2603. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2604. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2605. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2606. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2607. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2608. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2609. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2610. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2611. *
  2612. * This software consists of voluntary contributions made by many individuals
  2613. * and is licensed under the LGPL. For more information, see
  2614. * <http://www.doctrine-project.org>.
  2615. */
  2616. namespace Doctrine\DBAL\Driver\IBMDB2;
  2617. use Doctrine\DBAL\Driver,
  2618. Doctrine\DBAL\Connection;
  2619. /**
  2620. * IBM DB2 Driver
  2621. *
  2622. * @since 2.0
  2623. * @author Benjamin Eberlei <kontakt@beberlei.de>
  2624. */
  2625. class DB2Driver implements Driver
  2626. {
  2627. /**
  2628. * Attempts to create a connection with the database.
  2629. *
  2630. * @param array $params All connection parameters passed by the user.
  2631. * @param string $username The username to use when connecting.
  2632. * @param string $password The password to use when connecting.
  2633. * @param array $driverOptions The driver options to use when connecting.
  2634. * @return Doctrine\DBAL\Driver\Connection The database connection.
  2635. */
  2636. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  2637. {
  2638. if ( !isset($params['schema']) ) {
  2639. }
  2640. if ($params['host'] !== 'localhost' && $params['host'] != '127.0.0.1') {
  2641. // if the host isn't localhost, use extended connection params
  2642. $params['dbname'] = 'DRIVER={IBM DB2 ODBC DRIVER}' .
  2643. ';DATABASE=' . $params['dbname'] .
  2644. ';HOSTNAME=' . $params['host'] .
  2645. ';PORT=' . $params['port'] .
  2646. ';PROTOCOL=' . $params['protocol'] .
  2647. ';UID=' . $username .
  2648. ';PWD=' . $password .';';
  2649. $username = null;
  2650. $password = null;
  2651. }
  2652. return new DB2Connection($params, $username, $password, $driverOptions);
  2653. }
  2654. /**
  2655. * Gets the DatabasePlatform instance that provides all the metadata about
  2656. * the platform this driver connects to.
  2657. *
  2658. * @return Doctrine\DBAL\Platforms\AbstractPlatform The database platform.
  2659. */
  2660. public function getDatabasePlatform()
  2661. {
  2662. return new \Doctrine\DBAL\Platforms\DB2Platform;
  2663. }
  2664. /**
  2665. * Gets the SchemaManager that can be used to inspect and change the underlying
  2666. * database schema of the platform this driver connects to.
  2667. *
  2668. * @param Doctrine\DBAL\Connection $conn
  2669. * @return Doctrine\DBAL\SchemaManager
  2670. */
  2671. public function getSchemaManager(Connection $conn)
  2672. {
  2673. return new \Doctrine\DBAL\Schema\DB2SchemaManager($conn);
  2674. }
  2675. /**
  2676. * Gets the name of the driver.
  2677. *
  2678. * @return string The name of the driver.
  2679. */
  2680. public function getName()
  2681. {
  2682. return 'ibm_db2';
  2683. }
  2684. /**
  2685. * Get the name of the database connected to for this driver.
  2686. *
  2687. * @param Doctrine\DBAL\Connection $conn
  2688. * @return string $database
  2689. */
  2690. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  2691. {
  2692. $params = $conn->getParams();
  2693. return $params['dbname'];
  2694. }
  2695. }
  2696. <?php
  2697. /*
  2698. * $Id: Interface.php 3882 2008-02-22 18:11:35Z jwage $
  2699. *
  2700. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2701. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2702. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2703. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2704. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2705. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2706. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2707. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2708. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2709. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2710. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2711. *
  2712. * This software consists of voluntary contributions made by many individuals
  2713. * and is licensed under the LGPL. For more information, see
  2714. * <http://www.doctrine-project.org>.
  2715. */
  2716. namespace Doctrine\DBAL\Driver;
  2717. /**
  2718. * The PDO implementation of the Statement interface.
  2719. * Used by all PDO-based drivers.
  2720. *
  2721. * @since 2.0
  2722. */
  2723. class PDOStatement extends \PDOStatement implements Statement
  2724. {
  2725. private function __construct() {}
  2726. }<?php
  2727. /*
  2728. * $Id$
  2729. *
  2730. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2731. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2732. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2733. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2734. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2735. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2736. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2737. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2738. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2739. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2740. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2741. *
  2742. * This software consists of voluntary contributions made by many individuals
  2743. * and is licensed under the LGPL. For more information, see
  2744. * <http://www.doctrine-project.org>.
  2745. */
  2746. namespace Doctrine\DBAL\Driver\PDOMsSql;
  2747. /**
  2748. * The PDO-based MsSql driver.
  2749. *
  2750. * @since 2.0
  2751. */
  2752. class Driver implements \Doctrine\DBAL\Driver
  2753. {
  2754. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  2755. {
  2756. return new Connection(
  2757. $this->_constructPdoDsn($params),
  2758. $username,
  2759. $password,
  2760. $driverOptions
  2761. );
  2762. }
  2763. /**
  2764. * Constructs the MsSql PDO DSN.
  2765. *
  2766. * @return string The DSN.
  2767. */
  2768. private function _constructPdoDsn(array $params)
  2769. {
  2770. // TODO: This might need to be revisted once we have access to a mssql server
  2771. $dsn = 'mssql:';
  2772. if (isset($params['host'])) {
  2773. $dsn .= 'host=' . $params['host'] . ';';
  2774. }
  2775. if (isset($params['port'])) {
  2776. $dsn .= 'port=' . $params['port'] . ';';
  2777. }
  2778. if (isset($params['dbname'])) {
  2779. $dsn .= 'dbname=' . $params['dbname'] . ';';
  2780. }
  2781. return $dsn;
  2782. }
  2783. public function getDatabasePlatform()
  2784. {
  2785. return new \Doctrine\DBAL\Platforms\MsSqlPlatform();
  2786. }
  2787. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  2788. {
  2789. return new \Doctrine\DBAL\Schema\MsSqlSchemaManager($conn);
  2790. }
  2791. public function getName()
  2792. {
  2793. return 'pdo_mssql';
  2794. }
  2795. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  2796. {
  2797. $params = $conn->getParams();
  2798. return $params['dbname'];
  2799. }
  2800. }<?php
  2801. /*
  2802. * $Id$
  2803. *
  2804. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2805. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2806. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2807. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2808. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2809. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2810. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2811. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2812. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2813. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2814. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2815. *
  2816. * This software consists of voluntary contributions made by many individuals
  2817. * and is licensed under the LGPL. For more information, see
  2818. * <http://www.doctrine-project.org>.
  2819. */
  2820. namespace Doctrine\DBAL\Driver\PDOMsSql;
  2821. /**
  2822. * MsSql Connection implementation.
  2823. *
  2824. * @since 2.0
  2825. */
  2826. class Connection extends \PDO implements \Doctrine\DBAL\Driver\Connection
  2827. {
  2828. /**
  2829. * Performs the rollback.
  2830. *
  2831. * @override
  2832. */
  2833. public function rollback()
  2834. {
  2835. $this->exec('ROLLBACK TRANSACTION');
  2836. }
  2837. /**
  2838. * Performs the commit.
  2839. *
  2840. * @override
  2841. */
  2842. public function commit()
  2843. {
  2844. $this->exec('COMMIT TRANSACTION');
  2845. }
  2846. /**
  2847. * Begins a database transaction.
  2848. *
  2849. * @override
  2850. */
  2851. public function beginTransaction()
  2852. {
  2853. $this->exec('BEGIN TRANSACTION');
  2854. }
  2855. }<?php
  2856. /*
  2857. * $Id$
  2858. *
  2859. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2860. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2861. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2862. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2863. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2864. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2865. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2866. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2867. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2868. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2869. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2870. *
  2871. * This software consists of voluntary contributions made by many individuals
  2872. * and is licensed under the LGPL. For more information, see
  2873. * <http://www.doctrine-project.org>.
  2874. */
  2875. namespace Doctrine\DBAL\Driver;
  2876. use \PDO;
  2877. /**
  2878. * Statement interface.
  2879. * Drivers must implement this interface.
  2880. *
  2881. * This resembles (a subset of) the PDOStatement interface.
  2882. *
  2883. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  2884. * @author Roman Borschel <roman@code-factory.org>
  2885. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  2886. * @link www.doctrine-project.org
  2887. * @since 2.0
  2888. * @version $Revision$
  2889. */
  2890. interface Statement
  2891. {
  2892. /**
  2893. * Binds a value to a corresponding named or positional
  2894. * placeholder in the SQL statement that was used to prepare the statement.
  2895. *
  2896. * @param mixed $param Parameter identifier. For a prepared statement using named placeholders,
  2897. * this will be a parameter name of the form :name. For a prepared statement
  2898. * using question mark placeholders, this will be the 1-indexed position of the parameter
  2899. *
  2900. * @param mixed $value The value to bind to the parameter.
  2901. * @param integer $type Explicit data type for the parameter using the PDO::PARAM_* constants.
  2902. *
  2903. * @return boolean Returns TRUE on success or FALSE on failure.
  2904. */
  2905. function bindValue($param, $value, $type = null);
  2906. /**
  2907. * Binds a PHP variable to a corresponding named or question mark placeholder in the
  2908. * SQL statement that was use to prepare the statement. Unlike PDOStatement->bindValue(),
  2909. * the variable is bound as a reference and will only be evaluated at the time
  2910. * that PDOStatement->execute() is called.
  2911. *
  2912. * Most parameters are input parameters, that is, parameters that are
  2913. * used in a read-only fashion to build up the query. Some drivers support the invocation
  2914. * of stored procedures that return data as output parameters, and some also as input/output
  2915. * parameters that both send in data and are updated to receive it.
  2916. *
  2917. * @param mixed $param Parameter identifier. For a prepared statement using named placeholders,
  2918. * this will be a parameter name of the form :name. For a prepared statement
  2919. * using question mark placeholders, this will be the 1-indexed position of the parameter
  2920. *
  2921. * @param mixed $variable Name of the PHP variable to bind to the SQL statement parameter.
  2922. *
  2923. * @param integer $type Explicit data type for the parameter using the PDO::PARAM_* constants. To return
  2924. * an INOUT parameter from a stored procedure, use the bitwise OR operator to set the
  2925. * PDO::PARAM_INPUT_OUTPUT bits for the data_type parameter.
  2926. * @return boolean Returns TRUE on success or FALSE on failure.
  2927. */
  2928. function bindParam($column, &$variable, $type = null);
  2929. /**
  2930. * Closes the cursor, enabling the statement to be executed again.
  2931. *
  2932. * @return boolean Returns TRUE on success or FALSE on failure.
  2933. */
  2934. function closeCursor();
  2935. /**
  2936. * columnCount
  2937. * Returns the number of columns in the result set
  2938. *
  2939. * @return integer Returns the number of columns in the result set represented
  2940. * by the PDOStatement object. If there is no result set,
  2941. * this method should return 0.
  2942. */
  2943. function columnCount();
  2944. /**
  2945. * errorCode
  2946. * Fetch the SQLSTATE associated with the last operation on the statement handle
  2947. *
  2948. * @see Doctrine_Adapter_Interface::errorCode()
  2949. * @return string error code string
  2950. */
  2951. function errorCode();
  2952. /**
  2953. * errorInfo
  2954. * Fetch extended error information associated with the last operation on the statement handle
  2955. *
  2956. * @see Doctrine_Adapter_Interface::errorInfo()
  2957. * @return array error info array
  2958. */
  2959. function errorInfo();
  2960. /**
  2961. * Executes a prepared statement
  2962. *
  2963. * If the prepared statement included parameter markers, you must either:
  2964. * call PDOStatement->bindParam() to bind PHP variables to the parameter markers:
  2965. * bound variables pass their value as input and receive the output value,
  2966. * if any, of their associated parameter markers or pass an array of input-only
  2967. * parameter values
  2968. *
  2969. *
  2970. * @param array $params An array of values with as many elements as there are
  2971. * bound parameters in the SQL statement being executed.
  2972. * @return boolean Returns TRUE on success or FALSE on failure.
  2973. */
  2974. function execute($params = null);
  2975. /**
  2976. * fetch
  2977. *
  2978. * @see Query::HYDRATE_* constants
  2979. * @param integer $fetchStyle Controls how the next row will be returned to the caller.
  2980. * This value must be one of the Query::HYDRATE_* constants,
  2981. * defaulting to Query::HYDRATE_BOTH
  2982. *
  2983. * @param integer $cursorOrientation For a PDOStatement object representing a scrollable cursor,
  2984. * this value determines which row will be returned to the caller.
  2985. * This value must be one of the Query::HYDRATE_ORI_* constants, defaulting to
  2986. * Query::HYDRATE_ORI_NEXT. To request a scrollable cursor for your
  2987. * PDOStatement object,
  2988. * you must set the PDO::ATTR_CURSOR attribute to Doctrine::CURSOR_SCROLL when you
  2989. * prepare the SQL statement with Doctrine_Adapter_Interface->prepare().
  2990. *
  2991. * @param integer $cursorOffset For a PDOStatement object representing a scrollable cursor for which the
  2992. * $cursorOrientation parameter is set to Query::HYDRATE_ORI_ABS, this value specifies
  2993. * the absolute number of the row in the result set that shall be fetched.
  2994. *
  2995. * For a PDOStatement object representing a scrollable cursor for
  2996. * which the $cursorOrientation parameter is set to Query::HYDRATE_ORI_REL, this value
  2997. * specifies the row to fetch relative to the cursor position before
  2998. * PDOStatement->fetch() was called.
  2999. *
  3000. * @return mixed
  3001. */
  3002. function fetch($fetchStyle = PDO::FETCH_BOTH);
  3003. /**
  3004. * Returns an array containing all of the result set rows
  3005. *
  3006. * @param integer $fetchStyle Controls how the next row will be returned to the caller.
  3007. * This value must be one of the Query::HYDRATE_* constants,
  3008. * defaulting to Query::HYDRATE_BOTH
  3009. *
  3010. * @param integer $columnIndex Returns the indicated 0-indexed column when the value of $fetchStyle is
  3011. * Query::HYDRATE_COLUMN. Defaults to 0.
  3012. *
  3013. * @return array
  3014. */
  3015. function fetchAll($fetchStyle = PDO::FETCH_BOTH);
  3016. /**
  3017. * fetchColumn
  3018. * Returns a single column from the next row of a
  3019. * result set or FALSE if there are no more rows.
  3020. *
  3021. * @param integer $columnIndex 0-indexed number of the column you wish to retrieve from the row. If no
  3022. * value is supplied, PDOStatement->fetchColumn()
  3023. * fetches the first column.
  3024. *
  3025. * @return string returns a single column in the next row of a result set.
  3026. */
  3027. function fetchColumn($columnIndex = 0);
  3028. /**
  3029. * rowCount
  3030. * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
  3031. * executed by the corresponding object.
  3032. *
  3033. * If the last SQL statement executed by the associated Statement object was a SELECT statement,
  3034. * some databases may return the number of rows returned by that statement. However,
  3035. * this behaviour is not guaranteed for all databases and should not be
  3036. * relied on for portable applications.
  3037. *
  3038. * @return integer Returns the number of rows.
  3039. */
  3040. function rowCount();
  3041. }<?php
  3042. namespace Doctrine\DBAL\Driver\PDOPgSql;
  3043. use Doctrine\DBAL\Platforms;
  3044. /**
  3045. * Driver that connects through pdo_pgsql.
  3046. *
  3047. * @since 2.0
  3048. */
  3049. class Driver implements \Doctrine\DBAL\Driver
  3050. {
  3051. /**
  3052. * Attempts to connect to the database and returns a driver connection on success.
  3053. *
  3054. * @return Doctrine\DBAL\Driver\Connection
  3055. */
  3056. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  3057. {
  3058. return new \Doctrine\DBAL\Driver\PDOConnection(
  3059. $this->_constructPdoDsn($params),
  3060. $username,
  3061. $password,
  3062. $driverOptions
  3063. );
  3064. }
  3065. /**
  3066. * Constructs the Postgres PDO DSN.
  3067. *
  3068. * @return string The DSN.
  3069. */
  3070. private function _constructPdoDsn(array $params)
  3071. {
  3072. $dsn = 'pgsql:';
  3073. if (isset($params['host'])) {
  3074. $dsn .= 'host=' . $params['host'] . ' ';
  3075. }
  3076. if (isset($params['port'])) {
  3077. $dsn .= 'port=' . $params['port'] . ' ';
  3078. }
  3079. if (isset($params['dbname'])) {
  3080. $dsn .= 'dbname=' . $params['dbname'] . ' ';
  3081. }
  3082. return $dsn;
  3083. }
  3084. public function getDatabasePlatform()
  3085. {
  3086. return new \Doctrine\DBAL\Platforms\PostgreSqlPlatform();
  3087. }
  3088. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  3089. {
  3090. return new \Doctrine\DBAL\Schema\PostgreSqlSchemaManager($conn);
  3091. }
  3092. public function getName()
  3093. {
  3094. return 'pdo_pgsql';
  3095. }
  3096. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  3097. {
  3098. $params = $conn->getParams();
  3099. return $params['dbname'];
  3100. }
  3101. }<?php
  3102. /*
  3103. * $Id$
  3104. *
  3105. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3106. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3107. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3108. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3109. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3110. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3111. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3112. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3113. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3114. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3115. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3116. *
  3117. * This software consists of voluntary contributions made by many individuals
  3118. * and is licensed under the LGPL. For more information, see
  3119. * <http://www.doctrine-project.org>.
  3120. */
  3121. namespace Doctrine\DBAL\Driver;
  3122. use \PDO;
  3123. /**
  3124. * PDO implementation of the Connection interface.
  3125. * Used by all PDO-based drivers.
  3126. *
  3127. * @since 2.0
  3128. */
  3129. class PDOConnection extends PDO implements Connection
  3130. {
  3131. public function __construct($dsn, $user = null, $password = null, array $options = null)
  3132. {
  3133. parent::__construct($dsn, $user, $password, $options);
  3134. $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Doctrine\DBAL\Driver\PDOStatement', array()));
  3135. $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  3136. }
  3137. }<?php
  3138. /*
  3139. * $Id$
  3140. *
  3141. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3142. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3143. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3144. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3145. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3146. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3147. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3148. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3149. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3150. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3151. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3152. *
  3153. * This software consists of voluntary contributions made by many individuals
  3154. * and is licensed under the LGPL. For more information, see
  3155. * <http://www.doctrine-project.org>.
  3156. */
  3157. namespace Doctrine\DBAL\Driver\PDOIbm;
  3158. use Doctrine\DBAL\Connection;
  3159. /**
  3160. * Driver for the PDO IBM extension
  3161. *
  3162. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  3163. * @link www.doctrine-project.com
  3164. * @since 1.0
  3165. * @version $Revision$
  3166. * @author Benjamin Eberlei <kontakt@beberlei.de>
  3167. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  3168. * @author Jonathan Wage <jonwage@gmail.com>
  3169. * @author Roman Borschel <roman@code-factory.org>
  3170. */
  3171. class Driver implements \Doctrine\DBAL\Driver
  3172. {
  3173. /**
  3174. * Attempts to establish a connection with the underlying driver.
  3175. *
  3176. * @param array $params
  3177. * @param string $username
  3178. * @param string $password
  3179. * @param array $driverOptions
  3180. * @return Doctrine\DBAL\Driver\Connection
  3181. */
  3182. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  3183. {
  3184. $conn = new \Doctrine\DBAL\Driver\PDOConnection(
  3185. $this->_constructPdoDsn($params),
  3186. $username,
  3187. $password,
  3188. $driverOptions
  3189. );
  3190. return $conn;
  3191. }
  3192. /**
  3193. * Constructs the MySql PDO DSN.
  3194. *
  3195. * @return string The DSN.
  3196. */
  3197. private function _constructPdoDsn(array $params)
  3198. {
  3199. $dsn = 'ibm:';
  3200. if (isset($params['host'])) {
  3201. $dsn .= 'HOSTNAME=' . $params['host'] . ';';
  3202. }
  3203. if (isset($params['port'])) {
  3204. $dsn .= 'PORT=' . $params['port'] . ';';
  3205. }
  3206. $dsn .= 'PROTOCOL=TCPIP;';
  3207. if (isset($params['dbname'])) {
  3208. $dsn .= 'DATABASE=' . $params['dbname'] . ';';
  3209. }
  3210. return $dsn;
  3211. }
  3212. /**
  3213. * Gets the DatabasePlatform instance that provides all the metadata about
  3214. * the platform this driver connects to.
  3215. *
  3216. * @return Doctrine\DBAL\Platforms\AbstractPlatform The database platform.
  3217. */
  3218. public function getDatabasePlatform()
  3219. {
  3220. return new \Doctrine\DBAL\Platforms\DB2Platform;
  3221. }
  3222. /**
  3223. * Gets the SchemaManager that can be used to inspect and change the underlying
  3224. * database schema of the platform this driver connects to.
  3225. *
  3226. * @param Doctrine\DBAL\Connection $conn
  3227. * @return Doctrine\DBAL\SchemaManager
  3228. */
  3229. public function getSchemaManager(Connection $conn)
  3230. {
  3231. return new \Doctrine\DBAL\Schema\DB2SchemaManager($conn);
  3232. }
  3233. /**
  3234. * Gets the name of the driver.
  3235. *
  3236. * @return string The name of the driver.
  3237. */
  3238. public function getName()
  3239. {
  3240. return 'pdo_ibm';
  3241. }
  3242. /**
  3243. * Get the name of the database connected to for this driver.
  3244. *
  3245. * @param Doctrine\DBAL\Connection $conn
  3246. * @return string $database
  3247. */
  3248. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  3249. {
  3250. $params = $conn->getParams();
  3251. return $params['dbname'];
  3252. }
  3253. }<?php
  3254. /*
  3255. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3256. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3257. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3258. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3259. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3260. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3261. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3262. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3263. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3264. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3265. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3266. *
  3267. * This software consists of voluntary contributions made by many individuals
  3268. * and is licensed under the LGPL. For more information, see
  3269. * <http://www.doctrine-project.org>.
  3270. */
  3271. namespace Doctrine\DBAL\Driver\PDOMySql;
  3272. use Doctrine\DBAL\Connection;
  3273. /**
  3274. * PDO MySql driver.
  3275. *
  3276. * @since 2.0
  3277. */
  3278. class Driver implements \Doctrine\DBAL\Driver
  3279. {
  3280. /**
  3281. * Attempts to establish a connection with the underlying driver.
  3282. *
  3283. * @param array $params
  3284. * @param string $username
  3285. * @param string $password
  3286. * @param array $driverOptions
  3287. * @return Doctrine\DBAL\Driver\Connection
  3288. */
  3289. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  3290. {
  3291. $conn = new \Doctrine\DBAL\Driver\PDOConnection(
  3292. $this->_constructPdoDsn($params),
  3293. $username,
  3294. $password,
  3295. $driverOptions
  3296. );
  3297. return $conn;
  3298. }
  3299. /**
  3300. * Constructs the MySql PDO DSN.
  3301. *
  3302. * @return string The DSN.
  3303. */
  3304. private function _constructPdoDsn(array $params)
  3305. {
  3306. $dsn = 'mysql:';
  3307. if (isset($params['host'])) {
  3308. $dsn .= 'host=' . $params['host'] . ';';
  3309. }
  3310. if (isset($params['port'])) {
  3311. $dsn .= 'port=' . $params['port'] . ';';
  3312. }
  3313. if (isset($params['dbname'])) {
  3314. $dsn .= 'dbname=' . $params['dbname'] . ';';
  3315. }
  3316. if (isset($params['unix_socket'])) {
  3317. $dsn .= 'unix_socket=' . $params['unix_socket'] . ';';
  3318. }
  3319. return $dsn;
  3320. }
  3321. public function getDatabasePlatform()
  3322. {
  3323. return new \Doctrine\DBAL\Platforms\MySqlPlatform();
  3324. }
  3325. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  3326. {
  3327. return new \Doctrine\DBAL\Schema\MySqlSchemaManager($conn);
  3328. }
  3329. public function getName()
  3330. {
  3331. return 'pdo_mysql';
  3332. }
  3333. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  3334. {
  3335. $params = $conn->getParams();
  3336. return $params['dbname'];
  3337. }
  3338. }<?php
  3339. /*
  3340. * $Id$
  3341. *
  3342. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3343. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3344. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3345. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3346. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3347. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3348. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3349. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3350. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3351. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3352. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3353. *
  3354. * This software consists of voluntary contributions made by many individuals
  3355. * and is licensed under the LGPL. For more information, see
  3356. * <http://www.doctrine-project.org>.
  3357. */
  3358. namespace Doctrine\DBAL\Driver\PDOSqlsrv;
  3359. /**
  3360. * The PDO-based Sqlsrv driver.
  3361. *
  3362. * @since 2.0
  3363. */
  3364. class Driver implements \Doctrine\DBAL\Driver
  3365. {
  3366. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  3367. {
  3368. return new \Doctrine\DBAL\Driver\PDOConnection(
  3369. $this->_constructPdoDsn($params),
  3370. $username,
  3371. $password,
  3372. $driverOptions
  3373. );
  3374. }
  3375. /**
  3376. * Constructs the Sqlsrv PDO DSN.
  3377. *
  3378. * @return string The DSN.
  3379. */
  3380. private function _constructPdoDsn(array $params)
  3381. {
  3382. $dsn = 'sqlsrv:server=';
  3383. if (isset($params['host'])) {
  3384. $dsn .= $params['host'];
  3385. }
  3386. if (isset($params['port']) && !empty($params['port'])) {
  3387. $dsn .= ',' . $params['port'];
  3388. }
  3389. if (isset($params['dbname'])) {
  3390. $dsn .= ';Database=' . $params['dbname'];
  3391. }
  3392. return $dsn;
  3393. }
  3394. public function getDatabasePlatform()
  3395. {
  3396. return new \Doctrine\DBAL\Platforms\MsSqlPlatform();
  3397. }
  3398. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  3399. {
  3400. return new \Doctrine\DBAL\Schema\MsSqlSchemaManager($conn);
  3401. }
  3402. public function getName()
  3403. {
  3404. return 'pdo_sqlsrv';
  3405. }
  3406. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  3407. {
  3408. $params = $conn->getParams();
  3409. return $params['dbname'];
  3410. }
  3411. }<?php
  3412. /*
  3413. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3414. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3415. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3416. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3417. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3418. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3419. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3420. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3421. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3422. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3423. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3424. *
  3425. * This software consists of voluntary contributions made by many individuals
  3426. * and is licensed under the LGPL. For more information, see
  3427. * <http://www.doctrine-project.org>.
  3428. */
  3429. namespace Doctrine\DBAL\Driver\PDOSqlite;
  3430. /**
  3431. * The PDO Sqlite driver.
  3432. *
  3433. * @since 2.0
  3434. */
  3435. class Driver implements \Doctrine\DBAL\Driver
  3436. {
  3437. /**
  3438. * @var array
  3439. */
  3440. protected $_userDefinedFunctions = array(
  3441. 'sqrt' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfSqrt'), 'numArgs' => 1),
  3442. 'mod' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfMod'), 'numArgs' => 2),
  3443. 'locate' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfLocate'), 'numArgs' => -1),
  3444. );
  3445. /**
  3446. * Tries to establish a database connection to SQLite.
  3447. *
  3448. * @param array $params
  3449. * @param string $username
  3450. * @param string $password
  3451. * @param array $driverOptions
  3452. * @return Connection
  3453. */
  3454. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  3455. {
  3456. if (isset($driverOptions['userDefinedFunctions'])) {
  3457. $this->_userDefinedFunctions = array_merge(
  3458. $this->_userDefinedFunctions, $driverOptions['userDefinedFunctions']);
  3459. unset($driverOptions['userDefinedFunctions']);
  3460. }
  3461. $pdo = new \Doctrine\DBAL\Driver\PDOConnection(
  3462. $this->_constructPdoDsn($params),
  3463. $username,
  3464. $password,
  3465. $driverOptions
  3466. );
  3467. foreach ($this->_userDefinedFunctions AS $fn => $data) {
  3468. $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
  3469. }
  3470. return $pdo;
  3471. }
  3472. /**
  3473. * Constructs the Sqlite PDO DSN.
  3474. *
  3475. * @return string The DSN.
  3476. * @override
  3477. */
  3478. protected function _constructPdoDsn(array $params)
  3479. {
  3480. $dsn = 'sqlite:';
  3481. if (isset($params['path'])) {
  3482. $dsn .= $params['path'];
  3483. } else if (isset($params['memory'])) {
  3484. $dsn .= ':memory:';
  3485. }
  3486. return $dsn;
  3487. }
  3488. /**
  3489. * Gets the database platform that is relevant for this driver.
  3490. */
  3491. public function getDatabasePlatform()
  3492. {
  3493. return new \Doctrine\DBAL\Platforms\SqlitePlatform();
  3494. }
  3495. /**
  3496. * Gets the schema manager that is relevant for this driver.
  3497. *
  3498. * @param Doctrine\DBAL\Connection $conn
  3499. * @return Doctrine\DBAL\Schema\SqliteSchemaManager
  3500. */
  3501. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  3502. {
  3503. return new \Doctrine\DBAL\Schema\SqliteSchemaManager($conn);
  3504. }
  3505. public function getName()
  3506. {
  3507. return 'pdo_sqlite';
  3508. }
  3509. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  3510. {
  3511. $params = $conn->getParams();
  3512. return isset($params['path']) ? $params['path'] : null;
  3513. }
  3514. }<?php
  3515. /*
  3516. * $Id$
  3517. *
  3518. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3519. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3520. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3521. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3522. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3523. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3524. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3525. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3526. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3527. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3528. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3529. *
  3530. * This software consists of voluntary contributions made by many individuals
  3531. * and is licensed under the LGPL. For more information, see
  3532. * <http://www.doctrine-project.org>.
  3533. */
  3534. namespace Doctrine\DBAL\Driver\OCI8;
  3535. class OCI8Exception extends \Exception
  3536. {
  3537. static public function fromErrorInfo($error)
  3538. {
  3539. return new self($error['message'], $error['code']);
  3540. }
  3541. }
  3542. <?php
  3543. /*
  3544. * $Id$
  3545. *
  3546. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3547. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3548. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3549. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3550. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3551. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3552. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3553. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3554. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3555. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3556. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3557. *
  3558. * This software consists of voluntary contributions made by many individuals
  3559. * and is licensed under the LGPL. For more information, see
  3560. * <http://www.doctrine-project.org>.
  3561. */
  3562. namespace Doctrine\DBAL\Driver\OCI8;
  3563. use Doctrine\DBAL\Platforms;
  3564. /**
  3565. * A Doctrine DBAL driver for the Oracle OCI8 PHP extensions.
  3566. *
  3567. * @author Roman Borschel <roman@code-factory.org>
  3568. * @since 2.0
  3569. */
  3570. class Driver implements \Doctrine\DBAL\Driver
  3571. {
  3572. public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
  3573. {
  3574. return new OCI8Connection(
  3575. $username,
  3576. $password,
  3577. $this->_constructDsn($params)
  3578. );
  3579. }
  3580. /**
  3581. * Constructs the Oracle DSN.
  3582. *
  3583. * @return string The DSN.
  3584. */
  3585. private function _constructDsn(array $params)
  3586. {
  3587. $dsn = '';
  3588. if (isset($params['host'])) {
  3589. $dsn .= '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)' .
  3590. '(HOST=' . $params['host'] . ')';
  3591. if (isset($params['port'])) {
  3592. $dsn .= '(PORT=' . $params['port'] . ')';
  3593. } else {
  3594. $dsn .= '(PORT=1521)';
  3595. }
  3596. $dsn .= '))';
  3597. if (isset($params['dbname'])) {
  3598. $dsn .= '(CONNECT_DATA=(SID=' . $params['dbname'] . ')';
  3599. }
  3600. $dsn .= '))';
  3601. } else {
  3602. $dsn .= $params['dbname'];
  3603. }
  3604. if (isset($params['charset'])) {
  3605. $dsn .= ';charset=' . $params['charset'];
  3606. }
  3607. return $dsn;
  3608. }
  3609. public function getDatabasePlatform()
  3610. {
  3611. return new \Doctrine\DBAL\Platforms\OraclePlatform();
  3612. }
  3613. public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
  3614. {
  3615. return new \Doctrine\DBAL\Schema\OracleSchemaManager($conn);
  3616. }
  3617. public function getName()
  3618. {
  3619. return 'oci8';
  3620. }
  3621. public function getDatabase(\Doctrine\DBAL\Connection $conn)
  3622. {
  3623. $params = $conn->getParams();
  3624. return $params['user'];
  3625. }
  3626. }<?php
  3627. /*
  3628. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3629. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3630. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3631. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3632. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3633. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3634. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3635. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3636. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3637. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3638. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3639. *
  3640. * This software consists of voluntary contributions made by many individuals
  3641. * and is licensed under the LGPL. For more information, see
  3642. * <http://www.doctrine-project.org>.
  3643. */
  3644. namespace Doctrine\DBAL\Driver\OCI8;
  3645. use \PDO;
  3646. /**
  3647. * The OCI8 implementation of the Statement interface.
  3648. *
  3649. * @since 2.0
  3650. * @author Roman Borschel <roman@code-factory.org>
  3651. */
  3652. class OCI8Statement implements \Doctrine\DBAL\Driver\Statement
  3653. {
  3654. /** Statement handle. */
  3655. private $_sth;
  3656. private static $_PARAM = ':param';
  3657. private static $fetchStyleMap = array(
  3658. PDO::FETCH_BOTH => OCI_BOTH,
  3659. PDO::FETCH_ASSOC => OCI_ASSOC,
  3660. PDO::FETCH_NUM => OCI_NUM
  3661. );
  3662. private $_paramMap = array();
  3663. /**
  3664. * Creates a new OCI8Statement that uses the given connection handle and SQL statement.
  3665. *
  3666. * @param resource $dbh The connection handle.
  3667. * @param string $statement The SQL statement.
  3668. */
  3669. public function __construct($dbh, $statement)
  3670. {
  3671. list($statement, $paramMap) = self::convertPositionalToNamedPlaceholders($statement);
  3672. $this->_sth = oci_parse($dbh, $statement);
  3673. $this->_paramMap = $paramMap;
  3674. }
  3675. /**
  3676. * Convert positional (?) into named placeholders (:param<num>)
  3677. *
  3678. * Oracle does not support positional parameters, hence this method converts all
  3679. * positional parameters into artificially named parameters. Note that this conversion
  3680. * is not perfect. All question marks (?) in the original statement are treated as
  3681. * placeholders and converted to a named parameter.
  3682. *
  3683. * The algorithm uses a state machine with two possible states: InLiteral and NotInLiteral.
  3684. * Question marks inside literal strings are therefore handled correctly by this method.
  3685. * This comes at a cost, the whole sql statement has to be looped over.
  3686. *
  3687. * @todo extract into utility class in Doctrine\DBAL\Util namespace
  3688. * @todo review and test for lost spaces. we experienced missing spaces with oci8 in some sql statements.
  3689. * @param string $statement The SQL statement to convert.
  3690. * @return string
  3691. */
  3692. static public function convertPositionalToNamedPlaceholders($statement)
  3693. {
  3694. $count = 1;
  3695. $inLiteral = false; // a valid query never starts with quotes
  3696. $stmtLen = strlen($statement);
  3697. $paramMap = array();
  3698. for ($i = 0; $i < $stmtLen; $i++) {
  3699. if ($statement[$i] == '?' && !$inLiteral) {
  3700. // real positional parameter detected
  3701. $paramMap[$count] = ":param$count";
  3702. $len = strlen($paramMap[$count]);
  3703. $statement = substr_replace($statement, ":param$count", $i, 1);
  3704. $i += $len-1; // jump ahead
  3705. $stmtLen = strlen($statement); // adjust statement length
  3706. ++$count;
  3707. } else if ($statement[$i] == "'" || $statement[$i] == '"') {
  3708. $inLiteral = ! $inLiteral; // switch state!
  3709. }
  3710. }
  3711. return array($statement, $paramMap);
  3712. }
  3713. /**
  3714. * {@inheritdoc}
  3715. */
  3716. public function bindValue($param, $value, $type = null)
  3717. {
  3718. return $this->bindParam($param, $value, $type);
  3719. }
  3720. /**
  3721. * {@inheritdoc}
  3722. */
  3723. public function bindParam($column, &$variable, $type = null)
  3724. {
  3725. $column = isset($this->_paramMap[$column]) ? $this->_paramMap[$column] : $column;
  3726. return oci_bind_by_name($this->_sth, $column, $variable);
  3727. }
  3728. /**
  3729. * Closes the cursor, enabling the statement to be executed again.
  3730. *
  3731. * @return boolean Returns TRUE on success or FALSE on failure.
  3732. */
  3733. public function closeCursor()
  3734. {
  3735. return oci_free_statement($this->_sth);
  3736. }
  3737. /**
  3738. * {@inheritdoc}
  3739. */
  3740. public function columnCount()
  3741. {
  3742. return oci_num_fields($this->_sth);
  3743. }
  3744. /**
  3745. * {@inheritdoc}
  3746. */
  3747. public function errorCode()
  3748. {
  3749. $error = oci_error($this->_sth);
  3750. if ($error !== false) {
  3751. $error = $error['code'];
  3752. }
  3753. return $error;
  3754. }
  3755. /**
  3756. * {@inheritdoc}
  3757. */
  3758. public function errorInfo()
  3759. {
  3760. return oci_error($this->_sth);
  3761. }
  3762. /**
  3763. * {@inheritdoc}
  3764. */
  3765. public function execute($params = null)
  3766. {
  3767. if ($params) {
  3768. $hasZeroIndex = isset($params[0]);
  3769. foreach ($params as $key => $val) {
  3770. if ($hasZeroIndex && is_numeric($key)) {
  3771. $this->bindValue($key + 1, $val);
  3772. } else {
  3773. $this->bindValue($key, $val);
  3774. }
  3775. }
  3776. }
  3777. $ret = @oci_execute($this->_sth, OCI_DEFAULT);
  3778. if ( ! $ret) {
  3779. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  3780. }
  3781. return $ret;
  3782. }
  3783. /**
  3784. * {@inheritdoc}
  3785. */
  3786. public function fetch($fetchStyle = PDO::FETCH_BOTH)
  3787. {
  3788. if ( ! isset(self::$fetchStyleMap[$fetchStyle])) {
  3789. throw new \InvalidArgumentException("Invalid fetch style: " . $fetchStyle);
  3790. }
  3791. return oci_fetch_array($this->_sth, self::$fetchStyleMap[$fetchStyle] | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
  3792. }
  3793. /**
  3794. * {@inheritdoc}
  3795. */
  3796. public function fetchAll($fetchStyle = PDO::FETCH_BOTH)
  3797. {
  3798. if ( ! isset(self::$fetchStyleMap[$fetchStyle])) {
  3799. throw new \InvalidArgumentException("Invalid fetch style: " . $fetchStyle);
  3800. }
  3801. $result = array();
  3802. oci_fetch_all($this->_sth, $result, 0, -1,
  3803. self::$fetchStyleMap[$fetchStyle] | OCI_RETURN_NULLS | OCI_FETCHSTATEMENT_BY_ROW | OCI_RETURN_LOBS);
  3804. return $result;
  3805. }
  3806. /**
  3807. * {@inheritdoc}
  3808. */
  3809. public function fetchColumn($columnIndex = 0)
  3810. {
  3811. $row = oci_fetch_array($this->_sth, OCI_NUM | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
  3812. return $row[$columnIndex];
  3813. }
  3814. /**
  3815. * {@inheritdoc}
  3816. */
  3817. public function rowCount()
  3818. {
  3819. return oci_num_rows($this->_sth);
  3820. }
  3821. }<?php
  3822. /*
  3823. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3824. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3825. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3826. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3827. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3828. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3829. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3830. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3831. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3832. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3833. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3834. *
  3835. * This software consists of voluntary contributions made by many individuals
  3836. * and is licensed under the LGPL. For more information, see
  3837. * <http://www.doctrine-project.org>.
  3838. */
  3839. namespace Doctrine\DBAL\Driver\OCI8;
  3840. /**
  3841. * OCI8 implementation of the Connection interface.
  3842. *
  3843. * @since 2.0
  3844. */
  3845. class OCI8Connection implements \Doctrine\DBAL\Driver\Connection
  3846. {
  3847. private $_dbh;
  3848. /**
  3849. * Create a Connection to an Oracle Database using oci8 extension.
  3850. *
  3851. * @param string $username
  3852. * @param string $password
  3853. * @param string $db
  3854. */
  3855. public function __construct($username, $password, $db)
  3856. {
  3857. $this->_dbh = @oci_connect($username, $password, $db);
  3858. if (!$this->_dbh) {
  3859. throw new OCI8Exception($this->errorInfo());
  3860. }
  3861. }
  3862. /**
  3863. * Create a non-executed prepared statement.
  3864. *
  3865. * @param string $prepareString
  3866. * @return OCI8Statement
  3867. */
  3868. public function prepare($prepareString)
  3869. {
  3870. return new OCI8Statement($this->_dbh, $prepareString);
  3871. }
  3872. /**
  3873. * @param string $sql
  3874. * @return OCI8Statement
  3875. */
  3876. public function query()
  3877. {
  3878. $args = func_get_args();
  3879. $sql = $args[0];
  3880. //$fetchMode = $args[1];
  3881. $stmt = $this->prepare($sql);
  3882. $stmt->execute();
  3883. return $stmt;
  3884. }
  3885. /**
  3886. * Quote input value.
  3887. *
  3888. * @param mixed $input
  3889. * @param int $type PDO::PARAM*
  3890. * @return mixed
  3891. */
  3892. public function quote($input, $type=\PDO::PARAM_STR)
  3893. {
  3894. return is_numeric($input) ? $input : "'$input'";
  3895. }
  3896. /**
  3897. *
  3898. * @param string $statement
  3899. * @return int
  3900. */
  3901. public function exec($statement)
  3902. {
  3903. $stmt = $this->prepare($statement);
  3904. $stmt->execute();
  3905. return $stmt->rowCount();
  3906. }
  3907. public function lastInsertId($name = null)
  3908. {
  3909. //TODO: throw exception or support sequences?
  3910. }
  3911. /**
  3912. * Start a transactiom
  3913. *
  3914. * Oracle has to explicitly set the autocommit mode off. That means
  3915. * after connection, a commit or rollback there is always automatically
  3916. * opened a new transaction.
  3917. *
  3918. * @return bool
  3919. */
  3920. public function beginTransaction()
  3921. {
  3922. return true;
  3923. }
  3924. /**
  3925. * @throws OCI8Exception
  3926. * @return bool
  3927. */
  3928. public function commit()
  3929. {
  3930. if (!oci_commit($this->_dbh)) {
  3931. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  3932. }
  3933. return true;
  3934. }
  3935. /**
  3936. * @throws OCI8Exception
  3937. * @return bool
  3938. */
  3939. public function rollBack()
  3940. {
  3941. if (!oci_rollback($this->_dbh)) {
  3942. throw OCI8Exception::fromErrorInfo($this->errorInfo());
  3943. }
  3944. return true;
  3945. }
  3946. public function errorCode()
  3947. {
  3948. $error = oci_error($this->_dbh);
  3949. if ($error !== false) {
  3950. $error = $error['code'];
  3951. }
  3952. return $error;
  3953. }
  3954. public function errorInfo()
  3955. {
  3956. return oci_error($this->_dbh);
  3957. }
  3958. }<?php
  3959. /*
  3960. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  3961. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  3962. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  3963. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  3964. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  3965. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3966. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  3967. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  3968. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3969. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  3970. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3971. *
  3972. * This software consists of voluntary contributions made by many individuals
  3973. * and is licensed under the LGPL. For more information, see
  3974. * <http://www.doctrine-project.org>.
  3975. */
  3976. namespace Doctrine\DBAL\Driver;
  3977. /**
  3978. * Connection interface.
  3979. * Driver connections must implement this interface.
  3980. *
  3981. * This resembles (a subset of) the PDO interface.
  3982. *
  3983. * @since 2.0
  3984. */
  3985. interface Connection
  3986. {
  3987. function prepare($prepareString);
  3988. function query();
  3989. function quote($input, $type=\PDO::PARAM_STR);
  3990. function exec($statement);
  3991. function lastInsertId($name = null);
  3992. function beginTransaction();
  3993. function commit();
  3994. function rollBack();
  3995. function errorCode();
  3996. function errorInfo();
  3997. }<?php
  3998. /*
  3999. * $Id$
  4000. *
  4001. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4002. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4003. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4004. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  4005. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  4006. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4007. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  4008. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  4009. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  4010. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  4011. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4012. *
  4013. * This software consists of voluntary contributions made by many individuals
  4014. * and is licensed under the LGPL. For more information, see
  4015. * <http://www.doctrine-project.org>.
  4016. */
  4017. namespace Doctrine\DBAL;
  4018. /**
  4019. * Container for all DBAL events.
  4020. *
  4021. * This class cannot be instantiated.
  4022. *
  4023. * @author Roman Borschel <roman@code-factory.org>
  4024. * @since 2.0
  4025. */
  4026. final class Events
  4027. {
  4028. private function __construct() {}
  4029. const postConnect = 'postConnect';
  4030. }
  4031. <?php
  4032. /*
  4033. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4034. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4035. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4036. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  4037. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  4038. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4039. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  4040. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  4041. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  4042. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  4043. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4044. *
  4045. * This software consists of voluntary contributions made by many individuals
  4046. * and is licensed under the LGPL. For more information, see
  4047. * <http://www.doctrine-project.org>.
  4048. */
  4049. namespace Doctrine\DBAL;
  4050. use Doctrine\Common\EventManager;
  4051. /**
  4052. * Factory for creating Doctrine\DBAL\Connection instances.
  4053. *
  4054. * @author Roman Borschel <roman@code-factory.org>
  4055. * @since 2.0
  4056. */
  4057. final class DriverManager
  4058. {
  4059. /**
  4060. * List of supported drivers and their mappings to the driver classes.
  4061. *
  4062. * @var array
  4063. * @todo REMOVE. Users should directly supply class names instead.
  4064. */
  4065. private static $_driverMap = array(
  4066. 'pdo_mysql' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
  4067. 'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
  4068. 'pdo_pgsql' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
  4069. 'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
  4070. 'pdo_mssql' => 'Doctrine\DBAL\Driver\PDOMsSql\Driver',
  4071. 'oci8' => 'Doctrine\DBAL\Driver\OCI8\Driver',
  4072. 'ibm_db2' => 'Doctrine\DBAL\Driver\IBMDB2\DB2Driver',
  4073. 'pdo_ibm' => 'Doctrine\DBAL\Driver\PDOIbm\Driver',
  4074. 'pdo_sqlsrv' => 'Doctrine\DBAL\Driver\PDOSqlsrv\Driver',
  4075. );
  4076. /** Private constructor. This class cannot be instantiated. */
  4077. private function __construct() { }
  4078. /**
  4079. * Creates a connection object based on the specified parameters.
  4080. * This method returns a Doctrine\DBAL\Connection which wraps the underlying
  4081. * driver connection.
  4082. *
  4083. * $params must contain at least one of the following.
  4084. *
  4085. * Either 'driver' with one of the following values:
  4086. * pdo_mysql
  4087. * pdo_sqlite
  4088. * pdo_pgsql
  4089. * pdo_oracle
  4090. * pdo_mssql
  4091. *
  4092. * OR 'driverClass' that contains the full class name (with namespace) of the
  4093. * driver class to instantiate.
  4094. *
  4095. * Other (optional) parameters:
  4096. *
  4097. * <b>user (string)</b>:
  4098. * The username to use when connecting.
  4099. *
  4100. * <b>password (string)</b>:
  4101. * The password to use when connecting.
  4102. *
  4103. * <b>driverOptions (array)</b>:
  4104. * Any additional driver-specific options for the driver. These are just passed
  4105. * through to the driver.
  4106. *
  4107. * <b>pdo</b>:
  4108. * You can pass an existing PDO instance through this parameter. The PDO
  4109. * instance will be wrapped in a Doctrine\DBAL\Connection.
  4110. *
  4111. * <b>wrapperClass</b>:
  4112. * You may specify a custom wrapper class through the 'wrapperClass'
  4113. * parameter but this class MUST inherit from Doctrine\DBAL\Connection.
  4114. *
  4115. * @param array $params The parameters.
  4116. * @param Doctrine\DBAL\Configuration The configuration to use.
  4117. * @param Doctrine\Common\EventManager The event manager to use.
  4118. * @return Doctrine\DBAL\Connection
  4119. */
  4120. public static function getConnection(
  4121. array $params,
  4122. Configuration $config = null,
  4123. EventManager $eventManager = null)
  4124. {
  4125. // create default config and event manager, if not set
  4126. if ( ! $config) {
  4127. $config = new Configuration();
  4128. }
  4129. if ( ! $eventManager) {
  4130. $eventManager = new EventManager();
  4131. }
  4132. // check for existing pdo object
  4133. if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
  4134. throw DBALException::invalidPdoInstance();
  4135. } else if (isset($params['pdo'])) {
  4136. $params['pdo']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  4137. $params['driver'] = 'pdo_' . $params['pdo']->getAttribute(\PDO::ATTR_DRIVER_NAME);
  4138. } else {
  4139. self::_checkParams($params);
  4140. }
  4141. if (isset($params['driverClass'])) {
  4142. $className = $params['driverClass'];
  4143. } else {
  4144. $className = self::$_driverMap[$params['driver']];
  4145. }
  4146. $driver = new $className();
  4147. $wrapperClass = 'Doctrine\DBAL\Connection';
  4148. if (isset($params['wrapperClass'])) {
  4149. if (is_subclass_of($params['wrapperClass'], $wrapperClass)) {
  4150. $wrapperClass = $params['wrapperClass'];
  4151. } else {
  4152. throw DBALException::invalidWrapperClass($params['wrapperClass']);
  4153. }
  4154. }
  4155. return new $wrapperClass($params, $driver, $config, $eventManager);
  4156. }
  4157. /**
  4158. * Checks the list of parameters.
  4159. *
  4160. * @param array $params
  4161. */
  4162. private static function _checkParams(array $params)
  4163. {
  4164. // check existance of mandatory parameters
  4165. // driver
  4166. if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
  4167. throw DBALException::driverRequired();
  4168. }
  4169. // check validity of parameters
  4170. // driver
  4171. if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
  4172. throw DBALException::unknownDriver($params['driver'], array_keys(self::$_driverMap));
  4173. }
  4174. if (isset($params['driverClass']) && ! in_array('Doctrine\DBAL\Driver', class_implements($params['driverClass'], true))) {
  4175. throw DBALException::invalidDriverClass($params['driverClass']);
  4176. }
  4177. }
  4178. }<?php
  4179. /*
  4180. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4181. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4182. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4183. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  4184. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  4185. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4186. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  4187. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  4188. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  4189. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  4190. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4191. *
  4192. * This software consists of voluntary contributions made by many individuals
  4193. * and is licensed under the LGPL. For more information, see
  4194. * <http://www.doctrine-project.org>.
  4195. */
  4196. namespace Doctrine\DBAL;
  4197. use Doctrine\DBAL\Logging\SQLLogger;
  4198. /**
  4199. * Configuration container for the Doctrine DBAL.
  4200. *
  4201. * @since 2.0
  4202. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  4203. * @author Jonathan Wage <jonwage@gmail.com>
  4204. * @author Roman Borschel <roman@code-factory.org>
  4205. * @internal When adding a new configuration option just write a getter/setter
  4206. * pair and add the option to the _attributes array with a proper default value.
  4207. */
  4208. class Configuration
  4209. {
  4210. /**
  4211. * The attributes that are contained in the configuration.
  4212. * Values are default values.
  4213. *
  4214. * @var array
  4215. */
  4216. protected $_attributes = array();
  4217. /**
  4218. * Sets the SQL logger to use. Defaults to NULL which means SQL logging is disabled.
  4219. *
  4220. * @param SQLLogger $logger
  4221. */
  4222. public function setSQLLogger(SQLLogger $logger)
  4223. {
  4224. $this->_attributes['sqlLogger'] = $logger;
  4225. }
  4226. /**
  4227. * Gets the SQL logger that is used.
  4228. *
  4229. * @return SQLLogger
  4230. */
  4231. public function getSQLLogger()
  4232. {
  4233. return isset($this->_attributes['sqlLogger']) ?
  4234. $this->_attributes['sqlLogger'] : null;
  4235. }
  4236. }<?php
  4237. /*
  4238. * $Id$
  4239. *
  4240. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4241. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4242. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4243. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  4244. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  4245. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4246. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  4247. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  4248. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  4249. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  4250. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4251. *
  4252. * This software consists of voluntary contributions made by many individuals
  4253. * and is licensed under the LGPL. For more information, see
  4254. * <http://www.doctrine-project.org>.
  4255. */
  4256. namespace Doctrine\DBAL;
  4257. use PDO,
  4258. Doctrine\DBAL\Types\Type,
  4259. Doctrine\DBAL\Driver\Statement as DriverStatement;
  4260. /**
  4261. * A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
  4262. * for logging, DBAL mapping types, etc.
  4263. *
  4264. * @author Roman Borschel <roman@code-factory.org>
  4265. * @since 2.0
  4266. */
  4267. class Statement implements DriverStatement
  4268. {
  4269. /**
  4270. * @var string The SQL statement.
  4271. */
  4272. private $_sql;
  4273. /**
  4274. * @var array The bound parameters.
  4275. */
  4276. private $_params = array();
  4277. /**
  4278. * @var Doctrine\DBAL\Driver\Statement The underlying driver statement.
  4279. */
  4280. private $_stmt;
  4281. /**
  4282. * @var Doctrine\DBAL\Platforms\AbstractPlatform The underlying database platform.
  4283. */
  4284. private $_platform;
  4285. /**
  4286. * @var Doctrine\DBAL\Connection The connection this statement is bound to and executed on.
  4287. */
  4288. private $_conn;
  4289. /**
  4290. * Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
  4291. *
  4292. * @param string $sql The SQL of the statement.
  4293. * @param Doctrine\DBAL\Connection The connection on which the statement should be executed.
  4294. */
  4295. public function __construct($sql, Connection $conn)
  4296. {
  4297. $this->_sql = $sql;
  4298. $this->_stmt = $conn->getWrappedConnection()->prepare($sql);
  4299. $this->_conn = $conn;
  4300. $this->_platform = $conn->getDatabasePlatform();
  4301. }
  4302. /**
  4303. * Binds a parameter value to the statement.
  4304. *
  4305. * The value can optionally be bound with a PDO binding type or a DBAL mapping type.
  4306. * If bound with a DBAL mapping type, the binding type is derived from the mapping
  4307. * type and the value undergoes the conversion routines of the mapping type before
  4308. * being bound.
  4309. *
  4310. * @param $name The name or position of the parameter.
  4311. * @param $value The value of the parameter.
  4312. * @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance.
  4313. * @return boolean TRUE on success, FALSE on failure.
  4314. */
  4315. public function bindValue($name, $value, $type = null)
  4316. {
  4317. $this->_params[$name] = $value;
  4318. if ($type !== null) {
  4319. if (is_string($type)) {
  4320. $type = Type::getType($type);
  4321. }
  4322. if ($type instanceof Type) {
  4323. $value = $type->convertToDatabaseValue($value, $this->_platform);
  4324. $bindingType = $type->getBindingType();
  4325. } else {
  4326. $bindingType = $type; // PDO::PARAM_* constants
  4327. }
  4328. return $this->_stmt->bindValue($name, $value, $bindingType);
  4329. } else {
  4330. return $this->_stmt->bindValue($name, $value);
  4331. }
  4332. }
  4333. /**
  4334. * Binds a parameter to a value by reference.
  4335. *
  4336. * Binding a parameter by reference does not support DBAL mapping types.
  4337. *
  4338. * @param string $name The name or position of the parameter.
  4339. * @param mixed $value The reference to the variable to bind
  4340. * @param integer $type The PDO binding type.
  4341. * @return boolean TRUE on success, FALSE on failure.
  4342. */
  4343. public function bindParam($name, &$var, $type = PDO::PARAM_STR)
  4344. {
  4345. return $this->_stmt->bindParam($name, $var, $type);
  4346. }
  4347. /**
  4348. * Executes the statement with the currently bound parameters.
  4349. *
  4350. * @return boolean TRUE on success, FALSE on failure.
  4351. */
  4352. public function execute($params = null)
  4353. {
  4354. $hasLogger = $this->_conn->getConfiguration()->getSQLLogger();
  4355. if ($hasLogger) {
  4356. $this->_conn->getConfiguration()->getSQLLogger()->startQuery($this->_sql, $this->_params);
  4357. }
  4358. $stmt = $this->_stmt->execute($params);
  4359. if ($hasLogger) {
  4360. $this->_conn->getConfiguration()->getSQLLogger()->stopQuery();
  4361. }
  4362. $this->_params = array();
  4363. return $stmt;
  4364. }
  4365. /**
  4366. * Closes the cursor, freeing the database resources used by this statement.
  4367. *
  4368. * @return boolean TRUE on success, FALSE on failure.
  4369. */
  4370. public function closeCursor()
  4371. {
  4372. return $this->_stmt->closeCursor();
  4373. }
  4374. /**
  4375. * Returns the number of columns in the result set.
  4376. *
  4377. * @return integer
  4378. */
  4379. public function columnCount()
  4380. {
  4381. return $this->_stmt->columnCount();
  4382. }
  4383. /**
  4384. * Fetches the SQLSTATE associated with the last operation on the statement.
  4385. *
  4386. * @return string
  4387. */
  4388. public function errorCode()
  4389. {
  4390. return $this->_stmt->errorCode();
  4391. }
  4392. /**
  4393. * Fetches extended error information associated with the last operation on the statement.
  4394. *
  4395. * @return array
  4396. */
  4397. public function errorInfo()
  4398. {
  4399. return $this->_stmt->errorInfo();
  4400. }
  4401. /**
  4402. * Fetches the next row from a result set.
  4403. *
  4404. * @param integer $fetchStyle
  4405. * @return mixed The return value of this function on success depends on the fetch type.
  4406. * In all cases, FALSE is returned on failure.
  4407. */
  4408. public function fetch($fetchStyle = PDO::FETCH_BOTH)
  4409. {
  4410. return $this->_stmt->fetch($fetchStyle);
  4411. }
  4412. /**
  4413. * Returns an array containing all of the result set rows.
  4414. *
  4415. * @param integer $fetchStyle
  4416. * @param integer $columnIndex
  4417. * @return array An array containing all of the remaining rows in the result set.
  4418. */
  4419. public function fetchAll($fetchStyle = PDO::FETCH_BOTH, $columnIndex = 0)
  4420. {
  4421. if ($columnIndex != 0) {
  4422. return $this->_stmt->fetchAll($fetchStyle, $columnIndex);
  4423. }
  4424. return $this->_stmt->fetchAll($fetchStyle);
  4425. }
  4426. /**
  4427. * Returns a single column from the next row of a result set.
  4428. *
  4429. * @param integer $columnIndex
  4430. * @return mixed A single column from the next row of a result set or FALSE if there are no more rows.
  4431. */
  4432. public function fetchColumn($columnIndex = 0)
  4433. {
  4434. return $this->_stmt->fetchColumn($columnIndex);
  4435. }
  4436. /**
  4437. * Returns the number of rows affected by the last execution of this statement.
  4438. *
  4439. * @return integer The number of affected rows.
  4440. */
  4441. public function rowCount()
  4442. {
  4443. return $this->_stmt->rowCount();
  4444. }
  4445. /**
  4446. * Gets the wrapped driver statement.
  4447. *
  4448. * @return Doctrine\DBAL\Driver\Statement
  4449. */
  4450. public function getWrappedStatement()
  4451. {
  4452. return $this->_stmt;
  4453. }
  4454. }<?php
  4455. /*
  4456. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4457. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4458. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4459. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  4460. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  4461. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4462. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  4463. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  4464. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  4465. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  4466. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4467. *
  4468. * This software consists of voluntary contributions made by many individuals
  4469. * and is licensed under the LGPL. For more information, see
  4470. * <http://www.doctrine-project.org>.
  4471. */
  4472. namespace Doctrine\DBAL\Schema;
  4473. /**
  4474. * Schema manager for the MySql RDBMS.
  4475. *
  4476. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  4477. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  4478. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  4479. * @author Roman Borschel <roman@code-factory.org>
  4480. * @author Benjamin Eberlei <kontakt@beberlei.de>
  4481. * @version $Revision$
  4482. * @since 2.0
  4483. */
  4484. class MySqlSchemaManager extends AbstractSchemaManager
  4485. {
  4486. protected function _getPortableViewDefinition($view)
  4487. {
  4488. return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
  4489. }
  4490. protected function _getPortableTableDefinition($table)
  4491. {
  4492. return array_shift($table);
  4493. }
  4494. protected function _getPortableUserDefinition($user)
  4495. {
  4496. return array(
  4497. 'user' => $user['User'],
  4498. 'password' => $user['Password'],
  4499. );
  4500. }
  4501. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  4502. {
  4503. foreach($tableIndexes AS $k => $v) {
  4504. $v = array_change_key_case($v, CASE_LOWER);
  4505. if($v['key_name'] == 'PRIMARY') {
  4506. $v['primary'] = true;
  4507. } else {
  4508. $v['primary'] = false;
  4509. }
  4510. $tableIndexes[$k] = $v;
  4511. }
  4512. return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
  4513. }
  4514. protected function _getPortableSequenceDefinition($sequence)
  4515. {
  4516. return end($sequence);
  4517. }
  4518. protected function _getPortableDatabaseDefinition($database)
  4519. {
  4520. return $database['Database'];
  4521. }
  4522. /**
  4523. * Gets a portable column definition.
  4524. *
  4525. * The database type is mapped to a corresponding Doctrine mapping type.
  4526. *
  4527. * @param $tableColumn
  4528. * @return array
  4529. */
  4530. protected function _getPortableTableColumnDefinition($tableColumn)
  4531. {
  4532. $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
  4533. $dbType = strtolower($tableColumn['type']);
  4534. $dbType = strtok($dbType, '(), ');
  4535. if (isset($tableColumn['length'])) {
  4536. $length = $tableColumn['length'];
  4537. $decimal = '';
  4538. } else {
  4539. $length = strtok('(), ');
  4540. $decimal = strtok('(), ') ? strtok('(), '):null;
  4541. }
  4542. $type = array();
  4543. $unsigned = $fixed = null;
  4544. if ( ! isset($tableColumn['name'])) {
  4545. $tableColumn['name'] = '';
  4546. }
  4547. $scale = null;
  4548. $precision = null;
  4549. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  4550. switch ($dbType) {
  4551. case 'char':
  4552. $fixed = true;
  4553. break;
  4554. case 'float':
  4555. case 'double':
  4556. case 'real':
  4557. case 'numeric':
  4558. case 'decimal':
  4559. if(preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
  4560. $precision = $match[1];
  4561. $scale = $match[2];
  4562. $length = null;
  4563. }
  4564. break;
  4565. case 'tinyint':
  4566. case 'smallint':
  4567. case 'mediumint':
  4568. case 'int':
  4569. case 'integer':
  4570. case 'bigint':
  4571. case 'tinyblob':
  4572. case 'mediumblob':
  4573. case 'longblob':
  4574. case 'blob':
  4575. case 'binary':
  4576. case 'varbinary':
  4577. case 'year':
  4578. $length = null;
  4579. break;
  4580. }
  4581. $length = ((int) $length == 0) ? null : (int) $length;
  4582. $def = array(
  4583. 'type' => $type,
  4584. 'length' => $length,
  4585. 'unsigned' => (bool) $unsigned,
  4586. 'fixed' => (bool) $fixed
  4587. );
  4588. $options = array(
  4589. 'length' => $length,
  4590. 'unsigned' => (bool)$unsigned,
  4591. 'fixed' => (bool)$fixed,
  4592. 'default' => $tableColumn['default'],
  4593. 'notnull' => (bool) ($tableColumn['null'] != 'YES'),
  4594. 'scale' => null,
  4595. 'precision' => null,
  4596. 'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false),
  4597. );
  4598. if ($scale !== null && $precision !== null) {
  4599. $options['scale'] = $scale;
  4600. $options['precision'] = $precision;
  4601. }
  4602. return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  4603. }
  4604. public function _getPortableTableForeignKeyDefinition($tableForeignKey)
  4605. {
  4606. $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
  4607. if (!isset($tableForeignKey['delete_rule']) || $tableForeignKey['delete_rule'] == "RESTRICT") {
  4608. $tableForeignKey['delete_rule'] = null;
  4609. }
  4610. if (!isset($tableForeignKey['update_rule']) || $tableForeignKey['update_rule'] == "RESTRICT") {
  4611. $tableForeignKey['update_rule'] = null;
  4612. }
  4613. return new ForeignKeyConstraint(
  4614. (array)$tableForeignKey['column_name'],
  4615. $tableForeignKey['referenced_table_name'],
  4616. (array)$tableForeignKey['referenced_column_name'],
  4617. $tableForeignKey['constraint_name'],
  4618. array(
  4619. 'onUpdate' => $tableForeignKey['update_rule'],
  4620. 'onDelete' => $tableForeignKey['delete_rule'],
  4621. )
  4622. );
  4623. }
  4624. }<?php
  4625. /*
  4626. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4627. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4628. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4629. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  4630. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  4631. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4632. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  4633. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  4634. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  4635. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  4636. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4637. *
  4638. * This software consists of voluntary contributions made by many individuals
  4639. * and is licensed under the LGPL. For more information, see
  4640. * <http://www.doctrine-project.org>.
  4641. */
  4642. namespace Doctrine\DBAL\Schema;
  4643. /**
  4644. * xxx
  4645. *
  4646. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  4647. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  4648. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  4649. * @author Benjamin Eberlei <kontakt@beberlei.de>
  4650. * @version $Revision$
  4651. * @since 2.0
  4652. */
  4653. class PostgreSqlSchemaManager extends AbstractSchemaManager
  4654. {
  4655. protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
  4656. {
  4657. $onUpdate = null;
  4658. $onDelete = null;
  4659. if (preg_match('(ON UPDATE ([a-zA-Z0-9]+))', $tableForeignKey['condef'], $match)) {
  4660. $onUpdate = $match[1];
  4661. }
  4662. if (preg_match('(ON DELETE ([a-zA-Z0-9]+))', $tableForeignKey['condef'], $match)) {
  4663. $onDelete = $match[1];
  4664. }
  4665. if (preg_match('/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)/', $tableForeignKey['condef'], $values)) {
  4666. $localColumns = array_map('trim', explode(",", $values[1]));
  4667. $foreignColumns = array_map('trim', explode(",", $values[3]));
  4668. $foreignTable = $values[2];
  4669. }
  4670. return new ForeignKeyConstraint(
  4671. $localColumns, $foreignTable, $foreignColumns, $tableForeignKey['conname'],
  4672. array('onUpdate' => $onUpdate, 'onDelete' => $onDelete)
  4673. );
  4674. }
  4675. public function dropDatabase($database)
  4676. {
  4677. $params = $this->_conn->getParams();
  4678. $params["dbname"] = "postgres";
  4679. $tmpPlatform = $this->_platform;
  4680. $tmpConn = $this->_conn;
  4681. $this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params);
  4682. $this->_platform = $this->_conn->getDatabasePlatform();
  4683. parent::dropDatabase($database);
  4684. $this->_platform = $tmpPlatform;
  4685. $this->_conn = $tmpConn;
  4686. }
  4687. public function createDatabase($database)
  4688. {
  4689. $params = $this->_conn->getParams();
  4690. $params["dbname"] = "postgres";
  4691. $tmpPlatform = $this->_platform;
  4692. $tmpConn = $this->_conn;
  4693. $this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params);
  4694. $this->_platform = $this->_conn->getDatabasePlatform();
  4695. parent::createDatabase($database);
  4696. $this->_platform = $tmpPlatform;
  4697. $this->_conn = $tmpConn;
  4698. }
  4699. protected function _getPortableTriggerDefinition($trigger)
  4700. {
  4701. return $trigger['trigger_name'];
  4702. }
  4703. protected function _getPortableViewDefinition($view)
  4704. {
  4705. return new View($view['viewname'], $view['definition']);
  4706. }
  4707. protected function _getPortableUserDefinition($user)
  4708. {
  4709. return array(
  4710. 'user' => $user['usename'],
  4711. 'password' => $user['passwd']
  4712. );
  4713. }
  4714. protected function _getPortableTableDefinition($table)
  4715. {
  4716. return $table['table_name'];
  4717. }
  4718. /**
  4719. * @license New BSD License
  4720. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  4721. * @param array $tableIndexes
  4722. * @param string $tableName
  4723. * @return array
  4724. */
  4725. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  4726. {
  4727. $buffer = array();
  4728. foreach ($tableIndexes AS $row) {
  4729. $colNumbers = explode(' ', $row['indkey']);
  4730. $colNumbersSql = 'IN (' . join(' ,', $colNumbers) . ' )';
  4731. $columnNameSql = "SELECT attnum, attname FROM pg_attribute
  4732. WHERE attrelid={$row['indrelid']} AND attnum $colNumbersSql ORDER BY attnum ASC;";
  4733. $stmt = $this->_conn->executeQuery($columnNameSql);
  4734. $indexColumns = $stmt->fetchAll();
  4735. // required for getting the order of the columns right.
  4736. foreach ($colNumbers AS $colNum) {
  4737. foreach ($indexColumns as $colRow) {
  4738. if ($colNum == $colRow['attnum']) {
  4739. $buffer[] = array(
  4740. 'key_name' => $row['relname'],
  4741. 'column_name' => trim($colRow['attname']),
  4742. 'non_unique' => !$row['indisunique'],
  4743. 'primary' => $row['indisprimary']
  4744. );
  4745. }
  4746. }
  4747. }
  4748. }
  4749. return parent::_getPortableTableIndexesList($buffer);
  4750. }
  4751. protected function _getPortableDatabaseDefinition($database)
  4752. {
  4753. return $database['datname'];
  4754. }
  4755. protected function _getPortableSequenceDefinition($sequence)
  4756. {
  4757. $data = $this->_conn->fetchAll('SELECT min_value, increment_by FROM ' . $sequence['relname']);
  4758. return new Sequence($sequence['relname'], $data[0]['increment_by'], $data[0]['min_value']);
  4759. }
  4760. protected function _getPortableTableColumnDefinition($tableColumn)
  4761. {
  4762. $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
  4763. if (strtolower($tableColumn['type']) === 'varchar') {
  4764. // get length from varchar definition
  4765. $length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $tableColumn['complete_type']);
  4766. $tableColumn['length'] = $length;
  4767. }
  4768. $matches = array();
  4769. $autoincrement = false;
  4770. if (preg_match("/^nextval\('(.*)'(::.*)?\)$/", $tableColumn['default'], $matches)) {
  4771. $tableColumn['sequence'] = $matches[1];
  4772. $tableColumn['default'] = null;
  4773. $autoincrement = true;
  4774. }
  4775. if (stripos($tableColumn['default'], 'NULL') === 0) {
  4776. $tableColumn['default'] = null;
  4777. }
  4778. $length = (isset($tableColumn['length'])) ? $tableColumn['length'] : null;
  4779. if ($length == '-1' && isset($tableColumn['atttypmod'])) {
  4780. $length = $tableColumn['atttypmod'] - 4;
  4781. }
  4782. if ((int) $length <= 0) {
  4783. $length = null;
  4784. }
  4785. $type = array();
  4786. $fixed = null;
  4787. if (!isset($tableColumn['name'])) {
  4788. $tableColumn['name'] = '';
  4789. }
  4790. $precision = null;
  4791. $scale = null;
  4792. if ($this->_platform->hasDoctrineTypeMappingFor($tableColumn['type'])) {
  4793. $dbType = strtolower($tableColumn['type']);
  4794. } else {
  4795. $dbType = strtolower($tableColumn['domain_type']);
  4796. $tableColumn['complete_type'] = $tableColumn['domain_complete_type'];
  4797. }
  4798. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  4799. switch ($dbType) {
  4800. case 'smallint':
  4801. case 'int2':
  4802. $length = null;
  4803. break;
  4804. case 'int':
  4805. case 'int4':
  4806. case 'integer':
  4807. $length = null;
  4808. break;
  4809. case 'bigint':
  4810. case 'int8':
  4811. $length = null;
  4812. break;
  4813. case 'bool':
  4814. case 'boolean':
  4815. $length = null;
  4816. break;
  4817. case 'text':
  4818. $fixed = false;
  4819. break;
  4820. case 'varchar':
  4821. case 'interval':
  4822. case '_varchar':
  4823. $fixed = false;
  4824. break;
  4825. case 'char':
  4826. case 'bpchar':
  4827. $fixed = true;
  4828. break;
  4829. case 'float':
  4830. case 'float4':
  4831. case 'float8':
  4832. case 'double':
  4833. case 'double precision':
  4834. case 'real':
  4835. case 'decimal':
  4836. case 'money':
  4837. case 'numeric':
  4838. if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) {
  4839. $precision = $match[1];
  4840. $scale = $match[2];
  4841. $length = null;
  4842. }
  4843. break;
  4844. case 'year':
  4845. $length = null;
  4846. break;
  4847. }
  4848. $options = array(
  4849. 'length' => $length,
  4850. 'notnull' => (bool) $tableColumn['isnotnull'],
  4851. 'default' => $tableColumn['default'],
  4852. 'primary' => (bool) ($tableColumn['pri'] == 't'),
  4853. 'precision' => $precision,
  4854. 'scale' => $scale,
  4855. 'fixed' => $fixed,
  4856. 'unsigned' => false,
  4857. 'autoincrement' => $autoincrement,
  4858. );
  4859. return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  4860. }
  4861. }<?php
  4862. /*
  4863. * $Id$
  4864. *
  4865. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4866. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4867. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4868. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  4869. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  4870. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4871. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  4872. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  4873. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  4874. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  4875. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4876. *
  4877. * This software consists of voluntary contributions made by many individuals
  4878. * and is licensed under the LGPL. For more information, see
  4879. * <http://www.doctrine-project.org>.
  4880. */
  4881. namespace Doctrine\DBAL\Schema;
  4882. /**
  4883. * Representation of a Database View
  4884. *
  4885. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  4886. * @link www.doctrine-project.com
  4887. * @since 1.0
  4888. * @version $Revision$
  4889. * @author Benjamin Eberlei <kontakt@beberlei.de>
  4890. */
  4891. class View extends AbstractAsset
  4892. {
  4893. /**
  4894. * @var string
  4895. */
  4896. private $_sql;
  4897. public function __construct($name, $sql)
  4898. {
  4899. $this->_setName($name);
  4900. $this->_sql = $sql;
  4901. }
  4902. /**
  4903. * @return string
  4904. */
  4905. public function getSql()
  4906. {
  4907. return $this->_sql;
  4908. }
  4909. }<?php
  4910. /*
  4911. * $Id$
  4912. *
  4913. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4914. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  4915. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4916. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  4917. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  4918. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4919. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  4920. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  4921. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  4922. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  4923. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4924. *
  4925. * This software consists of voluntary contributions made by many individuals
  4926. * and is licensed under the LGPL. For more information, see
  4927. * <http://www.doctrine-project.org>.
  4928. */
  4929. namespace Doctrine\DBAL\Schema;
  4930. /**
  4931. * IBM Db2 Schema Manager
  4932. *
  4933. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  4934. * @link www.doctrine-project.com
  4935. * @since 1.0
  4936. * @version $Revision$
  4937. * @author Benjamin Eberlei <kontakt@beberlei.de>
  4938. */
  4939. class DB2SchemaManager extends AbstractSchemaManager
  4940. {
  4941. /**
  4942. * Return a list of all tables in the current database
  4943. *
  4944. * Apparently creator is the schema not the user who created it:
  4945. * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
  4946. *
  4947. * @return array
  4948. */
  4949. public function listTableNames()
  4950. {
  4951. $sql = $this->_platform->getListTablesSQL();
  4952. $sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')";
  4953. $tables = $this->_conn->fetchAll($sql);
  4954. return $this->_getPortableTablesList($tables);
  4955. }
  4956. /**
  4957. * Get Table Column Definition
  4958. *
  4959. * @param array $tableColumn
  4960. * @return Column
  4961. */
  4962. protected function _getPortableTableColumnDefinition($tableColumn)
  4963. {
  4964. $tableColumn = array_change_key_case($tableColumn, \CASE_LOWER);
  4965. $length = null;
  4966. $fixed = null;
  4967. $unsigned = false;
  4968. $scale = false;
  4969. $precision = false;
  4970. $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
  4971. switch (strtolower($tableColumn['typename'])) {
  4972. case 'varchar':
  4973. $length = $tableColumn['length'];
  4974. $fixed = false;
  4975. break;
  4976. case 'character':
  4977. $length = $tableColumn['length'];
  4978. $fixed = true;
  4979. break;
  4980. case 'clob':
  4981. $length = $tableColumn['length'];
  4982. break;
  4983. case 'decimal':
  4984. case 'double':
  4985. case 'real':
  4986. $scale = $tableColumn['scale'];
  4987. $precision = $tableColumn['length'];
  4988. break;
  4989. }
  4990. $options = array(
  4991. 'length' => $length,
  4992. 'unsigned' => (bool)$unsigned,
  4993. 'fixed' => (bool)$fixed,
  4994. 'default' => ($tableColumn['default'] == "NULL") ? null : $tableColumn['default'],
  4995. 'notnull' => (bool) ($tableColumn['nulls'] == 'N'),
  4996. 'scale' => null,
  4997. 'precision' => null,
  4998. 'platformOptions' => array(),
  4999. );
  5000. if ($scale !== null && $precision !== null) {
  5001. $options['scale'] = $scale;
  5002. $options['precision'] = $precision;
  5003. }
  5004. return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  5005. }
  5006. protected function _getPortableTablesList($tables)
  5007. {
  5008. $tableNames = array();
  5009. foreach ($tables AS $tableRow) {
  5010. $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
  5011. $tableNames[] = $tableRow['name'];
  5012. }
  5013. return $tableNames;
  5014. }
  5015. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  5016. {
  5017. $tableIndexRows = array();
  5018. $indexes = array();
  5019. foreach($tableIndexes AS $indexKey => $data) {
  5020. $data = array_change_key_case($data, \CASE_LOWER);
  5021. $unique = ($data['uniquerule'] == "D") ? false : true;
  5022. $primary = ($data['uniquerule'] == "P");
  5023. $indexName = strtolower($data['name']);
  5024. if ($primary) {
  5025. $keyName = 'primary';
  5026. } else {
  5027. $keyName = $indexName;
  5028. }
  5029. $indexes[$keyName] = new Index($indexName, explode("+", ltrim($data['colnames'], '+')), $unique, $primary);
  5030. }
  5031. return $indexes;
  5032. }
  5033. protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
  5034. {
  5035. $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
  5036. $tableForeignKey['deleterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['deleterule']);
  5037. $tableForeignKey['updaterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['updaterule']);
  5038. return new ForeignKeyConstraint(
  5039. array_map('trim', (array)$tableForeignKey['fkcolnames']),
  5040. $tableForeignKey['reftbname'],
  5041. array_map('trim', (array)$tableForeignKey['pkcolnames']),
  5042. $tableForeignKey['relname'],
  5043. array(
  5044. 'onUpdate' => $tableForeignKey['updaterule'],
  5045. 'onDelete' => $tableForeignKey['deleterule'],
  5046. )
  5047. );
  5048. }
  5049. protected function _getPortableForeignKeyRuleDef($def)
  5050. {
  5051. if ($def == "C") {
  5052. return "CASCADE";
  5053. } else if ($def == "N") {
  5054. return "SET NULL";
  5055. }
  5056. return null;
  5057. }
  5058. protected function _getPortableViewDefinition($view)
  5059. {
  5060. $view = array_change_key_case($view, \CASE_LOWER);
  5061. // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
  5062. //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
  5063. if (!is_resource($view['text'])) {
  5064. $pos = strpos($view['text'], ' AS ');
  5065. $sql = substr($view['text'], $pos+4);
  5066. } else {
  5067. $sql = '';
  5068. }
  5069. return new View($view['name'], $sql);
  5070. }
  5071. }<?php
  5072. /*
  5073. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  5074. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5075. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  5076. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  5077. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  5078. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5079. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  5080. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  5081. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5082. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  5083. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5084. *
  5085. * This software consists of voluntary contributions made by many individuals
  5086. * and is licensed under the LGPL. For more information, see
  5087. * <http://www.doctrine-project.org>.
  5088. */
  5089. namespace Doctrine\DBAL\Schema;
  5090. /**
  5091. * SqliteSchemaManager
  5092. *
  5093. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  5094. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  5095. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  5096. * @author Jonathan H. Wage <jonwage@gmail.com>
  5097. * @version $Revision$
  5098. * @since 2.0
  5099. */
  5100. class SqliteSchemaManager extends AbstractSchemaManager
  5101. {
  5102. /**
  5103. * {@inheritdoc}
  5104. *
  5105. * @override
  5106. */
  5107. public function dropDatabase($database)
  5108. {
  5109. if (file_exists($database)) {
  5110. unlink($database);
  5111. }
  5112. }
  5113. /**
  5114. * {@inheritdoc}
  5115. *
  5116. * @override
  5117. */
  5118. public function createDatabase($database)
  5119. {
  5120. $params = $this->_conn->getParams();
  5121. $driver = $params['driver'];
  5122. $options = array(
  5123. 'driver' => $driver,
  5124. 'path' => $database
  5125. );
  5126. $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
  5127. $conn->connect();
  5128. $conn->close();
  5129. }
  5130. protected function _getPortableTableDefinition($table)
  5131. {
  5132. return $table['name'];
  5133. }
  5134. /**
  5135. * @license New BSD License
  5136. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  5137. * @param array $tableIndexes
  5138. * @param string $tableName
  5139. * @return array
  5140. */
  5141. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  5142. {
  5143. $indexBuffer = array();
  5144. // fetch primary
  5145. $stmt = $this->_conn->executeQuery( "PRAGMA TABLE_INFO ('$tableName')" );
  5146. $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  5147. foreach($indexArray AS $indexColumnRow) {
  5148. if($indexColumnRow['pk'] == "1") {
  5149. $indexBuffer[] = array(
  5150. 'key_name' => 'primary',
  5151. 'primary' => true,
  5152. 'non_unique' => false,
  5153. 'column_name' => $indexColumnRow['name']
  5154. );
  5155. }
  5156. }
  5157. // fetch regular indexes
  5158. foreach($tableIndexes AS $tableIndex) {
  5159. $keyName = $tableIndex['name'];
  5160. $idx = array();
  5161. $idx['key_name'] = $keyName;
  5162. $idx['primary'] = false;
  5163. $idx['non_unique'] = $tableIndex['unique']?false:true;
  5164. $stmt = $this->_conn->executeQuery( "PRAGMA INDEX_INFO ( '{$keyName}' )" );
  5165. $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  5166. foreach ( $indexArray as $indexColumnRow ) {
  5167. $idx['column_name'] = $indexColumnRow['name'];
  5168. $indexBuffer[] = $idx;
  5169. }
  5170. }
  5171. return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
  5172. }
  5173. protected function _getPortableTableIndexDefinition($tableIndex)
  5174. {
  5175. return array(
  5176. 'name' => $tableIndex['name'],
  5177. 'unique' => (bool) $tableIndex['unique']
  5178. );
  5179. }
  5180. protected function _getPortableTableColumnDefinition($tableColumn)
  5181. {
  5182. $e = explode('(', $tableColumn['type']);
  5183. $tableColumn['type'] = $e[0];
  5184. if (isset($e[1])) {
  5185. $length = trim($e[1], ')');
  5186. $tableColumn['length'] = $length;
  5187. }
  5188. $dbType = strtolower($tableColumn['type']);
  5189. $length = isset($tableColumn['length']) ? $tableColumn['length'] : null;
  5190. $unsigned = (boolean) isset($tableColumn['unsigned']) ? $tableColumn['unsigned'] : false;
  5191. $fixed = false;
  5192. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  5193. $default = $tableColumn['dflt_value'];
  5194. if ($default == 'NULL') {
  5195. $default = null;
  5196. }
  5197. $notnull = (bool) $tableColumn['notnull'];
  5198. if ( ! isset($tableColumn['name'])) {
  5199. $tableColumn['name'] = '';
  5200. }
  5201. $precision = null;
  5202. $scale = null;
  5203. switch ($dbType) {
  5204. case 'char':
  5205. $fixed = true;
  5206. break;
  5207. case 'float':
  5208. case 'double':
  5209. case 'real':
  5210. case 'decimal':
  5211. case 'numeric':
  5212. list($precision, $scale) = array_map('trim', explode(', ', $tableColumn['length']));
  5213. $length = null;
  5214. break;
  5215. }
  5216. $options = array(
  5217. 'length' => $length,
  5218. 'unsigned' => (bool) $unsigned,
  5219. 'fixed' => $fixed,
  5220. 'notnull' => $notnull,
  5221. 'default' => $default,
  5222. 'precision' => $precision,
  5223. 'scale' => $scale,
  5224. 'autoincrement' => (bool) $tableColumn['pk'],
  5225. );
  5226. return new Column($tableColumn['name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  5227. }
  5228. protected function _getPortableViewDefinition($view)
  5229. {
  5230. return new View($view['name'], $view['sql']);
  5231. }
  5232. }<?php
  5233. /*
  5234. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  5235. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5236. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  5237. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  5238. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  5239. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5240. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  5241. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  5242. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5243. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  5244. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5245. *
  5246. * This software consists of voluntary contributions made by many individuals
  5247. * and is licensed under the LGPL. For more information, see
  5248. * <http://www.doctrine-project.org>.
  5249. */
  5250. namespace Doctrine\DBAL\Schema;
  5251. /**
  5252. * Compare to Schemas and return an instance of SchemaDiff
  5253. *
  5254. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  5255. * @license http://ez.no/licenses/new_bsd New BSD License
  5256. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  5257. * @link www.doctrine-project.org
  5258. * @since 2.0
  5259. * @version $Revision$
  5260. * @author Benjamin Eberlei <kontakt@beberlei.de>
  5261. */
  5262. class Comparator
  5263. {
  5264. /**
  5265. * @param Schema $fromSchema
  5266. * @param Schema $toSchema
  5267. * @return SchemaDiff
  5268. */
  5269. static public function compareSchemas( Schema $fromSchema, Schema $toSchema )
  5270. {
  5271. $c = new self();
  5272. return $c->compare($fromSchema, $toSchema);
  5273. }
  5274. /**
  5275. * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
  5276. *
  5277. * The returned diferences are returned in such a way that they contain the
  5278. * operations to change the schema stored in $fromSchema to the schema that is
  5279. * stored in $toSchema.
  5280. *
  5281. * @param Schema $fromSchema
  5282. * @param Schema $toSchema
  5283. *
  5284. * @return SchemaDiff
  5285. */
  5286. public function compare(Schema $fromSchema, Schema $toSchema)
  5287. {
  5288. $diff = new SchemaDiff();
  5289. $foreignKeysToTable = array();
  5290. foreach ( $toSchema->getTables() AS $tableName => $table ) {
  5291. if ( !$fromSchema->hasTable($tableName) ) {
  5292. $diff->newTables[$tableName] = $table;
  5293. } else {
  5294. $tableDifferences = $this->diffTable( $fromSchema->getTable($tableName), $table );
  5295. if ( $tableDifferences !== false ) {
  5296. $diff->changedTables[$tableName] = $tableDifferences;
  5297. }
  5298. }
  5299. }
  5300. /* Check if there are tables removed */
  5301. foreach ( $fromSchema->getTables() AS $tableName => $table ) {
  5302. if ( !$toSchema->hasTable($tableName) ) {
  5303. $diff->removedTables[$tableName] = $table;
  5304. }
  5305. // also remember all foreign keys that point to a specific table
  5306. foreach ($table->getForeignKeys() AS $foreignKey) {
  5307. $foreignTable = strtolower($foreignKey->getForeignTableName());
  5308. if (!isset($foreignKeysToTable[$foreignTable])) {
  5309. $foreignKeysToTable[$foreignTable] = array();
  5310. }
  5311. $foreignKeysToTable[$foreignTable][] = $foreignKey;
  5312. }
  5313. }
  5314. foreach ($diff->removedTables AS $tableName => $table) {
  5315. if (isset($foreignKeysToTable[$tableName])) {
  5316. $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
  5317. }
  5318. }
  5319. foreach ( $toSchema->getSequences() AS $sequenceName => $sequence) {
  5320. if (!$fromSchema->hasSequence($sequenceName)) {
  5321. $diff->newSequences[] = $sequence;
  5322. } else {
  5323. if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
  5324. $diff->changedSequences[] = $fromSchema->getSequence($sequenceName);
  5325. }
  5326. }
  5327. }
  5328. foreach ($fromSchema->getSequences() AS $sequenceName => $sequence) {
  5329. if (!$toSchema->hasSequence($sequenceName)) {
  5330. $diff->removedSequences[] = $sequence;
  5331. }
  5332. }
  5333. return $diff;
  5334. }
  5335. /**
  5336. *
  5337. * @param Sequence $sequence1
  5338. * @param Sequence $sequence2
  5339. */
  5340. public function diffSequence(Sequence $sequence1, Sequence $sequence2)
  5341. {
  5342. if($sequence1->getAllocationSize() != $sequence2->getAllocationSize()) {
  5343. return true;
  5344. }
  5345. if($sequence1->getInitialValue() != $sequence2->getInitialValue()) {
  5346. return true;
  5347. }
  5348. return false;
  5349. }
  5350. /**
  5351. * Returns the difference between the tables $table1 and $table2.
  5352. *
  5353. * If there are no differences this method returns the boolean false.
  5354. *
  5355. * @param Table $table1
  5356. * @param Table $table2
  5357. *
  5358. * @return bool|TableDiff
  5359. */
  5360. public function diffTable(Table $table1, Table $table2)
  5361. {
  5362. $changes = 0;
  5363. $tableDifferences = new TableDiff($table1->getName());
  5364. $table1Columns = $table1->getColumns();
  5365. $table2Columns = $table2->getColumns();
  5366. /* See if all the fields in table 1 exist in table 2 */
  5367. foreach ( $table2Columns as $columnName => $column ) {
  5368. if ( !$table1->hasColumn($columnName) ) {
  5369. $tableDifferences->addedColumns[$columnName] = $column;
  5370. $changes++;
  5371. }
  5372. }
  5373. /* See if there are any removed fields in table 2 */
  5374. foreach ( $table1Columns as $columnName => $column ) {
  5375. if ( !$table2->hasColumn($columnName) ) {
  5376. $tableDifferences->removedColumns[$columnName] = $column;
  5377. $changes++;
  5378. }
  5379. }
  5380. foreach ( $table1Columns as $columnName => $column ) {
  5381. if ( $table2->hasColumn($columnName) ) {
  5382. $changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) );
  5383. if (count($changedProperties) ) {
  5384. $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
  5385. $tableDifferences->changedColumns[$column->getName()] = $columnDiff;
  5386. $changes++;
  5387. }
  5388. }
  5389. }
  5390. $this->detectColumnRenamings($tableDifferences);
  5391. $table1Indexes = $table1->getIndexes();
  5392. $table2Indexes = $table2->getIndexes();
  5393. foreach ($table2Indexes AS $index2Name => $index2Definition) {
  5394. foreach ($table1Indexes AS $index1Name => $index1Definition) {
  5395. if ($this->diffIndex($index1Definition, $index2Definition) === false) {
  5396. unset($table1Indexes[$index1Name]);
  5397. unset($table2Indexes[$index2Name]);
  5398. } else {
  5399. if ($index1Name == $index2Name) {
  5400. $tableDifferences->changedIndexes[$index2Name] = $table2Indexes[$index2Name];
  5401. unset($table1Indexes[$index1Name]);
  5402. unset($table2Indexes[$index2Name]);
  5403. $changes++;
  5404. }
  5405. }
  5406. }
  5407. }
  5408. foreach ($table1Indexes AS $index1Name => $index1Definition) {
  5409. $tableDifferences->removedIndexes[$index1Name] = $index1Definition;
  5410. $changes++;
  5411. }
  5412. foreach ($table2Indexes AS $index2Name => $index2Definition) {
  5413. $tableDifferences->addedIndexes[$index2Name] = $index2Definition;
  5414. $changes++;
  5415. }
  5416. $fromFkeys = $table1->getForeignKeys();
  5417. $toFkeys = $table2->getForeignKeys();
  5418. foreach ($fromFkeys AS $key1 => $constraint1) {
  5419. foreach ($toFkeys AS $key2 => $constraint2) {
  5420. if($this->diffForeignKey($constraint1, $constraint2) === false) {
  5421. unset($fromFkeys[$key1]);
  5422. unset($toFkeys[$key2]);
  5423. } else {
  5424. if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) {
  5425. $tableDifferences->changedForeignKeys[] = $constraint2;
  5426. $changes++;
  5427. unset($fromFkeys[$key1]);
  5428. unset($toFkeys[$key2]);
  5429. }
  5430. }
  5431. }
  5432. }
  5433. foreach ($fromFkeys AS $key1 => $constraint1) {
  5434. $tableDifferences->removedForeignKeys[] = $constraint1;
  5435. $changes++;
  5436. }
  5437. foreach ($toFkeys AS $key2 => $constraint2) {
  5438. $tableDifferences->addedForeignKeys[] = $constraint2;
  5439. $changes++;
  5440. }
  5441. return $changes ? $tableDifferences : false;
  5442. }
  5443. /**
  5444. * Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
  5445. * however ambiguouties between different possibilites should not lead to renaming at all.
  5446. *
  5447. * @param TableDiff $tableDifferences
  5448. */
  5449. private function detectColumnRenamings(TableDiff $tableDifferences)
  5450. {
  5451. $renameCandidates = array();
  5452. foreach ($tableDifferences->addedColumns AS $addedColumnName => $addedColumn) {
  5453. foreach ($tableDifferences->removedColumns AS $removedColumnName => $removedColumn) {
  5454. if (count($this->diffColumn($addedColumn, $removedColumn)) == 0) {
  5455. $renameCandidates[$addedColumn->getName()][] = array($removedColumn, $addedColumn);
  5456. }
  5457. }
  5458. }
  5459. foreach ($renameCandidates AS $candidate => $candidateColumns) {
  5460. if (count($candidateColumns) == 1) {
  5461. list($removedColumn, $addedColumn) = $candidateColumns[0];
  5462. $tableDifferences->renamedColumns[$removedColumn->getName()] = $addedColumn;
  5463. unset($tableDifferences->addedColumns[$addedColumnName]);
  5464. unset($tableDifferences->removedColumns[$removedColumnName]);
  5465. }
  5466. }
  5467. }
  5468. /**
  5469. * @param ForeignKeyConstraint $key1
  5470. * @param ForeignKeyConstraint $key2
  5471. * @return bool
  5472. */
  5473. public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2)
  5474. {
  5475. if (array_map('strtolower', $key1->getLocalColumns()) != array_map('strtolower', $key2->getLocalColumns())) {
  5476. return true;
  5477. }
  5478. if (array_map('strtolower', $key1->getForeignColumns()) != array_map('strtolower', $key2->getForeignColumns())) {
  5479. return true;
  5480. }
  5481. if ($key1->onUpdate() != $key2->onUpdate()) {
  5482. return true;
  5483. }
  5484. if ($key1->onDelete() != $key2->onDelete()) {
  5485. return true;
  5486. }
  5487. return false;
  5488. }
  5489. /**
  5490. * Returns the difference between the fields $field1 and $field2.
  5491. *
  5492. * If there are differences this method returns $field2, otherwise the
  5493. * boolean false.
  5494. *
  5495. * @param Column $column1
  5496. * @param Column $column2
  5497. *
  5498. * @return array
  5499. */
  5500. public function diffColumn(Column $column1, Column $column2)
  5501. {
  5502. $changedProperties = array();
  5503. if ( $column1->getType() != $column2->getType() ) {
  5504. $changedProperties[] = 'type';
  5505. }
  5506. if ($column1->getNotnull() != $column2->getNotnull()) {
  5507. $changedProperties[] = 'notnull';
  5508. }
  5509. if ($column1->getDefault() != $column2->getDefault()) {
  5510. $changedProperties[] = 'default';
  5511. }
  5512. if ($column1->getUnsigned() != $column2->getUnsigned()) {
  5513. $changedProperties[] = 'unsigned';
  5514. }
  5515. if ($column1->getType() instanceof \Doctrine\DBAL\Types\StringType) {
  5516. if ($column1->getLength() != $column2->getLength()) {
  5517. $changedProperties[] = 'length';
  5518. }
  5519. if ($column1->getFixed() != $column2->getFixed()) {
  5520. $changedProperties[] = 'fixed';
  5521. }
  5522. }
  5523. if ($column1->getType() instanceof \Doctrine\DBAL\Types\DecimalType) {
  5524. if ($column1->getPrecision() != $column2->getPrecision()) {
  5525. $changedProperties[] = 'precision';
  5526. }
  5527. if ($column1->getScale() != $column2->getScale()) {
  5528. $changedProperties[] = 'scale';
  5529. }
  5530. }
  5531. if ($column1->getAutoincrement() != $column2->getAutoincrement()) {
  5532. $changedProperties[] = 'autoincrement';
  5533. }
  5534. return $changedProperties;
  5535. }
  5536. /**
  5537. * Finds the difference between the indexes $index1 and $index2.
  5538. *
  5539. * Compares $index1 with $index2 and returns $index2 if there are any
  5540. * differences or false in case there are no differences.
  5541. *
  5542. * @param Index $index1
  5543. * @param Index $index2
  5544. * @return bool
  5545. */
  5546. public function diffIndex(Index $index1, Index $index2)
  5547. {
  5548. if ($index1->isFullfilledBy($index2) && $index2->isFullfilledBy($index1)) {
  5549. return false;
  5550. }
  5551. return true;
  5552. }
  5553. }
  5554. <?php
  5555. /*
  5556. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  5557. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5558. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  5559. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  5560. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  5561. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5562. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  5563. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  5564. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5565. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  5566. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5567. *
  5568. * This software consists of voluntary contributions made by many individuals
  5569. * and is licensed under the LGPL. For more information, see
  5570. * <http://www.doctrine-project.org>.
  5571. */
  5572. namespace Doctrine\DBAL\Schema;
  5573. use Doctrine\DBAL\Schema\Visitor\Visitor;
  5574. class Index extends AbstractAsset implements Constraint
  5575. {
  5576. /**
  5577. * @var array
  5578. */
  5579. protected $_columns;
  5580. /**
  5581. * @var bool
  5582. */
  5583. protected $_isUnique = false;
  5584. /**
  5585. * @var bool
  5586. */
  5587. protected $_isPrimary = false;
  5588. /**
  5589. * @param string $indexName
  5590. * @param array $column
  5591. * @param bool $isUnique
  5592. * @param bool $isPrimary
  5593. */
  5594. public function __construct($indexName, array $columns, $isUnique=false, $isPrimary=false)
  5595. {
  5596. $isUnique = ($isPrimary)?true:$isUnique;
  5597. $this->_setName($indexName);
  5598. $this->_isUnique = $isUnique;
  5599. $this->_isPrimary = $isPrimary;
  5600. foreach($columns AS $column) {
  5601. $this->_addColumn($column);
  5602. }
  5603. }
  5604. /**
  5605. * @param string $column
  5606. */
  5607. protected function _addColumn($column)
  5608. {
  5609. if(is_string($column)) {
  5610. $this->_columns[] = strtolower($column);
  5611. } else {
  5612. throw new \InvalidArgumentException("Expecting a string as Index Column");
  5613. }
  5614. }
  5615. /**
  5616. * @return array
  5617. */
  5618. public function getColumns()
  5619. {
  5620. return $this->_columns;
  5621. }
  5622. /**
  5623. * @return bool
  5624. */
  5625. public function isUnique()
  5626. {
  5627. return $this->_isUnique;
  5628. }
  5629. /**
  5630. * @return bool
  5631. */
  5632. public function isPrimary()
  5633. {
  5634. return $this->_isPrimary;
  5635. }
  5636. /**
  5637. * @param string $columnName
  5638. * @param int $pos
  5639. * @return bool
  5640. */
  5641. public function hasColumnAtPosition($columnName, $pos=0)
  5642. {
  5643. $columnName = strtolower($columnName);
  5644. $indexColumns = \array_map('strtolower', $this->getColumns());
  5645. return \array_search($columnName, $indexColumns) === $pos;
  5646. }
  5647. /**
  5648. * Check if this index exactly spans the given column names in the correct order.
  5649. *
  5650. * @param array $columnNames
  5651. * @return boolean
  5652. */
  5653. public function spansColumns(array $columnNames)
  5654. {
  5655. $sameColumns = true;
  5656. for ($i = 0; $i < count($this->_columns); $i++) {
  5657. if (!isset($columnNames[$i]) || strtolower($this->_columns[$i]) != strtolower($columnNames[$i])) {
  5658. $sameColumns = false;
  5659. }
  5660. }
  5661. return $sameColumns;
  5662. }
  5663. /**
  5664. * Check if the other index already fullfills all the indexing and constraint needs of the current one.
  5665. *
  5666. * @param Index $other
  5667. * @return bool
  5668. */
  5669. public function isFullfilledBy(Index $other)
  5670. {
  5671. // allow the other index to be equally large only. It being larger is an option
  5672. // but it creates a problem with scenarios of the kind PRIMARY KEY(foo,bar) UNIQUE(foo)
  5673. if (count($other->getColumns()) != count($this->getColumns())) {
  5674. return false;
  5675. }
  5676. // Check if columns are the same, and even in the same order
  5677. $sameColumns = $this->spansColumns($other->getColumns());
  5678. if ($sameColumns) {
  5679. if (!$this->isUnique() && !$this->isPrimary()) {
  5680. // this is a special case: If the current key is neither primary or unique, any uniqe or
  5681. // primary key will always have the same effect for the index and there cannot be any constraint
  5682. // overlaps. This means a primary or unique index can always fullfill the requirements of just an
  5683. // index that has no constraints.
  5684. return true;
  5685. } else if ($other->isPrimary() != $this->isPrimary()) {
  5686. return false;
  5687. } else if ($other->isUnique() != $this->isUnique()) {
  5688. return false;
  5689. }
  5690. return true;
  5691. }
  5692. return false;
  5693. }
  5694. /**
  5695. * Detect if the other index is a non-unique, non primary index that can be overwritten by this one.
  5696. *
  5697. * @param Index $other
  5698. * @return bool
  5699. */
  5700. public function overrules(Index $other)
  5701. {
  5702. if ($other->isPrimary() || $other->isUnique()) {
  5703. return false;
  5704. }
  5705. if ($this->spansColumns($other->getColumns()) && ($this->isPrimary() || $this->isUnique())) {
  5706. return true;
  5707. }
  5708. return false;
  5709. }
  5710. }<?php
  5711. /*
  5712. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  5713. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5714. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  5715. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  5716. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  5717. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5718. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  5719. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  5720. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5721. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  5722. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5723. *
  5724. * This software consists of voluntary contributions made by many individuals
  5725. * and is licensed under the LGPL. For more information, see
  5726. * <http://www.doctrine-project.org>.
  5727. */
  5728. namespace Doctrine\DBAL\Schema;
  5729. use Doctrine\DBAL\Types;
  5730. use Doctrine\DBAL\DBALException;
  5731. use Doctrine\DBAL\Platforms\AbstractPlatform;
  5732. /**
  5733. * Base class for schema managers. Schema managers are used to inspect and/or
  5734. * modify the database schema/structure.
  5735. *
  5736. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  5737. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  5738. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  5739. * @author Roman Borschel <roman@code-factory.org>
  5740. * @author Jonathan H. Wage <jonwage@gmail.com>
  5741. * @author Benjamin Eberlei <kontakt@beberlei.de>
  5742. * @version $Revision$
  5743. * @since 2.0
  5744. */
  5745. abstract class AbstractSchemaManager
  5746. {
  5747. /**
  5748. * Holds instance of the Doctrine connection for this schema manager
  5749. *
  5750. * @var \Doctrine\DBAL\Connection
  5751. */
  5752. protected $_conn;
  5753. /**
  5754. * Holds instance of the database platform used for this schema manager
  5755. *
  5756. * @var \Doctrine\DBAL\Platforms\AbstractPlatform
  5757. */
  5758. protected $_platform;
  5759. /**
  5760. * Constructor. Accepts the Connection instance to manage the schema for
  5761. *
  5762. * @param \Doctrine\DBAL\Connection $conn
  5763. */
  5764. public function __construct(\Doctrine\DBAL\Connection $conn)
  5765. {
  5766. $this->_conn = $conn;
  5767. $this->_platform = $this->_conn->getDatabasePlatform();
  5768. }
  5769. /**
  5770. * Return associated platform.
  5771. *
  5772. * @return \Doctrine\DBAL\Platform\AbstractPlatform
  5773. */
  5774. public function getDatabasePlatform()
  5775. {
  5776. return $this->_platform;
  5777. }
  5778. /**
  5779. * Try any method on the schema manager. Normally a method throws an
  5780. * exception when your DBMS doesn't support it or if an error occurs.
  5781. * This method allows you to try and method on your SchemaManager
  5782. * instance and will return false if it does not work or is not supported.
  5783. *
  5784. * <code>
  5785. * $result = $sm->tryMethod('dropView', 'view_name');
  5786. * </code>
  5787. *
  5788. * @return mixed
  5789. */
  5790. public function tryMethod()
  5791. {
  5792. $args = func_get_args();
  5793. $method = $args[0];
  5794. unset($args[0]);
  5795. $args = array_values($args);
  5796. try {
  5797. return call_user_func_array(array($this, $method), $args);
  5798. } catch (\Exception $e) {
  5799. return false;
  5800. }
  5801. }
  5802. /**
  5803. * List the available databases for this connection
  5804. *
  5805. * @return array $databases
  5806. */
  5807. public function listDatabases()
  5808. {
  5809. $sql = $this->_platform->getListDatabasesSQL();
  5810. $databases = $this->_conn->fetchAll($sql);
  5811. return $this->_getPortableDatabasesList($databases);
  5812. }
  5813. /**
  5814. * List the available sequences for this connection
  5815. *
  5816. * @return Sequence[]
  5817. */
  5818. public function listSequences($database = null)
  5819. {
  5820. if (is_null($database)) {
  5821. $database = $this->_conn->getDatabase();
  5822. }
  5823. $sql = $this->_platform->getListSequencesSQL($database);
  5824. $sequences = $this->_conn->fetchAll($sql);
  5825. return $this->_getPortableSequencesList($sequences);
  5826. }
  5827. /**
  5828. * List the columns for a given table.
  5829. *
  5830. * In contrast to other libraries and to the old version of Doctrine,
  5831. * this column definition does try to contain the 'primary' field for
  5832. * the reason that it is not portable accross different RDBMS. Use
  5833. * {@see listTableIndexes($tableName)} to retrieve the primary key
  5834. * of a table. We're a RDBMS specifies more details these are held
  5835. * in the platformDetails array.
  5836. *
  5837. * @param string $table The name of the table.
  5838. * @return Column[]
  5839. */
  5840. public function listTableColumns($table)
  5841. {
  5842. $sql = $this->_platform->getListTableColumnsSQL($table);
  5843. $tableColumns = $this->_conn->fetchAll($sql);
  5844. return $this->_getPortableTableColumnList($tableColumns);
  5845. }
  5846. /**
  5847. * List the indexes for a given table returning an array of Index instances.
  5848. *
  5849. * Keys of the portable indexes list are all lower-cased.
  5850. *
  5851. * @param string $table The name of the table
  5852. * @return Index[] $tableIndexes
  5853. */
  5854. public function listTableIndexes($table)
  5855. {
  5856. $sql = $this->_platform->getListTableIndexesSQL($table);
  5857. $tableIndexes = $this->_conn->fetchAll($sql);
  5858. return $this->_getPortableTableIndexesList($tableIndexes, $table);
  5859. }
  5860. /**
  5861. * Return true if all the given tables exist.
  5862. *
  5863. * @param array $tableNames
  5864. * @return bool
  5865. */
  5866. public function tablesExist($tableNames)
  5867. {
  5868. $tableNames = array_map('strtolower', (array)$tableNames);
  5869. return count($tableNames) == count(\array_intersect($tableNames, array_map('strtolower', $this->listTableNames())));
  5870. }
  5871. /**
  5872. * Return a list of all tables in the current database
  5873. *
  5874. * @return array
  5875. */
  5876. public function listTableNames()
  5877. {
  5878. $sql = $this->_platform->getListTablesSQL();
  5879. $tables = $this->_conn->fetchAll($sql);
  5880. return $this->_getPortableTablesList($tables);
  5881. }
  5882. /**
  5883. * List the tables for this connection
  5884. *
  5885. * @return Table[]
  5886. */
  5887. public function listTables()
  5888. {
  5889. $tableNames = $this->listTableNames();
  5890. $tables = array();
  5891. foreach ($tableNames AS $tableName) {
  5892. $tables[] = $this->listTableDetails($tableName);
  5893. }
  5894. return $tables;
  5895. }
  5896. /**
  5897. * @param string $tableName
  5898. * @return Table
  5899. */
  5900. public function listTableDetails($tableName)
  5901. {
  5902. $columns = $this->listTableColumns($tableName);
  5903. $foreignKeys = array();
  5904. if ($this->_platform->supportsForeignKeyConstraints()) {
  5905. $foreignKeys = $this->listTableForeignKeys($tableName);
  5906. }
  5907. $indexes = $this->listTableIndexes($tableName);
  5908. return new Table($tableName, $columns, $indexes, $foreignKeys, false, array());
  5909. }
  5910. /**
  5911. * List the views this connection has
  5912. *
  5913. * @return View[]
  5914. */
  5915. public function listViews()
  5916. {
  5917. $database = $this->_conn->getDatabase();
  5918. $sql = $this->_platform->getListViewsSQL($database);
  5919. $views = $this->_conn->fetchAll($sql);
  5920. return $this->_getPortableViewsList($views);
  5921. }
  5922. /**
  5923. * List the foreign keys for the given table
  5924. *
  5925. * @param string $table The name of the table
  5926. * @return ForeignKeyConstraint[]
  5927. */
  5928. public function listTableForeignKeys($table, $database = null)
  5929. {
  5930. if (is_null($database)) {
  5931. $database = $this->_conn->getDatabase();
  5932. }
  5933. $sql = $this->_platform->getListTableForeignKeysSQL($table, $database);
  5934. $tableForeignKeys = $this->_conn->fetchAll($sql);
  5935. return $this->_getPortableTableForeignKeysList($tableForeignKeys);
  5936. }
  5937. /* drop*() Methods */
  5938. /**
  5939. * Drops a database.
  5940. *
  5941. * NOTE: You can not drop the database this SchemaManager is currently connected to.
  5942. *
  5943. * @param string $database The name of the database to drop
  5944. */
  5945. public function dropDatabase($database)
  5946. {
  5947. $this->_execSql($this->_platform->getDropDatabaseSQL($database));
  5948. }
  5949. /**
  5950. * Drop the given table
  5951. *
  5952. * @param string $table The name of the table to drop
  5953. */
  5954. public function dropTable($table)
  5955. {
  5956. $this->_execSql($this->_platform->getDropTableSQL($table));
  5957. }
  5958. /**
  5959. * Drop the index from the given table
  5960. *
  5961. * @param Index|string $index The name of the index
  5962. * @param string|Table $table The name of the table
  5963. */
  5964. public function dropIndex($index, $table)
  5965. {
  5966. if($index instanceof Index) {
  5967. $index = $index->getName();
  5968. }
  5969. $this->_execSql($this->_platform->getDropIndexSQL($index, $table));
  5970. }
  5971. /**
  5972. * Drop the constraint from the given table
  5973. *
  5974. * @param Constraint $constraint
  5975. * @param string $table The name of the table
  5976. */
  5977. public function dropConstraint(Constraint $constraint, $table)
  5978. {
  5979. $this->_execSql($this->_platform->getDropConstraintSQL($constraint, $table));
  5980. }
  5981. /**
  5982. * Drops a foreign key from a table.
  5983. *
  5984. * @param ForeignKeyConstraint|string $table The name of the table with the foreign key.
  5985. * @param Table|string $name The name of the foreign key.
  5986. * @return boolean $result
  5987. */
  5988. public function dropForeignKey($foreignKey, $table)
  5989. {
  5990. $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table));
  5991. }
  5992. /**
  5993. * Drops a sequence with a given name.
  5994. *
  5995. * @param string $name The name of the sequence to drop.
  5996. */
  5997. public function dropSequence($name)
  5998. {
  5999. $this->_execSql($this->_platform->getDropSequenceSQL($name));
  6000. }
  6001. /**
  6002. * Drop a view
  6003. *
  6004. * @param string $name The name of the view
  6005. * @return boolean $result
  6006. */
  6007. public function dropView($name)
  6008. {
  6009. $this->_execSql($this->_platform->getDropViewSQL($name));
  6010. }
  6011. /* create*() Methods */
  6012. /**
  6013. * Creates a new database.
  6014. *
  6015. * @param string $database The name of the database to create.
  6016. */
  6017. public function createDatabase($database)
  6018. {
  6019. $this->_execSql($this->_platform->getCreateDatabaseSQL($database));
  6020. }
  6021. /**
  6022. * Create a new table.
  6023. *
  6024. * @param Table $table
  6025. * @param int $createFlags
  6026. */
  6027. public function createTable(Table $table)
  6028. {
  6029. $createFlags = AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS;
  6030. $this->_execSql($this->_platform->getCreateTableSQL($table, $createFlags));
  6031. }
  6032. /**
  6033. * Create a new sequence
  6034. *
  6035. * @param Sequence $sequence
  6036. * @throws Doctrine\DBAL\ConnectionException if something fails at database level
  6037. */
  6038. public function createSequence($sequence)
  6039. {
  6040. $this->_execSql($this->_platform->getCreateSequenceSQL($sequence));
  6041. }
  6042. /**
  6043. * Create a constraint on a table
  6044. *
  6045. * @param Constraint $constraint
  6046. * @param string|Table $table
  6047. */
  6048. public function createConstraint(Constraint $constraint, $table)
  6049. {
  6050. $this->_execSql($this->_platform->getCreateConstraintSQL($constraint, $table));
  6051. }
  6052. /**
  6053. * Create a new index on a table
  6054. *
  6055. * @param Index $index
  6056. * @param string $table name of the table on which the index is to be created
  6057. */
  6058. public function createIndex(Index $index, $table)
  6059. {
  6060. $this->_execSql($this->_platform->getCreateIndexSQL($index, $table));
  6061. }
  6062. /**
  6063. * Create a new foreign key
  6064. *
  6065. * @param ForeignKeyConstraint $foreignKey ForeignKey instance
  6066. * @param string|Table $table name of the table on which the foreign key is to be created
  6067. */
  6068. public function createForeignKey(ForeignKeyConstraint $foreignKey, $table)
  6069. {
  6070. $this->_execSql($this->_platform->getCreateForeignKeySQL($foreignKey, $table));
  6071. }
  6072. /**
  6073. * Create a new view
  6074. *
  6075. * @param View $view
  6076. */
  6077. public function createView(View $view)
  6078. {
  6079. $this->_execSql($this->_platform->getCreateViewSQL($view->getName(), $view->getSql()));
  6080. }
  6081. /* dropAndCreate*() Methods */
  6082. /**
  6083. * Drop and create a constraint
  6084. *
  6085. * @param Constraint $constraint
  6086. * @param string $table
  6087. * @see dropConstraint()
  6088. * @see createConstraint()
  6089. */
  6090. public function dropAndCreateConstraint(Constraint $constraint, $table)
  6091. {
  6092. $this->tryMethod('dropConstraint', $constraint, $table);
  6093. $this->createConstraint($constraint, $table);
  6094. }
  6095. /**
  6096. * Drop and create a new index on a table
  6097. *
  6098. * @param string|Table $table name of the table on which the index is to be created
  6099. * @param Index $index
  6100. */
  6101. public function dropAndCreateIndex(Index $index, $table)
  6102. {
  6103. $this->tryMethod('dropIndex', $index->getName(), $table);
  6104. $this->createIndex($index, $table);
  6105. }
  6106. /**
  6107. * Drop and create a new foreign key
  6108. *
  6109. * @param ForeignKeyConstraint $foreignKey associative array that defines properties of the foreign key to be created.
  6110. * @param string|Table $table name of the table on which the foreign key is to be created
  6111. */
  6112. public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table)
  6113. {
  6114. $this->tryMethod('dropForeignKey', $foreignKey, $table);
  6115. $this->createForeignKey($foreignKey, $table);
  6116. }
  6117. /**
  6118. * Drop and create a new sequence
  6119. *
  6120. * @param Sequence $sequence
  6121. * @throws Doctrine\DBAL\ConnectionException if something fails at database level
  6122. */
  6123. public function dropAndCreateSequence(Sequence $sequence)
  6124. {
  6125. $this->tryMethod('createSequence', $seqName, $start, $allocationSize);
  6126. $this->createSequence($seqName, $start, $allocationSize);
  6127. }
  6128. /**
  6129. * Drop and create a new table.
  6130. *
  6131. * @param Table $table
  6132. */
  6133. public function dropAndCreateTable(Table $table)
  6134. {
  6135. $this->tryMethod('dropTable', $table->getName());
  6136. $this->createTable($table);
  6137. }
  6138. /**
  6139. * Drop and creates a new database.
  6140. *
  6141. * @param string $database The name of the database to create.
  6142. */
  6143. public function dropAndCreateDatabase($database)
  6144. {
  6145. $this->tryMethod('dropDatabase', $database);
  6146. $this->createDatabase($database);
  6147. }
  6148. /**
  6149. * Drop and create a new view
  6150. *
  6151. * @param View $view
  6152. */
  6153. public function dropAndCreateView(View $view)
  6154. {
  6155. $this->tryMethod('dropView', $view->getName());
  6156. $this->createView($view);
  6157. }
  6158. /* alterTable() Methods */
  6159. /**
  6160. * Alter an existing tables schema
  6161. *
  6162. * @param TableDiff $tableDiff
  6163. */
  6164. public function alterTable(TableDiff $tableDiff)
  6165. {
  6166. $queries = $this->_platform->getAlterTableSQL($tableDiff);
  6167. if (is_array($queries) && count($queries)) {
  6168. foreach ($queries AS $ddlQuery) {
  6169. $this->_execSql($ddlQuery);
  6170. }
  6171. }
  6172. }
  6173. /**
  6174. * Rename a given table to another name
  6175. *
  6176. * @param string $name The current name of the table
  6177. * @param string $newName The new name of the table
  6178. */
  6179. public function renameTable($name, $newName)
  6180. {
  6181. $tableDiff = new TableDiff($name);
  6182. $tableDiff->newName = $newName;
  6183. $this->alterTable($tableDiff);
  6184. }
  6185. /**
  6186. * Methods for filtering return values of list*() methods to convert
  6187. * the native DBMS data definition to a portable Doctrine definition
  6188. */
  6189. protected function _getPortableDatabasesList($databases)
  6190. {
  6191. $list = array();
  6192. foreach ($databases as $key => $value) {
  6193. if ($value = $this->_getPortableDatabaseDefinition($value)) {
  6194. $list[] = $value;
  6195. }
  6196. }
  6197. return $list;
  6198. }
  6199. protected function _getPortableDatabaseDefinition($database)
  6200. {
  6201. return $database;
  6202. }
  6203. protected function _getPortableFunctionsList($functions)
  6204. {
  6205. $list = array();
  6206. foreach ($functions as $key => $value) {
  6207. if ($value = $this->_getPortableFunctionDefinition($value)) {
  6208. $list[] = $value;
  6209. }
  6210. }
  6211. return $list;
  6212. }
  6213. protected function _getPortableFunctionDefinition($function)
  6214. {
  6215. return $function;
  6216. }
  6217. protected function _getPortableTriggersList($triggers)
  6218. {
  6219. $list = array();
  6220. foreach ($triggers as $key => $value) {
  6221. if ($value = $this->_getPortableTriggerDefinition($value)) {
  6222. $list[] = $value;
  6223. }
  6224. }
  6225. return $list;
  6226. }
  6227. protected function _getPortableTriggerDefinition($trigger)
  6228. {
  6229. return $trigger;
  6230. }
  6231. protected function _getPortableSequencesList($sequences)
  6232. {
  6233. $list = array();
  6234. foreach ($sequences as $key => $value) {
  6235. if ($value = $this->_getPortableSequenceDefinition($value)) {
  6236. $list[] = $value;
  6237. }
  6238. }
  6239. return $list;
  6240. }
  6241. /**
  6242. * @param array $sequence
  6243. * @return Sequence
  6244. */
  6245. protected function _getPortableSequenceDefinition($sequence)
  6246. {
  6247. throw DBALException::notSupported('Sequences');
  6248. }
  6249. /**
  6250. * Independent of the database the keys of the column list result are lowercased.
  6251. *
  6252. * The name of the created column instance however is kept in its case.
  6253. *
  6254. * @param array $tableColumns
  6255. * @return array
  6256. */
  6257. protected function _getPortableTableColumnList($tableColumns)
  6258. {
  6259. $list = array();
  6260. foreach ($tableColumns as $key => $column) {
  6261. if ($column = $this->_getPortableTableColumnDefinition($column)) {
  6262. $name = strtolower($column->getName());
  6263. $list[$name] = $column;
  6264. }
  6265. }
  6266. return $list;
  6267. }
  6268. /**
  6269. * Get Table Column Definition
  6270. *
  6271. * @param array $tableColumn
  6272. * @return Column
  6273. */
  6274. abstract protected function _getPortableTableColumnDefinition($tableColumn);
  6275. /**
  6276. * Aggregate and group the index results according to the required data result.
  6277. *
  6278. * @param array $tableIndexRows
  6279. * @param string $tableName
  6280. * @return array
  6281. */
  6282. protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
  6283. {
  6284. $result = array();
  6285. foreach($tableIndexRows AS $tableIndex) {
  6286. $indexName = $keyName = $tableIndex['key_name'];
  6287. if($tableIndex['primary']) {
  6288. $keyName = 'primary';
  6289. }
  6290. $keyName = strtolower($keyName);
  6291. if(!isset($result[$keyName])) {
  6292. $result[$keyName] = array(
  6293. 'name' => $indexName,
  6294. 'columns' => array($tableIndex['column_name']),
  6295. 'unique' => $tableIndex['non_unique'] ? false : true,
  6296. 'primary' => $tableIndex['primary'],
  6297. );
  6298. } else {
  6299. $result[$keyName]['columns'][] = $tableIndex['column_name'];
  6300. }
  6301. }
  6302. $indexes = array();
  6303. foreach($result AS $indexKey => $data) {
  6304. $indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
  6305. }
  6306. return $indexes;
  6307. }
  6308. protected function _getPortableTablesList($tables)
  6309. {
  6310. $list = array();
  6311. foreach ($tables as $key => $value) {
  6312. if ($value = $this->_getPortableTableDefinition($value)) {
  6313. $list[] = $value;
  6314. }
  6315. }
  6316. return $list;
  6317. }
  6318. protected function _getPortableTableDefinition($table)
  6319. {
  6320. return $table;
  6321. }
  6322. protected function _getPortableUsersList($users)
  6323. {
  6324. $list = array();
  6325. foreach ($users as $key => $value) {
  6326. if ($value = $this->_getPortableUserDefinition($value)) {
  6327. $list[] = $value;
  6328. }
  6329. }
  6330. return $list;
  6331. }
  6332. protected function _getPortableUserDefinition($user)
  6333. {
  6334. return $user;
  6335. }
  6336. protected function _getPortableViewsList($views)
  6337. {
  6338. $list = array();
  6339. foreach ($views as $key => $value) {
  6340. if ($view = $this->_getPortableViewDefinition($value)) {
  6341. $viewName = strtolower($view->getName());
  6342. $list[$viewName] = $view;
  6343. }
  6344. }
  6345. return $list;
  6346. }
  6347. protected function _getPortableViewDefinition($view)
  6348. {
  6349. return false;
  6350. }
  6351. protected function _getPortableTableForeignKeysList($tableForeignKeys)
  6352. {
  6353. $list = array();
  6354. foreach ($tableForeignKeys as $key => $value) {
  6355. if ($value = $this->_getPortableTableForeignKeyDefinition($value)) {
  6356. $list[] = $value;
  6357. }
  6358. }
  6359. return $list;
  6360. }
  6361. protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
  6362. {
  6363. return $tableForeignKey;
  6364. }
  6365. protected function _execSql($sql)
  6366. {
  6367. foreach ((array) $sql as $query) {
  6368. $this->_conn->executeUpdate($query);
  6369. }
  6370. }
  6371. /**
  6372. * Create a schema instance for the current database.
  6373. *
  6374. * @return Schema
  6375. */
  6376. public function createSchema()
  6377. {
  6378. $sequences = array();
  6379. if($this->_platform->supportsSequences()) {
  6380. $sequences = $this->listSequences();
  6381. }
  6382. $tables = $this->listTables();
  6383. return new Schema($tables, $sequences, $this->createSchemaConfig());
  6384. }
  6385. /**
  6386. * Create the configuration for this schema.
  6387. *
  6388. * @return SchemaConfig
  6389. */
  6390. public function createSchemaConfig()
  6391. {
  6392. $schemaConfig = new SchemaConfig();
  6393. $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength());
  6394. return $schemaConfig;
  6395. }
  6396. }<?php
  6397. /*
  6398. * $Id$
  6399. *
  6400. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6401. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  6402. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6403. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  6404. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  6405. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6406. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  6407. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  6408. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  6409. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  6410. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6411. *
  6412. * This software consists of voluntary contributions made by many individuals
  6413. * and is licensed under the LGPL. For more information, see
  6414. * <http://www.doctrine-project.org>.
  6415. */
  6416. namespace Doctrine\DBAL\Schema;
  6417. /**
  6418. * The abstract asset allows to reset the name of all assets without publishing this to the public userland.
  6419. *
  6420. * This encapsulation hack is necessary to keep a consistent state of the database schema. Say we have a list of tables
  6421. * array($tableName => Table($tableName)); if you want to rename the table, you have to make sure
  6422. *
  6423. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  6424. * @link www.doctrine-project.org
  6425. * @since 2.0
  6426. * @version $Revision$
  6427. * @author Benjamin Eberlei <kontakt@beberlei.de>
  6428. */
  6429. abstract class AbstractAsset
  6430. {
  6431. /**
  6432. * @var string
  6433. */
  6434. protected $_name;
  6435. /**
  6436. * Set name of this asset
  6437. *
  6438. * @param string $name
  6439. */
  6440. protected function _setName($name)
  6441. {
  6442. $this->_name = $name;
  6443. }
  6444. /**
  6445. * Return name of this schema asset.
  6446. *
  6447. * @return string
  6448. */
  6449. public function getName()
  6450. {
  6451. return $this->_name;
  6452. }
  6453. /**
  6454. * Generate an identifier from a list of column names obeying a certain string length.
  6455. *
  6456. * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars,
  6457. * however building idents automatically for foreign keys, composite keys or such can easily create
  6458. * very long names.
  6459. *
  6460. * @param array $columnNames
  6461. * @param string $postfix
  6462. * @param int $maxSize
  6463. * @return string
  6464. */
  6465. protected function _generateIdentifierName($columnNames, $postfix='', $maxSize=30)
  6466. {
  6467. $columnCount = count($columnNames);
  6468. $postfixLen = strlen($postfix);
  6469. $parts = array_map(function($columnName) use($columnCount, $postfixLen, $maxSize) {
  6470. return substr($columnName, -floor(($maxSize-$postfixLen)/$columnCount - 1));
  6471. }, $columnNames);
  6472. $parts[] = $postfix;
  6473. $identifier = trim(implode("_", $parts), '_');
  6474. // using implicit schema support of DB2 and Postgres there might be dots in the auto-generated
  6475. // identifier names which can easily be replaced by underscores.
  6476. $identifier = str_replace(".", "_", $identifier);
  6477. if (is_numeric(substr($identifier, 0, 1))) {
  6478. $identifier = "i" . substr($identifier, 0, strlen($identifier)-1);
  6479. }
  6480. return $identifier;
  6481. }
  6482. }<?php
  6483. /*
  6484. * $Id: Schema.php 6876 2009-12-06 23:11:35Z beberlei $
  6485. *
  6486. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6487. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  6488. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6489. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  6490. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  6491. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6492. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  6493. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  6494. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  6495. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  6496. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6497. *
  6498. * This software consists of voluntary contributions made by many individuals
  6499. * and is licensed under the LGPL. For more information, see
  6500. * <http://www.doctrine-project.org>.
  6501. */
  6502. namespace Doctrine\DBAL\Schema;
  6503. /**
  6504. * Configuration for a Schema
  6505. *
  6506. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  6507. * @link www.doctrine-project.org
  6508. * @since 2.0
  6509. * @version $Revision$
  6510. * @author Benjamin Eberlei <kontakt@beberlei.de>
  6511. */
  6512. class SchemaConfig
  6513. {
  6514. /**
  6515. * @var bool
  6516. */
  6517. protected $_hasExplicitForeignKeyIndexes = false;
  6518. /**
  6519. * @var int
  6520. */
  6521. protected $_maxIdentifierLength = 63;
  6522. /**
  6523. * @return bool
  6524. */
  6525. public function hasExplicitForeignKeyIndexes()
  6526. {
  6527. return $this->_hasExplicitForeignKeyIndexes;
  6528. }
  6529. /**
  6530. * @param bool $flag
  6531. */
  6532. public function setExplicitForeignKeyIndexes($flag)
  6533. {
  6534. $this->_hasExplicitForeignKeyIndexes = (bool)$flag;
  6535. }
  6536. /**
  6537. * @param int $length
  6538. */
  6539. public function setMaxIdentifierLength($length)
  6540. {
  6541. $this->_maxIdentifierLength = (int)$length;
  6542. }
  6543. /**
  6544. * @return int
  6545. */
  6546. public function getMaxIdentifierLength()
  6547. {
  6548. return $this->_maxIdentifierLength;
  6549. }
  6550. }<?php
  6551. /*
  6552. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6553. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  6554. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6555. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  6556. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  6557. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6558. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  6559. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  6560. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  6561. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  6562. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6563. *
  6564. * This software consists of voluntary contributions made by many individuals
  6565. * and is licensed under the LGPL. For more information, see
  6566. * <http://www.doctrine-project.org>.
  6567. */
  6568. namespace Doctrine\DBAL\Schema;
  6569. use \Doctrine\DBAL\Types\Type;
  6570. use Doctrine\DBAL\Schema\Visitor\Visitor;
  6571. /**
  6572. * Object representation of a database column
  6573. *
  6574. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  6575. * @link www.doctrine-project.org
  6576. * @since 2.0
  6577. * @version $Revision$
  6578. * @author Benjamin Eberlei <kontakt@beberlei.de>
  6579. */
  6580. class Column extends AbstractAsset
  6581. {
  6582. /**
  6583. * @var \Doctrine\DBAL\Types\Type
  6584. */
  6585. protected $_type;
  6586. /**
  6587. * @var int
  6588. */
  6589. protected $_length = null;
  6590. /**
  6591. * @var int
  6592. */
  6593. protected $_precision = 0;
  6594. /**
  6595. * @var int
  6596. */
  6597. protected $_scale = 0;
  6598. /**
  6599. * @var bool
  6600. */
  6601. protected $_unsigned = false;
  6602. /**
  6603. * @var bool
  6604. */
  6605. protected $_fixed = false;
  6606. /**
  6607. * @var bool
  6608. */
  6609. protected $_notnull = true;
  6610. /**
  6611. * @var string
  6612. */
  6613. protected $_default = null;
  6614. /**
  6615. * @var bool
  6616. */
  6617. protected $_autoincrement = false;
  6618. /**
  6619. * @var array
  6620. */
  6621. protected $_platformOptions = array();
  6622. /**
  6623. * @var string
  6624. */
  6625. protected $_columnDefinition = null;
  6626. /**
  6627. * Create a new Column
  6628. *
  6629. * @param string $columnName
  6630. * @param Doctrine\DBAL\Types\Type $type
  6631. * @param int $length
  6632. * @param bool $notNull
  6633. * @param mixed $default
  6634. * @param bool $unsigned
  6635. * @param bool $fixed
  6636. * @param int $precision
  6637. * @param int $scale
  6638. * @param array $platformOptions
  6639. */
  6640. public function __construct($columnName, Type $type, array $options=array())
  6641. {
  6642. $this->_setName($columnName);
  6643. $this->setType($type);
  6644. $this->setOptions($options);
  6645. }
  6646. /**
  6647. * @param array $options
  6648. * @return Column
  6649. */
  6650. public function setOptions(array $options)
  6651. {
  6652. foreach ($options AS $name => $value) {
  6653. $method = "set".$name;
  6654. if (method_exists($this, $method)) {
  6655. $this->$method($value);
  6656. }
  6657. }
  6658. return $this;
  6659. }
  6660. /**
  6661. * @param Type $type
  6662. * @return Column
  6663. */
  6664. public function setType(Type $type)
  6665. {
  6666. $this->_type = $type;
  6667. return $this;
  6668. }
  6669. /**
  6670. * @param int $length
  6671. * @return Column
  6672. */
  6673. public function setLength($length)
  6674. {
  6675. if($length !== null) {
  6676. $this->_length = (int)$length;
  6677. } else {
  6678. $this->_length = null;
  6679. }
  6680. return $this;
  6681. }
  6682. /**
  6683. * @param int $precision
  6684. * @return Column
  6685. */
  6686. public function setPrecision($precision)
  6687. {
  6688. $this->_precision = (int)$precision;
  6689. return $this;
  6690. }
  6691. /**
  6692. * @param int $scale
  6693. * @return Column
  6694. */
  6695. public function setScale($scale)
  6696. {
  6697. $this->_scale = $scale;
  6698. return $this;
  6699. }
  6700. /**
  6701. *
  6702. * @param bool $unsigned
  6703. * @return Column
  6704. */
  6705. public function setUnsigned($unsigned)
  6706. {
  6707. $this->_unsigned = (bool)$unsigned;
  6708. return $this;
  6709. }
  6710. /**
  6711. *
  6712. * @param bool $fixed
  6713. * @return Column
  6714. */
  6715. public function setFixed($fixed)
  6716. {
  6717. $this->_fixed = (bool)$fixed;
  6718. return $this;
  6719. }
  6720. /**
  6721. * @param bool $notnull
  6722. * @return Column
  6723. */
  6724. public function setNotnull($notnull)
  6725. {
  6726. $this->_notnull = (bool)$notnull;
  6727. return $this;
  6728. }
  6729. /**
  6730. *
  6731. * @param mixed $default
  6732. * @return Column
  6733. */
  6734. public function setDefault($default)
  6735. {
  6736. $this->_default = $default;
  6737. return $this;
  6738. }
  6739. /**
  6740. *
  6741. * @param array $platformOptions
  6742. * @return Column
  6743. */
  6744. public function setPlatformOptions(array $platformOptions)
  6745. {
  6746. $this->_platformOptions = $platformOptions;
  6747. return $this;
  6748. }
  6749. /**
  6750. *
  6751. * @param string $name
  6752. * @param mixed $value
  6753. * @return Column
  6754. */
  6755. public function setPlatformOption($name, $value)
  6756. {
  6757. $this->_platformOptions[$name] = $value;
  6758. return $this;
  6759. }
  6760. /**
  6761. *
  6762. * @param string
  6763. * @return Column
  6764. */
  6765. public function setColumnDefinition($value)
  6766. {
  6767. $this->_columnDefinition = $value;
  6768. return $this;
  6769. }
  6770. public function getType()
  6771. {
  6772. return $this->_type;
  6773. }
  6774. public function getLength()
  6775. {
  6776. return $this->_length;
  6777. }
  6778. public function getPrecision()
  6779. {
  6780. return $this->_precision;
  6781. }
  6782. public function getScale()
  6783. {
  6784. return $this->_scale;
  6785. }
  6786. public function getUnsigned()
  6787. {
  6788. return $this->_unsigned;
  6789. }
  6790. public function getFixed()
  6791. {
  6792. return $this->_fixed;
  6793. }
  6794. public function getNotnull()
  6795. {
  6796. return $this->_notnull;
  6797. }
  6798. public function getDefault()
  6799. {
  6800. return $this->_default;
  6801. }
  6802. public function getPlatformOptions()
  6803. {
  6804. return $this->_platformOptions;
  6805. }
  6806. public function hasPlatformOption($name)
  6807. {
  6808. return isset($this->_platformOptions[$name]);
  6809. }
  6810. public function getPlatformOption($name)
  6811. {
  6812. return $this->_platformOptions[$name];
  6813. }
  6814. public function getColumnDefinition()
  6815. {
  6816. return $this->_columnDefinition;
  6817. }
  6818. public function getAutoincrement()
  6819. {
  6820. return $this->_autoincrement;
  6821. }
  6822. public function setAutoincrement($flag)
  6823. {
  6824. $this->_autoincrement = $flag;
  6825. return $this;
  6826. }
  6827. /**
  6828. * @param Visitor $visitor
  6829. */
  6830. public function visit(\Doctrine\DBAL\Schema\Visitor $visitor)
  6831. {
  6832. $visitor->accept($this);
  6833. }
  6834. /**
  6835. * @return array
  6836. */
  6837. public function toArray()
  6838. {
  6839. return array_merge(array(
  6840. 'name' => $this->_name,
  6841. 'type' => $this->_type,
  6842. 'default' => $this->_default,
  6843. 'notnull' => $this->_notnull,
  6844. 'length' => $this->_length,
  6845. 'precision' => $this->_precision,
  6846. 'scale' => $this->_scale,
  6847. 'fixed' => $this->_fixed,
  6848. 'unsigned' => $this->_unsigned,
  6849. 'autoincrement' => $this->_autoincrement,
  6850. 'columnDefinition' => $this->_columnDefinition,
  6851. ), $this->_platformOptions);
  6852. }
  6853. }<?php
  6854. /*
  6855. * $Id$
  6856. *
  6857. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6858. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  6859. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6860. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  6861. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  6862. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6863. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  6864. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  6865. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  6866. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  6867. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6868. *
  6869. * This software consists of voluntary contributions made by many individuals
  6870. * and is licensed under the LGPL. For more information, see
  6871. * <http://www.doctrine-project.org>.
  6872. */
  6873. namespace Doctrine\DBAL\Schema;
  6874. /**
  6875. * Marker interface for contraints
  6876. *
  6877. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  6878. * @link www.doctrine-project.org
  6879. * @since 2.0
  6880. * @version $Revision$
  6881. * @author Benjamin Eberlei <kontakt@beberlei.de>
  6882. */
  6883. interface Constraint
  6884. {
  6885. public function getName();
  6886. public function getColumns();
  6887. }<?php
  6888. /*
  6889. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6890. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  6891. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6892. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  6893. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  6894. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6895. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  6896. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  6897. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  6898. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  6899. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6900. *
  6901. * This software consists of voluntary contributions made by many individuals
  6902. * and is licensed under the LGPL. For more information, see
  6903. * <http://www.doctrine-project.org>.
  6904. */
  6905. namespace Doctrine\DBAL\Schema;
  6906. use \Doctrine\DBAL\Platforms\AbstractPlatform;
  6907. /**
  6908. * Schema Diff
  6909. *
  6910. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  6911. * @link www.doctrine-project.org
  6912. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  6913. * @license http://ez.no/licenses/new_bsd New BSD License
  6914. * @since 2.0
  6915. * @version $Revision$
  6916. * @author Benjamin Eberlei <kontakt@beberlei.de>
  6917. */
  6918. class SchemaDiff
  6919. {
  6920. /**
  6921. * All added tables
  6922. *
  6923. * @var array(string=>ezcDbSchemaTable)
  6924. */
  6925. public $newTables = array();
  6926. /**
  6927. * All changed tables
  6928. *
  6929. * @var array(string=>ezcDbSchemaTableDiff)
  6930. */
  6931. public $changedTables = array();
  6932. /**
  6933. * All removed tables
  6934. *
  6935. * @var array(string=>Table)
  6936. */
  6937. public $removedTables = array();
  6938. /**
  6939. * @var array
  6940. */
  6941. public $newSequences = array();
  6942. /**
  6943. * @var array
  6944. */
  6945. public $changedSequences = array();
  6946. /**
  6947. * @var array
  6948. */
  6949. public $removedSequences = array();
  6950. /**
  6951. * @var array
  6952. */
  6953. public $orphanedForeignKeys = array();
  6954. /**
  6955. * Constructs an SchemaDiff object.
  6956. *
  6957. * @param array(string=>Table) $newTables
  6958. * @param array(string=>TableDiff) $changedTables
  6959. * @param array(string=>bool) $removedTables
  6960. */
  6961. public function __construct($newTables = array(), $changedTables = array(), $removedTables = array())
  6962. {
  6963. $this->newTables = $newTables;
  6964. $this->changedTables = $changedTables;
  6965. $this->removedTables = $removedTables;
  6966. }
  6967. /**
  6968. * The to save sql mode ensures that the following things don't happen:
  6969. *
  6970. * 1. Tables are deleted
  6971. * 2. Sequences are deleted
  6972. * 3. Foreign Keys which reference tables that would otherwise be deleted.
  6973. *
  6974. * This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
  6975. *
  6976. * @param AbstractPlatform $platform
  6977. * @return array
  6978. */
  6979. public function toSaveSql(AbstractPlatform $platform)
  6980. {
  6981. return $this->_toSql($platform, true);
  6982. }
  6983. /**
  6984. * @param AbstractPlatform $platform
  6985. * @return array
  6986. */
  6987. public function toSql(AbstractPlatform $platform)
  6988. {
  6989. return $this->_toSql($platform, false);
  6990. }
  6991. /**
  6992. * @param AbstractPlatform $platform
  6993. * @param bool $saveMode
  6994. * @return array
  6995. */
  6996. protected function _toSql(AbstractPlatform $platform, $saveMode = false)
  6997. {
  6998. $sql = array();
  6999. if ($platform->supportsForeignKeyConstraints() && $saveMode == false) {
  7000. foreach ($this->orphanedForeignKeys AS $orphanedForeignKey) {
  7001. $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTableName());
  7002. }
  7003. }
  7004. if ($platform->supportsSequences() == true) {
  7005. foreach ($this->changedSequences AS $sequence) {
  7006. $sql[] = $platform->getDropSequenceSQL($sequence);
  7007. $sql[] = $platform->getCreateSequenceSQL($sequence);
  7008. }
  7009. if ($saveMode === false) {
  7010. foreach ($this->removedSequences AS $sequence) {
  7011. $sql[] = $platform->getDropSequenceSQL($sequence);
  7012. }
  7013. }
  7014. foreach ($this->newSequences AS $sequence) {
  7015. $sql[] = $platform->getCreateSequenceSQL($sequence);
  7016. }
  7017. }
  7018. $foreignKeySql = array();
  7019. foreach ($this->newTables AS $table) {
  7020. $sql = array_merge(
  7021. $sql,
  7022. $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)
  7023. );
  7024. if ($platform->supportsForeignKeyConstraints()) {
  7025. foreach ($table->getForeignKeys() AS $foreignKey) {
  7026. $foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
  7027. }
  7028. }
  7029. }
  7030. $sql = array_merge($sql, $foreignKeySql);
  7031. if ($saveMode === false) {
  7032. foreach ($this->removedTables AS $table) {
  7033. $sql[] = $platform->getDropTableSQL($table);
  7034. }
  7035. }
  7036. foreach ($this->changedTables AS $tableDiff) {
  7037. $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
  7038. }
  7039. return $sql;
  7040. }
  7041. }<?php
  7042. /*
  7043. * $Id$
  7044. *
  7045. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7046. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7047. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7048. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7049. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  7050. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7051. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  7052. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  7053. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  7054. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  7055. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7056. *
  7057. * This software consists of voluntary contributions made by many individuals
  7058. * and is licensed under the LGPL. For more information, see
  7059. * <http://www.doctrine-project.org>.
  7060. */
  7061. namespace Doctrine\DBAL\Schema;
  7062. use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
  7063. use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
  7064. use Doctrine\DBAL\Schema\Visitor\Visitor;
  7065. /**
  7066. * Object representation of a database schema
  7067. *
  7068. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  7069. * @link www.doctrine-project.org
  7070. * @since 2.0
  7071. * @version $Revision$
  7072. * @author Benjamin Eberlei <kontakt@beberlei.de>
  7073. */
  7074. class Schema extends AbstractAsset
  7075. {
  7076. /**
  7077. * @var array
  7078. */
  7079. protected $_tables = array();
  7080. /**
  7081. * @var array
  7082. */
  7083. protected $_sequences = array();
  7084. /**
  7085. * @var SchemaConfig
  7086. */
  7087. protected $_schemaConfig = false;
  7088. /**
  7089. * @param array $tables
  7090. * @param array $sequences
  7091. * @param array $views
  7092. * @param array $triggers
  7093. * @param SchemaConfig $schemaConfig
  7094. */
  7095. public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
  7096. {
  7097. if ($schemaConfig == null) {
  7098. $schemaConfig = new SchemaConfig();
  7099. }
  7100. $this->_schemaConfig = $schemaConfig;
  7101. foreach ($tables AS $table) {
  7102. $this->_addTable($table);
  7103. }
  7104. foreach ($sequences AS $sequence) {
  7105. $this->_addSequence($sequence);
  7106. }
  7107. }
  7108. /**
  7109. * @return bool
  7110. */
  7111. public function hasExplicitForeignKeyIndexes()
  7112. {
  7113. return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
  7114. }
  7115. /**
  7116. * @param Table $table
  7117. */
  7118. protected function _addTable(Table $table)
  7119. {
  7120. $tableName = strtolower($table->getName());
  7121. if(isset($this->_tables[$tableName])) {
  7122. throw SchemaException::tableAlreadyExists($tableName);
  7123. }
  7124. $this->_tables[$tableName] = $table;
  7125. $table->setSchemaConfig($this->_schemaConfig);
  7126. }
  7127. /**
  7128. * @param Sequence $sequence
  7129. */
  7130. protected function _addSequence(Sequence $sequence)
  7131. {
  7132. $seqName = strtolower($sequence->getName());
  7133. if (isset($this->_sequences[$seqName])) {
  7134. throw SchemaException::sequenceAlreadyExists($seqName);
  7135. }
  7136. $this->_sequences[$seqName] = $sequence;
  7137. }
  7138. /**
  7139. * Get all tables of this schema.
  7140. *
  7141. * @return array
  7142. */
  7143. public function getTables()
  7144. {
  7145. return $this->_tables;
  7146. }
  7147. /**
  7148. * @param string $tableName
  7149. * @return Table
  7150. */
  7151. public function getTable($tableName)
  7152. {
  7153. $tableName = strtolower($tableName);
  7154. if (!isset($this->_tables[$tableName])) {
  7155. throw SchemaException::tableDoesNotExist($tableName);
  7156. }
  7157. return $this->_tables[$tableName];
  7158. }
  7159. /**
  7160. * Does this schema have a table with the given name?
  7161. *
  7162. * @param string $tableName
  7163. * @return Schema
  7164. */
  7165. public function hasTable($tableName)
  7166. {
  7167. $tableName = strtolower($tableName);
  7168. return isset($this->_tables[$tableName]);
  7169. }
  7170. /**
  7171. * @param string $sequenceName
  7172. * @return bool
  7173. */
  7174. public function hasSequence($sequenceName)
  7175. {
  7176. $sequenceName = strtolower($sequenceName);
  7177. return isset($this->_sequences[$sequenceName]);
  7178. }
  7179. /**
  7180. * @throws SchemaException
  7181. * @param string $sequenceName
  7182. * @return Doctrine\DBAL\Schema\Sequence
  7183. */
  7184. public function getSequence($sequenceName)
  7185. {
  7186. $sequenceName = strtolower($sequenceName);
  7187. if(!$this->hasSequence($sequenceName)) {
  7188. throw SchemaException::sequenceDoesNotExist($sequenceName);
  7189. }
  7190. return $this->_sequences[$sequenceName];
  7191. }
  7192. /**
  7193. * @return Doctrine\DBAL\Schema\Sequence[]
  7194. */
  7195. public function getSequences()
  7196. {
  7197. return $this->_sequences;
  7198. }
  7199. /**
  7200. * Create a new table
  7201. *
  7202. * @param string $tableName
  7203. * @return Table
  7204. */
  7205. public function createTable($tableName)
  7206. {
  7207. $table = new Table($tableName);
  7208. $this->_addTable($table);
  7209. return $table;
  7210. }
  7211. /**
  7212. * Rename a table
  7213. *
  7214. * @param string $oldTableName
  7215. * @param string $newTableName
  7216. * @return Schema
  7217. */
  7218. public function renameTable($oldTableName, $newTableName)
  7219. {
  7220. $table = $this->getTable($oldTableName);
  7221. $table->_setName($newTableName);
  7222. $this->dropTable($oldTableName);
  7223. $this->_addTable($table);
  7224. return $this;
  7225. }
  7226. /**
  7227. * Drop a table from the schema.
  7228. *
  7229. * @param string $tableName
  7230. * @return Schema
  7231. */
  7232. public function dropTable($tableName)
  7233. {
  7234. $tableName = strtolower($tableName);
  7235. $table = $this->getTable($tableName);
  7236. unset($this->_tables[$tableName]);
  7237. return $this;
  7238. }
  7239. /**
  7240. * Create a new sequence
  7241. *
  7242. * @param string $sequenceName
  7243. * @param int $allocationSize
  7244. * @param int $initialValue
  7245. * @return Sequence
  7246. */
  7247. public function createSequence($sequenceName, $allocationSize=1, $initialValue=1)
  7248. {
  7249. $seq = new Sequence($sequenceName, $allocationSize, $initialValue);
  7250. $this->_addSequence($seq);
  7251. return $seq;
  7252. }
  7253. /**
  7254. * @param string $sequenceName
  7255. * @return Schema
  7256. */
  7257. public function dropSequence($sequenceName)
  7258. {
  7259. $sequenceName = strtolower($sequenceName);
  7260. unset($this->_sequences[$sequenceName]);
  7261. return $this;
  7262. }
  7263. /**
  7264. * Return an array of necessary sql queries to create the schema on the given platform.
  7265. *
  7266. * @param AbstractPlatform $platform
  7267. * @return array
  7268. */
  7269. public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  7270. {
  7271. $sqlCollector = new CreateSchemaSqlCollector($platform);
  7272. $this->visit($sqlCollector);
  7273. return $sqlCollector->getQueries();
  7274. }
  7275. /**
  7276. * Return an array of necessary sql queries to drop the schema on the given platform.
  7277. *
  7278. * @param AbstractPlatform $platform
  7279. * @return array
  7280. */
  7281. public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  7282. {
  7283. $dropSqlCollector = new DropSchemaSqlCollector($platform);
  7284. $this->visit($dropSqlCollector);
  7285. return $dropSqlCollector->getQueries();
  7286. }
  7287. /**
  7288. * @param Schema $toSchema
  7289. * @param AbstractPlatform $platform
  7290. */
  7291. public function getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  7292. {
  7293. $comparator = new Comparator();
  7294. $schemaDiff = $comparator->compare($this, $toSchema);
  7295. return $schemaDiff->toSql($platform);
  7296. }
  7297. /**
  7298. * @param Schema $fromSchema
  7299. * @param AbstractPlatform $platform
  7300. */
  7301. public function getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  7302. {
  7303. $comparator = new Comparator();
  7304. $schemaDiff = $comparator->compare($fromSchema, $this);
  7305. return $schemaDiff->toSql($platform);
  7306. }
  7307. /**
  7308. * @param Visitor $visitor
  7309. */
  7310. public function visit(Visitor $visitor)
  7311. {
  7312. $visitor->acceptSchema($this);
  7313. foreach ($this->_tables AS $table) {
  7314. $table->visit($visitor);
  7315. }
  7316. foreach ($this->_sequences AS $sequence) {
  7317. $sequence->visit($visitor);
  7318. }
  7319. }
  7320. /**
  7321. * Cloning a Schema triggers a deep clone of all related assets.
  7322. *
  7323. * @return void
  7324. */
  7325. public function __clone()
  7326. {
  7327. foreach ($this->_tables AS $k => $table) {
  7328. $this->_tables[$k] = clone $table;
  7329. }
  7330. foreach ($this->_sequences AS $k => $sequence) {
  7331. $this->_sequences[$k] = clone $sequence;
  7332. }
  7333. }
  7334. }
  7335. <?php
  7336. /*
  7337. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7338. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7339. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7340. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7341. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  7342. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7343. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  7344. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  7345. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  7346. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  7347. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7348. *
  7349. * This software consists of voluntary contributions made by many individuals
  7350. * and is licensed under the LGPL. For more information, see
  7351. * <http://www.phpdoctrine.org>.
  7352. */
  7353. namespace Doctrine\DBAL\Schema;
  7354. /**
  7355. * xxx
  7356. *
  7357. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  7358. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  7359. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  7360. * @author Juozas Kaziukenas <juozas@juokaz.com>
  7361. * @version $Revision$
  7362. * @since 2.0
  7363. */
  7364. class MsSqlSchemaManager extends AbstractSchemaManager
  7365. {
  7366. /**
  7367. * @override
  7368. */
  7369. protected function _getPortableTableColumnDefinition($tableColumn)
  7370. {
  7371. $dbType = strtolower($tableColumn['TYPE_NAME']);
  7372. $autoincrement = false;
  7373. if (stripos($dbType, 'identity')) {
  7374. $dbType = trim(str_ireplace('identity', '', $dbType));
  7375. $autoincrement = true;
  7376. }
  7377. $type = array();
  7378. $unsigned = $fixed = null;
  7379. if (!isset($tableColumn['name'])) {
  7380. $tableColumn['name'] = '';
  7381. }
  7382. $default = $tableColumn['COLUMN_DEF'];
  7383. while ($default != ($default2 = preg_replace("/^\((.*)\)$/", '$1', $default))) {
  7384. $default = $default2;
  7385. }
  7386. $length = (int) $tableColumn['LENGTH'];
  7387. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  7388. switch ($type) {
  7389. case 'char':
  7390. if ($tableColumn['LENGTH'] == '1') {
  7391. $type = 'boolean';
  7392. if (preg_match('/^(is|has)/', $tableColumn['name'])) {
  7393. $type = array_reverse($type);
  7394. }
  7395. }
  7396. $fixed = true;
  7397. break;
  7398. case 'text':
  7399. $fixed = false;
  7400. break;
  7401. }
  7402. switch ($dbType) {
  7403. case 'nchar':
  7404. case 'nvarchar':
  7405. case 'ntext':
  7406. // Unicode data requires 2 bytes per character
  7407. $length = $length / 2;
  7408. break;
  7409. }
  7410. $options = array(
  7411. 'length' => ($length == 0 || !in_array($type, array('text', 'string'))) ? null : $length,
  7412. 'unsigned' => (bool) $unsigned,
  7413. 'fixed' => (bool) $fixed,
  7414. 'default' => $default !== 'NULL' ? $default : null,
  7415. 'notnull' => (bool) ($tableColumn['IS_NULLABLE'] != 'YES'),
  7416. 'scale' => $tableColumn['SCALE'],
  7417. 'precision' => $tableColumn['PRECISION'],
  7418. 'autoincrement' => $autoincrement,
  7419. );
  7420. return new Column($tableColumn['COLUMN_NAME'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  7421. }
  7422. /**
  7423. * @override
  7424. */
  7425. protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
  7426. {
  7427. $result = array();
  7428. foreach ($tableIndexRows AS $tableIndex) {
  7429. $indexName = $keyName = $tableIndex['index_name'];
  7430. if (strpos($tableIndex['index_description'], 'primary key') !== false) {
  7431. $keyName = 'primary';
  7432. }
  7433. $keyName = strtolower($keyName);
  7434. $result[$keyName] = array(
  7435. 'name' => $indexName,
  7436. 'columns' => explode(', ', $tableIndex['index_keys']),
  7437. 'unique' => strpos($tableIndex['index_description'], 'unique') !== false,
  7438. 'primary' => strpos($tableIndex['index_description'], 'primary key') !== false,
  7439. );
  7440. }
  7441. $indexes = array();
  7442. foreach ($result AS $indexKey => $data) {
  7443. $indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
  7444. }
  7445. return $indexes;
  7446. }
  7447. /**
  7448. * @override
  7449. */
  7450. public function _getPortableTableForeignKeyDefinition($tableForeignKey)
  7451. {
  7452. return new ForeignKeyConstraint(
  7453. (array) $tableForeignKey['ColumnName'],
  7454. $tableForeignKey['ReferenceTableName'],
  7455. (array) $tableForeignKey['ReferenceColumnName'],
  7456. $tableForeignKey['ForeignKey'],
  7457. array(
  7458. 'onUpdate' => str_replace('_', ' ', $tableForeignKey['update_referential_action_desc']),
  7459. 'onDelete' => str_replace('_', ' ', $tableForeignKey['delete_referential_action_desc']),
  7460. )
  7461. );
  7462. }
  7463. /**
  7464. * @override
  7465. */
  7466. protected function _getPortableTableDefinition($table)
  7467. {
  7468. return $table['name'];
  7469. }
  7470. /**
  7471. * @override
  7472. */
  7473. protected function _getPortableDatabaseDefinition($database)
  7474. {
  7475. return $database['name'];
  7476. }
  7477. /**
  7478. * @override
  7479. */
  7480. protected function _getPortableViewDefinition($view)
  7481. {
  7482. // @todo
  7483. return new View($view['name'], null);
  7484. }
  7485. }<?php
  7486. /*
  7487. * $Id$
  7488. *
  7489. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7490. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7491. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7492. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7493. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  7494. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7495. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  7496. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  7497. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  7498. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  7499. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7500. *
  7501. * This software consists of voluntary contributions made by many individuals
  7502. * and is licensed under the LGPL. For more information, see
  7503. * <http://www.doctrine-project.org>.
  7504. */
  7505. namespace Doctrine\DBAL\Schema\Visitor;
  7506. use Doctrine\DBAL\Platforms\AbstractPlatform,
  7507. Doctrine\DBAL\Schema\Table,
  7508. Doctrine\DBAL\Schema\Schema,
  7509. Doctrine\DBAL\Schema\Column,
  7510. Doctrine\DBAL\Schema\ForeignKeyConstraint,
  7511. Doctrine\DBAL\Schema\Constraint,
  7512. Doctrine\DBAL\Schema\Sequence,
  7513. Doctrine\DBAL\Schema\Index;
  7514. /**
  7515. * Schema Visitor used for Validation or Generation purposes.
  7516. *
  7517. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  7518. * @link www.doctrine-project.org
  7519. * @since 2.0
  7520. * @version $Revision$
  7521. * @author Benjamin Eberlei <kontakt@beberlei.de>
  7522. */
  7523. interface Visitor
  7524. {
  7525. /**
  7526. * @param Schema $schema
  7527. */
  7528. public function acceptSchema(Schema $schema);
  7529. /**
  7530. * @param Table $table
  7531. */
  7532. public function acceptTable(Table $table);
  7533. /**
  7534. * @param Column $column
  7535. */
  7536. public function acceptColumn(Table $table, Column $column);
  7537. /**
  7538. * @param Table $localTable
  7539. * @param ForeignKeyConstraint $fkConstraint
  7540. */
  7541. public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint);
  7542. /**
  7543. * @param Table $table
  7544. * @param Index $index
  7545. */
  7546. public function acceptIndex(Table $table, Index $index);
  7547. /**
  7548. * @param Sequence $sequence
  7549. */
  7550. public function acceptSequence(Sequence $sequence);
  7551. }<?php
  7552. /*
  7553. * $Id$
  7554. *
  7555. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7556. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7557. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7558. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7559. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  7560. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7561. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  7562. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  7563. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  7564. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  7565. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7566. *
  7567. * This software consists of voluntary contributions made by many individuals
  7568. * and is licensed under the LGPL. For more information, see
  7569. * <http://www.doctrine-project.org>.
  7570. */
  7571. namespace Doctrine\DBAL\Schema\Visitor;
  7572. use Doctrine\DBAL\Platforms\AbstractPlatform,
  7573. Doctrine\DBAL\Schema\Table,
  7574. Doctrine\DBAL\Schema\Schema,
  7575. Doctrine\DBAL\Schema\Column,
  7576. Doctrine\DBAL\Schema\ForeignKeyConstraint,
  7577. Doctrine\DBAL\Schema\Constraint,
  7578. Doctrine\DBAL\Schema\Sequence,
  7579. Doctrine\DBAL\Schema\Index;
  7580. class CreateSchemaSqlCollector implements Visitor
  7581. {
  7582. /**
  7583. * @var array
  7584. */
  7585. private $_createTableQueries = array();
  7586. /**
  7587. * @var array
  7588. */
  7589. private $_createSequenceQueries = array();
  7590. /**
  7591. * @var array
  7592. */
  7593. private $_createFkConstraintQueries = array();
  7594. /**
  7595. *
  7596. * @var \Doctrine\DBAL\Platforms\AbstractPlatform
  7597. */
  7598. private $_platform = null;
  7599. /**
  7600. * @param AbstractPlatform $platform
  7601. */
  7602. public function __construct(AbstractPlatform $platform)
  7603. {
  7604. $this->_platform = $platform;
  7605. }
  7606. /**
  7607. * @param Schema $schema
  7608. */
  7609. public function acceptSchema(Schema $schema)
  7610. {
  7611. }
  7612. /**
  7613. * Generate DDL Statements to create the accepted table with all its dependencies.
  7614. *
  7615. * @param Table $table
  7616. */
  7617. public function acceptTable(Table $table)
  7618. {
  7619. $this->_createTableQueries = array_merge($this->_createTableQueries,
  7620. $this->_platform->getCreateTableSQL($table)
  7621. );
  7622. }
  7623. public function acceptColumn(Table $table, Column $column)
  7624. {
  7625. }
  7626. /**
  7627. * @param Table $localTable
  7628. * @param ForeignKeyConstraint $fkConstraint
  7629. */
  7630. public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
  7631. {
  7632. // Append the foreign key constraints SQL
  7633. if ($this->_platform->supportsForeignKeyConstraints()) {
  7634. $this->_createFkConstraintQueries = array_merge($this->_createFkConstraintQueries,
  7635. (array) $this->_platform->getCreateForeignKeySQL($fkConstraint, $localTable->getName())
  7636. );
  7637. }
  7638. }
  7639. /**
  7640. * @param Table $table
  7641. * @param Index $index
  7642. */
  7643. public function acceptIndex(Table $table, Index $index)
  7644. {
  7645. }
  7646. /**
  7647. * @param Sequence $sequence
  7648. */
  7649. public function acceptSequence(Sequence $sequence)
  7650. {
  7651. $this->_createSequenceQueries = array_merge(
  7652. $this->_createSequenceQueries, (array)$this->_platform->getCreateSequenceSQL($sequence)
  7653. );
  7654. }
  7655. /**
  7656. * @return array
  7657. */
  7658. public function resetQueries()
  7659. {
  7660. $this->_createTableQueries = array();
  7661. $this->_createSequenceQueries = array();
  7662. $this->_createFkConstraintQueries = array();
  7663. }
  7664. /**
  7665. * Get all queries collected so far.
  7666. *
  7667. * @return array
  7668. */
  7669. public function getQueries()
  7670. {
  7671. return array_merge(
  7672. $this->_createTableQueries,
  7673. $this->_createSequenceQueries,
  7674. $this->_createFkConstraintQueries
  7675. );
  7676. }
  7677. }<?php
  7678. /*
  7679. * $Id$
  7680. *
  7681. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7682. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7683. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7684. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7685. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  7686. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7687. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  7688. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  7689. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  7690. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  7691. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7692. *
  7693. * This software consists of voluntary contributions made by many individuals
  7694. * and is licensed under the LGPL. For more information, see
  7695. * <http://www.doctrine-project.org>.
  7696. */
  7697. namespace Doctrine\DBAL\Schema\Visitor;
  7698. use Doctrine\DBAL\Platforms\AbstractPlatform,
  7699. Doctrine\DBAL\Schema\Table,
  7700. Doctrine\DBAL\Schema\Schema,
  7701. Doctrine\DBAL\Schema\Column,
  7702. Doctrine\DBAL\Schema\ForeignKeyConstraint,
  7703. Doctrine\DBAL\Schema\Constraint,
  7704. Doctrine\DBAL\Schema\Sequence,
  7705. Doctrine\DBAL\Schema\Index;
  7706. /**
  7707. * Gather SQL statements that allow to completly drop the current schema.
  7708. *
  7709. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  7710. * @link www.doctrine-project.org
  7711. * @since 2.0
  7712. * @version $Revision$
  7713. * @author Benjamin Eberlei <kontakt@beberlei.de>
  7714. */
  7715. class DropSchemaSqlCollector implements Visitor
  7716. {
  7717. /**
  7718. * @var array
  7719. */
  7720. private $_constraints = array();
  7721. /**
  7722. * @var array
  7723. */
  7724. private $_sequences = array();
  7725. /**
  7726. * @var array
  7727. */
  7728. private $_tables = array();
  7729. /**
  7730. *
  7731. * @var \Doctrine\DBAL\Platforms\AbstractPlatform
  7732. */
  7733. private $_platform = null;
  7734. /**
  7735. * @param AbstractPlatform $platform
  7736. */
  7737. public function __construct(AbstractPlatform $platform)
  7738. {
  7739. $this->_platform = $platform;
  7740. }
  7741. /**
  7742. * @param Schema $schema
  7743. */
  7744. public function acceptSchema(Schema $schema)
  7745. {
  7746. }
  7747. /**
  7748. * @param Table $table
  7749. */
  7750. public function acceptTable(Table $table)
  7751. {
  7752. $this->_tables[] = $this->_platform->getDropTableSQL($table->getName());
  7753. }
  7754. /**
  7755. * @param Column $column
  7756. */
  7757. public function acceptColumn(Table $table, Column $column)
  7758. {
  7759. }
  7760. /**
  7761. * @param Table $localTable
  7762. * @param ForeignKeyConstraint $fkConstraint
  7763. */
  7764. public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
  7765. {
  7766. if (strlen($fkConstraint->getName()) == 0) {
  7767. throw SchemaException::namedForeignKeyRequired($localTable, $fkConstraint);
  7768. }
  7769. $this->_constraints[] = $this->_platform->getDropForeignKeySQL($fkConstraint->getName(), $localTable->getName());
  7770. }
  7771. /**
  7772. * @param Table $table
  7773. * @param Index $index
  7774. */
  7775. public function acceptIndex(Table $table, Index $index)
  7776. {
  7777. }
  7778. /**
  7779. * @param Sequence $sequence
  7780. */
  7781. public function acceptSequence(Sequence $sequence)
  7782. {
  7783. $this->_sequences[] = $this->_platform->getDropSequenceSQL($sequence->getName());
  7784. }
  7785. /**
  7786. * @return array
  7787. */
  7788. public function clearQueries()
  7789. {
  7790. $this->_constraints = $this->_sequences = $this->_tables = array();
  7791. }
  7792. /**
  7793. * @return array
  7794. */
  7795. public function getQueries()
  7796. {
  7797. return array_merge($this->_constraints, $this->_tables, $this->_sequences);
  7798. }
  7799. }
  7800. <?php
  7801. /*
  7802. * $Id$
  7803. *
  7804. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7805. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7806. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7807. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7808. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  7809. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7810. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  7811. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  7812. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  7813. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  7814. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7815. *
  7816. * This software consists of voluntary contributions made by many individuals
  7817. * and is licensed under the LGPL. For more information, see
  7818. * <http://www.doctrine-project.org>.
  7819. */
  7820. namespace Doctrine\DBAL\Schema;
  7821. use Doctrine\DBAL\Schema\Visitor\Visitor;
  7822. class ForeignKeyConstraint extends AbstractAsset implements Constraint
  7823. {
  7824. /**
  7825. * @var Table
  7826. */
  7827. protected $_localTable;
  7828. /**
  7829. * @var array
  7830. */
  7831. protected $_localColumnNames;
  7832. /**
  7833. * @var string
  7834. */
  7835. protected $_foreignTableName;
  7836. /**
  7837. * @var array
  7838. */
  7839. protected $_foreignColumnNames;
  7840. /**
  7841. * @var string
  7842. */
  7843. protected $_cascade = '';
  7844. /**
  7845. * @var array
  7846. */
  7847. protected $_options;
  7848. /**
  7849. *
  7850. * @param array $localColumnNames
  7851. * @param string $foreignTableName
  7852. * @param array $foreignColumnNames
  7853. * @param string $cascade
  7854. * @param string|null $name
  7855. */
  7856. public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name=null, array $options=array())
  7857. {
  7858. $this->_setName($name);
  7859. $this->_localColumnNames = $localColumnNames;
  7860. $this->_foreignTableName = $foreignTableName;
  7861. $this->_foreignColumnNames = $foreignColumnNames;
  7862. $this->_options = $options;
  7863. }
  7864. /**
  7865. * @return string
  7866. */
  7867. public function getLocalTableName()
  7868. {
  7869. return $this->_localTable->getName();
  7870. }
  7871. /**
  7872. * @param Table $table
  7873. */
  7874. public function setLocalTable(Table $table)
  7875. {
  7876. $this->_localTable = $table;
  7877. }
  7878. /**
  7879. * @return array
  7880. */
  7881. public function getLocalColumns()
  7882. {
  7883. return $this->_localColumnNames;
  7884. }
  7885. public function getColumns()
  7886. {
  7887. return $this->_localColumnNames;
  7888. }
  7889. /**
  7890. * @return string
  7891. */
  7892. public function getForeignTableName()
  7893. {
  7894. return $this->_foreignTableName;
  7895. }
  7896. /**
  7897. * @return array
  7898. */
  7899. public function getForeignColumns()
  7900. {
  7901. return $this->_foreignColumnNames;
  7902. }
  7903. public function hasOption($name)
  7904. {
  7905. return isset($this->_options[$name]);
  7906. }
  7907. public function getOption($name)
  7908. {
  7909. return $this->_options[$name];
  7910. }
  7911. /**
  7912. * Foreign Key onUpdate status
  7913. *
  7914. * @return string|null
  7915. */
  7916. public function onUpdate()
  7917. {
  7918. return $this->_onEvent('onUpdate');
  7919. }
  7920. /**
  7921. * Foreign Key onDelete status
  7922. *
  7923. * @return string|null
  7924. */
  7925. public function onDelete()
  7926. {
  7927. return $this->_onEvent('onDelete');
  7928. }
  7929. /**
  7930. * @param string $event
  7931. * @return string|null
  7932. */
  7933. private function _onEvent($event)
  7934. {
  7935. if (isset($this->_options[$event])) {
  7936. $onEvent = strtoupper($this->_options[$event]);
  7937. if (!in_array($onEvent, array('NO ACTION', 'RESTRICT'))) {
  7938. return $onEvent;
  7939. }
  7940. }
  7941. return false;
  7942. }
  7943. }
  7944. <?php
  7945. /*
  7946. * $Id$
  7947. *
  7948. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7949. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7950. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7951. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7952. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  7953. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7954. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  7955. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  7956. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  7957. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  7958. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7959. *
  7960. * This software consists of voluntary contributions made by many individuals
  7961. * and is licensed under the LGPL. For more information, see
  7962. * <http://www.doctrine-project.org>.
  7963. */
  7964. namespace Doctrine\DBAL\Schema;
  7965. use Doctrine\DBAL\Types\Type;
  7966. use Doctrine\DBAL\Schema\Visitor\Visitor;
  7967. use Doctrine\DBAL\DBALException;
  7968. /**
  7969. * Object Representation of a table
  7970. *
  7971. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  7972. * @link www.doctrine-project.org
  7973. * @since 2.0
  7974. * @version $Revision$
  7975. * @author Benjamin Eberlei <kontakt@beberlei.de>
  7976. */
  7977. class Table extends AbstractAsset
  7978. {
  7979. /**
  7980. * @var string
  7981. */
  7982. protected $_name = null;
  7983. /**
  7984. * @var array
  7985. */
  7986. protected $_columns = array();
  7987. /**
  7988. * @var array
  7989. */
  7990. protected $_indexes = array();
  7991. /**
  7992. * @var string
  7993. */
  7994. protected $_primaryKeyName = false;
  7995. /**
  7996. * @var array
  7997. */
  7998. protected $_fkConstraints = array();
  7999. /**
  8000. * @var array
  8001. */
  8002. protected $_options = array();
  8003. /**
  8004. * @var SchemaConfig
  8005. */
  8006. protected $_schemaConfig = null;
  8007. /**
  8008. *
  8009. * @param string $tableName
  8010. * @param array $columns
  8011. * @param array $indexes
  8012. * @param array $fkConstraints
  8013. * @param int $idGeneratorType
  8014. * @param array $options
  8015. */
  8016. public function __construct($tableName, array $columns=array(), array $indexes=array(), array $fkConstraints=array(), $idGeneratorType = 0, array $options=array())
  8017. {
  8018. if (strlen($tableName) == 0) {
  8019. throw DBALException::invalidTableName($tableName);
  8020. }
  8021. $this->_setName($tableName);
  8022. $this->_idGeneratorType = $idGeneratorType;
  8023. foreach ($columns AS $column) {
  8024. $this->_addColumn($column);
  8025. }
  8026. foreach ($indexes AS $idx) {
  8027. $this->_addIndex($idx);
  8028. }
  8029. foreach ($fkConstraints AS $constraint) {
  8030. $this->_addForeignKeyConstraint($constraint);
  8031. }
  8032. $this->_options = $options;
  8033. }
  8034. /**
  8035. * @param SchemaConfig $schemaConfig
  8036. */
  8037. public function setSchemaConfig(SchemaConfig $schemaConfig)
  8038. {
  8039. $this->_schemaConfig = $schemaConfig;
  8040. }
  8041. /**
  8042. * @return int
  8043. */
  8044. protected function _getMaxIdentifierLength()
  8045. {
  8046. if ($this->_schemaConfig instanceof SchemaConfig) {
  8047. return $this->_schemaConfig->getMaxIdentifierLength();
  8048. } else {
  8049. return 63;
  8050. }
  8051. }
  8052. /**
  8053. * Set Primary Key
  8054. *
  8055. * @param array $columns
  8056. * @param string $indexName
  8057. * @return Table
  8058. */
  8059. public function setPrimaryKey(array $columns, $indexName = false)
  8060. {
  8061. $primaryKey = $this->_createIndex($columns, $indexName ?: "primary", true, true);
  8062. foreach ($columns AS $columnName) {
  8063. $column = $this->getColumn($columnName);
  8064. $column->setNotnull(true);
  8065. }
  8066. return $primaryKey;
  8067. }
  8068. /**
  8069. * @param array $columnNames
  8070. * @param string $indexName
  8071. * @return Table
  8072. */
  8073. public function addIndex(array $columnNames, $indexName = null)
  8074. {
  8075. if($indexName == null) {
  8076. $indexName = $this->_generateIdentifierName(
  8077. array_merge(array($this->getName()), $columnNames), "idx", $this->_getMaxIdentifierLength()
  8078. );
  8079. }
  8080. return $this->_createIndex($columnNames, $indexName, false, false);
  8081. }
  8082. /**
  8083. *
  8084. * @param array $columnNames
  8085. * @param string $indexName
  8086. * @return Table
  8087. */
  8088. public function addUniqueIndex(array $columnNames, $indexName = null)
  8089. {
  8090. if ($indexName == null) {
  8091. $indexName = $this->_generateIdentifierName(
  8092. array_merge(array($this->getName()), $columnNames), "uniq", $this->_getMaxIdentifierLength()
  8093. );
  8094. }
  8095. return $this->_createIndex($columnNames, $indexName, true, false);
  8096. }
  8097. /**
  8098. * Check if an index begins in the order of the given columns.
  8099. *
  8100. * @param array $columnsNames
  8101. * @return bool
  8102. */
  8103. public function columnsAreIndexed(array $columnsNames)
  8104. {
  8105. foreach ($this->getIndexes() AS $index) {
  8106. /* @var $index Index */
  8107. if ($index->spansColumns($columnsNames)) {
  8108. return true;
  8109. }
  8110. }
  8111. return false;
  8112. }
  8113. /**
  8114. *
  8115. * @param array $columnNames
  8116. * @param string $indexName
  8117. * @param bool $isUnique
  8118. * @param bool $isPrimary
  8119. * @return Table
  8120. */
  8121. private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary)
  8122. {
  8123. if (preg_match('(([^a-zA-Z0-9_]+))', $indexName)) {
  8124. throw SchemaException::indexNameInvalid($indexName);
  8125. }
  8126. foreach ($columnNames AS $columnName => $indexColOptions) {
  8127. if (is_numeric($columnName) && is_string($indexColOptions)) {
  8128. $columnName = $indexColOptions;
  8129. }
  8130. if ( ! $this->hasColumn($columnName)) {
  8131. throw SchemaException::columnDoesNotExist($columnName, $this->_name);
  8132. }
  8133. }
  8134. $this->_addIndex(new Index($indexName, $columnNames, $isUnique, $isPrimary));
  8135. return $this;
  8136. }
  8137. /**
  8138. * @param string $columnName
  8139. * @param string $columnType
  8140. * @param array $options
  8141. * @return Column
  8142. */
  8143. public function addColumn($columnName, $typeName, array $options=array())
  8144. {
  8145. $column = new Column($columnName, Type::getType($typeName), $options);
  8146. $this->_addColumn($column);
  8147. return $column;
  8148. }
  8149. /**
  8150. * Rename Column
  8151. *
  8152. * @param string $oldColumnName
  8153. * @param string $newColumnName
  8154. * @return Table
  8155. */
  8156. public function renameColumn($oldColumnName, $newColumnName)
  8157. {
  8158. $column = $this->getColumn($oldColumnName);
  8159. $this->dropColumn($oldColumnName);
  8160. $column->_setName($newColumnName);
  8161. return $this;
  8162. }
  8163. /**
  8164. * Change Column Details
  8165. *
  8166. * @param string $columnName
  8167. * @param array $options
  8168. * @return Table
  8169. */
  8170. public function changeColumn($columnName, array $options)
  8171. {
  8172. $column = $this->getColumn($columnName);
  8173. $column->setOptions($options);
  8174. return $this;
  8175. }
  8176. /**
  8177. * Drop Column from Table
  8178. *
  8179. * @param string $columnName
  8180. * @return Table
  8181. */
  8182. public function dropColumn($columnName)
  8183. {
  8184. $columnName = strtolower($columnName);
  8185. $column = $this->getColumn($columnName);
  8186. unset($this->_columns[$columnName]);
  8187. return $this;
  8188. }
  8189. /**
  8190. * Add a foreign key constraint
  8191. *
  8192. * Name is inferred from the local columns
  8193. *
  8194. * @param Table $foreignTable
  8195. * @param array $localColumns
  8196. * @param array $foreignColumns
  8197. * @param array $options
  8198. * @return Table
  8199. */
  8200. public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
  8201. {
  8202. $name = $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength());
  8203. return $this->addNamedForeignKeyConstraint($name, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
  8204. }
  8205. /**
  8206. * Add a foreign key constraint
  8207. *
  8208. * Name is to be generated by the database itsself.
  8209. *
  8210. * @param Table $foreignTable
  8211. * @param array $localColumns
  8212. * @param array $foreignColumns
  8213. * @param array $options
  8214. * @return Table
  8215. */
  8216. public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
  8217. {
  8218. return $this->addNamedForeignKeyConstraint(null, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
  8219. }
  8220. /**
  8221. * Add a foreign key constraint with a given name
  8222. *
  8223. * @param string $name
  8224. * @param Table $foreignTable
  8225. * @param array $localColumns
  8226. * @param array $foreignColumns
  8227. * @param array $options
  8228. * @return Table
  8229. */
  8230. public function addNamedForeignKeyConstraint($name, $foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
  8231. {
  8232. if ($foreignTable instanceof Table) {
  8233. $foreignTableName = $foreignTable->getName();
  8234. foreach ($foreignColumnNames AS $columnName) {
  8235. if ( ! $foreignTable->hasColumn($columnName)) {
  8236. throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName());
  8237. }
  8238. }
  8239. } else {
  8240. $foreignTableName = $foreignTable;
  8241. }
  8242. foreach ($localColumnNames AS $columnName) {
  8243. if ( ! $this->hasColumn($columnName)) {
  8244. throw SchemaException::columnDoesNotExist($columnName, $this->_name);
  8245. }
  8246. }
  8247. $constraint = new ForeignKeyConstraint(
  8248. $localColumnNames, $foreignTableName, $foreignColumnNames, $name, $options
  8249. );
  8250. $this->_addForeignKeyConstraint($constraint);
  8251. return $this;
  8252. }
  8253. /**
  8254. * @param string $name
  8255. * @param string $value
  8256. * @return Table
  8257. */
  8258. public function addOption($name, $value)
  8259. {
  8260. $this->_options[$name] = $value;
  8261. return $this;
  8262. }
  8263. /**
  8264. * @param Column $column
  8265. */
  8266. protected function _addColumn(Column $column)
  8267. {
  8268. $columnName = $column->getName();
  8269. $columnName = strtolower($columnName);
  8270. if (isset($this->_columns[$columnName])) {
  8271. throw SchemaException::columnAlreadyExists($this->getName(), $columnName);
  8272. }
  8273. $this->_columns[$columnName] = $column;
  8274. }
  8275. /**
  8276. * Add index to table
  8277. *
  8278. * @param Index $indexCandidate
  8279. * @return Table
  8280. */
  8281. protected function _addIndex(Index $indexCandidate)
  8282. {
  8283. // check for duplicates
  8284. foreach ($this->_indexes AS $existingIndex) {
  8285. if ($indexCandidate->isFullfilledBy($existingIndex)) {
  8286. return $this;
  8287. }
  8288. }
  8289. $indexName = $indexCandidate->getName();
  8290. $indexName = strtolower($indexName);
  8291. if (isset($this->_indexes[$indexName]) || ($this->_primaryKeyName != false && $indexCandidate->isPrimary())) {
  8292. throw SchemaException::indexAlreadyExists($indexName, $this->_name);
  8293. }
  8294. // remove overruled indexes
  8295. foreach ($this->_indexes AS $idxKey => $existingIndex) {
  8296. if ($indexCandidate->overrules($existingIndex)) {
  8297. unset($this->_indexes[$idxKey]);
  8298. }
  8299. }
  8300. if ($indexCandidate->isPrimary()) {
  8301. $this->_primaryKeyName = $indexName;
  8302. }
  8303. $this->_indexes[$indexName] = $indexCandidate;
  8304. return $this;
  8305. }
  8306. /**
  8307. * @param ForeignKeyConstraint $constraint
  8308. */
  8309. protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
  8310. {
  8311. $constraint->setLocalTable($this);
  8312. if(strlen($constraint->getName())) {
  8313. $name = $constraint->getName();
  8314. } else {
  8315. $name = $this->_generateIdentifierName(
  8316. array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength()
  8317. );
  8318. }
  8319. $name = strtolower($name);
  8320. $this->_fkConstraints[$name] = $constraint;
  8321. // add an explicit index on the foreign key columns. If there is already an index that fullfils this requirements drop the request.
  8322. // In the case of __construct calling this method during hydration from schema-details all the explicitly added indexes
  8323. // lead to duplicates. This creates compuation overhead in this case, however no duplicate indexes are ever added (based on columns).
  8324. $this->addIndex($constraint->getColumns());
  8325. }
  8326. /**
  8327. * Does Table have a foreign key constraint with the given name?
  8328. * *
  8329. * @param string $constraintName
  8330. * @return bool
  8331. */
  8332. public function hasForeignKey($constraintName)
  8333. {
  8334. $constraintName = strtolower($constraintName);
  8335. return isset($this->_fkConstraints[$constraintName]);
  8336. }
  8337. /**
  8338. * @param string $constraintName
  8339. * @return ForeignKeyConstraint
  8340. */
  8341. public function getForeignKey($constraintName)
  8342. {
  8343. $constraintName = strtolower($constraintName);
  8344. if(!$this->hasForeignKey($constraintName)) {
  8345. throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
  8346. }
  8347. return $this->_fkConstraints[$constraintName];
  8348. }
  8349. /**
  8350. * @return Column[]
  8351. */
  8352. public function getColumns()
  8353. {
  8354. $columns = $this->_columns;
  8355. $pkCols = array();
  8356. $fkCols = array();
  8357. if ($this->hasIndex($this->_primaryKeyName)) {
  8358. $pkCols = $this->getPrimaryKey()->getColumns();
  8359. }
  8360. foreach ($this->getForeignKeys() AS $fk) {
  8361. /* @var $fk ForeignKeyConstraint */
  8362. $fkCols = array_merge($fkCols, $fk->getColumns());
  8363. }
  8364. $colNames = array_unique(array_merge($pkCols, $fkCols, array_keys($columns)));
  8365. uksort($columns, function($a, $b) use($colNames) {
  8366. return (array_search($a, $colNames) >= array_search($b, $colNames));
  8367. });
  8368. return $columns;
  8369. }
  8370. /**
  8371. * Does this table have a column with the given name?
  8372. *
  8373. * @param string $columnName
  8374. * @return bool
  8375. */
  8376. public function hasColumn($columnName)
  8377. {
  8378. $columnName = strtolower($columnName);
  8379. return isset($this->_columns[$columnName]);
  8380. }
  8381. /**
  8382. * Get a column instance
  8383. *
  8384. * @param string $columnName
  8385. * @return Column
  8386. */
  8387. public function getColumn($columnName)
  8388. {
  8389. $columnName = strtolower($columnName);
  8390. if (!$this->hasColumn($columnName)) {
  8391. throw SchemaException::columnDoesNotExist($columnName, $this->_name);
  8392. }
  8393. return $this->_columns[$columnName];
  8394. }
  8395. /**
  8396. * @return Index
  8397. */
  8398. public function getPrimaryKey()
  8399. {
  8400. return $this->getIndex($this->_primaryKeyName);
  8401. }
  8402. /**
  8403. * @param string $indexName
  8404. * @return bool
  8405. */
  8406. public function hasIndex($indexName)
  8407. {
  8408. $indexName = strtolower($indexName);
  8409. return (isset($this->_indexes[$indexName]));
  8410. }
  8411. /**
  8412. * @param string $indexName
  8413. * @return Index
  8414. */
  8415. public function getIndex($indexName)
  8416. {
  8417. $indexName = strtolower($indexName);
  8418. if (!$this->hasIndex($indexName)) {
  8419. throw SchemaException::indexDoesNotExist($indexName, $this->_name);
  8420. }
  8421. return $this->_indexes[$indexName];
  8422. }
  8423. /**
  8424. * @return array
  8425. */
  8426. public function getIndexes()
  8427. {
  8428. return $this->_indexes;
  8429. }
  8430. /**
  8431. * Get Constraints
  8432. *
  8433. * @return array
  8434. */
  8435. public function getForeignKeys()
  8436. {
  8437. return $this->_fkConstraints;
  8438. }
  8439. public function hasOption($name)
  8440. {
  8441. return isset($this->_options[$name]);
  8442. }
  8443. public function getOption($name)
  8444. {
  8445. return $this->_options[$name];
  8446. }
  8447. public function getOptions()
  8448. {
  8449. return $this->_options;
  8450. }
  8451. /**
  8452. * @param Visitor $visitor
  8453. */
  8454. public function visit(Visitor $visitor)
  8455. {
  8456. $visitor->acceptTable($this);
  8457. foreach ($this->getColumns() AS $column) {
  8458. $visitor->acceptColumn($this, $column);
  8459. }
  8460. foreach ($this->getIndexes() AS $index) {
  8461. $visitor->acceptIndex($this, $index);
  8462. }
  8463. foreach ($this->getForeignKeys() AS $constraint) {
  8464. $visitor->acceptForeignKey($this, $constraint);
  8465. }
  8466. }
  8467. /**
  8468. * Clone of a Table triggers a deep clone of all affected assets
  8469. */
  8470. public function __clone()
  8471. {
  8472. foreach ($this->_columns AS $k => $column) {
  8473. $this->_columns[$k] = clone $column;
  8474. }
  8475. foreach ($this->_indexes AS $k => $index) {
  8476. $this->_indexes[$k] = clone $index;
  8477. }
  8478. foreach ($this->_fkConstraints AS $k => $fk) {
  8479. $this->_fkConstraints[$k] = clone $fk;
  8480. $this->_fkConstraints[$k]->setLocalTable($this);
  8481. }
  8482. }
  8483. }<?php
  8484. /*
  8485. * $Id$
  8486. *
  8487. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  8488. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8489. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8490. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  8491. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8492. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  8493. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  8494. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  8495. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  8496. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  8497. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8498. *
  8499. * This software consists of voluntary contributions made by many individuals
  8500. * and is licensed under the LGPL. For more information, see
  8501. * <http://www.doctrine-project.org>.
  8502. */
  8503. namespace Doctrine\DBAL\Schema;
  8504. /**
  8505. * Table Diff
  8506. *
  8507. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  8508. * @link www.doctrine-project.org
  8509. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  8510. * @license http://ez.no/licenses/new_bsd New BSD License
  8511. * @since 2.0
  8512. * @version $Revision$
  8513. * @author Benjamin Eberlei <kontakt@beberlei.de>
  8514. */
  8515. class TableDiff
  8516. {
  8517. /**
  8518. * @var string
  8519. */
  8520. public $name = null;
  8521. /**
  8522. * @var string
  8523. */
  8524. public $newName = false;
  8525. /**
  8526. * All added fields
  8527. *
  8528. * @var array(string=>Column)
  8529. */
  8530. public $addedColumns;
  8531. /**
  8532. * All changed fields
  8533. *
  8534. * @var array(string=>Column)
  8535. */
  8536. public $changedColumns = array();
  8537. /**
  8538. * All removed fields
  8539. *
  8540. * @var array(string=>bool)
  8541. */
  8542. public $removedColumns = array();
  8543. /**
  8544. * Columns that are only renamed from key to column instance name.
  8545. *
  8546. * @var array(string=>Column)
  8547. */
  8548. public $renamedColumns = array();
  8549. /**
  8550. * All added indexes
  8551. *
  8552. * @var array(string=>Index)
  8553. */
  8554. public $addedIndexes = array();
  8555. /**
  8556. * All changed indexes
  8557. *
  8558. * @var array(string=>Index)
  8559. */
  8560. public $changedIndexes = array();
  8561. /**
  8562. * All removed indexes
  8563. *
  8564. * @var array(string=>bool)
  8565. */
  8566. public $removedIndexes = array();
  8567. /**
  8568. * All added foreign key definitions
  8569. *
  8570. * @var array
  8571. */
  8572. public $addedForeignKeys = array();
  8573. /**
  8574. * All changed foreign keys
  8575. *
  8576. * @var array
  8577. */
  8578. public $changedForeignKeys = array();
  8579. /**
  8580. * All removed foreign keys
  8581. *
  8582. * @var array
  8583. */
  8584. public $removedForeignKeys = array();
  8585. /**
  8586. * Constructs an TableDiff object.
  8587. *
  8588. * @param array(string=>Column) $addedColumns
  8589. * @param array(string=>Column) $changedColumns
  8590. * @param array(string=>bool) $removedColumns
  8591. * @param array(string=>Index) $addedIndexes
  8592. * @param array(string=>Index) $changedIndexes
  8593. * @param array(string=>bool) $removedIndexes
  8594. */
  8595. public function __construct($tableName, $addedColumns = array(),
  8596. $changedColumns = array(), $removedColumns = array(), $addedIndexes = array(),
  8597. $changedIndexes = array(), $removedIndexes = array())
  8598. {
  8599. $this->name = $tableName;
  8600. $this->addedColumns = $addedColumns;
  8601. $this->changedColumns = $changedColumns;
  8602. $this->removedColumns = $removedColumns;
  8603. $this->addedIndexes = $addedIndexes;
  8604. $this->changedIndexes = $changedIndexes;
  8605. $this->removedIndexes = $removedIndexes;
  8606. }
  8607. }<?php
  8608. /*
  8609. * $Id$
  8610. *
  8611. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  8612. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8613. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8614. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  8615. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8616. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  8617. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  8618. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  8619. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  8620. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  8621. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8622. *
  8623. * This software consists of voluntary contributions made by many individuals
  8624. * and is licensed under the LGPL. For more information, see
  8625. * <http://www.phpdoctrine.org>.
  8626. */
  8627. namespace Doctrine\DBAL\Schema;
  8628. /**
  8629. * Oracle Schema Manager
  8630. *
  8631. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  8632. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  8633. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  8634. * @author Benjamin Eberlei <kontakt@beberlei.de>
  8635. * @version $Revision$
  8636. * @since 2.0
  8637. */
  8638. class OracleSchemaManager extends AbstractSchemaManager
  8639. {
  8640. protected function _getPortableViewDefinition($view)
  8641. {
  8642. $view = \array_change_key_case($view, CASE_LOWER);
  8643. return new View($view['view_name'], $view['text']);
  8644. }
  8645. protected function _getPortableUserDefinition($user)
  8646. {
  8647. $user = \array_change_key_case($user, CASE_LOWER);
  8648. return array(
  8649. 'user' => $user['username'],
  8650. );
  8651. }
  8652. protected function _getPortableTableDefinition($table)
  8653. {
  8654. $table = \array_change_key_case($table, CASE_LOWER);
  8655. return $table['table_name'];
  8656. }
  8657. /**
  8658. * @license New BSD License
  8659. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  8660. * @param array $tableIndexes
  8661. * @param string $tableName
  8662. * @return array
  8663. */
  8664. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  8665. {
  8666. $indexBuffer = array();
  8667. foreach ( $tableIndexes as $tableIndex ) {
  8668. $tableIndex = \array_change_key_case($tableIndex, CASE_LOWER);
  8669. $keyName = strtolower($tableIndex['name']);
  8670. if ( strtolower($tableIndex['is_primary']) == "p" ) {
  8671. $keyName = 'primary';
  8672. $buffer['primary'] = true;
  8673. $buffer['non_unique'] = false;
  8674. } else {
  8675. $buffer['primary'] = false;
  8676. $buffer['non_unique'] = ( $tableIndex['is_unique'] == 0 ) ? true : false;
  8677. }
  8678. $buffer['key_name'] = $keyName;
  8679. $buffer['column_name'] = $tableIndex['column_name'];
  8680. $indexBuffer[] = $buffer;
  8681. }
  8682. return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
  8683. }
  8684. protected function _getPortableTableColumnDefinition($tableColumn)
  8685. {
  8686. $tableColumn = \array_change_key_case($tableColumn, CASE_LOWER);
  8687. $dbType = strtolower($tableColumn['data_type']);
  8688. if(strpos($dbType, "timestamp(") === 0) {
  8689. if (strpos($dbType, "WITH TIME ZONE")) {
  8690. $dbType = "timestamptz";
  8691. } else {
  8692. $dbType = "timestamp";
  8693. }
  8694. }
  8695. $type = array();
  8696. $length = $unsigned = $fixed = null;
  8697. if ( ! empty($tableColumn['data_length'])) {
  8698. $length = $tableColumn['data_length'];
  8699. }
  8700. if ( ! isset($tableColumn['column_name'])) {
  8701. $tableColumn['column_name'] = '';
  8702. }
  8703. if (stripos($tableColumn['data_default'], 'NULL') !== null) {
  8704. $tableColumn['data_default'] = null;
  8705. }
  8706. $precision = null;
  8707. $scale = null;
  8708. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  8709. switch ($dbType) {
  8710. case 'number':
  8711. // @todo this sucks for the mapping stuff, is this deterministic maybe?
  8712. if($tableColumn['data_scale'] > 0) {
  8713. $precision = $tableColumn['data_precision'];
  8714. $scale = $tableColumn['data_scale'];
  8715. if ($precision == 0 && $scale == 1) {
  8716. $type = 'boolean';
  8717. } else {
  8718. $type = 'decimal';
  8719. }
  8720. }
  8721. $length = null;
  8722. break;
  8723. case 'pls_integer':
  8724. case 'binary_integer':
  8725. $length = null;
  8726. break;
  8727. case 'varchar':
  8728. case 'varchar2':
  8729. case 'nvarchar2':
  8730. $fixed = false;
  8731. break;
  8732. case 'char':
  8733. case 'nchar':
  8734. $fixed = true;
  8735. break;
  8736. case 'date':
  8737. case 'timestamp':
  8738. $length = null;
  8739. break;
  8740. case 'float':
  8741. $precision = $tableColumn['data_precision'];
  8742. $scale = $tableColumn['data_scale'];
  8743. $length = null;
  8744. break;
  8745. case 'clob':
  8746. case 'nclob':
  8747. $length = null;
  8748. break;
  8749. case 'blob':
  8750. case 'raw':
  8751. case 'long raw':
  8752. case 'bfile':
  8753. $length = null;
  8754. break;
  8755. case 'rowid':
  8756. case 'urowid':
  8757. default:
  8758. $length = null;
  8759. }
  8760. $options = array(
  8761. 'notnull' => (bool) ($tableColumn['nullable'] === 'N'),
  8762. 'fixed' => (bool) $fixed,
  8763. 'unsigned' => (bool) $unsigned,
  8764. 'default' => $tableColumn['data_default'],
  8765. 'length' => $length,
  8766. 'precision' => $precision,
  8767. 'scale' => $scale,
  8768. 'platformDetails' => array(),
  8769. );
  8770. return new Column($tableColumn['column_name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  8771. }
  8772. protected function _getPortableTableForeignKeysList($tableForeignKeys)
  8773. {
  8774. $list = array();
  8775. foreach ($tableForeignKeys as $key => $value) {
  8776. $value = \array_change_key_case($value, CASE_LOWER);
  8777. if (!isset($list[$value['constraint_name']])) {
  8778. if ($value['delete_rule'] == "NO ACTION") {
  8779. $value['delete_rule'] = null;
  8780. }
  8781. $list[$value['constraint_name']] = array(
  8782. 'name' => $value['constraint_name'],
  8783. 'local' => array(),
  8784. 'foreign' => array(),
  8785. 'foreignTable' => $value['references_table'],
  8786. 'onDelete' => $value['delete_rule'],
  8787. );
  8788. }
  8789. $list[$value['constraint_name']]['local'][$value['position']] = $value['local_column'];
  8790. $list[$value['constraint_name']]['foreign'][$value['position']] = $value['foreign_column'];
  8791. }
  8792. $result = array();
  8793. foreach($list AS $constraint) {
  8794. $result[] = new ForeignKeyConstraint(
  8795. array_values($constraint['local']), $constraint['foreignTable'],
  8796. array_values($constraint['foreign']), $constraint['name'],
  8797. array('onDelete' => $constraint['onDelete'])
  8798. );
  8799. }
  8800. return $result;
  8801. }
  8802. protected function _getPortableSequenceDefinition($sequence)
  8803. {
  8804. $sequence = \array_change_key_case($sequence, CASE_LOWER);
  8805. return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['min_value']);
  8806. }
  8807. protected function _getPortableFunctionDefinition($function)
  8808. {
  8809. $function = \array_change_key_case($function, CASE_LOWER);
  8810. return $function['name'];
  8811. }
  8812. protected function _getPortableDatabaseDefinition($database)
  8813. {
  8814. $database = \array_change_key_case($database, CASE_LOWER);
  8815. return $database['username'];
  8816. }
  8817. public function createDatabase($database = null)
  8818. {
  8819. if (is_null($database)) {
  8820. $database = $this->_conn->getDatabase();
  8821. }
  8822. $params = $this->_conn->getParams();
  8823. $username = $database;
  8824. $password = $params['password'];
  8825. $query = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
  8826. $result = $this->_conn->executeUpdate($query);
  8827. $query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
  8828. $result = $this->_conn->executeUpdate($query);
  8829. return true;
  8830. }
  8831. public function dropAutoincrement($table)
  8832. {
  8833. $sql = $this->_platform->getDropAutoincrementSql($table);
  8834. foreach ($sql as $query) {
  8835. $this->_conn->executeUpdate($query);
  8836. }
  8837. return true;
  8838. }
  8839. public function dropTable($name)
  8840. {
  8841. $this->dropAutoincrement($name);
  8842. return parent::dropTable($name);
  8843. }
  8844. }<?php
  8845. /*
  8846. * $Id$
  8847. *
  8848. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  8849. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8850. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8851. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  8852. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8853. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  8854. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  8855. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  8856. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  8857. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  8858. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8859. *
  8860. * This software consists of voluntary contributions made by many individuals
  8861. * and is licensed under the LGPL. For more information, see
  8862. * <http://www.doctrine-project.org>.
  8863. */
  8864. namespace Doctrine\DBAL\Schema;
  8865. use Doctrine\DBAL\Schema\Visitor\Visitor;
  8866. /**
  8867. * Sequence Structure
  8868. *
  8869. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  8870. * @link www.doctrine-project.org
  8871. * @since 2.0
  8872. * @version $Revision$
  8873. * @author Benjamin Eberlei <kontakt@beberlei.de>
  8874. */
  8875. class Sequence extends AbstractAsset
  8876. {
  8877. /**
  8878. * @var int
  8879. */
  8880. protected $_allocationSize = 1;
  8881. /**
  8882. * @var int
  8883. */
  8884. protected $_initialValue = 1;
  8885. /**
  8886. *
  8887. * @param string $name
  8888. * @param int $allocationSize
  8889. * @param int $initialValue
  8890. */
  8891. public function __construct($name, $allocationSize=1, $initialValue=1)
  8892. {
  8893. $this->_setName($name);
  8894. $this->_allocationSize = (is_numeric($allocationSize))?$allocationSize:1;
  8895. $this->_initialValue = (is_numeric($initialValue))?$initialValue:1;
  8896. }
  8897. public function getAllocationSize()
  8898. {
  8899. return $this->_allocationSize;
  8900. }
  8901. public function getInitialValue()
  8902. {
  8903. return $this->_initialValue;
  8904. }
  8905. /**
  8906. * @param Visitor $visitor
  8907. */
  8908. public function visit(Visitor $visitor)
  8909. {
  8910. $visitor->acceptSequence($this);
  8911. }
  8912. }
  8913. <?php
  8914. /*
  8915. * $Id$
  8916. *
  8917. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  8918. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8919. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8920. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  8921. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8922. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  8923. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  8924. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  8925. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  8926. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  8927. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8928. *
  8929. * This software consists of voluntary contributions made by many individuals
  8930. * and is licensed under the LGPL. For more information, see
  8931. * <http://www.doctrine-project.org>.
  8932. */
  8933. namespace Doctrine\DBAL\Schema;
  8934. /**
  8935. * Represent the change of a column
  8936. *
  8937. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  8938. * @link www.doctrine-project.org
  8939. * @since 2.0
  8940. * @version $Revision$
  8941. * @author Benjamin Eberlei <kontakt@beberlei.de>
  8942. */
  8943. class ColumnDiff
  8944. {
  8945. public $oldColumnName;
  8946. /**
  8947. * @var Column
  8948. */
  8949. public $column;
  8950. /**
  8951. * @var array
  8952. */
  8953. public $changedProperties = array();
  8954. public function __construct($oldColumnName, Column $column, array $changedProperties = array())
  8955. {
  8956. $this->oldColumnName = $oldColumnName;
  8957. $this->column = $column;
  8958. $this->changedProperties = $changedProperties;
  8959. }
  8960. public function hasChanged($propertyName)
  8961. {
  8962. return in_array($propertyName, $this->changedProperties);
  8963. }
  8964. }<?php
  8965. namespace Doctrine\DBAL\Schema;
  8966. class SchemaException extends \Doctrine\DBAL\DBALException
  8967. {
  8968. const TABLE_DOESNT_EXIST = 10;
  8969. const TABLE_ALREADY_EXISTS = 20;
  8970. const COLUMN_DOESNT_EXIST = 30;
  8971. const COLUMN_ALREADY_EXISTS = 40;
  8972. const INDEX_DOESNT_EXIST = 50;
  8973. const INDEX_ALREADY_EXISTS = 60;
  8974. const SEQUENCE_DOENST_EXIST = 70;
  8975. const SEQUENCE_ALREADY_EXISTS = 80;
  8976. const INDEX_INVALID_NAME = 90;
  8977. const FOREIGNKEY_DOESNT_EXIST = 100;
  8978. /**
  8979. * @param string $tableName
  8980. * @return SchemaException
  8981. */
  8982. static public function tableDoesNotExist($tableName)
  8983. {
  8984. return new self("There is no table with name '".$tableName."' in the schema.", self::TABLE_DOESNT_EXIST);
  8985. }
  8986. /**
  8987. * @param string $indexName
  8988. * @return SchemaException
  8989. */
  8990. static public function indexNameInvalid($indexName)
  8991. {
  8992. return new self("Invalid index-name $indexName given, has to be [a-zA-Z0-9_]", self::INDEX_INVALID_NAME);
  8993. }
  8994. /**
  8995. * @param string $indexName
  8996. * @return SchemaException
  8997. */
  8998. static public function indexDoesNotExist($indexName, $table)
  8999. {
  9000. return new self("Index '$indexName' does not exist on table '$table'.", self::INDEX_DOESNT_EXIST);
  9001. }
  9002. /**
  9003. * @param string $indexName
  9004. * @return SchemaException
  9005. */
  9006. static public function indexAlreadyExists($indexName, $table)
  9007. {
  9008. return new self("An index with name '$indexName' was already defined on table '$table'.", self::INDEX_ALREADY_EXISTS);
  9009. }
  9010. /**
  9011. * @param string $columnName
  9012. * @return SchemaException
  9013. */
  9014. static public function columnDoesNotExist($columnName, $table)
  9015. {
  9016. return new self("There is no column with name '$columnName' on table '$table'.", self::COLUMN_DOESNT_EXIST);
  9017. }
  9018. /**
  9019. *
  9020. * @param string $tableName
  9021. * @return SchemaException
  9022. */
  9023. static public function tableAlreadyExists($tableName)
  9024. {
  9025. return new self("The table with name '".$tableName."' already exists.", self::TABLE_ALREADY_EXISTS);
  9026. }
  9027. /**
  9028. *
  9029. * @param string $tableName
  9030. * @param string $columnName
  9031. * @return SchemaException
  9032. */
  9033. static public function columnAlreadyExists($tableName, $columnName)
  9034. {
  9035. return new self(
  9036. "The column '".$columnName."' on table '".$tableName."' already exists.", self::COLUMN_ALREADY_EXISTS
  9037. );
  9038. }
  9039. /**
  9040. * @param string $sequenceName
  9041. * @return SchemaException
  9042. */
  9043. static public function sequenceAlreadyExists($sequenceName)
  9044. {
  9045. return new self("The sequence '".$sequenceName."' already exists.", self::SEQUENCE_ALREADY_EXISTS);
  9046. }
  9047. /**
  9048. * @param string $sequenceName
  9049. * @return SchemaException
  9050. */
  9051. static public function sequenceDoesNotExist($sequenceName)
  9052. {
  9053. return new self("There exists no sequence with the name '".$sequenceName."'.", self::SEQUENCE_DOENST_EXIST);
  9054. }
  9055. /**
  9056. * @param string $fkName
  9057. * @return SchemaException
  9058. */
  9059. static public function foreignKeyDoesNotExist($fkName, $table)
  9060. {
  9061. return new self("There exists no foreign key with the name '$fkName' on table '$table'.", self::FOREIGNKEY_DOESNT_EXIST);
  9062. }
  9063. static public function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
  9064. {
  9065. return new self(
  9066. "The performed schema operation on ".$localTable->getName()." requires a named foreign key, ".
  9067. "but the given foreign key from (".implode(", ", $foreignKey->getColumns()).") onto foreign table ".
  9068. "'".$foreignKey->getForeignTableName()."' (".implode(", ", $foreignKey->getForeignColumns()).") is currently ".
  9069. "unnamed."
  9070. );
  9071. }
  9072. static public function alterTableChangeNotSupported($changeName) {
  9073. return new self ("Alter table change not supported, given '$changeName'");
  9074. }
  9075. }<?php
  9076. /*
  9077. * $Id$
  9078. *
  9079. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  9080. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9081. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  9082. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9083. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  9084. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9085. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  9086. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  9087. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  9088. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  9089. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9090. *
  9091. * This software consists of voluntary contributions made by many individuals
  9092. * and is licensed under the LGPL. For more information, see
  9093. * <http://www.doctrine-project.org>.
  9094. */
  9095. namespace Doctrine\DBAL;
  9096. /**
  9097. * Contains all DBAL LockModes
  9098. *
  9099. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  9100. * @link www.doctrine-project.com
  9101. * @since 1.0
  9102. * @version $Revision$
  9103. * @author Benjamin Eberlei <kontakt@beberlei.de>
  9104. * @author Roman Borschel <roman@code-factory.org>
  9105. */
  9106. class LockMode
  9107. {
  9108. const NONE = 0;
  9109. const OPTIMISTIC = 1;
  9110. const PESSIMISTIC_READ = 2;
  9111. const PESSIMISTIC_WRITE = 4;
  9112. final private function __construct() { }
  9113. }<?php
  9114. /*
  9115. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  9116. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9117. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  9118. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9119. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  9120. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9121. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  9122. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  9123. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  9124. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  9125. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9126. *
  9127. * This software consists of voluntary contributions made by many individuals
  9128. * and is licensed under the LGPL. For more information, see
  9129. * <http://www.doctrine-project.org>.
  9130. */
  9131. namespace Doctrine\DBAL;
  9132. /**
  9133. * Driver interface.
  9134. * Interface that all DBAL drivers must implement.
  9135. *
  9136. * @since 2.0
  9137. */
  9138. interface Driver
  9139. {
  9140. /**
  9141. * Attempts to create a connection with the database.
  9142. *
  9143. * @param array $params All connection parameters passed by the user.
  9144. * @param string $username The username to use when connecting.
  9145. * @param string $password The password to use when connecting.
  9146. * @param array $driverOptions The driver options to use when connecting.
  9147. * @return Doctrine\DBAL\Driver\Connection The database connection.
  9148. */
  9149. public function connect(array $params, $username = null, $password = null, array $driverOptions = array());
  9150. /**
  9151. * Gets the DatabasePlatform instance that provides all the metadata about
  9152. * the platform this driver connects to.
  9153. *
  9154. * @return Doctrine\DBAL\Platforms\AbstractPlatform The database platform.
  9155. */
  9156. public function getDatabasePlatform();
  9157. /**
  9158. * Gets the SchemaManager that can be used to inspect and change the underlying
  9159. * database schema of the platform this driver connects to.
  9160. *
  9161. * @param Doctrine\DBAL\Connection $conn
  9162. * @return Doctrine\DBAL\SchemaManager
  9163. */
  9164. public function getSchemaManager(Connection $conn);
  9165. /**
  9166. * Gets the name of the driver.
  9167. *
  9168. * @return string The name of the driver.
  9169. */
  9170. public function getName();
  9171. /**
  9172. * Get the name of the database connected to for this driver.
  9173. *
  9174. * @param Doctrine\DBAL\Connection $conn
  9175. * @return string $database
  9176. */
  9177. public function getDatabase(Connection $conn);
  9178. }<?php
  9179. /*
  9180. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  9181. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9182. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  9183. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9184. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  9185. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9186. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  9187. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  9188. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  9189. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  9190. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9191. *
  9192. * This software consists of voluntary contributions made by many individuals
  9193. * and is licensed under the LGPL. For more information, see
  9194. * <http://www.doctrine-project.org>.
  9195. */
  9196. namespace Doctrine\DBAL\Platforms;
  9197. use Doctrine\DBAL\Schema\TableDiff,
  9198. Doctrine\DBAL\Schema\Table;
  9199. /**
  9200. * PostgreSqlPlatform.
  9201. *
  9202. * @since 2.0
  9203. * @author Roman Borschel <roman@code-factory.org>
  9204. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  9205. * @author Benjamin Eberlei <kontakt@beberlei.de>
  9206. * @todo Rename: PostgreSQLPlatform
  9207. */
  9208. class PostgreSqlPlatform extends AbstractPlatform
  9209. {
  9210. /**
  9211. * Returns part of a string.
  9212. *
  9213. * Note: Not SQL92, but common functionality.
  9214. *
  9215. * @param string $value the target $value the string or the string column.
  9216. * @param int $from extract from this characeter.
  9217. * @param int $len extract this amount of characters.
  9218. * @return string sql that extracts part of a string.
  9219. * @override
  9220. */
  9221. public function getSubstringExpression($value, $from, $len = null)
  9222. {
  9223. if ($len === null) {
  9224. return 'SUBSTR(' . $value . ', ' . $from . ')';
  9225. } else {
  9226. return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
  9227. }
  9228. }
  9229. /**
  9230. * Returns the SQL string to return the current system date and time.
  9231. *
  9232. * @return string
  9233. */
  9234. public function getNowExpression()
  9235. {
  9236. return 'LOCALTIMESTAMP(0)';
  9237. }
  9238. /**
  9239. * regexp
  9240. *
  9241. * @return string the regular expression operator
  9242. * @override
  9243. */
  9244. public function getRegexpExpression()
  9245. {
  9246. return 'SIMILAR TO';
  9247. }
  9248. /**
  9249. * returns the position of the first occurrence of substring $substr in string $str
  9250. *
  9251. * @param string $substr literal string to find
  9252. * @param string $str literal string
  9253. * @param int $pos position to start at, beginning of string by default
  9254. * @return integer
  9255. */
  9256. public function getLocateExpression($str, $substr, $startPos = false)
  9257. {
  9258. if ($startPos !== false) {
  9259. $str = $this->getSubstringExpression($str, $startPos);
  9260. return 'CASE WHEN (POSITION('.$substr.' IN '.$str.') = 0) THEN 0 ELSE (POSITION('.$substr.' IN '.$str.') + '.($startPos-1).') END';
  9261. } else {
  9262. return 'POSITION('.$substr.' IN '.$str.')';
  9263. }
  9264. }
  9265. /**
  9266. * parses a literal boolean value and returns
  9267. * proper sql equivalent
  9268. *
  9269. * @param string $value boolean value to be parsed
  9270. * @return string parsed boolean value
  9271. */
  9272. /*public function parseBoolean($value)
  9273. {
  9274. return $value;
  9275. }*/
  9276. /**
  9277. * Whether the platform supports sequences.
  9278. * Postgres has native support for sequences.
  9279. *
  9280. * @return boolean
  9281. */
  9282. public function supportsSequences()
  9283. {
  9284. return true;
  9285. }
  9286. /**
  9287. * Whether the platform supports database schemas.
  9288. *
  9289. * @return boolean
  9290. */
  9291. public function supportsSchemas()
  9292. {
  9293. return true;
  9294. }
  9295. /**
  9296. * Whether the platform supports identity columns.
  9297. * Postgres supports these through the SERIAL keyword.
  9298. *
  9299. * @return boolean
  9300. */
  9301. public function supportsIdentityColumns()
  9302. {
  9303. return true;
  9304. }
  9305. /**
  9306. * Whether the platform prefers sequences for ID generation.
  9307. *
  9308. * @return boolean
  9309. */
  9310. public function prefersSequences()
  9311. {
  9312. return true;
  9313. }
  9314. public function getListDatabasesSQL()
  9315. {
  9316. return 'SELECT datname FROM pg_database';
  9317. }
  9318. public function getListSequencesSQL($database)
  9319. {
  9320. return "SELECT
  9321. relname
  9322. FROM
  9323. pg_class
  9324. WHERE relkind = 'S' AND relnamespace IN
  9325. (SELECT oid FROM pg_namespace
  9326. WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
  9327. }
  9328. public function getListTablesSQL()
  9329. {
  9330. return "SELECT
  9331. c.relname AS table_name
  9332. FROM pg_class c, pg_user u
  9333. WHERE c.relowner = u.usesysid
  9334. AND c.relkind = 'r'
  9335. AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname)
  9336. AND c.relname !~ '^(pg_|sql_)'
  9337. UNION
  9338. SELECT c.relname AS table_name
  9339. FROM pg_class c
  9340. WHERE c.relkind = 'r'
  9341. AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname)
  9342. AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner)
  9343. AND c.relname !~ '^pg_'";
  9344. }
  9345. public function getListViewsSQL($database)
  9346. {
  9347. return 'SELECT viewname, definition FROM pg_views';
  9348. }
  9349. public function getListTableForeignKeysSQL($table, $database = null)
  9350. {
  9351. return "SELECT r.conname, pg_catalog.pg_get_constraintdef(r.oid, true) as condef
  9352. FROM pg_catalog.pg_constraint r
  9353. WHERE r.conrelid =
  9354. (
  9355. SELECT c.oid
  9356. FROM pg_catalog.pg_class c
  9357. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  9358. WHERE c.relname = '" . $table . "' AND pg_catalog.pg_table_is_visible(c.oid)
  9359. )
  9360. AND r.contype = 'f'";
  9361. }
  9362. public function getCreateViewSQL($name, $sql)
  9363. {
  9364. return 'CREATE VIEW ' . $name . ' AS ' . $sql;
  9365. }
  9366. public function getDropViewSQL($name)
  9367. {
  9368. return 'DROP VIEW '. $name;
  9369. }
  9370. public function getListTableConstraintsSQL($table)
  9371. {
  9372. return "SELECT
  9373. relname
  9374. FROM
  9375. pg_class
  9376. WHERE oid IN (
  9377. SELECT indexrelid
  9378. FROM pg_index, pg_class
  9379. WHERE pg_class.relname = '$table'
  9380. AND pg_class.oid = pg_index.indrelid
  9381. AND (indisunique = 't' OR indisprimary = 't')
  9382. )";
  9383. }
  9384. /**
  9385. * @license New BSD License
  9386. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  9387. * @param string $table
  9388. * @return string
  9389. */
  9390. public function getListTableIndexesSQL($table)
  9391. {
  9392. return "SELECT relname, pg_index.indisunique, pg_index.indisprimary,
  9393. pg_index.indkey, pg_index.indrelid
  9394. FROM pg_class, pg_index
  9395. WHERE oid IN (
  9396. SELECT indexrelid
  9397. FROM pg_index, pg_class
  9398. WHERE pg_class.relname='$table' AND pg_class.oid=pg_index.indrelid
  9399. ) AND pg_index.indexrelid = oid";
  9400. }
  9401. public function getListTableColumnsSQL($table)
  9402. {
  9403. return "SELECT
  9404. a.attnum,
  9405. a.attname AS field,
  9406. t.typname AS type,
  9407. format_type(a.atttypid, a.atttypmod) AS complete_type,
  9408. (SELECT t1.typname FROM pg_catalog.pg_type t1 WHERE t1.oid = t.typbasetype) AS domain_type,
  9409. (SELECT format_type(t2.typbasetype, t2.typtypmod) FROM pg_catalog.pg_type t2
  9410. WHERE t2.typtype = 'd' AND t2.typname = format_type(a.atttypid, a.atttypmod)) AS domain_complete_type,
  9411. a.attnotnull AS isnotnull,
  9412. (SELECT 't'
  9413. FROM pg_index
  9414. WHERE c.oid = pg_index.indrelid
  9415. AND pg_index.indkey[0] = a.attnum
  9416. AND pg_index.indisprimary = 't'
  9417. ) AS pri,
  9418. (SELECT pg_attrdef.adsrc
  9419. FROM pg_attrdef
  9420. WHERE c.oid = pg_attrdef.adrelid
  9421. AND pg_attrdef.adnum=a.attnum
  9422. ) AS default
  9423. FROM pg_attribute a, pg_class c, pg_type t
  9424. WHERE c.relname = '$table'
  9425. AND a.attnum > 0
  9426. AND a.attrelid = c.oid
  9427. AND a.atttypid = t.oid
  9428. ORDER BY a.attnum";
  9429. }
  9430. /**
  9431. * create a new database
  9432. *
  9433. * @param string $name name of the database that should be created
  9434. * @throws PDOException
  9435. * @return void
  9436. * @override
  9437. */
  9438. public function getCreateDatabaseSQL($name)
  9439. {
  9440. return 'CREATE DATABASE ' . $name;
  9441. }
  9442. /**
  9443. * drop an existing database
  9444. *
  9445. * @param string $name name of the database that should be dropped
  9446. * @throws PDOException
  9447. * @access public
  9448. */
  9449. public function getDropDatabaseSQL($name)
  9450. {
  9451. return 'DROP DATABASE ' . $name;
  9452. }
  9453. /**
  9454. * Return the FOREIGN KEY query section dealing with non-standard options
  9455. * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  9456. *
  9457. * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey foreign key definition
  9458. * @return string
  9459. * @override
  9460. */
  9461. public function getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
  9462. {
  9463. $query = '';
  9464. if ($foreignKey->hasOption('match')) {
  9465. $query .= ' MATCH ' . $foreignKey->getOption('match');
  9466. }
  9467. $query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey);
  9468. if ($foreignKey->hasOption('deferrable') && $foreignKey->getOption('deferrable') !== false) {
  9469. $query .= ' DEFERRABLE';
  9470. } else {
  9471. $query .= ' NOT DEFERRABLE';
  9472. }
  9473. if ($foreignKey->hasOption('feferred') && $foreignKey->getOption('feferred') !== false) {
  9474. $query .= ' INITIALLY DEFERRED';
  9475. } else {
  9476. $query .= ' INITIALLY IMMEDIATE';
  9477. }
  9478. return $query;
  9479. }
  9480. /**
  9481. * generates the sql for altering an existing table on postgresql
  9482. *
  9483. * @param string $name name of the table that is intended to be changed.
  9484. * @param array $changes associative array that contains the details of each type *
  9485. * @param boolean $check indicates whether the function should just check if the DBMS driver
  9486. * can perform the requested table alterations if the value is true or
  9487. * actually perform them otherwise.
  9488. * @see Doctrine_Export::alterTable()
  9489. * @return array
  9490. * @override
  9491. */
  9492. public function getAlterTableSQL(TableDiff $diff)
  9493. {
  9494. $sql = array();
  9495. foreach ($diff->addedColumns as $column) {
  9496. $query = 'ADD ' . $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  9497. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  9498. }
  9499. foreach ($diff->removedColumns as $column) {
  9500. $query = 'DROP ' . $column->getName();
  9501. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  9502. }
  9503. foreach ($diff->changedColumns AS $columnDiff) {
  9504. $oldColumnName = $columnDiff->oldColumnName;
  9505. $column = $columnDiff->column;
  9506. if ($columnDiff->hasChanged('type')) {
  9507. $type = $column->getType();
  9508. // here was a server version check before, but DBAL API does not support this anymore.
  9509. $query = 'ALTER ' . $oldColumnName . ' TYPE ' . $type->getSqlDeclaration($column->toArray(), $this);
  9510. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  9511. }
  9512. if ($columnDiff->hasChanged('default')) {
  9513. $query = 'ALTER ' . $oldColumnName . ' SET ' . $this->getDefaultValueDeclarationSQL($column->toArray());
  9514. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  9515. }
  9516. if ($columnDiff->hasChanged('notnull')) {
  9517. $query = 'ALTER ' . $oldColumnName . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL';
  9518. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  9519. }
  9520. if ($columnDiff->hasChanged('autoincrement')) {
  9521. if ($column->getAutoincrement()) {
  9522. // add autoincrement
  9523. $seqName = $diff->name . '_' . $oldColumnName . '_seq';
  9524. $sql[] = "CREATE SEQUENCE " . $seqName;
  9525. $sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->name . "))";
  9526. $query = "ALTER " . $oldColumnName . " SET DEFAULT nextval('" . $seqName . "')";
  9527. $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
  9528. } else {
  9529. // Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have
  9530. $query = "ALTER " . $oldColumnName . " " . "DROP DEFAULT";
  9531. $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
  9532. }
  9533. }
  9534. }
  9535. foreach ($diff->renamedColumns as $oldColumnName => $column) {
  9536. $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName . ' TO ' . $column->getName();
  9537. }
  9538. if ($diff->newName !== false) {
  9539. $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
  9540. }
  9541. $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
  9542. return $sql;
  9543. }
  9544. /**
  9545. * Gets the SQL to create a sequence on this platform.
  9546. *
  9547. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  9548. * @return string
  9549. */
  9550. public function getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
  9551. {
  9552. return 'CREATE SEQUENCE ' . $sequence->getName() .
  9553. ' INCREMENT BY ' . $sequence->getAllocationSize() .
  9554. ' MINVALUE ' . $sequence->getInitialValue() .
  9555. ' START ' . $sequence->getInitialValue();
  9556. }
  9557. /**
  9558. * Drop existing sequence
  9559. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  9560. * @return string
  9561. */
  9562. public function getDropSequenceSQL($sequence)
  9563. {
  9564. if ($sequence instanceof \Doctrine\DBAL\Schema\Sequence) {
  9565. $sequence = $sequence->getName();
  9566. }
  9567. return 'DROP SEQUENCE ' . $sequence;
  9568. }
  9569. /**
  9570. * @param ForeignKeyConstraint|string $foreignKey
  9571. * @param Table|string $table
  9572. * @return string
  9573. */
  9574. public function getDropForeignKeySQL($foreignKey, $table)
  9575. {
  9576. return $this->getDropConstraintSQL($foreignKey, $table);
  9577. }
  9578. /**
  9579. * Gets the SQL used to create a table.
  9580. *
  9581. * @param unknown_type $tableName
  9582. * @param array $columns
  9583. * @param array $options
  9584. * @return unknown
  9585. */
  9586. protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
  9587. {
  9588. $queryFields = $this->getColumnDeclarationListSQL($columns);
  9589. if (isset($options['primary']) && ! empty($options['primary'])) {
  9590. $keyColumns = array_unique(array_values($options['primary']));
  9591. $queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
  9592. }
  9593. $query = 'CREATE TABLE ' . $tableName . ' (' . $queryFields . ')';
  9594. $sql[] = $query;
  9595. if (isset($options['indexes']) && ! empty($options['indexes'])) {
  9596. foreach ($options['indexes'] AS $index) {
  9597. $sql[] = $this->getCreateIndexSQL($index, $tableName);
  9598. }
  9599. }
  9600. if (isset($options['foreignKeys'])) {
  9601. foreach ((array) $options['foreignKeys'] as $definition) {
  9602. $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
  9603. }
  9604. }
  9605. return $sql;
  9606. }
  9607. /**
  9608. * Postgres wants boolean values converted to the strings 'true'/'false'.
  9609. *
  9610. * @param array $item
  9611. * @override
  9612. */
  9613. public function convertBooleans($item)
  9614. {
  9615. if (is_array($item)) {
  9616. foreach ($item as $key => $value) {
  9617. if (is_bool($value) || is_numeric($item)) {
  9618. $item[$key] = ($value) ? 'true' : 'false';
  9619. }
  9620. }
  9621. } else {
  9622. if (is_bool($item) || is_numeric($item)) {
  9623. $item = ($item) ? 'true' : 'false';
  9624. }
  9625. }
  9626. return $item;
  9627. }
  9628. public function getSequenceNextValSQL($sequenceName)
  9629. {
  9630. return "SELECT NEXTVAL('" . $sequenceName . "')";
  9631. }
  9632. public function getSetTransactionIsolationSQL($level)
  9633. {
  9634. return 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL '
  9635. . $this->_getTransactionIsolationLevelSQL($level);
  9636. }
  9637. /**
  9638. * @override
  9639. */
  9640. public function getBooleanTypeDeclarationSQL(array $field)
  9641. {
  9642. return 'BOOLEAN';
  9643. }
  9644. /**
  9645. * @override
  9646. */
  9647. public function getIntegerTypeDeclarationSQL(array $field)
  9648. {
  9649. if ( ! empty($field['autoincrement'])) {
  9650. return 'SERIAL';
  9651. }
  9652. return 'INT';
  9653. }
  9654. /**
  9655. * @override
  9656. */
  9657. public function getBigIntTypeDeclarationSQL(array $field)
  9658. {
  9659. if ( ! empty($field['autoincrement'])) {
  9660. return 'BIGSERIAL';
  9661. }
  9662. return 'BIGINT';
  9663. }
  9664. /**
  9665. * @override
  9666. */
  9667. public function getSmallIntTypeDeclarationSQL(array $field)
  9668. {
  9669. return 'SMALLINT';
  9670. }
  9671. /**
  9672. * @override
  9673. */
  9674. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  9675. {
  9676. return 'TIMESTAMP(0) WITHOUT TIME ZONE';
  9677. }
  9678. /**
  9679. * @override
  9680. */
  9681. public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
  9682. {
  9683. return 'TIMESTAMP(0) WITH TIME ZONE';
  9684. }
  9685. /**
  9686. * @override
  9687. */
  9688. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  9689. {
  9690. return 'DATE';
  9691. }
  9692. /**
  9693. * @override
  9694. */
  9695. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  9696. {
  9697. return 'TIME(0) WITHOUT TIME ZONE';
  9698. }
  9699. /**
  9700. * @override
  9701. */
  9702. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  9703. {
  9704. return '';
  9705. }
  9706. /**
  9707. * Gets the SQL snippet used to declare a VARCHAR column on the MySql platform.
  9708. *
  9709. * @params array $field
  9710. * @override
  9711. */
  9712. public function getVarcharTypeDeclarationSQL(array $field)
  9713. {
  9714. if ( ! isset($field['length'])) {
  9715. if (array_key_exists('default', $field)) {
  9716. $field['length'] = $this->getVarcharMaxLength();
  9717. } else {
  9718. $field['length'] = false;
  9719. }
  9720. }
  9721. $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
  9722. $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
  9723. return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
  9724. : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
  9725. }
  9726. /** @override */
  9727. public function getClobTypeDeclarationSQL(array $field)
  9728. {
  9729. return 'TEXT';
  9730. }
  9731. /**
  9732. * Get the platform name for this instance
  9733. *
  9734. * @return string
  9735. */
  9736. public function getName()
  9737. {
  9738. return 'postgresql';
  9739. }
  9740. /**
  9741. * Gets the character casing of a column in an SQL result set.
  9742. *
  9743. * PostgreSQL returns all column names in SQL result sets in lowercase.
  9744. *
  9745. * @param string $column The column name for which to get the correct character casing.
  9746. * @return string The column name in the character casing used in SQL result sets.
  9747. */
  9748. public function getSQLResultCasing($column)
  9749. {
  9750. return strtolower($column);
  9751. }
  9752. public function getDateTimeTzFormatString()
  9753. {
  9754. return 'Y-m-d H:i:sO';
  9755. }
  9756. /**
  9757. * Get the insert sql for an empty insert statement
  9758. *
  9759. * @param string $tableName
  9760. * @param string $identifierColumnName
  9761. * @return string $sql
  9762. */
  9763. public function getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierColumnName)
  9764. {
  9765. return 'INSERT INTO ' . $quotedTableName . ' (' . $quotedIdentifierColumnName . ') VALUES (DEFAULT)';
  9766. }
  9767. /**
  9768. * @inheritdoc
  9769. */
  9770. public function getTruncateTableSQL($tableName, $cascade = false)
  9771. {
  9772. return 'TRUNCATE '.$tableName.' '.($cascade)?'CASCADE':'';
  9773. }
  9774. public function getReadLockSQL()
  9775. {
  9776. return 'FOR SHARE';
  9777. }
  9778. protected function initializeDoctrineTypeMappings()
  9779. {
  9780. $this->doctrineTypeMapping = array(
  9781. 'smallint' => 'smallint',
  9782. 'int2' => 'smallint',
  9783. 'serial' => 'integer',
  9784. 'serial4' => 'integer',
  9785. 'int' => 'integer',
  9786. 'int4' => 'integer',
  9787. 'integer' => 'integer',
  9788. 'bigserial' => 'bigint',
  9789. 'serial8' => 'bigint',
  9790. 'bigint' => 'bigint',
  9791. 'int8' => 'bigint',
  9792. 'bool' => 'boolean',
  9793. 'boolean' => 'boolean',
  9794. 'text' => 'text',
  9795. 'varchar' => 'string',
  9796. 'interval' => 'string',
  9797. '_varchar' => 'string',
  9798. 'char' => 'string',
  9799. 'bpchar' => 'string',
  9800. 'date' => 'date',
  9801. 'datetime' => 'datetime',
  9802. 'timestamp' => 'datetime',
  9803. 'timestamptz' => 'datetimetz',
  9804. 'time' => 'time',
  9805. 'timetz' => 'time',
  9806. 'float' => 'float',
  9807. 'float4' => 'float',
  9808. 'float8' => 'float',
  9809. 'double' => 'float',
  9810. 'double precision' => 'float',
  9811. 'real' => 'float',
  9812. 'decimal' => 'decimal',
  9813. 'money' => 'decimal',
  9814. 'numeric' => 'decimal',
  9815. 'year' => 'date',
  9816. );
  9817. }
  9818. }
  9819. <?php
  9820. /*
  9821. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  9822. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9823. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  9824. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9825. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  9826. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9827. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  9828. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  9829. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  9830. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  9831. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9832. *
  9833. * This software consists of voluntary contributions made by many individuals
  9834. * and is licensed under the LGPL. For more information, see
  9835. * <http://www.doctrine-project.org>.
  9836. */
  9837. namespace Doctrine\DBAL\Platforms;
  9838. use Doctrine\DBAL\Schema\TableDiff;
  9839. use Doctrine\DBAL\DBALException;
  9840. use Doctrine\DBAL\Schema\Index, Doctrine\DBAL\Schema\Table;
  9841. /**
  9842. * The MsSqlPlatform provides the behavior, features and SQL dialect of the
  9843. * MySQL database platform.
  9844. *
  9845. * @since 2.0
  9846. * @author Roman Borschel <roman@code-factory.org>
  9847. * @author Jonathan H. Wage <jonwage@gmail.com>
  9848. * @author Benjamin Eberlei <kontakt@beberlei.de>
  9849. * @todo Rename: MsSQLPlatform
  9850. */
  9851. class MsSqlPlatform extends AbstractPlatform
  9852. {
  9853. /**
  9854. * Whether the platform prefers identity columns for ID generation.
  9855. * MsSql prefers "autoincrement" identity columns since sequences can only
  9856. * be emulated with a table.
  9857. *
  9858. * @return boolean
  9859. * @override
  9860. */
  9861. public function prefersIdentityColumns()
  9862. {
  9863. return true;
  9864. }
  9865. /**
  9866. * Whether the platform supports identity columns.
  9867. * MsSql supports this through AUTO_INCREMENT columns.
  9868. *
  9869. * @return boolean
  9870. * @override
  9871. */
  9872. public function supportsIdentityColumns()
  9873. {
  9874. return true;
  9875. }
  9876. /**
  9877. * Whether the platform supports releasing savepoints.
  9878. *
  9879. * @return boolean
  9880. */
  9881. public function supportsReleaseSavepoints()
  9882. {
  9883. return false;
  9884. }
  9885. /**
  9886. * create a new database
  9887. *
  9888. * @param string $name name of the database that should be created
  9889. * @return string
  9890. * @override
  9891. */
  9892. public function getCreateDatabaseSQL($name)
  9893. {
  9894. return 'CREATE DATABASE ' . $name;
  9895. }
  9896. /**
  9897. * drop an existing database
  9898. *
  9899. * @param string $name name of the database that should be dropped
  9900. * @return string
  9901. * @override
  9902. */
  9903. public function getDropDatabaseSQL($name)
  9904. {
  9905. // @todo do we really need to force drop?
  9906. return 'ALTER DATABASE [' . $name . ']
  9907. SET SINGLE_USER
  9908. WITH ROLLBACK IMMEDIATE;
  9909. DROP DATABASE ' . $name . ';';
  9910. }
  9911. /**
  9912. * @override
  9913. */
  9914. public function quoteIdentifier($str)
  9915. {
  9916. return '[' . $str . ']';
  9917. }
  9918. /**
  9919. * @override
  9920. */
  9921. public function getDropForeignKeySQL($foreignKey, $table)
  9922. {
  9923. if ($foreignKey instanceof \Doctrine\DBAL\Schema\ForeignKeyConstraint) {
  9924. $foreignKey = $foreignKey->getName();
  9925. }
  9926. if ($table instanceof \Doctrine\DBAL\Schema\Table) {
  9927. $table = $table->getName();
  9928. }
  9929. return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey;
  9930. }
  9931. /**
  9932. * @override
  9933. */
  9934. public function getDropIndexSQL($index, $table=null)
  9935. {
  9936. if ($index instanceof \Doctrine\DBAL\Schema\Index) {
  9937. $index_ = $index;
  9938. $index = $index->getName();
  9939. } else if (!is_string($index)) {
  9940. throw new \InvalidArgumentException('AbstractPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
  9941. }
  9942. if (!isset($table)) {
  9943. return 'DROP INDEX ' . $index;
  9944. } else {
  9945. if ($table instanceof \Doctrine\DBAL\Schema\Table) {
  9946. $table = $table->getName();
  9947. }
  9948. return "IF EXISTS (SELECT * FROM sysobjects WHERE name = '$index')
  9949. ALTER TABLE " . $this->quoteIdentifier($table) . " DROP CONSTRAINT " . $this->quoteIdentifier($index) . "
  9950. ELSE
  9951. DROP INDEX " . $this->quoteIdentifier($index) . " ON " . $this->quoteIdentifier($table);
  9952. }
  9953. }
  9954. /**
  9955. * @override
  9956. */
  9957. public function getCreateTableSQL(Table $table, $createFlags=self::CREATE_INDEXES)
  9958. {
  9959. $sql = parent::getCreateTableSQL($table, $createFlags);
  9960. $primary = array();
  9961. foreach ($table->getIndexes() AS $index) {
  9962. /* @var $index Index */
  9963. if ($index->isPrimary()) {
  9964. $primary = $index->getColumns();
  9965. }
  9966. }
  9967. if (count($primary) === 1) {
  9968. foreach ($table->getForeignKeys() AS $definition) {
  9969. $columns = $definition->getLocalColumns();
  9970. if (count($columns) === 1 && in_array($columns[0], $primary)) {
  9971. $sql[0] = str_replace(' IDENTITY', '', $sql[0]);
  9972. }
  9973. }
  9974. }
  9975. return $sql;
  9976. }
  9977. /**
  9978. * @override
  9979. */
  9980. protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
  9981. {
  9982. $columnListSql = $this->getColumnDeclarationListSQL($columns);
  9983. if (isset($options['uniqueConstraints']) && !empty($options['uniqueConstraints'])) {
  9984. foreach ($options['uniqueConstraints'] as $name => $definition) {
  9985. $columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($name, $definition);
  9986. }
  9987. }
  9988. if (isset($options['primary']) && !empty($options['primary'])) {
  9989. $columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')';
  9990. }
  9991. $query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql;
  9992. $check = $this->getCheckDeclarationSQL($columns);
  9993. if (!empty($check)) {
  9994. $query .= ', ' . $check;
  9995. }
  9996. $query .= ')';
  9997. $sql[] = $query;
  9998. if (isset($options['indexes']) && !empty($options['indexes'])) {
  9999. foreach ($options['indexes'] AS $index) {
  10000. $sql[] = $this->getCreateIndexSQL($index, $tableName);
  10001. }
  10002. }
  10003. if (isset($options['foreignKeys'])) {
  10004. foreach ((array) $options['foreignKeys'] AS $definition) {
  10005. $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
  10006. }
  10007. }
  10008. return $sql;
  10009. }
  10010. /**
  10011. * @override
  10012. */
  10013. public function getAlterTableSQL(TableDiff $diff)
  10014. {
  10015. $queryParts = array();
  10016. if ($diff->newName !== false) {
  10017. $queryParts[] = 'RENAME TO ' . $diff->newName;
  10018. }
  10019. foreach ($diff->addedColumns AS $fieldName => $column) {
  10020. $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  10021. }
  10022. foreach ($diff->removedColumns AS $column) {
  10023. $queryParts[] = 'DROP COLUMN ' . $column->getName();
  10024. }
  10025. foreach ($diff->changedColumns AS $columnDiff) {
  10026. /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
  10027. $column = $columnDiff->column;
  10028. $queryParts[] = 'CHANGE ' . ($columnDiff->oldColumnName) . ' '
  10029. . $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  10030. }
  10031. foreach ($diff->renamedColumns AS $oldColumnName => $column) {
  10032. $queryParts[] = 'CHANGE ' . $oldColumnName . ' '
  10033. . $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  10034. }
  10035. $sql = array();
  10036. foreach ($queryParts as $query) {
  10037. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
  10038. }
  10039. $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
  10040. return $sql;
  10041. }
  10042. /**
  10043. * @override
  10044. */
  10045. public function getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierColumnName)
  10046. {
  10047. return 'INSERT INTO ' . $quotedTableName . ' DEFAULT VALUES';
  10048. }
  10049. /**
  10050. * @override
  10051. */
  10052. public function getShowDatabasesSQL()
  10053. {
  10054. return 'SHOW DATABASES';
  10055. }
  10056. /**
  10057. * @override
  10058. */
  10059. public function getListTablesSQL()
  10060. {
  10061. return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
  10062. }
  10063. /**
  10064. * @override
  10065. */
  10066. public function getListTableColumnsSQL($table)
  10067. {
  10068. return 'exec sp_columns @table_name = ' . $table;
  10069. }
  10070. /**
  10071. * @override
  10072. */
  10073. public function getListTableForeignKeysSQL($table, $database = null)
  10074. {
  10075. return "SELECT f.name AS ForeignKey,
  10076. SCHEMA_NAME (f.SCHEMA_ID) AS SchemaName,
  10077. OBJECT_NAME (f.parent_object_id) AS TableName,
  10078. COL_NAME (fc.parent_object_id,fc.parent_column_id) AS ColumnName,
  10079. SCHEMA_NAME (o.SCHEMA_ID) ReferenceSchemaName,
  10080. OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
  10081. COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName,
  10082. f.delete_referential_action_desc,
  10083. f.update_referential_action_desc
  10084. FROM sys.foreign_keys AS f
  10085. INNER JOIN sys.foreign_key_columns AS fc
  10086. INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
  10087. ON f.OBJECT_ID = fc.constraint_object_id
  10088. WHERE OBJECT_NAME (f.parent_object_id) = '" . $table . "'";
  10089. }
  10090. /**
  10091. * @override
  10092. */
  10093. public function getListTableIndexesSQL($table)
  10094. {
  10095. return "exec sp_helpindex '" . $table . "'";
  10096. }
  10097. /**
  10098. * @override
  10099. */
  10100. public function getCreateViewSQL($name, $sql)
  10101. {
  10102. return 'CREATE VIEW ' . $name . ' AS ' . $sql;
  10103. }
  10104. /**
  10105. * @override
  10106. */
  10107. public function getListViewsSQL($database)
  10108. {
  10109. return "SELECT name FROM sysobjects WHERE type = 'V' ORDER BY name";
  10110. }
  10111. /**
  10112. * @override
  10113. */
  10114. public function getDropViewSQL($name)
  10115. {
  10116. return 'DROP VIEW ' . $name;
  10117. }
  10118. /**
  10119. * Returns the regular expression operator.
  10120. *
  10121. * @return string
  10122. * @override
  10123. */
  10124. public function getRegexpExpression()
  10125. {
  10126. return 'RLIKE';
  10127. }
  10128. /**
  10129. * Returns global unique identifier
  10130. *
  10131. * @return string to get global unique identifier
  10132. * @override
  10133. */
  10134. public function getGuidExpression()
  10135. {
  10136. return 'UUID()';
  10137. }
  10138. /**
  10139. * @override
  10140. */
  10141. public function getLocateExpression($str, $substr, $startPos = false)
  10142. {
  10143. if ($startPos == false) {
  10144. return 'CHARINDEX(' . $substr . ', ' . $str . ')';
  10145. } else {
  10146. return 'CHARINDEX(' . $substr . ', ' . $str . ', ' . $startPos . ')';
  10147. }
  10148. }
  10149. /**
  10150. * @override
  10151. */
  10152. public function getModExpression($expression1, $expression2)
  10153. {
  10154. return $expression1 . ' % ' . $expression2;
  10155. }
  10156. /**
  10157. * @override
  10158. */
  10159. public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
  10160. {
  10161. // @todo
  10162. $trimFn = '';
  10163. $trimChar = ($char != false) ? (', ' . $char) : '';
  10164. if ($pos == self::TRIM_LEADING) {
  10165. $trimFn = 'LTRIM';
  10166. } else if ($pos == self::TRIM_TRAILING) {
  10167. $trimFn = 'RTRIM';
  10168. } else {
  10169. return 'LTRIM(RTRIM(' . $str . '))';
  10170. }
  10171. return $trimFn . '(' . $str . ')';
  10172. }
  10173. /**
  10174. * @override
  10175. */
  10176. public function getConcatExpression()
  10177. {
  10178. $args = func_get_args();
  10179. return '(' . implode(' + ', $args) . ')';
  10180. }
  10181. public function getListDatabasesSQL()
  10182. {
  10183. return 'SELECT * FROM SYS.DATABASES';
  10184. }
  10185. /**
  10186. * @override
  10187. */
  10188. public function getSubstringExpression($value, $from, $len = null)
  10189. {
  10190. if (!is_null($len)) {
  10191. return 'SUBSTRING(' . $value . ', ' . $from . ', ' . $len . ')';
  10192. }
  10193. return 'SUBSTRING(' . $value . ', ' . $from . ', LEN(' . $value . ') - ' . $from . ' + 1)';
  10194. }
  10195. /**
  10196. * @override
  10197. */
  10198. public function getLengthExpression($column)
  10199. {
  10200. return 'LEN(' . $column . ')';
  10201. }
  10202. /**
  10203. * @override
  10204. */
  10205. public function getSetTransactionIsolationSQL($level)
  10206. {
  10207. return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
  10208. }
  10209. /**
  10210. * @override
  10211. */
  10212. public function getIntegerTypeDeclarationSQL(array $field)
  10213. {
  10214. return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
  10215. }
  10216. /**
  10217. * @override
  10218. */
  10219. public function getBigIntTypeDeclarationSQL(array $field)
  10220. {
  10221. return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
  10222. }
  10223. /**
  10224. * @override
  10225. */
  10226. public function getSmallIntTypeDeclarationSQL(array $field)
  10227. {
  10228. return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
  10229. }
  10230. /** @override */
  10231. public function getVarcharTypeDeclarationSQL(array $field)
  10232. {
  10233. if (!isset($field['length'])) {
  10234. if (array_key_exists('default', $field)) {
  10235. $field['length'] = $this->getVarcharMaxLength();
  10236. } else {
  10237. $field['length'] = false;
  10238. }
  10239. }
  10240. $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
  10241. $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
  10242. return $fixed ? ($length ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NTEXT');
  10243. }
  10244. /** @override */
  10245. public function getClobTypeDeclarationSQL(array $field)
  10246. {
  10247. return 'TEXT';
  10248. }
  10249. /**
  10250. * @override
  10251. */
  10252. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  10253. {
  10254. $autoinc = '';
  10255. if (!empty($columnDef['autoincrement'])) {
  10256. $autoinc = ' IDENTITY';
  10257. }
  10258. $unsigned = (isset($columnDef['unsigned']) && $columnDef['unsigned']) ? ' UNSIGNED' : '';
  10259. return $unsigned . $autoinc;
  10260. }
  10261. /**
  10262. * @override
  10263. */
  10264. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  10265. {
  10266. // 6 - microseconds precision length
  10267. return 'DATETIME2(6)';
  10268. }
  10269. /**
  10270. * @override
  10271. */
  10272. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  10273. {
  10274. return 'DATE';
  10275. }
  10276. /**
  10277. * @override
  10278. */
  10279. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  10280. {
  10281. return 'TIME(0)';
  10282. }
  10283. /**
  10284. * @override
  10285. */
  10286. public function getBooleanTypeDeclarationSQL(array $field)
  10287. {
  10288. return 'BIT';
  10289. }
  10290. /**
  10291. * Adds an adapter-specific LIMIT clause to the SELECT statement.
  10292. *
  10293. * @param string $query
  10294. * @param mixed $limit
  10295. * @param mixed $offset
  10296. * @link http://lists.bestpractical.com/pipermail/rt-devel/2005-June/007339.html
  10297. * @return string
  10298. */
  10299. public function modifyLimitQuery($query, $limit, $offset = null)
  10300. {
  10301. if ($limit > 0) {
  10302. $count = intval($limit);
  10303. $offset = intval($offset);
  10304. if ($offset < 0) {
  10305. throw new Doctrine_Connection_Exception("LIMIT argument offset=$offset is not valid");
  10306. }
  10307. if ($offset == 0) {
  10308. $query = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . $count . ' ', $query);
  10309. } else {
  10310. $orderby = stristr($query, 'ORDER BY');
  10311. if (!$orderby) {
  10312. $over = 'ORDER BY (SELECT 0)';
  10313. } else {
  10314. $over = preg_replace('/\"[^,]*\".\"([^,]*)\"/i', '"inner_tbl"."$1"', $orderby);
  10315. }
  10316. // Remove ORDER BY clause from $query
  10317. $query = preg_replace('/\s+ORDER BY(.*)/', '', $query);
  10318. // Add ORDER BY clause as an argument for ROW_NUMBER()
  10319. $query = "SELECT ROW_NUMBER() OVER ($over) AS \"doctrine_rownum\", * FROM ($query) AS inner_tbl";
  10320. $start = $offset + 1;
  10321. $end = $offset + $count;
  10322. $query = "WITH outer_tbl AS ($query) SELECT * FROM outer_tbl WHERE \"doctrine_rownum\" BETWEEN $start AND $end";
  10323. }
  10324. }
  10325. return $query;
  10326. }
  10327. /**
  10328. * @override
  10329. */
  10330. public function convertBooleans($item)
  10331. {
  10332. if (is_array($item)) {
  10333. foreach ($item as $key => $value) {
  10334. if (is_bool($value) || is_numeric($item)) {
  10335. $item[$key] = ($value) ? 'TRUE' : 'FALSE';
  10336. }
  10337. }
  10338. } else {
  10339. if (is_bool($item) || is_numeric($item)) {
  10340. $item = ($item) ? 'TRUE' : 'FALSE';
  10341. }
  10342. }
  10343. return $item;
  10344. }
  10345. /**
  10346. * @override
  10347. */
  10348. public function getCreateTemporaryTableSnippetSQL()
  10349. {
  10350. return "CREATE TABLE";
  10351. }
  10352. /**
  10353. * @override
  10354. */
  10355. public function getTemporaryTableName($tableName)
  10356. {
  10357. return '#' . $tableName;
  10358. }
  10359. /**
  10360. * @override
  10361. */
  10362. public function getDateTimeFormatString()
  10363. {
  10364. return 'Y-m-d H:i:s.u';
  10365. }
  10366. /**
  10367. * @override
  10368. */
  10369. public function getDateTimeTzFormatString()
  10370. {
  10371. return $this->getDateTimeFormatString();
  10372. }
  10373. /**
  10374. * Get the platform name for this instance
  10375. *
  10376. * @return string
  10377. */
  10378. public function getName()
  10379. {
  10380. return 'mssql';
  10381. }
  10382. protected function initializeDoctrineTypeMappings()
  10383. {
  10384. $this->doctrineTypeMapping = array(
  10385. 'bigint' => 'bigint',
  10386. 'numeric' => 'decimal',
  10387. 'bit' => 'boolean',
  10388. 'smallint' => 'smallint',
  10389. 'decimal' => 'decimal',
  10390. 'smallmoney' => 'integer',
  10391. 'int' => 'integer',
  10392. 'tinyint' => 'smallint',
  10393. 'money' => 'integer',
  10394. 'float' => 'float',
  10395. 'real' => 'float',
  10396. 'double' => 'float',
  10397. 'double precision' => 'float',
  10398. 'date' => 'date',
  10399. 'datetimeoffset' => 'datetimetz',
  10400. 'datetime2' => 'datetime',
  10401. 'smalldatetime' => 'datetime',
  10402. 'datetime' => 'datetime',
  10403. 'time' => 'time',
  10404. 'char' => 'string',
  10405. 'varchar' => 'string',
  10406. 'text' => 'text',
  10407. 'nchar' => 'string',
  10408. 'nvarchar' => 'string',
  10409. 'ntext' => 'text',
  10410. 'binary' => 'text',
  10411. 'varbinary' => 'text',
  10412. 'image' => 'text',
  10413. );
  10414. }
  10415. /**
  10416. * Generate SQL to create a new savepoint
  10417. *
  10418. * @param string $savepoint
  10419. * @return string
  10420. */
  10421. public function createSavePoint($savepoint)
  10422. {
  10423. return 'SAVE TRANSACTION ' . $savepoint;
  10424. }
  10425. /**
  10426. * Generate SQL to release a savepoint
  10427. *
  10428. * @param string $savepoint
  10429. * @return string
  10430. */
  10431. public function releaseSavePoint($savepoint)
  10432. {
  10433. return '';
  10434. }
  10435. /**
  10436. * Generate SQL to rollback a savepoint
  10437. *
  10438. * @param string $savepoint
  10439. * @return string
  10440. */
  10441. public function rollbackSavePoint($savepoint)
  10442. {
  10443. return 'ROLLBACK TRANSACTION ' . $savepoint;
  10444. }
  10445. }
  10446. <?php
  10447. /*
  10448. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  10449. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10450. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  10451. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  10452. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10453. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  10454. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10455. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  10456. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10457. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  10458. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10459. *
  10460. * This software consists of voluntary contributions made by many individuals
  10461. * and is licensed under the LGPL. For more information, see
  10462. * <http://www.doctrine-project.org>.
  10463. */
  10464. namespace Doctrine\DBAL\Platforms;
  10465. use Doctrine\DBAL\Schema\TableDiff;
  10466. /**
  10467. * OraclePlatform.
  10468. *
  10469. * @since 2.0
  10470. * @author Roman Borschel <roman@code-factory.org>
  10471. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  10472. * @author Benjamin Eberlei <kontakt@beberlei.de>
  10473. */
  10474. class OraclePlatform extends AbstractPlatform
  10475. {
  10476. /**
  10477. * return string to call a function to get a substring inside an SQL statement
  10478. *
  10479. * Note: Not SQL92, but common functionality.
  10480. *
  10481. * @param string $value an sql string literal or column name/alias
  10482. * @param integer $position where to start the substring portion
  10483. * @param integer $length the substring portion length
  10484. * @return string SQL substring function with given parameters
  10485. * @override
  10486. */
  10487. public function getSubstringExpression($value, $position, $length = null)
  10488. {
  10489. if ($length !== null) {
  10490. return "SUBSTR($value, $position, $length)";
  10491. }
  10492. return "SUBSTR($value, $position)";
  10493. }
  10494. /**
  10495. * Return string to call a variable with the current timestamp inside an SQL statement
  10496. * There are three special variables for current date and time:
  10497. * - CURRENT_TIMESTAMP (date and time, TIMESTAMP type)
  10498. * - CURRENT_DATE (date, DATE type)
  10499. * - CURRENT_TIME (time, TIME type)
  10500. *
  10501. * @return string to call a variable with the current timestamp
  10502. * @override
  10503. */
  10504. public function getNowExpression($type = 'timestamp')
  10505. {
  10506. switch ($type) {
  10507. case 'date':
  10508. case 'time':
  10509. case 'timestamp':
  10510. default:
  10511. return 'TO_CHAR(CURRENT_TIMESTAMP, \'YYYY-MM-DD HH24:MI:SS\')';
  10512. }
  10513. }
  10514. /**
  10515. * returns the position of the first occurrence of substring $substr in string $str
  10516. *
  10517. * @param string $substr literal string to find
  10518. * @param string $str literal string
  10519. * @param int $pos position to start at, beginning of string by default
  10520. * @return integer
  10521. */
  10522. public function getLocateExpression($str, $substr, $startPos = false)
  10523. {
  10524. if ($startPos == false) {
  10525. return 'INSTR('.$str.', '.$substr.')';
  10526. } else {
  10527. return 'INSTR('.$str.', '.$substr.', '.$startPos.')';
  10528. }
  10529. }
  10530. /**
  10531. * Returns global unique identifier
  10532. *
  10533. * @return string to get global unique identifier
  10534. * @override
  10535. */
  10536. public function getGuidExpression()
  10537. {
  10538. return 'SYS_GUID()';
  10539. }
  10540. /**
  10541. * Gets the SQL used to create a sequence that starts with a given value
  10542. * and increments by the given allocation size.
  10543. *
  10544. * Need to specifiy minvalue, since start with is hidden in the system and MINVALUE <= START WITH.
  10545. * Therefore we can use MINVALUE to be able to get a hint what START WITH was for later introspection
  10546. * in {@see listSequences()}
  10547. *
  10548. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  10549. * @return string
  10550. */
  10551. public function getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
  10552. {
  10553. return 'CREATE SEQUENCE ' . $sequence->getName() .
  10554. ' START WITH ' . $sequence->getInitialValue() .
  10555. ' MINVALUE ' . $sequence->getInitialValue() .
  10556. ' INCREMENT BY ' . $sequence->getAllocationSize();
  10557. }
  10558. /**
  10559. * {@inheritdoc}
  10560. *
  10561. * @param string $sequenceName
  10562. * @override
  10563. */
  10564. public function getSequenceNextValSQL($sequenceName)
  10565. {
  10566. return 'SELECT ' . $sequenceName . '.nextval FROM DUAL';
  10567. }
  10568. /**
  10569. * {@inheritdoc}
  10570. *
  10571. * @param integer $level
  10572. * @override
  10573. */
  10574. public function getSetTransactionIsolationSQL($level)
  10575. {
  10576. return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
  10577. }
  10578. protected function _getTransactionIsolationLevelSQL($level)
  10579. {
  10580. switch ($level) {
  10581. case \Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED:
  10582. return 'READ UNCOMMITTED';
  10583. case \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED:
  10584. return 'READ COMMITTED';
  10585. case \Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ:
  10586. case \Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE:
  10587. return 'SERIALIZABLE';
  10588. default:
  10589. return parent::_getTransactionIsolationLevelSQL($level);
  10590. }
  10591. }
  10592. /**
  10593. * @override
  10594. */
  10595. public function getBooleanTypeDeclarationSQL(array $field)
  10596. {
  10597. return 'NUMBER(1)';
  10598. }
  10599. /**
  10600. * @override
  10601. */
  10602. public function getIntegerTypeDeclarationSQL(array $field)
  10603. {
  10604. return 'NUMBER(10)';
  10605. }
  10606. /**
  10607. * @override
  10608. */
  10609. public function getBigIntTypeDeclarationSQL(array $field)
  10610. {
  10611. return 'NUMBER(20)';
  10612. }
  10613. /**
  10614. * @override
  10615. */
  10616. public function getSmallIntTypeDeclarationSQL(array $field)
  10617. {
  10618. return 'NUMBER(5)';
  10619. }
  10620. /**
  10621. * @override
  10622. */
  10623. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  10624. {
  10625. return 'TIMESTAMP(0)';
  10626. }
  10627. /**
  10628. * @override
  10629. */
  10630. public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
  10631. {
  10632. return 'TIMESTAMP(0) WITH TIME ZONE';
  10633. }
  10634. /**
  10635. * @override
  10636. */
  10637. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  10638. {
  10639. return 'DATE';
  10640. }
  10641. /**
  10642. * @override
  10643. */
  10644. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  10645. {
  10646. return 'DATE';
  10647. }
  10648. /**
  10649. * @override
  10650. */
  10651. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  10652. {
  10653. return '';
  10654. }
  10655. /**
  10656. * Gets the SQL snippet used to declare a VARCHAR column on the Oracle platform.
  10657. *
  10658. * @params array $field
  10659. * @override
  10660. */
  10661. public function getVarcharTypeDeclarationSQL(array $field)
  10662. {
  10663. if ( ! isset($field['length'])) {
  10664. if (array_key_exists('default', $field)) {
  10665. $field['length'] = $this->getVarcharMaxLength();
  10666. } else {
  10667. $field['length'] = false;
  10668. }
  10669. }
  10670. $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
  10671. $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
  10672. return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)')
  10673. : ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
  10674. }
  10675. /** @override */
  10676. public function getClobTypeDeclarationSQL(array $field)
  10677. {
  10678. return 'CLOB';
  10679. }
  10680. public function getListDatabasesSQL()
  10681. {
  10682. return 'SELECT username FROM all_users';
  10683. }
  10684. public function getListSequencesSQL($database)
  10685. {
  10686. return "SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ".
  10687. "WHERE SEQUENCE_OWNER = '".strtoupper($database)."'";
  10688. }
  10689. /**
  10690. *
  10691. * @param string $table
  10692. * @param array $columns
  10693. * @param array $options
  10694. * @return array
  10695. */
  10696. protected function _getCreateTableSQL($table, array $columns, array $options = array())
  10697. {
  10698. $indexes = isset($options['indexes']) ? $options['indexes'] : array();
  10699. $options['indexes'] = array();
  10700. $sql = parent::_getCreateTableSQL($table, $columns, $options);
  10701. foreach ($columns as $name => $column) {
  10702. if (isset($column['sequence'])) {
  10703. $sql[] = $this->getCreateSequenceSQL($column['sequence'], 1);
  10704. }
  10705. if (isset($column['autoincrement']) && $column['autoincrement'] ||
  10706. (isset($column['autoinc']) && $column['autoinc'])) {
  10707. $sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table));
  10708. }
  10709. }
  10710. if (isset($indexes) && ! empty($indexes)) {
  10711. foreach ($indexes as $indexName => $index) {
  10712. $sql[] = $this->getCreateIndexSQL($index, $table);
  10713. }
  10714. }
  10715. return $sql;
  10716. }
  10717. /**
  10718. * @license New BSD License
  10719. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html
  10720. * @param string $table
  10721. * @return string
  10722. */
  10723. public function getListTableIndexesSQL($table)
  10724. {
  10725. $table = strtoupper($table);
  10726. return "SELECT uind.index_name AS name, " .
  10727. " uind.index_type AS type, " .
  10728. " decode( uind.uniqueness, 'NONUNIQUE', 0, 'UNIQUE', 1 ) AS is_unique, " .
  10729. " uind_col.column_name AS column_name, " .
  10730. " uind_col.column_position AS column_pos, " .
  10731. " (SELECT ucon.constraint_type FROM user_constraints ucon WHERE ucon.constraint_name = uind.index_name) AS is_primary ".
  10732. "FROM user_indexes uind, user_ind_columns uind_col " .
  10733. "WHERE uind.index_name = uind_col.index_name AND uind_col.table_name = '$table' ORDER BY uind_col.column_position ASC";
  10734. }
  10735. public function getListTablesSQL()
  10736. {
  10737. return 'SELECT * FROM sys.user_tables';
  10738. }
  10739. public function getListViewsSQL($database)
  10740. {
  10741. return 'SELECT view_name, text FROM sys.user_views';
  10742. }
  10743. public function getCreateViewSQL($name, $sql)
  10744. {
  10745. return 'CREATE VIEW ' . $name . ' AS ' . $sql;
  10746. }
  10747. public function getDropViewSQL($name)
  10748. {
  10749. return 'DROP VIEW '. $name;
  10750. }
  10751. public function getCreateAutoincrementSql($name, $table, $start = 1)
  10752. {
  10753. $table = strtoupper($table);
  10754. $sql = array();
  10755. $indexName = $table . '_AI_PK';
  10756. $definition = array(
  10757. 'primary' => true,
  10758. 'columns' => array($name => true),
  10759. );
  10760. $idx = new \Doctrine\DBAL\Schema\Index($indexName, array($name), true, true);
  10761. $sql[] = 'DECLARE
  10762. constraints_Count NUMBER;
  10763. BEGIN
  10764. SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = \''.$table.'\' AND CONSTRAINT_TYPE = \'P\';
  10765. IF constraints_Count = 0 OR constraints_Count = \'\' THEN
  10766. EXECUTE IMMEDIATE \''.$this->getCreateConstraintSQL($idx, $table).'\';
  10767. END IF;
  10768. END;';
  10769. $sequenceName = $table . '_SEQ';
  10770. $sequence = new \Doctrine\DBAL\Schema\Sequence($sequenceName, $start);
  10771. $sql[] = $this->getCreateSequenceSQL($sequence);
  10772. $triggerName = $table . '_AI_PK';
  10773. $sql[] = 'CREATE TRIGGER ' . $triggerName . '
  10774. BEFORE INSERT
  10775. ON ' . $table . '
  10776. FOR EACH ROW
  10777. DECLARE
  10778. last_Sequence NUMBER;
  10779. last_InsertID NUMBER;
  10780. BEGIN
  10781. SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL;
  10782. IF (:NEW.' . $name . ' IS NULL OR :NEW.'.$name.' = 0) THEN
  10783. SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL;
  10784. ELSE
  10785. SELECT NVL(Last_Number, 0) INTO last_Sequence
  10786. FROM User_Sequences
  10787. WHERE Sequence_Name = \'' . $sequenceName . '\';
  10788. SELECT :NEW.' . $name . ' INTO last_InsertID FROM DUAL;
  10789. WHILE (last_InsertID > last_Sequence) LOOP
  10790. SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
  10791. END LOOP;
  10792. END IF;
  10793. END;';
  10794. return $sql;
  10795. }
  10796. public function getDropAutoincrementSql($table)
  10797. {
  10798. $table = strtoupper($table);
  10799. $trigger = $table . '_AI_PK';
  10800. if ($trigger) {
  10801. $sql[] = 'DROP TRIGGER ' . $trigger;
  10802. $sql[] = $this->getDropSequenceSQL($table.'_SEQ');
  10803. $indexName = $table . '_AI_PK';
  10804. $sql[] = $this->getDropConstraintSQL($indexName, $table);
  10805. }
  10806. return $sql;
  10807. }
  10808. public function getListTableForeignKeysSQL($table)
  10809. {
  10810. $table = strtoupper($table);
  10811. return "SELECT alc.constraint_name,
  10812. alc.DELETE_RULE,
  10813. alc.search_condition,
  10814. cols.column_name \"local_column\",
  10815. cols.position,
  10816. r_alc.table_name \"references_table\",
  10817. r_cols.column_name \"foreign_column\"
  10818. FROM all_cons_columns cols
  10819. LEFT JOIN all_constraints alc
  10820. ON alc.constraint_name = cols.constraint_name
  10821. AND alc.owner = cols.owner
  10822. LEFT JOIN all_constraints r_alc
  10823. ON alc.r_constraint_name = r_alc.constraint_name
  10824. AND alc.r_owner = r_alc.owner
  10825. LEFT JOIN all_cons_columns r_cols
  10826. ON r_alc.constraint_name = r_cols.constraint_name
  10827. AND r_alc.owner = r_cols.owner
  10828. AND cols.position = r_cols.position
  10829. WHERE alc.constraint_name = cols.constraint_name
  10830. AND alc.constraint_type = 'R'
  10831. AND alc.table_name = '".$table."'";
  10832. }
  10833. public function getListTableConstraintsSQL($table)
  10834. {
  10835. $table = strtoupper($table);
  10836. return 'SELECT * FROM user_constraints WHERE table_name = \'' . $table . '\'';
  10837. }
  10838. public function getListTableColumnsSQL($table)
  10839. {
  10840. $table = strtoupper($table);
  10841. return "SELECT * FROM all_tab_columns WHERE table_name = '" . $table . "' ORDER BY column_name";
  10842. }
  10843. /**
  10844. *
  10845. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  10846. * @return string
  10847. */
  10848. public function getDropSequenceSQL($sequence)
  10849. {
  10850. if ($sequence instanceof \Doctrine\DBAL\Schema\Sequence) {
  10851. $sequence = $sequence->getName();
  10852. }
  10853. return 'DROP SEQUENCE ' . $sequence;
  10854. }
  10855. /**
  10856. * @param ForeignKeyConstraint|string $foreignKey
  10857. * @param Table|string $table
  10858. * @return string
  10859. */
  10860. public function getDropForeignKeySQL($foreignKey, $table)
  10861. {
  10862. if ($foreignKey instanceof \Doctrine\DBAL\Schema\ForeignKeyConstraint) {
  10863. $foreignKey = $foreignKey->getName();
  10864. }
  10865. if ($table instanceof \Doctrine\DBAL\Schema\Table) {
  10866. $table = $table->getName();
  10867. }
  10868. return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey;
  10869. }
  10870. public function getDropDatabaseSQL($database)
  10871. {
  10872. return 'DROP USER ' . $database . ' CASCADE';
  10873. }
  10874. /**
  10875. * Gets the sql statements for altering an existing table.
  10876. *
  10877. * The method returns an array of sql statements, since some platforms need several statements.
  10878. *
  10879. * @param string $diff->name name of the table that is intended to be changed.
  10880. * @param array $changes associative array that contains the details of each type *
  10881. * @param boolean $check indicates whether the function should just check if the DBMS driver
  10882. * can perform the requested table alterations if the value is true or
  10883. * actually perform them otherwise.
  10884. * @return array
  10885. */
  10886. public function getAlterTableSQL(TableDiff $diff)
  10887. {
  10888. $sql = array();
  10889. $fields = array();
  10890. foreach ($diff->addedColumns AS $column) {
  10891. $fields[] = $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  10892. }
  10893. if (count($fields)) {
  10894. $sql[] = 'ALTER TABLE ' . $diff->name . ' ADD (' . implode(', ', $fields) . ')';
  10895. }
  10896. $fields = array();
  10897. foreach ($diff->changedColumns AS $columnDiff) {
  10898. $column = $columnDiff->column;
  10899. $fields[] = $column->getName(). ' ' . $this->getColumnDeclarationSQL('', $column->toArray());
  10900. }
  10901. if (count($fields)) {
  10902. $sql[] = 'ALTER TABLE ' . $diff->name . ' MODIFY (' . implode(', ', $fields) . ')';
  10903. }
  10904. foreach ($diff->renamedColumns AS $oldColumnName => $column) {
  10905. $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getName();
  10906. }
  10907. $fields = array();
  10908. foreach ($diff->removedColumns AS $column) {
  10909. $fields[] = $column->getName();
  10910. }
  10911. if (count($fields)) {
  10912. $sql[] = 'ALTER TABLE ' . $diff->name . ' DROP COLUMN ' . implode(', ', $fields);
  10913. }
  10914. if ($diff->newName !== false) {
  10915. $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
  10916. }
  10917. $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
  10918. return $sql;
  10919. }
  10920. /**
  10921. * Whether the platform prefers sequences for ID generation.
  10922. *
  10923. * @return boolean
  10924. */
  10925. public function prefersSequences()
  10926. {
  10927. return true;
  10928. }
  10929. /**
  10930. * Get the platform name for this instance
  10931. *
  10932. * @return string
  10933. */
  10934. public function getName()
  10935. {
  10936. return 'oracle';
  10937. }
  10938. /**
  10939. * Adds an driver-specific LIMIT clause to the query
  10940. *
  10941. * @param string $query query to modify
  10942. * @param integer $limit limit the number of rows
  10943. * @param integer $offset start reading from given offset
  10944. * @return string the modified query
  10945. */
  10946. public function modifyLimitQuery($query, $limit, $offset = null)
  10947. {
  10948. $limit = (int) $limit;
  10949. $offset = (int) $offset;
  10950. if (preg_match('/^\s*SELECT/i', $query)) {
  10951. if ( ! preg_match('/\sFROM\s/i', $query)) {
  10952. $query .= " FROM dual";
  10953. }
  10954. if ($limit > 0) {
  10955. $max = $offset + $limit;
  10956. $column = '*';
  10957. if ($offset > 0) {
  10958. $min = $offset + 1;
  10959. $query = 'SELECT b.'.$column.' FROM ('.
  10960. 'SELECT a.*, ROWNUM AS doctrine_rownum FROM ('
  10961. . $query . ') a '.
  10962. ') b '.
  10963. 'WHERE doctrine_rownum BETWEEN ' . $min . ' AND ' . $max;
  10964. } else {
  10965. $query = 'SELECT a.'.$column.' FROM (' . $query .') a WHERE ROWNUM <= ' . $max;
  10966. }
  10967. }
  10968. }
  10969. return $query;
  10970. }
  10971. /**
  10972. * Gets the character casing of a column in an SQL result set of this platform.
  10973. *
  10974. * Oracle returns all column names in SQL result sets in uppercase.
  10975. *
  10976. * @param string $column The column name for which to get the correct character casing.
  10977. * @return string The column name in the character casing used in SQL result sets.
  10978. */
  10979. public function getSQLResultCasing($column)
  10980. {
  10981. return strtoupper($column);
  10982. }
  10983. public function getCreateTemporaryTableSnippetSQL()
  10984. {
  10985. return "CREATE GLOBAL TEMPORARY TABLE";
  10986. }
  10987. public function getDateTimeTzFormatString()
  10988. {
  10989. return 'Y-m-d H:i:sP';
  10990. }
  10991. public function getDateFormatString()
  10992. {
  10993. return 'Y-m-d 00:00:00';
  10994. }
  10995. public function getTimeFormatString()
  10996. {
  10997. return '1900-01-01 H:i:s';
  10998. }
  10999. public function fixSchemaElementName($schemaElementName)
  11000. {
  11001. if (strlen($schemaElementName) > 30) {
  11002. // Trim it
  11003. return substr($schemaElementName, 0, 30);
  11004. }
  11005. return $schemaElementName;
  11006. }
  11007. /**
  11008. * Maximum length of any given databse identifier, like tables or column names.
  11009. *
  11010. * @return int
  11011. */
  11012. public function getMaxIdentifierLength()
  11013. {
  11014. return 30;
  11015. }
  11016. /**
  11017. * Whether the platform supports sequences.
  11018. *
  11019. * @return boolean
  11020. */
  11021. public function supportsSequences()
  11022. {
  11023. return true;
  11024. }
  11025. public function supportsForeignKeyOnUpdate()
  11026. {
  11027. return false;
  11028. }
  11029. /**
  11030. * Whether the platform supports releasing savepoints.
  11031. *
  11032. * @return boolean
  11033. */
  11034. public function supportsReleaseSavepoints()
  11035. {
  11036. return false;
  11037. }
  11038. /**
  11039. * @inheritdoc
  11040. */
  11041. public function getTruncateTableSQL($tableName, $cascade = false)
  11042. {
  11043. return 'TRUNCATE TABLE '.$tableName;
  11044. }
  11045. /**
  11046. * This is for test reasons, many vendors have special requirements for dummy statements.
  11047. *
  11048. * @return string
  11049. */
  11050. public function getDummySelectSQL()
  11051. {
  11052. return 'SELECT 1 FROM DUAL';
  11053. }
  11054. protected function initializeDoctrineTypeMappings()
  11055. {
  11056. $this->doctrineTypeMapping = array(
  11057. 'integer' => 'integer',
  11058. 'number' => 'integer',
  11059. 'pls_integer' => 'boolean',
  11060. 'binary_integer' => 'boolean',
  11061. 'varchar' => 'string',
  11062. 'varchar2' => 'string',
  11063. 'nvarchar2' => 'string',
  11064. 'char' => 'string',
  11065. 'nchar' => 'string',
  11066. 'date' => 'datetime',
  11067. 'timestamp' => 'datetime',
  11068. 'timestamptz' => 'datetimetz',
  11069. 'float' => 'float',
  11070. 'long' => 'string',
  11071. 'clob' => 'text',
  11072. 'nclob' => 'text',
  11073. 'rowid' => 'string',
  11074. 'urowid' => 'string'
  11075. );
  11076. }
  11077. /**
  11078. * Generate SQL to release a savepoint
  11079. *
  11080. * @param string $savepoint
  11081. * @return string
  11082. */
  11083. public function releaseSavePoint($savepoint)
  11084. {
  11085. return '';
  11086. }
  11087. }
  11088. <?php
  11089. /*
  11090. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  11091. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  11092. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  11093. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  11094. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11095. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11096. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  11097. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11098. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  11099. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  11100. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11101. *
  11102. * This software consists of voluntary contributions made by many individuals
  11103. * and is licensed under the LGPL. For more information, see
  11104. * <http://www.doctrine-project.org>.
  11105. */
  11106. namespace Doctrine\DBAL\Platforms;
  11107. use Doctrine\DBAL\DBALException;
  11108. use Doctrine\DBAL\Schema\Index;
  11109. use Doctrine\DBAL\Schema\TableDiff;
  11110. class DB2Platform extends AbstractPlatform
  11111. {
  11112. public function initializeDoctrineTypeMappings()
  11113. {
  11114. $this->doctrineTypeMapping = array(
  11115. 'smallint' => 'smallint',
  11116. 'bigint' => 'bigint',
  11117. 'integer' => 'integer',
  11118. 'time' => 'time',
  11119. 'date' => 'date',
  11120. 'varchar' => 'string',
  11121. 'character' => 'string',
  11122. 'clob' => 'text',
  11123. 'decimal' => 'decimal',
  11124. 'double' => 'decimal',
  11125. 'real' => 'decimal',
  11126. 'timestamp' => 'datetime',
  11127. );
  11128. }
  11129. /**
  11130. * Gets the SQL snippet used to declare a VARCHAR column type.
  11131. *
  11132. * @param array $field
  11133. */
  11134. public function getVarcharTypeDeclarationSQL(array $field)
  11135. {
  11136. if ( ! isset($field['length'])) {
  11137. if (array_key_exists('default', $field)) {
  11138. $field['length'] = $this->getVarcharMaxLength();
  11139. } else {
  11140. $field['length'] = false;
  11141. }
  11142. }
  11143. $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
  11144. $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
  11145. return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
  11146. : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
  11147. }
  11148. /**
  11149. * Gets the SQL snippet used to declare a CLOB column type.
  11150. *
  11151. * @param array $field
  11152. */
  11153. public function getClobTypeDeclarationSQL(array $field)
  11154. {
  11155. // todo clob(n) with $field['length'];
  11156. return 'CLOB(1M)';
  11157. }
  11158. /**
  11159. * Gets the name of the platform.
  11160. *
  11161. * @return string
  11162. */
  11163. public function getName()
  11164. {
  11165. return 'db2';
  11166. }
  11167. /**
  11168. * Gets the SQL snippet that declares a boolean column.
  11169. *
  11170. * @param array $columnDef
  11171. * @return string
  11172. */
  11173. public function getBooleanTypeDeclarationSQL(array $columnDef)
  11174. {
  11175. return 'SMALLINT';
  11176. }
  11177. /**
  11178. * Gets the SQL snippet that declares a 4 byte integer column.
  11179. *
  11180. * @param array $columnDef
  11181. * @return string
  11182. */
  11183. public function getIntegerTypeDeclarationSQL(array $columnDef)
  11184. {
  11185. return 'INTEGER' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
  11186. }
  11187. /**
  11188. * Gets the SQL snippet that declares an 8 byte integer column.
  11189. *
  11190. * @param array $columnDef
  11191. * @return string
  11192. */
  11193. public function getBigIntTypeDeclarationSQL(array $columnDef)
  11194. {
  11195. return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
  11196. }
  11197. /**
  11198. * Gets the SQL snippet that declares a 2 byte integer column.
  11199. *
  11200. * @param array $columnDef
  11201. * @return string
  11202. */
  11203. public function getSmallIntTypeDeclarationSQL(array $columnDef)
  11204. {
  11205. return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
  11206. }
  11207. /**
  11208. * Gets the SQL snippet that declares common properties of an integer column.
  11209. *
  11210. * @param array $columnDef
  11211. * @return string
  11212. */
  11213. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  11214. {
  11215. $autoinc = '';
  11216. if ( ! empty($columnDef['autoincrement'])) {
  11217. $autoinc = ' GENERATED BY DEFAULT AS IDENTITY';
  11218. }
  11219. return $autoinc;
  11220. }
  11221. /**
  11222. * Obtain DBMS specific SQL to be used to create datetime fields in
  11223. * statements like CREATE TABLE
  11224. *
  11225. * @param array $fieldDeclaration
  11226. * @return string
  11227. */
  11228. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  11229. {
  11230. if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
  11231. return "TIMESTAMP(0) WITH DEFAULT";
  11232. }
  11233. return 'TIMESTAMP(0)';
  11234. }
  11235. /**
  11236. * Obtain DBMS specific SQL to be used to create date fields in statements
  11237. * like CREATE TABLE.
  11238. *
  11239. * @param array $fieldDeclaration
  11240. * @return string
  11241. */
  11242. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  11243. {
  11244. return 'DATE';
  11245. }
  11246. /**
  11247. * Obtain DBMS specific SQL to be used to create time fields in statements
  11248. * like CREATE TABLE.
  11249. *
  11250. * @param array $fieldDeclaration
  11251. * @return string
  11252. */
  11253. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  11254. {
  11255. return 'TIME';
  11256. }
  11257. public function getListDatabasesSQL()
  11258. {
  11259. throw DBALException::notSupported(__METHOD__);
  11260. }
  11261. public function getListSequencesSQL($database)
  11262. {
  11263. throw DBALException::notSupported(__METHOD__);
  11264. }
  11265. public function getListTableConstraintsSQL($table)
  11266. {
  11267. throw DBALException::notSupported(__METHOD__);
  11268. }
  11269. /**
  11270. * This code fragment is originally from the Zend_Db_Adapter_Db2 class.
  11271. *
  11272. * @license New BSD License
  11273. * @param string $table
  11274. * @return string
  11275. */
  11276. public function getListTableColumnsSQL($table)
  11277. {
  11278. return "SELECT DISTINCT c.tabschema, c.tabname, c.colname, c.colno,
  11279. c.typename, c.default, c.nulls, c.length, c.scale,
  11280. c.identity, tc.type AS tabconsttype, k.colseq
  11281. FROM syscat.columns c
  11282. LEFT JOIN (syscat.keycoluse k JOIN syscat.tabconst tc
  11283. ON (k.tabschema = tc.tabschema
  11284. AND k.tabname = tc.tabname
  11285. AND tc.type = 'P'))
  11286. ON (c.tabschema = k.tabschema
  11287. AND c.tabname = k.tabname
  11288. AND c.colname = k.colname)
  11289. WHERE UPPER(c.tabname) = UPPER('" . $table . "') ORDER BY c.colno";
  11290. }
  11291. public function getListTablesSQL()
  11292. {
  11293. return "SELECT NAME FROM SYSIBM.SYSTABLES WHERE TYPE = 'T'";
  11294. }
  11295. public function getListUsersSQL()
  11296. {
  11297. throw DBALException::notSupported(__METHOD__);
  11298. }
  11299. /**
  11300. * Get the SQL to list all views of a database or user.
  11301. *
  11302. * @param string $database
  11303. * @return string
  11304. */
  11305. public function getListViewsSQL($database)
  11306. {
  11307. return "SELECT NAME, TEXT FROM SYSIBM.SYSVIEWS";
  11308. }
  11309. public function getListTableIndexesSQL($table)
  11310. {
  11311. return "SELECT NAME, COLNAMES, UNIQUERULE FROM SYSIBM.SYSINDEXES WHERE TBNAME = UPPER('" . $table . "')";
  11312. }
  11313. public function getListTableForeignKeysSQL($table)
  11314. {
  11315. return "SELECT TBNAME, RELNAME, REFTBNAME, DELETERULE, UPDATERULE, FKCOLNAMES, PKCOLNAMES ".
  11316. "FROM SYSIBM.SYSRELS WHERE TBNAME = UPPER('".$table."')";
  11317. }
  11318. public function getCreateViewSQL($name, $sql)
  11319. {
  11320. return "CREATE VIEW ".$name." AS ".$sql;
  11321. }
  11322. public function getDropViewSQL($name)
  11323. {
  11324. return "DROP VIEW ".$name;
  11325. }
  11326. public function getDropSequenceSQL($sequence)
  11327. {
  11328. throw DBALException::notSupported(__METHOD__);
  11329. }
  11330. public function getSequenceNextValSQL($sequenceName)
  11331. {
  11332. throw DBALException::notSupported(__METHOD__);
  11333. }
  11334. public function getCreateDatabaseSQL($database)
  11335. {
  11336. return "CREATE DATABASE ".$database;
  11337. }
  11338. public function getDropDatabaseSQL($database)
  11339. {
  11340. return "DROP DATABASE ".$database.";";
  11341. }
  11342. public function supportsCreateDropDatabase()
  11343. {
  11344. return false;
  11345. }
  11346. /**
  11347. * Whether the platform supports releasing savepoints.
  11348. *
  11349. * @return boolean
  11350. */
  11351. public function supportsReleaseSavepoints()
  11352. {
  11353. return false;
  11354. }
  11355. /**
  11356. * Gets the SQL specific for the platform to get the current date.
  11357. *
  11358. * @return string
  11359. */
  11360. public function getCurrentDateSQL()
  11361. {
  11362. return 'VALUES CURRENT DATE';
  11363. }
  11364. /**
  11365. * Gets the SQL specific for the platform to get the current time.
  11366. *
  11367. * @return string
  11368. */
  11369. public function getCurrentTimeSQL()
  11370. {
  11371. return 'VALUES CURRENT TIME';
  11372. }
  11373. /**
  11374. * Gets the SQL specific for the platform to get the current timestamp
  11375. *
  11376. * @return string
  11377. */
  11378. public function getCurrentTimestampSQL()
  11379. {
  11380. return "VALUES CURRENT TIMESTAMP";
  11381. }
  11382. /**
  11383. * Obtain DBMS specific SQL code portion needed to set an index
  11384. * declaration to be used in statements like CREATE TABLE.
  11385. *
  11386. * @param string $name name of the index
  11387. * @param Index $index index definition
  11388. * @return string DBMS specific SQL code portion needed to set an index
  11389. */
  11390. public function getIndexDeclarationSQL($name, Index $index)
  11391. {
  11392. return $this->getUniqueConstraintDeclarationSQL($name, $index);
  11393. }
  11394. /**
  11395. * @param string $tableName
  11396. * @param array $columns
  11397. * @param array $options
  11398. * @return array
  11399. */
  11400. protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
  11401. {
  11402. $indexes = array();
  11403. if (isset($options['indexes'])) {
  11404. $indexes = $options['indexes'];
  11405. }
  11406. $options['indexes'] = array();
  11407. $sqls = parent::_getCreateTableSQL($tableName, $columns, $options);
  11408. foreach ($indexes as $index => $definition) {
  11409. $sqls[] = $this->getCreateIndexSQL($definition, $tableName);
  11410. }
  11411. return $sqls;
  11412. }
  11413. /**
  11414. * Gets the SQL to alter an existing table.
  11415. *
  11416. * @param TableDiff $diff
  11417. * @return array
  11418. */
  11419. public function getAlterTableSQL(TableDiff $diff)
  11420. {
  11421. $sql = array();
  11422. $queryParts = array();
  11423. foreach ($diff->addedColumns AS $fieldName => $column) {
  11424. $queryParts[] = 'ADD COLUMN ' . $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  11425. }
  11426. foreach ($diff->removedColumns AS $column) {
  11427. $queryParts[] = 'DROP COLUMN ' . $column->getName();
  11428. }
  11429. foreach ($diff->changedColumns AS $columnDiff) {
  11430. /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
  11431. $column = $columnDiff->column;
  11432. $queryParts[] = 'ALTER ' . ($columnDiff->oldColumnName) . ' '
  11433. . $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  11434. }
  11435. foreach ($diff->renamedColumns AS $oldColumnName => $column) {
  11436. $queryParts[] = 'RENAME ' . $oldColumnName . ' TO ' . $column->getName();
  11437. }
  11438. if (count($queryParts) > 0) {
  11439. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(" ", $queryParts);
  11440. }
  11441. $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
  11442. if ($diff->newName !== false) {
  11443. $sql[] = 'RENAME TABLE TO ' . $diff->newName;
  11444. }
  11445. return $sql;
  11446. }
  11447. public function getDefaultValueDeclarationSQL($field)
  11448. {
  11449. if (isset($field['notnull']) && $field['notnull'] && !isset($field['default'])) {
  11450. if (in_array((string)$field['type'], array("Integer", "BigInteger", "SmallInteger"))) {
  11451. $field['default'] = 0;
  11452. } else if((string)$field['type'] == "DateTime") {
  11453. $field['default'] = "00-00-00 00:00:00";
  11454. } else if ((string)$field['type'] == "Date") {
  11455. $field['default'] = "00-00-00";
  11456. } else if((string)$field['type'] == "Time") {
  11457. $field['default'] = "00:00:00";
  11458. } else {
  11459. $field['default'] = '';
  11460. }
  11461. }
  11462. unset($field['default']); // @todo this needs fixing
  11463. if (isset($field['version']) && $field['version']) {
  11464. if ((string)$field['type'] != "DateTime") {
  11465. $field['default'] = "1";
  11466. }
  11467. }
  11468. return parent::getDefaultValueDeclarationSQL($field);
  11469. }
  11470. /**
  11471. * Get the insert sql for an empty insert statement
  11472. *
  11473. * @param string $tableName
  11474. * @param string $identifierColumnName
  11475. * @return string $sql
  11476. */
  11477. public function getEmptyIdentityInsertSQL($tableName, $identifierColumnName)
  11478. {
  11479. return 'INSERT INTO ' . $tableName . ' (' . $identifierColumnName . ') VALUES (DEFAULT)';
  11480. }
  11481. public function getCreateTemporaryTableSnippetSQL()
  11482. {
  11483. return "DECLARE GLOBAL TEMPORARY TABLE";
  11484. }
  11485. /**
  11486. * DB2 automatically moves temporary tables into the SESSION. schema.
  11487. *
  11488. * @param string $tableName
  11489. * @return string
  11490. */
  11491. public function getTemporaryTableName($tableName)
  11492. {
  11493. return "SESSION." . $tableName;
  11494. }
  11495. public function modifyLimitQuery($query, $limit, $offset = null)
  11496. {
  11497. if ($limit === null && $offset === null) {
  11498. return $query;
  11499. }
  11500. $limit = (int)$limit;
  11501. $offset = (int)(($offset)?:0);
  11502. // Todo OVER() needs ORDER BY data!
  11503. $sql = 'SELECT db22.* FROM (SELECT ROW_NUMBER() OVER() AS DC_ROWNUM, db21.* '.
  11504. 'FROM (' . $query . ') db21) db22 WHERE db22.DC_ROWNUM BETWEEN ' . ($offset+1) .' AND ' . ($offset+$limit);
  11505. return $sql;
  11506. }
  11507. /**
  11508. * returns the position of the first occurrence of substring $substr in string $str
  11509. *
  11510. * @param string $substr literal string to find
  11511. * @param string $str literal string
  11512. * @param int $pos position to start at, beginning of string by default
  11513. * @return integer
  11514. */
  11515. public function getLocateExpression($str, $substr, $startPos = false)
  11516. {
  11517. if ($startPos == false) {
  11518. return 'LOCATE(' . $substr . ', ' . $str . ')';
  11519. } else {
  11520. return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')';
  11521. }
  11522. }
  11523. /**
  11524. * return string to call a function to get a substring inside an SQL statement
  11525. *
  11526. * Note: Not SQL92, but common functionality.
  11527. *
  11528. * SQLite only supports the 2 parameter variant of this function
  11529. *
  11530. * @param string $value an sql string literal or column name/alias
  11531. * @param integer $from where to start the substring portion
  11532. * @param integer $len the substring portion length
  11533. * @return string
  11534. */
  11535. public function getSubstringExpression($value, $from, $len = null)
  11536. {
  11537. if ($len === null)
  11538. return 'SUBSTR(' . $value . ', ' . $from . ')';
  11539. else {
  11540. return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
  11541. }
  11542. }
  11543. public function supportsIdentityColumns()
  11544. {
  11545. return true;
  11546. }
  11547. public function prefersIdentityColumns()
  11548. {
  11549. return true;
  11550. }
  11551. /**
  11552. * Gets the character casing of a column in an SQL result set of this platform.
  11553. *
  11554. * DB2 returns all column names in SQL result sets in uppercase.
  11555. *
  11556. * @param string $column The column name for which to get the correct character casing.
  11557. * @return string The column name in the character casing used in SQL result sets.
  11558. */
  11559. public function getSQLResultCasing($column)
  11560. {
  11561. return strtoupper($column);
  11562. }
  11563. public function getForUpdateSQL()
  11564. {
  11565. return ' WITH RR USE AND KEEP UPDATE LOCKS';
  11566. }
  11567. public function getDummySelectSQL()
  11568. {
  11569. return 'SELECT 1 FROM sysibm.sysdummy1';
  11570. }
  11571. /**
  11572. * DB2 supports savepoints, but they work semantically different than on other vendor platforms.
  11573. *
  11574. * TODO: We have to investigate how to get DB2 up and running with savepoints.
  11575. *
  11576. * @return bool
  11577. */
  11578. public function supportsSavepoints()
  11579. {
  11580. return false;
  11581. }
  11582. }<?php
  11583. /*
  11584. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  11585. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  11586. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  11587. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  11588. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11589. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11590. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  11591. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11592. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  11593. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  11594. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11595. *
  11596. * This software consists of voluntary contributions made by many individuals
  11597. * and is licensed under the LGPL. For more information, see
  11598. * <http://www.doctrine-project.org>.
  11599. */
  11600. namespace Doctrine\DBAL\Platforms;
  11601. use Doctrine\DBAL\DBALException,
  11602. Doctrine\DBAL\Schema\TableDiff;
  11603. /**
  11604. * The MySqlPlatform provides the behavior, features and SQL dialect of the
  11605. * MySQL database platform. This platform represents a MySQL 5.0 or greater platform that
  11606. * uses the InnoDB storage engine.
  11607. *
  11608. * @since 2.0
  11609. * @author Roman Borschel <roman@code-factory.org>
  11610. * @author Benjamin Eberlei <kontakt@beberlei.de>
  11611. * @todo Rename: MySQLPlatform
  11612. */
  11613. class MySqlPlatform extends AbstractPlatform
  11614. {
  11615. /**
  11616. * Gets the character used for identifier quoting.
  11617. *
  11618. * @return string
  11619. * @override
  11620. */
  11621. public function getIdentifierQuoteCharacter()
  11622. {
  11623. return '`';
  11624. }
  11625. /**
  11626. * Returns the regular expression operator.
  11627. *
  11628. * @return string
  11629. * @override
  11630. */
  11631. public function getRegexpExpression()
  11632. {
  11633. return 'RLIKE';
  11634. }
  11635. /**
  11636. * Returns global unique identifier
  11637. *
  11638. * @return string to get global unique identifier
  11639. * @override
  11640. */
  11641. public function getGuidExpression()
  11642. {
  11643. return 'UUID()';
  11644. }
  11645. /**
  11646. * returns the position of the first occurrence of substring $substr in string $str
  11647. *
  11648. * @param string $substr literal string to find
  11649. * @param string $str literal string
  11650. * @param int $pos position to start at, beginning of string by default
  11651. * @return integer
  11652. */
  11653. public function getLocateExpression($str, $substr, $startPos = false)
  11654. {
  11655. if ($startPos == false) {
  11656. return 'LOCATE(' . $substr . ', ' . $str . ')';
  11657. } else {
  11658. return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')';
  11659. }
  11660. }
  11661. /**
  11662. * Returns a series of strings concatinated
  11663. *
  11664. * concat() accepts an arbitrary number of parameters. Each parameter
  11665. * must contain an expression or an array with expressions.
  11666. *
  11667. * @param string|array(string) strings that will be concatinated.
  11668. * @override
  11669. */
  11670. public function getConcatExpression()
  11671. {
  11672. $args = func_get_args();
  11673. return 'CONCAT(' . join(', ', (array) $args) . ')';
  11674. }
  11675. public function getListDatabasesSQL()
  11676. {
  11677. return 'SHOW DATABASES';
  11678. }
  11679. public function getListTableConstraintsSQL($table)
  11680. {
  11681. return 'SHOW INDEX FROM ' . $table;
  11682. }
  11683. public function getListTableIndexesSQL($table)
  11684. {
  11685. return 'SHOW INDEX FROM ' . $table;
  11686. }
  11687. public function getListViewsSQL($database)
  11688. {
  11689. return "SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$database."'";
  11690. }
  11691. public function getListTableForeignKeysSQL($table, $database = null)
  11692. {
  11693. $sql = "SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ".
  11694. "k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ".
  11695. "FROM information_schema.key_column_usage k /*!50116 ".
  11696. "INNER JOIN information_schema.referential_constraints c ON ".
  11697. " c.constraint_name = k.constraint_name AND ".
  11698. " c.table_name = '$table' */ WHERE k.table_name = '$table'";
  11699. if ($database) {
  11700. $sql .= " AND k.table_schema = '$database' /*!50116 AND c.constraint_schema = '$database' */";
  11701. }
  11702. $sql .= " AND k.`REFERENCED_COLUMN_NAME` is not NULL";
  11703. return $sql;
  11704. }
  11705. public function getCreateViewSQL($name, $sql)
  11706. {
  11707. return 'CREATE VIEW ' . $name . ' AS ' . $sql;
  11708. }
  11709. public function getDropViewSQL($name)
  11710. {
  11711. return 'DROP VIEW '. $name;
  11712. }
  11713. /**
  11714. * Gets the SQL snippet used to declare a VARCHAR column on the MySql platform.
  11715. *
  11716. * @params array $field
  11717. */
  11718. public function getVarcharTypeDeclarationSQL(array $field)
  11719. {
  11720. if ( ! isset($field['length'])) {
  11721. if (array_key_exists('default', $field)) {
  11722. $field['length'] = $this->getVarcharMaxLength();
  11723. } else {
  11724. $field['length'] = false;
  11725. }
  11726. }
  11727. $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
  11728. $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
  11729. return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
  11730. : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
  11731. }
  11732. /** @override */
  11733. public function getClobTypeDeclarationSQL(array $field)
  11734. {
  11735. if ( ! empty($field['length']) && is_numeric($field['length'])) {
  11736. $length = $field['length'];
  11737. if ($length <= 255) {
  11738. return 'TINYTEXT';
  11739. } else if ($length <= 65532) {
  11740. return 'TEXT';
  11741. } else if ($length <= 16777215) {
  11742. return 'MEDIUMTEXT';
  11743. }
  11744. }
  11745. return 'LONGTEXT';
  11746. }
  11747. /**
  11748. * @override
  11749. */
  11750. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  11751. {
  11752. if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
  11753. return 'TIMESTAMP';
  11754. } else {
  11755. return 'DATETIME';
  11756. }
  11757. }
  11758. /**
  11759. * @override
  11760. */
  11761. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  11762. {
  11763. return 'DATE';
  11764. }
  11765. /**
  11766. * @override
  11767. */
  11768. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  11769. {
  11770. return 'TIME';
  11771. }
  11772. /**
  11773. * @override
  11774. */
  11775. public function getBooleanTypeDeclarationSQL(array $field)
  11776. {
  11777. return 'TINYINT(1)';
  11778. }
  11779. /**
  11780. * Obtain DBMS specific SQL code portion needed to set the COLLATION
  11781. * of a field declaration to be used in statements like CREATE TABLE.
  11782. *
  11783. * @param string $collation name of the collation
  11784. * @return string DBMS specific SQL code portion needed to set the COLLATION
  11785. * of a field declaration.
  11786. */
  11787. public function getCollationFieldDeclaration($collation)
  11788. {
  11789. return 'COLLATE ' . $collation;
  11790. }
  11791. /**
  11792. * Whether the platform prefers identity columns for ID generation.
  11793. * MySql prefers "autoincrement" identity columns since sequences can only
  11794. * be emulated with a table.
  11795. *
  11796. * @return boolean
  11797. * @override
  11798. */
  11799. public function prefersIdentityColumns()
  11800. {
  11801. return true;
  11802. }
  11803. /**
  11804. * Whether the platform supports identity columns.
  11805. * MySql supports this through AUTO_INCREMENT columns.
  11806. *
  11807. * @return boolean
  11808. * @override
  11809. */
  11810. public function supportsIdentityColumns()
  11811. {
  11812. return true;
  11813. }
  11814. public function getShowDatabasesSQL()
  11815. {
  11816. return 'SHOW DATABASES';
  11817. }
  11818. public function getListTablesSQL()
  11819. {
  11820. return 'SHOW FULL TABLES WHERE Table_type = "BASE TABLE"';
  11821. }
  11822. public function getListTableColumnsSQL($table)
  11823. {
  11824. return 'DESCRIBE ' . $table;
  11825. }
  11826. /**
  11827. * create a new database
  11828. *
  11829. * @param string $name name of the database that should be created
  11830. * @return string
  11831. * @override
  11832. */
  11833. public function getCreateDatabaseSQL($name)
  11834. {
  11835. return 'CREATE DATABASE ' . $name;
  11836. }
  11837. /**
  11838. * drop an existing database
  11839. *
  11840. * @param string $name name of the database that should be dropped
  11841. * @return string
  11842. * @override
  11843. */
  11844. public function getDropDatabaseSQL($name)
  11845. {
  11846. return 'DROP DATABASE ' . $name;
  11847. }
  11848. /**
  11849. * create a new table
  11850. *
  11851. * @param string $tableName Name of the database that should be created
  11852. * @param array $columns Associative array that contains the definition of each field of the new table
  11853. * The indexes of the array entries are the names of the fields of the table an
  11854. * the array entry values are associative arrays like those that are meant to be
  11855. * passed with the field definitions to get[Type]Declaration() functions.
  11856. * array(
  11857. * 'id' => array(
  11858. * 'type' => 'integer',
  11859. * 'unsigned' => 1
  11860. * 'notnull' => 1
  11861. * 'default' => 0
  11862. * ),
  11863. * 'name' => array(
  11864. * 'type' => 'text',
  11865. * 'length' => 12
  11866. * ),
  11867. * 'password' => array(
  11868. * 'type' => 'text',
  11869. * 'length' => 12
  11870. * )
  11871. * );
  11872. * @param array $options An associative array of table options:
  11873. * array(
  11874. * 'comment' => 'Foo',
  11875. * 'charset' => 'utf8',
  11876. * 'collate' => 'utf8_unicode_ci',
  11877. * 'type' => 'innodb',
  11878. * );
  11879. *
  11880. * @return void
  11881. * @override
  11882. */
  11883. protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
  11884. {
  11885. $queryFields = $this->getColumnDeclarationListSQL($columns);
  11886. if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
  11887. foreach ($options['uniqueConstraints'] as $index => $definition) {
  11888. $queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($index, $definition);
  11889. }
  11890. }
  11891. // add all indexes
  11892. if (isset($options['indexes']) && ! empty($options['indexes'])) {
  11893. foreach($options['indexes'] as $index => $definition) {
  11894. $queryFields .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
  11895. }
  11896. }
  11897. // attach all primary keys
  11898. if (isset($options['primary']) && ! empty($options['primary'])) {
  11899. $keyColumns = array_unique(array_values($options['primary']));
  11900. $queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
  11901. }
  11902. $query = 'CREATE ';
  11903. if (!empty($options['temporary'])) {
  11904. $query .= 'TEMPORARY ';
  11905. }
  11906. $query.= 'TABLE ' . $tableName . ' (' . $queryFields . ')';
  11907. $optionStrings = array();
  11908. if (isset($options['comment'])) {
  11909. $optionStrings['comment'] = 'COMMENT = ' . $this->quote($options['comment'], 'text');
  11910. }
  11911. if (isset($options['charset'])) {
  11912. $optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
  11913. if (isset($options['collate'])) {
  11914. $optionStrings['charset'] .= ' COLLATE ' . $options['collate'];
  11915. }
  11916. }
  11917. // get the type of the table
  11918. if (isset($options['engine'])) {
  11919. $optionStrings[] = 'ENGINE = ' . $options['engine'];
  11920. } else {
  11921. // default to innodb
  11922. $optionStrings[] = 'ENGINE = InnoDB';
  11923. }
  11924. if ( ! empty($optionStrings)) {
  11925. $query.= ' '.implode(' ', $optionStrings);
  11926. }
  11927. $sql[] = $query;
  11928. if (isset($options['foreignKeys'])) {
  11929. foreach ((array) $options['foreignKeys'] as $definition) {
  11930. $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
  11931. }
  11932. }
  11933. return $sql;
  11934. }
  11935. /**
  11936. * Gets the SQL to alter an existing table.
  11937. *
  11938. * @param TableDiff $diff
  11939. * @return array
  11940. */
  11941. public function getAlterTableSQL(TableDiff $diff)
  11942. {
  11943. $queryParts = array();
  11944. if ($diff->newName !== false) {
  11945. $queryParts[] = 'RENAME TO ' . $diff->newName;
  11946. }
  11947. foreach ($diff->addedColumns AS $fieldName => $column) {
  11948. $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  11949. }
  11950. foreach ($diff->removedColumns AS $column) {
  11951. $queryParts[] = 'DROP ' . $column->getName();
  11952. }
  11953. foreach ($diff->changedColumns AS $columnDiff) {
  11954. /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
  11955. $column = $columnDiff->column;
  11956. $queryParts[] = 'CHANGE ' . ($columnDiff->oldColumnName) . ' '
  11957. . $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  11958. }
  11959. foreach ($diff->renamedColumns AS $oldColumnName => $column) {
  11960. $queryParts[] = 'CHANGE ' . $oldColumnName . ' '
  11961. . $this->getColumnDeclarationSQL($column->getName(), $column->toArray());
  11962. }
  11963. $sql = array();
  11964. if (count($queryParts) > 0) {
  11965. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
  11966. }
  11967. $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
  11968. return $sql;
  11969. }
  11970. /**
  11971. * Obtain DBMS specific SQL code portion needed to declare an integer type
  11972. * field to be used in statements like CREATE TABLE.
  11973. *
  11974. * @param string $name name the field to be declared.
  11975. * @param string $field associative array with the name of the properties
  11976. * of the field being declared as array indexes.
  11977. * Currently, the types of supported field
  11978. * properties are as follows:
  11979. *
  11980. * unsigned
  11981. * Boolean flag that indicates whether the field
  11982. * should be declared as unsigned integer if
  11983. * possible.
  11984. *
  11985. * default
  11986. * Integer value to be used as default for this
  11987. * field.
  11988. *
  11989. * notnull
  11990. * Boolean flag that indicates whether this field is
  11991. * constrained to not be set to null.
  11992. * @return string DBMS specific SQL code portion that should be used to
  11993. * declare the specified field.
  11994. * @override
  11995. */
  11996. public function getIntegerTypeDeclarationSQL(array $field)
  11997. {
  11998. return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
  11999. }
  12000. /** @override */
  12001. public function getBigIntTypeDeclarationSQL(array $field)
  12002. {
  12003. return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
  12004. }
  12005. /** @override */
  12006. public function getSmallIntTypeDeclarationSQL(array $field)
  12007. {
  12008. return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
  12009. }
  12010. /** @override */
  12011. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  12012. {
  12013. $autoinc = '';
  12014. if ( ! empty($columnDef['autoincrement'])) {
  12015. $autoinc = ' AUTO_INCREMENT';
  12016. }
  12017. $unsigned = (isset($columnDef['unsigned']) && $columnDef['unsigned']) ? ' UNSIGNED' : '';
  12018. return $unsigned . $autoinc;
  12019. }
  12020. /**
  12021. * Return the FOREIGN KEY query section dealing with non-standard options
  12022. * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  12023. *
  12024. * @param ForeignKeyConstraint $foreignKey
  12025. * @return string
  12026. * @override
  12027. */
  12028. public function getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
  12029. {
  12030. $query = '';
  12031. if ($foreignKey->hasOption('match')) {
  12032. $query .= ' MATCH ' . $foreignKey->getOption('match');
  12033. }
  12034. $query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey);
  12035. return $query;
  12036. }
  12037. /**
  12038. * Gets the SQL to drop an index of a table.
  12039. *
  12040. * @param Index $index name of the index to be dropped
  12041. * @param string|Table $table name of table that should be used in method
  12042. * @override
  12043. */
  12044. public function getDropIndexSQL($index, $table=null)
  12045. {
  12046. if($index instanceof \Doctrine\DBAL\Schema\Index) {
  12047. $index = $index->getName();
  12048. } else if(!is_string($index)) {
  12049. throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
  12050. }
  12051. if($table instanceof \Doctrine\DBAL\Schema\Table) {
  12052. $table = $table->getName();
  12053. } else if(!is_string($table)) {
  12054. throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
  12055. }
  12056. return 'DROP INDEX ' . $index . ' ON ' . $table;
  12057. }
  12058. /**
  12059. * Gets the SQL to drop a table.
  12060. *
  12061. * @param string $table The name of table to drop.
  12062. * @override
  12063. */
  12064. public function getDropTableSQL($table)
  12065. {
  12066. if ($table instanceof \Doctrine\DBAL\Schema\Table) {
  12067. $table = $table->getName();
  12068. } else if(!is_string($table)) {
  12069. throw new \InvalidArgumentException('MysqlPlatform::getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
  12070. }
  12071. return 'DROP TABLE ' . $table;
  12072. }
  12073. public function getSetTransactionIsolationSQL($level)
  12074. {
  12075. return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
  12076. }
  12077. /**
  12078. * Get the platform name for this instance.
  12079. *
  12080. * @return string
  12081. */
  12082. public function getName()
  12083. {
  12084. return 'mysql';
  12085. }
  12086. public function getReadLockSQL()
  12087. {
  12088. return 'LOCK IN SHARE MODE';
  12089. }
  12090. protected function initializeDoctrineTypeMappings()
  12091. {
  12092. $this->doctrineTypeMapping = array(
  12093. 'tinyint' => 'boolean',
  12094. 'smallint' => 'smallint',
  12095. 'mediumint' => 'integer',
  12096. 'int' => 'integer',
  12097. 'integer' => 'integer',
  12098. 'bigint' => 'bigint',
  12099. 'tinytext' => 'text',
  12100. 'mediumtext' => 'text',
  12101. 'longtext' => 'text',
  12102. 'text' => 'text',
  12103. 'varchar' => 'string',
  12104. 'string' => 'string',
  12105. 'char' => 'string',
  12106. 'date' => 'date',
  12107. 'datetime' => 'datetime',
  12108. 'timestamp' => 'datetime',
  12109. 'time' => 'time',
  12110. 'float' => 'decimal',
  12111. 'double' => 'decimal',
  12112. 'real' => 'decimal',
  12113. 'decimal' => 'decimal',
  12114. 'numeric' => 'decimal',
  12115. 'year' => 'date',
  12116. );
  12117. }
  12118. }
  12119. <?php
  12120. /*
  12121. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  12122. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  12123. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  12124. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  12125. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  12126. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  12127. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12128. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  12129. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12130. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  12131. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12132. *
  12133. * This software consists of voluntary contributions made by many individuals
  12134. * and is licensed under the LGPL. For more information, see
  12135. * <http://www.doctrine-project.org>.
  12136. */
  12137. namespace Doctrine\DBAL\Platforms;
  12138. use Doctrine\DBAL\DBALException,
  12139. Doctrine\DBAL\Connection,
  12140. Doctrine\DBAL\Types,
  12141. Doctrine\DBAL\Schema\Table,
  12142. Doctrine\DBAL\Schema\Index,
  12143. Doctrine\DBAL\Schema\ForeignKeyConstraint,
  12144. Doctrine\DBAL\Schema\TableDiff;
  12145. /**
  12146. * Base class for all DatabasePlatforms. The DatabasePlatforms are the central
  12147. * point of abstraction of platform-specific behaviors, features and SQL dialects.
  12148. * They are a passive source of information.
  12149. *
  12150. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  12151. * @link www.doctrine-project.org
  12152. * @since 2.0
  12153. * @version $Revision: 3938 $
  12154. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  12155. * @author Jonathan Wage <jonwage@gmail.com>
  12156. * @author Roman Borschel <roman@code-factory.org>
  12157. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  12158. * @author Benjamin Eberlei <kontakt@beberlei.de>
  12159. * @todo Remove any unnecessary methods.
  12160. */
  12161. abstract class AbstractPlatform
  12162. {
  12163. /**
  12164. * @var int
  12165. */
  12166. const CREATE_INDEXES = 1;
  12167. /**
  12168. * @var int
  12169. */
  12170. const CREATE_FOREIGNKEYS = 2;
  12171. /**
  12172. * @var int
  12173. */
  12174. const TRIM_UNSPECIFIED = 0;
  12175. /**
  12176. * @var int
  12177. */
  12178. const TRIM_LEADING = 1;
  12179. /**
  12180. * @var int
  12181. */
  12182. const TRIM_TRAILING = 2;
  12183. /**
  12184. * @var int
  12185. */
  12186. const TRIM_BOTH = 3;
  12187. /**
  12188. * @var array
  12189. */
  12190. protected $doctrineTypeMapping = null;
  12191. /**
  12192. * Constructor.
  12193. */
  12194. public function __construct() {}
  12195. /**
  12196. * Gets the SQL snippet that declares a boolean column.
  12197. *
  12198. * @param array $columnDef
  12199. * @return string
  12200. */
  12201. abstract public function getBooleanTypeDeclarationSQL(array $columnDef);
  12202. /**
  12203. * Gets the SQL snippet that declares a 4 byte integer column.
  12204. *
  12205. * @param array $columnDef
  12206. * @return string
  12207. */
  12208. abstract public function getIntegerTypeDeclarationSQL(array $columnDef);
  12209. /**
  12210. * Gets the SQL snippet that declares an 8 byte integer column.
  12211. *
  12212. * @param array $columnDef
  12213. * @return string
  12214. */
  12215. abstract public function getBigIntTypeDeclarationSQL(array $columnDef);
  12216. /**
  12217. * Gets the SQL snippet that declares a 2 byte integer column.
  12218. *
  12219. * @param array $columnDef
  12220. * @return string
  12221. */
  12222. abstract public function getSmallIntTypeDeclarationSQL(array $columnDef);
  12223. /**
  12224. * Gets the SQL snippet that declares common properties of an integer column.
  12225. *
  12226. * @param array $columnDef
  12227. * @return string
  12228. */
  12229. abstract protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef);
  12230. /**
  12231. * Lazy load Doctrine Type Mappings
  12232. *
  12233. * @return void
  12234. */
  12235. abstract protected function initializeDoctrineTypeMappings();
  12236. /**
  12237. * Gets the SQL snippet used to declare a VARCHAR column type.
  12238. *
  12239. * @param array $field
  12240. */
  12241. abstract public function getVarcharTypeDeclarationSQL(array $field);
  12242. /**
  12243. * Gets the SQL snippet used to declare a CLOB column type.
  12244. *
  12245. * @param array $field
  12246. */
  12247. abstract public function getClobTypeDeclarationSQL(array $field);
  12248. /**
  12249. * Gets the name of the platform.
  12250. *
  12251. * @return string
  12252. */
  12253. abstract public function getName();
  12254. /**
  12255. * Register a doctrine type to be used in conjunction with a column type of this platform.
  12256. *
  12257. * @param string $dbType
  12258. * @param string $doctrineType
  12259. */
  12260. public function registerDoctrineTypeMapping($dbType, $doctrineType)
  12261. {
  12262. if ($this->doctrineTypeMapping === null) {
  12263. $this->initializeDoctrineTypeMappings();
  12264. }
  12265. if (!Types\Type::hasType($doctrineType)) {
  12266. throw DBALException::typeNotFound($doctrineType);
  12267. }
  12268. $dbType = strtolower($dbType);
  12269. $this->doctrineTypeMapping[$dbType] = $doctrineType;
  12270. }
  12271. /**
  12272. * Get the Doctrine type that is mapped for the given database column type.
  12273. *
  12274. * @param string $dbType
  12275. * @return string
  12276. */
  12277. public function getDoctrineTypeMapping($dbType)
  12278. {
  12279. if ($this->doctrineTypeMapping === null) {
  12280. $this->initializeDoctrineTypeMappings();
  12281. }
  12282. $dbType = strtolower($dbType);
  12283. if (isset($this->doctrineTypeMapping[$dbType])) {
  12284. return $this->doctrineTypeMapping[$dbType];
  12285. } else {
  12286. throw new \Doctrine\DBAL\DBALException("Unknown database type ".$dbType." requested, " . get_class($this) . " may not support it.");
  12287. }
  12288. }
  12289. /**
  12290. * Check if a database type is currently supported by this platform.
  12291. *
  12292. * @param string $dbType
  12293. * @return bool
  12294. */
  12295. public function hasDoctrineTypeMappingFor($dbType)
  12296. {
  12297. if ($this->doctrineTypeMapping === null) {
  12298. $this->initializeDoctrineTypeMappings();
  12299. }
  12300. $dbType = strtolower($dbType);
  12301. return isset($this->doctrineTypeMapping[$dbType]);
  12302. }
  12303. /**
  12304. * Gets the character used for identifier quoting.
  12305. *
  12306. * @return string
  12307. */
  12308. public function getIdentifierQuoteCharacter()
  12309. {
  12310. return '"';
  12311. }
  12312. /**
  12313. * Gets the string portion that starts an SQL comment.
  12314. *
  12315. * @return string
  12316. */
  12317. public function getSqlCommentStartString()
  12318. {
  12319. return "--";
  12320. }
  12321. /**
  12322. * Gets the string portion that ends an SQL comment.
  12323. *
  12324. * @return string
  12325. */
  12326. public function getSqlCommentEndString()
  12327. {
  12328. return "\n";
  12329. }
  12330. /**
  12331. * Gets the maximum length of a varchar field.
  12332. *
  12333. * @return integer
  12334. */
  12335. public function getVarcharMaxLength()
  12336. {
  12337. return 255;
  12338. }
  12339. /**
  12340. * Gets all SQL wildcard characters of the platform.
  12341. *
  12342. * @return array
  12343. */
  12344. public function getWildcards()
  12345. {
  12346. return array('%', '_');
  12347. }
  12348. /**
  12349. * Returns the regular expression operator.
  12350. *
  12351. * @return string
  12352. */
  12353. public function getRegexpExpression()
  12354. {
  12355. throw DBALException::notSupported(__METHOD__);
  12356. }
  12357. /**
  12358. * Returns the average value of a column
  12359. *
  12360. * @param string $column the column to use
  12361. * @return string generated sql including an AVG aggregate function
  12362. */
  12363. public function getAvgExpression($column)
  12364. {
  12365. return 'AVG(' . $column . ')';
  12366. }
  12367. /**
  12368. * Returns the number of rows (without a NULL value) of a column
  12369. *
  12370. * If a '*' is used instead of a column the number of selected rows
  12371. * is returned.
  12372. *
  12373. * @param string|integer $column the column to use
  12374. * @return string generated sql including a COUNT aggregate function
  12375. */
  12376. public function getCountExpression($column)
  12377. {
  12378. return 'COUNT(' . $column . ')';
  12379. }
  12380. /**
  12381. * Returns the highest value of a column
  12382. *
  12383. * @param string $column the column to use
  12384. * @return string generated sql including a MAX aggregate function
  12385. */
  12386. public function getMaxExpression($column)
  12387. {
  12388. return 'MAX(' . $column . ')';
  12389. }
  12390. /**
  12391. * Returns the lowest value of a column
  12392. *
  12393. * @param string $column the column to use
  12394. * @return string
  12395. */
  12396. public function getMinExpression($column)
  12397. {
  12398. return 'MIN(' . $column . ')';
  12399. }
  12400. /**
  12401. * Returns the total sum of a column
  12402. *
  12403. * @param string $column the column to use
  12404. * @return string
  12405. */
  12406. public function getSumExpression($column)
  12407. {
  12408. return 'SUM(' . $column . ')';
  12409. }
  12410. // scalar functions
  12411. /**
  12412. * Returns the md5 sum of a field.
  12413. *
  12414. * Note: Not SQL92, but common functionality
  12415. *
  12416. * @return string
  12417. */
  12418. public function getMd5Expression($column)
  12419. {
  12420. return 'MD5(' . $column . ')';
  12421. }
  12422. /**
  12423. * Returns the length of a text field.
  12424. *
  12425. * @param string $expression1
  12426. * @param string $expression2
  12427. * @return string
  12428. */
  12429. public function getLengthExpression($column)
  12430. {
  12431. return 'LENGTH(' . $column . ')';
  12432. }
  12433. /**
  12434. * Rounds a numeric field to the number of decimals specified.
  12435. *
  12436. * @param string $expression1
  12437. * @param string $expression2
  12438. * @return string
  12439. */
  12440. public function getRoundExpression($column, $decimals = 0)
  12441. {
  12442. return 'ROUND(' . $column . ', ' . $decimals . ')';
  12443. }
  12444. /**
  12445. * Returns the remainder of the division operation
  12446. * $expression1 / $expression2.
  12447. *
  12448. * @param string $expression1
  12449. * @param string $expression2
  12450. * @return string
  12451. */
  12452. public function getModExpression($expression1, $expression2)
  12453. {
  12454. return 'MOD(' . $expression1 . ', ' . $expression2 . ')';
  12455. }
  12456. /**
  12457. * Trim a string, leading/trailing/both and with a given char which defaults to space.
  12458. *
  12459. * @param string $str
  12460. * @param int $pos
  12461. * @param string $char has to be quoted already
  12462. * @return string
  12463. */
  12464. public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
  12465. {
  12466. $posStr = '';
  12467. $trimChar = ($char != false) ? $char . ' FROM ' : '';
  12468. if ($pos == self::TRIM_LEADING) {
  12469. $posStr = 'LEADING '.$trimChar;
  12470. } else if($pos == self::TRIM_TRAILING) {
  12471. $posStr = 'TRAILING '.$trimChar;
  12472. } else if($pos == self::TRIM_BOTH) {
  12473. $posStr = 'BOTH '.$trimChar;
  12474. }
  12475. return 'TRIM(' . $posStr . $str . ')';
  12476. }
  12477. /**
  12478. * rtrim
  12479. * returns the string $str with proceeding space characters removed
  12480. *
  12481. * @param string $str literal string or column name
  12482. * @return string
  12483. */
  12484. public function getRtrimExpression($str)
  12485. {
  12486. return 'RTRIM(' . $str . ')';
  12487. }
  12488. /**
  12489. * ltrim
  12490. * returns the string $str with leading space characters removed
  12491. *
  12492. * @param string $str literal string or column name
  12493. * @return string
  12494. */
  12495. public function getLtrimExpression($str)
  12496. {
  12497. return 'LTRIM(' . $str . ')';
  12498. }
  12499. /**
  12500. * upper
  12501. * Returns the string $str with all characters changed to
  12502. * uppercase according to the current character set mapping.
  12503. *
  12504. * @param string $str literal string or column name
  12505. * @return string
  12506. */
  12507. public function getUpperExpression($str)
  12508. {
  12509. return 'UPPER(' . $str . ')';
  12510. }
  12511. /**
  12512. * lower
  12513. * Returns the string $str with all characters changed to
  12514. * lowercase according to the current character set mapping.
  12515. *
  12516. * @param string $str literal string or column name
  12517. * @return string
  12518. */
  12519. public function getLowerExpression($str)
  12520. {
  12521. return 'LOWER(' . $str . ')';
  12522. }
  12523. /**
  12524. * returns the position of the first occurrence of substring $substr in string $str
  12525. *
  12526. * @param string $substr literal string to find
  12527. * @param string $str literal string
  12528. * @param int $pos position to start at, beginning of string by default
  12529. * @return integer
  12530. */
  12531. public function getLocateExpression($str, $substr, $startPos = false)
  12532. {
  12533. throw DBALException::notSupported(__METHOD__);
  12534. }
  12535. /**
  12536. * Returns the current system date.
  12537. *
  12538. * @return string
  12539. */
  12540. public function getNowExpression()
  12541. {
  12542. return 'NOW()';
  12543. }
  12544. /**
  12545. * return string to call a function to get a substring inside an SQL statement
  12546. *
  12547. * Note: Not SQL92, but common functionality.
  12548. *
  12549. * SQLite only supports the 2 parameter variant of this function
  12550. *
  12551. * @param string $value an sql string literal or column name/alias
  12552. * @param integer $from where to start the substring portion
  12553. * @param integer $len the substring portion length
  12554. * @return string
  12555. */
  12556. public function getSubstringExpression($value, $from, $len = null)
  12557. {
  12558. if ($len === null)
  12559. return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';
  12560. else {
  12561. return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $len . ')';
  12562. }
  12563. }
  12564. /**
  12565. * Returns a series of strings concatinated
  12566. *
  12567. * concat() accepts an arbitrary number of parameters. Each parameter
  12568. * must contain an expression
  12569. *
  12570. * @param string $arg1, $arg2 ... $argN strings that will be concatinated.
  12571. * @return string
  12572. */
  12573. public function getConcatExpression()
  12574. {
  12575. return join(' || ' , func_get_args());
  12576. }
  12577. /**
  12578. * Returns the SQL for a logical not.
  12579. *
  12580. * Example:
  12581. * <code>
  12582. * $q = new Doctrine_Query();
  12583. * $e = $q->expr;
  12584. * $q->select('*')->from('table')
  12585. * ->where($e->eq('id', $e->not('null'));
  12586. * </code>
  12587. *
  12588. * @return string a logical expression
  12589. */
  12590. public function getNotExpression($expression)
  12591. {
  12592. return 'NOT(' . $expression . ')';
  12593. }
  12594. /**
  12595. * Returns the SQL to check if a value is one in a set of
  12596. * given values.
  12597. *
  12598. * in() accepts an arbitrary number of parameters. The first parameter
  12599. * must always specify the value that should be matched against. Successive
  12600. * must contain a logical expression or an array with logical expressions.
  12601. * These expressions will be matched against the first parameter.
  12602. *
  12603. * @param string $column the value that should be matched against
  12604. * @param string|array(string) values that will be matched against $column
  12605. * @return string logical expression
  12606. */
  12607. public function getInExpression($column, $values)
  12608. {
  12609. if ( ! is_array($values)) {
  12610. $values = array($values);
  12611. }
  12612. $values = $this->getIdentifiers($values);
  12613. if (count($values) == 0) {
  12614. throw \InvalidArgumentException('Values must not be empty.');
  12615. }
  12616. return $column . ' IN (' . implode(', ', $values) . ')';
  12617. }
  12618. /**
  12619. * Returns SQL that checks if a expression is null.
  12620. *
  12621. * @param string $expression the expression that should be compared to null
  12622. * @return string logical expression
  12623. */
  12624. public function getIsNullExpression($expression)
  12625. {
  12626. return $expression . ' IS NULL';
  12627. }
  12628. /**
  12629. * Returns SQL that checks if a expression is not null.
  12630. *
  12631. * @param string $expression the expression that should be compared to null
  12632. * @return string logical expression
  12633. */
  12634. public function getIsNotNullExpression($expression)
  12635. {
  12636. return $expression . ' IS NOT NULL';
  12637. }
  12638. /**
  12639. * Returns SQL that checks if an expression evaluates to a value between
  12640. * two values.
  12641. *
  12642. * The parameter $expression is checked if it is between $value1 and $value2.
  12643. *
  12644. * Note: There is a slight difference in the way BETWEEN works on some databases.
  12645. * http://www.w3schools.com/sql/sql_between.asp. If you want complete database
  12646. * independence you should avoid using between().
  12647. *
  12648. * @param string $expression the value to compare to
  12649. * @param string $value1 the lower value to compare with
  12650. * @param string $value2 the higher value to compare with
  12651. * @return string logical expression
  12652. */
  12653. public function getBetweenExpression($expression, $value1, $value2)
  12654. {
  12655. return $expression . ' BETWEEN ' .$value1 . ' AND ' . $value2;
  12656. }
  12657. public function getAcosExpression($value)
  12658. {
  12659. return 'ACOS(' . $value . ')';
  12660. }
  12661. public function getSinExpression($value)
  12662. {
  12663. return 'SIN(' . $value . ')';
  12664. }
  12665. public function getPiExpression()
  12666. {
  12667. return 'PI()';
  12668. }
  12669. public function getCosExpression($value)
  12670. {
  12671. return 'COS(' . $value . ')';
  12672. }
  12673. public function getForUpdateSQL()
  12674. {
  12675. return 'FOR UPDATE';
  12676. }
  12677. /**
  12678. * Honors that some SQL vendors such as MsSql use table hints for locking instead of the ANSI SQL FOR UPDATE specification.
  12679. *
  12680. * @param string $fromClause
  12681. * @param int $lockMode
  12682. * @return string
  12683. */
  12684. public function appendLockHint($fromClause, $lockMode)
  12685. {
  12686. return $fromClause;
  12687. }
  12688. /**
  12689. * Get the sql snippet to append to any SELECT statement which locks rows in shared read lock.
  12690. *
  12691. * This defaults to the ASNI SQL "FOR UPDATE", which is an exclusive lock (Write). Some database
  12692. * vendors allow to lighten this constraint up to be a real read lock.
  12693. *
  12694. * @return string
  12695. */
  12696. public function getReadLockSQL()
  12697. {
  12698. return $this->getForUpdateSQL();
  12699. }
  12700. /**
  12701. * Get the SQL snippet to append to any SELECT statement which obtains an exclusive lock on the rows.
  12702. *
  12703. * The semantics of this lock mode should equal the SELECT .. FOR UPDATE of the ASNI SQL standard.
  12704. *
  12705. * @return string
  12706. */
  12707. public function getWriteLockSQL()
  12708. {
  12709. return $this->getForUpdateSQL();
  12710. }
  12711. public function getDropDatabaseSQL($database)
  12712. {
  12713. return 'DROP DATABASE ' . $database;
  12714. }
  12715. /**
  12716. * Drop a Table
  12717. *
  12718. * @param Table|string $table
  12719. * @return string
  12720. */
  12721. public function getDropTableSQL($table)
  12722. {
  12723. if ($table instanceof \Doctrine\DBAL\Schema\Table) {
  12724. $table = $table->getName();
  12725. }
  12726. return 'DROP TABLE ' . $table;
  12727. }
  12728. /**
  12729. * Drop index from a table
  12730. *
  12731. * @param Index|string $name
  12732. * @param string|Table $table
  12733. * @return string
  12734. */
  12735. public function getDropIndexSQL($index, $table=null)
  12736. {
  12737. if($index instanceof \Doctrine\DBAL\Schema\Index) {
  12738. $index = $index->getName();
  12739. } else if(!is_string($index)) {
  12740. throw new \InvalidArgumentException('AbstractPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
  12741. }
  12742. return 'DROP INDEX ' . $index;
  12743. }
  12744. /**
  12745. * Get drop constraint sql
  12746. *
  12747. * @param \Doctrine\DBAL\Schema\Constraint $constraint
  12748. * @param string|Table $table
  12749. * @return string
  12750. */
  12751. public function getDropConstraintSQL($constraint, $table)
  12752. {
  12753. if ($constraint instanceof \Doctrine\DBAL\Schema\Constraint) {
  12754. $constraint = $constraint->getName();
  12755. }
  12756. if ($table instanceof \Doctrine\DBAL\Schema\Table) {
  12757. $table = $table->getName();
  12758. }
  12759. return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $constraint;
  12760. }
  12761. /**
  12762. * @param ForeignKeyConstraint|string $foreignKey
  12763. * @param Table|string $table
  12764. * @return string
  12765. */
  12766. public function getDropForeignKeySQL($foreignKey, $table)
  12767. {
  12768. if ($foreignKey instanceof \Doctrine\DBAL\Schema\ForeignKeyConstraint) {
  12769. $foreignKey = $foreignKey->getName();
  12770. }
  12771. if ($table instanceof \Doctrine\DBAL\Schema\Table) {
  12772. $table = $table->getName();
  12773. }
  12774. return 'ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $foreignKey;
  12775. }
  12776. /**
  12777. * Gets the SQL statement(s) to create a table with the specified name, columns and constraints
  12778. * on this platform.
  12779. *
  12780. * @param string $table The name of the table.
  12781. * @param int $createFlags
  12782. * @return array The sequence of SQL statements.
  12783. */
  12784. public function getCreateTableSQL(Table $table, $createFlags=self::CREATE_INDEXES)
  12785. {
  12786. if ( ! is_int($createFlags)) {
  12787. throw new \InvalidArgumentException("Second argument of AbstractPlatform::getCreateTableSQL() has to be integer.");
  12788. }
  12789. if (count($table->getColumns()) == 0) {
  12790. throw DBALException::noColumnsSpecifiedForTable($table->getName());
  12791. }
  12792. $tableName = $table->getName();
  12793. $options = $table->getOptions();
  12794. $options['uniqueConstraints'] = array();
  12795. $options['indexes'] = array();
  12796. $options['primary'] = array();
  12797. if (($createFlags&self::CREATE_INDEXES) > 0) {
  12798. foreach ($table->getIndexes() AS $index) {
  12799. /* @var $index Index */
  12800. if ($index->isPrimary()) {
  12801. $options['primary'] = $index->getColumns();
  12802. } else {
  12803. $options['indexes'][$index->getName()] = $index;
  12804. }
  12805. }
  12806. }
  12807. $columns = array();
  12808. foreach ($table->getColumns() AS $column) {
  12809. /* @var \Doctrine\DBAL\Schema\Column $column */
  12810. $columnData = array();
  12811. $columnData['name'] = $column->getName();
  12812. $columnData['type'] = $column->getType();
  12813. $columnData['length'] = $column->getLength();
  12814. $columnData['notnull'] = $column->getNotNull();
  12815. $columnData['unique'] = false; // TODO: what do we do about this?
  12816. $columnData['version'] = ($column->hasPlatformOption("version"))?$column->getPlatformOption('version'):false;
  12817. if(strtolower($columnData['type']) == "string" && $columnData['length'] === null) {
  12818. $columnData['length'] = 255;
  12819. }
  12820. $columnData['precision'] = $column->getPrecision();
  12821. $columnData['scale'] = $column->getScale();
  12822. $columnData['default'] = $column->getDefault();
  12823. $columnData['columnDefinition'] = $column->getColumnDefinition();
  12824. $columnData['autoincrement'] = $column->getAutoincrement();
  12825. if(in_array($column->getName(), $options['primary'])) {
  12826. $columnData['primary'] = true;
  12827. }
  12828. $columns[$columnData['name']] = $columnData;
  12829. }
  12830. if (($createFlags&self::CREATE_FOREIGNKEYS) > 0) {
  12831. $options['foreignKeys'] = array();
  12832. foreach ($table->getForeignKeys() AS $fkConstraint) {
  12833. $options['foreignKeys'][] = $fkConstraint;
  12834. }
  12835. }
  12836. return $this->_getCreateTableSQL($tableName, $columns, $options);
  12837. }
  12838. /**
  12839. * @param string $tableName
  12840. * @param array $columns
  12841. * @param array $options
  12842. * @return array
  12843. */
  12844. protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
  12845. {
  12846. $columnListSql = $this->getColumnDeclarationListSQL($columns);
  12847. if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
  12848. foreach ($options['uniqueConstraints'] as $name => $definition) {
  12849. $columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($name, $definition);
  12850. }
  12851. }
  12852. if (isset($options['primary']) && ! empty($options['primary'])) {
  12853. $columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')';
  12854. }
  12855. if (isset($options['indexes']) && ! empty($options['indexes'])) {
  12856. foreach($options['indexes'] as $index => $definition) {
  12857. $columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
  12858. }
  12859. }
  12860. $query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql;
  12861. $check = $this->getCheckDeclarationSQL($columns);
  12862. if ( ! empty($check)) {
  12863. $query .= ', ' . $check;
  12864. }
  12865. $query .= ')';
  12866. $sql[] = $query;
  12867. if (isset($options['foreignKeys'])) {
  12868. foreach ((array) $options['foreignKeys'] AS $definition) {
  12869. $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
  12870. }
  12871. }
  12872. return $sql;
  12873. }
  12874. public function getCreateTemporaryTableSnippetSQL()
  12875. {
  12876. return "CREATE TEMPORARY TABLE";
  12877. }
  12878. /**
  12879. * Gets the SQL to create a sequence on this platform.
  12880. *
  12881. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  12882. * @throws DBALException
  12883. */
  12884. public function getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
  12885. {
  12886. throw DBALException::notSupported(__METHOD__);
  12887. }
  12888. /**
  12889. * Gets the SQL to create a constraint on a table on this platform.
  12890. *
  12891. * @param Constraint $constraint
  12892. * @param string|Table $table
  12893. * @return string
  12894. */
  12895. public function getCreateConstraintSQL(\Doctrine\DBAL\Schema\Constraint $constraint, $table)
  12896. {
  12897. if ($table instanceof \Doctrine\DBAL\Schema\Table) {
  12898. $table = $table->getName();
  12899. }
  12900. $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $constraint->getName();
  12901. $columns = array();
  12902. foreach ($constraint->getColumns() as $column) {
  12903. $columns[] = $column;
  12904. }
  12905. $columnList = '('. implode(', ', $columns) . ')';
  12906. $referencesClause = '';
  12907. if ($constraint instanceof \Doctrine\DBAL\Schema\Index) {
  12908. if($constraint->isPrimary()) {
  12909. $query .= ' PRIMARY KEY';
  12910. } elseif ($constraint->isUnique()) {
  12911. $query .= ' UNIQUE';
  12912. } else {
  12913. throw new \InvalidArgumentException(
  12914. 'Can only create primary or unique constraints, no common indexes with getCreateConstraintSQL().'
  12915. );
  12916. }
  12917. } else if ($constraint instanceof \Doctrine\DBAL\Schema\ForeignKeyConstraint) {
  12918. $query .= ' FOREIGN KEY';
  12919. $foreignColumns = array();
  12920. foreach ($constraint->getForeignColumns() AS $column) {
  12921. $foreignColumns[] = $column;
  12922. }
  12923. $referencesClause = ' REFERENCES '.$constraint->getForeignTableName(). ' ('.implode(', ', $foreignColumns).')';
  12924. }
  12925. $query .= ' '.$columnList.$referencesClause;
  12926. return $query;
  12927. }
  12928. /**
  12929. * Gets the SQL to create an index on a table on this platform.
  12930. *
  12931. * @param Index $index
  12932. * @param string|Table $table name of the table on which the index is to be created
  12933. * @return string
  12934. */
  12935. public function getCreateIndexSQL(Index $index, $table)
  12936. {
  12937. if ($table instanceof Table) {
  12938. $table = $table->getName();
  12939. }
  12940. $name = $index->getName();
  12941. $columns = $index->getColumns();
  12942. if (count($columns) == 0) {
  12943. throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
  12944. }
  12945. $type = '';
  12946. if ($index->isUnique()) {
  12947. $type = 'UNIQUE ';
  12948. }
  12949. $query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
  12950. $query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
  12951. return $query;
  12952. }
  12953. /**
  12954. * Quotes a string so that it can be safely used as a table or column name,
  12955. * even if it is a reserved word of the platform.
  12956. *
  12957. * NOTE: Just because you CAN use quoted identifiers doesn't mean
  12958. * you SHOULD use them. In general, they end up causing way more
  12959. * problems than they solve.
  12960. *
  12961. * @param string $str identifier name to be quoted
  12962. * @return string quoted identifier string
  12963. */
  12964. public function quoteIdentifier($str)
  12965. {
  12966. $c = $this->getIdentifierQuoteCharacter();
  12967. return $c . $str . $c;
  12968. }
  12969. /**
  12970. * Create a new foreign key
  12971. *
  12972. * @param ForeignKeyConstraint $foreignKey ForeignKey instance
  12973. * @param string|Table $table name of the table on which the foreign key is to be created
  12974. * @return string
  12975. */
  12976. public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
  12977. {
  12978. if ($table instanceof \Doctrine\DBAL\Schema\Table) {
  12979. $table = $table->getName();
  12980. }
  12981. $query = 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclarationSQL($foreignKey);
  12982. return $query;
  12983. }
  12984. /**
  12985. * Gets the sql statements for altering an existing table.
  12986. *
  12987. * The method returns an array of sql statements, since some platforms need several statements.
  12988. *
  12989. * @param TableDiff $diff
  12990. * @return array
  12991. */
  12992. public function getAlterTableSQL(TableDiff $diff)
  12993. {
  12994. throw DBALException::notSupported(__METHOD__);
  12995. }
  12996. /**
  12997. * Common code for alter table statement generation that updates the changed Index and Foreign Key definitions.
  12998. *
  12999. * @param TableDiff $diff
  13000. * @return array
  13001. */
  13002. protected function _getAlterTableIndexForeignKeySQL(TableDiff $diff)
  13003. {
  13004. if ($diff->newName !== false) {
  13005. $tableName = $diff->newName;
  13006. } else {
  13007. $tableName = $diff->name;
  13008. }
  13009. $sql = array();
  13010. if ($this->supportsForeignKeyConstraints()) {
  13011. foreach ($diff->removedForeignKeys AS $foreignKey) {
  13012. $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
  13013. }
  13014. foreach ($diff->addedForeignKeys AS $foreignKey) {
  13015. $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
  13016. }
  13017. foreach ($diff->changedForeignKeys AS $foreignKey) {
  13018. $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
  13019. $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
  13020. }
  13021. }
  13022. foreach ($diff->addedIndexes AS $index) {
  13023. $sql[] = $this->getCreateIndexSQL($index, $tableName);
  13024. }
  13025. foreach ($diff->removedIndexes AS $index) {
  13026. $sql[] = $this->getDropIndexSQL($index, $tableName);
  13027. }
  13028. foreach ($diff->changedIndexes AS $index) {
  13029. $sql[] = $this->getDropIndexSQL($index, $tableName);
  13030. $sql[] = $this->getCreateIndexSQL($index, $tableName);
  13031. }
  13032. return $sql;
  13033. }
  13034. /**
  13035. * Get declaration of a number of fields in bulk
  13036. *
  13037. * @param array $fields a multidimensional associative array.
  13038. * The first dimension determines the field name, while the second
  13039. * dimension is keyed with the name of the properties
  13040. * of the field being declared as array indexes. Currently, the types
  13041. * of supported field properties are as follows:
  13042. *
  13043. * length
  13044. * Integer value that determines the maximum length of the text
  13045. * field. If this argument is missing the field should be
  13046. * declared to have the longest length allowed by the DBMS.
  13047. *
  13048. * default
  13049. * Text value to be used as default for this field.
  13050. *
  13051. * notnull
  13052. * Boolean flag that indicates whether this field is constrained
  13053. * to not be set to null.
  13054. * charset
  13055. * Text value with the default CHARACTER SET for this field.
  13056. * collation
  13057. * Text value with the default COLLATION for this field.
  13058. * unique
  13059. * unique constraint
  13060. *
  13061. * @return string
  13062. */
  13063. public function getColumnDeclarationListSQL(array $fields)
  13064. {
  13065. $queryFields = array();
  13066. foreach ($fields as $fieldName => $field) {
  13067. $query = $this->getColumnDeclarationSQL($fieldName, $field);
  13068. $queryFields[] = $query;
  13069. }
  13070. return implode(', ', $queryFields);
  13071. }
  13072. /**
  13073. * Obtain DBMS specific SQL code portion needed to declare a generic type
  13074. * field to be used in statements like CREATE TABLE.
  13075. *
  13076. * @param string $name name the field to be declared.
  13077. * @param array $field associative array with the name of the properties
  13078. * of the field being declared as array indexes. Currently, the types
  13079. * of supported field properties are as follows:
  13080. *
  13081. * length
  13082. * Integer value that determines the maximum length of the text
  13083. * field. If this argument is missing the field should be
  13084. * declared to have the longest length allowed by the DBMS.
  13085. *
  13086. * default
  13087. * Text value to be used as default for this field.
  13088. *
  13089. * notnull
  13090. * Boolean flag that indicates whether this field is constrained
  13091. * to not be set to null.
  13092. * charset
  13093. * Text value with the default CHARACTER SET for this field.
  13094. * collation
  13095. * Text value with the default COLLATION for this field.
  13096. * unique
  13097. * unique constraint
  13098. * check
  13099. * column check constraint
  13100. * columnDefinition
  13101. * a string that defines the complete column
  13102. *
  13103. * @return string DBMS specific SQL code portion that should be used to declare the column.
  13104. */
  13105. public function getColumnDeclarationSQL($name, array $field)
  13106. {
  13107. if (isset($field['columnDefinition'])) {
  13108. $columnDef = $this->getCustomTypeDeclarationSQL($field);
  13109. } else {
  13110. $default = $this->getDefaultValueDeclarationSQL($field);
  13111. $charset = (isset($field['charset']) && $field['charset']) ?
  13112. ' ' . $this->getColumnCharsetDeclarationSQL($field['charset']) : '';
  13113. $collation = (isset($field['collation']) && $field['collation']) ?
  13114. ' ' . $this->getColumnCollationDeclarationSQL($field['collation']) : '';
  13115. $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
  13116. $unique = (isset($field['unique']) && $field['unique']) ?
  13117. ' ' . $this->getUniqueFieldDeclarationSQL() : '';
  13118. $check = (isset($field['check']) && $field['check']) ?
  13119. ' ' . $field['check'] : '';
  13120. $typeDecl = $field['type']->getSqlDeclaration($field, $this);
  13121. $columnDef = $typeDecl . $charset . $default . $notnull . $unique . $check . $collation;
  13122. }
  13123. return $name . ' ' . $columnDef;
  13124. }
  13125. /**
  13126. * Gets the SQL snippet that declares a floating point column of arbitrary precision.
  13127. *
  13128. * @param array $columnDef
  13129. * @return string
  13130. */
  13131. public function getDecimalTypeDeclarationSQL(array $columnDef)
  13132. {
  13133. $columnDef['precision'] = ( ! isset($columnDef['precision']) || empty($columnDef['precision']))
  13134. ? 10 : $columnDef['precision'];
  13135. $columnDef['scale'] = ( ! isset($columnDef['scale']) || empty($columnDef['scale']))
  13136. ? 0 : $columnDef['scale'];
  13137. return 'NUMERIC(' . $columnDef['precision'] . ', ' . $columnDef['scale'] . ')';
  13138. }
  13139. /**
  13140. * Obtain DBMS specific SQL code portion needed to set a default value
  13141. * declaration to be used in statements like CREATE TABLE.
  13142. *
  13143. * @param array $field field definition array
  13144. * @return string DBMS specific SQL code portion needed to set a default value
  13145. */
  13146. public function getDefaultValueDeclarationSQL($field)
  13147. {
  13148. $default = empty($field['notnull']) ? ' DEFAULT NULL' : '';
  13149. if (isset($field['default'])) {
  13150. $default = " DEFAULT '".$field['default']."'";
  13151. if (isset($field['type'])) {
  13152. if (in_array((string)$field['type'], array("Integer", "BigInteger", "SmallInteger"))) {
  13153. $default = " DEFAULT ".$field['default'];
  13154. } else if ((string)$field['type'] == 'DateTime' && $field['default'] == $this->getCurrentTimestampSQL()) {
  13155. $default = " DEFAULT ".$this->getCurrentTimestampSQL();
  13156. }
  13157. }
  13158. }
  13159. return $default;
  13160. }
  13161. /**
  13162. * Obtain DBMS specific SQL code portion needed to set a CHECK constraint
  13163. * declaration to be used in statements like CREATE TABLE.
  13164. *
  13165. * @param array $definition check definition
  13166. * @return string DBMS specific SQL code portion needed to set a CHECK constraint
  13167. */
  13168. public function getCheckDeclarationSQL(array $definition)
  13169. {
  13170. $constraints = array();
  13171. foreach ($definition as $field => $def) {
  13172. if (is_string($def)) {
  13173. $constraints[] = 'CHECK (' . $def . ')';
  13174. } else {
  13175. if (isset($def['min'])) {
  13176. $constraints[] = 'CHECK (' . $field . ' >= ' . $def['min'] . ')';
  13177. }
  13178. if (isset($def['max'])) {
  13179. $constraints[] = 'CHECK (' . $field . ' <= ' . $def['max'] . ')';
  13180. }
  13181. }
  13182. }
  13183. return implode(', ', $constraints);
  13184. }
  13185. /**
  13186. * Obtain DBMS specific SQL code portion needed to set a unique
  13187. * constraint declaration to be used in statements like CREATE TABLE.
  13188. *
  13189. * @param string $name name of the unique constraint
  13190. * @param Index $index index definition
  13191. * @return string DBMS specific SQL code portion needed
  13192. * to set a constraint
  13193. */
  13194. public function getUniqueConstraintDeclarationSQL($name, Index $index)
  13195. {
  13196. if (count($index->getColumns()) == 0) {
  13197. throw \InvalidArgumentException("Incomplete definition. 'columns' required.");
  13198. }
  13199. return 'CONSTRAINT ' . $name . ' UNIQUE ('
  13200. . $this->getIndexFieldDeclarationListSQL($index->getColumns())
  13201. . ')';
  13202. }
  13203. /**
  13204. * Obtain DBMS specific SQL code portion needed to set an index
  13205. * declaration to be used in statements like CREATE TABLE.
  13206. *
  13207. * @param string $name name of the index
  13208. * @param Index $index index definition
  13209. * @return string DBMS specific SQL code portion needed to set an index
  13210. */
  13211. public function getIndexDeclarationSQL($name, Index $index)
  13212. {
  13213. $type = '';
  13214. if($index->isUnique()) {
  13215. $type = 'UNIQUE ';
  13216. }
  13217. if (count($index->getColumns()) == 0) {
  13218. throw \InvalidArgumentException("Incomplete definition. 'columns' required.");
  13219. }
  13220. return $type . 'INDEX ' . $name . ' ('
  13221. . $this->getIndexFieldDeclarationListSQL($index->getColumns())
  13222. . ')';
  13223. }
  13224. /**
  13225. * getCustomTypeDeclarationSql
  13226. * Obtail SQL code portion needed to create a custom column,
  13227. * e.g. when a field has the "columnDefinition" keyword.
  13228. * Only "AUTOINCREMENT" and "PRIMARY KEY" are added if appropriate.
  13229. *
  13230. * @return string
  13231. */
  13232. public function getCustomTypeDeclarationSQL(array $columnDef)
  13233. {
  13234. return $columnDef['columnDefinition'];
  13235. }
  13236. /**
  13237. * getIndexFieldDeclarationList
  13238. * Obtain DBMS specific SQL code portion needed to set an index
  13239. * declaration to be used in statements like CREATE TABLE.
  13240. *
  13241. * @return string
  13242. */
  13243. public function getIndexFieldDeclarationListSQL(array $fields)
  13244. {
  13245. $ret = array();
  13246. foreach ($fields as $field => $definition) {
  13247. if (is_array($definition)) {
  13248. $ret[] = $field;
  13249. } else {
  13250. $ret[] = $definition;
  13251. }
  13252. }
  13253. return implode(', ', $ret);
  13254. }
  13255. /**
  13256. * A method to return the required SQL string that fits between CREATE ... TABLE
  13257. * to create the table as a temporary table.
  13258. *
  13259. * Should be overridden in driver classes to return the correct string for the
  13260. * specific database type.
  13261. *
  13262. * The default is to return the string "TEMPORARY" - this will result in a
  13263. * SQL error for any database that does not support temporary tables, or that
  13264. * requires a different SQL command from "CREATE TEMPORARY TABLE".
  13265. *
  13266. * @return string The string required to be placed between "CREATE" and "TABLE"
  13267. * to generate a temporary table, if possible.
  13268. */
  13269. public function getTemporaryTableSQL()
  13270. {
  13271. return 'TEMPORARY';
  13272. }
  13273. /**
  13274. * Some vendors require temporary table names to be qualified specially.
  13275. *
  13276. * @param string $tableName
  13277. * @return string
  13278. */
  13279. public function getTemporaryTableName($tableName)
  13280. {
  13281. return $tableName;
  13282. }
  13283. /**
  13284. * Get sql query to show a list of database.
  13285. *
  13286. * @return string
  13287. */
  13288. public function getShowDatabasesSQL()
  13289. {
  13290. throw DBALException::notSupported(__METHOD__);
  13291. }
  13292. /**
  13293. * Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
  13294. * of a field declaration to be used in statements like CREATE TABLE.
  13295. *
  13296. * @param array $definition an associative array with the following structure:
  13297. * name optional constraint name
  13298. *
  13299. * local the local field(s)
  13300. *
  13301. * foreign the foreign reference field(s)
  13302. *
  13303. * foreignTable the name of the foreign table
  13304. *
  13305. * onDelete referential delete action
  13306. *
  13307. * onUpdate referential update action
  13308. *
  13309. * deferred deferred constraint checking
  13310. *
  13311. * The onDelete and onUpdate keys accept the following values:
  13312. *
  13313. * CASCADE: Delete or update the row from the parent table and automatically delete or
  13314. * update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
  13315. * Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column
  13316. * in the parent table or in the child table.
  13317. *
  13318. * SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the
  13319. * child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier
  13320. * specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
  13321. *
  13322. * NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary
  13323. * key value is not allowed to proceed if there is a related foreign key value in the referenced table.
  13324. *
  13325. * RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as
  13326. * omitting the ON DELETE or ON UPDATE clause.
  13327. *
  13328. * SET DEFAULT
  13329. *
  13330. * @return string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
  13331. * of a field declaration.
  13332. */
  13333. public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey)
  13334. {
  13335. $sql = $this->getForeignKeyBaseDeclarationSQL($foreignKey);
  13336. $sql .= $this->getAdvancedForeignKeyOptionsSQL($foreignKey);
  13337. return $sql;
  13338. }
  13339. /**
  13340. * Return the FOREIGN KEY query section dealing with non-standard options
  13341. * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  13342. *
  13343. * @param ForeignKeyConstraint $foreignKey foreign key definition
  13344. * @return string
  13345. */
  13346. public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
  13347. {
  13348. $query = '';
  13349. if ($this->supportsForeignKeyOnUpdate() && $foreignKey->hasOption('onUpdate')) {
  13350. $query .= ' ON UPDATE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onUpdate'));
  13351. }
  13352. if ($foreignKey->hasOption('onDelete')) {
  13353. $query .= ' ON DELETE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete'));
  13354. }
  13355. return $query;
  13356. }
  13357. /**
  13358. * returns given referential action in uppercase if valid, otherwise throws
  13359. * an exception
  13360. *
  13361. * @throws Doctrine_Exception_Exception if unknown referential action given
  13362. * @param string $action foreign key referential action
  13363. * @param string foreign key referential action in uppercase
  13364. */
  13365. public function getForeignKeyReferentialActionSQL($action)
  13366. {
  13367. $upper = strtoupper($action);
  13368. switch ($upper) {
  13369. case 'CASCADE':
  13370. case 'SET NULL':
  13371. case 'NO ACTION':
  13372. case 'RESTRICT':
  13373. case 'SET DEFAULT':
  13374. return $upper;
  13375. break;
  13376. default:
  13377. throw \InvalidArgumentException('Invalid foreign key action: ' . $upper);
  13378. }
  13379. }
  13380. /**
  13381. * Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
  13382. * of a field declaration to be used in statements like CREATE TABLE.
  13383. *
  13384. * @param ForeignKeyConstraint $foreignKey
  13385. * @return string
  13386. */
  13387. public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey)
  13388. {
  13389. $sql = '';
  13390. if (strlen($foreignKey->getName())) {
  13391. $sql .= 'CONSTRAINT ' . $foreignKey->getName() . ' ';
  13392. }
  13393. $sql .= 'FOREIGN KEY (';
  13394. if (count($foreignKey->getLocalColumns()) == 0) {
  13395. throw new \InvalidArgumentException("Incomplete definition. 'local' required.");
  13396. }
  13397. if (count($foreignKey->getForeignColumns()) == 0) {
  13398. throw new \InvalidArgumentException("Incomplete definition. 'foreign' required.");
  13399. }
  13400. if (strlen($foreignKey->getForeignTableName()) == 0) {
  13401. throw new \InvalidArgumentException("Incomplete definition. 'foreignTable' required.");
  13402. }
  13403. $sql .= implode(', ', $foreignKey->getLocalColumns())
  13404. . ') REFERENCES '
  13405. . $foreignKey->getForeignTableName() . '('
  13406. . implode(', ', $foreignKey->getForeignColumns()) . ')';
  13407. return $sql;
  13408. }
  13409. /**
  13410. * Obtain DBMS specific SQL code portion needed to set the UNIQUE constraint
  13411. * of a field declaration to be used in statements like CREATE TABLE.
  13412. *
  13413. * @return string DBMS specific SQL code portion needed to set the UNIQUE constraint
  13414. * of a field declaration.
  13415. */
  13416. public function getUniqueFieldDeclarationSQL()
  13417. {
  13418. return 'UNIQUE';
  13419. }
  13420. /**
  13421. * Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
  13422. * of a field declaration to be used in statements like CREATE TABLE.
  13423. *
  13424. * @param string $charset name of the charset
  13425. * @return string DBMS specific SQL code portion needed to set the CHARACTER SET
  13426. * of a field declaration.
  13427. */
  13428. public function getColumnCharsetDeclarationSQL($charset)
  13429. {
  13430. return '';
  13431. }
  13432. /**
  13433. * Obtain DBMS specific SQL code portion needed to set the COLLATION
  13434. * of a field declaration to be used in statements like CREATE TABLE.
  13435. *
  13436. * @param string $collation name of the collation
  13437. * @return string DBMS specific SQL code portion needed to set the COLLATION
  13438. * of a field declaration.
  13439. */
  13440. public function getColumnCollationDeclarationSQL($collation)
  13441. {
  13442. return '';
  13443. }
  13444. /**
  13445. * Whether the platform prefers sequences for ID generation.
  13446. * Subclasses should override this method to return TRUE if they prefer sequences.
  13447. *
  13448. * @return boolean
  13449. */
  13450. public function prefersSequences()
  13451. {
  13452. return false;
  13453. }
  13454. /**
  13455. * Whether the platform prefers identity columns (eg. autoincrement) for ID generation.
  13456. * Subclasses should override this method to return TRUE if they prefer identity columns.
  13457. *
  13458. * @return boolean
  13459. */
  13460. public function prefersIdentityColumns()
  13461. {
  13462. return false;
  13463. }
  13464. /**
  13465. * Some platforms need the boolean values to be converted.
  13466. *
  13467. * The default conversion in this implementation converts to integers (false => 0, true => 1).
  13468. *
  13469. * @param mixed $item
  13470. */
  13471. public function convertBooleans($item)
  13472. {
  13473. if (is_array($item)) {
  13474. foreach ($item as $k => $value) {
  13475. if (is_bool($value)) {
  13476. $item[$k] = (int) $value;
  13477. }
  13478. }
  13479. } else if (is_bool($item)) {
  13480. $item = (int) $item;
  13481. }
  13482. return $item;
  13483. }
  13484. /**
  13485. * Gets the SQL statement specific for the platform to set the charset.
  13486. *
  13487. * This function is MySQL specific and required by
  13488. * {@see \Doctrine\DBAL\Connection::setCharset($charset)}
  13489. *
  13490. * @param string $charset
  13491. * @return string
  13492. */
  13493. public function getSetCharsetSQL($charset)
  13494. {
  13495. return "SET NAMES '".$charset."'";
  13496. }
  13497. /**
  13498. * Gets the SQL specific for the platform to get the current date.
  13499. *
  13500. * @return string
  13501. */
  13502. public function getCurrentDateSQL()
  13503. {
  13504. return 'CURRENT_DATE';
  13505. }
  13506. /**
  13507. * Gets the SQL specific for the platform to get the current time.
  13508. *
  13509. * @return string
  13510. */
  13511. public function getCurrentTimeSQL()
  13512. {
  13513. return 'CURRENT_TIME';
  13514. }
  13515. /**
  13516. * Gets the SQL specific for the platform to get the current timestamp
  13517. *
  13518. * @return string
  13519. */
  13520. public function getCurrentTimestampSQL()
  13521. {
  13522. return 'CURRENT_TIMESTAMP';
  13523. }
  13524. /**
  13525. * Get sql for transaction isolation level Connection constant
  13526. *
  13527. * @param integer $level
  13528. */
  13529. protected function _getTransactionIsolationLevelSQL($level)
  13530. {
  13531. switch ($level) {
  13532. case Connection::TRANSACTION_READ_UNCOMMITTED:
  13533. return 'READ UNCOMMITTED';
  13534. case Connection::TRANSACTION_READ_COMMITTED:
  13535. return 'READ COMMITTED';
  13536. case Connection::TRANSACTION_REPEATABLE_READ:
  13537. return 'REPEATABLE READ';
  13538. case Connection::TRANSACTION_SERIALIZABLE:
  13539. return 'SERIALIZABLE';
  13540. default:
  13541. throw new \InvalidArgumentException('Invalid isolation level:' . $level);
  13542. }
  13543. }
  13544. public function getListDatabasesSQL()
  13545. {
  13546. throw DBALException::notSupported(__METHOD__);
  13547. }
  13548. public function getListSequencesSQL($database)
  13549. {
  13550. throw DBALException::notSupported(__METHOD__);
  13551. }
  13552. public function getListTableConstraintsSQL($table)
  13553. {
  13554. throw DBALException::notSupported(__METHOD__);
  13555. }
  13556. public function getListTableColumnsSQL($table)
  13557. {
  13558. throw DBALException::notSupported(__METHOD__);
  13559. }
  13560. public function getListTablesSQL()
  13561. {
  13562. throw DBALException::notSupported(__METHOD__);
  13563. }
  13564. public function getListUsersSQL()
  13565. {
  13566. throw DBALException::notSupported(__METHOD__);
  13567. }
  13568. /**
  13569. * Get the SQL to list all views of a database or user.
  13570. *
  13571. * @param string $database
  13572. * @return string
  13573. */
  13574. public function getListViewsSQL($database)
  13575. {
  13576. throw DBALException::notSupported(__METHOD__);
  13577. }
  13578. public function getListTableIndexesSQL($table)
  13579. {
  13580. throw DBALException::notSupported(__METHOD__);
  13581. }
  13582. public function getListTableForeignKeysSQL($table)
  13583. {
  13584. throw DBALException::notSupported(__METHOD__);
  13585. }
  13586. public function getCreateViewSQL($name, $sql)
  13587. {
  13588. throw DBALException::notSupported(__METHOD__);
  13589. }
  13590. public function getDropViewSQL($name)
  13591. {
  13592. throw DBALException::notSupported(__METHOD__);
  13593. }
  13594. public function getDropSequenceSQL($sequence)
  13595. {
  13596. throw DBALException::notSupported(__METHOD__);
  13597. }
  13598. public function getSequenceNextValSQL($sequenceName)
  13599. {
  13600. throw DBALException::notSupported(__METHOD__);
  13601. }
  13602. public function getCreateDatabaseSQL($database)
  13603. {
  13604. throw DBALException::notSupported(__METHOD__);
  13605. }
  13606. /**
  13607. * Get sql to set the transaction isolation level
  13608. *
  13609. * @param integer $level
  13610. */
  13611. public function getSetTransactionIsolationSQL($level)
  13612. {
  13613. throw DBALException::notSupported(__METHOD__);
  13614. }
  13615. /**
  13616. * Obtain DBMS specific SQL to be used to create datetime fields in
  13617. * statements like CREATE TABLE
  13618. *
  13619. * @param array $fieldDeclaration
  13620. * @return string
  13621. */
  13622. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  13623. {
  13624. throw DBALException::notSupported(__METHOD__);
  13625. }
  13626. /**
  13627. * Obtain DBMS specific SQL to be used to create datetime with timezone offset fields.
  13628. *
  13629. * @param array $fieldDeclaration
  13630. */
  13631. public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
  13632. {
  13633. return $this->getDateTimeTypeDeclarationSQL($fieldDeclaration);
  13634. }
  13635. /**
  13636. * Obtain DBMS specific SQL to be used to create date fields in statements
  13637. * like CREATE TABLE.
  13638. *
  13639. * @param array $fieldDeclaration
  13640. * @return string
  13641. */
  13642. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  13643. {
  13644. throw DBALException::notSupported(__METHOD__);
  13645. }
  13646. /**
  13647. * Obtain DBMS specific SQL to be used to create time fields in statements
  13648. * like CREATE TABLE.
  13649. *
  13650. * @param array $fieldDeclaration
  13651. * @return string
  13652. */
  13653. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  13654. {
  13655. throw DBALException::notSupported(__METHOD__);
  13656. }
  13657. public function getFloatDeclarationSQL(array $fieldDeclaration)
  13658. {
  13659. return 'DOUBLE PRECISION';
  13660. }
  13661. /**
  13662. * Gets the default transaction isolation level of the platform.
  13663. *
  13664. * @return integer The default isolation level.
  13665. * @see Doctrine\DBAL\Connection\TRANSACTION_* constants.
  13666. */
  13667. public function getDefaultTransactionIsolationLevel()
  13668. {
  13669. return Connection::TRANSACTION_READ_COMMITTED;
  13670. }
  13671. /* supports*() metods */
  13672. /**
  13673. * Whether the platform supports sequences.
  13674. *
  13675. * @return boolean
  13676. */
  13677. public function supportsSequences()
  13678. {
  13679. return false;
  13680. }
  13681. /**
  13682. * Whether the platform supports identity columns.
  13683. * Identity columns are columns that recieve an auto-generated value from the
  13684. * database on insert of a row.
  13685. *
  13686. * @return boolean
  13687. */
  13688. public function supportsIdentityColumns()
  13689. {
  13690. return false;
  13691. }
  13692. /**
  13693. * Whether the platform supports indexes.
  13694. *
  13695. * @return boolean
  13696. */
  13697. public function supportsIndexes()
  13698. {
  13699. return true;
  13700. }
  13701. public function supportsAlterTable()
  13702. {
  13703. return true;
  13704. }
  13705. /**
  13706. * Whether the platform supports transactions.
  13707. *
  13708. * @return boolean
  13709. */
  13710. public function supportsTransactions()
  13711. {
  13712. return true;
  13713. }
  13714. /**
  13715. * Whether the platform supports savepoints.
  13716. *
  13717. * @return boolean
  13718. */
  13719. public function supportsSavepoints()
  13720. {
  13721. return true;
  13722. }
  13723. /**
  13724. * Whether the platform supports releasing savepoints.
  13725. *
  13726. * @return boolean
  13727. */
  13728. public function supportsReleaseSavepoints()
  13729. {
  13730. return $this->supportsSavepoints();
  13731. }
  13732. /**
  13733. * Whether the platform supports primary key constraints.
  13734. *
  13735. * @return boolean
  13736. */
  13737. public function supportsPrimaryConstraints()
  13738. {
  13739. return true;
  13740. }
  13741. /**
  13742. * Does the platform supports foreign key constraints?
  13743. *
  13744. * @return boolean
  13745. */
  13746. public function supportsForeignKeyConstraints()
  13747. {
  13748. return true;
  13749. }
  13750. /**
  13751. * Does this platform supports onUpdate in foreign key constraints?
  13752. *
  13753. * @return bool
  13754. */
  13755. public function supportsForeignKeyOnUpdate()
  13756. {
  13757. return ($this->supportsForeignKeyConstraints() && true);
  13758. }
  13759. /**
  13760. * Whether the platform supports database schemas.
  13761. *
  13762. * @return boolean
  13763. */
  13764. public function supportsSchemas()
  13765. {
  13766. return false;
  13767. }
  13768. /**
  13769. * Some databases don't allow to create and drop databases at all or only with certain tools.
  13770. *
  13771. * @return bool
  13772. */
  13773. public function supportsCreateDropDatabase()
  13774. {
  13775. return true;
  13776. }
  13777. /**
  13778. * Whether the platform supports getting the affected rows of a recent
  13779. * update/delete type query.
  13780. *
  13781. * @return boolean
  13782. */
  13783. public function supportsGettingAffectedRows()
  13784. {
  13785. return true;
  13786. }
  13787. public function getIdentityColumnNullInsertSQL()
  13788. {
  13789. return "";
  13790. }
  13791. /**
  13792. * Gets the format string, as accepted by the date() function, that describes
  13793. * the format of a stored datetime value of this platform.
  13794. *
  13795. * @return string The format string.
  13796. */
  13797. public function getDateTimeFormatString()
  13798. {
  13799. return 'Y-m-d H:i:s';
  13800. }
  13801. /**
  13802. * Gets the format string, as accepted by the date() function, that describes
  13803. * the format of a stored datetime with timezone value of this platform.
  13804. *
  13805. * @return string The format string.
  13806. */
  13807. public function getDateTimeTzFormatString()
  13808. {
  13809. return 'Y-m-d H:i:s';
  13810. }
  13811. /**
  13812. * Gets the format string, as accepted by the date() function, that describes
  13813. * the format of a stored date value of this platform.
  13814. *
  13815. * @return string The format string.
  13816. */
  13817. public function getDateFormatString()
  13818. {
  13819. return 'Y-m-d';
  13820. }
  13821. /**
  13822. * Gets the format string, as accepted by the date() function, that describes
  13823. * the format of a stored time value of this platform.
  13824. *
  13825. * @return string The format string.
  13826. */
  13827. public function getTimeFormatString()
  13828. {
  13829. return 'H:i:s';
  13830. }
  13831. public function modifyLimitQuery($query, $limit, $offset = null)
  13832. {
  13833. if ( ! is_null($limit)) {
  13834. $query .= ' LIMIT ' . $limit;
  13835. }
  13836. if ( ! is_null($offset)) {
  13837. $query .= ' OFFSET ' . $offset;
  13838. }
  13839. return $query;
  13840. }
  13841. /**
  13842. * Gets the character casing of a column in an SQL result set of this platform.
  13843. *
  13844. * @param string $column The column name for which to get the correct character casing.
  13845. * @return string The column name in the character casing used in SQL result sets.
  13846. */
  13847. public function getSQLResultCasing($column)
  13848. {
  13849. return $column;
  13850. }
  13851. /**
  13852. * Makes any fixes to a name of a schema element (table, sequence, ...) that are required
  13853. * by restrictions of the platform, like a maximum length.
  13854. *
  13855. * @param string $schemaName
  13856. * @return string
  13857. */
  13858. public function fixSchemaElementName($schemaElementName)
  13859. {
  13860. return $schemaElementName;
  13861. }
  13862. /**
  13863. * Maximum length of any given databse identifier, like tables or column names.
  13864. *
  13865. * @return int
  13866. */
  13867. public function getMaxIdentifierLength()
  13868. {
  13869. return 63;
  13870. }
  13871. /**
  13872. * Get the insert sql for an empty insert statement
  13873. *
  13874. * @param string $tableName
  13875. * @param string $identifierColumnName
  13876. * @return string $sql
  13877. */
  13878. public function getEmptyIdentityInsertSQL($tableName, $identifierColumnName)
  13879. {
  13880. return 'INSERT INTO ' . $tableName . ' (' . $identifierColumnName . ') VALUES (null)';
  13881. }
  13882. /**
  13883. * Generate a Truncate Table SQL statement for a given table.
  13884. *
  13885. * Cascade is not supported on many platforms but would optionally cascade the truncate by
  13886. * following the foreign keys.
  13887. *
  13888. * @param string $tableName
  13889. * @param bool $cascade
  13890. * @return string
  13891. */
  13892. public function getTruncateTableSQL($tableName, $cascade = false)
  13893. {
  13894. return 'TRUNCATE '.$tableName;
  13895. }
  13896. /**
  13897. * This is for test reasons, many vendors have special requirements for dummy statements.
  13898. *
  13899. * @return string
  13900. */
  13901. public function getDummySelectSQL()
  13902. {
  13903. return 'SELECT 1';
  13904. }
  13905. /**
  13906. * Generate SQL to create a new savepoint
  13907. *
  13908. * @param string $savepoint
  13909. * @return string
  13910. */
  13911. public function createSavePoint($savepoint)
  13912. {
  13913. return 'SAVEPOINT ' . $savepoint;
  13914. }
  13915. /**
  13916. * Generate SQL to release a savepoint
  13917. *
  13918. * @param string $savepoint
  13919. * @return string
  13920. */
  13921. public function releaseSavePoint($savepoint)
  13922. {
  13923. return 'RELEASE SAVEPOINT ' . $savepoint;
  13924. }
  13925. /**
  13926. * Generate SQL to rollback a savepoint
  13927. *
  13928. * @param string $savepoint
  13929. * @return string
  13930. */
  13931. public function rollbackSavePoint($savepoint)
  13932. {
  13933. return 'ROLLBACK TO SAVEPOINT ' . $savepoint;
  13934. }
  13935. }<?php
  13936. /*
  13937. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13938. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  13939. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  13940. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  13941. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  13942. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  13943. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  13944. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13945. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  13946. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13947. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13948. *
  13949. * This software consists of voluntary contributions made by many individuals
  13950. * and is licensed under the LGPL. For more information, see
  13951. * <http://www.doctrine-project.org>.
  13952. */
  13953. namespace Doctrine\DBAL\Platforms;
  13954. use Doctrine\DBAL\DBALException;
  13955. /**
  13956. * The SqlitePlatform class describes the specifics and dialects of the SQLite
  13957. * database platform.
  13958. *
  13959. * @since 2.0
  13960. * @author Roman Borschel <roman@code-factory.org>
  13961. * @author Benjamin Eberlei <kontakt@beberlei.de>
  13962. * @todo Rename: SQLitePlatform
  13963. */
  13964. class SqlitePlatform extends AbstractPlatform
  13965. {
  13966. /**
  13967. * returns the regular expression operator
  13968. *
  13969. * @return string
  13970. * @override
  13971. */
  13972. public function getRegexpExpression()
  13973. {
  13974. return 'RLIKE';
  13975. }
  13976. /**
  13977. * Return string to call a variable with the current timestamp inside an SQL statement
  13978. * There are three special variables for current date and time.
  13979. *
  13980. * @return string sqlite function as string
  13981. * @override
  13982. */
  13983. public function getNowExpression($type = 'timestamp')
  13984. {
  13985. switch ($type) {
  13986. case 'time':
  13987. return 'time(\'now\')';
  13988. case 'date':
  13989. return 'date(\'now\')';
  13990. case 'timestamp':
  13991. default:
  13992. return 'datetime(\'now\')';
  13993. }
  13994. }
  13995. /**
  13996. * Trim a string, leading/trailing/both and with a given char which defaults to space.
  13997. *
  13998. * @param string $str
  13999. * @param int $pos
  14000. * @param string $char
  14001. * @return string
  14002. */
  14003. public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
  14004. {
  14005. $trimFn = '';
  14006. $trimChar = ($char != false) ? (', ' . $char) : '';
  14007. if ($pos == self::TRIM_LEADING) {
  14008. $trimFn = 'LTRIM';
  14009. } else if($pos == self::TRIM_TRAILING) {
  14010. $trimFn = 'RTRIM';
  14011. } else {
  14012. $trimFn = 'TRIM';
  14013. }
  14014. return $trimFn . '(' . $str . $trimChar . ')';
  14015. }
  14016. /**
  14017. * return string to call a function to get a substring inside an SQL statement
  14018. *
  14019. * Note: Not SQL92, but common functionality.
  14020. *
  14021. * SQLite only supports the 2 parameter variant of this function
  14022. *
  14023. * @param string $value an sql string literal or column name/alias
  14024. * @param integer $position where to start the substring portion
  14025. * @param integer $length the substring portion length
  14026. * @return string SQL substring function with given parameters
  14027. * @override
  14028. */
  14029. public function getSubstringExpression($value, $position, $length = null)
  14030. {
  14031. if ($length !== null) {
  14032. return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')';
  14033. }
  14034. return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))';
  14035. }
  14036. /**
  14037. * returns the position of the first occurrence of substring $substr in string $str
  14038. *
  14039. * @param string $substr literal string to find
  14040. * @param string $str literal string
  14041. * @param int $pos position to start at, beginning of string by default
  14042. * @return integer
  14043. */
  14044. public function getLocateExpression($str, $substr, $startPos = false)
  14045. {
  14046. if ($startPos == false) {
  14047. return 'LOCATE('.$str.', '.$substr.')';
  14048. } else {
  14049. return 'LOCATE('.$str.', '.$substr.', '.$startPos.')';
  14050. }
  14051. }
  14052. protected function _getTransactionIsolationLevelSQL($level)
  14053. {
  14054. switch ($level) {
  14055. case \Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED:
  14056. return 0;
  14057. case \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED:
  14058. case \Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ:
  14059. case \Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE:
  14060. return 1;
  14061. default:
  14062. return parent::_getTransactionIsolationLevelSQL($level);
  14063. }
  14064. }
  14065. public function getSetTransactionIsolationSQL($level)
  14066. {
  14067. return 'PRAGMA read_uncommitted = ' . $this->_getTransactionIsolationLevelSQL($level);
  14068. }
  14069. /**
  14070. * @override
  14071. */
  14072. public function prefersIdentityColumns()
  14073. {
  14074. return true;
  14075. }
  14076. /**
  14077. * @override
  14078. */
  14079. public function getBooleanTypeDeclarationSQL(array $field)
  14080. {
  14081. return 'BOOLEAN';
  14082. }
  14083. /**
  14084. * @override
  14085. */
  14086. public function getIntegerTypeDeclarationSQL(array $field)
  14087. {
  14088. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  14089. }
  14090. /**
  14091. * @override
  14092. */
  14093. public function getBigIntTypeDeclarationSQL(array $field)
  14094. {
  14095. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  14096. }
  14097. /**
  14098. * @override
  14099. */
  14100. public function getTinyIntTypeDeclarationSql(array $field)
  14101. {
  14102. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  14103. }
  14104. /**
  14105. * @override
  14106. */
  14107. public function getSmallIntTypeDeclarationSQL(array $field)
  14108. {
  14109. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  14110. }
  14111. /**
  14112. * @override
  14113. */
  14114. public function getMediumIntTypeDeclarationSql(array $field)
  14115. {
  14116. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  14117. }
  14118. /**
  14119. * @override
  14120. */
  14121. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  14122. {
  14123. return 'DATETIME';
  14124. }
  14125. /**
  14126. * @override
  14127. */
  14128. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  14129. {
  14130. return 'DATE';
  14131. }
  14132. /**
  14133. * @override
  14134. */
  14135. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  14136. {
  14137. return 'TIME';
  14138. }
  14139. /**
  14140. * @override
  14141. */
  14142. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  14143. {
  14144. $autoinc = ! empty($columnDef['autoincrement']) ? ' AUTOINCREMENT' : '';
  14145. $pk = ! empty($columnDef['primary']) && ! empty($autoinc) ? ' PRIMARY KEY' : '';
  14146. return 'INTEGER' . $pk . $autoinc;
  14147. }
  14148. /**
  14149. * create a new table
  14150. *
  14151. * @param string $name Name of the database that should be created
  14152. * @param array $fields Associative array that contains the definition of each field of the new table
  14153. * The indexes of the array entries are the names of the fields of the table an
  14154. * the array entry values are associative arrays like those that are meant to be
  14155. * passed with the field definitions to get[Type]Declaration() functions.
  14156. * array(
  14157. * 'id' => array(
  14158. * 'type' => 'integer',
  14159. * 'unsigned' => 1
  14160. * 'notnull' => 1
  14161. * 'default' => 0
  14162. * ),
  14163. * 'name' => array(
  14164. * 'type' => 'text',
  14165. * 'length' => 12
  14166. * ),
  14167. * 'password' => array(
  14168. * 'type' => 'text',
  14169. * 'length' => 12
  14170. * )
  14171. * );
  14172. * @param array $options An associative array of table options:
  14173. *
  14174. * @return void
  14175. * @override
  14176. */
  14177. protected function _getCreateTableSQL($name, array $columns, array $options = array())
  14178. {
  14179. $queryFields = $this->getColumnDeclarationListSQL($columns);
  14180. $autoinc = false;
  14181. foreach($columns as $field) {
  14182. if (isset($field['autoincrement']) && $field['autoincrement']) {
  14183. $autoinc = true;
  14184. break;
  14185. }
  14186. }
  14187. if ( ! $autoinc && isset($options['primary']) && ! empty($options['primary'])) {
  14188. $keyColumns = array_unique(array_values($options['primary']));
  14189. $keyColumns = array_map(array($this, 'quoteIdentifier'), $keyColumns);
  14190. $queryFields.= ', PRIMARY KEY('.implode(', ', $keyColumns).')';
  14191. }
  14192. $query[] = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
  14193. if (isset($options['indexes']) && ! empty($options['indexes'])) {
  14194. foreach ($options['indexes'] as $index => $indexDef) {
  14195. $query[] = $this->getCreateIndexSQL($indexDef, $name);
  14196. }
  14197. }
  14198. if (isset($options['unique']) && ! empty($options['unique'])) {
  14199. foreach ($options['unique'] as $index => $indexDef) {
  14200. $query[] = $this->getCreateIndexSQL($indexDef, $name);
  14201. }
  14202. }
  14203. return $query;
  14204. }
  14205. /**
  14206. * {@inheritdoc}
  14207. */
  14208. public function getVarcharTypeDeclarationSQL(array $field)
  14209. {
  14210. if ( ! isset($field['length'])) {
  14211. if (array_key_exists('default', $field)) {
  14212. $field['length'] = $this->getVarcharMaxLength();
  14213. } else {
  14214. $field['length'] = false;
  14215. }
  14216. }
  14217. $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
  14218. $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
  14219. return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
  14220. : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
  14221. }
  14222. public function getClobTypeDeclarationSQL(array $field)
  14223. {
  14224. return 'CLOB';
  14225. }
  14226. public function getListTableConstraintsSQL($table)
  14227. {
  14228. return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = '$table' AND sql NOT NULL ORDER BY name";
  14229. }
  14230. public function getListTableColumnsSQL($table)
  14231. {
  14232. return "PRAGMA table_info($table)";
  14233. }
  14234. public function getListTableIndexesSQL($table)
  14235. {
  14236. return "PRAGMA index_list($table)";
  14237. }
  14238. public function getListTablesSQL()
  14239. {
  14240. return "SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence' "
  14241. . "UNION ALL SELECT name FROM sqlite_temp_master "
  14242. . "WHERE type = 'table' ORDER BY name";
  14243. }
  14244. public function getListViewsSQL($database)
  14245. {
  14246. return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
  14247. }
  14248. public function getCreateViewSQL($name, $sql)
  14249. {
  14250. return 'CREATE VIEW ' . $name . ' AS ' . $sql;
  14251. }
  14252. public function getDropViewSQL($name)
  14253. {
  14254. return 'DROP VIEW '. $name;
  14255. }
  14256. /**
  14257. * SQLite does support foreign key constraints, but only in CREATE TABLE statements...
  14258. * This really limits their usefulness and requires SQLite specific handling, so
  14259. * we simply say that SQLite does NOT support foreign keys for now...
  14260. *
  14261. * @return boolean FALSE
  14262. * @override
  14263. */
  14264. public function supportsForeignKeyConstraints()
  14265. {
  14266. return false;
  14267. }
  14268. public function supportsAlterTable()
  14269. {
  14270. return false;
  14271. }
  14272. public function supportsIdentityColumns()
  14273. {
  14274. return true;
  14275. }
  14276. /**
  14277. * Get the platform name for this instance
  14278. *
  14279. * @return string
  14280. */
  14281. public function getName()
  14282. {
  14283. return 'sqlite';
  14284. }
  14285. /**
  14286. * @inheritdoc
  14287. */
  14288. public function getTruncateTableSQL($tableName, $cascade = false)
  14289. {
  14290. return 'DELETE FROM '.$tableName;
  14291. }
  14292. /**
  14293. * User-defined function for Sqlite that is used with PDO::sqliteCreateFunction()
  14294. *
  14295. * @param int|float $value
  14296. * @return float
  14297. */
  14298. static public function udfSqrt($value)
  14299. {
  14300. return sqrt($value);
  14301. }
  14302. /**
  14303. * User-defined function for Sqlite that implements MOD(a, b)
  14304. */
  14305. static public function udfMod($a, $b)
  14306. {
  14307. return ($a % $b);
  14308. }
  14309. /**
  14310. * @param string $str
  14311. * @param string $substr
  14312. * @param int $offset
  14313. */
  14314. static public function udfLocate($str, $substr, $offset = 0)
  14315. {
  14316. $pos = strpos($str, $substr, $offset);
  14317. if ($pos !== false) {
  14318. return $pos+1;
  14319. }
  14320. return 0;
  14321. }
  14322. public function getForUpdateSql()
  14323. {
  14324. return '';
  14325. }
  14326. protected function initializeDoctrineTypeMappings()
  14327. {
  14328. $this->doctrineTypeMapping = array(
  14329. 'boolean' => 'boolean',
  14330. 'tinyint' => 'boolean',
  14331. 'smallint' => 'smallint',
  14332. 'mediumint' => 'integer',
  14333. 'int' => 'integer',
  14334. 'integer' => 'integer',
  14335. 'serial' => 'integer',
  14336. 'bigint' => 'bigint',
  14337. 'bigserial' => 'bigint',
  14338. 'clob' => 'text',
  14339. 'tinytext' => 'text',
  14340. 'mediumtext' => 'text',
  14341. 'longtext' => 'text',
  14342. 'text' => 'text',
  14343. 'varchar' => 'string',
  14344. 'varchar2' => 'string',
  14345. 'nvarchar' => 'string',
  14346. 'image' => 'string',
  14347. 'ntext' => 'string',
  14348. 'char' => 'string',
  14349. 'date' => 'date',
  14350. 'datetime' => 'datetime',
  14351. 'timestamp' => 'datetime',
  14352. 'time' => 'time',
  14353. 'float' => 'float',
  14354. 'double' => 'float',
  14355. 'real' => 'float',
  14356. 'decimal' => 'decimal',
  14357. 'numeric' => 'decimal',
  14358. );
  14359. }
  14360. }
  14361. <?php
  14362. /*
  14363. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14364. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14365. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14366. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  14367. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  14368. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  14369. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  14370. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14371. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14372. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  14373. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14374. *
  14375. * This software consists of voluntary contributions made by many individuals
  14376. * and is licensed under the LGPL. For more information, see
  14377. * <http://www.doctrine-project.org>.
  14378. */
  14379. namespace Doctrine\DBAL;
  14380. /**
  14381. * Class to store and retrieve the version of Doctrine
  14382. *
  14383. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  14384. * @link www.doctrine-project.org
  14385. * @since 2.0
  14386. * @version $Revision$
  14387. * @author Benjamin Eberlei <kontakt@beberlei.de>
  14388. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  14389. * @author Jonathan Wage <jonwage@gmail.com>
  14390. * @author Roman Borschel <roman@code-factory.org>
  14391. */
  14392. class Version
  14393. {
  14394. /**
  14395. * Current Doctrine Version
  14396. */
  14397. const VERSION = '2.0.0RC1-DEV';
  14398. /**
  14399. * Compares a Doctrine version with the current one.
  14400. *
  14401. * @param string $version Doctrine version to compare.
  14402. * @return int Returns -1 if older, 0 if it is the same, 1 if version
  14403. * passed as argument is newer.
  14404. */
  14405. public static function compare($version)
  14406. {
  14407. $currentVersion = str_replace(' ', '', strtolower(self::VERSION));
  14408. $version = str_replace(' ', '', $version);
  14409. return version_compare($version, $currentVersion);
  14410. }
  14411. }<?php
  14412. /*
  14413. * $Id: Exception.php 4628 2008-07-04 16:32:19Z romanb $
  14414. *
  14415. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14416. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14417. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14418. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  14419. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  14420. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  14421. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  14422. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14423. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14424. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  14425. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14426. *
  14427. * This software consists of voluntary contributions made by many individuals
  14428. * and is licensed under the LGPL. For more information, see
  14429. * <http://www.doctrine-project.org>.
  14430. */
  14431. namespace Doctrine\DBAL;
  14432. /**
  14433. * Doctrine\DBAL\ConnectionException
  14434. *
  14435. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  14436. * @link www.doctrine-project.org
  14437. * @since 2.0
  14438. * @version $Revision: 4628 $
  14439. * @author Jonathan H. Wage <jonwage@gmail.com
  14440. */
  14441. class ConnectionException extends DBALException
  14442. {
  14443. public static function commitFailedRollbackOnly()
  14444. {
  14445. return new self("Transaction commit failed because the transaction has been marked for rollback only.");
  14446. }
  14447. public static function noActiveTransaction()
  14448. {
  14449. return new self("There is no active transaction.");
  14450. }
  14451. public static function savepointsNotSupported()
  14452. {
  14453. return new self("Savepoints are not supported by this driver.");
  14454. }
  14455. public static function mayNotAlterNestedTransactionWithSavepointsInTransaction()
  14456. {
  14457. return new self("May not alter the nested transaction with savepoints behavior while a transaction is open.");
  14458. }
  14459. }<?php
  14460. /*
  14461. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14462. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14463. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14464. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  14465. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  14466. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  14467. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  14468. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14469. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14470. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  14471. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14472. *
  14473. * This software consists of voluntary contributions made by many individuals
  14474. * and is licensed under the LGPL. For more information, see
  14475. * <http://www.doctrine-project.org>.
  14476. */
  14477. namespace Doctrine\DBAL;
  14478. use PDO, Closure, Exception,
  14479. Doctrine\DBAL\Types\Type,
  14480. Doctrine\DBAL\Driver\Connection as DriverConnection,
  14481. Doctrine\Common\EventManager,
  14482. Doctrine\DBAL\DBALException;
  14483. /**
  14484. * A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like
  14485. * events, transaction isolation levels, configuration, emulated transaction nesting,
  14486. * lazy connecting and more.
  14487. *
  14488. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  14489. * @link www.doctrine-project.org
  14490. * @since 2.0
  14491. * @version $Revision: 3938 $
  14492. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  14493. * @author Jonathan Wage <jonwage@gmail.com>
  14494. * @author Roman Borschel <roman@code-factory.org>
  14495. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  14496. * @author Lukas Smith <smith@pooteeweet.org> (MDB2 library)
  14497. * @author Benjamin Eberlei <kontakt@beberlei.de>
  14498. */
  14499. class Connection implements DriverConnection
  14500. {
  14501. /**
  14502. * Constant for transaction isolation level READ UNCOMMITTED.
  14503. */
  14504. const TRANSACTION_READ_UNCOMMITTED = 1;
  14505. /**
  14506. * Constant for transaction isolation level READ COMMITTED.
  14507. */
  14508. const TRANSACTION_READ_COMMITTED = 2;
  14509. /**
  14510. * Constant for transaction isolation level REPEATABLE READ.
  14511. */
  14512. const TRANSACTION_REPEATABLE_READ = 3;
  14513. /**
  14514. * Constant for transaction isolation level SERIALIZABLE.
  14515. */
  14516. const TRANSACTION_SERIALIZABLE = 4;
  14517. /**
  14518. * The wrapped driver connection.
  14519. *
  14520. * @var Doctrine\DBAL\Driver\Connection
  14521. */
  14522. protected $_conn;
  14523. /**
  14524. * @var Doctrine\DBAL\Configuration
  14525. */
  14526. protected $_config;
  14527. /**
  14528. * @var Doctrine\Common\EventManager
  14529. */
  14530. protected $_eventManager;
  14531. /**
  14532. * Whether or not a connection has been established.
  14533. *
  14534. * @var boolean
  14535. */
  14536. private $_isConnected = false;
  14537. /**
  14538. * The transaction nesting level.
  14539. *
  14540. * @var integer
  14541. */
  14542. private $_transactionNestingLevel = 0;
  14543. /**
  14544. * The currently active transaction isolation level.
  14545. *
  14546. * @var integer
  14547. */
  14548. private $_transactionIsolationLevel;
  14549. /**
  14550. * If nested transations should use savepoints
  14551. *
  14552. * @var integer
  14553. */
  14554. private $_nestTransactionsWithSavepoints;
  14555. /**
  14556. * The parameters used during creation of the Connection instance.
  14557. *
  14558. * @var array
  14559. */
  14560. private $_params = array();
  14561. /**
  14562. * The DatabasePlatform object that provides information about the
  14563. * database platform used by the connection.
  14564. *
  14565. * @var Doctrine\DBAL\Platforms\AbstractPlatform
  14566. */
  14567. protected $_platform;
  14568. /**
  14569. * The schema manager.
  14570. *
  14571. * @var Doctrine\DBAL\Schema\SchemaManager
  14572. */
  14573. protected $_schemaManager;
  14574. /**
  14575. * The used DBAL driver.
  14576. *
  14577. * @var Doctrine\DBAL\Driver
  14578. */
  14579. protected $_driver;
  14580. /**
  14581. * Flag that indicates whether the current transaction is marked for rollback only.
  14582. *
  14583. * @var boolean
  14584. */
  14585. private $_isRollbackOnly = false;
  14586. /**
  14587. * Initializes a new instance of the Connection class.
  14588. *
  14589. * @param array $params The connection parameters.
  14590. * @param Driver $driver
  14591. * @param Configuration $config
  14592. * @param EventManager $eventManager
  14593. */
  14594. public function __construct(array $params, Driver $driver, Configuration $config = null,
  14595. EventManager $eventManager = null)
  14596. {
  14597. $this->_driver = $driver;
  14598. $this->_params = $params;
  14599. if (isset($params['pdo'])) {
  14600. $this->_conn = $params['pdo'];
  14601. $this->_isConnected = true;
  14602. }
  14603. // Create default config and event manager if none given
  14604. if ( ! $config) {
  14605. $config = new Configuration();
  14606. }
  14607. if ( ! $eventManager) {
  14608. $eventManager = new EventManager();
  14609. }
  14610. $this->_config = $config;
  14611. $this->_eventManager = $eventManager;
  14612. if ( ! isset($params['platform'])) {
  14613. $this->_platform = $driver->getDatabasePlatform();
  14614. } else if ($params['platform'] instanceof Platforms\AbstractPlatform) {
  14615. $this->_platform = $params['platform'];
  14616. } else {
  14617. throw DBALException::invalidPlatformSpecified();
  14618. }
  14619. $this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
  14620. }
  14621. /**
  14622. * Gets the parameters used during instantiation.
  14623. *
  14624. * @return array $params
  14625. */
  14626. public function getParams()
  14627. {
  14628. return $this->_params;
  14629. }
  14630. /**
  14631. * Gets the name of the database this Connection is connected to.
  14632. *
  14633. * @return string $database
  14634. */
  14635. public function getDatabase()
  14636. {
  14637. return $this->_driver->getDatabase($this);
  14638. }
  14639. /**
  14640. * Gets the hostname of the currently connected database.
  14641. *
  14642. * @return string
  14643. */
  14644. public function getHost()
  14645. {
  14646. return isset($this->_params['host']) ? $this->_params['host'] : null;
  14647. }
  14648. /**
  14649. * Gets the port of the currently connected database.
  14650. *
  14651. * @return mixed
  14652. */
  14653. public function getPort()
  14654. {
  14655. return isset($this->_params['port']) ? $this->_params['port'] : null;
  14656. }
  14657. /**
  14658. * Gets the username used by this connection.
  14659. *
  14660. * @return string
  14661. */
  14662. public function getUsername()
  14663. {
  14664. return isset($this->_params['user']) ? $this->_params['user'] : null;
  14665. }
  14666. /**
  14667. * Gets the password used by this connection.
  14668. *
  14669. * @return string
  14670. */
  14671. public function getPassword()
  14672. {
  14673. return isset($this->_params['password']) ? $this->_params['password'] : null;
  14674. }
  14675. /**
  14676. * Gets the DBAL driver instance.
  14677. *
  14678. * @return Doctrine\DBAL\Driver
  14679. */
  14680. public function getDriver()
  14681. {
  14682. return $this->_driver;
  14683. }
  14684. /**
  14685. * Gets the Configuration used by the Connection.
  14686. *
  14687. * @return Doctrine\DBAL\Configuration
  14688. */
  14689. public function getConfiguration()
  14690. {
  14691. return $this->_config;
  14692. }
  14693. /**
  14694. * Gets the EventManager used by the Connection.
  14695. *
  14696. * @return Doctrine\Common\EventManager
  14697. */
  14698. public function getEventManager()
  14699. {
  14700. return $this->_eventManager;
  14701. }
  14702. /**
  14703. * Gets the DatabasePlatform for the connection.
  14704. *
  14705. * @return Doctrine\DBAL\Platforms\AbstractPlatform
  14706. */
  14707. public function getDatabasePlatform()
  14708. {
  14709. return $this->_platform;
  14710. }
  14711. /**
  14712. * Establishes the connection with the database.
  14713. *
  14714. * @return boolean TRUE if the connection was successfully established, FALSE if
  14715. * the connection is already open.
  14716. */
  14717. public function connect()
  14718. {
  14719. if ($this->_isConnected) return false;
  14720. $driverOptions = isset($this->_params['driverOptions']) ?
  14721. $this->_params['driverOptions'] : array();
  14722. $user = isset($this->_params['user']) ? $this->_params['user'] : null;
  14723. $password = isset($this->_params['password']) ?
  14724. $this->_params['password'] : null;
  14725. $this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions);
  14726. $this->_isConnected = true;
  14727. if ($this->_eventManager->hasListeners(Events::postConnect)) {
  14728. $eventArgs = new Event\ConnectionEventArgs($this);
  14729. $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
  14730. }
  14731. return true;
  14732. }
  14733. /**
  14734. * Prepares and executes an SQL query and returns the first row of the result
  14735. * as an associative array.
  14736. *
  14737. * @param string $statement The SQL query.
  14738. * @param array $params The query parameters.
  14739. * @return array
  14740. */
  14741. public function fetchAssoc($statement, array $params = array())
  14742. {
  14743. return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_ASSOC);
  14744. }
  14745. /**
  14746. * Prepares and executes an SQL query and returns the first row of the result
  14747. * as a numerically indexed array.
  14748. *
  14749. * @param string $statement sql query to be executed
  14750. * @param array $params prepared statement params
  14751. * @return array
  14752. */
  14753. public function fetchArray($statement, array $params = array())
  14754. {
  14755. return $this->executeQuery($statement, $params)->fetch(PDO::FETCH_NUM);
  14756. }
  14757. /**
  14758. * Prepares and executes an SQL query and returns the value of a single column
  14759. * of the first row of the result.
  14760. *
  14761. * @param string $statement sql query to be executed
  14762. * @param array $params prepared statement params
  14763. * @param int $colnum 0-indexed column number to retrieve
  14764. * @return mixed
  14765. */
  14766. public function fetchColumn($statement, array $params = array(), $colnum = 0)
  14767. {
  14768. return $this->executeQuery($statement, $params)->fetchColumn($colnum);
  14769. }
  14770. /**
  14771. * Whether an actual connection to the database is established.
  14772. *
  14773. * @return boolean
  14774. */
  14775. public function isConnected()
  14776. {
  14777. return $this->_isConnected;
  14778. }
  14779. /**
  14780. * Checks whether a transaction is currently active.
  14781. *
  14782. * @return boolean TRUE if a transaction is currently active, FALSE otherwise.
  14783. */
  14784. public function isTransactionActive()
  14785. {
  14786. return $this->_transactionNestingLevel > 0;
  14787. }
  14788. /**
  14789. * Executes an SQL DELETE statement on a table.
  14790. *
  14791. * @param string $table The name of the table on which to delete.
  14792. * @param array $identifier The deletion criteria. An associateve array containing column-value pairs.
  14793. * @return integer The number of affected rows.
  14794. */
  14795. public function delete($tableName, array $identifier)
  14796. {
  14797. $this->connect();
  14798. $criteria = array();
  14799. foreach (array_keys($identifier) as $columnName) {
  14800. $criteria[] = $columnName . ' = ?';
  14801. }
  14802. $query = 'DELETE FROM ' . $tableName . ' WHERE ' . implode(' AND ', $criteria);
  14803. return $this->executeUpdate($query, array_values($identifier));
  14804. }
  14805. /**
  14806. * Closes the connection.
  14807. *
  14808. * @return void
  14809. */
  14810. public function close()
  14811. {
  14812. unset($this->_conn);
  14813. $this->_isConnected = false;
  14814. }
  14815. /**
  14816. * Sets the transaction isolation level.
  14817. *
  14818. * @param integer $level The level to set.
  14819. */
  14820. public function setTransactionIsolation($level)
  14821. {
  14822. $this->_transactionIsolationLevel = $level;
  14823. return $this->executeUpdate($this->_platform->getSetTransactionIsolationSQL($level));
  14824. }
  14825. /**
  14826. * Gets the currently active transaction isolation level.
  14827. *
  14828. * @return integer The current transaction isolation level.
  14829. */
  14830. public function getTransactionIsolation()
  14831. {
  14832. return $this->_transactionIsolationLevel;
  14833. }
  14834. /**
  14835. * Executes an SQL UPDATE statement on a table.
  14836. *
  14837. * @param string $table The name of the table to update.
  14838. * @param array $identifier The update criteria. An associative array containing column-value pairs.
  14839. * @return integer The number of affected rows.
  14840. */
  14841. public function update($tableName, array $data, array $identifier)
  14842. {
  14843. $this->connect();
  14844. $set = array();
  14845. foreach ($data as $columnName => $value) {
  14846. $set[] = $columnName . ' = ?';
  14847. }
  14848. $params = array_merge(array_values($data), array_values($identifier));
  14849. $sql = 'UPDATE ' . $tableName . ' SET ' . implode(', ', $set)
  14850. . ' WHERE ' . implode(' = ? AND ', array_keys($identifier))
  14851. . ' = ?';
  14852. return $this->executeUpdate($sql, $params);
  14853. }
  14854. /**
  14855. * Inserts a table row with specified data.
  14856. *
  14857. * @param string $table The name of the table to insert data into.
  14858. * @param array $data An associative array containing column-value pairs.
  14859. * @return integer The number of affected rows.
  14860. */
  14861. public function insert($tableName, array $data)
  14862. {
  14863. $this->connect();
  14864. // column names are specified as array keys
  14865. $cols = array();
  14866. $placeholders = array();
  14867. foreach ($data as $columnName => $value) {
  14868. $cols[] = $columnName;
  14869. $placeholders[] = '?';
  14870. }
  14871. $query = 'INSERT INTO ' . $tableName
  14872. . ' (' . implode(', ', $cols) . ')'
  14873. . ' VALUES (' . implode(', ', $placeholders) . ')';
  14874. return $this->executeUpdate($query, array_values($data));
  14875. }
  14876. /**
  14877. * Sets the given charset on the current connection.
  14878. *
  14879. * @param string $charset The charset to set.
  14880. */
  14881. public function setCharset($charset)
  14882. {
  14883. $this->executeUpdate($this->_platform->getSetCharsetSQL($charset));
  14884. }
  14885. /**
  14886. * Quote a string so it can be safely used as a table or column name, even if
  14887. * it is a reserved name.
  14888. *
  14889. * Delimiting style depends on the underlying database platform that is being used.
  14890. *
  14891. * NOTE: Just because you CAN use quoted identifiers does not mean
  14892. * you SHOULD use them. In general, they end up causing way more
  14893. * problems than they solve.
  14894. *
  14895. * @param string $str The name to be quoted.
  14896. * @return string The quoted name.
  14897. */
  14898. public function quoteIdentifier($str)
  14899. {
  14900. return $this->_platform->quoteIdentifier($str);
  14901. }
  14902. /**
  14903. * Quotes a given input parameter.
  14904. *
  14905. * @param mixed $input Parameter to be quoted.
  14906. * @param string $type Type of the parameter.
  14907. * @return string The quoted parameter.
  14908. */
  14909. public function quote($input, $type = null)
  14910. {
  14911. $this->connect();
  14912. return $this->_conn->quote($input, $type);
  14913. }
  14914. /**
  14915. * Prepares and executes an SQL query and returns the result as an associative array.
  14916. *
  14917. * @param string $sql The SQL query.
  14918. * @param array $params The query parameters.
  14919. * @return array
  14920. */
  14921. public function fetchAll($sql, array $params = array())
  14922. {
  14923. return $this->executeQuery($sql, $params)->fetchAll(PDO::FETCH_ASSOC);
  14924. }
  14925. /**
  14926. * Prepares an SQL statement.
  14927. *
  14928. * @param string $statement The SQL statement to prepare.
  14929. * @return Doctrine\DBAL\Driver\Statement The prepared statement.
  14930. */
  14931. public function prepare($statement)
  14932. {
  14933. $this->connect();
  14934. return new Statement($statement, $this);
  14935. }
  14936. /**
  14937. * Executes an, optionally parameterized, SQL query.
  14938. *
  14939. * If the query is parameterized, a prepared statement is used.
  14940. * If an SQLLogger is configured, the execution is logged.
  14941. *
  14942. * @param string $query The SQL query to execute.
  14943. * @param array $params The parameters to bind to the query, if any.
  14944. * @return Doctrine\DBAL\Driver\Statement The executed statement.
  14945. * @internal PERF: Directly prepares a driver statement, not a wrapper.
  14946. */
  14947. public function executeQuery($query, array $params = array(), $types = array())
  14948. {
  14949. $this->connect();
  14950. $hasLogger = $this->_config->getSQLLogger() !== null;
  14951. if ($hasLogger) {
  14952. $this->_config->getSQLLogger()->startQuery($query, $params, $types);
  14953. }
  14954. if ($params) {
  14955. $stmt = $this->_conn->prepare($query);
  14956. if ($types) {
  14957. $this->_bindTypedValues($stmt, $params, $types);
  14958. $stmt->execute();
  14959. } else {
  14960. $stmt->execute($params);
  14961. }
  14962. } else {
  14963. $stmt = $this->_conn->query($query);
  14964. }
  14965. if ($hasLogger) {
  14966. $this->_config->getSQLLogger()->stopQuery();
  14967. }
  14968. return $stmt;
  14969. }
  14970. /**
  14971. * Executes an, optionally parameterized, SQL query and returns the result,
  14972. * applying a given projection/transformation function on each row of the result.
  14973. *
  14974. * @param string $query The SQL query to execute.
  14975. * @param array $params The parameters, if any.
  14976. * @param Closure $mapper The transformation function that is applied on each row.
  14977. * The function receives a single paramater, an array, that
  14978. * represents a row of the result set.
  14979. * @return mixed The projected result of the query.
  14980. */
  14981. public function project($query, array $params, Closure $function)
  14982. {
  14983. $result = array();
  14984. $stmt = $this->executeQuery($query, $params ?: array());
  14985. while ($row = $stmt->fetch()) {
  14986. $result[] = $function($row);
  14987. }
  14988. $stmt->closeCursor();
  14989. return $result;
  14990. }
  14991. /**
  14992. * Executes an SQL statement, returning a result set as a Statement object.
  14993. *
  14994. * @param string $statement
  14995. * @param integer $fetchType
  14996. * @return Doctrine\DBAL\Driver\Statement
  14997. */
  14998. public function query()
  14999. {
  15000. $this->connect();
  15001. return call_user_func_array(array($this->_conn, 'query'), func_get_args());
  15002. }
  15003. /**
  15004. * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
  15005. * and returns the number of affected rows.
  15006. *
  15007. * This method supports PDO binding types as well as DBAL mapping types.
  15008. *
  15009. * @param string $query The SQL query.
  15010. * @param array $params The query parameters.
  15011. * @param array $types The parameter types.
  15012. * @return integer The number of affected rows.
  15013. * @internal PERF: Directly prepares a driver statement, not a wrapper.
  15014. */
  15015. public function executeUpdate($query, array $params = array(), array $types = array())
  15016. {
  15017. $this->connect();
  15018. $hasLogger = $this->_config->getSQLLogger() !== null;
  15019. if ($hasLogger) {
  15020. $this->_config->getSQLLogger()->startQuery($query, $params, $types);
  15021. }
  15022. if ($params) {
  15023. $stmt = $this->_conn->prepare($query);
  15024. if ($types) {
  15025. $this->_bindTypedValues($stmt, $params, $types);
  15026. $stmt->execute();
  15027. } else {
  15028. $stmt->execute($params);
  15029. }
  15030. $result = $stmt->rowCount();
  15031. } else {
  15032. $result = $this->_conn->exec($query);
  15033. }
  15034. if ($hasLogger) {
  15035. $this->_config->getSQLLogger()->stopQuery();
  15036. }
  15037. return $result;
  15038. }
  15039. /**
  15040. * Execute an SQL statement and return the number of affected rows.
  15041. *
  15042. * @param string $statement
  15043. * @return integer The number of affected rows.
  15044. */
  15045. public function exec($statement)
  15046. {
  15047. $this->connect();
  15048. return $this->_conn->exec($statement);
  15049. }
  15050. /**
  15051. * Returns the current transaction nesting level.
  15052. *
  15053. * @return integer The nesting level. A value of 0 means there's no active transaction.
  15054. */
  15055. public function getTransactionNestingLevel()
  15056. {
  15057. return $this->_transactionNestingLevel;
  15058. }
  15059. /**
  15060. * Fetch the SQLSTATE associated with the last database operation.
  15061. *
  15062. * @return integer The last error code.
  15063. */
  15064. public function errorCode()
  15065. {
  15066. $this->connect();
  15067. return $this->_conn->errorCode();
  15068. }
  15069. /**
  15070. * Fetch extended error information associated with the last database operation.
  15071. *
  15072. * @return array The last error information.
  15073. */
  15074. public function errorInfo()
  15075. {
  15076. $this->connect();
  15077. return $this->_conn->errorInfo();
  15078. }
  15079. /**
  15080. * Returns the ID of the last inserted row, or the last value from a sequence object,
  15081. * depending on the underlying driver.
  15082. *
  15083. * Note: This method may not return a meaningful or consistent result across different drivers,
  15084. * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY
  15085. * columns or sequences.
  15086. *
  15087. * @param string $seqName Name of the sequence object from which the ID should be returned.
  15088. * @return string A string representation of the last inserted ID.
  15089. */
  15090. public function lastInsertId($seqName = null)
  15091. {
  15092. $this->connect();
  15093. return $this->_conn->lastInsertId($seqName);
  15094. }
  15095. /**
  15096. * Executes a function in a transaction.
  15097. *
  15098. * The function gets passed this Connection instance as an (optional) parameter.
  15099. *
  15100. * If an exception occurs during execution of the function or transaction commit,
  15101. * the transaction is rolled back and the exception re-thrown.
  15102. *
  15103. * @param Closure $func The function to execute transactionally.
  15104. */
  15105. public function transactional(Closure $func)
  15106. {
  15107. $this->beginTransaction();
  15108. try {
  15109. $func($this);
  15110. $this->commit();
  15111. } catch (Exception $e) {
  15112. $this->rollback();
  15113. throw $e;
  15114. }
  15115. }
  15116. /**
  15117. * Set if nested transactions should use savepoints
  15118. *
  15119. * @param boolean
  15120. * @return void
  15121. */
  15122. public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
  15123. {
  15124. if ($this->_transactionNestingLevel > 0) {
  15125. throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction();
  15126. }
  15127. if (!$this->_platform->supportsSavepoints()) {
  15128. throw ConnectionException::savepointsNotSupported();
  15129. }
  15130. $this->_nestTransactionsWithSavepoints = $nestTransactionsWithSavepoints;
  15131. }
  15132. /**
  15133. * Get if nested transactions should use savepoints
  15134. *
  15135. * @return boolean
  15136. */
  15137. public function getNestTransactionsWithSavepoints()
  15138. {
  15139. return $this->_nestTransactionsWithSavepoints;
  15140. }
  15141. /**
  15142. * Returns the savepoint name to use for nested transactions are false if they are not supported
  15143. * "savepointFormat" parameter is not set
  15144. *
  15145. * @return mixed a string with the savepoint name or false
  15146. */
  15147. protected function _getNestedTransactionSavePointName()
  15148. {
  15149. return 'DOCTRINE2_SAVEPOINT_'.$this->_transactionNestingLevel;
  15150. }
  15151. /**
  15152. * Starts a transaction by suspending auto-commit mode.
  15153. *
  15154. * @return void
  15155. */
  15156. public function beginTransaction()
  15157. {
  15158. $this->connect();
  15159. ++$this->_transactionNestingLevel;
  15160. if ($this->_transactionNestingLevel == 1) {
  15161. $this->_conn->beginTransaction();
  15162. } else if ($this->_nestTransactionsWithSavepoints) {
  15163. $this->createSavepoint($this->_getNestedTransactionSavePointName());
  15164. }
  15165. }
  15166. /**
  15167. * Commits the current transaction.
  15168. *
  15169. * @return void
  15170. * @throws ConnectionException If the commit failed due to no active transaction or
  15171. * because the transaction was marked for rollback only.
  15172. */
  15173. public function commit()
  15174. {
  15175. if ($this->_transactionNestingLevel == 0) {
  15176. throw ConnectionException::noActiveTransaction();
  15177. }
  15178. if ($this->_isRollbackOnly) {
  15179. throw ConnectionException::commitFailedRollbackOnly();
  15180. }
  15181. $this->connect();
  15182. if ($this->_transactionNestingLevel == 1) {
  15183. $this->_conn->commit();
  15184. } else if ($this->_nestTransactionsWithSavepoints) {
  15185. $this->releaseSavepoint($this->_getNestedTransactionSavePointName());
  15186. }
  15187. --$this->_transactionNestingLevel;
  15188. }
  15189. /**
  15190. * Cancel any database changes done during the current transaction.
  15191. *
  15192. * this method can be listened with onPreTransactionRollback and onTransactionRollback
  15193. * eventlistener methods
  15194. *
  15195. * @throws ConnectionException If the rollback operation failed.
  15196. */
  15197. public function rollback()
  15198. {
  15199. if ($this->_transactionNestingLevel == 0) {
  15200. throw ConnectionException::noActiveTransaction();
  15201. }
  15202. $this->connect();
  15203. if ($this->_transactionNestingLevel == 1) {
  15204. $this->_transactionNestingLevel = 0;
  15205. $this->_conn->rollback();
  15206. $this->_isRollbackOnly = false;
  15207. } else if ($this->_nestTransactionsWithSavepoints) {
  15208. $this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
  15209. --$this->_transactionNestingLevel;
  15210. } else {
  15211. $this->_isRollbackOnly = true;
  15212. --$this->_transactionNestingLevel;
  15213. }
  15214. }
  15215. /**
  15216. * createSavepoint
  15217. * creates a new savepoint
  15218. *
  15219. * @param string $savepoint name of a savepoint to set
  15220. * @return void
  15221. */
  15222. public function createSavepoint($savepoint)
  15223. {
  15224. if (!$this->_platform->supportsSavepoints()) {
  15225. throw ConnectionException::savepointsNotSupported();
  15226. }
  15227. $this->_conn->exec($this->_platform->createSavePoint($savepoint));
  15228. }
  15229. /**
  15230. * releaseSavePoint
  15231. * releases given savepoint
  15232. *
  15233. * @param string $savepoint name of a savepoint to release
  15234. * @return void
  15235. */
  15236. public function releaseSavepoint($savepoint)
  15237. {
  15238. if (!$this->_platform->supportsSavepoints()) {
  15239. throw ConnectionException::savepointsNotSupported();
  15240. }
  15241. if ($this->_platform->supportsReleaseSavepoints()) {
  15242. $this->_conn->exec($this->_platform->releaseSavePoint($savepoint));
  15243. }
  15244. }
  15245. /**
  15246. * rollbackSavePoint
  15247. * releases given savepoint
  15248. *
  15249. * @param string $savepoint name of a savepoint to rollback to
  15250. * @return void
  15251. */
  15252. public function rollbackSavepoint($savepoint)
  15253. {
  15254. if (!$this->_platform->supportsSavepoints()) {
  15255. throw ConnectionException::savepointsNotSupported();
  15256. }
  15257. $this->_conn->exec($this->_platform->rollbackSavePoint($savepoint));
  15258. }
  15259. /**
  15260. * Gets the wrapped driver connection.
  15261. *
  15262. * @return Doctrine\DBAL\Driver\Connection
  15263. */
  15264. public function getWrappedConnection()
  15265. {
  15266. $this->connect();
  15267. return $this->_conn;
  15268. }
  15269. /**
  15270. * Gets the SchemaManager that can be used to inspect or change the
  15271. * database schema through the connection.
  15272. *
  15273. * @return Doctrine\DBAL\Schema\SchemaManager
  15274. */
  15275. public function getSchemaManager()
  15276. {
  15277. if ( ! $this->_schemaManager) {
  15278. $this->_schemaManager = $this->_driver->getSchemaManager($this);
  15279. }
  15280. return $this->_schemaManager;
  15281. }
  15282. /**
  15283. * Marks the current transaction so that the only possible
  15284. * outcome for the transaction to be rolled back.
  15285. *
  15286. * @throws ConnectionException If no transaction is active.
  15287. */
  15288. public function setRollbackOnly()
  15289. {
  15290. if ($this->_transactionNestingLevel == 0) {
  15291. throw ConnectionException::noActiveTransaction();
  15292. }
  15293. $this->_isRollbackOnly = true;
  15294. }
  15295. /**
  15296. * Check whether the current transaction is marked for rollback only.
  15297. *
  15298. * @return boolean
  15299. * @throws ConnectionException If no transaction is active.
  15300. */
  15301. public function isRollbackOnly()
  15302. {
  15303. if ($this->_transactionNestingLevel == 0) {
  15304. throw ConnectionException::noActiveTransaction();
  15305. }
  15306. return $this->_isRollbackOnly;
  15307. }
  15308. /**
  15309. * Converts a given value to its database representation according to the conversion
  15310. * rules of a specific DBAL mapping type.
  15311. *
  15312. * @param mixed $value The value to convert.
  15313. * @param string $type The name of the DBAL mapping type.
  15314. * @return mixed The converted value.
  15315. */
  15316. public function convertToDatabaseValue($value, $type)
  15317. {
  15318. return Type::getType($type)->convertToDatabaseValue($value, $this->_platform);
  15319. }
  15320. /**
  15321. * Converts a given value to its PHP representation according to the conversion
  15322. * rules of a specific DBAL mapping type.
  15323. *
  15324. * @param mixed $value The value to convert.
  15325. * @param string $type The name of the DBAL mapping type.
  15326. * @return mixed The converted type.
  15327. */
  15328. public function convertToPHPValue($value, $type)
  15329. {
  15330. return Type::getType($type)->convertToPHPValue($value, $this->_platform);
  15331. }
  15332. /**
  15333. * Binds a set of parameters, some or all of which are typed with a PDO binding type
  15334. * or DBAL mapping type, to a given statement.
  15335. *
  15336. * @param $stmt The statement to bind the values to.
  15337. * @param array $params The map/list of named/positional parameters.
  15338. * @param array $types The parameter types (PDO binding types or DBAL mapping types).
  15339. * @internal Duck-typing used on the $stmt parameter to support driver statements as well as
  15340. * raw PDOStatement instances.
  15341. */
  15342. private function _bindTypedValues($stmt, array $params, array $types)
  15343. {
  15344. // Check whether parameters are positional or named. Mixing is not allowed, just like in PDO.
  15345. if (is_int(key($params))) {
  15346. // Positional parameters
  15347. $typeOffset = isset($types[0]) ? -1 : 0;
  15348. $bindIndex = 1;
  15349. foreach ($params as $position => $value) {
  15350. $typeIndex = $bindIndex + $typeOffset;
  15351. if (isset($types[$typeIndex])) {
  15352. $type = $types[$typeIndex];
  15353. if (is_string($type)) {
  15354. $type = Type::getType($type);
  15355. }
  15356. if ($type instanceof Type) {
  15357. $value = $type->convertToDatabaseValue($value, $this->_platform);
  15358. $bindingType = $type->getBindingType();
  15359. } else {
  15360. $bindingType = $type; // PDO::PARAM_* constants
  15361. }
  15362. $stmt->bindValue($bindIndex, $value, $bindingType);
  15363. } else {
  15364. $stmt->bindValue($bindIndex, $value);
  15365. }
  15366. ++$bindIndex;
  15367. }
  15368. } else {
  15369. // Named parameters
  15370. foreach ($params as $name => $value) {
  15371. if (isset($types[$name])) {
  15372. $type = $types[$name];
  15373. if (is_string($type)) {
  15374. $type = Type::getType($type);
  15375. }
  15376. if ($type instanceof Type) {
  15377. $value = $type->convertToDatabaseValue($value, $this->_platform);
  15378. $bindingType = $type->getBindingType();
  15379. } else {
  15380. $bindingType = $type; // PDO::PARAM_* constants
  15381. }
  15382. $stmt->bindValue($name, $value, $bindingType);
  15383. } else {
  15384. $stmt->bindValue($name, $value);
  15385. }
  15386. }
  15387. }
  15388. }
  15389. }<?php
  15390. /*
  15391. * $Id$
  15392. *
  15393. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15394. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15395. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15396. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15397. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15398. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15399. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15400. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15401. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15402. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15403. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15404. *
  15405. * This software consists of voluntary contributions made by many individuals
  15406. * and is licensed under the LGPL. For more information, see
  15407. * <http://www.doctrine-project.org>.
  15408. */
  15409. namespace Doctrine\DBAL\Logging;
  15410. /**
  15411. * A SQL logger that logs to the standard output using echo/var_dump.
  15412. *
  15413. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15414. * @link www.doctrine-project.org
  15415. * @since 2.0
  15416. * @version $Revision$
  15417. * @author Benjamin Eberlei <kontakt@beberlei.de>
  15418. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  15419. * @author Jonathan Wage <jonwage@gmail.com>
  15420. * @author Roman Borschel <roman@code-factory.org>
  15421. */
  15422. class EchoSQLLogger implements SQLLogger
  15423. {
  15424. /**
  15425. * {@inheritdoc}
  15426. */
  15427. public function startQuery($sql, array $params = null, array $types = null)
  15428. {
  15429. echo $sql . PHP_EOL;
  15430. if ($params) {
  15431. var_dump($params);
  15432. }
  15433. if ($types) {
  15434. var_dump($types);
  15435. }
  15436. }
  15437. /**
  15438. * {@inheritdoc}
  15439. */
  15440. public function stopQuery()
  15441. {
  15442. }
  15443. }<?php
  15444. /*
  15445. * $Id$
  15446. *
  15447. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15448. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15449. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15450. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15451. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15452. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15453. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15454. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15455. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15456. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15457. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15458. *
  15459. * This software consists of voluntary contributions made by many individuals
  15460. * and is licensed under the LGPL. For more information, see
  15461. * <http://www.doctrine-project.org>.
  15462. */
  15463. namespace Doctrine\DBAL\Logging;
  15464. /**
  15465. * Includes executed SQLs in a Debug Stack
  15466. *
  15467. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15468. * @link www.doctrine-project.org
  15469. * @since 2.0
  15470. * @version $Revision$
  15471. * @author Benjamin Eberlei <kontakt@beberlei.de>
  15472. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  15473. * @author Jonathan Wage <jonwage@gmail.com>
  15474. * @author Roman Borschel <roman@code-factory.org>
  15475. */
  15476. class DebugStack implements SQLLogger
  15477. {
  15478. /** @var array $queries Executed SQL queries. */
  15479. public $queries = array();
  15480. /** @var boolean $enabled If Debug Stack is enabled (log queries) or not. */
  15481. public $enabled = true;
  15482. public $start = null;
  15483. /**
  15484. * {@inheritdoc}
  15485. */
  15486. public function startQuery($sql, array $params = null, array $types = null)
  15487. {
  15488. if ($this->enabled) {
  15489. $this->start = microtime(true);
  15490. $this->queries[] = array('sql' => $sql, 'params' => $params, 'types' => $types, 'executionMS' => 0);
  15491. }
  15492. }
  15493. /**
  15494. * {@inheritdoc}
  15495. */
  15496. public function stopQuery()
  15497. {
  15498. $this->queries[(count($this->queries)-1)]['executionMS'] = microtime(true) - $this->start;
  15499. }
  15500. }
  15501. <?php
  15502. /*
  15503. * $Id$
  15504. *
  15505. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15506. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15507. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15508. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15509. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15510. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15511. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15512. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15513. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15514. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15515. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15516. *
  15517. * This software consists of voluntary contributions made by many individuals
  15518. * and is licensed under the LGPL. For more information, see
  15519. * <http://www.doctrine-project.org>.
  15520. */
  15521. namespace Doctrine\DBAL\Logging;
  15522. /**
  15523. * Interface for SQL loggers.
  15524. *
  15525. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15526. * @link www.doctrine-project.org
  15527. * @since 2.0
  15528. * @version $Revision$
  15529. * @author Benjamin Eberlei <kontakt@beberlei.de>
  15530. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  15531. * @author Jonathan Wage <jonwage@gmail.com>
  15532. * @author Roman Borschel <roman@code-factory.org>
  15533. */
  15534. interface SQLLogger
  15535. {
  15536. /**
  15537. * Logs a SQL statement somewhere.
  15538. *
  15539. * @param string $sql The SQL to be executed.
  15540. * @param array $params The SQL parameters.
  15541. * @param float $executionMS The microtime difference it took to execute this query.
  15542. * @return void
  15543. */
  15544. public function startQuery($sql, array $params = null, array $types = null);
  15545. /**
  15546. * Mark the last started query as stopped. This can be used for timing of queries.
  15547. *
  15548. * @return void
  15549. */
  15550. public function stopQuery();
  15551. }<?php
  15552. /*
  15553. * $Id$
  15554. *
  15555. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15556. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15557. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15558. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15559. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15560. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15561. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15562. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15563. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15564. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15565. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15566. *
  15567. * This software consists of voluntary contributions made by many individuals
  15568. * and is licensed under the LGPL. For more information, see
  15569. * <http://www.doctrine-project.org>.
  15570. */
  15571. namespace Doctrine\DBAL\Tools\Console\Command;
  15572. use Symfony\Component\Console\Input\InputArgument,
  15573. Symfony\Component\Console;
  15574. /**
  15575. * Task for executing arbitrary SQL that can come from a file or directly from
  15576. * the command line.
  15577. *
  15578. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15579. * @link www.doctrine-project.org
  15580. * @since 2.0
  15581. * @version $Revision$
  15582. * @author Benjamin Eberlei <kontakt@beberlei.de>
  15583. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  15584. * @author Jonathan Wage <jonwage@gmail.com>
  15585. * @author Roman Borschel <roman@code-factory.org>
  15586. */
  15587. class ImportCommand extends Console\Command\Command
  15588. {
  15589. /**
  15590. * @see Console\Command\Command
  15591. */
  15592. protected function configure()
  15593. {
  15594. $this
  15595. ->setName('dbal:import')
  15596. ->setDescription('Import SQL file(s) directly to Database.')
  15597. ->setDefinition(array(
  15598. new InputArgument(
  15599. 'file', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'File path(s) of SQL to be executed.'
  15600. )
  15601. ))
  15602. ->setHelp(<<<EOT
  15603. Import SQL file(s) directly to Database.
  15604. EOT
  15605. );
  15606. }
  15607. /**
  15608. * @see Console\Command\Command
  15609. */
  15610. protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  15611. {
  15612. $conn = $this->getHelper('db')->getConnection();
  15613. if (($fileNames = $input->getArgument('file')) !== null) {
  15614. foreach ((array) $fileNames as $fileName) {
  15615. $fileName = realpath($fileName);
  15616. if ( ! file_exists($fileName)) {
  15617. throw new \InvalidArgumentException(
  15618. sprintf("SQL file '<info>%s</info>' does not exist.", $fileName)
  15619. );
  15620. } else if ( ! is_readable($fileName)) {
  15621. throw new \InvalidArgumentException(
  15622. sprintf("SQL file '<info>%s</info>' does not have read permissions.", $fileName)
  15623. );
  15624. }
  15625. $output->write(sprintf("Processing file '<info>%s</info>'... ", $fileName));
  15626. $sql = file_get_contents($fileName);
  15627. if ($conn instanceof \Doctrine\DBAL\Driver\PDOConnection) {
  15628. // PDO Drivers
  15629. try {
  15630. $lines = 0;
  15631. $stmt = $conn->prepare($sql);
  15632. $stmt->execute();
  15633. do {
  15634. // Required due to "MySQL has gone away!" issue
  15635. $stmt->fetch();
  15636. $stmt->closeCursor();
  15637. $lines++;
  15638. } while ($stmt->nextRowset());
  15639. $output->write(sprintf('%d statements executed!', $lines) . PHP_EOL);
  15640. } catch (\PDOException $e) {
  15641. $output->write('error!' . PHP_EOL);
  15642. throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
  15643. }
  15644. } else {
  15645. // Non-PDO Drivers (ie. OCI8 driver)
  15646. $stmt = $conn->prepare($sql);
  15647. $rs = $stmt->execute();
  15648. if ($rs) {
  15649. $printer->writeln('OK!');
  15650. } else {
  15651. $error = $stmt->errorInfo();
  15652. $output->write('error!' . PHP_EOL);
  15653. throw new \RuntimeException($error[2], $error[0]);
  15654. }
  15655. $stmt->closeCursor();
  15656. }
  15657. }
  15658. }
  15659. }
  15660. }
  15661. <?php
  15662. /*
  15663. * $Id$
  15664. *
  15665. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15666. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15667. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15668. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15669. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15670. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15671. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15672. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15673. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15674. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15675. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15676. *
  15677. * This software consists of voluntary contributions made by many individuals
  15678. * and is licensed under the LGPL. For more information, see
  15679. * <http://www.doctrine-project.org>.
  15680. */
  15681. namespace Doctrine\DBAL\Tools\Console\Command;
  15682. use Symfony\Component\Console\Input\InputArgument,
  15683. Symfony\Component\Console\Input\InputOption,
  15684. Symfony\Component\Console;
  15685. /**
  15686. * Task for executing arbitrary SQL that can come from a file or directly from
  15687. * the command line.
  15688. *
  15689. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15690. * @link www.doctrine-project.org
  15691. * @since 2.0
  15692. * @version $Revision$
  15693. * @author Benjamin Eberlei <kontakt@beberlei.de>
  15694. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  15695. * @author Jonathan Wage <jonwage@gmail.com>
  15696. * @author Roman Borschel <roman@code-factory.org>
  15697. */
  15698. class RunSqlCommand extends Console\Command\Command
  15699. {
  15700. /**
  15701. * @see Console\Command\Command
  15702. */
  15703. protected function configure()
  15704. {
  15705. $this
  15706. ->setName('dbal:run-sql')
  15707. ->setDescription('Executes arbitrary SQL directly from the command line.')
  15708. ->setDefinition(array(
  15709. new InputArgument('sql', InputArgument::REQUIRED, 'The SQL statement to execute.'),
  15710. new InputOption('depth', null, InputOption::PARAMETER_REQUIRED, 'Dumping depth of result set.', 7)
  15711. ))
  15712. ->setHelp(<<<EOT
  15713. Executes arbitrary SQL directly from the command line.
  15714. EOT
  15715. );
  15716. }
  15717. /**
  15718. * @see Console\Command\Command
  15719. */
  15720. protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  15721. {
  15722. $conn = $this->getHelper('db')->getConnection();
  15723. if (($sql = $input->getArgument('sql')) === null) {
  15724. throw new \RuntimeException("Argument 'SQL' is required in order to execute this command correctly.");
  15725. }
  15726. $depth = $input->getOption('depth');
  15727. if ( ! is_numeric($depth)) {
  15728. throw new \LogicException("Option 'depth' must contains an integer value");
  15729. }
  15730. if (preg_match('/^select/i', $sql)) {
  15731. $resultSet = $conn->fetchAll($sql);
  15732. } else {
  15733. $resultSet = $conn->executeUpdate($sql);
  15734. }
  15735. \Doctrine\Common\Util\Debug::dump($resultSet, (int) $depth);
  15736. }
  15737. }
  15738. <?php
  15739. /*
  15740. * $Id$
  15741. *
  15742. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15743. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15744. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15745. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15746. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15747. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15748. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15749. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15750. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15751. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15752. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15753. *
  15754. * This software consists of voluntary contributions made by many individuals
  15755. * and is licensed under the LGPL. For more information, see
  15756. * <http://www.doctrine-project.org>.
  15757. */
  15758. namespace Doctrine\DBAL\Tools\Console\Helper;
  15759. use Symfony\Component\Console\Helper\Helper,
  15760. Doctrine\DBAL\Connection;
  15761. /**
  15762. * Doctrine CLI Connection Helper.
  15763. *
  15764. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15765. * @link www.doctrine-project.org
  15766. * @since 2.0
  15767. * @version $Revision$
  15768. * @author Benjamin Eberlei <kontakt@beberlei.de>
  15769. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  15770. * @author Jonathan Wage <jonwage@gmail.com>
  15771. * @author Roman Borschel <roman@code-factory.org>
  15772. */
  15773. class ConnectionHelper extends Helper
  15774. {
  15775. /**
  15776. * Doctrine Database Connection
  15777. * @var Connection
  15778. */
  15779. protected $_connection;
  15780. /**
  15781. * Constructor
  15782. *
  15783. * @param Connection $connection Doctrine Database Connection
  15784. */
  15785. public function __construct(Connection $connection)
  15786. {
  15787. $this->_connection = $connection;
  15788. }
  15789. /**
  15790. * Retrieves Doctrine Database Connection
  15791. *
  15792. * @return Connection
  15793. */
  15794. public function getConnection()
  15795. {
  15796. return $this->_connection;
  15797. }
  15798. /**
  15799. * @see Helper
  15800. */
  15801. public function getName()
  15802. {
  15803. return 'connection';
  15804. }
  15805. }
  15806. <?php
  15807. /*
  15808. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15809. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15810. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15811. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15812. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15813. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15814. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15815. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15816. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15817. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15818. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15819. *
  15820. * This software consists of voluntary contributions made by many individuals
  15821. * and is licensed under the LGPL. For more information, see
  15822. * <http://www.doctrine-project.org>.
  15823. */
  15824. namespace Doctrine\DBAL\Types;
  15825. use Doctrine\DBAL\Platforms\AbstractPlatform;
  15826. /**
  15827. * Variable DateTime Type using date_create() instead of DateTime::createFromFormat()
  15828. *
  15829. * This type has performance implications as it runs twice as long as the regular
  15830. * {@see DateTimeType}, however in certain PostgreSQL configurations with
  15831. * TIMESTAMP(n) columns where n > 0 it is necessary to use this type.
  15832. *
  15833. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15834. * @link www.doctrine-project.com
  15835. * @since 2.0
  15836. * @author Benjamin Eberlei <kontakt@beberlei.de>
  15837. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  15838. * @author Jonathan Wage <jonwage@gmail.com>
  15839. * @author Roman Borschel <roman@code-factory.org>
  15840. */
  15841. class VarDateTimeType extends DateTimeType
  15842. {
  15843. /**
  15844. * @throws ConversionException
  15845. * @param string $value
  15846. * @param AbstractPlatform $platform
  15847. * @return DateTime
  15848. */
  15849. public function convertToPHPValue($value, AbstractPlatform $platform)
  15850. {
  15851. if ($value === null) {
  15852. return null;
  15853. }
  15854. $val = date_create($value);
  15855. if (!$val) {
  15856. throw ConversionException::conversionFailed($value, $this->getName());
  15857. }
  15858. return $val;
  15859. }
  15860. }<?php
  15861. /*
  15862. * $Id$
  15863. *
  15864. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15865. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15866. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15867. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15868. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15869. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15870. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15871. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15872. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15873. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15874. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15875. *
  15876. * This software consists of voluntary contributions made by many individuals
  15877. * and is licensed under the LGPL. For more information, see
  15878. * <http://www.doctrine-project.org>.
  15879. */
  15880. namespace Doctrine\DBAL\Types;
  15881. use Doctrine\DBAL\Platforms\AbstractPlatform;
  15882. /**
  15883. * Type that maps an SQL INT to a PHP integer.
  15884. *
  15885. * @author Roman Borschel <roman@code-factory.org>
  15886. * @since 2.0
  15887. */
  15888. class IntegerType extends Type
  15889. {
  15890. public function getName()
  15891. {
  15892. return Type::INTEGER;
  15893. }
  15894. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  15895. {
  15896. return $platform->getIntegerTypeDeclarationSQL($fieldDeclaration);
  15897. }
  15898. public function convertToPHPValue($value, AbstractPlatform $platform)
  15899. {
  15900. return (null === $value) ? null : (int) $value;
  15901. }
  15902. public function getBindingType()
  15903. {
  15904. return \PDO::PARAM_INT;
  15905. }
  15906. }<?php
  15907. /*
  15908. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15909. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15910. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15911. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15912. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15913. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15914. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15915. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15916. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15917. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15918. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15919. *
  15920. * This software consists of voluntary contributions made by many individuals
  15921. * and is licensed under the LGPL. For more information, see
  15922. * <http://www.doctrine-project.org>.
  15923. */
  15924. namespace Doctrine\DBAL\Types;
  15925. use Doctrine\DBAL\Platforms\AbstractPlatform;
  15926. /**
  15927. * Type that maps an SQL DATE to a PHP Date object.
  15928. *
  15929. * @since 2.0
  15930. */
  15931. class DateType extends Type
  15932. {
  15933. public function getName()
  15934. {
  15935. return Type::DATE;
  15936. }
  15937. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  15938. {
  15939. return $platform->getDateTypeDeclarationSQL($fieldDeclaration);
  15940. }
  15941. public function convertToDatabaseValue($value, AbstractPlatform $platform)
  15942. {
  15943. return ($value !== null)
  15944. ? $value->format($platform->getDateFormatString()) : null;
  15945. }
  15946. public function convertToPHPValue($value, AbstractPlatform $platform)
  15947. {
  15948. if ($value === null) {
  15949. return null;
  15950. }
  15951. $val = \DateTime::createFromFormat('!'.$platform->getDateFormatString(), $value);
  15952. if (!$val) {
  15953. throw ConversionException::conversionFailed($value, $this->getName());
  15954. }
  15955. return $val;
  15956. }
  15957. }<?php
  15958. /*
  15959. * $Id$
  15960. *
  15961. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15962. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15963. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15964. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  15965. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15966. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  15967. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15968. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15969. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15970. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15971. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15972. *
  15973. * This software consists of voluntary contributions made by many individuals
  15974. * and is licensed under the LGPL. For more information, see
  15975. * <http://www.doctrine-project.org>.
  15976. */
  15977. namespace Doctrine\DBAL\Types;
  15978. use Doctrine\DBAL\Platforms\AbstractPlatform,
  15979. Doctrine\DBAL\DBALException;
  15980. /**
  15981. * The base class for so-called Doctrine mapping types.
  15982. *
  15983. * A Type object is obtained by calling the static {@link getType()} method.
  15984. *
  15985. * @author Roman Borschel <roman@code-factory.org>
  15986. * @since 2.0
  15987. */
  15988. abstract class Type
  15989. {
  15990. const TARRAY = 'array';
  15991. const BIGINT = 'bigint';
  15992. const BOOLEAN = 'boolean';
  15993. const DATETIME = 'datetime';
  15994. const DATETIMETZ = 'datetimetz';
  15995. const DATE = 'date';
  15996. const TIME = 'time';
  15997. const DECIMAL = 'decimal';
  15998. const INTEGER = 'integer';
  15999. const OBJECT = 'object';
  16000. const SMALLINT = 'smallint';
  16001. const STRING = 'string';
  16002. const TEXT = 'text';
  16003. const FLOAT = 'float';
  16004. /** Map of already instantiated type objects. One instance per type (flyweight). */
  16005. private static $_typeObjects = array();
  16006. /** The map of supported doctrine mapping types. */
  16007. private static $_typesMap = array(
  16008. self::TARRAY => 'Doctrine\DBAL\Types\ArrayType',
  16009. self::OBJECT => 'Doctrine\DBAL\Types\ObjectType',
  16010. self::BOOLEAN => 'Doctrine\DBAL\Types\BooleanType',
  16011. self::INTEGER => 'Doctrine\DBAL\Types\IntegerType',
  16012. self::SMALLINT => 'Doctrine\DBAL\Types\SmallIntType',
  16013. self::BIGINT => 'Doctrine\DBAL\Types\BigIntType',
  16014. self::STRING => 'Doctrine\DBAL\Types\StringType',
  16015. self::TEXT => 'Doctrine\DBAL\Types\TextType',
  16016. self::DATETIME => 'Doctrine\DBAL\Types\DateTimeType',
  16017. self::DATETIMETZ => 'Doctrine\DBAL\Types\DateTimeTzType',
  16018. self::DATE => 'Doctrine\DBAL\Types\DateType',
  16019. self::TIME => 'Doctrine\DBAL\Types\TimeType',
  16020. self::DECIMAL => 'Doctrine\DBAL\Types\DecimalType',
  16021. self::FLOAT => 'Doctrine\DBAL\Types\FloatType',
  16022. );
  16023. /* Prevent instantiation and force use of the factory method. */
  16024. final private function __construct() {}
  16025. /**
  16026. * Converts a value from its PHP representation to its database representation
  16027. * of this type.
  16028. *
  16029. * @param mixed $value The value to convert.
  16030. * @param AbstractPlatform $platform The currently used database platform.
  16031. * @return mixed The database representation of the value.
  16032. */
  16033. public function convertToDatabaseValue($value, AbstractPlatform $platform)
  16034. {
  16035. return $value;
  16036. }
  16037. /**
  16038. * Converts a value from its database representation to its PHP representation
  16039. * of this type.
  16040. *
  16041. * @param mixed $value The value to convert.
  16042. * @param AbstractPlatform $platform The currently used database platform.
  16043. * @return mixed The PHP representation of the value.
  16044. */
  16045. public function convertToPHPValue($value, AbstractPlatform $platform)
  16046. {
  16047. return $value;
  16048. }
  16049. /**
  16050. * Gets the default length of this type.
  16051. *
  16052. * @todo Needed?
  16053. */
  16054. public function getDefaultLength(AbstractPlatform $platform)
  16055. {
  16056. return null;
  16057. }
  16058. /**
  16059. * Gets the SQL declaration snippet for a field of this type.
  16060. *
  16061. * @param array $fieldDeclaration The field declaration.
  16062. * @param AbstractPlatform $platform The currently used database platform.
  16063. */
  16064. abstract public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform);
  16065. /**
  16066. * Gets the name of this type.
  16067. *
  16068. * @return string
  16069. * @todo Needed?
  16070. */
  16071. abstract public function getName();
  16072. /**
  16073. * Factory method to create type instances.
  16074. * Type instances are implemented as flyweights.
  16075. *
  16076. * @static
  16077. * @throws DBALException
  16078. * @param string $name The name of the type (as returned by getName()).
  16079. * @return Doctrine\DBAL\Types\Type
  16080. */
  16081. public static function getType($name)
  16082. {
  16083. if ( ! isset(self::$_typeObjects[$name])) {
  16084. if ( ! isset(self::$_typesMap[$name])) {
  16085. throw DBALException::unknownColumnType($name);
  16086. }
  16087. self::$_typeObjects[$name] = new self::$_typesMap[$name]();
  16088. }
  16089. return self::$_typeObjects[$name];
  16090. }
  16091. /**
  16092. * Adds a custom type to the type map.
  16093. *
  16094. * @static
  16095. * @param string $name Name of the type. This should correspond to what getName() returns.
  16096. * @param string $className The class name of the custom type.
  16097. * @throws DBALException
  16098. */
  16099. public static function addType($name, $className)
  16100. {
  16101. if (isset(self::$_typesMap[$name])) {
  16102. throw DBALException::typeExists($name);
  16103. }
  16104. self::$_typesMap[$name] = $className;
  16105. }
  16106. /**
  16107. * Checks if exists support for a type.
  16108. *
  16109. * @static
  16110. * @param string $name Name of the type
  16111. * @return boolean TRUE if type is supported; FALSE otherwise
  16112. */
  16113. public static function hasType($name)
  16114. {
  16115. return isset(self::$_typesMap[$name]);
  16116. }
  16117. /**
  16118. * Overrides an already defined type to use a different implementation.
  16119. *
  16120. * @static
  16121. * @param string $name
  16122. * @param string $className
  16123. * @throws DBALException
  16124. */
  16125. public static function overrideType($name, $className)
  16126. {
  16127. if ( ! isset(self::$_typesMap[$name])) {
  16128. throw DBALException::typeNotFound($name);
  16129. }
  16130. self::$_typesMap[$name] = $className;
  16131. }
  16132. /**
  16133. * Gets the (preferred) binding type for values of this type that
  16134. * can be used when binding parameters to prepared statements.
  16135. *
  16136. * This method should return one of the PDO::PARAM_* constants, that is, one of:
  16137. *
  16138. * PDO::PARAM_BOOL
  16139. * PDO::PARAM_NULL
  16140. * PDO::PARAM_INT
  16141. * PDO::PARAM_STR
  16142. * PDO::PARAM_LOB
  16143. *
  16144. * @return integer
  16145. */
  16146. public function getBindingType()
  16147. {
  16148. return \PDO::PARAM_STR;
  16149. }
  16150. /**
  16151. * Get the types array map which holds all registered types and the corresponding
  16152. * type class
  16153. *
  16154. * @return array $typesMap
  16155. */
  16156. public static function getTypesMap()
  16157. {
  16158. return self::$_typesMap;
  16159. }
  16160. public function __toString()
  16161. {
  16162. $e = explode('\\', get_class($this));
  16163. return str_replace('Type', '', end($e));
  16164. }
  16165. }<?php
  16166. /*
  16167. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16168. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16169. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16170. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16171. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16172. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16173. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16174. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16175. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16176. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16177. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16178. *
  16179. * This software consists of voluntary contributions made by many individuals
  16180. * and is licensed under the LGPL. For more information, see
  16181. * <http://www.doctrine-project.org>.
  16182. */
  16183. namespace Doctrine\DBAL\Types;
  16184. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16185. class FloatType extends Type
  16186. {
  16187. public function getName()
  16188. {
  16189. return Type::FLOAT;
  16190. }
  16191. /**
  16192. * @param array $fieldDeclaration
  16193. * @param AbstractPlatform $platform
  16194. * @return string
  16195. */
  16196. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16197. {
  16198. return $platform->getFloatDeclarationSQL($fieldDeclaration);
  16199. }
  16200. /**
  16201. * Converts a value from its database representation to its PHP representation
  16202. * of this type.
  16203. *
  16204. * @param mixed $value The value to convert.
  16205. * @param AbstractPlatform $platform The currently used database platform.
  16206. * @return mixed The PHP representation of the value.
  16207. */
  16208. public function convertToPHPValue($value, AbstractPlatform $platform)
  16209. {
  16210. return (null === $value) ? null : (double) $value;
  16211. }
  16212. }
  16213. <?php
  16214. /*
  16215. * $Id$
  16216. *
  16217. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16218. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16219. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16220. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16221. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16222. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16223. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16224. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16225. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16226. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16227. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16228. *
  16229. * This software consists of voluntary contributions made by many individuals
  16230. * and is licensed under the LGPL. For more information, see
  16231. * <http://www.doctrine-project.org>.
  16232. */
  16233. namespace Doctrine\DBAL\Types;
  16234. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16235. /**
  16236. * Type that maps a database SMALLINT to a PHP integer.
  16237. *
  16238. * @author robo
  16239. */
  16240. class SmallIntType extends Type
  16241. {
  16242. public function getName()
  16243. {
  16244. return Type::SMALLINT;
  16245. }
  16246. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16247. {
  16248. return $platform->getSmallIntTypeDeclarationSQL($fieldDeclaration);
  16249. }
  16250. public function convertToPHPValue($value, AbstractPlatform $platform)
  16251. {
  16252. return (null === $value) ? null : (int) $value;
  16253. }
  16254. public function getBindingType()
  16255. {
  16256. return \PDO::PARAM_INT;
  16257. }
  16258. }<?php
  16259. /*
  16260. * $Id$
  16261. *
  16262. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16263. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16264. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16265. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16266. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16267. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16268. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16269. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16270. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16271. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16272. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16273. *
  16274. * This software consists of voluntary contributions made by many individuals
  16275. * and is licensed under the LGPL. For more information, see
  16276. * <http://www.doctrine-project.org>.
  16277. */
  16278. namespace Doctrine\DBAL\Types;
  16279. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16280. /**
  16281. * Type that maps a database BIGINT to a PHP string.
  16282. *
  16283. * @author robo
  16284. * @since 2.0
  16285. */
  16286. class BigIntType extends Type
  16287. {
  16288. public function getName()
  16289. {
  16290. return Type::BIGINT;
  16291. }
  16292. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16293. {
  16294. return $platform->getBigIntTypeDeclarationSQL($fieldDeclaration);
  16295. }
  16296. public function getBindingType()
  16297. {
  16298. return \PDO::PARAM_INT;
  16299. }
  16300. }<?php
  16301. /*
  16302. * $Id$
  16303. *
  16304. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16305. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16306. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16307. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16308. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16309. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16310. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16311. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16312. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16313. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16314. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16315. *
  16316. * This software consists of voluntary contributions made by many individuals
  16317. * and is licensed under the LGPL. For more information, see
  16318. * <http://www.doctrine-project.org>.
  16319. */
  16320. namespace Doctrine\DBAL\Types;
  16321. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16322. /**
  16323. * Type that maps an SQL VARCHAR to a PHP string.
  16324. *
  16325. * @since 2.0
  16326. */
  16327. class StringType extends Type
  16328. {
  16329. /** @override */
  16330. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16331. {
  16332. return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
  16333. }
  16334. /** @override */
  16335. public function getDefaultLength(AbstractPlatform $platform)
  16336. {
  16337. return $platform->getVarcharDefaultLength();
  16338. }
  16339. /** @override */
  16340. public function getName()
  16341. {
  16342. return Type::STRING;
  16343. }
  16344. }<?php
  16345. namespace Doctrine\DBAL\Types;
  16346. /**
  16347. * Type that maps a PHP object to a clob SQL type.
  16348. *
  16349. * @since 2.0
  16350. */
  16351. class ObjectType extends Type
  16352. {
  16353. public function getSQLDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  16354. {
  16355. return $platform->getClobTypeDeclarationSQL($fieldDeclaration);
  16356. }
  16357. public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  16358. {
  16359. return serialize($value);
  16360. }
  16361. public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  16362. {
  16363. if ($value === null) {
  16364. return null;
  16365. }
  16366. $value = (is_resource($value)) ? stream_get_contents($value) : $value;
  16367. $val = unserialize($value);
  16368. if ($val === false) {
  16369. throw ConversionException::conversionFailed($value, $this->getName());
  16370. }
  16371. return $val;
  16372. }
  16373. public function getName()
  16374. {
  16375. return Type::OBJECT;
  16376. }
  16377. }<?php
  16378. /*
  16379. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16380. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16381. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16382. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16383. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16384. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16385. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16386. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16387. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16388. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16389. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16390. *
  16391. * This software consists of voluntary contributions made by many individuals
  16392. * and is licensed under the LGPL. For more information, see
  16393. * <http://www.doctrine-project.org>.
  16394. */
  16395. namespace Doctrine\DBAL\Types;
  16396. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16397. /**
  16398. * DateTime type saving additional timezone information.
  16399. *
  16400. * Caution: Databases are not necessarily experts at storing timezone related
  16401. * data of dates. First, of all the supported vendors only PostgreSQL and Oracle
  16402. * support storing Timezone data. But those two don't save the actual timezone
  16403. * attached to a DateTime instance (for example "Europe/Berlin" or "America/Montreal")
  16404. * but the current offset of them related to UTC. That means depending on daylight saving times
  16405. * or not you may get different offsets.
  16406. *
  16407. * This datatype makes only sense to use, if your application works with an offset, not
  16408. * with an actual timezone that uses transitions. Otherwise your DateTime instance
  16409. * attached with a timezone such as Europe/Berlin gets saved into the database with
  16410. * the offset and re-created from persistence with only the offset, not the original timezone
  16411. * attached.
  16412. *
  16413. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  16414. * @link www.doctrine-project.com
  16415. * @since 1.0
  16416. * @author Benjamin Eberlei <kontakt@beberlei.de>
  16417. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  16418. * @author Jonathan Wage <jonwage@gmail.com>
  16419. * @author Roman Borschel <roman@code-factory.org>
  16420. */
  16421. class DateTimeTzType extends Type
  16422. {
  16423. public function getName()
  16424. {
  16425. return Type::DATETIMETZ;
  16426. }
  16427. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16428. {
  16429. return $platform->getDateTimeTzTypeDeclarationSQL($fieldDeclaration);
  16430. }
  16431. public function convertToDatabaseValue($value, AbstractPlatform $platform)
  16432. {
  16433. return ($value !== null)
  16434. ? $value->format($platform->getDateTimeTzFormatString()) : null;
  16435. }
  16436. public function convertToPHPValue($value, AbstractPlatform $platform)
  16437. {
  16438. if ($value === null) {
  16439. return null;
  16440. }
  16441. $val = \DateTime::createFromFormat($platform->getDateTimeTzFormatString(), $value);
  16442. if (!$val) {
  16443. throw ConversionException::conversionFailed($value, $this->getName());
  16444. }
  16445. return $val;
  16446. }
  16447. }<?php
  16448. /*
  16449. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16450. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16451. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16452. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16453. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16454. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16455. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16456. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16457. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16458. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16459. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16460. *
  16461. * This software consists of voluntary contributions made by many individuals
  16462. * and is licensed under the LGPL. For more information, see
  16463. * <http://www.doctrine-project.org>.
  16464. */
  16465. /**
  16466. * Conversion Exception is thrown when the database to PHP conversion fails
  16467. *
  16468. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  16469. * @link www.doctrine-project.com
  16470. * @since 2.0
  16471. * @author Benjamin Eberlei <kontakt@beberlei.de>
  16472. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  16473. * @author Jonathan Wage <jonwage@gmail.com>
  16474. * @author Roman Borschel <roman@code-factory.org>
  16475. */
  16476. namespace Doctrine\DBAL\Types;
  16477. class ConversionException extends \Doctrine\DBAL\DBALException
  16478. {
  16479. /**
  16480. * Thrown when a Database to Doctrine Type Conversion fails.
  16481. *
  16482. * @param string $value
  16483. * @param string $toType
  16484. * @return ConversionException
  16485. */
  16486. static public function conversionFailed($value, $toType)
  16487. {
  16488. $value = (strlen($value) > 32) ? substr($value, 0, 20) . "..." : $value;
  16489. return new self('Could not convert database value "' . $value . '" to Doctrine Type ' . $toType);
  16490. }
  16491. }<?php
  16492. /*
  16493. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16494. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16495. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16496. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16497. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16498. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16499. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16500. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16501. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16502. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16503. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16504. *
  16505. * This software consists of voluntary contributions made by many individuals
  16506. * and is licensed under the LGPL. For more information, see
  16507. * <http://www.doctrine-project.org>.
  16508. */
  16509. namespace Doctrine\DBAL\Types;
  16510. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16511. /**
  16512. * Type that maps an SQL DATETIME/TIMESTAMP to a PHP DateTime object.
  16513. *
  16514. * @since 2.0
  16515. */
  16516. class DateTimeType extends Type
  16517. {
  16518. public function getName()
  16519. {
  16520. return Type::DATETIME;
  16521. }
  16522. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16523. {
  16524. return $platform->getDateTimeTypeDeclarationSQL($fieldDeclaration);
  16525. }
  16526. public function convertToDatabaseValue($value, AbstractPlatform $platform)
  16527. {
  16528. return ($value !== null)
  16529. ? $value->format($platform->getDateTimeFormatString()) : null;
  16530. }
  16531. public function convertToPHPValue($value, AbstractPlatform $platform)
  16532. {
  16533. if ($value === null) {
  16534. return null;
  16535. }
  16536. $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
  16537. if (!$val) {
  16538. throw ConversionException::conversionFailed($value, $this->getName());
  16539. }
  16540. return $val;
  16541. }
  16542. }<?php
  16543. /*
  16544. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16545. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16546. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16547. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16548. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16549. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16550. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16551. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16552. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16553. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16554. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16555. *
  16556. * This software consists of voluntary contributions made by many individuals
  16557. * and is licensed under the LGPL. For more information, see
  16558. * <http://www.doctrine-project.org>.
  16559. */
  16560. namespace Doctrine\DBAL\Types;
  16561. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16562. /**
  16563. * Type that maps an SQL DECIMAL to a PHP double.
  16564. *
  16565. * @since 2.0
  16566. */
  16567. class DecimalType extends Type
  16568. {
  16569. public function getName()
  16570. {
  16571. return Type::DECIMAL;
  16572. }
  16573. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16574. {
  16575. return $platform->getDecimalTypeDeclarationSQL($fieldDeclaration);
  16576. }
  16577. public function convertToPHPValue($value, AbstractPlatform $platform)
  16578. {
  16579. return (null === $value) ? null : (double) $value;
  16580. }
  16581. }<?php
  16582. /*
  16583. * $Id$
  16584. *
  16585. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16586. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16587. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16588. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16589. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16590. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16591. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16592. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16593. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16594. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16595. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16596. *
  16597. * This software consists of voluntary contributions made by many individuals
  16598. * and is licensed under the LGPL. For more information, see
  16599. * <http://www.doctrine-project.org>.
  16600. */
  16601. namespace Doctrine\DBAL\Types;
  16602. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16603. /**
  16604. * Type that maps an SQL CLOB to a PHP string.
  16605. *
  16606. * @since 2.0
  16607. */
  16608. class TextType extends Type
  16609. {
  16610. /** @override */
  16611. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16612. {
  16613. return $platform->getClobTypeDeclarationSQL($fieldDeclaration);
  16614. }
  16615. /**
  16616. * Converts a value from its database representation to its PHP representation
  16617. * of this type.
  16618. *
  16619. * @param mixed $value The value to convert.
  16620. * @param AbstractPlatform $platform The currently used database platform.
  16621. * @return mixed The PHP representation of the value.
  16622. */
  16623. public function convertToPHPValue($value, AbstractPlatform $platform)
  16624. {
  16625. return (is_resource($value)) ? stream_get_contents($value) : $value;
  16626. }
  16627. public function getName()
  16628. {
  16629. return Type::TEXT;
  16630. }
  16631. }<?php
  16632. /*
  16633. * $Id$
  16634. *
  16635. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16636. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16637. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16638. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16639. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16640. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16641. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16642. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16643. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16644. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16645. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16646. *
  16647. * This software consists of voluntary contributions made by many individuals
  16648. * and is licensed under the LGPL. For more information, see
  16649. * <http://www.doctrine-project.org>.
  16650. */
  16651. namespace Doctrine\DBAL\Types;
  16652. /**
  16653. * Type that maps a PHP array to a clob SQL type.
  16654. *
  16655. * @since 2.0
  16656. */
  16657. class ArrayType extends Type
  16658. {
  16659. public function getSQLDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  16660. {
  16661. return $platform->getClobTypeDeclarationSQL($fieldDeclaration);
  16662. }
  16663. public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  16664. {
  16665. return serialize($value);
  16666. }
  16667. public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  16668. {
  16669. if ($value === null) {
  16670. return null;
  16671. }
  16672. $value = (is_resource($value)) ? stream_get_contents($value) : $value;
  16673. $val = unserialize($value);
  16674. if ($val === false) {
  16675. throw ConversionException::conversionFailed($value, $this->getName());
  16676. }
  16677. return $val;
  16678. }
  16679. public function getName()
  16680. {
  16681. return Type::TARRAY;
  16682. }
  16683. }<?php
  16684. /*
  16685. * $Id$
  16686. *
  16687. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16688. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16689. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16690. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16691. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16692. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16693. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16694. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16695. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16696. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16697. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16698. *
  16699. * This software consists of voluntary contributions made by many individuals
  16700. * and is licensed under the LGPL. For more information, see
  16701. * <http://www.doctrine-project.org>.
  16702. */
  16703. namespace Doctrine\DBAL\Types;
  16704. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16705. /**
  16706. * Type that maps an SQL boolean to a PHP boolean.
  16707. *
  16708. * @since 2.0
  16709. */
  16710. class BooleanType extends Type
  16711. {
  16712. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16713. {
  16714. return $platform->getBooleanTypeDeclarationSQL($fieldDeclaration);
  16715. }
  16716. public function convertToDatabaseValue($value, AbstractPlatform $platform)
  16717. {
  16718. return $platform->convertBooleans($value);
  16719. }
  16720. public function convertToPHPValue($value, AbstractPlatform $platform)
  16721. {
  16722. return (null === $value) ? null : (bool) $value;
  16723. }
  16724. public function getName()
  16725. {
  16726. return Type::BOOLEAN;
  16727. }
  16728. public function getBindingType()
  16729. {
  16730. return \PDO::PARAM_BOOL;
  16731. }
  16732. }<?php
  16733. /*
  16734. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16735. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16736. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16737. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16738. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16739. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16740. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16741. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16742. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16743. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16744. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16745. *
  16746. * This software consists of voluntary contributions made by many individuals
  16747. * and is licensed under the LGPL. For more information, see
  16748. * <http://www.doctrine-project.org>.
  16749. */
  16750. namespace Doctrine\DBAL\Types;
  16751. use Doctrine\DBAL\Platforms\AbstractPlatform;
  16752. /**
  16753. * Type that maps an SQL TIME to a PHP DateTime object.
  16754. *
  16755. * @since 2.0
  16756. */
  16757. class TimeType extends Type
  16758. {
  16759. public function getName()
  16760. {
  16761. return Type::TIME;
  16762. }
  16763. /**
  16764. * {@inheritdoc}
  16765. */
  16766. public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
  16767. {
  16768. return $platform->getTimeTypeDeclarationSQL($fieldDeclaration);
  16769. }
  16770. /**
  16771. * {@inheritdoc}
  16772. */
  16773. public function convertToDatabaseValue($value, AbstractPlatform $platform)
  16774. {
  16775. return ($value !== null)
  16776. ? $value->format($platform->getTimeFormatString()) : null;
  16777. }
  16778. /**
  16779. * {@inheritdoc}
  16780. */
  16781. public function convertToPHPValue($value, AbstractPlatform $platform)
  16782. {
  16783. if ($value === null) {
  16784. return null;
  16785. }
  16786. $val = \DateTime::createFromFormat($platform->getTimeFormatString(), $value);
  16787. if (!$val) {
  16788. throw ConversionException::conversionFailed($value, $this->getName());
  16789. }
  16790. return $val;
  16791. }
  16792. }<?php
  16793. /*
  16794. * $Id$
  16795. *
  16796. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16797. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16798. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16799. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16800. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16801. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16802. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16803. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16804. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16805. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16806. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16807. *
  16808. * This software consists of voluntary contributions made by many individuals
  16809. * and is licensed under the LGPL. For more information, see
  16810. * <http://www.doctrine-project.org>.
  16811. */
  16812. namespace Doctrine\DBAL\Event;
  16813. use Doctrine\Common\EventArgs,
  16814. Doctrine\DBAL\Connection;
  16815. /**
  16816. * Event Arguments used when a Driver connection is established inside Doctrine\DBAL\Connection.
  16817. *
  16818. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  16819. * @link www.doctrine-project.com
  16820. * @since 1.0
  16821. * @version $Revision$
  16822. * @author Benjamin Eberlei <kontakt@beberlei.de>
  16823. */
  16824. class ConnectionEventArgs extends EventArgs
  16825. {
  16826. /**
  16827. * @var Connection
  16828. */
  16829. private $_connection = null;
  16830. public function __construct(Connection $connection)
  16831. {
  16832. $this->_connection = $connection;
  16833. }
  16834. /**
  16835. * @return Doctrine\DBAL\Connection
  16836. */
  16837. public function getConnection()
  16838. {
  16839. return $this->_connection;
  16840. }
  16841. /**
  16842. * @return Doctrine\DBAL\Driver
  16843. */
  16844. public function getDriver()
  16845. {
  16846. return $this->_connection->getDriver();
  16847. }
  16848. /**
  16849. * @return Doctrine\DBAL\Platforms\AbstractPlatform
  16850. */
  16851. public function getDatabasePlatform()
  16852. {
  16853. return $this->_connection->getDatabasePlatform();
  16854. }
  16855. /**
  16856. * @return Doctrine\DBAL\Schema\AbstractSchemaManager
  16857. */
  16858. public function getSchemaManager()
  16859. {
  16860. return $this->_connection->getSchemaManager();
  16861. }
  16862. }
  16863. <?php
  16864. /*
  16865. * $Id$
  16866. *
  16867. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16868. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16869. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16870. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16871. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16872. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16873. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16874. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16875. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16876. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16877. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16878. *
  16879. * This software consists of voluntary contributions made by many individuals
  16880. * and is licensed under the LGPL. For more information, see
  16881. * <http://www.doctrine-project.org>.
  16882. */
  16883. namespace Doctrine\DBAL\Event\Listeners;
  16884. use Doctrine\DBAL\Event\ConnectionEventArgs;
  16885. use Doctrine\DBAL\Events;
  16886. use Doctrine\Common\EventSubscriber;
  16887. /**
  16888. * MySQL Session Init Event Subscriber which allows to set the Client Encoding of the Connection
  16889. *
  16890. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  16891. * @link www.doctrine-project.com
  16892. * @since 1.0
  16893. * @version $Revision$
  16894. * @author Benjamin Eberlei <kontakt@beberlei.de>
  16895. */
  16896. class MysqlSessionInit implements EventSubscriber
  16897. {
  16898. /**
  16899. * @var string
  16900. */
  16901. private $_charset;
  16902. /**
  16903. * @var string
  16904. */
  16905. private $_collation;
  16906. /**
  16907. * Configure Charset and Collation options of MySQL Client for each Connection
  16908. *
  16909. * @param string $charset
  16910. * @param string $collation
  16911. */
  16912. public function __construct($charset = 'utf8', $collation = false)
  16913. {
  16914. $this->_charset = $charset;
  16915. $this->_collation = $collation;
  16916. }
  16917. /**
  16918. * @param ConnectionEventArgs $args
  16919. * @return void
  16920. */
  16921. public function postConnect(ConnectionEventArgs $args)
  16922. {
  16923. $collation = ($this->_collation) ? " COLLATE ".$this->_collation : "";
  16924. $args->getConnection()->executeUpdate("SET NAMES ".$this->_charset . $collation);
  16925. }
  16926. public function getSubscribedEvents()
  16927. {
  16928. return array(Events::postConnect);
  16929. }
  16930. }<?php
  16931. /*
  16932. * $Id$
  16933. *
  16934. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16935. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16936. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16937. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16938. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  16939. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  16940. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  16941. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  16942. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16943. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16944. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16945. *
  16946. * This software consists of voluntary contributions made by many individuals
  16947. * and is licensed under the LGPL. For more information, see
  16948. * <http://www.doctrine-project.org>.
  16949. */
  16950. namespace Doctrine\DBAL\Event\Listeners;
  16951. use Doctrine\DBAL\Event\ConnectionEventArgs;
  16952. use Doctrine\DBAL\Events;
  16953. use Doctrine\Common\EventSubscriber;
  16954. /**
  16955. * Should be used when Oracle Server default enviroment does not match the Doctrine requirements.
  16956. *
  16957. * The following enviroment variables are required for the Doctrine default date format:
  16958. *
  16959. * NLS_TIME_FORMAT="HH24:MI:SS"
  16960. * NLS_DATE_FORMAT="YYYY-MM-DD"
  16961. * NLS_TIMESTAMP_FORMAT="YYYY-MM-DD HH24:MI:SS"
  16962. * NLS_TIMESTAMP_TZ_FORMAT="YYYY-MM-DD HH24:MI:SS TZH:TZM"
  16963. *
  16964. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  16965. * @link www.doctrine-project.com
  16966. * @since 1.0
  16967. * @version $Revision$
  16968. * @author Benjamin Eberlei <kontakt@beberlei.de>
  16969. */
  16970. class OracleSessionInit implements EventSubscriber
  16971. {
  16972. protected $_defaultSessionVars = array(
  16973. 'NLS_TIME_FORMAT' => "HH24:MI:SS",
  16974. 'NLS_DATE_FORMAT' => "YYYY-MM-DD HH24:MI:SS",
  16975. 'NLS_TIMESTAMP_FORMAT' => "YYYY-MM-DD HH24:MI:SS",
  16976. 'NLS_TIMESTAMP_TZ_FORMAT' => "YYYY-MM-DD HH24:MI:SS TZH:TZM",
  16977. );
  16978. /**
  16979. * @param array $oracleSessionVars
  16980. */
  16981. public function __construct(array $oracleSessionVars = array())
  16982. {
  16983. $this->_defaultSessionVars = array_merge($this->_defaultSessionVars, $oracleSessionVars);
  16984. }
  16985. /**
  16986. * @param ConnectionEventArgs $args
  16987. * @return void
  16988. */
  16989. public function postConnect(ConnectionEventArgs $args)
  16990. {
  16991. if (count($this->_defaultSessionVars)) {
  16992. array_change_key_case($this->_defaultSessionVars, \CASE_UPPER);
  16993. $vars = array();
  16994. foreach ($this->_defaultSessionVars AS $option => $value) {
  16995. $vars[] = $option." = '".$value."'";
  16996. }
  16997. $sql = "ALTER SESSION SET ".implode(" ", $vars);
  16998. $args->getConnection()->executeUpdate($sql);
  16999. }
  17000. }
  17001. public function getSubscribedEvents()
  17002. {
  17003. return array(Events::postConnect);
  17004. }
  17005. }
  17006. <?php
  17007. /*
  17008. * $Id$
  17009. *
  17010. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17011. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17012. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17013. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17014. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17015. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  17016. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  17017. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  17018. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  17019. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  17020. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17021. *
  17022. * This software consists of voluntary contributions made by many individuals
  17023. * and is licensed under the LGPL. For more information, see
  17024. * <http://www.doctrine-project.org>.
  17025. */
  17026. namespace Doctrine\Common;
  17027. /**
  17028. * EventArgs is the base class for classes containing event data.
  17029. *
  17030. * This class contains no event data. It is used by events that do not pass state
  17031. * information to an event handler when an event is raised. The single empty EventArgs
  17032. * instance can be obtained through {@link getEmptyInstance}.
  17033. *
  17034. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  17035. * @link www.doctrine-project.org
  17036. * @since 2.0
  17037. * @version $Revision: 3938 $
  17038. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  17039. * @author Jonathan Wage <jonwage@gmail.com>
  17040. * @author Roman Borschel <roman@code-factory.org>
  17041. */
  17042. class EventArgs
  17043. {
  17044. /**
  17045. * @var EventArgs Single instance of EventArgs
  17046. * @static
  17047. */
  17048. private static $_emptyEventArgsInstance;
  17049. /**
  17050. * Gets the single, empty and immutable EventArgs instance.
  17051. *
  17052. * This instance will be used when events are dispatched without any parameter,
  17053. * like this: EventManager::dispatchEvent('eventname');
  17054. *
  17055. * The benefit from this is that only one empty instance is instantiated and shared
  17056. * (otherwise there would be instances for every dispatched in the abovementioned form)
  17057. *
  17058. * @see EventManager::dispatchEvent
  17059. * @link http://msdn.microsoft.com/en-us/library/system.eventargs.aspx
  17060. * @static
  17061. * @return EventArgs
  17062. */
  17063. public static function getEmptyInstance()
  17064. {
  17065. if ( ! self::$_emptyEventArgsInstance) {
  17066. self::$_emptyEventArgsInstance = new EventArgs;
  17067. }
  17068. return self::$_emptyEventArgsInstance;
  17069. }
  17070. }
  17071. <?php
  17072. /*
  17073. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17074. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17075. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17076. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17077. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17078. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  17079. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  17080. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  17081. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  17082. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  17083. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17084. *
  17085. * This software consists of voluntary contributions made by many individuals
  17086. * and is licensed under the LGPL. For more information, see
  17087. * <http://www.doctrine-project.org>.
  17088. */
  17089. namespace Doctrine\Common;
  17090. /**
  17091. * Base exception class for package Doctrine\Common
  17092. * @author heinrich
  17093. *
  17094. */
  17095. class CommonException extends \Exception {
  17096. }<?php
  17097. /*
  17098. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17099. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17100. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17101. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17102. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17103. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  17104. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  17105. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  17106. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  17107. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  17108. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17109. *
  17110. * This software consists of voluntary contributions made by many individuals
  17111. * and is licensed under the LGPL. For more information, see
  17112. * <http://www.doctrine-project.org>.
  17113. */
  17114. namespace Doctrine\Common\Annotations;
  17115. use Closure,
  17116. ReflectionClass,
  17117. ReflectionMethod,
  17118. ReflectionProperty,
  17119. Doctrine\Common\Cache\Cache;
  17120. /**
  17121. * A reader for docblock annotations.
  17122. *
  17123. * @since 2.0
  17124. * @author Benjamin Eberlei <kontakt@beberlei.de>
  17125. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  17126. * @author Jonathan Wage <jonwage@gmail.com>
  17127. * @author Roman Borschel <roman@code-factory.org>
  17128. */
  17129. class AnnotationReader
  17130. {
  17131. /**
  17132. * Cache salt
  17133. *
  17134. * @var string
  17135. * @static
  17136. */
  17137. private static $CACHE_SALT = '@<Annot>';
  17138. /**
  17139. * Annotations Parser
  17140. *
  17141. * @var Doctrine\Common\Annotations\Parser
  17142. */
  17143. private $parser;
  17144. /**
  17145. * Cache mechanism to store processed Annotations
  17146. *
  17147. * @var Doctrine\Common\Cache\Cache
  17148. */
  17149. private $cache;
  17150. /**
  17151. * Constructor. Initializes a new AnnotationReader that uses the given Cache provider.
  17152. *
  17153. * @param Cache $cache The cache provider to use. If none is provided, ArrayCache is used.
  17154. * @param Parser $parser The parser to use. If none is provided, the default parser is used.
  17155. */
  17156. public function __construct(Cache $cache = null, Parser $parser = null)
  17157. {
  17158. $this->parser = $parser ?: new Parser;
  17159. $this->cache = $cache ?: new \Doctrine\Common\Cache\ArrayCache;
  17160. }
  17161. /**
  17162. * Sets the default namespace that the AnnotationReader should assume for annotations
  17163. * with not fully qualified names.
  17164. *
  17165. * @param string $defaultNamespace
  17166. */
  17167. public function setDefaultAnnotationNamespace($defaultNamespace)
  17168. {
  17169. $this->parser->setDefaultAnnotationNamespace($defaultNamespace);
  17170. }
  17171. /**
  17172. * Sets the custom function to use for creating new annotations on the
  17173. * underlying parser.
  17174. *
  17175. * The function is supplied two arguments. The first argument is the name
  17176. * of the annotation and the second argument an array of values for this
  17177. * annotation. The function is assumed to return an object or NULL.
  17178. * Whenever the function returns NULL for an annotation, the implementation falls
  17179. * back to the default annotation creation process of the underlying parser.
  17180. *
  17181. * @param Closure $func
  17182. */
  17183. public function setAnnotationCreationFunction(Closure $func)
  17184. {
  17185. $this->parser->setAnnotationCreationFunction($func);
  17186. }
  17187. /**
  17188. * Sets an alias for an annotation namespace.
  17189. *
  17190. * @param $namespace
  17191. * @param $alias
  17192. */
  17193. public function setAnnotationNamespaceAlias($namespace, $alias)
  17194. {
  17195. $this->parser->setAnnotationNamespaceAlias($namespace, $alias);
  17196. }
  17197. /**
  17198. * Sets a flag whether to try to autoload annotation classes, as well as to distinguish
  17199. * between what is an annotation and what not by triggering autoloading.
  17200. *
  17201. * NOTE: Autoloading of annotation classes is inefficient and requires silently failing
  17202. * autoloaders. In particular, setting this option to TRUE renders this AnnotationReader
  17203. * incompatible with a {@link ClassLoader}.
  17204. * @param boolean $bool Boolean flag.
  17205. */
  17206. public function setAutoloadAnnotations($bool)
  17207. {
  17208. $this->parser->setAutoloadAnnotations($bool);
  17209. }
  17210. /**
  17211. * Gets a flag whether to try to autoload annotation classes.
  17212. *
  17213. * @see setAutoloadAnnotations
  17214. * @return boolean
  17215. */
  17216. public function getAutoloadAnnotations()
  17217. {
  17218. return $this->parser->getAutoloadAnnotations();
  17219. }
  17220. /**
  17221. * Gets the annotations applied to a class.
  17222. *
  17223. * @param string|ReflectionClass $class The name or ReflectionClass of the class from which
  17224. * the class annotations should be read.
  17225. * @return array An array of Annotations.
  17226. */
  17227. public function getClassAnnotations(ReflectionClass $class)
  17228. {
  17229. $cacheKey = $class->getName() . self::$CACHE_SALT;
  17230. // Attempt to grab data from cache
  17231. if (($data = $this->cache->fetch($cacheKey)) !== false) {
  17232. return $data;
  17233. }
  17234. $annotations = $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
  17235. $this->cache->save($cacheKey, $annotations, null);
  17236. return $annotations;
  17237. }
  17238. /**
  17239. * Gets a class annotation.
  17240. *
  17241. * @param $class
  17242. * @param string $annotation The name of the annotation.
  17243. * @return The Annotation or NULL, if the requested annotation does not exist.
  17244. */
  17245. public function getClassAnnotation(ReflectionClass $class, $annotation)
  17246. {
  17247. $annotations = $this->getClassAnnotations($class);
  17248. return isset($annotations[$annotation]) ? $annotations[$annotation] : null;
  17249. }
  17250. /**
  17251. * Gets the annotations applied to a property.
  17252. *
  17253. * @param string|ReflectionClass $class The name or ReflectionClass of the class that owns the property.
  17254. * @param string|ReflectionProperty $property The name or ReflectionProperty of the property
  17255. * from which the annotations should be read.
  17256. * @return array An array of Annotations.
  17257. */
  17258. public function getPropertyAnnotations(ReflectionProperty $property)
  17259. {
  17260. $cacheKey = $property->getDeclaringClass()->getName() . '$' . $property->getName() . self::$CACHE_SALT;
  17261. // Attempt to grab data from cache
  17262. if (($data = $this->cache->fetch($cacheKey)) !== false) {
  17263. return $data;
  17264. }
  17265. $context = 'property ' . $property->getDeclaringClass()->getName() . "::\$" . $property->getName();
  17266. $annotations = $this->parser->parse($property->getDocComment(), $context);
  17267. $this->cache->save($cacheKey, $annotations, null);
  17268. return $annotations;
  17269. }
  17270. /**
  17271. * Gets a property annotation.
  17272. *
  17273. * @param ReflectionProperty $property
  17274. * @param string $annotation The name of the annotation.
  17275. * @return The Annotation or NULL, if the requested annotation does not exist.
  17276. */
  17277. public function getPropertyAnnotation(ReflectionProperty $property, $annotation)
  17278. {
  17279. $annotations = $this->getPropertyAnnotations($property);
  17280. return isset($annotations[$annotation]) ? $annotations[$annotation] : null;
  17281. }
  17282. /**
  17283. * Gets the annotations applied to a method.
  17284. *
  17285. * @param string|ReflectionClass $class The name or ReflectionClass of the class that owns the method.
  17286. * @param string|ReflectionMethod $property The name or ReflectionMethod of the method from which
  17287. * the annotations should be read.
  17288. * @return array An array of Annotations.
  17289. */
  17290. public function getMethodAnnotations(ReflectionMethod $method)
  17291. {
  17292. $cacheKey = $method->getDeclaringClass()->getName() . '#' . $method->getName() . self::$CACHE_SALT;
  17293. // Attempt to grab data from cache
  17294. if (($data = $this->cache->fetch($cacheKey)) !== false) {
  17295. return $data;
  17296. }
  17297. $context = 'method ' . $method->getDeclaringClass()->getName() . '::' . $method->getName() . '()';
  17298. $annotations = $this->parser->parse($method->getDocComment(), $context);
  17299. $this->cache->save($cacheKey, $annotations, null);
  17300. return $annotations;
  17301. }
  17302. /**
  17303. * Gets a method annotation.
  17304. *
  17305. * @param ReflectionMethod $method
  17306. * @param string $annotation The name of the annotation.
  17307. * @return The Annotation or NULL, if the requested annotation does not exist.
  17308. */
  17309. public function getMethodAnnotation(ReflectionMethod $method, $annotation)
  17310. {
  17311. $annotations = $this->getMethodAnnotations($method);
  17312. return isset($annotations[$annotation]) ? $annotations[$annotation] : null;
  17313. }
  17314. }<?php
  17315. /*
  17316. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17317. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17318. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17319. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17320. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17321. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  17322. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  17323. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  17324. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  17325. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  17326. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17327. *
  17328. * This software consists of voluntary contributions made by many individuals
  17329. * and is licensed under the LGPL. For more information, see
  17330. * <http://www.doctrine-project.org>.
  17331. */
  17332. namespace Doctrine\Common\Annotations;
  17333. /**
  17334. * Description of AnnotationException
  17335. *
  17336. * @since 2.0
  17337. * @author Benjamin Eberlei <kontakt@beberlei.de>
  17338. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  17339. * @author Jonathan Wage <jonwage@gmail.com>
  17340. * @author Roman Borschel <roman@code-factory.org>
  17341. */
  17342. class AnnotationException extends \Exception
  17343. {
  17344. /**
  17345. * Creates a new AnnotationException describing a Syntax error.
  17346. *
  17347. * @param string $message Exception message
  17348. * @return AnnotationException
  17349. */
  17350. public static function syntaxError($message)
  17351. {
  17352. return new self('[Syntax Error] ' . $message);
  17353. }
  17354. /**
  17355. * Creates a new AnnotationException describing a Semantical error.
  17356. *
  17357. * @param string $message Exception message
  17358. * @return AnnotationException
  17359. */
  17360. public static function semanticalError($message)
  17361. {
  17362. return new self('[Semantical Error] ' . $message);
  17363. }
  17364. }<?php
  17365. /*
  17366. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17367. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17368. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17369. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17370. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17371. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  17372. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  17373. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  17374. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  17375. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  17376. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17377. *
  17378. * This software consists of voluntary contributions made by many individuals
  17379. * and is licensed under the LGPL. For more information, see
  17380. * <http://www.doctrine-project.org>.
  17381. */
  17382. namespace Doctrine\Common\Annotations;
  17383. use Closure, Doctrine\Common\ClassLoader;
  17384. /**
  17385. * A simple parser for docblock annotations.
  17386. *
  17387. * This Parser can be subclassed to customize certain aspects of the annotation
  17388. * parsing and/or creation process. Note though that currently no special care
  17389. * is taken to maintain full backwards compatibility for subclasses. Implementation
  17390. * details of the default Parser can change without explicit notice.
  17391. *
  17392. * @since 2.0
  17393. * @author Benjamin Eberlei <kontakt@beberlei.de>
  17394. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  17395. * @author Jonathan Wage <jonwage@gmail.com>
  17396. * @author Roman Borschel <roman@code-factory.org>
  17397. */
  17398. class Parser
  17399. {
  17400. /**
  17401. * Some common tags that are stripped prior to parsing in order to reduce parsing overhead.
  17402. *
  17403. * @var array
  17404. */
  17405. private static $strippedTags = array(
  17406. "{@internal", "{@inheritdoc", "{@link"
  17407. );
  17408. /**
  17409. * The lexer.
  17410. *
  17411. * @var Doctrine\Common\Annotations\Lexer
  17412. */
  17413. private $lexer;
  17414. /**
  17415. * Flag to control if the current annotation is nested or not.
  17416. *
  17417. * @var boolean
  17418. */
  17419. protected $isNestedAnnotation = false;
  17420. /**
  17421. * Default namespace for annotations.
  17422. *
  17423. * @var string
  17424. */
  17425. private $defaultAnnotationNamespace = '';
  17426. /**
  17427. * Hashmap to store namespace aliases.
  17428. *
  17429. * @var array
  17430. */
  17431. private $namespaceAliases = array();
  17432. /**
  17433. * @var string
  17434. */
  17435. private $context = '';
  17436. /**
  17437. * @var boolean Whether to try to autoload annotations that are not yet defined.
  17438. */
  17439. private $autoloadAnnotations = false;
  17440. /**
  17441. * @var Closure The custom function used to create new annotations, if any.
  17442. */
  17443. private $annotationCreationFunction;
  17444. /**
  17445. * Constructs a new AnnotationParser.
  17446. */
  17447. public function __construct(Lexer $lexer = null)
  17448. {
  17449. $this->lexer = $lexer ?: new Lexer;
  17450. }
  17451. /**
  17452. * Gets the lexer used by this parser.
  17453. *
  17454. * @return Lexer The lexer.
  17455. */
  17456. public function getLexer()
  17457. {
  17458. return $this->lexer;
  17459. }
  17460. /**
  17461. * Sets a flag whether to try to autoload annotation classes, as well as to distinguish
  17462. * between what is an annotation and what not by triggering autoloading.
  17463. *
  17464. * NOTE: Autoloading of annotation classes is inefficient and requires silently failing
  17465. * autoloaders. In particular, setting this option to TRUE renders the Parser
  17466. * incompatible with a {@link ClassLoader}.
  17467. * @param boolean $bool Boolean flag.
  17468. */
  17469. public function setAutoloadAnnotations($bool)
  17470. {
  17471. $this->autoloadAnnotations = $bool;
  17472. }
  17473. /**
  17474. * Sets the custom function to use for creating new annotations.
  17475. *
  17476. * The function is supplied two arguments. The first argument is the name
  17477. * of the annotation and the second argument an array of values for this
  17478. * annotation. The function is assumed to return an object or NULL.
  17479. * Whenever the function returns NULL for an annotation, the parser falls
  17480. * back to the default annotation creation process.
  17481. *
  17482. * Whenever the function returns NULL for an annotation, the implementation falls
  17483. * back to the default annotation creation process.
  17484. *
  17485. * @param Closure $func
  17486. */
  17487. public function setAnnotationCreationFunction(Closure $func)
  17488. {
  17489. $this->annotationCreationFunction = $func;
  17490. }
  17491. /**
  17492. * Gets a flag whether to try to autoload annotation classes.
  17493. *
  17494. * @see setAutoloadAnnotations
  17495. * @return boolean
  17496. */
  17497. public function getAutoloadAnnotations()
  17498. {
  17499. return $this->autoloadAnnotations;
  17500. }
  17501. /**
  17502. * Sets the default namespace that is assumed for an annotation that does not
  17503. * define a namespace prefix.
  17504. *
  17505. * @param string $defaultNamespace
  17506. */
  17507. public function setDefaultAnnotationNamespace($defaultNamespace)
  17508. {
  17509. $this->defaultAnnotationNamespace = $defaultNamespace;
  17510. }
  17511. /**
  17512. * Sets an alias for an annotation namespace.
  17513. *
  17514. * @param string $namespace
  17515. * @param string $alias
  17516. */
  17517. public function setAnnotationNamespaceAlias($namespace, $alias)
  17518. {
  17519. $this->namespaceAliases[$alias] = $namespace;
  17520. }
  17521. /**
  17522. * Gets the namespace alias mappings used by this parser.
  17523. *
  17524. * @return array The namespace alias mappings.
  17525. */
  17526. public function getNamespaceAliases()
  17527. {
  17528. return $this->namespaceAliases;
  17529. }
  17530. /**
  17531. * Parses the given docblock string for annotations.
  17532. *
  17533. * @param string $docBlockString The docblock string to parse.
  17534. * @param string $context The parsing context.
  17535. * @return array Array of annotations. If no annotations are found, an empty array is returned.
  17536. */
  17537. public function parse($docBlockString, $context='')
  17538. {
  17539. $this->context = $context;
  17540. // Strip out some known inline tags.
  17541. $input = str_replace(self::$strippedTags, '', $docBlockString);
  17542. // Cut of the beginning of the input until the first '@'.
  17543. $input = substr($input, strpos($input, '@'));
  17544. $this->lexer->reset();
  17545. $this->lexer->setInput(trim($input, '* /'));
  17546. $this->lexer->moveNext();
  17547. if ($this->lexer->isNextToken(Lexer::T_AT)) {
  17548. return $this->Annotations();
  17549. }
  17550. return array();
  17551. }
  17552. /**
  17553. * Attempts to match the given token with the current lookahead token.
  17554. * If they match, updates the lookahead token; otherwise raises a syntax error.
  17555. *
  17556. * @param int Token type.
  17557. * @return bool True if tokens match; false otherwise.
  17558. */
  17559. public function match($token)
  17560. {
  17561. if ( ! ($this->lexer->lookahead['type'] === $token)) {
  17562. $this->syntaxError($this->lexer->getLiteral($token));
  17563. }
  17564. $this->lexer->moveNext();
  17565. }
  17566. /**
  17567. * Generates a new syntax error.
  17568. *
  17569. * @param string $expected Expected string.
  17570. * @param array $token Optional token.
  17571. * @throws AnnotationException
  17572. */
  17573. private function syntaxError($expected, $token = null)
  17574. {
  17575. if ($token === null) {
  17576. $token = $this->lexer->lookahead;
  17577. }
  17578. $message = "Expected {$expected}, got ";
  17579. if ($this->lexer->lookahead === null) {
  17580. $message .= 'end of string';
  17581. } else {
  17582. $message .= "'{$token['value']}' at position {$token['position']}";
  17583. }
  17584. if (strlen($this->context)) {
  17585. $message .= ' in ' . $this->context;
  17586. }
  17587. $message .= '.';
  17588. throw AnnotationException::syntaxError($message);
  17589. }
  17590. /**
  17591. * Annotations ::= Annotation {[ "*" ]* [Annotation]}*
  17592. *
  17593. * @return array
  17594. */
  17595. public function Annotations()
  17596. {
  17597. $this->isNestedAnnotation = false;
  17598. $annotations = array();
  17599. $annot = $this->Annotation();
  17600. if ($annot !== false) {
  17601. $annotations[get_class($annot)] = $annot;
  17602. $this->lexer->skipUntil(Lexer::T_AT);
  17603. }
  17604. while ($this->lexer->lookahead !== null && $this->lexer->isNextToken(Lexer::T_AT)) {
  17605. $this->isNestedAnnotation = false;
  17606. $annot = $this->Annotation();
  17607. if ($annot !== false) {
  17608. $annotations[get_class($annot)] = $annot;
  17609. $this->lexer->skipUntil(Lexer::T_AT);
  17610. }
  17611. }
  17612. return $annotations;
  17613. }
  17614. /**
  17615. * Annotation ::= "@" AnnotationName ["(" [Values] ")"]
  17616. * AnnotationName ::= QualifiedName | SimpleName | AliasedName
  17617. * QualifiedName ::= NameSpacePart "\" {NameSpacePart "\"}* SimpleName
  17618. * AliasedName ::= Alias ":" SimpleName
  17619. * NameSpacePart ::= identifier
  17620. * SimpleName ::= identifier
  17621. * Alias ::= identifier
  17622. *
  17623. * @return mixed False if it is not a valid annotation.
  17624. */
  17625. public function Annotation()
  17626. {
  17627. $values = array();
  17628. $nameParts = array();
  17629. $this->match(Lexer::T_AT);
  17630. $this->match(Lexer::T_IDENTIFIER);
  17631. $nameParts[] = $this->lexer->token['value'];
  17632. while ($this->lexer->isNextToken(Lexer::T_NAMESPACE_SEPARATOR)) {
  17633. $this->match(Lexer::T_NAMESPACE_SEPARATOR);
  17634. $this->match(Lexer::T_IDENTIFIER);
  17635. $nameParts[] = $this->lexer->token['value'];
  17636. }
  17637. // Effectively pick the name of the class (append default NS if none, grab from NS alias, etc)
  17638. if (strpos($nameParts[0], ':')) {
  17639. list ($alias, $nameParts[0]) = explode(':', $nameParts[0]);
  17640. // If the namespace alias doesnt exist, skip until next annotation
  17641. if ( ! isset($this->namespaceAliases[$alias])) {
  17642. $this->lexer->skipUntil(Lexer::T_AT);
  17643. return false;
  17644. }
  17645. $name = $this->namespaceAliases[$alias] . implode('\\', $nameParts);
  17646. } else if (count($nameParts) == 1) {
  17647. $name = $this->defaultAnnotationNamespace . $nameParts[0];
  17648. } else {
  17649. $name = implode('\\', $nameParts);
  17650. }
  17651. // Does the annotation class exist?
  17652. if ( ! class_exists($name, $this->autoloadAnnotations)) {
  17653. $this->lexer->skipUntil(Lexer::T_AT);
  17654. return false;
  17655. }
  17656. // Next will be nested
  17657. $this->isNestedAnnotation = true;
  17658. if ($this->lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS)) {
  17659. $this->match(Lexer::T_OPEN_PARENTHESIS);
  17660. if ( ! $this->lexer->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) {
  17661. $values = $this->Values();
  17662. }
  17663. $this->match(Lexer::T_CLOSE_PARENTHESIS);
  17664. }
  17665. if ($this->annotationCreationFunction !== null) {
  17666. $func = $this->annotationCreationFunction;
  17667. $annot = $func($name, $values);
  17668. }
  17669. return isset($annot) ? $annot : $this->newAnnotation($name, $values);
  17670. }
  17671. /**
  17672. * Values ::= Array | Value {"," Value}*
  17673. *
  17674. * @return array
  17675. */
  17676. public function Values()
  17677. {
  17678. $values = array();
  17679. // Handle the case of a single array as value, i.e. @Foo({....})
  17680. if ($this->lexer->isNextToken(Lexer::T_OPEN_CURLY_BRACES)) {
  17681. $values['value'] = $this->Value();
  17682. return $values;
  17683. }
  17684. $values[] = $this->Value();
  17685. while ($this->lexer->isNextToken(Lexer::T_COMMA)) {
  17686. $this->match(Lexer::T_COMMA);
  17687. $value = $this->Value();
  17688. if ( ! is_array($value)) {
  17689. $this->syntaxError('Value', $value);
  17690. }
  17691. $values[] = $value;
  17692. }
  17693. foreach ($values as $k => $value) {
  17694. if (is_array($value) && is_string(key($value))) {
  17695. $key = key($value);
  17696. $values[$key] = $value[$key];
  17697. } else {
  17698. $values['value'] = $value;
  17699. }
  17700. unset($values[$k]);
  17701. }
  17702. return $values;
  17703. }
  17704. /**
  17705. * Value ::= PlainValue | FieldAssignment
  17706. *
  17707. * @return mixed
  17708. */
  17709. public function Value()
  17710. {
  17711. $peek = $this->lexer->glimpse();
  17712. if ($peek['value'] === '=') {
  17713. return $this->FieldAssignment();
  17714. }
  17715. return $this->PlainValue();
  17716. }
  17717. /**
  17718. * PlainValue ::= integer | string | float | boolean | Array | Annotation
  17719. *
  17720. * @return mixed
  17721. */
  17722. public function PlainValue()
  17723. {
  17724. if ($this->lexer->isNextToken(Lexer::T_OPEN_CURLY_BRACES)) {
  17725. return $this->Arrayx();
  17726. }
  17727. if ($this->lexer->isNextToken(Lexer::T_AT)) {
  17728. return $this->Annotation();
  17729. }
  17730. switch ($this->lexer->lookahead['type']) {
  17731. case Lexer::T_STRING:
  17732. $this->match(Lexer::T_STRING);
  17733. return $this->lexer->token['value'];
  17734. case Lexer::T_INTEGER:
  17735. $this->match(Lexer::T_INTEGER);
  17736. return $this->lexer->token['value'];
  17737. case Lexer::T_FLOAT:
  17738. $this->match(Lexer::T_FLOAT);
  17739. return $this->lexer->token['value'];
  17740. case Lexer::T_TRUE:
  17741. $this->match(Lexer::T_TRUE);
  17742. return true;
  17743. case Lexer::T_FALSE:
  17744. $this->match(Lexer::T_FALSE);
  17745. return false;
  17746. default:
  17747. $this->syntaxError('PlainValue');
  17748. }
  17749. }
  17750. /**
  17751. * FieldAssignment ::= FieldName "=" PlainValue
  17752. * FieldName ::= identifier
  17753. *
  17754. * @return array
  17755. */
  17756. public function FieldAssignment()
  17757. {
  17758. $this->match(Lexer::T_IDENTIFIER);
  17759. $fieldName = $this->lexer->token['value'];
  17760. $this->match(Lexer::T_EQUALS);
  17761. return array($fieldName => $this->PlainValue());
  17762. }
  17763. /**
  17764. * Array ::= "{" ArrayEntry {"," ArrayEntry}* "}"
  17765. *
  17766. * @return array
  17767. */
  17768. public function Arrayx()
  17769. {
  17770. $array = $values = array();
  17771. $this->match(Lexer::T_OPEN_CURLY_BRACES);
  17772. $values[] = $this->ArrayEntry();
  17773. while ($this->lexer->isNextToken(Lexer::T_COMMA)) {
  17774. $this->match(Lexer::T_COMMA);
  17775. $values[] = $this->ArrayEntry();
  17776. }
  17777. $this->match(Lexer::T_CLOSE_CURLY_BRACES);
  17778. foreach ($values as $value) {
  17779. list ($key, $val) = $value;
  17780. if ($key !== null) {
  17781. $array[$key] = $val;
  17782. } else {
  17783. $array[] = $val;
  17784. }
  17785. }
  17786. return $array;
  17787. }
  17788. /**
  17789. * ArrayEntry ::= Value | KeyValuePair
  17790. * KeyValuePair ::= Key "=" PlainValue
  17791. * Key ::= string | integer
  17792. *
  17793. * @return array
  17794. */
  17795. public function ArrayEntry()
  17796. {
  17797. $peek = $this->lexer->glimpse();
  17798. if ($peek['value'] == '=') {
  17799. $this->match(
  17800. $this->lexer->isNextToken(Lexer::T_INTEGER) ? Lexer::T_INTEGER : Lexer::T_STRING
  17801. );
  17802. $key = $this->lexer->token['value'];
  17803. $this->match(Lexer::T_EQUALS);
  17804. return array($key, $this->PlainValue());
  17805. }
  17806. return array(null, $this->Value());
  17807. }
  17808. /**
  17809. * Constructs a new annotation with a given map of values.
  17810. *
  17811. * The default construction procedure is to instantiate a new object of a class
  17812. * with the same name as the annotation. Subclasses can override this method to
  17813. * change the construction process of new annotations.
  17814. *
  17815. * @param string The name of the annotation.
  17816. * @param array The map of annotation values.
  17817. * @return mixed The new annotation with the given values.
  17818. */
  17819. protected function newAnnotation($name, array $values)
  17820. {
  17821. return new $name($values);
  17822. }
  17823. }<?php
  17824. /*
  17825. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17826. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17827. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17828. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17829. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17830. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  17831. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  17832. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  17833. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  17834. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  17835. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17836. *
  17837. * This software consists of voluntary contributions made by many individuals
  17838. * and is licensed under the LGPL. For more information, see
  17839. * <http://www.doctrine-project.org>.
  17840. */
  17841. namespace Doctrine\Common\Annotations;
  17842. /**
  17843. * Simple lexer for docblock annotations.
  17844. *
  17845. * This Lexer can be subclassed to customize certain aspects of the annotation
  17846. * lexing (token recognition) process. Note though that currently no special care
  17847. * is taken to maintain full backwards compatibility for subclasses. Implementation
  17848. * details of the default Lexer can change without explicit notice.
  17849. *
  17850. * @since 2.0
  17851. * @author Benjamin Eberlei <kontakt@beberlei.de>
  17852. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  17853. * @author Jonathan Wage <jonwage@gmail.com>
  17854. * @author Roman Borschel <roman@code-factory.org>
  17855. */
  17856. class Lexer extends \Doctrine\Common\Lexer
  17857. {
  17858. const T_NONE = 1;
  17859. const T_IDENTIFIER = 2;
  17860. const T_INTEGER = 3;
  17861. const T_STRING = 4;
  17862. const T_FLOAT = 5;
  17863. const T_AT = 101;
  17864. const T_CLOSE_CURLY_BRACES = 102;
  17865. const T_CLOSE_PARENTHESIS = 103;
  17866. const T_COMMA = 104;
  17867. const T_EQUALS = 105;
  17868. const T_FALSE = 106;
  17869. const T_NAMESPACE_SEPARATOR = 107;
  17870. const T_OPEN_CURLY_BRACES = 108;
  17871. const T_OPEN_PARENTHESIS = 109;
  17872. const T_TRUE = 110;
  17873. /**
  17874. * @inheritdoc
  17875. */
  17876. protected function getCatchablePatterns()
  17877. {
  17878. return array(
  17879. '[a-z_][a-z0-9_:]*',
  17880. '(?:[0-9]+(?:[\.][0-9]+)*)(?:e[+-]?[0-9]+)?',
  17881. '"(?:[^"]|"")*"'
  17882. );
  17883. }
  17884. /**
  17885. * @inheritdoc
  17886. */
  17887. protected function getNonCatchablePatterns()
  17888. {
  17889. return array('\s+', '\*+', '(.)');
  17890. }
  17891. /**
  17892. * @inheritdoc
  17893. */
  17894. protected function getType(&$value)
  17895. {
  17896. $type = self::T_NONE;
  17897. $newVal = $this->getNumeric($value);
  17898. // Checking numeric value
  17899. if ($newVal !== false) {
  17900. $value = $newVal;
  17901. return (strpos($value, '.') !== false || stripos($value, 'e') !== false)
  17902. ? self::T_FLOAT : self::T_INTEGER;
  17903. }
  17904. if ($value[0] === '"') {
  17905. $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
  17906. return self::T_STRING;
  17907. } else {
  17908. switch (strtolower($value)) {
  17909. case '@':
  17910. return self::T_AT;
  17911. case ',':
  17912. return self::T_COMMA;
  17913. case '(':
  17914. return self::T_OPEN_PARENTHESIS;
  17915. case ')':
  17916. return self::T_CLOSE_PARENTHESIS;
  17917. case '{':
  17918. return self::T_OPEN_CURLY_BRACES;
  17919. case '}': return self::T_CLOSE_CURLY_BRACES;
  17920. case '=':
  17921. return self::T_EQUALS;
  17922. case '\\':
  17923. return self::T_NAMESPACE_SEPARATOR;
  17924. case 'true':
  17925. return self::T_TRUE;
  17926. case 'false':
  17927. return self::T_FALSE;
  17928. default:
  17929. if (ctype_alpha($value[0]) || $value[0] === '_') {
  17930. return self::T_IDENTIFIER;
  17931. }
  17932. break;
  17933. }
  17934. }
  17935. return $type;
  17936. }
  17937. /**
  17938. * Checks if a value is numeric or not
  17939. *
  17940. * @param mixed $value Value to be inspected
  17941. * @return boolean|integer|float Processed value
  17942. * @todo Inline
  17943. */
  17944. private function getNumeric($value)
  17945. {
  17946. if ( ! is_scalar($value)) {
  17947. return false;
  17948. }
  17949. // Checking for valid numeric numbers: 1.234, -1.234e-2
  17950. if (is_numeric($value)) {
  17951. return $value;
  17952. }
  17953. return false;
  17954. }
  17955. }<?php
  17956. /*
  17957. * $Id$
  17958. *
  17959. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17960. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17961. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17962. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17963. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17964. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  17965. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  17966. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  17967. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  17968. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  17969. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17970. *
  17971. * This software consists of voluntary contributions made by many individuals
  17972. * and is licensed under the LGPL. For more information, see
  17973. * <http://www.doctrine-project.org>.
  17974. */
  17975. namespace Doctrine\Common\Annotations;
  17976. /**
  17977. * Annotations class
  17978. *
  17979. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  17980. * @link www.doctrine-project.org
  17981. * @since 2.0
  17982. * @version $Revision$
  17983. * @author Benjamin Eberlei <kontakt@beberlei.de>
  17984. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  17985. * @author Jonathan Wage <jonwage@gmail.com>
  17986. * @author Roman Borschel <roman@code-factory.org>
  17987. */
  17988. class Annotation
  17989. {
  17990. /**
  17991. * Value property. Common among all derived classes.
  17992. *
  17993. * @var string
  17994. */
  17995. public $value;
  17996. /**
  17997. * Constructor
  17998. *
  17999. * @param array $data Key-value for properties to be defined in this class
  18000. */
  18001. public final function __construct(array $data)
  18002. {
  18003. foreach ($data as $key => $value) {
  18004. $this->$key = $value;
  18005. }
  18006. }
  18007. /**
  18008. * Error handler for unknown property accessor in Annotation class.
  18009. *
  18010. * @param string $name Unknown property name
  18011. */
  18012. public function __get($name)
  18013. {
  18014. throw new \BadMethodCallException(
  18015. sprintf("Unknown property '%s' on annotation '%s'.", $name, get_class($this))
  18016. );
  18017. }
  18018. /**
  18019. * Error handler for unknown property mutator in Annotation class.
  18020. *
  18021. * @param string $name Unkown property name
  18022. * @param mixed $value Property value
  18023. */
  18024. public function __set($name, $value)
  18025. {
  18026. throw new \BadMethodCallException(
  18027. sprintf("Unknown property '%s' on annotation '%s'.", $name, get_class($this))
  18028. );
  18029. }
  18030. }<?php
  18031. /*
  18032. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18033. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18034. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18035. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18036. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18037. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18038. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18039. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  18040. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18041. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  18042. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18043. *
  18044. * This software consists of voluntary contributions made by many individuals
  18045. * and is licensed under the LGPL. For more information, see
  18046. * <http://www.doctrine-project.org>.
  18047. */
  18048. namespace Doctrine\Common;
  18049. /**
  18050. * A <tt>ClassLoader</tt> is an autoloader for class files that can be
  18051. * installed on the SPL autoload stack. It is a class loader that either loads only classes
  18052. * of a specific namespace or all namespaces and it is suitable for working together
  18053. * with other autoloaders in the SPL autoload stack.
  18054. *
  18055. * If no include path is configured through the constructor or {@link setIncludePath}, a ClassLoader
  18056. * relies on the PHP <code>include_path</code>.
  18057. *
  18058. * @author Roman Borschel <roman@code-factory.org>
  18059. * @since 2.0
  18060. */
  18061. class ClassLoader
  18062. {
  18063. private $fileExtension = '.php';
  18064. private $namespace;
  18065. private $includePath;
  18066. private $namespaceSeparator = '\\';
  18067. /**
  18068. * Creates a new <tt>ClassLoader</tt> that loads classes of the
  18069. * specified namespace from the specified include path.
  18070. *
  18071. * If no include path is given, the ClassLoader relies on the PHP include_path.
  18072. * If neither a namespace nor an include path is given, the ClassLoader will
  18073. * be responsible for loading all classes, thereby relying on the PHP include_path.
  18074. *
  18075. * @param string $ns The namespace of the classes to load.
  18076. * @param string $includePath The base include path to use.
  18077. */
  18078. public function __construct($ns = null, $includePath = null)
  18079. {
  18080. $this->namespace = $ns;
  18081. $this->includePath = $includePath;
  18082. }
  18083. /**
  18084. * Sets the namespace separator used by classes in the namespace of this ClassLoader.
  18085. *
  18086. * @param string $sep The separator to use.
  18087. */
  18088. public function setNamespaceSeparator($sep)
  18089. {
  18090. $this->namespaceSeparator = $sep;
  18091. }
  18092. /**
  18093. * Gets the namespace separator used by classes in the namespace of this ClassLoader.
  18094. *
  18095. * @return string
  18096. */
  18097. public function getNamespaceSeparator()
  18098. {
  18099. return $this->namespaceSeparator;
  18100. }
  18101. /**
  18102. * Sets the base include path for all class files in the namespace of this ClassLoader.
  18103. *
  18104. * @param string $includePath
  18105. */
  18106. public function setIncludePath($includePath)
  18107. {
  18108. $this->includePath = $includePath;
  18109. }
  18110. /**
  18111. * Gets the base include path for all class files in the namespace of this ClassLoader.
  18112. *
  18113. * @return string
  18114. */
  18115. public function getIncludePath()
  18116. {
  18117. return $this->includePath;
  18118. }
  18119. /**
  18120. * Sets the file extension of class files in the namespace of this ClassLoader.
  18121. *
  18122. * @param string $fileExtension
  18123. */
  18124. public function setFileExtension($fileExtension)
  18125. {
  18126. $this->fileExtension = $fileExtension;
  18127. }
  18128. /**
  18129. * Gets the file extension of class files in the namespace of this ClassLoader.
  18130. *
  18131. * @return string
  18132. */
  18133. public function getFileExtension()
  18134. {
  18135. return $this->fileExtension;
  18136. }
  18137. /**
  18138. * Registers this ClassLoader on the SPL autoload stack.
  18139. */
  18140. public function register()
  18141. {
  18142. spl_autoload_register(array($this, 'loadClass'));
  18143. }
  18144. /**
  18145. * Removes this ClassLoader from the SPL autoload stack.
  18146. */
  18147. public function unregister()
  18148. {
  18149. spl_autoload_unregister(array($this, 'loadClass'));
  18150. }
  18151. /**
  18152. * Loads the given class or interface.
  18153. *
  18154. * @param string $classname The name of the class to load.
  18155. * @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
  18156. */
  18157. public function loadClass($className)
  18158. {
  18159. if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
  18160. return false;
  18161. }
  18162. require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
  18163. . str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className)
  18164. . $this->fileExtension;
  18165. return true;
  18166. }
  18167. /**
  18168. * Asks this ClassLoader whether it can potentially load the class (file) with
  18169. * the given name.
  18170. *
  18171. * @param string $className The fully-qualified name of the class.
  18172. * @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise.
  18173. */
  18174. public function canLoadClass($className)
  18175. {
  18176. if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
  18177. return false;
  18178. }
  18179. return file_exists(($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
  18180. . str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className)
  18181. . $this->fileExtension);
  18182. }
  18183. /**
  18184. * Checks whether a class with a given name exists. A class "exists" if it is either
  18185. * already defined in the current request or if there is an autoloader on the SPL
  18186. * autoload stack that is a) responsible for the class in question and b) is able to
  18187. * load a class file in which the class definition resides.
  18188. *
  18189. * If the class is not already defined, each autoloader in the SPL autoload stack
  18190. * is asked whether it is able to tell if the class exists. If the autoloader is
  18191. * a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload
  18192. * function of the autoloader is invoked and expected to return a value that
  18193. * evaluates to TRUE if the class (file) exists. As soon as one autoloader reports
  18194. * that the class exists, TRUE is returned.
  18195. *
  18196. * Note that, depending on what kinds of autoloaders are installed on the SPL
  18197. * autoload stack, the class (file) might already be loaded as a result of checking
  18198. * for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates
  18199. * these responsibilities.
  18200. *
  18201. * @param string $className The fully-qualified name of the class.
  18202. * @return boolean TRUE if the class exists as per the definition given above, FALSE otherwise.
  18203. */
  18204. public static function classExists($className)
  18205. {
  18206. if (class_exists($className, false)) {
  18207. return true;
  18208. }
  18209. foreach (spl_autoload_functions() as $loader) {
  18210. if (is_array($loader)) { // array(???, ???)
  18211. if (is_object($loader[0])) {
  18212. if ($loader[0] instanceof ClassLoader) { // array($obj, 'methodName')
  18213. if ($loader[0]->canLoadClass($className)) {
  18214. return true;
  18215. }
  18216. } else if ($loader[0]->{$loader[1]}($className)) {
  18217. return true;
  18218. }
  18219. } else if ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName')
  18220. return true;
  18221. }
  18222. } else if ($loader instanceof \Closure) { // function($className) {..}
  18223. if ($loader($className)) {
  18224. return true;
  18225. }
  18226. } else if (is_string($loader) && $loader($className)) { // "MyClass::loadClass"
  18227. return true;
  18228. }
  18229. }
  18230. return false;
  18231. }
  18232. /**
  18233. * Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible
  18234. * for (and is able to load) the class with the given name.
  18235. *
  18236. * @param string $className The name of the class.
  18237. * @return The <tt>ClassLoader</tt> for the class or NULL if no such <tt>ClassLoader</tt> exists.
  18238. */
  18239. public static function getClassLoader($className)
  18240. {
  18241. foreach (spl_autoload_functions() as $loader) {
  18242. if (is_array($loader) && $loader[0] instanceof ClassLoader &&
  18243. $loader[0]->canLoadClass($className)) {
  18244. return $loader[0];
  18245. }
  18246. }
  18247. return null;
  18248. }
  18249. }<?php
  18250. /*
  18251. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18252. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18253. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18254. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18255. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18256. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18257. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18258. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  18259. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18260. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  18261. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18262. *
  18263. * This software consists of voluntary contributions made by many individuals
  18264. * and is licensed under the LGPL. For more information, see
  18265. * <http://www.doctrine-project.org>.
  18266. */
  18267. namespace Doctrine\Common\Collections;
  18268. use Closure, ArrayIterator;
  18269. /**
  18270. * An ArrayCollection is a Collection implementation that wraps a regular PHP array.
  18271. *
  18272. * @since 2.0
  18273. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  18274. * @author Jonathan Wage <jonwage@gmail.com>
  18275. * @author Roman Borschel <roman@code-factory.org>
  18276. */
  18277. class ArrayCollection implements Collection
  18278. {
  18279. /**
  18280. * An array containing the entries of this collection.
  18281. *
  18282. * @var array
  18283. */
  18284. private $_elements;
  18285. /**
  18286. * Initializes a new ArrayCollection.
  18287. *
  18288. * @param array $elements
  18289. */
  18290. public function __construct(array $elements = array())
  18291. {
  18292. $this->_elements = $elements;
  18293. }
  18294. /**
  18295. * Gets the PHP array representation of this collection.
  18296. *
  18297. * @return array The PHP array representation of this collection.
  18298. */
  18299. public function toArray()
  18300. {
  18301. return $this->_elements;
  18302. }
  18303. /**
  18304. * Sets the internal iterator to the first element in the collection and
  18305. * returns this element.
  18306. *
  18307. * @return mixed
  18308. */
  18309. public function first()
  18310. {
  18311. return reset($this->_elements);
  18312. }
  18313. /**
  18314. * Sets the internal iterator to the last element in the collection and
  18315. * returns this element.
  18316. *
  18317. * @return mixed
  18318. */
  18319. public function last()
  18320. {
  18321. return end($this->_elements);
  18322. }
  18323. /**
  18324. * Gets the current key/index at the current internal iterator position.
  18325. *
  18326. * @return mixed
  18327. */
  18328. public function key()
  18329. {
  18330. return key($this->_elements);
  18331. }
  18332. /**
  18333. * Moves the internal iterator position to the next element.
  18334. *
  18335. * @return mixed
  18336. */
  18337. public function next()
  18338. {
  18339. return next($this->_elements);
  18340. }
  18341. /**
  18342. * Gets the element of the collection at the current internal iterator position.
  18343. *
  18344. * @return mixed
  18345. */
  18346. public function current()
  18347. {
  18348. return current($this->_elements);
  18349. }
  18350. /**
  18351. * Removes an element with a specific key/index from the collection.
  18352. *
  18353. * @param mixed $key
  18354. * @return mixed The removed element or NULL, if no element exists for the given key.
  18355. */
  18356. public function remove($key)
  18357. {
  18358. if (isset($this->_elements[$key])) {
  18359. $removed = $this->_elements[$key];
  18360. unset($this->_elements[$key]);
  18361. return $removed;
  18362. }
  18363. return null;
  18364. }
  18365. /**
  18366. * Removes the specified element from the collection, if it is found.
  18367. *
  18368. * @param mixed $element The element to remove.
  18369. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  18370. */
  18371. public function removeElement($element)
  18372. {
  18373. $key = array_search($element, $this->_elements, true);
  18374. if ($key !== false) {
  18375. unset($this->_elements[$key]);
  18376. return true;
  18377. }
  18378. return false;
  18379. }
  18380. /**
  18381. * ArrayAccess implementation of offsetExists()
  18382. *
  18383. * @see containsKey()
  18384. */
  18385. public function offsetExists($offset)
  18386. {
  18387. return $this->containsKey($offset);
  18388. }
  18389. /**
  18390. * ArrayAccess implementation of offsetGet()
  18391. *
  18392. * @see get()
  18393. */
  18394. public function offsetGet($offset)
  18395. {
  18396. return $this->get($offset);
  18397. }
  18398. /**
  18399. * ArrayAccess implementation of offsetGet()
  18400. *
  18401. * @see add()
  18402. * @see set()
  18403. */
  18404. public function offsetSet($offset, $value)
  18405. {
  18406. if ( ! isset($offset)) {
  18407. return $this->add($value);
  18408. }
  18409. return $this->set($offset, $value);
  18410. }
  18411. /**
  18412. * ArrayAccess implementation of offsetUnset()
  18413. *
  18414. * @see remove()
  18415. */
  18416. public function offsetUnset($offset)
  18417. {
  18418. return $this->remove($offset);
  18419. }
  18420. /**
  18421. * Checks whether the collection contains a specific key/index.
  18422. *
  18423. * @param mixed $key The key to check for.
  18424. * @return boolean TRUE if the given key/index exists, FALSE otherwise.
  18425. */
  18426. public function containsKey($key)
  18427. {
  18428. return isset($this->_elements[$key]);
  18429. }
  18430. /**
  18431. * Checks whether the given element is contained in the collection.
  18432. * Only element values are compared, not keys. The comparison of two elements
  18433. * is strict, that means not only the value but also the type must match.
  18434. * For objects this means reference equality.
  18435. *
  18436. * @param mixed $element
  18437. * @return boolean TRUE if the given element is contained in the collection,
  18438. * FALSE otherwise.
  18439. */
  18440. public function contains($element)
  18441. {
  18442. return in_array($element, $this->_elements, true);
  18443. }
  18444. /**
  18445. * Tests for the existance of an element that satisfies the given predicate.
  18446. *
  18447. * @param Closure $p The predicate.
  18448. * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
  18449. */
  18450. public function exists(Closure $p)
  18451. {
  18452. foreach ($this->_elements as $key => $element)
  18453. if ($p($key, $element)) return true;
  18454. return false;
  18455. }
  18456. /**
  18457. * Searches for a given element and, if found, returns the corresponding key/index
  18458. * of that element. The comparison of two elements is strict, that means not
  18459. * only the value but also the type must match.
  18460. * For objects this means reference equality.
  18461. *
  18462. * @param mixed $element The element to search for.
  18463. * @return mixed The key/index of the element or FALSE if the element was not found.
  18464. */
  18465. public function indexOf($element)
  18466. {
  18467. return array_search($element, $this->_elements, true);
  18468. }
  18469. /**
  18470. * Gets the element with the given key/index.
  18471. *
  18472. * @param mixed $key The key.
  18473. * @return mixed The element or NULL, if no element exists for the given key.
  18474. */
  18475. public function get($key)
  18476. {
  18477. if (isset($this->_elements[$key])) {
  18478. return $this->_elements[$key];
  18479. }
  18480. return null;
  18481. }
  18482. /**
  18483. * Gets all keys/indexes of the collection elements.
  18484. *
  18485. * @return array
  18486. */
  18487. public function getKeys()
  18488. {
  18489. return array_keys($this->_elements);
  18490. }
  18491. /**
  18492. * Gets all elements.
  18493. *
  18494. * @return array
  18495. */
  18496. public function getValues()
  18497. {
  18498. return array_values($this->_elements);
  18499. }
  18500. /**
  18501. * Returns the number of elements in the collection.
  18502. *
  18503. * Implementation of the Countable interface.
  18504. *
  18505. * @return integer The number of elements in the collection.
  18506. */
  18507. public function count()
  18508. {
  18509. return count($this->_elements);
  18510. }
  18511. /**
  18512. * Adds/sets an element in the collection at the index / with the specified key.
  18513. *
  18514. * When the collection is a Map this is like put(key,value)/add(key,value).
  18515. * When the collection is a List this is like add(position,value).
  18516. *
  18517. * @param mixed $key
  18518. * @param mixed $value
  18519. */
  18520. public function set($key, $value)
  18521. {
  18522. $this->_elements[$key] = $value;
  18523. }
  18524. /**
  18525. * Adds an element to the collection.
  18526. *
  18527. * @param mixed $value
  18528. * @return boolean Always TRUE.
  18529. */
  18530. public function add($value)
  18531. {
  18532. $this->_elements[] = $value;
  18533. return true;
  18534. }
  18535. /**
  18536. * Checks whether the collection is empty.
  18537. *
  18538. * Note: This is preferrable over count() == 0.
  18539. *
  18540. * @return boolean TRUE if the collection is empty, FALSE otherwise.
  18541. */
  18542. public function isEmpty()
  18543. {
  18544. return ! $this->_elements;
  18545. }
  18546. /**
  18547. * Gets an iterator for iterating over the elements in the collection.
  18548. *
  18549. * @return ArrayIterator
  18550. */
  18551. public function getIterator()
  18552. {
  18553. return new ArrayIterator($this->_elements);
  18554. }
  18555. /**
  18556. * Applies the given function to each element in the collection and returns
  18557. * a new collection with the elements returned by the function.
  18558. *
  18559. * @param Closure $func
  18560. * @return Collection
  18561. */
  18562. public function map(Closure $func)
  18563. {
  18564. return new ArrayCollection(array_map($func, $this->_elements));
  18565. }
  18566. /**
  18567. * Returns all the elements of this collection that satisfy the predicate p.
  18568. * The order of the elements is preserved.
  18569. *
  18570. * @param Closure $p The predicate used for filtering.
  18571. * @return Collection A collection with the results of the filter operation.
  18572. */
  18573. public function filter(Closure $p)
  18574. {
  18575. return new ArrayCollection(array_filter($this->_elements, $p));
  18576. }
  18577. /**
  18578. * Applies the given predicate p to all elements of this collection,
  18579. * returning true, if the predicate yields true for all elements.
  18580. *
  18581. * @param Closure $p The predicate.
  18582. * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
  18583. */
  18584. public function forAll(Closure $p)
  18585. {
  18586. foreach ($this->_elements as $key => $element) {
  18587. if ( ! $p($key, $element)) {
  18588. return false;
  18589. }
  18590. }
  18591. return true;
  18592. }
  18593. /**
  18594. * Partitions this collection in two collections according to a predicate.
  18595. * Keys are preserved in the resulting collections.
  18596. *
  18597. * @param Closure $p The predicate on which to partition.
  18598. * @return array An array with two elements. The first element contains the collection
  18599. * of elements where the predicate returned TRUE, the second element
  18600. * contains the collection of elements where the predicate returned FALSE.
  18601. */
  18602. public function partition(Closure $p)
  18603. {
  18604. $coll1 = $coll2 = array();
  18605. foreach ($this->_elements as $key => $element) {
  18606. if ($p($key, $element)) {
  18607. $coll1[$key] = $element;
  18608. } else {
  18609. $coll2[$key] = $element;
  18610. }
  18611. }
  18612. return array(new ArrayCollection($coll1), new ArrayCollection($coll2));
  18613. }
  18614. /**
  18615. * Returns a string representation of this object.
  18616. *
  18617. * @return string
  18618. */
  18619. public function __toString()
  18620. {
  18621. return __CLASS__ . '@' . spl_object_hash($this);
  18622. }
  18623. /**
  18624. * Clears the collection.
  18625. */
  18626. public function clear()
  18627. {
  18628. $this->_elements = array();
  18629. }
  18630. /**
  18631. * Extract a slice of $length elements starting at position $offset from the Collection.
  18632. *
  18633. * If $length is null it returns all elements from $offset to the end of the Collection.
  18634. * Keys have to be preserved by this method. Calling this method will only return the
  18635. * selected slice and NOT change the elements contained in the collection slice is called on.
  18636. *
  18637. * @param int $offset
  18638. * @param int $length
  18639. * @return array
  18640. */
  18641. public function slice($offset, $length = null)
  18642. {
  18643. return array_slice($this->_elements, $offset, $length, true);
  18644. }
  18645. }
  18646. <?php
  18647. /*
  18648. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18649. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18650. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18651. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18652. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18653. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18654. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18655. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  18656. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18657. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  18658. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18659. *
  18660. * This software consists of voluntary contributions made by many individuals
  18661. * and is licensed under the LGPL. For more information, see
  18662. * <http://www.doctrine-project.org>.
  18663. */
  18664. namespace Doctrine\Common\Collections;
  18665. use Closure, Countable, IteratorAggregate, ArrayAccess;
  18666. /**
  18667. * The missing (SPL) Collection/Array/OrderedMap interface.
  18668. *
  18669. * A Collection resembles the nature of a regular PHP array. That is,
  18670. * it is essentially an <b>ordered map</b> that can also be used
  18671. * like a list.
  18672. *
  18673. * A Collection has an internal iterator just like a PHP array. In addition,
  18674. * a Collection can be iterated with external iterators, which is preferrable.
  18675. * To use an external iterator simply use the foreach language construct to
  18676. * iterate over the collection (which calls {@link getIterator()} internally) or
  18677. * explicitly retrieve an iterator though {@link getIterator()} which can then be
  18678. * used to iterate over the collection.
  18679. * You can not rely on the internal iterator of the collection being at a certain
  18680. * position unless you explicitly positioned it before. Prefer iteration with
  18681. * external iterators.
  18682. *
  18683. * @since 2.0
  18684. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  18685. * @author Jonathan Wage <jonwage@gmail.com>
  18686. * @author Roman Borschel <roman@code-factory.org>
  18687. */
  18688. interface Collection extends Countable, IteratorAggregate, ArrayAccess
  18689. {
  18690. /**
  18691. * Adds an element at the end of the collection.
  18692. *
  18693. * @param mixed $element The element to add.
  18694. * @return boolean Always TRUE.
  18695. */
  18696. function add($element);
  18697. /**
  18698. * Clears the collection, removing all elements.
  18699. */
  18700. function clear();
  18701. /**
  18702. * Checks whether an element is contained in the collection.
  18703. * This is an O(n) operation, where n is the size of the collection.
  18704. *
  18705. * @param mixed $element The element to search for.
  18706. * @return boolean TRUE if the collection contains the element, FALSE otherwise.
  18707. */
  18708. function contains($element);
  18709. /**
  18710. * Checks whether the collection is empty (contains no elements).
  18711. *
  18712. * @return boolean TRUE if the collection is empty, FALSE otherwise.
  18713. */
  18714. function isEmpty();
  18715. /**
  18716. * Removes the element at the specified index from the collection.
  18717. *
  18718. * @param string|integer $key The kex/index of the element to remove.
  18719. * @return mixed The removed element or NULL, if the collection did not contain the element.
  18720. */
  18721. function remove($key);
  18722. /**
  18723. * Removes the specified element from the collection, if it is found.
  18724. *
  18725. * @param mixed $element The element to remove.
  18726. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  18727. */
  18728. function removeElement($element);
  18729. /**
  18730. * Checks whether the collection contains an element with the specified key/index.
  18731. *
  18732. * @param string|integer $key The key/index to check for.
  18733. * @return boolean TRUE if the collection contains an element with the specified key/index,
  18734. * FALSE otherwise.
  18735. */
  18736. function containsKey($key);
  18737. /**
  18738. * Gets the element at the specified key/index.
  18739. *
  18740. * @param string|integer $key The key/index of the element to retrieve.
  18741. * @return mixed
  18742. */
  18743. function get($key);
  18744. /**
  18745. * Gets all keys/indices of the collection.
  18746. *
  18747. * @return array The keys/indices of the collection, in the order of the corresponding
  18748. * elements in the collection.
  18749. */
  18750. function getKeys();
  18751. /**
  18752. * Gets all values of the collection.
  18753. *
  18754. * @return array The values of all elements in the collection, in the order they
  18755. * appear in the collection.
  18756. */
  18757. function getValues();
  18758. /**
  18759. * Sets an element in the collection at the specified key/index.
  18760. *
  18761. * @param string|integer $key The key/index of the element to set.
  18762. * @param mixed $value The element to set.
  18763. */
  18764. function set($key, $value);
  18765. /**
  18766. * Gets a native PHP array representation of the collection.
  18767. *
  18768. * @return array
  18769. */
  18770. function toArray();
  18771. /**
  18772. * Sets the internal iterator to the first element in the collection and
  18773. * returns this element.
  18774. *
  18775. * @return mixed
  18776. */
  18777. function first();
  18778. /**
  18779. * Sets the internal iterator to the last element in the collection and
  18780. * returns this element.
  18781. *
  18782. * @return mixed
  18783. */
  18784. function last();
  18785. /**
  18786. * Gets the key/index of the element at the current iterator position.
  18787. *
  18788. */
  18789. function key();
  18790. /**
  18791. * Gets the element of the collection at the current iterator position.
  18792. *
  18793. */
  18794. function current();
  18795. /**
  18796. * Moves the internal iterator position to the next element.
  18797. *
  18798. */
  18799. function next();
  18800. /**
  18801. * Tests for the existence of an element that satisfies the given predicate.
  18802. *
  18803. * @param Closure $p The predicate.
  18804. * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
  18805. */
  18806. function exists(Closure $p);
  18807. /**
  18808. * Returns all the elements of this collection that satisfy the predicate p.
  18809. * The order of the elements is preserved.
  18810. *
  18811. * @param Closure $p The predicate used for filtering.
  18812. * @return Collection A collection with the results of the filter operation.
  18813. */
  18814. function filter(Closure $p);
  18815. /**
  18816. * Applies the given predicate p to all elements of this collection,
  18817. * returning true, if the predicate yields true for all elements.
  18818. *
  18819. * @param Closure $p The predicate.
  18820. * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
  18821. */
  18822. function forAll(Closure $p);
  18823. /**
  18824. * Applies the given function to each element in the collection and returns
  18825. * a new collection with the elements returned by the function.
  18826. *
  18827. * @param Closure $func
  18828. * @return Collection
  18829. */
  18830. function map(Closure $func);
  18831. /**
  18832. * Partitions this collection in two collections according to a predicate.
  18833. * Keys are preserved in the resulting collections.
  18834. *
  18835. * @param Closure $p The predicate on which to partition.
  18836. * @return array An array with two elements. The first element contains the collection
  18837. * of elements where the predicate returned TRUE, the second element
  18838. * contains the collection of elements where the predicate returned FALSE.
  18839. */
  18840. function partition(Closure $p);
  18841. /**
  18842. * Gets the index/key of a given element. The comparison of two elements is strict,
  18843. * that means not only the value but also the type must match.
  18844. * For objects this means reference equality.
  18845. *
  18846. * @param mixed $element The element to search for.
  18847. * @return mixed The key/index of the element or FALSE if the element was not found.
  18848. */
  18849. function indexOf($element);
  18850. /**
  18851. * Extract a slice of $length elements starting at position $offset from the Collection.
  18852. *
  18853. * If $length is null it returns all elements from $offset to the end of the Collection.
  18854. * Keys have to be preserved by this method. Calling this method will only return the
  18855. * selected slice and NOT change the elements contained in the collection slice is called on.
  18856. *
  18857. * @param int $offset
  18858. * @param int $length
  18859. * @return array
  18860. */
  18861. public function slice($offset, $length = null);
  18862. }<?php
  18863. /*
  18864. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18865. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18866. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18867. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18868. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18869. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18870. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18871. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  18872. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18873. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  18874. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18875. *
  18876. * This software consists of voluntary contributions made by many individuals
  18877. * and is licensed under the LGPL. For more information, see
  18878. * <http://www.doctrine-project.org>.
  18879. */
  18880. namespace Doctrine\Common;
  18881. /**
  18882. * Class to store and retrieve the version of Doctrine
  18883. *
  18884. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  18885. * @link www.doctrine-project.org
  18886. * @since 2.0
  18887. * @version $Revision$
  18888. * @author Benjamin Eberlei <kontakt@beberlei.de>
  18889. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  18890. * @author Jonathan Wage <jonwage@gmail.com>
  18891. * @author Roman Borschel <roman@code-factory.org>
  18892. */
  18893. class Version
  18894. {
  18895. /**
  18896. * Current Doctrine Version
  18897. */
  18898. const VERSION = '2.0.0RC2-DEV';
  18899. /**
  18900. * Compares a Doctrine version with the current one.
  18901. *
  18902. * @param string $version Doctrine version to compare.
  18903. * @return int Returns -1 if older, 0 if it is the same, 1 if version
  18904. * passed as argument is newer.
  18905. */
  18906. public static function compare($version)
  18907. {
  18908. $currentVersion = str_replace(' ', '', strtolower(self::VERSION));
  18909. $version = str_replace(' ', '', $version);
  18910. return version_compare($version, $currentVersion);
  18911. }
  18912. }<?php
  18913. /*
  18914. * $Id$
  18915. *
  18916. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18917. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18918. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18919. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18920. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18921. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18922. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18923. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  18924. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18925. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  18926. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18927. *
  18928. * This software consists of voluntary contributions made by many individuals
  18929. * and is licensed under the LGPL. For more information, see
  18930. * <http://www.doctrine-project.org>.
  18931. */
  18932. namespace Doctrine\Common\Cache;
  18933. /**
  18934. * Xcache cache driver.
  18935. *
  18936. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  18937. * @link www.doctrine-project.org
  18938. * @since 2.0
  18939. * @version $Revision: 3938 $
  18940. * @author Benjamin Eberlei <kontakt@beberlei.de>
  18941. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  18942. * @author Jonathan Wage <jonwage@gmail.com>
  18943. * @author Roman Borschel <roman@code-factory.org>
  18944. * @author David Abdemoulaie <dave@hobodave.com>
  18945. */
  18946. class XcacheCache extends AbstractCache
  18947. {
  18948. /**
  18949. * {@inheritdoc}
  18950. */
  18951. public function getIds()
  18952. {
  18953. $this->_checkAuth();
  18954. $keys = array();
  18955. for ($i = 0, $count = xcache_count(XC_TYPE_VAR); $i < $count; $i++) {
  18956. $entries = xcache_list(XC_TYPE_VAR, $i);
  18957. if (is_array($entries['cache_list'])) {
  18958. foreach ($entries['cache_list'] as $entry) {
  18959. $keys[] = $entry['name'];
  18960. }
  18961. }
  18962. }
  18963. return $keys;
  18964. }
  18965. /**
  18966. * {@inheritdoc}
  18967. */
  18968. protected function _doFetch($id)
  18969. {
  18970. return $this->_doContains($id) ? unserialize(xcache_get($id)) : false;
  18971. }
  18972. /**
  18973. * {@inheritdoc}
  18974. */
  18975. protected function _doContains($id)
  18976. {
  18977. return xcache_isset($id);
  18978. }
  18979. /**
  18980. * {@inheritdoc}
  18981. */
  18982. protected function _doSave($id, $data, $lifeTime = 0)
  18983. {
  18984. return xcache_set($id, serialize($data), (int) $lifeTime);
  18985. }
  18986. /**
  18987. * {@inheritdoc}
  18988. */
  18989. protected function _doDelete($id)
  18990. {
  18991. return xcache_unset($id);
  18992. }
  18993. /**
  18994. * Checks that xcache.admin.enable_auth is Off
  18995. *
  18996. * @throws \BadMethodCallException When xcache.admin.enable_auth is On
  18997. * @return void
  18998. */
  18999. protected function _checkAuth()
  19000. {
  19001. if (ini_get('xcache.admin.enable_auth')) {
  19002. throw new \BadMethodCallException('To use all features of \Doctrine\Common\Cache\XcacheCache, you must set "xcache.admin.enable_auth" to "Off" in your php.ini.');
  19003. }
  19004. }
  19005. }<?php
  19006. /*
  19007. * $Id$
  19008. *
  19009. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19010. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19011. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19012. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19013. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19014. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19015. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19016. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19017. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19018. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19019. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19020. *
  19021. * This software consists of voluntary contributions made by many individuals
  19022. * and is licensed under the LGPL. For more information, see
  19023. * <http://www.doctrine-project.org>.
  19024. */
  19025. namespace Doctrine\Common\Cache;
  19026. /**
  19027. * Array cache driver.
  19028. *
  19029. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  19030. * @link www.doctrine-project.org
  19031. * @since 2.0
  19032. * @version $Revision: 3938 $
  19033. * @author Benjamin Eberlei <kontakt@beberlei.de>
  19034. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19035. * @author Jonathan Wage <jonwage@gmail.com>
  19036. * @author Roman Borschel <roman@code-factory.org>
  19037. * @author David Abdemoulaie <dave@hobodave.com>
  19038. */
  19039. class ArrayCache extends AbstractCache
  19040. {
  19041. /**
  19042. * @var array $data
  19043. */
  19044. private $data = array();
  19045. /**
  19046. * {@inheritdoc}
  19047. */
  19048. public function getIds()
  19049. {
  19050. return array_keys($this->data);
  19051. }
  19052. /**
  19053. * {@inheritdoc}
  19054. */
  19055. protected function _doFetch($id)
  19056. {
  19057. if (isset($this->data[$id])) {
  19058. return $this->data[$id];
  19059. }
  19060. return false;
  19061. }
  19062. /**
  19063. * {@inheritdoc}
  19064. */
  19065. protected function _doContains($id)
  19066. {
  19067. return isset($this->data[$id]);
  19068. }
  19069. /**
  19070. * {@inheritdoc}
  19071. */
  19072. protected function _doSave($id, $data, $lifeTime = 0)
  19073. {
  19074. $this->data[$id] = $data;
  19075. return true;
  19076. }
  19077. /**
  19078. * {@inheritdoc}
  19079. */
  19080. protected function _doDelete($id)
  19081. {
  19082. unset($this->data[$id]);
  19083. return true;
  19084. }
  19085. }<?php
  19086. /*
  19087. * $Id$
  19088. *
  19089. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19090. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19091. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19092. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19093. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19094. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19095. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19096. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19097. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19098. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19099. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19100. *
  19101. * This software consists of voluntary contributions made by many individuals
  19102. * and is licensed under the LGPL. For more information, see
  19103. * <http://www.doctrine-project.org>.
  19104. */
  19105. namespace Doctrine\Common\Cache;
  19106. use \Memcache;
  19107. /**
  19108. * Memcache cache driver.
  19109. *
  19110. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  19111. * @link www.doctrine-project.org
  19112. * @since 2.0
  19113. * @version $Revision: 3938 $
  19114. * @author Benjamin Eberlei <kontakt@beberlei.de>
  19115. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19116. * @author Jonathan Wage <jonwage@gmail.com>
  19117. * @author Roman Borschel <roman@code-factory.org>
  19118. * @author David Abdemoulaie <dave@hobodave.com>
  19119. */
  19120. class MemcacheCache extends AbstractCache
  19121. {
  19122. /**
  19123. * @var Memcache
  19124. */
  19125. private $_memcache;
  19126. /**
  19127. * Sets the memcache instance to use.
  19128. *
  19129. * @param Memcache $memcache
  19130. */
  19131. public function setMemcache(Memcache $memcache)
  19132. {
  19133. $this->_memcache = $memcache;
  19134. }
  19135. /**
  19136. * Gets the memcache instance used by the cache.
  19137. *
  19138. * @return Memcache
  19139. */
  19140. public function getMemcache()
  19141. {
  19142. return $this->_memcache;
  19143. }
  19144. /**
  19145. * {@inheritdoc}
  19146. */
  19147. public function getIds()
  19148. {
  19149. $keys = array();
  19150. $allSlabs = $this->_memcache->getExtendedStats('slabs');
  19151. foreach ($allSlabs as $server => $slabs) {
  19152. if (is_array($slabs)) {
  19153. foreach (array_keys($slabs) as $slabId) {
  19154. $dump = $this->_memcache->getExtendedStats('cachedump', (int) $slabId);
  19155. if ($dump) {
  19156. foreach ($dump as $entries) {
  19157. if ($entries) {
  19158. $keys = array_merge($keys, array_keys($entries));
  19159. }
  19160. }
  19161. }
  19162. }
  19163. }
  19164. }
  19165. return $keys;
  19166. }
  19167. /**
  19168. * {@inheritdoc}
  19169. */
  19170. protected function _doFetch($id)
  19171. {
  19172. return $this->_memcache->get($id);
  19173. }
  19174. /**
  19175. * {@inheritdoc}
  19176. */
  19177. protected function _doContains($id)
  19178. {
  19179. return (bool) $this->_memcache->get($id);
  19180. }
  19181. /**
  19182. * {@inheritdoc}
  19183. */
  19184. protected function _doSave($id, $data, $lifeTime = 0)
  19185. {
  19186. return $this->_memcache->set($id, $data, 0, (int) $lifeTime);
  19187. }
  19188. /**
  19189. * {@inheritdoc}
  19190. */
  19191. protected function _doDelete($id)
  19192. {
  19193. return $this->_memcache->delete($id);
  19194. }
  19195. }<?php
  19196. /*
  19197. * $Id$
  19198. *
  19199. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19200. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19201. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19202. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19203. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19204. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19205. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19206. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19207. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19208. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19209. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19210. *
  19211. * This software consists of voluntary contributions made by many individuals
  19212. * and is licensed under the LGPL. For more information, see
  19213. * <http://www.doctrine-project.org>.
  19214. */
  19215. namespace Doctrine\Common\Cache;
  19216. /**
  19217. * Interface for cache drivers.
  19218. *
  19219. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  19220. * @link www.doctrine-project.org
  19221. * @since 2.0
  19222. * @version $Revision: 3938 $
  19223. * @author Benjamin Eberlei <kontakt@beberlei.de>
  19224. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19225. * @author Jonathan Wage <jonwage@gmail.com>
  19226. * @author Roman Borschel <roman@code-factory.org>
  19227. */
  19228. interface Cache
  19229. {
  19230. /**
  19231. * Fetches an entry from the cache.
  19232. *
  19233. * @param string $id cache id The id of the cache entry to fetch.
  19234. * @return string The cached data or FALSE, if no cache entry exists for the given id.
  19235. */
  19236. function fetch($id);
  19237. /**
  19238. * Test if an entry exists in the cache.
  19239. *
  19240. * @param string $id cache id The cache id of the entry to check for.
  19241. * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
  19242. */
  19243. function contains($id);
  19244. /**
  19245. * Puts data into the cache.
  19246. *
  19247. * @param string $id The cache id.
  19248. * @param string $data The cache entry/data.
  19249. * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this cache entry (0 => infinite lifeTime).
  19250. * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
  19251. */
  19252. function save($id, $data, $lifeTime = 0);
  19253. /**
  19254. * Deletes a cache entry.
  19255. *
  19256. * @param string $id cache id
  19257. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
  19258. */
  19259. function delete($id);
  19260. }<?php
  19261. /*
  19262. * $Id$
  19263. *
  19264. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19265. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19266. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19267. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19268. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19269. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19270. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19271. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19272. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19273. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19274. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19275. *
  19276. * This software consists of voluntary contributions made by many individuals
  19277. * and is licensed under the LGPL. For more information, see
  19278. * <http://www.doctrine-project.org>.
  19279. */
  19280. namespace Doctrine\Common\Cache;
  19281. /**
  19282. * APC cache driver.
  19283. *
  19284. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  19285. * @link www.doctrine-project.org
  19286. * @since 2.0
  19287. * @version $Revision$
  19288. * @author Benjamin Eberlei <kontakt@beberlei.de>
  19289. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19290. * @author Jonathan Wage <jonwage@gmail.com>
  19291. * @author Roman Borschel <roman@code-factory.org>
  19292. * @author David Abdemoulaie <dave@hobodave.com>
  19293. * @todo Rename: APCCache
  19294. */
  19295. class ApcCache extends AbstractCache
  19296. {
  19297. /**
  19298. * {@inheritdoc}
  19299. */
  19300. public function getIds()
  19301. {
  19302. $ci = apc_cache_info('user');
  19303. $keys = array();
  19304. foreach ($ci['cache_list'] as $entry) {
  19305. $keys[] = $entry['info'];
  19306. }
  19307. return $keys;
  19308. }
  19309. /**
  19310. * {@inheritdoc}
  19311. */
  19312. protected function _doFetch($id)
  19313. {
  19314. return apc_fetch($id);
  19315. }
  19316. /**
  19317. * {@inheritdoc}
  19318. */
  19319. protected function _doContains($id)
  19320. {
  19321. $found = false;
  19322. apc_fetch($id, $found);
  19323. return $found;
  19324. }
  19325. /**
  19326. * {@inheritdoc}
  19327. */
  19328. protected function _doSave($id, $data, $lifeTime = 0)
  19329. {
  19330. return (bool) apc_store($id, $data, (int) $lifeTime);
  19331. }
  19332. /**
  19333. * {@inheritdoc}
  19334. */
  19335. protected function _doDelete($id)
  19336. {
  19337. return apc_delete($id);
  19338. }
  19339. }<?php
  19340. /*
  19341. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19342. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19343. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19344. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19345. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19346. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19347. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19348. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19349. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19350. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19351. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19352. *
  19353. * This software consists of voluntary contributions made by many individuals
  19354. * and is licensed under the LGPL. For more information, see
  19355. * <http://www.doctrine-project.org>.
  19356. */
  19357. namespace Doctrine\Common\Cache;
  19358. /**
  19359. * Base class for cache driver implementations.
  19360. *
  19361. * @since 2.0
  19362. * @author Benjamin Eberlei <kontakt@beberlei.de>
  19363. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19364. * @author Jonathan Wage <jonwage@gmail.com>
  19365. * @author Roman Borschel <roman@code-factory.org>
  19366. */
  19367. abstract class AbstractCache implements Cache
  19368. {
  19369. /** @var string The cache id to store the index of cache ids under */
  19370. private $_cacheIdsIndexId = 'doctrine_cache_ids';
  19371. /** @var string The namespace to prefix all cache ids with */
  19372. private $_namespace = null;
  19373. /**
  19374. * Set the namespace to prefix all cache ids with.
  19375. *
  19376. * @param string $namespace
  19377. * @return void
  19378. */
  19379. public function setNamespace($namespace)
  19380. {
  19381. $this->_namespace = $namespace;
  19382. }
  19383. /**
  19384. * {@inheritdoc}
  19385. */
  19386. public function fetch($id)
  19387. {
  19388. return $this->_doFetch($this->_getNamespacedId($id));
  19389. }
  19390. /**
  19391. * {@inheritdoc}
  19392. */
  19393. public function contains($id)
  19394. {
  19395. return $this->_doContains($this->_getNamespacedId($id));
  19396. }
  19397. /**
  19398. * {@inheritdoc}
  19399. */
  19400. public function save($id, $data, $lifeTime = 0)
  19401. {
  19402. return $this->_doSave($this->_getNamespacedId($id), $data, $lifeTime);
  19403. }
  19404. /**
  19405. * {@inheritdoc}
  19406. */
  19407. public function delete($id)
  19408. {
  19409. $id = $this->_getNamespacedId($id);
  19410. if (strpos($id, '*') !== false) {
  19411. return $this->deleteByRegex('/' . str_replace('*', '.*', $id) . '/');
  19412. }
  19413. return $this->_doDelete($id);
  19414. }
  19415. /**
  19416. * Delete all cache entries.
  19417. *
  19418. * @return array $deleted Array of the deleted cache ids
  19419. */
  19420. public function deleteAll()
  19421. {
  19422. $ids = $this->getIds();
  19423. foreach ($ids as $id) {
  19424. $this->delete($id);
  19425. }
  19426. return $ids;
  19427. }
  19428. /**
  19429. * Delete cache entries where the id matches a PHP regular expressions
  19430. *
  19431. * @param string $regex
  19432. * @return array $deleted Array of the deleted cache ids
  19433. */
  19434. public function deleteByRegex($regex)
  19435. {
  19436. $deleted = array();
  19437. $ids = $this->getIds();
  19438. foreach ($ids as $id) {
  19439. if (preg_match($regex, $id)) {
  19440. $this->delete($id);
  19441. $deleted[] = $id;
  19442. }
  19443. }
  19444. return $deleted;
  19445. }
  19446. /**
  19447. * Delete cache entries where the id has the passed prefix
  19448. *
  19449. * @param string $prefix
  19450. * @return array $deleted Array of the deleted cache ids
  19451. */
  19452. public function deleteByPrefix($prefix)
  19453. {
  19454. $deleted = array();
  19455. $prefix = $this->_getNamespacedId($prefix);
  19456. $ids = $this->getIds();
  19457. foreach ($ids as $id) {
  19458. if (strpos($id, $prefix) === 0) {
  19459. $this->delete($id);
  19460. $deleted[] = $id;
  19461. }
  19462. }
  19463. return $deleted;
  19464. }
  19465. /**
  19466. * Delete cache entries where the id has the passed suffix
  19467. *
  19468. * @param string $suffix
  19469. * @return array $deleted Array of the deleted cache ids
  19470. */
  19471. public function deleteBySuffix($suffix)
  19472. {
  19473. $deleted = array();
  19474. $ids = $this->getIds();
  19475. foreach ($ids as $id) {
  19476. if (substr($id, -1 * strlen($suffix)) === $suffix) {
  19477. $this->delete($id);
  19478. $deleted[] = $id;
  19479. }
  19480. }
  19481. return $deleted;
  19482. }
  19483. /**
  19484. * Prefix the passed id with the configured namespace value
  19485. *
  19486. * @param string $id The id to namespace
  19487. * @return string $id The namespaced id
  19488. */
  19489. private function _getNamespacedId($id)
  19490. {
  19491. if ( ! $this->_namespace || strpos($id, $this->_namespace) === 0) {
  19492. return $id;
  19493. } else {
  19494. return $this->_namespace . $id;
  19495. }
  19496. }
  19497. /**
  19498. * Fetches an entry from the cache.
  19499. *
  19500. * @param string $id cache id The id of the cache entry to fetch.
  19501. * @return string The cached data or FALSE, if no cache entry exists for the given id.
  19502. */
  19503. abstract protected function _doFetch($id);
  19504. /**
  19505. * Test if an entry exists in the cache.
  19506. *
  19507. * @param string $id cache id The cache id of the entry to check for.
  19508. * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
  19509. */
  19510. abstract protected function _doContains($id);
  19511. /**
  19512. * Puts data into the cache.
  19513. *
  19514. * @param string $id The cache id.
  19515. * @param string $data The cache entry/data.
  19516. * @param int $lifeTime The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime).
  19517. * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
  19518. */
  19519. abstract protected function _doSave($id, $data, $lifeTime = false);
  19520. /**
  19521. * Deletes a cache entry.
  19522. *
  19523. * @param string $id cache id
  19524. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
  19525. */
  19526. abstract protected function _doDelete($id);
  19527. /**
  19528. * Get an array of all the cache ids stored
  19529. *
  19530. * @return array $ids
  19531. */
  19532. abstract public function getIds();
  19533. }<?php
  19534. /*
  19535. * $Id$
  19536. *
  19537. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19538. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19539. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19540. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19541. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19542. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19543. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19544. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19545. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19546. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19547. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19548. *
  19549. * This software consists of voluntary contributions made by many individuals
  19550. * and is licensed under the LGPL. For more information, see
  19551. * <http://www.doctrine-project.org>.
  19552. */
  19553. namespace Doctrine\Common\Util;
  19554. /**
  19555. * Static class containing most used debug methods.
  19556. *
  19557. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  19558. * @link www.doctrine-project.org
  19559. * @since 2.0
  19560. * @version $Revision: 3938 $
  19561. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19562. * @author Jonathan Wage <jonwage@gmail.com>
  19563. * @author Roman Borschel <roman@code-factory.org>
  19564. * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
  19565. */
  19566. final class Debug
  19567. {
  19568. /**
  19569. * Private constructor (prevents from instantiation)
  19570. *
  19571. */
  19572. private function __construct() {}
  19573. /**
  19574. * Prints a dump of the public, protected and private properties of $var.
  19575. *
  19576. * @static
  19577. * @link http://xdebug.org/
  19578. * @param mixed $var
  19579. * @param integer $maxDepth Maximum nesting level for object properties
  19580. */
  19581. public static function dump($var, $maxDepth = 2)
  19582. {
  19583. ini_set('html_errors', 'On');
  19584. if (extension_loaded('xdebug')) {
  19585. ini_set('xdebug.var_display_max_depth', $maxDepth);
  19586. }
  19587. $var = self::export($var, $maxDepth++);
  19588. ob_start();
  19589. var_dump($var);
  19590. $dump = ob_get_contents();
  19591. ob_end_clean();
  19592. echo strip_tags(html_entity_decode($dump));
  19593. ini_set('html_errors', 'Off');
  19594. }
  19595. public static function export($var, $maxDepth)
  19596. {
  19597. $return = null;
  19598. $isObj = is_object($var);
  19599. if ($isObj && in_array('Doctrine\Common\Collections\Collection', class_implements($var))) {
  19600. $var = $var->toArray();
  19601. }
  19602. if ($maxDepth) {
  19603. if (is_array($var)) {
  19604. $return = array();
  19605. foreach ($var as $k => $v) {
  19606. $return[$k] = self::export($v, $maxDepth - 1);
  19607. }
  19608. } else if ($isObj) {
  19609. if ($var instanceof \DateTime) {
  19610. $return = $var->format('c');
  19611. } else {
  19612. $reflClass = new \ReflectionClass(get_class($var));
  19613. $return = new \stdclass();
  19614. $return->{'__CLASS__'} = get_class($var);
  19615. if ($var instanceof \Doctrine\ORM\Proxy\Proxy && ! $var->__isInitialized__) {
  19616. $reflProperty = $reflClass->getProperty('_identifier');
  19617. $reflProperty->setAccessible(true);
  19618. foreach ($reflProperty->getValue($var) as $name => $value) {
  19619. $return->$name = self::export($value, $maxDepth - 1);
  19620. }
  19621. } else {
  19622. $excludeProperties = array();
  19623. if ($var instanceof \Doctrine\ORM\Proxy\Proxy) {
  19624. $excludeProperties = array('_entityPersister', '__isInitialized__', '_identifier');
  19625. }
  19626. foreach ($reflClass->getProperties() as $reflProperty) {
  19627. $name = $reflProperty->getName();
  19628. if ( ! in_array($name, $excludeProperties)) {
  19629. $reflProperty->setAccessible(true);
  19630. $return->$name = self::export($reflProperty->getValue($var), $maxDepth - 1);
  19631. }
  19632. }
  19633. }
  19634. }
  19635. } else {
  19636. $return = $var;
  19637. }
  19638. } else {
  19639. $return = is_object($var) ? get_class($var)
  19640. : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
  19641. }
  19642. return $return;
  19643. }
  19644. public static function toString($obj)
  19645. {
  19646. return method_exists('__toString', $obj) ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
  19647. }
  19648. }
  19649. <?php
  19650. /*
  19651. * $Id: Inflector.php 3189 2007-11-18 20:37:44Z meus $
  19652. *
  19653. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19654. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19655. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19656. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19657. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19658. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19659. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19660. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19661. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19662. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19663. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19664. *
  19665. * This software consists of voluntary contributions made by many individuals
  19666. * and is licensed under the LGPL. For more information, see
  19667. * <http://www.doctrine-project.org>.
  19668. */
  19669. namespace Doctrine\Common\Util;
  19670. /**
  19671. * Doctrine inflector has static methods for inflecting text
  19672. *
  19673. * The methods in these classes are from several different sources collected
  19674. * across several different php projects and several different authors. The
  19675. * original author names and emails are not known
  19676. *
  19677. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  19678. * @link www.doctrine-project.org
  19679. * @since 1.0
  19680. * @version $Revision: 3189 $
  19681. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  19682. * @author Jonathan H. Wage <jonwage@gmail.com>
  19683. */
  19684. class Inflector
  19685. {
  19686. /**
  19687. * Convert word in to the format for a Doctrine table name. Converts 'ModelName' to 'model_name'
  19688. *
  19689. * @param string $word Word to tableize
  19690. * @return string $word Tableized word
  19691. */
  19692. public static function tableize($word)
  19693. {
  19694. return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $word));
  19695. }
  19696. /**
  19697. * Convert a word in to the format for a Doctrine class name. Converts 'table_name' to 'TableName'
  19698. *
  19699. * @param string $word Word to classify
  19700. * @return string $word Classified word
  19701. */
  19702. public static function classify($word)
  19703. {
  19704. return str_replace(" ", "", ucwords(strtr($word, "_-", " ")));
  19705. }
  19706. /**
  19707. * Camelize a word. This uses the classify() method and turns the first character to lowercase
  19708. *
  19709. * @param string $word
  19710. * @return string $word
  19711. */
  19712. public static function camelize($word)
  19713. {
  19714. return lcfirst(self::classify($word));
  19715. }
  19716. }<?php
  19717. /*
  19718. * $Id$
  19719. *
  19720. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19721. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19722. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19723. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19724. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19725. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19726. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19727. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19728. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19729. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19730. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19731. *
  19732. * This software consists of voluntary contributions made by many individuals
  19733. * and is licensed under the LGPL. For more information, see
  19734. * <http://www.doctrine-project.org>.
  19735. */
  19736. namespace Doctrine\Common;
  19737. /**
  19738. * Contract for classes that provide the service of notifying listeners of
  19739. * changes to their properties.
  19740. *
  19741. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  19742. * @link www.doctrine-project.org
  19743. * @since 2.0
  19744. * @version $Revision: 3938 $
  19745. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19746. * @author Jonathan Wage <jonwage@gmail.com>
  19747. * @author Roman Borschel <roman@code-factory.org>
  19748. */
  19749. interface NotifyPropertyChanged
  19750. {
  19751. /**
  19752. * Adds a listener that wants to be notified about property changes.
  19753. *
  19754. * @param PropertyChangedListener $listener
  19755. */
  19756. function addPropertyChangedListener(PropertyChangedListener $listener);
  19757. }
  19758. <?php
  19759. /*
  19760. * $Id$
  19761. *
  19762. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19763. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19764. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19765. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19766. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19767. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19768. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19769. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19770. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19771. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19772. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19773. *
  19774. * This software consists of voluntary contributions made by many individuals
  19775. * and is licensed under the LGPL. For more information, see
  19776. * <http://www.doctrine-project.org>.
  19777. */
  19778. namespace Doctrine\Common;
  19779. /**
  19780. * Contract for classes that are potential listeners of a <tt>NotifyPropertyChanged</tt>
  19781. * implementor.
  19782. *
  19783. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  19784. * @link www.doctrine-project.org
  19785. * @since 2.0
  19786. * @version $Revision: 3938 $
  19787. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19788. * @author Jonathan Wage <jonwage@gmail.com>
  19789. * @author Roman Borschel <roman@code-factory.org>
  19790. */
  19791. interface PropertyChangedListener
  19792. {
  19793. /**
  19794. * Notifies the listener of a property change.
  19795. *
  19796. * @param object $sender The object on which the property changed.
  19797. * @param string $propertyName The name of the property that changed.
  19798. * @param mixed $oldValue The old value of the property that changed.
  19799. * @param mixed $newValue The new value of the property that changed.
  19800. */
  19801. function propertyChanged($sender, $propertyName, $oldValue, $newValue);
  19802. }
  19803. <?php
  19804. /*
  19805. * $Id$
  19806. *
  19807. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19808. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19809. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19810. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19811. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19812. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19813. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19814. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19815. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19816. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19817. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19818. *
  19819. * This software consists of voluntary contributions made by many individuals
  19820. * and is licensed under the LGPL. For more information, see
  19821. * <http://www.doctrine-project.org>.
  19822. */
  19823. namespace Doctrine\Common;
  19824. use Doctrine\Common\Events\Event;
  19825. /**
  19826. * The EventManager is the central point of Doctrine's event listener system.
  19827. * Listeners are registered on the manager and events are dispatched through the
  19828. * manager.
  19829. *
  19830. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  19831. * @link www.doctrine-project.org
  19832. * @since 2.0
  19833. * @version $Revision: 3938 $
  19834. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19835. * @author Jonathan Wage <jonwage@gmail.com>
  19836. * @author Roman Borschel <roman@code-factory.org>
  19837. */
  19838. class EventManager
  19839. {
  19840. /**
  19841. * Map of registered listeners.
  19842. * <event> => <listeners>
  19843. *
  19844. * @var array
  19845. */
  19846. private $_listeners = array();
  19847. /**
  19848. * Dispatches an event to all registered listeners.
  19849. *
  19850. * @param string $eventName The name of the event to dispatch. The name of the event is
  19851. * the name of the method that is invoked on listeners.
  19852. * @param EventArgs $eventArgs The event arguments to pass to the event handlers/listeners.
  19853. * If not supplied, the single empty EventArgs instance is used.
  19854. * @return boolean
  19855. */
  19856. public function dispatchEvent($eventName, EventArgs $eventArgs = null)
  19857. {
  19858. if (isset($this->_listeners[$eventName])) {
  19859. $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs;
  19860. foreach ($this->_listeners[$eventName] as $listener) {
  19861. $listener->$eventName($eventArgs);
  19862. }
  19863. }
  19864. }
  19865. /**
  19866. * Gets the listeners of a specific event or all listeners.
  19867. *
  19868. * @param string $event The name of the event.
  19869. * @return array The event listeners for the specified event, or all event listeners.
  19870. */
  19871. public function getListeners($event = null)
  19872. {
  19873. return $event ? $this->_listeners[$event] : $this->_listeners;
  19874. }
  19875. /**
  19876. * Checks whether an event has any registered listeners.
  19877. *
  19878. * @param string $event
  19879. * @return boolean TRUE if the specified event has any listeners, FALSE otherwise.
  19880. */
  19881. public function hasListeners($event)
  19882. {
  19883. return isset($this->_listeners[$event]) && $this->_listeners[$event];
  19884. }
  19885. /**
  19886. * Adds an event listener that listens on the specified events.
  19887. *
  19888. * @param string|array $events The event(s) to listen on.
  19889. * @param object $listener The listener object.
  19890. */
  19891. public function addEventListener($events, $listener)
  19892. {
  19893. // Picks the hash code related to that listener
  19894. $hash = spl_object_hash($listener);
  19895. foreach ((array) $events as $event) {
  19896. // Overrides listener if a previous one was associated already
  19897. // Prevents duplicate listeners on same event (same instance only)
  19898. $this->_listeners[$event][$hash] = $listener;
  19899. }
  19900. }
  19901. /**
  19902. * Removes an event listener from the specified events.
  19903. *
  19904. * @param string|array $events
  19905. * @param object $listener
  19906. */
  19907. public function removeEventListener($events, $listener)
  19908. {
  19909. // Picks the hash code related to that listener
  19910. $hash = spl_object_hash($listener);
  19911. foreach ((array) $events as $event) {
  19912. // Check if actually have this listener associated
  19913. if (isset($this->_listeners[$event][$hash])) {
  19914. unset($this->_listeners[$event][$hash]);
  19915. }
  19916. }
  19917. }
  19918. /**
  19919. * Adds an EventSubscriber. The subscriber is asked for all the events he is
  19920. * interested in and added as a listener for these events.
  19921. *
  19922. * @param Doctrine\Common\EventSubscriber $subscriber The subscriber.
  19923. */
  19924. public function addEventSubscriber(EventSubscriber $subscriber)
  19925. {
  19926. $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
  19927. }
  19928. }<?php
  19929. /*
  19930. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19931. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19932. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19933. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19934. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19935. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19936. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19937. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19938. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19939. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19940. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19941. *
  19942. * This software consists of voluntary contributions made by many individuals
  19943. * and is licensed under the LGPL. For more information, see
  19944. * <http://www.doctrine-project.org>.
  19945. */
  19946. namespace Doctrine\Common;
  19947. /**
  19948. * Base class for writing simple lexers, i.e. for creating small DSLs.
  19949. *
  19950. * @since 2.0
  19951. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  19952. * @author Jonathan Wage <jonwage@gmail.com>
  19953. * @author Roman Borschel <roman@code-factory.org>
  19954. * @todo Rename: AbstractLexer
  19955. */
  19956. abstract class Lexer
  19957. {
  19958. /**
  19959. * @var array Array of scanned tokens
  19960. */
  19961. private $tokens = array();
  19962. /**
  19963. * @var integer Current lexer position in input string
  19964. */
  19965. private $position = 0;
  19966. /**
  19967. * @var integer Current peek of current lexer position
  19968. */
  19969. private $peek = 0;
  19970. /**
  19971. * @var array The next token in the input.
  19972. */
  19973. public $lookahead;
  19974. /**
  19975. * @var array The last matched/seen token.
  19976. */
  19977. public $token;
  19978. /**
  19979. * Sets the input data to be tokenized.
  19980. *
  19981. * The Lexer is immediately reset and the new input tokenized.
  19982. * Any unprocessed tokens from any previous input are lost.
  19983. *
  19984. * @param string $input The input to be tokenized.
  19985. */
  19986. public function setInput($input)
  19987. {
  19988. $this->tokens = array();
  19989. $this->reset();
  19990. $this->scan($input);
  19991. }
  19992. /**
  19993. * Resets the lexer.
  19994. */
  19995. public function reset()
  19996. {
  19997. $this->lookahead = null;
  19998. $this->token = null;
  19999. $this->peek = 0;
  20000. $this->position = 0;
  20001. }
  20002. /**
  20003. * Resets the peek pointer to 0.
  20004. */
  20005. public function resetPeek()
  20006. {
  20007. $this->peek = 0;
  20008. }
  20009. /**
  20010. * Resets the lexer position on the input to the given position.
  20011. *
  20012. * @param integer $position Position to place the lexical scanner
  20013. */
  20014. public function resetPosition($position = 0)
  20015. {
  20016. $this->position = $position;
  20017. }
  20018. /**
  20019. * Checks whether a given token matches the current lookahead.
  20020. *
  20021. * @param integer|string $token
  20022. * @return boolean
  20023. */
  20024. public function isNextToken($token)
  20025. {
  20026. return $this->lookahead['type'] === $token;
  20027. }
  20028. /**
  20029. * Moves to the next token in the input string.
  20030. *
  20031. * A token is an associative array containing three items:
  20032. * - 'value' : the string value of the token in the input string
  20033. * - 'type' : the type of the token (identifier, numeric, string, input
  20034. * parameter, none)
  20035. * - 'position' : the position of the token in the input string
  20036. *
  20037. * @return array|null the next token; null if there is no more tokens left
  20038. */
  20039. public function moveNext()
  20040. {
  20041. $this->peek = 0;
  20042. $this->token = $this->lookahead;
  20043. $this->lookahead = (isset($this->tokens[$this->position]))
  20044. ? $this->tokens[$this->position++] : null;
  20045. return $this->lookahead !== null;
  20046. }
  20047. /**
  20048. * Tells the lexer to skip input tokens until it sees a token with the given value.
  20049. *
  20050. * @param $type The token type to skip until.
  20051. */
  20052. public function skipUntil($type)
  20053. {
  20054. while ($this->lookahead !== null && $this->lookahead['type'] !== $type) {
  20055. $this->moveNext();
  20056. }
  20057. }
  20058. /**
  20059. * Checks if given value is identical to the given token
  20060. *
  20061. * @param mixed $value
  20062. * @param integer $token
  20063. * @return boolean
  20064. */
  20065. public function isA($value, $token)
  20066. {
  20067. return $this->getType($value) === $token;
  20068. }
  20069. /**
  20070. * Moves the lookahead token forward.
  20071. *
  20072. * @return array | null The next token or NULL if there are no more tokens ahead.
  20073. */
  20074. public function peek()
  20075. {
  20076. if (isset($this->tokens[$this->position + $this->peek])) {
  20077. return $this->tokens[$this->position + $this->peek++];
  20078. } else {
  20079. return null;
  20080. }
  20081. }
  20082. /**
  20083. * Peeks at the next token, returns it and immediately resets the peek.
  20084. *
  20085. * @return array|null The next token or NULL if there are no more tokens ahead.
  20086. */
  20087. public function glimpse()
  20088. {
  20089. $peek = $this->peek();
  20090. $this->peek = 0;
  20091. return $peek;
  20092. }
  20093. /**
  20094. * Scans the input string for tokens.
  20095. *
  20096. * @param string $input a query string
  20097. */
  20098. protected function scan($input)
  20099. {
  20100. static $regex;
  20101. if ( ! isset($regex)) {
  20102. $regex = '/(' . implode(')|(', $this->getCatchablePatterns()) . ')|'
  20103. . implode('|', $this->getNonCatchablePatterns()) . '/i';
  20104. }
  20105. $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
  20106. $matches = preg_split($regex, $input, -1, $flags);
  20107. foreach ($matches as $match) {
  20108. // Must remain before 'value' assignment since it can change content
  20109. $type = $this->getType($match[0]);
  20110. $this->tokens[] = array(
  20111. 'value' => $match[0],
  20112. 'type' => $type,
  20113. 'position' => $match[1],
  20114. );
  20115. }
  20116. }
  20117. /**
  20118. * Gets the literal for a given token.
  20119. *
  20120. * @param integer $token
  20121. * @return string
  20122. */
  20123. public function getLiteral($token)
  20124. {
  20125. $className = get_class($this);
  20126. $reflClass = new \ReflectionClass($className);
  20127. $constants = $reflClass->getConstants();
  20128. foreach ($constants as $name => $value) {
  20129. if ($value === $token) {
  20130. return $className . '::' . $name;
  20131. }
  20132. }
  20133. return $token;
  20134. }
  20135. /**
  20136. * Lexical catchable patterns.
  20137. *
  20138. * @return array
  20139. */
  20140. abstract protected function getCatchablePatterns();
  20141. /**
  20142. * Lexical non-catchable patterns.
  20143. *
  20144. * @return array
  20145. */
  20146. abstract protected function getNonCatchablePatterns();
  20147. /**
  20148. * Retrieve token type. Also processes the token value if necessary.
  20149. *
  20150. * @param string $value
  20151. * @return integer
  20152. */
  20153. abstract protected function getType(&$value);
  20154. }<?php
  20155. /*
  20156. * $Id: EventListener.php 4653 2008-07-10 17:17:58Z romanb $
  20157. *
  20158. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20159. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20160. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20161. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20162. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20163. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20164. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20165. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20166. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20167. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20168. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20169. *
  20170. * This software consists of voluntary contributions made by many individuals
  20171. * and is licensed under the LGPL. For more information, see
  20172. * <http://www.doctrine-project.org>.
  20173. */
  20174. namespace Doctrine\Common;
  20175. /**
  20176. * An EventSubscriber knows himself what events he is interested in.
  20177. * If an EventSubscriber is added to an EventManager, the manager invokes
  20178. * {@link getSubscribedEvents} and registers the subscriber as a listener for all
  20179. * returned events.
  20180. *
  20181. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  20182. * @link www.doctrine-project.org
  20183. * @since 2.0
  20184. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  20185. * @author Jonathan Wage <jonwage@gmail.com>
  20186. * @author Roman Borschel <roman@code-factory.org>
  20187. */
  20188. interface EventSubscriber
  20189. {
  20190. /**
  20191. * Returns an array of events this subscriber wants to listen to.
  20192. *
  20193. * @return array
  20194. */
  20195. public function getSubscribedEvents();
  20196. }
  20197. <?php
  20198. namespace Symfony\Component\Yaml;
  20199. /*
  20200. * This file is part of the symfony package.
  20201. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  20202. *
  20203. * For the full copyright and license information, please view the LICENSE
  20204. * file that was distributed with this source code.
  20205. */
  20206. /**
  20207. * Yaml offers convenience methods to load and dump YAML.
  20208. *
  20209. * @package symfony
  20210. * @subpackage yaml
  20211. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20212. */
  20213. class Yaml
  20214. {
  20215. static protected $spec = '1.2';
  20216. /**
  20217. * Sets the YAML specification version to use.
  20218. *
  20219. * @param string $version The YAML specification version
  20220. */
  20221. static public function setSpecVersion($version)
  20222. {
  20223. if (!in_array($version, array('1.1', '1.2')))
  20224. {
  20225. throw new \InvalidArgumentException(sprintf('Version %s of the YAML specifications is not supported', $version));
  20226. }
  20227. self::$spec = $version;
  20228. }
  20229. /**
  20230. * Gets the YAML specification version to use.
  20231. *
  20232. * @return string The YAML specification version
  20233. */
  20234. static public function getSpecVersion()
  20235. {
  20236. return self::$spec;
  20237. }
  20238. /**
  20239. * Loads YAML into a PHP array.
  20240. *
  20241. * The load method, when supplied with a YAML stream (string or file),
  20242. * will do its best to convert YAML in a file into a PHP array.
  20243. *
  20244. * Usage:
  20245. * <code>
  20246. * $array = Yaml::load('config.yml');
  20247. * print_r($array);
  20248. * </code>
  20249. *
  20250. * @param string $input Path of YAML file or string containing YAML
  20251. *
  20252. * @return array The YAML converted to a PHP array
  20253. *
  20254. * @throws \InvalidArgumentException If the YAML is not valid
  20255. */
  20256. public static function load($input)
  20257. {
  20258. $file = '';
  20259. // if input is a file, process it
  20260. if (strpos($input, "\n") === false && is_file($input))
  20261. {
  20262. $file = $input;
  20263. ob_start();
  20264. $retval = include($input);
  20265. $content = ob_get_clean();
  20266. // if an array is returned by the config file assume it's in plain php form else in YAML
  20267. $input = is_array($retval) ? $retval : $content;
  20268. }
  20269. // if an array is returned by the config file assume it's in plain php form else in YAML
  20270. if (is_array($input))
  20271. {
  20272. return $input;
  20273. }
  20274. $yaml = new Parser();
  20275. try
  20276. {
  20277. $ret = $yaml->parse($input);
  20278. }
  20279. catch (\Exception $e)
  20280. {
  20281. throw new \InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ? sprintf('file "%s"', $file) : 'string', $e->getMessage()));
  20282. }
  20283. return $ret;
  20284. }
  20285. /**
  20286. * Dumps a PHP array to a YAML string.
  20287. *
  20288. * The dump method, when supplied with an array, will do its best
  20289. * to convert the array into friendly YAML.
  20290. *
  20291. * @param array $array PHP array
  20292. * @param integer $inline The level where you switch to inline YAML
  20293. *
  20294. * @return string A YAML string representing the original PHP array
  20295. */
  20296. public static function dump($array, $inline = 2)
  20297. {
  20298. $yaml = new Dumper();
  20299. return $yaml->dump($array, $inline);
  20300. }
  20301. }
  20302. <?php
  20303. namespace Symfony\Component\Yaml;
  20304. /*
  20305. * This file is part of the symfony package.
  20306. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  20307. *
  20308. * For the full copyright and license information, please view the LICENSE
  20309. * file that was distributed with this source code.
  20310. */
  20311. /**
  20312. * Dumper dumps PHP variables to YAML strings.
  20313. *
  20314. * @package symfony
  20315. * @subpackage yaml
  20316. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20317. */
  20318. class Dumper
  20319. {
  20320. /**
  20321. * Dumps a PHP value to YAML.
  20322. *
  20323. * @param mixed $input The PHP value
  20324. * @param integer $inline The level where you switch to inline YAML
  20325. * @param integer $indent The level o indentation indentation (used internally)
  20326. *
  20327. * @return string The YAML representation of the PHP value
  20328. */
  20329. public function dump($input, $inline = 0, $indent = 0)
  20330. {
  20331. $output = '';
  20332. $prefix = $indent ? str_repeat(' ', $indent) : '';
  20333. if ($inline <= 0 || !is_array($input) || empty($input))
  20334. {
  20335. $output .= $prefix.Inline::dump($input);
  20336. }
  20337. else
  20338. {
  20339. $isAHash = array_keys($input) !== range(0, count($input) - 1);
  20340. foreach ($input as $key => $value)
  20341. {
  20342. $willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
  20343. $output .= sprintf('%s%s%s%s',
  20344. $prefix,
  20345. $isAHash ? Inline::dump($key).':' : '-',
  20346. $willBeInlined ? ' ' : "\n",
  20347. $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + 2)
  20348. ).($willBeInlined ? "\n" : '');
  20349. }
  20350. }
  20351. return $output;
  20352. }
  20353. }
  20354. <?php
  20355. namespace Symfony\Component\Yaml;
  20356. /*
  20357. * This file is part of the symfony package.
  20358. *
  20359. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  20360. *
  20361. * For the full copyright and license information, please view the LICENSE
  20362. * file that was distributed with this source code.
  20363. */
  20364. /**
  20365. * Exception class used by all exceptions thrown by the component.
  20366. *
  20367. * @package symfony
  20368. * @subpackage yaml
  20369. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20370. */
  20371. class ParserException extends Exception
  20372. {
  20373. }
  20374. <?php
  20375. namespace Symfony\Component\Yaml;
  20376. /*
  20377. * This file is part of the symfony package.
  20378. *
  20379. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  20380. *
  20381. * For the full copyright and license information, please view the LICENSE
  20382. * file that was distributed with this source code.
  20383. */
  20384. /**
  20385. * Exception class used by all exceptions thrown by the component.
  20386. *
  20387. * @package symfony
  20388. * @subpackage yaml
  20389. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20390. */
  20391. class Exception extends \Exception
  20392. {
  20393. }
  20394. <?php
  20395. namespace Symfony\Component\Yaml;
  20396. /*
  20397. * This file is part of the symfony package.
  20398. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  20399. *
  20400. * For the full copyright and license information, please view the LICENSE
  20401. * file that was distributed with this source code.
  20402. */
  20403. /**
  20404. * Inline implements a YAML parser/dumper for the YAML inline syntax.
  20405. *
  20406. * @package symfony
  20407. * @subpackage yaml
  20408. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20409. */
  20410. class Inline
  20411. {
  20412. const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')';
  20413. /**
  20414. * Convert a YAML string to a PHP array.
  20415. *
  20416. * @param string $value A YAML string
  20417. *
  20418. * @return array A PHP array representing the YAML string
  20419. */
  20420. static public function load($value)
  20421. {
  20422. $value = trim($value);
  20423. if (0 == strlen($value))
  20424. {
  20425. return '';
  20426. }
  20427. switch ($value[0])
  20428. {
  20429. case '[':
  20430. return self::parseSequence($value);
  20431. case '{':
  20432. return self::parseMapping($value);
  20433. default:
  20434. return self::parseScalar($value);
  20435. }
  20436. }
  20437. /**
  20438. * Dumps a given PHP variable to a YAML string.
  20439. *
  20440. * @param mixed $value The PHP variable to convert
  20441. *
  20442. * @return string The YAML string representing the PHP array
  20443. */
  20444. static public function dump($value)
  20445. {
  20446. $trueValues = '1.1' == Yaml::getSpecVersion() ? array('true', 'on', '+', 'yes', 'y') : array('true');
  20447. $falseValues = '1.1' == Yaml::getSpecVersion() ? array('false', 'off', '-', 'no', 'n') : array('false');
  20448. switch (true)
  20449. {
  20450. case is_resource($value):
  20451. throw new Exception('Unable to dump PHP resources in a YAML file.');
  20452. case is_object($value):
  20453. return '!!php/object:'.serialize($value);
  20454. case is_array($value):
  20455. return self::dumpArray($value);
  20456. case null === $value:
  20457. return 'null';
  20458. case true === $value:
  20459. return 'true';
  20460. case false === $value:
  20461. return 'false';
  20462. case ctype_digit($value):
  20463. return is_string($value) ? "'$value'" : (int) $value;
  20464. case is_numeric($value):
  20465. return is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : (is_string($value) ? "'$value'" : $value);
  20466. case false !== strpos($value, "\n") || false !== strpos($value, "\r"):
  20467. return sprintf('"%s"', str_replace(array('"', "\n", "\r"), array('\\"', '\n', '\r'), $value));
  20468. case preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ - ? | < > = ! % @ ` ]/x', $value):
  20469. return sprintf("'%s'", str_replace('\'', '\'\'', $value));
  20470. case '' == $value:
  20471. return "''";
  20472. case preg_match(self::getTimestampRegex(), $value):
  20473. return "'$value'";
  20474. case in_array(strtolower($value), $trueValues):
  20475. return "'$value'";
  20476. case in_array(strtolower($value), $falseValues):
  20477. return "'$value'";
  20478. case in_array(strtolower($value), array('null', '~')):
  20479. return "'$value'";
  20480. default:
  20481. return $value;
  20482. }
  20483. }
  20484. /**
  20485. * Dumps a PHP array to a YAML string.
  20486. *
  20487. * @param array $value The PHP array to dump
  20488. *
  20489. * @return string The YAML string representing the PHP array
  20490. */
  20491. static protected function dumpArray($value)
  20492. {
  20493. // array
  20494. $keys = array_keys($value);
  20495. if (
  20496. (1 == count($keys) && '0' == $keys[0])
  20497. ||
  20498. (count($keys) > 1 && array_reduce($keys, function ($v, $w) { return (integer) $v + $w; }, 0) == count($keys) * (count($keys) - 1) / 2))
  20499. {
  20500. $output = array();
  20501. foreach ($value as $val)
  20502. {
  20503. $output[] = self::dump($val);
  20504. }
  20505. return sprintf('[%s]', implode(', ', $output));
  20506. }
  20507. // mapping
  20508. $output = array();
  20509. foreach ($value as $key => $val)
  20510. {
  20511. $output[] = sprintf('%s: %s', self::dump($key), self::dump($val));
  20512. }
  20513. return sprintf('{ %s }', implode(', ', $output));
  20514. }
  20515. /**
  20516. * Parses a scalar to a YAML string.
  20517. *
  20518. * @param scalar $scalar
  20519. * @param string $delimiters
  20520. * @param array $stringDelimiter
  20521. * @param integer $i
  20522. * @param boolean $evaluate
  20523. *
  20524. * @return string A YAML string
  20525. */
  20526. static public function parseScalar($scalar, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true)
  20527. {
  20528. if (in_array($scalar[$i], $stringDelimiters))
  20529. {
  20530. // quoted scalar
  20531. $output = self::parseQuotedScalar($scalar, $i);
  20532. }
  20533. else
  20534. {
  20535. // "normal" string
  20536. if (!$delimiters)
  20537. {
  20538. $output = substr($scalar, $i);
  20539. $i += strlen($output);
  20540. // remove comments
  20541. if (false !== $strpos = strpos($output, ' #'))
  20542. {
  20543. $output = rtrim(substr($output, 0, $strpos));
  20544. }
  20545. }
  20546. else if (preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match))
  20547. {
  20548. $output = $match[1];
  20549. $i += strlen($output);
  20550. }
  20551. else
  20552. {
  20553. throw new ParserException(sprintf('Malformed inline YAML string (%s).', $scalar));
  20554. }
  20555. $output = $evaluate ? self::evaluateScalar($output) : $output;
  20556. }
  20557. return $output;
  20558. }
  20559. /**
  20560. * Parses a quoted scalar to YAML.
  20561. *
  20562. * @param string $scalar
  20563. * @param integer $i
  20564. *
  20565. * @return string A YAML string
  20566. */
  20567. static protected function parseQuotedScalar($scalar, &$i)
  20568. {
  20569. if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/A', substr($scalar, $i), $match))
  20570. {
  20571. throw new ParserException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
  20572. }
  20573. $output = substr($match[0], 1, strlen($match[0]) - 2);
  20574. if ('"' == $scalar[$i])
  20575. {
  20576. // evaluate the string
  20577. $output = str_replace(array('\\"', '\\n', '\\r'), array('"', "\n", "\r"), $output);
  20578. }
  20579. else
  20580. {
  20581. // unescape '
  20582. $output = str_replace('\'\'', '\'', $output);
  20583. }
  20584. $i += strlen($match[0]);
  20585. return $output;
  20586. }
  20587. /**
  20588. * Parses a sequence to a YAML string.
  20589. *
  20590. * @param string $sequence
  20591. * @param integer $i
  20592. *
  20593. * @return string A YAML string
  20594. */
  20595. static protected function parseSequence($sequence, &$i = 0)
  20596. {
  20597. $output = array();
  20598. $len = strlen($sequence);
  20599. $i += 1;
  20600. // [foo, bar, ...]
  20601. while ($i < $len)
  20602. {
  20603. switch ($sequence[$i])
  20604. {
  20605. case '[':
  20606. // nested sequence
  20607. $output[] = self::parseSequence($sequence, $i);
  20608. break;
  20609. case '{':
  20610. // nested mapping
  20611. $output[] = self::parseMapping($sequence, $i);
  20612. break;
  20613. case ']':
  20614. return $output;
  20615. case ',':
  20616. case ' ':
  20617. break;
  20618. default:
  20619. $isQuoted = in_array($sequence[$i], array('"', "'"));
  20620. $value = self::parseScalar($sequence, array(',', ']'), array('"', "'"), $i);
  20621. if (!$isQuoted && false !== strpos($value, ': '))
  20622. {
  20623. // embedded mapping?
  20624. try
  20625. {
  20626. $value = self::parseMapping('{'.$value.'}');
  20627. }
  20628. catch (\InvalidArgumentException $e)
  20629. {
  20630. // no, it's not
  20631. }
  20632. }
  20633. $output[] = $value;
  20634. --$i;
  20635. }
  20636. ++$i;
  20637. }
  20638. throw new ParserException(sprintf('Malformed inline YAML string %s', $sequence));
  20639. }
  20640. /**
  20641. * Parses a mapping to a YAML string.
  20642. *
  20643. * @param string $mapping
  20644. * @param integer $i
  20645. *
  20646. * @return string A YAML string
  20647. */
  20648. static protected function parseMapping($mapping, &$i = 0)
  20649. {
  20650. $output = array();
  20651. $len = strlen($mapping);
  20652. $i += 1;
  20653. // {foo: bar, bar:foo, ...}
  20654. while ($i < $len)
  20655. {
  20656. switch ($mapping[$i])
  20657. {
  20658. case ' ':
  20659. case ',':
  20660. ++$i;
  20661. continue 2;
  20662. case '}':
  20663. return $output;
  20664. }
  20665. // key
  20666. $key = self::parseScalar($mapping, array(':', ' '), array('"', "'"), $i, false);
  20667. // value
  20668. $done = false;
  20669. while ($i < $len)
  20670. {
  20671. switch ($mapping[$i])
  20672. {
  20673. case '[':
  20674. // nested sequence
  20675. $output[$key] = self::parseSequence($mapping, $i);
  20676. $done = true;
  20677. break;
  20678. case '{':
  20679. // nested mapping
  20680. $output[$key] = self::parseMapping($mapping, $i);
  20681. $done = true;
  20682. break;
  20683. case ':':
  20684. case ' ':
  20685. break;
  20686. default:
  20687. $output[$key] = self::parseScalar($mapping, array(',', '}'), array('"', "'"), $i);
  20688. $done = true;
  20689. --$i;
  20690. }
  20691. ++$i;
  20692. if ($done)
  20693. {
  20694. continue 2;
  20695. }
  20696. }
  20697. }
  20698. throw new ParserException(sprintf('Malformed inline YAML string %s', $mapping));
  20699. }
  20700. /**
  20701. * Evaluates scalars and replaces magic values.
  20702. *
  20703. * @param string $scalar
  20704. *
  20705. * @return string A YAML string
  20706. */
  20707. static protected function evaluateScalar($scalar)
  20708. {
  20709. $scalar = trim($scalar);
  20710. $trueValues = '1.1' == Yaml::getSpecVersion() ? array('true', 'on', '+', 'yes', 'y') : array('true');
  20711. $falseValues = '1.1' == Yaml::getSpecVersion() ? array('false', 'off', '-', 'no', 'n') : array('false');
  20712. switch (true)
  20713. {
  20714. case 'null' == strtolower($scalar):
  20715. case '' == $scalar:
  20716. case '~' == $scalar:
  20717. return null;
  20718. case 0 === strpos($scalar, '!str'):
  20719. return (string) substr($scalar, 5);
  20720. case 0 === strpos($scalar, '! '):
  20721. return intval(self::parseScalar(substr($scalar, 2)));
  20722. case 0 === strpos($scalar, '!!php/object:'):
  20723. return unserialize(substr($scalar, 13));
  20724. case ctype_digit($scalar):
  20725. $raw = $scalar;
  20726. $cast = intval($scalar);
  20727. return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
  20728. case in_array(strtolower($scalar), $trueValues):
  20729. return true;
  20730. case in_array(strtolower($scalar), $falseValues):
  20731. return false;
  20732. case is_numeric($scalar):
  20733. return '0x' == $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar);
  20734. case 0 == strcasecmp($scalar, '.inf'):
  20735. case 0 == strcasecmp($scalar, '.NaN'):
  20736. return -log(0);
  20737. case 0 == strcasecmp($scalar, '-.inf'):
  20738. return log(0);
  20739. case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
  20740. return floatval(str_replace(',', '', $scalar));
  20741. case preg_match(self::getTimestampRegex(), $scalar):
  20742. return strtotime($scalar);
  20743. default:
  20744. return (string) $scalar;
  20745. }
  20746. }
  20747. static protected function getTimestampRegex()
  20748. {
  20749. return <<<EOF
  20750. ~^
  20751. (?P<year>[0-9][0-9][0-9][0-9])
  20752. -(?P<month>[0-9][0-9]?)
  20753. -(?P<day>[0-9][0-9]?)
  20754. (?:(?:[Tt]|[ \t]+)
  20755. (?P<hour>[0-9][0-9]?)
  20756. :(?P<minute>[0-9][0-9])
  20757. :(?P<second>[0-9][0-9])
  20758. (?:\.(?P<fraction>[0-9]*))?
  20759. (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
  20760. (?::(?P<tz_minute>[0-9][0-9]))?))?)?
  20761. $~x
  20762. EOF;
  20763. }
  20764. }
  20765. <?php
  20766. namespace Symfony\Component\Yaml;
  20767. /*
  20768. * This file is part of the symfony package.
  20769. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  20770. *
  20771. * For the full copyright and license information, please view the LICENSE
  20772. * file that was distributed with this source code.
  20773. */
  20774. /**
  20775. * Parser parses YAML strings to convert them to PHP arrays.
  20776. *
  20777. * @package symfony
  20778. * @subpackage yaml
  20779. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20780. */
  20781. class Parser
  20782. {
  20783. protected $offset = 0;
  20784. protected $lines = array();
  20785. protected $currentLineNb = -1;
  20786. protected $currentLine = '';
  20787. protected $refs = array();
  20788. /**
  20789. * Constructor
  20790. *
  20791. * @param integer $offset The offset of YAML document (used for line numbers in error messages)
  20792. */
  20793. public function __construct($offset = 0)
  20794. {
  20795. $this->offset = $offset;
  20796. }
  20797. /**
  20798. * Parses a YAML string to a PHP value.
  20799. *
  20800. * @param string $value A YAML string
  20801. *
  20802. * @return mixed A PHP value
  20803. *
  20804. * @throws \InvalidArgumentException If the YAML is not valid
  20805. */
  20806. public function parse($value)
  20807. {
  20808. $this->currentLineNb = -1;
  20809. $this->currentLine = '';
  20810. $this->lines = explode("\n", $this->cleanup($value));
  20811. $data = array();
  20812. while ($this->moveToNextLine())
  20813. {
  20814. if ($this->isCurrentLineEmpty())
  20815. {
  20816. continue;
  20817. }
  20818. // tab?
  20819. if (preg_match('#^\t+#', $this->currentLine))
  20820. {
  20821. throw new ParserException(sprintf('A YAML file cannot contain tabs as indentation at line %d (%s).', $this->getRealCurrentLineNb() + 1, $this->currentLine));
  20822. }
  20823. $isRef = $isInPlace = $isProcessed = false;
  20824. if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#', $this->currentLine, $values))
  20825. {
  20826. if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#', $values['value'], $matches))
  20827. {
  20828. $isRef = $matches['ref'];
  20829. $values['value'] = $matches['value'];
  20830. }
  20831. // array
  20832. if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#'))
  20833. {
  20834. $c = $this->getRealCurrentLineNb() + 1;
  20835. $parser = new Parser($c);
  20836. $parser->refs =& $this->refs;
  20837. $data[] = $parser->parse($this->getNextEmbedBlock());
  20838. }
  20839. else
  20840. {
  20841. if (isset($values['leadspaces'])
  20842. && ' ' == $values['leadspaces']
  20843. && preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{].*?) *\:(\s+(?P<value>.+?))?\s*$#', $values['value'], $matches))
  20844. {
  20845. // this is a compact notation element, add to next block and parse
  20846. $c = $this->getRealCurrentLineNb();
  20847. $parser = new Parser($c);
  20848. $parser->refs =& $this->refs;
  20849. $block = $values['value'];
  20850. if (!$this->isNextLineIndented())
  20851. {
  20852. $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + 2);
  20853. }
  20854. $data[] = $parser->parse($block);
  20855. }
  20856. else
  20857. {
  20858. $data[] = $this->parseValue($values['value']);
  20859. }
  20860. }
  20861. }
  20862. else if (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"].*?) *\:(\s+(?P<value>.+?))?\s*$#', $this->currentLine, $values))
  20863. {
  20864. $key = Inline::parseScalar($values['key']);
  20865. if ('<<' === $key)
  20866. {
  20867. if (isset($values['value']) && '*' === substr($values['value'], 0, 1))
  20868. {
  20869. $isInPlace = substr($values['value'], 1);
  20870. if (!array_key_exists($isInPlace, $this->refs))
  20871. {
  20872. throw new ParserException(sprintf('Reference "%s" does not exist at line %s (%s).', $isInPlace, $this->getRealCurrentLineNb() + 1, $this->currentLine));
  20873. }
  20874. }
  20875. else
  20876. {
  20877. if (isset($values['value']) && $values['value'] !== '')
  20878. {
  20879. $value = $values['value'];
  20880. }
  20881. else
  20882. {
  20883. $value = $this->getNextEmbedBlock();
  20884. }
  20885. $c = $this->getRealCurrentLineNb() + 1;
  20886. $parser = new Parser($c);
  20887. $parser->refs =& $this->refs;
  20888. $parsed = $parser->parse($value);
  20889. $merged = array();
  20890. if (!is_array($parsed))
  20891. {
  20892. throw new ParserException(sprintf("YAML merge keys used with a scalar value instead of an array at line %s (%s)", $this->getRealCurrentLineNb() + 1, $this->currentLine));
  20893. }
  20894. else if (isset($parsed[0]))
  20895. {
  20896. // Numeric array, merge individual elements
  20897. foreach (array_reverse($parsed) as $parsedItem)
  20898. {
  20899. if (!is_array($parsedItem))
  20900. {
  20901. throw new ParserException(sprintf("Merge items must be arrays at line %s (%s).", $this->getRealCurrentLineNb() + 1, $parsedItem));
  20902. }
  20903. $merged = array_merge($parsedItem, $merged);
  20904. }
  20905. }
  20906. else
  20907. {
  20908. // Associative array, merge
  20909. $merged = array_merge($merge, $parsed);
  20910. }
  20911. $isProcessed = $merged;
  20912. }
  20913. }
  20914. else if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#', $values['value'], $matches))
  20915. {
  20916. $isRef = $matches['ref'];
  20917. $values['value'] = $matches['value'];
  20918. }
  20919. if ($isProcessed)
  20920. {
  20921. // Merge keys
  20922. $data = $isProcessed;
  20923. }
  20924. // hash
  20925. else if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#'))
  20926. {
  20927. // if next line is less indented or equal, then it means that the current value is null
  20928. if ($this->isNextLineIndented())
  20929. {
  20930. $data[$key] = null;
  20931. }
  20932. else
  20933. {
  20934. $c = $this->getRealCurrentLineNb() + 1;
  20935. $parser = new Parser($c);
  20936. $parser->refs =& $this->refs;
  20937. $data[$key] = $parser->parse($this->getNextEmbedBlock());
  20938. }
  20939. }
  20940. else
  20941. {
  20942. if ($isInPlace)
  20943. {
  20944. $data = $this->refs[$isInPlace];
  20945. }
  20946. else
  20947. {
  20948. $data[$key] = $this->parseValue($values['value']);
  20949. }
  20950. }
  20951. }
  20952. else
  20953. {
  20954. // 1-liner followed by newline
  20955. if (2 == count($this->lines) && empty($this->lines[1]))
  20956. {
  20957. $value = Inline::load($this->lines[0]);
  20958. if (is_array($value))
  20959. {
  20960. $first = reset($value);
  20961. if ('*' === substr($first, 0, 1))
  20962. {
  20963. $data = array();
  20964. foreach ($value as $alias)
  20965. {
  20966. $data[] = $this->refs[substr($alias, 1)];
  20967. }
  20968. $value = $data;
  20969. }
  20970. }
  20971. return $value;
  20972. }
  20973. switch (preg_last_error())
  20974. {
  20975. case PREG_INTERNAL_ERROR:
  20976. $error = 'Internal PCRE error on line';
  20977. break;
  20978. case PREG_BACKTRACK_LIMIT_ERROR:
  20979. $error = 'pcre.backtrack_limit reached on line';
  20980. break;
  20981. case PREG_RECURSION_LIMIT_ERROR:
  20982. $error = 'pcre.recursion_limit reached on line';
  20983. break;
  20984. case PREG_BAD_UTF8_ERROR:
  20985. $error = 'Malformed UTF-8 data on line';
  20986. break;
  20987. case PREG_BAD_UTF8_OFFSET_ERROR:
  20988. $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point on line';
  20989. break;
  20990. default:
  20991. $error = 'Unable to parse line';
  20992. }
  20993. throw new ParserException(sprintf('%s %d (%s).', $error, $this->getRealCurrentLineNb() + 1, $this->currentLine));
  20994. }
  20995. if ($isRef)
  20996. {
  20997. $this->refs[$isRef] = end($data);
  20998. }
  20999. }
  21000. return empty($data) ? null : $data;
  21001. }
  21002. /**
  21003. * Returns the current line number (takes the offset into account).
  21004. *
  21005. * @return integer The current line number
  21006. */
  21007. protected function getRealCurrentLineNb()
  21008. {
  21009. return $this->currentLineNb + $this->offset;
  21010. }
  21011. /**
  21012. * Returns the current line indentation.
  21013. *
  21014. * @return integer The current line indentation
  21015. */
  21016. protected function getCurrentLineIndentation()
  21017. {
  21018. return strlen($this->currentLine) - strlen(ltrim($this->currentLine, ' '));
  21019. }
  21020. /**
  21021. * Returns the next embed block of YAML.
  21022. *
  21023. * @param integer $indentation The indent level at which the block is to be read, or null for default
  21024. *
  21025. * @return string A YAML string
  21026. */
  21027. protected function getNextEmbedBlock($indentation = null)
  21028. {
  21029. $this->moveToNextLine();
  21030. if (null === $indentation)
  21031. {
  21032. $newIndent = $this->getCurrentLineIndentation();
  21033. if (!$this->isCurrentLineEmpty() && 0 == $newIndent)
  21034. {
  21035. throw new ParserException(sprintf('Indentation problem at line %d (%s)', $this->getRealCurrentLineNb() + 1, $this->currentLine));
  21036. }
  21037. }
  21038. else
  21039. {
  21040. $newIndent = $indentation;
  21041. }
  21042. $data = array(substr($this->currentLine, $newIndent));
  21043. while ($this->moveToNextLine())
  21044. {
  21045. if ($this->isCurrentLineEmpty())
  21046. {
  21047. if ($this->isCurrentLineBlank())
  21048. {
  21049. $data[] = substr($this->currentLine, $newIndent);
  21050. }
  21051. continue;
  21052. }
  21053. $indent = $this->getCurrentLineIndentation();
  21054. if (preg_match('#^(?P<text> *)$#', $this->currentLine, $match))
  21055. {
  21056. // empty line
  21057. $data[] = $match['text'];
  21058. }
  21059. else if ($indent >= $newIndent)
  21060. {
  21061. $data[] = substr($this->currentLine, $newIndent);
  21062. }
  21063. else if (0 == $indent)
  21064. {
  21065. $this->moveToPreviousLine();
  21066. break;
  21067. }
  21068. else
  21069. {
  21070. throw new ParserException(sprintf('Indentation problem at line %d (%s)', $this->getRealCurrentLineNb() + 1, $this->currentLine));
  21071. }
  21072. }
  21073. return implode("\n", $data);
  21074. }
  21075. /**
  21076. * Moves the parser to the next line.
  21077. */
  21078. protected function moveToNextLine()
  21079. {
  21080. if ($this->currentLineNb >= count($this->lines) - 1)
  21081. {
  21082. return false;
  21083. }
  21084. $this->currentLine = $this->lines[++$this->currentLineNb];
  21085. return true;
  21086. }
  21087. /**
  21088. * Moves the parser to the previous line.
  21089. */
  21090. protected function moveToPreviousLine()
  21091. {
  21092. $this->currentLine = $this->lines[--$this->currentLineNb];
  21093. }
  21094. /**
  21095. * Parses a YAML value.
  21096. *
  21097. * @param string $value A YAML value
  21098. *
  21099. * @return mixed A PHP value
  21100. */
  21101. protected function parseValue($value)
  21102. {
  21103. if ('*' === substr($value, 0, 1))
  21104. {
  21105. if (false !== $pos = strpos($value, '#'))
  21106. {
  21107. $value = substr($value, 1, $pos - 2);
  21108. }
  21109. else
  21110. {
  21111. $value = substr($value, 1);
  21112. }
  21113. if (!array_key_exists($value, $this->refs))
  21114. {
  21115. throw new ParserException(sprintf('Reference "%s" does not exist (%s).', $value, $this->currentLine));
  21116. }
  21117. return $this->refs[$value];
  21118. }
  21119. if (preg_match('/^(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?$/', $value, $matches))
  21120. {
  21121. $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
  21122. return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), intval(abs($modifiers)));
  21123. }
  21124. else
  21125. {
  21126. return Inline::load($value);
  21127. }
  21128. }
  21129. /**
  21130. * Parses a folded scalar.
  21131. *
  21132. * @param string $separator The separator that was used to begin this folded scalar (| or >)
  21133. * @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
  21134. * @param integer $indentation The indentation that was used to begin this folded scalar
  21135. *
  21136. * @return string The text value
  21137. */
  21138. protected function parseFoldedScalar($separator, $indicator = '', $indentation = 0)
  21139. {
  21140. $separator = '|' == $separator ? "\n" : ' ';
  21141. $text = '';
  21142. $notEOF = $this->moveToNextLine();
  21143. while ($notEOF && $this->isCurrentLineBlank())
  21144. {
  21145. $text .= "\n";
  21146. $notEOF = $this->moveToNextLine();
  21147. }
  21148. if (!$notEOF)
  21149. {
  21150. return '';
  21151. }
  21152. if (!preg_match('#^(?P<indent>'.($indentation ? str_repeat(' ', $indentation) : ' +').')(?P<text>.*)$#', $this->currentLine, $matches))
  21153. {
  21154. $this->moveToPreviousLine();
  21155. return '';
  21156. }
  21157. $textIndent = $matches['indent'];
  21158. $previousIndent = 0;
  21159. $text .= $matches['text'].$separator;
  21160. while ($this->currentLineNb + 1 < count($this->lines))
  21161. {
  21162. $this->moveToNextLine();
  21163. if (preg_match('#^(?P<indent> {'.strlen($textIndent).',})(?P<text>.+)$#', $this->currentLine, $matches))
  21164. {
  21165. if (' ' == $separator && $previousIndent != $matches['indent'])
  21166. {
  21167. $text = substr($text, 0, -1)."\n";
  21168. }
  21169. $previousIndent = $matches['indent'];
  21170. $text .= str_repeat(' ', $diff = strlen($matches['indent']) - strlen($textIndent)).$matches['text'].($diff ? "\n" : $separator);
  21171. }
  21172. else if (preg_match('#^(?P<text> *)$#', $this->currentLine, $matches))
  21173. {
  21174. $text .= preg_replace('#^ {1,'.strlen($textIndent).'}#', '', $matches['text'])."\n";
  21175. }
  21176. else
  21177. {
  21178. $this->moveToPreviousLine();
  21179. break;
  21180. }
  21181. }
  21182. if (' ' == $separator)
  21183. {
  21184. // replace last separator by a newline
  21185. $text = preg_replace('/ (\n*)$/', "\n$1", $text);
  21186. }
  21187. switch ($indicator)
  21188. {
  21189. case '':
  21190. $text = preg_replace('#\n+$#s', "\n", $text);
  21191. break;
  21192. case '+':
  21193. break;
  21194. case '-':
  21195. $text = preg_replace('#\n+$#s', '', $text);
  21196. break;
  21197. }
  21198. return $text;
  21199. }
  21200. /**
  21201. * Returns true if the next line is indented.
  21202. *
  21203. * @return Boolean Returns true if the next line is indented, false otherwise
  21204. */
  21205. protected function isNextLineIndented()
  21206. {
  21207. $currentIndentation = $this->getCurrentLineIndentation();
  21208. $notEOF = $this->moveToNextLine();
  21209. while ($notEOF && $this->isCurrentLineEmpty())
  21210. {
  21211. $notEOF = $this->moveToNextLine();
  21212. }
  21213. if (false === $notEOF)
  21214. {
  21215. return false;
  21216. }
  21217. $ret = false;
  21218. if ($this->getCurrentLineIndentation() <= $currentIndentation)
  21219. {
  21220. $ret = true;
  21221. }
  21222. $this->moveToPreviousLine();
  21223. return $ret;
  21224. }
  21225. /**
  21226. * Returns true if the current line is blank or if it is a comment line.
  21227. *
  21228. * @return Boolean Returns true if the current line is empty or if it is a comment line, false otherwise
  21229. */
  21230. protected function isCurrentLineEmpty()
  21231. {
  21232. return $this->isCurrentLineBlank() || $this->isCurrentLineComment();
  21233. }
  21234. /**
  21235. * Returns true if the current line is blank.
  21236. *
  21237. * @return Boolean Returns true if the current line is blank, false otherwise
  21238. */
  21239. protected function isCurrentLineBlank()
  21240. {
  21241. return '' == trim($this->currentLine, ' ');
  21242. }
  21243. /**
  21244. * Returns true if the current line is a comment line.
  21245. *
  21246. * @return Boolean Returns true if the current line is a comment line, false otherwise
  21247. */
  21248. protected function isCurrentLineComment()
  21249. {
  21250. //checking explicitly the first char of the trim is faster than loops or strpos
  21251. $ltrimmedLine = ltrim($this->currentLine, ' ');
  21252. return $ltrimmedLine[0] === '#';
  21253. }
  21254. /**
  21255. * Cleanups a YAML string to be parsed.
  21256. *
  21257. * @param string $value The input YAML string
  21258. *
  21259. * @return string A cleaned up YAML string
  21260. */
  21261. protected function cleanup($value)
  21262. {
  21263. $value = str_replace(array("\r\n", "\r"), "\n", $value);
  21264. if (!preg_match("#\n$#", $value))
  21265. {
  21266. $value .= "\n";
  21267. }
  21268. // strip YAML header
  21269. $count = 0;
  21270. $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#s', '', $value, -1, $count);
  21271. $this->offset += $count;
  21272. // remove leading comments and/or ---
  21273. $trimmedValue = preg_replace('#^((\#.*?\n)|(\-\-\-.*?\n))*#s', '', $value, -1, $count);
  21274. if ($count == 1)
  21275. {
  21276. // items have been removed, update the offset
  21277. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  21278. $value = $trimmedValue;
  21279. }
  21280. return $value;
  21281. }
  21282. }
  21283. <?php
  21284. namespace Symfony\Component\Console\Command;
  21285. use Symfony\Component\Console\Input\InputArgument;
  21286. use Symfony\Component\Console\Input\InputOption;
  21287. use Symfony\Component\Console\Input\InputInterface;
  21288. use Symfony\Component\Console\Output\OutputInterface;
  21289. use Symfony\Component\Console\Output\Output;
  21290. use Symfony\Component\Console\Command\Command;
  21291. /*
  21292. * This file is part of the Symfony framework.
  21293. *
  21294. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  21295. *
  21296. * This source file is subject to the MIT license that is bundled
  21297. * with this source code in the file LICENSE.
  21298. */
  21299. /**
  21300. * ListCommand displays the list of all available commands for the application.
  21301. *
  21302. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21303. */
  21304. class ListCommand extends Command
  21305. {
  21306. /**
  21307. * @see Command
  21308. */
  21309. protected function configure()
  21310. {
  21311. $this
  21312. ->setDefinition(array(
  21313. new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
  21314. new InputOption('xml', null, InputOption::PARAMETER_NONE, 'To output help as XML'),
  21315. ))
  21316. ->setName('list')
  21317. ->setDescription('Lists commands')
  21318. ->setHelp(<<<EOF
  21319. The <info>list</info> command lists all commands:
  21320. <info>./symfony list</info>
  21321. You can also display the commands for a specific namespace:
  21322. <info>./symfony list test</info>
  21323. You can also output the information as XML by using the <comment>--xml</comment> option:
  21324. <info>./symfony list --xml</info>
  21325. EOF
  21326. );
  21327. }
  21328. /**
  21329. * @see Command
  21330. */
  21331. protected function execute(InputInterface $input, OutputInterface $output)
  21332. {
  21333. if ($input->getOption('xml')) {
  21334. $output->writeln($this->application->asXml($input->getArgument('namespace')), Output::OUTPUT_RAW);
  21335. } else {
  21336. $output->writeln($this->application->asText($input->getArgument('namespace')));
  21337. }
  21338. }
  21339. }
  21340. <?php
  21341. namespace Symfony\Component\Console\Command;
  21342. use Symfony\Component\Console\Input\InputArgument;
  21343. use Symfony\Component\Console\Input\InputOption;
  21344. use Symfony\Component\Console\Input\InputInterface;
  21345. use Symfony\Component\Console\Output\OutputInterface;
  21346. use Symfony\Component\Console\Output\Output;
  21347. use Symfony\Component\Console\Command\Command;
  21348. /*
  21349. * This file is part of the Symfony framework.
  21350. *
  21351. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  21352. *
  21353. * This source file is subject to the MIT license that is bundled
  21354. * with this source code in the file LICENSE.
  21355. */
  21356. /**
  21357. * HelpCommand displays the help for a given command.
  21358. *
  21359. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21360. */
  21361. class HelpCommand extends Command
  21362. {
  21363. protected $command;
  21364. /**
  21365. * @see Command
  21366. */
  21367. protected function configure()
  21368. {
  21369. $this->ignoreValidationErrors = true;
  21370. $this
  21371. ->setDefinition(array(
  21372. new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'),
  21373. new InputOption('xml', null, InputOption::PARAMETER_NONE, 'To output help as XML'),
  21374. ))
  21375. ->setName('help')
  21376. ->setAliases(array('?'))
  21377. ->setDescription('Displays help for a command')
  21378. ->setHelp(<<<EOF
  21379. The <info>help</info> command displays help for a given command:
  21380. <info>./symfony help list</info>
  21381. You can also output the help as XML by using the <comment>--xml</comment> option:
  21382. <info>./symfony help --xml list</info>
  21383. EOF
  21384. );
  21385. }
  21386. public function setCommand(Command $command)
  21387. {
  21388. $this->command = $command;
  21389. }
  21390. /**
  21391. * @see Command
  21392. */
  21393. protected function execute(InputInterface $input, OutputInterface $output)
  21394. {
  21395. if (null === $this->command) {
  21396. $this->command = $this->application->getCommand($input->getArgument('command_name'));
  21397. }
  21398. if ($input->getOption('xml')) {
  21399. $output->writeln($this->command->asXml(), Output::OUTPUT_RAW);
  21400. } else {
  21401. $output->writeln($this->command->asText());
  21402. }
  21403. }
  21404. }
  21405. <?php
  21406. namespace Symfony\Component\Console\Command;
  21407. use Symfony\Component\Console\Input\InputDefinition;
  21408. use Symfony\Component\Console\Input\InputOption;
  21409. use Symfony\Component\Console\Input\InputArgument;
  21410. use Symfony\Component\Console\Input\InputInterface;
  21411. use Symfony\Component\Console\Output\OutputInterface;
  21412. use Symfony\Component\Console\Application;
  21413. /*
  21414. * This file is part of the Symfony framework.
  21415. *
  21416. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  21417. *
  21418. * This source file is subject to the MIT license that is bundled
  21419. * with this source code in the file LICENSE.
  21420. */
  21421. /**
  21422. * Base class for all commands.
  21423. *
  21424. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21425. */
  21426. class Command
  21427. {
  21428. protected $name;
  21429. protected $namespace;
  21430. protected $aliases;
  21431. protected $definition;
  21432. protected $help;
  21433. protected $application;
  21434. protected $description;
  21435. protected $ignoreValidationErrors;
  21436. protected $applicationDefinitionMerged;
  21437. protected $code;
  21438. /**
  21439. * Constructor.
  21440. *
  21441. * @param string $name The name of the command
  21442. *
  21443. * @throws \LogicException When the command name is empty
  21444. */
  21445. public function __construct($name = null)
  21446. {
  21447. $this->definition = new InputDefinition();
  21448. $this->ignoreValidationErrors = false;
  21449. $this->applicationDefinitionMerged = false;
  21450. $this->aliases = array();
  21451. if (null !== $name) {
  21452. $this->setName($name);
  21453. }
  21454. $this->configure();
  21455. if (!$this->name) {
  21456. throw new \LogicException('The command name cannot be empty.');
  21457. }
  21458. }
  21459. /**
  21460. * Sets the application instance for this command.
  21461. *
  21462. * @param Application $application An Application instance
  21463. */
  21464. public function setApplication(Application $application = null)
  21465. {
  21466. $this->application = $application;
  21467. }
  21468. /**
  21469. * Configures the current command.
  21470. */
  21471. protected function configure()
  21472. {
  21473. }
  21474. /**
  21475. * Executes the current command.
  21476. *
  21477. * @param InputInterface $input An InputInterface instance
  21478. * @param OutputInterface $output An OutputInterface instance
  21479. *
  21480. * @return integer 0 if everything went fine, or an error code
  21481. *
  21482. * @throws \LogicException When this abstract class is not implemented
  21483. */
  21484. protected function execute(InputInterface $input, OutputInterface $output)
  21485. {
  21486. throw new \LogicException('You must override the execute() method in the concrete command class.');
  21487. }
  21488. /**
  21489. * Interacts with the user.
  21490. *
  21491. * @param InputInterface $input An InputInterface instance
  21492. * @param OutputInterface $output An OutputInterface instance
  21493. */
  21494. protected function interact(InputInterface $input, OutputInterface $output)
  21495. {
  21496. }
  21497. /**
  21498. * Initializes the command just after the input has been validated.
  21499. *
  21500. * This is mainly useful when a lot of commands extends one main command
  21501. * where some things need to be initialized based on the input arguments and options.
  21502. *
  21503. * @param InputInterface $input An InputInterface instance
  21504. * @param OutputInterface $output An OutputInterface instance
  21505. */
  21506. protected function initialize(InputInterface $input, OutputInterface $output)
  21507. {
  21508. }
  21509. /**
  21510. * Runs the command.
  21511. *
  21512. * @param InputInterface $input An InputInterface instance
  21513. * @param OutputInterface $output An OutputInterface instance
  21514. */
  21515. public function run(InputInterface $input, OutputInterface $output)
  21516. {
  21517. // add the application arguments and options
  21518. $this->mergeApplicationDefinition();
  21519. // bind the input against the command specific arguments/options
  21520. try {
  21521. $input->bind($this->definition);
  21522. } catch (\Exception $e) {
  21523. if (!$this->ignoreValidationErrors) {
  21524. throw $e;
  21525. }
  21526. }
  21527. $this->initialize($input, $output);
  21528. if ($input->isInteractive()) {
  21529. $this->interact($input, $output);
  21530. }
  21531. $input->validate();
  21532. if ($this->code) {
  21533. return call_user_func($this->code, $input, $output);
  21534. } else {
  21535. return $this->execute($input, $output);
  21536. }
  21537. }
  21538. /**
  21539. * Sets the code to execute when running this command.
  21540. *
  21541. * @param \Closure $code A \Closure
  21542. *
  21543. * @return Command The current instance
  21544. */
  21545. public function setCode(\Closure $code)
  21546. {
  21547. $this->code = $code;
  21548. return $this;
  21549. }
  21550. /**
  21551. * Merges the application definition with the command definition.
  21552. */
  21553. protected function mergeApplicationDefinition()
  21554. {
  21555. if (null === $this->application || true === $this->applicationDefinitionMerged) {
  21556. return;
  21557. }
  21558. $this->definition->setArguments(array_merge(
  21559. $this->application->getDefinition()->getArguments(),
  21560. $this->definition->getArguments()
  21561. ));
  21562. $this->definition->addOptions($this->application->getDefinition()->getOptions());
  21563. $this->applicationDefinitionMerged = true;
  21564. }
  21565. /**
  21566. * Sets an array of argument and option instances.
  21567. *
  21568. * @param array|Definition $definition An array of argument and option instances or a definition instance
  21569. *
  21570. * @return Command The current instance
  21571. */
  21572. public function setDefinition($definition)
  21573. {
  21574. if ($definition instanceof InputDefinition) {
  21575. $this->definition = $definition;
  21576. } else {
  21577. $this->definition->setDefinition($definition);
  21578. }
  21579. $this->applicationDefinitionMerged = false;
  21580. return $this;
  21581. }
  21582. /**
  21583. * Gets the InputDefinition attached to this Command.
  21584. *
  21585. * @return InputDefinition An InputDefinition instance
  21586. */
  21587. public function getDefinition()
  21588. {
  21589. return $this->definition;
  21590. }
  21591. /**
  21592. * Adds an argument.
  21593. *
  21594. * @param string $name The argument name
  21595. * @param integer $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
  21596. * @param string $description A description text
  21597. * @param mixed $default The default value (for InputArgument::OPTIONAL mode only)
  21598. *
  21599. * @return Command The current instance
  21600. */
  21601. public function addArgument($name, $mode = null, $description = '', $default = null)
  21602. {
  21603. $this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
  21604. return $this;
  21605. }
  21606. /**
  21607. * Adds an option.
  21608. *
  21609. * @param string $name The option name
  21610. * @param string $shortcut The shortcut (can be null)
  21611. * @param integer $mode The option mode: self::PARAMETER_REQUIRED, self::PARAMETER_NONE or self::PARAMETER_OPTIONAL
  21612. * @param string $description A description text
  21613. * @param mixed $default The default value (must be null for self::PARAMETER_REQUIRED or self::PARAMETER_NONE)
  21614. *
  21615. * @return Command The current instance
  21616. */
  21617. public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
  21618. {
  21619. $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
  21620. return $this;
  21621. }
  21622. /**
  21623. * Sets the name of the command.
  21624. *
  21625. * This method can set both the namespace and the name if
  21626. * you separate them by a colon (:)
  21627. *
  21628. * $command->setName('foo:bar');
  21629. *
  21630. * @param string $name The command name
  21631. *
  21632. * @return Command The current instance
  21633. *
  21634. * @throws \InvalidArgumentException When command name given is empty
  21635. */
  21636. public function setName($name)
  21637. {
  21638. if (false !== $pos = strrpos($name, ':')) {
  21639. $namespace = substr($name, 0, $pos);
  21640. $name = substr($name, $pos + 1);
  21641. } else {
  21642. $namespace = $this->namespace;
  21643. }
  21644. if (!$name) {
  21645. throw new \InvalidArgumentException('A command name cannot be empty.');
  21646. }
  21647. $this->namespace = $namespace;
  21648. $this->name = $name;
  21649. return $this;
  21650. }
  21651. /**
  21652. * Returns the command namespace.
  21653. *
  21654. * @return string The command namespace
  21655. */
  21656. public function getNamespace()
  21657. {
  21658. return $this->namespace;
  21659. }
  21660. /**
  21661. * Returns the command name
  21662. *
  21663. * @return string The command name
  21664. */
  21665. public function getName()
  21666. {
  21667. return $this->name;
  21668. }
  21669. /**
  21670. * Returns the fully qualified command name.
  21671. *
  21672. * @return string The fully qualified command name
  21673. */
  21674. public function getFullName()
  21675. {
  21676. return $this->getNamespace() ? $this->getNamespace().':'.$this->getName() : $this->getName();
  21677. }
  21678. /**
  21679. * Sets the description for the command.
  21680. *
  21681. * @param string $description The description for the command
  21682. *
  21683. * @return Command The current instance
  21684. */
  21685. public function setDescription($description)
  21686. {
  21687. $this->description = $description;
  21688. return $this;
  21689. }
  21690. /**
  21691. * Returns the description for the command.
  21692. *
  21693. * @return string The description for the command
  21694. */
  21695. public function getDescription()
  21696. {
  21697. return $this->description;
  21698. }
  21699. /**
  21700. * Sets the help for the command.
  21701. *
  21702. * @param string $help The help for the command
  21703. *
  21704. * @return Command The current instance
  21705. */
  21706. public function setHelp($help)
  21707. {
  21708. $this->help = $help;
  21709. return $this;
  21710. }
  21711. /**
  21712. * Returns the help for the command.
  21713. *
  21714. * @return string The help for the command
  21715. */
  21716. public function getHelp()
  21717. {
  21718. return $this->help;
  21719. }
  21720. /**
  21721. * Returns the processed help for the command replacing the %command.name% and
  21722. * %command.full_name% patterns with the real values dynamically.
  21723. *
  21724. * @return string The processed help for the command
  21725. */
  21726. public function getProcessedHelp()
  21727. {
  21728. $name = $this->namespace.':'.$this->name;
  21729. $placeholders = array(
  21730. '%command.name%',
  21731. '%command.full_name%'
  21732. );
  21733. $replacements = array(
  21734. $name,
  21735. $_SERVER['PHP_SELF'].' '.$name
  21736. );
  21737. return str_replace($placeholders, $replacements, $this->getHelp());
  21738. }
  21739. /**
  21740. * Sets the aliases for the command.
  21741. *
  21742. * @param array $aliases An array of aliases for the command
  21743. *
  21744. * @return Command The current instance
  21745. */
  21746. public function setAliases($aliases)
  21747. {
  21748. $this->aliases = $aliases;
  21749. return $this;
  21750. }
  21751. /**
  21752. * Returns the aliases for the command.
  21753. *
  21754. * @return array An array of aliases for the command
  21755. */
  21756. public function getAliases()
  21757. {
  21758. return $this->aliases;
  21759. }
  21760. /**
  21761. * Returns the synopsis for the command.
  21762. *
  21763. * @return string The synopsis
  21764. */
  21765. public function getSynopsis()
  21766. {
  21767. return sprintf('%s %s', $this->getFullName(), $this->definition->getSynopsis());
  21768. }
  21769. /**
  21770. * Gets a helper instance by name.
  21771. *
  21772. * @param string $name The helper name
  21773. *
  21774. * @return mixed The helper value
  21775. *
  21776. * @throws \InvalidArgumentException if the helper is not defined
  21777. */
  21778. protected function getHelper($name)
  21779. {
  21780. return $this->application->getHelperSet()->get($name);
  21781. }
  21782. /**
  21783. * Gets a helper instance by name.
  21784. *
  21785. * @param string $name The helper name
  21786. *
  21787. * @return mixed The helper value
  21788. *
  21789. * @throws \InvalidArgumentException if the helper is not defined
  21790. */
  21791. public function __get($name)
  21792. {
  21793. return $this->application->getHelperSet()->get($name);
  21794. }
  21795. /**
  21796. * Returns a text representation of the command.
  21797. *
  21798. * @return string A string representing the command
  21799. */
  21800. public function asText()
  21801. {
  21802. $messages = array(
  21803. '<comment>Usage:</comment>',
  21804. ' '.$this->getSynopsis(),
  21805. '',
  21806. );
  21807. if ($this->getAliases()) {
  21808. $messages[] = '<comment>Aliases:</comment> <info>'.implode(', ', $this->getAliases()).'</info>';
  21809. }
  21810. $messages[] = $this->definition->asText();
  21811. if ($help = $this->getProcessedHelp()) {
  21812. $messages[] = '<comment>Help:</comment>';
  21813. $messages[] = ' '.implode("\n ", explode("\n", $help))."\n";
  21814. }
  21815. return implode("\n", $messages);
  21816. }
  21817. /**
  21818. * Returns an XML representation of the command.
  21819. *
  21820. * @param Boolean $asDom Whether to return a DOM or an XML string
  21821. *
  21822. * @return string|DOMDocument An XML string representing the command
  21823. */
  21824. public function asXml($asDom = false)
  21825. {
  21826. $dom = new \DOMDocument('1.0', 'UTF-8');
  21827. $dom->formatOutput = true;
  21828. $dom->appendChild($commandXML = $dom->createElement('command'));
  21829. $commandXML->setAttribute('id', $this->getFullName());
  21830. $commandXML->setAttribute('namespace', $this->getNamespace() ? $this->getNamespace() : '_global');
  21831. $commandXML->setAttribute('name', $this->getName());
  21832. $commandXML->appendChild($usageXML = $dom->createElement('usage'));
  21833. $usageXML->appendChild($dom->createTextNode(sprintf($this->getSynopsis(), '')));
  21834. $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
  21835. $descriptionXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $this->getDescription()))));
  21836. $commandXML->appendChild($helpXML = $dom->createElement('help'));
  21837. $help = $this->help;
  21838. $helpXML->appendChild($dom->createTextNode(implode("\n ", explode("\n", $help))));
  21839. $commandXML->appendChild($aliasesXML = $dom->createElement('aliases'));
  21840. foreach ($this->getAliases() as $alias) {
  21841. $aliasesXML->appendChild($aliasXML = $dom->createElement('alias'));
  21842. $aliasXML->appendChild($dom->createTextNode($alias));
  21843. }
  21844. $definition = $this->definition->asXml(true);
  21845. $commandXML->appendChild($dom->importNode($definition->getElementsByTagName('arguments')->item(0), true));
  21846. $commandXML->appendChild($dom->importNode($definition->getElementsByTagName('options')->item(0), true));
  21847. return $asDom ? $dom : $dom->saveXml();
  21848. }
  21849. }
  21850. <?php
  21851. namespace Symfony\Component\Console\Output;
  21852. /*
  21853. * This file is part of the Symfony framework.
  21854. *
  21855. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  21856. *
  21857. * This source file is subject to the MIT license that is bundled
  21858. * with this source code in the file LICENSE.
  21859. */
  21860. /**
  21861. * ConsoleOutput is the default class for all CLI output. It uses STDOUT.
  21862. *
  21863. * This class is a convenient wrapper around `StreamOutput`.
  21864. *
  21865. * $output = new ConsoleOutput();
  21866. *
  21867. * This is equivalent to:
  21868. *
  21869. * $output = new StreamOutput(fopen('php://stdout', 'w'));
  21870. *
  21871. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21872. */
  21873. class ConsoleOutput extends StreamOutput
  21874. {
  21875. /**
  21876. * Constructor.
  21877. *
  21878. * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
  21879. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  21880. */
  21881. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null)
  21882. {
  21883. parent::__construct(fopen('php://stdout', 'w'), $verbosity, $decorated);
  21884. }
  21885. }
  21886. <?php
  21887. namespace Symfony\Component\Console\Output;
  21888. /*
  21889. * This file is part of the Symfony framework.
  21890. *
  21891. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  21892. *
  21893. * This source file is subject to the MIT license that is bundled
  21894. * with this source code in the file LICENSE.
  21895. */
  21896. /**
  21897. * OutputInterface is the interface implemented by all Output classes.
  21898. *
  21899. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21900. */
  21901. interface OutputInterface
  21902. {
  21903. /**
  21904. * Writes a message to the output.
  21905. *
  21906. * @param string|array $messages The message as an array of lines of a single string
  21907. * @param Boolean $newline Whether to add a newline or not
  21908. * @param integer $type The type of output
  21909. *
  21910. * @throws \InvalidArgumentException When unknown output type is given
  21911. */
  21912. function write($messages, $newline = false, $type = 0);
  21913. /**
  21914. * Sets the verbosity of the output.
  21915. *
  21916. * @param integer $level The level of verbosity
  21917. */
  21918. function setVerbosity($level);
  21919. /**
  21920. * Sets the decorated flag.
  21921. *
  21922. * @param Boolean $decorated Whether to decorated the messages or not
  21923. */
  21924. function setDecorated($decorated);
  21925. }
  21926. <?php
  21927. namespace Symfony\Component\Console\Output;
  21928. /*
  21929. * This file is part of the Symfony framework.
  21930. *
  21931. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  21932. *
  21933. * This source file is subject to the MIT license that is bundled
  21934. * with this source code in the file LICENSE.
  21935. */
  21936. /**
  21937. * StreamOutput writes the output to a given stream.
  21938. *
  21939. * Usage:
  21940. *
  21941. * $output = new StreamOutput(fopen('php://stdout', 'w'));
  21942. *
  21943. * As `StreamOutput` can use any stream, you can also use a file:
  21944. *
  21945. * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
  21946. *
  21947. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21948. */
  21949. class StreamOutput extends Output
  21950. {
  21951. protected $stream;
  21952. /**
  21953. * Constructor.
  21954. *
  21955. * @param mixed $stream A stream resource
  21956. * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
  21957. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  21958. *
  21959. * @throws \InvalidArgumentException When first argument is not a real stream
  21960. */
  21961. public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null)
  21962. {
  21963. if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
  21964. throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
  21965. }
  21966. $this->stream = $stream;
  21967. if (null === $decorated) {
  21968. $decorated = $this->hasColorSupport($decorated);
  21969. }
  21970. parent::__construct($verbosity, $decorated);
  21971. }
  21972. /**
  21973. * Gets the stream attached to this StreamOutput instance.
  21974. *
  21975. * @return resource A stream resource
  21976. */
  21977. public function getStream()
  21978. {
  21979. return $this->stream;
  21980. }
  21981. /**
  21982. * Writes a message to the output.
  21983. *
  21984. * @param string $message A message to write to the output
  21985. * @param Boolean $newline Whether to add a newline or not
  21986. *
  21987. * @throws \RuntimeException When unable to write output (should never happen)
  21988. */
  21989. public function doWrite($message, $newline)
  21990. {
  21991. if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) {
  21992. // @codeCoverageIgnoreStart
  21993. // should never happen
  21994. throw new \RuntimeException('Unable to write output.');
  21995. // @codeCoverageIgnoreEnd
  21996. }
  21997. flush();
  21998. }
  21999. /**
  22000. * Returns true if the stream supports colorization.
  22001. *
  22002. * Colorization is disabled if not supported by the stream:
  22003. *
  22004. * - windows without ansicon
  22005. * - non tty consoles
  22006. *
  22007. * @return Boolean true if the stream supports colorization, false otherwise
  22008. */
  22009. protected function hasColorSupport()
  22010. {
  22011. // @codeCoverageIgnoreStart
  22012. if (DIRECTORY_SEPARATOR == '\\') {
  22013. return false !== getenv('ANSICON');
  22014. } else {
  22015. return function_exists('posix_isatty') && @posix_isatty($this->stream);
  22016. }
  22017. // @codeCoverageIgnoreEnd
  22018. }
  22019. }
  22020. <?php
  22021. namespace Symfony\Component\Console\Output;
  22022. /*
  22023. * This file is part of the Symfony framework.
  22024. *
  22025. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22026. *
  22027. * This source file is subject to the MIT license that is bundled
  22028. * with this source code in the file LICENSE.
  22029. */
  22030. /**
  22031. * Base class for output classes.
  22032. *
  22033. * There is three level of verbosity:
  22034. *
  22035. * * normal: no option passed (normal output - information)
  22036. * * verbose: -v (more output - debug)
  22037. * * quiet: -q (no output)
  22038. *
  22039. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22040. */
  22041. abstract class Output implements OutputInterface
  22042. {
  22043. const VERBOSITY_QUIET = 0;
  22044. const VERBOSITY_NORMAL = 1;
  22045. const VERBOSITY_VERBOSE = 2;
  22046. const OUTPUT_NORMAL = 0;
  22047. const OUTPUT_RAW = 1;
  22048. const OUTPUT_PLAIN = 2;
  22049. protected $verbosity;
  22050. protected $decorated;
  22051. static protected $styles = array(
  22052. 'error' => array('bg' => 'red', 'fg' => 'white'),
  22053. 'info' => array('fg' => 'green'),
  22054. 'comment' => array('fg' => 'yellow'),
  22055. 'question' => array('bg' => 'cyan', 'fg' => 'black'),
  22056. );
  22057. static protected $options = array('bold' => 1, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'conceal' => 8);
  22058. static protected $foreground = array('black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37);
  22059. static protected $background = array('black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47);
  22060. /**
  22061. * Constructor.
  22062. *
  22063. * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
  22064. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  22065. */
  22066. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null)
  22067. {
  22068. $this->decorated = (Boolean) $decorated;
  22069. $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
  22070. }
  22071. /**
  22072. * Sets a new style.
  22073. *
  22074. * @param string $name The style name
  22075. * @param array $options An array of options
  22076. */
  22077. static public function setStyle($name, $options = array())
  22078. {
  22079. static::$styles[strtolower($name)] = $options;
  22080. }
  22081. /**
  22082. * Sets the decorated flag.
  22083. *
  22084. * @param Boolean $decorated Whether to decorated the messages or not
  22085. */
  22086. public function setDecorated($decorated)
  22087. {
  22088. $this->decorated = (Boolean) $decorated;
  22089. }
  22090. /**
  22091. * Gets the decorated flag.
  22092. *
  22093. * @return Boolean true if the output will decorate messages, false otherwise
  22094. */
  22095. public function isDecorated()
  22096. {
  22097. return $this->decorated;
  22098. }
  22099. /**
  22100. * Sets the verbosity of the output.
  22101. *
  22102. * @param integer $level The level of verbosity
  22103. */
  22104. public function setVerbosity($level)
  22105. {
  22106. $this->verbosity = (int) $level;
  22107. }
  22108. /**
  22109. * Gets the current verbosity of the output.
  22110. *
  22111. * @return integer The current level of verbosity
  22112. */
  22113. public function getVerbosity()
  22114. {
  22115. return $this->verbosity;
  22116. }
  22117. /**
  22118. * Writes a message to the output and adds a newline at the end.
  22119. *
  22120. * @param string|array $messages The message as an array of lines of a single string
  22121. * @param integer $type The type of output
  22122. */
  22123. public function writeln($messages, $type = 0)
  22124. {
  22125. $this->write($messages, true, $type);
  22126. }
  22127. /**
  22128. * Writes a message to the output.
  22129. *
  22130. * @param string|array $messages The message as an array of lines of a single string
  22131. * @param Boolean $newline Whether to add a newline or not
  22132. * @param integer $type The type of output
  22133. *
  22134. * @throws \InvalidArgumentException When unknown output type is given
  22135. */
  22136. public function write($messages, $newline = false, $type = 0)
  22137. {
  22138. if (self::VERBOSITY_QUIET === $this->verbosity) {
  22139. return;
  22140. }
  22141. if (!is_array($messages)) {
  22142. $messages = array($messages);
  22143. }
  22144. foreach ($messages as $message) {
  22145. switch ($type) {
  22146. case Output::OUTPUT_NORMAL:
  22147. $message = $this->format($message);
  22148. break;
  22149. case Output::OUTPUT_RAW:
  22150. break;
  22151. case Output::OUTPUT_PLAIN:
  22152. $message = strip_tags($this->format($message));
  22153. break;
  22154. default:
  22155. throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
  22156. }
  22157. $this->doWrite($message, $newline);
  22158. }
  22159. }
  22160. /**
  22161. * Writes a message to the output.
  22162. *
  22163. * @param string $message A message to write to the output
  22164. * @param Boolean $newline Whether to add a newline or not
  22165. */
  22166. abstract public function doWrite($message, $newline);
  22167. /**
  22168. * Formats a message according to the given styles.
  22169. *
  22170. * @param string $message The message to style
  22171. *
  22172. * @return string The styled message
  22173. */
  22174. protected function format($message)
  22175. {
  22176. $message = preg_replace_callback('#<([a-z][a-z0-9\-_=;]+)>#i', array($this, 'replaceStartStyle'), $message);
  22177. return preg_replace_callback('#</([a-z][a-z0-9\-_]*)?>#i', array($this, 'replaceEndStyle'), $message);
  22178. }
  22179. /**
  22180. * @throws \InvalidArgumentException When style is unknown
  22181. */
  22182. protected function replaceStartStyle($match)
  22183. {
  22184. if (!$this->decorated) {
  22185. return '';
  22186. }
  22187. if (isset(static::$styles[strtolower($match[1])])) {
  22188. $parameters = static::$styles[strtolower($match[1])];
  22189. } else {
  22190. // bg=blue;fg=red
  22191. if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($match[1]), $matches, PREG_SET_ORDER)) {
  22192. throw new \InvalidArgumentException(sprintf('Unknown style "%s".', $match[1]));
  22193. }
  22194. $parameters = array();
  22195. foreach ($matches as $match) {
  22196. $parameters[$match[1]] = $match[2];
  22197. }
  22198. }
  22199. $codes = array();
  22200. if (isset($parameters['fg'])) {
  22201. $codes[] = static::$foreground[$parameters['fg']];
  22202. }
  22203. if (isset($parameters['bg'])) {
  22204. $codes[] = static::$background[$parameters['bg']];
  22205. }
  22206. foreach (static::$options as $option => $value) {
  22207. if (isset($parameters[$option]) && $parameters[$option]) {
  22208. $codes[] = $value;
  22209. }
  22210. }
  22211. return "\033[".implode(';', $codes).'m';
  22212. }
  22213. protected function replaceEndStyle($match)
  22214. {
  22215. if (!$this->decorated) {
  22216. return '';
  22217. }
  22218. return "\033[0m";
  22219. }
  22220. }
  22221. <?php
  22222. namespace Symfony\Component\Console\Output;
  22223. /*
  22224. * This file is part of the Symfony framework.
  22225. *
  22226. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22227. *
  22228. * This source file is subject to the MIT license that is bundled
  22229. * with this source code in the file LICENSE.
  22230. */
  22231. /**
  22232. * NullOutput suppresses all output.
  22233. *
  22234. * $output = new NullOutput();
  22235. *
  22236. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22237. */
  22238. class NullOutput extends Output
  22239. {
  22240. /**
  22241. * Writes a message to the output.
  22242. *
  22243. * @param string $message A message to write to the output
  22244. * @param Boolean $newline Whether to add a newline or not
  22245. */
  22246. public function doWrite($message, $newline)
  22247. {
  22248. }
  22249. }
  22250. <?php
  22251. namespace Symfony\Component\Console;
  22252. use Symfony\Component\Console\Application;
  22253. use Symfony\Component\Console\Input\StringInput;
  22254. use Symfony\Component\Console\Output\ConsoleOutput;
  22255. /*
  22256. * This file is part of the Symfony framework.
  22257. *
  22258. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22259. *
  22260. * This source file is subject to the MIT license that is bundled
  22261. * with this source code in the file LICENSE.
  22262. */
  22263. /**
  22264. * A Shell wraps an Application to add shell capabilities to it.
  22265. *
  22266. * This class only works with a PHP compiled with readline support
  22267. * (either --with-readline or --with-libedit)
  22268. *
  22269. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22270. */
  22271. class Shell
  22272. {
  22273. protected $application;
  22274. protected $history;
  22275. protected $output;
  22276. /**
  22277. * Constructor.
  22278. *
  22279. * If there is no readline support for the current PHP executable
  22280. * a \RuntimeException exception is thrown.
  22281. *
  22282. * @param Application $application An application instance
  22283. *
  22284. * @throws \RuntimeException When Readline extension is not enabled
  22285. */
  22286. public function __construct(Application $application)
  22287. {
  22288. if (!function_exists('readline')) {
  22289. throw new \RuntimeException('Unable to start the shell as the Readline extension is not enabled.');
  22290. }
  22291. $this->application = $application;
  22292. $this->history = getenv('HOME').'/.history_'.$application->getName();
  22293. $this->output = new ConsoleOutput();
  22294. }
  22295. /**
  22296. * Runs the shell.
  22297. */
  22298. public function run()
  22299. {
  22300. $this->application->setAutoExit(false);
  22301. $this->application->setCatchExceptions(true);
  22302. readline_read_history($this->history);
  22303. readline_completion_function(array($this, 'autocompleter'));
  22304. $this->output->writeln($this->getHeader());
  22305. while (true) {
  22306. $command = readline($this->application->getName().' > ');
  22307. if (false === $command) {
  22308. $this->output->writeln("\n");
  22309. break;
  22310. }
  22311. readline_add_history($command);
  22312. readline_write_history($this->history);
  22313. if (0 !== $ret = $this->application->run(new StringInput($command), $this->output)) {
  22314. $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
  22315. }
  22316. }
  22317. }
  22318. /**
  22319. * Tries to return autocompletion for the current entered text.
  22320. *
  22321. * @param string $text The last segment of the entered text
  22322. * @param integer $position The current position
  22323. */
  22324. protected function autocompleter($text, $position)
  22325. {
  22326. $info = readline_info();
  22327. $text = substr($info['line_buffer'], 0, $info['end']);
  22328. if ($info['point'] !== $info['end']) {
  22329. return true;
  22330. }
  22331. // task name?
  22332. if (false === strpos($text, ' ') || !$text) {
  22333. return array_keys($this->application->getCommands());
  22334. }
  22335. // options and arguments?
  22336. try {
  22337. $command = $this->application->findCommand(substr($text, 0, strpos($text, ' ')));
  22338. } catch (\Exception $e) {
  22339. return true;
  22340. }
  22341. $list = array('--help');
  22342. foreach ($command->getDefinition()->getOptions() as $option) {
  22343. $list[] = '--'.$option->getName();
  22344. }
  22345. return $list;
  22346. }
  22347. /**
  22348. * Returns the shell header.
  22349. *
  22350. * @return string The header string
  22351. */
  22352. protected function getHeader()
  22353. {
  22354. return <<<EOF
  22355. Welcome to the <info>{$this->application->getName()}</info> shell (<comment>{$this->application->getVersion()}</comment>).
  22356. At the prompt, type <comment>help</comment> for some help,
  22357. or <comment>list</comment> to get a list available commands.
  22358. To exit the shell, type <comment>^D</comment>.
  22359. EOF;
  22360. }
  22361. }
  22362. <?php
  22363. namespace Symfony\Component\Console\Helper;
  22364. /*
  22365. * This file is part of the Symfony framework.
  22366. *
  22367. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22368. *
  22369. * This source file is subject to the MIT license that is bundled
  22370. * with this source code in the file LICENSE.
  22371. */
  22372. /**
  22373. * The Formatter class provides helpers to format messages.
  22374. *
  22375. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22376. */
  22377. class FormatterHelper extends Helper
  22378. {
  22379. /**
  22380. * Formats a message within a section.
  22381. *
  22382. * @param string $section The section name
  22383. * @param string $message The message
  22384. * @param string $style The style to apply to the section
  22385. */
  22386. public function formatSection($section, $message, $style = 'info')
  22387. {
  22388. return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
  22389. }
  22390. /**
  22391. * Formats a message as a block of text.
  22392. *
  22393. * @param string|array $messages The message to write in the block
  22394. * @param string $style The style to apply to the whole block
  22395. * @param Boolean $large Whether to return a large block
  22396. *
  22397. * @return string The formatter message
  22398. */
  22399. public function formatBlock($messages, $style, $large = false)
  22400. {
  22401. if (!is_array($messages)) {
  22402. $messages = array($messages);
  22403. }
  22404. $len = 0;
  22405. $lines = array();
  22406. foreach ($messages as $message) {
  22407. $lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
  22408. $len = max($this->strlen($message) + ($large ? 4 : 2), $len);
  22409. }
  22410. $messages = $large ? array(str_repeat(' ', $len)) : array();
  22411. foreach ($lines as $line) {
  22412. $messages[] = $line.str_repeat(' ', $len - $this->strlen($line));
  22413. }
  22414. if ($large) {
  22415. $messages[] = str_repeat(' ', $len);
  22416. }
  22417. foreach ($messages as &$message) {
  22418. $message = sprintf('<%s>%s</%s>', $style, $message, $style);
  22419. }
  22420. return implode("\n", $messages);
  22421. }
  22422. protected function strlen($string)
  22423. {
  22424. return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
  22425. }
  22426. /**
  22427. * Returns the helper's canonical name
  22428. */
  22429. public function getName()
  22430. {
  22431. return 'formatter';
  22432. }
  22433. }
  22434. <?php
  22435. namespace Symfony\Component\Console\Helper;
  22436. use Symfony\Component\Console\Command\Command;
  22437. /*
  22438. * This file is part of the Symfony framework.
  22439. *
  22440. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22441. *
  22442. * This source file is subject to the MIT license that is bundled
  22443. * with this source code in the file LICENSE.
  22444. */
  22445. /**
  22446. * HelperSet represents a set of helpers to be used with a command.
  22447. *
  22448. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22449. */
  22450. class HelperSet
  22451. {
  22452. protected $helpers;
  22453. protected $command;
  22454. /**
  22455. * @param Helper[] $helpers An array of helper.
  22456. */
  22457. public function __construct(array $helpers = array())
  22458. {
  22459. $this->helpers = array();
  22460. foreach ($helpers as $alias => $helper) {
  22461. $this->set($helper, is_int($alias) ? null : $alias);
  22462. }
  22463. }
  22464. /**
  22465. * Sets a helper.
  22466. *
  22467. * @param HelperInterface $value The helper instance
  22468. * @param string $alias An alias
  22469. */
  22470. public function set(HelperInterface $helper, $alias = null)
  22471. {
  22472. $this->helpers[$helper->getName()] = $helper;
  22473. if (null !== $alias) {
  22474. $this->helpers[$alias] = $helper;
  22475. }
  22476. $helper->setHelperSet($this);
  22477. }
  22478. /**
  22479. * Returns true if the helper if defined.
  22480. *
  22481. * @param string $name The helper name
  22482. *
  22483. * @return Boolean true if the helper is defined, false otherwise
  22484. */
  22485. public function has($name)
  22486. {
  22487. return isset($this->helpers[$name]);
  22488. }
  22489. /**
  22490. * Gets a helper value.
  22491. *
  22492. * @param string $name The helper name
  22493. *
  22494. * @return HelperInterface The helper instance
  22495. *
  22496. * @throws \InvalidArgumentException if the helper is not defined
  22497. */
  22498. public function get($name)
  22499. {
  22500. if (!$this->has($name)) {
  22501. throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  22502. }
  22503. return $this->helpers[$name];
  22504. }
  22505. /**
  22506. * Sets the command associated with this helper set.
  22507. *
  22508. * @param Command $command A Command instance
  22509. */
  22510. public function setCommand(Command $command = null)
  22511. {
  22512. $this->command = $command;
  22513. }
  22514. /**
  22515. * Gets the command associated with this helper set.
  22516. *
  22517. * @return Command A Command instance
  22518. */
  22519. public function getCommand()
  22520. {
  22521. return $this->command;
  22522. }
  22523. }
  22524. <?php
  22525. namespace Symfony\Component\Console\Helper;
  22526. /*
  22527. * This file is part of the Symfony framework.
  22528. *
  22529. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22530. *
  22531. * This source file is subject to the MIT license that is bundled
  22532. * with this source code in the file LICENSE.
  22533. */
  22534. /**
  22535. * Helper is the base class for all helper classes.
  22536. *
  22537. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22538. */
  22539. abstract class Helper implements HelperInterface
  22540. {
  22541. protected $helperSet = null;
  22542. /**
  22543. * Sets the helper set associated with this helper.
  22544. *
  22545. * @param HelperSet $helperSet A HelperSet instance
  22546. */
  22547. public function setHelperSet(HelperSet $helperSet = null)
  22548. {
  22549. $this->helperSet = $helperSet;
  22550. }
  22551. /**
  22552. * Gets the helper set associated with this helper.
  22553. *
  22554. * @return HelperSet A HelperSet instance
  22555. */
  22556. public function getHelperSet()
  22557. {
  22558. return $this->helperSet;
  22559. }
  22560. }
  22561. <?php
  22562. namespace Symfony\Component\Console\Helper;
  22563. /*
  22564. * This file is part of the Symfony framework.
  22565. *
  22566. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22567. *
  22568. * This source file is subject to the MIT license that is bundled
  22569. * with this source code in the file LICENSE.
  22570. */
  22571. /**
  22572. * HelperInterface is the interface all helpers must implement.
  22573. *
  22574. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22575. */
  22576. interface HelperInterface
  22577. {
  22578. /**
  22579. * Sets the helper set associated with this helper.
  22580. *
  22581. * @param HelperSet $helperSet A HelperSet instance
  22582. */
  22583. function setHelperSet(HelperSet $helperSet = null);
  22584. /**
  22585. * Gets the helper set associated with this helper.
  22586. *
  22587. * @return HelperSet A HelperSet instance
  22588. */
  22589. function getHelperSet();
  22590. /**
  22591. * Returns the canonical name of this helper.
  22592. *
  22593. * @return string The canonical name
  22594. */
  22595. function getName();
  22596. }
  22597. <?php
  22598. namespace Symfony\Component\Console\Helper;
  22599. use Symfony\Component\Console\Output\OutputInterface;
  22600. /*
  22601. * This file is part of the Symfony framework.
  22602. *
  22603. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22604. *
  22605. * This source file is subject to the MIT license that is bundled
  22606. * with this source code in the file LICENSE.
  22607. */
  22608. /**
  22609. * The Dialog class provides helpers to interact with the user.
  22610. *
  22611. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22612. */
  22613. class DialogHelper extends Helper
  22614. {
  22615. /**
  22616. * Asks a question to the user.
  22617. *
  22618. * @param OutputInterface $output
  22619. * @param string|array $question The question to ask
  22620. * @param string $default The default answer if none is given by the user
  22621. *
  22622. * @return string The user answer
  22623. */
  22624. public function ask(OutputInterface $output, $question, $default = null)
  22625. {
  22626. // @codeCoverageIgnoreStart
  22627. $output->writeln($question);
  22628. $ret = trim(fgets(STDIN));
  22629. return $ret ? $ret : $default;
  22630. // @codeCoverageIgnoreEnd
  22631. }
  22632. /**
  22633. * Asks a confirmation to the user.
  22634. *
  22635. * The question will be asked until the user answer by nothing, yes, or no.
  22636. *
  22637. * @param OutputInterface $output
  22638. * @param string|array $question The question to ask
  22639. * @param Boolean $default The default answer if the user enters nothing
  22640. *
  22641. * @return Boolean true if the user has confirmed, false otherwise
  22642. */
  22643. public function askConfirmation(OutputInterface $output, $question, $default = true)
  22644. {
  22645. // @codeCoverageIgnoreStart
  22646. $answer = 'z';
  22647. while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
  22648. $answer = $this->ask($output, $question);
  22649. }
  22650. if (false === $default) {
  22651. return $answer && 'y' == strtolower($answer[0]);
  22652. } else {
  22653. return !$answer || 'y' == strtolower($answer[0]);
  22654. }
  22655. // @codeCoverageIgnoreEnd
  22656. }
  22657. /**
  22658. * Asks for a value and validates the response.
  22659. *
  22660. * @param OutputInterface $output
  22661. * @param string|array $question
  22662. * @param Closure $validator
  22663. * @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
  22664. *
  22665. * @return mixed
  22666. *
  22667. * @throws \Exception When any of the validator returns an error
  22668. */
  22669. public function askAndValidate(OutputInterface $output, $question, \Closure $validator, $attempts = false)
  22670. {
  22671. // @codeCoverageIgnoreStart
  22672. $error = null;
  22673. while (false === $attempts || $attempts--) {
  22674. if (null !== $error) {
  22675. $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
  22676. }
  22677. $value = $this->ask($output, $question, null);
  22678. try {
  22679. return $validator($value);
  22680. } catch (\Exception $error) {
  22681. }
  22682. }
  22683. throw $error;
  22684. // @codeCoverageIgnoreEnd
  22685. }
  22686. /**
  22687. * Returns the helper's canonical name
  22688. */
  22689. public function getName()
  22690. {
  22691. return 'dialog';
  22692. }
  22693. }
  22694. <?php
  22695. namespace Symfony\Component\Console;
  22696. use Symfony\Component\Console\Input\InputInterface;
  22697. use Symfony\Component\Console\Input\ArgvInput;
  22698. use Symfony\Component\Console\Input\ArrayInput;
  22699. use Symfony\Component\Console\Input\InputDefinition;
  22700. use Symfony\Component\Console\Input\InputOption;
  22701. use Symfony\Component\Console\Input\InputArgument;
  22702. use Symfony\Component\Console\Output\OutputInterface;
  22703. use Symfony\Component\Console\Output\Output;
  22704. use Symfony\Component\Console\Output\ConsoleOutput;
  22705. use Symfony\Component\Console\Command\Command;
  22706. use Symfony\Component\Console\Command\HelpCommand;
  22707. use Symfony\Component\Console\Command\ListCommand;
  22708. use Symfony\Component\Console\Helper\HelperSet;
  22709. use Symfony\Component\Console\Helper\FormatterHelper;
  22710. use Symfony\Component\Console\Helper\DialogHelper;
  22711. /*
  22712. * This file is part of the Symfony framework.
  22713. *
  22714. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  22715. *
  22716. * This source file is subject to the MIT license that is bundled
  22717. * with this source code in the file LICENSE.
  22718. */
  22719. /**
  22720. * An Application is the container for a collection of commands.
  22721. *
  22722. * It is the main entry point of a Console application.
  22723. *
  22724. * This class is optimized for a standard CLI environment.
  22725. *
  22726. * Usage:
  22727. *
  22728. * $app = new Application('myapp', '1.0 (stable)');
  22729. * $app->addCommand(new SimpleCommand());
  22730. * $app->run();
  22731. *
  22732. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22733. */
  22734. class Application
  22735. {
  22736. protected $commands;
  22737. protected $aliases;
  22738. protected $wantHelps = false;
  22739. protected $runningCommand;
  22740. protected $name;
  22741. protected $version;
  22742. protected $catchExceptions;
  22743. protected $autoExit;
  22744. protected $definition;
  22745. protected $helperSet;
  22746. /**
  22747. * Constructor.
  22748. *
  22749. * @param string $name The name of the application
  22750. * @param string $version The version of the application
  22751. */
  22752. public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
  22753. {
  22754. $this->name = $name;
  22755. $this->version = $version;
  22756. $this->catchExceptions = true;
  22757. $this->autoExit = true;
  22758. $this->commands = array();
  22759. $this->aliases = array();
  22760. $this->helperSet = new HelperSet(array(
  22761. new FormatterHelper(),
  22762. new DialogHelper(),
  22763. ));
  22764. $this->addCommand(new HelpCommand());
  22765. $this->addCommand(new ListCommand());
  22766. $this->definition = new InputDefinition(array(
  22767. new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
  22768. new InputOption('--help', '-h', InputOption::PARAMETER_NONE, 'Display this help message.'),
  22769. new InputOption('--quiet', '-q', InputOption::PARAMETER_NONE, 'Do not output any message.'),
  22770. new InputOption('--verbose', '-v', InputOption::PARAMETER_NONE, 'Increase verbosity of messages.'),
  22771. new InputOption('--version', '-V', InputOption::PARAMETER_NONE, 'Display this program version.'),
  22772. new InputOption('--ansi', '-a', InputOption::PARAMETER_NONE, 'Force ANSI output.'),
  22773. new InputOption('--no-interaction', '-n', InputOption::PARAMETER_NONE, 'Do not ask any interactive question.'),
  22774. ));
  22775. }
  22776. /**
  22777. * Runs the current application.
  22778. *
  22779. * @param InputInterface $input An Input instance
  22780. * @param OutputInterface $output An Output instance
  22781. *
  22782. * @return integer 0 if everything went fine, or an error code
  22783. *
  22784. * @throws \Exception When doRun returns Exception
  22785. */
  22786. public function run(InputInterface $input = null, OutputInterface $output = null)
  22787. {
  22788. if (null === $input) {
  22789. $input = new ArgvInput();
  22790. }
  22791. if (null === $output) {
  22792. $output = new ConsoleOutput();
  22793. }
  22794. try {
  22795. $statusCode = $this->doRun($input, $output);
  22796. } catch (\Exception $e) {
  22797. if (!$this->catchExceptions) {
  22798. throw $e;
  22799. }
  22800. $this->renderException($e, $output);
  22801. $statusCode = $e->getCode();
  22802. $statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1;
  22803. }
  22804. if ($this->autoExit) {
  22805. if ($statusCode > 255) {
  22806. $statusCode = 255;
  22807. }
  22808. // @codeCoverageIgnoreStart
  22809. exit($statusCode);
  22810. // @codeCoverageIgnoreEnd
  22811. } else {
  22812. return $statusCode;
  22813. }
  22814. }
  22815. /**
  22816. * Runs the current application.
  22817. *
  22818. * @param InputInterface $input An Input instance
  22819. * @param OutputInterface $output An Output instance
  22820. *
  22821. * @return integer 0 if everything went fine, or an error code
  22822. */
  22823. public function doRun(InputInterface $input, OutputInterface $output)
  22824. {
  22825. $name = $this->getCommandName($input);
  22826. if (true === $input->hasParameterOption(array('--ansi', '-a'))) {
  22827. $output->setDecorated(true);
  22828. }
  22829. if (true === $input->hasParameterOption(array('--help', '-h'))) {
  22830. if (!$name) {
  22831. $name = 'help';
  22832. $input = new ArrayInput(array('command' => 'help'));
  22833. } else {
  22834. $this->wantHelps = true;
  22835. }
  22836. }
  22837. if (true === $input->hasParameterOption(array('--no-interaction', '-n'))) {
  22838. $input->setInteractive(false);
  22839. }
  22840. if (true === $input->hasParameterOption(array('--quiet', '-q'))) {
  22841. $output->setVerbosity(Output::VERBOSITY_QUIET);
  22842. } elseif (true === $input->hasParameterOption(array('--verbose', '-v'))) {
  22843. $output->setVerbosity(Output::VERBOSITY_VERBOSE);
  22844. }
  22845. if (true === $input->hasParameterOption(array('--version', '-V'))) {
  22846. $output->writeln($this->getLongVersion());
  22847. return 0;
  22848. }
  22849. if (!$name) {
  22850. $name = 'list';
  22851. $input = new ArrayInput(array('command' => 'list'));
  22852. }
  22853. // the command name MUST be the first element of the input
  22854. $command = $this->findCommand($name);
  22855. $this->runningCommand = $command;
  22856. $statusCode = $command->run($input, $output);
  22857. $this->runningCommand = null;
  22858. return is_numeric($statusCode) ? $statusCode : 0;
  22859. }
  22860. /**
  22861. * Set a helper set to be used with the command.
  22862. *
  22863. * @param HelperSet $helperSet The helper set
  22864. */
  22865. public function setHelperSet(HelperSet $helperSet)
  22866. {
  22867. $this->helperSet = $helperSet;
  22868. }
  22869. /**
  22870. * Get the helper set associated with the command
  22871. *
  22872. * @return HelperSet The HelperSet instance associated with this command
  22873. */
  22874. public function getHelperSet()
  22875. {
  22876. return $this->helperSet;
  22877. }
  22878. /**
  22879. * Gets the InputDefinition related to this Application.
  22880. *
  22881. * @return InputDefinition The InputDefinition instance
  22882. */
  22883. public function getDefinition()
  22884. {
  22885. return $this->definition;
  22886. }
  22887. /**
  22888. * Gets the help message.
  22889. *
  22890. * @return string A help message.
  22891. */
  22892. public function getHelp()
  22893. {
  22894. $messages = array(
  22895. $this->getLongVersion(),
  22896. '',
  22897. '<comment>Usage:</comment>',
  22898. sprintf(" [options] command [arguments]\n"),
  22899. '<comment>Options:</comment>',
  22900. );
  22901. foreach ($this->definition->getOptions() as $option) {
  22902. $messages[] = sprintf(' %-29s %s %s',
  22903. '<info>--'.$option->getName().'</info>',
  22904. $option->getShortcut() ? '<info>-'.$option->getShortcut().'</info>' : ' ',
  22905. $option->getDescription()
  22906. );
  22907. }
  22908. return implode("\n", $messages);
  22909. }
  22910. /**
  22911. * Sets whether to catch exceptions or not during commands execution.
  22912. *
  22913. * @param Boolean $boolean Whether to catch exceptions or not during commands execution
  22914. */
  22915. public function setCatchExceptions($boolean)
  22916. {
  22917. $this->catchExceptions = (Boolean) $boolean;
  22918. }
  22919. /**
  22920. * Sets whether to automatically exit after a command execution or not.
  22921. *
  22922. * @param Boolean $boolean Whether to automatically exit after a command execution or not
  22923. */
  22924. public function setAutoExit($boolean)
  22925. {
  22926. $this->autoExit = (Boolean) $boolean;
  22927. }
  22928. /**
  22929. * Gets the name of the application.
  22930. *
  22931. * @return string The application name
  22932. */
  22933. public function getName()
  22934. {
  22935. return $this->name;
  22936. }
  22937. /**
  22938. * Sets the application name.
  22939. *
  22940. * @param string $name The application name
  22941. */
  22942. public function setName($name)
  22943. {
  22944. $this->name = $name;
  22945. }
  22946. /**
  22947. * Gets the application version.
  22948. *
  22949. * @return string The application version
  22950. */
  22951. public function getVersion()
  22952. {
  22953. return $this->version;
  22954. }
  22955. /**
  22956. * Sets the application version.
  22957. *
  22958. * @param string $version The application version
  22959. */
  22960. public function setVersion($version)
  22961. {
  22962. $this->version = $version;
  22963. }
  22964. /**
  22965. * Returns the long version of the application.
  22966. *
  22967. * @return string The long application version
  22968. */
  22969. public function getLongVersion()
  22970. {
  22971. if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) {
  22972. return sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion());
  22973. } else {
  22974. return '<info>Console Tool</info>';
  22975. }
  22976. }
  22977. /**
  22978. * Registers a new command.
  22979. *
  22980. * @param string $name The command name
  22981. *
  22982. * @return Command The newly created command
  22983. */
  22984. public function register($name)
  22985. {
  22986. return $this->addCommand(new Command($name));
  22987. }
  22988. /**
  22989. * Adds an array of command objects.
  22990. *
  22991. * @param Command[] $commands An array of commands
  22992. */
  22993. public function addCommands(array $commands)
  22994. {
  22995. foreach ($commands as $command) {
  22996. $this->addCommand($command);
  22997. }
  22998. }
  22999. /**
  23000. * Adds a command object.
  23001. *
  23002. * If a command with the same name already exists, it will be overridden.
  23003. *
  23004. * @param Command $command A Command object
  23005. *
  23006. * @return Command The registered command
  23007. */
  23008. public function addCommand(Command $command)
  23009. {
  23010. $command->setApplication($this);
  23011. $this->commands[$command->getFullName()] = $command;
  23012. foreach ($command->getAliases() as $alias) {
  23013. $this->aliases[$alias] = $command;
  23014. }
  23015. return $command;
  23016. }
  23017. /**
  23018. * Returns a registered command by name or alias.
  23019. *
  23020. * @param string $name The command name or alias
  23021. *
  23022. * @return Command A Command object
  23023. *
  23024. * @throws \InvalidArgumentException When command name given does not exist
  23025. */
  23026. public function getCommand($name)
  23027. {
  23028. if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
  23029. throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
  23030. }
  23031. $command = isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
  23032. if ($this->wantHelps) {
  23033. $this->wantHelps = false;
  23034. $helpCommand = $this->getCommand('help');
  23035. $helpCommand->setCommand($command);
  23036. return $helpCommand;
  23037. }
  23038. return $command;
  23039. }
  23040. /**
  23041. * Returns true if the command exists, false otherwise
  23042. *
  23043. * @param string $name The command name or alias
  23044. *
  23045. * @return Boolean true if the command exists, false otherwise
  23046. */
  23047. public function hasCommand($name)
  23048. {
  23049. return isset($this->commands[$name]) || isset($this->aliases[$name]);
  23050. }
  23051. /**
  23052. * Returns an array of all unique namespaces used by currently registered commands.
  23053. *
  23054. * It does not returns the global namespace which always exists.
  23055. *
  23056. * @return array An array of namespaces
  23057. */
  23058. public function getNamespaces()
  23059. {
  23060. $namespaces = array();
  23061. foreach ($this->commands as $command) {
  23062. if ($command->getNamespace()) {
  23063. $namespaces[$command->getNamespace()] = true;
  23064. }
  23065. }
  23066. return array_keys($namespaces);
  23067. }
  23068. /**
  23069. * Finds a registered namespace by a name or an abbreviation.
  23070. *
  23071. * @return string A registered namespace
  23072. *
  23073. * @throws \InvalidArgumentException When namespace is incorrect or ambiguous
  23074. */
  23075. public function findNamespace($namespace)
  23076. {
  23077. $abbrevs = static::getAbbreviations($this->getNamespaces());
  23078. if (!isset($abbrevs[$namespace])) {
  23079. throw new \InvalidArgumentException(sprintf('There are no commands defined in the "%s" namespace.', $namespace));
  23080. }
  23081. if (count($abbrevs[$namespace]) > 1) {
  23082. throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions($abbrevs[$namespace])));
  23083. }
  23084. return $abbrevs[$namespace][0];
  23085. }
  23086. /**
  23087. * Finds a command by name or alias.
  23088. *
  23089. * Contrary to getCommand, this command tries to find the best
  23090. * match if you give it an abbreviation of a name or alias.
  23091. *
  23092. * @param string $name A command name or a command alias
  23093. *
  23094. * @return Command A Command instance
  23095. *
  23096. * @throws \InvalidArgumentException When command name is incorrect or ambiguous
  23097. */
  23098. public function findCommand($name)
  23099. {
  23100. // namespace
  23101. $namespace = '';
  23102. if (false !== $pos = strrpos($name, ':')) {
  23103. $namespace = $this->findNamespace(substr($name, 0, $pos));
  23104. $name = substr($name, $pos + 1);
  23105. }
  23106. $fullName = $namespace ? $namespace.':'.$name : $name;
  23107. // name
  23108. $commands = array();
  23109. foreach ($this->commands as $command) {
  23110. if ($command->getNamespace() == $namespace) {
  23111. $commands[] = $command->getName();
  23112. }
  23113. }
  23114. $abbrevs = static::getAbbreviations($commands);
  23115. if (isset($abbrevs[$name]) && 1 == count($abbrevs[$name])) {
  23116. return $this->getCommand($namespace ? $namespace.':'.$abbrevs[$name][0] : $abbrevs[$name][0]);
  23117. }
  23118. if (isset($abbrevs[$name]) && count($abbrevs[$name]) > 1) {
  23119. $suggestions = $this->getAbbreviationSuggestions(array_map(function ($command) use ($namespace) { return $namespace.':'.$command; }, $abbrevs[$name]));
  23120. throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $fullName, $suggestions));
  23121. }
  23122. // aliases
  23123. $abbrevs = static::getAbbreviations(array_keys($this->aliases));
  23124. if (!isset($abbrevs[$fullName])) {
  23125. throw new \InvalidArgumentException(sprintf('Command "%s" is not defined.', $fullName));
  23126. }
  23127. if (count($abbrevs[$fullName]) > 1) {
  23128. throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $fullName, $this->getAbbreviationSuggestions($abbrevs[$fullName])));
  23129. }
  23130. return $this->getCommand($abbrevs[$fullName][0]);
  23131. }
  23132. /**
  23133. * Gets the commands (registered in the given namespace if provided).
  23134. *
  23135. * The array keys are the full names and the values the command instances.
  23136. *
  23137. * @param string $namespace A namespace name
  23138. *
  23139. * @return array An array of Command instances
  23140. */
  23141. public function getCommands($namespace = null)
  23142. {
  23143. if (null === $namespace) {
  23144. return $this->commands;
  23145. }
  23146. $commands = array();
  23147. foreach ($this->commands as $name => $command) {
  23148. if ($namespace === $command->getNamespace()) {
  23149. $commands[$name] = $command;
  23150. }
  23151. }
  23152. return $commands;
  23153. }
  23154. /**
  23155. * Returns an array of possible abbreviations given a set of names.
  23156. *
  23157. * @param array $names An array of names
  23158. *
  23159. * @return array An array of abbreviations
  23160. */
  23161. static public function getAbbreviations($names)
  23162. {
  23163. $abbrevs = array();
  23164. foreach ($names as $name) {
  23165. for ($len = strlen($name) - 1; $len > 0; --$len) {
  23166. $abbrev = substr($name, 0, $len);
  23167. if (!isset($abbrevs[$abbrev])) {
  23168. $abbrevs[$abbrev] = array($name);
  23169. } else {
  23170. $abbrevs[$abbrev][] = $name;
  23171. }
  23172. }
  23173. }
  23174. // Non-abbreviations always get entered, even if they aren't unique
  23175. foreach ($names as $name) {
  23176. $abbrevs[$name] = array($name);
  23177. }
  23178. return $abbrevs;
  23179. }
  23180. /**
  23181. * Returns a text representation of the Application.
  23182. *
  23183. * @param string $namespace An optional namespace name
  23184. *
  23185. * @return string A string representing the Application
  23186. */
  23187. public function asText($namespace = null)
  23188. {
  23189. $commands = $namespace ? $this->getCommands($this->findNamespace($namespace)) : $this->commands;
  23190. $messages = array($this->getHelp(), '');
  23191. if ($namespace) {
  23192. $messages[] = sprintf("<comment>Available commands for the \"%s\" namespace:</comment>", $namespace);
  23193. } else {
  23194. $messages[] = '<comment>Available commands:</comment>';
  23195. }
  23196. $width = 0;
  23197. foreach ($commands as $command) {
  23198. $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
  23199. }
  23200. $width += 2;
  23201. // add commands by namespace
  23202. foreach ($this->sortCommands($commands) as $space => $commands) {
  23203. if (!$namespace && '_global' !== $space) {
  23204. $messages[] = '<comment>'.$space.'</comment>';
  23205. }
  23206. foreach ($commands as $command) {
  23207. $aliases = $command->getAliases() ? '<comment> ('.implode(', ', $command->getAliases()).')</comment>' : '';
  23208. $messages[] = sprintf(" <info>%-${width}s</info> %s%s", ($command->getNamespace() ? ':' : '').$command->getName(), $command->getDescription(), $aliases);
  23209. }
  23210. }
  23211. return implode("\n", $messages);
  23212. }
  23213. /**
  23214. * Returns an XML representation of the Application.
  23215. *
  23216. * @param string $namespace An optional namespace name
  23217. * @param Boolean $asDom Whether to return a DOM or an XML string
  23218. *
  23219. * @return string|DOMDocument An XML string representing the Application
  23220. */
  23221. public function asXml($namespace = null, $asDom = false)
  23222. {
  23223. $commands = $namespace ? $this->getCommands($this->findNamespace($namespace)) : $this->commands;
  23224. $dom = new \DOMDocument('1.0', 'UTF-8');
  23225. $dom->formatOutput = true;
  23226. $dom->appendChild($xml = $dom->createElement('symfony'));
  23227. $xml->appendChild($commandsXML = $dom->createElement('commands'));
  23228. if ($namespace) {
  23229. $commandsXML->setAttribute('namespace', $namespace);
  23230. } else {
  23231. $xml->appendChild($namespacesXML = $dom->createElement('namespaces'));
  23232. }
  23233. // add commands by namespace
  23234. foreach ($this->sortCommands($commands) as $space => $commands) {
  23235. if (!$namespace) {
  23236. $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
  23237. $namespaceArrayXML->setAttribute('id', $space);
  23238. }
  23239. foreach ($commands as $command) {
  23240. if (!$namespace) {
  23241. $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
  23242. $commandXML->appendChild($dom->createTextNode($command->getName()));
  23243. }
  23244. $node = $command->asXml(true)->getElementsByTagName('command')->item(0);
  23245. $node = $dom->importNode($node, true);
  23246. $commandsXML->appendChild($node);
  23247. }
  23248. }
  23249. return $asDom ? $dom : $dom->saveXml();
  23250. }
  23251. /**
  23252. * Renders a catched exception.
  23253. *
  23254. * @param Exception $e An exception instance
  23255. * @param OutputInterface $output An OutputInterface instance
  23256. */
  23257. public function renderException($e, $output)
  23258. {
  23259. $strlen = function ($string)
  23260. {
  23261. return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
  23262. };
  23263. $title = sprintf(' [%s] ', get_class($e));
  23264. $len = $strlen($title);
  23265. $lines = array();
  23266. foreach (explode("\n", $e->getMessage()) as $line) {
  23267. $lines[] = sprintf(' %s ', $line);
  23268. $len = max($strlen($line) + 4, $len);
  23269. }
  23270. $messages = array(str_repeat(' ', $len), $title.str_repeat(' ', $len - $strlen($title)));
  23271. foreach ($lines as $line) {
  23272. $messages[] = $line.str_repeat(' ', $len - $strlen($line));
  23273. }
  23274. $messages[] = str_repeat(' ', $len);
  23275. $output->writeln("\n");
  23276. foreach ($messages as $message) {
  23277. $output->writeln('<error>'.$message.'</error>');
  23278. }
  23279. $output->writeln("\n");
  23280. if (null !== $this->runningCommand) {
  23281. $output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
  23282. $output->writeln("\n");
  23283. }
  23284. if (Output::VERBOSITY_VERBOSE === $output->getVerbosity()) {
  23285. $output->writeln('</comment>Exception trace:</comment>');
  23286. // exception related properties
  23287. $trace = $e->getTrace();
  23288. array_unshift($trace, array(
  23289. 'function' => '',
  23290. 'file' => $e->getFile() != null ? $e->getFile() : 'n/a',
  23291. 'line' => $e->getLine() != null ? $e->getLine() : 'n/a',
  23292. 'args' => array(),
  23293. ));
  23294. for ($i = 0, $count = count($trace); $i < $count; $i++) {
  23295. $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
  23296. $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
  23297. $function = $trace[$i]['function'];
  23298. $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
  23299. $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
  23300. $output->writeln(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line));
  23301. }
  23302. $output->writeln("\n");
  23303. }
  23304. }
  23305. protected function getCommandName(InputInterface $input)
  23306. {
  23307. return $input->getFirstArgument('command');
  23308. }
  23309. protected function sortCommands($commands)
  23310. {
  23311. $namespacedCommands = array();
  23312. foreach ($commands as $name => $command) {
  23313. $key = $command->getNamespace() ? $command->getNamespace() : '_global';
  23314. if (!isset($namespacedCommands[$key])) {
  23315. $namespacedCommands[$key] = array();
  23316. }
  23317. $namespacedCommands[$key][$name] = $command;
  23318. }
  23319. ksort($namespacedCommands);
  23320. foreach ($namespacedCommands as $name => &$commands) {
  23321. ksort($commands);
  23322. }
  23323. return $namespacedCommands;
  23324. }
  23325. protected function getAbbreviationSuggestions($abbrevs)
  23326. {
  23327. return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : '');
  23328. }
  23329. }
  23330. <?php
  23331. namespace Symfony\Component\Console\Tester;
  23332. use Symfony\Component\Console\Application;
  23333. use Symfony\Component\Console\Input\ArrayInput;
  23334. use Symfony\Component\Console\Output\StreamOutput;
  23335. /*
  23336. * This file is part of the Symfony framework.
  23337. *
  23338. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  23339. *
  23340. * This source file is subject to the MIT license that is bundled
  23341. * with this source code in the file LICENSE.
  23342. */
  23343. /**
  23344. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23345. */
  23346. class ApplicationTester
  23347. {
  23348. protected $application;
  23349. protected $display;
  23350. protected $input;
  23351. protected $output;
  23352. /**
  23353. * Constructor.
  23354. *
  23355. * @param Application $application A Application instance to test.
  23356. */
  23357. public function __construct(Application $application)
  23358. {
  23359. $this->application = $application;
  23360. }
  23361. /**
  23362. * Executes the application.
  23363. *
  23364. * Available options:
  23365. *
  23366. * * interactive: Sets the input interactive flag
  23367. * * decorated: Sets the output decorated flag
  23368. * * verbosity: Sets the output verbosity flag
  23369. *
  23370. * @param array $input An array of arguments and options
  23371. * @param array $options An array of options
  23372. */
  23373. public function run(array $input, $options = array())
  23374. {
  23375. $this->input = new ArrayInput($input);
  23376. if (isset($options['interactive'])) {
  23377. $this->input->setInteractive($options['interactive']);
  23378. }
  23379. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  23380. if (isset($options['decorated'])) {
  23381. $this->output->setDecorated($options['decorated']);
  23382. }
  23383. if (isset($options['verbosity'])) {
  23384. $this->output->setVerbosity($options['verbosity']);
  23385. }
  23386. $ret = $this->application->run($this->input, $this->output);
  23387. rewind($this->output->getStream());
  23388. return $this->display = stream_get_contents($this->output->getStream());
  23389. }
  23390. /**
  23391. * Gets the display returned by the last execution of the application.
  23392. *
  23393. * @return string The display
  23394. */
  23395. public function getDisplay()
  23396. {
  23397. return $this->display;
  23398. }
  23399. /**
  23400. * Gets the input instance used by the last execution of the application.
  23401. *
  23402. * @return InputInterface The current input instance
  23403. */
  23404. public function getInput()
  23405. {
  23406. return $this->input;
  23407. }
  23408. /**
  23409. * Gets the output instance used by the last execution of the application.
  23410. *
  23411. * @return OutputInterface The current output instance
  23412. */
  23413. public function getOutput()
  23414. {
  23415. return $this->output;
  23416. }
  23417. }
  23418. <?php
  23419. namespace Symfony\Component\Console\Tester;
  23420. use Symfony\Component\Console\Command\Command;
  23421. use Symfony\Component\Console\Input\ArrayInput;
  23422. use Symfony\Component\Console\Output\StreamOutput;
  23423. /*
  23424. * This file is part of the Symfony framework.
  23425. *
  23426. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  23427. *
  23428. * This source file is subject to the MIT license that is bundled
  23429. * with this source code in the file LICENSE.
  23430. */
  23431. /**
  23432. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23433. */
  23434. class CommandTester
  23435. {
  23436. protected $command;
  23437. protected $display;
  23438. protected $input;
  23439. protected $output;
  23440. /**
  23441. * Constructor.
  23442. *
  23443. * @param Command $command A Command instance to test.
  23444. */
  23445. public function __construct(Command $command)
  23446. {
  23447. $this->command = $command;
  23448. }
  23449. /**
  23450. * Executes the command.
  23451. *
  23452. * Available options:
  23453. *
  23454. * * interactive: Sets the input interactive flag
  23455. * * decorated: Sets the output decorated flag
  23456. * * verbosity: Sets the output verbosity flag
  23457. *
  23458. * @param array $input An array of arguments and options
  23459. * @param array $options An array of options
  23460. */
  23461. public function execute(array $input, array $options = array())
  23462. {
  23463. $this->input = new ArrayInput($input);
  23464. if (isset($options['interactive'])) {
  23465. $this->input->setInteractive($options['interactive']);
  23466. }
  23467. $this->output = new StreamOutput(fopen('php://memory', 'w', false));
  23468. if (isset($options['decorated'])) {
  23469. $this->output->setDecorated($options['decorated']);
  23470. }
  23471. if (isset($options['verbosity'])) {
  23472. $this->output->setVerbosity($options['verbosity']);
  23473. }
  23474. $ret = $this->command->run($this->input, $this->output);
  23475. rewind($this->output->getStream());
  23476. return $this->display = stream_get_contents($this->output->getStream());
  23477. }
  23478. /**
  23479. * Gets the display returned by the last execution of the command.
  23480. *
  23481. * @return string The display
  23482. */
  23483. public function getDisplay()
  23484. {
  23485. return $this->display;
  23486. }
  23487. /**
  23488. * Gets the input instance used by the last execution of the command.
  23489. *
  23490. * @return InputInterface The current input instance
  23491. */
  23492. public function getInput()
  23493. {
  23494. return $this->input;
  23495. }
  23496. /**
  23497. * Gets the output instance used by the last execution of the command.
  23498. *
  23499. * @return OutputInterface The current output instance
  23500. */
  23501. public function getOutput()
  23502. {
  23503. return $this->output;
  23504. }
  23505. }
  23506. <?php
  23507. namespace Symfony\Component\Console\Input;
  23508. /*
  23509. * This file is part of the Symfony framework.
  23510. *
  23511. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  23512. *
  23513. * This source file is subject to the MIT license that is bundled
  23514. * with this source code in the file LICENSE.
  23515. */
  23516. /**
  23517. * Input is the base class for all concrete Input classes.
  23518. *
  23519. * Three concrete classes are provided by default:
  23520. *
  23521. * * `ArgvInput`: The input comes from the CLI arguments (argv)
  23522. * * `StringInput`: The input is provided as a string
  23523. * * `ArrayInput`: The input is provided as an array
  23524. *
  23525. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23526. */
  23527. abstract class Input implements InputInterface
  23528. {
  23529. protected $definition;
  23530. protected $options;
  23531. protected $arguments;
  23532. protected $interactive = true;
  23533. /**
  23534. * Constructor.
  23535. *
  23536. * @param InputDefinition $definition A InputDefinition instance
  23537. */
  23538. public function __construct(InputDefinition $definition = null)
  23539. {
  23540. if (null === $definition) {
  23541. $this->definition = new InputDefinition();
  23542. } else {
  23543. $this->bind($definition);
  23544. $this->validate();
  23545. }
  23546. }
  23547. /**
  23548. * Binds the current Input instance with the given arguments and options.
  23549. *
  23550. * @param InputDefinition $definition A InputDefinition instance
  23551. */
  23552. public function bind(InputDefinition $definition)
  23553. {
  23554. $this->arguments = array();
  23555. $this->options = array();
  23556. $this->definition = $definition;
  23557. $this->parse();
  23558. }
  23559. /**
  23560. * Processes command line arguments.
  23561. */
  23562. abstract protected function parse();
  23563. /**
  23564. * @throws \RuntimeException When not enough arguments are given
  23565. */
  23566. public function validate()
  23567. {
  23568. if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) {
  23569. throw new \RuntimeException('Not enough arguments.');
  23570. }
  23571. }
  23572. public function isInteractive()
  23573. {
  23574. return $this->interactive;
  23575. }
  23576. public function setInteractive($interactive)
  23577. {
  23578. $this->interactive = (Boolean) $interactive;
  23579. }
  23580. /**
  23581. * Returns the argument values.
  23582. *
  23583. * @return array An array of argument values
  23584. */
  23585. public function getArguments()
  23586. {
  23587. return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
  23588. }
  23589. /**
  23590. * Returns the argument value for a given argument name.
  23591. *
  23592. * @param string $name The argument name
  23593. *
  23594. * @return mixed The argument value
  23595. *
  23596. * @throws \InvalidArgumentException When argument given doesn't exist
  23597. */
  23598. public function getArgument($name)
  23599. {
  23600. if (!$this->definition->hasArgument($name)) {
  23601. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  23602. }
  23603. return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
  23604. }
  23605. /**
  23606. * Sets an argument value by name.
  23607. *
  23608. * @param string $name The argument name
  23609. * @param string $value The argument value
  23610. *
  23611. * @throws \InvalidArgumentException When argument given doesn't exist
  23612. */
  23613. public function setArgument($name, $value)
  23614. {
  23615. if (!$this->definition->hasArgument($name)) {
  23616. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  23617. }
  23618. $this->arguments[$name] = $value;
  23619. }
  23620. /**
  23621. * Returns true if an InputArgument object exists by name or position.
  23622. *
  23623. * @param string|integer $name The InputArgument name or position
  23624. *
  23625. * @return Boolean true if the InputArgument object exists, false otherwise
  23626. */
  23627. public function hasArgument($name)
  23628. {
  23629. return $this->definition->hasArgument($name);
  23630. }
  23631. /**
  23632. * Returns the options values.
  23633. *
  23634. * @return array An array of option values
  23635. */
  23636. public function getOptions()
  23637. {
  23638. return array_merge($this->definition->getOptionDefaults(), $this->options);
  23639. }
  23640. /**
  23641. * Returns the option value for a given option name.
  23642. *
  23643. * @param string $name The option name
  23644. *
  23645. * @return mixed The option value
  23646. *
  23647. * @throws \InvalidArgumentException When option given doesn't exist
  23648. */
  23649. public function getOption($name)
  23650. {
  23651. if (!$this->definition->hasOption($name)) {
  23652. throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  23653. }
  23654. return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
  23655. }
  23656. /**
  23657. * Sets an option value by name.
  23658. *
  23659. * @param string $name The option name
  23660. * @param string $value The option value
  23661. *
  23662. * @throws \InvalidArgumentException When option given doesn't exist
  23663. */
  23664. public function setOption($name, $value)
  23665. {
  23666. if (!$this->definition->hasOption($name)) {
  23667. throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  23668. }
  23669. $this->options[$name] = $value;
  23670. }
  23671. /**
  23672. * Returns true if an InputOption object exists by name.
  23673. *
  23674. * @param string $name The InputOption name
  23675. *
  23676. * @return Boolean true if the InputOption object exists, false otherwise
  23677. */
  23678. public function hasOption($name)
  23679. {
  23680. return $this->definition->hasOption($name);
  23681. }
  23682. }
  23683. <?php
  23684. namespace Symfony\Component\Console\Input;
  23685. /*
  23686. * This file is part of the Symfony framework.
  23687. *
  23688. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  23689. *
  23690. * This source file is subject to the MIT license that is bundled
  23691. * with this source code in the file LICENSE.
  23692. */
  23693. /**
  23694. * ArrayInput represents an input provided as an array.
  23695. *
  23696. * Usage:
  23697. *
  23698. * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
  23699. *
  23700. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23701. */
  23702. class ArrayInput extends Input
  23703. {
  23704. protected $parameters;
  23705. /**
  23706. * Constructor.
  23707. *
  23708. * @param array $param An array of parameters
  23709. * @param InputDefinition $definition A InputDefinition instance
  23710. */
  23711. public function __construct(array $parameters, InputDefinition $definition = null)
  23712. {
  23713. $this->parameters = $parameters;
  23714. parent::__construct($definition);
  23715. }
  23716. /**
  23717. * Returns the first argument from the raw parameters (not parsed).
  23718. *
  23719. * @return string The value of the first argument or null otherwise
  23720. */
  23721. public function getFirstArgument()
  23722. {
  23723. foreach ($this->parameters as $key => $value) {
  23724. if ($key && '-' === $key[0]) {
  23725. continue;
  23726. }
  23727. return $value;
  23728. }
  23729. }
  23730. /**
  23731. * Returns true if the raw parameters (not parsed) contains a value.
  23732. *
  23733. * This method is to be used to introspect the input parameters
  23734. * before it has been validated. It must be used carefully.
  23735. *
  23736. * @param string|array $value The values to look for in the raw parameters (can be an array)
  23737. *
  23738. * @return Boolean true if the value is contained in the raw parameters
  23739. */
  23740. public function hasParameterOption($values)
  23741. {
  23742. if (!is_array($values)) {
  23743. $values = array($values);
  23744. }
  23745. foreach ($this->parameters as $k => $v) {
  23746. if (!is_int($k)) {
  23747. $v = $k;
  23748. }
  23749. if (in_array($v, $values)) {
  23750. return true;
  23751. }
  23752. }
  23753. return false;
  23754. }
  23755. /**
  23756. * Processes command line arguments.
  23757. */
  23758. protected function parse()
  23759. {
  23760. foreach ($this->parameters as $key => $value) {
  23761. if ('--' === substr($key, 0, 2)) {
  23762. $this->addLongOption(substr($key, 2), $value);
  23763. } elseif ('-' === $key[0]) {
  23764. $this->addShortOption(substr($key, 1), $value);
  23765. } else {
  23766. $this->addArgument($key, $value);
  23767. }
  23768. }
  23769. }
  23770. /**
  23771. * Adds a short option value.
  23772. *
  23773. * @param string $shortcut The short option key
  23774. * @param mixed $value The value for the option
  23775. *
  23776. * @throws \RuntimeException When option given doesn't exist
  23777. */
  23778. protected function addShortOption($shortcut, $value)
  23779. {
  23780. if (!$this->definition->hasShortcut($shortcut)) {
  23781. throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
  23782. }
  23783. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  23784. }
  23785. /**
  23786. * Adds a long option value.
  23787. *
  23788. * @param string $name The long option key
  23789. * @param mixed $value The value for the option
  23790. *
  23791. * @throws \InvalidArgumentException When option given doesn't exist
  23792. * @throws \InvalidArgumentException When a required value is missing
  23793. */
  23794. protected function addLongOption($name, $value)
  23795. {
  23796. if (!$this->definition->hasOption($name)) {
  23797. throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
  23798. }
  23799. $option = $this->definition->getOption($name);
  23800. if (null === $value) {
  23801. if ($option->isParameterRequired()) {
  23802. throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
  23803. }
  23804. $value = $option->isParameterOptional() ? $option->getDefault() : true;
  23805. }
  23806. $this->options[$name] = $value;
  23807. }
  23808. /**
  23809. * Adds an argument value.
  23810. *
  23811. * @param string $name The argument name
  23812. * @param mixed $value The value for the argument
  23813. *
  23814. * @throws \InvalidArgumentException When argument given doesn't exist
  23815. */
  23816. protected function addArgument($name, $value)
  23817. {
  23818. if (!$this->definition->hasArgument($name)) {
  23819. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  23820. }
  23821. $this->arguments[$name] = $value;
  23822. }
  23823. }
  23824. <?php
  23825. namespace Symfony\Component\Console\Input;
  23826. /*
  23827. * This file is part of the Symfony framework.
  23828. *
  23829. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  23830. *
  23831. * This source file is subject to the MIT license that is bundled
  23832. * with this source code in the file LICENSE.
  23833. */
  23834. /**
  23835. * Represents a command line option.
  23836. *
  23837. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23838. */
  23839. class InputOption
  23840. {
  23841. const PARAMETER_NONE = 1;
  23842. const PARAMETER_REQUIRED = 2;
  23843. const PARAMETER_OPTIONAL = 4;
  23844. const PARAMETER_IS_ARRAY = 8;
  23845. protected $name;
  23846. protected $shortcut;
  23847. protected $mode;
  23848. protected $default;
  23849. protected $description;
  23850. /**
  23851. * Constructor.
  23852. *
  23853. * @param string $name The option name
  23854. * @param string $shortcut The shortcut (can be null)
  23855. * @param integer $mode The option mode: self::PARAMETER_REQUIRED, self::PARAMETER_NONE or self::PARAMETER_OPTIONAL
  23856. * @param string $description A description text
  23857. * @param mixed $default The default value (must be null for self::PARAMETER_REQUIRED or self::PARAMETER_NONE)
  23858. *
  23859. * @throws \InvalidArgumentException If option mode is invalid or incompatible
  23860. */
  23861. public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
  23862. {
  23863. if ('--' === substr($name, 0, 2)) {
  23864. $name = substr($name, 2);
  23865. }
  23866. if (empty($shortcut)) {
  23867. $shortcut = null;
  23868. }
  23869. if (null !== $shortcut) {
  23870. if ('-' === $shortcut[0]) {
  23871. $shortcut = substr($shortcut, 1);
  23872. }
  23873. }
  23874. if (null === $mode) {
  23875. $mode = self::PARAMETER_NONE;
  23876. } else if (!is_int($mode) || $mode > 15) {
  23877. throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
  23878. }
  23879. $this->name = $name;
  23880. $this->shortcut = $shortcut;
  23881. $this->mode = $mode;
  23882. $this->description = $description;
  23883. if ($this->isArray() && !$this->acceptParameter()) {
  23884. throw new \InvalidArgumentException('Impossible to have an option mode PARAMETER_IS_ARRAY if the option does not accept a parameter.');
  23885. }
  23886. $this->setDefault($default);
  23887. }
  23888. /**
  23889. * Returns the shortcut.
  23890. *
  23891. * @return string The shortcut
  23892. */
  23893. public function getShortcut()
  23894. {
  23895. return $this->shortcut;
  23896. }
  23897. /**
  23898. * Returns the name.
  23899. *
  23900. * @return string The name
  23901. */
  23902. public function getName()
  23903. {
  23904. return $this->name;
  23905. }
  23906. /**
  23907. * Returns true if the option accept a parameter.
  23908. *
  23909. * @return Boolean true if parameter mode is not self::PARAMETER_NONE, false otherwise
  23910. */
  23911. public function acceptParameter()
  23912. {
  23913. return $this->isParameterRequired() || $this->isParameterOptional();
  23914. }
  23915. /**
  23916. * Returns true if the option requires a parameter.
  23917. *
  23918. * @return Boolean true if parameter mode is self::PARAMETER_REQUIRED, false otherwise
  23919. */
  23920. public function isParameterRequired()
  23921. {
  23922. return self::PARAMETER_REQUIRED === (self::PARAMETER_REQUIRED & $this->mode);
  23923. }
  23924. /**
  23925. * Returns true if the option takes an optional parameter.
  23926. *
  23927. * @return Boolean true if parameter mode is self::PARAMETER_OPTIONAL, false otherwise
  23928. */
  23929. public function isParameterOptional()
  23930. {
  23931. return self::PARAMETER_OPTIONAL === (self::PARAMETER_OPTIONAL & $this->mode);
  23932. }
  23933. /**
  23934. * Returns true if the option can take multiple values.
  23935. *
  23936. * @return Boolean true if mode is self::PARAMETER_IS_ARRAY, false otherwise
  23937. */
  23938. public function isArray()
  23939. {
  23940. return self::PARAMETER_IS_ARRAY === (self::PARAMETER_IS_ARRAY & $this->mode);
  23941. }
  23942. /**
  23943. * Sets the default value.
  23944. *
  23945. * @param mixed $default The default value
  23946. */
  23947. public function setDefault($default = null)
  23948. {
  23949. if (self::PARAMETER_NONE === (self::PARAMETER_NONE & $this->mode) && null !== $default) {
  23950. throw new \LogicException('Cannot set a default value when using Option::PARAMETER_NONE mode.');
  23951. }
  23952. if ($this->isArray()) {
  23953. if (null === $default) {
  23954. $default = array();
  23955. } elseif (!is_array($default)) {
  23956. throw new \LogicException('A default value for an array option must be an array.');
  23957. }
  23958. }
  23959. $this->default = $this->acceptParameter() ? $default : false;
  23960. }
  23961. /**
  23962. * Returns the default value.
  23963. *
  23964. * @return mixed The default value
  23965. */
  23966. public function getDefault()
  23967. {
  23968. return $this->default;
  23969. }
  23970. /**
  23971. * Returns the description text.
  23972. *
  23973. * @return string The description text
  23974. */
  23975. public function getDescription()
  23976. {
  23977. return $this->description;
  23978. }
  23979. }
  23980. <?php
  23981. namespace Symfony\Component\Console\Input;
  23982. /*
  23983. * This file is part of the Symfony framework.
  23984. *
  23985. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  23986. *
  23987. * This source file is subject to the MIT license that is bundled
  23988. * with this source code in the file LICENSE.
  23989. */
  23990. /**
  23991. * A InputDefinition represents a set of valid command line arguments and options.
  23992. *
  23993. * Usage:
  23994. *
  23995. * $definition = new InputDefinition(array(
  23996. * new InputArgument('name', InputArgument::REQUIRED),
  23997. * new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED),
  23998. * ));
  23999. *
  24000. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  24001. */
  24002. class InputDefinition
  24003. {
  24004. protected $arguments;
  24005. protected $requiredCount;
  24006. protected $hasAnArrayArgument = false;
  24007. protected $hasOptional;
  24008. protected $options;
  24009. protected $shortcuts;
  24010. /**
  24011. * Constructor.
  24012. *
  24013. * @param array $definition An array of InputArgument and InputOption instance
  24014. */
  24015. public function __construct(array $definition = array())
  24016. {
  24017. $this->setDefinition($definition);
  24018. }
  24019. public function setDefinition(array $definition)
  24020. {
  24021. $arguments = array();
  24022. $options = array();
  24023. foreach ($definition as $item) {
  24024. if ($item instanceof InputOption) {
  24025. $options[] = $item;
  24026. } else {
  24027. $arguments[] = $item;
  24028. }
  24029. }
  24030. $this->setArguments($arguments);
  24031. $this->setOptions($options);
  24032. }
  24033. /**
  24034. * Sets the InputArgument objects.
  24035. *
  24036. * @param array $arguments An array of InputArgument objects
  24037. */
  24038. public function setArguments($arguments = array())
  24039. {
  24040. $this->arguments = array();
  24041. $this->requiredCount = 0;
  24042. $this->hasOptional = false;
  24043. $this->hasAnArrayArgument = false;
  24044. $this->addArguments($arguments);
  24045. }
  24046. /**
  24047. * Add an array of InputArgument objects.
  24048. *
  24049. * @param InputArgument[] $arguments An array of InputArgument objects
  24050. */
  24051. public function addArguments($arguments = array())
  24052. {
  24053. if (null !== $arguments) {
  24054. foreach ($arguments as $argument) {
  24055. $this->addArgument($argument);
  24056. }
  24057. }
  24058. }
  24059. /**
  24060. * Add an InputArgument object.
  24061. *
  24062. * @param InputArgument $argument An InputArgument object
  24063. *
  24064. * @throws \LogicException When incorrect argument is given
  24065. */
  24066. public function addArgument(InputArgument $argument)
  24067. {
  24068. if (isset($this->arguments[$argument->getName()])) {
  24069. throw new \LogicException(sprintf('An argument with name "%s" already exist.', $argument->getName()));
  24070. }
  24071. if ($this->hasAnArrayArgument) {
  24072. throw new \LogicException('Cannot add an argument after an array argument.');
  24073. }
  24074. if ($argument->isRequired() && $this->hasOptional) {
  24075. throw new \LogicException('Cannot add a required argument after an optional one.');
  24076. }
  24077. if ($argument->isArray()) {
  24078. $this->hasAnArrayArgument = true;
  24079. }
  24080. if ($argument->isRequired()) {
  24081. ++$this->requiredCount;
  24082. } else {
  24083. $this->hasOptional = true;
  24084. }
  24085. $this->arguments[$argument->getName()] = $argument;
  24086. }
  24087. /**
  24088. * Returns an InputArgument by name or by position.
  24089. *
  24090. * @param string|integer $name The InputArgument name or position
  24091. *
  24092. * @return InputArgument An InputArgument object
  24093. *
  24094. * @throws \InvalidArgumentException When argument given doesn't exist
  24095. */
  24096. public function getArgument($name)
  24097. {
  24098. $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
  24099. if (!$this->hasArgument($name)) {
  24100. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  24101. }
  24102. return $arguments[$name];
  24103. }
  24104. /**
  24105. * Returns true if an InputArgument object exists by name or position.
  24106. *
  24107. * @param string|integer $name The InputArgument name or position
  24108. *
  24109. * @return Boolean true if the InputArgument object exists, false otherwise
  24110. */
  24111. public function hasArgument($name)
  24112. {
  24113. $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
  24114. return isset($arguments[$name]);
  24115. }
  24116. /**
  24117. * Gets the array of InputArgument objects.
  24118. *
  24119. * @return array An array of InputArgument objects
  24120. */
  24121. public function getArguments()
  24122. {
  24123. return $this->arguments;
  24124. }
  24125. /**
  24126. * Returns the number of InputArguments.
  24127. *
  24128. * @return integer The number of InputArguments
  24129. */
  24130. public function getArgumentCount()
  24131. {
  24132. return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
  24133. }
  24134. /**
  24135. * Returns the number of required InputArguments.
  24136. *
  24137. * @return integer The number of required InputArguments
  24138. */
  24139. public function getArgumentRequiredCount()
  24140. {
  24141. return $this->requiredCount;
  24142. }
  24143. /**
  24144. * Gets the default values.
  24145. *
  24146. * @return array An array of default values
  24147. */
  24148. public function getArgumentDefaults()
  24149. {
  24150. $values = array();
  24151. foreach ($this->arguments as $argument) {
  24152. $values[$argument->getName()] = $argument->getDefault();
  24153. }
  24154. return $values;
  24155. }
  24156. /**
  24157. * Sets the InputOption objects.
  24158. *
  24159. * @param array $options An array of InputOption objects
  24160. */
  24161. public function setOptions($options = array())
  24162. {
  24163. $this->options = array();
  24164. $this->shortcuts = array();
  24165. $this->addOptions($options);
  24166. }
  24167. /**
  24168. * Add an array of InputOption objects.
  24169. *
  24170. * @param InputOption[] $options An array of InputOption objects
  24171. */
  24172. public function addOptions($options = array())
  24173. {
  24174. foreach ($options as $option) {
  24175. $this->addOption($option);
  24176. }
  24177. }
  24178. /**
  24179. * Add an InputOption object.
  24180. *
  24181. * @param InputOption $option An InputOption object
  24182. *
  24183. * @throws \LogicException When option given already exist
  24184. */
  24185. public function addOption(InputOption $option)
  24186. {
  24187. if (isset($this->options[$option->getName()])) {
  24188. throw new \LogicException(sprintf('An option named "%s" already exist.', $option->getName()));
  24189. } else if (isset($this->shortcuts[$option->getShortcut()])) {
  24190. throw new \LogicException(sprintf('An option with shortcut "%s" already exist.', $option->getShortcut()));
  24191. }
  24192. $this->options[$option->getName()] = $option;
  24193. if ($option->getShortcut()) {
  24194. $this->shortcuts[$option->getShortcut()] = $option->getName();
  24195. }
  24196. }
  24197. /**
  24198. * Returns an InputOption by name.
  24199. *
  24200. * @param string $name The InputOption name
  24201. *
  24202. * @return InputOption A InputOption object
  24203. */
  24204. public function getOption($name)
  24205. {
  24206. if (!$this->hasOption($name)) {
  24207. throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
  24208. }
  24209. return $this->options[$name];
  24210. }
  24211. /**
  24212. * Returns true if an InputOption object exists by name.
  24213. *
  24214. * @param string $name The InputOption name
  24215. *
  24216. * @return Boolean true if the InputOption object exists, false otherwise
  24217. */
  24218. public function hasOption($name)
  24219. {
  24220. return isset($this->options[$name]);
  24221. }
  24222. /**
  24223. * Gets the array of InputOption objects.
  24224. *
  24225. * @return array An array of InputOption objects
  24226. */
  24227. public function getOptions()
  24228. {
  24229. return $this->options;
  24230. }
  24231. /**
  24232. * Returns true if an InputOption object exists by shortcut.
  24233. *
  24234. * @param string $name The InputOption shortcut
  24235. *
  24236. * @return Boolean true if the InputOption object exists, false otherwise
  24237. */
  24238. public function hasShortcut($name)
  24239. {
  24240. return isset($this->shortcuts[$name]);
  24241. }
  24242. /**
  24243. * Gets an InputOption by shortcut.
  24244. *
  24245. * @return InputOption An InputOption object
  24246. */
  24247. public function getOptionForShortcut($shortcut)
  24248. {
  24249. return $this->getOption($this->shortcutToName($shortcut));
  24250. }
  24251. /**
  24252. * Gets an array of default values.
  24253. *
  24254. * @return array An array of all default values
  24255. */
  24256. public function getOptionDefaults()
  24257. {
  24258. $values = array();
  24259. foreach ($this->options as $option) {
  24260. $values[$option->getName()] = $option->getDefault();
  24261. }
  24262. return $values;
  24263. }
  24264. /**
  24265. * Returns the InputOption name given a shortcut.
  24266. *
  24267. * @param string $shortcut The shortcut
  24268. *
  24269. * @return string The InputOption name
  24270. *
  24271. * @throws \InvalidArgumentException When option given does not exist
  24272. */
  24273. protected function shortcutToName($shortcut)
  24274. {
  24275. if (!isset($this->shortcuts[$shortcut])) {
  24276. throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
  24277. }
  24278. return $this->shortcuts[$shortcut];
  24279. }
  24280. /**
  24281. * Gets the synopsis.
  24282. *
  24283. * @return string The synopsis
  24284. */
  24285. public function getSynopsis()
  24286. {
  24287. $elements = array();
  24288. foreach ($this->getOptions() as $option) {
  24289. $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
  24290. $elements[] = sprintf('['.($option->isParameterRequired() ? '%s--%s="..."' : ($option->isParameterOptional() ? '%s--%s[="..."]' : '%s--%s')).']', $shortcut, $option->getName());
  24291. }
  24292. foreach ($this->getArguments() as $argument) {
  24293. $elements[] = sprintf($argument->isRequired() ? '%s' : '[%s]', $argument->getName().($argument->isArray() ? '1' : ''));
  24294. if ($argument->isArray()) {
  24295. $elements[] = sprintf('... [%sN]', $argument->getName());
  24296. }
  24297. }
  24298. return implode(' ', $elements);
  24299. }
  24300. /**
  24301. * Returns a textual representation of the InputDefinition.
  24302. *
  24303. * @return string A string representing the InputDefinition
  24304. */
  24305. public function asText()
  24306. {
  24307. // find the largest option or argument name
  24308. $max = 0;
  24309. foreach ($this->getOptions() as $option) {
  24310. $max = strlen($option->getName()) + 2 > $max ? strlen($option->getName()) + 2 : $max;
  24311. }
  24312. foreach ($this->getArguments() as $argument) {
  24313. $max = strlen($argument->getName()) > $max ? strlen($argument->getName()) : $max;
  24314. }
  24315. ++$max;
  24316. $text = array();
  24317. if ($this->getArguments()) {
  24318. $text[] = '<comment>Arguments:</comment>';
  24319. foreach ($this->getArguments() as $argument) {
  24320. if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
  24321. $default = sprintf('<comment> (default: %s)</comment>', is_array($argument->getDefault()) ? str_replace("\n", '', var_export($argument->getDefault(), true)): $argument->getDefault());
  24322. } else {
  24323. $default = '';
  24324. }
  24325. $text[] = sprintf(" <info>%-${max}s</info> %s%s", $argument->getName(), $argument->getDescription(), $default);
  24326. }
  24327. $text[] = '';
  24328. }
  24329. if ($this->getOptions()) {
  24330. $text[] = '<comment>Options:</comment>';
  24331. foreach ($this->getOptions() as $option) {
  24332. if ($option->acceptParameter() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
  24333. $default = sprintf('<comment> (default: %s)</comment>', is_array($option->getDefault()) ? str_replace("\n", '', print_r($option->getDefault(), true)): $option->getDefault());
  24334. } else {
  24335. $default = '';
  24336. }
  24337. $multiple = $option->isArray() ? '<comment> (multiple values allowed)</comment>' : '';
  24338. $text[] = sprintf(' %-'.$max.'s %s%s%s%s', '<info>--'.$option->getName().'</info>', $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', $option->getDescription(), $default, $multiple);
  24339. }
  24340. $text[] = '';
  24341. }
  24342. return implode("\n", $text);
  24343. }
  24344. /**
  24345. * Returns an XML representation of the InputDefinition.
  24346. *
  24347. * @param Boolean $asDom Whether to return a DOM or an XML string
  24348. *
  24349. * @return string|DOMDocument An XML string representing the InputDefinition
  24350. */
  24351. public function asXml($asDom = false)
  24352. {
  24353. $dom = new \DOMDocument('1.0', 'UTF-8');
  24354. $dom->formatOutput = true;
  24355. $dom->appendChild($definitionXML = $dom->createElement('definition'));
  24356. $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
  24357. foreach ($this->getArguments() as $argument) {
  24358. $argumentsXML->appendChild($argumentXML = $dom->createElement('argument'));
  24359. $argumentXML->setAttribute('name', $argument->getName());
  24360. $argumentXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
  24361. $argumentXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
  24362. $argumentXML->appendChild($descriptionXML = $dom->createElement('description'));
  24363. $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
  24364. $argumentXML->appendChild($defaultsXML = $dom->createElement('defaults'));
  24365. $defaults = is_array($argument->getDefault()) ? $argument->getDefault() : ($argument->getDefault() ? array($argument->getDefault()) : array());
  24366. foreach ($defaults as $default) {
  24367. $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
  24368. $defaultXML->appendChild($dom->createTextNode($default));
  24369. }
  24370. }
  24371. $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
  24372. foreach ($this->getOptions() as $option) {
  24373. $optionsXML->appendChild($optionXML = $dom->createElement('option'));
  24374. $optionXML->setAttribute('name', '--'.$option->getName());
  24375. $optionXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
  24376. $optionXML->setAttribute('accept_parameter', $option->acceptParameter() ? 1 : 0);
  24377. $optionXML->setAttribute('is_parameter_required', $option->isParameterRequired() ? 1 : 0);
  24378. $optionXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
  24379. $optionXML->appendChild($descriptionXML = $dom->createElement('description'));
  24380. $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
  24381. if ($option->acceptParameter()) {
  24382. $optionXML->appendChild($defaultsXML = $dom->createElement('defaults'));
  24383. $defaults = is_array($option->getDefault()) ? $option->getDefault() : ($option->getDefault() ? array($option->getDefault()) : array());
  24384. foreach ($defaults as $default) {
  24385. $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
  24386. $defaultXML->appendChild($dom->createTextNode($default));
  24387. }
  24388. }
  24389. }
  24390. return $asDom ? $dom : $dom->saveXml();
  24391. }
  24392. }
  24393. <?php
  24394. namespace Symfony\Component\Console\Input;
  24395. /*
  24396. * This file is part of the Symfony framework.
  24397. *
  24398. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  24399. *
  24400. * This source file is subject to the MIT license that is bundled
  24401. * with this source code in the file LICENSE.
  24402. */
  24403. /**
  24404. * InputInterface is the interface implemented by all input classes.
  24405. *
  24406. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  24407. */
  24408. interface InputInterface
  24409. {
  24410. /**
  24411. * Returns the first argument from the raw parameters (not parsed).
  24412. *
  24413. * @return string The value of the first argument or null otherwise
  24414. */
  24415. function getFirstArgument();
  24416. /**
  24417. * Returns true if the raw parameters (not parsed) contains a value.
  24418. *
  24419. * This method is to be used to introspect the input parameters
  24420. * before it has been validated. It must be used carefully.
  24421. *
  24422. * @param string $value The value to look for in the raw parameters
  24423. *
  24424. * @return Boolean true if the value is contained in the raw parameters
  24425. */
  24426. function hasParameterOption($value);
  24427. /**
  24428. * Binds the current Input instance with the given arguments and options.
  24429. *
  24430. * @param InputDefinition $definition A InputDefinition instance
  24431. */
  24432. function bind(InputDefinition $definition);
  24433. function validate();
  24434. function getArguments();
  24435. function getArgument($name);
  24436. function getOptions();
  24437. function getOption($name);
  24438. }
  24439. <?php
  24440. namespace Symfony\Component\Console\Input;
  24441. /*
  24442. * This file is part of the Symfony framework.
  24443. *
  24444. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  24445. *
  24446. * This source file is subject to the MIT license that is bundled
  24447. * with this source code in the file LICENSE.
  24448. */
  24449. /**
  24450. * ArgvInput represents an input coming from the CLI arguments.
  24451. *
  24452. * Usage:
  24453. *
  24454. * $input = new ArgvInput();
  24455. *
  24456. * By default, the `$_SERVER['argv']` array is used for the input values.
  24457. *
  24458. * This can be overridden by explicitly passing the input values in the constructor:
  24459. *
  24460. * $input = new ArgvInput($_SERVER['argv']);
  24461. *
  24462. * If you pass it yourself, don't forget that the first element of the array
  24463. * is the name of the running program.
  24464. *
  24465. * When passing an argument to the constructor, be sure that it respects
  24466. * the same rules as the argv one. It's almost always better to use the
  24467. * `StringInput` when you want to provide your own input.
  24468. *
  24469. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  24470. *
  24471. * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
  24472. * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
  24473. */
  24474. class ArgvInput extends Input
  24475. {
  24476. protected $tokens;
  24477. protected $parsed;
  24478. /**
  24479. * Constructor.
  24480. *
  24481. * @param array $argv An array of parameters from the CLI (in the argv format)
  24482. * @param InputDefinition $definition A InputDefinition instance
  24483. */
  24484. public function __construct(array $argv = null, InputDefinition $definition = null)
  24485. {
  24486. if (null === $argv) {
  24487. $argv = $_SERVER['argv'];
  24488. }
  24489. // strip the program name
  24490. array_shift($argv);
  24491. $this->tokens = $argv;
  24492. parent::__construct($definition);
  24493. }
  24494. /**
  24495. * Processes command line arguments.
  24496. */
  24497. protected function parse()
  24498. {
  24499. $this->parsed = $this->tokens;
  24500. while (null !== $token = array_shift($this->parsed)) {
  24501. if ('--' === substr($token, 0, 2)) {
  24502. $this->parseLongOption($token);
  24503. } elseif ('-' === $token[0]) {
  24504. $this->parseShortOption($token);
  24505. } else {
  24506. $this->parseArgument($token);
  24507. }
  24508. }
  24509. }
  24510. /**
  24511. * Parses a short option.
  24512. *
  24513. * @param string $token The current token.
  24514. */
  24515. protected function parseShortOption($token)
  24516. {
  24517. $name = substr($token, 1);
  24518. if (strlen($name) > 1) {
  24519. if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptParameter()) {
  24520. // an option with a value (with no space)
  24521. $this->addShortOption($name[0], substr($name, 1));
  24522. } else {
  24523. $this->parseShortOptionSet($name);
  24524. }
  24525. } else {
  24526. $this->addShortOption($name, null);
  24527. }
  24528. }
  24529. /**
  24530. * Parses a short option set.
  24531. *
  24532. * @param string $token The current token
  24533. *
  24534. * @throws \RuntimeException When option given doesn't exist
  24535. */
  24536. protected function parseShortOptionSet($name)
  24537. {
  24538. $len = strlen($name);
  24539. for ($i = 0; $i < $len; $i++) {
  24540. if (!$this->definition->hasShortcut($name[$i])) {
  24541. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
  24542. }
  24543. $option = $this->definition->getOptionForShortcut($name[$i]);
  24544. if ($option->acceptParameter()) {
  24545. $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
  24546. break;
  24547. } else {
  24548. $this->addLongOption($option->getName(), true);
  24549. }
  24550. }
  24551. }
  24552. /**
  24553. * Parses a long option.
  24554. *
  24555. * @param string $token The current token
  24556. */
  24557. protected function parseLongOption($token)
  24558. {
  24559. $name = substr($token, 2);
  24560. if (false !== $pos = strpos($name, '=')) {
  24561. $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
  24562. } else {
  24563. $this->addLongOption($name, null);
  24564. }
  24565. }
  24566. /**
  24567. * Parses an argument.
  24568. *
  24569. * @param string $token The current token
  24570. *
  24571. * @throws \RuntimeException When too many arguments are given
  24572. */
  24573. protected function parseArgument($token)
  24574. {
  24575. if (!$this->definition->hasArgument(count($this->arguments))) {
  24576. throw new \RuntimeException('Too many arguments.');
  24577. }
  24578. $this->arguments[$this->definition->getArgument(count($this->arguments))->getName()] = $token;
  24579. }
  24580. /**
  24581. * Adds a short option value.
  24582. *
  24583. * @param string $shortcut The short option key
  24584. * @param mixed $value The value for the option
  24585. *
  24586. * @throws \RuntimeException When option given doesn't exist
  24587. */
  24588. protected function addShortOption($shortcut, $value)
  24589. {
  24590. if (!$this->definition->hasShortcut($shortcut)) {
  24591. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
  24592. }
  24593. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  24594. }
  24595. /**
  24596. * Adds a long option value.
  24597. *
  24598. * @param string $name The long option key
  24599. * @param mixed $value The value for the option
  24600. *
  24601. * @throws \RuntimeException When option given doesn't exist
  24602. */
  24603. protected function addLongOption($name, $value)
  24604. {
  24605. if (!$this->definition->hasOption($name)) {
  24606. throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
  24607. }
  24608. $option = $this->definition->getOption($name);
  24609. if (null === $value && $option->acceptParameter()) {
  24610. // if option accepts an optional or mandatory argument
  24611. // let's see if there is one provided
  24612. $next = array_shift($this->parsed);
  24613. if ('-' !== $next[0]) {
  24614. $value = $next;
  24615. } else {
  24616. array_unshift($this->parsed, $next);
  24617. }
  24618. }
  24619. if (null === $value) {
  24620. if ($option->isParameterRequired()) {
  24621. throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
  24622. }
  24623. $value = $option->isParameterOptional() ? $option->getDefault() : true;
  24624. }
  24625. $this->options[$name] = $value;
  24626. }
  24627. /**
  24628. * Returns the first argument from the raw parameters (not parsed).
  24629. *
  24630. * @return string The value of the first argument or null otherwise
  24631. */
  24632. public function getFirstArgument()
  24633. {
  24634. foreach ($this->tokens as $token) {
  24635. if ($token && '-' === $token[0]) {
  24636. continue;
  24637. }
  24638. return $token;
  24639. }
  24640. }
  24641. /**
  24642. * Returns true if the raw parameters (not parsed) contains a value.
  24643. *
  24644. * This method is to be used to introspect the input parameters
  24645. * before it has been validated. It must be used carefully.
  24646. *
  24647. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  24648. *
  24649. * @return Boolean true if the value is contained in the raw parameters
  24650. */
  24651. public function hasParameterOption($values)
  24652. {
  24653. if (!is_array($values)) {
  24654. $values = array($values);
  24655. }
  24656. foreach ($this->tokens as $v) {
  24657. if (in_array($v, $values)) {
  24658. return true;
  24659. }
  24660. }
  24661. return false;
  24662. }
  24663. }
  24664. <?php
  24665. namespace Symfony\Component\Console\Input;
  24666. /*
  24667. * This file is part of the Symfony framework.
  24668. *
  24669. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  24670. *
  24671. * This source file is subject to the MIT license that is bundled
  24672. * with this source code in the file LICENSE.
  24673. */
  24674. /**
  24675. * StringInput represents an input provided as a string.
  24676. *
  24677. * Usage:
  24678. *
  24679. * $input = new StringInput('foo --bar="foobar"');
  24680. *
  24681. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  24682. */
  24683. class StringInput extends ArgvInput
  24684. {
  24685. const REGEX_STRING = '([^ ]+?)(?: |(?<!\\\\)"|(?<!\\\\)\'|$)';
  24686. const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
  24687. /**
  24688. * Constructor.
  24689. *
  24690. * @param string $input An array of parameters from the CLI (in the argv format)
  24691. * @param InputDefinition $definition A InputDefinition instance
  24692. */
  24693. public function __construct($input, InputDefinition $definition = null)
  24694. {
  24695. parent::__construct(array(), $definition);
  24696. $this->tokens = $this->tokenize($input);
  24697. }
  24698. /**
  24699. * @throws \InvalidArgumentException When unable to parse input (should never happen)
  24700. */
  24701. protected function tokenize($input)
  24702. {
  24703. $input = preg_replace('/(\r\n|\r|\n|\t)/', ' ', $input);
  24704. $tokens = array();
  24705. $length = strlen($input);
  24706. $cursor = 0;
  24707. while ($cursor < $length) {
  24708. if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
  24709. } elseif (preg_match('/([^="\' ]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
  24710. $tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2)));
  24711. } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
  24712. $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
  24713. } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) {
  24714. $tokens[] = stripcslashes($match[1]);
  24715. } else {
  24716. // should never happen
  24717. // @codeCoverageIgnoreStart
  24718. throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
  24719. // @codeCoverageIgnoreEnd
  24720. }
  24721. $cursor += strlen($match[0]);
  24722. }
  24723. return $tokens;
  24724. }
  24725. }
  24726. <?php
  24727. namespace Symfony\Component\Console\Input;
  24728. /*
  24729. * This file is part of the Symfony framework.
  24730. *
  24731. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  24732. *
  24733. * This source file is subject to the MIT license that is bundled
  24734. * with this source code in the file LICENSE.
  24735. */
  24736. /**
  24737. * Represents a command line argument.
  24738. *
  24739. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  24740. */
  24741. class InputArgument
  24742. {
  24743. const REQUIRED = 1;
  24744. const OPTIONAL = 2;
  24745. const IS_ARRAY = 4;
  24746. protected $name;
  24747. protected $mode;
  24748. protected $default;
  24749. protected $description;
  24750. /**
  24751. * Constructor.
  24752. *
  24753. * @param string $name The argument name
  24754. * @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL
  24755. * @param string $description A description text
  24756. * @param mixed $default The default value (for self::OPTIONAL mode only)
  24757. *
  24758. * @throws \InvalidArgumentException When argument mode is not valid
  24759. */
  24760. public function __construct($name, $mode = null, $description = '', $default = null)
  24761. {
  24762. if (null === $mode) {
  24763. $mode = self::OPTIONAL;
  24764. } else if (is_string($mode) || $mode > 7) {
  24765. throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
  24766. }
  24767. $this->name = $name;
  24768. $this->mode = $mode;
  24769. $this->description = $description;
  24770. $this->setDefault($default);
  24771. }
  24772. /**
  24773. * Returns the argument name.
  24774. *
  24775. * @return string The argument name
  24776. */
  24777. public function getName()
  24778. {
  24779. return $this->name;
  24780. }
  24781. /**
  24782. * Returns true if the argument is required.
  24783. *
  24784. * @return Boolean true if parameter mode is self::REQUIRED, false otherwise
  24785. */
  24786. public function isRequired()
  24787. {
  24788. return self::REQUIRED === (self::REQUIRED & $this->mode);
  24789. }
  24790. /**
  24791. * Returns true if the argument can take multiple values.
  24792. *
  24793. * @return Boolean true if mode is self::IS_ARRAY, false otherwise
  24794. */
  24795. public function isArray()
  24796. {
  24797. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  24798. }
  24799. /**
  24800. * Sets the default value.
  24801. *
  24802. * @param mixed $default The default value
  24803. *
  24804. * @throws \LogicException When incorrect default value is given
  24805. */
  24806. public function setDefault($default = null)
  24807. {
  24808. if (self::REQUIRED === $this->mode && null !== $default) {
  24809. throw new \LogicException('Cannot set a default value except for Parameter::OPTIONAL mode.');
  24810. }
  24811. if ($this->isArray()) {
  24812. if (null === $default) {
  24813. $default = array();
  24814. } else if (!is_array($default)) {
  24815. throw new \LogicException('A default value for an array argument must be an array.');
  24816. }
  24817. }
  24818. $this->default = $default;
  24819. }
  24820. /**
  24821. * Returns the default value.
  24822. *
  24823. * @return mixed The default value
  24824. */
  24825. public function getDefault()
  24826. {
  24827. return $this->default;
  24828. }
  24829. /**
  24830. * Returns the description text.
  24831. *
  24832. * @return string The description text
  24833. */
  24834. public function getDescription()
  24835. {
  24836. return $this->description;
  24837. }
  24838. }
  24839. Ûes…yepâC ÷p͸Õ<)¸���GBMB