TableProperty.class.php 5.9 KB

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