Anyone have any insight into why the following results in false instead of true?
CREATE TEMPORARY FUNCTION test()
RETURNS BOOL
LANGUAGE js
AS
"""
const d = new Date();
return d instanceof Date;
""";
SELECT test();
Returns false (unexpected)
WORKAROUND:
CREATE TEMPORARY FUNCTION test()
RETURNS BOOL
LANGUAGE js
AS
"""
const d = new Date();
return Object.prototype.toString.call(d) === '[object Date]';
""";
SELECT test();
Returns true (as expected)
The instanceof Date seams not to work.
For other objects, like String it works fine.
There is a JavaScript workaround possible:
CREATE TEMPORARY FUNCTION test()
RETURNS BOOL
LANGUAGE js
AS
"""
var d = new Date();
var s= new String('String created with constructor');
//return s instanceof String;
return typeof d.getMonth === 'function';
""";
SELECT test();
Related
I want to know how to parse JSON if the key is dynamic, and how to define the path in such cases in bigquery?
PFA image with JSON format.
I am trying to get the endTime values in JSON.
I am stuck at response.data.menus. and not able to define path post menus as the key is dynamic.
Try below
create temp function get_keys(input string) returns array<string> language js as """
return Object.keys(JSON.parse(input));
""";
create temp function get_values(input string) returns array<string> language js as """
return Object.values(JSON.parse(input));
""";
create temp function get_leaves(input string) returns string language js as '''
function flattenObj(obj, parent = '', res = {}){
for(let key in obj){
let propName = parent ? parent + '.' + key : key;
if(typeof obj[key] == 'object'){
flattenObj(obj[key], propName, res);
} else {
res[propName] = obj[key];
}
}
return JSON.stringify(res);
}
return flattenObj(JSON.parse(input));
''';
select *
from (
select
keys[safe_offset(2)] as manu_id,
keys[safe_offset(6)] as regularHours,
keys[safe_offset(7)] as key,
val
from your_table, unnest([struct(get_leaves(response) as leaves)]),
unnest(get_keys(leaves)) key with offset
join unnest(get_values(leaves)) val with offset using(offset),
unnest([struct(split(key, '.') as keys)])
where ends_with(key, 'startTime')
or ends_with(key, 'endTime')
)
pivot (any_value(val) for key in ('startTime', 'endTime'))
if applied to sample response you provided in your question
output is
Note: I would not expected above to 100% fit your requirements/expectations - but at least it should give you good direction!
I am trying to execute an SQL statement at AWS QLDB like the example in the AWS SDK Git but using Kotlin. The example shows me that I can return something at "execute" (represented by "searchValue")
String searchValue = driver.execute(
txn -> {
Result result = txn.execute(searchQuery);
String value = "";
for (IonValue row : result) {
value = ((IonString) row).stringValue();
}
return value;
});
Based on the example, I've tried to receive the return in "executionReturn" and transform the values at "let" function but "executionReturn" cames as undefined.
val executionReturn = driver.execute { txn: TransactionExecutor ->
val result: Result = txn.execute(
"SELECT * FROM Table")
)
result
}
executionReturn.let {
list.plus(it as IonStruct)
}
How could I return a specific value from "driver.execute"?
Thanks for your interest in QLDB. Does something like the following help you?
val executionReturn = driver.execute<List<IonValue>> { txn: TransactionExecutor ->
val result = txn.execute(
"SELECT * FROM Table").toList()
result
}
executionReturn.let {
list.plus(it as IonStruct)
}
I've changed the approach of the collection. I've created a mutableListOf and then used the method "add"
Then it worked
contract KYCCHECK{
function KYCCHECK(){}
struct User{
uint id;
bool isVerified;
string fname;
string mname;
string lname;
string genderValue;
string maritalStat;
string stat;
string identity;
}
mapping(uint => User) kyclist;
function setKYCData(uint uid,string firstname,string middlename,string lastname,string gvalue,string maritalVal,string statusVal,string identityVal) {
kyclist[uid].fname = firstname;
kyclist[uid].mname = middlename;
kyclist[uid].lname = lastname;
kyclist[uid].genderValue = gvalue;
kyclist[uid].maritalStat = maritalVal;
kyclist[uid].stat = statusVal;
kyclist[uid].identity = identityVal;
}
function getFirstName(uint uid) constant returns (string retFNameVal) {
retFNameVal = kyclist[uid].fname;
return retFNameVal;
}
function getMiddleName(uint uid) constant returns (string retMNameVal) {
retMNameVal = kyclist[uid].mname;
return retMNameVal;
}
function getLastName(uint uid) constant returns (string retLNameVal) {
retLNameVal = kyclist[uid].lname;
return retLNameVal;
}
function getGender(uint uid) constant returns(string retGenderVal) {
retGenderVal = kyclist[uid].genderValue;
return retGenderVal;
}
function getMaritalStatus(uint uid) constant returns(string retMaritalVal){
retMaritalVal= kyclist[uid].maritalStat;
return retMaritalVal;
}
function getStatus(uint uid) constant returns(string retStatus){
retStatus = kyclist[uid].stat;
return retStatus;
}
function getIdentity(uint uid)constant returns(string retIdentity){
retIdentity = kyclist[uid].identity;
return retIdentity;
}
}
Hello everyone,
This the contract i am trying to implement.
I have a form(using bootstrap with form defined "") and i need to set and get the data through form.
Problem is when i pass more than 3 arguments in setKYCData function (in above contracts)and execute get function, the value isn't displayed.
but when set is modified to take 3 arguments and get function is called with 3 arguments it works fine.
please ask if you need any more details, if anybody can share a code to create a form using solidity, web3 that'll be appreciable.
thanks in adv
With linq I have to check if a value of a row is present in an array.
The equivalent of the sql query:
WHERE ID IN (2,3,4,5)
How can I do it?
.Contains
var resultset = from x in collection where new[] {2,3,4,5}.Contains(x) select x
Of course, with your simple problem, you could have something like:
var resultset = from x in collection where x >= 2 && x <= 5 select x
Perform the equivalent of an SQL IN with IEnumerable.Contains().
var idlist = new int[] { 2, 3, 4, 5 };
var result = from x in source
where idlist.Contains(x.Id)
select x;
db.SomeTable.Where(x => new[] {2,3,4,5}.Contains(x));
or
from x in db.SomeTable
where new[] {2,3,4,5}.Contains(x)
Intersect and Except are a little more concise and will probably be a bit faster too.
IN
collection.Intersect(new[] {2,3,4,5});
NOT IN
collection.Except(new[] {2,3,4,5});
or
Method syntax for IN
collection.Where(x => new[] {2,3,4,5}.Contains(x));
and NOT IN
collection.Where(x => !(new[] {2,3,4,5}.Contains(x)));
An IEnumerable<T>.Contains(T) statement should do what you're looking for.
A very basic example using .Contains()
List<int> list = new List<int>();
for (int k = 1; k < 10; k++)
{
list.Add(k);
}
int[] conditionList = new int[]{2,3,4};
var a = (from test in list
where conditionList.Contains(test)
select test);
The above situations work when the Contains function is used against primitives, but what if you are dealing with objects (e.g. myListOrArrayOfObjs.Contains(efObj))?
I found a solution! Convert your efObj into a string, thats separated by _ for each field (you can almost think of it as a CSV representation of your obj)
An example of such may look like this:
var reqAssetsDataStringRep = new List<string>();
foreach (var ra in onDemandQueueJobRequest.RequestedAssets)
{
reqAssetsDataStringRep.Add(ra.RequestedAssetId + "_" + ra.ImageId);
}
var requestedAssets = await (from reqAsset in DbContext.RequestedAssets
join image in DbContext.Images on reqAsset.ImageId equals image.Id
where reqAssetsDataStringRep.Contains(reqAsset.Id + "_" + image.Id)
select reqAsset
).ToListAsync();
You can write help-method:
public bool Contains(int x, params int[] set) {
return set.Contains(x);
}
and use short code:
var resultset = from x in collection
where Contains(x, 2, 3, 4, 5)
select x;
Following is a generic extension method that can be used to search a value within a list of values:
public static bool In<T>(this T searchValue, params T[] valuesToSearch)
{
if (valuesToSearch == null)
return false;
for (int i = 0; i < valuesToSearch.Length; i++)
if (searchValue.Equals(valuesToSearch[i]))
return true;
return false;
}
This can be used as:
int i = 5;
i.In(45, 44, 5, 234); // Returns true
string s = "test";
s.In("aa", "b", "c"); // Returns false
This is handy in conditional statements.
I'm wondering about JScript.NET private variables. Please take a look on the following code:
import System;
import System.Windows.Forms;
import System.Drawing;
var jsPDF = function(){
var state = 0;
var beginPage = function(){
state = 2;
out('beginPage');
}
var out = function(text){
if(state == 2){
var st = 3;
}
MessageBox.Show(text + ' ' + state);
}
var addHeader = function(){
out('header');
}
return {
endDocument: function(){
state = 1;
addHeader();
out('endDocument');
},
beginDocument: function(){
beginPage();
}
}
}
var j = new jsPDF();
j.beginDocument();
j.endDocument();
Output:
beginPage 2
header 2
endDocument 2
if I run the same script in any browser, the output is:
beginPage 2
header 1
endDocument 1
Why it is so??
Thanks,
Paul.
Just a guess, but it appears that JScript.NET doesn't support closures the same way as EMCAScript, so the state variable in endDocument() isn't referencing the private member of the outer function, but rather an local variable (undeclared). Odd.
You don't have to use new when calling jsPDF here since you're using a singleton pattern. jsPDF is returning an object literal so even without new you'll have access to the beginPage and endDocument methods. To be perfectly honest I don't know what the specifications call for when using new on a function that returns an object literal so I'm not sure if JScript.NET is getting it wrong or the browser. But for now try either getting rid of the new before jsPDF() or change your function to this:
var jsPDF = function(){
var state = 0;
var beginPage = function(){
state = 2;
out('beginPage');
};
var out = function(text){
if(state == 2){
var st = 3;
}
MessageBox.Show(text + ' ' + state);
};
var addHeader = function(){
out('header');
};
this.endDocument = function(){
state = 1;
addHeader();
out('endDocument');
};
this.beginDocument: function(){
beginPage();
};
}
That will allow you to use the new keyword and create more than one jsPDF object.
I've come across the same problem. In the following code, the closure bound to fun should contain only one variable called result. As the code stands, the variable result in the function with one parameter seems to be different to the result variable in the closure.
If in this function the line
result = [];
is removed, then the result in the line
return result;
refers to the result in the closure.
var fun = function() {
var result = [];
// recursive descent, collects property names of obj
// dummy parameter does nothing
var funAux = function(obj, pathToObj, dummy) {
if (typeof obj === "object") {
for (var propName in obj) {
if (obj.hasOwnProperty(propName)) {
funAux(obj[propName], pathToObj.concat(propName), dummy);
}
}
}
else {
// at leaf property, save path to leaf
result.push(pathToObj);
}
}
return function(obj) {
// remove line below and `result' 3 lines below is `result' in closure
result = []; // does not appear to be bound to `result' above
funAux(obj, [], "dummy");
return result; // if result 2 lines above is set, result is closure is a different variable
};
}();