tripal_chado.install.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. /**
  3. * @file
  4. * Functions to install chado schema through Drupal
  5. */
  6. /**
  7. * Load Chado Schema Form
  8. *
  9. * @ingroup tripal_chado
  10. */
  11. function tripal_chado_install_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_chado_install_form_validate($form, &$form_state) {
  63. if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.11 to v1.2") {
  64. // Make sure we are already not at v1.2
  65. $real_version = chado_get_version(TRUE);
  66. if ($real_version == "1.2") {
  67. form_set_error("action_to_do", "You are already at v1.2. There is no need to upgrade.");
  68. }
  69. }
  70. if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.2 to v1.3") {
  71. // Make sure we are already not at v1.2
  72. $real_version = chado_get_version(TRUE);
  73. if ($real_version == "1.3") {
  74. form_set_error("action_to_do", "You are already at v1.3. There is no need to upgrade.");
  75. }
  76. }
  77. }
  78. /**
  79. * Submit Load Chado Schema Form
  80. *
  81. * @ingroup tripal_chado
  82. */
  83. function tripal_chado_install_form_submit($form, &$form_state) {
  84. global $user;
  85. $action_to_do = trim($form_state['values']['action_to_do']);
  86. $args = array($action_to_do);
  87. $includes = array(module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.install'));
  88. tripal_add_job($action_to_do, 'tripal_chado',
  89. 'tripal_chado_install_chado', $args, $user->uid, 10, $includes);
  90. }
  91. /**
  92. * Install Chado Schema
  93. *
  94. * @ingroup tripal_chado
  95. */
  96. function tripal_chado_install_chado($action) {
  97. $vsql = "
  98. INSERT INTO {chadoprop} (type_id, value)
  99. VALUES (
  100. (SELECT cvterm_id
  101. FROM {cvterm} CVT
  102. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  103. WHERE CV.name = 'chado_properties' AND CVT.name = 'version'),
  104. :version)
  105. ";
  106. $vusql = "
  107. UPDATE {chadoprop}
  108. SET value = :version
  109. WHERE type_id = (SELECT cvterm_id
  110. FROM {cvterm} CVT
  111. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  112. WHERE CV.name = 'chado_properties' AND CVT.name = 'version')
  113. ";
  114. $transaction = db_transaction();
  115. try {
  116. if ($action == 'Install Chado v1.3') {
  117. tripal_chado_install_chado_1_3();
  118. chado_query($vsql, array(':version' => '1.3'));
  119. }
  120. elseif ($action == 'Upgrade Chado v1.2 to v1.3') {
  121. tripal_chado_upgrade_chado_1_2_to_1_3();
  122. chado_query($vusql, array(':version' => '1.3'));
  123. }
  124. elseif ($action == 'Install Chado v1.2') {
  125. tripal_chado_install_chado_1_2();
  126. chado_query($vsql, array(':version' => '1.2'));
  127. }
  128. elseif ($action == 'Upgrade Chado v1.11 to v1.2') {
  129. tripal_chado_upgrade_chado_1_11_to_1_2();
  130. chado_query($vsql, array(':version' => '1.2'));
  131. }
  132. elseif ($action == 'Install Chado v1.11') {
  133. tripal_chado_install_chado_1_11();
  134. }
  135. }
  136. catch (Exception $e) {
  137. $transaction->rollback();
  138. tripal_chado_install_done();
  139. tripal_report_error('tripal_chado', TRIPAL_ERROR, $e->getMessage(), array('print' => TRUE));
  140. return FALSE;
  141. }
  142. return TRUE;
  143. // Set a variable to indicate the site is prepared.
  144. variable_set('tripal_chado_is_prepared', FALSE);
  145. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.setup');
  146. tripal_chado_set_globals();
  147. tripal_chado_init();
  148. tripal_chado_prepare_chado();
  149. return TRUE;
  150. }
  151. /**
  152. * Installs Chado v1.3.
  153. */
  154. function tripal_chado_install_chado_1_3() {
  155. // Get the path to the schema and init SQL files.
  156. $schema_file = drupal_get_path('module', 'tripal_chado') .
  157. '/chado_schema/default_schema-1.3.sql';
  158. $init_file = drupal_get_path('module', 'tripal_chado') .
  159. '/chado_schema/initialize-1.3.sql';
  160. // Erase the Chado schema if it exists and perform the install.
  161. if (tripal_chado_reset_chado_schema()) {
  162. $success = tripal_chado_install_sql($schema_file);
  163. if ($success) {
  164. print "Install of Chado v1.3 (Step 1 of 2) Successful!\n";
  165. }
  166. else {
  167. throw new Exception("Installation (Step 1 of 2) Problems! Please check output above for errors.");
  168. }
  169. $success = tripal_chado_install_sql($init_file);
  170. if ($success) {
  171. print "Install of Chado v1.3 (Step 2 of 2) Successful.\nInstallation Complete\n";
  172. }
  173. else {
  174. throw new Exception("Installation (Step 2 of 2) Problems! Please check output above for errors.");
  175. }
  176. }
  177. else {
  178. throw new Exception("ERROR: cannot install chado. Please check database permissions");
  179. }
  180. }
  181. /**
  182. * Installs Chado v1.2.
  183. */
  184. function tripal_chado_install_chado_1_2() {
  185. // Get the path to the schema and init SQL files.
  186. $schema_file = drupal_get_path('module', 'tripal_chado') .
  187. '/chado_schema/default_schema-1.2.sql';
  188. $init_file = drupal_get_path('module', 'tripal_chado') .
  189. '/chado_schema/initialize-1.2.sql';
  190. // Erase the Chado schema if it exists and perform the install.
  191. if (tripal_chado_reset_chado_schema()) {
  192. $success = tripal_chado_install_sql($schema_file);
  193. if ($success) {
  194. print "Install of Chado v1.2 (Step 1 of 2) Successful!\n";
  195. }
  196. else {
  197. throw new Exception("Installation (Step 1 of 2) Problems! Please check output above for errors.");
  198. }
  199. $success = tripal_chado_install_sql($init_file);
  200. if ($success) {
  201. print "Install of Chado v1.2 (Step 2 of 2) Successful.\nInstallation Complete\n";
  202. }
  203. else {
  204. throw new Exception("Installation (Step 2 of 2) Problems! Please check output above for errors.");
  205. }
  206. }
  207. else {
  208. throw new Exception("ERROR: cannot install chado. Please check database permissions");
  209. }
  210. }
  211. /**
  212. *
  213. */
  214. function tripal_chado_install_chado_1_11() {
  215. // Get the path to the schema and init SQL files.
  216. $schema_file = drupal_get_path('module', 'tripal_chado') .
  217. '/chado_schema/default_schema-1.11.sql';
  218. $init_file = drupal_get_path('module', 'tripal_chado') .
  219. '/chado_schema/initialize-1.11.sql';
  220. // Erase the Chado schema if it exists and perform the install.
  221. if (tripal_chado_reset_chado_schema()) {
  222. $success = tripal_chado_install_sql($schema_file);
  223. if ($success) {
  224. print "Install of Chado v1.11 (Step 1 of 2) Successful!\n";
  225. }
  226. else {
  227. throw new Exception("Installation (Step 1 of 2) Problems! Please check output above for errors.");
  228. }
  229. $success = tripal_chado_install_sql($init_file);
  230. if ($success) {
  231. print "Install of Chado v1.11 (Step 2 of 2) Successful.\nInstallation Complete!\n";
  232. }
  233. else {
  234. throw new Exception("Installation (Step 2 of 2) Problems! Please check output above for errors.");
  235. }
  236. }
  237. else {
  238. throw new Exception("ERROR: cannot install chado. Please check database permissions");
  239. }
  240. }
  241. /**
  242. * Upgrades Chado from v1.2 to v1.3
  243. */
  244. function tripal_chado_upgrade_chado_1_2_to_1_3() {
  245. // Get the path to the diff schema and upgrade SQL files.
  246. $diff_file = drupal_get_path('module', 'tripal_chado') .
  247. '/chado_schema/default_schema-1.2-1.3-diff.sql';
  248. $success = tripal_chado_install_sql($diff_file);
  249. if ($success) {
  250. print "Upgrade from v1.2 to v1.3 Successful!\n";
  251. }
  252. else {
  253. throw new Exception("Upgrade problems! Please check output above for errors.");
  254. }
  255. }
  256. /**
  257. * Upgrades Chado from v1.11 to v1.2
  258. */
  259. function tripal_chado_upgrade_chado_1_11_to_1_2() {
  260. // Get the path to the schema diff and upgarde SQL files.
  261. $schema_file = drupal_get_path('module', 'tripal_chado') .
  262. '/chado_schema/default_schema-1.11-1.2-diff.sql';
  263. $init_file = drupal_get_path('module', 'tripal_chado') .
  264. '/chado_schema/upgrade-1.11-1.2.sql';
  265. $success = tripal_chado_install_sql($schema_file);
  266. if ($success) {
  267. print "Upgrade from v1.11 to v1.2 (Step 1 of 2) Successful!\n";
  268. }
  269. else {
  270. throw new Exception("Upgrade (Step 1 of 2) problems! Please check output above for errors.");
  271. }
  272. $success = tripal_chado_install_sql($init_file);
  273. if ($success) {
  274. print "Upgrade from v1.11 to v1.2 (Step 2 of 2) Successful.\nUpgrade Complete!\n";
  275. }
  276. else {
  277. throw new Exception("Upgrade (Step 2 of 2) problems! Please check output above for errors.");
  278. }
  279. }
  280. /**
  281. * Reset the Chado Schema
  282. * This drops the current chado and chado-related schema and re-creates it
  283. *
  284. * @ingroup tripal_chado
  285. */
  286. function tripal_chado_reset_chado_schema() {
  287. // drop current chado and chado-related schema
  288. if (chado_dbschema_exists('genetic_code')) {
  289. print "Dropping existing 'genetic_code' schema\n";
  290. db_query("drop schema genetic_code cascade");
  291. }
  292. if (chado_dbschema_exists('so')) {
  293. print "Dropping existing 'so' schema\n";
  294. db_query("drop schema so cascade");
  295. }
  296. if (chado_dbschema_exists('frange')) {
  297. print "Dropping existing 'frange' schema\n";
  298. db_query("drop schema frange cascade");
  299. }
  300. if (chado_dbschema_exists('chado')) {
  301. print "Dropping existing 'chado' schema\n";
  302. db_query("drop schema chado cascade");
  303. }
  304. // create the new chado schema
  305. print "Creating 'chado' schema\n";
  306. db_query("create schema chado");
  307. if (chado_dbschema_exists('chado')) {
  308. // before creating the plpgsql language let's check to make sure
  309. // it doesn't already exists
  310. $sql = "SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'";
  311. $results = db_query($sql);
  312. $count = $results->fetchObject();
  313. if (!$count or $count->count == 0) {
  314. db_query("create language plpgsql");
  315. }
  316. return TRUE;
  317. }
  318. return FALSE;
  319. }
  320. /**
  321. * Execute the provided SQL
  322. *
  323. * @param $sql_file
  324. * Contains SQL statements to be executed
  325. *
  326. * @ingroup tripal_chado
  327. */
  328. function tripal_chado_install_sql($sql_file) {
  329. $chado_local = chado_dbschema_exists('chado');
  330. if ($chado_local) {
  331. db_query("set search_path to chado");
  332. }
  333. print "Loading $sql_file...\n";
  334. $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
  335. if (!$lines) {
  336. return 'Cannot open $schema_file';
  337. }
  338. $stack = array();
  339. $in_string = 0;
  340. $in_function = FALSE;
  341. $query = '';
  342. $i = 0;
  343. $success = 1;
  344. foreach ($lines as $line_num => $line) {
  345. $i++;
  346. $type = '';
  347. // find and remove comments except when inside of strings
  348. if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) {
  349. $line = preg_replace('/--.*$/', '', $line); // remove comments
  350. }
  351. if (preg_match('/\/\*.*?\*\//', $line)) {
  352. $line = preg_replace('/\/\*.*?\*\//', '', $line); // remove comments
  353. }
  354. // skip empty lines
  355. if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) {
  356. continue;
  357. }
  358. // Find SQL for new objects
  359. if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string and !$in_function) {
  360. $stack[] = 'table';
  361. $line = preg_replace("/public\./", "chado.", $line);
  362. }
  363. if (preg_match('/^\s*ALTER\s+TABLE\s+/i', $line) and !$in_string and !$in_function) {
  364. $stack[] = 'alter_table';
  365. $line = preg_replace("/public\./", "chado.", $line);
  366. }
  367. if (preg_match('/^\s*SET/i', $line) and !$in_string and !$in_function) {
  368. $stack[] = 'set';
  369. }
  370. if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string and !$in_function) {
  371. $stack[] = 'schema';
  372. }
  373. if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string and !$in_function) {
  374. $stack[] = 'sequence';
  375. $line = preg_replace("/public\./", "chado.", $line);
  376. }
  377. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string and !$in_function) {
  378. $stack[] = 'view';
  379. $line = preg_replace("/public\./", "chado.", $line);
  380. }
  381. if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0 and !$in_function) {
  382. $stack[] = 'comment';
  383. $line = preg_replace("/public\./", "chado.", $line);
  384. }
  385. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string and !$in_function) {
  386. $in_function = TRUE;
  387. $stack[] = 'function';
  388. $line = preg_replace("/public\./", "chado.", $line);
  389. }
  390. if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string and !$in_function) {
  391. $stack[] = 'index';
  392. }
  393. if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string and !$in_function) {
  394. $stack[] = 'insert';
  395. $line = preg_replace("/public\./", "chado.", $line);
  396. }
  397. if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string and !$in_function) {
  398. $stack[] = 'type';
  399. }
  400. if (preg_match('/^\s*GRANT/i', $line) and !$in_string and !$in_function) {
  401. $stack[] = 'grant';
  402. }
  403. if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string and !$in_function) {
  404. $stack[] = 'aggregate';
  405. }
  406. if (preg_match('/^\s*DROP\s+FUNCTION/i', $line) and !$in_string and !$in_function) {
  407. $stack[] = 'drop_function';
  408. }
  409. if (preg_match('/^\s*DROP\s+VIEW/i', $line) and !$in_string and !$in_function) {
  410. $stack[] = 'drop_view';
  411. }
  412. if (preg_match('/^\s*DROP\s+INDEX/i', $line) and !$in_string and !$in_function) {
  413. $stack[] = 'drop_index';
  414. }
  415. if (preg_match('/^\s*DROP\s+SEQUENCE/i', $line) and !$in_string and !$in_function) {
  416. $stack[] = 'drop_seq';
  417. }
  418. if (preg_match('/^\s*ALTER\s+TYPE\s+/i', $line) and !$in_string and !$in_function) {
  419. $stack[] = 'alter_type';
  420. }
  421. if (preg_match('/^\s*ALTER\s+SEQUENCE\s+/i', $line) and !$in_string and !$in_function) {
  422. $stack[] = 'alter_seq';
  423. }
  424. // determine if we are in a string that spans a line
  425. $matches = preg_match_all("/[']/i", $line, $temp);
  426. $in_string = $in_string - ($matches % 2);
  427. $in_string = abs($in_string);
  428. // if we've reached the end of an object then pop the stack
  429. if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
  430. $type = array_pop($stack);
  431. }
  432. if (strcmp($stack[sizeof($stack)-1], 'alter_table') == 0 and preg_match('/;\s*$/', $line)) {
  433. $type = array_pop($stack);
  434. }
  435. if (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  436. $type = array_pop($stack);
  437. }
  438. if (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  439. $type = array_pop($stack);
  440. }
  441. if (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  442. $type = array_pop($stack);
  443. }
  444. if (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  445. $type = array_pop($stack);
  446. }
  447. if (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  448. $type = array_pop($stack);
  449. }
  450. if (strcmp($stack[sizeof($stack)-1], 'function') == 0) {
  451. if(preg_match('/LANGUAGE.*?;\s*$/i', $line)) {
  452. $type = array_pop($stack);
  453. $in_function = FALSE;
  454. //print "FUNCTION DONE ($i): $line";
  455. }
  456. else if(preg_match('/\$_\$;\s*$/i', $line)) {
  457. $type = array_pop($stack);
  458. $in_function = FALSE;
  459. //print "FUNCTION DONE ($i): $line";
  460. }
  461. else if(preg_match('/\$\$;\s*$/i', $line)) {
  462. $type = array_pop($stack);
  463. $in_function = FALSE;
  464. // print "FUNCTION DONE ($i): $line";
  465. }
  466. else {
  467. // print "FUNCTION ($i): $line";
  468. }
  469. }
  470. if (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  471. $type = array_pop($stack);
  472. }
  473. if (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
  474. $type = array_pop($stack);
  475. }
  476. if (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
  477. $type = array_pop($stack);
  478. }
  479. if (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  480. $type = array_pop($stack);
  481. }
  482. if (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
  483. $type = array_pop($stack);
  484. }
  485. if (strcmp($stack[sizeof($stack)-1], 'drop_function') == 0 and preg_match('/;\s*$/i', $line)) {
  486. $type = array_pop($stack);
  487. }
  488. if (strcmp($stack[sizeof($stack)-1], 'drop_view') == 0 and preg_match('/;\s*$/i', $line)) {
  489. $type = array_pop($stack);
  490. }
  491. if (strcmp($stack[sizeof($stack)-1], 'drop_index') == 0 and preg_match("/;\s*$/i", $line)) {
  492. $type = array_pop($stack);
  493. }
  494. if (strcmp($stack[sizeof($stack)-1], 'drop_seq') == 0 and preg_match("/;\s*$/i", $line)) {
  495. $type = array_pop($stack);
  496. }
  497. if (strcmp($stack[sizeof($stack)-1], 'alter_type') == 0 and preg_match('/;\s*$/i', $line)) {
  498. $type = array_pop($stack);
  499. }
  500. if (strcmp($stack[sizeof($stack)-1], 'alter_seq') == 0 and preg_match('/;\s*$/i', $line)) {
  501. $type = array_pop($stack);
  502. }
  503. // if we're in a recognized SQL statement then let's keep track of lines
  504. if ($type or sizeof($stack) > 0) {
  505. $query .= "$line";
  506. }
  507. else {
  508. throw new Exception("UNHANDLED $i, $in_string: $line");
  509. }
  510. if (preg_match_all("/\n/", $query, $temp) > 1000) {
  511. throw new Exception("SQL query is too long. Terminating:\n$query\n");
  512. }
  513. if ($type and sizeof($stack) == 0) {
  514. //print "Adding $type: line $i\n";
  515. // rewrite the set search_path to make 'public' be 'chado', but only if the
  516. // chado schema exists
  517. if (strcmp($type, 'set') == 0 and $chado_local) {
  518. $query = preg_replace("/public/m", "chado", $query);
  519. }
  520. // execute the statement
  521. try {
  522. $result = db_query($query);
  523. }
  524. catch (Exception $e) {
  525. $error = $e->getMessage();
  526. throw new Exception("FAILED. Line $i, $in_string\n$error:\n$query\n\n");
  527. }
  528. if (!$result) {
  529. $error = pg_last_error();
  530. throw new Exception("FAILED. Line $i, $in_string\n$error:\n$query\n\n");
  531. }
  532. $query = '';
  533. }
  534. }
  535. tripal_chado_install_done();
  536. return $success;
  537. }
  538. /**
  539. * Finish the Chado Schema Installation
  540. *
  541. * @ingroup tripal_chado
  542. */
  543. function tripal_chado_install_done() {
  544. db_query("set search_path to default");
  545. }