代码改变世界

Javascript 写 asp 再次封装 不断完善中

2011-07-28 09:18  dbfox  阅读(270)  评论(0编辑  收藏  举报
function Page(p, pc, size, prefix, suffix) {
    var args = arguments;
    p = args[0] || 1;
    pc = args[1] || 1;
    size = args[2] || 5;
    prefix = args[3] || '';
    suffix = args[4] || '';

    p < 1 ? p = 1 : p;
    p > pc ? p = pc : p;
    size > pc ? size = pc : size;

    this.getCode = function (cssNormal, cssSelect) {
        var code = "";
        if (pc <= size) {
            for (var i = 1; i <= pc; i++) {
                if (i == p) {
                    code += ' <span' + (cssSelect ? ' class="' + cssSelect + '"' : '') + '>' + i + '</span> ';
                } else {
                    code += ' <a' + (cssNormal ? ' class="' + cssNormal + '"' : '') + ' href="' + prefix + i + suffix + '">' + i + '</a> ';
                }
            }
        } else {

            var minx = (size / 2).toFixed(0);
            var sinx = p - minx + 1;
            sinx = (sinx > 1 ? sinx : 1); 				//开始
            einx = (pc + 1 > sinx + size ? sinx + size : pc + 1); //结束


            for (var i = sinx; i < einx; i++) {
                if (i == p) {
                    code += ' <span' + (cssSelect ? ' class="' + cssSelect + '"' : '') + '>' + i + '</span> ';
                } else {
                    code += ' <a' + (cssNormal ? ' class="' + cssNormal + '"' : '') + ' href="' + prefix + i + suffix + '">' + i + '</a> ';
                }
            }
        }

        return code;
    }

    this.getPrev = function (css) {
        return p > 1 ? '<a' + (css ? ' class="' + css + '"' : '') + ' href="' + prefix + (p - 1) + suffix + '">上一页</a>' : '';
    }

    this.getNext = function (css) {
        return p < pc ? '<a' + (css ? ' class="' + css + '"' : '') + ' href="' + prefix + (p + 1) + suffix + '">下一页</a>' : '';
    }
}




var FSO = new ActiveXObject("Scripting.FileSystemObject");
function Path(path) {
    if (!path.indexOf(':')) path = Server.MapPath(path);
    return path;
}
function File(path) {
    path = Path(path);

    //把自己删除掉,或者删除子文件夹 或子文件 相对路径
    this.Delete = function () {
        File.Delete(path);
    }	
	//判断自己是否存在,或者是否存在子文件夹 或子文件 相对路径
    this.Exists = function () {
        return File.Exists(path);
    }
}


//读取一个文本文件 绝对路径
File.Read = function (path) {
    path = Path(path);
    var ForReading = 1;
    var File = FSO.OpenTextFile(path, ForReading);
    return File.ReadAll();
}
//创建一个文件 绝对路径
File.Create = function (path, content) {
    path = Path(path);
    var File = FSO.CreateTextFile(path, true);
    File.Write(content);
    File.Close();
}
//删除一个文件 绝对路径
File.Delete = function (path) {
    path = Path(path);
    if (this.Exist(path)) {
        FSO.DeleteFile(path);
    }
}
//判断某文件是否存在 绝对路径
File.Exists = function (path) {
    path = Path(path);
    return FSO.FileExists(path);
}

function Folder(path) {
    path = Path(path);

    this.Delete = function () {
        Folder.Delete(path);
    }

    this.Exists = function () {
        return Folder.Exists(path);
    }

}

Folder.Create = function (path) {
    path = Path(path);
    if (!Folder.Exists(path)) {
        FSO.CreateFolder(path);
    }    
}

Folder.Delete=function(path){
    path = Path(path);
    if (Folder.Exists(path)) {
        FSO.DeleteFolder(path);
    }
}

Folder.Exists=function(path){
    path = Path(path);
    return FSO.FolderExists(path);
}



String.prototype.htmlEntity = function () {
   
    html = this
	.replace(/\&/g, "&")
	.replace(/</g, "<")
	.replace(/>/g, ">")
	.replace(/"/g, """)
	.replace(/'/g, "'");
    return html;
}

String.prototype.entityHtml = function () {
    entity = this
	.replace(/\&/g, "&")
	.replace(/\</g, "<")
	.replace(/\>/g, ">")
	.replace(/\"/g, '"')
	.replace(/\'/g, "'");
    return entity;
}

function getConfig(config, args) {
    if (args) {
        for (var proto in args) {
            config[proto] = args[proto];
        }
    }
    return config;
}

function getConnection(){
	return new ActiveXObject("ADODB.Connection");
}
function getRecordset(){
    return new ActiveXObject("ADODB.Recordset");
}


var DB = {};
DB.ConnectionString = '';
/*单记录操作*/
DB.Add = function (sql, keyValueCol) {
    var Conn = getConnection();
    Conn.Open(DB.ConnectionString);
    var Rs = getRecordset();
    Rs.Open(sql, Conn, 3, 2);

    Rs.AddNew();
    for (var key in keyValueCol) {
        Rs.Fields.Item(key).Value = keyValueCol[key];
    }

    Rs.Fields.Update();
    Rs.Close();
    Rs = null;
    Conn.Close();
    Conn = null;
}

DB.Upd = function (sql, keyValueCol) {
    var Conn = getConnection();
    Conn.Open(DB.ConnectionString);
    var Rs = getRecordset();
    Rs.Open(sql, Conn, 3, 2);

    for (var key in keyValueCol) {
        Rs.Fields.Item(key).Value = keyValueCol[key];
    }

    Rs.Fields.Update();
    Rs.Close();
    Rs = null;
    Conn.Close();
    Conn = null;
}

DB.Exe = function (sql) {
    var Conn = getConnection();
    Conn.Open(DB.ConnectionString);
    Conn.Execute(sql);
    Conn.Close();
    Conn = null;
}

DB.Get = function (sql) {
    var _record = null;
    var Conn = getConnection();
    Conn.Open(DB.ConnectionString);
    var Rs = getRecordset();
    Rs.Open(sql, Conn, 1, 1);
    if (!Rs.EOF) {
        _record = {};
        for (var i = 0; i < Rs.Fields.Count; i++) {
            _record[Rs.Fields.Item(i).Name] = Rs.Fields.Item(i).Value;
        }
    }

    Rs.Close();
    Rs = null;
    Conn.Close();
    Conn = null;
    return _record;
}

/*多记录操作*/

DB.Batch = function () {

    var Conn = getConnection();
    var Rs = getRecordset();
    var _Batch = this;
    _Batch.Open = function (sql) {
        Conn.Open(DB.ConnectionString);
        Rs.Open(sql, Conn, 3, 2);
    }

    _Batch.Add = function (keyValueCol) {
        Rs.AddNew();
        for (var key in keyValueCol) {
            Rs.Fields.Item(key).Value = keyValueCol[key];
        }
        Rs.Update();
    }

    _Batch.Get=function(){
        var record_arr=null;
        if (!Rs.EOF) {
            record_arr=[];
            while (!Rs.EOF) {
               var _record={};
			    for(var i=0;i<Rs.Fields.Count;i++){
				    _record[Rs.Fields.Item(i).Name] = Rs.Fields.Item(i).Value;
			    }
			    record_arr.push(_record);
			    Rs.MoveNext();
            }
        }
        return record_arr;
    }

    _Batch.Close = function () {
        Rs.Close();
        Rs = null;
        Conn.Close();
        Conn = null;
    }
}

DB.List = function () {
    var _Config;
    var _List = this;
    _List.Page = { PS: 20, AP: 1, PC: 1, RC: 1 };

    //{fields:,table:,where,sort,pk}
    _List.Query = function () {
        _Config = new getConfig({
            fields: " * ",
            table: null,
            where: " 1=1 ",
            sort: " ID desc ",
            pk: " ID "
        }, arguments[0]);

        _List.Page.RC = DB.Get("select count(" + _Config.pk + ") as [count] from " +
        _Config.table + " where " + _Config.where).count;

        _List.Page.PC = Math.ceil(_List.Page.RC % _List.Page.PS);
    }

    _List.Get = function (p) {
        _List.Page.AP = p;
        var sql = '';
        if (p > 1) {
            sql = "select top " + _List.Page.PS + " " + _Config.fields +
			" from " + _Config.table + " where " + _Config.where +
			" and " + _Config.pk +
			" not in(select top " + (p - 1) * _List.Page.PS + " " + _Config.pk +
			" from " + _Config.table + " where " + _Config.where +
			" order by " + _Config.sort + ") order by " + _Config.sort;
        } else {
            sql = "select top " + _List.Page.PS + " " + _Config.fields +
			" from " + _Config.table + " where " + _Config.where + " order by " + _Config.sort;
        }
        var return_arr = null;
        var Batch = new DB.Batch();
        Batch.Open(sql);
        return_arr = Batch.Get();
        Batch.Close();
        return return_arr;
    }
}



DB.Reader = function () {
	
    var Conn = getConnection();
    var Rs = getRecordset();
    var _Reader = this;
	_Reader.EOF = false;
    _Reader.Open = function (sql) {
        Conn.Open(DB.ConnectionString);
        Rs.Open(sql, Conn, 1, 1);
		_Reader.EOF = Rs.EOF;
    }

   
    _Reader.Read = function () {
        if (!Rs.EOF) {
            var _record = {};
            for (var i = 0; i < Rs.Fields.Count; i++) {
                _record[Rs.Fields.Item(i).Name] = Rs.Fields.Item(i).Value;
            }
            Rs.MoveNext();
            return _record;
        } else {
            _Reader.EOF = true;
        }
    }

    _Reader.Close = function () {
        Rs.Close();
        Rs = null;
        Conn.Close();
        Conn = null;
    }
}


/*DB.Add() demo*/
DB.Add('select title,content from [table] where 1=2', { title: '标题', content: '内容' });

/*DB.Upd() demo*/
DB.Upd('select title,content from [table] where id=2', { title: '标题', content: '内容' });

/*DB.Exe() demo*/
DB.Exe("update [table] title='xxx' where id=3");

/*DB.Get() demo*/
var record = DB.Get("select * from [table] where id=4");



/*batch demo*/
var batch = new DB.Batch();
batch.Open('select * from [table] where 1=2');
batch.Add({ key: '1', key2: '2' });
batch.Close();


/*list demo*/
var list = new DB.List();



list.Query({ table: 'table', where: '1=1', pk: 'id' });

var page = new Page(list.Page.AP, list.Page.PC, 11, '', '');
var data = list.Get(1);

for (var i = 0; i < data.length; i++) {

}



/*reader demo*/
var reader=new DB.Reader();
reader.Open("select * from [table] where 1=1");

while(!reader.EOF){
	var record = reader.Read();
}
reader.Close();