Compare commits

...

766 Commits

Author SHA1 Message Date
as2252258 8a985a65df eee 2026-06-12 23:57:19 +08:00
as2252258 c7b980d969 eee 2026-04-23 00:23:32 +08:00
as2252258 fea649413c eee 2026-04-23 00:20:00 +08:00
as2252258 959407a95e eee 2026-04-23 00:19:10 +08:00
as2252258 0abc7c32f7 eee 2026-04-21 14:59:51 +08:00
as2252258 9c6340d0b3 eee 2026-01-11 19:59:20 +08:00
as2252258 0f01416351 eee 2026-01-11 19:57:24 +08:00
as2252258 b1d2c0fb51 eee 2026-01-11 19:56:26 +08:00
as2252258 db18a2d73a eee 2026-01-11 19:55:00 +08:00
as2252258 3cb3dcf384 eee 2026-01-11 19:53:35 +08:00
as2252258 fd8652ccd1 eee 2026-01-11 19:32:32 +08:00
as2252258 279151825a eee 2026-01-11 19:29:35 +08:00
as2252258 3dd2d4abf2 eee 2026-01-06 21:44:27 +08:00
as2252258 8bf5027582 eee 2026-01-03 20:44:19 +08:00
as2252258 0b27a7dc7d eee 2026-01-02 01:08:47 +08:00
as2252258 c016853ff8 ea 2026-01-01 23:01:44 +08:00
as2252258 758c322679 eee 2026-01-01 21:46:00 +08:00
as2252258 cbbc6ea7f1 eee 2025-12-31 00:52:06 +08:00
as2252258 456c7f29b6 eee 2025-12-31 00:49:39 +08:00
as2252258 89d5b604d6 eee 2025-12-31 00:19:29 +08:00
as2252258 c943d6258b eee 2025-12-23 19:03:22 +08:00
as2252258 466cee4ab5 update composer.json.
xxx

