<commit_before>var util = require('util');
var querystring = require('querystring');

//数据库实例化对象
var dbInstances = {};
//数据表的字段信息
var tableFields = {};
//db缓存数据
var dbCacheData = {};

/**
 * Model类
 * @type {[type]}
 */
var Model = module.exports = Class(function(){
    "use strict";
    //解析page参数
    var parsePage = function(options){
        if ("page" in options) {
            var page = options.page + "";
            var num = 0;
            if (page.indexOf(",") > -1) {
                page = page.split(",");
                num = parseInt(page[1], 10);
                page = page[0];
            }
            num = num || C('db_nums_per_page');
            page = parseInt(page, 10) || 1;
            return {
                page: page,
                num: num
            };
        }
        return {
            page: 1,
            num: C('db_nums_per_page')
        };
    };
    /**
     * 字符串命名风格转换
     * @param  {[type]} name [description]
     * @param  {[type]} type [description]
     * @return {[type]}      [description]
     */
    var parseName = function(name, type){
        name = (name + "").trim();
        if (type) {
            name = name.replace(/_([a-zA-Z])/g, function(a, b){
                return b.toUpperCase();
            });
            return name.substr(0, 1).toUpperCase() + name.substr(1);
        } else {
            //首字母如果是大写，不转义为_x
            if (name.length >= 1) {
                name = name.substr(0, 1).toLowerCase() + name.substr(1);
            }
            return name.replace(/[A-Z]/g, function(a){
                return "_" + a;
            }).toLowerCase();
        }
    };

    return {
        // 当前数据库操作对象
        db: null,
        // 主键名称
        pk: "id",
        // 数据表前缀
        tablePrefix: "",
        // 数据库配置信息
        config: null,
        // 模型名称
        name: "",
        // 数据库名称
        dbName: "",
        // 数据表名（不包含表前缀）
        tableName: "",
        // 实际数据表名（包含表前缀）
        trueTableName: "",
        // 数据表子度信息
        fields: {},
        // 数据信息
        _data: {},
        // 参数
        _options: {},
        /**
         * 取得DB类的实例对象 字段检查
         * @access public
         * @param string $name 模型名称
         * @param string $tablePrefix 表前缀
         * @param mixed config 数据库连接信息
         */
        init: function(name, tablePrefix, config){
            // 获取模型名称
            if (name) {
                if (name.indexOf(".") > -1) {
                    name = name.split(".");
                    this.dbName = name[0];
                    this.name = name[1];
                }else{
                    this.name = name;
                }
            }else if(!this.name){
                this.getModelName();
            }
            if (!isString(tablePrefix)) {
                config = tablePrefix;
                tablePrefix = undefined;
            }
            this.config = config || "";
            //设置数据表前缀
            if (tablePrefix || this.config.db_prefix) {
                this.tablePrefix = tablePrefix || this.config.db_prefix;
            }else{
                if (!this.tablePrefix) {
                    this.tablePrefix = C("db_prefix");
                }
            }
            //子类的init方法
            if (this._init) {
                this._init();
            }
        },
        /**
         * 初始化数据库连接
         * @access public
         * @param integer $linkNum  连接序号
         * @param mixed $config  数据库连接信息
         * @param array $params  模型参数
         * @return Model
         */
        initDb: function(){
            if (this.db) {
                return this.db;
            }
            var config = this.config;
            var md5Value = md5(JSON.stringify(config));
            if (!dbInstances[md5Value]) {
                if (config && isString(config) && config.indexOf('/') === -1) {
                    config = C(config);
                }
                dbInstances[md5Value] = thinkRequire("Db").getInstance(config);
            }
            this.db = dbInstances[md5Value];
            return this.db;
        },
        /**
         * 获取模型名
         * @access public
         * @return string
         */
        getModelName: function(){
            if (this.name) {
                return this.name;
            }
            var filename = this.__filename || __filename;
            var name = filename.split("/").pop();
            this.name = name.substr(0, name.length - 8);
            return this.name;
        },
        /**
         * 获取表名
         * @return {[type]} [description]
         */
        getTableName: function(){
            var tableName;
            if (!this.trueTableName) {
                tableName = this.tablePrefix || "";
                tableName += this.tableName || parseName(this.getModelName());
                this.trueTableName = tableName.toLowerCase();
            }
            tableName = (this.dbName ? this.dbName + "." : "") + this.trueTableName;
            return tableName;
        },
        /**
         * 获取数据表信息
         * @access protected
         * @return Promise
         */
        getTableFields: function(table, all){
            if (table === true) {
                table = undefined;
                all = true;
            }<commit_msg>优化模型和高级模型的实现<commit_after>var util = require('util');
var querystring = require('querystring');

//数据库实例化对象
var dbInstances = {};
//数据表的字段信息
var tableFields = {};
//db缓存数据
var dbCacheData = {};

/**
 * Model类
 * @type {[type]}
 */
var Model = module.exports = Class(function(){
    "use strict";
    //解析page参数
    var parsePage = function(options){
        if ("page" in options) {
            var page = options.page + "";
            var num = 0;
            if (page.indexOf(",") > -1) {
                page = page.split(",");
                num = parseInt(page[1], 10);
                page = page[0];
            }
            num = num || C('db_nums_per_page');
            page = parseInt(page, 10) || 1;
            return {
                page: page,
                num: num
            };
        }
        return {
            page: 1,
            num: C('db_nums_per_page')
        };
    };
    /**
     * 字符串命名风格转换
     * @param  {[type]} name [description]
     * @param  {[type]} type [description]
     * @return {[type]}      [description]
     */
    var parseName = function(name, type){
        name = (name + "").trim();
        if (type) {
            name = name.replace(/_([a-zA-Z])/g, function(a, b){
                return b.toUpperCase();
            });
            return name.substr(0, 1).toUpperCase() + name.substr(1);
        } else {
            //首字母如果是大写，不转义为_x
            if (name.length >= 1) {
                name = name.substr(0, 1).toLowerCase() + name.substr(1);
            }
            return name.replace(/[A-Z]/g, function(a){
                return "_" + a;
            }).toLowerCase();
        }
    };

    return {
        // 当前数据库操作对象
        db: null,
        // 主键名称
        pk: "id",
        // 数据表前缀
        tablePrefix: "",
        // 数据库配置信息
        config: null,
        // 模型名称
        name: "",
        // 数据库名称
        dbName: "",
        // 数据表名（不包含表前缀）
        tableName: "",
        // 实际数据表名（包含表前缀）
        trueTableName: "",
        // 数据表子度信息
        fields: {},
        // 数据信息
        _data: {},
        // 参数
        _options: {},
        /**
         * 取得DB类的实例对象 字段检查
         * @access public
         * @param string $name 模型名称
         * @param string $tablePrefix 表前缀
         * @param mixed config 数据库连接信息
         */
        init: function(name, tablePrefix, config){
            // 获取模型名称
            if (name) {
                if (name.indexOf(".") > -1) {
                    name = name.split(".");
                    this.dbName = name[0];
                    this.name = name[1];
                }else{
                    this.name = name;
                }
            }else if(!this.name){
                this.getModelName();
            }
            if (!isString(tablePrefix)) {
                config = tablePrefix;
                tablePrefix = undefined;
            }
            this.config = config || "";
            //设置数据表前缀
            if (tablePrefix || this.config.db_prefix) {
                this.tablePrefix = tablePrefix || this.config.db_prefix;
            }else{
                if (!this.tablePrefix) {
                    this.tablePrefix = C("db_prefix");
                }
            }
            //子类的init方法
            if (this._init) {
                this._init();
            }
        },
        /**
         * 初始化数据库连接
         * @access public
         * @param integer $linkNum  连接序号
         * @param mixed $config  数据库连接信息
         * @param array $params  模型参数
         * @return Model
         */
        initDb: function(){
            if (this.db) {
                return this.db;
            }
            var config = this.config;
            var md5Value = md5(JSON.stringify(config));
            if (!dbInstances[md5Value]) {
                if (config && isString(config) && config.indexOf('/') === -1) {
                    config = C(config);
                }
                dbInstances[md5Value] = thinkRequire("Db").getInstance(config);
            }
            this.db = dbInstances[md5Value];
            return this.db;
        },
        /**
         * 获取模型名
         * @access public
         * @return string
         */
        getModelName: function(){
            if (this.name) {
                return this.name;
            }
            var filename = this.__filename || __filename;
            var name = filename.split("/").pop();
            this.name = name.substr(0, name.length - 8);
            return this.name;
        },
        /**
         * 获取表名
         * @return {[type]} [description]
         */
        getTableName: function(){
            var tableName;
            if (!this.trueTableName) {
                tableName = this.tablePrefix || "";
                tableName += this.tableName || parseName(this.getModelName());
                this.trueTableName = tableName.toLowerCase();
            }
            tableName = (this.dbName ? this.dbName + "." : "") + this.trueTableName;
            return tableName;
        },
        /**
         * 获取数据表信息
         * @access protected
         * @return Promise
         */
        getTableFields: function(table, all){
            this.initDb();
            if (table === true) {
                table = undefined;
                all = true;
            }
{'fraction_non_alphanumeric': 0.134557153671461, 'fraction_numerical': 0.005299015897047691, 'mean_word_length': 1.1442483262325016, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2603075', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3211, 'n_words': 611}