tripal_chado.install.inc 20 KB

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