-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtable_inspector_worker.class.php
More file actions
75 lines (60 loc) · 2.01 KB
/
table_inspector_worker.class.php
File metadata and controls
75 lines (60 loc) · 2.01 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
<?php
namespace PluSQL;
use Exception,mysqli,mysqli_result;
class TableInspectorWorker
{
private $table_name;
private $link;
private $primary_keys;
private $all_fields;
public function __construct($table_name,$link)
{
$this->table_name = $table_name;
$this->link = $link;
$this->primary_keys = NULL;
$this->all_fields = NULL;
}
public function name()
{
return $this->table_name;
}
public function allFields()
{
if($this->all_fields === NULL)
{
$describe_sql = 'DESCRIBE '.$this->table_name;
if($this->link instanceof mysqli)
$query = $this->link->query($describe_sql);
else
$query = mysql_query($describe_sql,$this->link);
if(!$query)
throw new TableInspectorException('It looks like: '.$this->table_name.' doesn\'t exist');
$this->all_fields = array();
while($row = self::queryRow($query))
$this->all_fields[] = $row;
}
return $this->all_fields;
}
public function primaryKeys()
{
if($this->primary_keys === NULL)
{
$this->primary_keys = array();
foreach($this->allFields() as $row)
{
if($row['Key'] == 'PRI')
$this->primary_keys[] = $row['Field'];
}
}
return $this->primary_keys;
}
public static function queryRow($query)
{
if($query instanceof mysqli_result)
$ret = $query->fetch_assoc();
else
$ret = mysql_fetch_assoc($query);
return $ret;
}
}
class TableInspectorException extends Exception {}