javascriptalasql

AlaSQL User Defined Functions with Parameters


I am new to 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);

I have a JSFiddle of the above here: https://jsfiddle.net/u4drqxaw/

The items I am having issues with are:

1) I realise that I am doing something wrong as my getCname function is being called twice because of my SELECT * FROM cnames WHERE getCname("www.fred.com.au")

In alaSQL, how can I just call my the function alasql.fn.getCname with parameter, i.e. something like:

var res = alasql('getCname("www.fred.com.au")');

2) Once I have the solution to my Q1, how do I reference the parameter passed into the function so that I can use it within the query, i.e. [domainName] ?

The end result is that I want to be able to call my user defined alaSQL function with the parameter of www.fred.com.au which in turn should just return me the one record from my cnames table for this cnames.domain value.


Solution

  • Managed to get this going and have updated my JSFiddle to reflect my required solution.

    See here:

    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);
    }
    

    JSFiddle solution