[ Index ]
 

Code source de phpMyAdmin 2.10.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/ -> pmd_relation_new.php (source)

   1  <?php
   2  /* $Id: pmd_relation_new.php 9828 2007-01-05 17:30:36Z lem9 $ */
   3  // vim: expandtab sw=4 ts=4 sts=4:
   4  
   5  include_once  'pmd_common.php';
   6  $die_save_pos = 0;
   7  include_once  'pmd_save_pos.php';
   8  require_once  './libraries/relation.lib.php';
   9  extract($_POST); 
  10  
  11  $tables = PMA_DBI_get_tables_full($db, $T1);
  12  $type_T1 = strtoupper($tables[$T1]['ENGINE']);
  13  $tables = PMA_DBI_get_tables_full($db, $T2);
  14  //print_r($tables);
  15  //die();
  16  $type_T2 = strtoupper($tables[$T2]['ENGINE']);
  17  
  18  //  I n n o D B
  19  if ($type_T1 == 'INNODB' and $type_T2 == 'INNODB') {
  20      // relation exists?
  21      $existrel_innodb = PMA_getForeigners($db, $T2, '', 'innodb'); 
  22      if (isset($existrel_innodb[$F2]) 
  23       && isset($existrel_innodb[$F2]['constraint'])) {
  24           PMD_return(0,'strErrorRelationExists');
  25      }
  26  // note: in InnoDB, the index does not requires to be on a PRIMARY
  27  // or UNIQUE key
  28  // improve: check all other requirements for InnoDB relations
  29      $result      = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T1) . ';');
  30      $index_array1   = array(); // will be use to emphasis prim. keys in the table view
  31      while ($row = PMA_DBI_fetch_assoc($result))
  32          $index_array1[$row['Column_name']] = 1;
  33      PMA_DBI_free_result($result);
  34    
  35      $result     = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T2) . ';');
  36      $index_array2  = array(); // will be used to emphasis prim. keys in the table view
  37      while ($row = PMA_DBI_fetch_assoc($result)) 
  38          $index_array2[$row['Column_name']] = 1;
  39      PMA_DBI_free_result($result);
  40  
  41      if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
  42          $upd_query  = 'ALTER TABLE ' . PMA_backquote($T2)
  43                   . ' ADD FOREIGN KEY ('
  44                   . PMA_backquote($F2) . ')'
  45                   . ' REFERENCES '
  46                   . PMA_backquote($db) . '.'
  47                   . PMA_backquote($T1) . '('
  48                   . PMA_backquote($F1) . ')';
  49  
  50          if ($on_delete != 'nix') { 
  51              $upd_query   .= ' ON DELETE ' . $on_delete;
  52          }
  53          if ($on_update != 'nix') {
  54              $upd_query   .= ' ON UPDATE ' . $on_update;
  55          }
  56          PMA_DBI_try_query($upd_query) or PMD_return(0,'strErrorRelationAdded');
  57      PMD_return(1,'strInnoDBRelationAdded');
  58      }
  59  
  60  //  n o n - I n n o D B
  61  } else {
  62      if ($GLOBALS['cfgRelation']['relwork'] == false) {
  63          PMD_return(0, 'strGeneralRelationFeat:strDisabled');
  64      } else {
  65          // no need to recheck if the keys are primary or unique at this point,
  66          // this was checked on the interface part
  67  
  68          $q  = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
  69                              . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
  70                              . ' values('
  71                              . '\'' . PMA_sqlAddslashes($db) . '\', '
  72                              . '\'' . PMA_sqlAddslashes($T2) . '\', '
  73                              . '\'' . PMA_sqlAddslashes($F2) . '\', '
  74                              . '\'' . PMA_sqlAddslashes($db) . '\', '
  75                              . '\'' . PMA_sqlAddslashes($T1) . '\','
  76                              . '\'' . PMA_sqlAddslashes($F1) . '\')';
  77          
  78          if (PMA_query_as_cu( $q , false, PMA_DBI_QUERY_STORE)) {
  79              PMD_return(1, 'strInternalRelationAdded'); 
  80          } else {
  81              PMD_return(0, 'strErrorRelationAdded'); 
  82          }
  83     }
  84  }
  85  
  86  function PMD_return($b,$ret)
  87  {
  88      global $db,$T1,$F1,$T2,$F2;
  89      header("Content-Type: text/xml; charset=utf-8");//utf-8 .$_GLOBALS['charset']
  90      header("Cache-Control: no-cache");
  91      die('<root act="relation_new" return="'.$ret.'" b="'.$b.
  92      '" DB1="'.urlencode($db).
  93      '" T1="'.urlencode($T1).
  94      '" F1="'.urlencode($F1).
  95      '" DB2="'.urlencode($db).
  96      '" T2="'.urlencode($T2).
  97      '" F2="'.urlencode($F2).
  98      '"></root>');
  99  }
 100  ?>


Généré le : Mon Nov 26 15:18:20 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics