DbSqlAdapterSqlite.php
2.5 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
<?php
class LtDbSqlAdapterSqlite implements LtDbSqlAdapter
{
public function setCharset($charset)
{
// return 'PRAGMA encoding = "' . $charset . '"';
return '';
}
public function setSchema($schema)
{
return '';
}
public function beginTransaction()
{
return 'BEGIN TRANSACTION';
}
public function commit()
{
return 'COMMIT TRANSACTION';
}
public function rollBack()
{
return 'ROLLBACK TRANSACTION';
}
public function showSchemas($database)
{
//return "SHOW DATABASES";
return '';
}
public function showTables($schema)
{
// 临时表及其索引不在 SQLITE_MASTER 表中而在 SQLITE_TEMP_MASTER 中出现
return "SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' ORDER BY name";
}
public function showFields($table)
{
return "PRAGMA table_info('" . $table . "')";
}
public function limit($limit, $offset)
{
return " LIMIT $limit OFFSET $offset";
}
public function getSchemas($queryResult)
{
}
public function getTables($queryResult)
{
return $queryResult;
}
public function getFields($queryResult)
{
$fields = array();
foreach ($queryResult as $key => $value)
{
// 字段名
$fields[$value['name']]['name'] = $value['name'];
// 字段类型
$fulltype = $value['type'];
$size = null;
$precision = null;
$scale = null;
if (preg_match('/^([^\(]+)\(\s*(\d+)\s*,\s*(\d+)\s*\)$/',$fulltype, $matches))
{
$type = $matches[1];
$precision = $matches[2];
$scale = $matches[3]; // aka precision
}
elseif (preg_match('/^([^\(]+)\(\s*(\d+)\s*\)$/',$fulltype, $matches))
{
$type = $matches[1];
$size = $matches[2];
}
else
{
$type = $fulltype;
}
$fields[$value['name']]['type'] = $type;
/**
* not null is 99, null is 0
*/
$fields[$value['name']]['notnull'] = (bool) ($value['notnull'] != 0);
$fields[$value['name']]['default'] = $value['dflt_value'];
$fields[$value['name']]['primary'] = (bool) ($value['pk'] == 1 && strtoupper($fulltype) == 'INTEGER');
}
return $fields;
}
public function detectQueryType($sql)
{
if (preg_match("/^\s*SELECT|^\s*PRAGMA/i", $sql))
{
$ret = 'SELECT';
}
else if (preg_match("/^\s*INSERT/i", $sql))
{
$ret = 'INSERT';
}
else if (preg_match("/^\s*UPDATE|^\s*DELETE|^\s*REPLACE/i", $sql))
{
$ret = 'CHANGE_ROWS';
}
else if (preg_match("/^\s*USE|^\s*SET/i", $sql))
{
$ret = 'SET_SESSION_VAR';
}
else
{
$ret = 'OTHER';
}
return $ret;
}
}