带参数的 AlaSQL 用户定义函数
AlaSQL User Defined Functions with Parameters
我是 AlaSQL 新手:
alasql('CREATE TABLE cnames(domain STRING, cname STRING)');
alasql('CREATE TABLE domains(domain STRING, ip_addr STRING)');
alasql('INSERT INTO domains VALUES ("fred.com.au","192.168.0.1")');
alasql('INSERT INTO cnames VALUES ("www.fred.com.au","fred.com.au")');
alasql('INSERT INTO cnames VALUES ("mail.fred.com.au","www.fred.com.au")');
alasql.fn.getCname = function(domainName) {
console.log('domainName ', domainName);
return alasql('SELECT cname FROM cnames WHERE domain = ?',[domainName]);
}
var res = alasql('SELECT * FROM cnames WHERE getCname("www.fred.com.au")');
document.getElementById('res').textContent = JSON.stringify(res);
我这里有上面的 JSFiddle:https://jsfiddle.net/u4drqxaw/
我遇到问题的项目是:
1) 我意识到我做错了什么,因为我的 getCname
函数被调用了两次,因为我的 SELECT * FROM cnames WHERE getCname("www.fred.com.au")
在 alaSQL 中,我怎样才能调用带有参数的函数 alasql.fn.getCname
,例如:
var res = alasql('getCname("www.fred.com.au")');
2) 一旦我有了 Q1 的解决方案,我如何引用传递给函数的参数以便我可以在查询中使用它,即 [domainName]
?
最终结果是我希望能够使用参数 www.fred.com.au
调用我的用户定义的 alaSQL 函数,这反过来应该 return 我的 cnames 中的一条记录 table 对于此 cnames.domain 值。
设法做到这一点并更新了我的 JSFiddle 以反映我所需的解决方案。
看这里:
alasql('CREATE TABLE cnames(domain STRING, cname STRING)');
alasql('CREATE TABLE domains(domain STRING, ip_addr STRING)');
alasql('INSERT INTO domains VALUES ("fred.com.au","192.168.0.1")');
alasql('INSERT INTO domains VALUES ("barney.com.au","192.168.2.3")');
alasql('INSERT INTO domains VALUES ("mail1.fred.com.au","192.168.9.9")');
alasql('INSERT INTO cnames VALUES ("www.fred.com.au","fred.com.au")');
alasql('INSERT INTO cnames VALUES ("mail.fred.com.au","www.fred.com.au")');
alasql('INSERT INTO cnames VALUES ("fred.com.au","barney.com.au")');
alasql.fn.getCname = function(domainName) {
return alasql('SELECT cname FROM cnames WHERE domain = ?',[domainName]);
}
alasql.fn.processCname = function(cnameVal) {
return alasql('SELECT getCname(?) [cname]',[cnameVal]);
}
alasql.fn.domainInfo = function(theDomain, theCname) {
return alasql('SELECT domain, ip_addr, "'+theCname+'" [cname] FROM domains WHERE domain = ?',[theDomain]);
}
let rec = alasql('SELECT getCname("mail.fred.com.au") [cname]');
let initialLength = rec[0]["cname"].length;
if (initialLength > 0) {
let cnameValue = rec[0]["cname"][0].cname;
let found = true;
while (found) {
rec = alasql('SELECT getCname(?) [cname]',[cnameValue]);
let recLength = rec[0]["cname"].length;
if (recLength > 0) {
cnameValue = rec[0]["cname"][0].cname;
} else {
found = false;
}
}
let res = alasql('SELECT domainInfo(?,?) [result]',[cnameValue,cnameValue]);
document.getElementById('res').textContent = JSON.stringify(res);
} else {
let res = alasql('SELECT domainInfo("mail.fred.com.au", "") [result]');
document.getElementById('res').textContent = JSON.stringify(res);
}
我是 AlaSQL 新手:
alasql('CREATE TABLE cnames(domain STRING, cname STRING)');
alasql('CREATE TABLE domains(domain STRING, ip_addr STRING)');
alasql('INSERT INTO domains VALUES ("fred.com.au","192.168.0.1")');
alasql('INSERT INTO cnames VALUES ("www.fred.com.au","fred.com.au")');
alasql('INSERT INTO cnames VALUES ("mail.fred.com.au","www.fred.com.au")');
alasql.fn.getCname = function(domainName) {
console.log('domainName ', domainName);
return alasql('SELECT cname FROM cnames WHERE domain = ?',[domainName]);
}
var res = alasql('SELECT * FROM cnames WHERE getCname("www.fred.com.au")');
document.getElementById('res').textContent = JSON.stringify(res);
我这里有上面的 JSFiddle:https://jsfiddle.net/u4drqxaw/
我遇到问题的项目是:
1) 我意识到我做错了什么,因为我的 getCname
函数被调用了两次,因为我的 SELECT * FROM cnames WHERE getCname("www.fred.com.au")
在 alaSQL 中,我怎样才能调用带有参数的函数 alasql.fn.getCname
,例如:
var res = alasql('getCname("www.fred.com.au")');
2) 一旦我有了 Q1 的解决方案,我如何引用传递给函数的参数以便我可以在查询中使用它,即 [domainName]
?
最终结果是我希望能够使用参数 www.fred.com.au
调用我的用户定义的 alaSQL 函数,这反过来应该 return 我的 cnames 中的一条记录 table 对于此 cnames.domain 值。
设法做到这一点并更新了我的 JSFiddle 以反映我所需的解决方案。
看这里:
alasql('CREATE TABLE cnames(domain STRING, cname STRING)');
alasql('CREATE TABLE domains(domain STRING, ip_addr STRING)');
alasql('INSERT INTO domains VALUES ("fred.com.au","192.168.0.1")');
alasql('INSERT INTO domains VALUES ("barney.com.au","192.168.2.3")');
alasql('INSERT INTO domains VALUES ("mail1.fred.com.au","192.168.9.9")');
alasql('INSERT INTO cnames VALUES ("www.fred.com.au","fred.com.au")');
alasql('INSERT INTO cnames VALUES ("mail.fred.com.au","www.fred.com.au")');
alasql('INSERT INTO cnames VALUES ("fred.com.au","barney.com.au")');
alasql.fn.getCname = function(domainName) {
return alasql('SELECT cname FROM cnames WHERE domain = ?',[domainName]);
}
alasql.fn.processCname = function(cnameVal) {
return alasql('SELECT getCname(?) [cname]',[cnameVal]);
}
alasql.fn.domainInfo = function(theDomain, theCname) {
return alasql('SELECT domain, ip_addr, "'+theCname+'" [cname] FROM domains WHERE domain = ?',[theDomain]);
}
let rec = alasql('SELECT getCname("mail.fred.com.au") [cname]');
let initialLength = rec[0]["cname"].length;
if (initialLength > 0) {
let cnameValue = rec[0]["cname"][0].cname;
let found = true;
while (found) {
rec = alasql('SELECT getCname(?) [cname]',[cnameValue]);
let recLength = rec[0]["cname"].length;
if (recLength > 0) {
cnameValue = rec[0]["cname"][0].cname;
} else {
found = false;
}
}
let res = alasql('SELECT domainInfo(?,?) [result]',[cnameValue,cnameValue]);
document.getElementById('res').textContent = JSON.stringify(res);
} else {
let res = alasql('SELECT domainInfo("mail.fred.com.au", "") [result]');
document.getElementById('res').textContent = JSON.stringify(res);
}