febs-db <-
STOP develop; Move to ->citong db库用于连接数据库,目前仅支持mysql
- exception
- define-table
- combined-primary-key
- connect-db
- exist
- query
- query-Lock-Row
- count
- add
- update
- remove
- transaction
- Class:database_table_base
exception
定义了常用的错误类型. 异常是如下的对象: {msg, code, filename, line}; 其中code有如下意义
// @desc: 数据查询条件错误。参数语句问题.exceptionDB_ERROR_SQL// @desc: 数据连接问题.exceptionDB_ERROR_CONNECT// @desc: 数据执行错误.exceptionDB_ERROR
事务处理中发生异常将自动rollback.
define table.
var citong_table = table; { super dbclient // database 'Admin' // table name. 'ID' // primary key. // cols. ID: type: 'integer' size: 8 key: true // the auto-incrementing Name: type: 'text' size:10 NumberCol:type: 'number' size: 4 IntCol: type: 'integer' size: 4 IntCol: type: 'integer' size: 8 // big int. BoolCol: type: 'boolean' ; }
example:
var database = database; var db = {};var tableDemo = db;
combined primary key
var citong_table = table; { super dbclient // database 'Admin' // table name. 'ID' 'IntCol' // primary keys. // cols. ID: type: 'integer' size: 8 Name: type: 'text' size:10 NumberCol:type: 'number' size: 4 IntCol: type: 'integer' size: 4 key: true // the auto-incrementing IntCol: type: 'integer' size: 8 // big int. BoolCol: type: 'boolean' ; }
connect db.
see mysql pool-options.
var database = database; var opt = connectionLimit : 10 supportBigNumbers : true bigNumberStrings : false host : '' port : 3306 user : '' password : '' database : '' /* ext */ queryTimeout : 5000;var db = opt;var table = db;
exist.
{ let r; r = table; r = table;}
query.
{ let r; r = table; r = table; // only query 'ID','Name' cols. r = table; // if combined primary key. r = table; r = table; // only query 'ID','Name' cols. let where = table; // == " `id`=43 ". where = table; // == " `name` LIKE '%dfdfd%' ". r = table; r = table; // where id = 43 limit 0,100 order by id asc. r = table; // only query 'ID','Name' cols. r = table; // only query 'ID','Name' cols. limit 0,100 r = table; // only query 'ID','Name' cols. orderby ID r = table;}
query Lock Row.
{ var conn = globaldb; if conn return conn; }
count.
{ let r; let where = table; // == " `id`=43 ". r = table;}
add.
{ var r = table;}
update.
{ var mod = ID: 1 name:"name" intCol:table intCol2:table ; var r = table; // ID=1,name="name",intCol=intCol+1,intCol2=intCol2+1}
remove.
{ var r = table;}
transaction.
{ var conn = globaldb; if conn return conn; }
Class database_table_base
- constructor
- isExist
- isExistWhere
- count
- add
- remove
- update
- queryById
- queryLockRow
- queryTop
- queryWhere
- get_conn
- escape
- make_condition
- make_condition_not_equal
- make_condition_more
- make_condition_more_equal
- make_condition_less_equal
- make_condition_less
- make_condition_like
- make_update_inc
所有的数据库查询方法都存在相应的同步调用方式, 如: queryWhereSync();
constructor.
/*** @param client: 数据库对象.* @param tablename: 本表名.* @param idKeyName: 本表主键列表, 如果为单主键可以直接为字符串, 如果为联合多主键则需要为数组.* @param model: 本表模型.*/*
model的定义格式如下:
{
colName: {type: 'integer', size: 8, key: true}, // the auto-incrementing
...
}
-
colName: 表示列名称
-
type: 表示列类型
类型 说明 size 'integer' 整型 指明字节长度 'text' 字符串 指明字符长度 'number' 浮点型 指明字节长度 'boolean' 布尔型 无意义 -
size: 字段长度
-
key: 是否是自增键; (同一个表只能有一个自增键, 当指定多个自增键时, 只认为最后一个为自增)
isExist.
/*** @desc: isExist* id is Object if table is combined primary. * the last param can be conn.* @return: boolean.*/*/*** @desc: isExist* id is Object if table is combined primary. * the last param can be conn.* @param id, cb* - cb: function(err, r:boolean) */
isExistWhere.
/*** @desc: isExitWhere* the last param can be conn.* @return: boolean.*/*/*** @desc: isExitWhere* the last param can be conn.* @param where, cb* - cb: function(err, r:boolrean) */
count.
/*** @desc: count* the last param can be conn.* @param: where* @return: int.*/*/*** @desc: count* the last param can be conn.* @param: where, cb* - cb: function(err, ret:int) */
add.
/*** @desc: add* the last param can be conn.* (insertId will set to item.id)* @return: bool*/*/*** @param cb: cb(err, r:boolean) * @return: void*/
remove.
/*** @desc: remove* the last param can be conn.* @return: bool.*/*/*** @param cb: cb(err, r:boolean) * @return:.*/
update.
/*** @desc: update; where id = item.id* if item.id is existed, sql condition is: 'id=value' AND (where)* otherwise sql condition is: where * the last param can be conn.* @param item, where.* @return: boolean.*/*/*** @desc: update; where id = item.id* if item.id is existed, sql condition is: 'id=value' AND (where)* otherwise sql condition is: where * the last param can be conn.* @param item, where, cb.* - cb: function(err, r:boolrean) * @return:.*/
queryById.
/*** @desc: query by id.* id is Object if table is combined primary. * the last param can be conn.* @param: id, [query_cols]* query_cols: [col1,col2], the cols will be query.* @return: mod.*/*/*** @desc: query by id.* id is Array if table is combined primary.* the last param can be conn.* @param: id, [query_cols], cb* - query_cols: [col1,col2], the cols will be query.* - cb: function(err, ret:mod) */
queryLockRow.
/*** @desc: query by id and lock row for update (use in transaction).* id is Object if table is combined primary.* the last param can be conn.* @param: id, [query_cols]* query_cols: [col1,col2], the cols will be query.* @return: mod.*/*/*** @desc: query by id and lock row for update (use in transaction).* id is Object if table is combined primary.* the last param can be conn.* @param: id, [query_cols], cb* - query_cols: [col1,col2], the cols will be query.* - cb: function(err, ret:mod) * @return: mod.*/
queryTop.
/*** @desc: query top.* the last param can be conn.* @param: where, {orderby}, [query_cols]* orderby: {key:true/false} true-means asc, false-means desc.* query_cols: [col1,col2], the cols will be query.* @return: mod.*/*/*** @desc: query top.* the last param can be conn.* @param: where, {orderby}, [query_cols], cb* - orderby: {key:true/false} true-means asc, false-means desc.* - query_cols: [col1,col2], the cols will be query.* - cb: function(err, ret:mod) */
queryWhere.
/*** @desc: query* the last param can be conn.* @param: where, [offset,limit], {orderby}, [query_cols]* - orderby: {key:true/false} true means asc, false means desc.* - query_cols: [col1,col2], the cols will be query. e.g. ['id', 'name']* @return: [mod,mod,...].*/*/*** @desc: query* the last param can be conn.* @param: where, [offset,limit], {orderby}, [query_cols]* - orderby: {key:true/false} true means asc, false means desc.* - query_cols: [col1,col2], the cols will be query. e.g. ['id', 'name']* -cb: function(err, ret:Array) */
get_conn.
/*** @desc: 获得最后一个参数,如果为 database_connection则返回.* @return:*/
escape.
/*** @desc: escape* @return: str.*/
make_condition.
/*** @desc: 构造一个 key=value的sql条件语句.* @return: sql;*/
make_condition_not_equal.
/*** @desc: 构造一个 key<>value的sql条件语句.* @return: sql;*/
make_condition_more.
/*** @desc: 构造一个 key>value的sql条件语句.* @return: sql;*/
make_condition_more_equal.
/*** @desc: 构造一个 key>=value的sql条件语句.* @return: sql;*/
make_condition_less_equal.
/*** @desc: 构造一个 key<=value的sql条件语句.* @return: sql;*/
make_condition_less.
/*** @desc: 构造一个 key<value的sql条件语句.* @return: sql;*/
make_condition_like.
/*** @desc: 构造一个 key LIKE value的sql条件语句.* @return: sql;*/
make_update_inc.
/*** @desc: 用于表明update时字段自增n.*/
make_update_inc.
/*** @desc: 用于表明使用原始v, 不做安全检验.*/