dbsql.php
3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
class DbSql
{
private $config = array('host'=>'localhost','user'=>'','password'=>'');
private $type = 'mysql';
private $conn;
public function __construct($config=null)
{
$exts=(array_change_key_case(array_flip(get_loaded_extensions()), CASE_LOWER));
if(isset($exts['mysql'])) $this->type = 'mysql';
else{
if(isset($exts['mysqli'])) $this->type = 'mysqli';
}
}
public function checkConnect($config=array()){
$config = array_merge($this->config,$config);
if($this->type == 'mysql'){
$this->conn = @mysql_connect($config['host'],$config['user'],$config['password']);
}else{
$this->conn = @mysqli_connect($config['host'],$config['user'],$config['password']);
}
return $this->conn;
}
public function parseSql($filename){
$lines=file($filename);
$lines[0]=str_replace(chr(239).chr(187).chr(191),"",$lines[0]);//去除BOM头
$flage=true;
$sqls=array();
$sql="";
foreach($lines as $line)
{
$line=trim($line);
$char=substr($line,0,1);
if($char!='#' && strlen($line)>0)
{
$prefix=substr($line,0,2);
switch($prefix)
{
case '/*':
{
$flage=(substr($line,-3)=='*/;'||substr($line,-2)=='*/')?true:false;
break 1;
}
case '--': break 1;
default :
{
if($flage)
{
$sql.=$line;
if(substr($line,-1)==";")
{
$sqls[]=$sql;
$sql="";
}
}
if(!$flage)$flage=(substr($line,-3)=='*/;'||substr($line,-2)=='*/')?true:false;
}
}
}
}
return $sqls;
}
public function getType()
{
return $this->type;
}
public function getConn()
{
return $this->conn;
}
//安装SQL函数
function installSql($sqls, $per = '', $force=false)
{
$flag=true;
if(is_array($sqls))
{
$total = count($sqls);
$num = 0;
foreach($sqls as $sql)
{
//$sql= preg_replace("/(create|drop|insert)([^`]+`)([a-zA-Z]*_)?(\w+)(`.*)/i","$1$2{$per}$4$5",$sql);
if(preg_match_all("/(create|drop|insert)([^`]+`)(Tiny_)?(\w+)(`.*)/i",$sql,$out)){
$sql = $out[1][0].$out[2][0].$per.$out[4][0].$out[5][0];
$op = strtolower($out[1][0]);
$str = '';
if($this->type == 'mysql'){
$dosql = mysql_query($sql);
}else{
$dosql = mysqli_query($this->conn,$sql);
}
if($dosql){
if($op=='create')$str= "<b style='color:#009900'> 创建表 ".($per.$out[4][0])."…………成功</b>";
//else if($op=='drop')echo "<div style='color:#009900'> 删除表 ".($per.$out[4][0])."…………成功</div>";
else if($op=='insert')$str= "<b style='color:#009900'> 写入表 ".($per.$out[4][0])."…………成功</b>";
}
else
{
if($op=='create')$str= "<b style='color:#990000'> 创建表 ".($per.$out[4][0])."…………失败</b>";
//else if($op=='drop')echo "<div style='color:#990000'> 删除表 ".($per.$out[4][0])."…………失败</div>";
else if($op=='insert')$str= "<b style='color:#990000'> 写入表 ".($per.$out[4][0])."…………失败</b>";
$flag=false;
}
if($flag) $num++;
if($str!=''){
echo("<script>parent.document.getElementById('install_status_info').innerHTML=\"{$str}\";parent.document.getElementById('install_bar').style.width='".(($num/$total)*100)."%';</script>");
ob_flush();
flush();
}
if($flag==false){
echo("<script>parent.document.getElementById('error_div').style.display='';parent.document.getElementById('error_info').innerHTML=\"数据库安装过程中发生以下错误:{$str}\";</script>");
ob_flush();
flush();
break;
}
}
}
if($flag){
echo("<script>parent.location='index.php?step=4';</script>");
ob_flush();
flush();
}
}
return $flag;
}
}