<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 12/30/2013	ELL
// 

	include("../connect/config.php");
	include("../connect/db_connect.php");
	
	echo "Parsing SQL...<br><br>\n";
	$sql_string_arr = array();
	$line_arr = array();
	// Split SQL input into lines.
	$line_arr = preg_split("[\n|\r]", $sql_string);
	$sql_string_arr[0] = "";
	$j = 0;
	$find_start = true;
	$got_script = false;
	$got_php = false;
	$got_style = false;
	// Search for commands and assemble into SQL command array.
	for ($i = 0; $i < count($line_arr); $i++)
	{
		// Test for comment line.
		// echo "i: $i ------ j: $j<br><br>\n";
		// echo "line_arr: " . $line_arr[$i] . "<br><br>\n";
		$curr_line = trim($line_arr[$i]);
		// echo "curr_line: $curr_line<br><br>\n";
		if ($find_start)
		{
			$first_chars = substr($curr_line, 0, 2);
			if (($first_chars == "--") || ($first_chars == "//"))
				continue;
		}
		$find_start = false;
		
		// It is possible for a line to end with a semicolon (;) if records contain JavaScript or PHP.  Need to turn off looking end of SQL command
		// if script or PHP is found until end of script or PHP is also found.  Otherwise we run the risk of trying to run only part of a record insert
		// which usually results in an SQL syntax error.
		if (!$got_script)
		{
			if (stripos($curr_line, "<script") !== false)
				$got_script = true;
		}
		if ($got_script)
		{
			if (stripos($curr_line, "</script>") !== false)
				$got_script = false;
		}
		
		if (!$got_php)
		{
			if (stripos($curr_line, "<?php") !== false)
				$got_php = true;
		}
		if ($got_php)
		{
			if (stripos($curr_line, "?>") !== false)
				$got_php = false;
		}
		
		if (!$got_style)
		{
			if (stripos($curr_line, "<style") !== false)
				$got_style = true;
		}
		if ($got_style)
		{
			if (stripos($curr_line, "</style>") !== false)
				$got_style = false;
		}
		
		// Build SQL command.
		$sql_string_arr[$j] = $sql_string_arr[$j] . $line_arr[$i];
		
		// Look for end of command.
		if ((!$got_script) && (!$got_php) && (!$got_style))
		{
			$last_char = substr($curr_line, (strlen($curr_line) - 1), 1);
			if ($last_char == ";")	// Got end of SQL command.
			{
				// Check for other possible reasons that ; is at end of line.
				$last_6_chars = substr($curr_line, (strlen($curr_line) - 7), 6);
				if (stripos($curr_line, "&") === false) // Looks like ; is not do to & code, good to go.
				{
					// echo "Full command: " . $sql_string_arr[$j] . "<br><br>\n";
					$j++;
					$sql_string_arr[$j] = "";
					$find_start = true;
				}
			}
		}
	}
	// Run SQL commands one at a time.
	for ($i = 0; $i < count($sql_string_arr); $i++)
	{
		if (strlen($sql_string_arr[$i]) > 2)
		{
			if (BWB_TABLE_PREFIX != "bwb_")
			{
				$sql_string_arr[$i] = str_replace("bwb_link", "@@@link_retention@@@", $sql_string_arr[$i]);
				$sql_string_arr[$i] = str_replace("bwb_", BWB_TABLE_PREFIX, $sql_string_arr[$i]);
				$sql_string_arr[$i] = str_replace("@@@link_retention@@@", "bwb_link", $sql_string_arr[$i]);
			}
			echo "SQL: " . substr($sql_string_arr[$i], 0, 50) . "...<br><br>\n";
			
			try	{
				$cnt = $bwb_dbh->exec($sql_string_arr[$i]);
			} catch (Exception $e) {
				echo $e->getMessage() . "<br>\n";
			}
			
			if (is_array($cnt))
			{
				echo "<pre>\n";
				print_r($cnt);
				echo "</pre>\n";
			}
			elseif ($cnt > 0)
				echo $cnt . " row(s) affected.<br>\n";
			else
				echo $bwb_dbh->errorInfo() . "<br>\n";
				
			if ($i < (count($sql_string_arr) - 1))
				echo "<br>-------------------------------------------------------------------------------<br>\n";
		}
	}
?>