How to access a database from Yate's Javascript module

From Yate Documentation
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 +
Using the example below you will learn how to:
 +
 +
* retrieve information from database
 +
 
===Configure script in Javascript module===
 
===Configure script in Javascript module===
  
Line 14: Line 18:
  
  
This example uses Javascript to retrieve information from DB
+
====Example uses Javascript to retrieve information from DB====
  
 
  // '''Get DB Object'''
 
  // '''Get DB Object'''
Line 36: Line 40:
 
     Engine.output("Query failed");
 
     Engine.output("Query failed");
 
  }
 
  }
 +
 +
====Example with call interaction====
 +
 +
 +
// Get DB Object, Account, Query
 +
var m = new Message("database");
 +
m.account = "yateadmin";
 +
m.query = "SELECT test from test";
 +
 +
// Run the Query
 +
if (m.dispatch())
 +
{
 +
Engine.output("Query Ok");
 +
if (m.rows > 0)
 +
{
 +
Engine.output("result[0,0] = " + m.getResult(0,0));
 +
 +
// assuming m.getResult(0,0) returns "tone/busy" or "sip/sip:345@10.11.12.13"
 +
Channel.callJust(m.getResult(0,0));
 +
}
 +
}
 +
 +
 +
 +
'''See also'''
 +
 +
* [[How to do routing using javascript]]

Revision as of 12:21, 19 March 2013

Using the example below you will learn how to:

  • retrieve information from database

Contents

Configure script in Javascript module

To configure a script for Javascript routing it must be listed in the javascript.conf file:

[general]
routing=example.js


Configure database account

DB Example

Example uses Javascript to retrieve information from DB

// Get DB Object
var m = new Message("database");
// Specify connection to use
m.account = "yateadmin";
// Define Query
m.query = "SELECT * FROM lines WHERE location IS NOT NULL ORDER BY line LIMIT 5";
// Run the Query
if (m.dispatch()) {
    if (m.rows > 0) {
        Engine.output("Got " + m.rows + " records of " + m.columns + " fields");
        Engine.output("result[0,0] = " + m.getResult(0,0));
        var res = m.getColumn("location");
        for (var i = 0; i < res.length; i++) {
            Engine.output("location[" + i + "] = " + res[i]);
        }
    }
}
else {
    Engine.output("Query failed");
}

Example with call interaction

// Get DB Object, Account, Query var m = new Message("database"); m.account = "yateadmin"; m.query = "SELECT test from test";

// Run the Query if (m.dispatch()) { Engine.output("Query Ok"); if (m.rows > 0) { Engine.output("result[0,0] = " + m.getResult(0,0));

// assuming m.getResult(0,0) returns "tone/busy" or "sip/sip:345@10.11.12.13" Channel.callJust(m.getResult(0,0)); } }


See also

Personal tools
Namespaces

Variants
Actions
Preface
Configuration
Administrators
Developers