From 327523f41bfaae788f2c0f8bc13e85c0e24f7101 Mon Sep 17 00:00:00 2001 From: Brian Moon Date: Tue, 3 Sep 2024 19:52:43 -0500 Subject: [PATCH 1/4] test --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6d0ee6b..03dd56e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # DealNews Datbase Library + ## Factory The factory creates PDO objects using \DealNews\GetConfigto From 46c1e7504ce1a42eeb0f89f4756e5312ef0f36cb Mon Sep 17 00:00:00 2001 From: Brian Moon Date: Fri, 18 Oct 2024 15:12:16 -0500 Subject: [PATCH 2/4] Add basic bin script for generating value objects and mappers from tables --- bin/create_objects.php | 250 +++++++++++++++++++++++++++++++++++++++++ composer.json | 5 +- 2 files changed, 254 insertions(+), 1 deletion(-) create mode 100755 bin/create_objects.php diff --git a/bin/create_objects.php b/bin/create_objects.php new file mode 100755 index 0000000..f0b143b --- /dev/null +++ b/bin/create_objects.php @@ -0,0 +1,250 @@ +#!/usr/bin/env php +pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + +$sql = "select + table_catalog as table_catalog, + table_schema as table_schema, + table_name as table_name, + column_name as column_name, + ordinal_position as ordinal_position, + column_default as column_default, + is_nullable as is_nullable, + data_type as data_type, + character_maximum_length as character_maximum_length, + character_octet_length as character_octet_length, + numeric_precision as numeric_precision, + numeric_scale as numeric_scale, + datetime_precision as datetime_precision, + character_set_name as character_set_name, + collation_name as collation_name + from + information_schema.columns + where + table_schema='{$opts["schema"]}' and + table_name='{$opts["table"]}'"; + +$schema = $db->runFetch($sql); + +$sql = "select + constraint_name as constraint_name, + column_name as column_name + from + information_schema.key_column_usage + where + table_schema='{$opts["schema"]}' and + table_name='{$opts["table"]}' + order by + constraint_name, + ordinal_position"; + +$keys = $db->runFetch($sql); + +$primary_key = ''; + +foreach($keys as $key) { + if ($driver === 'mysql' && $key['constraint_name'] == 'PRIMARY') { + $primary_key = $key['column_name']; + break; + } elseif ($driver === 'pgsql' && preg_match('/_pkey$/', $key['constraint_name'])) { + $primary_key = $key['column_name']; + break; + } +} + +$properties = []; + +foreach($schema as $column) { + + switch ($column['data_type']) { + + case 'int': + case 'integer': + case 'smallint': + case 'bigint': + case 'tinyint': + case 'year': + $type = 'int'; + $def_default = 0; + break; + + case 'boolean': + $type = 'bool'; + $def_default = true; + break; + + case 'float': + case 'double precision': + case 'real': + case 'decimal': + case 'double': + $type = 'float'; + $def_default = 0.00; + break; + + default: + $type = 'string'; + $def_default = ''; + } + + if (strtoupper($column['is_nullable']) === 'YES') { + $type = "?$type"; + $default = null; + } else { + $default = $def_default; + } + + + if (!empty($column['column_default'])) { + switch ($driver) { + case 'pgsql': + if (preg_match('/^\'(.*?)\'::/', $column['column_default'], $match)) { + $default = $match[1]; + } + break; + case 'mysql': + if (strpos($column['column_default'], 'CURRENT_TIMESTAMP') !== 0) { + $default = $column['column_default']; + } + } + } + + $properties[$column['column_name']] = [ + 'type' => $type, + 'default' => $default, + ]; +} + +$object_name = str_replace(' ', '', ucwords(str_replace('_', ' ', $opts['table']))); + +create_value_object($properties, $opts['namespace'], $object_name, $opts['base-class'], $opts['schema'], $opts['table'], $opts['dir']); +create_mapper($properties, $opts['namespace'], $object_name, $opts['base-class'], $opts['schema'], $opts['table'], $opts['dir'], $primary_key); + +function create_value_object($properties, $namespace, $object_name, $base_class, $schema, $table, $dir) { + + if (!empty($base_class)) { + $base_class = " extends $base_class"; + } + + $file = " $settings) { + + if ($settings['default'] === null) { + $default = 'null'; + } elseif ($settings['type'] === 'string' || $settings['type'] === '?string') { + $default = "'{$settings["default"]}'"; + } else { + $default = $settings["default"]; + } + + $file .= " /**\n"; + $file .= " * @var {$settings['type']}\n"; + $file .= " */\n"; + if (strpos($settings['type'], 'DateTime') !== false) { + $has_datetime = true; + $file .= " public {$settings['type']} \$$name;\n\n"; + } else { + $file .= " public {$settings['type']} \$$name = $default;\n\n"; + } + } + + if ($has_datetime) { + + $file .= " /**\n"; + $file .= " * Initialize properties that are objects\n"; + $file .= " */\n"; + $file .= " public function __construct() {\n"; + foreach ($properties as $name => $settings) { + if (strpos($settings['type'], 'DateTime') !== false) { + $file .= " \$this->$name = new \\DateTime();\n"; + } + } + $file .= " }\n"; + + } + + $file .= "}\n"; + + if (!file_exists("$dir/Data")) { + mkdir("$dir/Data", recursive: true); + } + + file_put_contents("$dir/Data/$object_name.php", $file); +} + +function create_mapper($properties, $namespace, $object_name, $base_class, $schema, $table, $dir, $primary_key) { + + $file = " [],\n"; + } + $file .= " ];\n"; + $file .= "}\n"; + + if (!file_exists("$dir/Mapper")) { + mkdir("$dir/Mapper", recursive: true); + } + + file_put_contents("$dir/Mapper/$object_name.php", $file); +} diff --git a/composer.json b/composer.json index aed340f..af693f2 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,9 @@ "discard-changes": true, "sort-packages": true }, + "bin": [ + "bin/create_objects.php" + ], "require": { "php": "^8.0", "dealnews/data-mapper": "^3.1.1", @@ -43,4 +46,4 @@ "php-cs-fixer fix --config .php-cs-fixer.dist.php src tests" ] } -} +} \ No newline at end of file From 8921c1044b7d55e53bb38b1801cd8da0f18ea79c Mon Sep 17 00:00:00 2001 From: Brian Moon Date: Mon, 21 Oct 2024 08:23:19 -0500 Subject: [PATCH 3/4] Phan fixes --- src/AbstractMapper.php | 2 +- src/Factory.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AbstractMapper.php b/src/AbstractMapper.php index 2735f93..256113d 100644 --- a/src/AbstractMapper.php +++ b/src/AbstractMapper.php @@ -74,7 +74,7 @@ abstract class AbstractMapper extends \DealNews\DataMapper\AbstractMapper { * * @param \DealNews\DB\CRUD|null $crud Optional CRUD object */ - public function __construct(CRUD $crud = null) { + public function __construct(?CRUD $crud = null) { if ($crud !== null) { $this->crud = $crud; } elseif (!empty($this::DATABASE_NAME)) { diff --git a/src/Factory.php b/src/Factory.php index b2db05b..3419c01 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -36,7 +36,7 @@ class Factory { * @throws \PDOException * @throws \LogicException */ - public static function init(string $db, array $options = null, string $type = null): PDO { + public static function init(string $db, ?array $options = null, ?string $type = null): PDO { static $objs = []; if (!empty($type)) { @@ -91,7 +91,7 @@ public static function build(array $config): PDO { * @throws \LogicException * @throws \UnexpectedValueException */ - public static function loadConfig(array $config, array $options = null, string $type = null): array { + public static function loadConfig(array $config, ?array $options = null, ?string $type = null): array { if (empty($config['server']) && empty($config['dsn'])) { throw new \LogicException('Either `server` or `dsn` is required', 3); } elseif (!empty($config['server'])) { @@ -157,7 +157,7 @@ public static function loadConfig(array $config, array $options = null, string $ * @return array * @throws \LogicException */ - public static function getConfig(string $db, GetConfig $cfg = null): array { + public static function getConfig(string $db, ?GetConfig $cfg = null): array { if (empty($cfg)) { $cfg = new GetConfig(); } From c1be2616396b0b2cdd08c478bfec353d2d7038b8 Mon Sep 17 00:00:00 2001 From: Brian Moon Date: Mon, 21 Oct 2024 08:23:31 -0500 Subject: [PATCH 4/4] make names singular --- bin/create_objects.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/create_objects.php b/bin/create_objects.php index f0b143b..a9d0463 100755 --- a/bin/create_objects.php +++ b/bin/create_objects.php @@ -138,7 +138,9 @@ ]; } -$object_name = str_replace(' ', '', ucwords(str_replace('_', ' ', $opts['table']))); +$object_name = rtrim(str_replace(' ', '', ucwords(str_replace('_', ' ', $opts['table']))), 's'); + + create_value_object($properties, $opts['namespace'], $object_name, $opts['base-class'], $opts['schema'], $opts['table'], $opts['dir']); create_mapper($properties, $opts['namespace'], $object_name, $opts['base-class'], $opts['schema'], $opts['table'], $opts['dir'], $primary_key);