PageRenderTime 27ms CodeModel.GetById 45ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezsqlModel.php

http://github.com/jv2222/ezSQL
PHP | 903 lines | 497 code | 62 blank | 344 comment | 35 complexity | f0da67304d43c7a81783bf1fad587a46 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. namespace ezsql;
  3. use ezsql\ezQuery;
  4. use ezsql\ezsqlModelInterface;
  5. use function ezsql\functions\{get_vendor, create_certificate};
  6. /**
  7. * Core class containing common functions to manipulate **query** `result sets` once returned.
  8. *
  9. * @method void setDebugAll($args);
  10. * A `ezsqlModel` class property that can be accessed with either
  11. * a `set` or `get` prefix as a function.
  12. * @method void setTrace($args);
  13. * A `ezsqlModel` class property that can be accessed with either
  14. * a `set` or `get` prefix as a function.
  15. * @method void setDebugCalled($args);
  16. * A `ezsqlModel` class property that can be accessed with either
  17. * a `set` or `get` prefix as a function.
  18. * @method void setVarDumpCalled($args);
  19. * A `ezsqlModel` class property that can be accessed with either
  20. * a `set` or `get` prefix as a function.
  21. * @method void setShowErrors($args);
  22. * A `ezsqlModel` class property that can be accessed with either
  23. * a `set` or `get` prefix as a function.
  24. * @method void setNumQueries($args);
  25. * A `ezsqlModel` class property that can be accessed with either
  26. * a `set` or `get` prefix as a function.
  27. * @method void setConnQueries($args);
  28. * A `ezsqlModel` class property that can be accessed with either
  29. * a `set` or `get` prefix as a function.
  30. * @method void setCapturedErrors($args);
  31. * A `ezsqlModel` class property that can be accessed with either
  32. * a `set` or `get` prefix as a function.
  33. * @method void setCacheDir($args);
  34. * A `ezsqlModel` class property that can be accessed with either
  35. * a `set` or `get` prefix as a function.
  36. * @method void setUseDiskCache($args);
  37. * A `ezsqlModel` class property that can be accessed with either
  38. * a `set` or `get` prefix as a function.
  39. * @method void setCacheTimeout($args);
  40. * A `ezsqlModel` class property that can be accessed with either
  41. * a `set` or `get` prefix as a function.
  42. * @method void setCacheQueries($args);
  43. * A `ezsqlModel` class property that can be accessed with either
  44. * a `set` or `get` prefix as a function.
  45. * @method void setCacheInserts($args);
  46. * A `ezsqlModel` class property that can be accessed with either
  47. * a `set` or `get` prefix as a function.
  48. * @method void setNumRows($args);
  49. * A `ezsqlModel` class property that can be accessed with either
  50. * a `set` or `get` prefix as a function.
  51. * @method void setDbConnectTime($args);
  52. * A `ezsqlModel` class property that can be accessed with either
  53. * a `set` or `get` prefix as a function.
  54. * @method void setSqlLogFile($args);
  55. * A `ezsqlModel` class property that can be accessed with either
  56. * a `set` or `get` prefix as a function.
  57. * @method void setProfileTimes($args);
  58. * A `ezsqlModel` class property that can be accessed with either
  59. * a `set` or `get` prefix as a function.
  60. * @method void setInsertId($args);
  61. * A `ezsqlModel` class property that can be accessed with either
  62. * a `set` or `get` prefix as a function.
  63. * @method void setLastQuery($args);
  64. * A `ezsqlModel` class property that can be accessed with either
  65. * a `set` or `get` prefix as a function.
  66. * @method void setLastError($args);
  67. * A `ezsqlModel` class property that can be accessed with either
  68. * a `set` or `get` prefix as a function.
  69. * @method void setColInfo($args);
  70. * A `ezsqlModel` class property that can be accessed with either
  71. * a `set` or `get` prefix as a function.
  72. * @method void setTimers($args);
  73. * A `ezsqlModel` class property that can be accessed with either
  74. * a `set` or `get` prefix as a function.
  75. * @method void setTotalQueryTime($args);
  76. * A `ezsqlModel` class property that can be accessed with either
  77. * a `set` or `get` prefix as a function.
  78. * @method void setTraceLog($args);
  79. * A `ezsqlModel` class property that can be accessed with either
  80. * a `set` or `get` prefix as a function.
  81. * @method void setUseTraceLog($args);
  82. * A `ezsqlModel` class property that can be accessed with either
  83. * a `set` or `get` prefix as a function.
  84. * @method void setDoProfile($args);
  85. * A `ezsqlModel` class property that can be accessed with either
  86. * a `set` or `get` prefix as a function.
  87. * @method void setLastResult($args);
  88. * A `ezsqlModel` class property that can be accessed with either
  89. * a `set` or `get` prefix as a function.
  90. * @method void setFromDiskCache($args);
  91. * A `ezsqlModel` class property that can be accessed with either
  92. * a `set` or `get` prefix as a function.
  93. * @method void setDebugEchoIsOn($args);
  94. * A `ezsqlModel` class property that can be accessed with either
  95. * a `set` or `get` prefix as a function.
  96. * @method void setFuncCall($args);
  97. * A `ezsqlModel` class property that can be accessed with either
  98. * a `set` or `get` prefix as a function.
  99. * @method void setAllFuncCalls($args);
  100. * A `ezsqlModel` class property that can be accessed with either
  101. * a `set` or `get` prefix as a function.
  102. * @method void setTable($name);
  103. * A `ezsqlModel` class property that can be accessed with either
  104. * a `set` or `get` prefix as a function.
  105. * @method void setPrefix($append);
  106. * A `ezsqlModel` class property that can be accessed with either
  107. * a `set` or `get` prefix as a function.
  108. *
  109. * @method string getDebugAll();
  110. * A `ezsqlModel` class property that can be accessed with either
  111. * a `set` or `get` prefix as a function.
  112. * @method string getTrace();
  113. * A `ezsqlModel` class property that can be accessed with either
  114. * a `set` or `get` prefix as a function.
  115. * @method string getDebugCalled();
  116. * A `ezsqlModel` class property that can be accessed with either
  117. * a `set` or `get` prefix as a function.
  118. * @method string getVarDumpCalled();
  119. * A `ezsqlModel` class property that can be accessed with either
  120. * a `set` or `get` prefix as a function.
  121. * @method string getShowErrors();
  122. * A `ezsqlModel` class property that can be accessed with either
  123. * a `set` or `get` prefix as a function.
  124. * @method string getNumQueries();
  125. * A `ezsqlModel` class property that can be accessed with either
  126. * a `set` or `get` prefix as a function.
  127. * @method string getConnQueries();
  128. * A `ezsqlModel` class property that can be accessed with either
  129. * a `set` or `get` prefix as a function.
  130. * @method string getCapturedErrors();
  131. * A `ezsqlModel` class property that can be accessed with either
  132. * a `set` or `get` prefix as a function.
  133. * @method string getCacheDir();
  134. * A `ezsqlModel` class property that can be accessed with either
  135. * a `set` or `get` prefix as a function.
  136. * @method string getUseDiskCache();
  137. * A `ezsqlModel` class property that can be accessed with either
  138. * a `set` or `get` prefix as a function.
  139. * @method string getCacheTimeout();
  140. * A `ezsqlModel` class property that can be accessed with either
  141. * a `set` or `get` prefix as a function.
  142. * @method string getCacheQueries();
  143. * A `ezsqlModel` class property that can be accessed with either
  144. * a `set` or `get` prefix as a function.
  145. * @method string getCacheInserts();
  146. * A `ezsqlModel` class property that can be accessed with either
  147. * a `set` or `get` prefix as a function.
  148. * @method string getNumRows();
  149. * A `ezsqlModel` class property that can be accessed with either
  150. * a `set` or `get` prefix as a function.
  151. * @method string getDbConnectTime();
  152. * A `ezsqlModel` class property that can be accessed with either
  153. * a `set` or `get` prefix as a function.
  154. * @method string getSqlLogFile();
  155. * A `ezsqlModel` class property that can be accessed with either
  156. * a `set` or `get` prefix as a function.
  157. * @method string getProfileTimes();
  158. * A `ezsqlModel` class property that can be accessed with either
  159. * a `set` or `get` prefix as a function.
  160. * @method string getInsertId();
  161. * A `ezsqlModel` class property that can be accessed with either
  162. * a `set` or `get` prefix as a function.
  163. * @method string getLastQuery();
  164. * A `ezsqlModel` class property that can be accessed with either
  165. * a `set` or `get` prefix as a function.
  166. * @method string getLastError();
  167. * A `ezsqlModel` class property that can be accessed with either
  168. * a `set` or `get` prefix as a function.
  169. * @method string getColInfo();
  170. * A `ezsqlModel` class property that can be accessed with either
  171. * a `set` or `get` prefix as a function.
  172. * @method string getTimers();
  173. * A `ezsqlModel` class property that can be accessed with either
  174. * a `set` or `get` prefix as a function.
  175. * @method string getTotalQueryTime();
  176. * A `ezsqlModel` class property that can be accessed with either
  177. * a `set` or `get` prefix as a function.
  178. * @method string getTraceLog();
  179. * A `ezsqlModel` class property that can be accessed with either
  180. * a `set` or `get` prefix as a function.
  181. * @method string getUseTraceLog();
  182. * A `ezsqlModel` class property that can be accessed with either
  183. * a `set` or `get` prefix as a function.
  184. * @method string getDoProfile();
  185. * A `ezsqlModel` class property that can be accessed with either
  186. * a `set` or `get` prefix as a function.
  187. * @method string getLastResult();
  188. * A `ezsqlModel` class property that can be accessed with either
  189. * a `set` or `get` prefix as a function.
  190. * @method string getFromDiskCache();
  191. * A `ezsqlModel` class property that can be accessed with either
  192. * a `set` or `get` prefix as a function.
  193. * @method string getDebugEchoIsOn();
  194. * A `ezsqlModel` class property that can be accessed with either
  195. * a `set` or `get` prefix as a function.
  196. * @method string getFuncCall();
  197. * A `ezsqlModel` class property that can be accessed with either
  198. * a `set` or `get` prefix as a function.
  199. * @method string getAllFuncCalls();
  200. * A `ezsqlModel` class property that can be accessed with either
  201. * a `set` or `get` prefix as a function.
  202. * @method string getTable();
  203. * A `ezsqlModel` class property that can be accessed with either
  204. * a `set` or `get` prefix as a function.
  205. * @method string getPrefix();
  206. * A `ezsqlModel` class property that can be accessed with either
  207. * a `set` or `get` prefix as a function.
  208. */
  209. class ezsqlModel extends ezQuery implements ezsqlModelInterface
  210. {
  211. protected $isSecure = false;
  212. protected $secureOptions = null;
  213. protected $sslKey = null;
  214. protected $sslCert = null;
  215. protected $sslCa = null;
  216. protected $sslPath = null;
  217. /**
  218. * If set to true (i.e. $db->debug_all = true;) Then it will print out ALL queries and ALL results of your script.
  219. * @var boolean
  220. */
  221. protected $debugAll = false;
  222. // same as $debug_all
  223. protected $trace = false;
  224. protected $debugCalled = false;
  225. protected $varDumpCalled = false;
  226. /**
  227. * Current show error state
  228. * @var boolean
  229. */
  230. protected $showErrors = true;
  231. /**
  232. * Keeps track of exactly how many 'real' (not cached)
  233. * queries were executed during the lifetime of the current script
  234. * @var int
  235. */
  236. protected $numQueries = 0;
  237. protected $connQueries = 0;
  238. protected $capturedErrors = array();
  239. /**
  240. * Specify a cache dir. Path is taken from calling script
  241. * @var string
  242. */
  243. protected $cacheDir = 'tmp' . \_DS . 'ez_cache';
  244. /**
  245. * Disk Cache Setup
  246. * (1. You must create this dir. first!)
  247. * (2. Might need to do chmod 775)
  248. *
  249. * Global override setting to turn disc caching off (but not on)
  250. * @var boolean
  251. */
  252. protected $useDiskCache = false;
  253. /**
  254. * Cache expiry, this is hours
  255. * @var int
  256. */
  257. protected $cacheTimeout = 24;
  258. /**
  259. * if you want to cache EVERYTHING just do..
  260. *
  261. * $use_disk_cache = true;
  262. * $cache_queries = true;
  263. * $cache_timeout = 24;
  264. */
  265. /**
  266. * By wrapping up queries you can ensure that the default
  267. * is NOT to cache unless specified
  268. * @var boolean
  269. */
  270. protected $cacheQueries = false;
  271. protected $cacheInserts = false;
  272. /**
  273. * Log number of rows the query returned
  274. * @var int Default is null
  275. */
  276. protected $numRows = null;
  277. protected $dbConnectTime = 0;
  278. protected $sqlLogFile = false;
  279. protected $profileTimes = array();
  280. /**
  281. * ID generated from the AUTO_INCREMENT of the previous INSERT operation (if any)
  282. * @var int
  283. */
  284. protected $insertId = null;
  285. /**
  286. * Use to keep track of the last query for debug..
  287. * @var string
  288. */
  289. protected $lastQuery = null;
  290. /**
  291. * The table `name` to use on calls to `ing` ending m
  292. * `CRUD` methods/functions.
  293. *
  294. * @var string
  295. */
  296. protected $table = '';
  297. /**
  298. * A `prefix` to append to `table` name on calls to `ing` ending
  299. * `CRUD` methods/functions.
  300. *
  301. * @var string
  302. */
  303. protected $prefix = '';
  304. /**
  305. * Use to keep track of last error
  306. * @var string
  307. */
  308. protected $lastError = null;
  309. /**
  310. * Saved info on the table column
  311. * @var mixed
  312. */
  313. protected $colInfo = array();
  314. protected $timers = array();
  315. protected $totalQueryTime = 0;
  316. protected $traceLog = array();
  317. protected $useTraceLog = false;
  318. protected $doProfile = false;
  319. /**
  320. * The last query result
  321. * @var object Default is null
  322. */
  323. protected $lastResult = null;
  324. /**
  325. * Get data from disk cache
  326. * @var boolean Default is false
  327. */
  328. protected $fromDiskCache = false;
  329. /**
  330. * Needed for echo of debug function
  331. * @var boolean Default is false
  332. */
  333. protected $debugEchoIsOn = false;
  334. /**
  335. * Whether the database connection is established, or not
  336. * @var boolean Default is false
  337. */
  338. protected $_connected = false;
  339. /**
  340. * Contains the number of affected rows of a query
  341. * @var int Default is 0
  342. */
  343. protected $_affectedRows = 0;
  344. /**
  345. * Function called
  346. * @var string
  347. */
  348. private $funcCall;
  349. /**
  350. * All functions called
  351. * @var array
  352. */
  353. private $allFuncCalls = array();
  354. /**
  355. * Constructor
  356. */
  357. public function __construct()
  358. {
  359. parent::__construct();
  360. }
  361. /**
  362. * Magic methods for Calling Non-Existent Functions, handling Getters and Setters.
  363. * @method set/get{property} - a property that needs to be accessed
  364. *
  365. * @property-read function
  366. * @property-write args
  367. *
  368. * @return mixed
  369. * @throws \Exception
  370. */
  371. public function __call($function, $args)
  372. {
  373. $prefix = \substr($function, 0, 3);
  374. $property = \lcfirst(\substr($function, 3, \strlen($function)));
  375. // Todo: make properties PSR-1, add following for backward compatibility
  376. if (\strpos($property, '_') !== false)
  377. $property = \str_replace('_', '', $property);
  378. if (($prefix == 'set') && \property_exists($this, $property)) {
  379. $this->$property = $args[0];
  380. } elseif (($prefix == 'get') && \property_exists($this, $property)) {
  381. return $this->$property;
  382. } else {
  383. throw new \Exception("$function does not exist");
  384. }
  385. }
  386. public function get_host_port(string $host, bool $default = false)
  387. {
  388. $port = $default;
  389. if (false !== \strpos($host, ':')) {
  390. list($host, $port) = \explode(':', $host);
  391. $port = (int) $port;
  392. }
  393. return array($host, $port);
  394. }
  395. public function register_error(string $err_str, bool $displayError = true)
  396. {
  397. // Keep track of last error
  398. $this->lastError = $err_str;
  399. // Capture all errors to an error array no matter what happens
  400. $this->capturedErrors[] = array(
  401. 'error_str' => $err_str,
  402. 'query' => $this->lastQuery
  403. );
  404. if ($this->showErrors && $displayError)
  405. \trigger_error(\htmlentities($err_str), \E_USER_WARNING);
  406. return false;
  407. }
  408. public function show_errors()
  409. {
  410. $this->showErrors = true;
  411. }
  412. public function hide_errors()
  413. {
  414. $this->showErrors = false;
  415. }
  416. /**
  417. * Turn on echoing of debug info, for `debug()`
  418. */
  419. public function debugOn()
  420. {
  421. $this->debugEchoIsOn = true;
  422. }
  423. /**
  424. * Turn off echoing of debug info, the default, for `debug()`
  425. */
  426. public function debugOff()
  427. {
  428. $this->debugEchoIsOn = false;
  429. }
  430. public function flush()
  431. {
  432. // Get rid of these
  433. $this->lastResult = null;
  434. $this->colInfo = array();
  435. $this->lastQuery = null;
  436. $this->allFuncCalls = array();
  437. $this->fromDiskCache = false;
  438. $this->clearPrepare();
  439. }
  440. public function log_query(string $query)
  441. {
  442. // Log how the last function was called
  443. $this->funcCall = $query;
  444. // Keep an running Log of all functions called
  445. \array_push($this->allFuncCalls, $this->funcCall);
  446. }
  447. public function get_var(string $query = null, int $x = 0, int $y = 0, bool $use_prepare = false)
  448. {
  449. // Log how the function was called
  450. $this->log_query("\$db->get_var(\"$query\",$x,$y)");
  451. // If there is a query then perform it if not then use cached results..
  452. if ($query) {
  453. $this->query($query, $use_prepare);
  454. }
  455. // Extract public out of cached results based x,y values
  456. if (isset($this->lastResult[$y])) {
  457. $values = \array_values(\get_object_vars($this->lastResult[$y]));
  458. }
  459. // If there is a value return it else return null
  460. return (isset($values[$x]) && $values[$x] !== null) ? $values[$x] : null;
  461. }
  462. public function get_row(string $query = null, $output = \OBJECT, int $y = 0, bool $use_prepare = false)
  463. {
  464. // Log how the function was called
  465. $this->log_query("\$db->get_row(\"$query\",$output,$y)");
  466. // If there is a query then perform it if not then use cached results..
  467. if ($query) {
  468. $this->query($query, $use_prepare);
  469. }
  470. if ($output == OBJECT) {
  471. // If the output is an object then return object using the row offset..
  472. return isset($this->lastResult[$y]) ? $this->lastResult[$y] : null;
  473. } elseif ($output == \ARRAY_A) {
  474. // If the output is an associative array then return row as such..
  475. return isset($this->lastResult[$y]) ? \get_object_vars($this->lastResult[$y]) : null;
  476. } elseif ($output == \ARRAY_N) {
  477. // If the output is an numerical array then return row as such..
  478. return isset($this->lastResult[$y]) ? \array_values(\get_object_vars($this->lastResult[$y])) : null;
  479. } else {
  480. // If invalid output type was specified..
  481. $this->showErrors ? \trigger_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N", \E_USER_WARNING) : null;
  482. }
  483. }
  484. public function get_col(string $query = null, int $x = 0, bool $use_prepare = false)
  485. {
  486. $new_array = array();
  487. // If there is a query then perform it if not then use cached results..
  488. if ($query) {
  489. $this->query($query, $use_prepare);
  490. }
  491. // Extract the column values
  492. if (\is_array($this->lastResult)) {
  493. $j = \count($this->lastResult);
  494. for ($i = 0; $i < $j; $i++) {
  495. $new_array[$i] = $this->get_var(null, $x, $i, $use_prepare);
  496. }
  497. }
  498. return $new_array;
  499. }
  500. public function get_results(string $query = null, $output = \OBJECT, bool $use_prepare = false)
  501. {
  502. // Log how the function was called
  503. $this->log_query("\$db->get_results(\"$query\", $output, $use_prepare)");
  504. // If there is a query then perform it if not then use cached results..
  505. if ($query) {
  506. $this->query($query, $use_prepare);
  507. }
  508. if ($output === \OBJECT) {
  509. return $this->lastResult;
  510. } elseif ($output == \JSON) {
  511. return \json_encode($this->lastResult); // return as json output
  512. } elseif ($output == \ARRAY_A || $output == \ARRAY_N) {
  513. $new_array = [];
  514. if ($this->lastResult) {
  515. $i = 0;
  516. foreach ($this->lastResult as $row) {
  517. $new_array[$i] = \get_object_vars($row);
  518. if ($output == \ARRAY_N) {
  519. $new_array[$i] = \array_values($new_array[$i]);
  520. }
  521. $i++;
  522. }
  523. }
  524. return $new_array;
  525. }
  526. }
  527. public function get_col_info(string $info_type = "name", int $col_offset = -1)
  528. {
  529. if ($this->colInfo) {
  530. $new_array = [];
  531. if ($col_offset == -1) {
  532. $i = 0;
  533. foreach ($this->colInfo as $col) {
  534. $new_array[$i] = $col->{$info_type};
  535. $i++;
  536. }
  537. return $new_array;
  538. }
  539. return $this->colInfo[$col_offset]->{$info_type};
  540. }
  541. }
  542. public function create_cache(string $path = null)
  543. {
  544. $cache_dir = empty($path) ? $this->cacheDir : $path;
  545. if (!\is_dir($cache_dir)) {
  546. $this->cacheDir = $cache_dir;
  547. @\mkdir($cache_dir, ('\\' == \DIRECTORY_SEPARATOR ? null : 0755), true);
  548. }
  549. }
  550. public function store_cache(string $query, bool $is_insert = false)
  551. {
  552. // The would be cache file for this query
  553. $cache_file = $this->cacheDir . \_DS . \md5($query);
  554. // disk caching of queries
  555. if (
  556. $this->useDiskCache
  557. && ($this->cacheQueries && !$is_insert) || ($this->cacheInserts && $is_insert)
  558. ) {
  559. $this->create_cache();
  560. if (!\is_dir($this->cacheDir)) {
  561. return $this->register_error("Could not open cache dir: $this->cacheDir");
  562. } else {
  563. // Cache all result values
  564. $result_cache = array(
  565. 'col_info' => $this->colInfo,
  566. 'last_result' => $this->lastResult,
  567. 'num_rows' => $this->numRows,
  568. 'return_value' => $this->numRows,
  569. );
  570. \file_put_contents($cache_file, \serialize($result_cache));
  571. if (\file_exists($cache_file . ".updating"))
  572. \unlink($cache_file . ".updating");
  573. }
  574. }
  575. }
  576. public function get_cache(string $query)
  577. {
  578. // The would be cache file for this query
  579. $cache_file = $this->cacheDir . \_DS . \md5($query);
  580. // Try to get previously cached version
  581. if ($this->useDiskCache && \file_exists($cache_file)) {
  582. // Only use this cache file if less than 'cache_timeout' (hours)
  583. if ((\time() - \filemtime($cache_file)) > ($this->cacheTimeout * 3600)
  584. && !(\file_exists($cache_file . ".updating")
  585. && (\time() - \filemtime($cache_file . ".updating") < 60))
  586. ) {
  587. \touch($cache_file . ".updating"); // Show that we in the process of updating the cache
  588. } else {
  589. $result_cache = \unserialize(\file_get_contents($cache_file));
  590. $this->colInfo = $result_cache['col_info'];
  591. $this->lastResult = $result_cache['last_result'];
  592. $this->numRows = $result_cache['num_rows'];
  593. $this->fromDiskCache = true;
  594. // If debug ALL queries
  595. $this->trace || $this->debugAll ? $this->debug() : null;
  596. return $result_cache['return_value'];
  597. }
  598. }
  599. }
  600. public function varDump($mixed = null)
  601. {
  602. // Start output buffering
  603. \ob_start();
  604. echo "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>";
  605. echo "<pre><font face=arial>";
  606. if (!$this->varDumpCalled) {
  607. echo "<font color=800080><b>ezSQL</b> (v" . \EZSQL_VERSION . ") <b>Variable Dump..</b></font>\n\n";
  608. }
  609. $var_type = \gettype($mixed);
  610. \print_r(($mixed ? $mixed : "<font color=red>No Value / False</font>"));
  611. echo "\n\n<b>Type:</b> " . \ucfirst($var_type) . "\n";
  612. echo "<b>Last Query</b> [$this->numQueries]<b>:</b> " . ($this->lastQuery ? $this->lastQuery : "NULL") . "\n";
  613. echo "<b>Last Function Call:</b> " . ($this->funcCall ? $this->funcCall : "None") . "\n";
  614. if (\count($this->allFuncCalls) > 1) {
  615. echo "<b>List of All Function Calls:</b><br>";
  616. foreach ($this->allFuncCalls as $func_string)
  617. echo " " . $func_string . "<br>\n";
  618. }
  619. echo "<b>Last Rows Returned:</b><br>";
  620. echo ((!empty($this->lastResult) && \count($this->lastResult) > 0) ? print_r($this->lastResult[0]) : 'No rows returned') . "\n";
  621. echo "</font></pre></font></blockquote></td></tr></table>"; //.$this->donation();
  622. echo "\n<hr size=1 noshade color=dddddd>";
  623. // Stop output buffering and capture debug HTML
  624. $html = \ob_get_contents();
  625. \ob_end_clean();
  626. // Only echo output if it is turned on
  627. if ($this->debugEchoIsOn) {
  628. echo $html;
  629. }
  630. $this->varDumpCalled = true;
  631. return $html;
  632. }
  633. /**
  634. * @internal ezsqlModel::varDump
  635. */
  636. public function dump_var($mixed = null)
  637. {
  638. return $this->varDump($mixed);
  639. }
  640. public function debug($print_to_screen = true)
  641. {
  642. // Start output buffering
  643. \ob_start();
  644. echo "\n\n<blockquote>";
  645. // Only show ezSQL credits once..
  646. if (!$this->debugCalled) {
  647. echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v" . \EZSQL_VERSION . ")\n <b>Debug.. \n</b></font><p>";
  648. }
  649. if ($this->lastError) {
  650. echo "<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>$this->lastError \n</b></font>]<p>";
  651. }
  652. if ($this->fromDiskCache) {
  653. echo "<font face=arial size=2 color=000099><b>Results retrieved from disk cache</b></font><p>\n";
  654. }
  655. echo "<font face=arial size=2 color=000099><b>Query</b> [$this->numQueries] \n<b>--</b>";
  656. echo "[<font color=000000><b>$this->lastQuery \n</b></font>]</font><p>";
  657. echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>\n";
  658. echo "<blockquote>\n";
  659. if ($this->colInfo) {
  660. // Results top rows
  661. echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>\n";
  662. echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>\n";
  663. for ($i = 0, $j = count($this->colInfo); $i < $j; $i++) {
  664. echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>\n";
  665. /* when `select` count(*) the maxlengh is not set, size is set instead. */
  666. if (isset($this->colInfo[$i]->type))
  667. echo "{$this->colInfo[$i]->type}";
  668. if (isset($this->colInfo[$i]->size))
  669. echo "{$this->colInfo[$i]->size}";
  670. if (isset($this->colInfo[$i]->max_length))
  671. echo "{$this->colInfo[$i]->max_length}";
  672. echo "\n</font><br><span style='font-family: arial; font-size: 10pt; font-weight: bold;'>";
  673. if (isset($this->colInfo[$i]->name))
  674. echo "{$this->colInfo[$i]->name}";
  675. echo "\n</span></td>";
  676. }
  677. echo "</tr>\n";
  678. // print main results
  679. if ($this->lastResult) {
  680. $i = 0;
  681. foreach ($this->get_results(null, \ARRAY_N) as $one_row) {
  682. $i++;
  683. echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i \n</font></td>";
  684. foreach ($one_row as $item) {
  685. echo "<td nowrap><font face=arial size=2>$item \n</font></td>";
  686. }
  687. echo "</tr>\n";
  688. }
  689. } else {
  690. // if last result
  691. echo "<tr bgcolor=ffffff><td colspan=" . (\count($this->colInfo) + 1) . "><font face=arial size=2>No Results</font></td></tr>\n";
  692. }
  693. echo "</table>\n";
  694. } else {
  695. // if col_info
  696. echo "<font face=arial size=2>No Results \n</font>";
  697. }
  698. //echo "</blockquote></blockquote>".$this->donation()."<hr noshade color=dddddd size=1>";
  699. // Stop output buffering and capture debug HTML
  700. $html = \ob_get_contents();
  701. \ob_end_clean();
  702. // Only echo output if it is turned on
  703. if ($this->debugEchoIsOn && $print_to_screen) {
  704. echo $html;
  705. }
  706. $this->debugCalled = true;
  707. return $html;
  708. }
  709. public function timer_get_cur()
  710. {
  711. list($usec, $sec) = \explode(" ", \microtime());
  712. return ((float) $usec + (float) $sec);
  713. }
  714. public function timer_start($timer_name)
  715. {
  716. $this->timers[$timer_name] = $this->timer_get_cur();
  717. }
  718. public function timer_elapsed($timer_name)
  719. {
  720. return \round($this->timer_get_cur() - $this->timers[$timer_name], 2);
  721. }
  722. public function timer_update_global($timer_name)
  723. {
  724. if ($this->doProfile) {
  725. $this->profileTimes[] = array(
  726. 'query' => $this->lastQuery,
  727. 'time' => $this->timer_elapsed($timer_name)
  728. );
  729. }
  730. $this->totalQueryTime += $this->timer_elapsed($timer_name);
  731. }
  732. public function count($all = true, $increase = false)
  733. {
  734. if ($increase) {
  735. $this->numQueries++;
  736. $this->connQueries++;
  737. }
  738. return ($all) ? $this->numQueries : $this->connQueries;
  739. }
  740. public function secureSetup(
  741. string $key = 'certificate.key',
  742. string $cert = 'certificate.crt',
  743. string $ca = 'cacert.pem',
  744. string $path = '.' . \_DS
  745. ) {
  746. if (!\file_exists($path . $cert) || !\file_exists($path . $key)) {
  747. $vendor = get_vendor();
  748. if (($vendor != \SQLITE) || ($vendor != \MSSQL))
  749. $path = create_certificate();
  750. } elseif ($path == '.' . \_DS) {
  751. $ssl_path = \getcwd();
  752. $path = \preg_replace('/\\\/', \_DS, $ssl_path) . \_DS;
  753. }
  754. $this->isSecure = true;
  755. $this->sslKey = $key;
  756. $this->sslCert = $cert;
  757. $this->sslCa = $ca;
  758. $this->sslPath = $path;
  759. }
  760. public function secureReset()
  761. {
  762. $this->isSecure = false;
  763. $this->sslKey = null;
  764. $this->sslCert = null;
  765. $this->sslCa = null;
  766. $this->sslPath = null;
  767. $this->secureOptions = null;
  768. }
  769. public function isConnected()
  770. {
  771. return $this->_connected;
  772. } // isConnected
  773. public function affectedRows()
  774. {
  775. return $this->_affectedRows;
  776. } // affectedRows
  777. public function queryResult()
  778. {
  779. return $this->lastResult;
  780. }
  781. public function tableSetup(string $name = '', string $prefix = '')
  782. {
  783. $this->table = $name;
  784. $this->prefix = $prefix;
  785. }
  786. } // ezsqlModel