tripal_core.chado_install.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /**
  3. * @file
  4. * Functions to install chado schema through Drupal
  5. */
  6. /**
  7. * Load Chado Schema Form
  8. *
  9. * @ingroup tripal_core
  10. */
  11. function tripal_core_chado_load_form() {
  12. // we want to force the version of Chado to be set properly
  13. $real_version = chado_get_version(TRUE);
  14. // get the effective version. Pass true as second argument
  15. // to warn the user if the current version is not compatible
  16. $version = chado_get_version(FALSE, TRUE);
  17. if ($real_version == '1.2') {
  18. drupal_set_message('Please note: the upgrade of Chado from v1.2 to v1.3 may
  19. require three fixes to your database. All of the primary keys
  20. in Chado were changed from integers to big integers to support larger
  21. tables. First, if your site has custom materialized views that will hold
  22. data derived from fields changed to big integers then you may need to
  23. alter the views to change the fields from integers to big integers
  24. and repopulate those views. Second, if you have made
  25. any custom PL/pgSQL functions that expect primary and foreign key fields
  26. to be integers, then those functions will need to be altered to accept
  27. big integers. Third, if you have PostgreSQL Views that use fields
  28. that are converted to big integers then most likely this upgrade will
  29. fail. You must first remove those views, perform the upgrade and then
  30. recreate them with the appropriate fields change to big integers.
  31. The Tripal upgrader is not able to fix these problems automatically',
  32. 'warning');
  33. }
  34. $form['current_version'] = array(
  35. '#type' => 'item',
  36. '#title' => t("Current installed version of Chado:"),
  37. '#description' => $real_version,
  38. );
  39. $form['action_to_do'] = array(
  40. '#type' => 'radios',
  41. '#title' => 'Installation/Upgrade Action',
  42. '#options' => array(
  43. 'Install Chado v1.3' => t('New Install of Chado v1.3 (erases all existing Chado data if Chado already exists)'),
  44. 'Upgrade Chado v1.2 to v1.3' => t('Upgrade existing Chado v1.2 to v1.3 (no data is lost)'),
  45. 'Install Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'),
  46. 'Upgrade Chado v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'),
  47. 'Install Chado v1.11' => t('New Install of Chado v1.11 (erases all existing Chado data if Chado already exists)'),
  48. ),
  49. '#description' => t('Select an action to perform. If you want to install Chado all other Tripal modules must not be installed.'),
  50. '#required' => TRUE,
  51. );
  52. $form['warning'] = array(
  53. '#markup' => "<div><font color=\"red\">WARNING:</font>" . t('A new install of
  54. Chado will remove and recreate the Chado database if it already exists.') . '</div>',
  55. );
  56. $form['button'] = array(
  57. '#type' => 'submit',
  58. '#value' => t('Install/Upgrade Chado'),
  59. );
  60. return $form;
  61. }
  62. function tripal_core_chado_load_form_validate($form, &$form_state) {
  63. // We do not want to allow re-installation of Chado if other
  64. // Tripal modules are installed. This is because the install files
  65. // of those modules may add content to Chado and reinstalling Chado
  66. // removes that content which may break the modules.
  67. if ($form_state['values']['action_to_do'] == "Install Chado v1.3" or
  68. $form_state['values']['action_to_do'] == "Install Chado v1.2" or
  69. $form_state['values']['action_to_do'] == "Install Chado v1.11") {
  70. $modules = system_get_info('module');
  71. // The tripal_views module should not be included as it's a rquired
  72. // dependency of tripal_core
  73. unset($modules['tripal_views']);
  74. $list = array();
  75. foreach ($modules as $mname => $module) {
  76. if (array_key_exists('dependencies', $module) and in_array('tripal_core', $module['dependencies'])) {
  77. $list[] = $module['name'] . " ($mname)";
  78. }
  79. }
  80. if (count($list) > 0) {
  81. form_set_error("action_to_do", "Chado cannot be installed while other Tripal modules
  82. are enabled. You must fully uninstall the following modules if you
  83. would like to install or re-install chado.<br>" .
  84. implode("<br>", $list));
  85. }
  86. }
  87. if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.11 to v1.2") {
  88. // Make sure we are already not at v1.2
  89. $real_version = chado_get_version(TRUE);
  90. if ($real_version == "1.2") {
  91. form_set_error("action_to_do", "You are already at v1.2. There is no need to upgrade.");
  92. }
  93. }
  94. if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.2 to v1.3") {
  95. // Make sure we are already not at v1.3
  96. $real_version = chado_get_version(TRUE);
  97. if ($real_version == "1.3") {
  98. form_set_error("action_to_do", "You are already at v1.3. There is no need to upgrade.");
  99. }
  100. }
  101. }
  102. /**
  103. * Submit Load Chado Schema Form
  104. *
  105. * @ingroup tripal_core
  106. */
  107. function tripal_core_chado_load_form_submit($form, &$form_state) {
  108. global $user;
  109. $action_to_do = trim($form_state['values']['action_to_do']);
  110. $args = array($action_to_do);
  111. tripal_add_job($action_to_do, 'tripal_core',
  112. 'tripal_core_install_chado', $args, $user->uid);
  113. }
  114. /**
  115. * Install Chado Schema
  116. *
  117. * @ingroup tripal_core
  118. */
  119. function tripal_core_install_chado($action) {
  120. $vsql = "
  121. INSERT INTO {chadoprop} (type_id, value)
  122. VALUES (
  123. (SELECT cvterm_id
  124. FROM {cvterm} CVT
  125. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  126. WHERE CV.name = 'chado_properties' AND CVT.name = 'version'),
  127. :version)
  128. ";
  129. $vusql = "
  130. UPDATE {chadoprop}
  131. SET value = :version
  132. WHERE type_id = (SELECT cvterm_id
  133. FROM {cvterm} CVT
  134. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  135. WHERE CV.name = 'chado_properties' AND CVT.name = 'version')
  136. ";
  137. $transaction = db_transaction();
  138. try {
  139. if ($action == 'Install Chado v1.3') {
  140. tripal_core_install_chado_1_3();
  141. chado_query($vsql, array(':version' => '1.3'));
  142. }
  143. elseif ($action == 'Upgrade Chado v1.2 to v1.3') {
  144. tripal_core_upgrade_chado_1_2_to_1_3();
  145. chado_query($vusql, array(':version' => '1.3'));
  146. }
  147. elseif ($action == 'Install Chado v1.2') {
  148. tripal_core_install_chado_1_2();
  149. chado_query($vsql, array(':version' => '1.2'));
  150. }
  151. elseif ($action == 'Upgrade Chado v1.11 to v1.2') {
  152. tripal_core_upgrade_chado_1_11_to_1_2();
  153. chado_query($vsql, array(':version' => '1.2'));
  154. }
  155. elseif ($action == 'Install Chado v1.11') {
  156. tripal_core_install_chado_1_11();
  157. }
  158. }
  159. catch (Exception $e) {
  160. $transaction->rollback();
  161. tripal_core_chado_install_done();
  162. tripal_report_error('tripal_core', TRIPAL_ERROR, $e->getMessage(), array('print' => TRUE));
  163. return FALSE;
  164. }
  165. return TRUE;
  166. }
  167. /**
  168. * Installs Chado v1.3.
  169. */
  170. function tripal_core_install_chado_1_3() {
  171. // Get the path to the schema and init SQL files.
  172. $schema_file = drupal_get_path('module', 'tripal_core') .
  173. '/chado_schema/default_schema-1.3.sql';
  174. $init_file = drupal_get_path('module', 'tripal_core') .
  175. '/chado_schema/initialize-1.3.sql';
  176. // Erase the Chado schema if it exists and perform the install.
  177. if (tripal_core_reset_chado_schema()) {
  178. $success = tripal_core_install_sql($schema_file);
  179. if ($success) {
  180. print "Install of Chado v1.3 (Step 1 of 2) Successful!\n";
  181. }
  182. else {
  183. throw new Exception("Installation (Step 1 of 2) Problems! Please check output above for errors.");
  184. }
  185. $success = tripal_core_install_sql($init_file);
  186. if ($success) {
  187. print "Install of Chado v1.3 (Step 2 of 2) Successful.\nInstallation Complete\n";
  188. }
  189. else {
  190. throw new Exception("Installation (Step 2 of 2) Problems! Please check output above for errors.");
  191. }
  192. }
  193. else {
  194. throw new Exception("ERROR: cannot install chado. Please check database permissions");
  195. }
  196. }
  197. /**
  198. * Installs Chado v1.2.
  199. */
  200. function tripal_core_install_chado_1_2() {
  201. // Get the path to the schema and init SQL files.
  202. $schema_file = drupal_get_path('module', 'tripal_core') .
  203. '/chado_schema/default_schema-1.2.sql';
  204. $init_file = drupal_get_path('module', 'tripal_core') .
  205. '/chado_schema/initialize-1.2.sql';
  206. // Erase the Chado schema if it exists and perform the install.
  207. if (tripal_core_reset_chado_schema()) {
  208. $success = tripal_core_install_sql($schema_file);
  209. if ($success) {
  210. print "Install of Chado v1.2 (Step 1 of 2) Successful!\n";
  211. }
  212. else {
  213. throw new Exception("Installation (Step 1 of 2) Problems! Please check output above for errors.");
  214. }
  215. $success = tripal_core_install_sql($init_file);
  216. if ($success) {
  217. print "Install of Chado v1.2 (Step 2 of 2) Successful.\nInstallation Complete\n";
  218. }
  219. else {
  220. throw new Exception("Installation (Step 2 of 2) Problems! Please check output above for errors.");
  221. }
  222. }
  223. else {
  224. throw new Exception("ERROR: cannot install chado. Please check database permissions");
  225. }
  226. }
  227. /**
  228. *
  229. */
  230. function tripal_core_install_chado_1_11() {
  231. // Get the path to the schema and init SQL files.
  232. $schema_file = drupal_get_path('module', 'tripal_core') .
  233. '/chado_schema/default_schema-1.11.sql';
  234. $init_file = drupal_get_path('module', 'tripal_core') .
  235. '/chado_schema/initialize-1.11.sql';
  236. // Erase the Chado schema if it exists and perform the install.
  237. if (tripal_core_reset_chado_schema()) {
  238. $success = tripal_core_install_sql($schema_file);
  239. if ($success) {
  240. print "Install of Chado v1.11 (Step 1 of 2) Successful!\n";
  241. }
  242. else {
  243. throw new Exception("Installation (Step 1 of 2) Problems! Please check output above for errors.");
  244. }
  245. $success = tripal_core_install_sql($init_file);
  246. if ($success) {
  247. print "Install of Chado v1.11 (Step 2 of 2) Successful.\nInstallation Complete!\n";
  248. }
  249. else {
  250. throw new Exception("Installation (Step 2 of 2) Problems! Please check output above for errors.");
  251. }
  252. }
  253. else {
  254. throw new Exception("ERROR: cannot install chado. Please check database permissions");
  255. }
  256. }
  257. /**
  258. * Upgrades Chado from v1.2 to v1.3
  259. */
  260. function tripal_core_upgrade_chado_1_2_to_1_3() {
  261. // Get the path to the diff schema and upgrade SQL files.
  262. $diff_file = drupal_get_path('module', 'tripal_core') .
  263. '/chado_schema/default_schema-1.2-1.3-diff.sql';
  264. $success = tripal_core_install_sql($diff_file);
  265. if ($success) {
  266. print "Upgrade from v1.2 to v1.3 Successful!\n";
  267. }
  268. else {
  269. throw new Exception("Upgrade problems! Please check output above for errors.");
  270. }
  271. }
  272. /**
  273. * Upgrades Chado from v1.11 to v1.2
  274. */
  275. function tripal_core_upgrade_chado_1_11_to_1_2() {
  276. // Get the path to the schema diff and upgarde SQL files.
  277. $schema_file = drupal_get_path('module', 'tripal_core') .
  278. '/chado_schema/default_schema-1.11-1.2-diff.sql';
  279. $init_file = drupal_get_path('module', 'tripal_core') .
  280. '/chado_schema/upgrade-1.11-1.2.sql';
  281. $success = tripal_core_install_sql($schema_file);
  282. if ($success) {
  283. print "Upgrade from v1.11 to v1.2 (Step 1 of 2) Successful!\n";
  284. }
  285. else {
  286. throw new Exception("Upgrade (Step 1 of 2) problems! Please check output above for errors.");
  287. }
  288. $success = tripal_core_install_sql($init_file);
  289. if ($success) {
  290. print "Upgrade from v1.11 to v1.2 (Step 2 of 2) Successful.\nUpgrade Complete!\n";
  291. }
  292. else {
  293. throw new Exception("Upgrade (Step 2 of 2) problems! Please check output above for errors.");
  294. }
  295. }
  296. /**
  297. * Reset the Chado Schema
  298. * This drops the current chado and chado-related schema and re-creates it
  299. *
  300. * @ingroup tripal_core
  301. */
  302. function tripal_core_reset_chado_schema() {
  303. // determine the schema name.
  304. $chado_schema = tripal_get_schema_name('chado');
  305. $chado_dot = $chado_schema . '.';
  306. // drop current chado and chado-related schema
  307. if (chado_dbschema_exists('genetic_code')) {
  308. print "Dropping existing 'genetic_code' schema\n";
  309. db_query("drop schema genetic_code cascade");
  310. }
  311. if (chado_dbschema_exists('so')) {
  312. print "Dropping existing 'so' schema\n";
  313. db_query("drop schema so cascade");
  314. }
  315. if (chado_dbschema_exists('frange')) {
  316. print "Dropping existing 'frange' schema\n";
  317. db_query("drop schema frange cascade");
  318. }
  319. if (chado_dbschema_exists('chado')) {
  320. print "Dropping existing 'chado' schema\n";
  321. db_query("drop schema $chado_schema cascade");
  322. }
  323. // create the new chado schema
  324. print "Creating 'chado' schema\n";
  325. db_query("create schema $chado_schema");
  326. if (chado_dbschema_exists('chado')) {
  327. // before creating the plpgsql language let's check to make sure
  328. // it doesn't already exists
  329. $sql = "SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'";
  330. $results = db_query($sql);
  331. $count = $results->fetchObject();
  332. if (!$count or $count->count == 0) {
  333. db_query("create language plpgsql");
  334. }
  335. return TRUE;
  336. }
  337. return FALSE;
  338. }
  339. /**
  340. * Execute the provided SQL
  341. *
  342. * @param $sql_file
  343. * Contains SQL statements to be executed
  344. *
  345. * @ingroup tripal_core
  346. */
  347. function tripal_core_install_sql($sql_file) {
  348. $chado_local = chado_dbschema_exists('chado');
  349. // determine the schema name.
  350. $chado_schema = tripal_get_schema_name('chado');
  351. $chado_dot = $chado_schema . '.';
  352. if ($chado_local) {
  353. db_query("set search_path to $chado_schema");
  354. }
  355. print "Loading $sql_file...\n";
  356. $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
  357. if (!$lines) {
  358. return 'Cannot open $schema_file';
  359. }
  360. $stack = array();
  361. $in_string = 0;
  362. $in_function = FALSE;
  363. $query = '';
  364. $i = 0;
  365. $success = 1;
  366. foreach ($lines as $line_num => $line) {
  367. $i++;
  368. $type = '';
  369. // find and remove comments except when inside of strings
  370. if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) {
  371. $line = preg_replace('/--.*$/', '', $line); // remove comments
  372. }
  373. if (preg_match('/\/\*.*?\*\//', $line)) {
  374. $line = preg_replace('/\/\*.*?\*\//', '', $line); // remove comments
  375. }
  376. // skip empty lines
  377. if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) {
  378. continue;
  379. }
  380. // Find SQL for new objects
  381. if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string and !$in_function) {
  382. $stack[] = 'table';
  383. $line = preg_replace("/public\./", $chado_dot, $line);
  384. }
  385. if (preg_match('/^\s*ALTER\s+TABLE\s+/i', $line) and !$in_string and !$in_function) {
  386. $stack[] = 'alter_table';
  387. $line = preg_replace("/public\./", $chado_dot, $line);
  388. }
  389. if (preg_match('/^\s*SET/i', $line) and !$in_string and !$in_function) {
  390. $stack[] = 'set';
  391. }
  392. if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string and !$in_function) {
  393. $stack[] = 'schema';
  394. }
  395. if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string and !$in_function) {
  396. $stack[] = 'sequence';
  397. $line = preg_replace("/public\./", $chado_dot, $line);
  398. }
  399. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string and !$in_function) {
  400. $stack[] = 'view';
  401. $line = preg_replace("/public\./", $chado_dot, $line);
  402. }
  403. if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0 and !$in_function) {
  404. $stack[] = 'comment';
  405. $line = preg_replace("/public\./", $chado_dot, $line);
  406. }
  407. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string and !$in_function) {
  408. $in_function = TRUE;
  409. $stack[] = 'function';
  410. $line = preg_replace("/public\./", $chado_dot, $line);
  411. }
  412. if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string and !$in_function) {
  413. $stack[] = 'index';
  414. }
  415. if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string and !$in_function) {
  416. $stack[] = 'insert';
  417. $line = preg_replace("/public\./", $chado_dot, $line);
  418. }
  419. if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string and !$in_function) {
  420. $stack[] = 'type';
  421. }
  422. if (preg_match('/^\s*GRANT/i', $line) and !$in_string and !$in_function) {
  423. $stack[] = 'grant';
  424. }
  425. if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string and !$in_function) {
  426. $stack[] = 'aggregate';
  427. }
  428. if (preg_match('/^\s*DROP\s+FUNCTION/i', $line) and !$in_string and !$in_function) {
  429. $stack[] = 'drop_function';
  430. }
  431. if (preg_match('/^\s*DROP\s+VIEW/i', $line) and !$in_string and !$in_function) {
  432. $stack[] = 'drop_view';
  433. }
  434. if (preg_match('/^\s*DROP\s+INDEX/i', $line) and !$in_string and !$in_function) {
  435. $stack[] = 'drop_index';
  436. }
  437. if (preg_match('/^\s*DROP\s+SEQUENCE/i', $line) and !$in_string and !$in_function) {
  438. $stack[] = 'drop_seq';
  439. }
  440. if (preg_match('/^\s*ALTER\s+TYPE\s+/i', $line) and !$in_string and !$in_function) {
  441. $stack[] = 'alter_type';
  442. }
  443. if (preg_match('/^\s*ALTER\s+SEQUENCE\s+/i', $line) and !$in_string and !$in_function) {
  444. $stack[] = 'alter_seq';
  445. }
  446. // determine if we are in a string that spans a line
  447. $matches = preg_match_all("/[']/i", $line, $temp);
  448. $in_string = $in_string - ($matches % 2);
  449. $in_string = abs($in_string);
  450. // if we've reached the end of an object then pop the stack
  451. if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
  452. $type = array_pop($stack);
  453. }
  454. elseif (strcmp($stack[sizeof($stack)-1], 'alter_table') == 0 and preg_match('/;\s*$/', $line)) {
  455. $type = array_pop($stack);
  456. }
  457. elseif (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  458. $type = array_pop($stack);
  459. }
  460. elseif (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  461. $type = array_pop($stack);
  462. }
  463. elseif (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  464. $type = array_pop($stack);
  465. }
  466. elseif (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  467. $type = array_pop($stack);
  468. }
  469. elseif (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  470. $type = array_pop($stack);
  471. }
  472. elseif (strcmp($stack[sizeof($stack)-1], 'function') == 0) {
  473. if(preg_match('/LANGUAGE.*?;\s*$/i', $line)) {
  474. $type = array_pop($stack);
  475. $in_function = FALSE;
  476. //print "FUNCTION DONE ($i): $line";
  477. }
  478. else if(preg_match('/\$_\$;\s*$/i', $line)) {
  479. $type = array_pop($stack);
  480. $in_function = FALSE;
  481. //print "FUNCTION DONE ($i): $line";
  482. }
  483. else if(preg_match('/\$\$;\s*$/i', $line)) {
  484. $type = array_pop($stack);
  485. $in_function = FALSE;
  486. // print "FUNCTION DONE ($i): $line";
  487. }
  488. else {
  489. // print "FUNCTION ($i): $line";
  490. }
  491. }
  492. elseif (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  493. $type = array_pop($stack);
  494. }
  495. elseif (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
  496. $type = array_pop($stack);
  497. }
  498. elseif (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
  499. $type = array_pop($stack);
  500. }
  501. elseif (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  502. $type = array_pop($stack);
  503. }
  504. elseif (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
  505. $type = array_pop($stack);
  506. }
  507. elseif (strcmp($stack[sizeof($stack)-1], 'drop_function') == 0 and preg_match('/;\s*$/i', $line)) {
  508. $type = array_pop($stack);
  509. }
  510. elseif (strcmp($stack[sizeof($stack)-1], 'drop_view') == 0 and preg_match('/;\s*$/i', $line)) {
  511. $type = array_pop($stack);
  512. }
  513. elseif (strcmp($stack[sizeof($stack)-1], 'drop_index') == 0 and preg_match("/;\s*$/i", $line)) {
  514. $type = array_pop($stack);
  515. }
  516. elseif (strcmp($stack[sizeof($stack)-1], 'drop_seq') == 0 and preg_match("/;\s*$/i", $line)) {
  517. $type = array_pop($stack);
  518. }
  519. elseif (strcmp($stack[sizeof($stack)-1], 'alter_type') == 0 and preg_match('/;\s*$/i', $line)) {
  520. $type = array_pop($stack);
  521. }
  522. elseif (strcmp($stack[sizeof($stack)-1], 'alter_seq') == 0 and preg_match('/;\s*$/i', $line)) {
  523. $type = array_pop($stack);
  524. }
  525. // if we're in a recognized SQL statement then let's keep track of lines
  526. if ($type or sizeof($stack) > 0) {
  527. $query .= "$line";
  528. }
  529. else {
  530. throw new Exception("UNHANDLED $i, $in_string: $line");
  531. }
  532. if (preg_match_all("/\n/", $query, $temp) > 1000) {
  533. throw new Exception("SQL query is too long. Terminating:\n$query\n");
  534. }
  535. if ($type and sizeof($stack) == 0) {
  536. //print "Adding $type: line $i\n";
  537. // rewrite the set search_path to make 'public' be 'chado', but only if the
  538. // chado schema exists
  539. if (strcmp($type, 'set') == 0 and $chado_local) {
  540. $query = preg_replace("/public/m", $chado_schema, $query);
  541. }
  542. // execute the statement
  543. try {
  544. $result = db_query($query);
  545. }
  546. catch (Exception $e) {
  547. $error = $e->getMessage();
  548. throw new Exception("FAILED. Line $i, $in_string\n$error:\n$query\n\n");
  549. }
  550. if (!$result) {
  551. $error = pg_last_error();
  552. throw new Exception("FAILED. Line $i, $in_string\n$error:\n$query\n\n");
  553. }
  554. $query = '';
  555. }
  556. }
  557. tripal_core_chado_install_done();
  558. return $success;
  559. }
  560. /**
  561. * Finish the Chado Schema Installation
  562. *
  563. * @ingroup tripal_core
  564. */
  565. function tripal_core_chado_install_done() {
  566. $drupal_schema = tripal_get_schema_name('drupal');
  567. db_query("set search_path to $drupal_schema");
  568. }