chado_install.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. }
  68. if(preg_match('/^\s*ALTER\s+TABLE/i',$line) and !$in_string){
  69. $stack[] = 'alter table';
  70. }
  71. if(preg_match('/^\s*SET/i',$line) and !$in_string){
  72. $stack[] = 'set';
  73. }
  74. if(preg_match('/^\s*CREATE\s+SCHEMA/i',$line) and !$in_string){
  75. $stack[] = 'schema';
  76. }
  77. if(preg_match('/^\s*CREATE\s+SEQUENCE/i',$line) and !$in_string){
  78. $stack[] = 'sequence';
  79. }
  80. if(preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i',$line) and !$in_string){
  81. $stack[] = 'view';
  82. }
  83. if(preg_match('/^\s*COMMENT/i',$line) and !$in_string and sizeof($stack)==0){
  84. $stack[] = 'comment';
  85. }
  86. if(preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i',$line) and !$in_string){
  87. $stack[] = 'function';
  88. }
  89. if(preg_match('/^\s*CREATE\s+INDEX/i',$line) and !$in_string){
  90. $stack[] = 'index';
  91. }
  92. if(preg_match('/^\s*INSERT\s+INTO/i',$line) and !$in_string){
  93. $stack[] = 'insert';
  94. }
  95. if(preg_match('/^\s*CREATE\s+TYPE/i',$line) and !$in_string){
  96. $stack[] = 'type';
  97. }
  98. if(preg_match('/^\s*GRANT/i',$line) and !$in_string){
  99. $stack[] = 'grant';
  100. }
  101. if(preg_match('/^\s*CREATE\s+AGGREGATE/i',$line) and !$in_string){
  102. $stack[] = 'aggregate';
  103. }
  104. // determine if we are in a string that spans a line
  105. $matches = preg_match_all("/[']/i",$line,$temp);
  106. $in_string = $in_string - ($matches % 2);
  107. $in_string = abs($in_string);
  108. // if we've reached the end of an object the pop the stack
  109. if(strcmp($stack[sizeof($stack)-1],'table') == 0 and preg_match('/\);\s*$/',$line)){
  110. $type = array_pop($stack);
  111. }
  112. if(strcmp($stack[sizeof($stack)-1],'alter table') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  113. $type = array_pop($stack);
  114. }
  115. if(strcmp($stack[sizeof($stack)-1],'set') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  116. $type = array_pop($stack);
  117. }
  118. if(strcmp($stack[sizeof($stack)-1],'schema') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  119. $type = array_pop($stack);
  120. }
  121. if(strcmp($stack[sizeof($stack)-1],'sequence') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  122. $type = array_pop($stack);
  123. }
  124. if(strcmp($stack[sizeof($stack)-1],'view') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  125. $type = array_pop($stack);
  126. }
  127. if(strcmp($stack[sizeof($stack)-1],'comment') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  128. $type = array_pop($stack);
  129. }
  130. if(strcmp($stack[sizeof($stack)-1],'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i",$line)){
  131. $type = array_pop($stack);
  132. }
  133. if(strcmp($stack[sizeof($stack)-1],'index') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  134. $type = array_pop($stack);
  135. }
  136. if(strcmp($stack[sizeof($stack)-1],'insert') == 0 and preg_match('/\);\s*$/',$line)){
  137. $type = array_pop($stack);
  138. }
  139. if(strcmp($stack[sizeof($stack)-1],'type') == 0 and preg_match('/\);\s*$/',$line)){
  140. $type = array_pop($stack);
  141. }
  142. if(strcmp($stack[sizeof($stack)-1],'grant') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  143. $type = array_pop($stack);
  144. }
  145. if(strcmp($stack[sizeof($stack)-1],'aggregate') == 0 and preg_match('/\);\s*$/',$line)){
  146. $type = array_pop($stack);
  147. }
  148. // if we're in a recognized SQL statement then let's keep track of lines
  149. if($type or sizeof($stack) > 0){
  150. $query .= "$line";
  151. } else {
  152. print "UNHANDLED $i, $in_string: $line";
  153. return tripal_core_chado_install_done();
  154. }
  155. if(preg_match_all("/\n/",$query,$temp) > 100){
  156. print "SQL query is too long. Terminating:\n$query\n";
  157. return tripal_core_chado_install_done();
  158. }
  159. if($type and sizeof($stack) == 0){
  160. print "Adding $type: line $i\n";
  161. // rewrite the set serach_path to make 'public' be 'chado'
  162. if(strcmp($type,'set')==0){
  163. $query = preg_replace("/public/m","chado",$query);
  164. }
  165. $result = pg_query($active_db, $query);
  166. if(!$result){
  167. $error = pg_last_error();
  168. print "Installation failed:\nSQL $i, $in_string: $query\n$error\n";
  169. pg_query($active_db,"set search_path to public,chado");
  170. return tripal_core_chado_install_done();
  171. }
  172. $query = '';
  173. }
  174. }
  175. print "Installation Complete!\n";
  176. tripal_core_chado_install_done();
  177. }
  178. /*************************************************************************
  179. *
  180. */
  181. function tripal_core_chado_install_done (){
  182. // return the search path to normal
  183. global $active_db;
  184. pg_query($active_db,"set search_path to public,chado");
  185. }