2
0

SqliteItemRepository.cs 225 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971
  1. #nullable disable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading;
  12. using Emby.Server.Implementations.Playlists;
  13. using Jellyfin.Data.Enums;
  14. using Jellyfin.Extensions;
  15. using Jellyfin.Extensions.Json;
  16. using MediaBrowser.Controller;
  17. using MediaBrowser.Controller.Channels;
  18. using MediaBrowser.Controller.Configuration;
  19. using MediaBrowser.Controller.Drawing;
  20. using MediaBrowser.Controller.Entities;
  21. using MediaBrowser.Controller.Entities.Audio;
  22. using MediaBrowser.Controller.Entities.Movies;
  23. using MediaBrowser.Controller.Entities.TV;
  24. using MediaBrowser.Controller.Extensions;
  25. using MediaBrowser.Controller.LiveTv;
  26. using MediaBrowser.Controller.Persistence;
  27. using MediaBrowser.Controller.Playlists;
  28. using MediaBrowser.Model.Dto;
  29. using MediaBrowser.Model.Entities;
  30. using MediaBrowser.Model.Globalization;
  31. using MediaBrowser.Model.LiveTv;
  32. using MediaBrowser.Model.Querying;
  33. using Microsoft.Data.Sqlite;
  34. using Microsoft.Extensions.Configuration;
  35. using Microsoft.Extensions.Logging;
  36. namespace Emby.Server.Implementations.Data
  37. {
  38. /// <summary>
  39. /// Class SQLiteItemRepository.
  40. /// </summary>
  41. public class SqliteItemRepository : BaseSqliteRepository, IItemRepository
  42. {
  43. private const string FromText = " from TypedBaseItems A";
  44. private const string ChaptersTableName = "Chapters2";
  45. private const string SaveItemCommandText =
  46. @"replace into TypedBaseItems
  47. (guid,type,data,Path,StartDate,EndDate,ChannelId,IsMovie,IsSeries,EpisodeTitle,IsRepeat,CommunityRating,CustomRating,IndexNumber,IsLocked,Name,OfficialRating,MediaType,Overview,ParentIndexNumber,PremiereDate,ProductionYear,ParentId,Genres,InheritedParentalRatingValue,SortName,ForcedSortName,RunTimeTicks,Size,DateCreated,DateModified,PreferredMetadataLanguage,PreferredMetadataCountryCode,Width,Height,DateLastRefreshed,DateLastSaved,IsInMixedFolder,LockedFields,Studios,Audio,ExternalServiceId,Tags,IsFolder,UnratedType,TopParentId,TrailerTypes,CriticRating,CleanName,PresentationUniqueKey,OriginalTitle,PrimaryVersionId,DateLastMediaAdded,Album,LUFS,NormalizationGain,IsVirtualItem,SeriesName,UserDataKey,SeasonName,SeasonId,SeriesId,ExternalSeriesId,Tagline,ProviderIds,Images,ProductionLocations,ExtraIds,TotalBitrate,ExtraType,Artists,AlbumArtists,ExternalId,SeriesPresentationUniqueKey,ShowId,OwnerId)
  48. values (@guid,@type,@data,@Path,@StartDate,@EndDate,@ChannelId,@IsMovie,@IsSeries,@EpisodeTitle,@IsRepeat,@CommunityRating,@CustomRating,@IndexNumber,@IsLocked,@Name,@OfficialRating,@MediaType,@Overview,@ParentIndexNumber,@PremiereDate,@ProductionYear,@ParentId,@Genres,@InheritedParentalRatingValue,@SortName,@ForcedSortName,@RunTimeTicks,@Size,@DateCreated,@DateModified,@PreferredMetadataLanguage,@PreferredMetadataCountryCode,@Width,@Height,@DateLastRefreshed,@DateLastSaved,@IsInMixedFolder,@LockedFields,@Studios,@Audio,@ExternalServiceId,@Tags,@IsFolder,@UnratedType,@TopParentId,@TrailerTypes,@CriticRating,@CleanName,@PresentationUniqueKey,@OriginalTitle,@PrimaryVersionId,@DateLastMediaAdded,@Album,@LUFS,@NormalizationGain,@IsVirtualItem,@SeriesName,@UserDataKey,@SeasonName,@SeasonId,@SeriesId,@ExternalSeriesId,@Tagline,@ProviderIds,@Images,@ProductionLocations,@ExtraIds,@TotalBitrate,@ExtraType,@Artists,@AlbumArtists,@ExternalId,@SeriesPresentationUniqueKey,@ShowId,@OwnerId)";
  49. private readonly IServerConfigurationManager _config;
  50. private readonly IServerApplicationHost _appHost;
  51. private readonly ILocalizationManager _localization;
  52. // TODO: Remove this dependency. GetImageCacheTag() is the only method used and it can be converted to a static helper method
  53. private readonly IImageProcessor _imageProcessor;
  54. private readonly TypeMapper _typeMapper;
  55. private readonly JsonSerializerOptions _jsonOptions;
  56. private readonly ItemFields[] _allItemFields = Enum.GetValues<ItemFields>();
  57. private static readonly string[] _retrieveItemColumns =
  58. {
  59. "type",
  60. "data",
  61. "StartDate",
  62. "EndDate",
  63. "ChannelId",
  64. "IsMovie",
  65. "IsSeries",
  66. "EpisodeTitle",
  67. "IsRepeat",
  68. "CommunityRating",
  69. "CustomRating",
  70. "IndexNumber",
  71. "IsLocked",
  72. "PreferredMetadataLanguage",
  73. "PreferredMetadataCountryCode",
  74. "Width",
  75. "Height",
  76. "DateLastRefreshed",
  77. "Name",
  78. "Path",
  79. "PremiereDate",
  80. "Overview",
  81. "ParentIndexNumber",
  82. "ProductionYear",
  83. "OfficialRating",
  84. "ForcedSortName",
  85. "RunTimeTicks",
  86. "Size",
  87. "DateCreated",
  88. "DateModified",
  89. "guid",
  90. "Genres",
  91. "ParentId",
  92. "Audio",
  93. "ExternalServiceId",
  94. "IsInMixedFolder",
  95. "DateLastSaved",
  96. "LockedFields",
  97. "Studios",
  98. "Tags",
  99. "TrailerTypes",
  100. "OriginalTitle",
  101. "PrimaryVersionId",
  102. "DateLastMediaAdded",
  103. "Album",
  104. "LUFS",
  105. "NormalizationGain",
  106. "CriticRating",
  107. "IsVirtualItem",
  108. "SeriesName",
  109. "SeasonName",
  110. "SeasonId",
  111. "SeriesId",
  112. "PresentationUniqueKey",
  113. "InheritedParentalRatingValue",
  114. "ExternalSeriesId",
  115. "Tagline",
  116. "ProviderIds",
  117. "Images",
  118. "ProductionLocations",
  119. "ExtraIds",
  120. "TotalBitrate",
  121. "ExtraType",
  122. "Artists",
  123. "AlbumArtists",
  124. "ExternalId",
  125. "SeriesPresentationUniqueKey",
  126. "ShowId",
  127. "OwnerId"
  128. };
  129. private static readonly string _retrieveItemColumnsSelectQuery = $"select {string.Join(',', _retrieveItemColumns)} from TypedBaseItems where guid = @guid";
  130. private static readonly string[] _mediaStreamSaveColumns =
  131. {
  132. "ItemId",
  133. "StreamIndex",
  134. "StreamType",
  135. "Codec",
  136. "Language",
  137. "ChannelLayout",
  138. "Profile",
  139. "AspectRatio",
  140. "Path",
  141. "IsInterlaced",
  142. "BitRate",
  143. "Channels",
  144. "SampleRate",
  145. "IsDefault",
  146. "IsForced",
  147. "IsExternal",
  148. "Height",
  149. "Width",
  150. "AverageFrameRate",
  151. "RealFrameRate",
  152. "Level",
  153. "PixelFormat",
  154. "BitDepth",
  155. "IsAnamorphic",
  156. "RefFrames",
  157. "CodecTag",
  158. "Comment",
  159. "NalLengthSize",
  160. "IsAvc",
  161. "Title",
  162. "TimeBase",
  163. "CodecTimeBase",
  164. "ColorPrimaries",
  165. "ColorSpace",
  166. "ColorTransfer",
  167. "DvVersionMajor",
  168. "DvVersionMinor",
  169. "DvProfile",
  170. "DvLevel",
  171. "RpuPresentFlag",
  172. "ElPresentFlag",
  173. "BlPresentFlag",
  174. "DvBlSignalCompatibilityId",
  175. "IsHearingImpaired",
  176. "Rotation"
  177. };
  178. private static readonly string _mediaStreamSaveColumnsInsertQuery =
  179. $"insert into mediastreams ({string.Join(',', _mediaStreamSaveColumns)}) values ";
  180. private static readonly string _mediaStreamSaveColumnsSelectQuery =
  181. $"select {string.Join(',', _mediaStreamSaveColumns)} from mediastreams where ItemId=@ItemId";
  182. private static readonly string[] _mediaAttachmentSaveColumns =
  183. {
  184. "ItemId",
  185. "AttachmentIndex",
  186. "Codec",
  187. "CodecTag",
  188. "Comment",
  189. "Filename",
  190. "MIMEType"
  191. };
  192. private static readonly string _mediaAttachmentSaveColumnsSelectQuery =
  193. $"select {string.Join(',', _mediaAttachmentSaveColumns)} from mediaattachments where ItemId=@ItemId";
  194. private static readonly string _mediaAttachmentInsertPrefix = BuildMediaAttachmentInsertPrefix();
  195. private static readonly BaseItemKind[] _programTypes = new[]
  196. {
  197. BaseItemKind.Program,
  198. BaseItemKind.TvChannel,
  199. BaseItemKind.LiveTvProgram,
  200. BaseItemKind.LiveTvChannel
  201. };
  202. private static readonly BaseItemKind[] _programExcludeParentTypes = new[]
  203. {
  204. BaseItemKind.Series,
  205. BaseItemKind.Season,
  206. BaseItemKind.MusicAlbum,
  207. BaseItemKind.MusicArtist,
  208. BaseItemKind.PhotoAlbum
  209. };
  210. private static readonly BaseItemKind[] _serviceTypes = new[]
  211. {
  212. BaseItemKind.TvChannel,
  213. BaseItemKind.LiveTvChannel
  214. };
  215. private static readonly BaseItemKind[] _startDateTypes = new[]
  216. {
  217. BaseItemKind.Program,
  218. BaseItemKind.LiveTvProgram
  219. };
  220. private static readonly BaseItemKind[] _seriesTypes = new[]
  221. {
  222. BaseItemKind.Book,
  223. BaseItemKind.AudioBook,
  224. BaseItemKind.Episode,
  225. BaseItemKind.Season
  226. };
  227. private static readonly BaseItemKind[] _artistExcludeParentTypes = new[]
  228. {
  229. BaseItemKind.Series,
  230. BaseItemKind.Season,
  231. BaseItemKind.PhotoAlbum
  232. };
  233. private static readonly BaseItemKind[] _artistsTypes = new[]
  234. {
  235. BaseItemKind.Audio,
  236. BaseItemKind.MusicAlbum,
  237. BaseItemKind.MusicVideo,
  238. BaseItemKind.AudioBook
  239. };
  240. private static readonly Dictionary<BaseItemKind, string> _baseItemKindNames = new()
  241. {
  242. { BaseItemKind.AggregateFolder, typeof(AggregateFolder).FullName },
  243. { BaseItemKind.Audio, typeof(Audio).FullName },
  244. { BaseItemKind.AudioBook, typeof(AudioBook).FullName },
  245. { BaseItemKind.BasePluginFolder, typeof(BasePluginFolder).FullName },
  246. { BaseItemKind.Book, typeof(Book).FullName },
  247. { BaseItemKind.BoxSet, typeof(BoxSet).FullName },
  248. { BaseItemKind.Channel, typeof(Channel).FullName },
  249. { BaseItemKind.CollectionFolder, typeof(CollectionFolder).FullName },
  250. { BaseItemKind.Episode, typeof(Episode).FullName },
  251. { BaseItemKind.Folder, typeof(Folder).FullName },
  252. { BaseItemKind.Genre, typeof(Genre).FullName },
  253. { BaseItemKind.Movie, typeof(Movie).FullName },
  254. { BaseItemKind.LiveTvChannel, typeof(LiveTvChannel).FullName },
  255. { BaseItemKind.LiveTvProgram, typeof(LiveTvProgram).FullName },
  256. { BaseItemKind.MusicAlbum, typeof(MusicAlbum).FullName },
  257. { BaseItemKind.MusicArtist, typeof(MusicArtist).FullName },
  258. { BaseItemKind.MusicGenre, typeof(MusicGenre).FullName },
  259. { BaseItemKind.MusicVideo, typeof(MusicVideo).FullName },
  260. { BaseItemKind.Person, typeof(Person).FullName },
  261. { BaseItemKind.Photo, typeof(Photo).FullName },
  262. { BaseItemKind.PhotoAlbum, typeof(PhotoAlbum).FullName },
  263. { BaseItemKind.Playlist, typeof(Playlist).FullName },
  264. { BaseItemKind.PlaylistsFolder, typeof(PlaylistsFolder).FullName },
  265. { BaseItemKind.Season, typeof(Season).FullName },
  266. { BaseItemKind.Series, typeof(Series).FullName },
  267. { BaseItemKind.Studio, typeof(Studio).FullName },
  268. { BaseItemKind.Trailer, typeof(Trailer).FullName },
  269. { BaseItemKind.TvChannel, typeof(LiveTvChannel).FullName },
  270. { BaseItemKind.TvProgram, typeof(LiveTvProgram).FullName },
  271. { BaseItemKind.UserRootFolder, typeof(UserRootFolder).FullName },
  272. { BaseItemKind.UserView, typeof(UserView).FullName },
  273. { BaseItemKind.Video, typeof(Video).FullName },
  274. { BaseItemKind.Year, typeof(Year).FullName }
  275. };
  276. /// <summary>
  277. /// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
  278. /// </summary>
  279. /// <param name="config">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  280. /// <param name="appHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
  281. /// <param name="logger">Instance of the <see cref="ILogger{SqliteItemRepository}"/> interface.</param>
  282. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  283. /// <param name="imageProcessor">Instance of the <see cref="IImageProcessor"/> interface.</param>
  284. /// <param name="configuration">Instance of the <see cref="IConfiguration"/> interface.</param>
  285. /// <exception cref="ArgumentNullException">config is null.</exception>
  286. public SqliteItemRepository(
  287. IServerConfigurationManager config,
  288. IServerApplicationHost appHost,
  289. ILogger<SqliteItemRepository> logger,
  290. ILocalizationManager localization,
  291. IImageProcessor imageProcessor,
  292. IConfiguration configuration)
  293. : base(logger)
  294. {
  295. _config = config;
  296. _appHost = appHost;
  297. _localization = localization;
  298. _imageProcessor = imageProcessor;
  299. _typeMapper = new TypeMapper();
  300. _jsonOptions = JsonDefaults.Options;
  301. DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db");
  302. CacheSize = configuration.GetSqliteCacheSize();
  303. }
  304. /// <inheritdoc />
  305. protected override int? CacheSize { get; }
  306. /// <inheritdoc />
  307. protected override TempStoreMode TempStore => TempStoreMode.Memory;
  308. /// <summary>
  309. /// Opens the connection to the database.
  310. /// </summary>
  311. public override void Initialize()
  312. {
  313. base.Initialize();
  314. const string CreateMediaStreamsTableCommand
  315. = "create table if not exists mediastreams (ItemId GUID, StreamIndex INT, StreamType TEXT, Codec TEXT, Language TEXT, ChannelLayout TEXT, Profile TEXT, AspectRatio TEXT, Path TEXT, IsInterlaced BIT, BitRate INT NULL, Channels INT NULL, SampleRate INT NULL, IsDefault BIT, IsForced BIT, IsExternal BIT, Height INT NULL, Width INT NULL, AverageFrameRate FLOAT NULL, RealFrameRate FLOAT NULL, Level FLOAT NULL, PixelFormat TEXT, BitDepth INT NULL, IsAnamorphic BIT NULL, RefFrames INT NULL, CodecTag TEXT NULL, Comment TEXT NULL, NalLengthSize TEXT NULL, IsAvc BIT NULL, Title TEXT NULL, TimeBase TEXT NULL, CodecTimeBase TEXT NULL, ColorPrimaries TEXT NULL, ColorSpace TEXT NULL, ColorTransfer TEXT NULL, DvVersionMajor INT NULL, DvVersionMinor INT NULL, DvProfile INT NULL, DvLevel INT NULL, RpuPresentFlag INT NULL, ElPresentFlag INT NULL, BlPresentFlag INT NULL, DvBlSignalCompatibilityId INT NULL, IsHearingImpaired BIT NULL, Rotation INT NULL, PRIMARY KEY (ItemId, StreamIndex))";
  316. const string CreateMediaAttachmentsTableCommand
  317. = "create table if not exists mediaattachments (ItemId GUID, AttachmentIndex INT, Codec TEXT, CodecTag TEXT NULL, Comment TEXT NULL, Filename TEXT NULL, MIMEType TEXT NULL, PRIMARY KEY (ItemId, AttachmentIndex))";
  318. string[] queries =
  319. {
  320. "create table if not exists TypedBaseItems (guid GUID primary key NOT NULL, type TEXT NOT NULL, data BLOB NULL, ParentId GUID NULL, Path TEXT NULL)",
  321. "create table if not exists AncestorIds (ItemId GUID NOT NULL, AncestorId GUID NOT NULL, AncestorIdText TEXT NOT NULL, PRIMARY KEY (ItemId, AncestorId))",
  322. "create index if not exists idx_AncestorIds1 on AncestorIds(AncestorId)",
  323. "create index if not exists idx_AncestorIds5 on AncestorIds(AncestorIdText,ItemId)",
  324. "create table if not exists ItemValues (ItemId GUID NOT NULL, Type INT NOT NULL, Value TEXT NOT NULL, CleanValue TEXT NOT NULL)",
  325. "create table if not exists People (ItemId GUID, Name TEXT NOT NULL, Role TEXT, PersonType TEXT, SortOrder int, ListOrder int)",
  326. "drop index if exists idxPeopleItemId",
  327. "create index if not exists idxPeopleItemId1 on People(ItemId,ListOrder)",
  328. "create index if not exists idxPeopleName on People(Name)",
  329. "create table if not exists " + ChaptersTableName + " (ItemId GUID, ChapterIndex INT NOT NULL, StartPositionTicks BIGINT NOT NULL, Name TEXT, ImagePath TEXT, PRIMARY KEY (ItemId, ChapterIndex))",
  330. CreateMediaStreamsTableCommand,
  331. CreateMediaAttachmentsTableCommand,
  332. "pragma shrink_memory"
  333. };
  334. string[] postQueries =
  335. {
  336. "create index if not exists idx_PathTypedBaseItems on TypedBaseItems(Path)",
  337. "create index if not exists idx_ParentIdTypedBaseItems on TypedBaseItems(ParentId)",
  338. "create index if not exists idx_PresentationUniqueKey on TypedBaseItems(PresentationUniqueKey)",
  339. "create index if not exists idx_GuidTypeIsFolderIsVirtualItem on TypedBaseItems(Guid,Type,IsFolder,IsVirtualItem)",
  340. "create index if not exists idx_CleanNameType on TypedBaseItems(CleanName,Type)",
  341. // covering index
  342. "create index if not exists idx_TopParentIdGuid on TypedBaseItems(TopParentId,Guid)",
  343. // series
  344. "create index if not exists idx_TypeSeriesPresentationUniqueKey1 on TypedBaseItems(Type,SeriesPresentationUniqueKey,PresentationUniqueKey,SortName)",
  345. // series counts
  346. // seriesdateplayed sort order
  347. "create index if not exists idx_TypeSeriesPresentationUniqueKey3 on TypedBaseItems(SeriesPresentationUniqueKey,Type,IsFolder,IsVirtualItem)",
  348. // live tv programs
  349. "create index if not exists idx_TypeTopParentIdStartDate on TypedBaseItems(Type,TopParentId,StartDate)",
  350. // covering index for getitemvalues
  351. "create index if not exists idx_TypeTopParentIdGuid on TypedBaseItems(Type,TopParentId,Guid)",
  352. // used by movie suggestions
  353. "create index if not exists idx_TypeTopParentIdGroup on TypedBaseItems(Type,TopParentId,PresentationUniqueKey)",
  354. "create index if not exists idx_TypeTopParentId5 on TypedBaseItems(TopParentId,IsVirtualItem)",
  355. // latest items
  356. "create index if not exists idx_TypeTopParentId9 on TypedBaseItems(TopParentId,Type,IsVirtualItem,PresentationUniqueKey,DateCreated)",
  357. "create index if not exists idx_TypeTopParentId8 on TypedBaseItems(TopParentId,IsFolder,IsVirtualItem,PresentationUniqueKey,DateCreated)",
  358. // resume
  359. "create index if not exists idx_TypeTopParentId7 on TypedBaseItems(TopParentId,MediaType,IsVirtualItem,PresentationUniqueKey)",
  360. // items by name
  361. "create index if not exists idx_ItemValues6 on ItemValues(ItemId,Type,CleanValue)",
  362. "create index if not exists idx_ItemValues7 on ItemValues(Type,CleanValue,ItemId)",
  363. // Used to update inherited tags
  364. "create index if not exists idx_ItemValues8 on ItemValues(Type, ItemId, Value)",
  365. "CREATE INDEX IF NOT EXISTS idx_TypedBaseItemsUserDataKeyType ON TypedBaseItems(UserDataKey, Type)",
  366. "CREATE INDEX IF NOT EXISTS idx_PeopleNameListOrder ON People(Name, ListOrder)"
  367. };
  368. using (var connection = GetConnection())
  369. using (var transaction = connection.BeginTransaction())
  370. {
  371. connection.Execute(string.Join(';', queries));
  372. var existingColumnNames = GetColumnNames(connection, "AncestorIds");
  373. AddColumn(connection, "AncestorIds", "AncestorIdText", "Text", existingColumnNames);
  374. existingColumnNames = GetColumnNames(connection, "TypedBaseItems");
  375. AddColumn(connection, "TypedBaseItems", "Path", "Text", existingColumnNames);
  376. AddColumn(connection, "TypedBaseItems", "StartDate", "DATETIME", existingColumnNames);
  377. AddColumn(connection, "TypedBaseItems", "EndDate", "DATETIME", existingColumnNames);
  378. AddColumn(connection, "TypedBaseItems", "ChannelId", "Text", existingColumnNames);
  379. AddColumn(connection, "TypedBaseItems", "IsMovie", "BIT", existingColumnNames);
  380. AddColumn(connection, "TypedBaseItems", "CommunityRating", "Float", existingColumnNames);
  381. AddColumn(connection, "TypedBaseItems", "CustomRating", "Text", existingColumnNames);
  382. AddColumn(connection, "TypedBaseItems", "IndexNumber", "INT", existingColumnNames);
  383. AddColumn(connection, "TypedBaseItems", "IsLocked", "BIT", existingColumnNames);
  384. AddColumn(connection, "TypedBaseItems", "Name", "Text", existingColumnNames);
  385. AddColumn(connection, "TypedBaseItems", "OfficialRating", "Text", existingColumnNames);
  386. AddColumn(connection, "TypedBaseItems", "MediaType", "Text", existingColumnNames);
  387. AddColumn(connection, "TypedBaseItems", "Overview", "Text", existingColumnNames);
  388. AddColumn(connection, "TypedBaseItems", "ParentIndexNumber", "INT", existingColumnNames);
  389. AddColumn(connection, "TypedBaseItems", "PremiereDate", "DATETIME", existingColumnNames);
  390. AddColumn(connection, "TypedBaseItems", "ProductionYear", "INT", existingColumnNames);
  391. AddColumn(connection, "TypedBaseItems", "ParentId", "GUID", existingColumnNames);
  392. AddColumn(connection, "TypedBaseItems", "Genres", "Text", existingColumnNames);
  393. AddColumn(connection, "TypedBaseItems", "SortName", "Text", existingColumnNames);
  394. AddColumn(connection, "TypedBaseItems", "ForcedSortName", "Text", existingColumnNames);
  395. AddColumn(connection, "TypedBaseItems", "RunTimeTicks", "BIGINT", existingColumnNames);
  396. AddColumn(connection, "TypedBaseItems", "DateCreated", "DATETIME", existingColumnNames);
  397. AddColumn(connection, "TypedBaseItems", "DateModified", "DATETIME", existingColumnNames);
  398. AddColumn(connection, "TypedBaseItems", "IsSeries", "BIT", existingColumnNames);
  399. AddColumn(connection, "TypedBaseItems", "EpisodeTitle", "Text", existingColumnNames);
  400. AddColumn(connection, "TypedBaseItems", "IsRepeat", "BIT", existingColumnNames);
  401. AddColumn(connection, "TypedBaseItems", "PreferredMetadataLanguage", "Text", existingColumnNames);
  402. AddColumn(connection, "TypedBaseItems", "PreferredMetadataCountryCode", "Text", existingColumnNames);
  403. AddColumn(connection, "TypedBaseItems", "DateLastRefreshed", "DATETIME", existingColumnNames);
  404. AddColumn(connection, "TypedBaseItems", "DateLastSaved", "DATETIME", existingColumnNames);
  405. AddColumn(connection, "TypedBaseItems", "IsInMixedFolder", "BIT", existingColumnNames);
  406. AddColumn(connection, "TypedBaseItems", "LockedFields", "Text", existingColumnNames);
  407. AddColumn(connection, "TypedBaseItems", "Studios", "Text", existingColumnNames);
  408. AddColumn(connection, "TypedBaseItems", "Audio", "Text", existingColumnNames);
  409. AddColumn(connection, "TypedBaseItems", "ExternalServiceId", "Text", existingColumnNames);
  410. AddColumn(connection, "TypedBaseItems", "Tags", "Text", existingColumnNames);
  411. AddColumn(connection, "TypedBaseItems", "IsFolder", "BIT", existingColumnNames);
  412. AddColumn(connection, "TypedBaseItems", "InheritedParentalRatingValue", "INT", existingColumnNames);
  413. AddColumn(connection, "TypedBaseItems", "UnratedType", "Text", existingColumnNames);
  414. AddColumn(connection, "TypedBaseItems", "TopParentId", "Text", existingColumnNames);
  415. AddColumn(connection, "TypedBaseItems", "TrailerTypes", "Text", existingColumnNames);
  416. AddColumn(connection, "TypedBaseItems", "CriticRating", "Float", existingColumnNames);
  417. AddColumn(connection, "TypedBaseItems", "CleanName", "Text", existingColumnNames);
  418. AddColumn(connection, "TypedBaseItems", "PresentationUniqueKey", "Text", existingColumnNames);
  419. AddColumn(connection, "TypedBaseItems", "OriginalTitle", "Text", existingColumnNames);
  420. AddColumn(connection, "TypedBaseItems", "PrimaryVersionId", "Text", existingColumnNames);
  421. AddColumn(connection, "TypedBaseItems", "DateLastMediaAdded", "DATETIME", existingColumnNames);
  422. AddColumn(connection, "TypedBaseItems", "Album", "Text", existingColumnNames);
  423. AddColumn(connection, "TypedBaseItems", "LUFS", "Float", existingColumnNames);
  424. AddColumn(connection, "TypedBaseItems", "NormalizationGain", "Float", existingColumnNames);
  425. AddColumn(connection, "TypedBaseItems", "IsVirtualItem", "BIT", existingColumnNames);
  426. AddColumn(connection, "TypedBaseItems", "SeriesName", "Text", existingColumnNames);
  427. AddColumn(connection, "TypedBaseItems", "UserDataKey", "Text", existingColumnNames);
  428. AddColumn(connection, "TypedBaseItems", "SeasonName", "Text", existingColumnNames);
  429. AddColumn(connection, "TypedBaseItems", "SeasonId", "GUID", existingColumnNames);
  430. AddColumn(connection, "TypedBaseItems", "SeriesId", "GUID", existingColumnNames);
  431. AddColumn(connection, "TypedBaseItems", "ExternalSeriesId", "Text", existingColumnNames);
  432. AddColumn(connection, "TypedBaseItems", "Tagline", "Text", existingColumnNames);
  433. AddColumn(connection, "TypedBaseItems", "ProviderIds", "Text", existingColumnNames);
  434. AddColumn(connection, "TypedBaseItems", "Images", "Text", existingColumnNames);
  435. AddColumn(connection, "TypedBaseItems", "ProductionLocations", "Text", existingColumnNames);
  436. AddColumn(connection, "TypedBaseItems", "ExtraIds", "Text", existingColumnNames);
  437. AddColumn(connection, "TypedBaseItems", "TotalBitrate", "INT", existingColumnNames);
  438. AddColumn(connection, "TypedBaseItems", "ExtraType", "Text", existingColumnNames);
  439. AddColumn(connection, "TypedBaseItems", "Artists", "Text", existingColumnNames);
  440. AddColumn(connection, "TypedBaseItems", "AlbumArtists", "Text", existingColumnNames);
  441. AddColumn(connection, "TypedBaseItems", "ExternalId", "Text", existingColumnNames);
  442. AddColumn(connection, "TypedBaseItems", "SeriesPresentationUniqueKey", "Text", existingColumnNames);
  443. AddColumn(connection, "TypedBaseItems", "ShowId", "Text", existingColumnNames);
  444. AddColumn(connection, "TypedBaseItems", "OwnerId", "Text", existingColumnNames);
  445. AddColumn(connection, "TypedBaseItems", "Width", "INT", existingColumnNames);
  446. AddColumn(connection, "TypedBaseItems", "Height", "INT", existingColumnNames);
  447. AddColumn(connection, "TypedBaseItems", "Size", "BIGINT", existingColumnNames);
  448. existingColumnNames = GetColumnNames(connection, "ItemValues");
  449. AddColumn(connection, "ItemValues", "CleanValue", "Text", existingColumnNames);
  450. existingColumnNames = GetColumnNames(connection, ChaptersTableName);
  451. AddColumn(connection, ChaptersTableName, "ImageDateModified", "DATETIME", existingColumnNames);
  452. existingColumnNames = GetColumnNames(connection, "MediaStreams");
  453. AddColumn(connection, "MediaStreams", "IsAvc", "BIT", existingColumnNames);
  454. AddColumn(connection, "MediaStreams", "TimeBase", "TEXT", existingColumnNames);
  455. AddColumn(connection, "MediaStreams", "CodecTimeBase", "TEXT", existingColumnNames);
  456. AddColumn(connection, "MediaStreams", "Title", "TEXT", existingColumnNames);
  457. AddColumn(connection, "MediaStreams", "NalLengthSize", "TEXT", existingColumnNames);
  458. AddColumn(connection, "MediaStreams", "Comment", "TEXT", existingColumnNames);
  459. AddColumn(connection, "MediaStreams", "CodecTag", "TEXT", existingColumnNames);
  460. AddColumn(connection, "MediaStreams", "PixelFormat", "TEXT", existingColumnNames);
  461. AddColumn(connection, "MediaStreams", "BitDepth", "INT", existingColumnNames);
  462. AddColumn(connection, "MediaStreams", "RefFrames", "INT", existingColumnNames);
  463. AddColumn(connection, "MediaStreams", "KeyFrames", "TEXT", existingColumnNames);
  464. AddColumn(connection, "MediaStreams", "IsAnamorphic", "BIT", existingColumnNames);
  465. AddColumn(connection, "MediaStreams", "ColorPrimaries", "TEXT", existingColumnNames);
  466. AddColumn(connection, "MediaStreams", "ColorSpace", "TEXT", existingColumnNames);
  467. AddColumn(connection, "MediaStreams", "ColorTransfer", "TEXT", existingColumnNames);
  468. AddColumn(connection, "MediaStreams", "DvVersionMajor", "INT", existingColumnNames);
  469. AddColumn(connection, "MediaStreams", "DvVersionMinor", "INT", existingColumnNames);
  470. AddColumn(connection, "MediaStreams", "DvProfile", "INT", existingColumnNames);
  471. AddColumn(connection, "MediaStreams", "DvLevel", "INT", existingColumnNames);
  472. AddColumn(connection, "MediaStreams", "RpuPresentFlag", "INT", existingColumnNames);
  473. AddColumn(connection, "MediaStreams", "ElPresentFlag", "INT", existingColumnNames);
  474. AddColumn(connection, "MediaStreams", "BlPresentFlag", "INT", existingColumnNames);
  475. AddColumn(connection, "MediaStreams", "DvBlSignalCompatibilityId", "INT", existingColumnNames);
  476. AddColumn(connection, "MediaStreams", "IsHearingImpaired", "BIT", existingColumnNames);
  477. AddColumn(connection, "MediaStreams", "Rotation", "INT", existingColumnNames);
  478. connection.Execute(string.Join(';', postQueries));
  479. transaction.Commit();
  480. }
  481. }
  482. /// <inheritdoc />
  483. public void SaveImages(BaseItem item)
  484. {
  485. ArgumentNullException.ThrowIfNull(item);
  486. CheckDisposed();
  487. var images = SerializeImages(item.ImageInfos);
  488. using var connection = GetConnection();
  489. using var transaction = connection.BeginTransaction();
  490. using var saveImagesStatement = PrepareStatement(connection, "Update TypedBaseItems set Images=@Images where guid=@Id");
  491. saveImagesStatement.TryBind("@Id", item.Id);
  492. saveImagesStatement.TryBind("@Images", images);
  493. saveImagesStatement.ExecuteNonQuery();
  494. transaction.Commit();
  495. }
  496. /// <summary>
  497. /// Saves the items.
  498. /// </summary>
  499. /// <param name="items">The items.</param>
  500. /// <param name="cancellationToken">The cancellation token.</param>
  501. /// <exception cref="ArgumentNullException">
  502. /// <paramref name="items"/> or <paramref name="cancellationToken"/> is <c>null</c>.
  503. /// </exception>
  504. public void SaveItems(IReadOnlyList<BaseItem> items, CancellationToken cancellationToken)
  505. {
  506. ArgumentNullException.ThrowIfNull(items);
  507. cancellationToken.ThrowIfCancellationRequested();
  508. CheckDisposed();
  509. var itemsLen = items.Count;
  510. var tuples = new ValueTuple<BaseItem, List<Guid>, BaseItem, string, List<string>>[itemsLen];
  511. for (int i = 0; i < itemsLen; i++)
  512. {
  513. var item = items[i];
  514. var ancestorIds = item.SupportsAncestors ?
  515. item.GetAncestorIds().Distinct().ToList() :
  516. null;
  517. var topParent = item.GetTopParent();
  518. var userdataKey = item.GetUserDataKeys().FirstOrDefault();
  519. var inheritedTags = item.GetInheritedTags();
  520. tuples[i] = (item, ancestorIds, topParent, userdataKey, inheritedTags);
  521. }
  522. using var connection = GetConnection();
  523. using var transaction = connection.BeginTransaction();
  524. SaveItemsInTransaction(connection, tuples);
  525. transaction.Commit();
  526. }
  527. private void SaveItemsInTransaction(ManagedConnection db, IEnumerable<(BaseItem Item, List<Guid> AncestorIds, BaseItem TopParent, string UserDataKey, List<string> InheritedTags)> tuples)
  528. {
  529. using (var saveItemStatement = PrepareStatement(db, SaveItemCommandText))
  530. using (var deleteAncestorsStatement = PrepareStatement(db, "delete from AncestorIds where ItemId=@ItemId"))
  531. {
  532. var requiresReset = false;
  533. foreach (var tuple in tuples)
  534. {
  535. if (requiresReset)
  536. {
  537. saveItemStatement.Parameters.Clear();
  538. deleteAncestorsStatement.Parameters.Clear();
  539. }
  540. var item = tuple.Item;
  541. var topParent = tuple.TopParent;
  542. var userDataKey = tuple.UserDataKey;
  543. SaveItem(item, topParent, userDataKey, saveItemStatement);
  544. var inheritedTags = tuple.InheritedTags;
  545. if (item.SupportsAncestors)
  546. {
  547. UpdateAncestors(item.Id, tuple.AncestorIds, db, deleteAncestorsStatement);
  548. }
  549. UpdateItemValues(item.Id, GetItemValuesToSave(item, inheritedTags), db);
  550. requiresReset = true;
  551. }
  552. }
  553. }
  554. private string GetPathToSave(string path)
  555. {
  556. if (path is null)
  557. {
  558. return null;
  559. }
  560. return _appHost.ReverseVirtualPath(path);
  561. }
  562. private string RestorePath(string path)
  563. {
  564. return _appHost.ExpandVirtualPath(path);
  565. }
  566. private void SaveItem(BaseItem item, BaseItem topParent, string userDataKey, SqliteCommand saveItemStatement)
  567. {
  568. Type type = item.GetType();
  569. saveItemStatement.TryBind("@guid", item.Id);
  570. saveItemStatement.TryBind("@type", type.FullName);
  571. if (TypeRequiresDeserialization(type))
  572. {
  573. saveItemStatement.TryBind("@data", JsonSerializer.SerializeToUtf8Bytes(item, type, _jsonOptions), true);
  574. }
  575. else
  576. {
  577. saveItemStatement.TryBindNull("@data");
  578. }
  579. saveItemStatement.TryBind("@Path", GetPathToSave(item.Path));
  580. if (item is IHasStartDate hasStartDate)
  581. {
  582. saveItemStatement.TryBind("@StartDate", hasStartDate.StartDate);
  583. }
  584. else
  585. {
  586. saveItemStatement.TryBindNull("@StartDate");
  587. }
  588. if (item.EndDate.HasValue)
  589. {
  590. saveItemStatement.TryBind("@EndDate", item.EndDate.Value);
  591. }
  592. else
  593. {
  594. saveItemStatement.TryBindNull("@EndDate");
  595. }
  596. saveItemStatement.TryBind("@ChannelId", item.ChannelId.IsEmpty() ? null : item.ChannelId.ToString("N", CultureInfo.InvariantCulture));
  597. if (item is IHasProgramAttributes hasProgramAttributes)
  598. {
  599. saveItemStatement.TryBind("@IsMovie", hasProgramAttributes.IsMovie);
  600. saveItemStatement.TryBind("@IsSeries", hasProgramAttributes.IsSeries);
  601. saveItemStatement.TryBind("@EpisodeTitle", hasProgramAttributes.EpisodeTitle);
  602. saveItemStatement.TryBind("@IsRepeat", hasProgramAttributes.IsRepeat);
  603. }
  604. else
  605. {
  606. saveItemStatement.TryBindNull("@IsMovie");
  607. saveItemStatement.TryBindNull("@IsSeries");
  608. saveItemStatement.TryBindNull("@EpisodeTitle");
  609. saveItemStatement.TryBindNull("@IsRepeat");
  610. }
  611. saveItemStatement.TryBind("@CommunityRating", item.CommunityRating);
  612. saveItemStatement.TryBind("@CustomRating", item.CustomRating);
  613. saveItemStatement.TryBind("@IndexNumber", item.IndexNumber);
  614. saveItemStatement.TryBind("@IsLocked", item.IsLocked);
  615. saveItemStatement.TryBind("@Name", item.Name);
  616. saveItemStatement.TryBind("@OfficialRating", item.OfficialRating);
  617. saveItemStatement.TryBind("@MediaType", item.MediaType.ToString());
  618. saveItemStatement.TryBind("@Overview", item.Overview);
  619. saveItemStatement.TryBind("@ParentIndexNumber", item.ParentIndexNumber);
  620. saveItemStatement.TryBind("@PremiereDate", item.PremiereDate);
  621. saveItemStatement.TryBind("@ProductionYear", item.ProductionYear);
  622. var parentId = item.ParentId;
  623. if (parentId.IsEmpty())
  624. {
  625. saveItemStatement.TryBindNull("@ParentId");
  626. }
  627. else
  628. {
  629. saveItemStatement.TryBind("@ParentId", parentId);
  630. }
  631. if (item.Genres.Length > 0)
  632. {
  633. saveItemStatement.TryBind("@Genres", string.Join('|', item.Genres));
  634. }
  635. else
  636. {
  637. saveItemStatement.TryBindNull("@Genres");
  638. }
  639. saveItemStatement.TryBind("@InheritedParentalRatingValue", item.InheritedParentalRatingValue);
  640. saveItemStatement.TryBind("@SortName", item.SortName);
  641. saveItemStatement.TryBind("@ForcedSortName", item.ForcedSortName);
  642. saveItemStatement.TryBind("@RunTimeTicks", item.RunTimeTicks);
  643. saveItemStatement.TryBind("@Size", item.Size);
  644. saveItemStatement.TryBind("@DateCreated", item.DateCreated);
  645. saveItemStatement.TryBind("@DateModified", item.DateModified);
  646. saveItemStatement.TryBind("@PreferredMetadataLanguage", item.PreferredMetadataLanguage);
  647. saveItemStatement.TryBind("@PreferredMetadataCountryCode", item.PreferredMetadataCountryCode);
  648. if (item.Width > 0)
  649. {
  650. saveItemStatement.TryBind("@Width", item.Width);
  651. }
  652. else
  653. {
  654. saveItemStatement.TryBindNull("@Width");
  655. }
  656. if (item.Height > 0)
  657. {
  658. saveItemStatement.TryBind("@Height", item.Height);
  659. }
  660. else
  661. {
  662. saveItemStatement.TryBindNull("@Height");
  663. }
  664. if (item.DateLastRefreshed != default(DateTime))
  665. {
  666. saveItemStatement.TryBind("@DateLastRefreshed", item.DateLastRefreshed);
  667. }
  668. else
  669. {
  670. saveItemStatement.TryBindNull("@DateLastRefreshed");
  671. }
  672. if (item.DateLastSaved != default(DateTime))
  673. {
  674. saveItemStatement.TryBind("@DateLastSaved", item.DateLastSaved);
  675. }
  676. else
  677. {
  678. saveItemStatement.TryBindNull("@DateLastSaved");
  679. }
  680. saveItemStatement.TryBind("@IsInMixedFolder", item.IsInMixedFolder);
  681. if (item.LockedFields.Length > 0)
  682. {
  683. saveItemStatement.TryBind("@LockedFields", string.Join('|', item.LockedFields));
  684. }
  685. else
  686. {
  687. saveItemStatement.TryBindNull("@LockedFields");
  688. }
  689. if (item.Studios.Length > 0)
  690. {
  691. saveItemStatement.TryBind("@Studios", string.Join('|', item.Studios));
  692. }
  693. else
  694. {
  695. saveItemStatement.TryBindNull("@Studios");
  696. }
  697. if (item.Audio.HasValue)
  698. {
  699. saveItemStatement.TryBind("@Audio", item.Audio.Value.ToString());
  700. }
  701. else
  702. {
  703. saveItemStatement.TryBindNull("@Audio");
  704. }
  705. if (item is LiveTvChannel liveTvChannel)
  706. {
  707. saveItemStatement.TryBind("@ExternalServiceId", liveTvChannel.ServiceName);
  708. }
  709. else
  710. {
  711. saveItemStatement.TryBindNull("@ExternalServiceId");
  712. }
  713. if (item.Tags.Length > 0)
  714. {
  715. saveItemStatement.TryBind("@Tags", string.Join('|', item.Tags));
  716. }
  717. else
  718. {
  719. saveItemStatement.TryBindNull("@Tags");
  720. }
  721. saveItemStatement.TryBind("@IsFolder", item.IsFolder);
  722. saveItemStatement.TryBind("@UnratedType", item.GetBlockUnratedType().ToString());
  723. if (topParent is null)
  724. {
  725. saveItemStatement.TryBindNull("@TopParentId");
  726. }
  727. else
  728. {
  729. saveItemStatement.TryBind("@TopParentId", topParent.Id.ToString("N", CultureInfo.InvariantCulture));
  730. }
  731. if (item is Trailer trailer && trailer.TrailerTypes.Length > 0)
  732. {
  733. saveItemStatement.TryBind("@TrailerTypes", string.Join('|', trailer.TrailerTypes));
  734. }
  735. else
  736. {
  737. saveItemStatement.TryBindNull("@TrailerTypes");
  738. }
  739. saveItemStatement.TryBind("@CriticRating", item.CriticRating);
  740. if (string.IsNullOrWhiteSpace(item.Name))
  741. {
  742. saveItemStatement.TryBindNull("@CleanName");
  743. }
  744. else
  745. {
  746. saveItemStatement.TryBind("@CleanName", GetCleanValue(item.Name));
  747. }
  748. saveItemStatement.TryBind("@PresentationUniqueKey", item.PresentationUniqueKey);
  749. saveItemStatement.TryBind("@OriginalTitle", item.OriginalTitle);
  750. if (item is Video video)
  751. {
  752. saveItemStatement.TryBind("@PrimaryVersionId", video.PrimaryVersionId);
  753. }
  754. else
  755. {
  756. saveItemStatement.TryBindNull("@PrimaryVersionId");
  757. }
  758. if (item is Folder folder && folder.DateLastMediaAdded.HasValue)
  759. {
  760. saveItemStatement.TryBind("@DateLastMediaAdded", folder.DateLastMediaAdded.Value);
  761. }
  762. else
  763. {
  764. saveItemStatement.TryBindNull("@DateLastMediaAdded");
  765. }
  766. saveItemStatement.TryBind("@Album", item.Album);
  767. saveItemStatement.TryBind("@LUFS", item.LUFS);
  768. saveItemStatement.TryBind("@NormalizationGain", item.NormalizationGain);
  769. saveItemStatement.TryBind("@IsVirtualItem", item.IsVirtualItem);
  770. if (item is IHasSeries hasSeriesName)
  771. {
  772. saveItemStatement.TryBind("@SeriesName", hasSeriesName.SeriesName);
  773. }
  774. else
  775. {
  776. saveItemStatement.TryBindNull("@SeriesName");
  777. }
  778. if (string.IsNullOrWhiteSpace(userDataKey))
  779. {
  780. saveItemStatement.TryBindNull("@UserDataKey");
  781. }
  782. else
  783. {
  784. saveItemStatement.TryBind("@UserDataKey", userDataKey);
  785. }
  786. if (item is Episode episode)
  787. {
  788. saveItemStatement.TryBind("@SeasonName", episode.SeasonName);
  789. var nullableSeasonId = episode.SeasonId.IsEmpty() ? (Guid?)null : episode.SeasonId;
  790. saveItemStatement.TryBind("@SeasonId", nullableSeasonId);
  791. }
  792. else
  793. {
  794. saveItemStatement.TryBindNull("@SeasonName");
  795. saveItemStatement.TryBindNull("@SeasonId");
  796. }
  797. if (item is IHasSeries hasSeries)
  798. {
  799. var nullableSeriesId = hasSeries.SeriesId.IsEmpty() ? (Guid?)null : hasSeries.SeriesId;
  800. saveItemStatement.TryBind("@SeriesId", nullableSeriesId);
  801. saveItemStatement.TryBind("@SeriesPresentationUniqueKey", hasSeries.SeriesPresentationUniqueKey);
  802. }
  803. else
  804. {
  805. saveItemStatement.TryBindNull("@SeriesId");
  806. saveItemStatement.TryBindNull("@SeriesPresentationUniqueKey");
  807. }
  808. saveItemStatement.TryBind("@ExternalSeriesId", item.ExternalSeriesId);
  809. saveItemStatement.TryBind("@Tagline", item.Tagline);
  810. saveItemStatement.TryBind("@ProviderIds", SerializeProviderIds(item.ProviderIds));
  811. saveItemStatement.TryBind("@Images", SerializeImages(item.ImageInfos));
  812. if (item.ProductionLocations.Length > 0)
  813. {
  814. saveItemStatement.TryBind("@ProductionLocations", string.Join('|', item.ProductionLocations));
  815. }
  816. else
  817. {
  818. saveItemStatement.TryBindNull("@ProductionLocations");
  819. }
  820. if (item.ExtraIds.Length > 0)
  821. {
  822. saveItemStatement.TryBind("@ExtraIds", string.Join('|', item.ExtraIds));
  823. }
  824. else
  825. {
  826. saveItemStatement.TryBindNull("@ExtraIds");
  827. }
  828. saveItemStatement.TryBind("@TotalBitrate", item.TotalBitrate);
  829. if (item.ExtraType.HasValue)
  830. {
  831. saveItemStatement.TryBind("@ExtraType", item.ExtraType.Value.ToString());
  832. }
  833. else
  834. {
  835. saveItemStatement.TryBindNull("@ExtraType");
  836. }
  837. string artists = null;
  838. if (item is IHasArtist hasArtists && hasArtists.Artists.Count > 0)
  839. {
  840. artists = string.Join('|', hasArtists.Artists);
  841. }
  842. saveItemStatement.TryBind("@Artists", artists);
  843. string albumArtists = null;
  844. if (item is IHasAlbumArtist hasAlbumArtists
  845. && hasAlbumArtists.AlbumArtists.Count > 0)
  846. {
  847. albumArtists = string.Join('|', hasAlbumArtists.AlbumArtists);
  848. }
  849. saveItemStatement.TryBind("@AlbumArtists", albumArtists);
  850. saveItemStatement.TryBind("@ExternalId", item.ExternalId);
  851. if (item is LiveTvProgram program)
  852. {
  853. saveItemStatement.TryBind("@ShowId", program.ShowId);
  854. }
  855. else
  856. {
  857. saveItemStatement.TryBindNull("@ShowId");
  858. }
  859. Guid ownerId = item.OwnerId;
  860. if (ownerId.IsEmpty())
  861. {
  862. saveItemStatement.TryBindNull("@OwnerId");
  863. }
  864. else
  865. {
  866. saveItemStatement.TryBind("@OwnerId", ownerId);
  867. }
  868. saveItemStatement.ExecuteNonQuery();
  869. }
  870. internal static string SerializeProviderIds(Dictionary<string, string> providerIds)
  871. {
  872. StringBuilder str = new StringBuilder();
  873. foreach (var i in providerIds)
  874. {
  875. // Ideally we shouldn't need this IsNullOrWhiteSpace check,
  876. // but we're seeing some cases of bad data slip through
  877. if (string.IsNullOrWhiteSpace(i.Value))
  878. {
  879. continue;
  880. }
  881. str.Append(i.Key)
  882. .Append('=')
  883. .Append(i.Value)
  884. .Append('|');
  885. }
  886. if (str.Length == 0)
  887. {
  888. return null;
  889. }
  890. str.Length -= 1; // Remove last |
  891. return str.ToString();
  892. }
  893. internal static void DeserializeProviderIds(string value, IHasProviderIds item)
  894. {
  895. if (string.IsNullOrWhiteSpace(value))
  896. {
  897. return;
  898. }
  899. foreach (var part in value.SpanSplit('|'))
  900. {
  901. var providerDelimiterIndex = part.IndexOf('=');
  902. // Don't let empty values through
  903. if (providerDelimiterIndex != -1 && part.Length != providerDelimiterIndex + 1)
  904. {
  905. item.SetProviderId(part[..providerDelimiterIndex].ToString(), part[(providerDelimiterIndex + 1)..].ToString());
  906. }
  907. }
  908. }
  909. internal string SerializeImages(ItemImageInfo[] images)
  910. {
  911. if (images.Length == 0)
  912. {
  913. return null;
  914. }
  915. StringBuilder str = new StringBuilder();
  916. foreach (var i in images)
  917. {
  918. if (string.IsNullOrWhiteSpace(i.Path))
  919. {
  920. continue;
  921. }
  922. AppendItemImageInfo(str, i);
  923. str.Append('|');
  924. }
  925. str.Length -= 1; // Remove last |
  926. return str.ToString();
  927. }
  928. internal ItemImageInfo[] DeserializeImages(string value)
  929. {
  930. if (string.IsNullOrWhiteSpace(value))
  931. {
  932. return Array.Empty<ItemImageInfo>();
  933. }
  934. // TODO The following is an ugly performance optimization, but it's extremely unlikely that the data in the database would be malformed
  935. var valueSpan = value.AsSpan();
  936. var count = valueSpan.Count('|') + 1;
  937. var position = 0;
  938. var result = new ItemImageInfo[count];
  939. foreach (var part in valueSpan.Split('|'))
  940. {
  941. var image = ItemImageInfoFromValueString(part);
  942. if (image is not null)
  943. {
  944. result[position++] = image;
  945. }
  946. }
  947. if (position == count)
  948. {
  949. return result;
  950. }
  951. if (position == 0)
  952. {
  953. return Array.Empty<ItemImageInfo>();
  954. }
  955. // Extremely unlikely, but somehow one or more of the image strings were malformed. Cut the array.
  956. return result[..position];
  957. }
  958. private void AppendItemImageInfo(StringBuilder bldr, ItemImageInfo image)
  959. {
  960. const char Delimiter = '*';
  961. var path = image.Path ?? string.Empty;
  962. bldr.Append(GetPathToSave(path))
  963. .Append(Delimiter)
  964. .Append(image.DateModified.Ticks)
  965. .Append(Delimiter)
  966. .Append(image.Type)
  967. .Append(Delimiter)
  968. .Append(image.Width)
  969. .Append(Delimiter)
  970. .Append(image.Height);
  971. var hash = image.BlurHash;
  972. if (!string.IsNullOrEmpty(hash))
  973. {
  974. bldr.Append(Delimiter)
  975. // Replace delimiters with other characters.
  976. // This can be removed when we migrate to a proper DB.
  977. .Append(hash.Replace(Delimiter, '/').Replace('|', '\\'));
  978. }
  979. }
  980. internal ItemImageInfo ItemImageInfoFromValueString(ReadOnlySpan<char> value)
  981. {
  982. const char Delimiter = '*';
  983. var nextSegment = value.IndexOf(Delimiter);
  984. if (nextSegment == -1)
  985. {
  986. return null;
  987. }
  988. ReadOnlySpan<char> path = value[..nextSegment];
  989. value = value[(nextSegment + 1)..];
  990. nextSegment = value.IndexOf(Delimiter);
  991. if (nextSegment == -1)
  992. {
  993. return null;
  994. }
  995. ReadOnlySpan<char> dateModified = value[..nextSegment];
  996. value = value[(nextSegment + 1)..];
  997. nextSegment = value.IndexOf(Delimiter);
  998. if (nextSegment == -1)
  999. {
  1000. nextSegment = value.Length;
  1001. }
  1002. ReadOnlySpan<char> imageType = value[..nextSegment];
  1003. var image = new ItemImageInfo
  1004. {
  1005. Path = RestorePath(path.ToString())
  1006. };
  1007. if (long.TryParse(dateModified, CultureInfo.InvariantCulture, out var ticks)
  1008. && ticks >= DateTime.MinValue.Ticks
  1009. && ticks <= DateTime.MaxValue.Ticks)
  1010. {
  1011. image.DateModified = new DateTime(ticks, DateTimeKind.Utc);
  1012. }
  1013. else
  1014. {
  1015. return null;
  1016. }
  1017. if (Enum.TryParse(imageType, true, out ImageType type))
  1018. {
  1019. image.Type = type;
  1020. }
  1021. else
  1022. {
  1023. return null;
  1024. }
  1025. // Optional parameters: width*height*blurhash
  1026. if (nextSegment + 1 < value.Length - 1)
  1027. {
  1028. value = value[(nextSegment + 1)..];
  1029. nextSegment = value.IndexOf(Delimiter);
  1030. if (nextSegment == -1 || nextSegment == value.Length)
  1031. {
  1032. return image;
  1033. }
  1034. ReadOnlySpan<char> widthSpan = value[..nextSegment];
  1035. value = value[(nextSegment + 1)..];
  1036. nextSegment = value.IndexOf(Delimiter);
  1037. if (nextSegment == -1)
  1038. {
  1039. nextSegment = value.Length;
  1040. }
  1041. ReadOnlySpan<char> heightSpan = value[..nextSegment];
  1042. if (int.TryParse(widthSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var width)
  1043. && int.TryParse(heightSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var height))
  1044. {
  1045. image.Width = width;
  1046. image.Height = height;
  1047. }
  1048. if (nextSegment < value.Length - 1)
  1049. {
  1050. value = value[(nextSegment + 1)..];
  1051. var length = value.Length;
  1052. Span<char> blurHashSpan = stackalloc char[length];
  1053. for (int i = 0; i < length; i++)
  1054. {
  1055. var c = value[i];
  1056. blurHashSpan[i] = c switch
  1057. {
  1058. '/' => Delimiter,
  1059. '\\' => '|',
  1060. _ => c
  1061. };
  1062. }
  1063. image.BlurHash = new string(blurHashSpan);
  1064. }
  1065. }
  1066. return image;
  1067. }
  1068. /// <summary>
  1069. /// Internal retrieve from items or users table.
  1070. /// </summary>
  1071. /// <param name="id">The id.</param>
  1072. /// <returns>BaseItem.</returns>
  1073. /// <exception cref="ArgumentNullException"><paramref name="id"/> is <c>null</c>.</exception>
  1074. /// <exception cref="ArgumentException"><paramr name="id"/> is <seealso cref="Guid.Empty"/>.</exception>
  1075. public BaseItem RetrieveItem(Guid id)
  1076. {
  1077. if (id.IsEmpty())
  1078. {
  1079. throw new ArgumentException("Guid can't be empty", nameof(id));
  1080. }
  1081. CheckDisposed();
  1082. using (var connection = GetConnection(true))
  1083. using (var statement = PrepareStatement(connection, _retrieveItemColumnsSelectQuery))
  1084. {
  1085. statement.TryBind("@guid", id);
  1086. foreach (var row in statement.ExecuteQuery())
  1087. {
  1088. return GetItem(row, new InternalItemsQuery());
  1089. }
  1090. }
  1091. return null;
  1092. }
  1093. private bool TypeRequiresDeserialization(Type type)
  1094. {
  1095. if (_config.Configuration.SkipDeserializationForBasicTypes)
  1096. {
  1097. if (type == typeof(Channel)
  1098. || type == typeof(UserRootFolder))
  1099. {
  1100. return false;
  1101. }
  1102. }
  1103. return type != typeof(Season)
  1104. && type != typeof(MusicArtist)
  1105. && type != typeof(Person)
  1106. && type != typeof(MusicGenre)
  1107. && type != typeof(Genre)
  1108. && type != typeof(Studio)
  1109. && type != typeof(PlaylistsFolder)
  1110. && type != typeof(PhotoAlbum)
  1111. && type != typeof(Year)
  1112. && type != typeof(Book)
  1113. && type != typeof(LiveTvProgram)
  1114. && type != typeof(AudioBook)
  1115. && type != typeof(MusicAlbum);
  1116. }
  1117. private BaseItem GetItem(SqliteDataReader reader, InternalItemsQuery query)
  1118. {
  1119. return GetItem(reader, query, HasProgramAttributes(query), HasEpisodeAttributes(query), HasServiceName(query), HasStartDate(query), HasTrailerTypes(query), HasArtistFields(query), HasSeriesFields(query), false);
  1120. }
  1121. private BaseItem GetItem(SqliteDataReader reader, InternalItemsQuery query, bool enableProgramAttributes, bool hasEpisodeAttributes, bool hasServiceName, bool queryHasStartDate, bool hasTrailerTypes, bool hasArtistFields, bool hasSeriesFields, bool skipDeserialization)
  1122. {
  1123. var typeString = reader.GetString(0);
  1124. var type = _typeMapper.GetType(typeString);
  1125. if (type is null)
  1126. {
  1127. return null;
  1128. }
  1129. BaseItem item = null;
  1130. if (TypeRequiresDeserialization(type) && !skipDeserialization)
  1131. {
  1132. try
  1133. {
  1134. item = JsonSerializer.Deserialize(reader.GetStream(1), type, _jsonOptions) as BaseItem;
  1135. }
  1136. catch (JsonException ex)
  1137. {
  1138. Logger.LogError(ex, "Error deserializing item with JSON: {Data}", reader.GetString(1));
  1139. }
  1140. }
  1141. if (item is null)
  1142. {
  1143. try
  1144. {
  1145. item = Activator.CreateInstance(type) as BaseItem;
  1146. }
  1147. catch
  1148. {
  1149. }
  1150. }
  1151. if (item is null)
  1152. {
  1153. return null;
  1154. }
  1155. var index = 2;
  1156. if (queryHasStartDate)
  1157. {
  1158. if (item is IHasStartDate hasStartDate && reader.TryReadDateTime(index, out var startDate))
  1159. {
  1160. hasStartDate.StartDate = startDate;
  1161. }
  1162. index++;
  1163. }
  1164. if (reader.TryReadDateTime(index++, out var endDate))
  1165. {
  1166. item.EndDate = endDate;
  1167. }
  1168. if (reader.TryGetGuid(index, out var guid))
  1169. {
  1170. item.ChannelId = guid;
  1171. }
  1172. index++;
  1173. if (enableProgramAttributes)
  1174. {
  1175. if (item is IHasProgramAttributes hasProgramAttributes)
  1176. {
  1177. if (reader.TryGetBoolean(index++, out var isMovie))
  1178. {
  1179. hasProgramAttributes.IsMovie = isMovie;
  1180. }
  1181. if (reader.TryGetBoolean(index++, out var isSeries))
  1182. {
  1183. hasProgramAttributes.IsSeries = isSeries;
  1184. }
  1185. if (reader.TryGetString(index++, out var episodeTitle))
  1186. {
  1187. hasProgramAttributes.EpisodeTitle = episodeTitle;
  1188. }
  1189. if (reader.TryGetBoolean(index++, out var isRepeat))
  1190. {
  1191. hasProgramAttributes.IsRepeat = isRepeat;
  1192. }
  1193. }
  1194. else
  1195. {
  1196. index += 4;
  1197. }
  1198. }
  1199. if (reader.TryGetSingle(index++, out var communityRating))
  1200. {
  1201. item.CommunityRating = communityRating;
  1202. }
  1203. if (HasField(query, ItemFields.CustomRating))
  1204. {
  1205. if (reader.TryGetString(index++, out var customRating))
  1206. {
  1207. item.CustomRating = customRating;
  1208. }
  1209. }
  1210. if (reader.TryGetInt32(index++, out var indexNumber))
  1211. {
  1212. item.IndexNumber = indexNumber;
  1213. }
  1214. if (HasField(query, ItemFields.Settings))
  1215. {
  1216. if (reader.TryGetBoolean(index++, out var isLocked))
  1217. {
  1218. item.IsLocked = isLocked;
  1219. }
  1220. if (reader.TryGetString(index++, out var preferredMetadataLanguage))
  1221. {
  1222. item.PreferredMetadataLanguage = preferredMetadataLanguage;
  1223. }
  1224. if (reader.TryGetString(index++, out var preferredMetadataCountryCode))
  1225. {
  1226. item.PreferredMetadataCountryCode = preferredMetadataCountryCode;
  1227. }
  1228. }
  1229. if (HasField(query, ItemFields.Width))
  1230. {
  1231. if (reader.TryGetInt32(index++, out var width))
  1232. {
  1233. item.Width = width;
  1234. }
  1235. }
  1236. if (HasField(query, ItemFields.Height))
  1237. {
  1238. if (reader.TryGetInt32(index++, out var height))
  1239. {
  1240. item.Height = height;
  1241. }
  1242. }
  1243. if (HasField(query, ItemFields.DateLastRefreshed))
  1244. {
  1245. if (reader.TryReadDateTime(index++, out var dateLastRefreshed))
  1246. {
  1247. item.DateLastRefreshed = dateLastRefreshed;
  1248. }
  1249. }
  1250. if (reader.TryGetString(index++, out var name))
  1251. {
  1252. item.Name = name;
  1253. }
  1254. if (reader.TryGetString(index++, out var restorePath))
  1255. {
  1256. item.Path = RestorePath(restorePath);
  1257. }
  1258. if (reader.TryReadDateTime(index++, out var premiereDate))
  1259. {
  1260. item.PremiereDate = premiereDate;
  1261. }
  1262. if (HasField(query, ItemFields.Overview))
  1263. {
  1264. if (reader.TryGetString(index++, out var overview))
  1265. {
  1266. item.Overview = overview;
  1267. }
  1268. }
  1269. if (reader.TryGetInt32(index++, out var parentIndexNumber))
  1270. {
  1271. item.ParentIndexNumber = parentIndexNumber;
  1272. }
  1273. if (reader.TryGetInt32(index++, out var productionYear))
  1274. {
  1275. item.ProductionYear = productionYear;
  1276. }
  1277. if (reader.TryGetString(index++, out var officialRating))
  1278. {
  1279. item.OfficialRating = officialRating;
  1280. }
  1281. if (HasField(query, ItemFields.SortName))
  1282. {
  1283. if (reader.TryGetString(index++, out var forcedSortName))
  1284. {
  1285. item.ForcedSortName = forcedSortName;
  1286. }
  1287. }
  1288. if (reader.TryGetInt64(index++, out var runTimeTicks))
  1289. {
  1290. item.RunTimeTicks = runTimeTicks;
  1291. }
  1292. if (reader.TryGetInt64(index++, out var size))
  1293. {
  1294. item.Size = size;
  1295. }
  1296. if (HasField(query, ItemFields.DateCreated))
  1297. {
  1298. if (reader.TryReadDateTime(index++, out var dateCreated))
  1299. {
  1300. item.DateCreated = dateCreated;
  1301. }
  1302. }
  1303. if (reader.TryReadDateTime(index++, out var dateModified))
  1304. {
  1305. item.DateModified = dateModified;
  1306. }
  1307. item.Id = reader.GetGuid(index++);
  1308. if (HasField(query, ItemFields.Genres))
  1309. {
  1310. if (reader.TryGetString(index++, out var genres))
  1311. {
  1312. item.Genres = genres.Split('|', StringSplitOptions.RemoveEmptyEntries);
  1313. }
  1314. }
  1315. if (reader.TryGetGuid(index++, out var parentId))
  1316. {
  1317. item.ParentId = parentId;
  1318. }
  1319. if (reader.TryGetString(index++, out var audioString))
  1320. {
  1321. if (Enum.TryParse(audioString, true, out ProgramAudio audio))
  1322. {
  1323. item.Audio = audio;
  1324. }
  1325. }
  1326. // TODO: Even if not needed by apps, the server needs it internally
  1327. // But get this excluded from contexts where it is not needed
  1328. if (hasServiceName)
  1329. {
  1330. if (item is LiveTvChannel liveTvChannel)
  1331. {
  1332. if (reader.TryGetString(index, out var serviceName))
  1333. {
  1334. liveTvChannel.ServiceName = serviceName;
  1335. }
  1336. }
  1337. index++;
  1338. }
  1339. if (reader.TryGetBoolean(index++, out var isInMixedFolder))
  1340. {
  1341. item.IsInMixedFolder = isInMixedFolder;
  1342. }
  1343. if (HasField(query, ItemFields.DateLastSaved))
  1344. {
  1345. if (reader.TryReadDateTime(index++, out var dateLastSaved))
  1346. {
  1347. item.DateLastSaved = dateLastSaved;
  1348. }
  1349. }
  1350. if (HasField(query, ItemFields.Settings))
  1351. {
  1352. if (reader.TryGetString(index++, out var lockedFields))
  1353. {
  1354. List<MetadataField> fields = null;
  1355. foreach (var i in lockedFields.AsSpan().Split('|'))
  1356. {
  1357. if (Enum.TryParse(i, true, out MetadataField parsedValue))
  1358. {
  1359. (fields ??= new List<MetadataField>()).Add(parsedValue);
  1360. }
  1361. }
  1362. item.LockedFields = fields?.ToArray() ?? Array.Empty<MetadataField>();
  1363. }
  1364. }
  1365. if (HasField(query, ItemFields.Studios))
  1366. {
  1367. if (reader.TryGetString(index++, out var studios))
  1368. {
  1369. item.Studios = studios.Split('|', StringSplitOptions.RemoveEmptyEntries);
  1370. }
  1371. }
  1372. if (HasField(query, ItemFields.Tags))
  1373. {
  1374. if (reader.TryGetString(index++, out var tags))
  1375. {
  1376. item.Tags = tags.Split('|', StringSplitOptions.RemoveEmptyEntries);
  1377. }
  1378. }
  1379. if (hasTrailerTypes)
  1380. {
  1381. if (item is Trailer trailer)
  1382. {
  1383. if (reader.TryGetString(index, out var trailerTypes))
  1384. {
  1385. List<TrailerType> types = null;
  1386. foreach (var i in trailerTypes.AsSpan().Split('|'))
  1387. {
  1388. if (Enum.TryParse(i, true, out TrailerType parsedValue))
  1389. {
  1390. (types ??= new List<TrailerType>()).Add(parsedValue);
  1391. }
  1392. }
  1393. trailer.TrailerTypes = types?.ToArray() ?? Array.Empty<TrailerType>();
  1394. }
  1395. }
  1396. index++;
  1397. }
  1398. if (HasField(query, ItemFields.OriginalTitle))
  1399. {
  1400. if (reader.TryGetString(index++, out var originalTitle))
  1401. {
  1402. item.OriginalTitle = originalTitle;
  1403. }
  1404. }
  1405. if (item is Video video)
  1406. {
  1407. if (reader.TryGetString(index, out var primaryVersionId))
  1408. {
  1409. video.PrimaryVersionId = primaryVersionId;
  1410. }
  1411. }
  1412. index++;
  1413. if (HasField(query, ItemFields.DateLastMediaAdded))
  1414. {
  1415. if (item is Folder folder && reader.TryReadDateTime(index, out var dateLastMediaAdded))
  1416. {
  1417. folder.DateLastMediaAdded = dateLastMediaAdded;
  1418. }
  1419. index++;
  1420. }
  1421. if (reader.TryGetString(index++, out var album))
  1422. {
  1423. item.Album = album;
  1424. }
  1425. if (reader.TryGetSingle(index++, out var lUFS))
  1426. {
  1427. item.LUFS = lUFS;
  1428. }
  1429. if (reader.TryGetSingle(index++, out var normalizationGain))
  1430. {
  1431. item.NormalizationGain = normalizationGain;
  1432. }
  1433. if (reader.TryGetSingle(index++, out var criticRating))
  1434. {
  1435. item.CriticRating = criticRating;
  1436. }
  1437. if (reader.TryGetBoolean(index++, out var isVirtualItem))
  1438. {
  1439. item.IsVirtualItem = isVirtualItem;
  1440. }
  1441. if (item is IHasSeries hasSeriesName)
  1442. {
  1443. if (reader.TryGetString(index, out var seriesName))
  1444. {
  1445. hasSeriesName.SeriesName = seriesName;
  1446. }
  1447. }
  1448. index++;
  1449. if (hasEpisodeAttributes)
  1450. {
  1451. if (item is Episode episode)
  1452. {
  1453. if (reader.TryGetString(index, out var seasonName))
  1454. {
  1455. episode.SeasonName = seasonName;
  1456. }
  1457. index++;
  1458. if (reader.TryGetGuid(index, out var seasonId))
  1459. {
  1460. episode.SeasonId = seasonId;
  1461. }
  1462. }
  1463. else
  1464. {
  1465. index++;
  1466. }
  1467. index++;
  1468. }
  1469. var hasSeries = item as IHasSeries;
  1470. if (hasSeriesFields)
  1471. {
  1472. if (hasSeries is not null)
  1473. {
  1474. if (reader.TryGetGuid(index, out var seriesId))
  1475. {
  1476. hasSeries.SeriesId = seriesId;
  1477. }
  1478. }
  1479. index++;
  1480. }
  1481. if (HasField(query, ItemFields.PresentationUniqueKey))
  1482. {
  1483. if (reader.TryGetString(index++, out var presentationUniqueKey))
  1484. {
  1485. item.PresentationUniqueKey = presentationUniqueKey;
  1486. }
  1487. }
  1488. if (HasField(query, ItemFields.InheritedParentalRatingValue))
  1489. {
  1490. if (reader.TryGetInt32(index++, out var parentalRating))
  1491. {
  1492. item.InheritedParentalRatingValue = parentalRating;
  1493. }
  1494. }
  1495. if (HasField(query, ItemFields.ExternalSeriesId))
  1496. {
  1497. if (reader.TryGetString(index++, out var externalSeriesId))
  1498. {
  1499. item.ExternalSeriesId = externalSeriesId;
  1500. }
  1501. }
  1502. if (HasField(query, ItemFields.Taglines))
  1503. {
  1504. if (reader.TryGetString(index++, out var tagLine))
  1505. {
  1506. item.Tagline = tagLine;
  1507. }
  1508. }
  1509. if (item.ProviderIds.Count == 0 && reader.TryGetString(index, out var providerIds))
  1510. {
  1511. DeserializeProviderIds(providerIds, item);
  1512. }
  1513. index++;
  1514. if (query.DtoOptions.EnableImages)
  1515. {
  1516. if (item.ImageInfos.Length == 0 && reader.TryGetString(index, out var imageInfos))
  1517. {
  1518. item.ImageInfos = DeserializeImages(imageInfos);
  1519. }
  1520. index++;
  1521. }
  1522. if (HasField(query, ItemFields.ProductionLocations))
  1523. {
  1524. if (reader.TryGetString(index++, out var productionLocations))
  1525. {
  1526. item.ProductionLocations = productionLocations.Split('|', StringSplitOptions.RemoveEmptyEntries);
  1527. }
  1528. }
  1529. if (HasField(query, ItemFields.ExtraIds))
  1530. {
  1531. if (reader.TryGetString(index++, out var extraIds))
  1532. {
  1533. item.ExtraIds = SplitToGuids(extraIds);
  1534. }
  1535. }
  1536. if (reader.TryGetInt32(index++, out var totalBitrate))
  1537. {
  1538. item.TotalBitrate = totalBitrate;
  1539. }
  1540. if (reader.TryGetString(index++, out var extraTypeString))
  1541. {
  1542. if (Enum.TryParse(extraTypeString, true, out ExtraType extraType))
  1543. {
  1544. item.ExtraType = extraType;
  1545. }
  1546. }
  1547. if (hasArtistFields)
  1548. {
  1549. if (item is IHasArtist hasArtists && reader.TryGetString(index, out var artists))
  1550. {
  1551. hasArtists.Artists = artists.Split('|', StringSplitOptions.RemoveEmptyEntries);
  1552. }
  1553. index++;
  1554. if (item is IHasAlbumArtist hasAlbumArtists && reader.TryGetString(index, out var albumArtists))
  1555. {
  1556. hasAlbumArtists.AlbumArtists = albumArtists.Split('|', StringSplitOptions.RemoveEmptyEntries);
  1557. }
  1558. index++;
  1559. }
  1560. if (reader.TryGetString(index++, out var externalId))
  1561. {
  1562. item.ExternalId = externalId;
  1563. }
  1564. if (HasField(query, ItemFields.SeriesPresentationUniqueKey))
  1565. {
  1566. if (hasSeries is not null)
  1567. {
  1568. if (reader.TryGetString(index, out var seriesPresentationUniqueKey))
  1569. {
  1570. hasSeries.SeriesPresentationUniqueKey = seriesPresentationUniqueKey;
  1571. }
  1572. }
  1573. index++;
  1574. }
  1575. if (enableProgramAttributes)
  1576. {
  1577. if (item is LiveTvProgram program && reader.TryGetString(index, out var showId))
  1578. {
  1579. program.ShowId = showId;
  1580. }
  1581. index++;
  1582. }
  1583. if (reader.TryGetGuid(index, out var ownerId))
  1584. {
  1585. item.OwnerId = ownerId;
  1586. }
  1587. return item;
  1588. }
  1589. private static Guid[] SplitToGuids(string value)
  1590. {
  1591. var ids = value.Split('|');
  1592. var result = new Guid[ids.Length];
  1593. for (var i = 0; i < result.Length; i++)
  1594. {
  1595. result[i] = new Guid(ids[i]);
  1596. }
  1597. return result;
  1598. }
  1599. /// <inheritdoc />
  1600. public List<ChapterInfo> GetChapters(BaseItem item)
  1601. {
  1602. CheckDisposed();
  1603. var chapters = new List<ChapterInfo>();
  1604. using (var connection = GetConnection(true))
  1605. using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
  1606. {
  1607. statement.TryBind("@ItemId", item.Id);
  1608. foreach (var row in statement.ExecuteQuery())
  1609. {
  1610. chapters.Add(GetChapter(row, item));
  1611. }
  1612. }
  1613. return chapters;
  1614. }
  1615. /// <inheritdoc />
  1616. public ChapterInfo GetChapter(BaseItem item, int index)
  1617. {
  1618. CheckDisposed();
  1619. using (var connection = GetConnection(true))
  1620. using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex"))
  1621. {
  1622. statement.TryBind("@ItemId", item.Id);
  1623. statement.TryBind("@ChapterIndex", index);
  1624. foreach (var row in statement.ExecuteQuery())
  1625. {
  1626. return GetChapter(row, item);
  1627. }
  1628. }
  1629. return null;
  1630. }
  1631. /// <summary>
  1632. /// Gets the chapter.
  1633. /// </summary>
  1634. /// <param name="reader">The reader.</param>
  1635. /// <param name="item">The item.</param>
  1636. /// <returns>ChapterInfo.</returns>
  1637. private ChapterInfo GetChapter(SqliteDataReader reader, BaseItem item)
  1638. {
  1639. var chapter = new ChapterInfo
  1640. {
  1641. StartPositionTicks = reader.GetInt64(0)
  1642. };
  1643. if (reader.TryGetString(1, out var chapterName))
  1644. {
  1645. chapter.Name = chapterName;
  1646. }
  1647. if (reader.TryGetString(2, out var imagePath))
  1648. {
  1649. chapter.ImagePath = imagePath;
  1650. chapter.ImageTag = _imageProcessor.GetImageCacheTag(item, chapter);
  1651. }
  1652. if (reader.TryReadDateTime(3, out var imageDateModified))
  1653. {
  1654. chapter.ImageDateModified = imageDateModified;
  1655. }
  1656. return chapter;
  1657. }
  1658. /// <summary>
  1659. /// Saves the chapters.
  1660. /// </summary>
  1661. /// <param name="id">The item id.</param>
  1662. /// <param name="chapters">The chapters.</param>
  1663. public void SaveChapters(Guid id, IReadOnlyList<ChapterInfo> chapters)
  1664. {
  1665. CheckDisposed();
  1666. if (id.IsEmpty())
  1667. {
  1668. throw new ArgumentNullException(nameof(id));
  1669. }
  1670. ArgumentNullException.ThrowIfNull(chapters);
  1671. using var connection = GetConnection();
  1672. using var transaction = connection.BeginTransaction();
  1673. // First delete chapters
  1674. using var command = connection.PrepareStatement($"delete from {ChaptersTableName} where ItemId=@ItemId");
  1675. command.TryBind("@ItemId", id);
  1676. command.ExecuteNonQuery();
  1677. InsertChapters(id, chapters, connection);
  1678. transaction.Commit();
  1679. }
  1680. private void InsertChapters(Guid idBlob, IReadOnlyList<ChapterInfo> chapters, ManagedConnection db)
  1681. {
  1682. var startIndex = 0;
  1683. var limit = 100;
  1684. var chapterIndex = 0;
  1685. const string StartInsertText = "insert into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values ";
  1686. var insertText = new StringBuilder(StartInsertText, 256);
  1687. while (startIndex < chapters.Count)
  1688. {
  1689. var endIndex = Math.Min(chapters.Count, startIndex + limit);
  1690. for (var i = startIndex; i < endIndex; i++)
  1691. {
  1692. insertText.AppendFormat(CultureInfo.InvariantCulture, "(@ItemId, @ChapterIndex{0}, @StartPositionTicks{0}, @Name{0}, @ImagePath{0}, @ImageDateModified{0}),", i.ToString(CultureInfo.InvariantCulture));
  1693. }
  1694. insertText.Length -= 1; // Remove trailing comma
  1695. using (var statement = PrepareStatement(db, insertText.ToString()))
  1696. {
  1697. statement.TryBind("@ItemId", idBlob);
  1698. for (var i = startIndex; i < endIndex; i++)
  1699. {
  1700. var index = i.ToString(CultureInfo.InvariantCulture);
  1701. var chapter = chapters[i];
  1702. statement.TryBind("@ChapterIndex" + index, chapterIndex);
  1703. statement.TryBind("@StartPositionTicks" + index, chapter.StartPositionTicks);
  1704. statement.TryBind("@Name" + index, chapter.Name);
  1705. statement.TryBind("@ImagePath" + index, chapter.ImagePath);
  1706. statement.TryBind("@ImageDateModified" + index, chapter.ImageDateModified);
  1707. chapterIndex++;
  1708. }
  1709. statement.ExecuteNonQuery();
  1710. }
  1711. startIndex += limit;
  1712. insertText.Length = StartInsertText.Length;
  1713. }
  1714. }
  1715. private static bool EnableJoinUserData(InternalItemsQuery query)
  1716. {
  1717. if (query.User is null)
  1718. {
  1719. return false;
  1720. }
  1721. var sortingFields = new HashSet<ItemSortBy>(query.OrderBy.Select(i => i.OrderBy));
  1722. return sortingFields.Contains(ItemSortBy.IsFavoriteOrLiked)
  1723. || sortingFields.Contains(ItemSortBy.IsPlayed)
  1724. || sortingFields.Contains(ItemSortBy.IsUnplayed)
  1725. || sortingFields.Contains(ItemSortBy.PlayCount)
  1726. || sortingFields.Contains(ItemSortBy.DatePlayed)
  1727. || sortingFields.Contains(ItemSortBy.SeriesDatePlayed)
  1728. || query.IsFavoriteOrLiked.HasValue
  1729. || query.IsFavorite.HasValue
  1730. || query.IsResumable.HasValue
  1731. || query.IsPlayed.HasValue
  1732. || query.IsLiked.HasValue;
  1733. }
  1734. private bool HasField(InternalItemsQuery query, ItemFields name)
  1735. {
  1736. switch (name)
  1737. {
  1738. case ItemFields.Tags:
  1739. return query.DtoOptions.ContainsField(name) || HasProgramAttributes(query);
  1740. case ItemFields.CustomRating:
  1741. case ItemFields.ProductionLocations:
  1742. case ItemFields.Settings:
  1743. case ItemFields.OriginalTitle:
  1744. case ItemFields.Taglines:
  1745. case ItemFields.SortName:
  1746. case ItemFields.Studios:
  1747. case ItemFields.ExtraIds:
  1748. case ItemFields.DateCreated:
  1749. case ItemFields.Overview:
  1750. case ItemFields.Genres:
  1751. case ItemFields.DateLastMediaAdded:
  1752. case ItemFields.PresentationUniqueKey:
  1753. case ItemFields.InheritedParentalRatingValue:
  1754. case ItemFields.ExternalSeriesId:
  1755. case ItemFields.SeriesPresentationUniqueKey:
  1756. case ItemFields.DateLastRefreshed:
  1757. case ItemFields.DateLastSaved:
  1758. return query.DtoOptions.ContainsField(name);
  1759. case ItemFields.ServiceName:
  1760. return HasServiceName(query);
  1761. default:
  1762. return true;
  1763. }
  1764. }
  1765. private bool HasProgramAttributes(InternalItemsQuery query)
  1766. {
  1767. if (query.ParentType is not null && _programExcludeParentTypes.Contains(query.ParentType.Value))
  1768. {
  1769. return false;
  1770. }
  1771. if (query.IncludeItemTypes.Length == 0)
  1772. {
  1773. return true;
  1774. }
  1775. return query.IncludeItemTypes.Any(x => _programTypes.Contains(x));
  1776. }
  1777. private bool HasServiceName(InternalItemsQuery query)
  1778. {
  1779. if (query.ParentType is not null && _programExcludeParentTypes.Contains(query.ParentType.Value))
  1780. {
  1781. return false;
  1782. }
  1783. if (query.IncludeItemTypes.Length == 0)
  1784. {
  1785. return true;
  1786. }
  1787. return query.IncludeItemTypes.Any(x => _serviceTypes.Contains(x));
  1788. }
  1789. private bool HasStartDate(InternalItemsQuery query)
  1790. {
  1791. if (query.ParentType is not null && _programExcludeParentTypes.Contains(query.ParentType.Value))
  1792. {
  1793. return false;
  1794. }
  1795. if (query.IncludeItemTypes.Length == 0)
  1796. {
  1797. return true;
  1798. }
  1799. return query.IncludeItemTypes.Any(x => _startDateTypes.Contains(x));
  1800. }
  1801. private bool HasEpisodeAttributes(InternalItemsQuery query)
  1802. {
  1803. if (query.IncludeItemTypes.Length == 0)
  1804. {
  1805. return true;
  1806. }
  1807. return query.IncludeItemTypes.Contains(BaseItemKind.Episode);
  1808. }
  1809. private bool HasTrailerTypes(InternalItemsQuery query)
  1810. {
  1811. if (query.IncludeItemTypes.Length == 0)
  1812. {
  1813. return true;
  1814. }
  1815. return query.IncludeItemTypes.Contains(BaseItemKind.Trailer);
  1816. }
  1817. private bool HasArtistFields(InternalItemsQuery query)
  1818. {
  1819. if (query.ParentType is not null && _artistExcludeParentTypes.Contains(query.ParentType.Value))
  1820. {
  1821. return false;
  1822. }
  1823. if (query.IncludeItemTypes.Length == 0)
  1824. {
  1825. return true;
  1826. }
  1827. return query.IncludeItemTypes.Any(x => _artistsTypes.Contains(x));
  1828. }
  1829. private bool HasSeriesFields(InternalItemsQuery query)
  1830. {
  1831. if (query.ParentType == BaseItemKind.PhotoAlbum)
  1832. {
  1833. return false;
  1834. }
  1835. if (query.IncludeItemTypes.Length == 0)
  1836. {
  1837. return true;
  1838. }
  1839. return query.IncludeItemTypes.Any(x => _seriesTypes.Contains(x));
  1840. }
  1841. private void SetFinalColumnsToSelect(InternalItemsQuery query, List<string> columns)
  1842. {
  1843. foreach (var field in _allItemFields)
  1844. {
  1845. if (!HasField(query, field))
  1846. {
  1847. switch (field)
  1848. {
  1849. case ItemFields.Settings:
  1850. columns.Remove("IsLocked");
  1851. columns.Remove("PreferredMetadataCountryCode");
  1852. columns.Remove("PreferredMetadataLanguage");
  1853. columns.Remove("LockedFields");
  1854. break;
  1855. case ItemFields.ServiceName:
  1856. columns.Remove("ExternalServiceId");
  1857. break;
  1858. case ItemFields.SortName:
  1859. columns.Remove("ForcedSortName");
  1860. break;
  1861. case ItemFields.Taglines:
  1862. columns.Remove("Tagline");
  1863. break;
  1864. case ItemFields.Tags:
  1865. columns.Remove("Tags");
  1866. break;
  1867. case ItemFields.IsHD:
  1868. // do nothing
  1869. break;
  1870. default:
  1871. columns.Remove(field.ToString());
  1872. break;
  1873. }
  1874. }
  1875. }
  1876. if (!HasProgramAttributes(query))
  1877. {
  1878. columns.Remove("IsMovie");
  1879. columns.Remove("IsSeries");
  1880. columns.Remove("EpisodeTitle");
  1881. columns.Remove("IsRepeat");
  1882. columns.Remove("ShowId");
  1883. }
  1884. if (!HasEpisodeAttributes(query))
  1885. {
  1886. columns.Remove("SeasonName");
  1887. columns.Remove("SeasonId");
  1888. }
  1889. if (!HasStartDate(query))
  1890. {
  1891. columns.Remove("StartDate");
  1892. }
  1893. if (!HasTrailerTypes(query))
  1894. {
  1895. columns.Remove("TrailerTypes");
  1896. }
  1897. if (!HasArtistFields(query))
  1898. {
  1899. columns.Remove("AlbumArtists");
  1900. columns.Remove("Artists");
  1901. }
  1902. if (!HasSeriesFields(query))
  1903. {
  1904. columns.Remove("SeriesId");
  1905. }
  1906. if (!HasEpisodeAttributes(query))
  1907. {
  1908. columns.Remove("SeasonName");
  1909. columns.Remove("SeasonId");
  1910. }
  1911. if (!query.DtoOptions.EnableImages)
  1912. {
  1913. columns.Remove("Images");
  1914. }
  1915. if (EnableJoinUserData(query))
  1916. {
  1917. columns.Add("UserDatas.UserId");
  1918. columns.Add("UserDatas.lastPlayedDate");
  1919. columns.Add("UserDatas.playbackPositionTicks");
  1920. columns.Add("UserDatas.playcount");
  1921. columns.Add("UserDatas.isFavorite");
  1922. columns.Add("UserDatas.played");
  1923. columns.Add("UserDatas.rating");
  1924. }
  1925. if (query.SimilarTo is not null)
  1926. {
  1927. var item = query.SimilarTo;
  1928. var builder = new StringBuilder();
  1929. builder.Append('(');
  1930. if (item.InheritedParentalRatingValue == 0)
  1931. {
  1932. builder.Append("((InheritedParentalRatingValue=0) * 10)");
  1933. }
  1934. else
  1935. {
  1936. builder.Append(
  1937. @"(SELECT CASE WHEN COALESCE(InheritedParentalRatingValue, 0)=0
  1938. THEN 0
  1939. ELSE 10.0 / (1.0 + ABS(InheritedParentalRatingValue - @InheritedParentalRatingValue))
  1940. END)");
  1941. }
  1942. if (item.ProductionYear.HasValue)
  1943. {
  1944. builder.Append("+(Select Case When Abs(COALESCE(ProductionYear, 0) - @ItemProductionYear) < 10 Then 10 Else 0 End )");
  1945. builder.Append("+(Select Case When Abs(COALESCE(ProductionYear, 0) - @ItemProductionYear) < 5 Then 5 Else 0 End )");
  1946. }
  1947. // genres, tags, studios, person, year?
  1948. builder.Append("+ (Select count(1) * 10 from ItemValues where ItemId=Guid and CleanValue in (select CleanValue from ItemValues where ItemId=@SimilarItemId))");
  1949. builder.Append("+ (Select count(1) * 10 from People where ItemId=Guid and Name in (select Name from People where ItemId=@SimilarItemId))");
  1950. if (item is MusicArtist)
  1951. {
  1952. // Match albums where the artist is AlbumArtist against other albums.
  1953. // It is assumed that similar albums => similar artists.
  1954. builder.Append(
  1955. @"+ (WITH artistValues AS (
  1956. SELECT DISTINCT albumValues.CleanValue
  1957. FROM ItemValues albumValues
  1958. INNER JOIN ItemValues artistAlbums ON albumValues.ItemId = artistAlbums.ItemId
  1959. INNER JOIN TypedBaseItems artistItem ON artistAlbums.CleanValue = artistItem.CleanName AND artistAlbums.TYPE = 1 AND artistItem.Guid = @SimilarItemId
  1960. ), similarArtist AS (
  1961. SELECT albumValues.ItemId
  1962. FROM ItemValues albumValues
  1963. INNER JOIN ItemValues artistAlbums ON albumValues.ItemId = artistAlbums.ItemId
  1964. INNER JOIN TypedBaseItems artistItem ON artistAlbums.CleanValue = artistItem.CleanName AND artistAlbums.TYPE = 1 AND artistItem.Guid = A.Guid
  1965. ) SELECT COUNT(DISTINCT(CleanValue)) * 10 FROM ItemValues WHERE ItemId IN (SELECT ItemId FROM similarArtist) AND CleanValue IN (SELECT CleanValue FROM artistValues))");
  1966. }
  1967. builder.Append(") as SimilarityScore");
  1968. columns.Add(builder.ToString());
  1969. query.ExcludeItemIds = [.. query.ExcludeItemIds, item.Id, .. item.ExtraIds];
  1970. query.ExcludeProviderIds = item.ProviderIds;
  1971. }
  1972. if (!string.IsNullOrEmpty(query.SearchTerm))
  1973. {
  1974. var builder = new StringBuilder();
  1975. builder.Append('(');
  1976. builder.Append("((CleanName like @SearchTermStartsWith or (OriginalTitle not null and OriginalTitle like @SearchTermStartsWith)) * 10)");
  1977. builder.Append("+ ((CleanName = @SearchTermStartsWith COLLATE NOCASE or (OriginalTitle not null and OriginalTitle = @SearchTermStartsWith COLLATE NOCASE)) * 10)");
  1978. if (query.SearchTerm.Length > 1)
  1979. {
  1980. builder.Append("+ ((CleanName like @SearchTermContains or (OriginalTitle not null and OriginalTitle like @SearchTermContains)) * 10)");
  1981. }
  1982. builder.Append(") as SearchScore");
  1983. columns.Add(builder.ToString());
  1984. }
  1985. }
  1986. private void BindSearchParams(InternalItemsQuery query, SqliteCommand statement)
  1987. {
  1988. var searchTerm = query.SearchTerm;
  1989. if (string.IsNullOrEmpty(searchTerm))
  1990. {
  1991. return;
  1992. }
  1993. searchTerm = FixUnicodeChars(searchTerm);
  1994. searchTerm = GetCleanValue(searchTerm);
  1995. var commandText = statement.CommandText;
  1996. if (commandText.Contains("@SearchTermStartsWith", StringComparison.OrdinalIgnoreCase))
  1997. {
  1998. statement.TryBind("@SearchTermStartsWith", searchTerm + "%");
  1999. }
  2000. if (commandText.Contains("@SearchTermContains", StringComparison.OrdinalIgnoreCase))
  2001. {
  2002. statement.TryBind("@SearchTermContains", "%" + searchTerm + "%");
  2003. }
  2004. }
  2005. private void BindSimilarParams(InternalItemsQuery query, SqliteCommand statement)
  2006. {
  2007. var item = query.SimilarTo;
  2008. if (item is null)
  2009. {
  2010. return;
  2011. }
  2012. var commandText = statement.CommandText;
  2013. if (commandText.Contains("@ItemOfficialRating", StringComparison.OrdinalIgnoreCase))
  2014. {
  2015. statement.TryBind("@ItemOfficialRating", item.OfficialRating);
  2016. }
  2017. if (commandText.Contains("@ItemProductionYear", StringComparison.OrdinalIgnoreCase))
  2018. {
  2019. statement.TryBind("@ItemProductionYear", item.ProductionYear ?? 0);
  2020. }
  2021. if (commandText.Contains("@SimilarItemId", StringComparison.OrdinalIgnoreCase))
  2022. {
  2023. statement.TryBind("@SimilarItemId", item.Id);
  2024. }
  2025. if (commandText.Contains("@InheritedParentalRatingValue", StringComparison.OrdinalIgnoreCase))
  2026. {
  2027. statement.TryBind("@InheritedParentalRatingValue", item.InheritedParentalRatingValue);
  2028. }
  2029. }
  2030. private string GetJoinUserDataText(InternalItemsQuery query)
  2031. {
  2032. if (!EnableJoinUserData(query))
  2033. {
  2034. return string.Empty;
  2035. }
  2036. return " left join UserDatas on UserDataKey=UserDatas.Key And (UserId=@UserId)";
  2037. }
  2038. private string GetGroupBy(InternalItemsQuery query)
  2039. {
  2040. var enableGroupByPresentationUniqueKey = EnableGroupByPresentationUniqueKey(query);
  2041. if (enableGroupByPresentationUniqueKey && query.GroupBySeriesPresentationUniqueKey)
  2042. {
  2043. return " Group by PresentationUniqueKey, SeriesPresentationUniqueKey";
  2044. }
  2045. if (enableGroupByPresentationUniqueKey)
  2046. {
  2047. return " Group by PresentationUniqueKey";
  2048. }
  2049. if (query.GroupBySeriesPresentationUniqueKey)
  2050. {
  2051. return " Group by SeriesPresentationUniqueKey";
  2052. }
  2053. return string.Empty;
  2054. }
  2055. /// <inheritdoc />
  2056. public int GetCount(InternalItemsQuery query)
  2057. {
  2058. ArgumentNullException.ThrowIfNull(query);
  2059. CheckDisposed();
  2060. // Hack for right now since we currently don't support filtering out these duplicates within a query
  2061. if (query.Limit.HasValue && query.EnableGroupByMetadataKey)
  2062. {
  2063. query.Limit = query.Limit.Value + 4;
  2064. }
  2065. var columns = new List<string> { "count(distinct PresentationUniqueKey)" };
  2066. SetFinalColumnsToSelect(query, columns);
  2067. var commandTextBuilder = new StringBuilder("select ", 256)
  2068. .AppendJoin(',', columns)
  2069. .Append(FromText)
  2070. .Append(GetJoinUserDataText(query));
  2071. var whereClauses = GetWhereClauses(query, null);
  2072. if (whereClauses.Count != 0)
  2073. {
  2074. commandTextBuilder.Append(" where ")
  2075. .AppendJoin(" AND ", whereClauses);
  2076. }
  2077. var commandText = commandTextBuilder.ToString();
  2078. using (new QueryTimeLogger(Logger, commandText))
  2079. using (var connection = GetConnection(true))
  2080. using (var statement = PrepareStatement(connection, commandText))
  2081. {
  2082. if (EnableJoinUserData(query))
  2083. {
  2084. statement.TryBind("@UserId", query.User.InternalId);
  2085. }
  2086. BindSimilarParams(query, statement);
  2087. BindSearchParams(query, statement);
  2088. // Running this again will bind the params
  2089. GetWhereClauses(query, statement);
  2090. return statement.SelectScalarInt();
  2091. }
  2092. }
  2093. /// <inheritdoc />
  2094. public List<BaseItem> GetItemList(InternalItemsQuery query)
  2095. {
  2096. ArgumentNullException.ThrowIfNull(query);
  2097. CheckDisposed();
  2098. // Hack for right now since we currently don't support filtering out these duplicates within a query
  2099. if (query.Limit.HasValue && query.EnableGroupByMetadataKey)
  2100. {
  2101. query.Limit = query.Limit.Value + 4;
  2102. }
  2103. var columns = _retrieveItemColumns.ToList();
  2104. SetFinalColumnsToSelect(query, columns);
  2105. var commandTextBuilder = new StringBuilder("select ", 1024)
  2106. .AppendJoin(',', columns)
  2107. .Append(FromText)
  2108. .Append(GetJoinUserDataText(query));
  2109. var whereClauses = GetWhereClauses(query, null);
  2110. if (whereClauses.Count != 0)
  2111. {
  2112. commandTextBuilder.Append(" where ")
  2113. .AppendJoin(" AND ", whereClauses);
  2114. }
  2115. commandTextBuilder.Append(GetGroupBy(query))
  2116. .Append(GetOrderByText(query));
  2117. if (query.Limit.HasValue || query.StartIndex.HasValue)
  2118. {
  2119. var offset = query.StartIndex ?? 0;
  2120. if (query.Limit.HasValue || offset > 0)
  2121. {
  2122. commandTextBuilder.Append(" LIMIT ")
  2123. .Append(query.Limit ?? int.MaxValue);
  2124. }
  2125. if (offset > 0)
  2126. {
  2127. commandTextBuilder.Append(" OFFSET ")
  2128. .Append(offset);
  2129. }
  2130. }
  2131. var commandText = commandTextBuilder.ToString();
  2132. var items = new List<BaseItem>();
  2133. using (new QueryTimeLogger(Logger, commandText))
  2134. using (var connection = GetConnection(true))
  2135. using (var statement = PrepareStatement(connection, commandText))
  2136. {
  2137. if (EnableJoinUserData(query))
  2138. {
  2139. statement.TryBind("@UserId", query.User.InternalId);
  2140. }
  2141. BindSimilarParams(query, statement);
  2142. BindSearchParams(query, statement);
  2143. // Running this again will bind the params
  2144. GetWhereClauses(query, statement);
  2145. var hasEpisodeAttributes = HasEpisodeAttributes(query);
  2146. var hasServiceName = HasServiceName(query);
  2147. var hasProgramAttributes = HasProgramAttributes(query);
  2148. var hasStartDate = HasStartDate(query);
  2149. var hasTrailerTypes = HasTrailerTypes(query);
  2150. var hasArtistFields = HasArtistFields(query);
  2151. var hasSeriesFields = HasSeriesFields(query);
  2152. foreach (var row in statement.ExecuteQuery())
  2153. {
  2154. var item = GetItem(row, query, hasProgramAttributes, hasEpisodeAttributes, hasServiceName, hasStartDate, hasTrailerTypes, hasArtistFields, hasSeriesFields, query.SkipDeserialization);
  2155. if (item is not null)
  2156. {
  2157. items.Add(item);
  2158. }
  2159. }
  2160. }
  2161. // Hack for right now since we currently don't support filtering out these duplicates within a query
  2162. if (query.EnableGroupByMetadataKey)
  2163. {
  2164. var limit = query.Limit ?? int.MaxValue;
  2165. limit -= 4;
  2166. var newList = new List<BaseItem>();
  2167. foreach (var item in items)
  2168. {
  2169. AddItem(newList, item);
  2170. if (newList.Count >= limit)
  2171. {
  2172. break;
  2173. }
  2174. }
  2175. items = newList;
  2176. }
  2177. return items;
  2178. }
  2179. private string FixUnicodeChars(string buffer)
  2180. {
  2181. buffer = buffer.Replace('\u2013', '-'); // en dash
  2182. buffer = buffer.Replace('\u2014', '-'); // em dash
  2183. buffer = buffer.Replace('\u2015', '-'); // horizontal bar
  2184. buffer = buffer.Replace('\u2017', '_'); // double low line
  2185. buffer = buffer.Replace('\u2018', '\''); // left single quotation mark
  2186. buffer = buffer.Replace('\u2019', '\''); // right single quotation mark
  2187. buffer = buffer.Replace('\u201a', ','); // single low-9 quotation mark
  2188. buffer = buffer.Replace('\u201b', '\''); // single high-reversed-9 quotation mark
  2189. buffer = buffer.Replace('\u201c', '\"'); // left double quotation mark
  2190. buffer = buffer.Replace('\u201d', '\"'); // right double quotation mark
  2191. buffer = buffer.Replace('\u201e', '\"'); // double low-9 quotation mark
  2192. buffer = buffer.Replace("\u2026", "...", StringComparison.Ordinal); // horizontal ellipsis
  2193. buffer = buffer.Replace('\u2032', '\''); // prime
  2194. buffer = buffer.Replace('\u2033', '\"'); // double prime
  2195. buffer = buffer.Replace('\u0060', '\''); // grave accent
  2196. return buffer.Replace('\u00B4', '\''); // acute accent
  2197. }
  2198. private void AddItem(List<BaseItem> items, BaseItem newItem)
  2199. {
  2200. for (var i = 0; i < items.Count; i++)
  2201. {
  2202. var item = items[i];
  2203. foreach (var providerId in newItem.ProviderIds)
  2204. {
  2205. if (string.Equals(providerId.Key, nameof(MetadataProvider.TmdbCollection), StringComparison.Ordinal))
  2206. {
  2207. continue;
  2208. }
  2209. if (string.Equals(item.GetProviderId(providerId.Key), providerId.Value, StringComparison.Ordinal))
  2210. {
  2211. if (newItem.SourceType == SourceType.Library)
  2212. {
  2213. items[i] = newItem;
  2214. }
  2215. return;
  2216. }
  2217. }
  2218. }
  2219. items.Add(newItem);
  2220. }
  2221. /// <inheritdoc />
  2222. public QueryResult<BaseItem> GetItems(InternalItemsQuery query)
  2223. {
  2224. ArgumentNullException.ThrowIfNull(query);
  2225. CheckDisposed();
  2226. if (!query.EnableTotalRecordCount || (!query.Limit.HasValue && (query.StartIndex ?? 0) == 0))
  2227. {
  2228. var returnList = GetItemList(query);
  2229. return new QueryResult<BaseItem>(
  2230. query.StartIndex,
  2231. returnList.Count,
  2232. returnList);
  2233. }
  2234. // Hack for right now since we currently don't support filtering out these duplicates within a query
  2235. if (query.Limit.HasValue && query.EnableGroupByMetadataKey)
  2236. {
  2237. query.Limit = query.Limit.Value + 4;
  2238. }
  2239. var columns = _retrieveItemColumns.ToList();
  2240. SetFinalColumnsToSelect(query, columns);
  2241. var commandTextBuilder = new StringBuilder("select ", 512)
  2242. .AppendJoin(',', columns)
  2243. .Append(FromText)
  2244. .Append(GetJoinUserDataText(query));
  2245. var whereClauses = GetWhereClauses(query, null);
  2246. var whereText = whereClauses.Count == 0 ?
  2247. string.Empty :
  2248. string.Join(" AND ", whereClauses);
  2249. if (!string.IsNullOrEmpty(whereText))
  2250. {
  2251. commandTextBuilder.Append(" where ")
  2252. .Append(whereText);
  2253. }
  2254. commandTextBuilder.Append(GetGroupBy(query))
  2255. .Append(GetOrderByText(query));
  2256. if (query.Limit.HasValue || query.StartIndex.HasValue)
  2257. {
  2258. var offset = query.StartIndex ?? 0;
  2259. if (query.Limit.HasValue || offset > 0)
  2260. {
  2261. commandTextBuilder.Append(" LIMIT ")
  2262. .Append(query.Limit ?? int.MaxValue);
  2263. }
  2264. if (offset > 0)
  2265. {
  2266. commandTextBuilder.Append(" OFFSET ")
  2267. .Append(offset);
  2268. }
  2269. }
  2270. var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0;
  2271. var itemQuery = string.Empty;
  2272. var totalRecordCountQuery = string.Empty;
  2273. if (!isReturningZeroItems)
  2274. {
  2275. itemQuery = commandTextBuilder.ToString();
  2276. }
  2277. if (query.EnableTotalRecordCount)
  2278. {
  2279. commandTextBuilder.Clear();
  2280. commandTextBuilder.Append(" select ");
  2281. List<string> columnsToSelect;
  2282. if (EnableGroupByPresentationUniqueKey(query))
  2283. {
  2284. columnsToSelect = new List<string> { "count (distinct PresentationUniqueKey)" };
  2285. }
  2286. else if (query.GroupBySeriesPresentationUniqueKey)
  2287. {
  2288. columnsToSelect = new List<string> { "count (distinct SeriesPresentationUniqueKey)" };
  2289. }
  2290. else
  2291. {
  2292. columnsToSelect = new List<string> { "count (guid)" };
  2293. }
  2294. SetFinalColumnsToSelect(query, columnsToSelect);
  2295. commandTextBuilder.AppendJoin(',', columnsToSelect)
  2296. .Append(FromText)
  2297. .Append(GetJoinUserDataText(query));
  2298. if (!string.IsNullOrEmpty(whereText))
  2299. {
  2300. commandTextBuilder.Append(" where ")
  2301. .Append(whereText);
  2302. }
  2303. totalRecordCountQuery = commandTextBuilder.ToString();
  2304. }
  2305. var list = new List<BaseItem>();
  2306. var result = new QueryResult<BaseItem>();
  2307. using var connection = GetConnection(true);
  2308. using var transaction = connection.BeginTransaction();
  2309. if (!isReturningZeroItems)
  2310. {
  2311. using (new QueryTimeLogger(Logger, itemQuery, "GetItems.ItemQuery"))
  2312. using (var statement = PrepareStatement(connection, itemQuery))
  2313. {
  2314. if (EnableJoinUserData(query))
  2315. {
  2316. statement.TryBind("@UserId", query.User.InternalId);
  2317. }
  2318. BindSimilarParams(query, statement);
  2319. BindSearchParams(query, statement);
  2320. // Running this again will bind the params
  2321. GetWhereClauses(query, statement);
  2322. var hasEpisodeAttributes = HasEpisodeAttributes(query);
  2323. var hasServiceName = HasServiceName(query);
  2324. var hasProgramAttributes = HasProgramAttributes(query);
  2325. var hasStartDate = HasStartDate(query);
  2326. var hasTrailerTypes = HasTrailerTypes(query);
  2327. var hasArtistFields = HasArtistFields(query);
  2328. var hasSeriesFields = HasSeriesFields(query);
  2329. foreach (var row in statement.ExecuteQuery())
  2330. {
  2331. var item = GetItem(row, query, hasProgramAttributes, hasEpisodeAttributes, hasServiceName, hasStartDate, hasTrailerTypes, hasArtistFields, hasSeriesFields, false);
  2332. if (item is not null)
  2333. {
  2334. list.Add(item);
  2335. }
  2336. }
  2337. }
  2338. }
  2339. if (query.EnableTotalRecordCount)
  2340. {
  2341. using (new QueryTimeLogger(Logger, totalRecordCountQuery, "GetItems.TotalRecordCount"))
  2342. using (var statement = PrepareStatement(connection, totalRecordCountQuery))
  2343. {
  2344. if (EnableJoinUserData(query))
  2345. {
  2346. statement.TryBind("@UserId", query.User.InternalId);
  2347. }
  2348. BindSimilarParams(query, statement);
  2349. BindSearchParams(query, statement);
  2350. // Running this again will bind the params
  2351. GetWhereClauses(query, statement);
  2352. result.TotalRecordCount = statement.SelectScalarInt();
  2353. }
  2354. }
  2355. transaction.Commit();
  2356. result.StartIndex = query.StartIndex ?? 0;
  2357. result.Items = list;
  2358. return result;
  2359. }
  2360. private string GetOrderByText(InternalItemsQuery query)
  2361. {
  2362. var orderBy = query.OrderBy;
  2363. bool hasSimilar = query.SimilarTo is not null;
  2364. bool hasSearch = !string.IsNullOrEmpty(query.SearchTerm);
  2365. if (hasSimilar || hasSearch)
  2366. {
  2367. List<(ItemSortBy, SortOrder)> prepend = new List<(ItemSortBy, SortOrder)>(4);
  2368. if (hasSearch)
  2369. {
  2370. prepend.Add((ItemSortBy.SearchScore, SortOrder.Descending));
  2371. prepend.Add((ItemSortBy.SortName, SortOrder.Ascending));
  2372. }
  2373. if (hasSimilar)
  2374. {
  2375. prepend.Add((ItemSortBy.SimilarityScore, SortOrder.Descending));
  2376. prepend.Add((ItemSortBy.Random, SortOrder.Ascending));
  2377. }
  2378. orderBy = query.OrderBy = [.. prepend, .. orderBy];
  2379. }
  2380. else if (orderBy.Count == 0)
  2381. {
  2382. return string.Empty;
  2383. }
  2384. return " ORDER BY " + string.Join(',', orderBy.Select(i =>
  2385. {
  2386. var sortBy = MapOrderByField(i.OrderBy, query);
  2387. var sortOrder = i.SortOrder == SortOrder.Ascending ? "ASC" : "DESC";
  2388. return sortBy + " " + sortOrder;
  2389. }));
  2390. }
  2391. private string MapOrderByField(ItemSortBy sortBy, InternalItemsQuery query)
  2392. {
  2393. return sortBy switch
  2394. {
  2395. ItemSortBy.AirTime => "SortName", // TODO
  2396. ItemSortBy.Runtime => "RuntimeTicks",
  2397. ItemSortBy.Random => "RANDOM()",
  2398. ItemSortBy.DatePlayed when query.GroupBySeriesPresentationUniqueKey => "MAX(LastPlayedDate)",
  2399. ItemSortBy.DatePlayed => "LastPlayedDate",
  2400. ItemSortBy.PlayCount => "PlayCount",
  2401. ItemSortBy.IsFavoriteOrLiked => "(Select Case When IsFavorite is null Then 0 Else IsFavorite End )",
  2402. ItemSortBy.IsFolder => "IsFolder",
  2403. ItemSortBy.IsPlayed => "played",
  2404. ItemSortBy.IsUnplayed => "played",
  2405. ItemSortBy.DateLastContentAdded => "DateLastMediaAdded",
  2406. ItemSortBy.Artist => "(select CleanValue from ItemValues where ItemId=Guid and Type=0 LIMIT 1)",
  2407. ItemSortBy.AlbumArtist => "(select CleanValue from ItemValues where ItemId=Guid and Type=1 LIMIT 1)",
  2408. ItemSortBy.OfficialRating => "InheritedParentalRatingValue",
  2409. ItemSortBy.Studio => "(select CleanValue from ItemValues where ItemId=Guid and Type=3 LIMIT 1)",
  2410. ItemSortBy.SeriesDatePlayed => "(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where Played=1 and B.SeriesPresentationUniqueKey=A.PresentationUniqueKey)",
  2411. ItemSortBy.SeriesSortName => "SeriesName",
  2412. ItemSortBy.AiredEpisodeOrder => "AiredEpisodeOrder",
  2413. ItemSortBy.Album => "Album",
  2414. ItemSortBy.DateCreated => "DateCreated",
  2415. ItemSortBy.PremiereDate => "PremiereDate",
  2416. ItemSortBy.StartDate => "StartDate",
  2417. ItemSortBy.Name => "Name",
  2418. ItemSortBy.CommunityRating => "CommunityRating",
  2419. ItemSortBy.ProductionYear => "ProductionYear",
  2420. ItemSortBy.CriticRating => "CriticRating",
  2421. ItemSortBy.VideoBitRate => "VideoBitRate",
  2422. ItemSortBy.ParentIndexNumber => "ParentIndexNumber",
  2423. ItemSortBy.IndexNumber => "IndexNumber",
  2424. ItemSortBy.SimilarityScore => "SimilarityScore",
  2425. ItemSortBy.SearchScore => "SearchScore",
  2426. _ => "SortName"
  2427. };
  2428. }
  2429. /// <inheritdoc />
  2430. public List<Guid> GetItemIdsList(InternalItemsQuery query)
  2431. {
  2432. ArgumentNullException.ThrowIfNull(query);
  2433. CheckDisposed();
  2434. var columns = new List<string> { "guid" };
  2435. SetFinalColumnsToSelect(query, columns);
  2436. var commandTextBuilder = new StringBuilder("select ", 256)
  2437. .AppendJoin(',', columns)
  2438. .Append(FromText)
  2439. .Append(GetJoinUserDataText(query));
  2440. var whereClauses = GetWhereClauses(query, null);
  2441. if (whereClauses.Count != 0)
  2442. {
  2443. commandTextBuilder.Append(" where ")
  2444. .AppendJoin(" AND ", whereClauses);
  2445. }
  2446. commandTextBuilder.Append(GetGroupBy(query))
  2447. .Append(GetOrderByText(query));
  2448. if (query.Limit.HasValue || query.StartIndex.HasValue)
  2449. {
  2450. var offset = query.StartIndex ?? 0;
  2451. if (query.Limit.HasValue || offset > 0)
  2452. {
  2453. commandTextBuilder.Append(" LIMIT ")
  2454. .Append(query.Limit ?? int.MaxValue);
  2455. }
  2456. if (offset > 0)
  2457. {
  2458. commandTextBuilder.Append(" OFFSET ")
  2459. .Append(offset);
  2460. }
  2461. }
  2462. var commandText = commandTextBuilder.ToString();
  2463. var list = new List<Guid>();
  2464. using (new QueryTimeLogger(Logger, commandText))
  2465. using (var connection = GetConnection(true))
  2466. using (var statement = PrepareStatement(connection, commandText))
  2467. {
  2468. if (EnableJoinUserData(query))
  2469. {
  2470. statement.TryBind("@UserId", query.User.InternalId);
  2471. }
  2472. BindSimilarParams(query, statement);
  2473. BindSearchParams(query, statement);
  2474. // Running this again will bind the params
  2475. GetWhereClauses(query, statement);
  2476. foreach (var row in statement.ExecuteQuery())
  2477. {
  2478. list.Add(row.GetGuid(0));
  2479. }
  2480. }
  2481. return list;
  2482. }
  2483. private bool IsAlphaNumeric(string str)
  2484. {
  2485. if (string.IsNullOrWhiteSpace(str))
  2486. {
  2487. return false;
  2488. }
  2489. for (int i = 0; i < str.Length; i++)
  2490. {
  2491. if (!char.IsLetter(str[i]) && !char.IsNumber(str[i]))
  2492. {
  2493. return false;
  2494. }
  2495. }
  2496. return true;
  2497. }
  2498. private bool IsValidPersonType(string value)
  2499. {
  2500. return IsAlphaNumeric(value);
  2501. }
  2502. #nullable enable
  2503. private List<string> GetWhereClauses(InternalItemsQuery query, SqliteCommand? statement)
  2504. {
  2505. if (query.IsResumable ?? false)
  2506. {
  2507. query.IsVirtualItem = false;
  2508. }
  2509. var minWidth = query.MinWidth;
  2510. var maxWidth = query.MaxWidth;
  2511. if (query.IsHD.HasValue)
  2512. {
  2513. const int Threshold = 1200;
  2514. if (query.IsHD.Value)
  2515. {
  2516. minWidth = Threshold;
  2517. }
  2518. else
  2519. {
  2520. maxWidth = Threshold - 1;
  2521. }
  2522. }
  2523. if (query.Is4K.HasValue)
  2524. {
  2525. const int Threshold = 3800;
  2526. if (query.Is4K.Value)
  2527. {
  2528. minWidth = Threshold;
  2529. }
  2530. else
  2531. {
  2532. maxWidth = Threshold - 1;
  2533. }
  2534. }
  2535. var whereClauses = new List<string>();
  2536. if (minWidth.HasValue)
  2537. {
  2538. whereClauses.Add("Width>=@MinWidth");
  2539. statement?.TryBind("@MinWidth", minWidth);
  2540. }
  2541. if (query.MinHeight.HasValue)
  2542. {
  2543. whereClauses.Add("Height>=@MinHeight");
  2544. statement?.TryBind("@MinHeight", query.MinHeight);
  2545. }
  2546. if (maxWidth.HasValue)
  2547. {
  2548. whereClauses.Add("Width<=@MaxWidth");
  2549. statement?.TryBind("@MaxWidth", maxWidth);
  2550. }
  2551. if (query.MaxHeight.HasValue)
  2552. {
  2553. whereClauses.Add("Height<=@MaxHeight");
  2554. statement?.TryBind("@MaxHeight", query.MaxHeight);
  2555. }
  2556. if (query.IsLocked.HasValue)
  2557. {
  2558. whereClauses.Add("IsLocked=@IsLocked");
  2559. statement?.TryBind("@IsLocked", query.IsLocked);
  2560. }
  2561. var tags = query.Tags.ToList();
  2562. var excludeTags = query.ExcludeTags.ToList();
  2563. if (query.IsMovie == true)
  2564. {
  2565. if (query.IncludeItemTypes.Length == 0
  2566. || query.IncludeItemTypes.Contains(BaseItemKind.Movie)
  2567. || query.IncludeItemTypes.Contains(BaseItemKind.Trailer))
  2568. {
  2569. whereClauses.Add("(IsMovie is null OR IsMovie=@IsMovie)");
  2570. }
  2571. else
  2572. {
  2573. whereClauses.Add("IsMovie=@IsMovie");
  2574. }
  2575. statement?.TryBind("@IsMovie", true);
  2576. }
  2577. else if (query.IsMovie.HasValue)
  2578. {
  2579. whereClauses.Add("IsMovie=@IsMovie");
  2580. statement?.TryBind("@IsMovie", query.IsMovie);
  2581. }
  2582. if (query.IsSeries.HasValue)
  2583. {
  2584. whereClauses.Add("IsSeries=@IsSeries");
  2585. statement?.TryBind("@IsSeries", query.IsSeries);
  2586. }
  2587. if (query.IsSports.HasValue)
  2588. {
  2589. if (query.IsSports.Value)
  2590. {
  2591. tags.Add("Sports");
  2592. }
  2593. else
  2594. {
  2595. excludeTags.Add("Sports");
  2596. }
  2597. }
  2598. if (query.IsNews.HasValue)
  2599. {
  2600. if (query.IsNews.Value)
  2601. {
  2602. tags.Add("News");
  2603. }
  2604. else
  2605. {
  2606. excludeTags.Add("News");
  2607. }
  2608. }
  2609. if (query.IsKids.HasValue)
  2610. {
  2611. if (query.IsKids.Value)
  2612. {
  2613. tags.Add("Kids");
  2614. }
  2615. else
  2616. {
  2617. excludeTags.Add("Kids");
  2618. }
  2619. }
  2620. if (query.SimilarTo is not null && query.MinSimilarityScore > 0)
  2621. {
  2622. whereClauses.Add("SimilarityScore > " + (query.MinSimilarityScore - 1).ToString(CultureInfo.InvariantCulture));
  2623. }
  2624. if (!string.IsNullOrEmpty(query.SearchTerm))
  2625. {
  2626. whereClauses.Add("SearchScore > 0");
  2627. }
  2628. if (query.IsFolder.HasValue)
  2629. {
  2630. whereClauses.Add("IsFolder=@IsFolder");
  2631. statement?.TryBind("@IsFolder", query.IsFolder);
  2632. }
  2633. var includeTypes = query.IncludeItemTypes;
  2634. // Only specify excluded types if no included types are specified
  2635. if (query.IncludeItemTypes.Length == 0)
  2636. {
  2637. var excludeTypes = query.ExcludeItemTypes;
  2638. if (excludeTypes.Length == 1)
  2639. {
  2640. if (_baseItemKindNames.TryGetValue(excludeTypes[0], out var excludeTypeName))
  2641. {
  2642. whereClauses.Add("type<>@type");
  2643. statement?.TryBind("@type", excludeTypeName);
  2644. }
  2645. else
  2646. {
  2647. Logger.LogWarning("Undefined BaseItemKind to Type mapping: {BaseItemKind}", excludeTypes[0]);
  2648. }
  2649. }
  2650. else if (excludeTypes.Length > 1)
  2651. {
  2652. var whereBuilder = new StringBuilder("type not in (");
  2653. foreach (var excludeType in excludeTypes)
  2654. {
  2655. if (_baseItemKindNames.TryGetValue(excludeType, out var baseItemKindName))
  2656. {
  2657. whereBuilder
  2658. .Append('\'')
  2659. .Append(baseItemKindName)
  2660. .Append("',");
  2661. }
  2662. else
  2663. {
  2664. Logger.LogWarning("Undefined BaseItemKind to Type mapping: {BaseItemKind}", excludeType);
  2665. }
  2666. }
  2667. // Remove trailing comma.
  2668. whereBuilder.Length--;
  2669. whereBuilder.Append(')');
  2670. whereClauses.Add(whereBuilder.ToString());
  2671. }
  2672. }
  2673. else if (includeTypes.Length == 1)
  2674. {
  2675. if (_baseItemKindNames.TryGetValue(includeTypes[0], out var includeTypeName))
  2676. {
  2677. whereClauses.Add("type=@type");
  2678. statement?.TryBind("@type", includeTypeName);
  2679. }
  2680. else
  2681. {
  2682. Logger.LogWarning("Undefined BaseItemKind to Type mapping: {BaseItemKind}", includeTypes[0]);
  2683. }
  2684. }
  2685. else if (includeTypes.Length > 1)
  2686. {
  2687. var whereBuilder = new StringBuilder("type in (");
  2688. foreach (var includeType in includeTypes)
  2689. {
  2690. if (_baseItemKindNames.TryGetValue(includeType, out var baseItemKindName))
  2691. {
  2692. whereBuilder
  2693. .Append('\'')
  2694. .Append(baseItemKindName)
  2695. .Append("',");
  2696. }
  2697. else
  2698. {
  2699. Logger.LogWarning("Undefined BaseItemKind to Type mapping: {BaseItemKind}", includeType);
  2700. }
  2701. }
  2702. // Remove trailing comma.
  2703. whereBuilder.Length--;
  2704. whereBuilder.Append(')');
  2705. whereClauses.Add(whereBuilder.ToString());
  2706. }
  2707. if (query.ChannelIds.Count == 1)
  2708. {
  2709. whereClauses.Add("ChannelId=@ChannelId");
  2710. statement?.TryBind("@ChannelId", query.ChannelIds[0].ToString("N", CultureInfo.InvariantCulture));
  2711. }
  2712. else if (query.ChannelIds.Count > 1)
  2713. {
  2714. var inClause = string.Join(',', query.ChannelIds.Select(i => "'" + i.ToString("N", CultureInfo.InvariantCulture) + "'"));
  2715. whereClauses.Add($"ChannelId in ({inClause})");
  2716. }
  2717. if (!query.ParentId.IsEmpty())
  2718. {
  2719. whereClauses.Add("ParentId=@ParentId");
  2720. statement?.TryBind("@ParentId", query.ParentId);
  2721. }
  2722. if (!string.IsNullOrWhiteSpace(query.Path))
  2723. {
  2724. whereClauses.Add("Path=@Path");
  2725. statement?.TryBind("@Path", GetPathToSave(query.Path));
  2726. }
  2727. if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey))
  2728. {
  2729. whereClauses.Add("PresentationUniqueKey=@PresentationUniqueKey");
  2730. statement?.TryBind("@PresentationUniqueKey", query.PresentationUniqueKey);
  2731. }
  2732. if (query.MinCommunityRating.HasValue)
  2733. {
  2734. whereClauses.Add("CommunityRating>=@MinCommunityRating");
  2735. statement?.TryBind("@MinCommunityRating", query.MinCommunityRating.Value);
  2736. }
  2737. if (query.MinIndexNumber.HasValue)
  2738. {
  2739. whereClauses.Add("IndexNumber>=@MinIndexNumber");
  2740. statement?.TryBind("@MinIndexNumber", query.MinIndexNumber.Value);
  2741. }
  2742. if (query.MinParentAndIndexNumber.HasValue)
  2743. {
  2744. whereClauses.Add("((ParentIndexNumber=@MinParentAndIndexNumberParent and IndexNumber>=@MinParentAndIndexNumberIndex) or ParentIndexNumber>@MinParentAndIndexNumberParent)");
  2745. statement?.TryBind("@MinParentAndIndexNumberParent", query.MinParentAndIndexNumber.Value.ParentIndexNumber);
  2746. statement?.TryBind("@MinParentAndIndexNumberIndex", query.MinParentAndIndexNumber.Value.IndexNumber);
  2747. }
  2748. if (query.MinDateCreated.HasValue)
  2749. {
  2750. whereClauses.Add("DateCreated>=@MinDateCreated");
  2751. statement?.TryBind("@MinDateCreated", query.MinDateCreated.Value);
  2752. }
  2753. if (query.MinDateLastSaved.HasValue)
  2754. {
  2755. whereClauses.Add("(DateLastSaved not null and DateLastSaved>=@MinDateLastSavedForUser)");
  2756. statement?.TryBind("@MinDateLastSaved", query.MinDateLastSaved.Value);
  2757. }
  2758. if (query.MinDateLastSavedForUser.HasValue)
  2759. {
  2760. whereClauses.Add("(DateLastSaved not null and DateLastSaved>=@MinDateLastSavedForUser)");
  2761. statement?.TryBind("@MinDateLastSavedForUser", query.MinDateLastSavedForUser.Value);
  2762. }
  2763. if (query.IndexNumber.HasValue)
  2764. {
  2765. whereClauses.Add("IndexNumber=@IndexNumber");
  2766. statement?.TryBind("@IndexNumber", query.IndexNumber.Value);
  2767. }
  2768. if (query.ParentIndexNumber.HasValue)
  2769. {
  2770. whereClauses.Add("ParentIndexNumber=@ParentIndexNumber");
  2771. statement?.TryBind("@ParentIndexNumber", query.ParentIndexNumber.Value);
  2772. }
  2773. if (query.ParentIndexNumberNotEquals.HasValue)
  2774. {
  2775. whereClauses.Add("(ParentIndexNumber<>@ParentIndexNumberNotEquals or ParentIndexNumber is null)");
  2776. statement?.TryBind("@ParentIndexNumberNotEquals", query.ParentIndexNumberNotEquals.Value);
  2777. }
  2778. var minEndDate = query.MinEndDate;
  2779. var maxEndDate = query.MaxEndDate;
  2780. if (query.HasAired.HasValue)
  2781. {
  2782. if (query.HasAired.Value)
  2783. {
  2784. maxEndDate = DateTime.UtcNow;
  2785. }
  2786. else
  2787. {
  2788. minEndDate = DateTime.UtcNow;
  2789. }
  2790. }
  2791. if (minEndDate.HasValue)
  2792. {
  2793. whereClauses.Add("EndDate>=@MinEndDate");
  2794. statement?.TryBind("@MinEndDate", minEndDate.Value);
  2795. }
  2796. if (maxEndDate.HasValue)
  2797. {
  2798. whereClauses.Add("EndDate<=@MaxEndDate");
  2799. statement?.TryBind("@MaxEndDate", maxEndDate.Value);
  2800. }
  2801. if (query.MinStartDate.HasValue)
  2802. {
  2803. whereClauses.Add("StartDate>=@MinStartDate");
  2804. statement?.TryBind("@MinStartDate", query.MinStartDate.Value);
  2805. }
  2806. if (query.MaxStartDate.HasValue)
  2807. {
  2808. whereClauses.Add("StartDate<=@MaxStartDate");
  2809. statement?.TryBind("@MaxStartDate", query.MaxStartDate.Value);
  2810. }
  2811. if (query.MinPremiereDate.HasValue)
  2812. {
  2813. whereClauses.Add("PremiereDate>=@MinPremiereDate");
  2814. statement?.TryBind("@MinPremiereDate", query.MinPremiereDate.Value);
  2815. }
  2816. if (query.MaxPremiereDate.HasValue)
  2817. {
  2818. whereClauses.Add("PremiereDate<=@MaxPremiereDate");
  2819. statement?.TryBind("@MaxPremiereDate", query.MaxPremiereDate.Value);
  2820. }
  2821. StringBuilder clauseBuilder = new StringBuilder();
  2822. const string Or = " OR ";
  2823. var trailerTypes = query.TrailerTypes;
  2824. int trailerTypesLen = trailerTypes.Length;
  2825. if (trailerTypesLen > 0)
  2826. {
  2827. clauseBuilder.Append('(');
  2828. for (int i = 0; i < trailerTypesLen; i++)
  2829. {
  2830. var paramName = "@TrailerTypes" + i;
  2831. clauseBuilder.Append("TrailerTypes like ")
  2832. .Append(paramName)
  2833. .Append(Or);
  2834. statement?.TryBind(paramName, "%" + trailerTypes[i] + "%");
  2835. }
  2836. clauseBuilder.Length -= Or.Length;
  2837. clauseBuilder.Append(')');
  2838. whereClauses.Add(clauseBuilder.ToString());
  2839. clauseBuilder.Length = 0;
  2840. }
  2841. if (query.IsAiring.HasValue)
  2842. {
  2843. if (query.IsAiring.Value)
  2844. {
  2845. whereClauses.Add("StartDate<=@MaxStartDate");
  2846. statement?.TryBind("@MaxStartDate", DateTime.UtcNow);
  2847. whereClauses.Add("EndDate>=@MinEndDate");
  2848. statement?.TryBind("@MinEndDate", DateTime.UtcNow);
  2849. }
  2850. else
  2851. {
  2852. whereClauses.Add("(StartDate>@IsAiringDate OR EndDate < @IsAiringDate)");
  2853. statement?.TryBind("@IsAiringDate", DateTime.UtcNow);
  2854. }
  2855. }
  2856. int personIdsLen = query.PersonIds.Length;
  2857. if (personIdsLen > 0)
  2858. {
  2859. // TODO: Should this query with CleanName ?
  2860. clauseBuilder.Append('(');
  2861. Span<byte> idBytes = stackalloc byte[16];
  2862. for (int i = 0; i < personIdsLen; i++)
  2863. {
  2864. string paramName = "@PersonId" + i;
  2865. clauseBuilder.Append("(guid in (select itemid from People where Name = (select Name from TypedBaseItems where guid=")
  2866. .Append(paramName)
  2867. .Append("))) OR ");
  2868. statement?.TryBind(paramName, query.PersonIds[i]);
  2869. }
  2870. clauseBuilder.Length -= Or.Length;
  2871. clauseBuilder.Append(')');
  2872. whereClauses.Add(clauseBuilder.ToString());
  2873. clauseBuilder.Length = 0;
  2874. }
  2875. if (!string.IsNullOrWhiteSpace(query.Person))
  2876. {
  2877. whereClauses.Add("Guid in (select ItemId from People where Name=@PersonName)");
  2878. statement?.TryBind("@PersonName", query.Person);
  2879. }
  2880. if (!string.IsNullOrWhiteSpace(query.MinSortName))
  2881. {
  2882. whereClauses.Add("SortName>=@MinSortName");
  2883. statement?.TryBind("@MinSortName", query.MinSortName);
  2884. }
  2885. if (!string.IsNullOrWhiteSpace(query.ExternalSeriesId))
  2886. {
  2887. whereClauses.Add("ExternalSeriesId=@ExternalSeriesId");
  2888. statement?.TryBind("@ExternalSeriesId", query.ExternalSeriesId);
  2889. }
  2890. if (!string.IsNullOrWhiteSpace(query.ExternalId))
  2891. {
  2892. whereClauses.Add("ExternalId=@ExternalId");
  2893. statement?.TryBind("@ExternalId", query.ExternalId);
  2894. }
  2895. if (!string.IsNullOrWhiteSpace(query.Name))
  2896. {
  2897. whereClauses.Add("CleanName=@Name");
  2898. statement?.TryBind("@Name", GetCleanValue(query.Name));
  2899. }
  2900. // These are the same, for now
  2901. var nameContains = query.NameContains;
  2902. if (!string.IsNullOrWhiteSpace(nameContains))
  2903. {
  2904. whereClauses.Add("(CleanName like @NameContains or OriginalTitle like @NameContains)");
  2905. if (statement is not null)
  2906. {
  2907. nameContains = FixUnicodeChars(nameContains);
  2908. statement.TryBind("@NameContains", "%" + GetCleanValue(nameContains) + "%");
  2909. }
  2910. }
  2911. if (!string.IsNullOrWhiteSpace(query.NameStartsWith))
  2912. {
  2913. whereClauses.Add("SortName like @NameStartsWith");
  2914. statement?.TryBind("@NameStartsWith", query.NameStartsWith + "%");
  2915. }
  2916. if (!string.IsNullOrWhiteSpace(query.NameStartsWithOrGreater))
  2917. {
  2918. whereClauses.Add("SortName >= @NameStartsWithOrGreater");
  2919. // lowercase this because SortName is stored as lowercase
  2920. statement?.TryBind("@NameStartsWithOrGreater", query.NameStartsWithOrGreater.ToLowerInvariant());
  2921. }
  2922. if (!string.IsNullOrWhiteSpace(query.NameLessThan))
  2923. {
  2924. whereClauses.Add("SortName < @NameLessThan");
  2925. // lowercase this because SortName is stored as lowercase
  2926. statement?.TryBind("@NameLessThan", query.NameLessThan.ToLowerInvariant());
  2927. }
  2928. if (query.ImageTypes.Length > 0)
  2929. {
  2930. foreach (var requiredImage in query.ImageTypes)
  2931. {
  2932. whereClauses.Add("Images like '%" + requiredImage + "%'");
  2933. }
  2934. }
  2935. if (query.IsLiked.HasValue)
  2936. {
  2937. if (query.IsLiked.Value)
  2938. {
  2939. whereClauses.Add("rating>=@UserRating");
  2940. statement?.TryBind("@UserRating", UserItemData.MinLikeValue);
  2941. }
  2942. else
  2943. {
  2944. whereClauses.Add("(rating is null or rating<@UserRating)");
  2945. statement?.TryBind("@UserRating", UserItemData.MinLikeValue);
  2946. }
  2947. }
  2948. if (query.IsFavoriteOrLiked.HasValue)
  2949. {
  2950. if (query.IsFavoriteOrLiked.Value)
  2951. {
  2952. whereClauses.Add("IsFavorite=@IsFavoriteOrLiked");
  2953. }
  2954. else
  2955. {
  2956. whereClauses.Add("(IsFavorite is null or IsFavorite=@IsFavoriteOrLiked)");
  2957. }
  2958. statement?.TryBind("@IsFavoriteOrLiked", query.IsFavoriteOrLiked.Value);
  2959. }
  2960. if (query.IsFavorite.HasValue)
  2961. {
  2962. if (query.IsFavorite.Value)
  2963. {
  2964. whereClauses.Add("IsFavorite=@IsFavorite");
  2965. }
  2966. else
  2967. {
  2968. whereClauses.Add("(IsFavorite is null or IsFavorite=@IsFavorite)");
  2969. }
  2970. statement?.TryBind("@IsFavorite", query.IsFavorite.Value);
  2971. }
  2972. if (EnableJoinUserData(query))
  2973. {
  2974. if (query.IsPlayed.HasValue)
  2975. {
  2976. // We should probably figure this out for all folders, but for right now, this is the only place where we need it
  2977. if (query.IncludeItemTypes.Length == 1 && query.IncludeItemTypes[0] == BaseItemKind.Series)
  2978. {
  2979. if (query.IsPlayed.Value)
  2980. {
  2981. whereClauses.Add("PresentationUniqueKey not in (select S.SeriesPresentationUniqueKey from TypedBaseitems S left join UserDatas UD on S.UserDataKey=UD.Key And UD.UserId=@UserId where Coalesce(UD.Played, 0)=0 and S.IsFolder=0 and S.IsVirtualItem=0 and S.SeriesPresentationUniqueKey not null)");
  2982. }
  2983. else
  2984. {
  2985. whereClauses.Add("PresentationUniqueKey in (select S.SeriesPresentationUniqueKey from TypedBaseitems S left join UserDatas UD on S.UserDataKey=UD.Key And UD.UserId=@UserId where Coalesce(UD.Played, 0)=0 and S.IsFolder=0 and S.IsVirtualItem=0 and S.SeriesPresentationUniqueKey not null)");
  2986. }
  2987. }
  2988. else
  2989. {
  2990. if (query.IsPlayed.Value)
  2991. {
  2992. whereClauses.Add("(played=@IsPlayed)");
  2993. }
  2994. else
  2995. {
  2996. whereClauses.Add("(played is null or played=@IsPlayed)");
  2997. }
  2998. statement?.TryBind("@IsPlayed", query.IsPlayed.Value);
  2999. }
  3000. }
  3001. }
  3002. if (query.IsResumable.HasValue)
  3003. {
  3004. if (query.IsResumable.Value)
  3005. {
  3006. whereClauses.Add("playbackPositionTicks > 0");
  3007. }
  3008. else
  3009. {
  3010. whereClauses.Add("(playbackPositionTicks is null or playbackPositionTicks = 0)");
  3011. }
  3012. }
  3013. if (query.ArtistIds.Length > 0)
  3014. {
  3015. clauseBuilder.Append('(');
  3016. for (var i = 0; i < query.ArtistIds.Length; i++)
  3017. {
  3018. clauseBuilder.Append("(guid in (select itemid from ItemValues where CleanValue = (select CleanName from TypedBaseItems where guid=@ArtistIds")
  3019. .Append(i)
  3020. .Append(") and Type<=1)) OR ");
  3021. statement?.TryBind("@ArtistIds" + i, query.ArtistIds[i]);
  3022. }
  3023. clauseBuilder.Length -= Or.Length;
  3024. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3025. clauseBuilder.Length = 0;
  3026. }
  3027. if (query.AlbumArtistIds.Length > 0)
  3028. {
  3029. clauseBuilder.Append('(');
  3030. for (var i = 0; i < query.AlbumArtistIds.Length; i++)
  3031. {
  3032. clauseBuilder.Append("(guid in (select itemid from ItemValues where CleanValue = (select CleanName from TypedBaseItems where guid=@ArtistIds")
  3033. .Append(i)
  3034. .Append(") and Type=1)) OR ");
  3035. statement?.TryBind("@ArtistIds" + i, query.AlbumArtistIds[i]);
  3036. }
  3037. clauseBuilder.Length -= Or.Length;
  3038. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3039. clauseBuilder.Length = 0;
  3040. }
  3041. if (query.ContributingArtistIds.Length > 0)
  3042. {
  3043. clauseBuilder.Append('(');
  3044. for (var i = 0; i < query.ContributingArtistIds.Length; i++)
  3045. {
  3046. clauseBuilder.Append("((select CleanName from TypedBaseItems where guid=@ArtistIds")
  3047. .Append(i)
  3048. .Append(") in (select CleanValue from ItemValues where ItemId=Guid and Type=0) AND (select CleanName from TypedBaseItems where guid=@ArtistIds")
  3049. .Append(i)
  3050. .Append(") not in (select CleanValue from ItemValues where ItemId=Guid and Type=1)) OR ");
  3051. statement?.TryBind("@ArtistIds" + i, query.ContributingArtistIds[i]);
  3052. }
  3053. clauseBuilder.Length -= Or.Length;
  3054. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3055. clauseBuilder.Length = 0;
  3056. }
  3057. if (query.AlbumIds.Length > 0)
  3058. {
  3059. clauseBuilder.Append('(');
  3060. for (var i = 0; i < query.AlbumIds.Length; i++)
  3061. {
  3062. clauseBuilder.Append("Album in (select Name from typedbaseitems where guid=@AlbumIds")
  3063. .Append(i)
  3064. .Append(") OR ");
  3065. statement?.TryBind("@AlbumIds" + i, query.AlbumIds[i]);
  3066. }
  3067. clauseBuilder.Length -= Or.Length;
  3068. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3069. clauseBuilder.Length = 0;
  3070. }
  3071. if (query.ExcludeArtistIds.Length > 0)
  3072. {
  3073. clauseBuilder.Append('(');
  3074. for (var i = 0; i < query.ExcludeArtistIds.Length; i++)
  3075. {
  3076. clauseBuilder.Append("(guid not in (select itemid from ItemValues where CleanValue = (select CleanName from TypedBaseItems where guid=@ExcludeArtistId")
  3077. .Append(i)
  3078. .Append(") and Type<=1)) OR ");
  3079. statement?.TryBind("@ExcludeArtistId" + i, query.ExcludeArtistIds[i]);
  3080. }
  3081. clauseBuilder.Length -= Or.Length;
  3082. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3083. clauseBuilder.Length = 0;
  3084. }
  3085. if (query.GenreIds.Count > 0)
  3086. {
  3087. clauseBuilder.Append('(');
  3088. for (var i = 0; i < query.GenreIds.Count; i++)
  3089. {
  3090. clauseBuilder.Append("(guid in (select itemid from ItemValues where CleanValue = (select CleanName from TypedBaseItems where guid=@GenreId")
  3091. .Append(i)
  3092. .Append(") and Type=2)) OR ");
  3093. statement?.TryBind("@GenreId" + i, query.GenreIds[i]);
  3094. }
  3095. clauseBuilder.Length -= Or.Length;
  3096. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3097. clauseBuilder.Length = 0;
  3098. }
  3099. if (query.Genres.Count > 0)
  3100. {
  3101. clauseBuilder.Append('(');
  3102. for (var i = 0; i < query.Genres.Count; i++)
  3103. {
  3104. clauseBuilder.Append("@Genre")
  3105. .Append(i)
  3106. .Append(" in (select CleanValue from ItemValues where ItemId=Guid and Type=2) OR ");
  3107. statement?.TryBind("@Genre" + i, GetCleanValue(query.Genres[i]));
  3108. }
  3109. clauseBuilder.Length -= Or.Length;
  3110. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3111. clauseBuilder.Length = 0;
  3112. }
  3113. if (tags.Count > 0)
  3114. {
  3115. clauseBuilder.Append('(');
  3116. for (var i = 0; i < tags.Count; i++)
  3117. {
  3118. clauseBuilder.Append("@Tag")
  3119. .Append(i)
  3120. .Append(" in (select CleanValue from ItemValues where ItemId=Guid and Type=4) OR ");
  3121. statement?.TryBind("@Tag" + i, GetCleanValue(tags[i]));
  3122. }
  3123. clauseBuilder.Length -= Or.Length;
  3124. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3125. clauseBuilder.Length = 0;
  3126. }
  3127. if (excludeTags.Count > 0)
  3128. {
  3129. clauseBuilder.Append('(');
  3130. for (var i = 0; i < excludeTags.Count; i++)
  3131. {
  3132. clauseBuilder.Append("@ExcludeTag")
  3133. .Append(i)
  3134. .Append(" not in (select CleanValue from ItemValues where ItemId=Guid and Type=4) OR ");
  3135. statement?.TryBind("@ExcludeTag" + i, GetCleanValue(excludeTags[i]));
  3136. }
  3137. clauseBuilder.Length -= Or.Length;
  3138. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3139. clauseBuilder.Length = 0;
  3140. }
  3141. if (query.StudioIds.Length > 0)
  3142. {
  3143. clauseBuilder.Append('(');
  3144. for (var i = 0; i < query.StudioIds.Length; i++)
  3145. {
  3146. clauseBuilder.Append("(guid in (select itemid from ItemValues where CleanValue = (select CleanName from TypedBaseItems where guid=@StudioId")
  3147. .Append(i)
  3148. .Append(") and Type=3)) OR ");
  3149. statement?.TryBind("@StudioId" + i, query.StudioIds[i]);
  3150. }
  3151. clauseBuilder.Length -= Or.Length;
  3152. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3153. clauseBuilder.Length = 0;
  3154. }
  3155. if (query.OfficialRatings.Length > 0)
  3156. {
  3157. clauseBuilder.Append('(');
  3158. for (var i = 0; i < query.OfficialRatings.Length; i++)
  3159. {
  3160. clauseBuilder.Append("OfficialRating=@OfficialRating").Append(i).Append(Or);
  3161. statement?.TryBind("@OfficialRating" + i, query.OfficialRatings[i]);
  3162. }
  3163. clauseBuilder.Length -= Or.Length;
  3164. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3165. clauseBuilder.Length = 0;
  3166. }
  3167. clauseBuilder.Append('(');
  3168. if (query.HasParentalRating ?? false)
  3169. {
  3170. clauseBuilder.Append("InheritedParentalRatingValue not null");
  3171. if (query.MinParentalRating.HasValue)
  3172. {
  3173. clauseBuilder.Append(" AND InheritedParentalRatingValue >= @MinParentalRating");
  3174. statement?.TryBind("@MinParentalRating", query.MinParentalRating.Value);
  3175. }
  3176. if (query.MaxParentalRating.HasValue)
  3177. {
  3178. clauseBuilder.Append(" AND InheritedParentalRatingValue <= @MaxParentalRating");
  3179. statement?.TryBind("@MaxParentalRating", query.MaxParentalRating.Value);
  3180. }
  3181. }
  3182. else if (query.BlockUnratedItems.Length > 0)
  3183. {
  3184. const string ParamName = "@UnratedType";
  3185. clauseBuilder.Append("(InheritedParentalRatingValue is null AND UnratedType not in (");
  3186. for (int i = 0; i < query.BlockUnratedItems.Length; i++)
  3187. {
  3188. clauseBuilder.Append(ParamName).Append(i).Append(',');
  3189. statement?.TryBind(ParamName + i, query.BlockUnratedItems[i].ToString());
  3190. }
  3191. // Remove trailing comma
  3192. clauseBuilder.Length--;
  3193. clauseBuilder.Append("))");
  3194. if (query.MinParentalRating.HasValue || query.MaxParentalRating.HasValue)
  3195. {
  3196. clauseBuilder.Append(" OR (");
  3197. }
  3198. if (query.MinParentalRating.HasValue)
  3199. {
  3200. clauseBuilder.Append("InheritedParentalRatingValue >= @MinParentalRating");
  3201. statement?.TryBind("@MinParentalRating", query.MinParentalRating.Value);
  3202. }
  3203. if (query.MaxParentalRating.HasValue)
  3204. {
  3205. if (query.MinParentalRating.HasValue)
  3206. {
  3207. clauseBuilder.Append(" AND ");
  3208. }
  3209. clauseBuilder.Append("InheritedParentalRatingValue <= @MaxParentalRating");
  3210. statement?.TryBind("@MaxParentalRating", query.MaxParentalRating.Value);
  3211. }
  3212. if (query.MinParentalRating.HasValue || query.MaxParentalRating.HasValue)
  3213. {
  3214. clauseBuilder.Append(')');
  3215. }
  3216. if (!(query.MinParentalRating.HasValue || query.MaxParentalRating.HasValue))
  3217. {
  3218. clauseBuilder.Append(" OR InheritedParentalRatingValue not null");
  3219. }
  3220. }
  3221. else if (query.MinParentalRating.HasValue)
  3222. {
  3223. clauseBuilder.Append("InheritedParentalRatingValue is null OR (InheritedParentalRatingValue >= @MinParentalRating");
  3224. statement?.TryBind("@MinParentalRating", query.MinParentalRating.Value);
  3225. if (query.MaxParentalRating.HasValue)
  3226. {
  3227. clauseBuilder.Append(" AND InheritedParentalRatingValue <= @MaxParentalRating");
  3228. statement?.TryBind("@MaxParentalRating", query.MaxParentalRating.Value);
  3229. }
  3230. clauseBuilder.Append(')');
  3231. }
  3232. else if (query.MaxParentalRating.HasValue)
  3233. {
  3234. clauseBuilder.Append("InheritedParentalRatingValue is null OR InheritedParentalRatingValue <= @MaxParentalRating");
  3235. statement?.TryBind("@MaxParentalRating", query.MaxParentalRating.Value);
  3236. }
  3237. else if (!query.HasParentalRating ?? false)
  3238. {
  3239. clauseBuilder.Append("InheritedParentalRatingValue is null");
  3240. }
  3241. if (clauseBuilder.Length > 1)
  3242. {
  3243. whereClauses.Add(clauseBuilder.Append(')').ToString());
  3244. clauseBuilder.Length = 0;
  3245. }
  3246. if (query.HasOfficialRating.HasValue)
  3247. {
  3248. if (query.HasOfficialRating.Value)
  3249. {
  3250. whereClauses.Add("(OfficialRating not null AND OfficialRating<>'')");
  3251. }
  3252. else
  3253. {
  3254. whereClauses.Add("(OfficialRating is null OR OfficialRating='')");
  3255. }
  3256. }
  3257. if (query.HasOverview.HasValue)
  3258. {
  3259. if (query.HasOverview.Value)
  3260. {
  3261. whereClauses.Add("(Overview not null AND Overview<>'')");
  3262. }
  3263. else
  3264. {
  3265. whereClauses.Add("(Overview is null OR Overview='')");
  3266. }
  3267. }
  3268. if (query.HasOwnerId.HasValue)
  3269. {
  3270. if (query.HasOwnerId.Value)
  3271. {
  3272. whereClauses.Add("OwnerId not null");
  3273. }
  3274. else
  3275. {
  3276. whereClauses.Add("OwnerId is null");
  3277. }
  3278. }
  3279. if (!string.IsNullOrWhiteSpace(query.HasNoAudioTrackWithLanguage))
  3280. {
  3281. whereClauses.Add("((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Audio' and MediaStreams.Language=@HasNoAudioTrackWithLanguage limit 1) is null)");
  3282. statement?.TryBind("@HasNoAudioTrackWithLanguage", query.HasNoAudioTrackWithLanguage);
  3283. }
  3284. if (!string.IsNullOrWhiteSpace(query.HasNoInternalSubtitleTrackWithLanguage))
  3285. {
  3286. whereClauses.Add("((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' and MediaStreams.IsExternal=0 and MediaStreams.Language=@HasNoInternalSubtitleTrackWithLanguage limit 1) is null)");
  3287. statement?.TryBind("@HasNoInternalSubtitleTrackWithLanguage", query.HasNoInternalSubtitleTrackWithLanguage);
  3288. }
  3289. if (!string.IsNullOrWhiteSpace(query.HasNoExternalSubtitleTrackWithLanguage))
  3290. {
  3291. whereClauses.Add("((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' and MediaStreams.IsExternal=1 and MediaStreams.Language=@HasNoExternalSubtitleTrackWithLanguage limit 1) is null)");
  3292. statement?.TryBind("@HasNoExternalSubtitleTrackWithLanguage", query.HasNoExternalSubtitleTrackWithLanguage);
  3293. }
  3294. if (!string.IsNullOrWhiteSpace(query.HasNoSubtitleTrackWithLanguage))
  3295. {
  3296. whereClauses.Add("((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' and MediaStreams.Language=@HasNoSubtitleTrackWithLanguage limit 1) is null)");
  3297. statement?.TryBind("@HasNoSubtitleTrackWithLanguage", query.HasNoSubtitleTrackWithLanguage);
  3298. }
  3299. if (query.HasSubtitles.HasValue)
  3300. {
  3301. if (query.HasSubtitles.Value)
  3302. {
  3303. whereClauses.Add("((select type from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' limit 1) not null)");
  3304. }
  3305. else
  3306. {
  3307. whereClauses.Add("((select type from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' limit 1) is null)");
  3308. }
  3309. }
  3310. if (query.HasChapterImages.HasValue)
  3311. {
  3312. if (query.HasChapterImages.Value)
  3313. {
  3314. whereClauses.Add("((select imagepath from Chapters2 where Chapters2.ItemId=A.Guid and imagepath not null limit 1) not null)");
  3315. }
  3316. else
  3317. {
  3318. whereClauses.Add("((select imagepath from Chapters2 where Chapters2.ItemId=A.Guid and imagepath not null limit 1) is null)");
  3319. }
  3320. }
  3321. if (query.HasDeadParentId.HasValue && query.HasDeadParentId.Value)
  3322. {
  3323. whereClauses.Add("ParentId NOT NULL AND ParentId NOT IN (select guid from TypedBaseItems)");
  3324. }
  3325. if (query.IsDeadArtist.HasValue && query.IsDeadArtist.Value)
  3326. {
  3327. whereClauses.Add("CleanName not in (Select CleanValue From ItemValues where Type in (0,1))");
  3328. }
  3329. if (query.IsDeadStudio.HasValue && query.IsDeadStudio.Value)
  3330. {
  3331. whereClauses.Add("CleanName not in (Select CleanValue From ItemValues where Type = 3)");
  3332. }
  3333. if (query.IsDeadPerson.HasValue && query.IsDeadPerson.Value)
  3334. {
  3335. whereClauses.Add("Name not in (Select Name From People)");
  3336. }
  3337. if (query.Years.Length == 1)
  3338. {
  3339. whereClauses.Add("ProductionYear=@Years");
  3340. statement?.TryBind("@Years", query.Years[0].ToString(CultureInfo.InvariantCulture));
  3341. }
  3342. else if (query.Years.Length > 1)
  3343. {
  3344. var val = string.Join(',', query.Years);
  3345. whereClauses.Add("ProductionYear in (" + val + ")");
  3346. }
  3347. var isVirtualItem = query.IsVirtualItem ?? query.IsMissing;
  3348. if (isVirtualItem.HasValue)
  3349. {
  3350. whereClauses.Add("IsVirtualItem=@IsVirtualItem");
  3351. statement?.TryBind("@IsVirtualItem", isVirtualItem.Value);
  3352. }
  3353. if (query.IsSpecialSeason.HasValue)
  3354. {
  3355. if (query.IsSpecialSeason.Value)
  3356. {
  3357. whereClauses.Add("IndexNumber = 0");
  3358. }
  3359. else
  3360. {
  3361. whereClauses.Add("IndexNumber <> 0");
  3362. }
  3363. }
  3364. if (query.IsUnaired.HasValue)
  3365. {
  3366. if (query.IsUnaired.Value)
  3367. {
  3368. whereClauses.Add("PremiereDate >= DATETIME('now')");
  3369. }
  3370. else
  3371. {
  3372. whereClauses.Add("PremiereDate < DATETIME('now')");
  3373. }
  3374. }
  3375. if (query.MediaTypes.Length == 1)
  3376. {
  3377. whereClauses.Add("MediaType=@MediaTypes");
  3378. statement?.TryBind("@MediaTypes", query.MediaTypes[0].ToString());
  3379. }
  3380. else if (query.MediaTypes.Length > 1)
  3381. {
  3382. var val = string.Join(',', query.MediaTypes.Select(i => $"'{i}'"));
  3383. whereClauses.Add("MediaType in (" + val + ")");
  3384. }
  3385. if (query.ItemIds.Length > 0)
  3386. {
  3387. var includeIds = new List<string>();
  3388. var index = 0;
  3389. foreach (var id in query.ItemIds)
  3390. {
  3391. includeIds.Add("Guid = @IncludeId" + index);
  3392. statement?.TryBind("@IncludeId" + index, id);
  3393. index++;
  3394. }
  3395. whereClauses.Add("(" + string.Join(" OR ", includeIds) + ")");
  3396. }
  3397. if (query.ExcludeItemIds.Length > 0)
  3398. {
  3399. var excludeIds = new List<string>();
  3400. var index = 0;
  3401. foreach (var id in query.ExcludeItemIds)
  3402. {
  3403. excludeIds.Add("Guid <> @ExcludeId" + index);
  3404. statement?.TryBind("@ExcludeId" + index, id);
  3405. index++;
  3406. }
  3407. whereClauses.Add(string.Join(" AND ", excludeIds));
  3408. }
  3409. if (query.ExcludeProviderIds is not null && query.ExcludeProviderIds.Count > 0)
  3410. {
  3411. var excludeIds = new List<string>();
  3412. var index = 0;
  3413. foreach (var pair in query.ExcludeProviderIds)
  3414. {
  3415. if (string.Equals(pair.Key, nameof(MetadataProvider.TmdbCollection), StringComparison.OrdinalIgnoreCase))
  3416. {
  3417. continue;
  3418. }
  3419. var paramName = "@ExcludeProviderId" + index;
  3420. excludeIds.Add("(ProviderIds is null or ProviderIds not like " + paramName + ")");
  3421. statement?.TryBind(paramName, "%" + pair.Key + "=" + pair.Value + "%");
  3422. index++;
  3423. break;
  3424. }
  3425. if (excludeIds.Count > 0)
  3426. {
  3427. whereClauses.Add(string.Join(" AND ", excludeIds));
  3428. }
  3429. }
  3430. if (query.HasAnyProviderId is not null && query.HasAnyProviderId.Count > 0)
  3431. {
  3432. var hasProviderIds = new List<string>();
  3433. var index = 0;
  3434. foreach (var pair in query.HasAnyProviderId)
  3435. {
  3436. if (string.Equals(pair.Key, nameof(MetadataProvider.TmdbCollection), StringComparison.OrdinalIgnoreCase))
  3437. {
  3438. continue;
  3439. }
  3440. // TODO this seems to be an idea for a better schema where ProviderIds are their own table
  3441. // but this is not implemented
  3442. // hasProviderIds.Add("(COALESCE((select value from ProviderIds where ItemId=Guid and Name = '" + pair.Key + "'), '') <> " + paramName + ")");
  3443. // TODO this is a really BAD way to do it since the pair:
  3444. // Tmdb, 1234 matches Tmdb=1234 but also Tmdb=1234567
  3445. // and maybe even NotTmdb=1234.
  3446. // this is a placeholder for this specific pair to correlate it in the bigger query
  3447. var paramName = "@HasAnyProviderId" + index;
  3448. // this is a search for the placeholder
  3449. hasProviderIds.Add("ProviderIds like " + paramName);
  3450. // this replaces the placeholder with a value, here: %key=val%
  3451. statement?.TryBind(paramName, "%" + pair.Key + "=" + pair.Value + "%");
  3452. index++;
  3453. break;
  3454. }
  3455. if (hasProviderIds.Count > 0)
  3456. {
  3457. whereClauses.Add("(" + string.Join(" OR ", hasProviderIds) + ")");
  3458. }
  3459. }
  3460. if (query.HasImdbId.HasValue)
  3461. {
  3462. whereClauses.Add(GetProviderIdClause(query.HasImdbId.Value, "imdb"));
  3463. }
  3464. if (query.HasTmdbId.HasValue)
  3465. {
  3466. whereClauses.Add(GetProviderIdClause(query.HasTmdbId.Value, "tmdb"));
  3467. }
  3468. if (query.HasTvdbId.HasValue)
  3469. {
  3470. whereClauses.Add(GetProviderIdClause(query.HasTvdbId.Value, "tvdb"));
  3471. }
  3472. var queryTopParentIds = query.TopParentIds;
  3473. if (queryTopParentIds.Length > 0)
  3474. {
  3475. var includedItemByNameTypes = GetItemByNameTypesInQuery(query);
  3476. var enableItemsByName = (query.IncludeItemsByName ?? false) && includedItemByNameTypes.Count > 0;
  3477. if (queryTopParentIds.Length == 1)
  3478. {
  3479. if (enableItemsByName && includedItemByNameTypes.Count == 1)
  3480. {
  3481. whereClauses.Add("(TopParentId=@TopParentId or Type=@IncludedItemByNameType)");
  3482. statement?.TryBind("@IncludedItemByNameType", includedItemByNameTypes[0]);
  3483. }
  3484. else if (enableItemsByName && includedItemByNameTypes.Count > 1)
  3485. {
  3486. var itemByNameTypeVal = string.Join(',', includedItemByNameTypes.Select(i => "'" + i + "'"));
  3487. whereClauses.Add("(TopParentId=@TopParentId or Type in (" + itemByNameTypeVal + "))");
  3488. }
  3489. else
  3490. {
  3491. whereClauses.Add("(TopParentId=@TopParentId)");
  3492. }
  3493. statement?.TryBind("@TopParentId", queryTopParentIds[0].ToString("N", CultureInfo.InvariantCulture));
  3494. }
  3495. else if (queryTopParentIds.Length > 1)
  3496. {
  3497. var val = string.Join(',', queryTopParentIds.Select(i => "'" + i.ToString("N", CultureInfo.InvariantCulture) + "'"));
  3498. if (enableItemsByName && includedItemByNameTypes.Count == 1)
  3499. {
  3500. whereClauses.Add("(Type=@IncludedItemByNameType or TopParentId in (" + val + "))");
  3501. statement?.TryBind("@IncludedItemByNameType", includedItemByNameTypes[0]);
  3502. }
  3503. else if (enableItemsByName && includedItemByNameTypes.Count > 1)
  3504. {
  3505. var itemByNameTypeVal = string.Join(',', includedItemByNameTypes.Select(i => "'" + i + "'"));
  3506. whereClauses.Add("(Type in (" + itemByNameTypeVal + ") or TopParentId in (" + val + "))");
  3507. }
  3508. else
  3509. {
  3510. whereClauses.Add("TopParentId in (" + val + ")");
  3511. }
  3512. }
  3513. }
  3514. if (query.AncestorIds.Length == 1)
  3515. {
  3516. whereClauses.Add("Guid in (select itemId from AncestorIds where AncestorId=@AncestorId)");
  3517. statement?.TryBind("@AncestorId", query.AncestorIds[0]);
  3518. }
  3519. if (query.AncestorIds.Length > 1)
  3520. {
  3521. var inClause = string.Join(',', query.AncestorIds.Select(i => "'" + i.ToString("N", CultureInfo.InvariantCulture) + "'"));
  3522. whereClauses.Add(string.Format(CultureInfo.InvariantCulture, "Guid in (select itemId from AncestorIds where AncestorIdText in ({0}))", inClause));
  3523. }
  3524. if (!string.IsNullOrWhiteSpace(query.AncestorWithPresentationUniqueKey))
  3525. {
  3526. var inClause = "select guid from TypedBaseItems where PresentationUniqueKey=@AncestorWithPresentationUniqueKey";
  3527. whereClauses.Add(string.Format(CultureInfo.InvariantCulture, "Guid in (select itemId from AncestorIds where AncestorId in ({0}))", inClause));
  3528. statement?.TryBind("@AncestorWithPresentationUniqueKey", query.AncestorWithPresentationUniqueKey);
  3529. }
  3530. if (!string.IsNullOrWhiteSpace(query.SeriesPresentationUniqueKey))
  3531. {
  3532. whereClauses.Add("SeriesPresentationUniqueKey=@SeriesPresentationUniqueKey");
  3533. statement?.TryBind("@SeriesPresentationUniqueKey", query.SeriesPresentationUniqueKey);
  3534. }
  3535. if (query.ExcludeInheritedTags.Length > 0)
  3536. {
  3537. var paramName = "@ExcludeInheritedTags";
  3538. if (statement is null)
  3539. {
  3540. int index = 0;
  3541. string excludedTags = string.Join(',', query.ExcludeInheritedTags.Select(_ => paramName + index++));
  3542. whereClauses.Add("((select CleanValue from ItemValues where ItemId=Guid and Type=6 and cleanvalue in (" + excludedTags + ")) is null)");
  3543. }
  3544. else
  3545. {
  3546. for (int index = 0; index < query.ExcludeInheritedTags.Length; index++)
  3547. {
  3548. statement.TryBind(paramName + index, GetCleanValue(query.ExcludeInheritedTags[index]));
  3549. }
  3550. }
  3551. }
  3552. if (query.IncludeInheritedTags.Length > 0)
  3553. {
  3554. var paramName = "@IncludeInheritedTags";
  3555. if (statement is null)
  3556. {
  3557. int index = 0;
  3558. string includedTags = string.Join(',', query.IncludeInheritedTags.Select(_ => paramName + index++));
  3559. // Episodes do not store inherit tags from their parents in the database, and the tag may be still required by the client.
  3560. // In addtion to the tags for the episodes themselves, we need to manually query its parent (the season)'s tags as well.
  3561. if (includeTypes.Length == 1 && includeTypes.FirstOrDefault() is BaseItemKind.Episode)
  3562. {
  3563. whereClauses.Add($"""
  3564. ((select CleanValue from ItemValues where ItemId=Guid and Type=6 and CleanValue in ({includedTags})) is not null
  3565. OR (select CleanValue from ItemValues where ItemId=ParentId and Type=6 and CleanValue in ({includedTags})) is not null)
  3566. """);
  3567. }
  3568. // A playlist should be accessible to its owner regardless of allowed tags.
  3569. else if (includeTypes.Length == 1 && includeTypes.FirstOrDefault() is BaseItemKind.Playlist)
  3570. {
  3571. whereClauses.Add($"""
  3572. ((select CleanValue from ItemValues where ItemId=Guid and Type=6 and CleanValue in ({includedTags})) is not null
  3573. OR data like @PlaylistOwnerUserId)
  3574. """);
  3575. }
  3576. else
  3577. {
  3578. whereClauses.Add("((select CleanValue from ItemValues where ItemId=Guid and Type=6 and cleanvalue in (" + includedTags + ")) is not null)");
  3579. }
  3580. }
  3581. else
  3582. {
  3583. for (int index = 0; index < query.IncludeInheritedTags.Length; index++)
  3584. {
  3585. statement.TryBind(paramName + index, GetCleanValue(query.IncludeInheritedTags[index]));
  3586. }
  3587. if (query.User is not null)
  3588. {
  3589. statement.TryBind("@PlaylistOwnerUserId", $"""%"OwnerUserId":"{query.User.Id.ToString("N")}"%""");
  3590. }
  3591. }
  3592. }
  3593. if (query.SeriesStatuses.Length > 0)
  3594. {
  3595. var statuses = new List<string>();
  3596. foreach (var seriesStatus in query.SeriesStatuses)
  3597. {
  3598. statuses.Add("data like '%" + seriesStatus + "%'");
  3599. }
  3600. whereClauses.Add("(" + string.Join(" OR ", statuses) + ")");
  3601. }
  3602. if (query.BoxSetLibraryFolders.Length > 0)
  3603. {
  3604. var folderIdQueries = new List<string>();
  3605. foreach (var folderId in query.BoxSetLibraryFolders)
  3606. {
  3607. folderIdQueries.Add("data like '%" + folderId.ToString("N", CultureInfo.InvariantCulture) + "%'");
  3608. }
  3609. whereClauses.Add("(" + string.Join(" OR ", folderIdQueries) + ")");
  3610. }
  3611. if (query.VideoTypes.Length > 0)
  3612. {
  3613. var videoTypes = new List<string>();
  3614. foreach (var videoType in query.VideoTypes)
  3615. {
  3616. videoTypes.Add("data like '%\"VideoType\":\"" + videoType + "\"%'");
  3617. }
  3618. whereClauses.Add("(" + string.Join(" OR ", videoTypes) + ")");
  3619. }
  3620. if (query.Is3D.HasValue)
  3621. {
  3622. if (query.Is3D.Value)
  3623. {
  3624. whereClauses.Add("data like '%Video3DFormat%'");
  3625. }
  3626. else
  3627. {
  3628. whereClauses.Add("data not like '%Video3DFormat%'");
  3629. }
  3630. }
  3631. if (query.IsPlaceHolder.HasValue)
  3632. {
  3633. if (query.IsPlaceHolder.Value)
  3634. {
  3635. whereClauses.Add("data like '%\"IsPlaceHolder\":true%'");
  3636. }
  3637. else
  3638. {
  3639. whereClauses.Add("(data is null or data not like '%\"IsPlaceHolder\":true%')");
  3640. }
  3641. }
  3642. if (query.HasSpecialFeature.HasValue)
  3643. {
  3644. if (query.HasSpecialFeature.Value)
  3645. {
  3646. whereClauses.Add("ExtraIds not null");
  3647. }
  3648. else
  3649. {
  3650. whereClauses.Add("ExtraIds is null");
  3651. }
  3652. }
  3653. if (query.HasTrailer.HasValue)
  3654. {
  3655. if (query.HasTrailer.Value)
  3656. {
  3657. whereClauses.Add("ExtraIds not null");
  3658. }
  3659. else
  3660. {
  3661. whereClauses.Add("ExtraIds is null");
  3662. }
  3663. }
  3664. if (query.HasThemeSong.HasValue)
  3665. {
  3666. if (query.HasThemeSong.Value)
  3667. {
  3668. whereClauses.Add("ExtraIds not null");
  3669. }
  3670. else
  3671. {
  3672. whereClauses.Add("ExtraIds is null");
  3673. }
  3674. }
  3675. if (query.HasThemeVideo.HasValue)
  3676. {
  3677. if (query.HasThemeVideo.Value)
  3678. {
  3679. whereClauses.Add("ExtraIds not null");
  3680. }
  3681. else
  3682. {
  3683. whereClauses.Add("ExtraIds is null");
  3684. }
  3685. }
  3686. return whereClauses;
  3687. }
  3688. /// <summary>
  3689. /// Formats a where clause for the specified provider.
  3690. /// </summary>
  3691. /// <param name="includeResults">Whether or not to include items with this provider's ids.</param>
  3692. /// <param name="provider">Provider name.</param>
  3693. /// <returns>Formatted SQL clause.</returns>
  3694. private string GetProviderIdClause(bool includeResults, string provider)
  3695. {
  3696. return string.Format(
  3697. CultureInfo.InvariantCulture,
  3698. "ProviderIds {0} like '%{1}=%'",
  3699. includeResults ? string.Empty : "not",
  3700. provider);
  3701. }
  3702. #nullable disable
  3703. private List<string> GetItemByNameTypesInQuery(InternalItemsQuery query)
  3704. {
  3705. var list = new List<string>();
  3706. if (IsTypeInQuery(BaseItemKind.Person, query))
  3707. {
  3708. list.Add(typeof(Person).FullName);
  3709. }
  3710. if (IsTypeInQuery(BaseItemKind.Genre, query))
  3711. {
  3712. list.Add(typeof(Genre).FullName);
  3713. }
  3714. if (IsTypeInQuery(BaseItemKind.MusicGenre, query))
  3715. {
  3716. list.Add(typeof(MusicGenre).FullName);
  3717. }
  3718. if (IsTypeInQuery(BaseItemKind.MusicArtist, query))
  3719. {
  3720. list.Add(typeof(MusicArtist).FullName);
  3721. }
  3722. if (IsTypeInQuery(BaseItemKind.Studio, query))
  3723. {
  3724. list.Add(typeof(Studio).FullName);
  3725. }
  3726. return list;
  3727. }
  3728. private bool IsTypeInQuery(BaseItemKind type, InternalItemsQuery query)
  3729. {
  3730. if (query.ExcludeItemTypes.Contains(type))
  3731. {
  3732. return false;
  3733. }
  3734. return query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(type);
  3735. }
  3736. private string GetCleanValue(string value)
  3737. {
  3738. if (string.IsNullOrWhiteSpace(value))
  3739. {
  3740. return value;
  3741. }
  3742. return value.RemoveDiacritics().ToLowerInvariant();
  3743. }
  3744. private bool EnableGroupByPresentationUniqueKey(InternalItemsQuery query)
  3745. {
  3746. if (!query.GroupByPresentationUniqueKey)
  3747. {
  3748. return false;
  3749. }
  3750. if (query.GroupBySeriesPresentationUniqueKey)
  3751. {
  3752. return false;
  3753. }
  3754. if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey))
  3755. {
  3756. return false;
  3757. }
  3758. if (query.User is null)
  3759. {
  3760. return false;
  3761. }
  3762. if (query.IncludeItemTypes.Length == 0)
  3763. {
  3764. return true;
  3765. }
  3766. return query.IncludeItemTypes.Contains(BaseItemKind.Episode)
  3767. || query.IncludeItemTypes.Contains(BaseItemKind.Video)
  3768. || query.IncludeItemTypes.Contains(BaseItemKind.Movie)
  3769. || query.IncludeItemTypes.Contains(BaseItemKind.MusicVideo)
  3770. || query.IncludeItemTypes.Contains(BaseItemKind.Series)
  3771. || query.IncludeItemTypes.Contains(BaseItemKind.Season);
  3772. }
  3773. /// <inheritdoc />
  3774. public void UpdateInheritedValues()
  3775. {
  3776. const string Statements = """
  3777. delete from ItemValues where type = 6;
  3778. insert into ItemValues (ItemId, Type, Value, CleanValue) select ItemId, 6, Value, CleanValue from ItemValues where Type=4;
  3779. insert into ItemValues (ItemId, Type, Value, CleanValue) select AncestorIds.itemid, 6, ItemValues.Value, ItemValues.CleanValue
  3780. FROM AncestorIds
  3781. LEFT JOIN ItemValues ON (AncestorIds.AncestorId = ItemValues.ItemId)
  3782. where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type = 4;
  3783. """;
  3784. using var connection = GetConnection();
  3785. using var transaction = connection.BeginTransaction();
  3786. connection.Execute(Statements);
  3787. transaction.Commit();
  3788. }
  3789. /// <inheritdoc />
  3790. public void DeleteItem(Guid id)
  3791. {
  3792. if (id.IsEmpty())
  3793. {
  3794. throw new ArgumentNullException(nameof(id));
  3795. }
  3796. CheckDisposed();
  3797. using var connection = GetConnection();
  3798. using var transaction = connection.BeginTransaction();
  3799. // Delete people
  3800. ExecuteWithSingleParam(connection, "delete from People where ItemId=@Id", id);
  3801. // Delete chapters
  3802. ExecuteWithSingleParam(connection, "delete from " + ChaptersTableName + " where ItemId=@Id", id);
  3803. // Delete media streams
  3804. ExecuteWithSingleParam(connection, "delete from mediastreams where ItemId=@Id", id);
  3805. // Delete ancestors
  3806. ExecuteWithSingleParam(connection, "delete from AncestorIds where ItemId=@Id", id);
  3807. // Delete item values
  3808. ExecuteWithSingleParam(connection, "delete from ItemValues where ItemId=@Id", id);
  3809. // Delete the item
  3810. ExecuteWithSingleParam(connection, "delete from TypedBaseItems where guid=@Id", id);
  3811. transaction.Commit();
  3812. }
  3813. private void ExecuteWithSingleParam(ManagedConnection db, string query, Guid value)
  3814. {
  3815. using (var statement = PrepareStatement(db, query))
  3816. {
  3817. statement.TryBind("@Id", value);
  3818. statement.ExecuteNonQuery();
  3819. }
  3820. }
  3821. /// <inheritdoc />
  3822. public List<string> GetPeopleNames(InternalPeopleQuery query)
  3823. {
  3824. ArgumentNullException.ThrowIfNull(query);
  3825. CheckDisposed();
  3826. var commandText = new StringBuilder("select Distinct p.Name from People p");
  3827. var whereClauses = GetPeopleWhereClauses(query, null);
  3828. if (whereClauses.Count != 0)
  3829. {
  3830. commandText.Append(" where ").AppendJoin(" AND ", whereClauses);
  3831. }
  3832. commandText.Append(" order by ListOrder");
  3833. if (query.Limit > 0)
  3834. {
  3835. commandText.Append(" LIMIT ").Append(query.Limit);
  3836. }
  3837. var list = new List<string>();
  3838. using (var connection = GetConnection(true))
  3839. using (var statement = PrepareStatement(connection, commandText.ToString()))
  3840. {
  3841. // Run this again to bind the params
  3842. GetPeopleWhereClauses(query, statement);
  3843. foreach (var row in statement.ExecuteQuery())
  3844. {
  3845. list.Add(row.GetString(0));
  3846. }
  3847. }
  3848. return list;
  3849. }
  3850. /// <inheritdoc />
  3851. public List<PersonInfo> GetPeople(InternalPeopleQuery query)
  3852. {
  3853. ArgumentNullException.ThrowIfNull(query);
  3854. CheckDisposed();
  3855. StringBuilder commandText = new StringBuilder("select ItemId, Name, Role, PersonType, SortOrder from People p");
  3856. var whereClauses = GetPeopleWhereClauses(query, null);
  3857. if (whereClauses.Count != 0)
  3858. {
  3859. commandText.Append(" where ").AppendJoin(" AND ", whereClauses);
  3860. }
  3861. commandText.Append(" order by ListOrder");
  3862. if (query.Limit > 0)
  3863. {
  3864. commandText.Append(" LIMIT ").Append(query.Limit);
  3865. }
  3866. var list = new List<PersonInfo>();
  3867. using (var connection = GetConnection(true))
  3868. using (var statement = PrepareStatement(connection, commandText.ToString()))
  3869. {
  3870. // Run this again to bind the params
  3871. GetPeopleWhereClauses(query, statement);
  3872. foreach (var row in statement.ExecuteQuery())
  3873. {
  3874. list.Add(GetPerson(row));
  3875. }
  3876. }
  3877. return list;
  3878. }
  3879. private List<string> GetPeopleWhereClauses(InternalPeopleQuery query, SqliteCommand statement)
  3880. {
  3881. var whereClauses = new List<string>();
  3882. if (query.User is not null && query.IsFavorite.HasValue)
  3883. {
  3884. whereClauses.Add(@"p.Name IN (
  3885. SELECT Name FROM TypedBaseItems WHERE UserDataKey IN (
  3886. SELECT key FROM UserDatas WHERE isFavorite=@IsFavorite AND userId=@UserId)
  3887. AND Type = @InternalPersonType)");
  3888. statement?.TryBind("@IsFavorite", query.IsFavorite.Value);
  3889. statement?.TryBind("@InternalPersonType", typeof(Person).FullName);
  3890. statement?.TryBind("@UserId", query.User.InternalId);
  3891. }
  3892. if (!query.ItemId.IsEmpty())
  3893. {
  3894. whereClauses.Add("ItemId=@ItemId");
  3895. statement?.TryBind("@ItemId", query.ItemId);
  3896. }
  3897. if (!query.AppearsInItemId.IsEmpty())
  3898. {
  3899. whereClauses.Add("p.Name in (Select Name from People where ItemId=@AppearsInItemId)");
  3900. statement?.TryBind("@AppearsInItemId", query.AppearsInItemId);
  3901. }
  3902. var queryPersonTypes = query.PersonTypes.Where(IsValidPersonType).ToList();
  3903. if (queryPersonTypes.Count == 1)
  3904. {
  3905. whereClauses.Add("PersonType=@PersonType");
  3906. statement?.TryBind("@PersonType", queryPersonTypes[0]);
  3907. }
  3908. else if (queryPersonTypes.Count > 1)
  3909. {
  3910. var val = string.Join(',', queryPersonTypes.Select(i => "'" + i + "'"));
  3911. whereClauses.Add("PersonType in (" + val + ")");
  3912. }
  3913. var queryExcludePersonTypes = query.ExcludePersonTypes.Where(IsValidPersonType).ToList();
  3914. if (queryExcludePersonTypes.Count == 1)
  3915. {
  3916. whereClauses.Add("PersonType<>@PersonType");
  3917. statement?.TryBind("@PersonType", queryExcludePersonTypes[0]);
  3918. }
  3919. else if (queryExcludePersonTypes.Count > 1)
  3920. {
  3921. var val = string.Join(',', queryExcludePersonTypes.Select(i => "'" + i + "'"));
  3922. whereClauses.Add("PersonType not in (" + val + ")");
  3923. }
  3924. if (query.MaxListOrder.HasValue)
  3925. {
  3926. whereClauses.Add("ListOrder<=@MaxListOrder");
  3927. statement?.TryBind("@MaxListOrder", query.MaxListOrder.Value);
  3928. }
  3929. if (!string.IsNullOrWhiteSpace(query.NameContains))
  3930. {
  3931. whereClauses.Add("p.Name like @NameContains");
  3932. statement?.TryBind("@NameContains", "%" + query.NameContains + "%");
  3933. }
  3934. return whereClauses;
  3935. }
  3936. private void UpdateAncestors(Guid itemId, List<Guid> ancestorIds, ManagedConnection db, SqliteCommand deleteAncestorsStatement)
  3937. {
  3938. if (itemId.IsEmpty())
  3939. {
  3940. throw new ArgumentNullException(nameof(itemId));
  3941. }
  3942. ArgumentNullException.ThrowIfNull(ancestorIds);
  3943. CheckDisposed();
  3944. // First delete
  3945. deleteAncestorsStatement.TryBind("@ItemId", itemId);
  3946. deleteAncestorsStatement.ExecuteNonQuery();
  3947. if (ancestorIds.Count == 0)
  3948. {
  3949. return;
  3950. }
  3951. var insertText = new StringBuilder("insert into AncestorIds (ItemId, AncestorId, AncestorIdText) values ");
  3952. for (var i = 0; i < ancestorIds.Count; i++)
  3953. {
  3954. insertText.AppendFormat(
  3955. CultureInfo.InvariantCulture,
  3956. "(@ItemId, @AncestorId{0}, @AncestorIdText{0}),",
  3957. i.ToString(CultureInfo.InvariantCulture));
  3958. }
  3959. // Remove trailing comma
  3960. insertText.Length--;
  3961. using (var statement = PrepareStatement(db, insertText.ToString()))
  3962. {
  3963. statement.TryBind("@ItemId", itemId);
  3964. for (var i = 0; i < ancestorIds.Count; i++)
  3965. {
  3966. var index = i.ToString(CultureInfo.InvariantCulture);
  3967. var ancestorId = ancestorIds[i];
  3968. statement.TryBind("@AncestorId" + index, ancestorId);
  3969. statement.TryBind("@AncestorIdText" + index, ancestorId.ToString("N", CultureInfo.InvariantCulture));
  3970. }
  3971. statement.ExecuteNonQuery();
  3972. }
  3973. }
  3974. /// <inheritdoc />
  3975. public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetAllArtists(InternalItemsQuery query)
  3976. {
  3977. return GetItemValues(query, new[] { 0, 1 }, typeof(MusicArtist).FullName);
  3978. }
  3979. /// <inheritdoc />
  3980. public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetArtists(InternalItemsQuery query)
  3981. {
  3982. return GetItemValues(query, new[] { 0 }, typeof(MusicArtist).FullName);
  3983. }
  3984. /// <inheritdoc />
  3985. public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetAlbumArtists(InternalItemsQuery query)
  3986. {
  3987. return GetItemValues(query, new[] { 1 }, typeof(MusicArtist).FullName);
  3988. }
  3989. /// <inheritdoc />
  3990. public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetStudios(InternalItemsQuery query)
  3991. {
  3992. return GetItemValues(query, new[] { 3 }, typeof(Studio).FullName);
  3993. }
  3994. /// <inheritdoc />
  3995. public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetGenres(InternalItemsQuery query)
  3996. {
  3997. return GetItemValues(query, new[] { 2 }, typeof(Genre).FullName);
  3998. }
  3999. /// <inheritdoc />
  4000. public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetMusicGenres(InternalItemsQuery query)
  4001. {
  4002. return GetItemValues(query, new[] { 2 }, typeof(MusicGenre).FullName);
  4003. }
  4004. /// <inheritdoc />
  4005. public List<string> GetStudioNames()
  4006. {
  4007. return GetItemValueNames(new[] { 3 }, Array.Empty<string>(), Array.Empty<string>());
  4008. }
  4009. /// <inheritdoc />
  4010. public List<string> GetAllArtistNames()
  4011. {
  4012. return GetItemValueNames(new[] { 0, 1 }, Array.Empty<string>(), Array.Empty<string>());
  4013. }
  4014. /// <inheritdoc />
  4015. public List<string> GetMusicGenreNames()
  4016. {
  4017. return GetItemValueNames(
  4018. new[] { 2 },
  4019. new string[]
  4020. {
  4021. typeof(Audio).FullName,
  4022. typeof(MusicVideo).FullName,
  4023. typeof(MusicAlbum).FullName,
  4024. typeof(MusicArtist).FullName
  4025. },
  4026. Array.Empty<string>());
  4027. }
  4028. /// <inheritdoc />
  4029. public List<string> GetGenreNames()
  4030. {
  4031. return GetItemValueNames(
  4032. new[] { 2 },
  4033. Array.Empty<string>(),
  4034. new string[]
  4035. {
  4036. typeof(Audio).FullName,
  4037. typeof(MusicVideo).FullName,
  4038. typeof(MusicAlbum).FullName,
  4039. typeof(MusicArtist).FullName
  4040. });
  4041. }
  4042. private List<string> GetItemValueNames(int[] itemValueTypes, IReadOnlyList<string> withItemTypes, IReadOnlyList<string> excludeItemTypes)
  4043. {
  4044. CheckDisposed();
  4045. var stringBuilder = new StringBuilder("Select Value From ItemValues where Type", 128);
  4046. if (itemValueTypes.Length == 1)
  4047. {
  4048. stringBuilder.Append('=')
  4049. .Append(itemValueTypes[0]);
  4050. }
  4051. else
  4052. {
  4053. stringBuilder.Append(" in (")
  4054. .AppendJoin(',', itemValueTypes)
  4055. .Append(')');
  4056. }
  4057. if (withItemTypes.Count > 0)
  4058. {
  4059. stringBuilder.Append(" AND ItemId In (select guid from typedbaseitems where type in (")
  4060. .AppendJoinInSingleQuotes(',', withItemTypes)
  4061. .Append("))");
  4062. }
  4063. if (excludeItemTypes.Count > 0)
  4064. {
  4065. stringBuilder.Append(" AND ItemId not In (select guid from typedbaseitems where type in (")
  4066. .AppendJoinInSingleQuotes(',', excludeItemTypes)
  4067. .Append("))");
  4068. }
  4069. stringBuilder.Append(" Group By CleanValue");
  4070. var commandText = stringBuilder.ToString();
  4071. var list = new List<string>();
  4072. using (new QueryTimeLogger(Logger, commandText))
  4073. using (var connection = GetConnection(true))
  4074. using (var statement = PrepareStatement(connection, commandText))
  4075. {
  4076. foreach (var row in statement.ExecuteQuery())
  4077. {
  4078. if (row.TryGetString(0, out var result))
  4079. {
  4080. list.Add(result);
  4081. }
  4082. }
  4083. }
  4084. return list;
  4085. }
  4086. private QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetItemValues(InternalItemsQuery query, int[] itemValueTypes, string returnType)
  4087. {
  4088. ArgumentNullException.ThrowIfNull(query);
  4089. if (!query.Limit.HasValue)
  4090. {
  4091. query.EnableTotalRecordCount = false;
  4092. }
  4093. CheckDisposed();
  4094. var typeClause = itemValueTypes.Length == 1 ?
  4095. ("Type=" + itemValueTypes[0]) :
  4096. ("Type in (" + string.Join(',', itemValueTypes) + ")");
  4097. InternalItemsQuery typeSubQuery = null;
  4098. string itemCountColumns = null;
  4099. var stringBuilder = new StringBuilder(1024);
  4100. var typesToCount = query.IncludeItemTypes;
  4101. if (typesToCount.Length > 0)
  4102. {
  4103. stringBuilder.Append("(select group_concat(type, '|') from TypedBaseItems B");
  4104. typeSubQuery = new InternalItemsQuery(query.User)
  4105. {
  4106. ExcludeItemTypes = query.ExcludeItemTypes,
  4107. IncludeItemTypes = query.IncludeItemTypes,
  4108. MediaTypes = query.MediaTypes,
  4109. AncestorIds = query.AncestorIds,
  4110. ExcludeItemIds = query.ExcludeItemIds,
  4111. ItemIds = query.ItemIds,
  4112. TopParentIds = query.TopParentIds,
  4113. ParentId = query.ParentId,
  4114. IsPlayed = query.IsPlayed
  4115. };
  4116. var whereClauses = GetWhereClauses(typeSubQuery, null);
  4117. stringBuilder.Append(" where ")
  4118. .AppendJoin(" AND ", whereClauses)
  4119. .Append(" AND ")
  4120. .Append("guid in (select ItemId from ItemValues where ItemValues.CleanValue=A.CleanName AND ")
  4121. .Append(typeClause)
  4122. .Append(")) as itemTypes");
  4123. itemCountColumns = stringBuilder.ToString();
  4124. stringBuilder.Clear();
  4125. }
  4126. List<string> columns = _retrieveItemColumns.ToList();
  4127. // Unfortunately we need to add it to columns to ensure the order of the columns in the select
  4128. if (!string.IsNullOrEmpty(itemCountColumns))
  4129. {
  4130. columns.Add(itemCountColumns);
  4131. }
  4132. // do this first before calling GetFinalColumnsToSelect, otherwise ExcludeItemIds will be set by SimilarTo
  4133. var innerQuery = new InternalItemsQuery(query.User)
  4134. {
  4135. ExcludeItemTypes = query.ExcludeItemTypes,
  4136. IncludeItemTypes = query.IncludeItemTypes,
  4137. MediaTypes = query.MediaTypes,
  4138. AncestorIds = query.AncestorIds,
  4139. ItemIds = query.ItemIds,
  4140. TopParentIds = query.TopParentIds,
  4141. ParentId = query.ParentId,
  4142. IsAiring = query.IsAiring,
  4143. IsMovie = query.IsMovie,
  4144. IsSports = query.IsSports,
  4145. IsKids = query.IsKids,
  4146. IsNews = query.IsNews,
  4147. IsSeries = query.IsSeries
  4148. };
  4149. SetFinalColumnsToSelect(query, columns);
  4150. var innerWhereClauses = GetWhereClauses(innerQuery, null);
  4151. stringBuilder.Append(" where Type=@SelectType And CleanName In (Select CleanValue from ItemValues where ")
  4152. .Append(typeClause)
  4153. .Append(" AND ItemId in (select guid from TypedBaseItems");
  4154. if (innerWhereClauses.Count > 0)
  4155. {
  4156. stringBuilder.Append(" where ")
  4157. .AppendJoin(" AND ", innerWhereClauses);
  4158. }
  4159. stringBuilder.Append("))");
  4160. var outerQuery = new InternalItemsQuery(query.User)
  4161. {
  4162. IsPlayed = query.IsPlayed,
  4163. IsFavorite = query.IsFavorite,
  4164. IsFavoriteOrLiked = query.IsFavoriteOrLiked,
  4165. IsLiked = query.IsLiked,
  4166. IsLocked = query.IsLocked,
  4167. NameLessThan = query.NameLessThan,
  4168. NameStartsWith = query.NameStartsWith,
  4169. NameStartsWithOrGreater = query.NameStartsWithOrGreater,
  4170. Tags = query.Tags,
  4171. OfficialRatings = query.OfficialRatings,
  4172. StudioIds = query.StudioIds,
  4173. GenreIds = query.GenreIds,
  4174. Genres = query.Genres,
  4175. Years = query.Years,
  4176. NameContains = query.NameContains,
  4177. SearchTerm = query.SearchTerm,
  4178. SimilarTo = query.SimilarTo,
  4179. ExcludeItemIds = query.ExcludeItemIds
  4180. };
  4181. var outerWhereClauses = GetWhereClauses(outerQuery, null);
  4182. if (outerWhereClauses.Count != 0)
  4183. {
  4184. stringBuilder.Append(" AND ")
  4185. .AppendJoin(" AND ", outerWhereClauses);
  4186. }
  4187. var whereText = stringBuilder.ToString();
  4188. stringBuilder.Clear();
  4189. stringBuilder.Append("select ")
  4190. .AppendJoin(',', columns)
  4191. .Append(FromText)
  4192. .Append(GetJoinUserDataText(query))
  4193. .Append(whereText)
  4194. .Append(" group by PresentationUniqueKey");
  4195. if (query.OrderBy.Count != 0
  4196. || query.SimilarTo is not null
  4197. || !string.IsNullOrEmpty(query.SearchTerm))
  4198. {
  4199. stringBuilder.Append(GetOrderByText(query));
  4200. }
  4201. else
  4202. {
  4203. stringBuilder.Append(" order by SortName");
  4204. }
  4205. if (query.Limit.HasValue || query.StartIndex.HasValue)
  4206. {
  4207. var offset = query.StartIndex ?? 0;
  4208. if (query.Limit.HasValue || offset > 0)
  4209. {
  4210. stringBuilder.Append(" LIMIT ")
  4211. .Append(query.Limit ?? int.MaxValue);
  4212. }
  4213. if (offset > 0)
  4214. {
  4215. stringBuilder.Append(" OFFSET ")
  4216. .Append(offset);
  4217. }
  4218. }
  4219. var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0;
  4220. string commandText = string.Empty;
  4221. if (!isReturningZeroItems)
  4222. {
  4223. commandText = stringBuilder.ToString();
  4224. }
  4225. string countText = string.Empty;
  4226. if (query.EnableTotalRecordCount)
  4227. {
  4228. stringBuilder.Clear();
  4229. var columnsToSelect = new List<string> { "count (distinct PresentationUniqueKey)" };
  4230. SetFinalColumnsToSelect(query, columnsToSelect);
  4231. stringBuilder.Append("select ")
  4232. .AppendJoin(',', columnsToSelect)
  4233. .Append(FromText)
  4234. .Append(GetJoinUserDataText(query))
  4235. .Append(whereText);
  4236. countText = stringBuilder.ToString();
  4237. }
  4238. var list = new List<(BaseItem, ItemCounts)>();
  4239. var result = new QueryResult<(BaseItem, ItemCounts)>();
  4240. using (new QueryTimeLogger(Logger, commandText))
  4241. using (var connection = GetConnection(true))
  4242. using (var transaction = connection.BeginTransaction())
  4243. {
  4244. if (!isReturningZeroItems)
  4245. {
  4246. using (var statement = PrepareStatement(connection, commandText))
  4247. {
  4248. statement.TryBind("@SelectType", returnType);
  4249. if (EnableJoinUserData(query))
  4250. {
  4251. statement.TryBind("@UserId", query.User.InternalId);
  4252. }
  4253. if (typeSubQuery is not null)
  4254. {
  4255. GetWhereClauses(typeSubQuery, null);
  4256. }
  4257. BindSimilarParams(query, statement);
  4258. BindSearchParams(query, statement);
  4259. GetWhereClauses(innerQuery, statement);
  4260. GetWhereClauses(outerQuery, statement);
  4261. var hasEpisodeAttributes = HasEpisodeAttributes(query);
  4262. var hasProgramAttributes = HasProgramAttributes(query);
  4263. var hasServiceName = HasServiceName(query);
  4264. var hasStartDate = HasStartDate(query);
  4265. var hasTrailerTypes = HasTrailerTypes(query);
  4266. var hasArtistFields = HasArtistFields(query);
  4267. var hasSeriesFields = HasSeriesFields(query);
  4268. foreach (var row in statement.ExecuteQuery())
  4269. {
  4270. var item = GetItem(row, query, hasProgramAttributes, hasEpisodeAttributes, hasServiceName, hasStartDate, hasTrailerTypes, hasArtistFields, hasSeriesFields, false);
  4271. if (item is not null)
  4272. {
  4273. var countStartColumn = columns.Count - 1;
  4274. list.Add((item, GetItemCounts(row, countStartColumn, typesToCount)));
  4275. }
  4276. }
  4277. }
  4278. }
  4279. if (query.EnableTotalRecordCount)
  4280. {
  4281. using (var statement = PrepareStatement(connection, countText))
  4282. {
  4283. statement.TryBind("@SelectType", returnType);
  4284. if (EnableJoinUserData(query))
  4285. {
  4286. statement.TryBind("@UserId", query.User.InternalId);
  4287. }
  4288. if (typeSubQuery is not null)
  4289. {
  4290. GetWhereClauses(typeSubQuery, null);
  4291. }
  4292. BindSimilarParams(query, statement);
  4293. BindSearchParams(query, statement);
  4294. GetWhereClauses(innerQuery, statement);
  4295. GetWhereClauses(outerQuery, statement);
  4296. result.TotalRecordCount = statement.SelectScalarInt();
  4297. }
  4298. }
  4299. transaction.Commit();
  4300. }
  4301. if (result.TotalRecordCount == 0)
  4302. {
  4303. result.TotalRecordCount = list.Count;
  4304. }
  4305. result.StartIndex = query.StartIndex ?? 0;
  4306. result.Items = list;
  4307. return result;
  4308. }
  4309. private static ItemCounts GetItemCounts(SqliteDataReader reader, int countStartColumn, BaseItemKind[] typesToCount)
  4310. {
  4311. var counts = new ItemCounts();
  4312. if (typesToCount.Length == 0)
  4313. {
  4314. return counts;
  4315. }
  4316. if (!reader.TryGetString(countStartColumn, out var typeString))
  4317. {
  4318. return counts;
  4319. }
  4320. foreach (var typeName in typeString.AsSpan().Split('|'))
  4321. {
  4322. if (typeName.Equals(typeof(Series).FullName, StringComparison.OrdinalIgnoreCase))
  4323. {
  4324. counts.SeriesCount++;
  4325. }
  4326. else if (typeName.Equals(typeof(Episode).FullName, StringComparison.OrdinalIgnoreCase))
  4327. {
  4328. counts.EpisodeCount++;
  4329. }
  4330. else if (typeName.Equals(typeof(Movie).FullName, StringComparison.OrdinalIgnoreCase))
  4331. {
  4332. counts.MovieCount++;
  4333. }
  4334. else if (typeName.Equals(typeof(MusicAlbum).FullName, StringComparison.OrdinalIgnoreCase))
  4335. {
  4336. counts.AlbumCount++;
  4337. }
  4338. else if (typeName.Equals(typeof(MusicArtist).FullName, StringComparison.OrdinalIgnoreCase))
  4339. {
  4340. counts.ArtistCount++;
  4341. }
  4342. else if (typeName.Equals(typeof(Audio).FullName, StringComparison.OrdinalIgnoreCase))
  4343. {
  4344. counts.SongCount++;
  4345. }
  4346. else if (typeName.Equals(typeof(Trailer).FullName, StringComparison.OrdinalIgnoreCase))
  4347. {
  4348. counts.TrailerCount++;
  4349. }
  4350. counts.ItemCount++;
  4351. }
  4352. return counts;
  4353. }
  4354. private List<(int MagicNumber, string Value)> GetItemValuesToSave(BaseItem item, List<string> inheritedTags)
  4355. {
  4356. var list = new List<(int, string)>();
  4357. if (item is IHasArtist hasArtist)
  4358. {
  4359. list.AddRange(hasArtist.Artists.Select(i => (0, i)));
  4360. }
  4361. if (item is IHasAlbumArtist hasAlbumArtist)
  4362. {
  4363. list.AddRange(hasAlbumArtist.AlbumArtists.Select(i => (1, i)));
  4364. }
  4365. list.AddRange(item.Genres.Select(i => (2, i)));
  4366. list.AddRange(item.Studios.Select(i => (3, i)));
  4367. list.AddRange(item.Tags.Select(i => (4, i)));
  4368. // keywords was 5
  4369. list.AddRange(inheritedTags.Select(i => (6, i)));
  4370. // Remove all invalid values.
  4371. list.RemoveAll(i => string.IsNullOrWhiteSpace(i.Item2));
  4372. return list;
  4373. }
  4374. private void UpdateItemValues(Guid itemId, List<(int MagicNumber, string Value)> values, ManagedConnection db)
  4375. {
  4376. if (itemId.IsEmpty())
  4377. {
  4378. throw new ArgumentNullException(nameof(itemId));
  4379. }
  4380. ArgumentNullException.ThrowIfNull(values);
  4381. CheckDisposed();
  4382. // First delete
  4383. using var command = db.PrepareStatement("delete from ItemValues where ItemId=@Id");
  4384. command.TryBind("@Id", itemId);
  4385. command.ExecuteNonQuery();
  4386. InsertItemValues(itemId, values, db);
  4387. }
  4388. private void InsertItemValues(Guid id, List<(int MagicNumber, string Value)> values, ManagedConnection db)
  4389. {
  4390. const int Limit = 100;
  4391. var startIndex = 0;
  4392. const string StartInsertText = "insert into ItemValues (ItemId, Type, Value, CleanValue) values ";
  4393. var insertText = new StringBuilder(StartInsertText);
  4394. while (startIndex < values.Count)
  4395. {
  4396. var endIndex = Math.Min(values.Count, startIndex + Limit);
  4397. for (var i = startIndex; i < endIndex; i++)
  4398. {
  4399. insertText.AppendFormat(
  4400. CultureInfo.InvariantCulture,
  4401. "(@ItemId, @Type{0}, @Value{0}, @CleanValue{0}),",
  4402. i);
  4403. }
  4404. // Remove trailing comma
  4405. insertText.Length--;
  4406. using (var statement = PrepareStatement(db, insertText.ToString()))
  4407. {
  4408. statement.TryBind("@ItemId", id);
  4409. for (var i = startIndex; i < endIndex; i++)
  4410. {
  4411. var index = i.ToString(CultureInfo.InvariantCulture);
  4412. var currentValueInfo = values[i];
  4413. var itemValue = currentValueInfo.Value;
  4414. statement.TryBind("@Type" + index, currentValueInfo.MagicNumber);
  4415. statement.TryBind("@Value" + index, itemValue);
  4416. statement.TryBind("@CleanValue" + index, GetCleanValue(itemValue));
  4417. }
  4418. statement.ExecuteNonQuery();
  4419. }
  4420. startIndex += Limit;
  4421. insertText.Length = StartInsertText.Length;
  4422. }
  4423. }
  4424. /// <inheritdoc />
  4425. public void UpdatePeople(Guid itemId, List<PersonInfo> people)
  4426. {
  4427. if (itemId.IsEmpty())
  4428. {
  4429. throw new ArgumentNullException(nameof(itemId));
  4430. }
  4431. CheckDisposed();
  4432. using var connection = GetConnection();
  4433. using var transaction = connection.BeginTransaction();
  4434. // Delete all existing people first
  4435. using var command = connection.CreateCommand();
  4436. command.CommandText = "delete from People where ItemId=@ItemId";
  4437. command.TryBind("@ItemId", itemId);
  4438. command.ExecuteNonQuery();
  4439. if (people is not null)
  4440. {
  4441. InsertPeople(itemId, people, connection);
  4442. }
  4443. transaction.Commit();
  4444. }
  4445. private void InsertPeople(Guid id, List<PersonInfo> people, ManagedConnection db)
  4446. {
  4447. const int Limit = 100;
  4448. var startIndex = 0;
  4449. var listIndex = 0;
  4450. const string StartInsertText = "insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values ";
  4451. var insertText = new StringBuilder(StartInsertText);
  4452. while (startIndex < people.Count)
  4453. {
  4454. var endIndex = Math.Min(people.Count, startIndex + Limit);
  4455. for (var i = startIndex; i < endIndex; i++)
  4456. {
  4457. insertText.AppendFormat(
  4458. CultureInfo.InvariantCulture,
  4459. "(@ItemId, @Name{0}, @Role{0}, @PersonType{0}, @SortOrder{0}, @ListOrder{0}),",
  4460. i.ToString(CultureInfo.InvariantCulture));
  4461. }
  4462. // Remove trailing comma
  4463. insertText.Length--;
  4464. using (var statement = PrepareStatement(db, insertText.ToString()))
  4465. {
  4466. statement.TryBind("@ItemId", id);
  4467. for (var i = startIndex; i < endIndex; i++)
  4468. {
  4469. var index = i.ToString(CultureInfo.InvariantCulture);
  4470. var person = people[i];
  4471. statement.TryBind("@Name" + index, person.Name);
  4472. statement.TryBind("@Role" + index, person.Role);
  4473. statement.TryBind("@PersonType" + index, person.Type.ToString());
  4474. statement.TryBind("@SortOrder" + index, person.SortOrder);
  4475. statement.TryBind("@ListOrder" + index, listIndex);
  4476. listIndex++;
  4477. }
  4478. statement.ExecuteNonQuery();
  4479. }
  4480. startIndex += Limit;
  4481. insertText.Length = StartInsertText.Length;
  4482. }
  4483. }
  4484. private PersonInfo GetPerson(SqliteDataReader reader)
  4485. {
  4486. var item = new PersonInfo
  4487. {
  4488. ItemId = reader.GetGuid(0),
  4489. Name = reader.GetString(1)
  4490. };
  4491. if (reader.TryGetString(2, out var role))
  4492. {
  4493. item.Role = role;
  4494. }
  4495. if (reader.TryGetString(3, out var type)
  4496. && Enum.TryParse(type, true, out PersonKind personKind))
  4497. {
  4498. item.Type = personKind;
  4499. }
  4500. if (reader.TryGetInt32(4, out var sortOrder))
  4501. {
  4502. item.SortOrder = sortOrder;
  4503. }
  4504. return item;
  4505. }
  4506. /// <inheritdoc />
  4507. public List<MediaStream> GetMediaStreams(MediaStreamQuery query)
  4508. {
  4509. CheckDisposed();
  4510. ArgumentNullException.ThrowIfNull(query);
  4511. var cmdText = _mediaStreamSaveColumnsSelectQuery;
  4512. if (query.Type.HasValue)
  4513. {
  4514. cmdText += " AND StreamType=@StreamType";
  4515. }
  4516. if (query.Index.HasValue)
  4517. {
  4518. cmdText += " AND StreamIndex=@StreamIndex";
  4519. }
  4520. cmdText += " order by StreamIndex ASC";
  4521. using (var connection = GetConnection(true))
  4522. {
  4523. var list = new List<MediaStream>();
  4524. using (var statement = PrepareStatement(connection, cmdText))
  4525. {
  4526. statement.TryBind("@ItemId", query.ItemId);
  4527. if (query.Type.HasValue)
  4528. {
  4529. statement.TryBind("@StreamType", query.Type.Value.ToString());
  4530. }
  4531. if (query.Index.HasValue)
  4532. {
  4533. statement.TryBind("@StreamIndex", query.Index.Value);
  4534. }
  4535. foreach (var row in statement.ExecuteQuery())
  4536. {
  4537. list.Add(GetMediaStream(row));
  4538. }
  4539. }
  4540. return list;
  4541. }
  4542. }
  4543. /// <inheritdoc />
  4544. public void SaveMediaStreams(Guid id, IReadOnlyList<MediaStream> streams, CancellationToken cancellationToken)
  4545. {
  4546. CheckDisposed();
  4547. if (id.IsEmpty())
  4548. {
  4549. throw new ArgumentNullException(nameof(id));
  4550. }
  4551. ArgumentNullException.ThrowIfNull(streams);
  4552. cancellationToken.ThrowIfCancellationRequested();
  4553. using var connection = GetConnection();
  4554. using var transaction = connection.BeginTransaction();
  4555. // Delete existing mediastreams
  4556. using var command = connection.PrepareStatement("delete from mediastreams where ItemId=@ItemId");
  4557. command.TryBind("@ItemId", id);
  4558. command.ExecuteNonQuery();
  4559. InsertMediaStreams(id, streams, connection);
  4560. transaction.Commit();
  4561. }
  4562. private void InsertMediaStreams(Guid id, IReadOnlyList<MediaStream> streams, ManagedConnection db)
  4563. {
  4564. const int Limit = 10;
  4565. var startIndex = 0;
  4566. var insertText = new StringBuilder(_mediaStreamSaveColumnsInsertQuery);
  4567. while (startIndex < streams.Count)
  4568. {
  4569. var endIndex = Math.Min(streams.Count, startIndex + Limit);
  4570. for (var i = startIndex; i < endIndex; i++)
  4571. {
  4572. if (i != startIndex)
  4573. {
  4574. insertText.Append(',');
  4575. }
  4576. var index = i.ToString(CultureInfo.InvariantCulture);
  4577. insertText.Append("(@ItemId, ");
  4578. foreach (var column in _mediaStreamSaveColumns.Skip(1))
  4579. {
  4580. insertText.Append('@').Append(column).Append(index).Append(',');
  4581. }
  4582. insertText.Length -= 1; // Remove the last comma
  4583. insertText.Append(')');
  4584. }
  4585. using (var statement = PrepareStatement(db, insertText.ToString()))
  4586. {
  4587. statement.TryBind("@ItemId", id);
  4588. for (var i = startIndex; i < endIndex; i++)
  4589. {
  4590. var index = i.ToString(CultureInfo.InvariantCulture);
  4591. var stream = streams[i];
  4592. statement.TryBind("@StreamIndex" + index, stream.Index);
  4593. statement.TryBind("@StreamType" + index, stream.Type.ToString());
  4594. statement.TryBind("@Codec" + index, stream.Codec);
  4595. statement.TryBind("@Language" + index, stream.Language);
  4596. statement.TryBind("@ChannelLayout" + index, stream.ChannelLayout);
  4597. statement.TryBind("@Profile" + index, stream.Profile);
  4598. statement.TryBind("@AspectRatio" + index, stream.AspectRatio);
  4599. statement.TryBind("@Path" + index, GetPathToSave(stream.Path));
  4600. statement.TryBind("@IsInterlaced" + index, stream.IsInterlaced);
  4601. statement.TryBind("@BitRate" + index, stream.BitRate);
  4602. statement.TryBind("@Channels" + index, stream.Channels);
  4603. statement.TryBind("@SampleRate" + index, stream.SampleRate);
  4604. statement.TryBind("@IsDefault" + index, stream.IsDefault);
  4605. statement.TryBind("@IsForced" + index, stream.IsForced);
  4606. statement.TryBind("@IsExternal" + index, stream.IsExternal);
  4607. // Yes these are backwards due to a mistake
  4608. statement.TryBind("@Width" + index, stream.Height);
  4609. statement.TryBind("@Height" + index, stream.Width);
  4610. statement.TryBind("@AverageFrameRate" + index, stream.AverageFrameRate);
  4611. statement.TryBind("@RealFrameRate" + index, stream.RealFrameRate);
  4612. statement.TryBind("@Level" + index, stream.Level);
  4613. statement.TryBind("@PixelFormat" + index, stream.PixelFormat);
  4614. statement.TryBind("@BitDepth" + index, stream.BitDepth);
  4615. statement.TryBind("@IsAnamorphic" + index, stream.IsAnamorphic);
  4616. statement.TryBind("@IsExternal" + index, stream.IsExternal);
  4617. statement.TryBind("@RefFrames" + index, stream.RefFrames);
  4618. statement.TryBind("@CodecTag" + index, stream.CodecTag);
  4619. statement.TryBind("@Comment" + index, stream.Comment);
  4620. statement.TryBind("@NalLengthSize" + index, stream.NalLengthSize);
  4621. statement.TryBind("@IsAvc" + index, stream.IsAVC);
  4622. statement.TryBind("@Title" + index, stream.Title);
  4623. statement.TryBind("@TimeBase" + index, stream.TimeBase);
  4624. statement.TryBind("@CodecTimeBase" + index, stream.CodecTimeBase);
  4625. statement.TryBind("@ColorPrimaries" + index, stream.ColorPrimaries);
  4626. statement.TryBind("@ColorSpace" + index, stream.ColorSpace);
  4627. statement.TryBind("@ColorTransfer" + index, stream.ColorTransfer);
  4628. statement.TryBind("@DvVersionMajor" + index, stream.DvVersionMajor);
  4629. statement.TryBind("@DvVersionMinor" + index, stream.DvVersionMinor);
  4630. statement.TryBind("@DvProfile" + index, stream.DvProfile);
  4631. statement.TryBind("@DvLevel" + index, stream.DvLevel);
  4632. statement.TryBind("@RpuPresentFlag" + index, stream.RpuPresentFlag);
  4633. statement.TryBind("@ElPresentFlag" + index, stream.ElPresentFlag);
  4634. statement.TryBind("@BlPresentFlag" + index, stream.BlPresentFlag);
  4635. statement.TryBind("@DvBlSignalCompatibilityId" + index, stream.DvBlSignalCompatibilityId);
  4636. statement.TryBind("@IsHearingImpaired" + index, stream.IsHearingImpaired);
  4637. statement.TryBind("@Rotation" + index, stream.Rotation);
  4638. }
  4639. statement.ExecuteNonQuery();
  4640. }
  4641. startIndex += Limit;
  4642. insertText.Length = _mediaStreamSaveColumnsInsertQuery.Length;
  4643. }
  4644. }
  4645. /// <summary>
  4646. /// Gets the media stream.
  4647. /// </summary>
  4648. /// <param name="reader">The reader.</param>
  4649. /// <returns>MediaStream.</returns>
  4650. private MediaStream GetMediaStream(SqliteDataReader reader)
  4651. {
  4652. var item = new MediaStream
  4653. {
  4654. Index = reader.GetInt32(1),
  4655. Type = Enum.Parse<MediaStreamType>(reader.GetString(2), true)
  4656. };
  4657. if (reader.TryGetString(3, out var codec))
  4658. {
  4659. item.Codec = codec;
  4660. }
  4661. if (reader.TryGetString(4, out var language))
  4662. {
  4663. item.Language = language;
  4664. }
  4665. if (reader.TryGetString(5, out var channelLayout))
  4666. {
  4667. item.ChannelLayout = channelLayout;
  4668. }
  4669. if (reader.TryGetString(6, out var profile))
  4670. {
  4671. item.Profile = profile;
  4672. }
  4673. if (reader.TryGetString(7, out var aspectRatio))
  4674. {
  4675. item.AspectRatio = aspectRatio;
  4676. }
  4677. if (reader.TryGetString(8, out var path))
  4678. {
  4679. item.Path = RestorePath(path);
  4680. }
  4681. item.IsInterlaced = reader.GetBoolean(9);
  4682. if (reader.TryGetInt32(10, out var bitrate))
  4683. {
  4684. item.BitRate = bitrate;
  4685. }
  4686. if (reader.TryGetInt32(11, out var channels))
  4687. {
  4688. item.Channels = channels;
  4689. }
  4690. if (reader.TryGetInt32(12, out var sampleRate))
  4691. {
  4692. item.SampleRate = sampleRate;
  4693. }
  4694. item.IsDefault = reader.GetBoolean(13);
  4695. item.IsForced = reader.GetBoolean(14);
  4696. item.IsExternal = reader.GetBoolean(15);
  4697. if (reader.TryGetInt32(16, out var width))
  4698. {
  4699. item.Width = width;
  4700. }
  4701. if (reader.TryGetInt32(17, out var height))
  4702. {
  4703. item.Height = height;
  4704. }
  4705. if (reader.TryGetSingle(18, out var averageFrameRate))
  4706. {
  4707. item.AverageFrameRate = averageFrameRate;
  4708. }
  4709. if (reader.TryGetSingle(19, out var realFrameRate))
  4710. {
  4711. item.RealFrameRate = realFrameRate;
  4712. }
  4713. if (reader.TryGetSingle(20, out var level))
  4714. {
  4715. item.Level = level;
  4716. }
  4717. if (reader.TryGetString(21, out var pixelFormat))
  4718. {
  4719. item.PixelFormat = pixelFormat;
  4720. }
  4721. if (reader.TryGetInt32(22, out var bitDepth))
  4722. {
  4723. item.BitDepth = bitDepth;
  4724. }
  4725. if (reader.TryGetBoolean(23, out var isAnamorphic))
  4726. {
  4727. item.IsAnamorphic = isAnamorphic;
  4728. }
  4729. if (reader.TryGetInt32(24, out var refFrames))
  4730. {
  4731. item.RefFrames = refFrames;
  4732. }
  4733. if (reader.TryGetString(25, out var codecTag))
  4734. {
  4735. item.CodecTag = codecTag;
  4736. }
  4737. if (reader.TryGetString(26, out var comment))
  4738. {
  4739. item.Comment = comment;
  4740. }
  4741. if (reader.TryGetString(27, out var nalLengthSize))
  4742. {
  4743. item.NalLengthSize = nalLengthSize;
  4744. }
  4745. if (reader.TryGetBoolean(28, out var isAVC))
  4746. {
  4747. item.IsAVC = isAVC;
  4748. }
  4749. if (reader.TryGetString(29, out var title))
  4750. {
  4751. item.Title = title;
  4752. }
  4753. if (reader.TryGetString(30, out var timeBase))
  4754. {
  4755. item.TimeBase = timeBase;
  4756. }
  4757. if (reader.TryGetString(31, out var codecTimeBase))
  4758. {
  4759. item.CodecTimeBase = codecTimeBase;
  4760. }
  4761. if (reader.TryGetString(32, out var colorPrimaries))
  4762. {
  4763. item.ColorPrimaries = colorPrimaries;
  4764. }
  4765. if (reader.TryGetString(33, out var colorSpace))
  4766. {
  4767. item.ColorSpace = colorSpace;
  4768. }
  4769. if (reader.TryGetString(34, out var colorTransfer))
  4770. {
  4771. item.ColorTransfer = colorTransfer;
  4772. }
  4773. if (reader.TryGetInt32(35, out var dvVersionMajor))
  4774. {
  4775. item.DvVersionMajor = dvVersionMajor;
  4776. }
  4777. if (reader.TryGetInt32(36, out var dvVersionMinor))
  4778. {
  4779. item.DvVersionMinor = dvVersionMinor;
  4780. }
  4781. if (reader.TryGetInt32(37, out var dvProfile))
  4782. {
  4783. item.DvProfile = dvProfile;
  4784. }
  4785. if (reader.TryGetInt32(38, out var dvLevel))
  4786. {
  4787. item.DvLevel = dvLevel;
  4788. }
  4789. if (reader.TryGetInt32(39, out var rpuPresentFlag))
  4790. {
  4791. item.RpuPresentFlag = rpuPresentFlag;
  4792. }
  4793. if (reader.TryGetInt32(40, out var elPresentFlag))
  4794. {
  4795. item.ElPresentFlag = elPresentFlag;
  4796. }
  4797. if (reader.TryGetInt32(41, out var blPresentFlag))
  4798. {
  4799. item.BlPresentFlag = blPresentFlag;
  4800. }
  4801. if (reader.TryGetInt32(42, out var dvBlSignalCompatibilityId))
  4802. {
  4803. item.DvBlSignalCompatibilityId = dvBlSignalCompatibilityId;
  4804. }
  4805. item.IsHearingImpaired = reader.TryGetBoolean(43, out var result) && result;
  4806. if (reader.TryGetInt32(44, out var rotation))
  4807. {
  4808. item.Rotation = rotation;
  4809. }
  4810. if (item.Type is MediaStreamType.Audio or MediaStreamType.Subtitle)
  4811. {
  4812. item.LocalizedDefault = _localization.GetLocalizedString("Default");
  4813. item.LocalizedExternal = _localization.GetLocalizedString("External");
  4814. if (item.Type is MediaStreamType.Subtitle)
  4815. {
  4816. item.LocalizedUndefined = _localization.GetLocalizedString("Undefined");
  4817. item.LocalizedForced = _localization.GetLocalizedString("Forced");
  4818. item.LocalizedHearingImpaired = _localization.GetLocalizedString("HearingImpaired");
  4819. }
  4820. }
  4821. return item;
  4822. }
  4823. /// <inheritdoc />
  4824. public List<MediaAttachment> GetMediaAttachments(MediaAttachmentQuery query)
  4825. {
  4826. CheckDisposed();
  4827. ArgumentNullException.ThrowIfNull(query);
  4828. var cmdText = _mediaAttachmentSaveColumnsSelectQuery;
  4829. if (query.Index.HasValue)
  4830. {
  4831. cmdText += " AND AttachmentIndex=@AttachmentIndex";
  4832. }
  4833. cmdText += " order by AttachmentIndex ASC";
  4834. var list = new List<MediaAttachment>();
  4835. using (var connection = GetConnection(true))
  4836. using (var statement = PrepareStatement(connection, cmdText))
  4837. {
  4838. statement.TryBind("@ItemId", query.ItemId);
  4839. if (query.Index.HasValue)
  4840. {
  4841. statement.TryBind("@AttachmentIndex", query.Index.Value);
  4842. }
  4843. foreach (var row in statement.ExecuteQuery())
  4844. {
  4845. list.Add(GetMediaAttachment(row));
  4846. }
  4847. }
  4848. return list;
  4849. }
  4850. /// <inheritdoc />
  4851. public void SaveMediaAttachments(
  4852. Guid id,
  4853. IReadOnlyList<MediaAttachment> attachments,
  4854. CancellationToken cancellationToken)
  4855. {
  4856. CheckDisposed();
  4857. if (id.IsEmpty())
  4858. {
  4859. throw new ArgumentException("Guid can't be empty.", nameof(id));
  4860. }
  4861. ArgumentNullException.ThrowIfNull(attachments);
  4862. cancellationToken.ThrowIfCancellationRequested();
  4863. using (var connection = GetConnection())
  4864. using (var transaction = connection.BeginTransaction())
  4865. using (var command = connection.PrepareStatement("delete from mediaattachments where ItemId=@ItemId"))
  4866. {
  4867. command.TryBind("@ItemId", id);
  4868. command.ExecuteNonQuery();
  4869. InsertMediaAttachments(id, attachments, connection, cancellationToken);
  4870. transaction.Commit();
  4871. }
  4872. }
  4873. private void InsertMediaAttachments(
  4874. Guid id,
  4875. IReadOnlyList<MediaAttachment> attachments,
  4876. ManagedConnection db,
  4877. CancellationToken cancellationToken)
  4878. {
  4879. const int InsertAtOnce = 10;
  4880. var insertText = new StringBuilder(_mediaAttachmentInsertPrefix);
  4881. for (var startIndex = 0; startIndex < attachments.Count; startIndex += InsertAtOnce)
  4882. {
  4883. var endIndex = Math.Min(attachments.Count, startIndex + InsertAtOnce);
  4884. for (var i = startIndex; i < endIndex; i++)
  4885. {
  4886. insertText.Append("(@ItemId, ");
  4887. foreach (var column in _mediaAttachmentSaveColumns.Skip(1))
  4888. {
  4889. insertText.Append('@')
  4890. .Append(column)
  4891. .Append(i)
  4892. .Append(',');
  4893. }
  4894. insertText.Length -= 1;
  4895. insertText.Append("),");
  4896. }
  4897. insertText.Length--;
  4898. cancellationToken.ThrowIfCancellationRequested();
  4899. using (var statement = PrepareStatement(db, insertText.ToString()))
  4900. {
  4901. statement.TryBind("@ItemId", id);
  4902. for (var i = startIndex; i < endIndex; i++)
  4903. {
  4904. var index = i.ToString(CultureInfo.InvariantCulture);
  4905. var attachment = attachments[i];
  4906. statement.TryBind("@AttachmentIndex" + index, attachment.Index);
  4907. statement.TryBind("@Codec" + index, attachment.Codec);
  4908. statement.TryBind("@CodecTag" + index, attachment.CodecTag);
  4909. statement.TryBind("@Comment" + index, attachment.Comment);
  4910. statement.TryBind("@Filename" + index, attachment.FileName);
  4911. statement.TryBind("@MIMEType" + index, attachment.MimeType);
  4912. }
  4913. statement.ExecuteNonQuery();
  4914. }
  4915. insertText.Length = _mediaAttachmentInsertPrefix.Length;
  4916. }
  4917. }
  4918. /// <summary>
  4919. /// Gets the attachment.
  4920. /// </summary>
  4921. /// <param name="reader">The reader.</param>
  4922. /// <returns>MediaAttachment.</returns>
  4923. private MediaAttachment GetMediaAttachment(SqliteDataReader reader)
  4924. {
  4925. var item = new MediaAttachment
  4926. {
  4927. Index = reader.GetInt32(1)
  4928. };
  4929. if (reader.TryGetString(2, out var codec))
  4930. {
  4931. item.Codec = codec;
  4932. }
  4933. if (reader.TryGetString(3, out var codecTag))
  4934. {
  4935. item.CodecTag = codecTag;
  4936. }
  4937. if (reader.TryGetString(4, out var comment))
  4938. {
  4939. item.Comment = comment;
  4940. }
  4941. if (reader.TryGetString(5, out var fileName))
  4942. {
  4943. item.FileName = fileName;
  4944. }
  4945. if (reader.TryGetString(6, out var mimeType))
  4946. {
  4947. item.MimeType = mimeType;
  4948. }
  4949. return item;
  4950. }
  4951. private static string BuildMediaAttachmentInsertPrefix()
  4952. {
  4953. var queryPrefixText = new StringBuilder();
  4954. queryPrefixText.Append("insert into mediaattachments (");
  4955. foreach (var column in _mediaAttachmentSaveColumns)
  4956. {
  4957. queryPrefixText.Append(column)
  4958. .Append(',');
  4959. }
  4960. queryPrefixText.Length -= 1;
  4961. queryPrefixText.Append(") values ");
  4962. return queryPrefixText.ToString();
  4963. }
  4964. #nullable enable
  4965. private readonly struct QueryTimeLogger : IDisposable
  4966. {
  4967. private readonly ILogger _logger;
  4968. private readonly string _commandText;
  4969. private readonly string _methodName;
  4970. private readonly long _startTimestamp;
  4971. public QueryTimeLogger(ILogger logger, string commandText, [CallerMemberName] string methodName = "")
  4972. {
  4973. _logger = logger;
  4974. _commandText = commandText;
  4975. _methodName = methodName;
  4976. _startTimestamp = logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : -1;
  4977. }
  4978. public void Dispose()
  4979. {
  4980. if (_startTimestamp == -1)
  4981. {
  4982. return;
  4983. }
  4984. var elapsedMs = Stopwatch.GetElapsedTime(_startTimestamp).TotalMilliseconds;
  4985. #if DEBUG
  4986. const int SlowThreshold = 100;
  4987. #else
  4988. const int SlowThreshold = 10;
  4989. #endif
  4990. if (elapsedMs >= SlowThreshold)
  4991. {
  4992. _logger.LogDebug(
  4993. "{Method} query time (slow): {ElapsedMs}ms. Query: {Query}",
  4994. _methodName,
  4995. elapsedMs,
  4996. _commandText);
  4997. }
  4998. }
  4999. }
  5000. }
  5001. }