How to apply a function before comparison in an esqueleto query - database

For a query simple as that
runDb . select . from $ \cell -> do
where_ $ cell ^. CellCode ==. val "x"
return cell
I want to apply a function before the comparison of the field value with "x". The reason is that the cell code has trailing spaces in the database and nothing easier than trimming them away, e.g. with strip from Data.Text. However, my initial approach of using fmap (twice) resulted in
No Instance for (Functor SqlExpr)
I know that there are functions provides by Esqueleto, like just, that accomplish similar things specifically (I couldn't find the implementation of just, though).
Is there a way to apply any function on the packed value?
While writing: in my specific case, I just might want to use like.
EDIT: Added the specific function I want to apply.

What kind of function to you want to apply?
Here is how someone added the ability to call the chr() function in a query:
https://github.com/krisajenkins/esqueleto/commit/fa1d1c888770e297fef52d76b6cb68342a6c0376
If it is a built-in function (or a user-definable function), perhaps you can do something similar.

See here for a post that adds the postgresql function trim:
import Database.Esqueleto.Internal.Sql
trim :: (IsString s) => SqlExpr (Value s) -> SqlExpr (Value s) -> SqlExpr (Value s)
trim pattern target =
unsafeSqlFunction "trim" (unsafeSqlBinOp "FROM" pattern target)
(If you're not using postgres, you may need to consult the documentation from your database to find if it supports something similar.)
unsafeSqlFunction can be used to import any function your database supports, but it is unsafe because you have the responsibility to make sure the type signature is actually what your database expects. The name will be copied literally to your SQL.
unsafeSqlBinOp is similar, but it defines a binary operation: unsafeSqlBinOp "FROM" "a" "b" is translated into the SQL "a" FROM "b".
With this, you should be able to do:
runDb . select . from $ \cell -> do
where_ $ trim " " (cell ^. CellCode) ==. val "x"
return cell

Related

Compile error when trying to use jooq any operator in kotlin

I have problems using jooq with kotlin and the any clause.
Given the following:
I have a Database field in a postgreSQL database which is an array
I have search parameters which are a List of Strings
I want to use jooq any operator to search in the array
I have the following code which is not working:
fun findAll(
someArrayListOfStrings: List<String>?
): List<SomeDTO> {
val filters = ArrayList<Condition>()
filters.add(TABLE.SOME_FIELD.eq(DSL.any(someArrayListOfStrings)))
}
Here I want to dynamically create filters (jooq Conditions) to be added to some SQL statement. It should work to look if SOME_FIELD (PostgreSQL Array Type) contains one of the following strings using the ANY clause (PostgreSQL jooq binding). However I get the following compile-time error:
None of the following functions can be called with the arguments supplied:
public abstract fun eq(p0: Array<(out) String!>!): Condition defined in org.jooq.TableField
public abstract fun eq(p0: Field<Array<(out) String!>!>!): Condition defined in org.jooq.TableField
public abstract fun eq(p0: QuantifiedSelect<out Record1<Array<(out) String!>!>!>!): Condition defined in org.jooq.TableField
public abstract fun eq(p0: Select<out Record1<Array<(out) String!>!>!>!): Condition defined in org.jooq.TableField
But my function call should match the third type where QuantifiedSelect is used.
I looked for hours on the internet but was not able to find any solution. Any site I found told me to try the solution I already have. Does anyone have an idea what I could try and why it does not work?
Thank you!
The method you're calling here is DSL.any(T...), which takes a generic varargs array (in Java). You're passing a List<String>, so this binds T = List<String>, which doesn't satisfy the type constraint on the eq() method.
But even if you changed that to an Array<String>, it wouldn't work because the jOOQ ANY operator doesn't do the exact same thing as the PostgreSQL any(array) operator. So, just resort to either plain SQL templating:
condition("{0} = any({1})", TABLE.SOME_FIELD,
DSL.value(someArrayListOfStrings.toTypedArray()))
Or just use the IN predicate
TABLE.SOME_FIELD.in(someArrayListOfStrings)

fetch a discord link from a long text

૮₍ • ᴥ • ₎ა・Raiden ▬▭⋱𓂅
ᘏ⑅ᘏ╭╯Welcome╰╮𓂃ᘏᗢ
・・・・・・・・・・・・・・・・・・・・・
https://discord.gg/rsCC8y7WC4
・・・・・・・・・・・・・・・・・・・・・
Join!
・・・・・・・・・・・・・・・・・・・・・
How can I pull the "discord.gg/rsCC8y7WC4" link from a text like this
console.log(invitelink) ==> discord.gg/rsCC8y7WC4
Use String.match() for this. String.match accepts a regex argument which looks like this:
let str = 'hey there this is just a random string';
let res = str.match(/random/);
//res is now ['random']
Now for your problem, you are probably looking for this:
if(msg.content.match(/discord\.gg\/.+/) || msg.content.match(/discordapp\.com\/invite\/.+/)) return msg.channel.send('Hey! You put an invite in your message!');
Now that regex may look a bit messy/complicated but the \s are to escape the character and make sure regex knows that it’s not the special character it uses, and is actually just a character part of the search. To clarify why the above example should work, here’s a little explanation:
match() returns either an array (if it gets a match) or null (if it gets no match). You are searching for the strings 'discord.gg/' followed by any characters and also checking for the string 'discordapp.com/invite/' also followed by any characters.
If this doesn’t work, please tell me.

How to convert ASCII integers to string in Streambase?

I am using streambase, and have created a map to convert incoming integers (0-255) into character codes. Right now, I'm using manual if/else statements. For example:
if (message_code == '106') then
message_code = 'J'
else if (message_code == '110') then
message_code= 'N'
However, I'd like to just use this more generally. Searching Streambase Studio help didn't really provide anything as far as I could tell. I know this can be done in Java, but would probably require calling a java external function. I'm not so competent at this, so am a bit lost.
Is there a better way to do this in StreamBase?
Initial investigations confirm that the StreamBase Expression Language or built-in Functions don't have an obvious way to convert ints representing ASCII character values into a one-character string corresponding to the ASCII value. If there's some non-obvious trick I discover later, I'll come back and edit this answer.
I definitely wouldn't use a 255-clause if/then/else expression!
I would probably use a Java function and here is an example I've lightly tested:
package example;
public class ASCIIIntToString {
public static String ASCIIToString(Integer a) {
if (a == null || a < 0 || a > 255) {
return null;
} else {
return Character.toString((char) (int) a);
}
}
}
And the custom-function declaration to drop into your sbd.sbconf would look like this:
<custom-functions>
<custom-function alias="ASCIIToString"
args="auto"
class="example.ASCIIIntToString"
language="java"
name="ASCIIToString"
type="simple"/>
</custom-functions>
But if you would rather not drop into Java and stay in EventFlow then two methods come to mind:
Load a CSV file with int -> string mappings in them into a Query Table and dto a lookup with Query Read operator, perhaps using the Initial Contents tab to reference the CSV file since ASCII isn't going to change anytime soon.
Create a list constant called, say, ASCII, that has all the strings you want to convert to and index the list by the int. For example, in a Map operator do Add c ASCII[i], and add some bounds checking logic (perhaps in a user function).
Disclosure/Disclaimer: I am an employee of TIBCO Software, Inc. Opinions expressed here are my own and not TIBCO's.

Efficiently Creating Multiple Variables Using apply in R

I have a data frame DF which contains numerous variables. Each variable is present twice because I am conducting an analysis of "couples".
Among others, DF has a series of indicators of diversity :
DF$div1.1, DF$div2.1, .... , DF$divN.1, DF$div.1.2, ..., DF$divN.2
Similarly, it has a series of indicators of another characteristic:
DF$char1.1, DF$char2.1, .... , DF$charM.1, DF$char.1.2, ..., DF$charM.2
Here's a link to an example of DF: http://shorttext.com/5d90dd64
Each time the ".1", ".2" stand for the couple member considered.
My goal:
For each indicator divI and charJ, I want to create another variable DF$divchar that takes the value DF$divI.1 when DF$charJ.1>DF$charJ.2; and DF$divI.2 when DF$charJ.1<DF$charJ.2.
Here is the solution I came up with, it seems somehow very intricate and sometimes behaves in strange ways:
I created a series of binary variables that take the value one if DF$charJ.1>DF$charJ.2. The are stored under DF$CharMax.1.
Here's how I created it:
DF$CharMax.1 <- as.data.frame(
sapply(1:length(nam),
function(n)
as.numeric(DF[names(DF)==names.1[n]]
>DF[names(DF)==names.2[n]])
))
I created the function BinaryExtract:
BinaryExtract <- function(var1, var2, extract) {var1*extract +var2*(1-extract)}
I created the matrix NameFull that contains all the possible combinations of div and char, separated with "YY"
NameFull <- sapply(c("div1",...,"divN")
, function(nam) paste(nam, names(DF$YMax.1), sep="YY")
And then I create all my variables:
DF[, as.vector(NameFull)] <- lapply(as.vector(NameFull), function(e)
BinaryExtract(DF[,paste0(unlist(strsplit(e,"YY"))[1],".1")]
, DF[, paste0(unlist(strsplit(e,"YY"))[1],".1")]
, DF$charMax.1[unlist(strsplit(e,"YY"))[2]]))
My Problem
A. It looks like a very complicated solution for something that simple. What am I missing?
B. Moreover, when I print DF, just typing DF in the command window, I do not see the variables NameFull. They seem to appear with the names of char.
Here's what I get: http://shorttext.com/5d9102c
Similarly, I have tried to change all their names to get rid of the "YY" and it does not seem to work:
names(DF[, as.vector(NameFull)]) <- as.vector(c("div1",...,"divN"), sapply(, function(nam)
paste(nam, names(DF$YMax.1), sep=".")))
When I look at names(DF), I keep getting the old names with the "YY"
However, I do get a result if I explicitly call for them
> DF[,"divIYYcharJ"]
I would really appreciate any suggestion, comment and explanation. I am quite new to R ad was more used to Stata. I feel there is something deeply inefficient here. Thanks

Syntax highlighting of C function calls in emacs

I use a custom syntax highlighting theme for C in emacs, but I'm missing the possibility of highlighting function calls. For example:
int func(int foo)
{
return foo;
}
void main()
{
int bar = func(3);
}
Is there any way to highlight the call to "func" in this example?
It doesn't matter if macros are highlighted too. Keywords like if, switch or sizeof should not match.
Thanks!
The order of entries in the keyword list is significant. So if you put your entries after the ones that highlight keywords and function declarations, these won't be matched.
(font-lock-add-keywords 'c-mode
'(("\\(\\w+\\)\\s-*\("
(1 rumpsteak-font-lock-function-call-face)))
t)
Alternatively, you can use a function instead of a regexp as the MATCHER. Overkill for your question if you've stated your requirements exactly, but useful in harder cases. Untested (typed directly in the browser, in fact, so I don't even guarantee balanced parentheses).
(defun rumpsteak-match-function-call (&optional limit)
(while (and (search-forward-regexp "\\(\\w+\\)\\s-*\(" limit 'no-error)
(not (save-match-data
(string-match c-keywords-regexp (match-string 1))))
(not (save-excursion
(backward-char)
(forward-sexp)
(c-skip-whitespace-forward)
(or (eobp) (= ?\{ (char-after (point)))))))))
(font-lock-add-keywords 'c-mode
'((rumpsteak-match-function-call
(1 rumpsteak-font-lock-function-call-face))))
(font-lock-add-keywords 'c-mode
'(("\\<\\([a-zA-Z_]*\\) *(" 1 font-lock-keyword-face)))
in your .emacs. Replace font-lock-keyword-face by the one you want (M-X list-faces-display to get a list of predefined one).
You can try Ctrl-s for searching or Ctrl-r for searching backward. Emacs will highlight your function for you.

Resources