package dev.kkorolyov.sqlob.statement;

import dev.kkorolyov.sqlob.construct.RowEntry;

/**
 * A SQL statement encapsulated as an executable command.
 */
public abstract class StatementCommand {
	private static final String PARAMETER_MARKER = "\\?";
	
	private String baseStatement;
	private RowEntry[] 	values,
											criteria;
	
	StatementCommand(String baseStatement, RowEntry[] values, RowEntry[] criteria) {
		this.baseStatement = baseStatement;
		this.values = values;
		this.criteria = criteria;
	}
	
	/** @return a statement which accomplishes the opposite of this statement, or {@code null} if this statement has no inverse */
	public abstract StatementCommand getInverseStatement();
	
	/** @return	base SQL statement */
	public String getBaseStatement() {
		return baseStatement;
	}
	
	/** @return statement values */
	public RowEntry[] getValues() {
		return values;
	}
	/** @return statement criteria */
	public RowEntry[] getCritera() {
		return criteria;
	}
	/** @return all statement parameters in the order: {@code values}, {@code criteria} */
	public RowEntry[] getParameters() {
		RowEntry[] parameters = (values != null || criteria != null) ? (new RowEntry[(values == null ? 0 : values.length) + (criteria == null ? 0 : criteria.length)]) : null;
		int i = 0;
		
		if (values != null) {
			for (RowEntry value : values)
				parameters[i++] = value;
		}
		if (criteria != null) {
			for (RowEntry criterion : criteria)
				parameters[i++] = criterion;
		}
		return parameters;
	}
	
	@Override
	public String toString() {
		String result = baseStatement;
		
		for (RowEntry parameter : getParameters())
			result = result.replaceFirst(PARAMETER_MARKER, parameter.getValue().toString());
		
		return result;
	}
}

@@ -1053,16 +1053,69 @@
 riteria%7D
+, or %7B@code null%7D if this statement has no parameters
  */%0A%09pub
@@ -1625,16 +1625,88 @@
 ent;%0A%09%09%0A
+%09%09RowEntry%5B%5D parameters = getParameters();%0A%09%09if (parameters != null) %7B%0A%09
 %09%09for (R
@@ -1725,20 +1725,17 @@
 meter : 
-getP
+p
 arameter
@@ -1739,12 +1739,11 @@
 ters
-()
 )%0A
+%09
 %09%09%09r
@@ -1824,16 +1824,17 @@
 g());%0A%09%09
+%7D
 %0A%09%09retur

Fix issue of null parameters