tripal_entities.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. /**
  3. * @file
  4. * Install for a tripal data entity - creates the base table for our entity.
  5. */
  6. function tripal_entities_install() {
  7. // add any external databases used by the file module.
  8. // tripal_entities_add_dbs();
  9. // add any controlled vocabularies used by the file module.
  10. // tripal_entities_add_cvs();
  11. // add any controlled vocabulary terms
  12. // tripal_entities_add_cvterms();
  13. // add any custom tables. For this case we will add an 'file' table to the
  14. // chado schema
  15. tripal_entities_add_custom_tables();
  16. drupal_set_message('The current Entity Example (Genes) requires the Sequence Ontology.', 'warning');
  17. }
  18. /**
  19. *
  20. */
  21. function tripal_entities_add_custom_tables() {
  22. tripal_entities_add_tripal_entity_type_table();
  23. tripal_entities_add_tripal_entity_type_source_table();
  24. tripal_entities_add_tripal_entity_bundle_table();
  25. tripal_entities_add_tripal_entity_bundle_source_table();
  26. tripal_entities_add_tripal_entity_relationship_table();
  27. }
  28. /**
  29. *
  30. */
  31. function tripal_entities_add_tripal_entity_type_table() {
  32. $schema = array (
  33. 'table' => 'tripal_entity_type',
  34. 'fields' => array (
  35. 'entity_type_id' => array(
  36. 'type' => 'serial',
  37. 'not null' => TRUE
  38. ),
  39. 'cv_id' => array (
  40. 'type' => 'int',
  41. 'not null' => TRUE
  42. ),
  43. 'db_id' => array (
  44. 'type' => 'int',
  45. 'not null' => TRUE
  46. ),
  47. 'publish' => array (
  48. 'type' => 'int',
  49. 'not null' => TRUE,
  50. 'default' => 0
  51. ),
  52. ),
  53. 'primary key' => array (
  54. 0 => 'entity_type_id'
  55. ),
  56. 'foreign keys' => array (
  57. 'cv' => array (
  58. 'table' => 'cv',
  59. 'columns' => array (
  60. 'cv_id' => 'cv_id'
  61. )
  62. ),
  63. 'db' => array (
  64. 'table' => 'db',
  65. 'columns' => array (
  66. 'db_id' => 'db_id'
  67. )
  68. ),
  69. ),
  70. 'unique keys' => array (
  71. 'tripal_entity_type_cvdb' => array (
  72. 'cv_id', 'db_id'
  73. ),
  74. ),
  75. 'indexes' => array(
  76. 'tripal_entity_type_cv_id' => array('cv_id'),
  77. 'tripal_entity_type_db_id' => array('db_id'),
  78. )
  79. );
  80. chado_create_custom_table('tripal_entity_type', $schema, TRUE);
  81. }
  82. /**
  83. *
  84. */
  85. function tripal_entities_add_tripal_entity_bundle_table() {
  86. $schema = array (
  87. 'table' => 'tripal_entity_bundle',
  88. 'fields' => array (
  89. 'bundle_id' => array(
  90. 'type' => 'serial',
  91. 'not null' => TRUE
  92. ),
  93. 'entity_type_id' => array (
  94. 'type' => 'int',
  95. 'not null' => TRUE
  96. ),
  97. 'cvterm_id' => array (
  98. 'type' => 'int',
  99. 'not null' => TRUE
  100. ),
  101. 'publish' => array (
  102. 'type' => 'int',
  103. 'not null' => TRUE,
  104. 'default' => 0
  105. ),
  106. ),
  107. 'primary key' => array (
  108. 0 => 'bundle_id'
  109. ),
  110. 'foreign keys' => array (
  111. 'cvterm' => array (
  112. 'table' => 'cvterm',
  113. 'columns' => array (
  114. 'cvterm_id' => 'cvterm_id'
  115. )
  116. ),
  117. 'tripal_entity_type' => array (
  118. 'table' => 'tripal_entity_type',
  119. 'columns' => array (
  120. 'entity_type_id' => 'entity_type_id'
  121. )
  122. ),
  123. ),
  124. 'unique keys' => array (
  125. 'tripal_entity_bundle_unq' => array (
  126. 'entity_type_id', 'cvterm_id'
  127. ),
  128. ),
  129. 'indexes' => array(
  130. 'tripal_entity_bundle_entity_type_id' => array('entity_type_id'),
  131. 'tripal_entity_bundle_cvterm_id' => array('cvterm_id'),
  132. ),
  133. );
  134. chado_create_custom_table('tripal_entity_bundle', $schema, TRUE);
  135. }
  136. /**
  137. *
  138. */
  139. function tripal_entities_add_tripal_entity_type_source_table(){
  140. $schema = array (
  141. 'table' => 'tripal_entity_type_source',
  142. 'fields' => array (
  143. 'entity_type_source_id' => array(
  144. 'type' => 'serial',
  145. 'not null' => TRUE
  146. ),
  147. 'entity_type_id' => array (
  148. 'type' => 'int',
  149. 'not null' => TRUE
  150. ),
  151. 'data_table' => array (
  152. 'type' => 'varchar',
  153. 'length' => 128,
  154. 'not null' => TRUE
  155. ),
  156. 'type_table' => array (
  157. 'type' => 'varchar',
  158. 'length' => 128,
  159. 'not null' => TRUE
  160. ),
  161. 'field' => array (
  162. 'type' => 'varchar',
  163. 'length' => 128,
  164. 'not null' => TRUE
  165. ),
  166. ),
  167. 'primary key' => array (
  168. 0 => 'entity_type_source_id'
  169. ),
  170. 'foreign keys' => array (
  171. 'tripal_entity_type' => array (
  172. 'table' => 'tripal_entity_type',
  173. 'columns' => array (
  174. 'entity_type_id' => 'entity_type_id'
  175. ),
  176. ),
  177. ),
  178. 'unique keys' => array (
  179. 'tripal_entity_type_ridbase' => array (
  180. 'entity_type_id', 'data_table'
  181. ),
  182. ),
  183. 'indexes' => array(
  184. 'tripal_entity_type_entity_type_id' => array('entity_type_id'),
  185. 'tripal_entity_type_data_table' => array('data_table'),
  186. 'tripal_entity_type_type_table' => array('type_table'),
  187. ),
  188. );
  189. chado_create_custom_table('tripal_entity_type_source', $schema, TRUE);
  190. }
  191. /**
  192. *
  193. */
  194. function tripal_entities_add_tripal_entity_bundle_source_table(){
  195. $schema = array (
  196. 'table' => 'tripal_entity_bundle_source',
  197. 'fields' => array (
  198. 'bundle_source_id' => array(
  199. 'type' => 'serial',
  200. 'not null' => TRUE
  201. ),
  202. 'bundle_id' => array (
  203. 'type' => 'int',
  204. 'not null' => TRUE
  205. ),
  206. 'data_table' => array (
  207. 'type' => 'varchar',
  208. 'length' => 128,
  209. 'not null' => TRUE
  210. ),
  211. 'type_table' => array (
  212. 'type' => 'varchar',
  213. 'length' => 128,
  214. 'not null' => TRUE
  215. ),
  216. 'field' => array (
  217. 'type' => 'varchar',
  218. 'length' => 128,
  219. 'not null' => TRUE
  220. ),
  221. ),
  222. 'primary key' => array (
  223. 0 => 'bundle_source_id'
  224. ),
  225. 'foreign keys' => array (
  226. 'tripal_entity_bundle' => array (
  227. 'table' => 'tripal_entity_bundle',
  228. 'columns' => array (
  229. 'bundle_id' => 'bundle_id'
  230. ),
  231. ),
  232. ),
  233. 'unique keys' => array (
  234. 'tripal_entity_bundle_source_ridbase' => array (
  235. 'bundle_id', 'type_table', 'field'
  236. ),
  237. ),
  238. 'indexes' => array(
  239. 'tripal_entity_bundle_source_bundle_id' => array('bundle_id'),
  240. ),
  241. );
  242. chado_create_custom_table('tripal_entity_bundle_source', $schema, TRUE);
  243. }
  244. /**
  245. *
  246. */
  247. function tripal_entities_add_tripal_entity_relationship_table(){
  248. $schema = array (
  249. 'table' => 'tripal_entity_relationship',
  250. 'fields' => array (
  251. 'relationship_id' => array(
  252. 'type' => 'serial',
  253. 'not null' => TRUE
  254. ),
  255. 'subject_id' => array (
  256. 'type' => 'int',
  257. 'not null' => TRUE
  258. ),
  259. 'type_id' => array (
  260. 'type' => 'int',
  261. 'not null' => TRUE
  262. ),
  263. 'object_id' => array (
  264. 'type' => 'int',
  265. 'not null' => FALSE
  266. ),
  267. 'fieldname' => array(
  268. 'type' => 'varchar',
  269. 'length' => 128,
  270. 'not null' => FALSE,
  271. )
  272. ),
  273. 'primary key' => array (
  274. 0 => 'relationship_id'
  275. ),
  276. 'foreign keys' => array (
  277. 'tripal_entity_bundle' => array (
  278. 'table' => 'tripal_entity_bundle',
  279. 'columns' => array (
  280. 'subject_id' => 'bundle_id',
  281. 'object_id' => 'bundle_id',
  282. ),
  283. ),
  284. ),
  285. 'unique keys' => array (
  286. 'tripal_entity_relationship_unq' => array (
  287. 'subject_id', 'type_id', 'object_id'
  288. ),
  289. ),
  290. 'indexes' => array(
  291. 'tripal_entity_relationship_subject_id' => array('subject_id'),
  292. 'tripal_entity_relationship_object_id' => array('object_id'),
  293. 'tripal_entity_relationship_type_id' => array('type_id'),
  294. ),
  295. );
  296. chado_create_custom_table('tripal_entity_relationship', $schema, TRUE);
  297. }
  298. /**
  299. * Implements hook_schema().
  300. *
  301. * @ingroup entity_example
  302. */
  303. function tripal_entities_schema() {
  304. $schema['tripal_data'] = array(
  305. 'description' => 'The base table for Tripal Vocabulary-based entities.',
  306. 'fields' => array(
  307. 'id' => array(
  308. 'description' => 'The primary identifier for a vocabulary entity.',
  309. 'type' => 'serial',
  310. 'unsigned' => TRUE,
  311. 'not null' => TRUE,
  312. ),
  313. 'type' => array(
  314. 'description' => 'The type of entity. This should be an official term ID.',
  315. 'type' => 'varchar',
  316. 'length' => 64,
  317. 'not null' => TRUE,
  318. 'default' => '',
  319. ),
  320. 'cvterm_id' => array(
  321. 'description' => 'The cvterm_id for the type of entity. This cvterm_id should match a record in the Chado cvterm table.',
  322. 'type' => 'varchar',
  323. 'length' => 32,
  324. 'not null' => TRUE,
  325. 'default' => '',
  326. ),
  327. 'tablename' => array(
  328. 'description' => 'The Chado table that contains the record that this entity is associated with.',
  329. 'type' => 'varchar',
  330. 'length' => 128,
  331. 'not null' => TRUE,
  332. 'default' => ''
  333. ),
  334. 'record_id' => array(
  335. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  336. 'type' => 'int',
  337. 'not null' => TRUE,
  338. ),
  339. 'title' => array(
  340. 'description' => 'The title of this node, always treated as non-markup plain text.',
  341. 'type' => 'text',
  342. 'not null' => TRUE,
  343. 'default' => '',
  344. ),
  345. 'uid' => array(
  346. 'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
  347. 'type' => 'int',
  348. 'not null' => TRUE,
  349. 'default' => 0,
  350. ),
  351. 'status' => array(
  352. 'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
  353. 'type' => 'int',
  354. 'not null' => TRUE,
  355. 'default' => 1,
  356. ),
  357. 'created' => array(
  358. 'description' => 'The Unix timestamp when the node was created.',
  359. 'type' => 'int',
  360. 'not null' => TRUE,
  361. 'default' => 0,
  362. ),
  363. 'changed' => array(
  364. 'description' => 'The Unix timestamp when the node was most recently saved.',
  365. 'type' => 'int',
  366. 'not null' => TRUE,
  367. 'default' => 0,
  368. ),
  369. ),
  370. 'indexes' => array(
  371. 'entity_changed' => array('changed'),
  372. 'entity_created' => array('created'),
  373. 'tablename' => array('tablename'),
  374. 'record_id' => array('record_id'),
  375. 'tripal_record' => array('tablename', 'record_id'),
  376. 'type' => array('type'),
  377. 'cvterm_id' => array('cvterm_id'),
  378. 'uid' => array('uid'),
  379. ),
  380. 'unique keys' => array(
  381. 'record' => array('tablename', 'record_id'),
  382. ),
  383. 'primary key' => array('id'),
  384. );
  385. $schema['tripal_data_type'] = array(
  386. 'description' => 'Stores information about defined tripal data types.',
  387. 'fields' => array(
  388. 'id' => array(
  389. 'type' => 'serial',
  390. 'not null' => TRUE,
  391. 'description' => 'Primary Key: Unique Chado data type identifier.',
  392. ),
  393. 'type' => array(
  394. 'description' => 'The machine-readable name of this tripal data type.',
  395. 'type' => 'varchar',
  396. 'length' => 255,
  397. 'not null' => TRUE,
  398. ),
  399. 'label' => array(
  400. 'description' => 'The human-readable name of this tripal data type.',
  401. 'type' => 'varchar',
  402. 'length' => 255,
  403. 'not null' => TRUE,
  404. 'default' => '',
  405. ),
  406. 'weight' => array(
  407. 'type' => 'int',
  408. 'not null' => TRUE,
  409. 'default' => 0,
  410. 'size' => 'tiny',
  411. 'description' => 'The weight of this tripal data type in relation to others.',
  412. ),
  413. 'data' => array(
  414. 'type' => 'text',
  415. 'not null' => FALSE,
  416. 'size' => 'big',
  417. 'serialize' => TRUE,
  418. 'description' => 'A serialized array of additional data related to this tripal data type.',
  419. ),
  420. ) + entity_exportable_schema_fields(),
  421. 'primary key' => array('id'),
  422. 'unique keys' => array(
  423. 'type' => array('type'),
  424. ),
  425. );
  426. return $schema;
  427. }
  428. /**
  429. * Implements hook_uninstall().
  430. *
  431. * At uninstall time we'll notify field.module that the entity was deleted
  432. * so that attached fields can be cleaned up.
  433. */
  434. function tripal_entities_uninstall() {
  435. // TODO: make this dynamic (not hardcoded bundle).
  436. field_attach_delete_bundle('tripal_data', 'gene');
  437. }