<commit_before>var _app_factory_db_utils = function ($scope) {
    $scope.DB = {};
    var _db;
    
    var _db_checked = false;

    var _init_db = function () {
        
        //$.console_trace("var _init_db = function () {");
        //throw "var _init_db = function () {";
        //return;
        //alert(!window.openDatabase);
        if (_db_checked === true) {
            return;
        }
        
        if (_db === undefined) {
            try {
                _db = openDatabase('open_database', '1.0', 'Pulipuli Open Database', 2 * 1024 * 1024 );
                //$.console_trace("finish");
            }
            catch (_error) {
                //$.console_trace("finish1");
                $.console_trace("OpenDatabase Error", _error.message);
                _db_checked = true;
                alert("資料庫初始化失敗，不能使用");
            }
            //alert(typeof(_db.transaction));
            
            //console.log(typeof(_db.transaction));
        }
        return _db;
    };

    var _register_data = {};

    /**
     * 註冊
     * @param {String} _table 表格名稱
     * @param {String[]} _fields 欄位名稱
     */
    $scope.DB.register_table = function (_table, _fields, _callback) {
        _register_data[_table] = _fields;
        $.trigger_callback(_callback);
        return this;
    };

    // ----------------------------------

    $scope.DB.exec = function (_sql, _data, _success_callback) {

        //$.console_trace("exec", _sql);
        //return;
        //alert(_sql);
        if (typeof (_sql) !== "string") {
            $.console_trace("$scope.DB.exec error: _sql is not string");
            return;
        }

        _init_tables(function () {
            _db.transaction(function (_tx) {
                //alert("完成1");
                //$.console_trace("表格建好了，準備執行");
                _tx.executeSql(_sql, _data, function (_tx, _results) {
                    //alert("完成2");
                    if (typeof (_success_callback) === "function") {
                        var _data = [];
                        for (var _r = 0; _r < _results.rows.length; _r++) {
                            //if (typeof(_results.rows.item))
                            //var _row = _results.rows[_r];
                            var _row = _results.rows.item(_r);
                            //alert(JSON.stringify(_row));
                            //return;
                            var _d = {};
                            for (var _c in _row) {
                                var _value = _row[_c];
                                _value = $.unescape_quotation(_value);
                                _d[_c] = _value;
                            }
                            _data.push(_d);
                        }
                        
                        _success_callback(_data);
                    }
                }, function (_tx, _error) {
                    $scope.DB.error_handler(_tx, _error, _sql);
                });
            });
            //alert("完成1a");
        });
        return this;
    };

    // ----------------------------------

    var _tables_inited = false;

    var _init_tables = function (_callback) {
        //$.console_trace("_init_tables()");
        //return;
        
        if (_tables_inited === false) {
            //$.console_trace("before _init_db();");
            _init_db();
            //$.console_trace("_init_db();");
            var _keys = $.array_keys(_register_data);
            
            //$.console_trace("_init_tables", _keys);
            var _loop = function (_i) {
                if (_i < _keys.length) {
                    var _table_name = _keys[_i];
                    var _fields = _register_data[_table_name];
                    //$.console_trace("before $scope.DB.create_table(_table_name, _fields, function () {");
                    $scope.DB.create_table(_table_name, _fields, function () {
                        //$.console_trace("after $scope.DB.create_table(_table_name, _fields, function () {");
                        _i++;
                        _loop(_i);
                    });
                }
                else {
                    _tables_inited = true;
                    //$.console_trace("1a");
                    $.trigger_callback(_callback);
                }
            };
            _loop(0);
        }
        else {
            //$.console_trace("1b");
            $.trigger_callback(_callback);
        }
    };

    // ----------------------------------

    $scope.DB.create_table = function (_table_name, _field_name_list, _success_callback) {
        //$.console_trace("$scope.DB.create_table");
        //return;
        var _sql = "CREATE TABLE IF NOT EXISTS " + _table_name + " (";

        //_sql = _sql + "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ";
        _sql = _sql + "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ";

        for (var _i in _field_name_list) {
            var _field_name = _field_name_list[_i];
            _sql = _sql + _field_name;
            if (_i < _field_name_list.length - 1) {
                _sql = _sql + ",";
            }
        }
        _sql = _sql + ")";
        //$scope.DB.exec(_sql, _success_callback);
        //$.console_trace("create table_name", _table_name);
        _init_db();
        
        //$.console_trace("before _db.transaction(function (_tx) {");
        _db.transaction(function (_tx) {
            //alert("table 1");
            //$.console_trace("before _tx.executeSql(_sql, [], _success_callback);");
            var _error_handler = function (_tx, _error) {
                //alert(_error);
                
                // 如果建立table失敗，那就失敗吧，應該有記得吧？
                $scope.DB.error_handler(_tx, _error, _sql);
                
                $scope.DB.drop_table("log", function () {
                    $scope.DB.drop_table("target_history", function () {
                        $scope.DB.drop_table("sqlite_sequence", function () {
                            $scope.DB.create_table(_table_name, _field_name_list, _success_callback);
                        });
                    });<commit_msg>0427 更新資料庫的宣告方式<commit_after>var _app_factory_db_utils = function ($scope) {
    $scope.DB = {};
    var _db;
    
    var _db_checked = false;

    var _init_db = function () {
        
        //$.console_trace("var _init_db = function () {");
        //throw "var _init_db = function () {";
        //return;
        //alert(!window.openDatabase);
        if (_db_checked === true) {
            return;
        }
        
        if (_db === undefined) {
            try {
                _db = openDatabase('open_database', '1.0', 'Pulipuli Open Database', 2 * 1024 * 1024 );
                //$.console_trace("finish");
            }
            catch (_error) {
                //$.console_trace("finish1");
                $.console_trace("OpenDatabase Error", _error.message);
                _db_checked = true;
                alert("資料庫初始化失敗，不能使用");
            }
            //alert(typeof(_db.transaction));
            
            //console.log(typeof(_db.transaction));
        }
        return _db;
    };

    var _register_data = {};

    /**
     * 註冊
     * @param {String} _table 表格名稱
     * @param {String[]} _fields 欄位名稱
     */
    $scope.DB.register_table = function (_table, _fields, _callback) {
        _register_data[_table] = _fields;
        $.trigger_callback(_callback);
        return this;
    };

    // ----------------------------------

    $scope.DB.exec = function (_sql, _data, _success_callback) {

        //$.console_trace("exec", _sql);
        //return;
        //alert(_sql);
        if (typeof (_sql) !== "string") {
            $.console_trace("$scope.DB.exec error: _sql is not string");
            return;
        }

        _init_tables(function () {
            _db.transaction(function (_tx) {
                //alert("完成1");
                //$.console_trace("表格建好了，準備執行");
                _tx.executeSql(_sql, _data, function (_tx, _results) {
                    //alert("完成2");
                    if (typeof (_success_callback) === "function") {
                        var _data = [];
                        for (var _r = 0; _r < _results.rows.length; _r++) {
                            //if (typeof(_results.rows.item))
                            //var _row = _results.rows[_r];
                            var _row = _results.rows.item(_r);
                            //alert(JSON.stringify(_row));
                            //return;
                            var _d = {};
                            for (var _c in _row) {
                                var _value = _row[_c];
                                _value = $.unescape_quotation(_value);
                                _d[_c] = _value;
                            }
                            _data.push(_d);
                        }
                        
                        _success_callback(_data);
                    }
                }, function (_tx, _error) {
                    $scope.DB.error_handler(_tx, _error, _sql);
                });
            });
            //alert("完成1a");
        });
        return this;
    };

    // ----------------------------------

    var _tables_inited = false;

    var _init_tables = function (_callback) {
        //$.console_trace("_init_tables()");
        //return;
        
        if (_tables_inited === false) {
            //$.console_trace("before _init_db();");
            _init_db();
            //$.console_trace("_init_db();");
            var _keys = $.array_keys(_register_data);
            
            //$.console_trace("_init_tables", _keys);
            var _loop = function (_i) {
                if (_i < _keys.length) {
                    var _table_name = _keys[_i];
                    var _fields = _register_data[_table_name];
                    //$.console_trace("before $scope.DB.create_table(_table_name, _fields, function () {");
                    $scope.DB.create_table(_table_name, _fields, function () {
                        //$.console_trace("after $scope.DB.create_table(_table_name, _fields, function () {");
                        _i++;
                        _loop(_i);
                    });
                }
                else {
                    _tables_inited = true;
                    //$.console_trace("1a");
                    $.trigger_callback(_callback);
                }
            };
            _loop(0);
        }
        else {
            //$.console_trace("1b");
            $.trigger_callback(_callback);
        }
    };

    // ----------------------------------
    var LOCK = 0;
    $scope.DB.create_table = function (_table_name, _field_name_list, _success_callback) {
        //$.console_trace("$scope.DB.create_table");
        //return;
        var _sql = "CREATE TABLE IF NOT EXISTS " + _table_name + " (";

        //_sql = _sql + "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ";
        _sql = _sql + "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ";

        for (var _i in _field_name_list) {
            var _field_name = _field_name_list[_i];
            _sql = _sql + _field_name;
            if (_i < _field_name_list.length - 1) {
                _sql = _sql + ",";
            }
        }
        _sql = _sql + ")";
        //$scope.DB.exec(_sql, _success_callback);
        //$.console_trace("create table_name", _table_name);
        _init_db();
        
        //$.console_trace("before _db.transaction(function (_tx) {");
        _db.transaction(function (_tx) {
            //alert("table 1");
            //$.console_trace("before _tx.executeSql(_sql, [], _success_callback);");
            var _error_handler = function (_tx, _error) {
                //alert(_error);
                
                // 如果建立table失敗，那就失敗吧，應該有記得吧？
                $scope.DB.error_handler(_tx, _error, _sql);
                if (LOCK === 1) {
                    return;
                }
                
                $scope.DB.drop_table("log", function () {
                    $scope.DB.drop_table("target_history", function () {
                        $scope.DB.drop_table("sqlite_sequence", function () {
                            LOCK = 1;
                            $scope.DB.create_table(_table_name, _field_name_list, _success_callback);
                        });
                    });
{'fraction_non_alphanumeric': 0.14516258530710557, 'fraction_numerical': 0.003934162986752308, 'mean_word_length': 1.1902584842623527, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6604993', 'n_tokens_mistral': 3548, 'n_tokens_neox': 3414, 'n_words': 602}