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('', '',$explainMsg); } else { $syntaxMsg = $explainDB->getErrorMsg(); $syntaxMsg = str_replace('SQL=EXPLAIN ' , ' :

', $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 = '

';	
								$nrows = oci_fetch_all($stmt, $rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
								foreach ( $rows as $row ) {
									$explainMsg .= $row["Operation"] . ' ' . $row["Object"] . "
"; } $explainMsg .= '
'; 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 = '
'; $explainMsg .= '

'; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; while($row = mssql_fetch_assoc($conn)) { $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; $explainMsg .= ''; } $explainMsg .= '
StmtTxtStmtIdNodeIdParentPhysicalOpLogicalOpEstimateRowsEstimateCPUAvgRowSizeTotalSubtreeCost
' . $row['StmtText'] . '' . $row['StmtId'] . '' . $row['NodeId'] . '' . $row['Parent'] . '' . $row['PhysicalOp'] . '' . $row['LogicalOp'] . '' . $row['EstimateRows'] . '' . $row['EstimateCPU'] . '' . $row['AvgRowSize'] . '' . $row['TotalSubtreeCost'] . '

'; } } 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 .= '' . $row[0] . ''; } if ( $explainStr == '' ) { $sql = 'explain analyze ' . $query; $res = @pg_query($explainDB, $sql); $syntaxMsg = pg_last_error($explainDB); $explainMsg = JText::_('EXPLAIN_ERR_MSG'); } else { $syntaxMsg =''; $explainMsg = '' . $explainStr . '
'; $explainMsg = '' . $explainMsg . '
Explain Plan for : ' . $query . '
'; } } } elseif ( $dbType == 6 ) { // Custom Database Driver $str = JText::_(' not yet supported for Custom Database Connections'); echo 'Syntax check' . $str . '' . 'Explain' . $str . ''; exit; } } else { $syntaxMsg = JText::_('ERROR : Could not connect to the database!'); } if ( !$livesyntax ) { $chckSyntax = '

' . JText::_('Check Syntax') . '

'; $syntaxMsg .= $chckSyntax ; $chckExplain = '

' . JText::_('Explain SQL') . '

'; $explainMsg .= $chckSyntax ; } echo '' . $syntaxMsg . '' . '' . $explainMsg . ''; 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 . '
'; $tableInfo .= ''; foreach ( $result as $tableName ) { $tableInfo .= ''; } $tableInfo .= '
' . $tableName . ' ' . $this->insertStr($tableName, 'table') . '
'; } 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 . '
'; $tableInfo .= ''; $nrows = oci_fetch_all($stmt, $rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW); foreach ( $rows as $row ) { $tableInfo .= ''; } oci_free_statement($stmt); oci_close($tablesDB); $tableInfo .= '
' . $row['TABLE_NAME'] . ' ' . $this->insertStr($row['TABLE_NAME'], 'table') . '
'; } } } 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 . '
'; $tableInfo .= ''; $i=0; while($row = mssql_fetch_assoc($conn)) { $tableInfo .= ''; } $tableInfo .= '
' . $row['name'] . ' ' . $this->insertStr($row['name'], 'table') . '
'; } } 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 . '
'; $tableInfo .= ''; while ($row = pg_fetch_row($res)) { if ( strlen($row[0]) > 0 ) { $tableInfo .= ''; } } $tableInfo .= '
' . $row[0] . ' ' . $this->insertStr($row[0], 'table') . '
'; } elseif ( $dbType == 6 ) { $tableInfo = 'Table information not yet supported for Custom Database Connections'; } } else { $tableInfo = JText::_('ERROR : Could not connect to the database!'); } echo '' . $tableInfo . ''; } function insertStr($str, $what) { return '' . JText::_('Insert') . ' ' . JText::_($what) . ' ' . JText::_('into query') . ''; } /** * 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 = '' . $dbName . ' > ' . $tableName . ' ' . $this->insertStr($tableName, 'table') . '
'; $tableInfo .= '
'; if ( $result && isset($result[$tableName]) ) { $result = $result[$tableName]; $tableInfo .= '

'; $tableInfo .= ''; $tableInfo .= ''; $tableInfo .= ''; $tableInfo .= ''; $colNames = array_keys($result); foreach ( $colNames as $col ) { $tableInfo .= ''; } $tableInfo .= '
ColumnType
' . $col . ' ' . JText::_('Insert column into query') . '' . $result[$col] . '

'; } } 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 = '' . $dbName . ' > ' . $tableName . ' ' . $this->insertStr($tableName, 'table') . '
'; $tableInfo .= '
'; $nrows = oci_fetch_all($stmt, $rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW); if ( $nrows > 0 ) { $tableInfo .= '

'; $tableInfo .= ''; $tableInfo .= ''; $tableInfo .= ''; $tableInfo .= ''; foreach ( $rows as $row ) { $tableInfo .= ''; } } oci_free_statement($stmt); oci_close($tablesDB); $tableInfo .= '
ColumnType
' . $row['COLUMN_NAME'] . ' ' . $this->insertStr($row['COLUMN_NAME'], 'column') . '' . $row['DATA_TYPE'] . '

'; } } } 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 = '' . $dbName . ' > ' . $tableName . ' ' . $this->insertStr($tableName, 'table') . '
'; $tableInfo .= '
'; if(!mssql_num_rows($conn)) { $tableInfo = ''; // No columns found !? } else { $tableInfo .= '

'; $tableInfo .= ''; $tableInfo .= ''; $tableInfo .= ''; $tableInfo .= ''; while($row = mssql_fetch_assoc($conn)) { $tableInfo .= ''; } $tableInfo .= '
ColumnType
' . $row['column_name'] . ' ' . $this->insertStr($row['column_name'], 'column') . '' . $row['data_type'] . '

'; } $tableInfo .= '

'; } 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 = '' . $dbName . ' > ' . $tableName . ' ' . $this->insertStr($tableName, 'table') . '
'; $tableInfo .= '
'; $res = @pg_query($tablesDB, $query); if ( !$res ) { $tableInfo = ''; // No columns found !? } else { $tableInfo .= '

'; $tableInfo .= ''; $tableInfo .= ''; $tableInfo .= ''; $tableInfo .= ''; while ($row = pg_fetch_row($res)) { if ( strlen($row[0]) > 0 ) { $tableInfo .= ''; } } $tableInfo .= '
ColumnType
' . $row[0] . ' ' . JText::_('Insert column into query') . '' . $row[1] . '

'; } } } else { $tableInfo = JText::_('ERROR : Could not connect to the database!'); } echo '' . $tableInfo . ''; } 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); } }