Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for SQL conversion #346

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
366 changes: 125 additions & 241 deletions composer.lock

Large diffs are not rendered by default.

16,000 changes: 5,872 additions & 10,128 deletions countries.json

Large diffs are not rendered by default.

766 changes: 766 additions & 0 deletions dist/UniversalCountryId.php

Large diffs are not rendered by default.

4,804 changes: 4,804 additions & 0 deletions dist/countries-inserts.sql

Large diffs are not rendered by default.

500 changes: 250 additions & 250 deletions dist/countries-unescaped.json
100755 → 100644

Large diffs are not rendered by default.

195 changes: 195 additions & 0 deletions dist/countries-updates.sql

Large diffs are not rendered by default.

504 changes: 252 additions & 252 deletions dist/countries.csv
100755 → 100644

Large diffs are not rendered by default.

251 changes: 1 addition & 250 deletions dist/countries.json
100755 → 100644

Large diffs are not rendered by default.

500 changes: 250 additions & 250 deletions dist/countries.xml
100755 → 100644

Large diffs are not rendered by default.

500 changes: 250 additions & 250 deletions dist/countries.yml
100755 → 100644

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/MLD/Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class ExportCommand extends Command
'csv' => ['class' => '\MLD\Converter\CsvConverter', 'output_file' => 'countries.csv'],
'xml' => ['class' => '\MLD\Converter\XmlConverter', 'output_file' => 'countries.xml'],
'yml' => ['class' => '\MLD\Converter\YamlConverter', 'output_file' => 'countries.yml'],
'sql_insert' => ['class' => '\MLD\Converter\SQL\SQLInsertConverter', 'output_file' => 'countries-inserts.sql'],
'sql_update' => ['class' => '\MLD\Converter\SQL\SQLUpdateConverter', 'output_file' => 'countries-updates.sql'],
# 'php_universal_country_id' => ['class' => '\MLD\Converter\SQL\UniversalCountryIdGenerator', 'output_file' => 'UniversalCountryId.php']
];

