Exemplo n.º 1
0
function create_queries($db, $from_table, $start, $end, $toEntity, $per_limits = 50)
{
    global $mongoClient;
    $data = $queries = [];
    $tableObj = new ReflectionClass($toEntity);
    $table = $tableObj->getConstant('TABLE_NAME');
    $ref = $tableObj->getConstant('REF');
    $props = $tableObj->getDefaultProperties();
    $columns = array_keys($props);
    $collection = $mongoClient->selectCollection($db, $from_table);
    $cursor = $collection->find(['createTime' => ['$gt' => $start, '$lte' => $end]]);
    $inserts = [];
    while ($cursor->hasNext()) {
        $temp = $cursor->getNext();
        $data = [];
        foreach ($columns as $col) {
            if (isset($ref[$col]) && isset($temp[$ref[$col]])) {
                $data[] = $temp[$ref[$col]];
            } elseif (isset($temp[$col])) {
                $data[] = $temp[$col];
            } else {
                $data[] = '';
            }
        }
        if ($temp['createTime'] == $temp['updateTime'] || !$temp['updateTime']) {
            $inserts[] = $data;
        } else {
            $queries[] = create_update_query($data, $table, $columns);
        }
        if (count($inserts) == $per_limits) {
            $queries = array_merge($queries, create_insert_query($inserts, $table, $columns));
            $inserts = [];
        }
        //        create_insert_query($data, $table, $columns, $data);
    }
    $queries = array_merge($queries, create_insert_query($inserts, $table, $columns));
    return $queries;
}
Exemplo n.º 2
0
        trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
    }
}
function create_insert_query($line, $db_name, $table_name)
{
    $line_array = explode(',', $line);
    if ($line_array[0] != 'id') {
        $query = "insert into `{$db_name}`.`{$table_name}` (`id`, `postcode`, `latitude`, `longitude`) values ('{$line_array['0']}', '{$line_array['1']}', '{$line_array['2']}', '{$line_array['3']}');";
        return $query;
    }
}
$table_creation_query = "CREATE TABLE `{$table_name}` (`id` int(11) NOT NULL, `postcode` varchar(11) NOT NULL, `latitude` float NOT NULL, `longitude` float NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `postcode` (`postcode`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
if ($echo) {
    echo $table_creation_query;
} else {
    $conn->query($table_creation_query);
}
$handle = fopen($file_path, "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $query = create_insert_query($line, $db_name, $table_name);
        if ($echo) {
            echo $query;
        } else {
            $conn->query($query);
        }
    }
} else {
    echo "Error reading file.";
}
fclose($handle);