|
Zhiroc Adept
Joined: 04 Feb 2005 Posts: 246
|
Posted: Fri Jan 11, 2008 5:26 pm
Suggestion: add default value to %db() function |
I find myself often doing:
Code: |
#IF (%iskey(@x, k)) {$val = %db(@x, k)} {$val = "default"} |
or something similar. It would save time (both CPU and coding) as well as being clearer (I think) to have a default value option, much like NVL() in most SQL languages:
Code: |
$val = %db(@x, k, "default") |
|
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Jan 11, 2008 6:01 pm |
Nice idea. I'll add that to the wish list.
|
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Fri Jan 11, 2008 6:20 pm |
That's a great idea. It'll probably get put in during the Database re-write.
|
|
_________________ Asati di tempari! |
|
|
|
ReedN Wizard
Joined: 04 Jan 2006 Posts: 1279 Location: Portland, Oregon
|
Posted: Fri Jan 11, 2008 6:22 pm |
I like that idea Zhiroc, I do that a lot too.
On a similar note, when doing:
$val = %db(@x, k)
I have always assumed I need to do a %iskey(@x, k) before trying to access it. Is %db(@x, k) going to return a consistant %null if "k" isn't defined in @x? That'd save me some code if I could always assume that trying to access a key that isn't there returned a known quantity. |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Fri Jan 11, 2008 7:10 pm |
Could just use Lua instead
val = var[key] or "default" |
;P |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Jan 11, 2008 8:20 pm |
Quote: |
Is %db(@x, k) going to return a consistant %null if "k" isn't defined in @x? |
Yes, it should. |
|
|
|
Zhiroc Adept
Joined: 04 Feb 2005 Posts: 246
|
Posted: Fri Jan 11, 2008 8:26 pm |
ReedN wrote: |
I have always assumed I need to do a %iskey(@x, k) before trying to access it. Is %db(@x, k) going to return a consistant %null if "k" isn't defined in @x? That'd save me some code if I could always assume that trying to access a key that isn't there returned a known quantity. |
I believe you can count on %null(%db(...)) returning true if the key doesn't exit. However, it also returns true if the key is present, but has a blank value. Sometimes you can have code that wants to tell the difference, and in that case, using %iskey() is the only way. |
|
|
|
|
|