|
1
|
|
|
<?php |
|
2
|
|
|
namespace suda\application\database; |
|
3
|
|
|
|
|
4
|
|
|
use ReflectionException; |
|
5
|
|
|
use suda\application\Application; |
|
6
|
|
|
use suda\orm\middleware\Middleware; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* 数据表抽象对象 |
|
10
|
|
|
* |
|
11
|
|
|
* 用于提供对数据表的操作 |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
class DataAccess extends \suda\orm\DataAccess |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* 应用引用 |
|
19
|
|
|
* |
|
20
|
|
|
* @var Application |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $application; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var StatementSet |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $statement; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* 创建对数据的操作 |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $object |
|
33
|
|
|
* @param Middleware|null $middleware |
|
34
|
|
|
* @throws ReflectionException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(string $object, ?Middleware $middleware = null) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct($object, static::$application->getDataSource(), $middleware); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* 加载语句集合 |
|
43
|
|
|
* @param string $statement |
|
44
|
|
|
* @return $this |
|
45
|
|
|
*/ |
|
46
|
|
|
public function load(string $statement) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->statement = new StatementSet($this->access, $statement); |
|
49
|
|
|
$this->statement->load(static::$application); |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* 从变量创建中间件 |
|
55
|
|
|
* |
|
56
|
|
|
* @param object $object |
|
57
|
|
|
* @return DataAccess |
|
58
|
|
|
* @throws ReflectionException |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function create($object):DataAccess |
|
61
|
|
|
{ |
|
62
|
|
|
$middleware = null; |
|
63
|
|
|
if ($object instanceof Middleware) { |
|
64
|
|
|
$middleware = $object; |
|
65
|
|
|
} |
|
66
|
|
|
return new self(get_class($object), $middleware); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* 创建访问工具 |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $object |
|
73
|
|
|
* @param Middleware|null $middleware |
|
74
|
|
|
* @return DataAccess |
|
75
|
|
|
* @throws ReflectionException |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function new(string $object, ?Middleware $middleware = null):DataAccess |
|
78
|
|
|
{ |
|
79
|
|
|
return new self($object, $middleware); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return StatementSet |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getStatement(): StatementSet |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->statement; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* 从应用创建表 |
|
92
|
|
|
* |
|
93
|
|
|
* @param Application $application |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function loadApplication(Application $application) |
|
97
|
|
|
{ |
|
98
|
|
|
static::$application = $application; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get 应用引用 |
|
103
|
|
|
* |
|
104
|
|
|
* @return Application |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function application() |
|
107
|
|
|
{ |
|
108
|
|
|
return static::$application; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|