chado_install.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*************************************************************************
  3. *
  4. */
  5. function tripal_core_install_job (){
  6. global $user;
  7. $args = array();
  8. tripal_add_job("Install Chado",'tripal_core',
  9. 'tripal_core_install_chado',$args,$user->uid);
  10. return '';
  11. }
  12. /*************************************************************************
  13. *
  14. */
  15. function tripal_core_install_chado ($dummy = NULL, $job = NULL){
  16. $schema_file = drupal_get_path('module', 'tripal_core').'/default_schema.sql';
  17. $init_file = drupal_get_path('module', 'tripal_core').'/initialize.sql';
  18. tripal_core_reset_chado_schema();
  19. tripal_core_install_sql($schema_file);
  20. tripal_core_install_sql($init_file);
  21. }
  22. /*************************************************************************
  23. *
  24. */
  25. function tripal_core_reset_chado_schema (){
  26. global $active_db;
  27. // iterate through the lines of the schema file and rebuild the SQL
  28. pg_query($active_db,"drop schema chado cascade");
  29. pg_query($active_db,"drop schema genetic_code cascade");
  30. pg_query($active_db,"drop schema so cascade");
  31. pg_query($active_db,"drop schema frange cascade");
  32. pg_query($active_db,"create schema chado");
  33. pg_query($active_db,"create language plpgsql");
  34. }
  35. /*************************************************************************
  36. *
  37. */
  38. function tripal_core_install_sql ($sql_file){
  39. global $active_db;
  40. pg_query($active_db,"set search_path to chado,public");
  41. print "Loading $sql_file...\n";
  42. $lines = file($sql_file,FILE_SKIP_EMPTY_LINES);
  43. if(!$lines){
  44. return 'Cannot open $schema_file';
  45. }
  46. $stack = array();
  47. $in_string = 0;
  48. $query = '';
  49. $i = 0;
  50. foreach ($lines as $line_num => $line) {
  51. $i++;
  52. $type = '';
  53. // find and remove comments except when inside of strings
  54. if(preg_match('/--/',$line) and !$in_string and !preg_match("/'.*?--.*?'/",$line)){
  55. $line = preg_replace('/--.*$/','',$line); // remove comments
  56. }
  57. if(preg_match('/\/\*.*?\*\//',$line)){
  58. $line = preg_replace('/\/\*.*?\*\//','',$line); // remove comments
  59. }
  60. // skip empty lines
  61. if(preg_match('/^\s*$/',$line) or strcmp($line,'')==0){
  62. continue;
  63. }
  64. // Find SQL for new objects
  65. if(preg_match('/^\s*CREATE\s+TABLE/i',$line) and !$in_string){
  66. $stack[] = 'table';
  67. $line = preg_replace("/public./","chado.",$line);
  68. }
  69. if(preg_match('/^\s*ALTER\s+TABLE/i',$line) and !$in_string){
  70. $stack[] = 'alter table';
  71. $line = preg_replace("/public./","chado.",$line);
  72. }
  73. if(preg_match('/^\s*SET/i',$line) and !$in_string){
  74. $stack[] = 'set';
  75. }
  76. if(preg_match('/^\s*CREATE\s+SCHEMA/i',$line) and !$in_string){
  77. $stack[] = 'schema';
  78. }
  79. if(preg_match('/^\s*CREATE\s+SEQUENCE/i',$line) and !$in_string){
  80. $stack[] = 'sequence';
  81. $line = preg_replace("/public./","chado.",$line);
  82. }
  83. if(preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i',$line) and !$in_string){
  84. $stack[] = 'view';
  85. $line = preg_replace("/public./","chado.",$line);
  86. }
  87. if(preg_match('/^\s*COMMENT/i',$line) and !$in_string and sizeof($stack)==0){
  88. $stack[] = 'comment';
  89. $line = preg_replace("/public./","chado.",$line);
  90. }
  91. if(preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i',$line) and !$in_string){
  92. $stack[] = 'function';
  93. $line = preg_replace("/public./","chado.",$line);
  94. }
  95. if(preg_match('/^\s*CREATE\s+INDEX/i',$line) and !$in_string){
  96. $stack[] = 'index';
  97. }
  98. if(preg_match('/^\s*INSERT\s+INTO/i',$line) and !$in_string){
  99. $stack[] = 'insert';
  100. $line = preg_replace("/public./","chado.",$line);
  101. }
  102. if(preg_match('/^\s*CREATE\s+TYPE/i',$line) and !$in_string){
  103. $stack[] = 'type';
  104. }
  105. if(preg_match('/^\s*GRANT/i',$line) and !$in_string){
  106. $stack[] = 'grant';
  107. }
  108. if(preg_match('/^\s*CREATE\s+AGGREGATE/i',$line) and !$in_string){
  109. $stack[] = 'aggregate';
  110. }
  111. // determine if we are in a string that spans a line
  112. $matches = preg_match_all("/[']/i",$line,$temp);
  113. $in_string = $in_string - ($matches % 2);
  114. $in_string = abs($in_string);
  115. // if we've reached the end of an object the pop the stack
  116. if(strcmp($stack[sizeof($stack)-1],'table') == 0 and preg_match('/\);\s*$/',$line)){
  117. $type = array_pop($stack);
  118. }
  119. if(strcmp($stack[sizeof($stack)-1],'alter table') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  120. $type = array_pop($stack);
  121. }
  122. if(strcmp($stack[sizeof($stack)-1],'set') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  123. $type = array_pop($stack);
  124. }
  125. if(strcmp($stack[sizeof($stack)-1],'schema') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  126. $type = array_pop($stack);
  127. }
  128. if(strcmp($stack[sizeof($stack)-1],'sequence') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  129. $type = array_pop($stack);
  130. }
  131. if(strcmp($stack[sizeof($stack)-1],'view') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  132. $type = array_pop($stack);
  133. }
  134. if(strcmp($stack[sizeof($stack)-1],'comment') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  135. $type = array_pop($stack);
  136. }
  137. if(strcmp($stack[sizeof($stack)-1],'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i",$line)){
  138. $type = array_pop($stack);
  139. }
  140. if(strcmp($stack[sizeof($stack)-1],'index') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  141. $type = array_pop($stack);
  142. }
  143. if(strcmp($stack[sizeof($stack)-1],'insert') == 0 and preg_match('/\);\s*$/',$line)){
  144. $type = array_pop($stack);
  145. }
  146. if(strcmp($stack[sizeof($stack)-1],'type') == 0 and preg_match('/\);\s*$/',$line)){
  147. $type = array_pop($stack);
  148. }
  149. if(strcmp($stack[sizeof($stack)-1],'grant') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  150. $type = array_pop($stack);
  151. }
  152. if(strcmp($stack[sizeof($stack)-1],'aggregate') == 0 and preg_match('/\);\s*$/',$line)){
  153. $type = array_pop($stack);
  154. }
  155. // if we're in a recognized SQL statement then let's keep track of lines
  156. if($type or sizeof($stack) > 0){
  157. $query .= "$line";
  158. } else {
  159. print "UNHANDLED $i, $in_string: $line";
  160. return tripal_core_chado_install_done();
  161. }
  162. if(preg_match_all("/\n/",$query,$temp) > 100){
  163. print "SQL query is too long. Terminating:\n$query\n";
  164. return tripal_core_chado_install_done();
  165. }
  166. if($type and sizeof($stack) == 0){
  167. print "Adding $type: line $i\n";
  168. // rewrite the set serach_path to make 'public' be 'chado'
  169. if(strcmp($type,'set')==0){
  170. $query = preg_replace("/public/m","chado",$query);
  171. }
  172. $result = pg_query($active_db, $query);
  173. if(!$result){
  174. $error = pg_last_error();
  175. print "Installation failed:\nSQL $i, $in_string: $query\n$error\n";
  176. pg_query($active_db,"set search_path to public,chado");
  177. return tripal_core_chado_install_done();
  178. }
  179. $query = '';
  180. }
  181. }
  182. print "Installation Complete!\n";
  183. tripal_core_chado_install_done();
  184. }
  185. /*************************************************************************
  186. *
  187. */
  188. function tripal_core_chado_install_done (){
  189. // return the search path to normal
  190. global $active_db;
  191. pg_query($active_db,"set search_path to public,chado");
  192. }