Skip to content

Commit 78af75c

Browse files
committed
Update exceptions, examples
1 parent e13535b commit 78af75c

34 files changed

+184
-153
lines changed

examples/app/Commands/BasicExampleCommand.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ protected function createTables()
167167
->primaryKey('series_id')
168168
);
169169

170-
$this->print('Table `series` has been created.');
171-
172170
});
173171

172+
$this->print('Table `series` has been created.');
173+
174174
$this->ydb->table()->retrySession(function (Session $session) {
175175

176176
$session->createTable(
@@ -184,10 +184,10 @@ protected function createTables()
184184
->primaryKey(['series_id', 'season_id'])
185185
);
186186

187-
$this->print('Table `seasons` has been created.');
188-
189187
});
190188

189+
$this->print('Table `seasons` has been created.');
190+
191191
$this->ydb->table()->retrySession(function (Session $session) {
192192

193193
$session->createTable(
@@ -201,37 +201,37 @@ protected function createTables()
201201
->primaryKey(['series_id', 'season_id', 'episode_id'])
202202
);
203203

204-
$this->print('Table `episodes` has been created.');
205-
206204
});
205+
206+
$this->print('Table `episodes` has been created.');
207207
}
208208

209209
/**
210210
* @param string $table
211211
*/
212212
protected function describeTable($table)
213213
{
214-
$this->ydb->table()->retrySession(function (Session $session) use ($table) {
214+
$data = $this->ydb->table()->retrySession(function (Session $session) use ($table) {
215+
216+
return $session->describeTable($table);
215217

216-
$data = $session->describeTable($table);
218+
});
217219

218-
$columns = [];
220+
$columns = [];
219221

220-
foreach ($data['columns'] as $column) {
221-
if (isset($column['type']['optionalType']['item']['typeId'])) {
222-
$columns[] = [
223-
'Name' => $column['name'],
224-
'Type' => $column['type']['optionalType']['item']['typeId'],
225-
];
226-
}
222+
foreach ($data['columns'] as $column) {
223+
if (isset($column['type']['optionalType']['item']['typeId'])) {
224+
$columns[] = [
225+
'Name' => $column['name'],
226+
'Type' => $column['type']['optionalType']['item']['typeId'],
227+
];
227228
}
229+
}
228230

229-
$this->print('Table `' . $table . '`');
230-
$this->print($columns);
231-
$this->print('');
232-
$this->print('Primary key: ' . implode(', ', (array)$data['primaryKey']));
233-
234-
});
231+
$this->print('Table `' . $table . '`');
232+
$this->print($columns);
233+
$this->print('');
234+
$this->print('Primary key: ' . implode(', ', (array)$data['primaryKey']));
235235
}
236236

237237
protected function fillTablesWithData()
@@ -271,11 +271,11 @@ protected function selectSimple()
271271

272272
protected function upsertSimple()
273273
{
274-
$this->ydb->table()->transaction(function (Session $session) {
274+
$this->ydb->table()->retryTransaction(function (Session $session) {
275275
return $session->query('
276276
UPSERT INTO episodes (series_id, season_id, episode_id, title)
277277
VALUES (2, 6, 1, "TBD");');
278-
});
278+
}, true);
279279

280280
$this->print('Finished.');
281281
}
@@ -296,7 +296,7 @@ protected function bulkUpsert()
296296
'air_date' => 'Uint64',
297297
]
298298
);
299-
});
299+
}, true);
300300

