No Description

Db.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. class LtDb
  3. {
  4. public $configHandle;
  5. public $group;
  6. public $node;
  7. protected $dbh;
  8. public function __construct()
  9. {
  10. if (! $this->configHandle instanceof LtConfig)
  11. {
  12. if (class_exists("LtObjectUtil", false))
  13. {
  14. $this->configHandle = LtObjectUtil::singleton("LtConfig");
  15. }
  16. else
  17. {
  18. $this->configHandle = new LtConfig;
  19. }
  20. }
  21. }
  22. public function init()
  23. {
  24. $this->dbh = new LtDbHandle;
  25. $this->dbh->configHandle = $this->configHandle;
  26. $this->dbh->group = $this->getGroup();
  27. $this->dbh->node = $this->getNode();
  28. $this->dbh->init();
  29. }
  30. public function getDbHandle()
  31. {
  32. return $this->dbh;
  33. }
  34. public function getTDG($tableName)
  35. {
  36. $tg = new LtDbTableDataGateway;
  37. $tg->configHandle = $this->configHandle;
  38. $tg->tableName = $tableName;
  39. $tg->createdColumn = 'created';
  40. $tg->modifiedColumn = 'modified';
  41. $tg->dbh = $this->dbh;
  42. return $tg;
  43. }
  44. public function getSqlMapClient()
  45. {
  46. $smc = new LtDbSqlMapClient;
  47. $smc->configHandle = $this->configHandle;
  48. $smc->dbh = $this->dbh;
  49. return $smc;
  50. }
  51. public function changeNode($node)
  52. {
  53. $this->node = $node;
  54. $this->dbh->node = $node;
  55. }
  56. protected function getGroup()
  57. {
  58. if ($this->group)
  59. {
  60. return $this->group;
  61. }
  62. $servers = $this->configHandle->get("db.servers");
  63. if (1 == count($servers))
  64. {
  65. return key($servers);
  66. }
  67. return false;
  68. }
  69. protected function getNode()
  70. {
  71. if ($this->node)
  72. {
  73. return $this->node;
  74. }
  75. $servers = $this->configHandle->get("db.servers");
  76. if (1 == count($servers[$this->getGroup()]))
  77. {
  78. return key($servers[$this->getGroup()]);
  79. }
  80. return false;
  81. }
  82. }