<?php
/*********************************************************      
 程序说明:         
    功能说明:本程序实现对数据表中的参数的初始化，根据参数类型的定义，
              在参数表中填入缺省参数值和参数对象编号。

    调用关系:调用: connectdb.inc,lib.inc     
              被调用:type_set_down.php     
 
    变量说明:由type_set_down.php 传来$sys_type
              $device_table = OMC_device_Def;

    返回值:无

 作者:      
      
修改注释:      
NO.1      
姓名 :            
时间:      
修改说明:      
*********************************************************/ 
@set_time_limit(600);
require("../../inc/header.inc");

$DEBUG = 0;
$pubDb = 'OMC_PUB';

function omcGetObjDb($sysTypeNo){
	$dbName = 'OBJ_'.$sysTypeNo;
	return($dbName);
}
$objDb=omcGetObjDb($sysTypeNo);

function omcGetObjTbl($tableType='paramConf',$sysTypeNo,$sysNo=0,$subSysNo=0){
	$dbName = 'OBJ_'.$sysTypeNo;
	switch($tableType){
		case 'paramConf':
			$tableName = $dbName.'.paramConf';
			break;
		case 'param':
			$tableName = $dbName.'.tmpSubscriberParam_'.$sysNo;
			break;
		default:
			echo "<br><b>function omcGetObjTbl(): Unknown table Type:$tableType</b>";
			exit();
	}
	return($tableName);
}
$sysSubsConfTable	='OMC_PUB.sysSubsConf';
$systemConfTable	= 'OMC_PUB.sysConf';

$getConfSql="SELECT sysTypeName,dataTypeNo,indexFieldName,subsDataTable,tmpDataTable  
				FROM $sysSubsConfTable,$systemConfTable 
				WHERE $sysSubsConfTable.sysTypeNo='$sysTypeNo' 
				AND $systemConfTable.sysTypeNo='$sysTypeNo'
				AND $sysSubsConfTable.dataTypeNo='$dataTypeNo'
				";
if($DEBUG) echo "<br>getConfSql=$getConfSql";
$getConfResult=mysqli_query($pubConn,$getConfSql);
echo mysqli_error($pubConn);
$getConfRows=mysqli_fetch_array($getConfResult);
$keyFieldName	=$getConfRows[indexFieldName];
$srcDataTable	=$getConfRows[subsDataTable];
$tmpDataTable	=$getConfRows[tmpDataTable];

//复制一个表并且增加一个字段:editFlag
$dropTmpTable="DROP TABLE $tmpDataTable";
if($DEBUG) echo "<br>dropTmpTable=$dropTmpTable";
$dropRes=mysqli_query($pubConn,$dropTmpTable);
echo mysqli_error($pubConn);

$getCreateSql="SHOW CREATE TABLE $srcDataTable";
if($DEBUG) echo "<br>getCreateSql=$getCreateSql";
$getCreateRes=@mysqli_query($pubConn,$getCreateSql);
$row = mysqli_fetch_array($getCreateRes);
echo mysqli_error($pubConn);

$srcNameArr=explode('.',$srcDataTable);
$srcTableName=$srcNameArr[1];
$tmpNameArr=explode('.',$tmpDataTable);
$tmpTableName=$tmpNameArr[1];

if($DEBUG) echo "<br>str_replace($srcTableName,$tmpTableName,$row[1])";
$createTmpSql=str_replace($srcTableName,$tmpTableName,$row[1]);
if($DEBUG) echo "<br>createTmpSql=$createTmpSql";
$createTmpRes=mysqli_query($pubConn,$createTmpSql);
echo mysqli_error($pubConn);

$alterSql="ALTER TABLE $tmpDataTable ADD editFlag TINYINT not null FIRST"; 
if($DEBUG) echo "<br>alterSql=$alterSql";
$alterRes=mysqli_query($pubConn,$alterSql);
echo mysqli_error($pubConn);

$alterSql="ALTER TABLE $tmpDataTable ADD INDEX (editFlag)";
if($DEBUG) echo "<br>alterSql=$alterSql";
$alterRes=mysqli_query($pubConn,$alterSql);
echo mysqli_error($pubConn);
?>