Db client
From Yate Documentation
(Difference between revisions)
Line 33: | Line 33: | ||
init.js | init.js | ||
− | + | ||
#require "[[Db_client.class.js|db_client.class.js]]" | #require "[[Db_client.class.js|db_client.class.js]]" | ||
− | + | ||
// db_client('use account name', 'database module, default pgsql'); | // db_client('use account name', 'database module, default pgsql'); | ||
var db = new db_client('yate_my', 'mysql'); | var db = new db_client('yate_my', 'mysql'); | ||
− | or | + | // or |
var db = new db_client('yate_pg', 'pgsql'); | var db = new db_client('yate_pg', 'pgsql'); | ||
− | + | ||
db.query('INSERT INTO extension (extension,password) VALUES ($,$)', ['100','100']); | db.query('INSERT INTO extension (extension,password) VALUES ($,$)', ['100','100']); | ||
+ | |||
+ | |||
+ | Methods: | ||
+ | |||
+ | <ol> | ||
+ | <li><b>query</b> - return false or resource(true)</li> | ||
+ | <li><b>query_insert</b> - return false or last inser id</li> | ||
+ | <li><b>get_affected_rows</b> - return false or affected rows</li> | ||
+ | <li><b>get_value_query</b> - return false or first value</li> | ||
+ | <li><b>get_array_list</b> - return false or array</li> | ||
+ | <li><b>getKeyValArray</b> - return false or array</li> | ||
+ | <li><b>get_one_line_assoc</b> - return false or array</li> | ||
+ | <li><b>get_assoc_column</b> - return false or array</li> | ||
+ | <li><b>get_assoc_column1</b> - return false or array</li> | ||
+ | </ol> | ||
+ | |||
+ | Example table users:<br /> | ||
+ | |||
+ | |||
+ | <table> | ||
+ | <tr> | ||
+ | <td>id</td> | ||
+ | <td>fname</td> | ||
+ | <td>lname</td> | ||
+ | <td>group</td> | ||
+ | <td>login</td> | ||
+ | <td>pass</td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td>1</td> | ||
+ | <td>John</td> | ||
+ | <td>Smit</td> | ||
+ | <td>0</td> | ||
+ | <td>test1</td> | ||
+ | <td>qwe</td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td>2</td> | ||
+ | <td>Tom</td> | ||
+ | <td>Bink</td> | ||
+ | <td>1</td> | ||
+ | <td>test2</td> | ||
+ | <td>ytr</td> | ||
+ | </tr> | ||
+ | <tr> | ||
+ | <td>3</td> | ||
+ | <td>Mike</td> | ||
+ | <td>Depp</td> | ||
+ | <td>1</td> | ||
+ | <td>test3</td> | ||
+ | <td>odi</td> | ||
+ | </tr> | ||
+ | </table> | ||
+ | <br /> | ||
+ | |||
+ | <b>query:</b><br /> | ||
+ | |||
+ | var q = db.query('UPDATE users SET pass=$ WHERE id=$', [pass, id]); | ||
+ | q == false or true; | ||
+ | |||
+ | <b>query_insert:</b><br /> | ||
+ | |||
+ | var q = db.query_insert('INSERT INTO users (fname,lname,group,pass) VALUES($,$,$,$)', ['Grag','Sher','1','123']); | ||
+ | q == false or 4; | ||
+ | |||
+ | <b>get_affected_rows:</b><br /> | ||
+ | |||
+ | var q = db.get_affected_rows('UPDATE users SET group=$ WHERE group=$', ['1','0']); | ||
+ | q == false or 2; | ||
+ | |||
+ | <b>get_value_query:</b><br /> | ||
+ | |||
+ | var q = db.get_value_query('SELECT pass FROM users WHERE id=$', '1'); | ||
+ | q == false or qwe; | ||
+ | |||
+ | <b>get_array_list:</b><br /> | ||
+ | |||
+ | 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'} | ||
+ | ]; | ||
+ | |||
+ | <b>getKeyValArray:</b><br /> | ||
+ | |||
+ | var q = db.getKeyValArray('SELECT id, login FROM users'); | ||
+ | q == false or { | ||
+ | '1' : 'test1', | ||
+ | '2' : 'test2', | ||
+ | '3' : 'test3' | ||
+ | }; | ||
+ | |||
+ | <b>get_one_line_assoc:</b><br /> | ||
+ | |||
+ | 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' | ||
+ | }; | ||
+ | |||
+ | <b>get_assoc_column:</b><br /> | ||
+ | |||
+ | var q = db.get_assoc_column('SELECT login FROM users'); | ||
+ | q == false or [ | ||
+ | 'test1', | ||
+ | 'test2', | ||
+ | 'test3' | ||
+ | ]; | ||
+ | |||
+ | <b>get_assoc_column1[js]:</b><br /> | ||
+ | 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'} | ||
+ | }; |
Revision as of 17:39, 25 February 2014
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:
- query - return false or resource(true)
- query_insert - return false or last inser id
- get_affected_rows - return false or affected rows
- get_value_query - return false or first value
- get_array_list - return false or array
- getKeyValArray - return false or array
- get_one_line_assoc - return false or array
- get_assoc_column - return false or array
- get_assoc_column1 - return false or array
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[js]:
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'}
};