547 lines
18 KiB
PHP
547 lines
18 KiB
PHP
|
|
<?php
|
||
|
|
/*
|
||
|
|
* @component SQL 2 Excel Component
|
||
|
|
* @copyright Copyright (C) Joomla-R-Us, joomla-r-us.com
|
||
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
|
||
|
|
*/
|
||
|
|
|
||
|
|
// No direct access
|
||
|
|
defined( '_JEXEC' ) or die( 'Restricted access' );
|
||
|
|
|
||
|
|
require_once( JPATH_COMPONENT.DS.'controllers'.DS.'download.php' );
|
||
|
|
require_once(JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'excel_writer.php');
|
||
|
|
|
||
|
|
class Sql2excelControllerSqlexcelsyntax extends Sql2excelController
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* constructor (registers additional tasks to methods)
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
function __construct()
|
||
|
|
{
|
||
|
|
parent::__construct();
|
||
|
|
|
||
|
|
// Register Extra tasks
|
||
|
|
$this->registerTask( 'check', 'checkSyntax' );
|
||
|
|
$this->registerTask( 'tables', 'getTables' );
|
||
|
|
$this->registerTask( 'cols', 'getColumns' );
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check Syntax of the passed query
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
function checkSyntax()
|
||
|
|
{
|
||
|
|
|
||
|
|
$post = JRequest::get('post');
|
||
|
|
$query = JRequest::getVar( 'query', '', 'post', 'string', JREQUEST_ALLOWRAW );
|
||
|
|
$query = $this->utf8_strrev($query);
|
||
|
|
$dbID = JRequest::getVar( 'db', '', 'post', 'int');
|
||
|
|
$livesyntax = JRequest::getVar( 'ls', '', 'post', 'int');
|
||
|
|
|
||
|
|
$explainMsg = '';
|
||
|
|
$syntaxMsg = '';
|
||
|
|
|
||
|
|
// Get database connection and info
|
||
|
|
$dbInfo = $this->getDB($dbID);
|
||
|
|
$explainDB = $dbInfo[0];
|
||
|
|
$dbName = $dbInfo[1];
|
||
|
|
$dbType = $dbInfo[2];
|
||
|
|
$dbPrefix = $dbInfo[3];
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if ( $explainDB ) {
|
||
|
|
|
||
|
|
// Custom Database Driver?
|
||
|
|
if ( $dbType == 6 && file_exists($dbPrefix) ) {
|
||
|
|
// Load the driver class
|
||
|
|
require_once($dbPrefix);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Get basic Substitution variables
|
||
|
|
$substParms = Sql2excelControllerDownload::getSubstParms();
|
||
|
|
|
||
|
|
// Substitute basic variables in query
|
||
|
|
$query = writeExcel::replace_vars($query, $substParms);
|
||
|
|
|
||
|
|
if ( $dbType <= 2 ) {
|
||
|
|
// Joomla DB or MySQL
|
||
|
|
|
||
|
|
// Parse the query for multiple statements
|
||
|
|
// Make sure we have only \n for line breaks
|
||
|
|
$queryStr = writeExcel::removeCR($query,2);
|
||
|
|
|
||
|
|
// Split statements that end with ; and a new line
|
||
|
|
$queryTokens = explode(";\n",$query);
|
||
|
|
|
||
|
|
// Use the last statement
|
||
|
|
$query = $queryTokens[sizeof($queryTokens)-1];
|
||
|
|
|
||
|
|
|
||
|
|
$explainDB->setQuery( $query );
|
||
|
|
$explain = $explainDB->explain();
|
||
|
|
|
||
|
|
|
||
|
|
if ( $explain != null ) {
|
||
|
|
$explainMsg = $explain;
|
||
|
|
$explainMsg = str_replace('<tr><th>', '<tr style="background: #FFC;"><th>',$explainMsg);
|
||
|
|
|
||
|
|
} else {
|
||
|
|
$syntaxMsg = $explainDB->getErrorMsg();
|
||
|
|
$syntaxMsg = str_replace('SQL=EXPLAIN ' , ' : <br><p>', $syntaxMsg);
|
||
|
|
$explainMsg = JText::_('EXPLAIN_ERR_MSG');
|
||
|
|
}
|
||
|
|
} elseif ( $dbType == 3 ) {
|
||
|
|
// Oracle
|
||
|
|
$sql = 'DELETE plan_table';
|
||
|
|
$stmt = @oci_parse($explainDB, $sql);
|
||
|
|
$res = oci_execute($stmt);
|
||
|
|
|
||
|
|
$sql = 'explain plan for ' . $query;
|
||
|
|
$stmt = @oci_parse($explainDB, $sql);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if (!$stmt) {
|
||
|
|
$e = oci_error($db);
|
||
|
|
$syntaxMsg = $e['message'];
|
||
|
|
} else {
|
||
|
|
|
||
|
|
$res = oci_execute($stmt);
|
||
|
|
if (!$res) {
|
||
|
|
$e = oci_error($stmt);
|
||
|
|
$syntaxMsg = $e['message'] . ' | ' . $e['sqltext'];
|
||
|
|
$explainMsg = JText::_('EXPLAIN_ERR_MSG');
|
||
|
|
} else {
|
||
|
|
$syntaxMsg = '';
|
||
|
|
$explainMsg = JText::_('Could not retrieve Explain info for your query from Oracle');
|
||
|
|
// Get the Explain info from plan_table
|
||
|
|
$sql = "select
|
||
|
|
substr (lpad(' ', level-1) || operation || ' (' || options || ')',1,30 ) \"Operation\",
|
||
|
|
object_name \"Object\"
|
||
|
|
from plan_table
|
||
|
|
start with id = 0
|
||
|
|
connect by prior id=parent_id";
|
||
|
|
|
||
|
|
$stmt = @oci_parse($explainDB, $sql);
|
||
|
|
if ( $stmt ) {
|
||
|
|
|
||
|
|
$res = oci_execute($stmt);
|
||
|
|
if ( $res ) {
|
||
|
|
$explainMsg = '<pre>';
|
||
|
|
$nrows = oci_fetch_all($stmt, $rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
|
||
|
|
foreach ( $rows as $row ) {
|
||
|
|
$explainMsg .= $row["Operation"] . ' ' . $row["Object"] . "<br>";
|
||
|
|
|
||
|
|
}
|
||
|
|
$explainMsg .= '</pre>';
|
||
|
|
oci_free_statement($stmt);
|
||
|
|
oci_close($tablesDB);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} elseif ( $dbType == 4 ) {
|
||
|
|
// MSSQL
|
||
|
|
$sql = 'SET SHOWPLAN_ALL ON';
|
||
|
|
$conn = @mssql_query($sql);
|
||
|
|
if ( $conn ) {
|
||
|
|
$conn = @mssql_query($query);
|
||
|
|
if ( $conn ) {
|
||
|
|
if(mssql_num_rows($conn)) {
|
||
|
|
|
||
|
|
$explainMsg = '<div class="wshelp">';
|
||
|
|
$explainMsg .= '<p><table>';
|
||
|
|
$explainMsg .= '<tr class="yellow">';
|
||
|
|
$explainMsg .= '<td>StmtTxt</td>';
|
||
|
|
$explainMsg .= '<td>StmtId</td>';
|
||
|
|
$explainMsg .= '<td>NodeId</td>';
|
||
|
|
$explainMsg .= '<td>Parent</td>';
|
||
|
|
$explainMsg .= '<td>PhysicalOp</td>';
|
||
|
|
$explainMsg .= '<td>LogicalOp</td>';
|
||
|
|
$explainMsg .= '<td>EstimateRows</td>';
|
||
|
|
$explainMsg .= '<td>EstimateCPU</td>';
|
||
|
|
$explainMsg .= '<td>AvgRowSize</td>';
|
||
|
|
$explainMsg .= '<td>TotalSubtreeCost</td>';
|
||
|
|
$explainMsg .= '</tr>';
|
||
|
|
while($row = mssql_fetch_assoc($conn)) {
|
||
|
|
$explainMsg .= '<tr>';
|
||
|
|
$explainMsg .= '<td>' . $row['StmtText'] . '</td>';
|
||
|
|
$explainMsg .= '<td>' . $row['StmtId'] . '</td>';
|
||
|
|
$explainMsg .= '<td>' . $row['NodeId'] . '</td>';
|
||
|
|
$explainMsg .= '<td>' . $row['Parent'] . '</td>';
|
||
|
|
$explainMsg .= '<td>' . $row['PhysicalOp'] . '</td>';
|
||
|
|
$explainMsg .= '<td>' . $row['LogicalOp'] . '</td>';
|
||
|
|
$explainMsg .= '<td>' . $row['EstimateRows'] . '</td>';
|
||
|
|
$explainMsg .= '<td>' . $row['EstimateCPU'] . '</td>';
|
||
|
|
$explainMsg .= '<td>' . $row['AvgRowSize'] . '</td>';
|
||
|
|
$explainMsg .= '<td>' . $row['TotalSubtreeCost'] . '</td>';
|
||
|
|
$explainMsg .= '</tr>';
|
||
|
|
}
|
||
|
|
$explainMsg .= '</table></p></div>';
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$explainMsg = JText::_('EXPLAIN_ERR_MSG');
|
||
|
|
$syntaxMsg = mssql_get_last_message();
|
||
|
|
}
|
||
|
|
$sql = 'SET SHOWPLAN_ALL OFF';
|
||
|
|
$conn = @mssql_query($sql);
|
||
|
|
} else {
|
||
|
|
$explainMsg = 'ERROR?';
|
||
|
|
}
|
||
|
|
|
||
|
|
} elseif ( $dbType == 5 ) {
|
||
|
|
// postgreSQL
|
||
|
|
$sql = 'explain ' . $query;
|
||
|
|
$res = @pg_query($explainDB, $sql);
|
||
|
|
|
||
|
|
if ( !res ) {
|
||
|
|
// Error
|
||
|
|
$syntaxMsg = 'ERROR! Is database availabe?';
|
||
|
|
$explainMsg = JText::_('EXPLAIN_ERR_MSG');
|
||
|
|
} else {
|
||
|
|
// OK
|
||
|
|
$syntaxMsg = '';
|
||
|
|
$explainMsg = '';
|
||
|
|
|
||
|
|
$explainStr = '';
|
||
|
|
while ($row = pg_fetch_row($res)) {
|
||
|
|
$explainStr .= '<tr><td>' . $row[0] . '</td></tr>';
|
||
|
|
}
|
||
|
|
if ( $explainStr == '' ) {
|
||
|
|
$sql = 'explain analyze ' . $query;
|
||
|
|
$res = @pg_query($explainDB, $sql);
|
||
|
|
$syntaxMsg = pg_last_error($explainDB);
|
||
|
|
$explainMsg = JText::_('EXPLAIN_ERR_MSG');
|
||
|
|
} else {
|
||
|
|
$syntaxMsg ='';
|
||
|
|
$explainMsg = '<table>' . $explainStr . '</table>';
|
||
|
|
$explainMsg = '<table><tr><th><tr><td><b>Explain Plan for : </b>' . $query . '</td></tr>' . $explainMsg . '</table>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} elseif ( $dbType == 6 ) {
|
||
|
|
// Custom Database Driver
|
||
|
|
$str = JText::_(' not yet supported for Custom Database Connections');
|
||
|
|
echo '<SYNTAX>Syntax check' . $str . '</SYNTAX>' . '<EXPLAIN>Explain' . $str . '</EXPLAIN>';
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
} else {
|
||
|
|
$syntaxMsg = JText::_('ERROR : Could not connect to the database!');
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( !$livesyntax ) {
|
||
|
|
$chckSyntax = '<p><a href="" onClick="return sqlChange(0);">' . JText::_('Check Syntax') . '</a></p>';
|
||
|
|
$syntaxMsg .= $chckSyntax ;
|
||
|
|
$chckExplain = '<p><a href="" onClick="return sqlChange(0);">' . JText::_('Explain SQL') . '</a></p>';
|
||
|
|
$explainMsg .= $chckSyntax ;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
echo '<SYNTAX>' . $syntaxMsg . '</SYNTAX>' . '<EXPLAIN>' . $explainMsg . '</EXPLAIN>';
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
function utf8_strrev($str, $reverse_numbers = true){
|
||
|
|
$pattern = $reverse_numbers ? '/./us' : '/(\d+)?./us';
|
||
|
|
preg_match_all($pattern, $str, $ar);
|
||
|
|
return join('',array_reverse($ar[0]));
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get tables from database
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
function getTables()
|
||
|
|
{
|
||
|
|
|
||
|
|
$post = JRequest::get('post');
|
||
|
|
$dbID = JRequest::getVar( 'db', '', 'post', 'int');
|
||
|
|
|
||
|
|
// Get database connection and info
|
||
|
|
$dbInfo = $this->getDB($dbID);
|
||
|
|
$tablesDB = $dbInfo[0];
|
||
|
|
$dbName = $dbInfo[1];
|
||
|
|
$dbType = $dbInfo[2];
|
||
|
|
|
||
|
|
if ( $tablesDB ) {
|
||
|
|
|
||
|
|
if ( $dbType <= 2 ) {
|
||
|
|
// Joomla DB or MySQL
|
||
|
|
$result = $tablesDB->getTableList();
|
||
|
|
$tableInfo = $dbName . '<br><div class="wshelp">';
|
||
|
|
$tableInfo .= '<table>';
|
||
|
|
foreach ( $result as $tableName ) {
|
||
|
|
$tableInfo .= '<tr><td><a href="" onClick="return getFields(currDB,\'' . $tableName . '\');">' . $tableName . '</a> ' . $this->insertStr($tableName, 'table') . '</td></tr>';
|
||
|
|
}
|
||
|
|
$tableInfo .= '</table></div>';
|
||
|
|
} elseif ( $dbType == 3 ) {
|
||
|
|
// Oracle
|
||
|
|
$query = 'SELECT TABLE_NAME FROM USER_TABLES';
|
||
|
|
$stmt = @oci_parse($tablesDB, $query);
|
||
|
|
|
||
|
|
if (!$stmt) {
|
||
|
|
$e = oci_error($db);
|
||
|
|
$tableInfo = $e['message'];
|
||
|
|
} else {
|
||
|
|
$res = oci_execute($stmt);
|
||
|
|
if (!$res) {
|
||
|
|
$e = oci_error($stmt);
|
||
|
|
$tableInfo = $e['message'] . ' | ' . $e['sqltext'];
|
||
|
|
} else {
|
||
|
|
$tableInfo = $dbName . '<br><div class="wshelp">';
|
||
|
|
$tableInfo .= '<table>';
|
||
|
|
|
||
|
|
$nrows = oci_fetch_all($stmt, $rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
|
||
|
|
foreach ( $rows as $row ) {
|
||
|
|
$tableInfo .= '<tr><td><a href="" onClick="return getFields(currDB,\'' . $row['TABLE_NAME'] . '\');">' . $row['TABLE_NAME'] . '</a> ' . $this->insertStr($row['TABLE_NAME'], 'table') . '</td></tr>';
|
||
|
|
|
||
|
|
}
|
||
|
|
oci_free_statement($stmt);
|
||
|
|
oci_close($tablesDB);
|
||
|
|
$tableInfo .= '</table></div>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} elseif ( $dbType == 4 ) {
|
||
|
|
// MSSQL
|
||
|
|
$tableInfo = 'MSSQL';
|
||
|
|
|
||
|
|
$query = 'select name from sysobjects where xtype=\'U\'';
|
||
|
|
$conn = @mssql_query($query);
|
||
|
|
|
||
|
|
if ( $conn ) {
|
||
|
|
if(!mssql_num_rows($conn)) {
|
||
|
|
$tableInfo = ''; // No tables
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
$tableInfo = $dbName . '<br><div class="wshelp">';
|
||
|
|
$tableInfo .= '<table>';
|
||
|
|
$i=0;
|
||
|
|
while($row = mssql_fetch_assoc($conn)) {
|
||
|
|
$tableInfo .= '<tr><td><a href="" onClick="return getFields(currDB,\'' . $row['name'] . '\');">' . $row['name'] . '</a> ' . $this->insertStr($row['name'], 'table') . '</td></tr>';
|
||
|
|
}
|
||
|
|
$tableInfo .= '</table></div>';
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$tableInfo = JText::_( 'SQL Error') . ' : ' . mssql_get_last_message();
|
||
|
|
}
|
||
|
|
|
||
|
|
} elseif ( $dbType == 5 ) {
|
||
|
|
// postgreSQL
|
||
|
|
$query = "select tablename from pg_tables where tablename !~ '^pg_+' order by tablename";
|
||
|
|
$res = @pg_query($tablesDB, $query);
|
||
|
|
$tableInfo = $dbName . '<div class="wshelp">';
|
||
|
|
$tableInfo .= '<table>';
|
||
|
|
while ($row = pg_fetch_row($res)) {
|
||
|
|
if ( strlen($row[0]) > 0 ) {
|
||
|
|
$tableInfo .= '<tr><td><a href="" onClick="return getFields(currDB,\'' . $row[0] . '\');">' . $row[0] . '</a> ' . $this->insertStr($row[0], 'table') . '</td></tr>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$tableInfo .= '</table></div>';
|
||
|
|
|
||
|
|
} elseif ( $dbType == 6 ) {
|
||
|
|
$tableInfo = 'Table information not yet supported for Custom Database Connections';
|
||
|
|
}
|
||
|
|
|
||
|
|
} else {
|
||
|
|
$tableInfo = JText::_('ERROR : Could not connect to the database!');
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
echo '<TABLEDATA>' . $tableInfo . '</TABLEDATA>';
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
function insertStr($str, $what) {
|
||
|
|
return '<a href="" onClick="return insertStr(\'' . $str . '\');"><img src="components/com_sql2excel/assets/images/plus.gif" border=0 alt="' . JText::_('Insert') . ' ' . JText::_($what) . ' ' . JText::_('into query') . '"></a>';
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get columns from a table
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
function getColumns()
|
||
|
|
{
|
||
|
|
|
||
|
|
$post = JRequest::get('post');
|
||
|
|
$dbID = JRequest::getVar( 'db', '', 'post', 'int');
|
||
|
|
$tableName = JRequest::getVar( 'table', '', 'post', 'string');
|
||
|
|
|
||
|
|
// Get database connection and info
|
||
|
|
$dbInfo = $this->getDB($dbID);
|
||
|
|
$tablesDB = $dbInfo[0];
|
||
|
|
$dbName = $dbInfo[1];
|
||
|
|
$dbType = $dbInfo[2];
|
||
|
|
|
||
|
|
|
||
|
|
if ( $tablesDB && $tableName != "" ) {
|
||
|
|
|
||
|
|
if ( $dbType <= 2 ) {
|
||
|
|
// Joomla DB or MySQL
|
||
|
|
$result = $tablesDB->getTableFields($tableName);
|
||
|
|
|
||
|
|
$tableInfo = '<a href="" onClick="getTables(currDB);return false">' . $dbName . '</a> > ' . $tableName . ' ' . $this->insertStr($tableName, 'table') . '<br>';
|
||
|
|
$tableInfo .= '<div class="wshelp">';
|
||
|
|
|
||
|
|
if ( $result && isset($result[$tableName]) ) {
|
||
|
|
$result = $result[$tableName];
|
||
|
|
$tableInfo .= '<p><table>';
|
||
|
|
$tableInfo .= '<tr class="yellow">';
|
||
|
|
$tableInfo .= '<td>Column</td>';
|
||
|
|
$tableInfo .= '<td>Type</td>';
|
||
|
|
$tableInfo .= '</tr>';
|
||
|
|
|
||
|
|
$colNames = array_keys($result);
|
||
|
|
foreach ( $colNames as $col ) {
|
||
|
|
$tableInfo .= '<tr><td>' . $col . ' <a href="" onClick="return insertStr(\'' . $col . '\');"><img src="components/com_sql2excel/assets/images/plus.gif" border=0 alt="' . JText::_('Insert column into query') . '"></a></td><td>' . $result[$col] . '</td></tr>';
|
||
|
|
}
|
||
|
|
$tableInfo .= '</table></p></div>';
|
||
|
|
}
|
||
|
|
} elseif ( $dbType == 3 ) {
|
||
|
|
// Oracle
|
||
|
|
$query = 'SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, NULLABLE ' .
|
||
|
|
'FROM user_tab_columns ' .
|
||
|
|
'WHERE table_name=\'' . $tableName . '\'';
|
||
|
|
|
||
|
|
$stmt = @oci_parse($tablesDB, $query);
|
||
|
|
if (!$stmt) {
|
||
|
|
$e = oci_error($db);
|
||
|
|
$tableInfo = $e['message'];
|
||
|
|
} else {
|
||
|
|
$res = oci_execute($stmt);
|
||
|
|
if (!$res) {
|
||
|
|
$e = oci_error($stmt);
|
||
|
|
$tableInfo = $e['message'] . ' | ' . $e['sqltext'];
|
||
|
|
} else {
|
||
|
|
$tableInfo = '<a href="" onClick="getTables(currDB);return false">' . $dbName . '</a> > ' . $tableName . ' ' . $this->insertStr($tableName, 'table') . '<br>';
|
||
|
|
$tableInfo .= '<div class="wshelp">';
|
||
|
|
|
||
|
|
$nrows = oci_fetch_all($stmt, $rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
|
||
|
|
if ( $nrows > 0 ) {
|
||
|
|
$tableInfo .= '<p><table>';
|
||
|
|
$tableInfo .= '<tr class="yellow">';
|
||
|
|
$tableInfo .= '<td>Column</td>';
|
||
|
|
$tableInfo .= '<td>Type</td>';
|
||
|
|
$tableInfo .= '</tr>';
|
||
|
|
foreach ( $rows as $row ) {
|
||
|
|
$tableInfo .= '<tr><td><a href="" onClick="return getFields(currDB,\'' . $row['COLUMN_NAME'] . '\');">' . $row['COLUMN_NAME'] . '</a> ' . $this->insertStr($row['COLUMN_NAME'], 'column') . '</td><td>' . $row['DATA_TYPE'] . '</td></tr>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
oci_free_statement($stmt);
|
||
|
|
oci_close($tablesDB);
|
||
|
|
$tableInfo .= '</table></p></div>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} elseif ( $dbType == 4 ) {
|
||
|
|
// MSSQL
|
||
|
|
$query = 'SELECT column_name, data_type ' .
|
||
|
|
'FROM information_schema.columns ' .
|
||
|
|
'WHERE table_name=\'' . $tableName . '\'';
|
||
|
|
|
||
|
|
$conn = @mssql_query($query);
|
||
|
|
|
||
|
|
if ( $conn ) {
|
||
|
|
$tableInfo = '<a href="" onClick="getTables(currDB);return false">' . $dbName . '</a> > ' . $tableName . ' ' . $this->insertStr($tableName, 'table') . '<br>';
|
||
|
|
$tableInfo .= '<div class="wshelp">';
|
||
|
|
|
||
|
|
if(!mssql_num_rows($conn)) {
|
||
|
|
$tableInfo = ''; // No columns found !?
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
$tableInfo .= '<p><table>';
|
||
|
|
$tableInfo .= '<tr class="yellow">';
|
||
|
|
$tableInfo .= '<td>Column</td>';
|
||
|
|
$tableInfo .= '<td>Type</td>';
|
||
|
|
$tableInfo .= '</tr>';
|
||
|
|
while($row = mssql_fetch_assoc($conn)) {
|
||
|
|
$tableInfo .= '<tr><td><a href="" onClick="return getFields(currDB,\'' . $row['column_name'] . '\');">' . $row['column_name'] . '</a> ' . $this->insertStr($row['column_name'], 'column') . '</td><td>' . $row['data_type'] . '</td></tr>';
|
||
|
|
}
|
||
|
|
$tableInfo .= '</table></div>';
|
||
|
|
}
|
||
|
|
|
||
|
|
$tableInfo .= '</table></p></div>';
|
||
|
|
|
||
|
|
} else {
|
||
|
|
$tableInfo = JText::_( 'SQL Error') . ' : ' . mssql_get_last_message();
|
||
|
|
}
|
||
|
|
} elseif ( $dbType == 5 ) {
|
||
|
|
// postgreSQL
|
||
|
|
|
||
|
|
$query = 'SELECT a.attname AS field, t.typname AS type, a.attlen AS length, a.atttypmod AS lengthvar, a.attnotnull AS notnull ' .
|
||
|
|
'FROM pg_class c, pg_attribute a, pg_type t ' .
|
||
|
|
"WHERE c.relname = '" . $tableName . "' " .
|
||
|
|
'and a.attnum > 0 ' .
|
||
|
|
'and a.attrelid = c.oid ' .
|
||
|
|
'and a.atttypid = t.oid ' .
|
||
|
|
'ORDER BY a.attnum';
|
||
|
|
|
||
|
|
$tableInfo = '<a href="" onClick="getTables(currDB);return false">' . $dbName . '</a> > ' . $tableName . ' ' . $this->insertStr($tableName, 'table') . '<br>';
|
||
|
|
$tableInfo .= '<div class="wshelp">';
|
||
|
|
|
||
|
|
$res = @pg_query($tablesDB, $query);
|
||
|
|
if ( !$res ) {
|
||
|
|
$tableInfo = ''; // No columns found !?
|
||
|
|
} else {
|
||
|
|
$tableInfo .= '<p><table>';
|
||
|
|
$tableInfo .= '<tr class="yellow">';
|
||
|
|
$tableInfo .= '<td>Column</td>';
|
||
|
|
$tableInfo .= '<td>Type</td>';
|
||
|
|
$tableInfo .= '</tr>';
|
||
|
|
while ($row = pg_fetch_row($res)) {
|
||
|
|
if ( strlen($row[0]) > 0 ) {
|
||
|
|
$tableInfo .= '<tr><td>' . $row[0] . ' <a href="" onClick="return insertStr(\'' . $row[0] . '\');"><img src="components/com_sql2excel/assets/images/plus.gif" border=0 alt="' . JText::_('Insert column into query') . '"></a></td><td>' . $row[1] . '</td></tr>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$tableInfo .= '</table></div>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} else {
|
||
|
|
$tableInfo = JText::_('ERROR : Could not connect to the database!');
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
echo '<TABLEDATA>' . $tableInfo . '</TABLEDATA>';
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
function getDB($dbID) {
|
||
|
|
|
||
|
|
$db = & JFactory::getDBO();
|
||
|
|
$mydb = $db;
|
||
|
|
$dbName = 'Joomla!';
|
||
|
|
$dbType = 1;
|
||
|
|
|
||
|
|
if ( $dbID > 1 ) {
|
||
|
|
// Get the database information
|
||
|
|
$db->setQuery( 'SELECT * FROM #__sql2excel_databases WHERE ID=' . $dbID );
|
||
|
|
$dbInfo = $db->loadObjectList();
|
||
|
|
if ( $dbInfo ) {
|
||
|
|
$dbInfo = $dbInfo[0];
|
||
|
|
$dbType = $dbInfo->db_type;
|
||
|
|
$dbName = $dbInfo->db_name;
|
||
|
|
$dbPrefix = $dbInfo->db_prefix;
|
||
|
|
|
||
|
|
// Get Database Connection
|
||
|
|
require_once JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'excel_writer.php';
|
||
|
|
$mydb = writeExcel::getDB($dbInfo);
|
||
|
|
if ( get_class($explainDB) == 'JException' ) {
|
||
|
|
$mydb = null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return array($mydb, $dbName, $dbType, $dbPrefix);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|