2017-06-25 00:09:27 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Service;
|
2018-06-21 00:18:30 +08:00
|
|
|
use App\Support\Database;
|
2017-06-25 01:59:07 +08:00
|
|
|
use Carbon\Carbon;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Webpatser\Uuid\Uuid;
|
2017-06-25 01:59:07 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class DatabaseService
|
|
|
|
*/
|
|
|
|
class DatabaseService extends Service
|
2017-06-25 02:20:24 +08:00
|
|
|
{
|
2017-06-25 02:30:13 +08:00
|
|
|
protected $time_fields = [
|
2017-06-25 02:23:30 +08:00
|
|
|
'created_at',
|
2018-08-27 00:40:04 +08:00
|
|
|
'updated_at',
|
2017-06-25 02:23:30 +08:00
|
|
|
];
|
|
|
|
|
2017-06-25 02:30:13 +08:00
|
|
|
protected $uuid_tables = [
|
2017-12-31 04:13:47 +08:00
|
|
|
'acars',
|
2017-06-25 02:20:24 +08:00
|
|
|
'flights',
|
2017-06-25 02:20:39 +08:00
|
|
|
'pireps',
|
2017-06-25 02:20:24 +08:00
|
|
|
];
|
2017-06-25 00:09:27 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-06-25 01:59:07 +08:00
|
|
|
protected function time(): string
|
|
|
|
{
|
2018-05-04 04:07:16 +08:00
|
|
|
return Carbon::now('UTC'); //->format('Y-m-d H:i:s');
|
2017-06-25 01:59:07 +08:00
|
|
|
}
|
|
|
|
|
2017-12-30 07:34:09 +08:00
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $yaml_file
|
2017-12-30 07:34:09 +08:00
|
|
|
* @param bool $ignore_errors
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-30 07:34:09 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return array
|
2017-12-30 07:34:09 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function seed_from_yaml_file($yaml_file, $ignore_errors = false): array
|
2017-06-25 01:59:07 +08:00
|
|
|
{
|
2018-06-21 00:18:30 +08:00
|
|
|
return Database::seed_from_yaml_file($yaml_file, $ignore_errors);
|
2017-06-25 02:20:24 +08:00
|
|
|
}
|
2017-06-25 01:59:07 +08:00
|
|
|
|
2017-12-30 07:34:09 +08:00
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $yml
|
2017-12-30 07:34:09 +08:00
|
|
|
* @param bool $ignore_errors
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-30 07:34:09 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return array
|
2017-12-30 07:34:09 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function seed_from_yaml($yml, $ignore_errors = false): array
|
2017-06-25 02:20:24 +08:00
|
|
|
{
|
2018-06-21 00:18:30 +08:00
|
|
|
return Database::seed_from_yaml($yml, $ignore_errors);
|
2017-06-25 01:59:07 +08:00
|
|
|
}
|
2018-05-04 04:07:16 +08:00
|
|
|
|
|
|
|
/**
|
2019-06-19 06:58:09 +08:00
|
|
|
* @param $table
|
|
|
|
* @param $row
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-04 04:07:16 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return mixed
|
2018-05-04 04:07:16 +08:00
|
|
|
*/
|
2018-08-27 00:40:04 +08:00
|
|
|
public function insert_row($table, $row)
|
|
|
|
{
|
|
|
|
// see if this table uses a UUID as the PK
|
|
|
|
// if no ID is specified
|
2018-05-04 04:07:16 +08:00
|
|
|
if (\in_array($table, $this->uuid_tables, true)) {
|
2018-08-27 02:51:47 +08:00
|
|
|
/* @noinspection NestedPositiveIfStatementsInspection */
|
2018-05-04 04:07:16 +08:00
|
|
|
if (!array_key_exists('id', $row)) {
|
|
|
|
$row['id'] = Uuid::generate()->string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 00:18:30 +08:00
|
|
|
return Database::insert_row($table, $row);
|
2018-05-04 04:07:16 +08:00
|
|
|
}
|
2017-06-25 00:09:27 +08:00
|
|
|
}
|