<commit_before>#!/usr/bin/env ruby

module Mystic
  class Model
    def self.table_name
      to_s.downcase
    end
		
		def self.visible_cols
			["*"]
		end
		
		def self.wrapper_sql(opts={})
			sym_opts = opts.symbolize

			sql = sym_opts[:sql] || "SELECT 1"
			return_rows = sym_opts[:return_rows] || false
			return_json = sym_opts[:return_json] || false
			return_rows = true if return_json
			
			op = sql.split(/\s+/,2).first
			
			sql << " RETURNING #{visible_cols*','}" if return_rows && op != "SELECT"
			
			s = []
			
			if return_json
				s << "WITH res AS (#{sql}) SELECT"
				s << "row_to_json(res)" if op == "INSERT"
				s << "array_to_json(array_agg(res))" unless op == "INSERT"
				s << "AS #{Mystic::JSON_COL}"
				s << "FROM res"
			else
				s << sql
			end
			
			s*' '
		end

    def self.function_sql(returns_rows, funcname, *params)
			"SELECT #{returns_rows ? "* FROM" : ""} #{funcname}(#{params.sqlize*','})"
    end
    
    def self.select_sql(params={}, opts={})
			sym_opts = opts.symbolize
      count = sym_opts[:count] || 0
			
			sql = "SELECT #{visible_cols*','} FROM #{table_name} WHERE #{params.sqlize*' AND '}"
			sql << " LIMIT #{count.to_i}" if count > 0
			
			wrapper_sql(
				:sql => sql,
				:return_rows => true,
				:return_json => sym_opts[:return_json] && Mystic.adapter.json_supported?
			)
    end
    
    def self.update_sql(where={}, set={}, opts={})
      return "" if where.empty?
      return "" if set.empty?
			
			sym_opts = opts.symbolize
			
			wrapper_sql(
				:sql => "UPDATE #{table_name} SET #{set.sqlize*','} WHERE #{where.sqlize*' AND '}",
				:return_rows => sym_opts[:return_rows],
				:return_json => sym_opts[:return_json] && Mystic.adapter.json_supported?
			)
    end
    
    def self.insert_sql(params={}, opts={})
			return "" if params.empty?
      
			sym_opts = opts.symbolize
			
			wrapper_sql(
				:sql => "INSERT INTO #{table_name} (#{params.keys*','}) VALUES (#{params.values.sqlize*','})",
				:return_rows => sym_opts[:return_rows],
				:return_json => sym_opts[:return_json] && Mystic.adapter.json_supported?
			)
    end
    
    def self.delete_sql(params={}, opts={})
      return "" if params.empty?
			
			sym_opts = opts.symbolize

			wrapper_sql(
				:sql => "DELETE FROM #{table_name} WHERE #{params.sqlize*' AND '}",
				:return_rows => sym_opts[:return_rows],
				:return_json => sym_opts[:return_json] && Mystic.adapter.json_supported?
			)
    end
    
    def self.select(params={}, opts={})
      Mystic.execute select_sql(params, opts)
    end
    
    def self.fetch(params={}, opts={})
      res = select(params,opts.merge({:count => 1}))
			return res if res.is_a?(String)
			res.first
    end
    
    def self.create(params={}, opts={})
      res = Mystic.execute insert_sql(params, opts)
			return res.first if res.is_a?(Array)
			res
    end
    
    def self.update(where={}, set={}, opts={})
      Mystic.execute update_sql(where, set, opts.merge({ :return_rows => true }))
    end
    
    def self.delete(params={}, opts={})
			Mystic.execute delete_sql(params, opts)
    end
		
		def self.exec_func(funcname, *params)
			Mystic.execute function_sql(false, funcname, *params)
		end
		
		def self.exec_func_rows(funcname, *params)
			Mystic.execute function_sql(true, funcname, *params)
		end
  end
end<commit_msg>Fix invalid sql for SELECT queries with no WHERE clause.<commit_after>#!/usr/bin/env ruby

