<commit_before>/*
 *  Copyright 2014 TWO SIGMA OPEN SOURCE, LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * SQL eval plugin
 * For creating and config evaluators that compile and/or evaluate SQL code and update code cell results.
 */
define(function(require, exports, module) {
  'use strict';
  var PLUGIN_NAME = "SQL";
  var COMMAND = "sqlsh/sqlshPlugin";
  var serviceBase = null;
  var cometdUtil = bkHelper.getUpdateService();
  var SqlShCancelFunction = null;
  
  var SqlSh = {
    pluginName: PLUGIN_NAME,
    cmMode: "text/x-sql",
    background: "#E0FFE0",
    bgColor: "##F8981C",
    fgColor: "#FFFFFF",
    borderColor: "",
    shortName: "Sq",
    tooltip: "SQL is one of the oldest and most common database query languages.",
    newShell: function(shellId, cb, ecb) {
      if (!shellId)
        shellId = "";
      bkHelper.httpPost(bkHelper.serverUrl(serviceBase + "/rest/sqlsh/getShell"), { shellId: shellId, sessionId: bkHelper.getSessionId() })
          .success(cb)
          .error(function() {
            console.log("failed to create shell", arguments);
            ecb("failed to create shell");
          });
    },
    
    evaluateWithPassword : function(code, modelOutput, refreshObj, deferred) {
      if (SqlShCancelFunction) {
        deferred.reject("An evaluation is already in progress");
        return deferred.promise;
      }
      var self = this;
      bkHelper.setupProgressOutput(modelOutput);

      $.ajax({
        type: "POST",
        datatype: "json",
        url: bkHelper.serverUrl(serviceBase + "/rest/sqlsh/evaluate"),
        data: {shellId: self.settings.shellID, code: code}
      }).done(function(ret) {
        SqlShCancelFunction = function() {
          $.ajax({
            type : "POST",
            datatype : "json",
            url : bkHelper.serverUrl(serviceBase + "/rest/sqlsh/cancelExecution"),
            data : {
              shellId : self.settings.shellID
            }
          }).done(function(ret) {
            console.log("done cancelExecution", ret);
          });
          bkHelper.setupCancellingOutput(modelOutput);
        }
        var onEvalStatusUpdate = function(evaluation) {
          if (bkHelper.receiveEvaluationUpdate(modelOutput, evaluation, PLUGIN_NAME, self.settings.shellID)) {
            cometdUtil.unsubscribe(evaluation.update_id);
            SqlShCancelFunction = null;
            if (evaluation.status === "ERROR")
              deferred.reject(evaluation.payload);
            else
              deferred.resolve(evaluation.payload);
          }
          if (refreshObj !== undefined)
            refreshObj.outputRefreshed();
          else
            bkHelper.refreshRootScope();
        };
        onEvalStatusUpdate(ret);
        if (ret.update_id) {
          cometdUtil.subscribe(ret.update_id, onEvalStatusUpdate);
        }
      });
    },
    
    evaluate : function(code, modelOutput, refreshObj) {
      var self = this;
      var deferred = Q.defer();
      return getListOfConnectiononWhoNeedDialog(self.settings.shellID).success(function(ret) {

        if (ret.length > 0) {
          for (var i = 0; i < ret.length; i++) {

            bkHelper.showSQLLoginModalDialog(ret[i].connectionName, 
                ret[i].connectionString, 
                ret[i].user, 
                function(sqlConnectionData) {
                  return setShellUserPassword(self.settings.shellID, 
                    sqlConnectionData.connectionName, 
                    sqlConnectionData.user, 
                    sqlConnectionData.password
                  ).success(function() {
                    self.evaluateWithPassword(code, modelOutput, refreshObj, deferred);
                    return deferred.promise;
                  })
            }, function() {
              bkHelper.printCanceledAnswer(modelOutput);
            });
            
          }
        } else {
          self.evaluateWithPassword(code, modelOutput, refreshObj, deferred);
          return deferred.promise;
        }
      });
    },
    
    interrupt: function() {
      this.cancelExecution();
    },
    cancelExecution: function () {
      if (SqlShCancelFunction) {
        SqlShCancelFunction();
      }
    },
    resetEnvironment: function () {
      var deferred = bkHelper.newDeferred();
      $.ajax({
        type: "POST",<commit_msg>issue #3312 Redone "evaluate" function : test fix<commit_after>/*
 *  Copyright 2014 TWO SIGMA OPEN SOURCE, LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * SQL eval plugin
 * For creating and config evaluators that compile and/or evaluate SQL code and update code cell results.
 */
define(function(require, exports, module) {
  'use strict';
  var PLUGIN_NAME = "SQL";
  var COMMAND = "sqlsh/sqlshPlugin";
  var serviceBase = null;
  var cometdUtil = bkHelper.getUpdateService();
  var SqlShCancelFunction = null;
  
  var SqlSh = {
    pluginName: PLUGIN_NAME,
    cmMode: "text/x-sql",
    background: "#E0FFE0",
    bgColor: "##F8981C",
    fgColor: "#FFFFFF",
    borderColor: "",
    shortName: "Sq",
    tooltip: "SQL is one of the oldest and most common database query languages.",
    newShell: function(shellId, cb, ecb) {
      if (!shellId)
        shellId = "";
      bkHelper.httpPost(bkHelper.serverUrl(serviceBase + "/rest/sqlsh/getShell"), { shellId: shellId, sessionId: bkHelper.getSessionId() })
          .success(cb)
          .error(function() {
            console.log("failed to create shell", arguments);
            ecb("failed to create shell");
          });
    },
    
    evaluateWithPassword : function(code, modelOutput, refreshObj, deferred) {
      if (SqlShCancelFunction) {
        deferred.reject("An evaluation is already in progress");
        return deferred.promise;
      }
      var self = this;

      $.ajax({
        type: "POST",
        datatype: "json",
        url: bkHelper.serverUrl(serviceBase + "/rest/sqlsh/evaluate"),
        data: {shellId: self.settings.shellID, code: code}
      }).done(function(ret) {
        SqlShCancelFunction = function() {
          $.ajax({
            type : "POST",
            datatype : "json",
            url : bkHelper.serverUrl(serviceBase + "/rest/sqlsh/cancelExecution"),
            data : {
              shellId : self.settings.shellID
            }
          }).done(function(ret) {
            console.log("done cancelExecution", ret);
          });
          bkHelper.setupCancellingOutput(modelOutput);
        }
        var onEvalStatusUpdate = function(evaluation) {
          if (bkHelper.receiveEvaluationUpdate(modelOutput, evaluation, PLUGIN_NAME, self.settings.shellID)) {
            cometdUtil.unsubscribe(evaluation.update_id);
            SqlShCancelFunction = null;
            if (evaluation.status === "ERROR")
              deferred.reject(evaluation.payload);
            else
              deferred.resolve(evaluation.payload);
          }
          if (refreshObj !== undefined)
            refreshObj.outputRefreshed();
          else
            bkHelper.refreshRootScope();
        };
        onEvalStatusUpdate(ret);
        if (ret.update_id) {
          cometdUtil.subscribe(ret.update_id, onEvalStatusUpdate);
        }
      });
    },
    
    
    evaluate : function(code, modelOutput, refreshObj) {

      var self = this;
      var deferred = Q.defer();
      
      var promise = new Promise(function(resolve, reject) {
        
        getListOfConnectiononWhoNeedDialog(self.settings.shellID).success(function(ret) {

          if (ret.length > 0) {
            for (var i = 0; i < ret.length; i++) {

              bkHelper.showSQLLoginModalDialog(ret[i].connectionName, 
                  ret[i].connectionString, 
                  ret[i].user, 
                  function(sqlConnectionData) {
                    setShellUserPassword(self.settings.shellID, 
                      sqlConnectionData.connectionName, 
                      sqlConnectionData.user, 
                      sqlConnectionData.password
                    ).success(function() {
                      resolve("OK");
                    })
              }, function() {
                resolve("CANCEL");
              });
              
            }
          } else {
            resolve("OK");
          }
        });
        
      });
      
      promise.then(
          result => {
            if(result == "OK"){
              bkHelper.setupProgressOutput(modelOutput);
              self.evaluateWithPassword(code, modelOutput, refreshObj, deferred);
            }else{
              bkHelper.printCanceledAnswer(modelOutput);
              deferred.reject();
            }
          }
      );
      
      return deferred.promise;
    },
    
    interrupt: function() {
      this.cancelExecution();
    },
    cancelExecution: function () {
      if (SqlShCancelFunction) {
        SqlShCancelFunction();
      }
    },
    resetEnvironment: function () {
      var deferred = bkHelper.newDeferred();
      $.ajax({
        type: "POST",
{'fraction_non_alphanumeric': 0.10753221010901882, 'fraction_numerical': 0.00356788899900892, 'mean_word_length': 2.028511404561825, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2621830', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2684, 'n_words': 649}