Example #1
0
/**
 * Запуск скрипта
 *
 * @param array &$ServiceManager объект сервис-менеджера
 */
function run(array &$ServiceManager)
{
    $MysqlFactory =& ServiceManager\getFactory($ServiceManager, 'Mysql');
    $shardsConfig = MysqlFactory\getShardsConfig($MysqlFactory);
    // Заказчики
    $CustomerRepository =& ServiceManager\get($ServiceManager, 'CustomerRepository');
    $maxCustomerId = 1;
    foreach ($shardsConfig[CustomerRepository\SHARD_CONFIG] as $shardId => $_) {
        $Mysql = MysqlFactory\createShard($MysqlFactory, CustomerRepository\SHARD_CONFIG, $shardId);
        $data = Mysql\query($Mysql, 'SELECT * FROM ' . CustomerRepository\DATABASE_NAME . '.' . CustomerRepository\TABLE_NAME);
        while (($customerData = Mysql\fetchAssoc($Mysql, $data)) !== false) {
            $Customer =& Customer\unserializeFromMysql($customerData);
            CustomerRepository\saveToMemcached($CustomerRepository, $Customer);
            CustomerRepository\savePasswordHashToMemcached($CustomerRepository, $Customer, $customerData['password_hash']);
            $maxCustomerId = max($maxCustomerId, Customer\getId($Customer));
        }
    }
    CustomerRepository\syncLastCustomerId($CustomerRepository, $maxCustomerId);
    // Исполнители
    $ExecutorRepository =& ServiceManager\get($ServiceManager, 'ExecutorRepository');
    $maxExecutorId = 1;
    foreach ($shardsConfig[ExecutorRepository\SHARD_CONFIG] as $shardId => $_) {
        $Mysql =& MysqlFactory\createShard($MysqlFactory, ExecutorRepository\SHARD_CONFIG, $shardId);
        $data = Mysql\query($Mysql, 'SELECT * FROM ' . ExecutorRepository\TABLE_NAME . '.' . ExecutorRepository\TABLE_NAME);
        while (($executorData = Mysql\fetchAssoc($Mysql, $data)) !== false) {
            $Executor =& Executor\unserializeFromMysql($executorData);
            ExecutorRepository\saveLoginToMemcached($ExecutorRepository, Executor\getLogin($Executor), Executor\getId($Executor));
            ExecutorRepository\savePasswordHashToMemcached($ExecutorRepository, $Executor, $executorData['password_hash']);
            $maxExecutorId = max($maxExecutorId, Executor\getId($Executor));
        }
    }
    ExecutorRepository\syncLastExecutorId($ExecutorRepository, $maxExecutorId);
    // Заказы
    $OrderRepository =& ServiceManager\get($ServiceManager, 'OrderRepository');
    foreach (OrderRepository\fetchAll($OrderRepository) as $Order) {
        OrderRepository\savePriceToMemcached($OrderRepository, Order\getPrice($Order));
    }
}
/**
 * Обновление зарплаты исполнителя
 *
 * @param array &$ExecutorRepository объект репозитория исполнителя
 * @param array &$Executor объект исполнителя
 * @param int $money на сколько изменить зарплату
 */
function updateSalary(array &$ExecutorRepository, array &$Executor, $money)
{
    $Mysql =& MysqlFactory\createShard(getMysqlFactory($ExecutorRepository), SHARD_CONFIG, getShardId(Executor\getId($Executor)));
    Mysql\query($Mysql, '
        UPDATE ' . DATABASE_NAME . '.' . TABLE_NAME . '
        SET SALARY = SALARY + ' . (int) $money . '
        WHERE id = ' . (int) Executor\getId($Executor));
    Executor\setSalary($Executor, Executor\getSalary($Executor) + (int) $money);
}
/**
 * Создаёт новый шард в mysql
 *
 * @param array &$CustomerRepository объект репозитория заказчика
 * @param int $shardId id шарда
 *
 * @return bool успех создания
 */
function createShard(array &$CustomerRepository, $shardId)
{
    $MysqlFactory =& getMysqlFactory($CustomerRepository);
    $Mysql =& MysqlFactory\createShard($MysqlFactory, SHARD_CONFIG, $shardId);
    Mysql\query($Mysql, 'CREATE DATABASE ' . DATABASE_NAME . ' CHARACTER SET utf8 COLLATE utf8_unicode_ci');
    return Mysql\query($Mysql, '
        CREATE TABLE ' . DATABASE_NAME . '.' . TABLE_NAME . ' (
          id            INT UNSIGNED  NOT NULL,
          login         VARCHAR(16)   NOT NULL,
          fio           VARCHAR(32)   NOT NULL,
          password_hash VARCHAR(60)   NOT NULL
        ) ENGINE=InnoDB;
    ');
}