PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/code/classes/pinetd/SQL.class.php

https://github.com/blekkzor/pinetd2
PHP | 71 lines | 55 code | 4 blank | 12 comment | 4 complexity | 0df2be87d8cfb73319a23a792a05ca3c MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. namespace pinetd;
  3. use \Exception;
  4. class SQL {
  5. static private $inst = array(); // instances list
  6. /**
  7. * @brief Current process has been forked.
  8. * @internal
  9. *
  10. * This method is called by the child after forking.
  11. */
  12. static public function forked() {
  13. self::$inst = array();
  14. }
  15. /**
  16. * @brief Generate an unique instance of a SQL class
  17. *
  18. * This function will take a "Storage" localConfig entry. Typical call:
  19. * $sql = SQL::factory($this->localConfig['Storage']);
  20. */
  21. static public function factory($config) {
  22. $type_f = null;
  23. foreach($config as $type => $settings) {
  24. if ($type != '_') $type_f = $type;
  25. }
  26. if (is_null($type_f)) throw new Exception('SQL Factory: no SQL config provided');
  27. $type = $type_f;
  28. $settings = $config[$type];
  29. $key = self::genKey($type, $settings);
  30. if (isset(self::$inst[$key])) return self::$inst[$key];
  31. $class = 'pinetd\\SQL\\'.$type;
  32. self::$inst[$key] = new $class($settings);
  33. return self::$inst[$key];
  34. }
  35. /**
  36. * @brief Parent has forked signal, from parent
  37. * @internal
  38. */
  39. static public function parentForked() {
  40. foreach(self::$inst as &$class) $class->reconnect();
  41. }
  42. static private function genKey($type, array $cfg) {
  43. ksort($cfg);
  44. $sum = $type;
  45. foreach($cfg as $var => $val) $sum.=':'.$var.'='.$val;
  46. return md5($sum);
  47. }
  48. }
  49. /**
  50. * @brief Class for SQL expressions
  51. */
  52. class Expr {
  53. private $value;
  54. public function __construct($value) {
  55. $this->value = $value;
  56. }
  57. public function __toString() {
  58. return $value;
  59. }
  60. }