DbConfigBuilder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. class LtDbConfigBuilder
  3. {
  4. protected $servers = array();
  5. protected $tables = array();
  6. protected $adapters = array(
  7. //"php_ext" => array("connection_adapter" => "", "sql_adapter" => "")
  8. "pgsql" => array("connection_adapter" => "pgsql", "sql_adapter" => "pgsql"),
  9. "pdo_pgsql" => array("connection_adapter" => "pdo", "sql_adapter" => "pgsql"),
  10. "oci" => array("connection_adapter" => "oci", "sql_adapter" => "oracle"),
  11. "pdo_oci" => array("connection_adapter" => "pdo", "sql_adapter" => "oracle"),
  12. "mssql" => array("connection_adapter" => "mssql", "sql_adapter" => "mssql"),
  13. "pdo_dblib" => array("connection_adapter" => "pdo", "sql_adapter" => "mssql"),
  14. "mysql" => array("connection_adapter" => "mysql", "sql_adapter" => "mysql"),
  15. "mysqli" => array("connection_adapter" => "mysqli", "sql_adapter" => "mysql"),
  16. "pdo_mysql" => array("connection_adapter" => "pdo", "sql_adapter" => "mysql"),
  17. "sqlite" => array("connection_adapter" => "sqlite", "sql_adapter" => "sqlite"),
  18. "sqlite3" => array("connection_adapter" => "sqlite3", "sql_adapter" => "sqlite"),
  19. "pdo_sqlite" => array("connection_adapter" => "pdo", "sql_adapter" => "sqlite"),
  20. );
  21. protected $defaultConfig = array(
  22. "host" => "localhost", //some ip, hostname
  23. //"port" => 3306,
  24. "username" => "root",
  25. "password" => null,
  26. //"adapter" => "mysql", //mysql,mysqli,pdo_mysql,sqlite,pdo_sqlite
  27. "charset" => "UTF-8",
  28. "pconnect" => true, //true,false
  29. "connection_ttl" => 3600, //any seconds
  30. "dbname" => null, //default dbname
  31. "schema" => null, //default schema
  32. "connection_adapter" => null,
  33. "sql_adapter" => null,
  34. );
  35. protected $defaultAdapterConfigs = array(
  36. "pgsql" => array(
  37. "port" => 5432,
  38. ),
  39. "oracle" => array(
  40. "port" => 1521,
  41. ),
  42. "mssql" => array(
  43. "port" => 1433,
  44. ),
  45. "mysql" => array(
  46. "port" => 3306,
  47. "pconnect" => false,
  48. "connection_ttl" => 30,
  49. ),
  50. );
  51. public function addSingleHost($hostConfig)
  52. {
  53. $this->addHost("group_0", "node_0", "master", $hostConfig);
  54. }
  55. public function addHost($groupId, $nodeId = "node_0", $role = "master", $hostConfig)
  56. {
  57. if (isset($this->servers[$groupId][$nodeId][$role]))
  58. {//以相同role的第一个host为默认配置
  59. $ref = $this->servers[$groupId][$nodeId][$role][0];
  60. }
  61. else if ("slave" == $role && isset($this->servers[$groupId][$nodeId]["master"]))
  62. {//slave host以master的第一个host为默认配置
  63. $ref = $this->servers[$groupId][$nodeId]["master"][0];
  64. }
  65. else if (isset($this->servers[$groupId]) && count($this->servers[$groupId]))
  66. {//以本group第一个node的master第一个host为默认配置
  67. $refNode = key($this->servers[$groupId]);
  68. $ref = $this->servers[$groupId][$refNode]["master"][0];
  69. }
  70. else
  71. {
  72. if (!isset($hostConfig["adapter"]))
  73. {
  74. trigger_error("No db adapter specified");
  75. }
  76. $ref = $this->defaultConfig;
  77. if (isset($this->defaultAdapterConfigs[$this->adapters[$hostConfig["adapter"]]["sql_adapter"]]))
  78. {
  79. $ref = array_merge($ref, $this->defaultAdapterConfigs[$this->adapters[$hostConfig["adapter"]]["sql_adapter"]]);
  80. }
  81. }
  82. $conf = array_merge($ref, $hostConfig);
  83. $conf = array_merge($conf, $this->adapters[$conf["adapter"]]);
  84. $conf = $this->convertDbnameToSchema($conf);
  85. $this->servers[$groupId][$nodeId][$role][] = $conf;
  86. }
  87. public function getServers()
  88. {
  89. return $this->servers;
  90. }
  91. public function getTables()
  92. {
  93. return $this->tables;
  94. }
  95. public function buildTablesConfig()
  96. {
  97. }
  98. /**
  99. * Convert dbname to schema for: FrontBase, MySQL, mSQL, MS SQL Server, MaxDB, Sybase
  100. * See: http://www.php.net/manual-lookup.php?pattern=_select_db
  101. */
  102. protected function convertDbnameToSchema($conf)
  103. {
  104. if (preg_match("/fbsql|mysql|msql|mssql|maxdb|sybase/i", $conf["sql_adapter"]) && isset($conf["dbname"]))
  105. {
  106. $conf["schema"] = $conf["dbname"];
  107. $conf["dbname"] = null;
  108. }
  109. return $conf;
  110. }
  111. }