PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/exchange/code/trunk/administrator/components/com_exchange/adapters/export/sql.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 104 lines | 67 code | 13 blank | 24 comment | 7 complexity | 0312551a11db13f1d2c372471f198658 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: sql.php 280 2010-09-18 02:14:15Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_exchange
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License version 2 or later.
  8. * @link http://www.theartofjoomla.com
  9. */
  10. // no direct access
  11. defined('_JEXEC') or die;
  12. /**
  13. * @package NewLifeInIT
  14. * @subpackage com_exchange
  15. */
  16. class ExportAdapterSQL
  17. {
  18. /**
  19. * Creates an XMI document that can be imported into Visual Paradigm
  20. */
  21. function export(&$tables, &$table_fields, &$table_creates, &$options)
  22. {
  23. $source = JArrayHelper::getValue($options, 'source', '');
  24. $sourceStructure = eregi('s', $source);
  25. $sourceData = eregi('d', $source);
  26. $buffer = '';
  27. foreach ($tables as $table) {
  28. if ($sourceStructure) {
  29. $buffer .= "#\n# Structure for table `$table`\n#\n";
  30. $buffer .= $this->_createTableStructure($table, $table_creates[$table], $options);
  31. $buffer .= "\n\n";
  32. }
  33. if ($sourceData) {
  34. $buffer .= "#\n# Data for table `$table`\n#\n";
  35. $buffer .= $this->_createTableData($table, $table_fields[$table], $options);
  36. $buffer .= "\n\n";
  37. }
  38. }
  39. return $buffer;
  40. }
  41. /**
  42. * @param string The table name
  43. * @param array The create syntax for the tables
  44. * @param boolean A switch to add the DROP TABLE systax (if true)
  45. */
  46. function _createTableStructure($table, &$create, $options=array())
  47. {
  48. $dropTables = JArrayHelper::getValue($options, 'droptables', 0);
  49. $buffer = '';
  50. if ($dropTables) {
  51. $buffer .= 'DROP TABLE IF EXISTS ' . $table . ";\n";
  52. }
  53. $buffer .= $create . ';';
  54. return $buffer;
  55. }
  56. /**
  57. *
  58. */
  59. function _createTableData($table, &$fields, $options=array())
  60. {
  61. $database = &JFactory::getDBO();
  62. $numbers = 'DATE TIME DATETIME CHAR VARCHAR TEXT TINYTEXT MEDIUMTEXT LONGTEXT BLOB TINYBLOB MEDIUMBLOB LONGBLOB ENUM SET';
  63. $completeInserts = JArrayHelper::getValue($options, 'cinsert', 0);
  64. $fieldNames = '';
  65. if ($completeInserts) {
  66. $fieldNames = '(`';
  67. $fieldNames .= implode('`,`', array_keys($fields));
  68. $fieldNames .= '`)';
  69. }
  70. $database->setQuery('SELECT * FROM ' . $table);
  71. $rows = $database->loadAssocList();
  72. $buffer = '';
  73. foreach ($rows as $row) {
  74. $buffer .= 'INSERT INTO ' . $table . ' ' . $fieldNames . ' VALUES ';
  75. $values = array();
  76. foreach ($row as $key => $value) {
  77. $value = addslashes($value);
  78. $value = str_replace("\n", '\r\n', $value);
  79. $value = str_replace("\r", '', $value);
  80. if (preg_match("/\b" . $fields[$key] . "\b/i", $numbers)) {
  81. $value = "'$value'";
  82. } else if ($value === '') {
  83. $value = 'null';
  84. }
  85. $values[] = $value;
  86. }
  87. $buffer .= '(' . implode(',', $values) . ");\n";
  88. }
  89. return $buffer;
  90. }
  91. }