PageRenderTime 43ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.0.0/app/src/Lib/Data/DB/adodb_lite/adodbSQL_drivers/gladius/gladius_meta_module.inc

http://scalr.googlecode.com/
PHP | 376 lines | 225 code | 55 blank | 96 comment | 47 complexity | 1afbcbd4db441a282c7a5815b43b263e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * ADOdb Lite Meta Module for Gladius
  4. *
  5. * Portions of the Meta Coding came from ADOdb
  6. */
  7. /*
  8. (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.
  9. Released under both BSD license and Lesser GPL library license.
  10. Whenever there is any discrepancy between the two licenses,
  11. the BSD license will take precedence. See License.txt.
  12. */
  13. eval('class gladius_meta_EXTENDER extends '. $last_module . '_ADOConnection { }');
  14. class gladius_meta_ADOConnection extends gladius_meta_EXTENDER
  15. {
  16. var $metaTablesSQL = "SHOW TABLES";
  17. var $metaColumnsSQL = "SHOW COLUMNS FROM %s";
  18. function MetaError($err=false)
  19. {
  20. include_once(ADODB_DIR."/adodb-error.inc.php");
  21. if ($err === false)
  22. $err = $this->ErrorNo();
  23. return adodb_error($this->dataProvider,$this->databaseType,$err);
  24. }
  25. function MetaErrorMsg($errno)
  26. {
  27. include_once(ADODB_DIR."/adodb-error.inc.php");
  28. return adodb_errormsg($errno);
  29. }
  30. /**
  31. * @returns an array with the primary key columns in it.
  32. */
  33. function MetaPrimaryKeys($table, $owner=false)
  34. {
  35. // owner not used in base class - see oci8
  36. $p = array();
  37. $objs =& $this->MetaColumns($table);
  38. if ($objs) {
  39. foreach($objs as $v) {
  40. if (!empty($v->primary_key))
  41. $p[] = $v->name;
  42. }
  43. }
  44. if (sizeof($p)) return $p;
  45. if (function_exists('ADODB_VIEW_PRIMARYKEYS'))
  46. return ADODB_VIEW_PRIMARYKEYS($this->databaseType, $this->database, $table, $owner);
  47. return false;
  48. }
  49. /**
  50. * @returns assoc array where keys are tables, and values are foreign keys
  51. */
  52. function MetaForeignKeys($table, $owner=false, $upper=false)
  53. {
  54. return false;
  55. }
  56. // not the fastest implementation - quick and dirty - jlim
  57. // for best performance, use the actual $rs->MetaType().
  58. function MetaType($t,$len=-1,$fieldobj=false)
  59. {
  60. if (empty($this->_metars)) {
  61. $rsclass = $this->last_module_name . "_ResultSet";
  62. $this->_metars =& new $rsclass(false,$this->fetchMode);
  63. }
  64. return $this->_metars->MetaType($t,$len,$fieldobj);
  65. }
  66. /**
  67. * return the databases that the driver can connect to.
  68. * Some databases will return an empty array.
  69. *
  70. * @return an array of database names.
  71. */
  72. function MetaDatabases()
  73. {
  74. global $ADODB_FETCH_MODE;
  75. if ($this->metaDatabasesSQL) {
  76. $save = $ADODB_FETCH_MODE;
  77. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  78. if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  79. $arr = $this->GetCol($this->metaDatabasesSQL);
  80. if (isset($savem)) $this->SetFetchMode($savem);
  81. $ADODB_FETCH_MODE = $save;
  82. return $arr;
  83. }
  84. return false;
  85. }
  86. /**
  87. * @param ttype can either be 'VIEW' or 'TABLE' or false.
  88. * If false, both views and tables are returned.
  89. * "VIEW" returns only views
  90. * "TABLE" returns only tables
  91. * @param showSchema returns the schema/user with the table name, eg. USER.TABLE
  92. * @param mask is the input mask - only supported by oci8 and postgresql
  93. *
  94. * @return array of tables for current database.
  95. */
  96. function &MetaTables($ttype=false,$showSchema=false,$mask=false)
  97. {
  98. global $ADODB_FETCH_MODE;
  99. $false = false;
  100. if ($mask) {
  101. return $false;
  102. }
  103. if ($this->metaTablesSQL) {
  104. $save = $ADODB_FETCH_MODE;
  105. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  106. if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  107. $rs = $this->Execute($this->metaTablesSQL);
  108. if (isset($savem)) $this->SetFetchMode($savem);
  109. $ADODB_FETCH_MODE = $save;
  110. if ($rs === false) return $false;
  111. $arr =& $rs->GetArray();
  112. $arr2 = array();
  113. if ($hast = ($ttype && isset($arr[0][1]))) {
  114. $showt = strncmp($ttype,'T',1);
  115. }
  116. for ($i=0; $i < sizeof($arr); $i++) {
  117. if ($hast) {
  118. if ($showt == 0) {
  119. if (strncmp($arr[$i][1],'T',1) == 0) $arr2[] = trim($arr[$i][0]);
  120. } else {
  121. if (strncmp($arr[$i][1],'V',1) == 0) $arr2[] = trim($arr[$i][0]);
  122. }
  123. } else
  124. $arr2[] = trim($arr[$i][0]);
  125. }
  126. $rs->Close();
  127. return $arr2;
  128. }
  129. return $false;
  130. }
  131. function _findschema(&$table,&$schema)
  132. {
  133. if (!$schema && ($at = strpos($table,'.')) !== false) {
  134. $schema = substr($table,0,$at);
  135. $table = substr($table,$at+1);
  136. }
  137. }
  138. /**
  139. * List columns in a database as an array of ADOFieldObjects.
  140. * See top of file for definition of object.
  141. *
  142. * @param $table table name to query
  143. * @param $normalize makes table name case-insensitive (required by some databases)
  144. * @schema is optional database schema to use - not supported by all databases.
  145. *
  146. * @return array of ADOFieldObjects for current table.
  147. */
  148. function &MetaColumns($table,$normalize=true)
  149. {
  150. global $ADODB_FETCH_MODE;
  151. $false = false;
  152. if (!empty($this->metaColumnsSQL)) {
  153. $schema = false;
  154. $this->_findschema($table,$schema);
  155. $save = $ADODB_FETCH_MODE;
  156. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  157. if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  158. $rs = $this->Execute(sprintf($this->metaColumnsSQL,($normalize)?strtoupper($table):$table));
  159. if (isset($savem)) $this->SetFetchMode($savem);
  160. $ADODB_FETCH_MODE = $save;
  161. if ($rs === false || $rs->EOF) return $false;
  162. $retarr = array();
  163. while (!$rs->EOF) { //print_r($rs->fields);
  164. $fld = new ADOFieldObject();
  165. $fld->name = $rs->fields[0];
  166. $fld->type = $rs->fields[1];
  167. if (isset($rs->fields[3]) && $rs->fields[3]) {
  168. if ($rs->fields[3]>0) $fld->max_length = $rs->fields[3];
  169. $fld->scale = $rs->fields[4];
  170. if ($fld->scale>0) $fld->max_length += 1;
  171. } else
  172. $fld->max_length = $rs->fields[2];
  173. if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;
  174. else $retarr[strtoupper($fld->name)] = $fld;
  175. $rs->MoveNext();
  176. }
  177. $rs->Close();
  178. return $retarr;
  179. }
  180. return $false;
  181. }
  182. /**
  183. * List indexes on a table as an array.
  184. * @param table table name to query
  185. * @param primary true to only show primary keys. Not actually used for most databases
  186. *
  187. * @return array of indexes on current table. Each element represents an index, and is itself an associative array.
  188. Array (
  189. [name_of_index] => Array
  190. (
  191. [unique] => true or false
  192. [columns] => Array
  193. (
  194. [0] => firstname
  195. [1] => lastname
  196. )
  197. )
  198. */
  199. function &MetaIndexes($table, $primary = false, $owner = false)
  200. {
  201. $false = false;
  202. return $false;
  203. }
  204. /**
  205. * List columns names in a table as an array.
  206. * @param table table name to query
  207. *
  208. * @return array of column names for current table.
  209. */
  210. function &MetaColumnNames($table, $numIndexes=false,$useattnum=false /* only for postgres */)
  211. {
  212. $objarr =& $this->MetaColumns($table);
  213. if (!is_array($objarr)) {
  214. $false = false;
  215. return $false;
  216. }
  217. $arr = array();
  218. if ($numIndexes) {
  219. $i = 0;
  220. if ($useattnum) {
  221. foreach($objarr as $v)
  222. $arr[$v->attnum] = $v->name;
  223. } else
  224. foreach($objarr as $v) $arr[$i++] = $v->name;
  225. } else
  226. foreach($objarr as $v) $arr[strtoupper($v->name)] = $v->name;
  227. return $arr;
  228. }
  229. function MetaTransaction($mode,$db)
  230. {
  231. $mode = strtoupper($mode);
  232. $mode = str_replace('ISOLATION LEVEL ','',$mode);
  233. switch($mode) {
  234. case 'READ UNCOMMITTED':
  235. switch($db) {
  236. case 'oci8':
  237. case 'oracle':
  238. return 'ISOLATION LEVEL READ COMMITTED';
  239. default:
  240. return 'ISOLATION LEVEL READ UNCOMMITTED';
  241. }
  242. break;
  243. case 'READ COMMITTED':
  244. return 'ISOLATION LEVEL READ COMMITTED';
  245. break;
  246. case 'REPEATABLE READ':
  247. switch($db) {
  248. case 'oci8':
  249. case 'oracle':
  250. return 'ISOLATION LEVEL SERIALIZABLE';
  251. default:
  252. return 'ISOLATION LEVEL REPEATABLE READ';
  253. }
  254. break;
  255. case 'SERIALIZABLE':
  256. return 'ISOLATION LEVEL SERIALIZABLE';
  257. break;
  258. default:
  259. return $mode;
  260. }
  261. }
  262. }
  263. eval('class gladius_meta_resultset_EXTENDER extends '. $last_module . '_ResultSet { }');
  264. class gladius_meta_ResultSet extends gladius_meta_resultset_EXTENDER
  265. {
  266. /**
  267. * Get the metatype of the column. This is used for formatting. This is because
  268. * many databases use different names for the same type, so we transform the original
  269. * type to our standardised version which uses 1 character codes:
  270. *
  271. * @param t is the type passed in. Normally is ADOFieldObject->type.
  272. * @param len is the maximum length of that field. This is because we treat character
  273. * fields bigger than a certain size as a 'B' (blob).
  274. * @param fieldobj is the field object returned by the database driver. Can hold
  275. * additional info (eg. primary_key for mysql).
  276. *
  277. * @return the general type of the data:
  278. * C for character < 250 chars
  279. * X for teXt (>= 250 chars)
  280. * B for Binary
  281. * N for numeric or floating point
  282. * D for date
  283. * T for timestamp
  284. * L for logical/Boolean
  285. * I for integer
  286. * R for autoincrement counter/integer
  287. *
  288. *
  289. */
  290. function MetaType($t,$len=-1,$fieldobj=false)
  291. {
  292. if (is_object($t))
  293. {
  294. $fieldobj = $t;
  295. $t = $fieldobj->type;
  296. $len = $fieldobj->max_length;
  297. }
  298. $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
  299. $len = -1; // max_length is not accurate
  300. switch (strtolower($t))
  301. {
  302. case 'varchar':
  303. return 'C';
  304. case 'text':
  305. return 'X';
  306. // 'B' binary not yet supported in Gladius
  307. case 'date': return 'D';
  308. case 'time': return 'T';
  309. case 'datetime':
  310. return 'DT';
  311. case 'timestamp': return 'TS';
  312. case 'double':
  313. return 'F';
  314. case 'int':
  315. return $is_serial ? 'R' : 'I';
  316. // default: return 'N';
  317. }
  318. }
  319. }
  320. ?>