1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| <?php declare (strict_types = 1);
namespace app\util;
use think\facade\Db;
class Subtable {
private static $tables = [ 'users' => 16, 'order' => 32, ];
public static function db(string $name, ?int $uid = null): Db { return Db::table(self::name($name, $uid)); }
public static function name(string $name, ?int $uid = null): string { $index = self::index($name, $uid);
return sprintf('%s_%s', $name, $index); }
public static function index(string $name, ?int $uid = null) :string { if(is_null($uid)) { throw new \Exception('UID 值异常'); }
if(!isset(self::$tables[$name])) { throw new \Exception(sprintf('不支持当前 %s 表', $name)); }
if((int) $uid === 0) { throw new \Exception('UID 值异常'); }
return sprintf('%x', $uid % self::$tables[$name]); }
public static function number(string $name) :int { if(!isset(self::$tables[$name])) { throw new \Exception(sprintf('不支持当前 %s 表', $name)); }
return (int) self::$tables[$name]; }
public static function union(string $name, string $field = '*', string $other = '', ?bool $has_all = true) :string { $number = self::number($name);
$sql = '';
for($i = 0; $i < $number; $i++) { if($i !== 0) { $sql .= $has_all ? ' UNION ALL ' : ' UNION '; }
$sql .= sprintf('SELECT %s FROM %s_%x %s', $field, $name, $i, $other); }
return $sql; } }
|