TableProperty.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Plugins\Export\Helpers\TableProperty class
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage CodeGen
  8. */
  9. namespace PhpMyAdmin\Plugins\Export\Helpers;
  10. use PhpMyAdmin\Plugins\Export\ExportCodegen;
  11. /**
  12. * PhpMyAdmin\Plugins\Export\Helpers\TableProperty class
  13. *
  14. * @package PhpMyAdmin-Export
  15. * @subpackage CodeGen
  16. */
  17. class TableProperty
  18. {
  19. /**
  20. * Name
  21. *
  22. * @var string
  23. */
  24. public $name;
  25. /**
  26. * Type
  27. *
  28. * @var string
  29. */
  30. public $type;
  31. /**
  32. * Whether the key is nullable or not
  33. *
  34. * @var bool
  35. */
  36. public $nullable;
  37. /**
  38. * The key
  39. *
  40. * @var int
  41. */
  42. public $key;
  43. /**
  44. * Default value
  45. *
  46. * @var mixed
  47. */
  48. public $defaultValue;
  49. /**
  50. * Extension
  51. *
  52. * @var string
  53. */
  54. public $ext;
  55. /**
  56. * Constructor
  57. *
  58. * @param array $row table row
  59. */
  60. public function __construct(array $row)
  61. {
  62. $this->name = trim($row[0]);
  63. $this->type = trim($row[1]);
  64. $this->nullable = trim($row[2]);
  65. $this->key = trim($row[3]);
  66. $this->defaultValue = trim($row[4]);
  67. $this->ext = trim($row[5]);
  68. }
  69. /**
  70. * Gets the pure type
  71. *
  72. * @return string type
  73. */
  74. public function getPureType()
  75. {
  76. $pos = mb_strpos($this->type, "(");
  77. if ($pos > 0) {
  78. return mb_substr($this->type, 0, $pos);
  79. }
  80. return $this->type;
  81. }
  82. /**
  83. * Tells whether the key is null or not
  84. *
  85. * @return bool true if the key is not null, false otherwise
  86. */
  87. public function isNotNull()
  88. {
  89. return $this->nullable == "NO" ? "true" : "false";
  90. }
  91. /**
  92. * Tells whether the key is unique or not
  93. *
  94. * @return bool true if the key is unique, false otherwise
  95. */
  96. public function isUnique()
  97. {
  98. return $this->key == "PRI" || $this->key == "UNI" ? "true" : "false";
  99. }
  100. /**
  101. * Gets the .NET primitive type
  102. *
  103. * @return string type
  104. */
  105. public function getDotNetPrimitiveType()
  106. {
  107. if (mb_strpos($this->type, "int") === 0) {
  108. return "int";
  109. }
  110. if (mb_strpos($this->type, "longtext") === 0) {
  111. return "string";
  112. }
  113. if (mb_strpos($this->type, "long") === 0) {
  114. return "long";
  115. }
  116. if (mb_strpos($this->type, "char") === 0) {
  117. return "string";
  118. }
  119. if (mb_strpos($this->type, "varchar") === 0) {
  120. return "string";
  121. }
  122. if (mb_strpos($this->type, "text") === 0) {
  123. return "string";
  124. }
  125. if (mb_strpos($this->type, "tinyint") === 0) {
  126. return "bool";
  127. }
  128. if (mb_strpos($this->type, "datetime") === 0) {
  129. return "DateTime";
  130. }
  131. return "unknown";
  132. }
  133. /**
  134. * Gets the .NET object type
  135. *
  136. * @return string type
  137. */
  138. public function getDotNetObjectType()
  139. {
  140. if (mb_strpos($this->type, "int") === 0) {
  141. return "Int32";
  142. }
  143. if (mb_strpos($this->type, "longtext") === 0) {
  144. return "String";
  145. }
  146. if (mb_strpos($this->type, "long") === 0) {
  147. return "Long";
  148. }
  149. if (mb_strpos($this->type, "char") === 0) {
  150. return "String";
  151. }
  152. if (mb_strpos($this->type, "varchar") === 0) {
  153. return "String";
  154. }
  155. if (mb_strpos($this->type, "text") === 0) {
  156. return "String";
  157. }
  158. if (mb_strpos($this->type, "tinyint") === 0) {
  159. return "Boolean";
  160. }
  161. if (mb_strpos($this->type, "datetime") === 0) {
  162. return "DateTime";
  163. }
  164. return "Unknown";
  165. }
  166. /**
  167. * Gets the index name
  168. *
  169. * @return string containing the name of the index
  170. */
  171. public function getIndexName()
  172. {
  173. if (strlen($this->key) > 0) {
  174. return "index=\""
  175. . htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8')
  176. . "\"";
  177. }
  178. return "";
  179. }
  180. /**
  181. * Tells whether the key is primary or not
  182. *
  183. * @return bool true if the key is primary, false otherwise
  184. */
  185. public function isPK()
  186. {
  187. return $this->key == "PRI";
  188. }
  189. /**
  190. * Formats a string for C#
  191. *
  192. * @param string $text string to be formatted
  193. *
  194. * @return string formatted text
  195. */
  196. public function formatCs($text)
  197. {
  198. $text = str_replace(
  199. "#name#",
  200. ExportCodegen::cgMakeIdentifier($this->name, false),
  201. $text
  202. );
  203. return $this->format($text);
  204. }
  205. /**
  206. * Formats a string for XML
  207. *
  208. * @param string $text string to be formatted
  209. *
  210. * @return string formatted text
  211. */
  212. public function formatXml($text)
  213. {
  214. $text = str_replace(
  215. "#name#",
  216. htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8'),
  217. $text
  218. );
  219. $text = str_replace(
  220. "#indexName#",
  221. $this->getIndexName(),
  222. $text
  223. );
  224. return $this->format($text);
  225. }
  226. /**
  227. * Formats a string
  228. *
  229. * @param string $text string to be formatted
  230. *
  231. * @return string formatted text
  232. */
  233. public function format($text)
  234. {
  235. $text = str_replace(
  236. "#ucfirstName#",
  237. ExportCodegen::cgMakeIdentifier($this->name),
  238. $text
  239. );
  240. $text = str_replace(
  241. "#dotNetPrimitiveType#",
  242. $this->getDotNetPrimitiveType(),
  243. $text
  244. );
  245. $text = str_replace(
  246. "#dotNetObjectType#",
  247. $this->getDotNetObjectType(),
  248. $text
  249. );
  250. $text = str_replace(
  251. "#type#",
  252. $this->getPureType(),
  253. $text
  254. );
  255. $text = str_replace(
  256. "#notNull#",
  257. $this->isNotNull(),
  258. $text
  259. );
  260. $text = str_replace(
  261. "#unique#",
  262. $this->isUnique(),
  263. $text
  264. );
  265. return $text;
  266. }
  267. }