Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Last active September 28, 2015 03:57
Show Gist options
  • Save chanmix51/1380334 to your computer and use it in GitHub Desktop.
Save chanmix51/1380334 to your computer and use it in GitHub Desktop.
SQL schema
CREATE TYPE transformer_winding AS (voltage NUMERIC(5,1), current NUMERIC(5,3));
CREATE TABLE power_supply_transformer(reference CHAR(6) PRIMARY KEY, pri transformer_winding NOT NULL, secs transformer_winding[] NOT NULL, size_reference VARCHAR(5) NOT NULL);
<?php
$loader = require __DIR__."/../vendor/.composer/autoload.php";
$loader->add('YourDb', "/tmp"); // Set yours
use YourDb\PublicSchema\PowerSupplyTransformer;
use YourDb\PublicSchema\PowerSupplyTransformerMap;
use YourDb\PublicSchema\Converter\TransformerWindingConverter;
$database = new Pomm\Connection\Database(array('dsn' => 'pgsql://user:password@host:port/db_name'));
$connection = $database
->registerConverter('TransformerWinding', new TransformerWindingConverter(), array('public.transformer_winding'))
->getConnection();
$transformers = $connection
->getMapFor('YourDb\PublicSchema\PowerSupplyTransformer')
->findWhere('array_upper(secs, 1) > 1'); // find only transfo with more than 1 windings on the secondary.
foreach($transformers as $transformer)
{
printf("%s\n", print_r($transformer, true));
}
/**
YourDb\PublicSchema\PowerSupplyTransformer Object
(
[fields:protected] => Array
(
[reference] => PST044
[pri] => TransformerWinding Object
(
[voltage] => 220.0
[current] => 0.100
)
[secs] => Array
(
[0] => TransformerWinding Object
(
[voltage] => 12.0
[current] => 1.000
)
[1] => TransformerWinding Object
(
[voltage] => 12.0
[current] => 1.000
)
)
[size_reference] => EI84
)
[fields_definition:protected] => Array
(
[reference] => String
[pri] => TransformerWinding
[secs] => TransformerWinding[]
[size_reference] => String
)
[status:protected] => 1
[primary_key:protected] => Array
(
[0] => reference
)
)
**/
<?php
namespace YourDb\PublicSchema\Type;
class TransformerWinding
{
public $voltage;
public $current;
public function __construct($voltage, $current)
{
$this->voltage = $voltage;
$this->current = $current;
}
}
<?php
namespace YourDb\PublicSchema\Converter;
use Pomm\Converter\ConverterInterface;
use YourDb\PublicSchema\TransformerWinding;
class TransformerWindingConverter implements ConverterInterface
{
public function fromPg($data, $type = null)
{
$data = trim($data, "()");
$elts = preg_split('/,/', $data);
if (count($elts) !== 2)
{
throw new Exception(sprintf("Cannot parse winding data '%s'.", $data));
}
return new TransformerWinding($elts[0], $elts[1]);
}
public function toPg($data, $type = null)
{
return sprintf("(%5.1f,%5.3f)", $data->voltage, $data->current);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment