duckdb.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * ZenTaoPHP的DuckDB类。
  4. * The dao and sql class file of ZenTaoPHP framework.
  5. *
  6. * The author disclaims copyright to this source code. In place of
  7. * a legal notice, here is a blessing:
  8. *
  9. * May you do good and not evil.
  10. * May you find forgiveness for yourself and forgive others.
  11. * May you share freely, never taking more than you give.
  12. */
  13. /**
  14. * DuckDB类。
  15. * DuckDB, data access object.
  16. *
  17. * @package framework
  18. */
  19. class duckdb
  20. {
  21. /**
  22. * 全局变量$baseRoot
  23. * The global basePath variable.
  24. *
  25. * @var object
  26. * @access public
  27. */
  28. public $baseRoot;
  29. /**
  30. * 全局变量$tmpRoot
  31. * The global tmpRoot variable.
  32. *
  33. * @var object
  34. * @access public
  35. */
  36. public $tmpRoot;
  37. /**
  38. * 全局变量$prefix
  39. * The global prefix variable.
  40. *
  41. * @var object
  42. * @access public
  43. */
  44. public $prefix;
  45. /**
  46. * 全局变量$sql
  47. * The global sql variable.
  48. *
  49. * @var object
  50. * @access public
  51. */
  52. public $sql;
  53. /**
  54. * 全局变量$binPath
  55. * The global binPath variable.
  56. *
  57. * @var object
  58. * @access public
  59. */
  60. public $binPath;
  61. /**
  62. * 全局变量$tmpPath
  63. * The global tmpPath variable.
  64. *
  65. * @var object
  66. * @access public
  67. */
  68. public $tmpPath;
  69. /**
  70. * 构造方法。
  71. * The construct method.
  72. *
  73. * @access public
  74. * @return void
  75. */
  76. public function __construct()
  77. {
  78. global $app, $config;
  79. $this->prefix = $config->db->prefix;
  80. $this->baseRoot = $app->getBasePath();
  81. $this->tmpRoot = $app->getTmpRoot();
  82. $this->setBinPath();
  83. $this->setTmpPath();
  84. }
  85. /**
  86. * 设置 duckdb 可执行文件路径。
  87. * Set duckdb bin path.
  88. *
  89. * @access public
  90. * @return void.
  91. */
  92. public function setBinPath()
  93. {
  94. $duckdbBin = $this->getBinConfig();
  95. $sourcePath = $this->tmpRoot . 'duckdb' . DS;
  96. $zboxPath = $duckdbBin['path'];
  97. $file = $duckdbBin['file'];
  98. $this->binPath = $sourcePath . $file;
  99. if(file_exists($this->binPath) && is_executable($this->binPath)) return;
  100. $this->binPath = $zboxPath . $file;
  101. }
  102. /**
  103. * 获取duckdb的bin目录配置。
  104. * Get bin config.
  105. *
  106. * @access public
  107. * @return void
  108. */
  109. public function getBinConfig()
  110. {
  111. global $config;
  112. $os = PHP_OS == 'WINNT' ? 'win' : 'linux';
  113. $duckdbBin = $config->bi->duckdbBin[$os];
  114. if($os == 'win') $duckdbBin['path'] = dirname(dirname($this->baseRoot)) . $duckdbBin['path'];
  115. return $duckdbBin;
  116. }
  117. /**
  118. * 设置 duckdb tmp parquet 文件路径。
  119. * Set duckdb tmp parquet file path.
  120. *
  121. * @access public
  122. * @return void.
  123. */
  124. public function setTmpPath($module = 'bi')
  125. {
  126. $this->tmpPath = $this->tmpRoot . 'duckdb' . DS . $module . DS;
  127. }
  128. /**
  129. * Query 方法。
  130. * Query function.
  131. *
  132. * @param string sql
  133. * @access public
  134. * @return this.
  135. */
  136. public function query($sql = '')
  137. {
  138. $sql = $this->replaceBackQuote($sql);
  139. $sql = $this->replaceTable2Parquet($sql);
  140. $sql = $this->standLimit($sql);
  141. $this->sql = $sql;
  142. return $this;
  143. }
  144. /**
  145. * 将mysql的``替换为duckdb可执行的""。
  146. * Replace ` to ".
  147. *
  148. * @param string sql
  149. * @access public
  150. * @return sql.
  151. */
  152. private function replaceBackQuote($sql)
  153. {
  154. $sql = trim($sql);
  155. $sql = trim($sql, ';');
  156. $sql = str_replace(array('`', '"'), array('', '\"'), $sql);
  157. return $sql;
  158. }
  159. /**
  160. * 替换sql语句中的表为parquet文件路径。
  161. * Replace sql table to parquet file path.
  162. *
  163. * @param string sql
  164. * @access public
  165. * @return sql.
  166. */
  167. private function replaceTable2Parquet($sql)
  168. {
  169. $ztpattern = "/\b{$this->prefix}([a-zA-Z0-9_]+)\b/";
  170. $ztvpattern = "/\bztv_([a-zA-Z0-9_]+)\b/";
  171. $replace = "'__biPath__$0.parquet'";
  172. $sql = preg_replace($ztpattern, $replace, $sql);
  173. $sql = preg_replace($ztvpattern, $replace, $sql);
  174. $sql = str_replace('zt_action.parquet', 'zt_action_*.parquet', $sql);
  175. $sql = str_replace('__biPath__', $this->tmpPath, $sql);
  176. return $sql;
  177. }
  178. /**
  179. * 获取sql中的字段。
  180. * Get fields form sqlparser statment.
  181. *
  182. * @param object $statment
  183. * @access public
  184. * @return array
  185. * @param object $statement
  186. */
  187. public function getFields($statement)
  188. {
  189. $fields = array();
  190. if($statement->expr)
  191. {
  192. foreach($statement->expr as $fieldInfo)
  193. {
  194. $column = $fieldInfo->column;
  195. $alias = $fieldInfo->alias;
  196. $fields[$column] = array('column' => $column, 'alias' => $alias);
  197. }
  198. }
  199. return $fields;
  200. }
  201. /**
  202. * 将LIMIT语句替换为duckdb可执行的格式。
  203. * Standard LIMIT syntax.
  204. *
  205. * @param string sql
  206. * @access public
  207. * @return sql.
  208. */
  209. private function standLimit($sql)
  210. {
  211. // 匹配 "LIMIT x, y" 并替换为 "LIMIT y OFFSET x"
  212. $limitpattern = '/LIMIT\s+(\d+)\s*,\s*(\d+)/i';
  213. $replacement = 'LIMIT $2 OFFSET $1';
  214. $sql = preg_replace($limitpattern, $replacement, $sql);
  215. return $sql;
  216. }
  217. /**
  218. * 尝试执行duckdb sql并获取返回结果。
  219. * Try to exec duckdb sql.
  220. *
  221. * @access public
  222. * @return this.
  223. */
  224. public function getResult()
  225. {
  226. $exec = "$this->binPath :memory: \"$this->sql\" -json 2>&1";
  227. $output = shell_exec($exec);
  228. if(empty($output)) $output = '';
  229. $rows = json_decode($output);
  230. /* 有内容但是 json 解析失败,说明是报错。*/
  231. if($output and !$rows)
  232. {
  233. throw new Exception($output);
  234. }
  235. else
  236. {
  237. return $rows ? $rows : array();
  238. }
  239. }
  240. //-------------------- Fetch相关方法(Fetch related methods) -------------------//
  241. /**
  242. * 获取一个记录。
  243. * Fetch one record.
  244. *
  245. * @access public
  246. * @return object|mixed
  247. */
  248. public function fetch()
  249. {
  250. $rows = $this->getResult();
  251. return $rows ? current($rows) : '';
  252. }
  253. /**
  254. * 获取所有记录。
  255. * Fetch all records.
  256. *
  257. * @access public
  258. * @return array
  259. */
  260. public function fetchAll()
  261. {
  262. return $this->getResult();
  263. }
  264. }