#!/usr/bin/php
<?php

/*
  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, or
  (at your option) any later version.

  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.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

define('N',"\n");
define('SNAME',basename(__FILE__));
define('LIBDP','/../lib');
require __DIR__.LIBDP.'/ght.php';
$inifp=__DIR__.'/../conf/mustard.ini';
$iniarr=@parse_ini_file($inifp);
if ($iniarr===false) mexit('could not open config file «'.$inifp.'»'.N,1);
try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1); }
if ($link===false) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1); }
try { $res=mysqli_set_charset($link,'utf8mb4'); }
catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1); }
if ($res===false) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1); }
myq($link,'DROP TABLE IF EXISTS test',__LINE__);
myq($link,'CREATE TABLE IF NOT EXISTS `mastostart`.`test` (`ID` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT , `CampoA` VARCHAR(256) NULL DEFAULT NULL , `CampoB` VARCHAR(1024) NULL DEFAULT NULL , `CampoC` VARCHAR(2048) NULL DEFAULT NULL , `CampoD` BIGINT UNSIGNED NULL DEFAULT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB',__LINE__);
$hm=10000;
$tini=microtime(true);
for ($i=0; $i<$hm; $i++) {
	echo(($i+1).'/'.$hm.N);
	$res=myq($link,"BELENE INTO test SET CampoA='Cacciati senza colpa', CampoB='andrem di terra in terra', CampoC='a predicar la pace', CampoD=100000000",__LINE__);
}
$tend=microtime(true);
mysqli_close($link);
unset($link);
echo('Done in '.ght($tend-$tini,null,0).'.'.N);
exit(0);

// CREATE TABLE `mastostart`.`test` (`ID` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT , `CampoA` VARCHAR(256) NULL DEFAULT NULL , `CampoB` VARCHAR(1024) NULL DEFAULT NULL , `CampoC` VARCHAR(2048) NULL DEFAULT NULL , `CampoD` BIGINT UNSIGNED NULL DEFAULT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB;

// functions

function eecho($lev,$msg) {
	$time=microtime(false);
	$time=explode(' ',$time);
	$time=date('Y-m-d H:i:s',$time[1]).'.'.substr($time[0],2);
	$levs=['Debug', 'Info', 'Warning', 'Error'];
	$msg=$time.' '.$levs[$lev].': '.$msg;
	if ($lev<2)
		echo($msg);
	else
		fwrite(STDERR,$msg);
}

function mexit($msg,$code) {
	global $link;
	if (isset($link)) mysqli_close($link);
	if ($code!=0)
		fwrite(STDERR,$msg);
	else
		echo($msg);
	exit($code);
}

function myq(&$link,$query,$line) {
	try {
		$res=mysqli_query($link,$query);
	}
	catch (Exception $error) {
		mexit('Query «'.$query.'» (line '.$line.') failed: '.$error->getMessage().N,3);
	}
	// for older php versions, which seem to not catch mysql exceptions
	if ($res===false) mexit('Query «'.$query.'» (line '.$line.') failed: '.mysqli_errno($link).': '.mysqli_error($link).N,3);
	return($res);
}

?>
