<commit_before># coding: UTF-8

class Api::Json::QueriesController < Api::ApplicationController
  ssl_required :run
    
  def run
    # sanity check
    raise "You must indicate a sql query in the query parameter" if params[:sql].blank?

    # execute query
    query_result = current_user.run_pg_query(params[:sql])
    
    if params[:sql].downcase.include? "create table "
      begin
        params[:sql].downcase.split("create table ").each do |statement|
          table_name = statement.split(' ').first
          if table_name
            @table = Table.new
            @table.user_id = current_user.id
            @table.migrate_existing_table = table_name
            @table.save  
          end
        end
      # or, should the process of 'creating' the table fail if we can't register the table?
      rescue => e
        errors = e.is_a?(CartoDB::ErrorRunningQuery) ? [e.db_message, e.syntax_message] : [e.message]
        CartoDB::Logger.info "exception automatically creating new cartodb table", errors
      end
    end
    
    # log results of query
    @to_log = params[:sql]          
    Resque.enqueue(Resque::QueriesThresholdJobs, current_user.id, params[:sql], query_result[:time])
    
    # paginate if requested
    # TODO: Actually window the SQL here          
    if params[:rows_per_page] || params[:page]
      page, per_page = CartoDB::Pagination.get_page_and_per_page(params)
      query_result[:rows] = query_result[:rows][page...page+per_page]            <commit_msg>create table detection now allows no space, e.g. create table newtablename(col varchar)...<commit_after># coding: UTF-8

class Api::Json::QueriesController < Api::ApplicationController
  ssl_required :run
    
  def run
    # sanity check
    raise "You must indicate a sql query in the query parameter" if params[:sql].blank?

    # execute query
    query_result = current_user.run_pg_query(params[:sql])
    
    if params[:sql].downcase.include? "create table "
      begin
        params[:sql].downcase.split("create table ").each do |statement|
          table_name = statement.split(/[\s\(]/).first
          if table_name
            @table = Table.new
            @table.user_id = current_user.id
            @table.migrate_existing_table = table_name
            @table.save  
          end
        end
      # or, should the process of 'creating' the table fail if we can't register the table?
      rescue => e
        errors = e.is_a?(CartoDB::ErrorRunningQuery) ? [e.db_message, e.syntax_message] : [e.message]
        CartoDB::Logger.info "exception automatically creating new cartodb table", errors
      end
    end
    
    # log results of query
    @to_log = params[:sql]          
    Resque.enqueue(Resque::QueriesThresholdJobs, current_user.id, params[:sql], query_result[:time])
    
    # paginate if requested
    # TODO: Actually window the SQL here          
    if params[:rows_per_page] || params[:page]
      page, per_page = CartoDB::Pagination.get_page_and_per_page(params)
      query_result[:rows] = query_result[:rows][page...page+per_page]            
{'fraction_non_alphanumeric': 0.10245636716224951, 'fraction_numerical': 0.0006464124111182935, 'mean_word_length': 2.6670616113744074, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2358850', 'n_tokens_mistral': 916, 'n_tokens_neox': 910, 'n_words': 234}