示例#1
0
 public function getDbName($logic_column_value)
 {
     if (parent::getIsSingleDb()) {
         return parent::getSingleDbName();
     }
     if (parent::getIsDateDb()) {
         return parent::get_date_db_name();
     }
     if (parent::getDbLogicColumnType() && parent::getDbLogicColumnType() == 'string' && !is_numeric($logic_column_value)) {
         $logic_column_value = cls_dbroute::strToInt($logic_column_value);
     }
     if (parent::getConsistentHashOneDbOneTable()) {
         //每库一表时
         $mod = $logic_column_value;
     } else {
         $mod = intval($logic_column_value % $this->getConsistentHashSeparateModMaxValue());
     }
     $default_db_name = null;
     $db_name = null;
     foreach ($this->getNodeList() as $node) {
         if ($mod >= $node->getStart() && $mod < $node->getEnd()) {
             $db_name = $node->getDbName();
             break;
         }
         if ($node->getIsDefaultDb()) {
             $default_db_name = $node->getDbName();
         }
     }
     $return_db = $db_name ? $db_name : $default_db_name;
     if (empty($return_db)) {
         throw new DBRouteException('未找到db_name');
     }
     return $return_db;
 }