<?php
/**
* (c) 2000, 2001, 2002 Remistech Sdn Bhd. All Rights Reserved.
*  Developed by John Lim
*  Licensed to Natsoft (M) Sdn Bhd to distribute and support. Refer to LICENSE document.
*/
include("./init.php");
include(ADODB_DIR."/tohtml.inc.php");

// given a series of sql statements, split it
function LensSplitSQL($sql)
{
	$arr = explode(';',$sql);
	return $arr;
}

$HTTP_VARS = array_merge($HTTP_POST_VARS,$HTTP_GET_VARS);

error_reporting(63);
$PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
$sql = isset($HTTP_VARS['sql']) ? $HTTP_VARS['sql'] : '';
$sql = undomq($sql);

if (isset($HTTP_SESSION_VARS['phplens_sqlrows'])) $rows = $HTTP_SESSION_VARS['phplens_sqlrows'];
else $rows = 3;

if (isset($HTTP_VARS['SMALLER'])) {
	$rows /= 2;
	if ($rows < 3) $rows = 3;
	$HTTP_SESSION_VARS['phplens_sqlrows'] = $rows;
}
if (isset($HTTP_VARS['BIGGER'])) {
	$rows *= 2;
	$HTTP_SESSION_VARS['phplens_sqlrows'] = $rows;
}

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo isset($PHPLENS_CHARSET) ? $PHPLENS_CHARSET : 'iso-8859-1'?>">
<title></title>
</head>
 <SCRIPT>
function fetchdb()
{
	var at=document.getdriver.drivers.selectedIndex;
	var dr = document.getdriver.drivers.options[at].text;
	window.location.href = 'sql.php?driver='+escape(dr)+'&rnd='+Math.random()
}

</SCRIPT>
<body bgcolor=white topmargin=0>
<table width=100% cellpadding="5" cellspacing="0"><tr bgcolor=<?php print PHPLENS_MENU_COLOR;?>>
<td><FORM name=getdriver> <b>Database</b> 
	<?php
	$arr = array();
	foreach($PHPLENS_DATABASES as $k=>$v) {
		$arr[] = $k;
	}
	print PHPLensArrayMenu('drivers onchange=fetchdb() ',$arr,false,$lens_qb_drivername,false);
	?>

&nbsp; <a href="sql.php?doListTables=1">List Tables</a>
</td></FORM><td align=right>
 &nbsp; &nbsp; <a href=../ target=_top><b>Applets</b></a>
 &nbsp; &nbsp; <a href=index.php target=_top><b>Builder</b></a>
  &nbsp; &nbsp; <a><b>SQL Tool</b></a>
 &nbsp; &nbsp; <a href=../ide/ target=_top><b>Editor</b></a>

</td></tr></table>
<form method="POST" action="<?php echo $PHP_SELF ?>">
<table><tr>
<td> Form size: <input type="submit" value=" &lt; " name="SMALLER"><input type="submit" value=" &gt; &gt; " name="BIGGER">
</td>
<td align=right>
<input type="submit" value=" Run SQL Below " name="RUN">
</td></tr>
  <tr>
  <td colspan=2><textarea rows=<?php print $rows; ?> name="sql" cols="80"><?php print htmlspecialchars($sql) ?></textarea>
  </td>
  </tr>
 </table>
</form>
<?php
 
if (isset($HTTP_VARS['doListTables'])) {
	$arr = $gDB->MetaTables();
	if (is_array($arr)) {
		for ($i=0; $i < sizeof($arr); $i++) {
			$t = trim($arr[$i]);
			if (strpos($t,' ') !== false) $t = '"'.$t.'"';
			$s = urlencode("select * from $t");
			$arr[$i] = "<a href=$PHP_SELF?RUN=submit&&sql=$s&tab={$arr[$i]}>".$arr[$i].'</a>';
		}
		arr2html($arr);
	}
}
			
if ($sql && isset($HTTP_VARS['RUN'])) {
	if (substr($sql,0,1) == ';') {
		$print = true;
		$sqla = LensSplitSQL(substr($sql,1));
	} else  {
		$print = false;
		$sqla = array($sql);
	}
	foreach($sqla as $sqls) {
		if ($print) {
			print "<p>".htmlspecialchars($sqls)."</p>";
			flush();
		}
		$rs = $gDB->Execute($sqls);
		if ($rs && is_object($rs) && !$rs->EOF) {
			rs2html($rs);
			while ($rs->NextRecordSet()) {
				print "<table width=98% bgcolor=#C0C0FF><tr><td>&nbsp;</td></tr></table>";
				rs2html($rs);
			}
		} else {
			$e2 = $gDB->ErrorMsg();
			$e1 = $gDB->ErrorNo();
		if (($e1) || ($e2)) {
			print ' &nbsp; '.$e1.': '.$e2;
		} else {
			print "<p>No Recordset returned<br></p>";
		}
		}
	}
}

if (isset($HTTP_VARS['tab'])) {
	$arr = $gDB->MetaColumns($HTTP_VARS['tab']);
	print "<p>";
	if (is_array($arr))
	foreach($arr as $k)  {
		if (isset($k->scale) && $k->scale) $scale = '.'.$k->scale;
		else $scale = '';
		$more = '';
		if (!empty($k->has_default)) $more .= ' default='.$k->default_value;
		print "<b>$k->name</b> $k->type $k->max_length$scale$more<br>";
	}
}
?> 

<?php
#include_once(PHPLENS_DIR.'/adodb/adodb-crosstab.inc.php');
?>
</body>
</html>