ShardManager.php 3.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * <http://www.doctrine-project.org>.
 */

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
35
 * a shard Y but then a shard X is selected when its actually executed you
36 37 38 39 40 41 42
 * will hit the wrong shard.
 *
 * @author Benjamin Eberlei <kontakt@beberlei.de>
 */
interface ShardManager
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
43
     * Selects global database with global data.
44 45 46 47 48 49 50 51 52
     *
     * This is the default database that is connected when no shard is
     * selected.
     *
     * @return void
     */
    function selectGlobal();

    /**
Benjamin Morel's avatar
Benjamin Morel committed
53 54 55
     * Selects the shard against which the queries after this statement will be issued.
     *
     * @param string $distributionValue
56 57
     *
     * @return void
Benjamin Morel's avatar
Benjamin Morel committed
58 59
     *
     * @throws \Doctrine\DBAL\Sharding\ShardingException If no value is passed as shard identifier.
60 61 62 63
     */
    function selectShard($distributionValue);

    /**
Benjamin Morel's avatar
Benjamin Morel committed
64
     * Gets the distribution value currently used for sharding.
65
     *
66
     * @return string|null
67 68 69 70
     */
    function getCurrentDistributionValue();

    /**
Benjamin Morel's avatar
Benjamin Morel committed
71
     * Gets information about the amount of shards and other details.
72
     *
73 74
     * Format is implementation specific, each shard is one element and has an
     * 'id' attribute at least.
75 76 77 78 79 80
     *
     * @return array
     */
    function getShards();

    /**
Benjamin Morel's avatar
Benjamin Morel committed
81
     * Queries all shards in undefined order and return the results appended to
82 83
     * each other. Restore the previous distribution value after execution.
     *
Benjamin Morel's avatar
Benjamin Morel committed
84
     * Using {@link \Doctrine\DBAL\Connection::fetchAll} to retrieve rows internally.
85 86
     *
     * @param string $sql
Benjamin Morel's avatar
Benjamin Morel committed
87 88 89
     * @param array  $params
     * @param array  $types
     *
90 91 92 93
     * @return array
     */
    function queryAll($sql, array $params, array $types);
}