当前位置:网站首页>PHP implementation of SQLite udf/udaf
PHP implementation of SQLite udf/udaf
2022-07-22 19:42:00 【xchenhao】
SQLite UDF/UDAF
<?php
// sqlite UDF/UDAF Support
// https://blog.csdn.net/wyqwilliam/article/details/84500578
// Reference resources :
// php Version implementation :https://www.php.net/manual/en/ref.pdo-sqlite.connection.php
// go Version implementation :https://github.com/mattn/go-sqlite3/pull/229/files
$db = new \PDO('sqlite::memory:');
//$db->setAttribute(PDO::ATTR)
// $db = new \PDO('sqlite:');
// $db = new \PDO('sqlite:tmp.db');
$db->exec(' CREATE TABLE "role" ( "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "actor" TEXT, "movie" TEXT, "year" INTEGER ); ');
$insert = $db->exec(' INSERT INTO role (actor, movie, year) VALUES (" Yang Mi ", " New Liaozhai has different aspirations ", 2005), (" Yang Mi ", " Fairy sword legend ", 2009), (" Hu Ge ", " Fairy sword legend ", 2005), (" Hu Ge ", " List of reed {langya} ", 2015), (" Hu Ge ", " myth ", 2010), (" Xun Zhou ", " Daming Palace ci ", 2000), (" Xun Zhou ", " April in the world ", 2000) ; ');
$db->sqliteCreateFunction('IF', function ($expr, $value1, $value2) {
if ($expr) {
return $value1;
}
return $value2;
});
$db->sqliteCreateFunction('GREATEST', function ($value1, $value2) {
return max([$value1, $value2]);
});
$query = $db
->query('SELECT actor, movie, year, IF (year <= 2005, "old", "new") AS `type` FROM role');
while ($data = $query->fetchObject()) {
print_r($data);
}
/* stdClass Object ( [actor] => Yang Mi [movie] => New Liaozhai has different aspirations [year] => 2005 [type] => old ) stdClass Object ( [actor] => Yang Mi [movie] => Fairy sword legend [year] => 2009 [type] => new ) stdClass Object ( [actor] => Hu Ge [movie] => Fairy sword legend [year] => 2005 [type] => old ) stdClass Object ( [actor] => Hu Ge [movie] => List of reed {langya} [year] => 2015 [type] => new ) stdClass Object ( [actor] => Hu Ge [movie] => myth [year] => 2010 [type] => new ) stdClass Object ( [actor] => Xun Zhou [movie] => Daming Palace ci [year] => 2000 [type] => old ) stdClass Object ( [actor] => Xun Zhou [movie] => April in the world [year] => 2000 [type] => old ) */
$query = $db
->query('SELECT actor, GROUP_CONCAT(movie) AS all_movies FROM role GROUP BY actor');
while ($data = $query->fetchObject()) {
print_r($data);
}
/* stdClass Object ( [actor] => Xun Zhou [all_movies] => Daming Palace ci , April in the world ) stdClass Object ( [actor] => Yang Mi [all_movies] => New Liaozhai has different aspirations , Fairy sword legend ) stdClass Object ( [actor] => Hu Ge [all_movies] => Fairy sword legend , List of reed {langya} , myth ) */
$db->sqliteCreateAggregate('GROUP_CONCAT', function ($context, $row_number, $field) {
if (is_null($context)) {
return $field;
}
return $context . '/' . $field;
}, function ($context, $rowcount) {
return $context; });
/* stdClass Object ( [actor] => Xun Zhou [all_movies] => Daming Palace ci / April in the world ) stdClass Object ( [actor] => Yang Mi [all_movies] => New Liaozhai has different aspirations / Fairy sword legend ) stdClass Object ( [actor] => Hu Ge [all_movies] => Fairy sword legend / List of reed {langya} / myth ) */
边栏推荐
- pytest接口自动化测试框架 | unittest与pytest的区别
- pytest接口自动化测试框架 | 为什么要做pytest插件的二次开发
- 常用性能工具:工欲善其事,必先利其器
- Explain the adapter technology in NLP from simple to deep
- 友元函数和友元类
- 解决Couldn‘t determine repo type for URL
- 货币增发为什么会造成通货膨胀? 如何造成
- Data Lake (18): Flink and iceberg integrate SQL API operations
- Markdown数学公式语法
- C# winform excel根据当前选中内容,自动插入/编辑批注
猜你喜欢
Definition of derived classes
QT中多线程的使用
multiple inheritance
派生类的定义
Leetcode notes
Pytest interface automated testing framework | plug-in secondary development practice
[learn C and fly] Chapter 3 branch structure (exercise 3.1 simple guessing game)
国产立体声音频数模D/A转换器DP4344替代兼容CS4344
关于数据产品经理的三个小的知识点
消息的可靠性与重复性
随机推荐
【带你学c带你飞】浙大版《C语言程序设计(第3版)》第3章 分支结构(习题3)
Neo4j 示例:三国志人物关系图谱
相比技术与产品 倾听与意见才是Web3当下的首要任务
C# winform excel根据当前选中内容,自动插入/编辑批注
派生类的构造函数和析构函数
NLP text classification top meeting paper reading notes for natural language processing (2)
C# 自动生成字典(数据多的时候)
多重继承
错过等一年!百度超级链数藏发行服务限时五折
pytest接口自动化测试框架 | 接口自动化至yaml数据驱动
PHP curl 请求中设置 proxy 方法
静态成员
Sentry nodestore_node 表中的数据查看
Why does additional currency issue cause inflation? How to cause
如何不获取root权限使用第三方主题:MIUI篇
pytest接口自动化测试框架 | pytest断言
中国最幸运极客:二十出头身家过亿,三次创业全身而退
索引为什么选择B+树
树和二叉树:树的概念
派生类成员的访问