module Mystic
  class Model
    def self.table_name
      to_s.downcase
    end
		
		def self.visible_cols
			["*"]
		end
		
		def self.wrapper_sql(opts={})
			sym_opts = opts.symbolize

			sql = sym_opts[:sql] || "SELECT 1"
			return_rows = sym_opts[:return_rows] || false
			return_json = sym_opts[:return_json] || false
			return_rows = true if return_json
			
			op = sql.split(/\s+/,2).first
			
			sql << " RETURNING #{visible_cols*','}" if return_rows && op != "SELECT"
			
			s = []
			
			if return_json
				s << "WITH res AS (#{sql}) SELECT"
				s << "row_to_json(res)" if op == "INSERT"
				s << "array_to_json(array_agg(res))" unless op == "INSERT"
				s << "AS #{Mystic::JSON_COL}"
				s << "FROM res"
			else
				s << sql
			end
			
			s*' '
		end

    def self.function_sql(returns_rows, funcname, *params)
			"SELECT #{returns_rows ? "* FROM" : ""} #{funcname}(#{params.sqlize*','})"
    end
    
    def self.select_sql(params={}, opts={})
			sym_opts = opts.symbolize
      count = sym_opts[:count] || 0
			
			sql = []
			sql = "SELECT #{visible_cols*','} FROM #{table_name}"
			sql << "WHERE #{params.sqlize*' AND '}" if params.count > 0
			sql << "LIMIT #{count.to_i}" if count > 0
			
			wrapper_sql(
				:sql => sql.join(' '),
				:return_rows => true,
				:return_json => sym_opts[:return_json] && Mystic.adapter.json_supported?
			)
    end
    
    def self.update_sql(where={}, set={}, opts={})
      return "" if where.empty?
      return "" if set.empty?
			
			sym_opts = opts.symbolize
			
			wrapper_sql(
				:sql => "UPDATE #{table_name} SET #{set.sqlize*','} WHERE #{where.sqlize*' AND '}",
				:return_rows => sym_opts[:return_rows],
				:return_json => sym_opts[:return_json] && Mystic.adapter.json_supported?
			)
    end
    
    def self.insert_sql(params={}, opts={})
			return "" if params.empty?
      
			sym_opts = opts.symbolize
			
			wrapper_sql(
				:sql => "INSERT INTO #{table_name} (#{params.keys*','}) VALUES (#{params.values.sqlize*','})",
				:return_rows => sym_opts[:return_rows],
				:return_json => sym_opts[:return_json] && Mystic.adapter.json_supported?
			)
    end
    
    def self.delete_sql(params={}, opts={})
      return "" if params.empty?
			
			sym_opts = opts.symbolize

			wrapper_sql(
				:sql => "DELETE FROM #{table_name} WHERE #{params.sqlize*' AND '}",
				:return_rows => sym_opts[:return_rows],
				:return_json => sym_opts[:return_json] && Mystic.adapter.json_supported?
			)
    end
    
    def self.select(params={}, opts={})
      Mystic.execute select_sql(params, opts)
    end
    
    def self.fetch(params={}, opts={})
      res = select(params,opts.merge({:count => 1}))
			return res if res.is_a?(String)
			res.first
    end
    
    def self.create(params={}, opts={})
      res = Mystic.execute insert_sql(params, opts)
			return res.first if res.is_a?(Array)
			res
    end
    
    def self.update(where={}, set={}, opts={})
      Mystic.execute update_sql(where, set, opts.merge({ :return_rows => true }))
    end
    
    def self.delete(params={}, opts={})
			Mystic.execute delete_sql(params, opts)
    end
		
		def self.exec_func(funcname, *params)
			Mystic.execute function_sql(false, funcname, *params)
		end
		
		def self.exec_func_rows(funcname, *params)
			Mystic.execute function_sql(true, funcname, *params)
		end
  end
end
{'fraction_non_alphanumeric': 0.16014814814814815, 'fraction_numerical': 0.0016296296296296295, 'mean_word_length': 2.9711764705882353, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 34, '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': '765954', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2343, 'n_words': 513}