1- <?php
2- namespace Swango \Db ;
3- /**
4- * master每个请求只存在一个,slave整个进程只有一个
5- *
6- * @author fdrea
7- *
8- */
9- abstract class Adapter {
10- protected Pool $ pool ;
11- /**
12- * 因兼容性原因,保留此函数
13- * @return Adapter
14- * @deprecated
15- */
16- public function getAdapter (): Adapter {
17- return $ this ;
18- }
19- /**
20- * 立即返回所有数据
21- *
22- * @param string|\Sql\AbstractSql $sql
23- * @return array 若为查询,则以数组形式返回查询结果;其他情况返回true
24- */
25- public function query ($ sql , ...$ params ) {
26- // 最多尝试两次
27- for ($ i = 0 ; $ i < 2 ; ++$ i ) {
28- try {
29- $ db = $ this ->pool ->pop ();
30- $ ret = $ db ->query ($ sql , ...$ params );
31- $ this ->pool ->push ($ db );
32- return $ ret ;
33- } catch (Exception \QueryErrorException $ e ) {
34- // 2002 Connection reset by peer or Transport endpoint is not connected
35- // 2006 MySQL server has gone away
36- if ($ e ->errno !== 2002 && $ e ->errno !== 2006 ) {
37- throw $ e ;
38- }
39- // 抛弃出现问题的连接
40- unset($ db );
41- }
42- }
43- throw $ e ;
44- }
45- /**
46- * 返回迭代器
47- *
48- * @param string|\Sql\Select $sql
49- * @param mixed ...$params
50- * @return \Swango\Db\Statement 可以直接对其执行 foreach
51- * @throws \Swango\Db\Exception\QueryErrorException
52- */
53- public function selectWith ($ sql , ...$ params ): Statement {
54- // 最多尝试两次
55- for ($ i = 0 ; $ i < 2 ; ++$ i ) {
56- try {
57- $ db = $ this ->pool ->pop ();
58- return $ db ->selectWith ($ sql , ...$ params );
59- // DB会在Statement销毁时push回pool
60- // if (! $db->inDeferMode())
61- // $this->pool->push($db);
62- } catch (Exception \QueryErrorException $ e ) {
63- // 2002 Connection reset by peer or Transport endpoint is not connected
64- // 2006 MySQL server has gone away
65- if ($ e ->errno !== 2002 && $ e ->errno !== 2006 ) {
66- throw $ e ;
67- }
68- // 抛弃出现问题的连接
69- unset($ db );
70- }
71- }
72- throw $ e ;
73- }
74- abstract public function getTransactionSerial (): ?int ;
75- abstract public function inTransaction (): bool ;
76- abstract public function beginTransaction (): bool ;
77- abstract public function submit (): bool ;
78- abstract public function rollback (): bool ;
1+ <?php
2+ namespace Swango \Db ;
3+ /**
4+ * master每个请求只存在一个,slave整个进程只有一个
5+ *
6+ * @author fdrea
7+ *
8+ */
9+ abstract class Adapter {
10+ protected Pool $ pool ;
11+ /**
12+ * 因兼容性原因,保留此函数
13+ * @return Adapter
14+ * @deprecated
15+ */
16+ public function getAdapter (): Adapter {
17+ return $ this ;
18+ }
19+ /**
20+ * 立即返回所有数据
21+ *
22+ * @param string|\Sql\AbstractSql $sql
23+ * @return array 若为查询,则以数组形式返回查询结果;其他情况返回true
24+ */
25+ public function query ($ sql , ...$ params ) {
26+ // 最多尝试两次
27+ for ($ i = 0 ; $ i < 2 ; ++$ i ) {
28+ try {
29+ $ db = $ this ->pool ->pop ();
30+ $ ret = $ db ->query ($ sql , ...$ params );
31+ $ this ->pool ->push ($ db );
32+ return $ ret ;
33+ } catch (Exception \QueryErrorException $ e ) {
34+ // 2002 Connection reset by peer or Transport endpoint is not connected
35+ // 2006 MySQL server has gone away
36+ if ($ e ->errno !== 2002 && $ e ->errno !== 2006 ) {
37+ throw $ e ;
38+ }
39+ // 抛弃出现问题的连接
40+ unset($ db );
41+ }
42+ }
43+ throw $ e ;
44+ }
45+ /**
46+ * 返回迭代器
47+ *
48+ * @param string|\Sql\Select $sql
49+ * @param mixed ...$params
50+ * @return \Swango\Db\Statement 可以直接对其执行 foreach
51+ * @throws \Swango\Db\Exception\QueryErrorException
52+ */
53+ public function selectWith ($ sql , ...$ params ): Statement {
54+ // 最多尝试两次
55+ for ($ i = 0 ; $ i < 2 ; ++$ i ) {
56+ try {
57+ $ db = $ this ->pool ->pop ();
58+ return $ db ->selectWith ($ sql , ...$ params );
59+ // DB会在Statement销毁时push回pool
60+ // if (! $db->inDeferMode())
61+ // $this->pool->push($db);
62+ } catch (Exception \QueryErrorException $ e ) {
63+ // 2002 Connection reset by peer or Transport endpoint is not connected
64+ // 2006 MySQL server has gone away
65+ if ($ e ->errno !== 2002 && $ e ->errno !== 2006 ) {
66+ throw $ e ;
67+ }
68+ // 抛弃出现问题的连接
69+ unset($ db );
70+ }
71+ }
72+ throw $ e ;
73+ }
74+ abstract public function getTransactionSerial (): ?int ;
75+ abstract public function inTransaction (): bool ;
76+ abstract public function beginTransaction (): bool ;
77+ abstract public function submit (): bool ;
78+ abstract public function rollback (): bool ;
7979}
0 commit comments