TableProperty.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. * @return void
  62. */
  63. function __construct($row)
  64. {
  65. $this->name = trim($row[0]);
  66. $this->type = trim($row[1]);
  67. $this->nullable = trim($row[2]);
  68. $this->key = trim($row[3]);
  69. $this->defaultValue = trim($row[4]);
  70. $this->ext = trim($row[5]);
  71. }
  72. /**
  73. * Gets the pure type
  74. *
  75. * @return string type
  76. */
  77. function getPureType()
  78. {
  79. $pos = strpos($this->type, "(");
  80. if ($pos > 0) {
  81. return substr($this->type, 0, $pos);
  82. }
  83. return $this->type;
  84. }
  85. /**
  86. * Tells whether the key is null or not
  87. *
  88. * @return bool true if the key is not null, false otherwise
  89. */
  90. function isNotNull()
  91. {
  92. return $this->nullable == "NO" ? "true" : "false";
  93. }
  94. /**
  95. * Tells whether the key is unique or not
  96. *
  97. * @return bool true if the key is unique, false otherwise
  98. */
  99. function isUnique()
  100. {
  101. return $this->key == "PRI" || $this->key == "UNI" ? "true" : "false";
  102. }
  103. /**
  104. * Gets the .NET primitive type
  105. *
  106. * @return string type
  107. */
  108. function getDotNetPrimitiveType()
  109. {
  110. if (strpos($this->type, "int") === 0) {
  111. return "int";
  112. }
  113. if (strpos($this->type, "long") === 0) {
  114. return "long";
  115. }
  116. if (strpos($this->type, "char") === 0) {
  117. return "string";
  118. }
  119. if (strpos($this->type, "varchar") === 0) {
  120. return "string";
  121. }
  122. if (strpos($this->type, "text") === 0) {
  123. return "string";
  124. }
  125. if (strpos($this->type, "longtext") === 0) {
  126. return "string";
  127. }
  128. if (strpos($this->type, "tinyint") === 0) {
  129. return "bool";
  130. }
  131. if (strpos($this->type, "datetime") === 0) {
  132. return "DateTime";
  133. }
  134. return "unknown";
  135. }
  136. /**
  137. * Gets the .NET object type
  138. *
  139. * @return string type
  140. */
  141. function getDotNetObjectType()
  142. {
  143. if (strpos($this->type, "int") === 0) {
  144. return "Int32";
  145. }
  146. if (strpos($this->type, "long") === 0) {
  147. return "Long";
  148. }
  149. if (strpos($this->type, "char") === 0) {
  150. return "String";
  151. }
  152. if (strpos($this->type, "varchar") === 0) {
  153. return "String";
  154. }
  155. if (strpos($this->type, "text") === 0) {
  156. return "String";
  157. }
  158. if (strpos($this->type, "longtext") === 0) {
  159. return "String";
  160. }
  161. if (strpos($this->type, "tinyint") === 0) {
  162. return "Boolean";
  163. }
  164. if (strpos($this->type, "datetime") === 0) {
  165. return "DateTime";
  166. }
  167. return "Unknown";
  168. }
  169. /**
  170. * Gets the index name
  171. *
  172. * @return string containing the name of the index
  173. */
  174. function getIndexName()
  175. {
  176. if (strlen($this->key) > 0) {
  177. return "index=\""
  178. . htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8')
  179. . "\"";
  180. }
  181. return "";
  182. }
  183. /**
  184. * Tells whether the key is primary or not
  185. *
  186. * @return bool true if the key is primary, false otherwise
  187. */
  188. function isPK()
  189. {
  190. return $this->key=="PRI";
  191. }
  192. /**
  193. * Formats a string for C#
  194. *
  195. * @param string $text string to be formatted
  196. *
  197. * @return string formatted text
  198. */
  199. function formatCs($text)
  200. {
  201. $text = str_replace(
  202. "#name#",
  203. ExportCodegen::cgMakeIdentifier($this->name, false),
  204. $text
  205. );
  206. return $this->format($text);
  207. }
  208. /**
  209. * Formats a string for XML
  210. *
  211. * @param string $text string to be formatted
  212. *
  213. * @return string formatted text
  214. */
  215. function formatXml($text)
  216. {
  217. $text = str_replace(
  218. "#name#",
  219. htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8'),
  220. $text
  221. );
  222. $text = str_replace(
  223. "#indexName#",
  224. $this->getIndexName(),
  225. $text
  226. );
  227. return $this->format($text);
  228. }
  229. /**
  230. * Formats a string
  231. *
  232. * @param string $text string to be formatted
  233. *
  234. * @return string formatted text
  235. */
  236. function format($text)
  237. {
  238. $text = str_replace(
  239. "#ucfirstName#",
  240. ExportCodegen::cgMakeIdentifier($this->name),
  241. $text
  242. );
  243. $text = str_replace(
  244. "#dotNetPrimitiveType#",
  245. $this->getDotNetPrimitiveType(),
  246. $text
  247. );
  248. $text = str_replace(
  249. "#dotNetObjectType#",
  250. $this->getDotNetObjectType(),
  251. $text
  252. );
  253. $text = str_replace(
  254. "#type#",
  255. $this->getPureType(),
  256. $text
  257. );
  258. $text = str_replace(
  259. "#notNull#",
  260. $this->isNotNull(),
  261. $text
  262. );
  263. $text = str_replace(
  264. "#unique#",
  265. $this->isUnique(),
  266. $text
  267. );
  268. return $text;
  269. }
  270. }
  271. ?>