#!/bin/bash

# Copyright © 2012-2026 ScriptFanix
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# A copy of the GNU General Public License v3 is includded in the LICENSE file
# at the root of the project.

Delete() {
#Delete table <	where_key where_operator where_value
#		[where_key where_operator where_value
#		[…]]
	local			\
		table="$1"	\
		key		\
		operator	\
		value		\
		where_statement	\
		results
	# Build WHERE clause from stdin: one "key op value" triple per line
	while read key operator value
	do
		(( ${#where_statement} )) && where_statement+=( "AND" )
		if [[ $value == NULL ]]
		then
			# NULL comparisons require IS NULL, not = "NULL"
			where_statement+=( "$key is NULL" )
		else
			# Double embedded quotes to safely escape string values
			where_statement+=( "$key $operator "'"'"${value//\"/\"\"}"'"' )
		fi
	done
	echo "DELETE from $table WHERE ${where_statement[@]};" >&3
}
