<commit_before>package com.zeroturnaround.alvor.checkers.sqldynamic;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.zeroturnaround.alvor.checkers.IAbstractStringChecker;
import com.zeroturnaround.alvor.checkers.ISQLErrorHandler;
import com.zeroturnaround.alvor.checkers.IStringNodeDescriptor;
import com.zeroturnaround.alvor.common.logging.ILog;
import com.zeroturnaround.alvor.common.logging.Logs;
import com.zeroturnaround.alvor.string.Position;
import com.zeroturnaround.alvor.string.samplegen.SampleGenerator;
import com.zeroturnaround.alvor.string.util.AbstractStringSizeCounter;

public class DynamicSQLChecker implements IAbstractStringChecker {
	private static final ILog LOG = Logs.getLog(DynamicSQLChecker.class);
	private static final int SIZE_LIMIT = 10000;

	@Override
	public void checkAbstractStrings(List<IStringNodeDescriptor> descriptors,
			ISQLErrorHandler errorHandler, Map<String, String> options) {
		if (descriptors.size() == 0) {
			return;
		}
		
		
		if (options.get("DBDriverName") == null || options.get("DBUrl") == null
				|| options.get("DBUsername") == null || options.get("DBPassword") == null
				|| options.get("DBDriverName").toString().isEmpty()
				|| options.get("DBDriverName").toString().isEmpty()) {
			errorHandler.handleSQLWarning("SQL checker: Test database configuration is not complete, dynamic checking will be disabled", 
					new Position(options.get("SourceFileName"), 0, 0));
			return;
		}
		
		SQLStringAnalyzer analyzer = null;
		try {
			analyzer = new SQLStringAnalyzer(				
				options.get("DBDriverName").toString(),
				options.get("DBUrl").toString(),
				options.get("DBUsername").toString(),
				options.get("DBPassword").toString());
		} catch (Exception e) {
			// for position use first pos from the list
			errorHandler.handleSQLError("Dynamic SQL checker: can't connect with database schema: "
					+ e.getMessage(), descriptors.get(0).getPosition());
			return;
		}
		
		int totalConcrete = 0;
		Map<String, Integer> concretes = new HashMap<String, Integer>();
		
		assert LOG.message("============================================");
		
		for (IStringNodeDescriptor nodeDesc: descriptors) {
			assert LOG.message("ABS: " + nodeDesc.getAbstractValue());
			
			if (AbstractStringSizeCounter.size(nodeDesc.getAbstractValue()) > SIZE_LIMIT) {
				errorHandler.handleSQLWarning("Dynamic SQL checker: abstract string is too big", 
						nodeDesc.getPosition());
				continue;
			}
			
			List<String> concreteStr = SampleGenerator.getConcreteStrings(nodeDesc.getAbstractValue());
//			LOG.message(conc);
			totalConcrete += concreteStr.size();
			<commit_msg>Error marker on configuration file when connecting fails<commit_after>package com.zeroturnaround.alvor.checkers.sqldynamic;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.zeroturnaround.alvor.checkers.IAbstractStringChecker;
import com.zeroturnaround.alvor.checkers.ISQLErrorHandler;
import com.zeroturnaround.alvor.checkers.IStringNodeDescriptor;
import com.zeroturnaround.alvor.common.logging.ILog;
import com.zeroturnaround.alvor.common.logging.Logs;
import com.zeroturnaround.alvor.string.Position;
import com.zeroturnaround.alvor.string.samplegen.SampleGenerator;
import com.zeroturnaround.alvor.string.util.AbstractStringSizeCounter;

public class DynamicSQLChecker implements IAbstractStringChecker {
	private static final ILog LOG = Logs.getLog(DynamicSQLChecker.class);
	private static final int SIZE_LIMIT = 10000;

	@Override
	public void checkAbstractStrings(List<IStringNodeDescriptor> descriptors,
			ISQLErrorHandler errorHandler, Map<String, String> options) {
		if (descriptors.size() == 0) {
			return;
		}
		
		
		if (options.get("DBDriverName") == null || options.get("DBUrl") == null
				|| options.get("DBUsername") == null || options.get("DBPassword") == null
				|| options.get("DBDriverName").toString().isEmpty()
				|| options.get("DBDriverName").toString().isEmpty()) {
			errorHandler.handleSQLWarning("SQL checker: Test database configuration is not complete, dynamic checking will be disabled", 
					new Position(options.get("SourceFileName"), 0, 0));
			return;
		}
		
		SQLStringAnalyzer analyzer = null;
		try {
			analyzer = new SQLStringAnalyzer(				
				options.get("DBDriverName").toString(),
				options.get("DBUrl").toString(),
				options.get("DBUsername").toString(),
				options.get("DBPassword").toString());
		} catch (Exception e) {
			errorHandler.handleSQLError("SQL checker: can't connect with test database: "
					+ e.getMessage(), new Position(options.get("SourceFileName"), 0, 0));
			return;
		}
		
		int totalConcrete = 0;
		Map<String, Integer> concretes = new HashMap<String, Integer>();
		
		assert LOG.message("============================================");
		
		for (IStringNodeDescriptor nodeDesc: descriptors) {
			assert LOG.message("ABS: " + nodeDesc.getAbstractValue());
			
			if (AbstractStringSizeCounter.size(nodeDesc.getAbstractValue()) > SIZE_LIMIT) {
				errorHandler.handleSQLWarning("Dynamic SQL checker: abstract string is too big", 
						nodeDesc.getPosition());
				continue;
			}
			
			List<String> concreteStr = SampleGenerator.getConcreteStrings(nodeDesc.getAbstractValue());
//			LOG.message(conc);
			totalConcrete += concreteStr.size();
			
{'fraction_non_alphanumeric': 0.13729093916559457, 'fraction_numerical': 0.003859584635177357, 'mean_word_length': 5.094064949608063, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '2568107', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1593, 'n_words': 317}