Db client

From Yate Documentation
Revision as of 15:23, 26 February 2014 by Monica (Talk | contribs)

Jump to: navigation, search

Setup

javascript.conf

[general]
scripts_dir=/your/dir/where/js/files/
....
[scripts]
init=init.js

If you use MySql edit mysqldb.conf

[general]
priority=100

[yate_my]
host=localhost
port=3306
database=database_name
user=database_user
password=yourpass123

If you use PostgreSql edit pgsqldb.conf

[general]
priority=100

[yate_pg]
host=localhost
port=5432
database=database_name
user=database_user
password=yourpass123


init.js

#require "db_client.class.js"

// db_client('use account name', 'database module, default pgsql');
var db = new db_client('yate_my', 'mysql');
// or 
var db = new db_client('yate_pg', 'pgsql');

db.query('INSERT INTO extension (extension,password) VALUES ($,$)', ['100','100']);


Methods

  1. query - return false or resource(true)
  2. query_insert - return false or last inser id
  3. get_affected_rows - return false or affected rows
  4. get_value_query - return false or first value
  5. get_array_list - return false or array
  6. getKeyValArray - return false or array
  7. get_one_line_assoc - return false or array
  8. get_assoc_column - return false or array
  9. get_assoc_column1 - return false or array

Examples

Example table users:


id fname lname group login pass
1 John Smit 0 test1 qwe
2 Tom Bink 1 test2 ytr
3 Mike Depp 1 test3 odi


query:

var q = db.query('UPDATE users SET pass=$ WHERE id=$', [pass, id]);
q == false or true;

query_insert:

var q = db.query_insert('INSERT INTO users (fname,lname,group,pass) VALUES($,$,$,$)', ['Grag','Sher','1','123']);
q == false or 4;

get_affected_rows:

var q = db.get_affected_rows('UPDATE users SET group=$ WHERE group=$', ['1','0']);
q == false or 2;

get_value_query:

var q = db.get_value_query('SELECT pass FROM users WHERE id=$', '1');
q == false or qwe;

get_array_list:

var q = db.get_array_list('SELECT fname, lname, group FROM users');
q == false or [
 {'fname'=>'John', 'lname'=>'Smit', 'group'=>'0'},
 {'fname'=>'Tom', 'lname'=>'Bink', 'group'=>'1'},
 {'fname'=>'Mike', 'lname'=>'Depp', 'group'=>'1'}
];

getKeyValArray:

var q = db.getKeyValArray('SELECT id, login FROM users');
q == false or {
 '1' : 'test1',
 '2' : 'test2',
 '3' : 'test3'
};

get_one_line_assoc:

var q = db.get_one_line_assoc('SELECT * FROM users WHERE id=$', '2');
q == false or {
 'id' : '2',
 'fname' : 'Tom',
 'lname' : 'Bink',
 'group' : '1',
 'login' : 'test2',
 'pass' : 'ytr'
};

get_assoc_column:

var q = db.get_assoc_column('SELECT login FROM users');
q == false or [
 'test1',
 'test2',
 'test3'
];

get_assoc_column1:
key object first value

var q = db.get_assoc_column1('SELECT * FROM users');
q == false or {
 '1' : {'fname':'John', 'lname':'Smit', 'group':'0', 'login':'test1', 'pass':'qwe'},
 '2' : {'fname':'Tom', 'lname':'Bink', 'group':'1', 'login':'test2', 'pass':'ytr'},
 '3' : {'fname':'Mike', 'lname':'Depp', 'group':'1', 'login':'test3', 'pass':'odi'}
};
var q = db.get_assoc_column1('SELECT login, fname, lname FROM users');
q == false or {
 'test1' : {'fname':'John', 'lname':'Smit'},
 'test2' : {'fname':'Tom', 'lname':'Bink'},
 'test3' : {'fname':'Mike', 'lname':'Depp'}
};
Personal tools
Namespaces

Variants
Actions
Preface
Configuration
Administrators
Developers