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QAWE5VQC OE75DBM36Z3EXJ7XPQVSSBEIT4UR275RP43URXF4OOFSXKEYTA4AC I4MTA4E4GTMWJ4NC42QRWV55JRZ2K2W7SDSH5GAW2N4MIAQWS3DQC WYTMZJFYVKHR4QH7AV5JUNWXT6NAC5NNQNPZCQSDI6LGI7DVXFYAC X4XDT5S7ISHUZAJ4TK2FBEV55NY6CCUVZL2LN7DTBQ7AJCP5P3QAC create unique index upi_product_3p on product (producer, joined_name, type) where brand is null;create unique index upi_product_3b on product (brand, joined_name, type) where producer is null;create unique index upi_product_4 on product (producer, brand, joined_name, type) where brand is not null and producer is not null;
create unique index upi_product_3 on product (brand, joined_name, type) where producer is null;create unique index upi_product_4 on product (producer, brand, joined_name, type) where producer is not null;
$$ language sql;create or replace function thc2tc(thc numrange, cbd numrange) returns numeric as $$select ((lower(thc) + upper(thc)) / 2) / (((lower(thc) + upper(thc)) / 2) + ((lower(cbd) + upper(cbd)) / 2))$$ language sql;
$$ language sql stable;create or replace function avg_range(numrange) returns numeric as $$select (lower($1) + upper($1)) / 2$$ language sql immutable;--idk if immutable actually helps or makes sense(documentation is a little unclear. is argument a value or a reference to a row's column??)create or replace function thc2tc(thc numrange, cbd numrange) returns numeric as $$--how does nested inlining work?? stackoverflow.com/q/53040170. time to raise notice or case when pg_sleep(.01) is null then 2 else actual end.... yay fun. constant folding? can't explain analyze functions, so leave as separate exercise dba.stackexchange.com/q/84414select avg_range(thc) / (avg_range(thc) + avg_range(cbd))$$ language sql immutable;
_store_id integer = null,--_user_id integer = null,_species species = null,_effect effect = null,--not working yet_strain_id integer = null,_type product_type = null,in_stock boolean = null,
_store_id integer = null,--soon to be array? for geolocation sort_species species[] = null,--how about default to empty array?_effects effect[] = null,_strain_ids integer[] = null,_types product_type[] = null,frequency text = 'daily',in_stock boolean = null,--true in stock only. false oos only. null everything
sort_thc boolean = null,--not working yetsort_cbd boolean = null,--not working yet
sort_thc boolean = null,--in frontend, warn user that the sort does not make sense if types from more than one method of consumption(inhalation, sublingual, ingestion) are included in filtersort_cbd boolean = null,--ditto
search text = null
search text = null,--_user_id integer = null, cte vs plpgsql variable?. will replace below parameters once user stuff is doneakt1 akt1 = null,comt comt = null,cyp2c19_2 cyp2c19_2 = null,cyp2c19_3 cyp2c19_3 = null,cyp2c19_17 cyp2c19_17 = null,cyp2c9 cyp2c9 = null
(strain_id = _strain_id or _strain_id is null) and(type = _type or _type is null) and(species = _species or _species is null)
(strain_id = any(_strain_ids) or _strain_ids is null) and(type = any(_types) or _types is null) and(species = any(_species) or _species is null) and casewhen akt1 in ('C/T', 'C/C') then thc2tc(thc, cbd) < .6else trueend and casewhen comt in ('Val/Val', 'Val/Met') thenthc2tc(thc, cbd) < .75 orterpenes[1] ilike '%pinene' orterpenes[2] ilike '%pinene'else trueend and case--cbdwhen--slow(cyp2c19_17 = '*1/*1' or cyp2c19_17 = '*1/*17') and((cyp2c19_2 = '*1/*1' and cyp2c19_3 = '*1/*3') or (cyp2c19_2 = '*1/*2' and cyp2c19_3 = '*1/*1'))thentype in ('flower', 'preroll', 'cartridge', 'disposable') orthc2tc(thc, cbd) > .3 oravg_range(thc) < case--76. need to fixwhen type = 'oil' then 20when type = 'spray' then 2when type in ('capsule', 'tea', 'gummy', 'mint', 'chocolate', 'baked') then 5 / firstendwhen--very slowcyp2c19_17 = '*1/*1' and (cyp2c19_2 = '*1/*1' and cyp2c19_3 = '*3/*3' orcyp2c19_2 = '*1/*2' and cyp2c19_3 = '*1/*3' orcyp2c19_2 = '*2/*2' and cyp2c19_3 = '*1/*1')thentype in ('flower', 'preroll', 'cartridge', 'disposable') orthc2tc(thc, cbd) > .3 oravg_range(thc) < case--20. need to fixwhen type = 'oil' then 20when type = 'spray' then 2when type in ('capsule', 'tea', 'gummy', 'mint', 'chocolate', 'baked') then 5 / firstendelse trueend and casewhen (frequency = 'daily' and (cyp2c9 is null or cyp2c9 in ('inconclusive', '*1/*1'))) or type in ('oil', 'spray') then trueelse avg_range(thc) < casewhen type in ('flower', 'preroll') then casewhen frequency = 'daily' then casewhen cyp2c9 = '*1/*3' then 240when cyp2c9 = '*3/*3' then 200endwhen frequency = 'weekly' then casewhen cyp2c9 = '*1/*3' then 215when cyp2c9 = '*3/*3' then 165else 365endwhen frequency = 'monthly' then casewhen cyp2c9 = '*1/*3' then 70when cyp2c9 = '*3/*3' then 40else 130endelse case--'rarely'when cyp2c9 = '*1/*3' then 50when cyp2c9 = '*3/*3' then 25else 100endendwhen type in ('cartridge', 'disposable') then casewhen frequency = 'daily' then casewhen cyp2c9 = '*1/*3' then 240--fixmewhen cyp2c9 = '*3/*3' then 200--fixmeendwhen frequency = 'weekly' then casewhen cyp2c9 = '*1/*3' then 215--fixmewhen cyp2c9 = '*3/*3' then 165--fixmeelse 365endwhen frequency = 'monthly' then casewhen cyp2c9 = '*1/*3' then 70--fixmewhen cyp2c9 = '*3/*3' then 40--fixmeelse 130endelse case--'rarely'when cyp2c9 = '*1/*3' then 50--fixmewhen cyp2c9 = '*3/*3' then 25--fixmeelse 100endendwhen type in ('capsule', 'tea', 'gummy', 'mint', 'chocolate', 'baked') then casewhen frequency = 'daily' then case--need to divide by first, but capsule breaks it...when cyp2c9 = '*1/*3' then 7.5when cyp2c9 = '*3/*3' then 5endwhen frequency = 'weekly' then casewhen cyp2c9 = '*1/*3' then 5when cyp2c9 = '*3/*3' then 3else 7.5endwhen frequency = 'monthly' then casewhen cyp2c9 = '*1/*3' then 3when cyp2c9 = '*3/*3' then 2else 5endelse case--'rarely'when cyp2c9 = '*1/*3' then 2.4when cyp2c9 = '*3/*3' then 1.2else 3endendendend andcoalesce((select bool_or(casewhen x = 'sleep' then species = 'indica' and thc2tc(thc, cbd) >= .1when x = 'calm' then species in ('indica', 'hybrid', 'blend') and thc2tc(thc, cbd) <= .6when x = 'pain' then thc2tc(thc, cbd) between .1 and .8when x = 'happy' then species = 'sativa' or species = 'hybrid' and thc2tc(thc, cbd) >= .35when x = 'energetic' then species = 'sativa' and thc2tc(thc, cbd) >= .1when x = 'focused' then species = 'hybrid' and thc2tc(thc, cbd) between .25 and .7 or species = 'sativa' and avg_range(thc) <= casewhen type in ('flower', 'preroll') then 100when type in ('disposable', 'cartridge') then 550when type = 'oil' then 20when type = 'spray' then 2when type in ('capsule', 'tea', 'gummy', 'mint', 'chocolate', 'baked') then 5 / firstendend) from unnest(_effects) as t(x)), true)-- and--(search is null or word_similarity(concat_ws(' ', producer, brand, product.display_name), lower(search)) > 0.5)--0.5 is too high... maybe limit instead if present?
case when not sort_price then min end desc$$ language sql;
case when not sort_price then min end desc,casewhen cyp2c9 in ('*1/*3', '*3/*3') then casewhen type in ('flower', 'preroll', 'cartridge', 'disposable') then 1when type in ('oil', 'spray') then 2when type in ('capsule', 'tea', 'gummy', 'mint', 'chocolate', 'baked') then 3endend,casewhen'sleep' = any(_effects) and terpenes[1] ilike '%myrcene' or'calm' = any(_effects) and terpenes[1] ilike '%limonene' or'pain' = any(_effects) and terpenes[1] ilike '%caryo%lene' and terpenes[2] ilike '%humulene' or'focused' = any(_effects) and terpenes[1] ilike '%pinene' or'energetic' = any(_effects) and terpenes[1] ilike '%terpinolene' or'happy' = any(_effects) and terpenes[1] ilike '%linalool'then 1when'sleep' = any(_effects) and terpenes[2] ilike '%myrcene' or'calm' = any(_effects) and terpenes[1] ilike '%caryo%lene' or'pain' = any(_effects) and terpenes[1] ilike '%caryo%lene' or'focused' = any(_effects) and terpenes[2] ilike '%pinene' or'energetic' = any(_effects) and terpenes[2] ilike '%terpinolene' or'happy' = any(_effects) and terpenes[2] ilike '%linalool'then 2when'calm' = any(_effects) and terpenes[2] ilike '%limonene' or'pain' = any(_effects) and terpenes[2] ilike '%caryo%lene'then 3when'calm' = any(_effects) and terpenes[2] ilike '%caryo%lene'then 4when strain.effect = any(_effects) then 10else 11end$$ language sql stable;
round(@ ((lower(thc) + upper(thc)) / 2) - (select ((lower(thc) + upper(thc)) / 2) from p), -1) asc nulls last--this is wrong thc_mg, but temporary(take into account user pref for flower, recommend at variant level) but maybe doesn't matter since same type? need to fix if type becomes optional though
round(@ avg_range(thc) - (select avg_range(thc) from p), -1) asc nulls last--this is wrong thc_mg, but temporary(take into account user pref for flower, recommend at variant level) but maybe doesn't matter since same type? need to fix if type becomes optional though
let s = Number(parameters.store);if(Number.isInteger(s)) {qs += `_store_id => $${params.push(s)},`;
if((parameters.store = Number(parameters.store)) && Number.isInteger(parameters.store)) {qs += `_store_id => $${params.push(parameters.store)},`;
let st = Number(parameters.strain_id);if(Number.isInteger(st)) {qs += `_strain_id => $${params.push(st)},`;
if(Array.isArray(parameters.strain_ids) && (parameters.strain_ids = parameters.strain_ids.map(Number)).every(Number.isInteger)) {qs += `_strain_ids => $${params.push(parameters.strain_ids)},`;
if(typeof parameters.type === 'string') {qs += `_type => $${params.push(parameters.type)},`;
if(Array.isArray(parameters.types) && parameters.types.every(s => typeof s === 'string')) {qs += `_types => $${params.push(parameters.types)},`;
if(typeof parameters.frequency === 'string') {qs += `frequency => $${params.push(parameters.frequency)},`;}if(typeof parameters.akt1 === 'string') {qs += `akt1 => $${params.push(parameters.akt1)},`;}if(typeof parameters.comt === 'string') {qs += `comt => $${params.push(parameters.comt)},`;}if(typeof parameters.cyp2c9 === 'string') {qs += `cyp2c9 => $${params.push(parameters.cyp2c9)},`;}if(typeof parameters.cyp2c19_2 === 'string') {qs += `cyp2c19_2 => $${params.push(parameters.cyp2c19_2)},`;}if(typeof parameters.cyp2c19_3 === 'string') {qs += `cyp2c19_3 => $${params.push(parameters.cyp2c19_3)},`;}if(typeof parameters.cyp2c19_17 === 'string') {qs += `cyp2c19_17 => $${params.push(parameters.cyp2c19_17)},`;}
}if(Array.isArray(parameters.producers) && parameters.producers.every(p => typeof p === 'object' && typeof p.producer === 'string' && (Array.isArray(p.brands) || p.brands === undefined))) {wack = ` where ${parameters.producers.map(p => `producer = $${params.push(p.producer)}${Array.isArray(p.brands) ? ` and brand = any($${params.push(p.brands)})`: ''}`).join(' or ')}`;