ShardManager.php 2.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

namespace Doctrine\DBAL\Sharding;

/**
 * Sharding Manager gives access to APIs to implementing sharding on top of
 * Doctrine\DBAL\Connection instances.
 *
 * For simplicity and developer ease-of-use (and understanding) the sharding
 * API only covers single shard queries, no fan-out support. It is primarily
 * suited for multi-tenant applications.
 *
 * The assumption about sharding here
 * is that a distribution value can be found that gives access to all the
 * necessary data for all use-cases. Switching between shards should be done with
 * caution, especially if lazy loading is implemented. Any query is always
 * executed against the last shard that was selected. If a query is created for
Pascal Borreli's avatar
Pascal Borreli committed
18
 * a shard Y but then a shard X is selected when its actually executed you
19 20 21 22 23
 * will hit the wrong shard.
 */
interface ShardManager
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
24
     * Selects global database with global data.
25 26 27 28 29 30
     *
     * This is the default database that is connected when no shard is
     * selected.
     *
     * @return void
     */
31
    public function selectGlobal();
32 33

    /**
Benjamin Morel's avatar
Benjamin Morel committed
34 35 36
     * Selects the shard against which the queries after this statement will be issued.
     *
     * @param string $distributionValue
37 38
     *
     * @return void
Benjamin Morel's avatar
Benjamin Morel committed
39
     *
40
     * @throws ShardingException If no value is passed as shard identifier.
41
     */
42
    public function selectShard($distributionValue);
43 44

    /**
Benjamin Morel's avatar
Benjamin Morel committed
45
     * Gets the distribution value currently used for sharding.
46
     *
47
     * @return string|null
48
     */
49
    public function getCurrentDistributionValue();
50 51

    /**
Benjamin Morel's avatar
Benjamin Morel committed
52
     * Gets information about the amount of shards and other details.
53
     *
54 55
     * Format is implementation specific, each shard is one element and has an
     * 'id' attribute at least.
56
     *
57
     * @return mixed[][]
58
     */
59
    public function getShards();
60 61

    /**
Benjamin Morel's avatar
Benjamin Morel committed
62
     * Queries all shards in undefined order and return the results appended to
63 64
     * each other. Restore the previous distribution value after execution.
     *
Benjamin Morel's avatar
Benjamin Morel committed
65
     * Using {@link \Doctrine\DBAL\Connection::fetchAll} to retrieve rows internally.
66
     *
67 68 69
     * @param string         $sql
     * @param mixed[]        $params
     * @param int[]|string[] $types
Benjamin Morel's avatar
Benjamin Morel committed
70
     *
71
     * @return mixed[]
72
     */
73
    public function queryAll($sql, array $params, array $types);
74
}