therselman 7 years ago
parent
commit
cb083438e1
  1. 20
      src/Schema/Schema.php
  2. 2
      src/Schema/Table.php

20
src/Schema/Schema.php

@ -13,30 +13,30 @@ class Schema @@ -13,30 +13,30 @@ class Schema
private static $cache = null;
// Set db to MySQL Connection object - if not set, then procedural style will be used
public static setConn($conn)
public static function setConn($conn)
{
self::$db = $conn;
}
// Set the database schema name
public static setSchema($schema)
public static function setSchema($schema)
{
self::$schema = $schema != 'DATABASE()' ? '"' . $schema . '"' : 'DATABASE()';
}
// Set the schema cache directory, where we can store files for the composer autoloader
public static setCache($cache)
public static function setCache($cache)
{
self::$cache = $cache;
}
public static __callStatic(string $table, array $args)
public static function __callStatic(string $table, array $args)
{
if ( ! isset($tables[$table]))
{
try
{
$class = 'Schema\\' . self::toPascalCase($table);
$class = 'Twister\\Schema\\' . self::toPascalCase($table);
$tables[$table] = new $class();
}
catch (\Exception $e)
@ -51,7 +51,7 @@ class Schema @@ -51,7 +51,7 @@ class Schema
* Converts $table to PascalCase, preserving leading and trailing underscores '_'
* eg. nation_capitals__ => NationCapitals__
*/
private static toPascalCase($table)
private static function toPascalCase($table)
{
return str_repeat('_', strspn($table, '_')) . str_replace('_', '', ucwords($table, '_')) . str_repeat('_', strspn(strrev($table), '_'));
}
@ -62,8 +62,11 @@ class Schema @@ -62,8 +62,11 @@ class Schema
$cache = $cache ?: self::$cache;
static $methods = null;
if ($methods === null)
{
// here for potential caching possibilities !?!? Hopefully PHP can set an internal ref-counter and not re-define the functions each time!
static $methods = [ 'date' => function ($type, string $value) { return $value === null && $type->nullable || preg_match('~^\d\d\d\d-\d\d-\d\d$~', $value) === 1; }, // TODO: Add DateTime object validation! ie. instanceof DateTime
self::$methods = [ 'date' => function ($type, string $value) { return $value === null && $type->nullable || preg_match('~^\d\d\d\d-\d\d-\d\d$~', $value) === 1; }, // TODO: Add DateTime object validation! ie. instanceof DateTime
'datetime' => function ($type, string $value) { return $value === null && $type->nullable || preg_match('~^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$~', $value) === 1; },
'time' => function ($type, string $value) { return $value === null && $type->nullable || preg_match('~^\d\d:\d\d:\d\d$~', $value) === 1; },
'time_ex' => function ($type, string $value) { return $value === null && $type->nullable || preg_match('~^-?\d?\d\d:\d\d:\d\d$~', $value) === 1; }, // From: http://www.mysqltutorial.org/mysql-time/ A TIME value ranges from -838:59:59 to 838:59:59. In addition, a TIME value can have fractional seconds part that is up to microseconds precision (6 digits). To define a column whose data type is TIME with a fractional second precision part, you use the following syntax: column_name TIME(N);
@ -76,8 +79,9 @@ class Schema @@ -76,8 +79,9 @@ class Schema
'clamp' => function ($type, $value) { return isset($value) && is_numeric($value) ? min(max($value, $type->min), $type->max) : $type->default ?: ($type->nullable ? null : min(max(0, $type->min), $type->max)); },
'isTrue' => function ($type) { return true; },
'isFalse' => function ($type) { return false; },
'isUnsigned' => function ($type) { return $type->min >= 0; },
'isUnsigned' => function ($type) { return $type->min >= 0; }
];
}
$sql = 'SELECT ' .
'TABLE_NAME,' .

2
src/Schema/Table.php

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
namespace Twister\Schema;
class Table
abstract class Table
{
protected $fields = null;

Loading…
Cancel
Save