DatabaseModule.class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * @name eolinker ams open source,eolinker开源版本
  4. * @link https://www.eolinker.com/
  5. * @package eolinker
  6. * @author www.eolinker.com 广州银云信息科技有限公司 ©2015-2018
  7. * eoLinker是目前全球领先、国内最大的在线API接口管理平台,提供自动生成API文档、API自动化测试、Mock测试、团队协作等功能,旨在解决由于前后端分离导致的开发效率低下问题。
  8. * 如在使用的过程中有任何问题,欢迎加入用户讨论群进行反馈,我们将会以最快的速度,最好的服务态度为您解决问题。
  9. *
  10. * eoLinker AMS开源版的开源协议遵循Apache License 2.0,如需获取最新的eolinker开源版以及相关资讯,请访问:https://www.eolinker.com/#/os/download
  11. *
  12. * 官方网站:https://www.eolinker.com/
  13. * 官方博客以及社区:http://blog.eolinker.com/
  14. * 使用教程以及帮助:http://help.eolinker.com/
  15. * 商务合作邮箱:market@eolinker.com
  16. * 用户讨论QQ群:284421832
  17. */
  18. class DatabaseModule
  19. {
  20. public function __construct()
  21. {
  22. @session_start();
  23. }
  24. /**
  25. * get userType from database
  26. * 获取数据字典用户类型
  27. * @param $dbID int 数据库ID
  28. * @return bool|int
  29. */
  30. public function getUserType(&$dbID)
  31. {
  32. $dao = new AuthorizationDao();
  33. $result = $dao->getDatabaseUserType($_SESSION['userID'], $dbID);
  34. if ($result === FALSE) {
  35. return -1;
  36. } else {
  37. return $result;
  38. }
  39. }
  40. /**
  41. * add database
  42. * 添加数据库
  43. * @param $dbName string 数据库名
  44. * @param $dbVersion string 数据库版本,默认1.0
  45. * @return bool|int
  46. */
  47. public function addDatabase(&$dbName, &$dbVersion = "1.0")
  48. {
  49. $databaseDao = new DatabaseDao;
  50. return $databaseDao->addDatabase($dbName, $dbVersion, $_SESSION['userID']);
  51. }
  52. /**
  53. * delete database
  54. * 删除数据库
  55. * @param $dbID int 数据库ID
  56. * @return bool
  57. */
  58. public function deleteDatabase(&$dbID)
  59. {
  60. $databaseDao = new DatabaseDao;
  61. if ($dbID = $databaseDao->checkDatabasePermission($dbID, $_SESSION['userID'])) {
  62. return $databaseDao->deleteDatabase($dbID);
  63. } else
  64. return FALSE;
  65. }
  66. /**
  67. * get all database list
  68. * 获取数据库列表
  69. * @return bool|array
  70. */
  71. public function getDatabase()
  72. {
  73. $databaseDao = new DatabaseDao;
  74. return $databaseDao->getDatabase($_SESSION['userID']);
  75. }
  76. /**
  77. * edit database
  78. * 修改数据库
  79. * @param $dbID int 数据库ID
  80. * @param $dbName string 数据库名
  81. * @param $dbVersion string 数据库版本
  82. * @return bool
  83. */
  84. public function editDatabase(&$dbID, &$dbName, &$dbVersion)
  85. {
  86. $databaseDao = new DatabaseDao;
  87. if ($dbID = $databaseDao->checkDatabasePermission($dbID, $_SESSION['userID'])) {
  88. return $databaseDao->editDatabase($dbID, $dbName, $dbVersion);
  89. } else
  90. return FALSE;
  91. }
  92. /**
  93. * Import database table which export from mysql
  94. * 导入数据表
  95. * @param $dbID int 数据库ID
  96. * @param $tables array 数据库表
  97. * @return bool
  98. */
  99. public function importDatabase(&$dbID, &$tables)
  100. {
  101. $userID = $_SESSION['userID'];
  102. $databaseDao = new DatabaseDao;
  103. $databaseTableDao = new DatabaseTableDao;
  104. if ($dbID = $databaseDao->checkDatabasePermission($dbID, $_SESSION['userID'])) {
  105. $tableList = array();
  106. foreach ($tables as $table) {
  107. $fieldList = array();
  108. //将各字段信息分割成一行一个
  109. preg_match_all('/.+?[\r\n]+/s', $table['tableField'], $fields);
  110. $primaryKeys = '';
  111. foreach ($fields[0] as $field) {
  112. $field = trim($field);
  113. //以'`'开头的是字段
  114. if (strpos($field, '`') === 0) {
  115. $fieldName = substr($field, 1, strpos(substr($field, 1), '`'));
  116. //将字段类型和长度的混合提取出来
  117. preg_match('/`\\s(.+?)\\s/', $field, $type);
  118. preg_match("/COMMENT.*'(.*?)'/", $field, $fieldDesc);
  119. if (empty($fieldDesc)) {
  120. preg_match("/comment.*'(.*?)'/", $field, $fieldDesc);
  121. }
  122. if (!$type[1]) {
  123. $type[1] = substr($field, strlen($fieldName) + 3, strpos(substr($field, strlen($fieldName) + 3), ','));
  124. }
  125. if (!$type[1]) {
  126. $type[1] = substr($field, strlen($fieldName) + 3);
  127. }
  128. if (strpos($type[1], '(')) {
  129. $fieldType = substr($type[1], 0, strpos($type[1], '('));
  130. if (preg_match('/\([0-9]{1,10}/', $type[1], $match)) {
  131. //长度用左括号右边第一个10位内数字表示
  132. $fieldLength = substr($match[0], 1);
  133. } else {
  134. $fieldLength = '0';
  135. }
  136. } else {
  137. $fieldType = $type[1];
  138. //未注明长度,默认为0
  139. $fieldLength = '0';
  140. }
  141. if (strpos($field, 'NOT NULL') !== FALSE) {
  142. $isNotNull = 1;
  143. } else
  144. $isNotNull = 0;
  145. $fieldList[] = array(
  146. 'fieldName' => $fieldName,
  147. 'fieldDesc' => $fieldDesc[1],
  148. 'fieldType' => $fieldType,
  149. 'fieldLength' => $fieldLength,
  150. 'isNotNull' => $isNotNull
  151. );
  152. }
  153. //以PRIMARY KEY开头的是整个表中主键的集合
  154. if (strpos($field, 'PRIMARY') !== FALSE) {
  155. $table['primaryKey'] = $table['primaryKey'] . substr($field, strpos($field, '('));
  156. }
  157. }
  158. //判断各字段是否为主键
  159. foreach ($fieldList as &$tableField) {
  160. if (strpos($table['primaryKey'], $tableField['fieldName']) !== FALSE) {
  161. $tableField['isPrimaryKey'] = 1;
  162. } else {
  163. $tableField['isPrimaryKey'] = 0;
  164. }
  165. }
  166. $tableList[] = array(
  167. 'tableName' => $table['tableName'],
  168. 'tableDesc' => $table['tableDesc'],
  169. 'fieldList' => $fieldList
  170. );
  171. unset($fieldList);
  172. }
  173. if (isset($tableList[0]))
  174. return $databaseDao->importDatabase($dbID, $tableList);
  175. else
  176. return FALSE;
  177. } else
  178. return FALSE;
  179. }
  180. /**
  181. * Import database by database's data which export from the api named exportDatabase
  182. * 导入数据字典界面数据库
  183. * @param $data string 数据库相关数据
  184. * @return bool
  185. */
  186. public function importDatabseByJson(&$data)
  187. {
  188. $user_id = $_SESSION['userID'];
  189. $service = new DatabaseDao;
  190. $result = $service->importDatabaseByJson($user_id, $data);
  191. if ($result)
  192. return TRUE;
  193. else
  194. return FALSE;
  195. }
  196. /**
  197. * Export database's data
  198. * 数据表导出成为json格式
  199. * @param $dbID int 数据库ID
  200. * @return bool|string
  201. */
  202. public function exportDatabase(&$dbID)
  203. {
  204. $userID = $_SESSION['userID'];
  205. $dao = new DatabaseDao;
  206. if ($dao->checkDatabasePermission($dbID, $_SESSION['userID'])) {
  207. $dumpJson = json_encode($dao->getDatabaseInfo($dbID));
  208. $fileName = 'eolinker_export_' . $_SESSION['userName'] . '_' . time() . '.export';
  209. if (file_put_contents(realpath('./dump') . DIRECTORY_SEPARATOR . $fileName, $dumpJson)) {
  210. return $fileName;
  211. } else {
  212. return FALSE;
  213. }
  214. } else
  215. return FALSE;
  216. }
  217. }
  218. ?>