/**
Expand Down
197 changes: 197 additions & 0 deletions src/MLD/Converter/SQL/AbstractSQLConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php

namespace MLD\Converter\SQL;

use MLD\Converter\AbstractConverter;
use MLD\Converter\SQL\UniversalCountryId;
use Exception;

const UCID = "ucid";
const PRIMARYKEY = "id";
const COUNTRY_PRIMARYKEY = "country_id";
const NAME = "name";
const OFFICIAL = "official";
const COMMON = "common";
const TLD = "tld";
const CCA2 = "cca2";
const CCN3 = "ccn3";
const CCA3 = "cca3";
const CIOC = "cioc";
const INDEPENDENT = "independent";
const STATUS = "status";
const CAPITAL = "capital";
const REGION = "region";
const SUBREGION = "subregion";
const IDD = "idd";
const LATITUDE = "lat";
const LONGITUDE = "lng";
const LANGUAGE = "language";
const FLAG = "flag";

/**
* Class AbstractSQLConverter
*/
abstract class AbstractSQLConverter extends AbstractConverter
{
/**
* @var string
*/
private $body = '';

/**
* @var int
*/
private $translationPrimaryKey = 1;

/**
* @return string data converted to Yaml
*/
public function convert()
{
# print_r($this->countries[0]);
array_walk($this->countries, [$this, 'processCountry']);
return $this->body;
}

private function ucidValidation(int $currentUcid, string $common, string $official, string $tld = null)
{

if(isset(UniversalCountryId::COMMON_MAP[$common])) {
$previousUcid = UniversalCountryId::COMMON_MAP[$common];
if ($previousUcid == $currentUcid) {
return;
} else {
throw new Exception('Universal country ID from "' . $common . '", did change from "' . $previousUcid . '" to "' . $currentUcid . '"!');
}
}

if (isset(UniversalCountryId::OFFICIAL_MAP[$official])) {
$previousUcid = UniversalCountryId::OFFICIAL_MAP[$official];
if ($previousUcid == $currentUcid) {
return;
} else {
throw new Exception('Universal country ID from "' . $official . '", did change from "' . $previousUcid . '" to "' . $currentUcid . '"!');
}
}

if (isset(UniversalCountryId::TLD_MAP[$tld])) {
$previousUcid = UniversalCountryId::TLD_MAP[$tld];
if ($previousUcid == $currentUcid) {
return;
} else {
throw new Exception('Universal country ID from "' . $tld . '", did change from "' . $previousUcid . '" to "' . $currentUcid . '"!');
}
}

throw new Exception('Country "' . $common . ', ' . $official . ', ' . $tld . '" does not have an universal country ID!');
}

/**
* Processes a country.
* @param $array
*/
private function processCountry($data, $key)
{
// if (isset($data['currencies'])) {
// $data['currencies'] = array_keys($data['currencies']);
// }

$primaryKey = $data[UCID];
if(!isset($primaryKey)) {
return;
}

$values = array();
$values[PRIMARYKEY] = $primaryKey;
$values[NAME] = $data['name']['common'];
$values[OFFICIAL] = $data['name']['official'];

if (isset($data['tld'][0])) {
$values[TLD] = $data['tld'][0];
$this->ucidValidation($values[PRIMARYKEY], $values[NAME], $values[OFFICIAL], $values[TLD]);
} else {
$this->ucidValidation($values[PRIMARYKEY], $values[NAME], $values[OFFICIAL]);
}
if (isset($data['cca2'])) {
$values[CCA2] = $data['cca2'];
}
if (isset($data['ccn3'])) {
$values[CCN3] = $data['ccn3'];
}
if (isset($data['cca3'])) {
$values[CCA3] = $data['cca3'];
}
if (isset($data['cioc'])) {
$values[CIOC] = $data['cioc'];
} else {
$values[CIOC] = "";
}
if (isset($data['independent'])) {
$values[INDEPENDENT] = $data['independent'];
}
if (isset($data['status'])) {
$values[STATUS] = $data['status'];
}
if (isset($data['capital'][0])) {
$values[CAPITAL] = $data['capital'][0];
}
if (isset($data['region'])) {
$values[REGION] = $data['region'];
}
if (isset($data['subregion'])) {
$values[SUBREGION] = $data['subregion'];
}
$values[IDD] = $data['idd']['root'];
if (isset($data['idd']['suffixes'])) {
$values[IDD] = implode(',', $data['idd']['suffixes']);;
}
if (is_array($data['latlng']) && count($data['latlng']) == 2) {
$values[LATITUDE] = $data['latlng'][0];
$values[LONGITUDE] = $data['latlng'][1];
}
if (isset($data['flag'])) {
$values[FLAG] = $this->encode($data['flag']);
}
$stmt = $this->generateStatement("country", $values, PRIMARYKEY, $primaryKey);
if($stmt == null) {
return;
}
$this->body .= $stmt . ";\n";
if (is_array($data['translations']) && count($data['translations']) > 0) {
array_walk($data['translations'], [$this, 'processTranslationsForCountry'], $primaryKey);
}
}

/**
* Processes all the translations for a country.
* @param $array
*/
private function processTranslationsForCountry($value, $key, $countryPrimaryKey)
{
if (!is_array($value)) {
return;
}
$translation = array();
$translation[COUNTRY_PRIMARYKEY] = $countryPrimaryKey;
$translation[LANGUAGE] = $key;
$translation[OFFICIAL] = $value['official'];
$translation[COMMON] = $value['common'];
$stmt = $this->generateStatement("country_translations", $translation);
if($stmt == null) {
return;
}
$this->body .= $stmt . ";\n";
$this->translationPrimaryKey++;
}

private function encode(string $value)
{
return addslashes(trim(json_encode($value), '"'));
}

/**
* Generate a statement from an array of values.
* @param $array
*/
abstract protected function generateStatement(string $table, array $values, string $primaryKeyColumn = null, int $primaryKey = -1);
}
23 changes: 23 additions & 0 deletions src/MLD/Converter/SQL/SQLInsertConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace MLD\Converter\SQL;

use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;

/**
* Class SQLInsertConverter
*/
class SQLInsertConverter extends AbstractSQLConverter
{
function generateStatement($table, $values, $primaryKeyColumn = null, $primaryKey = -1)
{
$builder = new GenericBuilder();
$query = $builder->insert()
->setTable($table)
->setValues($values);
$sql = $builder->write($query);
$values = $builder->getValues();
array_walk($values, function(&$value, $key) { $value = '"'.$value.'"'; } );
return strtr($sql, $values);
}
}
33 changes: 33 additions & 0 deletions src/MLD/Converter/SQL/SQLUpdateConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace MLD\Converter\SQL;

use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;

/**
* Class SQLUpdateConverter
*/
class SQLUpdateConverter extends AbstractSQLConverter
{
function generateStatement($table, $values, $primaryKeyColumn = null, $primaryKey = -1)
{
if ($primaryKeyColumn == null || $primaryKey < 0 || !in_array($primaryKeyColumn, $values))
{
// throw new Exception('Missing primaryKey and primaryKeyColumn!');
return null;
}

unset($values[$primaryKeyColumn]);
$builder = new GenericBuilder();
$query = $builder->update()
->setTable($table)
->setValues($values)
->where()
->equals($primaryKeyColumn, $primaryKey)
->end();
$sql = $builder->write($query);
$values = $builder->getValues();
array_walk($values, function(&$value, $key) { $value = '"'.$value.'"'; } );
return strtr($sql, $values);
}
}
Loading