Signed-off-by: 向林 <as2252258@163.com>
2025-12-23 02:28:56 +00:00
as2252258 ee1a5a993d eee 2025-12-16 20:20:08 +08:00
as2252258 0dcd548645 eee 2025-12-07 14:01:37 +08:00
as2252258 d796a86ce3 eee 2025-12-07 12:18:24 +08:00
as2252258 ef19079727 ea 2025-11-23 23:11:49 +08:00
as2252258 dcee10b77b eee 2025-07-14 18:34:02 +08:00
as2252258 2bf66fc17f eee 2025-07-14 18:05:40 +08:00
as2252258 61f549362d eee 2025-07-14 18:03:42 +08:00
as2252258 583e2214c3 eee 2025-07-14 15:34:45 +08:00
as2252258 e190c42dd0 eee 2025-07-14 11:04:30 +08:00
as2252258 190c272ea3 eee 2025-07-14 10:59:34 +08:00
as2252258 28c64daae7 eee 2025-07-14 08:57:17 +08:00
as2252258 7c347bc373 eee 2025-07-09 10:34:46 +08:00
as2252258 d5c9b3bc86 eee 2025-07-09 10:28:20 +08:00
as2252258 ef7e85746d eee 2025-05-19 11:20:32 +08:00
as2252258 89f6949556 eee 2025-05-19 11:19:20 +08:00
as2252258 555d160b40 eee 2025-05-19 11:09:33 +08:00
as2252258 45b949747d eee 2025-05-19 11:06:27 +08:00
as2252258 94857ecb46 eee 2025-02-17 16:29:10 +08:00
as2252258 0f0a3349fb eee 2025-02-17 16:26:32 +08:00
as2252258 7fb9c3ea05 eee 2025-01-18 20:28:24 +08:00
as2252258 11a939e8a1 eee 2024-12-31 15:52:20 +08:00
as2252258 1666af9906 eee 2024-12-29 21:34:12 +08:00
as2252258 78f7faba55 eee 2024-12-29 21:10:58 +08:00
as2252258 b3f50fc6e4 eee 2024-12-23 10:03:35 +08:00
as2252258 7b4604ce98 eee 2024-12-20 18:20:54 +08:00
as2252258 654cc483b3 eee 2024-12-18 11:42:09 +08:00
as2252258 c6b88256e3 eee 2024-12-18 11:31:15 +08:00
as2252258 5b4a6b9caa eee 2024-12-13 15:14:13 +08:00
as2252258 42af2e8bf9 eee 2024-12-13 15:03:09 +08:00
as2252258 db92238e9e eee 2024-12-13 14:45:03 +08:00
as2252258 530104ceb6 eee 2024-12-13 14:44:20 +08:00
as2252258 7ea245fc89 eee 2024-12-13 14:44:00 +08:00
as2252258 0cc1d3d9f7 eee 2024-12-13 14:35:53 +08:00
as2252258 c444de4815 eee 2024-12-13 14:35:04 +08:00
as2252258 263f99c8c3 eee 2024-12-13 14:34:06 +08:00
as2252258 450870ea19 eee 2024-12-13 14:08:04 +08:00
as2252258 b3e3e95081 eee 2024-12-13 14:04:58 +08:00
as2252258 f084c171ff ea 2024-12-12 12:10:19 +08:00
as2252258 7ca732f153 eee 2024-11-18 16:39:59 +08:00
as2252258 f9771fed3f eee 2024-11-18 16:38:09 +08:00
as2252258 8d006568ef eee 2024-11-18 16:35:37 +08:00
as2252258 6304684683 eee 2024-11-06 21:41:43 +08:00
as2252258 528f43c7ac eee 2024-11-06 21:33:36 +08:00
as2252258 622176c3ff eee 2024-11-06 21:28:29 +08:00
as2252258 e343595e7d eee 2024-11-06 21:16:30 +08:00
as2252258 4f31065818 eee 2024-11-06 21:15:11 +08:00
as2252258 4fe1558a7e eee 2024-11-06 21:13:12 +08:00
as2252258 904ba8cc97 eee 2024-11-06 21:11:43 +08:00
as2252258 4e59053d5c eee 2024-11-06 21:06:01 +08:00
as2252258 59c9a2e944 eee 2024-11-06 21:03:11 +08:00
as2252258 f563126cd0 eee 2024-11-06 20:59:30 +08:00
as2252258 41166dd998 eee 2024-11-06 20:53:24 +08:00
as2252258 3d4327a92e eee 2024-11-06 20:52:17 +08:00
as2252258 28beb3678e eee 2024-11-06 20:36:59 +08:00
as2252258 32164f66ab eee 2024-10-24 10:53:00 +08:00
as2252258 3244f8cfac eee 2024-10-23 14:54:04 +08:00
as2252258 8027effa8c eee 2024-10-23 14:51:09 +08:00
as2252258 59375e567e eee 2024-10-23 14:36:55 +08:00
as2252258 eb39acb7e9 eee 2024-10-23 14:36:20 +08:00
as2252258 be3fc004d5 eee 2024-10-23 14:32:09 +08:00
as2252258 bbe0631f5b eee 2024-10-23 14:10:14 +08:00
as2252258 bf5d988ba4 eee 2024-10-11 11:55:32 +08:00
as2252258 4fc9b42540 eee 2024-10-09 16:01:22 +08:00
as2252258 e5453807f2 eee 2024-09-05 15:42:24 +08:00
as2252258 5a6f6da70a eee 2024-09-02 12:12:39 +08:00
as2252258 beb48f41d2 eee 2024-08-27 11:19:22 +08:00
as2252258 8d917b0f92 eee 2024-08-08 11:12:09 +08:00
as2252258 08fbc49091 eee 2024-08-08 10:52:48 +08:00
as2252258 08e49cfa65 eee 2024-07-10 16:18:27 +08:00
as2252258 d58850b158 eee 2024-07-08 12:06:41 +08:00
as2252258 aba45e8cb9 eee 2024-07-08 10:58:52 +08:00
as2252258 ed22201bef eee 2024-07-02 21:38:30 +08:00
as2252258 ccbac52a16 eee 2024-05-01 02:06:14 +08:00
as2252258 9fe0698d1c eee 2024-05-01 02:02:58 +08:00
as2252258 55e1f6235e eee 2024-05-01 01:54:38 +08:00
as2252258 ed0d044223 eee 2024-04-29 21:55:31 +08:00
as2252258 2b85cb4ec3 eee 2024-04-29 21:55:19 +08:00
as2252258 73b9923740 eee 2024-04-29 21:49:08 +08:00
as2252258 b2c8160314 eee 2024-04-26 17:01:25 +08:00
as2252258 199312d326 eee 2024-04-26 15:39:13 +08:00
as2252258 87f568bdfe eee 2024-04-26 15:36:55 +08:00
as2252258 2fd37f90a3 eee 2024-04-26 15:28:40 +08:00
as2252258 c100190155 eee 2024-04-26 15:27:11 +08:00
as2252258 71766ee914 eee 2024-04-26 15:26:10 +08:00
as2252258 3a39eaabf4 eee 2024-04-26 15:25:12 +08:00
as2252258 722b286f91 eee 2024-04-26 15:23:37 +08:00
as2252258 f9ee3aa014 eee 2024-04-26 15:22:16 +08:00
as2252258 4341efcb8c eee 2024-04-26 15:20:22 +08:00
as2252258 8acf74c9ed eee 2024-04-26 15:15:36 +08:00
as2252258 b9750d743b eee 2024-04-26 15:12:31 +08:00
as2252258 ea34371652 eee 2024-04-16 17:24:22 +08:00
as2252258 92f0c1a973 eee 2024-01-10 18:12:39 +08:00
as2252258 11ad7217c2 eee 2024-01-10 18:08:41 +08:00
as2252258 b647716f1c eee 2024-01-10 18:07:40 +08:00
as2252258 d8b2d1ca2c eee 2024-01-10 17:59:05 +08:00
as2252258 a439c616b4 eee 2024-01-10 17:48:54 +08:00
as2252258 d036e440cd eee 2024-01-10 17:43:10 +08:00
as2252258 88fb563c99 eee 2024-01-10 17:35:55 +08:00
as2252258 1066aa03e8 eee 2023-12-26 17:41:03 +08:00
as2252258 609e5d4749 eee 2023-12-26 17:40:48 +08:00
as2252258 ea8060b047 eee 2023-12-22 11:49:35 +08:00
as2252258 29b3accadb eee 2023-12-22 11:41:55 +08:00
as2252258 22a04814dc eee 2023-12-22 11:41:24 +08:00
as2252258 bb875bd672 eee 2023-12-20 21:05:54 +08:00
as2252258 c2a5d7c5e3 e 2023-12-20 20:56:48 +08:00
as2252258 797d44f816 eee 2023-12-20 18:03:36 +08:00
as2252258 2d067b9d6a eee 2023-12-20 17:26:58 +08:00
as2252258 666dce01bc eee 2023-12-20 17:21:25 +08:00
as2252258 8174848de6 eee 2023-12-18 23:17:26 +08:00
as2252258 d2acc197da eee 2023-12-18 23:16:09 +08:00
as2252258 c0ff5be651 eee 2023-12-18 23:14:52 +08:00
as2252258 b77e4be3e8 eee 2023-12-18 22:00:50 +08:00
as2252258 dc34638a31 eee 2023-12-18 21:59:43 +08:00
as2252258 b94f654ad0 eee 2023-12-18 18:36:08 +08:00
as2252258 9b249e538b eee 2023-12-18 18:11:58 +08:00
as2252258 4bcd4a8a44 eee 2023-12-18 17:59:42 +08:00
as2252258 8c3cc60cc3 eee 2023-12-18 17:40:01 +08:00
as2252258 2ea6d3705d eee 2023-12-18 17:39:43 +08:00
as2252258 1ede0aa3bd eee 2023-12-18 17:12:52 +08:00
as2252258 3d37e11497 eee 2023-12-18 17:11:57 +08:00
as2252258 7f98be6b1c eee 2023-12-18 16:09:21 +08:00
as2252258 abb561f185 eee 2023-12-18 16:03:24 +08:00
as2252258 6b96e54ab0 eee 2023-12-18 15:46:06 +08:00
as2252258 c9255a6c2b eee 2023-12-18 15:11:04 +08:00
as2252258 7d06791d67 eee 2023-12-18 15:01:08 +08:00
as2252258 e248bbbea0 eee 2023-12-14 09:50:49 +08:00
as2252258 694028f983 eee 2023-12-13 19:09:09 +08:00
as2252258 4fbbbe0cfe eee 2023-12-13 19:05:44 +08:00
as2252258 0833e87362 eee 2023-12-13 18:34:48 +08:00
as2252258 8cd195f4ee eee 2023-12-13 18:27:12 +08:00
as2252258 b690228ea2 eee 2023-12-13 18:25:06 +08:00
as2252258 0e20ab2167 eee 2023-12-13 18:23:24 +08:00
as2252258 8685e039a4 eee 2023-12-13 18:19:36 +08:00
as2252258 d28af2fa09 eee 2023-12-13 18:14:42 +08:00
as2252258 89592ff638 eee 2023-12-13 18:07:39 +08:00
as2252258 b490e85a34 eee 2023-12-13 17:50:42 +08:00
as2252258 2d1d780618 eee 2023-12-13 17:48:07 +08:00
as2252258 3e73149716 eee 2023-12-13 17:43:25 +08:00
as2252258 a1d42a0831 eee 2023-12-13 17:25:40 +08:00
as2252258 1092273257 eee 2023-12-13 17:08:31 +08:00
as2252258 f614795f95 eee 2023-12-13 17:07:01 +08:00
as2252258 79b7bb432f eee 2023-12-13 17:02:29 +08:00
as2252258 9a670f626e eee 2023-12-13 16:25:17 +08:00
as2252258 109cc2ac27 eee 2023-12-13 16:24:11 +08:00
as2252258 3ab08c9ca7 eee 2023-12-13 16:22:25 +08:00
as2252258 c84d9f8328 eee 2023-12-13 16:19:16 +08:00
as2252258 658445927a eee 2023-12-13 16:18:12 +08:00
as2252258 a73b5c0e2a eee 2023-12-13 15:36:51 +08:00
as2252258 3179e95a82 eee 2023-12-13 14:47:23 +08:00
as2252258 85eaeba8ba eee 2023-12-13 10:14:26 +08:00
as2252258 16fe127019 eee 2023-12-13 10:05:42 +08:00
as2252258 013be605f4 eee 2023-12-13 09:47:00 +08:00
as2252258 64087d6258 eee 2023-12-12 17:47:40 +08:00
as2252258 9c433855d5 eee 2023-12-12 17:46:25 +08:00
as2252258 fd71d23802 eee 2023-12-12 17:40:51 +08:00
as2252258 2eb63c6bd4 eee 2023-12-12 17:21:25 +08:00
as2252258 e2c0f62532 eee 2023-12-12 15:35:35 +08:00
as2252258 510a13e3da eee 2023-12-06 18:33:25 +08:00
as2252258 1b1eec5f78 eee 2023-12-06 18:32:21 +08:00
as2252258 0b782de1f1 eee 2023-12-06 18:24:53 +08:00
as2252258 641187e240 eee 2023-12-06 18:23:11 +08:00
as2252258 501ace9e36 eee 2023-11-30 17:02:20 +08:00
as2252258 c352c46a0f eee 2023-11-24 11:01:07 +08:00
as2252258 fec858ffbd eee 2023-11-24 10:46:44 +08:00
as2252258 c119186ad4 eee 2023-11-24 10:38:18 +08:00
as2252258 66d5d46add eee 2023-11-23 15:30:57 +08:00
as2252258 d42e30ecbe eee 2023-11-23 15:27:57 +08:00
as2252258 f8a1e700a7 eee 2023-11-23 15:26:20 +08:00
as2252258 53dbf7a9b2 eee 2023-11-14 15:01:30 +08:00
as2252258 a1238613f9 eee 2023-11-13 17:52:38 +08:00
as2252258 dcf4e0a614 eee 2023-10-24 17:22:29 +08:00
as2252258 b0e627c4fb eee 2023-10-24 15:14:46 +08:00
as2252258 67c7dd8421 eee 2023-10-12 00:46:07 +08:00
as2252258 d097b66187 eee 2023-10-12 00:18:54 +08:00
as2252258 82f38732ef eee 2023-10-11 23:51:54 +08:00
as2252258 e1e5af2173 eee 2023-10-07 20:40:59 +08:00
as2252258 4b304e52a2 eee 2023-10-05 15:48:35 +08:00
as2252258 77419f2d10 eee 2023-10-05 15:47:39 +08:00
as2252258 2fb2111526 eee 2023-10-05 15:33:18 +08:00
as2252258 0c2134a361 eee 2023-10-05 15:23:15 +08:00
as2252258 d942db93d8 eee 2023-10-05 15:15:50 +08:00
as2252258 0dd7b7e0e7 eee 2023-09-30 20:22:49 +08:00
as2252258 34ad1f58fe eee 2023-09-30 20:22:24 +08:00
as2252258 f9a9652726 eee 2023-09-30 20:21:10 +08:00
as2252258 ba59b9431e eee 2023-09-30 20:12:28 +08:00
as2252258 fc73dbf98d eee 2023-09-30 20:11:03 +08:00
as2252258 d4b9dd6758 eee 2023-09-30 20:10:07 +08:00
as2252258 b5b5db4d2c eee 2023-09-30 20:09:18 +08:00
as2252258 735801f756 eee 2023-09-30 20:03:37 +08:00
as2252258 fd8ac37742 eee 2023-09-30 20:03:15 +08:00
as2252258 4cac66b1d1 eee 2023-09-30 19:56:58 +08:00
as2252258 bc43a32b10 eee 2023-09-30 19:56:02 +08:00
as2252258 f61c678d54 eee 2023-09-30 19:53:44 +08:00
as2252258 beb541f01b eee 2023-09-30 19:50:33 +08:00
as2252258 bae4e54dd5 eee 2023-09-30 19:44:05 +08:00
as2252258 f477b181de eee 2023-09-30 19:43:07 +08:00
as2252258 5b3b16e4f6 eee 2023-09-30 19:40:42 +08:00
as2252258 28451d6e02 eee 2023-09-30 19:36:07 +08:00
as2252258 a0871d9cbd eee 2023-09-30 19:35:46 +08:00
as2252258 313092dd75 eee 2023-09-30 19:32:41 +08:00
as2252258 6be1180962 eee 2023-09-11 11:02:34 +08:00
as2252258 370c50f1fe eee 2023-09-01 10:56:06 +08:00
as2252258 ba68102405 eee 2023-09-01 10:54:55 +08:00
as2252258 2ee482241d eee 2023-09-01 10:53:38 +08:00
as2252258 0bff75446d eee 2023-08-31 12:29:02 +08:00
as2252258 a3fa4521fc eee 2023-08-30 23:25:21 +08:00
as2252258 1d37abf5d5 eee 2023-08-29 22:47:48 +08:00
as2252258 a2ceb7310e eee 2023-08-29 22:27:47 +08:00
as2252258 2b902f3624 eee 2023-08-29 22:21:42 +08:00
as2252258 c036cea809 eee 2023-08-29 22:20:30 +08:00
as2252258 91edb08238 eee 2023-08-29 22:13:37 +08:00
as2252258 5158b709bd eee 2023-08-29 21:54:02 +08:00
as2252258 9c8c8c6bb3 eee 2023-08-29 21:46:05 +08:00
as2252258 cc723a7630 eee 2023-08-29 21:43:37 +08:00
as2252258 138549f54d eee 2023-08-29 21:39:04 +08:00
as2252258 99ff8b1ed5 eee 2023-08-29 21:35:57 +08:00
as2252258 2feff52291 eee 2023-08-29 21:35:19 +08:00
as2252258 e08e811580 eee 2023-08-29 15:19:39 +08:00
as2252258 6f979394a3 qqq 2023-08-25 10:28:47 +08:00
as2252258 9eb7a11b6c qqq 2023-08-25 10:21:56 +08:00
as2252258 e6b7b5ee57 qqq 2023-08-25 10:21:35 +08:00
as2252258 666de7ce40 qqq 2023-08-25 10:10:09 +08:00
as2252258 d377deab8e qqq 2023-08-25 10:04:55 +08:00
as2252258 703300182b qqq 2023-08-25 10:01:53 +08:00
as2252258 5caed72909 qqq 2023-08-25 10:00:48 +08:00
as2252258 250cdda33e qqq 2023-08-25 09:37:58 +08:00
as2252258 69b5ef6437 qqq 2023-08-25 00:32:38 +08:00
as2252258 d37f43c4ac qqq 2023-08-25 00:18:44 +08:00
as2252258 5b0e69f190 qqq 2023-08-25 00:17:17 +08:00
as2252258 b81be565ae qqq 2023-08-25 00:16:49 +08:00
as2252258 c30a58a4c5 qqq 2023-08-24 16:04:24 +08:00
as2252258 812505f914 qqq 2023-08-24 16:01:20 +08:00
as2252258 45968536c5 qqq 2023-08-24 14:19:47 +08:00
as2252258 d0375388b4 qqq 2023-08-24 14:15:00 +08:00
as2252258 c3eced973a qqq 2023-08-24 14:14:03 +08:00
as2252258 6cb5899fe1 qqq 2023-08-24 14:13:22 +08:00
as2252258 853f23bb3d qqq 2023-08-24 14:08:55 +08:00
as2252258 1976822784 qqq 2023-08-24 14:07:01 +08:00
as2252258 17a5dcb2bb qqq 2023-08-24 14:06:35 +08:00
as2252258 58cbfce778 qqq 2023-08-24 14:05:00 +08:00
as2252258 c86d6791c9 qqq 2023-08-24 14:02:58 +08:00
as2252258 636c96b73c qqq 2023-08-24 14:01:20 +08:00
as2252258 7f885aca09 qqq 2023-08-24 13:59:44 +08:00
as2252258 8a25e4b7be qqq 2023-08-24 13:56:12 +08:00
as2252258 6dab6eba8c qqq 2023-08-24 13:49:58 +08:00
as2252258 b1ac46f14f qqq 2023-08-24 13:41:27 +08:00
as2252258 36e768e3e6 qqq 2023-08-24 12:29:18 +08:00
as2252258 327f40faea qqq 2023-08-24 12:11:35 +08:00
as2252258 d3dacf96df qqq 2023-08-24 12:11:23 +08:00
as2252258 76d75806e5 qqq 2023-08-24 12:10:08 +08:00
as2252258 677bda01a0 qqq 2023-08-24 12:06:54 +08:00
as2252258 2c63caaaf8 qqq 2023-08-24 12:06:35 +08:00
as2252258 0bb294edc5 qqq 2023-08-24 12:01:34 +08:00
as2252258 e06c04a5bb qqq 2023-08-24 11:55:43 +08:00
as2252258 8b35064395 qqq 2023-08-24 11:46:27 +08:00
as2252258 8f69385eaa qqq 2023-08-24 11:42:03 +08:00
as2252258 a85fc58630 qqq 2023-08-24 11:25:42 +08:00
as2252258 2e01419960 qqq 2023-08-23 01:48:34 +08:00
as2252258 543995f5cd qqq 2023-08-23 01:48:18 +08:00
as2252258 4b55ee837e qqq 2023-08-21 16:56:19 +08:00
as2252258 2493b4e15d qqq 2023-08-21 16:47:30 +08:00
as2252258 d9ffa9dae2 qqq 2023-08-21 16:44:54 +08:00
as2252258 37c9bcc4d2 qqq 2023-08-21 09:38:37 +08:00
as2252258 0d3a37084e qqq 2023-08-19 19:48:10 +08:00
as2252258 6283d6cd49 qqq 2023-08-18 15:05:05 +08:00
as2252258 1fb13aa3a3 qqq 2023-08-18 15:04:39 +08:00
as2252258 0ea9339816 qqq 2023-08-17 17:41:26 +08:00
as2252258 1318c9e5b3 qqq 2023-08-17 17:33:55 +08:00
as2252258 05f90fe4a4 qqq 2023-08-17 17:33:28 +08:00
as2252258 c30c830169 qqq 2023-08-17 17:32:36 +08:00
as2252258 bbd4d7b353 qqq 2023-08-17 17:30:53 +08:00
as2252258 c718e852dd qqq 2023-08-17 17:24:46 +08:00
as2252258 77648f3878 qqq 2023-08-17 17:23:30 +08:00
as2252258 7015e39fa6 qqq 2023-08-17 11:31:54 +08:00
as2252258 ccacfcbc02 qqq 2023-08-17 10:16:08 +08:00
as2252258 37abaa8035 qqq 2023-08-17 09:48:44 +08:00
as2252258 c208d023e7 qqq 2023-08-16 16:38:33 +08:00
as2252258 add36ab75c qqq 2023-08-16 16:35:54 +08:00
as2252258 321c256100 qqq 2023-08-16 16:33:56 +08:00
as2252258 f6cd6756ce qqq 2023-08-16 16:18:03 +08:00
as2252258 ce7539ab62 qqq 2023-08-16 16:16:29 +08:00
as2252258 3ba95cc3dd qqq 2023-08-16 16:14:41 +08:00
as2252258 745a5820ba qqq 2023-08-16 16:11:56 +08:00
as2252258 373a1b0fed qqq 2023-08-16 15:55:50 +08:00
as2252258 720c5498f1 qqq 2023-08-16 15:51:56 +08:00
as2252258 9a680d56a7 qqq 2023-08-16 14:54:02 +08:00
as2252258 1d2bca40a2 qqq 2023-08-16 14:47:26 +08:00
as2252258 4c49a09047 qqq 2023-08-16 14:39:46 +08:00
as2252258 e597224e86 qqq 2023-08-16 14:32:06 +08:00
as2252258 2a83160792 qqq 2023-08-16 14:30:52 +08:00
as2252258 b3759bfa53 qqq 2023-08-16 14:30:39 +08:00
as2252258 9f51ff4659 qqq 2023-08-16 14:29:39 +08:00
as2252258 a264b2b8a3 qqq 2023-08-16 14:25:48 +08:00
as2252258 be09229577 qqq 2023-08-16 14:25:19 +08:00
as2252258 25296ee714 qqq 2023-08-16 14:22:38 +08:00
as2252258 6678011261 qqq 2023-08-16 14:18:09 +08:00
as2252258 17b194f50c qqq 2023-08-16 12:36:45 +08:00
as2252258 b77e6a7618 qqq 2023-08-16 12:34:56 +08:00
as2252258 aa14ff3376 qqq 2023-08-16 12:27:03 +08:00
as2252258 e5c5ce822c qqq 2023-08-16 12:19:47 +08:00
as2252258 68035a95e4 qqq 2023-08-16 12:15:59 +08:00
as2252258 5f5b90ac49 qqq 2023-08-16 12:00:10 +08:00
as2252258 ed749d7a34 qqq 2023-08-16 10:26:48 +08:00
as2252258 bec9cd1a3f qqq 2023-08-16 01:01:47 +08:00
as2252258 55bb55f955 qqq 2023-08-16 00:57:33 +08:00
as2252258 5b3b3b7a6b qqq 2023-08-16 00:55:05 +08:00
as2252258 e1c893c4e1 qqq 2023-08-16 00:43:39 +08:00
as2252258 0267b9ca11 qqq 2023-08-16 00:39:55 +08:00
as2252258 a95f2af9ed qqq 2023-08-15 23:50:26 +08:00
as2252258 4a51a8a336 qqq 2023-08-14 18:56:45 +08:00
as2252258 0a567634d6 qqq 2023-08-14 14:14:40 +08:00
as2252258 08e9211e0e qqq 2023-08-14 14:03:30 +08:00
as2252258 d42d284f62 qqq 2023-08-14 14:02:16 +08:00
as2252258 63ddc36958 qqq 2023-08-14 12:41:16 +08:00
as2252258 3d0fd3d5d0 qqq 2023-08-12 00:14:39 +08:00
as2252258 4c27cae802 qqq 2023-08-11 23:06:14 +08:00
as2252258 dc73de9abd qqq 2023-08-11 14:02:51 +08:00
as2252258 d96f1c27a5 qqq 2023-08-02 14:46:01 +08:00
as2252258 bc36df50f8 qqq 2023-08-02 14:37:59 +08:00
as2252258 2c893a52d5 qqq 2023-07-31 23:28:59 +08:00
as2252258 e575254139 qqq 2023-07-31 23:26:58 +08:00
as2252258 00a2125efc qqq 2023-07-31 23:08:59 +08:00
as2252258 45d0712cb2 qqq 2023-07-31 15:57:15 +08:00
as2252258 8eb4f5869f qqq 2023-07-27 22:59:12 +08:00
as2252258 29aae506c1 qqq 2023-07-27 22:59:00 +08:00
as2252258 9ab0aa1fe7 qqq 2023-07-26 11:00:08 +08:00
as2252258 4abc1a621b qqq 2023-07-25 09:33:24 +08:00
as2252258 4c6dbc9b3c qqq 2023-07-20 15:01:24 +08:00
as2252258 d4219b902a qqq 2023-07-14 23:25:41 +08:00
as2252258 83138dbcb4 qqq 2023-07-13 10:37:14 +08:00
as2252258 9d7605b8e4 qqq 2023-07-10 11:28:53 +08:00
as2252258 203eda17ad qqq 2023-07-10 11:26:35 +08:00
as2252258 1c07e59357 qqq 2023-07-10 11:23:38 +08:00
as2252258 a4a5025e95 qqq 2023-07-10 11:20:55 +08:00
as2252258 3da1c718d9 qqq 2023-07-10 11:14:26 +08:00
as2252258 51039961c1 qqq 2023-07-06 13:33:50 +08:00
as2252258 a403421bfe qqq 2023-06-12 15:34:08 +08:00
as2252258 5d161fd1e7 qqq 2023-06-12 15:31:45 +08:00
as2252258 65bbc6888a qqq 2023-05-26 09:22:34 +08:00
as2252258 c8159194bf qqq 2023-05-26 09:20:29 +08:00
as2252258 49742a08ad qqq 2023-05-25 17:00:58 +08:00
as2252258 efa6cf5421 qqq 2023-05-25 16:59:18 +08:00
as2252258 ac443a49b1 qqq 2023-05-25 16:38:12 +08:00
as2252258 c57e606e55 qqq 2023-05-20 23:05:38 +08:00
as2252258 4f6952bdc5 r 2023-05-02 15:43:20 +08:00
as2252258 ae5e86c7e2 ed 2023-05-02 15:37:10 +08:00
as2252258 027ac6f25b 变更 2023-04-25 23:12:51 +08:00
as2252258 6ff252fe2a 变更 2023-04-25 23:09:16 +08:00
as2252258 e747a988ff 变更 2023-04-25 23:05:54 +08:00
as2252258 b6629987ab 变更 2023-04-25 22:50:21 +08:00
as2252258 8529a8eb0d 变更 2023-04-25 22:48:45 +08:00
as2252258 5e4beed63c 变更 2023-04-25 22:11:40 +08:00
as2252258 def1e1e9c1 变更 2023-04-25 18:04:31 +08:00
as2252258 3a577aaf03 变更 2023-04-25 17:39:27 +08:00
as2252258 7d8ed087f5 变更 2023-04-25 17:36:21 +08:00
as2252258 84568c504a 变更 2023-04-25 17:05:06 +08:00
as2252258 1793fa3667 变更 2023-04-23 11:00:05 +08:00
as2252258 bb9112d3b5 变更 2023-04-23 10:41:42 +08:00
as2252258 688f188d7c 变更 2023-04-21 17:15:13 +08:00
as2252258 711750dba3 变更 2023-04-20 23:22:53 +08:00
as2252258 eedfbb900e 变更 2023-04-20 23:16:36 +08:00
as2252258 d65d7b0d87 变更 2023-04-20 23:08:10 +08:00
as2252258 08f664e136 变更 2023-04-20 22:41:25 +08:00
as2252258 d2029e94a0 变更 2023-04-20 22:37:22 +08:00
as2252258 af966999d0 变更 2023-04-19 21:39:55 +08:00
as2252258 ed0035a707 变更 2023-04-19 10:51:08 +08:00
as2252258 ebbc3368bd 变更 2023-04-18 23:47:30 +08:00
as2252258 7472217482 变更 2023-04-17 11:03:22 +08:00
as2252258 e2c6c2ae46 变更 2023-04-17 10:59:27 +08:00
as2252258 2486bfd4d1 变更 2023-04-17 10:57:38 +08:00
as2252258 1b65f84962 变更 2023-04-17 10:56:35 +08:00
as2252258 5057135590 变更 2023-04-17 10:34:23 +08:00
as2252258 ec1c68be39 变更 2023-04-17 10:27:32 +08:00
as2252258 e47e3f8514 变更 2023-04-17 10:25:01 +08:00
as2252258 8c6f9676b5 变更 2023-04-17 10:18:59 +08:00
as2252258 409f75eff6 变更 2023-04-17 01:39:31 +08:00
as2252258 2e258c7e08 变更 2023-04-16 15:34:50 +08:00
as2252258 64d3f31138 变更 2023-04-16 14:41:20 +08:00
as2252258 0bf69d576d 变更 2023-04-16 02:46:54 +08:00
as2252258 d22955233c 变更 2023-04-16 02:01:26 +08:00
as2252258 f6d4b0fbca 变更 2023-04-16 01:45:33 +08:00
as2252258 3cd2e8d889 变更 2023-04-16 00:59:31 +08:00
as2252258 5600ede420 变更 2023-04-12 10:08:11 +08:00
as2252258 6aac4ac878 变更 2023-04-12 10:07:45 +08:00
as2252258 522d9233ca 变更 2023-04-11 23:49:00 +08:00
as2252258 b8906ef54f 变更 2023-04-11 18:59:17 +08:00
as2252258 fadd7501ee 变更 2023-04-11 18:36:43 +08:00
as2252258 9ab58b43f9 变更 2023-04-11 18:09:29 +08:00
as2252258 ea780bda4a 变更 2023-04-11 18:03:08 +08:00
as2252258 a489d465a7 变更 2023-04-11 18:02:44 +08:00
as2252258 c64e05a81e 变更 2023-04-11 17:05:03 +08:00
as2252258 3b7dd9c709 变更 2023-04-10 18:56:36 +08:00
as2252258 5b3e2bb569 变更 2023-04-10 17:53:20 +08:00
as2252258 ecfe15c3ac 变更 2023-04-10 17:45:29 +08:00
as2252258 b069bc425b 变更 2023-04-10 17:42:12 +08:00
as2252258 a7d8ee83ee 变更 2023-04-10 17:39:36 +08:00
as2252258 f8f22c39c4 变更 2023-04-10 17:35:59 +08:00
as2252258 fdf696624c 变更 2023-04-10 17:34:19 +08:00
as2252258 e002e4b4d0 变更 2023-04-10 17:26:28 +08:00
as2252258 ee8b5fba20 变更 2023-04-10 17:20:48 +08:00
as2252258 94ebb18b7e 变更 2023-04-10 17:13:24 +08:00
as2252258 aaa3fba8ba 变更 2023-04-10 11:36:33 +08:00
as2252258 c77b2ef3eb 变更 2023-04-10 11:19:37 +08:00
as2252258 37009c3f59 变更 2023-04-07 21:57:47 +08:00
as2252258 f7f855d279 变更 2023-04-07 21:54:22 +08:00
as2252258 ad43d02673 变更 2023-04-07 21:47:15 +08:00
as2252258 521fe64b62 变更 2023-04-07 21:45:47 +08:00
as2252258 0a6fbed5d0 变更 2023-04-07 21:41:49 +08:00
as2252258 44d350cfea 变更 2023-04-07 21:37:14 +08:00
as2252258 2170ea7d29 变更 2023-04-07 21:23:38 +08:00
as2252258 33fa378018 变更 2023-04-07 21:20:30 +08:00
as2252258 478106c6f7 变更 2023-04-07 21:13:01 +08:00
as2252258 12d3d05001 变更 2023-04-07 21:10:44 +08:00
as2252258 ebf6862834 变更 2023-04-07 19:00:37 +08:00
as2252258 2153481f69 变更 2023-04-07 18:56:17 +08:00
as2252258 b2807ffe6f 变更 2023-04-07 18:50:26 +08:00
as2252258 b4041de60b 变更 2023-04-07 18:50:02 +08:00
as2252258 5c24528957 变更 2023-04-07 18:35:29 +08:00
as2252258 7833abe644 变更 2023-04-07 18:32:27 +08:00
as2252258 7a782322b5 变更 2023-04-07 18:29:48 +08:00
as2252258 5d41c7219f 变更 2023-04-07 18:23:27 +08:00
as2252258 6d122c4ae1 变更 2023-04-07 18:00:25 +08:00
as2252258 66ecf2585a 变更 2023-04-07 00:11:28 +08:00
as2252258 84f7346d49 变更 2023-04-06 23:08:12 +08:00
as2252258 d3772c0134 变更 2023-04-06 23:07:22 +08:00
as2252258 ff751db14c 变更 2023-04-06 23:01:18 +08:00
as2252258 7fc9a5a908 变更 2023-04-06 22:59:04 +08:00
as2252258 6fe699413d 变更 2023-04-06 22:58:43 +08:00
as2252258 84d1701ff7 变更 2023-04-06 22:55:34 +08:00
as2252258 5a96ca96cc 变更 2023-04-06 22:54:50 +08:00
as2252258 4e978cf919 变更 2023-04-06 22:47:34 +08:00
as2252258 b9dae95149 变更 2023-04-06 22:45:34 +08:00
as2252258 fe1711325b 变更 2023-04-06 22:43:43 +08:00
as2252258 3d8c01a42c 变更 2023-04-06 22:39:31 +08:00
as2252258 4843abfdfa 变更 2023-04-06 22:20:08 +08:00
as2252258 919ed98422 变更 2023-04-06 22:19:45 +08:00
as2252258 a8924c270b 变更 2023-04-06 18:21:55 +08:00
as2252258 582583cd32 变更 2023-04-05 22:09:13 +08:00
as2252258 dd65dfbd17 变更 2023-04-05 21:46:54 +08:00
as2252258 ffc8cd7a33 变更 2023-04-05 20:27:00 +08:00
as2252258 9bb8582aee 变更 2023-04-05 20:19:05 +08:00
as2252258 ca516ee766 变更 2023-04-05 20:15:40 +08:00
as2252258 03c908e4da 变更 2023-04-05 14:09:46 +08:00
as2252258 b75204e53e 变更 2023-04-05 14:06:00 +08:00
as2252258 b5a7dfa98e 变更 2023-04-05 13:58:31 +08:00
as2252258 81c2b0e114 变更 2023-04-05 13:49:17 +08:00
as2252258 f6c87aadaf 变更 2023-04-05 12:35:18 +08:00
as2252258 225af0038d 变更 2023-04-05 11:08:34 +08:00
as2252258 f52609844c 变更 2023-04-05 11:06:45 +08:00
as2252258 6a673152c1 变更 2023-04-05 10:53:28 +08:00
as2252258 3ecc781002 变更 2023-04-05 10:50:17 +08:00
as2252258 732b922a4c 变更 2023-04-05 10:26:28 +08:00
as2252258 c86eb1a83d 变更 2023-04-05 10:15:50 +08:00
as2252258 bc3c286416 变更 2023-04-05 09:53:32 +08:00
as2252258 5c6663c002 变更 2023-04-04 14:49:43 +08:00
as2252258 dd0eb233bb 变更 2023-04-04 14:49:26 +08:00
as2252258 1370891b93 变更 2023-04-04 14:44:09 +08:00
as2252258 a25634f07a 变更 2023-04-04 14:38:27 +08:00
as2252258 5064e5d683 变更 2023-04-04 14:29:33 +08:00
as2252258 e55eb19768 变更 2023-04-04 14:26:11 +08:00
as2252258 faed634616 变更 2023-04-03 17:56:12 +08:00
as2252258 1d7ffcda89 变更 2023-04-03 17:40:44 +08:00
as2252258 92ab70d59d 变更 2023-04-03 17:30:07 +08:00
as2252258 4e109ffdf8 变更 2023-04-03 13:45:59 +08:00
as2252258 7eb80d607b 变更 2023-04-03 11:08:11 +08:00
as2252258 6d646ea2a3 变更 2023-04-03 00:43:06 +08:00
as2252258 3c240efa7a 变更 2023-04-03 00:37:30 +08:00
as2252258 59cf5e7d64 变更 2023-04-02 23:58:50 +08:00
as2252258 aafb0dd5e0 变更 2023-04-02 23:31:33 +08:00
as2252258 d9fd802312 变更 2023-04-02 23:28:23 +08:00
as2252258 cd9327f5c2 变更 2023-04-02 23:23:55 +08:00
as2252258 f1faeb038f 变更 2023-04-02 23:07:16 +08:00
as2252258 f3a863f55c 变更 2023-04-02 20:13:56 +08:00
as2252258 0a323b5f62 变更 2023-04-02 20:13:19 +08:00
as2252258 735c077f46 变更 2023-04-02 20:09:40 +08:00
as2252258 0f28608180 变更 2023-04-02 20:03:19 +08:00
as2252258 67fdee8fd9 变更 2023-04-02 19:57:48 +08:00
as2252258 f554b4c81e 变更 2023-04-02 19:53:43 +08:00
as2252258 685a0d72b2 变更 2023-04-02 01:26:10 +08:00
as2252258 69648e3373 变更 2023-04-02 01:23:48 +08:00
as2252258 555ef93965 变更 2023-04-02 01:21:39 +08:00
as2252258 4fd91f5f4f 变更 2023-04-01 21:05:50 +08:00
as2252258 0450226d36 变更 2023-04-01 21:04:28 +08:00
as2252258 eac2800561 变更 2023-04-01 20:57:07 +08:00
as2252258 6493bf91bf 变更 2023-04-01 20:48:03 +08:00
as2252258 1dfe32bf3c 变更 2023-04-01 18:50:44 +08:00
as2252258 cd94d2dc57 变更 2023-04-01 17:11:47 +08:00
as2252258 2aec2afc59 变更 2023-04-01 16:03:02 +08:00
as2252258 509eabe78e 变更 2023-04-01 15:58:17 +08:00
as2252258 c805173600 变更 2023-04-01 15:56:45 +08:00
as2252258 9b1489f576 变更 2023-04-01 15:54:38 +08:00
as2252258 e2f94c9202 变更 2023-04-01 01:27:57 +08:00
as2252258 98fff7302e 变更 2023-04-01 00:37:52 +08:00
as2252258 afec8be942 变更 2023-04-01 00:32:04 +08:00
as2252258 0f911db7ee 变更 2023-04-01 00:29:28 +08:00
as2252258 f17b163f83 变更 2023-04-01 00:27:08 +08:00
as2252258 79db796ba3 变更 2023-04-01 00:25:22 +08:00
as2252258 c165f5f205 变更 2023-03-31 23:36:07 +08:00
as2252258 656753cf60 变更 2023-03-31 11:09:12 +08:00
as2252258 783703eb0f 变更 2023-03-31 10:37:15 +08:00
as2252258 104ddcc34a 变更 2023-03-31 10:31:50 +08:00
as2252258 ff27c0de65 变更 2023-03-30 23:50:51 +08:00
as2252258 32ab38d5cd 变更 2023-03-30 23:46:00 +08:00
as2252258 24d8959aaf 变更 2023-03-30 23:42:55 +08:00
as2252258 5c9f92f955 变更 2023-03-30 20:39:00 +08:00
as2252258 bfa39c3669 变更 2023-03-30 18:59:50 +08:00
as2252258 af3e6d580e 变更 2023-02-13 17:00:32 +08:00
as2252258 677bb7ad42 变更 2023-02-13 16:47:50 +08:00
as2252258 8d1c0d056f 变更 2023-02-13 14:37:07 +08:00
as2252258 ed24164680 变更 2023-02-13 14:27:28 +08:00
as2252258 59e25c1648 变更 2023-02-13 14:23:28 +08:00
as2252258 97d8f8fac5 变更 2023-02-10 10:56:19 +08:00
as2252258 17e51b7898 变更 2023-02-08 17:33:07 +08:00
as2252258 dba7d6085a 变更 2023-02-07 17:44:02 +08:00
as2252258 add511ed2a 变更 2023-02-07 17:05:21 +08:00
as2252258 dfbfa701e5 变更 2023-02-07 17:01:45 +08:00
as2252258 73561a8983 变更 2023-02-07 16:46:13 +08:00
as2252258 603e0a41fc 变更 2023-02-07 15:17:08 +08:00
as2252258 728b253c0b 变更 2023-02-06 14:10:54 +08:00
as2252258 5cc86e0f0c 变更 2023-02-06 14:08:38 +08:00
as2252258 3c73bc89ba 变更 2023-02-06 14:04:40 +08:00
as2252258 564877c579 变更 2023-02-06 14:01:06 +08:00
as2252258 007c40a0eb 变更 2023-02-06 13:54:53 +08:00
as2252258 f86d155059 变更 2023-01-30 11:03:58 +08:00
as2252258 767288886f 变更 2022-12-12 17:31:12 +08:00
as2252258 78200ebffd 变更 2022-12-12 15:04:11 +08:00
as2252258 707e13609f 变更 2022-12-12 11:27:34 +08:00
as2252258 d53b001ef7 变更 2022-12-12 11:19:27 +08:00
as2252258 77f63a85ed 变更 2022-09-29 23:06:07 +08:00
as2252258 76154631e5 变更 2022-09-29 22:58:43 +08:00
as2252258 62f3747701 变更 2022-09-29 22:53:39 +08:00
as2252258 f87a3c386f 变更 2022-09-29 22:51:41 +08:00
as2252258 7554f6b161 变更 2022-09-29 22:38:25 +08:00
as2252258 322168ff68 变更 2022-09-29 18:58:32 +08:00
as2252258 02eddb98a2 变更 2022-09-29 18:54:14 +08:00
as2252258 fc828df683 变更 2022-09-21 14:40:24 +08:00
as2252258 1bc5e49b42 变更 2022-09-21 14:27:26 +08:00
as2252258 4a654821a1 变更 2022-09-20 18:53:49 +08:00
as2252258 dbf33b6d7d 变更 2022-09-20 18:35:20 +08:00
as2252258 202852a95d 变更 2022-09-20 18:34:10 +08:00
as2252258 72cfba6a5d 变更 2022-09-20 18:33:03 +08:00
as2252258 145970316f 变更 2022-09-20 18:23:19 +08:00
as2252258 58c3b38c73 变更 2022-09-20 18:15:50 +08:00
as2252258 4dc55fd772 变更 2022-09-20 11:17:59 +08:00
as2252258 41bce52883 变更 2022-09-19 18:39:28 +08:00
as2252258 17c8d45349 变更 2022-09-19 18:24:12 +08:00
as2252258 91cc3c0613 变更 2022-09-18 03:34:13 +08:00
as2252258 ca011c4557 change delete returned 2022-09-12 02:49:18 +08:00
as2252258 9d81bc6479 变更 2022-09-07 14:36:32 +08:00
as2252258 c7ad5123e2 变更 2022-09-07 14:31:10 +08:00
as2252258 e3169b1348 Merge branch 'master' of github.com:as2252258/kiri-databases 2022-09-02 16:40:17 +08:00
as2252258 8f20d59ff8 变更 2022-09-02 16:39:17 +08:00
as2252258 1c287da18d 变更 2022-07-12 02:02:36 +08:00
as2252258 2845c6c1ac 变更 2022-07-12 01:42:32 +08:00
as2252258 e41d03ea27 modify plugin name 2022-07-11 18:53:19 +08:00
as2252258 9fa91a05ef modify plugin name 2022-07-11 18:34:30 +08:00
as2252258 f14b198e25 modify plugin name 2022-07-11 16:28:05 +08:00
as2252258 49cd130c81 modify plugin name 2022-07-11 16:26:51 +08:00
as2252258 e33776314b modify plugin name 2022-07-11 16:26:14 +08:00
as2252258 64fd34a23c modify plugin name 2022-07-11 16:09:57 +08:00
as2252258 bad445aeb6 modify plugin name 2022-07-11 14:54:46 +08:00
as2252258 db97de7e40 modify plugin name 2022-07-11 14:46:46 +08:00
as2252258 531b54edb2 modify plugin name 2022-07-11 14:34:41 +08:00
as2252258 56fdf310c8 modify plugin name 2022-07-11 14:26:49 +08:00
as2252258 c946eed53f modify plugin name 2022-07-11 14:16:16 +08:00
as2252258 bc728071c7 modify plugin name 2022-07-11 14:15:21 +08:00
as2252258 4316e88677 变更 2022-07-10 02:31:30 +08:00
as2252258 7509597582 变更 2022-07-10 01:23:32 +08:00
as2252258 86798409fd 变更 2022-07-10 01:19:09 +08:00
as2252258 bd3eb54a05 变更 2022-07-10 01:09:08 +08:00
as2252258 8f6a639317 变更 2022-07-10 00:41:56 +08:00
as2252258 8796e60285 变更 2022-07-10 00:31:10 +08:00
as2252258 be3ceb8ccc 变更 2022-06-30 16:40:28 +08:00
as2252258 e0fc63d78c 变更 2022-06-23 00:45:43 +08:00
as2252258 0d4a690e28 modify plugin name 2022-06-22 19:16:07 +08:00
as2252258 20eff5c29a modify plugin name 2022-06-22 18:28:46 +08:00
as2252258 c72c5d4e0d modify plugin name 2022-06-22 16:29:42 +08:00
as2252258 e98c67e0c3 modify plugin name 2022-06-20 18:40:14 +08:00
as2252258 a6c59331af modify plugin name 2022-06-17 15:09:10 +08:00
as2252258 1314ccc421 modify plugin name 2022-06-17 15:08:41 +08:00
as2252258 39777985f7 modify plugin name 2022-06-17 14:25:29 +08:00
as2252258 744acdc826 modify plugin name 2022-06-17 14:11:26 +08:00
as2252258 cf79882a8f modify plugin name 2022-06-17 14:07:16 +08:00
as2252258 e4bf6f22be modify plugin name 2022-06-17 14:04:23 +08:00
as2252258 6c721e957b modify plugin name 2022-06-17 13:54:11 +08:00
as2252258 e224d2e46b modify plugin name 2022-06-17 12:17:23 +08:00
as2252258 ad140bbc00 modify plugin name 2022-06-17 12:05:37 +08:00
as2252258 1f2475b09a modify plugin name 2022-06-17 11:59:19 +08:00
as2252258 41ae0c6118 modify plugin name 2022-06-16 17:38:22 +08:00
as2252258 7d5aec2716 modify plugin name 2022-06-09 10:40:43 +08:00
as2252258 d9b9b78128 modify plugin name 2022-06-09 10:05:37 +08:00
as2252258 9a93687c53 modify plugin name 2022-06-09 10:03:14 +08:00
as2252258 5d395bd6f4 modify plugin name 2022-06-09 10:02:57 +08:00
as2252258 92c6b48cb2 modify plugin name 2022-06-08 16:44:45 +08:00
as2252258 2f9e0a9196 modify plugin name 2022-06-08 16:42:16 +08:00
as2252258 62591edf44 modify plugin name 2022-06-08 16:13:16 +08:00
as2252258 eb36a30b26 modify plugin name 2022-06-08 15:13:53 +08:00
as2252258 a5dde8a140 modify plugin name 2022-06-08 15:13:02 +08:00
as2252258 ba6d854035 modify plugin name 2022-06-08 14:44:55 +08:00
as2252258 9ce43895b1 modify plugin name 2022-06-08 14:43:23 +08:00
as2252258 b8542f6a70 modify plugin name 2022-06-08 14:14:17 +08:00
as2252258 c829192e9c modify plugin name 2022-06-08 14:08:31 +08:00
as2252258 56a20d0f90 变更 2022-05-30 16:55:42 +08:00
as2252258 01d58b4896 变更 2022-05-30 16:55:15 +08:00
as2252258 6046f112f9 变更 2022-05-30 16:54:57 +08:00
as2252258 5670dca21a 变更 2022-05-29 01:52:10 +08:00
as2252258 63d58218e4 modify mysql result 2022-04-28 03:24:58 +08:00
as2252258 2ace780fcb modify mysql result 2022-04-28 03:22:53 +08:00
as2252258 7ea0bee73f modify mysql result 2022-04-28 03:15:44 +08:00
as2252258 6e7955f1d9 modify mysql result 2022-04-10 14:15:39 +08:00
as2252258 cb377b4e69 modify mysql result 2022-04-08 19:19:50 +08:00
as2252258 d4cf6da9fe modify mysql result 2022-04-08 19:18:02 +08:00
as2252258 2057f2ef53 modify plugin name 2022-03-03 18:30:59 +08:00
as2252258 be3106394f modify plugin name 2022-03-01 17:39:54 +08:00
as2252258 cbcad9e7c3 modify plugin name 2022-03-01 17:37:45 +08:00
as2252258 cdec00038c modify plugin name 2022-03-01 17:32:45 +08:00
as2252258 963c495a4d modify plugin name 2022-03-01 17:23:26 +08:00
as2252258 9d23efaed8 modify plugin name 2022-03-01 17:17:23 +08:00
as2252258 918374f1b2 modify plugin name 2022-03-01 17:16:53 +08:00
as2252258 1c7e0814b8 modify plugin name 2022-03-01 16:55:26 +08:00
as2252258 ae135e3538 modify plugin name 2022-03-01 16:51:54 +08:00
as2252258 9fe4c7fd50 modify plugin name 2022-02-28 14:40:13 +08:00
as2252258 ceb8e10d1c modify plugin name 2022-02-27 15:35:31 +08:00
as2252258 036a722753 modify plugin name 2022-02-27 15:31:55 +08:00
as2252258 500f4904f9 modify plugin name 2022-02-27 15:31:21 +08:00
as2252258 9e94626ca5 modify plugin name 2022-02-27 15:29:23 +08:00
as2252258 d4998d4ffc modify plugin name 2022-02-27 10:48:48 +08:00
as2252258 42b2715cf5 modify plugin name 2022-02-27 10:31:35 +08:00
as2252258 df4165ce52 modify plugin name 2022-02-25 19:00:32 +08:00
as2252258 34ae502806 modify plugin name 2022-02-25 18:54:26 +08:00
as2252258 de52786503 modify plugin name 2022-02-25 18:47:45 +08:00
as2252258 0900304c17 modify plugin name 2022-02-25 18:40:41 +08:00
as2252258 521bb2ee7f modify plugin name 2022-02-25 18:37:49 +08:00
as2252258 d6dce71c6c modify plugin name 2022-02-25 18:32:20 +08:00
as2252258 a78760cc3e modify plugin name 2022-02-25 18:23:49 +08:00
as2252258 1e27d52857 modify plugin name 2022-02-25 18:21:57 +08:00
as2252258 1c24908d23 modify plugin name 2022-02-25 18:18:43 +08:00
as2252258 1d809efda8 modify plugin name 2022-02-25 18:16:50 +08:00
as2252258 6febf0c20f modify plugin name 2022-02-25 18:11:09 +08:00
as2252258 eff6ae0e95 modify plugin name 2022-02-25 18:06:04 +08:00
as2252258 dd6424b4ba modify plugin name 2022-02-25 18:05:46 +08:00
as2252258 093d316605 modify plugin name 2022-02-25 18:05:27 +08:00
as2252258 80620e181b modify plugin name 2022-02-25 16:25:32 +08:00
as2252258 1079cab985 modify plugin name 2022-02-25 15:57:40 +08:00
as2252258 a8a2a1fbeb modify plugin name 2022-02-25 15:57:05 +08:00
as2252258 6b3953311a modify plugin name 2022-02-25 15:39:08 +08:00
as2252258 4aff922c83 modify plugin name 2022-02-24 15:24:28 +08:00
as2252258 453481abdb modify plugin name 2022-02-24 11:34:53 +08:00
as2252258 69dbc1e7f3 modify plugin name 2022-02-24 11:00:00 +08:00
as2252258 55f665e10b modify plugin name 2022-02-23 18:02:28 +08:00
as2252258 f54f6fbde3 modify plugin name 2022-02-23 17:24:33 +08:00
as2252258 1ace36b6f6 modify plugin name 2022-02-23 17:08:35 +08:00
as2252258 49d8f03cd0 modify plugin name 2022-02-23 17:07:24 +08:00
as2252258 f5fd9401dd modify plugin name 2022-02-23 16:32:08 +08:00
as2252258 71c95a824c modify plugin name 2022-02-23 11:17:50 +08:00
as2252258 5e0e76fa57 modify plugin name 2022-02-23 11:16:19 +08:00
as2252258 89b4e57726 modify plugin name 2022-02-23 11:13:18 +08:00
as2252258 29c6478169 modify plugin name 2022-02-23 11:08:35 +08:00
as2252258 ecb63ff136 modify plugin name 2022-02-23 11:01:19 +08:00
as2252258 17316250a9 modify plugin name 2022-02-21 10:56:37 +08:00
as2252258 50b6d79756 modify plugin name 2022-02-21 10:52:24 +08:00
as2252258 9626e061b8 modify plugin name 2022-02-21 10:48:43 +08:00
as2252258 3d12b2ea6a modify plugin name 2022-02-18 17:53:05 +08:00
as2252258 ad02cbba65 modify plugin name 2022-02-18 17:27:24 +08:00
as2252258 0278a4aac2 modify plugin name 2022-02-18 15:28:29 +08:00
as2252258 0bd7139232 modify plugin name 2022-02-18 15:16:40 +08:00
as2252258 389ad887e6 modify plugin name 2022-02-18 15:11:44 +08:00
as2252258 576f97a53d modify plugin name 2022-02-18 14:54:54 +08:00
as2252258 42bea9d93a modify plugin name 2022-02-18 14:45:58 +08:00
as2252258 0163758d8e modify plugin name 2022-02-17 18:31:33 +08:00
as2252258 450569fce7 modify plugin name 2022-02-17 18:24:15 +08:00
as2252258 83a29b770a modify plugin name 2022-02-17 18:22:24 +08:00
as2252258 2ebdcbd06b modify plugin name 2022-02-14 17:56:53 +08:00
as2252258 5bac6a843d Revert "改名"
This reverts commit fdf58326
2022-02-10 11:46:02 +08:00
as2252258 5aa5d3ea13 Revert "改名"
This reverts commit fdf58326
2022-01-20 19:04:15 +08:00
as2252258 b10ffd832a Revert "改名"
This reverts commit fdf58326
2022-01-19 14:59:46 +08:00
as2252258 d42fc588b4 Revert "改名"
This reverts commit fdf58326
2022-01-19 14:57:14 +08:00
as2252258 d5b58a1fc7 Revert "改名"
This reverts commit fdf58326
2022-01-19 14:32:23 +08:00
as2252258 cada515dc7 Revert "改名"
This reverts commit fdf58326
2022-01-19 11:30:58 +08:00
as2252258 5d73bb2c33 Revert "改名"
This reverts commit fdf58326
2022-01-18 18:55:36 +08:00
as2252258 164065493e Revert "改名"
This reverts commit fdf58326
2022-01-18 18:53:54 +08:00
as2252258 7cf28a3aaa Revert "改名"
This reverts commit fdf58326
2022-01-18 18:07:58 +08:00
as2252258 63a1322025 Revert "改名"
This reverts commit fdf58326
2022-01-18 18:07:35 +08:00
as2252258 e89062a2d7 Revert "改名"
This reverts commit fdf58326
2022-01-18 18:06:24 +08:00
as2252258 bc0fa6b8d5 Revert "改名"
This reverts commit fdf58326
2022-01-18 18:01:44 +08:00
as2252258 ffadbdab83 Revert "改名"
This reverts commit fdf58326
2022-01-18 17:56:47 +08:00
as2252258 267358263a Revert "改名"
This reverts commit fdf58326
2022-01-18 17:55:39 +08:00
as2252258 209621f222 Revert "改名"
This reverts commit fdf58326
2022-01-18 17:53:32 +08:00
as2252258 6e75fa462b Revert "改名"
This reverts commit fdf58326
2022-01-18 17:24:50 +08:00
as2252258 57f9e3254d Revert "改名"
This reverts commit fdf58326
2022-01-17 11:48:11 +08:00
as2252258 22194bbdbc Revert "改名"
This reverts commit fdf58326
2022-01-17 11:31:26 +08:00
as2252258 5b22955966 Revert "改名"
This reverts commit fdf58326
2022-01-14 16:50:01 +08:00
as2252258 ac0c08f909 Revert "改名"
This reverts commit fdf58326
2022-01-14 16:23:15 +08:00
as2252258 c1e480cd43 Revert "改名"
This reverts commit fdf58326
2022-01-14 16:12:20 +08:00
as2252258 61741f926f Revert "改名"
This reverts commit fdf58326
2022-01-14 16:11:23 +08:00
as2252258 f3d878e21c Revert "改名"
This reverts commit fdf58326
2022-01-14 16:07:57 +08:00
as2252258 4ca0a61344 Revert "改名"
This reverts commit fdf58326
2022-01-14 16:06:31 +08:00
as2252258 6d699df26e Revert "改名"
This reverts commit fdf58326
2022-01-14 16:05:11 +08:00
as2252258 22e094804a Revert "改名"
This reverts commit fdf58326
2022-01-14 16:03:26 +08:00
as2252258 f8a07c30ae Revert "改名"
This reverts commit fdf58326
2022-01-14 16:03:01 +08:00
as2252258 c735776e58 Revert "改名"
This reverts commit fdf58326
2022-01-14 15:52:38 +08:00
as2252258 c5b4f4795b Revert "改名"
This reverts commit fdf58326
2022-01-14 14:15:34 +08:00
as2252258 661206b94b Revert "改名"
This reverts commit fdf58326
2022-01-14 11:29:15 +08:00
as2252258 61f589058e Revert "改名"
This reverts commit fdf58326
2022-01-13 16:32:44 +08:00
as2252258 6d83f647f4 Revert "改名"
This reverts commit fdf58326
2022-01-12 17:51:00 +08:00
as2252258 870595b810 Revert "改名"
This reverts commit fdf58326
2022-01-12 17:49:02 +08:00
as2252258 970f22804d Revert "改名"
This reverts commit fdf58326
2022-01-12 17:48:48 +08:00
as2252258 dad6a63ee2 Revert "改名"
This reverts commit fdf58326
2022-01-12 14:10:32 +08:00
as2252258 47ed967e46 Revert "改名"
This reverts commit fdf58326
2022-01-11 10:51:50 +08:00
as2252258 227aad2910 Revert "改名"
This reverts commit fdf58326
2022-01-10 11:39:56 +08:00
as2252258 5d77face53 1 2022-01-09 14:46:34 +08:00
as2252258 2109ed7667 e 2022-01-09 03:49:51 +08:00
as2252258 a45d71d760 1 2022-01-09 03:46:41 +08:00
as2252258 e71adc7cf3 1 2022-01-09 02:44:07 +08:00
as2252258 ef3e874c0c Revert "改名"
This reverts commit fdf58326
2022-01-08 18:49:06 +08:00
as2252258 614b601afa Revert "改名"
This reverts commit fdf58326
2022-01-07 14:38:36 +08:00
as2252258 cf2f26ec21 Revert "改名"
This reverts commit fdf58326
2022-01-04 17:27:37 +08:00
as2252258 2a52172af6 Revert "改名"
This reverts commit fdf58326
2022-01-04 16:04:22 +08:00
as2252258 5bf8a7feb1 1 2021-12-17 04:30:21 +08:00
as2252258 52d26c4481 1 2021-12-17 04:28:23 +08:00
as2252258 dd369d348c 1 2021-12-17 04:12:44 +08:00
as2252258 547bb85ba9 1 2021-12-12 23:55:38 +08:00
as2252258 4ddcc87263 1 2021-12-12 23:54:27 +08:00
as2252258 31a6862d62 1 2021-12-12 23:52:35 +08:00
as2252258 6e1d1a300a 1 2021-12-12 06:06:08 +08:00
as2252258 e260e43c17 1 2021-12-12 04:29:35 +08:00
as2252258 76292522e4 1 2021-12-12 04:29:09 +08:00
as2252258 7eb6151111 改名 2021-12-08 11:32:31 +08:00
as2252258 33db0bc463 改名 2021-12-07 16:43:19 +08:00
as2252258 014ccb5fb8 改名 2021-12-06 17:01:55 +08:00
as2252258 ad537d1085 改名 2021-12-06 16:13:52 +08:00
as2252258 8be23dd4c7 改名 2021-12-06 16:12:53 +08:00
as2252258 f75457b18a 改名 2021-12-06 16:12:10 +08:00
as2252258 89b30b8bc8 改名 2021-12-06 16:11:23 +08:00
as2252258 beb522a8bf 改名 2021-12-06 16:07:52 +08:00
as2252258 a883a65e3b 改名 2021-12-06 16:03:39 +08:00
as2252258 34f8aaaca5 改名 2021-12-06 15:47:11 +08:00
as2252258 89bd7a8ee9 改名 2021-12-06 15:39:35 +08:00
as2252258 bf0d2f7611 改名 2021-12-03 15:42:04 +08:00
as2252258 348b850c31 改名 2021-12-03 15:29:18 +08:00
as2252258 fd830c6a9e 改名 2021-12-03 14:51:20 +08:00
as2252258 6936b5cc8f 改名 2021-11-30 15:10:01 +08:00
as2252258 f50782c930 改名 2021-11-30 11:37:13 +08:00
as2252258 731af328d7 1 2021-11-27 17:43:28 +08:00
84 changed files with 6289 additions and 7312 deletions
+34 -34
View File
@@ -1,34 +1,34 @@
# Created by .ignore support plugin (hsz.mobi)
### Yii template
assets/*
!assets/.gitignore
protected/runtime/*
!protected/runtime/.gitignore
protected/data/*.db
themes/classic/views/
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
composer.lock
*.log
commands/result
config/setting.php
tests/
vendor/
runtime/
*.xml
*.lock
oot
d
composer.lock
# Created by .ignore support plugin (hsz.mobi)
### Yii template
assets/*
!assets/.gitignore
protected/runtime/*
!protected/runtime/.gitignore
protected/data/*.db
themes/classic/views/
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
composer.lock
*.log
commands/result
config/setting.php
tests/
vendor/
runtime/
*.xml
*.lock
oot
d
composer.lock
-15
View File
@@ -1,15 +0,0 @@
<?php
namespace PHPSTORM_META {
// Reflect
use Kiri\Di\Container;
override(Container::get(0), map('@'));
override(Container::create(0), map('@'));
// override(\Hyperf\Utils\Context::get(0), map('@'));
// override(\make(0), map('@'));
override(\di(0), map('@'));
override(\duplicate(0), map('@'));
}
+293
View File
@@ -0,0 +1,293 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 14:42
*/
declare(strict_types=1);
namespace Database;
use Closure;
use Database\Traits\QueryTrait;
use Kiri\Di\Context;
use Swoole\Coroutine;
/**
* Class ActiveQuery
* @package Database
*/
class ActiveQuery extends QueryTrait implements ISqlBuilder
{
public bool $asArray = FALSE;
protected mixed $_mock = NULL;
/**
* @param bool $asArray
*
* @return static
*/
public function asArray(bool $asArray = TRUE): static
{
$this->asArray = $asArray;
return $this;
}
/**
* @param array $methods
*
* @return $this
*/
public function with(array $methods): static
{
$this->modelClass->setWith($methods);
return $this;
}
/**
* @return ModelInterface|array|null|bool
* @throws
*/
public function first(): ModelInterface|null|array|bool
{
$data = $this->buildCommand($this->builder->one())->one();
if (is_array($data)) {
return $this->populate($data);
}
return NULL;
}
/**
* @return bool|Collection
*/
public function get(): bool|Collection
{
$data = $this->buildCommand($this->builder->all())->all();
if (is_array($data)) {
return new Collection($this, $this->modelClass, $data);
}
return FALSE;
}
/**
* @param string $column
* @param int|float $amount
*
* @return bool
*/
public function increment(string $column, int|float $amount = 1): bool
{
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount]))->exec();
}
/**
* @param array $attributes
*
* @return bool
*/
public function increments(array $attributes): bool
{
return (bool)$this->buildCommand($this->builder->mathematics($attributes))->exec();
}
/**
* @param string $column
* @param int|float $amount
*
* @return bool
*/
public function decrement(string $column, int|float $amount = 1): bool
{
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount], '-'))->exec();
}
/**
* @param array $attributes
*
* @return bool
*/
public function decrements(array $attributes): bool
{
return (bool)$this->buildCommand($this->builder->mathematics($attributes, '-'))->exec();
}
/**
* @throws
*/
public function flush(): bool
{
return (bool)$this->buildCommand($this->builder->truncate())->exec();
}
/**
* @param int $size
* @param Closure $closure
*
* @return void
*/
public function chunk(int $size, Closure $closure): void
{
if ($this->offset === -1) $this->offset = 0;
$data = $this->offset($this->offset)->limit($size)->get();
if (!$data || $data->isEmpty()) {
return;
}
if (Context::inCoroutine()) {
Coroutine::create(fn() => $closure($data));
} else {
call_user_func($closure, $data);
}
$this->offset += $size;
$this->chunk($size, $closure);
}
/**
* @param string $field
* @param string|null $setKey
*
* @return array|null
*/
public function column(string $field, ?string $setKey = NULL): ?array
{
return $this->get()->column($field, $setKey);
}
/**
* @param mixed $value
*
* @return $this
*/
public function withMock(mixed $value): static
{
$this->_mock = $value;
return $this;
}
/**
* @return mixed
*/
public function mock(): mixed
{
return $this->_mock;
}
/**
* @param array $data
*
* @return ModelInterface|array
* @throws
*/
public function populate(array $data): ModelInterface|array
{
$model = $this->modelClass->populates($data);
return $this->asArray ? $model->toArray() : $model;
}
/**
* @return string
*/
public function toSql(): string
{
return $this->builder->get();
}
/**
* @return int
* @throws
*/
public function count(): int
{
return $this->buildCommand($this->builder->count())->rowCount();
}
/**
* @param array $data
*
* @return bool
* @throws
*/
public function update(array $data): bool
{
if (count($data) < 1) {
return TRUE;
}
$generate = $this->builder->update($data);
if (!is_bool($generate)) {
return (bool)$this->buildCommand($generate)->exec();
} else {
return $generate;
}
}
/**
* @param array $data
*
* @return bool
*/
public function insert(array $data): bool
{
$sql = $this->builder->insert($data, isset($data[0]));
return (bool)$this->buildCommand($sql)->exec();
}
/**
* @param string $filed
*
* @return mixed
* @throws
*/
public function value(string $filed): mixed
{
return $this->first()[$filed] ?? NULL;
}
/**
* @return bool
* @throws
*/
public function exists(): bool
{
return $this->buildCommand($this->limit(1)->builder->exists())->exists();
}
/**
* @param string $sql
* @param array $params
*
* @return int|bool
*/
public function execute(string $sql, array $params = []): int|bool
{
return $this->buildCommand($sql, $params)->exec();
}
/**
* @return bool
*/
public function delete(): bool
{
return (bool)$this->buildCommand($this->builder->delete())->exec();
}
}
@@ -1,8 +1,8 @@
<?php
namespace Database\Affair;
class BeginTransaction
{
}
<?php
namespace Database\Affair;
class BeginTransaction
{
}
+8 -10
View File
@@ -1,10 +1,8 @@
<?php
namespace Database\Affair;
use Exception;
class Commit
{
}
<?php
namespace Database\Affair;
class Commit
{
}
@@ -1,8 +1,8 @@
<?php
namespace Database\Affair;
class Rollback
{
}
<?php
namespace Database\Affair;
class Rollback
{
}
+288
View File
@@ -0,0 +1,288 @@
<?php
declare(strict_types=1);
namespace Database;
use Co\Channel;
use Exception;
use Swoole\Coroutine;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
*
*/
class BackupCommand extends Command
{
public string $command = 'db:backup';
public string $description = 'php kiri.php db:backup --database users --table u_user --data 1 /Users/admin/snowflake-bi/test.sql';
public array $percentStatus = [];
/**
*
*/
protected function configure(): void
{
$this->setName('db:backup')
->addOption('data', 'd', InputArgument::OPTIONAL)
->addArgument('path', InputArgument::REQUIRED, "save to path", null)
->addOption('table', 't', InputArgument::OPTIONAL)
->addOption('database', 'db', InputArgument::OPTIONAL)
->setDescription('php kiri.php db:backup --database users --table u_user --data 1 /Users/admin/snowflake-bi/test.sql');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws Exception
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
try {
/** @var Connection $data */
$data = \Kiri::getDi()->get(DatabasesProviders::class)->get($input->getOption('database'));
$table = $input->getOption('table');
if ($table !== null) {
$table = explode(',', $table);
} else {
$tmp = $data->createCommand('show tables from `' . $data->database . '`')->all();
$table = [];
foreach ($tmp as $value) {
$table[] = current($value);
}
}
$path = $input->getArgument('path');
if (!str_starts_with($path, '/')) {
$path = APP_PATH . $path;
}
$isData = $input->getOption('data');
if (!is_dir($path)) {
mkdir($path);
}
$waite = new Coroutine\WaitGroup();
foreach ($table as $value) {
$tableInfo = $data->createCommand('show create table `' . $data->database . '`.`' . $value . '`')->one();
$tmp = rtrim($path, '/') . '/' . current($tableInfo) . '.sql';
if (!file_exists($tmp)) {
touch($tmp);
}
file_put_contents($tmp, '');
$tableCreator = next($tableInfo);
if (preg_match('/AUTO_INCREMENT=\d+\s/', $tableCreator)) {
$tableCreator = preg_replace('/AUTO_INCREMENT=\d+\s/', 'AUTO_INCREMENT=1 ', $tableCreator);
}
file_put_contents($tmp, $tableCreator . ';' . PHP_EOL . PHP_EOL, FILE_APPEND);
if ($isData == 1) {
$waite->add(1);
Coroutine::create(function () use ($waite, $input, $value, $tmp) {
defer(function () use ($waite) {
$waite->done();
});
$this->writeData($input->getOption('database'), $value, $tmp);
});
}
}
$waite->wait();
$output->write('dump data success');
} catch (\Throwable $throwable) {
$output->writeln(throwable($throwable));
} finally {
return 1;
}
}
public bool $isEnd = false;
/**
* @param string $dbname
* @param string $tableName
* @param string $path
* @return void
* @throws Exception
*/
public function writeData(string $dbname, string $tableName, string $path): void
{
$offset = 0;
$size = 1000;
$channel = new Channel(200);
for ($i = 0; $i < 200; $i++) {
Coroutine::create(function () use ($channel, $path, $dbname, $tableName) {
while ($channel->errCode != SWOOLE_CHANNEL_CLOSED) {
if (($data = $channel->pop()) === false) {
break;
}
if (($value = json_decode($data, true)) === null) {
continue;
}
$value = $this->toSql($dbname, $tableName, $value);
file_put_contents($path, $value . PHP_EOL, FILE_APPEND);
}
});
}
$id = Coroutine::create(function () use ($channel) {
$data = Coroutine::waitSignal(SIGTERM | SIGINT);
if ($data) {
$this->isEnd = true;
}
});
/** @var Connection $database */
$database = \Kiri::getDi()->get(DatabasesProviders::class)->get($dbname);
$total = $database->createCommand("SELECT COUNT(*) as total FROM " . $tableName)->one()['total'];
$startTime = time();
$wait = new Coroutine\WaitGroup();
while ($this->isEnd === false && $offset < $total) {
$wait->add(1);
Coroutine::create(function () use ($wait, $offset, $size, $total, $dbname, $tableName, $channel) {
defer(function () use ($wait) {
$wait->done();
});
/** @var Connection $database */
$database = \Kiri::getDi()->get(DatabasesProviders::class)->get($dbname);
$data = $database->createCommand("SELECT * FROM $tableName LIMIT $offset,$size")->all();
if (is_bool($data) || count($data) < 1) {
return;
}
$channel->push(json_encode($data));
$this->percentStatus[$dbname . $tableName] = intval(round(($offset + $size) / $total, 2) * 100);
$this->outputProgress(true);
});
$offset += $size;
}
$wait->wait();
echo 'use time ' . (time() - $startTime) . 's' . PHP_EOL;
$channel->close();
Coroutine::cancel($id);
}
/**
* @param string $dbname
* @param string $value
* @param array $data
* @return string
*/
public function toSql(string $dbname, string $value, array $data): string
{
$strings = ['INSERT INTO ' . $dbname . '.' . $value, '(' . implode(',', array_keys($data[0])) . ') VALUES'];
foreach ($data as $datum) {
$keys = array_values($datum);
$encode = [];
foreach ($keys as $val) {
if (is_string($val)) {
$encode[] = '\'' . addcslashes($val, "\\'") . '\'';
} else {
$encode[] = $val === '' ? '\'\'' : $val;
}
}
$strings[] = '(' . implode(',', $encode) . '),';
}
return rtrim(implode('', $strings), ',') . ';';
}
/**
* @param $key
* @param $percent
* @return string
* 组合成进度条
*/
public function buildLine($key, $percent): string
{
$repeatTimes = 100;
if ($percent > 100) $percent = 100;
if ($percent > 0) {
$hasColor = str_repeat('■', $percent);
} else {
$hasColor = '';
}
if ($repeatTimes - $percent > 0) {
$noColor = str_repeat(' ', $repeatTimes - $percent);
} else {
$noColor = '';
}
if ($percent !== 100) {
$percentString = sprintf("[ %s %-6s]", $key, $percent . '%');
} else {
$percentString = sprintf("[ %s %-5s]", $key, 'OK');;
}
return $percentString . "[{$hasColor}{$noColor}]" . "\r";
}
/**
* @param bool $clear
* @return void
* 输出进度条
*/
public function outputProgress(bool $clear = false): void
{
if ($clear) {
$number = count($this->percentStatus);
for ($i = 0; $i < $number; $i++) {
system("tput cuu1");
system("tput el");
}
}
foreach ($this->percentStatus as $key => $value) {
echo $this->buildLine($key, $value) . "\n";
}
}
/**
* @param $k
* @param $value
* @return void
* 更新进度条值
*/
public function updateProgressValue($k, $value): void
{
$this->percentStatus[$k] = $value;
if ($this->percentStatus[$k] >= 1000) {
$this->percentStatus[$k] = 1000;
$this->outputProgress(true);
return;
}
$this->outputProgress(true);
usleep(50000);
}
}
@@ -1,161 +1,167 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/9 0009
* Time: 9:44
*/
declare(strict_types=1);
namespace Database\Base;
use ArrayIterator;
use Database\ActiveQuery;
use Database\ModelInterface;
use Kiri\ToArray;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
use Traversable;
/**
* Class AbstractCollection
* @package Database\Base
*/
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, ToArray
{
/**
* @var ModelInterface[]
*/
protected array $_item = [];
protected ModelInterface|string|null $model;
protected ActiveQuery $query;
public function clean()
{
unset($this->query, $this->model, $this->_item);
}
/**
* Collection constructor.
*
* @param $query
* @param array $array
* @param ModelInterface|null $model
* @throws Exception
*/
public function __construct($query, array $array = [], ModelInterface $model = null)
{
$this->_item = $array;
$this->query = $query;
$this->model = $model;
parent::__construct([]);
}
/**
* @return int
*/
#[Pure] public function getLength(): int
{
return count($this->_item);
}
/**
* @param $item
*/
public function setItems($item)
{
$this->_item = $item;
}
/**
* @param $model
*/
public function setModel($model)
{
$this->model = $model;
}
/**
* @param $item
*/
public function addItem($item)
{
array_push($this->_item, $item);
}
/**
* @return Traversable|CollectionIterator|ArrayIterator
* @throws Exception
*/
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
{
return new CollectionIterator($this->model, $this->query, $this->_item);
}
/**
* @return mixed
* @throws Exception
*/
public function getModel(): ModelInterface
{
return $this->model;
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return !empty($this->_item) && isset($this->_item[$offset]);
}
/**
* @param mixed $offset
* @return ModelInterface|null
* @throws Exception
*/
public function offsetGet(mixed $offset): ?ModelInterface
{
if (!$this->offsetExists($offset)) {
return NULL;
}
if (!($this->_item[$offset] instanceof ModelInterface)) {
return $this->model->populates($this->_item[$offset]);
}
return $this->_item[$offset];
}
/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet(mixed $offset, mixed $value)
{
$this->_item[$offset] = $value;
}
/**
* @param mixed $offset
*/
public function offsetUnset(mixed $offset)
{
if ($this->offsetExists($offset)) {
unset($this->_item[$offset]);
}
}
}
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/9 0009
* Time: 9:44
*/
declare(strict_types=1);
namespace Database\Base;
use ArrayIterator;
use Database\ActiveQuery;
use Database\ModelInterface;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
use ReturnTypeWillChange;
use Traversable;
/**
* Class AbstractCollection
* @package Database\Base
*/
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, \Arrayable
{
/**
* @var ModelInterface[]
*/
private array $_item;
/**
* @return array
*/
public function getItems(): array
{
// TODO: Change the autogenerated stub
return $this->_item;
}
/**
* Collection constructor.
*
* @param ActiveQuery $query
* @param array $array
* @param ModelInterface|null $model
*/
public function __construct(public ActiveQuery $query, public ?ModelInterface $model = null, array $array = [])
{
$this->_item = $array;
parent::__construct();
}
/**
* @return int
*/
#[Pure] public function getLength(): int
{
return count($this->_item);
}
/**
* @param $item
*/
public function setItems($item): void
{
$this->_item = $item;
}
/**
* @param $model
*/
public function setModel($model): void
{
$this->model = $model;
}
/**
* @param $item
*/
public function addItem($item): void
{
$this->_item[] = $item;
}
/**
* @return Traversable|CollectionIterator|ArrayIterator
* @throws
*/
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
{
return new CollectionIterator($this->model, $this->_item);
}
/**
* @return mixed
* @throws
*/
public function getModel(): ModelInterface
{
return $this->model;
}
/**
* @return ActiveQuery
*/
public function makeNewQuery(): ActiveQuery
{
return $this->model::query();
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return !empty($this->_item) && isset($this->_item[$offset]);
}
/**
* @param mixed $offset
* @return ModelInterface|null
* @throws
*/
public function offsetGet(mixed $offset): ?ModelInterface
{
if (!$this->offsetExists($offset)) {
return NULL;
}
if (!($this->_item[$offset] instanceof ModelInterface)) {
return $this->model->populates($this->_item[$offset]);
}
return $this->_item[$offset];
}
/**
* @param mixed $offset
* @param mixed $value
*/
#[ReturnTypeWillChange]
public function offsetSet(mixed $offset, mixed $value): void
{
$this->_item[$offset] = $value;
}
/**
* @param mixed $offset
*/
#[ReturnTypeWillChange]
public function offsetUnset(mixed $offset): void
{
if ($this->offsetExists($offset)) {
unset($this->_item[$offset]);
}
}
}
+270
View File
@@ -0,0 +1,270 @@
<?php
namespace Database\Base;
use Closure;
use Database\Collection;
use Database\Traits\QueryTrait;
interface ActiveQueryInterface
{
/**
* @param string $column
* @param callable $callable
* @return QueryTrait
*/
public function when(string $column, callable $callable): QueryTrait;
/**
* @param string $whereRaw
* @return QueryTrait
*/
public function whereRaw(string $whereRaw): QueryTrait;
/**
* @param string $column
* @param string $value
* @return QueryTrait
*/
public function whereLocate(string $column, string $value): QueryTrait;
/**
* @param string $column
* @return QueryTrait
*/
public function whereNull(string $column): QueryTrait;
/**
* @param string $column
* @return QueryTrait
*/
public function whereEmpty(string $column): QueryTrait;
/**
* @param string $column
* @return QueryTrait
*/
public function whereNotEmpty(string $column): QueryTrait;
/**
* @param string $column
* @return QueryTrait
*/
public function whereNotNull(string $column): QueryTrait;
/**
* @param string $alias
*
* @return QueryTrait select * from tableName as t1
*
* select * from tableName as t1
*/
public function alias(string $alias = 't1'): QueryTrait;
/**
* @param string|Closure $tableName
*
* @return QueryTrait
*/
public function from(string|Closure $tableName): QueryTrait;
/**
* @param string $tableName
* @param string $alias
* @param array $onCondition
* @param array $param
* @return QueryTrait
*/
public function leftJoin(string $tableName, string $alias, array $onCondition, array $param = []): QueryTrait;
/**
* @param string $tableName
* @param string $alias
* @param array $onCondition
* @param array $param
* @return QueryTrait
*/
public function rightJoin(string $tableName, string $alias, array $onCondition, array $param = []): QueryTrait;
/**
* @param string $tableName
* @param string $alias
* @param array $onCondition
* @param array $param
* @return QueryTrait
*/
public function innerJoin(string $tableName, string $alias, array $onCondition, array $param = []): QueryTrait;
/**
* @param string $field
*
* @return QueryTrait
*/
public function sum(string $field): QueryTrait;
/**
* @param string $field
* @return QueryTrait
*/
public function max(string $field): QueryTrait;
/**
* @param string $lngField
* @param string $latField
* @param int|float $lng1
* @param int|float $lat1
*
* @return QueryTrait
*/
public function distance(string $lngField, string $latField, int|float $lng1, int|float $lat1): QueryTrait;
/**
* @param string|array $column
* @param string $sort
*
* @return QueryTrait [
*
* [
* 'addTime',
* 'descTime desc'
* ]
*/
public function orderBy(string|array $column, string $sort = 'DESC'): QueryTrait;
/**
* @param array $column
*
* @return QueryTrait
*/
public function select(array $column = ['*']): QueryTrait;
/**
* @return QueryTrait
*/
public function rand(): QueryTrait;
/**
* @param array|Closure|string $conditionArray
* @param string $opera
* @param null $value
* @return QueryTrait
*/
public function whereOr(array|Closure|string $conditionArray = [], string $opera = '=', $value = null): QueryTrait;
/**
* @param string $column
* @param string $value
* @return QueryTrait
*/
public function whereLike(string $column, string $value): QueryTrait;
/**
* @param string $column
* @param string $value
* @return QueryTrait
*/
public function whereLeftLike(string $column, string $value): QueryTrait;
/**
* @param string $column
* @param string $value
* @return QueryTrait
*/
public function whereRightLike(string $column, string $value): QueryTrait;
/**
* @param string $column
* @param string $value
* @return QueryTrait
*/
public function whereNotLike(string $column, string $value): QueryTrait;
/**
* @param string $columns
* @param array|Closure $value
* @return QueryTrait
*/
public function whereIn(string $columns, array|Closure $value): QueryTrait;
/**
* @param string $columns
* @param array $value
* @return QueryTrait
*/
public function whereNotIn(string $columns, array $value): QueryTrait;
/**
* @param string $column
* @param int|float $start
* @param int|float $end
* @return QueryTrait
*/
public function whereBetween(string $column, int|float $start, int|float $end): QueryTrait;
/**
* @param string $column
* @param int|float $start
* @param int|float $end
* @return QueryTrait
*/
public function whereNotBetween(string $column, int|float $start, int|float $end): QueryTrait;
/**
* @param array|string $column
* @param mixed|null $value
* @return QueryTrait
*/
public function where(array|string $column, mixed $value = null): QueryTrait;
/**
* @param string $column
* @param string $opera
* @param mixed $value
* @return QueryTrait
*/
public function whereMath(string $column, string $opera, mixed $value): QueryTrait;
/**
* @param Closure $closure
* @return QueryTrait
*/
public function whereClosure(Closure $closure): QueryTrait;
/**
* @param string $name
* @param string|null $having
*
* @return QueryTrait
*/
public function groupBy(string $name, ?string $having = NULL): QueryTrait;
/**
* @param int $limit
*
* @return QueryTrait
*/
public function limit(int $limit = 20): QueryTrait;
/**
* @param int $offset
* @return QueryTrait
*/
public function offset(int $offset): QueryTrait;
}
+62
View File
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Database\Base;
use Database\ModelInterface;
/**
* Class CollectionIterator
* @package Database\Base
*/
class CollectionIterator extends \ArrayIterator
{
/**
* @var ModelInterface|string
*/
private ModelInterface|string $model;
/**
* CollectionIterator constructor.
* @param $model
* @param array $array
* @param int $flags
* @throws
*/
public function __construct($model, array $array = [], int $flags = 0)
{
$this->model = $model;
parent::__construct($array, $flags);
}
/**
* @param $current
* @return ModelInterface
* @throws
*/
protected function newModel($current): ModelInterface
{
return $this->model->populates($current);
}
/**
* @throws
*/
public function current(): ModelInterface
{
if (is_array($current = parent::current())) {
$current = $this->newModel($current);
}
return $current;
}
}
+909
View File
@@ -0,0 +1,909 @@
<?php
/** @noinspection ALL */
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:39
*/
declare(strict_types=1);
namespace Database\Base;
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
use ArrayAccess;
use Database\ActiveQuery;
use Database\Collection;
use Database\Connection;
use Database\DatabasesProviders;
use Database\ModelInterface;
use Database\Relation;
use Database\SqlBuilder;
use Exception;
use Kiri\Di\Context;
use Kiri;
use Kiri\Abstracts\Component;
use ReturnTypeWillChange;
use ReflectionException;
use validator\Validator;
enum Driver
{
case Mysql;
case Pgsql;
}
/**
* Class BOrm
*
* @package Kiri\Abstracts
*
* @property bool $isNowExample
* @property array $attributes
* @property array $oldAttributes
*/
abstract class Model extends Component implements ModelInterface, ArrayAccess, \Arrayable
{
/** @var array */
protected array $_attributes = [];
/** @var array */
protected array $_oldAttributes = [];
/** @var null|string */
protected string $primary = '';
/**
* @var bool
*/
protected bool $isNewExample = TRUE;
/**
* @var bool
*/
protected bool $skipValidate = FALSE;
/**
* @var string
*/
protected string $table = '';
/**
* @var string
*/
protected string $connection = 'db';
/**
* @var array
*/
protected array $_with = [];
/**
* @throws Exception
*/
public function __construct()
{
parent::__construct();
$this->init();
}
/**
* @return array
*/
public function rules(): array
{
return [];
}
/**
* @param array $data
*
* @return Model
*/
public function setWith(array $data): static
{
$this->_with = $data;
return $this;
}
/**
* @return array
*/
public function getWith(): array
{
return $this->_with;
}
/**
* @return bool
*/
public function hasWith(): bool
{
return count($this->_with) > 0;
}
/**
* object init
*/
public function clean(): void
{
$this->_attributes = [];
$this->_oldAttributes = [];
}
/**
* @throws Exception
*/
public function init(): void
{
$container = Kiri::getDi();
$container->resolveProperties($container->getReflectionClass(get_called_class()), $this);
}
/**
* @return bool
*/
public function getIsNowExample(): bool
{
return $this->isNewExample === TRUE;
}
/**
* @param bool $bool
*
* @return $this
*/
public function setIsNowExample(bool $bool = FALSE): static
{
$this->isNewExample = $bool;
return $this;
}
/**
* @return string
* @throws
* get last exception or other error
*/
public function getLastError(): string
{
return $this->getLogger()->getLastError('mysql');
}
/**
* @return bool
* @throws
*/
public function hasPrimary(): bool
{
return $this->primary != '';
}
/**
* @return null|string
* @throws
*/
public function getPrimary(): string
{
if (!$this->hasPrimary()) {
return '';
}
return $this->primary;
}
/**
* @return bool
* @throws
*/
public function hasPrimaryValue(): bool
{
if ($this->hasPrimary()) {
return $this->getPrimaryValue() === NULL;
}
return FALSE;
}
/**
* @return int|null
* @throws
*/
public function getPrimaryValue(): ?int
{
if ($this->hasPrimary() && isset($this->_oldAttributes[$this->getPrimary()])) {
return (int)$this->_oldAttributes[$this->getPrimary()];
} else {
return NULL;
}
}
/**
* @param int|string|array $param
*
* @return Model|null
* @throws
*/
public static function findOne(int|string|array|null $param): ?static
{
if (empty($param)) {
return NULL;
}
$query = new ActiveQuery($model = static::instance());
$query->from($model->getTable())->alias('t1');
if (is_numeric($param)) {
$query->where([$model->getPrimary() => +$param]);
} else if (is_array($param)) {
$query->where($param);
} else {
$query->whereRaw($param);
}
if (($data = $query->first()) === FALSE) {
throw new Exception($model->getLastError());
} else {
return $data;
}
}
/**
* @param int $param
*
* @return Model|null
* @throws
*/
public static function primary(int $param): ?static
{
$model = static::instance();
$query = new ActiveQuery($model);
$query->from($model->getTable())->alias('t1')->where([$model->getPrimary() => $param]);
return $query->first();
}
/**
* @return bool|int
* @throws Exception
*/
public function optimize(): bool|int
{
return static::query()->execute('OPTIMIZE TABLE ' . $this->getTable());
}
/**
* @return static
*/
protected static function instance(): static
{
return new static;
}
/**
* @param int|string|array $condition
*
* @return static|null
* @throws
*/
public static function first(int|string|array $condition): ?static
{
return static::findOne($condition);
}
/**
* @param string|array $condition
*
* @return Collection
* @throws
*/
public static function all(string|array $condition): Collection
{
$model = new ActiveQuery(static::instance());
$model->from($model->getTable())->alias('t1');
if (is_array($condition)) {
$model->where($condition);
} else {
$model->whereRaw($condition);
}
return $model->get();
}
/**
* @return ActiveQuery
* @throws
*/
public static function query(): ActiveQuery
{
$model = new ActiveQuery(static::instance());
$model->select()->from($model->getTable())->alias('t1');
return $model;
}
/**
* @return Connection
* @throws
*/
public function getConnection(): Connection
{
return Kiri::getDi()->get(DatabasesProviders::class)->get($this->connection);
}
/**
* @param array|string $condition
* @param array $attributes
*
* @return bool
*/
protected static function deleteByCondition(array|string $condition = [], array $attributes = []): bool
{
$model = static::query();
$model->bindParams($attributes);
if (is_string($condition)) {
$model->whereRaw($condition);
} else {
$model->where($condition);
}
return $model->delete();
}
/**
* @return array
* @throws
*/
public function getAttributes(): array
{
return $this->_attributes;
}
/**
* @return array
*/
public function getOldAttributes(): array
{
return $this->_oldAttributes;
}
/**
* @param string $name
* @param mixed $value
*
* @return mixed
*/
public function setAttribute(string $name, mixed $value): mixed
{
$method = 'set' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) {
$value = $this->{$method}($value);
}
return $this->_attributes[$name] = $value;
}
/**
* @param string $name
* @param mixed $value
*
* @return mixed
*/
public function setOldAttribute(string $name, mixed $value): mixed
{
$method = 'set' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) {
$value = $this->{$method}($value);
}
return $this->_oldAttributes[$name] = $value;
}
/**
* @param array $param
*
* @return $this
* @throws
*/
public function setAttributes(array $param): static
{
foreach ($param as $key => $attribute) {
$this->setAttribute($key, $attribute);
}
return $this;
}
/**
* @param array $param
*
* @return $this
*/
public function setOldAttributes(array $param): static
{
foreach ($param as $key => $attribute) {
$this->setOldAttribute($key, $attribute);
}
return $this;
}
/**
* @return $this|bool
* @throws
*/
private function insert(): bool|static
{
$sql = SqlBuilder::builder($query = static::query())->insert($this->_attributes);
$lastId = $this->getConnection()->createCommand($sql, $query->params)->exec();
if ($lastId === FALSE) {
return FALSE;
}
if (!$this->hasPrimary()) {
return $this->refresh()->afterSave($this->_attributes, []);
}
$this->_attributes[$this->getPrimary()] = $lastId;
return $this->refresh()->afterSave($this->_attributes, [$this->getPrimary() => $lastId]);
}
/**
* @param array $old
* @param array $condition
* @param array $change
*
* @return $this|bool
* @throws
*/
protected function updateInternal(array $old, array $condition, array $change): bool|static
{
$query = static::query()->where($condition);
if (count($change) < 1) {
return TRUE;
}
$generate = SqlBuilder::builder($query)->update($change);
if ($generate === FALSE) {
return FALSE;
}
if (!$this->getConnection()->createCommand($generate, $query->params)->exec()) {
return FALSE;
}
return $this->refresh()->afterSave($old, $change);
}
/**
* @return bool|$this
* @throws
*/
public function save(): static|bool
{
if (!$this->validator($this->rules(), $this->_attributes) || !$this->beforeSave($this)) {
return FALSE;
}
if (!$this->isNewExample) {
return $this->updateInternal(...$this->arrayIntersect($this->_attributes));
} else {
return $this->insert();
}
}
/**
* @return array<array, array, array>
* @throws
*/
protected function arrayIntersect(array $params): array
{
$condition = [];
$oldPrams = [];
foreach ($this->_oldAttributes as $key => $attribute) {
if (!array_key_exists($key, $params) || $params[$key] == $attribute) {
$condition[$key] = $attribute;
unset($params[$key]);
} else {
$oldPrams[$key] = $this->_oldAttributes[$key];
}
}
if ($this->hasPrimary()) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
}
return [$oldPrams, $condition, $params];
}
/**
* @return array
*/
public function getChanges(): array
{
if (!$this->isNewExample) {
return \array_intersect_assoc($this->_oldAttributes, $this->_attributes);
}
return $this->_attributes;
}
/**
* @param array $value
*
* @return $this
*/
public function populates(array $value): static
{
$this->_attributes = $value;
$this->_oldAttributes = $value;
$this->setIsNowExample();
return $this;
}
/**
* @param array $rule
*
* @return bool
* @throws
*/
public function validator(array $rule, array $params): bool
{
if (count($rule) < 1 || $this->skipValidate) {
return TRUE;
}
$validate = $this->resolve($rule);
if (!$validate->validation($params)) {
return \Kiri::getLogger()->logCategory($validate->getError() . PHP_EOL, 'mysql');
} else {
return TRUE;
}
}
/**
* @param array $rule
*
* @return Validator
* @throws
*/
private function resolve(array $rule): Validator
{
$validate = new Validator;
foreach ($rule as $val) {
$field = array_shift($val);
if (is_string($field)) {
$validate->make($this, [$field], $val);
} else {
$validate->make($this, $field, $val);
}
}
return $validate;
}
/**
* @param string $name
*
* @return null
* @throws
*/
public function getAttribute(string $name): mixed
{
return $this->_attributes[$name] ?? NULL;
}
/**
* @return Relation|null
* @throws
*/
public function getRelation(): ?Relation
{
return di(Relation::class);
}
/**
* @param string $attribute
*
* @return bool
* @throws
*/
public function has(string $attribute): bool
{
return TRUE;
}
/**ƒ
* @return string
* @throws
*/
public function getTable(): string
{
$connection = $this->getConnection();
$tablePrefix = $connection->tablePrefix;
if (empty($this->table)) {
throw new Exception('You need add static method `tableName` and return table name.');
}
$table = trim($this->table, '{%}');
if (!empty($tablePrefix) && !str_starts_with($table, $tablePrefix)) {
$table = $tablePrefix . $table;
}
$driver = strtolower($connection->driver ?? 'mysql');
if (in_array($driver, ['pgsql', 'postgresql'])) {
// PostgreSQL 使用双引号,并且 schema.table 格式
return '"' . $connection->database . '"."' . $table . '"';
}
// MySQL 使用反引号
return '`' . $connection->database . '`.' . $table;
}
/**
* @param array $oldAttributes
* @param array $changeAttributes
*
* @return bool
*/
public function afterSave(array $oldAttributes, array $changeAttributes): bool
{
return TRUE;
}
/**
* @param self $model
*
* @return bool
* @throws
*/
public function beforeSave(self $model): bool
{
return TRUE;
}
/**
* @return static
*/
public function refresh(): static
{
$this->_oldAttributes = $this->_attributes;
$this->isNewExample = FALSE;
return $this;
}
/**
* @param string $name
* @param mixed $value
*
* @return void
*/
public function __set(string $name, mixed $value): void
{
$prefix = 'set' . ucfirst($name);
if (method_exists($this, $prefix)) {
$this->{$prefix}($value);
return;
}
$method = $prefix . 'Attribute';
if (method_exists($this, $method)) {
$this->_attributes[$name] = $this->{$method}($value);
} else {
$this->_attributes[$name] = $value;
}
}
/**
* @param string $name
*
* @return mixed
*/
public function __get(string $name): mixed
{
if (isset($this->_attributes[$name])) {
return $this->withPropertyOverride($name, $this->_attributes[$name]);
}
if (isset($this->_oldAttributes[$name])) {
return $this->withPropertyOverride($name, $this->_oldAttributes[$name]);
}
if (!method_exists($this, 'get' . ucfirst($name))) {
return NULL;
}
return $this->{'get' . ucfirst($name)}();
}
/**
* @param string $name
* @param mixed|null $value
*
* @return mixed
*/
protected function withPropertyOverride(string $name, mixed $value = NULL): mixed
{
$method = 'get' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) {
return $this->{$method}($value);
} else {
return $value;
}
}
/**
* @param string $name
* @param string $prefix
*
* @return bool
*/
protected function hasRelateMethod(string $name): bool
{
$reflection = $this->container->get(static::class);
if (!$reflection->hasMethod($name)) {
return FALSE;
}
if ($reflection->getMethod($name)->isStatic() || !$reflection->getMethod($name)->isPublic()) {
return FALSE;
} else {
return TRUE;
}
}
/**
* @param string $name
*
* @return mixed
*/
protected function withRelate(string $name): mixed
{
$response = $this->{'get' . ucfirst($name)}();
if ($response instanceof \Database\Traits\Relation) {
$response = $response->get();
}
return $response;
}
/**
* @param $name
*
* @return bool
*/
public function __isset(string $name): bool
{
return isset($this->_attributes[$name]);
}
/**
* @param mixed $offset
*
* @return bool
* @throws
*/
public function offsetExists(mixed $offset): bool
{
return isset($this->_attributes[$offset]) || isset($this->_oldAttributes[$offset]);
}
/**
* @param mixed $offset
*
* @return mixed
* @throws
*/
public function offsetGet(mixed $offset): mixed
{
return $this->__get($offset);
}
/**
* @param mixed $offset
* @param mixed $value
*
* @throws
*/
#[ReturnTypeWillChange]
public function offsetSet(mixed $offset, mixed $value): void
{
$this->__set($offset, $value);
}
/**
* @param mixed $offset
*
* @throws
*/
#[ReturnTypeWillChange]
public function offsetUnset(mixed $offset): void
{
if (!isset($this->_attributes[$offset]) && !isset($this->_oldAttributes[$offset])) {
return;
}
unset($this->_attributes[$offset]);
unset($this->_oldAttributes[$offset]);
}
/**
* @param string ...$params
*
* @return array
*/
public function unset(string ...$params): array
{
return array_diff_assoc($params, $this->_attributes);
}
/**
* @param array $data
*
* @return static
* @throws
*/
public static function populate(array $data): static
{
$model = new static;
$model->_attributes = $data;
$model->_oldAttributes = $data;
$model->setIsNowExample();
return $model;
}
/**
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public static function __callStatic(string $name, array $arguments)
{
return (new static)->{$name}(...$arguments);
}
/**
* @param string $field
*
* @return array
*/
public function getOldAttribute(string $field): mixed
{
return $this->_oldAttributes[$field] ?? NULL;
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace Database\Base;
class PDO extends \PDO
{
readonly public string $driver;
/**
* @param string $database
* @param string $host
* @param string $username
* @param string $password
* @param array $options
* @param string $driver
*/
public function __construct(
readonly public string $database,
readonly public string $host,
readonly public string $username,
readonly public string $password,
readonly public array $options,
string $driver = 'mysql',
)
{
$this->driver = strtolower($driver);
$dsn = $this->buildDsn();
parent::__construct($dsn, $this->username, $this->password, $this->options);
}
/**
* @return string
*/
private function buildDsn(): string
{
return match ($this->driver) {
'pgsql', 'postgresql' => 'pgsql:host=' . $this->host . ';dbname=' . $this->database,
default => 'mysql:dbname=' . $this->database . ';host=' . $this->host,
};
}
}
+234
View File
@@ -0,0 +1,234 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 13:38
*/
declare(strict_types=1);
namespace Database;
use Database\Base\AbstractCollection;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class Collection
* @package Database
* @property-read $length
*/
class Collection extends AbstractCollection
{
/**
* @param string $field
*
* @return array|null
*/
public function values(string $field): ?array
{
return array_values($this->column($field));
}
/**
* @param array $attributes
* @return bool
* @throws
*/
public function update(array $attributes): bool
{
if ($this->isEmpty()) {
return $this->getLogger()->logCategory('No data by update', 'mysql');
}
return $this->batch()->update($attributes);
}
/**
* @param string $field
* @return array|null
*/
public function keyBy(string $field): ?array
{
$array = $this->toArray();
$column = array_flip(array_column($array, $field));
foreach ($column as $key => $value) {
$column[$key] = $array[$value];
}
return $column;
}
/**
* @param int $start
* @param int $length
*
* @return array
*/
#[Pure] public function slice(int $start = 0, int $length = 20): array
{
if (empty($this->getItems())) {
return [];
}
if (\count($this->getItems()) < $length) {
return $this->getItems();
} else {
return array_slice($this->getItems(), $start, $length);
}
}
/**
* @param string $field
* @param string $setKey
*
* @return array|null
*/
public function column(string $field, string $setKey = ''): ?array
{
$data = $this->toArray();
if (empty($data)) {
return [];
}
if (!empty($setKey) && is_string($setKey)) {
return array_column($data, $field, $setKey);
} else {
return array_column($data, $field);
}
}
/**
* @param string $field
*
* @return float|int|null
*/
public function sum(string $field): float|int|null
{
$array = $this->column($field);
if (empty($array)) {
return NULL;
}
return array_sum($array);
}
/**
* @return ModelInterface|array
*/
#[Pure] public function current(): ModelInterface|array
{
return current($this->getItems());
}
/**
* @return int
*/
#[Pure] public function size(): int
{
return count($this->getItems());
}
/**
* @return array
* @throws
*/
public function toArray(): array
{
$array = [];
/** @var Model $value */
foreach ($this as $value) {
$array[] = $value->toArray();
}
return $array;
}
/**
* @throws
* 批量删除
*/
public function delete(): bool
{
$model = $this->getModel();
if ($this->isEmpty()) {
return $this->getLogger()->logCategory('No data by delete', 'mysql');
}
if (!$model->hasPrimary()) {
throw new Exception('Must set primary key. if you want to delete data');
}
return $this->batch()->delete();
}
/**
* @return ActiveQuery
* @throws Exception
*/
private function batch(): ActiveQuery
{
return $this->makeNewQuery()->whereIn($this->getModel()->getPrimary(),
$this->column($this->getModel()->getPrimary()));
}
/**
* @param array $condition
* @return Collection
* @throws
*/
public function filter(array $condition): Collection|static
{
$_filters = [];
if (empty($condition)) {
return $this;
}
foreach ($this as $value) {
if (!$this->filterCheck($value, $condition)) {
continue;
}
$_filters[] = $value;
}
return new Collection($this->query, $this->model, $_filters);
}
/**
* @param array|ModelInterface $value
* @param array $condition
* @return bool
*/
private function filterCheck(array|ModelInterface $value, array $condition): bool
{
$_value = $value;
if ($_value instanceof ModelInterface) {
$_value = $_value->toArray();
}
$_tmp = array_intersect_key($_value, $condition);
if (count(array_diff_assoc($_tmp, $condition)) > 0) {
return false;
}
return true;
}
/**
* @param string $key
* @param mixed $value
* @return mixed
*/
public function exists(string $key, mixed $value): mixed
{
foreach ($this as $item) {
if ($item->$key === $value) {
return $item;
}
}
return null;
}
/**
* @return bool
*/
#[Pure] public function isEmpty(): bool
{
return $this->size() < 1;
}
}
+260
View File
@@ -0,0 +1,260 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 15:23
*/
declare(strict_types=1);
namespace Database;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Di\Container;
use PDO;
use Throwable;
/**
* Class Command
* @package Database
*/
class Command extends Component
{
public Connection $connection;
public ?string $sql = '';
public array $params = [];
/**
* @param array $params
* @throws
*/
public function __construct(array $params = [])
{
parent::__construct();
Container::configure($this, $params);
}
/**
* @return bool
* @throws
*/
public function incrOrDecr(): bool
{
return (bool)$this->_prepare();
}
/**
* @return bool|array
* @throws
*/
public function all(): bool|array
{
return $this->search('fetchAll');
}
/**
* @return array|bool|null
* @throws
*/
public function one(): array|null|bool
{
return $this->search('fetch');
}
/**
* @return bool
* @throws Exception
*/
public function exists(): bool
{
$data = $this->search('fetch');
if (!$data) {
return false;
}
return true;
}
/**
* @return mixed
* @throws
*/
public function fetchColumn(): mixed
{
return $this->search('fetchColumn');
}
/**
* @return mixed
* @throws
*/
public function rowCount(): int
{
$data = $this->search('fetch');
return !$data ? 0 : +current($data);
}
/**
* @param string $method
* @return mixed
* @throws
*/
protected function search(string $method): mixed
{
$client = $this->connection->getConnection();
try {
$startTime = microtime(true);
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
}
$prepare->execute($this->params);
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
$prepare->closeCursor();
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
return $result;
} catch (Throwable $throwable) {
$this->getLogger()->json_log($throwable);
if ($this->isRefresh($throwable)) {
return $this->search($method);
}
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
} finally {
$this->connection->release($client);
}
}
/**
* @return bool
* @throws
*/
public function flush(): bool
{
return (bool)$this->_prepare();
}
/**
* @return int|bool
* @throws
*/
private function _prepare(): int|bool
{
$client = $this->connection->getConnection();
try {
$startTime = microtime(true);
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
}
if ($prepare->execute($this->params) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
}
$prepare->closeCursor();
$result = $client->lastInsertId();
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
if (str_starts_with($this->sql, 'DELETE')) {
return $prepare->rowCount();
}
return $result == 0 ? $prepare->rowCount() : (int)$result;
} catch (Throwable $throwable) {
$this->getLogger()->json_log($throwable);
if ($this->isRefresh($throwable)) {
return $this->_prepare();
}
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
} finally {
$this->connection->release($client);
}
}
/**
* @param Throwable $throwable
* @return bool
*/
protected function isRefresh(Throwable $throwable): bool
{
$message = $throwable->getMessage();
// MySQL 错误处理
if (str_contains($message, 'MySQL server has gone away')) {
return true;
}
if (str_contains($message, 'Send of 14 bytes failed with errno=32 Broken pipe')) {
return true;
}
if (str_contains($message, 'Lost connection to MySQL server during query')) {
return true;
}
// PostgreSQL 错误处理
if (str_contains($message, 'server closed the connection unexpectedly')) {
return true;
}
if (str_contains($message, 'Connection refused')) {
return true;
}
if (str_contains($message, 'Broken pipe')) {
return true;
}
if (str_contains($message, 'connection to server was lost')) {
return true;
}
return false;
}
/**
* @return bool
* @throws
*/
public function delete(): bool
{
return $this->_prepare();
}
/**
* @return int|bool
*/
public function exec(): int|bool
{
return $this->_prepare();
}
/**
* @param array $data
* @return $this
*/
public function bindValues(array $data = []): static
{
if (count($data) > 0) {
$this->params = array_merge($this->params, $data);
}
return $this;
}
}
@@ -1,23 +1,23 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class BetweenCondition
* @package Database\Condition
*/
class BetweenCondition extends Condition
{
/**
* @return string
*/
public function builder(): string
{
return $this->column . ' BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class BetweenCondition
* @package Database\Condition
*/
class BetweenCondition extends Condition
{
/**
* @return string
*/
public function builder(): string
{
return $this->column . ' BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
}
}
@@ -1,22 +1,22 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class ChildCondition
* @package Database\Condition
*/
class ChildCondition extends Condition
{
/**
* @return string
*/
public function builder(): string
{
return $this->column . ' ' . $this->opera . ' (' . $this->value . ')';
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class ChildCondition
* @package Database\Condition
*/
class ChildCondition extends Condition
{
/**
* @return string
*/
public function builder(): string
{
return $this->column . ' ' . $this->opera . ' (' . $this->value . ')';
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
/**
* Class Condition
* @package Database\Condition
*/
abstract class Condition extends Component
{
protected string $column = '';
protected string $opera = '=';
protected mixed $value;
protected array $attributes = [];
abstract public function builder();
/**
* @param string $column
*/
public function setColumn(string $column): void
{
$this->column = $column;
}
/**
* @param string $opera
*/
public function setOpera(string $opera): void
{
$this->opera = $opera;
}
/**
* @param $params
*/
public function setValue($params): void
{
if (is_array($params)) {
$values = array_map(function ($value) {
return is_numeric($value) ? $value : '\'' . $value . '\'';
}, $params);
$this->value = $values;
} else {
$this->value = $this->checkIsSqlString($params);
}
}
/**
* @param $params
* @return int|string
*/
#[Pure] private function checkIsSqlString($params): int|string
{
if (is_numeric($params)) {
return $params;
}
$check = ltrim($params, '(');
$check = strtolower(substr($check, 0, 6));
if (in_array($check, ['update', 'select', 'insert', 'delete'])) {
return $params;
} else {
return sprintf('\'%s\'', $params);
}
}
}
@@ -1,24 +1,24 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class DefaultCondition
* @package Database\Condition
*/
class DefaultCondition extends Condition
{
/**
* @return string
*/
#[Pure] public function builder(): string
{
return sprintf('%s %s %s', $this->column, $this->opera, addslashes($this->value));
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class DefaultCondition
* @package Database\Condition
*/
class DefaultCondition extends Condition
{
/**
* @return string
*/
#[Pure] public function builder(): string
{
return $this->column . ' ' . $this->opera . ' ' . addslashes($this->value);
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class HashCondition
* @package Yoc\db\condition
*/
class HashCondition extends Condition
{
/**
* @return string
* @throws
*/
public function builder(): string
{
$array = [];
if (count($this->value) < 1) {
throw new \Exception('Builder data by a empty array.');
}
foreach ($this->value as $key => $value) {
$array[] = $key . '=' . addslashes($value);
}
return implode(' AND ', $array);
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class InCondition
* @package Database\Condition
*/
class InCondition extends Condition
{
/**
* @return string
* @throws
*/
#[Pure] public function builder(): string
{
if (is_array($this->value)) {
return $this->column . ' IN (' . implode(',', $this->value) . ')';
} else {
return $this->column . ' IN (' . $this->value . ')';
}
}
}
@@ -1,20 +1,24 @@
<?php
namespace Database\Condition;
/**
* Class JsonCondition
* @package Database\Condition
*/
class JsonCondition extends Condition
{
public function builder()
{
// TODO: Implement builder() method.
}
}
<?php
namespace Database\Condition;
/**
* Class JsonCondition
* @package Database\Condition
*/
class JsonCondition extends Condition
{
/**
* @return bool
*/
public function builder(): bool
{
// TODO: Implement builder() method.
return \json_validate($this->value);
}
}
@@ -1,28 +1,26 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use Kiri\Core\Str;
/**
* Class LLikeCondition
* @package Database\Condition
*/
class LLikeCondition extends Condition
{
public string $pos = '';
/**
* @return string
*/
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
return $this->column . ' LIKE \'%' . addslashes($this->value) . '\'';
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class LLikeCondition
* @package Database\Condition
*/
class LLikeCondition extends Condition
{
public string $pos = '';
/**
* @return string
*/
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
return $this->column . ' LIKE \'%' . addslashes($this->value) . '\'';
}
}
@@ -1,28 +1,26 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use Kiri\Core\Str;
/**
* Class LikeCondition
* @package Database\Condition
*/
class LikeCondition extends Condition
{
public string $pos = '';
/**
* @return string
*/
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
return $this->column . ' LIKE \'%' . addslashes($this->value) . '%\'';
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class LikeCondition
* @package Database\Condition
*/
class LikeCondition extends Condition
{
public string $pos = '';
/**
* @return string
*/
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
return $this->column . ' LIKE \'%' . addslashes($this->value) . '%\'';
}
}
@@ -1,78 +1,78 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class MathematicsCondition
* @package Database\Condition
*/
class MathematicsCondition extends Condition
{
public string $type = '';
/**
* @return mixed
*/
public function builder(): mixed
{
return $this->{strtolower($this->type)}((float)$this->value);
}
/**
* @param $value
* @return string
*/
public function eq($value): string
{
return $this->column . ' = ' . $value;
}
/**
* @param $value
* @return string
*/
public function neq($value): string
{
return $this->column . ' <> ' . $value;
}
/**
* @param $value
* @return string
*/
public function gt($value): string
{
return $this->column . ' > ' . $value;
}
/**
* @param $value
* @return string
*/
public function egt($value): string
{
return $this->column . ' >= ' . $value;
}
/**
* @param $value
* @return string
*/
public function lt($value): string
{
return $this->column . ' < ' . $value;
}
/**
* @param $value
* @return string
*/
public function elt($value): string
{
return $this->column . ' <= ' . $value;
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class MathematicsCondition
* @package Database\Condition
*/
class MathematicsCondition extends Condition
{
public string $type = '';
/**
* @return mixed
*/
public function builder(): mixed
{
return $this->{strtolower($this->type)}((float)$this->value);
}
/**
* @param $value
* @return string
*/
public function eq($value): string
{
return $this->column . ' = ' . $value;
}
/**
* @param $value
* @return string
*/
public function neq($value): string
{
return $this->column . ' <> ' . $value;
}
/**
* @param $value
* @return string
*/
public function gt($value): string
{
return $this->column . ' > ' . $value;
}
/**
* @param $value
* @return string
*/
public function egt($value): string
{
return $this->column . ' >= ' . $value;
}
/**
* @param $value
* @return string
*/
public function lt($value): string
{
return $this->column . ' < ' . $value;
}
/**
* @param $value
* @return string
*/
public function elt($value): string
{
return $this->column . ' <= ' . $value;
}
}
@@ -1,22 +1,22 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class NotBetweenCondition
* @package Database\Condition
*/
class NotBetweenCondition extends Condition
{
/**
* @return string
*/
public function builder(): string
{
return $this->column . ' NOT BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class NotBetweenCondition
* @package Database\Condition
*/
class NotBetweenCondition extends Condition
{
/**
* @return string
*/
public function builder(): string
{
return $this->column . ' NOT BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class NotInCondition
* @package Database\Condition
*/
class NotInCondition extends Condition
{
/**
* @return string|null
* @throws
*/
public function builder(): ?string
{
if (!is_array($this->value)) {
throw new \Exception('Builder data by a empty string. need array');
}
$value = '\'' . implode('\',\'', $this->value) . '\'';
return '`' . $this->column . '` not in(' . $value . ')';
}
}
@@ -1,28 +1,26 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use Kiri\Core\Str;
/**
* Class NotLikeCondition
* @package Database\Condition
*/
class NotLikeCondition extends Condition
{
public string $pos = '';
/**
* @return string
*/
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
return $this->column . ' NOT LIKE \'%' . addslashes($this->value) . '%\'';
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class NotLikeCondition
* @package Database\Condition
*/
class NotLikeCondition extends Condition
{
public string $pos = '';
/**
* @return string
*/
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
return $this->column . ' NOT LIKE \'%' . addslashes($this->value) . '%\'';
}
}
@@ -1,27 +1,27 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class OrCondition
* @package Database\Condition
*/
class OrCondition extends Condition
{
public array $oldParams = [];
/**
* @return string
*/
#[Pure] public function builder(): string
{
return sprintf('(%s) OR %s', implode(' AND ', $this->oldParams), addslashes($this->value));
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class OrCondition
* @package Database\Condition
*/
class OrCondition extends Condition
{
public array $oldParams = [];
/**
* @return string
*/
#[Pure] public function builder(): string
{
return '(' . implode(' AND ', $this->oldParams) . ') OR ' . addslashes($this->value);
}
}
@@ -1,28 +1,26 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use Kiri\Core\Str;
/**
* Class RLikeCondition
* @package Database\Condition
*/
class RLikeCondition extends Condition
{
public string $pos = '';
/**
* @return string
*/
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
return sprintf('%s LIKE \'%s\'', $this->column, addslashes($this->value));
}
}
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class RLikeCondition
* @package Database\Condition
*/
class RLikeCondition extends Condition
{
public string $pos = '';
/**
* @return string
*/
public function builder(): string
{
if (!is_string($this->value)) {
$this->value = array_shift($this->value);
}
return sprintf('%s LIKE \'%s\'', $this->column, addslashes($this->value));
}
}
+394
View File
@@ -0,0 +1,394 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:09
*/
declare(strict_types=1);
namespace Database;
use Database\Affair\BeginTransaction;
use Database\Affair\Commit;
use Database\Affair\Rollback;
use Database\Base\Driver;
use Database\Base\PDO;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Di\Context;
use Kiri\Di\Inject\Container;
use Kiri\Error\StdoutLogger;
use Kiri\Events\EventProvider;
use Kiri\Pool\Pool;
use Kiri\Server\Events\OnAfterRequest;
use Kiri\Server\Events\OnTaskerStart;
use Kiri\Server\Events\OnWorkerExit;
use Kiri\Server\Events\OnWorkerStart;
use Psr\Log\LoggerInterface;
use Swoole\Timer;
//use PDO;
/**
* Class Connection
* @package Database
*/
class Connection extends Component
{
public string $id = 'db';
public string $cds = '';
public string $password = '';
public string $username = '';
public string $charset = 'utf-8';
public string $tablePrefix = '';
public string $database = '';
public string $driver = 'mysql';
public int $timeout = 30;
public int $waite_time = 3;
public int $tick_time = 60000;
public int $idle_count = 3;
public int $idle_time = 60;
public array $pool = ['max' => 10, 'min' => 1];
private int $storey = 0;
protected int $timerId = -1;
public bool $enableCache = false;
public string $cacheDriver = 'redis';
public array $attributes = [];
public array $slave = [];
protected ?\Closure $_println = null;
/**
* @var StdoutLogger
*/
#[Container(LoggerInterface::class)]
public StdoutLogger $logger;
/**
* @var EventProvider
*/
#[Container(EventProvider::class)]
public EventProvider $eventProvider;
/**
* @param Pool $connections
*/
public function __construct(public Pool $connections)
{
parent::__construct();
$this->_println = \config('databases.logger', null);
}
/**
* @param float $startTime
* @param float $endTime
* @param string $sql
* @param array $params
* @return void
*/
public function println(float $startTime, float $endTime, string $sql, array $params = []): void
{
if (is_callable($this->_println)) {
call_user_func($this->_println, $startTime, $endTime, $sql, $params);
}
}
/**
* @return void
* @throws
*/
public function init(): void
{
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
$this->eventProvider->on(OnAfterRequest::class, [$this, 'clear']);
$this->eventProvider->on(OnWorkerExit::class, [$this, 'disconnect']);
$this->eventProvider->on(OnWorkerStart::class, [$this, 'tick']);
$this->eventProvider->on(OnTaskerStart::class, [$this, 'tick']);
}
/**
* @return void
*/
public function tick(): void
{
$this->timerId = Timer::tick($this->tick_time, fn () => $this->checkClientHealth($this->pool()));
}
/**
* @param Pool $pool
* @return void
* @throws
*/
protected function checkClientHealth(Pool $pool): void
{
$pool->flush($this->getName(), $this->pool['min'] ?? 1);
// $length = $pool->size($this->getName());
// for ($i = 0; $i < $length; $i++) {
// try {
// if (($client = $this->validator($pool)) === false) {
// break;
// }
// $pool->push($this->getName(), $client);
// } catch (\Throwable $exception) {
// if (!str_contains($exception->getMessage(), 'Client timeout.')) {
// $this->logger->error(throwable($exception), [$this->cds]);
// }
// }
// }
}
/**
* @return string
*/
private function getName(): string
{
return strtolower($this->driver) . '.' . $this->cds;
}
/**
* @param Pool $pool
* @return PDO|bool
* @throws
*/
protected function validator(Pool $pool): PDO|bool
{
/** @var $client PDO */
if (($client = $pool->get($this->getName())) === false) {
return false;
}
if ($client->query('select 1') === false) {
throw new Exception($client->errorInfo()[1]);
}
return $client;
}
/**
* @return PDO
* @throws
*/
public function getConnection(): PDO
{
if (!$this->inTransaction()) {
return $this->getNormalClientHealth();
} else {
return $this->getTransactionClient();
}
}
/**
* @return PDO
* @throws
*/
protected function getNormalClientHealth(): PDO
{
$data = $this->pool()->get($this->getName(), $this->waite_time);
if ($data === false) {
throw new Exception('Client Waite timeout.');
}
return $data;
}
/**
* @return $this
* @throws
*/
public function beginTransaction(): static
{
if ($this->storey < 0) {
$this->storey = 0;
}
$this->storey++;
return $this;
}
/**
* @return PDO
* @throws
*/
public function getTransactionClient(): PDO
{
$pdo = Context::get($this->cds);
if ($pdo === null) {
/** @var PDO $pdo */
$pdo = Context::set($this->cds, $this->getNormalClientHealth());
}
if ($this->storey > 0 && !$pdo->inTransaction()) {
$pdo->beginTransaction();
}
return $pdo;
}
/**
* @return bool
* @throws
*/
public function inTransaction(): bool
{
return $this->storey > 0;
}
/**
* @throws
* 事务回滚
*/
public function rollback(): void
{
$this->storey--;
if ($this->storey == 0) {
if (!Context::exists($this->cds)) {
return;
}
$pdo = $this->getTransactionClient();
if ($pdo->inTransaction()) {
$pdo->rollback();
}
$this->release($pdo);
}
}
/**
* @throws
* 事务提交
*/
public function commit(): void
{
$this->storey--;
if ($this->storey == 0) {
if (!Context::exists($this->cds)) {
return;
}
$pdo = $this->getTransactionClient();
if ($pdo->inTransaction()) {
$pdo->commit();
}
$this->release($pdo);
}
}
/**
* @return void
* @throws
*/
public function clear(): void
{
/** @var PDO $pdo */
$pdo = Context::get($this->cds);
if ($pdo === null) {
return;
}
if ($this->inTransaction()) {
$pdo->rollback();
}
$this->release($pdo);
}
/**
* @param string $sql
* @param array $attributes
* @return Command
* @throws
*/
public function createCommand(string $sql, array $attributes = []): Command
{
return new Command(['connection' => $this, 'sql' => $sql])->bindValues($attributes);
}
/**
* 回收链接
* @throws
*/
public function release(PDO $pdo): void
{
if (!$this->inTransaction()) {
$this->pool()->push($this->getName(), $pdo);
}
}
/**
*
* 回收链接
* @throws
*/
public function clear_connection(): void
{
$this->pool()->flush($this->getName(), 0);
}
/**
* @throws
*/
public function disconnect(): void
{
if ($this->timerId > -1) {
Timer::clear($this->timerId);
}
$this->pool()->close($this->getName());
}
/**
* @return PDO
*/
public function newConnect(): \PDO
{
$driver = strtolower($this->driver);
$options = [
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
\PDO::ATTR_STRINGIFY_FETCHES => false,
\PDO::ATTR_EMULATE_PREPARES => true,
\PDO::ATTR_TIMEOUT => $this->timeout,
];
// MySQL 特定的选项
if ($driver === 'mysql') {
$options[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $this->charset;
}
$pdo = new PDO($this->database, $this->cds, $this->username, $this->password, $options, $driver);
foreach ($this->attributes as $key => $attribute) {
$pdo->setAttribute($key, $attribute);
}
return $pdo;
}
/**
* @return Pool
*/
protected function pool(): Pool
{
if (!$this->connections->hasChannel($this->getName())) {
$this->connections->created($this->getName(), $this->pool['max'] ?? 1, [$this, 'newConnect']);
}
return $this->connections;
}
}
+92
View File
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace Database;
use Exception;
use Kiri;
use Kiri\Abstracts\Providers;
/**
* Class DatabasesProviders
* @package Database
*/
class DatabasesProviders extends Providers
{
/**
* @var array
*/
protected array $connections = [];
/**
* @return void
* @throws
*/
public function onImport(): void
{
$main = Kiri::getDi()->get(Kiri\Application::class);
$main->command(BackupCommand::class, ImplodeCommand::class);
$databases = \config('databases.connections', []);
if (count($databases) < 1) {
return;
}
foreach ($databases as $key => $database) {
$this->set($key, $this->_settings($database));
}
}
/**
* @param string $name
* @return Connection
* @throws Exception
*/
public function get(string $name): Connection
{
return $this->connections[$name];
}
/**
* @param string $key
* @param array $connection
* @return void
* @throws Exception
*/
protected function set(string $key, array $connection): void
{
$this->connections[$key] = Kiri::createObject($connection);
}
/**
* @param array $database
* @return array
*/
private function _settings(array $database): array
{
$clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60];
return [
'id' => $database['id'],
'cds' => $database['cds'],
'class' => Connection::class,
'username' => $database['username'],
'password' => $database['password'],
'tablePrefix' => $database['tablePrefix'],
'database' => $database['database'],
'driver' => $database['driver'] ?? 'mysql',
'timeout' => $database['timeout'] ?? 10,
'tick_time' => $database['tick_time'] ?? 60000,
'waite_time' => $database['waite_time'] ?? 3,
'pool' => $clientPool,
'attributes' => $database['attributes'] ?? [],
'charset' => $database['charset'] ?? 'utf8mb4'
];
}
}
+261
View File
@@ -0,0 +1,261 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 15:40
*/
declare(strict_types=1);
namespace Database;
use Closure;
use Database\Affair\BeginTransaction;
use Database\Affair\Commit;
use Database\Affair\Rollback;
use Database\Traits\QueryTrait;
use Exception;
use Kiri;
use Throwable;
/**
* Class Db
* @package Database
*/
class Db extends QueryTrait implements ISqlBuilder
{
private static bool $_inTransaction = false;
/**
* @var Connection|null
*/
private ?Connection $connection = null;
/**
* @param string|Connection $dbname
* @return Db
* @throws
*/
public static function connect(string|Connection $dbname): Db
{
$db = new Db();
if (is_string($dbname)) {
$db->connection = Kiri::getDi()->get(DatabasesProviders::class)->get($dbname);
} else {
$db->connection = $dbname;
}
return $db;
}
/**
* @return void
* @throws
*/
public static function beginTransaction(): void
{
fire(new BeginTransaction());
}
/**
* @return void
* @throws
*/
public static function commit(): void
{
fire(new Commit());
}
/**
* @return void
* @throws
*/
public static function rollback(): void
{
fire(new Rollback());
}
/**
* @param string $table
* @param string $database
* @return static
*/
public static function table(string $table, string $database = 'db'): Db|static
{
return self::connect($database)->from($table);
}
/**
* @param string $column
* @param string $alias
* @return string
*/
public static function any_value(string $column, string $alias = ''): string
{
if (empty($alias)) {
$alias = $column . '_any_value';
}
return 'ANY_VALUE(' . $column . ') as ' . $alias;
}
/**
* @return int
*/
public function count(): int
{
return $this->connection->createCommand(SqlBuilder::builder($this)->count())->rowCount();
}
/**
* @return bool
* @throws Exception
*/
public function exists(): bool
{
return $this->connection->createCommand(SqlBuilder::builder($this->limit(1))->exists())->exists();
}
/**
* @param string $sql
* @param array $attributes
* @return array|bool|int|string|null
* @throws
*/
public function fetchAll(string $sql, array $attributes = []): int|bool|array|string|null
{
return $this->connection->createCommand($sql, $attributes)->all();
}
/**
* @param string $sql
* @param array $attributes
* @return array|null
*/
public function fetch(string $sql, array $attributes = []): ?array
{
return $this->connection->createCommand($sql, $attributes)->one();
}
/**
* @return bool
*/
public function delete(): bool
{
return $this->connection->createCommand(SqlBuilder::builder($this)->delete())->delete();
}
/**
* @return bool|array|null
*/
public function first(): bool|array|null
{
return $this->connection->createCommand(SqlBuilder::builder($this)->one())->one();
}
/**
* @return bool|array
*/
public function get(): bool|array
{
return $this->connection->createCommand(SqlBuilder::builder($this)->all())->all();
}
/**
* @param string $sql
* @return mixed
*/
public function exec(string $sql): mixed
{
return $this->connection->createCommand($sql)->exec();
}
/**
* @param string $sql
* @return array|bool|null
*/
public function query(string $sql): array|bool|null
{
return $this->connection->createCommand($sql)->one();
}
/**
* @param string $sql
* @return array|bool
*/
public function queryAll(string $sql): array|bool
{
return $this->connection->createCommand($sql)->all();
}
/**
* @param string $table
* @return array|bool|null
* @throws
*/
public static function show(string $table): array|bool|null
{
if ($table == '') {
return null;
}
$connection = static::getDefaultConnection();
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
return $connection->createCommand((new Query())
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
->where(['REFERENCED_TABLE_NAME' => $table])
->build())->one();
}
/**
* @param string $table
* @param Connection|null $connection
* @param string $database
* @return array|null
* @throws
*/
public static function desc(string $table, ?Connection $connection = null, string $database = 'db'): ?array
{
$sql = SqlBuilder::builder(new Query())->columns($table);
$connection = self::getDefaultConnection($connection, $database);
return $connection->createCommand($sql)->all();
}
/**
* @param null|Connection $connection
* @param string $database
* @return mixed
* @throws
*/
public static function getDefaultConnection(?Connection $connection = null, string $database = 'db'): Connection
{
if ($connection instanceof Connection) {
return $connection;
}
$databases = \config('databases.connections', []);
$providers = Kiri::getDi()->get(DatabasesProviders::class);
if (empty($databases) || !is_array($databases)) {
throw new Exception('Please configure the database link.');
}
return $providers->get($databases[$database]);
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Database;
use Database\Traits\HasBase;
use Kiri;
use Kiri\Di\Context;
/**
* Class HasCount
* @package Database
*/
class HasCount extends HasBase
{
/**
* @return array|null|ModelInterface
* @throws
*/
public function get(): array|ModelInterface|null
{
return di(Relation::class)->get($this->name);
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 13:58
*/
declare(strict_types=1);
namespace Database;
use Database\Traits\HasBase;
use Exception;
use Kiri;
use Kiri\Di\Context;
/**
* Class HasMany
* @package Database
*
* @method with($name)
*/
class HasMany extends HasBase
{
/**
* @return array|null|Collection
* @throws
*/
public function get(): array|Collection|null
{
return di(Relation::class)->get($this->name);
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 13:47
*/
declare(strict_types=1);
namespace Database;
use Database\Traits\HasBase;
use Exception;
use Kiri;
use Kiri\Di\Context;
/**
* Class HasOne
* @package Database
* @internal Query
*/
class HasOne extends HasBase
{
/**
* @return array|null|ModelInterface
* @throws
*/
public function get(): array|ModelInterface|null
{
return di(Relation::class)->first($this->name);
}
}
+10 -10
View File
@@ -1,10 +1,10 @@
<?php
namespace Database;
interface ISqlBuilder
{
}
<?php
namespace Database;
interface ISqlBuilder
{
}
+100
View File
@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace Database;
use Co\Channel;
use Exception;
use Swoole\Coroutine;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
*
*/
class ImplodeCommand extends Command
{
/**
* @var string
*/
public string $command = 'db:implode';
/**
* @var string
*/
public string $description = 'php kiri.php db:implode --database users /Users/admin/snowflake-bi/test.sql';
/**
* @var Channel
*/
protected Channel $channel;
protected array $data;
/**
*
*/
protected function configure(): void
{
$this->setName('db:implode')
->addArgument('path', InputArgument::REQUIRED, "save to path", null)
->addOption('database', 'db', InputArgument::OPTIONAL)
->setDescription('php kiri.php db:implode --database users /Users/admin/snowflake-bi/test.sql');
$this->data = array_flip(get_html_translation_table(ENT_QUOTES | ENT_SUBSTITUTE));
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
try {
/** @var Connection $data */
$data = \Kiri::getDi()->get(DatabasesProviders::class)->get($input->getOption('database'));
$path = $input->getArgument('path');
if (!str_starts_with($path, '/')) {
$path = APP_PATH . $path;
}
$waite = new Coroutine\WaitGroup();
$stream = fopen($path, 'r');
if ($stream) {
while (($line = fgets($stream)) !== false) {
if (!str_starts_with(strtoupper($line), 'INSERT INTO')) {
continue;
}
$waite->add();
$insert = str_replace('&#039;', "\'", str_replace('&quot;', '"', $line));
$insert = strtr($insert, $this->data);
Coroutine::create(function () use ($waite, $insert, $data) {
Coroutine\defer(fn() => $waite->done());
$data->createCommand($insert)->exec();
});
}
fclose($stream);
}
$waite->wait();
$output->write('dump data success');
} catch (\Throwable $throwable) {
$output->writeln(throwable($throwable));
} finally {
return 1;
}
}
}
+374
View File
@@ -0,0 +1,374 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:39
*/
declare(strict_types=1);
namespace Database;
use Database\Base\ActiveQueryInterface;
use Exception;
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
/**
* Class Orm
* @package Database
*
* @property $attributes
* @property-read $oldAttributes
*
* @mixin ActiveQueryInterface
*/
class Model extends Base\Model
{
/**
* @param string $column
* @param int|float $value
* @return ModelInterface|false
* @throws
*/
public function increment(string $column, int|float $value): bool|ModelInterface
{
if (!$this->mathematics([$column => $value], '+')) {
return false;
}
$this->{$column} += $value;
return $this->refresh();
}
/**
* @param string $column
* @param int|float $value
* @return ModelInterface|false
* @throws
*/
public function decrement(string $column, int|float $value): bool|ModelInterface
{
if (!$this->mathematics([$column => $value], '-')) {
return false;
}
$this->{$column} -= $value;
return $this->refresh();
}
/**
* @param array $columns
* @return ModelInterface|false
* @throws
*/
public function increments(array $columns): bool|static
{
if (!$this->mathematics($columns, '+')) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key += $attribute;
}
return $this;
}
/**
* @param array $columns
* @return ModelInterface|false
* @throws
*/
public function decrements(array $columns): bool|static
{
if (!$this->mathematics($columns, '-')) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key -= $attribute;
}
return $this;
}
/**
* @param array $condition
* @param array $attributes
* @return bool|static
* @throws
*/
public static function findOrCreate(array $condition, array $attributes): bool|static
{
$model = static::instance();
$connection = $model->getConnection()->beginTransaction();
try {
/** @var static $select */
$select = $model::query()->where($condition)->first();
if ($select === null) {
$select = $model::populate(array_merge($condition, $attributes));
if (!$select->save()) {
throw new Exception('保存失败: ' . $model->getLastError());
}
}
$connection->commit();
return $select;
} catch (\Throwable $throwable) {
$model->getLogger()->json_log($throwable);
$connection->rollback();
return false;
}
}
/**
* @param array $condition
* @param array $attributes
* @return bool|static
* @throws
*/
public static function createOrUpdate(array $condition, array $attributes = []): bool|static
{
$model = static::instance();
$connection = $model->getConnection()->beginTransaction();
try {
/** @var static $select */
$select = static::query()->where($condition)->first();
if (empty($select)) {
$select = static::populate($condition);
}
$select->attributes = $attributes;
if (!$select->save()) {
throw new Exception('保存失败: ' . $model->getLastError());
}
$connection->commit();
return $select;
} catch (\Throwable $throwable) {
$model->getLogger()->json_log($throwable);
$connection->rollback();
return false;
}
}
/**
* @param string $name
* @param array $arguments
* @return Model|ActiveQuery
*/
public static function __callStatic(string $name, array $arguments): static|ActiveQuery
{
// TODO: Change the autogenerated stub
if (method_exists(static::class, $name)) {
return call_user_func(static::class . '::' . $name, ...$arguments);
} else {
return static::query()->{$name}(...$arguments);
}
}
/**
* @param array $columns
* @param string $action
* @return array|bool|int|string|null
*/
private function mathematics(array $columns, string $action): int|bool|array|string|null
{
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
$activeQuery = static::query()->where($condition);
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
if (is_bool($create)) {
return false;
}
return $activeQuery->buildCommand($create)->exec();
}
/**
* @param array $params
* @return ModelInterface|bool
* @throws
*/
public function update(array $params): static|bool
{
if (!$this->validator($this->rules(), $params) || !$this->beforeSave($this)) {
return FALSE;
}
return $this->updateInternal(...$this->arrayIntersect($params));
}
/**
* @param array $data
* @return bool
* @throws
*/
public static function inserts(array $data): bool
{
if (empty($data)) {
return \Kiri::getLogger()->logCategory('Insert data empty.', 'mysql');
} else {
return static::query()->insert($data);
}
}
/**
* @return bool
* @throws
*/
public function delete(): bool
{
if (!$this->beforeDelete()) {
return false;
}
if ($this->hasPrimary()) {
$result = static::deleteByCondition(['id' => $this->getPrimaryValue()]);
} else {
$result = static::deleteByCondition($this->_attributes);
}
return $this->afterDelete($result);
}
/**
* @return array
* @throws
*/
public function toArray(): array
{
$data = $this->_attributes;
foreach ($data as $key => $datum) {
$method = 'get' . ucfirst($key) . 'Attribute';
if (!method_exists($this, $method)) {
continue;
}
$data[$key] = $this->{$method}($datum);
}
return $this->withs($data);
}
/**
* @param array $data
* @return array
*/
private function withs(array $data): array
{
$with = $this->getWith();
foreach ($with as $value) {
$join = $this->withRelate($value);
if ($join instanceof \Arrayable) {
$join = $join->toArray();
}
$data[$value] = $join;
}
return $data;
}
/**
* @param string $modelName
* @param string $foreignKey
* @param string $localKey
* @return string
* @throws Exception
*/
private function _hasBase(string $modelName, string $foreignKey, string $localKey): string
{
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
$relation = di(Relation::class);
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . $value;
if (!$relation->hasIdentification($primaryKey)) {
/** @var $modelName ModelInterface */
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
}
return $primaryKey;
}
/**
* @param string $modelName
* @param string $foreignKey
* @param string $localKey
* @return HasOne|ActiveQuery
* @throws Exception
*/
public function hasOne(string $modelName, string $foreignKey, string $localKey): HasOne|ActiveQuery
{
return new HasOne($this->_hasBase($modelName, $foreignKey, $localKey));
}
/**
* @param string $modelName
* @param string $foreignKey
* @param string $localKey
* @return ActiveQuery|HasCount
* @throws Exception
*/
public function hasCount(string $modelName, string $foreignKey, string $localKey): ActiveQuery|HasCount
{
return new HasCount($this->_hasBase($modelName, $foreignKey, $localKey));
}
/**
* @param string $modelName
* @param string $foreignKey
* @param string $localKey
* @return ActiveQuery|HasMany
* @throws Exception
*/
public function hasMany(string $modelName, string $foreignKey, string $localKey): ActiveQuery|HasMany
{
return new HasMany($this->_hasBase($modelName, $foreignKey, $localKey));
}
/**
* @param string $modelName
* @param string $foreignKey
* @param string $localKey
* @return ActiveQuery|HasMany
* @throws Exception
*/
public function hasIn(string $modelName, string $foreignKey, string $localKey): ActiveQuery|HasMany
{
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
$relation = di(Relation::class);
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . implode('_', $value);
if (!$relation->hasIdentification($primaryKey)) {
/** @var $modelName ModelInterface */
$relation->bindIdentification($primaryKey, $modelName::query()->whereIn($foreignKey, $value));
}
return new HasMany($primaryKey);
}
/**
* @param bool $result
* @return bool
*/
public function afterDelete(bool $result): bool
{
return $result;
}
/**
* @return bool
* @throws Exception
*/
public function beforeDelete(): bool
{
return TRUE;
}
}
+116
View File
@@ -0,0 +1,116 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:39
*/
declare(strict_types=1);
namespace Database;
/**
* Interface ModelInterface
* @package Database
*/
interface ModelInterface
{
/**
* @param array|string|int|null $param
* @return ModelInterface|null
*/
public static function findOne(array|string|int|null $param): ?static;
/**
* @return mixed
*/
public function optimize(): mixed;
/**
* @param int $param
* @return ModelInterface|null
*/
public static function primary(int $param): ?static;
/**
* @param array $data
* @return static
*/
public static function populate(array $data): static;
/**
* @return ActiveQuery
* return a sql queryBuilder
*/
public static function query(): ActiveQuery;
/**
* @return ?string
*/
public function getPrimary(): ?string;
/**
* @return string
*/
public function getTable(): string;
/**
* @return Connection
*/
public function getConnection(): Connection;
/**
* @param string $field
* @return mixed
*/
public function getAttribute(string $field): mixed;
/**
* @param string $field
* @return mixed
*/
public function getOldAttribute(string $field): mixed;
/**
* @param string $field
* @param mixed $value
* @return mixed
*/
public function setAttribute(string $field, mixed $value): mixed;
/**
* @param string $field
* @param mixed $value
* @return mixed
*/
public function setOldAttribute(string $field, mixed $value): mixed;
/**
* @return array
*/
public function getOldAttributes(): array;
/**
* @return array
*/
public function getAttributes(): array;
/**
* @return array
*/
public function getChanges(): array;
}
+33
View File
@@ -0,0 +1,33 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/6/27 0027
* Time: 17:49
*/
declare(strict_types=1);
namespace Database;
use Database\Traits\QueryTrait;
/**
* Class Query
* @package Database
*/
class Query extends QueryTrait implements ISqlBuilder
{
/**
* @return string
* @throws
*/
public function getCondition(): string
{
return $this->builder->getCondition();
}
}
+92
View File
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace Database;
use Database\Base\ActiveQueryInterface;
use Kiri\Abstracts\Component;
use Kiri\Di\Context;
use Swoole\Coroutine;
/**
* Class Relation
* @package Kiri\db
*/
class Relation extends Component
{
/** @var ActiveQuery[] $_query */
private array $_query = [];
/**
* @param string $identification
* @param ActiveQuery $query
* @return $this
*/
public function bindIdentification(string $identification, ActiveQuery $query): static
{
$this->_query[$identification] = $query;
return $this;
}
/**
* @param string $identification
* @return bool
*/
public function hasIdentification(string $identification): bool
{
return isset($this->_query[$identification]);
}
/**
* @param string $name
* @return ActiveQuery|null
*/
public function getQuery(string $name): ?ActiveQuery
{
return $this->_query[$name] ?? null;
}
/**
* @param string $_identification
* @return mixed
* @throws
*/
public function first(string $_identification): mixed
{
if (!Context::exists($_identification)) {
Context::set($_identification, $this->_query[$_identification]->first());
}
return Context::get($_identification);
}
/**
* @param string $_identification
* @return mixed
*/
public function count(string $_identification): mixed
{
if (!Context::exists($_identification)) {
Context::set($_identification, $this->_query[$_identification]->count());
}
return Context::get($_identification);
}
/**
* @param string $_identification
* @return mixed
* @throws
*/
public function get(string $_identification): mixed
{
if (Context::exists($_identification)) {
Context::set($_identification, $this->_query[$_identification]->get());
}
return Context::get($_identification);
}
}
+462
View File
@@ -0,0 +1,462 @@
<?php
/** @noinspection ALL */
declare(strict_types=1);
namespace Database;
use JetBrains\PhpStorm\Pure;
use Kiri;
use Kiri\Abstracts\Component;
/**
* Class SqlBuilder
* @package Database
*/
class SqlBuilder extends Component
{
/**
* @var ActiveQuery|Query|ISqlBuilder|null
*/
public ActiveQuery|Query|ISqlBuilder|null $query;
/**
* @param ActiveQuery|Query|ISqlBuilder|null $config
*/
public function __construct(ActiveQuery|Query|null|ISqlBuilder $config)
{
parent::__construct();
$this->query = $config;
}
/**
* @param ISqlBuilder|null $query
* @return $this
* @throws
*/
public static function builder(ISqlBuilder|null $query): static
{
return new static($query);
}
/**
* @return string
* @throws
*/
public function getCondition(): string
{
return $this->where($this->query);
}
/**
* @param array $compiler
* @return string
* @throws
*/
public function hashCompiler(array $compiler): string
{
return $this->where($compiler);
}
/**
* @param array $attributes
* @return bool|array
* @throws
*/
public function update(array $attributes): bool|string
{
$conditions = $this->query;
$this->query = [];
$data = $this->__updateBuilder($this->makeParams($attributes));
foreach ($conditions as $condition) {
$this->query->pushParam($condition);
}
return $data;
}
/**
* @param array $attributes
* @param string $opera
* @return bool|array
* @throws
*/
public function mathematics(array $attributes, string $opera = '+'): bool|string
{
$string = [];
foreach ($attributes as $attribute => $value) {
$string[] = $attribute . '=' . $attribute . $opera . $value;
}
return $this->__updateBuilder($string);
}
/**
* @param array $string
* @return string|bool
* @throws
*/
private function __updateBuilder(array $string): string|bool
{
if (empty($string)) {
return Kiri::getLogger()->logCategory('None data update.');
}
return 'UPDATE ' . $this->query . ' SET ' . implode(',', $string) . $this->make();
}
/**
* @param array $attributes
* @param false $isBatch
* @return array
* @throws
*/
public function insert(array $attributes, bool $isBatch = false): string
{
$update = 'INSERT INTO ' . $this->query;
if ($isBatch === false) {
$attributes = [$attributes];
}
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
$keys = [];
foreach ($attributes as $attribute) {
$_keys = $this->makeParams($attribute, true);
$keys[] = implode(',', $_keys);
}
return $update . '(' . implode('),(', $keys) . ')';
}
/**
* @return string
* @throws
*/
public function delete(): string
{
return 'DELETE FROM ' . $this->query . $this->make();
}
/**
* @param $attributes
* @return array
*/
#[Pure] private function getFields(array $attributes): array
{
return array_keys(current($attributes));
}
/**
* @param array $attributes
* @param bool $isInsert
* @return array[]
* a=:b,
*/
private function makeParams(array $attributes, bool $isInsert = false): array
{
$keys = [];
foreach ($attributes as $key => $value) {
if ($isInsert === true) {
$keys[] = '?';
$this->query->pushParam($value);
} else {
$keys = $this->resolveParams($key, $value, $keys);
}
}
return $keys;
}
/**
* @param string $key
* @param mixed $value
* @param array $keys
* @return array
*/
private function resolveParams(string $key, mixed $value, array $keys): array
{
if (is_null($value)) {
return $keys;
}
if (preg_match('/^[+|-]\s\d+$/', (string)$value)) {
$keys[] = $key . '=' . $key . ' ' . $value;
} else {
$this->query->pushParam($value);
$keys[] = $key . '= ?';
}
return $keys;
}
/**
* @return string
* @throws
*/
public function one(): string
{
$this->query->offset(0)->limit(1);
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit();
}
/**
* @return string
* @throws
*/
public function all(): string
{
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit();
}
/**
* @return string
* @throws
*/
public function count(): string
{
return $this->makeSelect(['COUNT(*)']) . $this->make();
}
/**
* @return string
* @throws
*/
public function exists(): string
{
return $this->makeSelect(['0']) . $this->make();
}
/**
* @param string $table
* @return string
* @throws
*/
public function columns(string $table): string
{
$driver = $this->getDriver();
if (in_array($driver, ['pgsql', 'postgresql'])) {
// PostgreSQL 使用 information_schema
$tableName = trim($table, '"`');
return "SELECT
column_name as Field,
data_type as Type,
is_nullable as Null,
column_default as Default,
'' as Extra,
'' as Key
FROM information_schema.columns
WHERE table_name = '$tableName'
ORDER BY ordinal_position";
}
// MySQL 使用 SHOW FULL FIELDS
return 'SHOW FULL FIELDS FROM ' . $table;
}
/**
* @return string
* @throws
*/
private function make(): string
{
$select = $this->makeCondition();
$select .= $this->makeGroup();
$select .= $this->makeOrder();
return $select;
}
/**
* @param array $select
* @return string
*/
private function makeSelect(array $select = ['*']): string
{
$select = "SELECT " . implode(',', $select) . " FROM " . $this->query;
if ($this->query != "") {
$select .= " AS " . $this->query;
}
if (count($this->query) > 0) {
$select .= ' ' . implode(' ', $this->query);
}
return $select;
}
/**
* @return string
*/
private function makeGroup(): string
{
if ($this->query != "") {
return ' GROUP BY ' . $this->query;
}
return '';
}
/**
* @return string
*/
private function makeOrder(): string
{
if (count($this->query) > 0) {
return ' ORDER BY ' . implode(',', $this->query);
}
return '';
}
/**
* @return string
*/
private function makeCondition(): string
{
$condition = $this->where($this->query);
if (empty($condition)) {
return '';
}
return ' WHERE ' . $condition;
}
/**
* @return string
* @throws
*/
private function makeLimit(): string
{
if ($this->query >= 0 && $this->query >= 1) {
$driver = $this->getDriver();
if (in_array($driver, ['pgsql', 'postgresql'])) {
// PostgreSQL 使用 LIMIT ... OFFSET ... 语法
return ' LIMIT ' . $this->query . ' OFFSET ' . $this->query;
}
// MySQL 使用 LIMIT offset,limit 语法
return ' LIMIT ' . $this->query . ',' . $this->query;
}
return '';
}
/**
* @param false $isCount
* @return string
* @throws
*/
public function get(bool $isCount = false): string
{
if ($isCount === false) {
return $this->all();
}
return $this->count();
}
/**
* @return string
* @throws
*/
public function truncate(): string
{
return sprintf('TRUNCATE %s', $this->query);
}
/**
* @param array $where
* @return string
*/
private function where(array $where): string
{
if (count($where) < 1) {
return '';
}
$_tmp = [];
foreach ($where as $key => $value) {
$_tmp[] = $this->resolveCondition($key, $value);
}
return implode(' AND ', $_tmp);
}
/**
* @param string $field
* @param mixed $condition
* @return string
*/
private function resolveCondition(string|int $field, mixed $condition): string
{
if (is_string($field)) {
$this->query->pushParam($condition);
return $field . ' = ?';
} else if (is_string($condition)) {
return $condition;
} else {
return implode(' AND ', $this->_hashMap($condition));
}
}
/**
* @param $condition
* @return array
*/
private function _hashMap(array $condition): array
{
$_array = [];
foreach ($condition as $key => $value) {
$this->query->pushParam($value);
$_array[] = $key . '= ?';
}
return $_array;
}
/**
* 获取数据库驱动类型
* @return string
* @throws
*/
private function getDriver(): string
{
try {
// 尝试从 query 对象获取 connection
if ($this->query instanceof ActiveQuery && $this->query->modelClass !== null) {
if ($this->query->modelClass instanceof Model) {
$connection = $this->query->modelClass->getConnection();
if ($connection instanceof Connection) {
return strtolower($connection->driver ?? 'mysql');
}
}
}
// 如果是 Db 查询,尝试获取默认 connection
if (method_exists($this->query, 'getConnection')) {
$connection = $this->query->getConnection();
if ($connection instanceof Connection) {
return strtolower($connection->driver ?? 'mysql');
}
}
} catch (\Throwable $e) {
// 忽略错误,返回默认值
$this->getLogger()->json_log($e);
}
// 默认返回 mysql
return 'mysql';
}
}
+82
View File
@@ -0,0 +1,82 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 15:47
*/
declare(strict_types=1);
namespace Database\Traits;
use Database\ModelInterface;
use Database\Collection;
use Database\Relation;
/**
* Class HasBase
* @package Database
*
* @include Query
*
* @method first($name)
* @method all($name)
* @method count($name)
*/
abstract class HasBase implements \Database\Traits\Relation
{
/** @var ModelInterface|Collection */
protected mixed $data = null;
/**
* @var ModelInterface
*/
protected mixed $model;
protected mixed $value = 0;
/**
* HasBase constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param string $name
* @param array $arguments
* @return $this|mixed
* @throws \Exception
*/
public function __call(string $name, array $arguments)
{
if ($name !== 'get') {
$query = di(Relation::class)->getQuery($this->name);
if (is_null($query)) {
throw new \Exception('Unknown relation key: ' . $this->name);
}
$query->$name(...$arguments);
return $this;
} else {
return $this->get();
}
}
/**
* @param string $name
* @return mixed
*/
public function __get(string $name): mixed
{
if ($this->data === null) {
$this->data = $this->get();
}
return $this->data;
}
}
+971
View File
@@ -0,0 +1,971 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:56
*/
declare(strict_types=1);
namespace Database\Traits;
use Closure;
use Database\Base\ActiveQueryInterface;
use Database\Command;
use Database\ISqlBuilder;
use Database\ModelInterface;
use Database\Query;
use Database\SqlBuilder;
use Exception;
use Kiri;
use Kiri\Abstracts\Component;
/**
* Trait QueryTrait
* @package Database\Traits
*/
abstract class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
{
public array $where = [] {
&get {
return $this->where;
}
}
public array $select = ['t1.*'] {
&get {
return $this->select;
}
}
public array $join = [] {
&get {
return $this->join;
}
}
public array $order = [] {
&get {
return $this->order;
}
}
public int $offset = -1 {
get {
return $this->offset;
}
set {
$this->offset = $value;
}
}
public int $limit = -1 {
get {
return $this->limit;
}
set {
$this->limit = $value;
}
}
public string $group = '' {
get {
return $this->group;
}
set {
$this->group = $value;
}
}
public string $from = '' {
get {
return $this->from;
}
set {
$this->from = $value;
}
}
public string $alias = '' {
get {
return $this->alias;
}
set {
$this->alias = $value;
}
}
protected array $filter = [] {
&get {
return $this->filter;
}
}
protected bool $lock = FALSE;
protected SqlBuilder $builder;
public array $params = [] {
&get {
return $this->params;
}
}
protected array $_alias = [] {
&get {
return $this->_alias;
}
}
/**
* @var ModelInterface|string|null
*/
protected ModelInterface|string|null $modelClass {
get {
return $this->modelClass;
}
}
/**
* Comply constructor.
* @throws
*/
public function __construct($model = NULL)
{
if (!is_null($model)) {
$this->modelClass = $model;
}
$this->builder = SqlBuilder::builder($this);
parent::__construct();
}
/**
* @param Closure|Query $callback
* @param array $attributes
*
* @return $this
*/
public function whereNotExists(Closure|Query $callback, array $attributes = []): static
{
if ($callback instanceof Query) {
$this->where[] = 'NOT EXISTS(' . $callback->build() . ')';
$this->mergeParams($callback->params);
} else {
$this->where[] = 'NOT EXISTS(' . $this->makeClosureFunction($callback) . ')';
}
if (count($attributes) > 0) {
$this->bindParams($attributes);
}
return $this;
}
/**
* @param Closure|Query $callback
* @param array $attributes
*
* @return $this
*/
public function whereExists(Closure|Query $callback, array $attributes = []): static
{
if ($callback instanceof Query) {
$this->where[] = 'EXISTS(' . $callback->build() . ')';
$this->mergeParams($callback->params);
} else {
$this->where[] = 'EXISTS(' . $this->makeClosureFunction($callback) . ')';
}
if (count($attributes) > 0) {
$this->bindParams($attributes);
}
return $this;
}
/**
* @param array $params
* @return void
*/
public function mergeParams(array $params): void
{
foreach ($params as $key => $value) {
$this->pushParam($value);
}
}
/**
* @param string $column
* @param callable $callable
*
* @return $this
*/
public function when(string $column, callable $callable): static
{
$caseWhen = new When($column, $this);
call_user_func($callable, $caseWhen);
$this->where[] = $caseWhen->end();
return $this;
}
/**
* @param bool $lock
*
* @return $this
*/
public function lock(bool $lock): static
{
$this->lock = $lock;
return $this;
}
/**
* @param string $whereRaw
*
* @return QueryTrait
*/
public function whereRaw(string $whereRaw): static
{
if ($whereRaw == '') {
return $this;
}
$this->where[] = $whereRaw;
return $this;
}
/**
* @return string
* @throws
*/
public function getTable(): string
{
return $this->modelClass->getTable();
}
/**
* @param string $column
* @param string $value
*
* @return $this
*/
public function whereLocate(string $column, string $value): static
{
$this->where[] = 'LOCATE(' . $column . ',\'' . addslashes($value) . '\') > 0';
return $this;
}
/**
* @param string $column
*
* @return $this
*/
public function whereNull(string $column): static
{
$this->where[] = $column . ' IS NULL';
return $this;
}
/**
* @param string $column
*
* @return $this
*/
public function whereEmpty(string $column): static
{
$this->where[] = $column . ' = \'\'';
return $this;
}
/**
* @param string $column
*
* @return $this
*/
public function whereNotEmpty(string $column): static
{
$this->where[] = $column . ' <> \'\'';
return $this;
}
/**
* @param string $column
*
* @return $this
*/
public function whereNotNull(string $column): static
{
$this->where[] = $column . ' IS NOT NULL';
return $this;
}
/**
* @param string $alias
*
* @return $this
*
* select * from tableName as t1
*/
public function alias(string $alias = 't1'): static
{
$this->alias = $alias;
$this->_alias = [$alias];
return $this;
}
/**
* @param string|Closure|Query $tableName
*
* @return $this
*/
public function from(string|Closure|Query $tableName): static
{
if ($tableName instanceof Query) {
$this->from = '(' . $tableName->build() . ')';
$this->mergeParams($tableName->params);
} else if ($tableName instanceof Closure) {
$this->from = '(' . $this->makeClosureFunction($tableName) . ')';
} else if (class_exists($tableName)) {
$this->from = (new $tableName)->getTable();
} else {
$this->from = $tableName;
}
return $this;
}
/**
* @param string $tableName
* @param string $alias
* @param array $on
* @param array $param
*
* @return $this
* $query->join([$tableName, ['userId'=>'uuvOd']], $param)
* $query->join([$tableName, ['userId'=>'uuvOd'], $param])
* $query->join($tableName, ['userId'=>'uuvOd',$param])
*/
private function join(string $tableName, string $alias, array $on, array $param = []): static
{
if (empty($on)) {
return $this;
}
$this->_alias[] = $alias;
$join[] = $tableName . ' AS ' . $alias;
$join[] = 'ON ' . $this->onCondition($on);
if (empty($join)) {
return $this;
}
$this->join[] = implode(' ', $join);
if (!empty($param)) {
$this->bindParams($param);
}
return $this;
}
/**
* @param array $params
*
* @return void
*/
public function bindParams(array $params): void
{
foreach ($params as $param) {
$this->pushParam($param);
}
}
/**
* @param array $condition
*
* @return string
*/
private function onCondition(array $condition): string
{
$array = [];
foreach ($condition as $key => $item) {
if (is_numeric($item) || !$this->isAliasField($item)) {
$array[] = $key . '= ?';
$this->pushParam($item);
} else {
$array[] = $key . '=' . $item;
}
}
return implode(' AND ', $array);
}
/**
* @param string $value
*
* @return bool
*/
private function isAliasField(string $value): bool
{
foreach ($this->_alias as $alias) {
if (str_starts_with($value, $alias . '.')) {
return TRUE;
}
}
return FALSE;
}
/**
* @param string $tableName
* @param string $alias
* @param array $onCondition
* @param array $param
*
* @return $this
* @throws Exception
*/
public function leftJoin(string $tableName, string $alias, array $onCondition, array $param = []): static
{
if (class_exists($tableName)) {
$model = Kiri::getDi()->get($tableName);
if (!($model instanceof ModelInterface)) {
throw new Exception('Model must implement ' . ModelInterface::class);
}
$tableName = $model->getTable();
}
return $this->join("LEFT JOIN " . $tableName, $alias, $onCondition, $param);
}
/**
* @param string $tableName
* @param string $alias
* @param array $onCondition
* @param array $param
*
* @return $this
* @throws Exception
*/
public function rightJoin(string $tableName, string $alias, array $onCondition, array $param = []): static
{
if (class_exists($tableName)) {
$model = Kiri::getDi()->get($tableName);
if (!($model instanceof ModelInterface)) {
throw new Exception('Model must implement ' . ModelInterface::class);
}
$tableName = $model->getTable();
}
return $this->join("RIGHT JOIN " . $tableName, $alias, $onCondition, $param);
}
/**
* @param string $tableName
* @param string $alias
* @param array $onCondition
* @param array $param
*
* @return $this
* @throws Exception
*/
public function innerJoin(string $tableName, string $alias, array $onCondition, array $param = []): static
{
if (class_exists($tableName)) {
$model = Kiri::getDi()->get($tableName);
if (!($model instanceof ModelInterface)) {
throw new Exception('Model must implement ' . ModelInterface::class);
}
$tableName = $model->getTable();
}
return $this->join("INNER JOIN " . $tableName, $alias, $onCondition, $param);
}
/**
* @param string $field
*
* @return $this
*/
public function sum(string $field): static
{
$this->select[] = 'SUM(' . $field . ') AS ' . $field;
return $this;
}
/**
* @param string $field
*
* @return $this
*/
public function max(string $field): static
{
$this->select[] = 'MAX(' . $field . ') AS ' . $field;
return $this;
}
/**
* @param string $lngField
* @param string $latField
* @param int|float $lng1
* @param int|float $lat1
*
* @return $this
*/
public function distance(string $lngField, string $latField, int|float $lng1, int|float $lat1): static
{
$sql = "ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(($lat1 * PI() / 180 - $lat1 * PI() / 180) / 2),2) + COS($lat1 * PI() / 180) * COS($latField * PI() / 180) * POW(SIN(($lng1 * PI() / 180 - $lngField * PI() / 180) / 2),2))) * 1000) AS distance";
$this->select[] = $sql;
return $this;
}
/**
* @param string|array $column
* @param string $sort
*
* @return $this
*
* [
* 'addTime',
* 'descTime desc'
* ]
*/
public function orderBy(string|array $column, string $sort = 'DESC'): static
{
if (empty($column)) {
return $this;
}
if (is_string($column)) {
return $this->addOrder(...func_get_args());
}
foreach ($column as $val) {
$this->addOrder($val);
}
return $this;
}
/**
* @param string $column
* @param string $sort
*
* @return $this
*
*/
private function addOrder(string $column, string $sort = 'DESC'): static
{
if (func_num_args() == 1 || str_contains($column, ' ')) {
$this->order[] = $column;
} else {
$this->order[] = "$column $sort";
}
return $this;
}
/**
* @param array $column
*
* @return $this
*/
public function select(array $column = ['*']): static
{
$this->select = $column;
return $this;
}
/**
* @return $this
*/
public function rand(): static
{
$this->order[] = 'RAND()';
return $this;
}
/**
* @param array|Closure|string $conditionArray
* @param string $opera
* @param null $value
*
* @return QueryTrait
* @throws
*/
public function whereOr(array|Closure|string $conditionArray = [], string $opera = '=', $value = NULL): static
{
if ($conditionArray instanceof Closure) {
$conditionArray = $this->makeClosureFunction($conditionArray);
}
if (func_num_args() > 1) {
[$conditionArray, $opera, $value] = $this->opera(...func_get_args());
$conditionArray = $this->sprintf($conditionArray, $value, $opera);
}
$this->where = ['((' . implode(' AND ', $this->where) . ') OR (' . $conditionArray . '))'];
return $this;
}
/**
* @param string $column
* @param string $value
*
* @return $this
*/
public function whereLike(string $column, string $value): static
{
$this->pushParam($value);
$this->where[] = $column . ' LIKE \'%?%\'';
return $this;
}
/**
* @param string $column
* @param string $value
*
* @return $this
*/
public function whereLeftLike(string $column, string $value): static
{
$this->pushParam($value);
$this->where[] = $column . ' LLike \'%?\'';
return $this;
}
/**
* @param string $column
* @param string $value
*
* @return $this
*/
public function whereRightLike(string $column, string $value): static
{
$this->pushParam($value);
$this->where[] = $column . ' RLike \'?%\'';
return $this;
}
/**
* @param string $column
* @param string $value
*
* @return $this
*/
public function whereNotLike(string $column, string $value): static
{
$this->pushParam($value);
$this->where[] = $column . ' NOT LIKE \'%?%\'';
return $this;
}
/**
* @param string $columns
* @param array|Closure|Query $value
*
* @return $this
* @throws Exception
*/
public function whereIn(string $columns, array|Closure|Query $value): static
{
if (is_array($value)) {
if (count($value) < 1)
throw new Exception('Value must be an not empty array');
$this->where[] = $columns . ' IN (' . implode(',', $value) . ')';
} else if ($value instanceof Query) {
$this->where[] = $columns . ' IN (' . $value->build() . ')';
$this->mergeParams($value->params);
} else {
$this->where[] = $columns . ' IN (' . $this->makeClosureFunction($value) . ')';
}
return $this;
}
/**
* @return Query
* @throws
*/
public function queryInstance(): Query
{
return new Query;
}
/**
* @param string $columns
* @param array $value
*
* @return $this
*/
public function whereNotIn(string $columns, array $value): static
{
if (count($value) < 1) {
$value = [-1];
}
$this->where[] = $columns . ' NOT IN (' . implode(',', $value) . ')';
return $this;
}
/**
* @param string $column
* @param int|float $start
* @param int|float $end
*
* @return $this
*/
public function whereBetween(string $column, int|float $start, int|float $end): static
{
if (empty($column) || empty($start) || empty($end)) {
return $this;
}
$this->pushParam($start);
$this->pushParam($end);
$this->where[] = $column . ' BETWEEN ? AND ?';
return $this;
}
/**
* @param string $column
* @param int|float $start
* @param int|float $end
*
* @return $this
*/
public function whereNotBetween(string $column, int|float $start, int|float $end): static
{
if (empty($column) || empty($start) || empty($end)) {
return $this;
}
$this->pushParam($start);
$this->pushParam($end);
$this->where[] = $column . ' NOT BETWEEN ? AND ?';
return $this;
}
/**
* @param mixed $value
*
* @return $this
*/
public function pushParam(mixed $value): static
{
$this->params[] = $value;
return $this;
}
/**
* @param array|string $column
* @param mixed $value
*
* @return $this
*/
public function where(array|string $column, mixed $value = NULL): static
{
if (func_num_args() === 2) {
return $this->addArray([$column => $value]);
}
if (is_string($column)) {
return $this->whereRaw($column);
} else {
return $this->addArray($column);
}
}
/**
* @param string $column
* @param string $opera
* @param mixed $value
*
* @return $this
*/
public function whereMath(string $column, string $opera, mixed $value): static
{
$this->pushParam($value);
$this->where[] = $column . ' ' . $opera . ' ?';
return $this;
}
/**
* @param Closure $closure
*
* @return $this
*/
public function whereClosure(Closure $closure): static
{
$this->where[] = $this->makeClosureFunction($closure);
return $this;
}
/**
* @param Closure|array $closure
*
* @return string
* @throws
*/
public function makeClosureFunction(Closure|array $closure): string
{
$generate = $this->queryInstance();
if ($closure instanceof Closure) {
call_user_func($closure, $generate);
} else {
$generate->addArray($closure);
}
$this->mergeParams($generate->params);
return $generate->build();
}
/**
* @param string $name
* @param string|null $having
*
* @return $this
*/
public function groupBy(string $name, ?string $having = NULL): static
{
$this->group = $name;
if (empty($having)) {
return $this;
}
$this->group .= ' HAVING ' . $having;
return $this;
}
/**
* @param int $limit
*
* @return $this
*/
public function limit(int $limit = 20): static
{
$this->limit = $limit;
return $this;
}
/**
* @param int $offset
*
* @return $this
*/
public function offset(int $offset): static
{
$this->offset = $offset;
return $this;
}
/**
* @return array
*/
private function opera(): array
{
if (func_num_args() == 3) {
[$column, $opera, $value] = func_get_args();
} else {
[$column, $value] = func_get_args();
}
if (!isset($opera)) {
$opera = '=';
}
return [$column, $opera, $value];
}
/**
* @param array $array
*
* @return $this
*/
private function addArray(array $array): static
{
foreach ($array as $key => $value) {
if ($value === NULL) {
continue;
}
$this->where[] = is_numeric($key) ? $value : $this->sprintf($key, $value);
}
return $this;
}
/**
* @param string $column
* @param mixed $value
* @param string $opera
*
* @return string
*/
private function sprintf(string $column, mixed $value, string $opera = '='): string
{
if (!is_numeric($value)) {
if (str_contains($column, '.')) {
[$alias, $field] = explode('.', $value);
} else {
$alias = '';
}
if (in_array($alias, $this->_alias)) {
return $column . ' ' . $opera . ' ' . $value;
}
}
$this->pushParam($value);
return $column . ' ' . $opera . ' ?';
}
/**
* @param string $querySql
* @param array $params
*
* @return Command
*/
public function buildCommand(string $querySql, array $params = []): Command
{
$connection = $this->modelClass->getConnection();
if (count($params) > 0) {
$this->bindParams($params);
}
return $connection->createCommand($querySql, $this->params);
}
/**
* @return string
* @throws
*/
public function build(): string
{
return $this->builder->get();
}
}
+15 -10
View File
@@ -1,10 +1,15 @@
<?php
namespace Database\Traits;
interface Relation
{
public function get(): mixed;
}
<?php
declare(strict_types=1);
namespace Database\Traits;
interface Relation
{
/**
* @return mixed
*/
public function get(): mixed;
}
+97 -77
View File
@@ -1,77 +1,97 @@
<?php
namespace Database\Traits;
use Database\ActiveQuery;
use Database\ISqlBuilder;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class CaseWhen
* @package Database\Traits
*/
class When
{
public ActiveQuery|ISqlBuilder $query;
private array $_condition = [];
private string $else = '';
/**
* CaseWhen constructor.
* @param string $column
* @param ActiveQuery|ISqlBuilder $activeQuery
*/
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
{
$this->_condition[] = 'CASE ' . $column;
}
/**
* @param string|int $condition
* @param string $then
* @return $this
* @throws Exception
*/
public function when(string|int $condition, string $then): static
{
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
return $this;
}
/**
* @param string $alias
*/
public function else(string $alias)
{
$this->else = $alias;
}
/**
* @return string
*/
#[Pure] public function end(): string
{
if (empty($this->_condition)) {
return '';
}
$prefix = implode(' ', $this->_condition);
if (!empty($this->else)) {
$prefix .= ' ELSE ' . $this->else;
}
return '(' . $prefix . ' END)';
}
}
<?php
declare(strict_types=1);
namespace Database\Traits;
use Database\ActiveQuery;
use Database\ISqlBuilder;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class CaseWhen
* @package Database\Traits
*/
class When
{
public ActiveQuery|ISqlBuilder $query;
/**
* @var array
*/
private array $_condition = [] {
&get {
return $this->_condition;
}
}
/**
* @var string
*/
private string $else = '' {
get {
return $this->else;
}
set(string $value) {
$this->else = $value;
}
}
/**
* CaseWhen constructor.
* @param string $column
* @param ActiveQuery|ISqlBuilder $activeQuery
*/
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
{
$this->_condition[] = 'CASE ' . $column;
}
/**
* @param string|int $condition
* @param string $then
* @return $this
* @throws
*/
public function when(string|int $condition, string $then): static
{
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
return $this;
}
/**
* @param string $alias
*/
public function else(string $alias): void
{
$this->else = $alias;
}
/**
* @return string
*/
#[Pure]
public function end(): string
{
if (empty($this->_condition)) {
return '';
}
$prefix = implode(' ', $this->_condition);
if (!empty($this->else)) {
$prefix .= ' ELSE ' . $this->else;
}
return '(' . $prefix . ' END)';
}
}
+3 -4
View File
@@ -9,15 +9,14 @@
],
"license": "MIT",
"require": {
"php": ">=8.0",
"php": ">=8.5",
"ext-json": "*",
"ext-pdo": "*",
"game-worker/kiri-validator": "^v1.2",
"game-worker/kiri-event": "^v1.0"
"game-worker/kiri-pool": "^v1.0"
},
"autoload": {
"psr-4": {
"Database\\": "src/"
"Database\\": "./"
}
},
"require-dev": {
-309
View File
@@ -1,309 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 14:42
*/
declare(strict_types=1);
namespace Database;
use Database\Traits\QueryTrait;
use Exception;
use Kiri\Abstracts\Component;
/**
* Class ActiveQuery
* @package Database
*/
class ActiveQuery extends Component implements ISqlBuilder
{
use QueryTrait;
/** @var array */
public array $with = [];
/** @var bool */
public bool $asArray = FALSE;
/** @var bool */
public bool $useCache = FALSE;
/**
* @var Connection|null
*/
public ?Connection $db = NULL;
/**
* @var array
* 参数绑定
*/
public array $attributes = [];
/**
* Comply constructor.
* @param $model
* @param array $config
* @throws
*/
public function __construct($model, array $config = [])
{
$this->modelClass = $model;
$this->builder = SqlBuilder::builder($this);
parent::__construct($config);
}
/**
* 清除不完整数据
*/
public function clear()
{
$this->db = null;
$this->useCache = false;
$this->with = [];
}
/**
* @param $key
* @param $value
* @return $this
*/
public function addParam($key, $value): static
{
$this->attributes[$key] = $value;
return $this;
}
/**
* @param array $values
* @return $this
*/
public function addParams(array $values): static
{
foreach ($values as $key => $val) {
$this->addParam($key, $val);
}
return $this;
}
/**
* @param $name
* @return $this
*/
public function with($name): static
{
if (empty($name)) {
return $this;
}
if (is_string($name)) {
$name = explode(',', $name);
}
foreach ($name as $val) {
array_push($this->with, $val);
}
return $this;
}
/**
* @param $sql
* @param array $params
* @return mixed
* @throws Exception
*/
public function execute($sql, array $params = []): Command
{
return $this->modelClass->getConnection()->createCommand($sql, $params);
}
/**
* @return ModelInterface|null
* @throws Exception
*/
public function first(): ModelInterface|null
{
$data = $this->execute($this->builder->one())->one();
if (empty($data)) {
return NULL;
}
return $this->populate($data);
}
/**
* @return string
* @throws Exception
*/
public function toSql(): string
{
return $this->builder->get();
}
/**
* @return array|Collection
*/
public function get(): Collection|array
{
return $this->all();
}
/**
* @throws Exception
*/
public function flush(): array|bool|int|string|null
{
return $this->execute($this->builder->truncate())->exec();
}
/**
* @param int $size
* @param callable $callback
* @return Pagination
* @throws Exception
*/
public function page(int $size, callable $callback): Pagination
{
$pagination = new Pagination($this);
$pagination->setOffset(0);
$pagination->setLimit($size);
$pagination->setCallback($callback);
return $pagination;
}
/**
* @param string $field
* @param string $setKey
*
* @return array|null
* @throws Exception
*/
public function column(string $field, string $setKey = ''): ?array
{
return $this->all()->column($field, $setKey);
}
/**
* @return array|Collection
* @throws
*/
public function all(): Collection|array
{
$data = $this->execute($this->builder->all())->all();
if (!empty($this->with)){
$this->getWith($this->modelClass);
}
$collect = new Collection($this, $data, $this->modelClass);
if ($this->asArray) {
return $collect->toArray();
}
return $collect;
}
/**
* @param $data
* @return ModelInterface
* @throws Exception
*/
public function populate($data): ModelInterface
{
return $this->getWith($this->modelClass::populate($data));
}
/**
* @param ModelInterface $model
* @return ModelInterface
*/
public function getWith(ModelInterface $model): ModelInterface
{
if (empty($this->with) || !is_array($this->with)) {
return $model;
}
return $model->setWith($this->with);
}
/**
* @return int
* @throws Exception
*/
public function count(): int
{
$data = $this->execute($this->builder->count())->one();
if ($data && is_array($data)) {
return (int)array_shift($data);
}
return 0;
}
/**
* @param array $data
* @return array|Command|bool|int|string
* @throws Exception
*/
public function batchUpdate(array $data): Command|array|bool|int|string
{
$generate = $this->builder->update($data);
if (is_bool($generate)) {
return $generate;
}
return $this->execute(...$generate)->exec();
}
/**
* @param array $data
* @return bool
* @throws Exception
*/
public function batchInsert(array $data): bool
{
[$sql, $params] = $this->builder->insert($data, true);
return $this->execute($sql, $params)->exec();
}
/**
* @param $filed
*
* @return null
* @throws Exception
*/
public function value($filed)
{
return $this->first()[$filed] ?? null;
}
/**
* @return bool
* @throws Exception
*/
public function exists(): bool
{
return !empty($this->execute($this->builder->one())->fetchColumn());
}
/**
* @param bool $getSql
* @return int|bool|string|null
* @throws Exception
*/
public function delete(bool $getSql = false): int|bool|string|null
{
$sql = $this->builder->delete();
if ($getSql === false) {
return $this->execute($sql)->delete();
}
return $sql;
}
}
-44
View File
@@ -1,44 +0,0 @@
<?php
namespace Database\Annotation;
use Attribute;
use Database\Base\Getter;
use Exception;
/**
* Class Get
* @package Annotation\Model
*/
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Annotation\Attribute
{
/**
* Get constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param static $params
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws \Kiri\Exception\NotFindClassException
* @throws \ReflectionException
*/
public function execute(mixed $class, mixed $method = null): bool
{
di(Getter::class)->addGetter($this->name, $class, $method);
return true;
}
}
-41
View File
@@ -1,41 +0,0 @@
<?php
namespace Database\Annotation;
use Annotation\Attribute;
use Database\Base\Relate;
use Exception;
/**
* Class Relation
* @package Annotation\Model
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Relation extends Attribute
{
/**
* Relation constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
di(Relate::class)->addRelate($this->name, $class, $method);
return true;
}
}
-39
View File
@@ -1,39 +0,0 @@
<?php
namespace Database\Annotation;
use Annotation\Attribute;
use Database\Base\Setter;
use Exception;
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute
{
/**
* Set constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param static $params
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws \Kiri\Exception\NotFindClassException
* @throws \ReflectionException
*/
public function execute(mixed $class, mixed $method = null): bool
{
di(Setter::class)->addSetter($this->name, $class, $method);
return true;
}
}
-75
View File
@@ -1,75 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Base;
use Database\ActiveQuery;
use Database\ModelInterface;
use Exception;
/**
* Class CollectionIterator
* @package Database\Base
*/
class CollectionIterator extends \ArrayIterator
{
private ModelInterface|string $model;
/** @var ActiveQuery */
private ActiveQuery $query;
private ?ModelInterface $_clone = null;
public function clean()
{
unset($this->query);
}
/**
* CollectionIterator constructor.
* @param $model
* @param $query
* @param array $array
* @param int $flags
* @throws Exception
*/
public function __construct($model, $query, array $array = [], int $flags = 0)
{
$this->model = $model;
$this->query = $query;
parent::__construct($array, $flags);
}
/**
* @param $current
* @return ModelInterface
* @throws Exception
*/
protected function newModel($current): ModelInterface
{
return $this->model->populates($current);
}
/**
* @throws Exception
*/
public function current(): ModelInterface
{
if (is_array($current = parent::current())) {
$current = $this->newModel($current);
}
return $current;
}
}
-71
View File
@@ -1,71 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Base;
use Database\Condition\BetweenCondition;
use Database\Condition\InCondition;
use Database\Condition\LikeCondition;
use Database\Condition\LLikeCondition;
use Database\Condition\MathematicsCondition;
use Database\Condition\NotBetweenCondition;
use Database\Condition\NotInCondition;
use Database\Condition\NotLikeCondition;
use Database\Condition\RLikeCondition;
/**
* Class ConditionClassMap
* @package Database\Base
*/
class ConditionClassMap
{
public static array $conditionMap = [
'IN' => [
'class' => InCondition::class
],
'NOT IN' => [
'class' => NotInCondition::class
],
'LIKE' => [
'class' => LikeCondition::class
],
'NOT LIKE' => [
'class' => NotLikeCondition::class
],
'LLike' => [
'class' => LLikeCondition::class
],
'RLike' => [
'class' => RLikeCondition::class
],
'EQ' => [
'class' => MathematicsCondition::class,
'type' => 'EQ'
],
'NEQ' => [
'class' => MathematicsCondition::class,
'type' => 'NEQ'
],
'GT' => [
'class' => MathematicsCondition::class,
'type' => 'GT'
],
'EGT' => [
'class' => MathematicsCondition::class,
'type' => 'EGT'
],
'LT' => [
'class' => MathematicsCondition::class,
'type' => 'LT'
],
'ELT' => [
'class' => MathematicsCondition::class,
'type' => 'ELT'
],
'BETWEEN' => BetweenCondition::class,
'NOT BETWEEN' => NotBetweenCondition::class,
];
}
-35
View File
@@ -1,35 +0,0 @@
<?php
namespace Database\Base;
class Getter
{
private array $_classMapping = [];
/**
* @param $name
* @param $class
* @param $method
*/
public function addGetter($name, $class, $method)
{
$this->_classMapping[$class][$name] = $method;
}
/**
* @param $class
* @param null $name
* @return array|string|null
*/
public function getGetter($class, $name = null): null|array|string
{
if (!empty($name)) {
return $this->_classMapping[$class][$name] ?? null;
}
return $this->_classMapping[$class] ?? [];
}
}
-1093
View File
File diff suppressed because it is too large Load Diff
-36
View File
@@ -1,36 +0,0 @@
<?php
namespace Database\Base;
class Relate
{
private array $_classMapping = [];
/**
* @param $name
* @param $class
* @param $method
*/
public function addRelate($name, $class, $method)
{
$this->_classMapping[$class][$name] = $method;
}
/**
* @param $class
* @param $name
* @return null|array|string
*/
public function getRelate($class, $name = null): null|array|string
{
if (!empty($name)) {
return $this->_classMapping[$class][$name] ?? null;
}
return $this->_classMapping[$class] ?? [];
}
}
-32
View File
@@ -1,32 +0,0 @@
<?php
namespace Database\Base;
class Setter
{
private array $_classMapping = [];
/**
* @param $name
* @param $class
* @param $method
*/
public function addSetter($name, $class, $method)
{
$this->_classMapping[$class][$name] = $method;
}
/**
* @param $class
* @param $name
* @return null|array|string
*/
public function getSetter($class, $name): null|array|string
{
return $this->_classMapping[$class][$name] ?? null;
}
}
-243
View File
@@ -1,243 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 13:38
*/
declare(strict_types=1);
namespace Database;
use Database\Base\AbstractCollection;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class Collection
* @package Database
* @property-read $length
*/
class Collection extends AbstractCollection
{
/**
* @return array
*/
public function getItems(): array
{
// TODO: Change the autogenerated stub
return $this->_item;
}
/**
* @param $field
*
* @return array|null
* @throws Exception
*/
public function values($field): ?array
{
if (empty($field) || !is_string($field)) {
return NULL;
}
$_tmp = [];
$data = $this->toArray();
foreach ($data as $val) {
/** @var ModelInterface $val */
$_tmp[] = $val[$field];
}
return $_tmp;
}
/**
* @param string $field
* @return array|null
*/
public function keyBy(string $field): ?array
{
$array = $this->toArray();
$column = array_flip(array_column($array, $field));
foreach ($column as $key => $value) {
$column[$key] = $array[$value];
}
return $column;
}
/**
* @return $this
*/
public function orderRand(): static
{
shuffle($this->_item);
return $this;
}
/**
* @param int $start
* @param int $length
*
* @return array
*/
#[Pure] public function slice(int $start = 0, int $length = 20): array
{
if (empty($this->_item) || !is_array($this->_item)) {
return [];
}
if (count($this->_item) < $length) {
return $this->_item;
} else {
return array_slice($this->_item, $start, $length);
}
}
/**
* @param string $field
* @param string $setKey
*
* @return array|null
*/
public function column(string $field, string $setKey = ''): ?array
{
$data = $this->toArray();
if (empty($data)) {
return [];
}
if (!empty($setKey) && is_string($setKey)) {
return array_column($data, $field, $setKey);
} else {
return array_column($data, $field);
}
}
/**
* @param string $field
*
* @return float|int|null
*/
public function sum(string $field): float|int|null
{
$array = $this->column($field);
if (empty($array)) {
return NULL;
}
return array_sum($array);
}
/**
* @return ModelInterface|array
*/
#[Pure] public function current(): ModelInterface|array
{
return current($this->_item);
}
/**
* @return int
*/
#[Pure] public function size(): int
{
return (int)count($this->_item);
}
/**
* @return array
* @throws
*/
public function toArray(): array
{
$array = [];
/** @var Model $value */
foreach ($this as $value) {
if (!is_object($value)) {
continue;
}
$array[] = $value->setWith($this->query->with)->toArray();
}
$this->_item = [];
return $array;
}
/**
* @throws Exception
* 批量删除
*/
public function delete(): bool
{
$model = $this->getModel();
if (!$model->hasPrimary()) return false;
$ids = [];
foreach ($this as $item) {
$id = $item->getPrimaryValue();
if (!empty($id)) {
$ids[] = $id;
}
}
return $model::query()->whereIn($model->getPrimary(), $ids)->delete();
}
/**
* @param array $condition
* @return Collection
* @throws
*/
public function filter(array $condition): Collection|static
{
$_filters = [];
if (empty($condition)) {
return $this;
}
foreach ($this as $value) {
if (!$this->filterCheck($value, $condition)) {
continue;
}
$_filters[] = $value;
}
return new Collection($this->query, $_filters, $this->model);
}
/**
* @param $value
* @param $condition
* @return bool
* @throws Exception
*/
private function filterCheck($value, $condition): bool
{
$_value = $value;
if ($_value instanceof ModelInterface) {
$_value = $_value->toArray();
}
$_tmp = array_intersect_key($_value, $condition);
if (count(array_diff_assoc($_tmp, $condition)) > 0) {
return false;
}
return true;
}
/**
* @param $key
* @param $value
* @return mixed
*/
public function exists($key, $value): mixed
{
foreach ($this as $item) {
if ($item->$key === $value) {
return $item;
}
}
return null;
}
/**
* @return bool
*/
#[Pure] public function isEmpty(): bool
{
return $this->size() < 1;
}
}
-203
View File
@@ -1,203 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 15:23
*/
declare(strict_types=1);
namespace Database;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Core\Json;
use PDOStatement;
/**
* Class Command
* @package Database
*/
class Command extends Component
{
const ROW_COUNT = 'ROW_COUNT';
const FETCH = 'FETCH';
const FETCH_ALL = 'FETCH_ALL';
const EXECUTE = 'EXECUTE';
const FETCH_COLUMN = 'FETCH_COLUMN';
/** @var Connection */
public Connection $db;
/** @var ?string */
public ?string $sql = '';
/** @var array */
public array $params = [];
/** @var string */
public string $dbname = '';
/** @var PDOStatement|null */
private ?PDOStatement $prepare = null;
/**
* @return array|bool|int|string|PDOStatement|null
* @throws Exception
*/
public function incrOrDecr(): array|bool|int|string|PDOStatement|null
{
return $this->execute(static::EXECUTE);
}
/**
* @param bool $isInsert
* @param mixed $hasAutoIncrement
* @return int|bool|array|string|null
* @throws Exception
*/
public function save(bool $isInsert = TRUE, mixed $hasAutoIncrement = null): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function all(): int|bool|array|string|null
{
return $this->execute(static::FETCH_ALL);
}
/**
* @return array|bool|int|string|null
* @throws Exception
*/
public function one(): null|array|bool|int|string
{
return $this->execute(static::FETCH);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function fetchColumn(): int|bool|array|string|null
{
return $this->execute(static::FETCH_COLUMN);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function rowCount(): int|bool|array|string|null
{
return $this->execute(static::ROW_COUNT);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function flush(): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
/**
* @param $type
* @return int|bool|array|string|null
* @throws Exception
*/
private function execute($type): int|bool|array|string|null
{
try {
$time = microtime(true);
if ($type === static::EXECUTE) {
$result = $this->db->getConnect($this->sql)->execute($this->sql,$this->params);
} else {
$result = $this->search($type);
}
if (microtime(true) - $time >= 0.02) {
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
}
} catch (\Throwable $exception) {
$result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
} finally {
$this->db->release();
return $result;
}
}
/**
* @param $type
* @return array|int|bool|null
* @throws Exception
*/
private function search($type): array|int|bool|null
{
$pdo = $this->db->getConnect($this->sql);
if ($type === static::FETCH_COLUMN) {
$data = $pdo->fetchColumn($this->sql, $this->params);
} else if ($type === static::ROW_COUNT) {
$data = $pdo->count($this->sql, $this->params);
} else if ($type === static::FETCH_ALL) {
$data = $pdo->fetchAll($this->sql, $this->params);
} else {
$data = $pdo->fetch($this->sql, $this->params);
}
return $data;
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function delete(): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function exec(): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
/**
* @param array $data
* @return $this
*/
public function bindValues(array $data = []): static
{
if (!is_array($this->params)) {
$this->params = [];
}
if (!empty($data)) {
$this->params = array_merge($this->params, $data);
}
return $this;
}
/**
* @param $sql
* @return $this
* @throws Exception
*/
public function setSql($sql): static
{
$this->sql = $sql;
return $this;
}
}
-83
View File
@@ -1,83 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\BaseObject;
use Kiri\Core\Str;
/**
* Class Condition
* @package Database\Condition
*/
abstract class Condition extends BaseObject
{
protected string $column = '';
protected string $opera = '=';
/** @var array|mixed */
protected $value;
const INT_TYPE = ['bit', 'bool', 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp'];
protected array $attributes = [];
abstract public function builder();
/**
* @param string $column
*/
public function setColumn(string $column): void
{
$this->column = $column;
}
/**
* @param string $opera
*/
public function setOpera(string $opera): void
{
$this->opera = $opera;
}
/**
* @param $params
*/
public function setValue($params): void
{
if (is_array($params)) {
$values = [];
foreach ($params as $item => $value) {
$values[$item] = is_numeric($value) ? $value : '\'' . $value . '\'';
}
$this->value = $values;
} else {
$this->value = $this->checkIsSqlString($params);
}
}
/**
* @param $params
* @return int|string
*/
#[Pure] private function checkIsSqlString($params): int|string
{
if (is_numeric($params)) {
return $params;
}
$check = ltrim($params, '(');
$check = strtolower(substr($check, 0, 6));
if (in_array($check, ['update', 'select', 'insert', 'delete'])) {
return $params;
} else {
return sprintf('\'%s\'', $params);
}
}
}
-30
View File
@@ -1,30 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
/**
* Class HashCondition
* @package Yoc\db\condition
*/
class HashCondition extends Condition
{
/**
* @return string
*/
public function builder(): string
{
$array = [];
if (empty($this->value)) {
return '';
}
foreach ($this->value as $key => $value) {
if (is_null($value)) continue;
$array[] = sprintf("%s = '%s'", $key, addslashes($value));
}
return implode(' AND ', $array);
}
}
-31
View File
@@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use Database\ActiveQuery;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class InCondition
* @package Database\Condition
*/
class InCondition extends Condition
{
/**
* @return string
* @throws Exception
*/
#[Pure] public function builder(): string
{
if (is_array($this->value)) {
return sprintf('%s IN (%s)', $this->column, implode(',', $this->value));
} else {
return sprintf('%s IN (%s)', $this->column, $this->value);
}
}
}
-28
View File
@@ -1,28 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Condition;
use JetBrains\PhpStorm\Pure;
/**
* Class NotInCondition
* @package Database\Condition
*/
class NotInCondition extends Condition
{
/**
* @return string|null
*/
#[Pure] public function builder(): ?string
{
if (!is_array($this->value)) {
return null;
}
$value = '\'' . implode('\',\'', $this->value) . '\'';
return '`' . $this->column . '` not in(' . $value . ')';
}
}
-352
View File
@@ -1,352 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:09
*/
declare(strict_types=1);
namespace Database;
use Annotation\Inject;
use Database\Affair\BeginTransaction;
use Database\Affair\Commit;
use Database\Affair\Rollback;
use Database\Mysql\PDO;
use Database\Mysql\Schema;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Events\EventProvider;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Events\OnWorkerExit;
use Server\Events\OnWorkerStop;
/**
* Class Connection
* @package Database
*/
class Connection extends Component
{
public string $id = 'db';
public string $cds = '';
public string $password = '';
public string $username = '';
public string $charset = 'utf-8';
public string $tablePrefix = '';
public string $database = '';
public int $timeout = 1900;
/**
* @var bool
* enable database cache
*/
public bool $enableCache = false;
/**
* @var string
*/
public string $cacheDriver = 'redis';
/**
* @var array
*/
public array $slaveConfig = [];
/**
* @var Schema
*/
#[Inject(Schema::class)]
public Schema $_schema;
/**
* @var EventProvider
*/
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
/**
* execute by __construct
* @throws Exception
*/
public function init()
{
$this->eventProvider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0);
$this->eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0);
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
if (Db::transactionsActive()) {
$this->beginTransaction();
}
$this->_schema->db = $this;
}
/**
* @param null $sql
* @return PDO
* @throws Exception
*/
public function getConnect($sql = NULL): PDO
{
return $this->getPdo($sql);
}
/**
* @param $config
* @return $this
*/
public function configure($config): static
{
Kiri::configure($this, $config);
return $this;
}
/**
* @throws Exception
*/
public function fill()
{
$connections = $this->connections();
$pool = Config::get('databases.pool.max', 10);
$connections->initConnections('Mysql:' . $this->cds, true, $pool);
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
$connections->initConnections('Mysql:' . $this->slaveConfig['cds'], false, $pool);
}
}
/**
* @param $sql
* @return PDO
* @throws Exception
*/
private function getPdo($sql): PDO
{
if ($this->isWrite($sql)) {
return $this->masterInstance();
} else {
return $this->slaveInstance();
}
}
/**
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public function getSchema(): Schema
{
if ($this->_schema === null) {
$this->_schema = Kiri::createObject([
'class' => Schema::class,
'db' => $this
]);
}
return $this->_schema;
}
/**
* @param $sql
* @return bool
*/
public function isWrite($sql): bool
{
if (empty($sql)) return false;
if (str_starts_with(strtolower($sql), 'select')) {
return false;
}
return true;
}
/**
* @return mixed
* @throws Exception
*/
public function getCacheDriver(): mixed
{
if (!$this->enableCache) {
return null;
}
return Kiri::app()->get($this->cacheDriver);
}
/**
* @return PDO
* @throws Exception
*/
public function masterInstance(): PDO
{
return $this->connections()->get([
'cds' => $this->cds,
'username' => $this->username,
'password' => $this->password,
'database' => $this->database
], true);
}
/**
* @return PDO
* @throws Exception
*/
public function slaveInstance(): PDO
{
if (empty($this->slaveConfig) || Db::transactionsActive()) {
return $this->masterInstance();
}
if ($this->slaveConfig['cds'] == $this->cds) {
return $this->masterInstance();
}
return $this->connections()->get($this->slaveConfig, false);
}
/**
* @return \Kiri\Pool\Connection
* @throws Exception
*/
private function connections(): \Kiri\Pool\Connection
{
return Kiri::getDi()->get(\Kiri\Pool\Connection::class);
}
/**
* @return $this
* @throws Exception
*/
public function beginTransaction(): static
{
$this->connections()->beginTransaction($this->cds);
return $this;
}
/**
* @return $this|bool
* @throws Exception
*/
public function inTransaction(): bool|static
{
return $this->connections()->inTransaction($this->cds);
}
/**
* @throws Exception
* 事务回滚
*/
public function rollback()
{
$this->connections()->rollback($this->cds);
$this->release();
}
/**
* @throws Exception
* 事务提交
*/
public function commit()
{
$this->connections()->commit($this->cds);
$this->release();
}
/**
* @param $sql
* @return PDO
* @throws Exception
*/
public function refresh($sql): PDO
{
if ($this->isWrite($sql)) {
$instance = $this->masterInstance();
} else {
$instance = $this->slaveInstance();
}
return $instance;
}
/**
* @param null $sql
* @param array $attributes
* @return Command
* @throws Exception
*/
public function createCommand($sql = null, array $attributes = []): Command
{
$command = new Command(['db' => $this, 'sql' => $sql]);
return $command->bindValues($attributes);
}
/**
*
* 回收链接
* @throws
*/
public function release()
{
if (!Kiri::isWorker() && !Kiri::isProcess()) {
$this->clear_connection();
return;
}
$connections = $this->connections();
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
}
/**
* @throws Exception
*/
public function recovery()
{
$connections = $this->connections();
$connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false);
}
/**
*
* 回收链接
* @throws
*/
public function clear_connection()
{
$connections = $this->connections();
$connections->connection_clear($this->cds, true);
$connections->connection_clear($this->slaveConfig['cds'], false);
}
/**
* @throws Exception
*/
public function disconnect()
{
$connections = $this->connections();
$connections->disconnect($this->cds, true);
$connections->disconnect($this->slaveConfig['cds'], false);
}
}
-113
View File
@@ -1,113 +0,0 @@
<?php
declare(strict_types=1);
namespace Database;
use Annotation\Inject;
use Exception;
use Kiri\Abstracts\Config;
use Kiri\Abstracts\Providers;
use Kiri\Application;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Server\Events\OnWorkerStart;
/**
* Class DatabasesProviders
* @package Database
*/
class DatabasesProviders extends Providers
{
private array $_pooLength = ['min' => 0, 'max' => 1];
/**
* @var EventProvider
*/
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
/**
* @param Application $application
* @throws Exception
*/
public function onImport(Application $application)
{
$application->set('db', $this);
$this->_pooLength = Config::get('databases.pool', ['min' => 0, 'max' => 1]);
$this->eventProvider->on(OnWorkerStart::class, [$this, 'createPool']);
}
/**
* @param $name
* @return Connection
* @throws ConfigException
* @throws Exception
*/
public function get($name): Connection
{
$config = $this->_settings($this->getConfig($name));
return Kiri::getDi()->get(Connection::class)->configure($config);
}
/**
* @throws ConfigException
* @throws Exception
*/
public function createPool(OnWorkerStart $onWorkerStart)
{
$databases = Config::get('databases.connections', []);
if (empty($databases)) {
return;
}
$connection = Kiri::getDi()->get(Connection::class);
foreach ($databases as $database) {
/** @var Connection $connection */
$connection->configure($database)->fill();
}
}
/**
* @param $database
* @return array
*/
private function _settings($database): array
{
return [
'class' => Connection::class,
'id' => $database['id'],
'cds' => $database['cds'],
'username' => $database['username'],
'password' => $database['password'],
'tablePrefix' => $database['tablePrefix'],
'database' => $database['database'],
'maxNumber' => $this->_pooLength['max'],
'minNumber' => $this->_pooLength['min'],
'charset' => $database['charset'] ?? 'utf8mb4',
'slaveConfig' => $database['slaveConfig']
];
}
/**
* @param $name
* @return mixed
* @throws ConfigException
*/
public function getConfig($name): mixed
{
return Config::get('databases.connections.' . $name, null, true);
}
}
-365
View File
@@ -1,365 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 15:40
*/
declare(strict_types=1);
namespace Database;
use Database\Affair\BeginTransaction;
use Database\Affair\Commit;
use Database\Affair\Rollback;
use Database\Traits\QueryTrait;
use Exception;
use Kiri\Abstracts\Config;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
/**
* Class Db
* @package Database
*/
class Db implements ISqlBuilder
{
use QueryTrait;
private static bool $_inTransaction = false;
/**
* @return bool
*/
public static function transactionsActive(): bool
{
return static::$_inTransaction === true;
}
/**
* @throws Exception
*/
public static function beginTransaction()
{
if (!static::transactionsActive()) {
di(EventDispatch::class)->dispatch(new BeginTransaction());
}
static::$_inTransaction = true;
}
/**
* @throws Exception
*/
public static function commit()
{
if (static::transactionsActive()) {
di(EventDispatch::class)->dispatch(new Commit());
}
static::$_inTransaction = false;
}
/**
* @throws Exception
*/
public static function rollback()
{
if (static::transactionsActive()) {
di(EventDispatch::class)->dispatch(new Rollback());
}
static::$_inTransaction = false;
}
/**
* @param $table
*
* @return static
*/
public static function table($table): Db|static
{
$connection = new Db();
$connection->from($table);
return $connection;
}
/**
* @param string $column
* @param string $alias
* @return string
*/
public static function any_value(string $column, string $alias = ''): string
{
if (empty($alias)) {
$alias = $column . '_any_value';
}
return 'ANY_VALUE(' . $column . ') as ' . $alias;
}
/**
* @param string $column
* @return string
*/
public static function increment(string $column): string
{
return '+ ' . $column;
}
/**
* @param string $column
* @return string
*/
public static function decrement(string $column): string
{
return '- ' . $column;
}
/**
* @param Connection|null $connection
* @return mixed
* @throws Exception
*/
public function get(Connection $connection = NULL): mixed
{
$connection = static::getDefaultConnection($connection);
return $connection->createCommand(SqlBuilder::builder($this)->one())
->all();
}
/**
* @param $column
* @return string
*/
public static function raw($column): string
{
return '`' . $column . '`';
}
/**
* @param Connection|null $connection
* @return mixed
* @throws Exception
*/
public function find(Connection $connection = NULL): mixed
{
$connection = static::getDefaultConnection($connection);
return $connection->createCommand(SqlBuilder::builder($this)->all())
->one();
}
/**
* @param Connection|NULL $connection
* @return bool|int
* @throws Exception
*/
public function count(Connection $connection = NULL): bool|int
{
$connection = static::getDefaultConnection($connection);
return $connection->createCommand(SqlBuilder::builder($this)->count())
->exec();
}
/**
* @param Connection|NULL $connection
* @return bool|int
* @throws Exception
*/
public function exists(Connection $connection = NULL): bool|int
{
$connection = static::getDefaultConnection($connection);
return $connection->createCommand(SqlBuilder::builder($this)->one())
->fetchColumn();
}
/**
* @param string $sql
* @param array $attributes
* @param Connection|null $connection
* @return array|bool|int|string|null
* @throws Exception
*/
public static function findAllBySql(string $sql, array $attributes = [], Connection $connection = NULL): int|bool|array|string|null
{
$connection = static::getDefaultConnection($connection);
return $connection->createCommand($sql, $attributes)->all();
}
/**
* @param string $sql
* @param array $attributes
* @param Connection|NULL $connection
* @return string|array|bool|int|null
* @throws Exception
*/
public static function findBySql(string $sql, array $attributes = [], Connection $connection = NULL): string|array|bool|int|null
{
$connection = static::getDefaultConnection($connection);
return $connection->createCommand($sql, $attributes)->one();
}
/**
* @param string $field
* @return array|null
* @throws Exception
*/
public function values(string $field): ?array
{
$data = $this->get();
if (empty($data) || empty($field)) {
return NULL;
}
$first = current($data);
if (!isset($first[$field])) {
return NULL;
}
return array_column($data, $field);
}
/**
* @param $field
* @return mixed
* @throws Exception
*/
public function value($field): mixed
{
$data = $this->find();
if (!empty($field) && isset($data[$field])) {
return $data[$field];
}
return $data;
}
/**
* @param Connection|null $connection
* @return bool|int
* @throws ConfigException
* @throws Exception
*/
public function delete(?Connection $connection = null): bool|int
{
$connection = static::getDefaultConnection($connection);
return $connection->createCommand($connection->getBuild()->builder($this))->delete();
}
/**
* @param string $table
* @param null $connection
* @return bool|int
* @throws ConfigException
* @throws Exception
*/
public static function drop(string $table, $connection = null): bool|int
{
$connection = static::getDefaultConnection($connection);
$sprint = sprintf('DROP TABLE `%s`.`%s`', $connection->database, $table);
return $connection->createCommand($sprint)->delete();
}
/**
* @param string $table
* @param null $connection
* @return bool|int
* @throws Exception
*/
public static function truncate(string $table, $connection = null): bool|int
{
$connection = static::getDefaultConnection($connection);
$sprint = sprintf('TRUNCATE `%s`.`%s`', $connection->database, $table);
return $connection->createCommand($sprint)->exec();
}
/**
* @param string $table
* @param Connection|NULL $connection
* @return mixed
* @throws ConfigException
* @throws Exception
*/
public static function showCreateSql(string $table, Connection $connection = NULL): mixed
{
$connection = static::getDefaultConnection($connection);
$sprint = sprintf('SHOW CREATE TABLE `%s`.`%s`', $connection->database, $table);
return $connection->createCommand($sprint)->one();
}
/**
* @param string $table
* @param Connection|NULL $connection
* @return bool|int|null
* @throws ConfigException
* @throws Exception
*/
public static function desc(string $table, Connection $connection = NULL): bool|int|null
{
$connection = static::getDefaultConnection($connection);
$sprint = sprintf('SHOW FULL FIELDS FROM `%s`.`%s`', $connection->database, $table);
return $connection->createCommand($sprint)->all();
}
/**
* @param string $table
* @param Connection|NULL $connection
* @return mixed
* @throws Exception
*/
public static function show(string $table, Connection $connection = NULL): mixed
{
if (empty($table)) {
return null;
}
$connection = static::getDefaultConnection($connection);
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
return $connection->createCommand((new Query())
->select('*')
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
->where(['REFERENCED_TABLE_NAME' => $table])
->getSql())->one();
}
/**
* @param null|Connection $connection
* @param null $name
* @return mixed
* @throws ConfigException
* @throws Exception
*/
public static function getDefaultConnection(?Connection $connection, $name = null): Connection
{
if ($connection instanceof Connection) {
return $connection;
}
$databases = Config::get('databases.connections', []);
if (empty($databases) || !is_array($databases)) {
throw new Exception('Please configure the database link.');
}
if (!empty($name)) {
if (!isset($databases[$name])) {
throw new Exception('Please configure the database link.');
}
return $databases[$name];
}
return current($databases);
}
}
-39
View File
@@ -1,39 +0,0 @@
<?php
declare(strict_types=1);
namespace Database;
use Exception;
use Database\Traits\HasBase;
/**
* Class HasCount
* @package Database
*/
class HasCount extends HasBase
{
/**
* @param $name
* @param $arguments
* @return ActiveQuery
* @throws Exception
*/
public function __call($name, $arguments): mixed
{
if (method_exists($this, $name)) {
return call_user_func([$this, $name], ...$arguments);
}
return $this->_relation->getQuery($this->model::className())->$name(...$arguments);
}
/**
* @return array|null|ModelInterface
* @throws Exception
*/
public function get(): array|ModelInterface|null
{
return $this->_relation->count($this->model::className(), $this->value);
}
}
-44
View File
@@ -1,44 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 13:58
*/
declare(strict_types=1);
namespace Database;
use Exception;
use Database\Traits\HasBase;
/**
* Class HasMany
* @package Database
*
* @method with($name)
*/
class HasMany extends HasBase
{
/**
* @param $name
* @param $arguments
* @return ActiveQuery
*/
public function __call($name, $arguments): mixed
{
if (method_exists($this, $name)) {
return call_user_func([$this, $name], ...$arguments);
}
return $this->_relation->getQuery($this->model::className())->$name(...$arguments);
}
/**
* @return array|null|ModelInterface
* @throws Exception
*/
public function get(): array|ModelInterface|null
{
return $this->_relation->get($this->model::className(), $this->value);
}
}
-45
View File
@@ -1,45 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 13:47
*/
declare(strict_types=1);
namespace Database;
use Exception;
use Database\Traits\HasBase;
/**
* Class HasOne
* @package Database
* @internal Query
*/
class HasOne extends HasBase
{
/**
* @param $name
* @param $arguments
* @return ActiveQuery
*/
public function __call($name, $arguments): mixed
{
if (method_exists($this, $name)) {
return call_user_func([$this, $name], ...$arguments);
}
$this->_relation->getQuery($this->model::className())->$name(...$arguments);
return $this;
}
/**
* @return array|null|ModelInterface
* @throws Exception
*/
public function get(): array|ModelInterface|null
{
return $this->_relation->first($this->model::className(), $this->value);
}
}
-419
View File
@@ -1,419 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:39
*/
declare(strict_types=1);
namespace Database;
use Database\Base\Getter;
use Database\Traits\HasBase;
use Exception;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use Kiri\ToArray;
use ReflectionException;
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
/**
* Class Orm
* @package Database
*
* @property $attributes
* @property-read $oldAttributes
*/
class Model extends Base\Model
{
/**
* @param string $column
* @param int $value
* @return ModelInterface|false
* @throws Exception
*/
public function increment(string $column, int $value): bool|ModelInterface
{
if (!$this->mathematics([$column => $value], '+')) {
return false;
}
$this->{$column} += $value;
return $this->refresh();
}
/**
* @param string $column
* @param int $value
* @return ModelInterface|false
* @throws Exception
*/
public function decrement(string $column, int $value): bool|ModelInterface
{
if (!$this->mathematics([$column => $value], '-')) {
return false;
}
$this->{$column} -= $value;
return $this->refresh();
}
/**
* @param array $columns
* @return ModelInterface|false
* @throws Exception
*/
public function increments(array $columns): bool|static
{
if (!$this->mathematics($columns, '+')) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key += $attribute;
}
return $this;
}
/**
* @param array $columns
* @return ModelInterface|false
* @throws Exception
*/
public function decrements(array $columns): bool|static
{
if (!$this->mathematics($columns, '-')) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key -= $attribute;
}
return $this;
}
/**
* @param array $condition
* @param array $attributes
* @return bool|ModelInterface
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public static function findOrCreate(array $condition, array $attributes): bool|static
{
$logger = Kiri::app()->getLogger();
if (empty($attributes)) {
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
}
Db::beginTransaction();
/** @var static $select */
$select = static::query()->where($condition)->first();
if (empty($select)) {
$select = duplicate(static::class);
$select->attributes = $attributes;
if (!$select->save()) {
Db::rollback();
return $logger->addError($select->getLastError(), 'mysql');
}
}
Db::commit();
return $select;
}
/**
* @param array $condition
* @param array $attributes
* @return bool|static
* @throws Exception
*/
public static function createOrUpdate(array $condition, array $attributes = []): bool|static
{
$logger = Kiri::app()->getLogger();
if (empty($attributes)) {
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
}
Db::beginTransaction();
/** @var static $select */
$select = static::query()->where($condition)->first();
if (empty($select)) {
$select = duplicate(static::class);
}
$select->attributes = $attributes;
if (!$select->save()) {
Db::rollback();
$select = $logger->addError($select->getLastError(), 'mysql');
} else {
Db::commit();
}
return $select;
}
/**
* @param $action
* @param $columns
* @param null|array $condition
* @return array|bool|int|string|null
* @throws Exception
*/
private function mathematics($columns, $action, ?array $condition = null): int|bool|array|string|null
{
if (empty($condition)) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
}
$activeQuery = static::query()->where($condition);
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
if (is_bool($create)) {
return false;
}
return $this->getConnection()->createCommand($create[0], $create[1])->exec();
}
/**
* @param array $fields
* @return ModelInterface|bool
* @throws Exception
*/
public function update(array $fields): static|bool
{
return $this->save($fields);
}
/**
* @param array $data
* @return bool
* @throws Exception
*/
public static function inserts(array $data): bool
{
$result = false;
if (empty($data)) {
error('Insert data empty.', 'mysql');
} else {
$result = static::query()->batchInsert($data);
}
return $result;
}
/**
* @return bool
* @throws Exception
*/
public function delete(): bool
{
$conditions = $this->_oldAttributes;
if (empty($conditions)) {
return $this->addError("Delete condition do not empty.", 'mysql');
}
$primary = $this->getPrimary();
if (!empty($primary)) {
$conditions = [$primary => $this->getAttribute($primary)];
}
return static::deleteByCondition($conditions);
}
/**
* @param mixed $condition
* @param array $attributes
*
* @return bool
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function updateAll(mixed $condition, array $attributes = []): bool
{
$condition = static::query()->where($condition);
return $condition->batchUpdate($attributes);
}
/**
* @param $condition
* @return array|Collection
* @throws NotFindClassException
* @throws ReflectionException
*/
public static function get($condition): Collection|array
{
return static::query()->where($condition)->all();
}
/**
* @param $condition
* @param array $attributes
*
* @return array|Collection
* @throws Exception
*/
public static function findAll($condition, array $attributes = []): array|Collection
{
$query = static::query()->where($condition);
if (!empty($attributes)) {
$query->bindParams($attributes);
}
return $query->all();
}
/**
* @param $method
* @return mixed
* @throws Exception
*/
private function withRelation($method): mixed
{
$method = $this->getRelate($method);
if (empty($method)) {
return null;
}
$resolve = $this->{$method}();
if ($resolve instanceof HasBase) {
$resolve = $resolve->get();
}
if ($resolve instanceof ToArray) {
return $resolve->toArray();
} else if (is_object($resolve)) {
return get_object_vars($resolve);
} else {
return $resolve;
}
}
/**
* @return array
* @throws Exception
*/
public function toArray(): array
{
$data = $this->_attributes;
$lists = di(Getter::class)->getGetter(static::class);
foreach ($lists as $key => $item) {
$data[$key] = $this->{$item}($data[$key] ?? null);
}
return $this->withRelates($data);
}
/**
* @param $relates
* @return array
* @throws Exception
*/
private function withRelates($relates): array
{
if (empty($with = $this->getWith())) {
return $relates;
}
foreach ($with as $val) {
$relates[$val] = $this->withRelation($val);
}
return $relates;
}
/**
* @param string $modelName
* @param $foreignKey
* @param $localKey
* @return HasOne|ActiveQuery
* @throws Exception
*/
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
{
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
$relation = $this->getRelation();
return new HasOne($modelName, $foreignKey, $value, $relation);
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery|HasCount
* @throws Exception
*/
public function hasCount($modelName, $foreignKey, $localKey): ActiveQuery|HasCount
{
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
$relation = $this->getRelation();
return new HasCount($modelName, $foreignKey, $value, $relation);
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery|HasMany
* @throws Exception
*/
public function hasMany($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
{
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
$relation = $this->getRelation();
return new HasMany($modelName, $foreignKey, $value, $relation);
}
/**
* @param $modelName
* @param $foreignKey
* @param $localKey
* @return ActiveQuery|HasMany
* @throws Exception
*/
public function hasIn($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
{
if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key.");
}
$relation = $this->getRelation();
return new HasMany($modelName, $foreignKey, $value, $relation);
}
/**
* @return bool
* @throws Exception
*/
public function afterDelete(): bool
{
return TRUE;
}
/**
* @return bool
* @throws Exception
*/
public function beforeDelete(): bool
{
return TRUE;
}
}
-61
View File
@@ -1,61 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:39
*/
declare(strict_types=1);
namespace Database;
/**
* Interface ModelInterface
* @package Database
*/
interface ModelInterface
{
/**
* @param $param
* @param null $db
* @return ModelInterface
*/
public static function findOne($param, $db = NULL): mixed;
/**
* @param string|int $param
* @return ModelInterface
*/
public static function find(string|int $param): mixed;
/**
* @param array $data
* @return static
*/
public static function populate(array $data): static;
/**
* @return ActiveQuery
* return a sql queryBuilder
*/
public static function query(): ActiveQuery;
/**
* @return string
*/
public function getTable(): string;
/**
* @return Connection
*/
public function getConnection(): Connection;
}
-406
View File
@@ -1,406 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-18
* Time: 17:22
*/
declare(strict_types=1);
namespace Database\Mysql;
use Database\Connection;
use Database\SqlBuilder;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
use Kiri\Core\Json;
/**
* Class Columns
* @package Database\Mysql
*/
class Columns extends Component
{
/**
* @var array
* field types
*/
private array $columns = [];
/**
* @var Connection
* Mysql client
*/
public Connection $db;
/**
* @var string
* tableName
*/
public string $table = '';
/**
* @var array
* field primary key
*/
private array $_primary = [];
/**
* @var array
* by mysql field auto_increment
*/
private array $_auto_increment = [];
private array $_fields = [];
/**
* @param string $table
* @return $this
* @throws Exception
*/
public function table(string $table): static
{
$this->structure($this->table = $table);
return $this;
}
/**
* @return string
*/
public function getTable(): string
{
return $this->table;
}
/**
* @param $key
* @param $val
* @return mixed
* @throws Exception
*/
public function fieldFormat($key, $val): mixed
{
return $this->encode($val, $this->get_fields($key));
}
/**
* @param $data
* @return array
* @throws
*/
public function populate($data): array
{
$column = $this->get_fields();
foreach ($data as $key => $val) {
if (!isset($column[$key])) {
continue;
}
$data[$key] = $this->decode($val, $column[$key]);
}
return $data;
}
/**
* @param $val
* @param null $format
* @return mixed
*/
public function decode($val, $format = null): mixed
{
if (empty($format) || $val === null) {
return $val;
}
$format = strtolower($format);
if ($this->isInt($format)) {
return (int)$val;
} else if ($this->isJson($format)) {
return Json::decode($val, true);
} else if ($this->isFloat($format)) {
return (float)$val;
} else {
return stripslashes($val);
}
}
/**
* @param string $name
* @param $value
* @return mixed
* @throws Exception
*/
public function _decode(string $name, $value): mixed
{
return $this->decode($value, $this->get_fields($name));
}
/**
* @param $val
* @param null $format
* @return float|bool|int|string
* @throws Exception
*/
public function encode($val, $format = null): float|bool|int|string
{
if (empty($format)) {
return $val;
}
$format = strtolower($format);
if ($this->isInt($format)) {
return (int)$val;
} else if ($this->isJson($format)) {
return Json::encode($val);
} else if ($this->isFloat($format)) {
return (float)$val;
} else {
return addslashes($val);
}
}
/**
* @param $format
* @return bool
*/
#[Pure] public function isInt($format): bool
{
return in_array($format, ['int', 'bigint', 'tinyint', 'smallint', 'mediumint']);
}
/**
* @param $format
* @return bool
*/
#[Pure] public function isFloat($format): bool
{
return in_array($format, ['float', 'double', 'decimal']);
}
/**
* @param $format
* @return bool
*/
#[Pure] public function isJson($format): bool
{
return in_array($format, ['json']);
}
/**
* @param $format
* @return bool
*/
#[Pure] public function isString($format): bool
{
return in_array($format, ['varchar', 'char', 'text', 'longtext', 'tinytext', 'mediumtext']);
}
/**
* @return array
* @throws
*/
public function format(): array
{
return $this->columns('Default', 'Field');
}
/**
* @return mixed
* @throws Exception
*/
public function getFields(): array
{
if (empty($this->_fields)) {
$this->structure($this->table);
}
return $this->_fields[$this->table];
}
/**
* @param string $name
* @return bool
* @throws Exception
*/
public function hasField(string $name): bool
{
return array_key_exists($name, $this->getFields());
}
/**
* @return int|string|null
* @throws Exception
*/
public function getAutoIncrement(): int|string|null
{
return $this->_auto_increment[$this->table] ?? null;
}
/**
* @return array|null|string
*
* @throws Exception
*/
public function getPrimaryKeys(): array|string|null
{
if (isset($this->_auto_increment[$this->table])) {
return $this->_auto_increment[$this->table];
}
return $this->_primary[$this->table] ?? null;
}
/**
* @return array|null|string
*
* @throws Exception
*/
#[Pure] public function getFirstPrimary(): array|string|null
{
if (isset($this->_auto_increment[$this->table])) {
return $this->_auto_increment[$this->table];
}
if (isset($this->_primary[$this->table])) {
return current($this->_primary[$this->table]);
}
return null;
}
/**
* @param $name
* @param null $index
* @return array
* @throws Exception
*/
private function columns($name, $index = null): array
{
if (empty($index)) {
return array_column($this->getColumns(), $name);
} else {
return array_column($this->getColumns(), $name, $index);
}
}
/**
* @return array|static
* @throws Exception
*/
private function getColumns(): array|static
{
return $this->structure($this->getTable());
}
/**
* @param $table
* @return array|Columns
* @throws Exception
*/
private function structure($table): array|static
{
if (!isset($this->columns[$table]) || empty($this->columns[$table])) {
$column = $this->db->createCommand(SqlBuilder::builder(null)->columns($table))->all();
if (empty($column)) {
throw new Exception("The table " . $table . " not exists.");
}
return $this->columns[$table] = $this->resolve($column, $table);
}
return $this->columns[$table];
}
/**
* @param array $column
* @param $table
* @return array
*/
private function resolve(array $column, $table): array
{
foreach ($column as $key => $item) {
$this->addPrimary($item, $table);
$column[$key]['Type'] = $this->clean($item['Type']);
}
$this->_fields[$table] = array_column($column, 'Default', 'Field');
return $column;
}
/**
* @param $item
* @param $table
*/
private function addPrimary($item, $table)
{
if (!isset($this->_primary[$table])) {
$this->_primary[$table] = [];
}
if ($item['Key'] === 'PRI') {
$this->_primary[$table][] = $item['Field'];
}
$this->addIncrement($item, $table);
}
/**
* @param $item
* @param $table
*/
private function addIncrement($item, $table)
{
if ($item['Extra'] !== 'auto_increment') {
return;
}
$this->_auto_increment[$table] = $item['Field'];
}
/**
* @param $type
* @return string
*/
public function clean($type): string
{
if (!str_contains($type, ')')) {
return $type;
}
$replace = preg_replace('/\(\d+(,\d+)?\)(\s+\w+)*/', '', $type);
if (str_contains($replace, ' ')) {
$replace = explode(' ', $replace)[1];
}
return $replace;
}
/**
* @param null $field
* @return array|string|null
* @throws Exception
*/
public function get_fields($field = null): array|string|null
{
$fields = $this->getAllField();
if (empty($field)) {
return $fields;
}
if (isset($fields[$field])) {
return strtolower($fields[$field]);
}
return null;
}
/**
* @return array
* @throws Exception
*/
public function getAllField(): array
{
return $this->columns('Type', 'Field');
}
}
-299
View File
@@ -1,299 +0,0 @@
<?php
namespace Database\Mysql;
use Exception;
use Kiri\Abstracts\Logger;
use Kiri\Context;
use Kiri\Kiri;
use Kiri\Pool\StopHeartbeatCheck;
use PDOStatement;
use Swoole\Timer;
/**
*
*/
class PDO implements StopHeartbeatCheck
{
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
private ?\PDO $pdo = null;
private int $_transaction = 0;
private int $_timer = -1;
private int $_last = 0;
/**
* @param string $dbname
* @param string $cds
* @param string $username
* @param string $password
* @param string $chatset
* @throws
*/
public function __construct(public string $dbname, public string $cds,
public string $username, public string $password, public string $chatset = 'utf8mb4')
{
}
public function init()
{
$this->heartbeat_check();
}
/**
* @return bool
*/
public function inTransaction(): bool
{
return $this->_transaction > 0;
}
/**
*
*/
public function heartbeat_check(): void
{
if (env('state', 'start') == 'exit') {
return;
}
if ($this->_timer === -1 && Context::inCoroutine()) {
$this->_timer = Timer::tick(1000, function () {
try {
if (env('state', 'start') == 'exit') {
Kiri::getDi()->get(Logger::class)->critical('timer end');
$this->stopHeartbeatCheck();
}
if (time() - $this->_last > 10 * 60) {
$this->stopHeartbeatCheck();
$this->pdo = null;
}
} catch (\Throwable $throwable) {
error($throwable);
}
});
}
}
/**
*
*/
public function stopHeartbeatCheck(): void
{
if ($this->_timer > -1) {
Timer::clear($this->_timer);
}
$this->_timer = -1;
}
/**
*
*/
public function beginTransaction()
{
if ($this->_transaction == 0) {
$this->_pdo()->beginTransaction();
}
$this->_transaction++;
}
/**
*
*/
public function commit()
{
if ($this->_transaction == 0) {
$this->_pdo()->commit();
}
$this->_transaction--;
}
/**
*
*/
public function rollback()
{
if ($this->_transaction == 0) {
$this->_pdo()->rollBack();
}
$this->_transaction--;
}
/**
* @param string $sql
* @param array $params
* @return bool|array|null
* @throws Exception
*/
public function fetchAll(string $sql, array $params = []): bool|null|array
{
$pdo = $this->queryPrev($sql, $params);
$result = $pdo->fetchAll(\PDO::FETCH_ASSOC);
$pdo->closeCursor();
return $result;
}
/**
* @param string $sql
* @param array $params
* @return bool|array|null
* @throws Exception
*/
public function fetch(string $sql, array $params = []): bool|null|array
{
$pdo = $this->queryPrev($sql, $params);
$result = $pdo->fetch(\PDO::FETCH_ASSOC);
$pdo->closeCursor();
return $result;
}
/**
* @param string $sql
* @param array $params
* @return bool|array|null
* @throws \Exception
*/
public function fetchColumn(string $sql, array $params = []): bool|null|array
{
$pdo = $this->queryPrev($sql, $params);
$result = $pdo->fetchColumn(\PDO::FETCH_ASSOC);
$pdo->closeCursor();
return $result;
}
/**
* @param string $sql
* @param array $params
* @return int
* @throws Exception
*/
public function count(string $sql, array $params = []): int
{
$pdo = $this->queryPrev($sql, $params);
$result = $pdo->rowCount();
$pdo->closeCursor();
return $result;
}
/**
* @param string $sql
* @param array $params
* @return PDOStatement
* @throws Exception
*/
private function queryPrev(string $sql, array $params = []): PDOStatement
{
$this->_last = time();
try {
if (($statement = $this->_pdo()->query($sql)) === false) {
throw new Exception($this->_pdo()->errorInfo()[1]);
}
return $this->bindValue($statement, $params);
} catch (\PDOException | \Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->pdo = null;
return $this->queryPrev($sql, $params);
}
throw new Exception($throwable->getMessage());
}
}
/**
* @param PDOStatement $statement
* @param array $params
* @return PDOStatement
*/
private function bindValue(PDOStatement $statement, array $params = []): PDOStatement
{
if (empty($params)) return $statement;
foreach ($params as $key => $param) {
$statement->bindValue($key, $param);
}
return $statement;
}
/**
* @param string $sql
* @param array $params
* @return int
* @throws Exception
*/
public function execute(string $sql, array $params = []): int
{
$this->_last = time();
$pdo = $this->_pdo();
if (!(($prepare = $pdo->prepare($sql)) instanceof PDOStatement)) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
}
if ($prepare->execute($params) === false) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
}
$result = (int)$pdo->lastInsertId();
$prepare->closeCursor();
if ($result == 0) {
return true;
}
return $result;
}
/**
* @return \PDO
*/
public function _pdo(): \PDO
{
if ($this->_timer === -1) {
$this->heartbeat_check();
}
if (!($this->pdo instanceof \PDO)) {
$this->pdo = $this->newClient();
}
return $this->pdo;
}
/**
* @return \PDO
*/
private function newClient(): \PDO
{
$link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
\PDO::ATTR_TIMEOUT => 60,
\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->chatset
]);
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
return $link;
}
}
-36
View File
@@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Mysql;
use Exception;
use Kiri\Abstracts\Component;
use Database\Connection;
/**
* Class Schema
* @package Database\Mysql
*/
class Schema extends Component
{
/** @var ?Connection */
public ?Connection $db = null;
/** @var ?Columns $_column*/
private ?Columns $_column = null;
/**
* @return Columns|null
* @throws Exception
*/
public function getColumns(): ?Columns
{
if ($this->_column === null) {
$this->_column = new Columns(['db' => $this->db]);
}
return $this->_column;
}
}
-29
View File
@@ -1,29 +0,0 @@
<?php
declare(strict_types=1);
namespace Database\Orm;
use Database\Traits\Builder;
use Exception;
/**
* Trait Condition
* @package Database\Orm
*/
trait Condition
{
use Builder;
/**
* @param $query
* @return string
* @throws Exception
*/
public function getWhere($query): string
{
return $this->where($query);
}
}
-204
View File
@@ -1,204 +0,0 @@
<?php
declare(strict_types=1);
namespace Database;
use Closure;
use Exception;
use Kiri\Abstracts\Component;
/**
* Class Pagination
* @package Database
*/
class Pagination extends Component
{
/** @var ActiveQuery */
private ActiveQuery $activeQuery;
/** @var int 从第几个开始查 */
private int $_offset = 0;
/** @var int 每页数量 */
private int $_limit = 100;
/** @var int 最大查询数量 */
private int $_max = 0;
/** @var int 当前已查询数量 */
private int $_length = 0;
/** @var Closure */
private Closure $_callback;
/**
* PaginationIteration constructor.
* @param ActiveQuery $activeQuery
* @param array $config
* @throws Exception
*/
public function __construct(ActiveQuery $activeQuery, array $config = [])
{
parent::__construct($config);
$this->activeQuery = $activeQuery;
}
public function clean()
{
unset($this->activeQuery, $this->_callback, $this->_group);
$this->_offset = 0;
$this->_limit = 100;
$this->_max = 0;
$this->_length = 0;;
}
/**
* recover class by clone
*/
public function __clone()
{
$this->clean();
}
/**
* @param array|Closure $callback
* @throws Exception
*/
public function setCallback(array|Closure $callback)
{
if (!is_callable($callback, true)) {
throw new Exception('非法回调函数~');
}
$this->_callback = $callback;
}
/**
* @param int $number
* @return Pagination
*/
public function setOffset(int $number): static
{
if ($number < 0) {
$number = 0;
}
$this->_offset = $number;
return $this;
}
/**
* @param int $number
* @return Pagination
*/
public function setLimit(int $number): static
{
if ($number < 1) {
$number = 100;
} else if ($number > 5000) {
$number = 5000;
}
$this->_limit = $number;
return $this;
}
/**
* @param int $number
* @return Pagination
*/
public function setMax(int $number): static
{
if ($number < 0) {
return $this;
}
$this->_max = $number;
return $this;
}
/**
* @param array $param
* @return void
* @throws Exception
*/
public function plunk(array $param = [])
{
$this->loop($param);
}
/**
* 轮训
* @param $param
* @return array
* @throws Exception
*/
public function loop($param): array
{
if ($this->_max > 0 && $this->_length >= $this->_max) {
return $this->output();
}
[$length, $data] = $this->get();
$this->executed($data, $param);
unset($data);
if ($length < $this->_limit) {
return $this->output();
}
return $this->loop($param);
}
/**
* @return array
*/
public function output(): array
{
return [];
}
/**
* @param $data
* @param $param
* @throws Exception
*/
private function executed($data, $param): void
{
try {
call_user_func($this->_callback, $data, $param);
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
}
}
/**
* @return array|Collection
*/
private function get(): Collection|array
{
if ($this->_max > 0 && $this->_length + $this->_limit > $this->_max) {
$this->_limit = $this->_length + $this->_limit - $this->_max;
}
$data = $this->activeQuery->limit($this->_offset, $this->_limit)->get();
$this->_offset += $this->_limit;
if (is_array($data)) {
$size = count($data);
} else {
$size = $data->size();
}
$this->_length += $size;
return [$size, $data];
}
}
-54
View File
@@ -1,54 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/6/27 0027
* Time: 17:49
*/
declare(strict_types=1);
namespace Database;
use Database\Traits\QueryTrait;
use Exception;
/**
* Class Query
* @package Database
*/
class Query implements ISqlBuilder
{
use QueryTrait;
/**
* @throws Exception
*/
public function __construct()
{
$this->builder = SqlBuilder::builder($this);
}
/**
* @return string
* @throws Exception
*/
public function getSql(): string
{
return $this->builder->get();
}
/**
* @return string
* @throws Exception
*/
public function getCondition(): string
{
return $this->builder->getCondition();
}
}
-111
View File
@@ -1,111 +0,0 @@
<?php
declare(strict_types=1);
namespace Database;
use Exception;
use Kiri\Abstracts\Component;
/**
* Class Relation
* @package Kiri\db
*/
class Relation extends Component
{
private array $_relations = [];
/** @var ActiveQuery[] $_query */
private array $_query = [];
/**
* @param string $identification
* @param ActiveQuery $query
* @return $this
*/
public function bindIdentification(string $identification, ActiveQuery $query): static
{
$this->_query[$identification] = $query;
return $this;
}
/**
* @param string $name
* @return ActiveQuery|null
*/
public function getQuery(string $name): ?ActiveQuery
{
return $this->_query[$name] ?? null;
}
/**
* @param string $identification
* @param $localValue
* @return mixed
* @throws Exception
*/
public function first(string $identification, $localValue): mixed
{
$_identification = $identification . '_first_' . $localValue;
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
}
$activeModel = $this->_query[$identification]->first();
if (empty($activeModel)) {
return null;
}
return $this->_relations[$_identification] = $activeModel;
}
/**
* @param string $identification
* @param $localValue
* @return mixed
* @throws Exception
*/
public function count(string $identification, $localValue): mixed
{
$_identification = $identification . '_count_' . $localValue;
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
}
$activeModel = $this->_query[$identification]->count();
if (empty($activeModel)) {
return null;
}
return $this->_relations[$_identification] = $activeModel;
}
/**
* @param string $identification
* @param $localValue
* @return mixed
*/
public function get(string $identification, $localValue): mixed
{
if (is_array($localValue)) {
$_identification = $identification . '_get_' . implode('_', $localValue);
} else {
$_identification = $identification . '_get_' . $localValue;
}
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
}
$activeModel = $this->_query[$identification]->get();
if (empty($activeModel)) {
return null;
}
return $this->_relations[$_identification] = $activeModel;
}
}
-375
View File
@@ -1,375 +0,0 @@
<?php
namespace Database;
use Database\Traits\Builder;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
/**
* Class SqlBuilder
* @package Database
*/
class SqlBuilder extends Component
{
use Builder;
public ActiveQuery|Query|null $query;
/**
* @param ISqlBuilder|null $query
* @return $this
* @throws Exception
*/
public static function builder(ISqlBuilder|null $query): static
{
return new static(['query' => $query]);
}
/**
* @return string
* @throws Exception
*/
public function getCondition(): string
{
return $this->conditionToString();
}
/**
* @param array $compiler
* @return string
* @throws Exception
*/
public function hashCompiler(array $compiler): string
{
return $this->where($compiler);
}
/**
* @param array $attributes
* @return bool|array
* @throws Exception
*/
public function update(array $attributes): bool|array
{
[$string, $array] = $this->builderParams($attributes);
return $this->__updateBuilder($string, $array);
}
/**
* @param array $attributes
* @param string $opera
* @return bool|array
* @throws Exception
*/
public function mathematics(array $attributes, string $opera = '+'): bool|array
{
$string = [];
foreach ($attributes as $attribute => $value) {
$string[] = $attribute . '=' . $attribute . $opera . $value;
}
return $this->__updateBuilder($string, []);
}
/**
* @param array $string
* @param array $params
* @return array|bool
* @throws Exception
*/
private function __updateBuilder(array $string, array $params): array|bool
{
if (empty($string)) {
return $this->addError('None data update.');
}
$condition = $this->conditionToString();
if (!empty($condition)) {
$condition = ' WHERE ' . $condition;
}
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
$update .= $this->builderLimit($this->query, false);
return [$update, $params];
}
/**
* @param array $attributes
* @param false $isBatch
* @return array
* @throws Exception
*/
public function insert(array $attributes, bool $isBatch = false): array
{
$update = 'INSERT INTO ' . $this->tableName();
if ($isBatch === false) {
$attributes = [$attributes];
}
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
$order = 0;
$keys = $params = [];
foreach ($attributes as $attribute) {
[$_keys, $params] = $this->builderParams($attribute, true, $params, $order);
$keys[] = implode(',', $_keys);
$order++;
}
return [$update . '(' . implode('),(', $keys) . ')', $params];
}
/**
* @return string
* @throws Exception
*/
public function delete(): string
{
$delete = sprintf('DELETE FROM %s ', $this->query->modelClass->getTable());
$this->query->from = null;
return $delete . ' WHERE ' . $this->_prefix(true);
}
/**
* @param $attributes
* @return array
*/
#[Pure] private function getFields($attributes): array
{
return array_keys(current($attributes));
}
/**
* @param array $attributes
* @param bool $isInsert
* @param array $params
* @param int $order
* @return array[]
* a=:b,
*/
#[Pure] private function builderParams(array $attributes, bool $isInsert = false, array $params = [], int $order = 0): array
{
$keys = [];
foreach ($attributes as $key => $value) {
if ($isInsert === true) {
$keys[] = ':' . $key . $order;
$params[$key . $order] = $value;
} else {
[$keys, $params] = $this->resolveParams($key, $value, $order, $params, $keys);
}
}
return [$keys, $params];
}
/**
* @param string $key
* @param mixed $value
* @param int $order
* @param array $params
* @param array $keys
* @return array
*/
private function resolveParams(string $key, mixed $value, int $order, array $params, array $keys): array
{
if (
str_starts_with($value, '+ ') ||
str_starts_with($value, '- ')
) {
$keys[] = $key . '=' . $key . ' ' . $value;
} else {
$params[$key . $order] = $value;
$keys[] = $key . '=:' . $key . $order;
}
return [$keys, $params];
}
/**
* @return string
* @throws Exception
*/
public function one(): string
{
$this->query->limit(0, 1);
if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable());
}
return $this->_prefix(true);
}
/**
* @return string
* @throws Exception
*/
public function all(): string
{
if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable());
}
return $this->_prefix(true);
}
/**
* @return string
* @throws Exception
*/
public function count(): string
{
if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable());
}
return $this->_prefix(false, true);
}
/**
* @param $table
* @return string
*/
public function columns($table): string
{
return 'SHOW FULL FIELDS FROM ' . $table;
}
/**
* @param bool $hasOrder
* @param bool $isCount
* @return string
* @throws Exception
*/
private function _prefix(bool $hasOrder = false, bool $isCount = false): string
{
$select = '';
if (!empty($this->query->from)) {
$select = $this->_selectPrefix($isCount);
}
$select = $this->_wherePrefix($select);
if (!empty($this->query->attributes) && is_array($this->query->attributes)) {
$select = strtr($select, $this->query->attributes);
}
if (!empty($this->query->group)) {
$select .= $this->builderGroup($this->query->group);
}
if ($hasOrder === true && !empty($this->query->order)) {
$select .= $this->builderOrder($this->query->order);
}
$sql = $select . $this->builderLimit($this->query);
if ($this->query->lock) {
$sql .= ' FOR UPDATE';
}
return $sql;
}
/**
* @param $select
* @return string
* @throws Exception
*/
private function _wherePrefix($select): string
{
$condition = $this->conditionToString();
if (empty($condition)) {
return $select;
} else if (empty($select)) {
return $condition;
}
return sprintf('%s WHERE %s', $select, $condition);
}
/**
* @param bool $isCount
* @return string
* @throws Exception
*/
private function _selectPrefix(bool $isCount): string
{
$select = $this->builderSelect($this->query->select, $isCount) . ' FROM ' . $this->tableName();
if (!empty($this->query->alias)) {
$select .= $this->builderAlias($this->query->alias);
}
if (!empty($this->query->join)) {
$select .= $this->builderJoin($this->query->join);
}
return $select;
}
/**
* @param false $isCount
* @return string
* @throws Exception
*/
public function get(bool $isCount = false): string
{
if ($isCount === false) {
return $this->all();
}
return $this->count();
}
/**
* @return string
* @throws Exception
*/
public function truncate(): string
{
return sprintf('TRUNCATE %s', $this->tableName());
}
/**
* @return string
* @throws Exception
*/
private function conditionToString(): string
{
return $this->where($this->query->where);
}
/**
* @return string
* @throws Exception
*/
public function tableName(): string
{
if ($this->query->from instanceof \Closure) {
$this->query->from = sprintf('(%s)', $this->query->makeClosureFunction($this->query->from));
}
if ($this->query->from instanceof ActiveQuery) {
$this->query->from = sprintf('%s', SqlBuilder::builder($this->query->from)->get($this->query->from));
}
if (empty($this->query->from)) {
return $this->query->modelClass->getTable();
}
return $this->query->from;
}
}
-26
View File
@@ -1,26 +0,0 @@
<?php
namespace Database;
use Database\Model;
/**
*
*/
class TestModel extends Model
{
protected string $connection = '';
protected string $table = '';
public ?string $primary = '';
}
TestModel::query()->get();
-232
View File
@@ -1,232 +0,0 @@
<?php
namespace Database\Traits;
use Database\ActiveQuery;
use Database\Base\ConditionClassMap;
use Database\Condition\HashCondition;
use Database\Condition\OrCondition;
use Database\Query;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
/**
* Trait Builder
* @package Database\Traits
*/
trait Builder
{
/**
* @param $alias
* @return string
*/
private function builderAlias($alias): string
{
return " AS " . $alias;
}
/**
* @param $table
* @return string
* @throws Exception
*/
private function builderFrom($table): string
{
if ($table instanceof ActiveQuery) {
$table = '(' . $table->toSql() . ')';
}
return " FROM " . $table;
}
/**
* @param $join
* @return string
*/
#[Pure] private function builderJoin($join): string
{
if (!empty($join)) {
return ' ' . implode(' ', $join);
}
return '';
}
/**
* @param null $select
* @param bool $isCount
* @return string
*/
#[Pure] private function builderSelect($select = NULL, bool $isCount = false): string
{
if ($isCount) {
return "SELECT COUNT(*)";
}
if (empty($select)) {
return "SELECT *";
}
if (is_array($select)) {
return "SELECT " . implode(',', $select);
} else {
return "SELECT " . $select;
}
}
/**
* @param $group
* @return string
*/
private function builderGroup($group): string
{
if (empty($group)) {
return '';
}
return ' GROUP BY ' . $group;
}
/**
* @param $order
* @return string
*/
#[Pure] private function builderOrder($order): string
{
if (!empty($order)) {
return ' ORDER BY ' . implode(',', $order);
} else {
return '';
}
}
/**
* @param ActiveQuery|Query $query
* @param bool $hasLimit
* @return string
*/
#[Pure] private function builderLimit(ActiveQuery|Query $query, bool $hasLimit = true): string
{
if (!is_numeric($query->limit) || $query->limit < 1) {
return "";
}
if ($query->offset !== null && $hasLimit) {
return ' LIMIT ' . $query->offset . ',' . $query->limit;
}
return ' LIMIT ' . $query->limit;
}
/**
* @param $where
* @return string
* @throws Exception
*/
private function where($where): string
{
$_tmp = [];
if (empty($where)) return '';
if (is_string($where)) return $where;
foreach ($where as $key => $value) {
if (is_null($value)) continue;
$_value = $this->resolveCondition($key, $value, $_tmp);
if (empty($_value)) continue;
$_tmp[] = $_value;
}
if (!empty($_tmp)) {
return implode(' AND ', $_tmp);
}
return '';
}
/**
* @param $field
* @param $condition
* @param $_tmp
* @return string
* @throws NotFindClassException
* @throws ReflectionException|Exception
*/
private function resolveCondition($field, $condition, $_tmp): string
{
if (is_string($field)) {
$_value = sprintf('%s = \'%s\'', $field, $condition);
} else if (is_string($condition)) {
$_value = $condition;
} else {
$_value = $this->_arrayMap($condition, $_tmp);
}
return $_value;
}
/**
* @param $condition
* @param $array
* @return string
* @throws Exception
*/
private function _arrayMap($condition, $array): string
{
if (!isset($condition[0])) {
return implode(' AND ', $this->_hashMap($condition));
}
$stroppier = strtoupper($condition[0]);
if (str_contains($stroppier, 'OR')) {
if (!is_string($condition[2])) {
$condition[2] = $this->_hashMap($condition[2]);
}
$builder = Kiri::getDi()->get(OrCondition::class);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
$builder->oldParams = $array;
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
$class = $defaultConfig['class'];
unset($defaultConfig['class']);
$builder = Kiri::getDi()->get($class, [], $defaultConfig);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
} else {
$builder = Kiri::getDi()->get(HashCondition::class);
$builder->setValue($condition);
}
$array[] = $builder->builder();
return implode(' AND ', $array);
}
/**
* @param $condition
* @return array
*/
private function _hashMap($condition): array
{
$_array = [];
foreach ($condition as $key => $value) {
if (is_null($value)) continue;
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (!is_numeric($key)) {
$_array[] = sprintf('%s = %s', $key, $value);
} else {
$_array[] = $value;
}
}
return $_array;
}
}
-81
View File
@@ -1,81 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 15:47
*/
declare(strict_types=1);
namespace Database\Traits;
use Database\ModelInterface;
use Database\Collection;
use Database\Relation;
use Exception;
/**
* Class HasBase
* @package Database
*
* @include Query
*/
abstract class HasBase implements \Database\Traits\Relation
{
/** @var ModelInterface|Collection */
protected Collection|ModelInterface $data;
/**
* @var ModelInterface
*/
protected mixed $model;
protected mixed $value = [];
/** @var Relation $_relation */
protected Relation $_relation;
/**
* HasBase constructor.
* @param ModelInterface $model
* @param $primaryId
* @param $value
* @param Relation $relation
* @throws Exception
*/
public function __construct(mixed $model, $primaryId, $value, Relation $relation)
{
if (!class_exists($model)) {
throw new Exception('Model must implement ' . $model);
}
if (!in_array(ModelInterface::class, class_implements($model))) {
throw new Exception('Model must implement ' . $model);
}
if (is_array($value)) {
if (empty($value)) $value = [];
$_model = $model::query()->whereIn($primaryId, $value);
} else {
$_model = $model::query()->where(['t1.' . $primaryId => $value]);
}
$this->_relation = $relation->bindIdentification($model, $_model);
$this->model = $model;
$this->value = $value;
}
/**
* @param $name
* @return mixed
*/
public function __get($name): mixed
{
if (empty($this->value)) {
return null;
}
return $this->get();
}
}
-963
View File
@@ -1,963 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:56
*/
declare(strict_types=1);
namespace Database\Traits;
use Closure;
use Database\ActiveQuery;
use Database\Condition\MathematicsCondition;
use Database\ModelInterface;
use Database\Query;
use Database\SqlBuilder;
use Exception;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
/**
* Trait QueryTrait
* @package Database\Traits
*/
trait QueryTrait
{
public array $where = [];
public array $select = [];
public array $join = [];
public array $order = [];
public ?int $offset = NULL;
public ?int $limit = NULL;
public string $group = '';
public string|Closure|ActiveQuery|null $from = '';
public string $alias = 't1';
public array $filter = [];
public bool $lock = false;
public bool $ifNotWhere = false;
private SqlBuilder $builder;
/**
* @var ModelInterface|string|null
*/
public ModelInterface|string|null $modelClass;
/**
* clear
*/
public function clear()
{
$this->where = [];
$this->select = [];
$this->join = [];
$this->order = [];
$this->offset = NULL;
$this->limit = NULL;
$this->group = '';
$this->from = '';
$this->alias = 't1';
$this->filter = [];
}
/**
* @param string $column
* @param callable $callable
* @return $this
*/
public function when(string $column, callable $callable): static
{
$caseWhen = new When($column, $this);
call_user_func($callable, $caseWhen);
$this->where[] = $caseWhen->end();
return $this;
}
/**
* @param bool $lock
* @return $this
*/
public function lock(bool $lock): static
{
$this->lock = $lock;
return $this;
}
/**
* @param string $whereRaw
* @return QueryTrait
*/
public function whereRaw(string $whereRaw): static
{
if (empty($whereRaw)) {
return $this;
}
$this->where[] = $whereRaw;
return $this;
}
/**
* @param string|array|Closure $condition
* @param string|array|Closure $condition1
* @param string|array|Closure $condition2
* @return $this
* @throws NotFindClassException
* @throws ReflectionException
*/
public function whereIf(string|array|Closure $condition, string|array|Closure $condition1, string|array|Closure $condition2): static
{
if (!is_string($condition)) {
$condition = $this->makeClosureFunction($condition);
}
if (!is_string($condition1)) {
$condition1 = $this->makeClosureFunction($condition1);
}
if (!is_string($condition2)) {
$condition2 = $this->makeClosureFunction($condition2);
}
$this->where[] = 'IF(' . $condition . ', ' . $condition1 . ', ' . $condition2 . ')';
return $this;
}
/**
* @param $bool
* @return $this
*/
public function ifNotWhere($bool): static
{
$this->ifNotWhere = $bool;
return $this;
}
/**
* @return string
* @throws Exception
*/
public function getTable(): string
{
return $this->modelClass->getTable();
}
/**
* @param string $column
* @param string $value
* @return $this
*/
public function whereLocate(string $column, string $value): static
{
$this->where[] = 'LOCATE(' . $column . ',\'' . addslashes($value) . '\') > 0';
return $this;
}
/**
* @param string $column
* @return $this
*/
public function whereNull(string $column): static
{
$this->where[] = $column . ' IS NULL';
return $this;
}
/**
* @param string $column
* @return $this
*/
public function whereEmpty(string $column): static
{
$this->where[] = $column . ' = \'\'';
return $this;
}
/**
* @param string $column
* @return $this
*/
public function whereNotEmpty(string $column): static
{
$this->where[] = $column . ' <> \'\'';
return $this;
}
/**
* @param string $column
* @return $this
*/
public function whereNotNull(string $column): static
{
$this->where[] = $column . ' IS NOT NULL';
return $this;
}
/**
* @param array|Closure|string $columns
* @return $this
*/
public function filter(array|Closure|string $columns): static
{
if (!$columns) {
return $this;
}
if (is_callable($columns, TRUE)) {
return call_user_func($columns, $this);
}
if (is_string($columns)) {
$columns = explode(',', $columns);
}
if (!is_array($columns)) {
return $this;
}
$this->filter = $columns;
return $this;
}
/**
* @param string $alias
*
* @return $this
*
* select * from tableName as t1
*/
public function alias(string $alias = 't1'): static
{
$this->alias = $alias;
return $this;
}
/**
* @param string|Closure $tableName
*
* @return $this
*/
public function from(string|Closure $tableName): static
{
$this->from = $tableName;
return $this;
}
/**
* @param string $tableName
* @param string $alias
* @param null $on
* @param array|null $param
* @return $this
* $query->join([$tableName, ['userId'=>'uuvOd']], $param)
* $query->join([$tableName, ['userId'=>'uuvOd'], $param])
* $query->join($tableName, ['userId'=>'uuvOd',$param])
*/
private function join(string $tableName, string $alias, $on = NULL, array $param = NULL): static
{
if (empty($on)) {
return $this;
}
$join[] = $tableName . ' AS ' . $alias;
$join[] = 'ON ' . $this->onCondition($alias, $on);
if (empty($join)) {
return $this;
}
$this->join[] = implode(' ', $join);
if (!empty($param)) {
$this->addParams($param);
}
return $this;
}
/**
* @param $alias
* @param $on
* @return string
*/
private function onCondition($alias, $on): string
{
$array = [];
foreach ($on as $key => $item) {
if (!str_contains($item, '.')) {
$this->addParam($key, $item);
} else {
$explode = explode('.', $item);
if (isset($explode[1]) && ($explode[0] == $alias || $this->alias == $explode[0])) {
$array[] = $key . '=' . $item;
} else {
$this->addParam($key, $item);
}
}
}
return implode(' AND ', $array);
}
/**
* @param string $tableName
* @param string $alias
* @param $onCondition
* @param null $param
* @return $this
* @throws Exception
*/
public function leftJoin(string $tableName, string $alias, $onCondition, $param = NULL): static
{
if (class_exists($tableName)) {
$model = Kiri::getDi()->get($tableName);
if (!($model instanceof ModelInterface)) {
throw new Exception('Model must implement ' . ModelInterface::class);
}
$tableName = $model->getTable();
}
return $this->join(...["LEFT JOIN " . $tableName, $alias, $onCondition, $param]);
}
/**
* @param $tableName
* @param $alias
* @param $onCondition
* @param null $param
* @return $this
* @throws Exception
*/
public function rightJoin($tableName, $alias, $onCondition, $param = NULL): static
{
if (class_exists($tableName)) {
$model = Kiri::getDi()->get($tableName);
if (!($model instanceof ModelInterface)) {
throw new Exception('Model must implement ' . ModelInterface::class);
}
$tableName = $model->getTable();
}
return $this->join(...["RIGHT JOIN " . $tableName, $alias, $onCondition, $param]);
}
/**
* @param $tableName
* @param $alias
* @param $onCondition
* @param null $param
* @return $this
* @throws Exception
*/
public function innerJoin($tableName, $alias, $onCondition, $param = NULL): static
{
if (class_exists($tableName)) {
$model = Kiri::getDi()->get($tableName);
if (!($model instanceof ModelInterface)) {
throw new Exception('Model must implement ' . ModelInterface::class);
}
$tableName = $model->getTable();
}
return $this->join(...["INNER JOIN " . $tableName, $alias, $onCondition, $param]);
}
/**
* @param $array
*
* @return string
*/
private function toString($array): string
{
$tmp = [];
if (!is_array($array)) {
return $array;
}
foreach ($array as $key => $val) {
if (is_array($val)) {
$tmp[] = $this->toString($array);
} else {
$tmp[] = $key . '=:' . $key;
$this->attributes[':' . $key] = $val;
}
}
return implode(' AND ', $tmp);
}
/**
* @param string $field
*
* @return $this
*/
public function sum(string $field): static
{
$this->select[] = 'SUM(' . $field . ') AS ' . $field;
return $this;
}
/**
* @param string $field
* @return $this
*/
public function max(string $field): static
{
$this->select[] = 'MAX(' . $field . ') AS ' . $field;
return $this;
}
/**
* @param string $lngField
* @param string $latField
* @param int $lng1
* @param int $lat1
*
* @return $this
*/
public function distance(string $lngField, string $latField, int $lng1, int $lat1): static
{
$sql = "ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(($lat1 * PI() / 180 - $lat1 * PI() / 180) / 2),2) + COS($lat1 * PI() / 180) * COS($latField * PI() / 180) * POW(SIN(($lng1 * PI() / 180 - $lngField * PI() / 180) / 2),2))) * 1000) AS distance";
$this->select[] = $sql;
return $this;
}
/**
* @param string|array $column
* @param string $sort
*
* @return $this
*
* [
* 'addTime',
* 'descTime desc'
* ]
*/
public function orderBy(string|array $column, string $sort = 'DESC'): static
{
if (empty($column)) {
return $this;
}
if (is_string($column)) {
return $this->addOrder(...func_get_args());
}
foreach ($column as $key => $val) {
$this->addOrder($val);
}
return $this;
}
/**
* @param string $column
* @param string $sort
*
* @return $this
*
*/
private function addOrder(string $column, string $sort = 'DESC'): static
{
$column = trim($column);
if (func_num_args() == 1 || str_contains($column, ' ')) {
$this->order[] = $column;
} else {
$this->order[] = "$column $sort";
}
return $this;
}
/**
* @param array|string $column
*
* @return $this
*/
public function select(array|string $column = '*'): static
{
if ($column == '*') {
$this->select = $column;
} else {
if (!is_array($column)) {
$column = explode(',', $column);
}
foreach ($column as $val) {
$this->select[] = $val;
}
}
return $this;
}
/**
* @return $this
*/
public function orderRand(): static
{
$this->order[] = 'RAND()';
return $this;
}
/**
* @param array|Closure|string $conditionArray
* @param string $opera
* @param null $value
* @return QueryTrait
* @throws NotFindClassException
* @throws ReflectionException
*/
public function whereOr(array|Closure|string $conditionArray = [], string $opera = '=', $value = null): static
{
if ($conditionArray instanceof Closure) {
$conditionArray = $this->makeClosureFunction($conditionArray);
}
if (func_num_args() > 1) {
[$conditionArray, $opera, $value] = $this->opera(...func_get_args());
$conditionArray = $this->sprintf($conditionArray, $value, $opera);
}
$this->where = ['((' . implode(' AND ', $this->where) . ') OR (' . $conditionArray . '))'];
return $this;
}
/**
* @param string $columns
* @param string|int|bool|null $value
*
* @param string $opera
* @return QueryTrait
*/
public function whereAnd(string $columns, string $opera = '=', string|int|null|bool $value = NULL): static
{
[$columns, $opera, $value] = $this->opera(...func_get_args());
$this->where[] = $this->sprintf($columns, $value, $opera);
return $this;
}
/**
* @param string $columns
* @param string $value
* @return $this
* @throws Exception
*/
public function whereLike(string $columns, string $value): static
{
if (empty($columns) || (empty($value) && $value != 0)) {
return $this;
}
if (is_array($columns)) {
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
}
$this->where[] = $columns . ' LIKE \'%' . addslashes($value) . '%\'';
return $this;
}
/**
* @param string $columns
* @param string $value
* @return $this
* @throws Exception
*/
public function whereLeftLike(string $columns, string $value): static
{
if (empty($columns) || (empty($value) && $value != 0)) {
return $this;
}
if (is_array($columns)) {
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
}
$this->where[] = $columns . ' LLike \'%' . addslashes($value) . '\'';
return $this;
}
/**
* @param string $columns
* @param string $value
* @return $this
* @throws Exception
*/
public function whereRightLike(string $columns, string $value): static
{
if (empty($columns) || (empty($value) && $value != 0)) {
return $this;
}
if (is_array($columns)) {
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
}
$this->where[] = $columns . ' RLike \'' . addslashes($value) . '%\'';
return $this;
}
/**
* @param string $columns
* @param string $value
* @return $this
* @throws Exception
*/
public function whereNotLike(string $columns, string $value): static
{
if (empty($columns) || (empty($value) && $value != 0)) {
return $this;
}
if (is_array($columns)) {
$columns = 'CONCAT(' . implode(',^,', $columns) . ')';
}
$this->where[] = $columns . ' NOT LIKE \'%' . addslashes($value) . '%\'';
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @throws Exception
* @see MathematicsCondition
*/
public function whereEq(string $column, int $value): static
{
$this->where[] = ['EQ', $column, $value];
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @throws Exception
* @see MathematicsCondition
*/
public function whereNeq(string $column, int $value): static
{
$this->where[] = ['NEQ', $column, $value];
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @throws Exception
* @see MathematicsCondition
*/
public function whereGt(string $column, int $value): static
{
$this->where[] = ['GT', $column, $value];
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @throws Exception
* @see MathematicsCondition
*/
public function whereEgt(string $column, int $value): static
{
$this->where[] = ['EGT', $column, $value];
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @throws Exception
* @see MathematicsCondition
*/
public function whereLt(string $column, int $value): static
{
$this->where[] = ['LT', $column, $value];
return $this;
}
/**
* @param string $column
* @param int $value
* @return $this
* @throws Exception
* @see MathematicsCondition
*/
public function whereElt(string $column, int $value): static
{
$this->where[] = ['ELT', $column, $value];
return $this;
}
/**
* @param string $columns
* @param array|Closure $value
* @return $this
* @throws NotFindClassException
* @throws ReflectionException
*/
public function whereIn(string $columns, array|Closure $value): static
{
if ($value instanceof Closure) {
$value = $this->makeClosureFunction($value);
}
if (empty($value)) {
$value = [-1];
}
$this->where[] = ['IN', $columns, $value];
return $this;
}
/**
* @param $value
* @return ActiveQuery
* @throws Exception
*/
public function makeNewQuery($value): ActiveQuery
{
$activeQuery = new ActiveQuery($this->modelClass);
call_user_func($value, $activeQuery);
if (empty($activeQuery->from)) {
$activeQuery->from($activeQuery->modelClass->getTable());
}
return $activeQuery;
}
/**
* @return Query
* @throws ReflectionException
* @throws NotFindClassException
*/
public function makeNewSqlGenerate(): Query
{
return Kiri::createObject(['class' => Query::class]);
}
/**
* @param string $columns
* @param array $value
* @return $this
*/
public function whereNotIn(string $columns, array $value): static
{
if (empty($value) || !is_array($value)) {
$value = [-1];
}
$this->where[] = ['NOT IN', $columns, $value];
return $this;
}
/**
* @param string $column
* @param int $start
* @param int $end
* @return $this
*/
public function whereBetween(string $column, int $start, int $end): static
{
if (empty($column) || empty($start) || empty($end)) {
return $this;
}
$this->where[] = $column . ' BETWEEN ' . $start . ' AND ' . $end;
return $this;
}
/**
* @param string $column
* @param int $start
* @param int $end
* @return $this
*/
public function whereNotBetween(string $column, int $start, int $end): static
{
if (empty($column) || empty($start) || empty($end)) {
return $this;
}
$this->where[] = $column . 'NOT BETWEEN' . $start . ' AND ' . $end;
return $this;
}
/**
* @param array $params
*
* @return $this
*/
public function bindParams(array $params = []): static
{
if (empty($params)) {
return $this;
}
$this->attributes = $params;
return $this;
}
/**
* @param Closure|array|string $column
* @param string $opera
* @param null $value
* @return $this
* @throws NotFindClassException
* @throws ReflectionException
*/
public function where(Closure|array|string $column, string $opera = '=', $value = null): static
{
if (is_array($column)) {
return $this->addArray($column);
}
if ($column instanceof Closure) {
$this->where[] = $this->makeClosureFunction($column);
return $this;
}
if (is_string($column)) {
$this->where[] = $column;
} else {
[$column, $opera, $value] = $this->opera(...func_get_args());
$this->where[] = "$column $opera $value";
}
return $this;
}
/**
* @param Closure|array $closure
* @return string
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function makeClosureFunction(Closure|array $closure): string
{
$generate = $this->makeNewSqlGenerate();
if ($closure instanceof Closure) {
call_user_func($closure, $generate);
} else {
$generate->where($closure);
}
return $generate->getSql();
}
/**
* @param string $name
* @param string|null $having
*
* @return $this
*/
public function groupBy(string $name, string $having = NULL): static
{
$this->group = $name;
if (empty($having)) {
return $this;
}
$this->group .= ' HAVING ' . $having;
return $this;
}
/**
* @param int $offset
* @param int $limit
*
* @return $this
*/
public function limit(int $offset, int $limit = 20): static
{
$this->offset = $offset;
$this->limit = $limit;
return $this;
}
/**
* @return array
*/
private function opera(): array
{
if (func_num_args() == 3) {
[$column, $opera, $value] = func_get_args();
} else {
[$column, $value] = func_get_args();
}
if (!isset($opera)) {
$opera = '=';
}
return [$column, $opera, $value];
}
/**
* @param array $array
* @return $this
*/
private function addArray(array $array): static
{
foreach ($array as $key => $value) {
if (is_null($value)) continue;
if (is_numeric($key)) {
[$column, $opera, $value] = $this->opera(...$value);
$this->where[] = $this->sprintf($column, $value, $opera);
} else {
$this->where[] = $this->sprintf($key, $value);
}
}
return $this;
}
/**
* @param $column
* @param $value
* @param string $opera
* @return string
*/
private function sprintf($column, $value, string $opera = '='): string
{
if (is_string($value)) {
$value = trim($value, '\'"');
}
return "$column $opera '$value'";
}
/**
* @return $this
*/
public function oneLimit(): static
{
$this->limit = 1;
return $this;
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
use Database\Query;
class Users extends \Database\Model
{
public function hasD()
{
return $this->hasOne(static::class, 'id', 'id')->with([]);
}
}
Users::query()
->select(['*', (new Query(Users::class))
->where(['id' => 2])])
->from(function (Query $query) {
$query->from(Users::class)
->where(['id' => 1])
->groupBy('name DESC');
})->toSql();