Last active
May 1, 2020 02:31
-
-
Save daubac402/b10ed4dfd4d9ae0c4fb443a18eb58bb0 to your computer and use it in GitHub Desktop.
Oracle DDL to C# Data modal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$str = 'ID VARCHAR2(5) not null | |
NAME VARCHAR2(50), | |
ADDRESS VARCHAR2(200), | |
TEL VARCHAR2(30), | |
FAX VARCHAR2(30)'; | |
function db_column($field) { | |
echo '[Column("' . $field . '")]' . "\r\n"; | |
} | |
function dtcc($string) { | |
return str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($string)))); | |
} | |
function require_check($line) { | |
echo preg_match('/not null/', $line) ? "[Required]\r\n" : ''; | |
} | |
foreach (preg_split("/((\r?\n)|(\r\n?))/", $str) as $line) { | |
$line = trim($line); | |
$line = preg_replace('/\s+/', ' ', $line); | |
$elements = explode(' ', $line); | |
$field = $elements[0]; | |
$type = $elements[1]; | |
if (preg_match('/NUMBER\(\d+\)/', $type, $out)) { | |
db_column($field); | |
require_check($line); | |
echo 'public long? ' . dtcc($field) . ' { get; set; }' . "\r\n"; | |
} elseif (preg_match('/NUMBER\(\d+, \d+\)/', $type, $out) || preg_match('/NUMBER\(\d+,\d+\)/', $type, $out)) { | |
db_column($field); | |
require_check($line); | |
echo 'public decimal? ' . dtcc($field) . ' { get; set; }' . "\r\n"; | |
} elseif (preg_match('/DATE/', $type, $out)) { | |
db_column($field); | |
require_check($line); | |
echo 'public DateTime? ' . dtcc($field) . ' { get; set; }' . "\r\n"; | |
} elseif (preg_match('/VARCHAR/', $type, $out)) { | |
db_column($field); | |
require_check($line); | |
echo 'public string ' . dtcc($field) . ' { get; set; }' . "\r\n"; | |
} elseif (preg_match('/CHAR/', $type, $out)) { | |
db_column($field); | |
require_check($line); | |
echo 'public string ' . dtcc($field) . ' { get; set; }' . "\r\n"; | |
} else { | |
db_column($field); | |
require_check($line); | |
echo 'public string ' . dtcc($field) . ' { get; set; }' . "\r\n"; | |
} | |
echo "\r\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment