<?php

function SYNC_MYDATABASE_SQIX($CNF_ZASOB_SRC,$src_tables_array,$CNF_ZASOB_DST) {
global $CNF_ZASOB;

echo "\nF.SYNC_MYDATABASE_SQIX($CNF_ZASOB_SRC,$src_tables_array,$CNF_ZASOB_DST)";

if ($CNF_ZASOB_SRC == 14 && empty($CNF_ZASOB['14'])) {
	global $INTERFACE_ADDR, $SQL_USER, $SQL_USER_PASS, $SQL_DATABASE;
	$CNF_ZASOB['14']['INTERFACE_ADDR'] = $INTERFACE_ADDR;
	$CNF_ZASOB['14']['SQL_USER'] = $SQL_USER;
	$CNF_ZASOB['14']['SQL_USER_PASS'] = $SQL_USER_PASS;
	$CNF_ZASOB['14']['SQL_DATABASE'] = $SQL_DATABASE;
}

//print_r($CNF_ZASOB);
//$TABLES=array('USERS2','KIKE_AUTH','IN7_MK_BAZA_DYSTRYBUCJI','IN7_DZIENNIK_KORESP','229AnkietaZakup','229Wezly','229Uczestnicy','229Trasa','KONFERENCJE_USERS');
//print_r($CNF_ZASOB);

	$src_db_conn=mysql_pconnect($CNF_ZASOB[$CNF_ZASOB_SRC]['INTERFACE_ADDR'],$CNF_ZASOB[$CNF_ZASOB_SRC]['SQL_USER'],$CNF_ZASOB[$CNF_ZASOB_SRC]['SQL_USER_PASS']) or die("Can not connect to 719-1 !".$CNF_ZASOB_SRC." ".$CNF_ZASOB[$CNF_ZASOB_SRC]['INTERFACE_ADDR']." ".$CNF_ZASOB[$CNF_ZASOB_SRC]['SQL_USER']);
	mysql_select_db($CNF_ZASOB[$CNF_ZASOB_SRC]['SQL_DATABASE'],$src_db_conn) or die("ERROR podczas wyboru bazy R50A!:".$CNF_ZASOB_SRC."/".$CNF_ZASOB[$CNF_ZASOB_SRC]['SQL_DATABASE']);


$dst_db_conn=mysql_pconnect($CNF_ZASOB[$CNF_ZASOB_DST]['INTERFACE_ADDR'],$CNF_ZASOB[$CNF_ZASOB_DST]['SQL_USER'],$CNF_ZASOB[$CNF_ZASOB_DST]['SQL_USER_PASS']) or die("Can not connect to 719-2 !".$CNF_ZASOB_DST);
	mysql_select_db($CNF_ZASOB[$CNF_ZASOB_DST]['SQL_DATABASE'],$dst_db_conn) or die("ERROR podczas wyboru bazy R50B!:"."/".$CNF_ZASOB[$CNF_ZASOB_DST]['SQL_DATABASE']);




foreach($src_tables_array as $TABLE) { //foreach 1 

 //odczytujemy strukture
 $zap_sql1_SE="show fields from ".$TABLE." ;" ;
 echo "\n".$zap_sql1_SE;
 $r1=mysql_query($zap_sql1_SE,$src_db_conn); 

   $ZAP_REMOTE_CREATE="";
   $COLUMNS = array();
 while($R1=mysql_fetch_object($r1)) {
//  print_r($R1);
  if($R1->Type=='text') $TYPE=' text ' ; 
  else if(strstr($R1->Type,'int(')) $TYPE=$R1->Type ; 
  else    $TYPE=' varchar(255) ';

  if($ZAP_REMOTE_CREATE) $ZAP_REMOTE_CREATE.=" , `".$R1->Field."` ".$TYPE." "; 
  else $ZAP_REMOTE_CREATE="create table ".$TABLE." (`".$R1->Field."` ".$TYPE." "; 
  $COLUMNS[]=$R1->Field;
 }
	$ZAP_REMOTE_CREATE.=" ) 
	";
	
	

	
//flushujemy tabele USERS2;
$zap_sql1_DUO="drop table if exists ".$TABLE." ;";
 echo "\n".$zap_sql1_DUO;
 mysql_query($zap_sql1_DUO,$dst_db_conn) or die("DIE1:".$zap_sql1_DUO);
 //mysql_query($zap_sql1_CREATE,$conn_DUO) or die($zap_sql1_DUO);
 echo "\n ".$ZAP_REMOTE_CREATE;
 mysql_query($ZAP_REMOTE_CREATE,$dst_db_conn) or die("DIE2:".$ZAP_REMOTE_CREATE);
// mysql_query($ZAP_REMOTE_CREATE,$conn_GRONET) or die($ZAP_REMOTE_CREATE);

 //indeksy !?
 $zap_sql1_SE="show indexes from ".$TABLE." ;" ;
 echo "\n".$zap_sql1_SE;
 $ZAP_REMOTE_INDEX_CREATE="";
 $r1=mysql_query($zap_sql1_SE,$src_db_conn); 
 while($R1=mysql_fetch_object($r1)) {
  //print_r($R1);
  	if($R1->Sub_part>0)   $ZAP_REMOTE_INDEX_CREATE="alter table ".$TABLE." add index(".$R1->Column_name."(".$R1->Sub_part."))" ; //jest dlugosc klucza
  	else  $ZAP_REMOTE_INDEX_CREATE="alter table ".$TABLE." add index(".$R1->Column_name.")" ;
  echo $ZAP_REMOTE_INDEX_CREATE." \n";
   mysql_query($ZAP_REMOTE_INDEX_CREATE,$dst_db_conn) or die("\nDIE2:".$ZAP_REMOTE_INDEX_CREATE."\n");
   flush();
 }
	



// mysql_query($ZAP_REMOTE_CREATE,$conn_GRONET) or die($ZAP_REMOTE_CREATE);
//w petli wprowadzamy rekordy z GLWONEK do DUO
 $zap_sql1_SE="select * from ".$TABLE." ;";
 $r1_SE=mysql_query($zap_sql1_SE,$src_db_conn);
 $test_i=0;
 
 while($R1_SE=mysql_fetch_object($r1_SE)) { //while L65
  $INSERT = array();
	  $test_i++;
	 if($test_i==100) {
	  flush();
	  $test_i=0;
	  echo ".";
	 }
     foreach($COLUMNS as $COLUMN) { //foreach L67
         if(empty($INSERT)) {
           $INSERT['VALS']=" insert into ".$TABLE." ( `".$COLUMN."`" ;
           $INSERT['COLS']=" '".addslashes($R1_SE->$COLUMN)."' " ;
         } else {
           $INSERT['VALS'].=" , `".$COLUMN."`" ;
           $INSERT['COLS'].=" , '".addslashes($R1_SE->$COLUMN)."' ";
         }
  
     }//eof foreach L67
          $INSERT['SQL']=$INSERT['VALS']." ) values ( ".$INSERT['COLS']." ); ";

//          echo "\n".$INSERT['SQL'];     
//     print_r($INSERT);
        mysql_query($INSERT['SQL'],$dst_db_conn) or die("\nProblem z INSERT ".$INSERT['SQL']. " error: ".mysql_error());  
//        mysql_query($INSERT['SQL'],$conn_GRONET) or die("\nProblem z INSERT ".$INSERT['SQL']);  
 
 } //eof while L65
 
 
 $ZAP_REMOTE_INDEX="alter table ".$TABLE." add index(ID)";
 mysql_query($ZAP_REMOTE_INDEX,$dst_db_conn) ; //or echo $ZAP_REMOTE_INDEX."problem";
 echo "<br>".$ZAP_REMOTE_INDEX;
 $ZAP_REMOTE_INDEX="ALTER TABLE  `".$TABLE."` CHANGE  `ID`  `ID` INT( 11 ) NULL DEFAULT NULL";
 mysql_query($ZAP_REMOTE_INDEX,$dst_db_conn) ; //or die($ZAP_REMOTE_INDEX);
  $ZAP_REMOTE_INDEX="ALTER TABLE  `".$TABLE."` CHANGE  `P_ID`  `P_ID` INT( 11 ) NULL DEFAULT NULL";
 mysql_query($ZAP_REMOTE_INDEX,$dst_db_conn) ; //or die($ZAP_REMOTE_INDEX);
 $ZAP_REMOTE_INDEX="alter table ".$TABLE." add index(A_RECORD_CREATE_DATE)";
 mysql_query($ZAP_REMOTE_INDEX,$dst_db_conn) ; //or die($ZAP_REMOTE_INDEX);
 $ZAP_REMOTE_INDEX="alter table ".$TABLE." add index(ID_BILLING_USERS)";
 mysql_query($ZAP_REMOTE_INDEX,$dst_db_conn) ; //or die($ZAP_REMOTE_INDEX);
 $ZAP_REMOTE_INDEX="alter table ".$TABLE." add index(ID_PROJECT)";
 mysql_query($ZAP_REMOTE_INDEX,$dst_db_conn) ;//or die($ZAP_REMOTE_INDEX);
 $ZAP_REMOTE_INDEX="alter table ".$TABLE." add index(K_OD_KOGO)";
 mysql_query($ZAP_REMOTE_INDEX,$dst_db_conn) ;//or die($ZAP_REMOTE_INDEX);
 $ZAP_REMOTE_INDEX="alter table ".$TABLE." add index(K_DATA_OTRZYM_KORESP)";
 mysql_query($ZAP_REMOTE_INDEX,$dst_db_conn) ;//or die($ZAP_REMOTE_INDEX);


} //eof foreach 1



//include("http://db.kike.pl/SE/sync.php");


}