301301
$this->print('Finished.');
302302
}
@@ -308,7 +308,7 @@ protected function bulkUpsert()
308308
*/
309309
protected function selectPrepared($series_id, $season_id, $episode_id)
310310
{
311-
$this->ydb->table()->retryTransaction(function (Session $session) use ($series_id, $season_id, $episode_id) {
311+
$result = $this->ydb->table()->retryTransaction(function (Session $session) use ($series_id, $season_id, $episode_id) {
312312

313313
$prepared_query = $session->prepare('
314314
DECLARE $series_id AS Uint64;
@@ -322,14 +322,14 @@ protected function selectPrepared($series_id, $season_id, $episode_id)
322322
FROM episodes
323323
WHERE series_id = $series_id AND season_id = $season_id AND episode_id = $episode_id;');
324324

325-
$result = $prepared_query->execute(compact(
325+
return $prepared_query->execute(compact(
326326
'series_id',
327327
'season_id',
328328
'episode_id'
329329
));
330+
},true);
330331

331-
$this->print($result->rows());
332-
});
332+
$this->print($result->rows());
333333
}
334334

335335
/**

examples/app/Commands/CreateTableCommand.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
4949

5050
$ydb = $this->appService->initYdb();
5151

52-
$ydb->table()->retrySession(function (Session $session) use ($output, $columns, $table_name) {
52+
$ydb->table()->retrySession(function (Session $session) use ($columns, $table_name) {
53+
$session->createTable($table_name, $columns, 'id');
54+
},true);
5355

54-
$result = $session->createTable($table_name, $columns, 'id');
56+
$output->writeln('Table ' . $table_name . ' has been created.');
5557

56-
$output->writeln('Table ' . $table_name . ' has been created.');
57-
58-
$session->transaction(function($session) use ($table_name, $columns) {
58+
$ydb->table()->retryTransaction(function (Session $session) use ($columns, $table_name) {
5959
$session->query('upsert into `' . $table_name . '` (`' . implode('`, `', array_keys($columns)) . '`) values (' . implode('), (', $this->getData()) . ');');
60-
});
61-
62-
$output->writeln('Table ' . $table_name . ' has been populated with some data.');
63-
});
60+
},true);
61+
$output->writeln('Table ' . $table_name . ' has been populated with some data.');
6462

6563
return Command::SUCCESS;
6664
}

examples/app/Commands/ListDirectoryCommand.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,33 @@ protected function execute(InputInterface $input, OutputInterface $output)
4747

4848
$ydb = $this->appService->initYdb();
4949

50-
$ydb->retry(function (Ydb $ydb) use ($output, $dirname) {
50+
$result = $ydb->retry(function (Ydb $ydb) use ($output, $dirname) {
5151

5252
$scheme = $ydb->scheme();
5353

54-
$result = $scheme->listDirectory($dirname);
55-
56-
if (!empty($result))
57-
{
58-
$t = new Table($output);
59-
$t
60-
->setHeaders(['name', 'type', 'owner'])
61-
->setRows(array_map(function($row) {
62-
return [
63-
$row['name'] ?? null,
64-
$row['type'] ?? null,
65-
$row['owner'] ?? null,
66-
];
67-
}, $result))
68-
;
69-
$t->render();
70-
}
71-
else
72-
{
73-
$output->writeln('Empty directory');
74-
}
54+
return $scheme->listDirectory($dirname);
7555

7656
});
7757

78-
58+
if (!empty($result))
59+
{
60+
$t = new Table($output);
61+
$t
62+
->setHeaders(['name', 'type', 'owner'])
63+
->setRows(array_map(function($row) {
64+
return [
65+
$row['name'] ?? null,
66+
$row['type'] ?? null,
67+
$row['owner'] ?? null,
68+
];
69+
}, $result))
70+
;
71+
$t->render();
72+
}
73+
else
74+
{
75+
$output->writeln('Empty directory');
76+
}
7977

8078
return Command::SUCCESS;
8179
}

examples/app/Commands/ListEndpointsCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Command\Command;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
9+
use YdbPlatform\Ydb\Ydb;
910

1011
class ListEndpointsCommand extends Command
1112
{
@@ -40,16 +41,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
4041
{
4142
$ydb = $this->appService->initYdb();
4243

43-
$ydb->retry(function (Ydb $ydb) use ($output) {
44+
$result = $ydb->retry(function (Ydb $ydb) use ($output) {
4445

4546
$discovery = $ydb->discovery();
4647

47-
$result = $discovery->listEndpoints();
48-
49-
$output->writeln(json_encode($result, 480));
48+
return $discovery->listEndpoints();
5049

5150
});
5251

52+
$output->writeln(json_encode($result, 480));
53+
5354
return Command::SUCCESS;
5455
}
5556
}

examples/app/Commands/MakeDirectoryCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
4646

4747
$ydb = $this->appService->initYdb();
4848

49-
$ydb->retry(function (Ydb $ydb) use ($output, $dirname) {
49+
$result = $ydb->retry(function (Ydb $ydb) use ($output, $dirname) {
5050
$scheme = $ydb->scheme();
5151

52-
$result = $scheme->makeDirectory($dirname);
53-
54-
$output->writeln(json_encode($result, 480));
52+
return $scheme->makeDirectory($dirname);
5553
});
5654

55+
$output->writeln(json_encode($result, 480));
56+
5757
return Command::SUCCESS;
5858
}
5959
}

examples/app/Commands/ReadTableCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Console\Input\InputArgument;
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Output\OutputInterface;
11+
use YdbPlatform\Ydb\Exception;
1112
use YdbPlatform\Ydb\Ydb;
1213

1314
class ReadTableCommand extends Command
@@ -48,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4849

4950
$ydb = $this->appService->initYdb();
5051

51-
$ydb->retry(function (Ydb $ydb) use ($table_name, $output) {
52+
$return = $ydb->retry(function (Ydb $ydb) use ($table_name, $output) {
5253
$table = $ydb->table();
5354

5455
$description = $table->describeTable($table_name);
@@ -62,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6263

6364
if ( ! $columns)
6465
{
65-
throw new \Exception('Failed to get columns for table ' . $table_name);
66+
throw new Exception('Failed to get columns for table ' . $table_name);
6667
}
6768

6869
$options = [

examples/app/Commands/RemoveDirectoryCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
4646

4747
$ydb = $this->appService->initYdb();
4848

49-
$ydb->retry(function (Ydb $ydb) use ($output, $dirname) {
49+
$result = $ydb->retry(function (Ydb $ydb) use ($output, $dirname) {
5050

5151
$scheme = $ydb->scheme();
5252

53-
$result = $scheme->removeDirectory($dirname);
54-
55-
$output->writeln(json_encode($result, 480));
53+
return $scheme->removeDirectory($dirname);
5654

5755
});
5856

57+
$output->writeln(json_encode($result, 480));
58+
5959
return Command::SUCCESS;
6060
}
6161
}

examples/app/Commands/Select1Command.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
4444
{
4545
$ydb = $this->appService->initYdb();
4646

47-
$ydb->retry(function (Ydb $ydb) use ($output) {
47+
$result = $ydb->retry(function (Ydb $ydb) use ($output) {
4848

4949
$table = $ydb->table();
5050

51-
$result = $table->session()->query('select 1;');
51+
return $table->session()->query('select 1;');
5252

53-
$output->writeln('Column count: ' . $result->columnCount());
54-
$output->writeln('Row count: ' . $result->rowCount());
53+
}, true);
5554

56-
$t = new Table($output);
57-
$t
58-
->setHeaders(array_map(function($column) {
59-
return $column['name'];
60-
}, $result->columns()))
61-
->setRows($result->rows())
62-
;
63-
$t->render();
55+
$output->writeln('Column count: ' . $result->columnCount());
56+
$output->writeln('Row count: ' . $result->rowCount());
6457

65-
});
58+
$t = new Table($output);
59+
$t
60+
->setHeaders(array_map(function($column) {
61+
return $column['name'];
62+
}, $result->columns()))
63+
->setRows($result->rows())
64+
;
65+
$t->render();
6666

6767
return Command::SUCCESS;
6868
}

examples/app/Commands/SelectCommand.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
4949

5050
$ydb = $this->appService->initYdb();
5151

52-
$ydb->table()->retrySession(function (Session $session) use ($table_name, $output) {
52+
$result = $ydb->table()->retrySession(function (Session $session) use ($table_name, $output) {
5353

54-
$result = $session->query('select * from `' . $table_name . '` limit 10;');
54+
return $session->query('select * from `' . $table_name . '` limit 10;');
5555

56-
$output->writeln('Column count: ' . $result->columnCount());
57-
$output->writeln('Row count: ' . $result->rowCount());
56+
}, true);
5857

59-
$t = new Table($output);
60-
$t
61-
->setHeaders(array_map(function($column) {
62-
return $column['name'];
63-
}, $result->columns()))
64-
->setRows($result->rows())
65-
;
66-
$t->render();
58+
$output->writeln('Column count: ' . $result->columnCount());
59+
$output->writeln('Row count: ' . $result->rowCount());
60+
61+
$t = new Table($output);
62+
$t
63+
->setHeaders(array_map(function($column) {
64+
return $column['name'];
65+
}, $result->columns()))
66+
->setRows($result->rows())
67+
;
68+
$t->render();
6769

68-
});
6970

7071
return Command::SUCCESS;
7172
}

0 commit comments

Comments
 (0)