/exchange/code/trunk/administrator/components/com_exchange/adapters/export/sql.php
PHP | 104 lines | 67 code | 13 blank | 24 comment | 7 complexity | 0312551a11db13f1d2c372471f198658 MD5 | raw file
- <?php
- /**
- * @version $Id: sql.php 280 2010-09-18 02:14:15Z eddieajau $
- * @package NewLifeInIT
- * @subpackage com_exchange
- * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License version 2 or later.
- * @link http://www.theartofjoomla.com
- */
- // no direct access
- defined('_JEXEC') or die;
- /**
- * @package NewLifeInIT
- * @subpackage com_exchange
- */
- class ExportAdapterSQL
- {
- /**
- * Creates an XMI document that can be imported into Visual Paradigm
- */
- function export(&$tables, &$table_fields, &$table_creates, &$options)
- {
- $source = JArrayHelper::getValue($options, 'source', '');
- $sourceStructure = eregi('s', $source);
- $sourceData = eregi('d', $source);
- $buffer = '';
- foreach ($tables as $table) {
- if ($sourceStructure) {
- $buffer .= "#\n# Structure for table `$table`\n#\n";
- $buffer .= $this->_createTableStructure($table, $table_creates[$table], $options);
- $buffer .= "\n\n";
- }
- if ($sourceData) {
- $buffer .= "#\n# Data for table `$table`\n#\n";
- $buffer .= $this->_createTableData($table, $table_fields[$table], $options);
- $buffer .= "\n\n";
- }
- }
- return $buffer;
- }
- /**
- * @param string The table name
- * @param array The create syntax for the tables
- * @param boolean A switch to add the DROP TABLE systax (if true)
- */
- function _createTableStructure($table, &$create, $options=array())
- {
- $dropTables = JArrayHelper::getValue($options, 'droptables', 0);
- $buffer = '';
- if ($dropTables) {
- $buffer .= 'DROP TABLE IF EXISTS ' . $table . ";\n";
- }
- $buffer .= $create . ';';
- return $buffer;
- }
- /**
- *
- */
- function _createTableData($table, &$fields, $options=array())
- {
- $database = &JFactory::getDBO();
- $numbers = 'DATE TIME DATETIME CHAR VARCHAR TEXT TINYTEXT MEDIUMTEXT LONGTEXT BLOB TINYBLOB MEDIUMBLOB LONGBLOB ENUM SET';
- $completeInserts = JArrayHelper::getValue($options, 'cinsert', 0);
- $fieldNames = '';
- if ($completeInserts) {
- $fieldNames = '(`';
- $fieldNames .= implode('`,`', array_keys($fields));
- $fieldNames .= '`)';
- }
- $database->setQuery('SELECT * FROM ' . $table);
- $rows = $database->loadAssocList();
- $buffer = '';
- foreach ($rows as $row) {
- $buffer .= 'INSERT INTO ' . $table . ' ' . $fieldNames . ' VALUES ';
- $values = array();
- foreach ($row as $key => $value) {
- $value = addslashes($value);
- $value = str_replace("\n", '\r\n', $value);
- $value = str_replace("\r", '', $value);
- if (preg_match("/\b" . $fields[$key] . "\b/i", $numbers)) {
- $value = "'$value'";
- } else if ($value === '') {
- $value = 'null';
- }
- $values[] = $value;
- }
- $buffer .= '(' . implode(',', $values) . ");\n";
- }
- return $buffer;
- }
- }