Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@
"entry": "ServiceBuilder.php"
}
}
}
}
4 changes: 2 additions & 2 deletions src/BigQuery/BigQueryClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Google\Cloud\BigQuery\Connection\ConnectionInterface;
use Google\Cloud\BigQuery\Connection\Rest;
use Google\Cloud\BigQuery\Job;
use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\ExponentialBackoff;
use Google\Cloud\Core\Int64;
Expand All @@ -43,7 +42,6 @@
*/
class BigQueryClient
{
use ArrayTrait;

This comment was marked as spam.

use ClientTrait;
use JobConfigurationTrait;

Expand Down Expand Up @@ -254,6 +252,8 @@ public function runQuery($query, array $options = [])
* If not provided default settings will be used, with the exception
* of `configuration.query.useLegacySql`, which defaults to `false`
* in this client.
* @type string $jobIdPrefix If given, the returned job ID will be of
* format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* }
* @return Job
*/
Expand Down
29 changes: 29 additions & 0 deletions src/BigQuery/JobConfigurationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@

namespace Google\Cloud\BigQuery;

use Google\Cloud\Core\ArrayTrait;
use Ramsey\Uuid\Uuid;

/**
* A trait used to build out configuration for jobs.
*/
trait JobConfigurationTrait
{
use ArrayTrait;

/**
* Builds a configuration for a job.
*
Expand All @@ -33,6 +38,8 @@ trait JobConfigurationTrait
*/
public function buildJobConfig($name, $projectId, array $config, array $userDefinedOptions)
{
$jobIdPrefix = $this->pluck('jobIdPrefix', $userDefinedOptions, false);

if (isset($userDefinedOptions['jobConfig'])) {
$config = $userDefinedOptions['jobConfig'] + $config;
}
Expand All @@ -41,9 +48,31 @@ public function buildJobConfig($name, $projectId, array $config, array $userDefi

return [
'projectId' => $projectId,
'jobReference' => [
'projectId' => $projectId,
'jobId' => $this->generateJobId($jobIdPrefix)
],
'configuration' => [
$name => $config
]
] + $userDefinedOptions;
}

/**
* Generate a Job ID with an optional user-defined prefix.
*
* @param string $jobIdPrefix [optional] If given, the returned job ID will
* be of format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* @return string
*/
protected function generateJobId($jobIdPrefix = null)
{
$jobId = '';

if ($jobIdPrefix) {
$jobId = $jobIdPrefix . '-';
}

return $jobId . Uuid::uuid4();
}
}
8 changes: 6 additions & 2 deletions src/BigQuery/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
namespace Google\Cloud\BigQuery;

use Google\Cloud\BigQuery\Connection\ConnectionInterface;
use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\ConcurrencyControlTrait;
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Core\Iterator\ItemIterator;
Expand All @@ -33,7 +32,6 @@
*/
class Table
{
use ArrayTrait;
use ConcurrencyControlTrait;
use JobConfigurationTrait;

Expand Down Expand Up @@ -239,6 +237,8 @@ function (array $row) use ($schema) {
* @type array $jobConfig Configuration settings for a copy job are
* outlined in the [API Docs for `configuration.copy`](https://goo.gl/m8dro9).
* If not provided default settings will be used.
* @type string $jobIdPrefix If given, the returned job ID will be of
* format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* }
* @return Job
*/
Expand Down Expand Up @@ -287,6 +287,8 @@ public function copy(Table $destination, array $options = [])
* @type array $jobConfig Configuration settings for an extract job are
* outlined in the [API Docs for `configuration.extract`](https://goo.gl/SQ9XAE).
* If not provided default settings will be used.
* @type string $jobIdPrefix If given, the returned job ID will be of
* format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* }
* @return Job
*/
Expand Down Expand Up @@ -334,6 +336,8 @@ public function export($destination, array $options = [])
* @type array $jobConfig Configuration settings for a load job are
* outlined in the [API Docs for `configuration.load`](https://goo.gl/j6jyHv).
* If not provided default settings will be used.
* @type string $jobIdPrefix If given, the returned job ID will be of
* format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* }
* @return Job
*/
Expand Down
3 changes: 2 additions & 1 deletion src/BigQuery/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "Apache-2.0",
"minimum-stability": "stable",
"require": {
"google/cloud-core": "^1.0"
"google/cloud-core": "^1.0",
"ramsey/uuid": "~3"
},
"suggest": {
"google/cloud-storage": "Makes it easier to load data from Cloud Storage into BigQuery"
Expand Down
14 changes: 13 additions & 1 deletion tests/snippets/BigQuery/BigQueryClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*/
class BigQueryClientTest extends SnippetTestCase
{
const JOBID = 'myJobId';

private $connection;
private $client;
private $result = [
Expand Down Expand Up @@ -63,7 +65,7 @@ class BigQueryClientTest extends SnippetTestCase
public function setUp()
{
$this->connection = $this->prophesize(ConnectionInterface::class);
$this->client = \Google\Cloud\Dev\stub(BigQueryClient::class);
$this->client = \Google\Cloud\Dev\stub(BigQueryTestClient::class);
$this->client->___setProperty('connection', $this->connection->reveal());
}

Expand Down Expand Up @@ -106,6 +108,7 @@ public function testRunQueryWithNamedParameters()
$this->connection
->insertJob([
'projectId' => 'my-awesome-project',
'jobReference' => ['projectId' => 'my-awesome-project', 'jobId' => self::JOBID],
'configuration' => [
'query' => [
'parameterMode' => 'named',
Expand Down Expand Up @@ -159,6 +162,7 @@ public function testRunQueryWithPositionalParameters()
$this->connection
->insertJob([
'projectId' => 'my-awesome-project',
'jobReference' => ['projectId' => 'my-awesome-project', 'jobId' => self::JOBID],
'configuration' => [
'query' => [
'parameterMode' => 'positional',
Expand Down Expand Up @@ -351,3 +355,11 @@ public function testTimestamp()
$this->assertInstanceOf(Timestamp::class, $res->returnVal());
}
}

class BigQueryTestClient extends BigQueryClient
{
protected function generateJobId($jobIdPrefix = null)
{
return $jobIdPrefix ? $jobIdPrefix . '-' . BigQueryClientTest::JOBID : BigQueryClientTest::JOBID;
}
}
Loading