tripal_core.chado_install.inc 21 KB

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