dandylion / advancedmedoo
为 Medoo 添加额外功能
v1.2.1
2023-10-29 13:33 UTC
Requires
- php: >=8.0.0
- dandylion/medoo: ^1.0
README
欢迎来到 Advanced Medoo,它是 Medoo 的强大扩展,可提升您的 PHP 数据库交互水平。告别重复的数据库代码,迎接更高效的数据库操作方式!
特性
- 自定义列选择: Advanced Medoo 允许您以更灵活的方式选择列,使您更容易获取所需的数据。
- 修补和同步功能: 使用修补和同步功能简化数据同步,旨在简化您的操作流程。
- 列相似度搜索: 使用 "SIMILAR" 函数,轻松确定特定列与给定字符串的相似度。
安装
您可以使用 Composer 快速安装 Advanced Medoo。这将自动安装 Medoo。
composer require dandylion/advancedmedoo
用法
开始使用 Advanced Medoo 非常简单。以下是如何使用它的示例
use Dandylion\AdvancedMedoo;
// Initialize Advanced Medoo with your database configuration
$database = new AdvancedMedoo([
'database_type' => 'mysql',
'database_name' => 'your_database',
'server' => 'localhost',
'username' => 'your_username',
'password' => 'your_password'
]);
选择表中的所有列
$database->select(
'user',
'[>]post'=>'user_id'
[
'user.name'
'post.*'
]
);
//[{
// "name": "my_name",
// "user_id": 1,
// "text": "post_text"
// "date": "01/01/1970"
//}]
/* You can also put text in brackets to prepend a string in front of all the columns incase of duplicate column names */
$database->select(
'user',
'[>]post'=>'user_id'
[
'user.name'
'post.* (post_)'
]
);
//[{
// "name": "my_name",
// "post_user_id": 1,
// "post_text": "post_text",
// "post_date": "01/01/1970"
//}]
修补方法 (允许您在不检查记录是否存在的情况下更新或创建记录)
//[
// {
// "user_id": 1,
// "post_id": 1,
// "text": "This is my first post",
// "privacy": "public"
// },
// {
// "user_id": 2,
// "post_id": 2,
// "text": "This is a different user post",
// "privacy": "public"
// }
//]
$database->patch(
'posts',
[
[
"user_id": 1,
"post_id": 1,
"text": "This is my first post",
"privacy": "private"
],
[
"user_id": 1,
"post_id": 3,
"text": "This is my second post",
"privacy": "public"
]
],
[ // uses AND to check if record exists
"user_id",
"post_id"
]
);
//[
// {
// "user_id": 1,
// "post_id": 1,
// "text": "This is my first post",
// "privacy": "private"
// },
// {
// "user_id": 2,
// "post_id": 2,
// "text": "This is a different user post",
// "privacy": "public"
// },
// {
// "user_id": 1,
// "post_id": 3,
// "text": "This is my second post",
// "privacy": "public"
// }
//]
同步方法 (与修补方法相同,但也会删除未找到的记录)
//[
// {
// "user_id": 1,
// "post_id": 1,
// "text": "This is my first post",
// "privacy": "public"
// },
// {
// "user_id": 2,
// "post_id": 2,
// "text": "This is a different user post",
// "privacy": "public"
// }
//]
$database->patch(
'posts',
[
[
"user_id": 1,
"post_id": 1,
"text": "This is my first post",
"privacy": "private"
],
[
"user_id": 1,
"post_id": 3,
"text": "This is my second post",
"privacy": "public"
]
],
[ // uses AND to check if record exists
"user_id",
"post_id"
]
);
//[
// {
// "user_id": 1,
// "post_id": 1,
// "text": "This is my first post",
// "privacy": "private"
// },
// {
// "user_id": 1,
// "post_id": 3,
// "text": "This is my second post",
// "privacy": "public"
// }
//]
SIMILAR 存储过程
此存储过程计算列与给定字符串的相似度。
请在使用 SQL 函数之前在数据库上运行此 SQL 查询。
DROP FUNCTION IF EXISTS SIMILAR;
DELIMITER //
CREATE FUNCTION SIMILAR(s1 VARCHAR(255), s2 VARCHAR(255))
RETURNS FLOAT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, matches INT;
DECLARE s3,checker VARCHAR(255);
IF LENGTH(s1) > LENGTH(s2) THEN
SET s3 = s2;
SET s2 = s1;
SET s1 = s3;
END IF;
SET s1_len = LENGTH(s1) - 2;
SET s2_len = LENGTH(s2) - 2;
SET i = 1;
SET matches = 0;
WHILE i <= s1_len DO
SET j = 1;
SET checker = SUBSTRING(s1,i,3);
WHILE j <= s2_len DO
IF checker = SUBSTRING(s2,j,3) THEN
SET matches = matches + 1;
END IF;
SET j = j + 1;
END WHILE;
SET i = i + 1;
END WHILE;
RETURN matches/s1_len;
END//
DELIMITER ;
SIMILAR 函数可用于 select 和 get 方法中的列部分。
许可证
Advanced Medoo 根据 MIT 许可证授权。
致谢
我们感谢 Medoo 社区为他们的优秀数据库库所做的工作。
用 Advanced Medoo 提升您的编码水平!