I have this snippet:
SELECT
CASE WHEN
AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600 >= 10
THEN
ROUND(AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600,0)
ELSE
ROUND(AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600,1)
END
FROM
...
Can I do anything to remove the duplication from this? Something along these lines, for instance: (Hypothetical code follows):
SET var = AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600
SELECT
CASE WHEN
var > 10
THEN
ROUND(var,0)
ELSE
ROUND(var,1)
END
FROM
...
With a subquery you can do something like this :
SELECT
CASE WHEN avgtiPN >= 10 THEN ROUND(avgtiPN,0) ELSE ROUND(avgtiPN,1) END
FROM
(SELECT
AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600 AS avgtiPN
FROM
...) AS AVGQuery
But I am still uncertain if it is more readable.
Yes, you can, but variable processing order is undefined for user-defined variables. This reference in the MySQL documentation explains when this works and when it doesnt.
Related
How to get rid of "the expression you entered exceeds the 1,024-character limit for the query design? As I would need to extract the huge data based on the query in the access below.
Please see my query below. Any help is appreciated.
SELECT dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE]) AS STARTDATE
FROM dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING, Input_BP
WHERE (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE)="BP1B") AND ((DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE])) Between #4/1/2021# And #4/30/2021#))
GROUP BY dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE])
HAVING (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140218" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140226" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140952" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300310001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300313005" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300352052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300469013" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300473072" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300482024" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582096" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582120" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300602001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300629020" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300654028" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300670016" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300761021" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300774099" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786051" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786069" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786085" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300843142" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300894517" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300897973" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300897981" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300964005" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300992725" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300992733" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300992741" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001294" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001302" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001310" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301001328" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301003407" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301003464" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301003472" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301010147" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301010154" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301030434" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301030558" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035045" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035060" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035144" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035151" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035169" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035276" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035284" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035300" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035318" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035326" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301035334" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301036027" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301040052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301047008" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049269" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049293" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049301" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049319" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301049343" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301064003" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301128006" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301176898" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301218849" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9301344538"));
Thanks
Using an alias for the table name and replacing OR col = val1 OR col = val1 OR col2 = val ... with col in (val1, val2, val3, ...) will save you a lot of characters. It will also make the code a lot easier to understand. I also removed the reference to Input_BP, which did not seem to be contributing anything except the possibility of a ton of duplicate results.
SELECT T.Year,
T.Month,
T.Day,
T.PERIOD,
T.CONFIGURATIONNAME,
T.CONNECTIONMEMBERNAME,
T.VALUE,
T.MSSLID,
T.PRICINGRUNGROUPCODE,
DateValue([T]![STARTDATE]) AS STARTDATE
FROM dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING As T
WHERE (((T.PRICINGRUNGROUPCODE)="BP1B")
AND ((DateValue([T]![STARTDATE])) Between #4/1/2021# And #4/30/2021#))
GROUP BY T.Year,
T.Month,
T.Day,
T.PERIOD,
T.CONFIGURATIONNAME,
T.CONNECTIONMEMBERNAME,
T.VALUE,
T.MSSLID,
T.PRICINGRUNGROUPCODE,
DateValue([T]![STARTDATE])
HAVING (T.MSSLID IN ("9300140218", ...));
If you want to be able to access the query design builder, I recommend copying and pasting maybe half of the repeated fields towards the bottom to a note/word file so you can delete some of the fields in SQL? And I'm not sure if this is for a report or a form, but try to use VBA to reduce the query; or maybe use the function Right() for the field MSSLID so you don't have to write the first "9300"?
(This is just a reduced version of your code; I'm not sure if you're making a report or form or if you just wanted to make a query)
SELECT dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE]) AS STARTDATE FROM dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING, Input_BP WHERE (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE)="BP1B") AND ((DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE])) Between #4/1/2021# And #4/30/2021#)) GROUP BY dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Year, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Month, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.Day, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PERIOD, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONFIGURATIONNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.CONNECTIONMEMBERNAME, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.VALUE, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID, dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.PRICINGRUNGROUPCODE, DateValue([dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING]![STARTDATE]) HAVING (((dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140218" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140226" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300140952" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300310001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300313005" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300352052" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300469013" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300473072" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300482024" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582096" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300582120" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300602001" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300629020" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300654028" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300670016" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300761021" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300774099" Or (dbo_plp_MCSEMVALCONSUMPTIONVALIDATIONVIEW_BILLING.MSSLID)="9300786051"));
Let me know if this helps!
Actually I don't want to check the else condition of below code and status 1 is compulsory only in the case of co_order_paymenttype_id is 1( both are in same table)
this is my code
DeliveryOrder::where('cb_order_type' , 0)->whereRaw('IF (`co_order_paymenttype_id` = 1,
`status` = 1,`status` = 0)')->paginate($page);
Ok as I commented above if you don't need else then it's not the case that you should use if statment.
you shoud use only where to get your needs and to make that in laravel you need to use advanced where
DeliveryOrder::where('cb_order_type' , 0)->where(function($query){
$query->where('co_order_paymenttype_id', 1)
->where('status' = 1);
})->paginate($page);
if you need to and any other condition you need to change it as you want, you can add another orWhere for expample.
please read the documentaion in the link above.
Using MySQL Latest Django:
I have a vaguely complex Django query that works quite quickly--until I add an additional "AND" with a Boolean Field--
See Below:
queriedForms = queryFormtype.form_set.filter(is_public=True)
newQuery = queriedForms.filter(formrecordattributevalue__record_value__icontains=term['TVAL'], formrecordattributevalue__record_attribute_type__pk=rtypePK)
newQuery = newQuery.filter(flagged_for_deletion=False)
logger.info(newQuery.query)
term['count'] = newQuery.count()
If I either remove the initial "is_public=True" or the final "flagged_for_deletion=False)--it works incredibly fast. If I use both as filters, it increases the time for the count() function by something like 2000%
The different QuerySet.query outputs are below:
SELECT `maqluengine_form`.`id`, `maqluengine_form`.`form_name`, `maqluengine_form`.`form_number`, `maqluengine_form`.`form_geojson_string`, `maqluengine_form`.`hierarchy_parent_id`, `maqluengine_form`.`is_public`, `maqluengine_form`.`project_id`, `maqluengine_form`.`date_created`, `maqluengine_form`.`created_by_id`, `maqluengine_form`.`date_last_modified`, `maqluengine_form`.`modified_by_id`, `maqluengine_form`.`sort_index`, `maqluengine_form`.`form_type_id`, `maqluengine_form`.`flagged_for_deletion` FROM `maqluengine_form` INNER JOIN `maqluengine_formrecordattributevalue` ON (`maqluengine_form`.`id` = `maqluengine_formrecordattributevalue`.`form_parent_id`) WHERE (`maqluengine_form`.`form_type_id` = 319 AND `maqluengine_form`.`is_public` = True AND `maqluengine_formrecordattributevalue`.`record_value` LIKE %seal% AND `maqluengine_formrecordattributevalue`.`record_attribute_type_id` = 18510 AND `maqluengine_form`.`flagged_for_deletion` = False)
SELECT `maqluengine_form`.`id`, `maqluengine_form`.`form_name`, `maqluengine_form`.`form_number`, `maqluengine_form`.`form_geojson_string`, `maqluengine_form`.`hierarchy_parent_id`, `maqluengine_form`.`is_public`, `maqluengine_form`.`project_id`, `maqluengine_form`.`date_created`, `maqluengine_form`.`created_by_id`, `maqluengine_form`.`date_last_modified`, `maqluengine_form`.`modified_by_id`, `maqluengine_form`.`sort_index`, `maqluengine_form`.`form_type_id`, `maqluengine_form`.`flagged_for_deletion` FROM `maqluengine_form` INNER JOIN `maqluengine_formrecordattributevalue` ON (`maqluengine_form`.`id` = `maqluengine_formrecordattributevalue`.`form_parent_id`) WHERE (`maqluengine_form`.`form_type_id` = 319 AND `maqluengine_form`.`is_public` = True AND `maqluengine_formrecordattributevalue`.`record_value` LIKE %seal% AND `maqluengine_formrecordattributevalue`.`record_attribute_type_id` = 18510)
The first takes about 20/30 seconds to perform the count(), while the second with only 1 of the two BooleanField's takes less than a second to perform the count()
=======================================
EDIT=======================
Apologies: since the question isn't obvious enough--why is adding an additional AND with a BooleanField increasing the query time by +2000%? Is anyone able to assist in figuring out WHY that's occurring. Thanks.
EDIT=========================
Also discovered that using a exclude(is_public=False) rather than filter(is_public=True) has the same effect as the solution below. Does anyone happen to know why an exclude() works fine--whereas the filter() does not?
==============================
Solution I came up with after a night's rest:
--I keep the query as is(I need it for later because it continues getting chain filtered)
--I need the count() from this stage--which is taking substantially longer than it should with the additional BooleanField AND
--I take a temporary values list to perform a len() on instead:
queriedForms = queryFormtype.form_set.all()
newQuery = queriedForms.filter(formrecordattributevalue__record_value__icontains=term['TVAL'], formrecordattributevalue__record_attribute_type__pk=rtypePK)
newQuery = newQuery.filter(flagged_for_deletion=False)
tempQuery = newQuery.values_list('is_public',flat=True)
finalQuery = [entry for entry in tempQuery if entry != 'False'] #Remove any indices that contain "False"
term['count'] = len(finalQuery)
The following counts that use chained filters after use the same technique--it's significantly faster--if not as fast as removing one of the Booleans from the filters.
I'm new to Lua, so (naturally) I got stuck at the first thing I tried to program. I'm working with an example script provided with the Corona Developer package. Here's a simplified version of the function (irrelevant material removed) I'm trying to call:
function new( imageSet, slideBackground, top, bottom )
function g:jumpToImage(num)
print(num)
local i = 0
print("jumpToImage")
print("#images", #images)
for i = 1, #images do
if i < num then
images[i].x = -screenW*.5;
elseif i > num then
images[i].x = screenW*1.5 + pad
else
images[i].x = screenW*.5 - pad
end
end
imgNum = num
initImage(imgNum)
end
end
If I try to call that function like this:
local test = slideView.new( myImages )
test.jumpToImage(2)
I get this error:
attempt to compare number with nil
at line 225. It would seem that "num" is not getting passed into the function. Why is this?
Where are you declaring g? You're adding a method to g, which doesn't exist (as a local). Then you're never returning g either. But most likely those were just copying errors or something. The real error is probably the notation that you're using to call test:jumpToImage.
You declare g:jumpToImage(num). That colon there means that the first argument should be treated as self. So really, your function is g.jumpToImage(self, num)
Later, you call it as test.jumpToImage(2). That makes the actual arguments of self be 2 and num be nil. What you want to do is test:jumpToImage(2). The colon there makes the expression expand to test.jumpToImage(test, 2)
Take a look at this page for an explanation of Lua's : syntax.
We have some often-hit code in our app that increments a column, like so:
if (r = customer.find_or_generate_reminder)
r.counter += 1
r.save!
end
We're getting lock wait timeouts, so I'm thinking about making this an atomic operation. Naively, what I want to do looks like this:
if (r = customer.find_or_generate_reminder)
connection.excute('UPDATE customer_reminders SET counter=counter+1, updated_at=now() WHERE id = ' + r.id)
end
Is there a ruby-world way of doing the same thing?
You can use the class method increment_counter:
Customer.increment_counter :counter, customer
That'll create something like:
UPDATE `customers` SET `counter` = COALESCE(`counter`, 0) + 1 WHERE (`customers`.`id` = 53)
(you have to pass either an id or an instance of the class into this method (customer) unlike the customer.increment!(:counter) method which is not atomic)
I created a gem to make this more object oriented:
https://github.com/imme5150/rails_atomic_increment
Allows you to do:
customer.atomic_increment!( :counter )
Let me know what you think.
If you don't mind a little bit of SQL:
CustomerReminder.where(id: id).update_all('counter = counter + 1, updated_at = now()')