PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/xmldb/classes/XMLDBStatement.class.php

https://bitbucket.org/systime/screening2
PHP | 423 lines | 252 code | 57 blank | 114 comment | 45 complexity | bb68478942e42790cf36db93c0cb586b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0, BSD-3-Clause, LGPL-2.0
  1. <?php // $Id: XMLDBStatement.class.php,v 1.7.2.1 2010/09/05 21:15:12 skodak Exp $
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.com //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
  11. // //
  12. // This program is free software; you can redistribute it and/or modify //
  13. // it under the terms of the GNU General Public License as published by //
  14. // the Free Software Foundation; either version 2 of the License, or //
  15. // (at your option) any later version. //
  16. // //
  17. // This program is distributed in the hope that it will be useful, //
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  20. // GNU General Public License for more details: //
  21. // //
  22. // http://www.gnu.org/copyleft/gpl.html //
  23. // //
  24. ///////////////////////////////////////////////////////////////////////////
  25. /// This class represent one XMLDB Statement
  26. /// (a group of SQL arbitrary sentences)
  27. /// (only INSERT is allowed for now)
  28. class XMLDBStatement extends XMLDBObject {
  29. var $table; // Table we are handling
  30. var $type; // XMLDB_STATEMENT_TYPE
  31. var $sentences; // Collection of sentences in the statement
  32. /**
  33. * Creates one new XMLDBStatement
  34. */
  35. function XMLDBStatement($name) {
  36. parent::XMLDBObject($name);
  37. $this->table = NULL;
  38. $this->type = XMLDB_STATEMENT_INCORRECT;
  39. $this->sentences = array();
  40. }
  41. /**
  42. * Get the statement table
  43. */
  44. function getTable() {
  45. return $this->table;
  46. }
  47. /**
  48. * Get the statement type
  49. */
  50. function getType() {
  51. return $this->type;
  52. }
  53. /**
  54. * Get the statement sentences
  55. */
  56. function &getSentences() {
  57. return $this->sentences;
  58. }
  59. /**
  60. * Set the statement table
  61. */
  62. function setTable($table) {
  63. $this->table = $table;
  64. }
  65. /**
  66. * Set the statement type
  67. */
  68. function setType($type) {
  69. $this->type = $type;
  70. }
  71. /**
  72. * Add one statement sentence
  73. */
  74. function addSentence($sentence) {
  75. $this->sentences[] = $sentence;
  76. }
  77. /**
  78. * Load data from XML to the index
  79. */
  80. function arr2XMLDBStatement($xmlarr) {
  81. $result = true;
  82. /// Debug the table
  83. /// traverse_xmlize($xmlarr); //Debug
  84. /// print_object ($GLOBALS['traverse_array']); //Debug
  85. /// $GLOBALS['traverse_array']=""; //Debug
  86. /// Process key attributes (table, type, comment, previous, next)
  87. if (isset($xmlarr['@']['TABLE'])) {
  88. $this->table = strtolower(trim($xmlarr['@']['TABLE']));
  89. } else {
  90. $this->errormsg = 'Missing TABLE attribute';
  91. $this->debug($this->errormsg);
  92. $result = false;
  93. }
  94. if (isset($xmlarr['@']['TYPE'])) {
  95. /// Check for valid type
  96. $type = $this->getXMLDBStatementType(trim($xmlarr['@']['TYPE']));
  97. if ($type) {
  98. $this->type = $type;
  99. } else {
  100. $this->errormsg = 'Invalid TYPE attribute';
  101. $this->debug($this->errormsg);
  102. $result = false;
  103. }
  104. } else {
  105. $this->errormsg = 'Missing TYPE attribute';
  106. $this->debug($this->errormsg);
  107. $result = false;
  108. }
  109. /// Look for sentences
  110. $sentencesarr = array();
  111. if (isset($xmlarr['#']['SENTENCES'])) {
  112. $sentences = $xmlarr['#']['SENTENCES'][0]['#']['SENTENCE'];
  113. if ($sentences) {
  114. foreach ($sentences as $sentence) {
  115. if (isset($sentence['@']['TEXT'])) {
  116. $sentencesarr[] = trim($sentence['@']['TEXT']);
  117. } else {
  118. $this->errormsg = 'Missing TEXT attribute in sentence';
  119. $this->debug($this->errormsg);
  120. $result = false;
  121. }
  122. }
  123. }
  124. }
  125. /// Finally, set the array of sentences
  126. $this->sentences = $sentencesarr;
  127. /// Now, perform some validations over sentences
  128. /// XMLDB_STATEMENT_INSERT checks
  129. if ($this->type == XMLDB_STATEMENT_INSERT) {
  130. /// Separate fields and values into two arrays
  131. if ($this->sentences) {
  132. foreach ($this->sentences as $sentence) {
  133. $fields = $this->getFieldsFromInsertSentence($sentence);
  134. $values = $this->getValuesFromInsertSentence($sentence);
  135. /// Check that we aren't inserting the id field
  136. if (in_array('id', $fields)) {
  137. $this->errormsg = 'Cannot insert the "id" field. It is an autonumeric column';
  138. $this->debug($this->errormsg);
  139. $result = false;
  140. }
  141. if ($result && count($fields) == 0) {
  142. $this->errormsg = 'Missing fields in sentence "' . $sentence . '"';
  143. $this->debug($this->errormsg);
  144. $result = false;
  145. }
  146. if ($result && count($values) == 0) {
  147. $this->errormsg = 'Missing values in sentence "' . $sentence . '"';
  148. $this->debug($this->errormsg);
  149. $result = false;
  150. }
  151. if ($result && count($fields) != count($values)) {
  152. $this->errormsg = 'Incorrect number of fields (' .implode(', ', $fields) . ') or values (' . implode(', ', $values) . ')';
  153. $this->debug($this->errormsg);
  154. $result = false;
  155. }
  156. }
  157. }
  158. } else {
  159. /// Sentences different from INSERT are not valid for now
  160. $this->errormsg = 'Only INSERT statements are supported';
  161. $this->debug($this->errormsg);
  162. $result = false;
  163. }
  164. if (isset($xmlarr['@']['COMMENT'])) {
  165. $this->comment = trim($xmlarr['@']['COMMENT']);
  166. }
  167. if (isset($xmlarr['@']['PREVIOUS'])) {
  168. $this->previous = trim($xmlarr['@']['PREVIOUS']);
  169. }
  170. if (isset($xmlarr['@']['NEXT'])) {
  171. $this->next = trim($xmlarr['@']['NEXT']);
  172. }
  173. /// Set some attributes
  174. if ($result) {
  175. $this->loaded = true;
  176. }
  177. $this->calculateHash();
  178. return $result;
  179. }
  180. /**
  181. * This function returns the correct XMLDB_STATEMENT_XXX value for the
  182. * string passed as argument
  183. */
  184. function getXMLDBStatementType($type) {
  185. $result = XMLDB_STATEMENT_INCORRECT;
  186. switch (strtolower($type)) {
  187. case 'insert':
  188. $result = XMLDB_STATEMENT_INSERT;
  189. break;
  190. case 'update':
  191. $result = XMLDB_STATEMENT_UPDATE;
  192. break;
  193. case 'delete':
  194. $result = XMLDB_STATEMENT_DELETE;
  195. break;
  196. case 'custom':
  197. $result = XMLDB_STATEMENT_CUSTOM;
  198. break;
  199. }
  200. /// Return the normalized XMLDB_STATEMENT
  201. return $result;
  202. }
  203. /**
  204. * This function returns the correct name value for the
  205. * XMLDB_STATEMENT_XXX passed as argument
  206. */
  207. function getXMLDBStatementName($type) {
  208. $result = '';
  209. switch (strtolower($type)) {
  210. case XMLDB_STATEMENT_INSERT:
  211. $result = 'insert';
  212. break;
  213. case XMLDB_STATEMENT_UPDATE:
  214. $result = 'update';
  215. break;
  216. case XMLDB_STATEMENT_DELETE:
  217. $result = 'delete';
  218. break;
  219. case XMLDB_STATEMENT_CUSTOM:
  220. $result = 'custom';
  221. break;
  222. }
  223. /// Return the normalized name
  224. return $result;
  225. }
  226. /**
  227. * This function calculate and set the hash of one XMLDBStatement
  228. */
  229. function calculateHash($recursive = false) {
  230. if (!$this->loaded) {
  231. $this->hash = NULL;
  232. } else {
  233. $key = $this->table . $this->type . implode (', ', $this->sentences);
  234. $this->hash = md5($key);
  235. }
  236. }
  237. /**
  238. * This function will output the XML text for one statement
  239. */
  240. function xmlOutput() {
  241. $o = '';
  242. $o.= ' <STATEMENT NAME="' . $this->name . '" TYPE="' . XMLDBStatement::getXMLDBStatementName($this->type) . '" TABLE="' . $this->table . '"';
  243. if ($this->comment) {
  244. $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"';
  245. }
  246. if ($this->previous) {
  247. $o.= ' PREVIOUS="' . $this->previous . '"';
  248. }
  249. if ($this->next) {
  250. $o.= ' NEXT="' . $this->next . '"';
  251. }
  252. if ($this->sentences) {
  253. $o.= '>' . "\n";
  254. $o.= ' <SENTENCES>' . "\n";
  255. foreach ($this->sentences as $sentence) {
  256. $o.= ' <SENTENCE TEXT="' . htmlspecialchars($sentence) . '" />' . "\n";
  257. }
  258. $o.= ' </SENTENCES>' . "\n";
  259. $o.= ' </STATEMENT>' . "\n";
  260. } else {
  261. $o.= '/>' . "\n";
  262. }
  263. return $o;
  264. }
  265. /**
  266. * This function will set all the attributes of the XMLDBIndex object
  267. * based on information passed in one ADOindex
  268. */
  269. function setFromADOIndex($adoindex) {
  270. /// Set the unique field
  271. $this->unique = false;
  272. /// Set the fields
  273. $this->fields = $adoindex['columns'];
  274. /// Some more fields
  275. $this->loaded = true;
  276. $this->changed = true;
  277. }
  278. /**
  279. * Shows info in a readable format
  280. */
  281. function readableInfo() {
  282. $o = '';
  283. /// unique
  284. if ($this->unique) {
  285. $o .= 'unique';
  286. } else {
  287. $o .= 'not unique';
  288. }
  289. /// fields
  290. $o .= ' (' . implode(', ', $this->fields) . ')';
  291. return $o;
  292. }
  293. /**
  294. * This function will return an array of fields from one INSERT sentence
  295. */
  296. function getFieldsFromInsertSentence($sentence) {
  297. $fields = array();
  298. /// Get first part from the sentence (before VALUES)
  299. preg_match('/^\((.*)\)\s+VALUES/Us', $sentence, $matches);
  300. if (isset($matches[1])) {
  301. $part = $matches[1];
  302. /// Convert the comma separated string to an array
  303. $arr = $this->comma2array($part);
  304. if ($arr) {
  305. $fields = $arr;
  306. }
  307. }
  308. return $fields;
  309. }
  310. /**
  311. * This function will return an array of values from one INSERT sentence
  312. */
  313. function getValuesFromInsertSentence($sentence) {
  314. $values = array();
  315. /// Get second part from the sentence (after VALUES)
  316. preg_match('/VALUES\s*\((.*)\)$/is', $sentence, $matches);
  317. if (isset($matches[1])) {
  318. $part = $matches[1];
  319. /// Convert the comma separated string to an array
  320. $arr = $this->comma2array($part);
  321. if ($arr) {
  322. $values = $arr;
  323. }
  324. }
  325. return $values;
  326. }
  327. /**
  328. * This function will return the code needed to execute a collection
  329. * of sentences present inside one statement for the specified BD
  330. * and prefix.
  331. * For now it only supports INSERT statements
  332. */
  333. function getExecuteStatementSQL ($dbtype, $prefix, $statement_end=true) {
  334. $results = array();
  335. /// Based on statement type
  336. switch ($this->type) {
  337. case XMLDB_STATEMENT_INSERT:
  338. $results = $this->getExecuteInsertSQL($dbtype, $prefix, $statement_end);
  339. break;
  340. case XMLDB_STATEMENT_UPDATE:
  341. break;
  342. case XMLDB_STATEMENT_DELETE:
  343. break;
  344. case XMLDB_STATEMENT_CUSTOM:
  345. break;
  346. }
  347. return $results;
  348. }
  349. /**
  350. * This function will return the code needed to execute a collection
  351. * of insert sentences present inside the statement for the specified BD
  352. * and prefix. Just one simple wrapper over generators.
  353. */
  354. function getExecuteInsertSQL ($dbtype, $prefix, $statement_end=true) {
  355. $results = array();
  356. $classname = 'XMLDB' . $dbtype;
  357. $generator = new $classname();
  358. $generator->setPrefix($prefix);
  359. $results = $generator->getExecuteInsertSQL($this);
  360. if ($statement_end) {
  361. $results = $generator->getEndedStatements($results);
  362. }
  363. return $results;
  364. }
  365. }
  366. ?>