PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpxbase-2006-10-11/Record.class.php

https://github.com/plank/Seed-Framework
PHP | 313 lines | 257 code | 17 blank | 39 comment | 43 complexity | 9f9a3699f865c05afe65bdd18c948ad5 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?
  2. /**
  3. * ----------------------------------------------------------------
  4. * XBase
  5. * Record.class.php
  6. *
  7. * Developer : Erwin Kooi
  8. * released at : Nov 2005
  9. * last modified by : Erwin Kooi
  10. * date modified : Jan 2005
  11. *
  12. * You're free to use this code as long as you don't alter it
  13. * Copyright (c) 2005 Cyane Dynamic Web Solutions
  14. * Info? Mail to info@cyane.nl
  15. *
  16. * --------------------------------------------------------------
  17. *
  18. * This class defines the data access functions to a DBF record
  19. * Do not construct an instance yourself, generate records through the nextRecord function of XBaseTable
  20. *
  21. **/
  22. define ("DBFFIELD_TYPE_MEMO","M"); // Memo type field.
  23. define ("DBFFIELD_TYPE_CHAR","C"); // Character field.
  24. define ("DBFFIELD_TYPE_NUMERIC","N"); // Numeric
  25. define ("DBFFIELD_TYPE_FLOATING","F"); // Floating point
  26. define ("DBFFIELD_TYPE_DATE","D"); // Date
  27. define ("DBFFIELD_TYPE_LOGICAL","L"); // Logical - ? Y y N n T t F f (? when not initialized).
  28. define ("DBFFIELD_TYPE_DATETIME","T"); // DateTime
  29. define ("DBFFIELD_TYPE_INDEX","I"); // Index
  30. define ("DBFFIELD_IGNORE_0","0"); // ignore this field
  31. class XBaseRecord {
  32. var $zerodate = 0x253d8c;
  33. var $table;
  34. var $choppedData;
  35. var $deleted;
  36. var $inserted;
  37. var $recordIndex;
  38. function XBaseRecord($table, $recordIndex, $rawData=false) {
  39. $this->table =& $table;
  40. $this->recordIndex=$recordIndex;
  41. $this->choppedData = array();
  42. if ($rawData && strlen($rawData)>0) {
  43. $this->inserted=false;
  44. $this->deleted=(ord($rawData[0])!="32");
  45. foreach ($table->getColumns() as $column) {
  46. $this->choppedData[]=substr($rawData,$column->getBytePos(),$column->getDataLength());
  47. }
  48. } else {
  49. $this->inserted=true;
  50. $this->deleted=false;
  51. foreach ($table->getColumns() as $column) {
  52. $this->choppedData[]=str_pad("", $column->getDataLength(),chr(0));
  53. }
  54. }
  55. }
  56. function isDeleted() {
  57. return $this->deleted;
  58. }
  59. function getColumns() {
  60. return $this->table->getColumns();
  61. }
  62. function getColumnByName($name) {
  63. return $this->table->getColumnByName($name);
  64. }
  65. function getColumn($index) {
  66. return $this->table->getColumn($index);
  67. }
  68. function getColumnIndex($name) {
  69. return $this->table->getColumnIndex($name);
  70. }
  71. function getRecordIndex() {
  72. return $this->recordIndex;
  73. }
  74. /**
  75. * -------------------------------------------------------------------------
  76. * Get data functions
  77. * -------------------------------------------------------------------------
  78. */
  79. function getStringByName($columnName) {
  80. return $this->getString($this->table->getColumnByName($columnName));
  81. }
  82. function getStringByIndex($columnIndex) {
  83. return $this->getString($this->table->getColumn($columnIndex));
  84. }
  85. function getString($columnObj) {
  86. if ($columnObj->getType()==DBFFIELD_TYPE_CHAR) {
  87. return $this->forceGetString($columnObj);
  88. } else {
  89. $result = $this->getObject($columnObj);
  90. if ($result && ($columnObj->getType()==DBFFIELD_TYPE_DATETIME || $columnObj->getType()==DBFFIELD_TYPE_DATE)) return @date("r",$result);
  91. if ($columnObj->getType()==DBFFIELD_TYPE_LOGICAL) return $result?"1":"0";
  92. return $result;
  93. }
  94. }
  95. function forceGetString($columnObj) {
  96. if (ord($this->choppedData[$columnObj->getColIndex()][0])=="0") return false;
  97. return trim($this->choppedData[$columnObj->getColIndex()]);
  98. }
  99. function getObjectByName($columnName) {
  100. return $this->getObject($this->table->getColumnByName($columnName));
  101. }
  102. function getObjectByIndex($columnIndex) {
  103. return $this->getObject($this->table->getColumn($columnIndex));
  104. }
  105. function getObject($columnObj) {
  106. switch ($columnObj->getType()) {
  107. case DBFFIELD_TYPE_CHAR : return $this->getString($columnObj);
  108. case DBFFIELD_TYPE_DATE : return $this->getDate($columnObj);
  109. case DBFFIELD_TYPE_DATETIME : return $this->getDateTime($columnObj);
  110. case DBFFIELD_TYPE_FLOATING : return $this->getFloat($columnObj);
  111. case DBFFIELD_TYPE_LOGICAL : return $this->getBoolean($columnObj);
  112. case DBFFIELD_TYPE_MEMO : return $this->getMemo($columnObj);
  113. case DBFFIELD_TYPE_NUMERIC : return $this->getInt($columnObj);
  114. case DBFFIELD_TYPE_INDEX : return $this->getIndex($columnObj);
  115. case DBFFIELD_IGNORE_0 : return false;
  116. }
  117. trigger_error ("cannot handle datatype".$columnObj->getType(), E_USER_ERROR);
  118. }
  119. function getDate($columnObj) {
  120. if ($columnObj->getType()!=DBFFIELD_TYPE_DATE) trigger_error ($columnObj->getName()." is not a Date column", E_USER_ERROR);
  121. $s = $this->forceGetString($columnObj);
  122. if (!$s) return false;
  123. return strtotime($s);
  124. }
  125. function getDateTime($columnObj) {
  126. if ($columnObj->getType()!=DBFFIELD_TYPE_DATETIME) trigger_error ($columnObj->getName()." is not a DateTime column", E_USER_ERROR);
  127. $raw = $this->choppedData[$columnObj->getColIndex()];
  128. $buf = unpack("i",substr($raw,0,4));
  129. $intdate = $buf[1];
  130. $buf = unpack("i",substr($raw,4,4));
  131. $inttime = $buf[1];
  132. if ($intdate==0 && $inttime==0) return false;
  133. $longdate = ($intdate-$this->zerodate)*86400;
  134. return $longdate+$inttime;
  135. }
  136. function getBoolean($columnObj) {
  137. if ($columnObj->getType()!=DBFFIELD_TYPE_LOGICAL) trigger_error ($columnObj->getName()." is not a DateTime column", E_USER_ERROR);
  138. $s = $this->forceGetString($columnObj);
  139. if (!$s) return false;
  140. switch (strtoupper($s[0])) {
  141. case 'T':
  142. case 'Y':
  143. case 'J':
  144. case '1':
  145. return true;
  146. default: return false;
  147. }
  148. }
  149. function getMemo($columnObj) {
  150. if ($columnObj->getType()!=DBFFIELD_TYPE_MEMO) trigger_error ($columnObj->getName()." is not a Memo column", E_USER_ERROR);
  151. return $this->forceGetString($columnObj);
  152. }
  153. function getFloat($columnObj) {
  154. if ($columnObj->getType()!=DBFFIELD_TYPE_FLOATING) trigger_error ($columnObj->getName()." is not a Float column", E_USER_ERROR);
  155. $s = $this->forceGetString($columnObj);
  156. if (!$s) return false;
  157. $s = str_replace(",",".",$s);
  158. return floatval($s);
  159. }
  160. function getInt($columnObj) {
  161. if ($columnObj->getType()!=DBFFIELD_TYPE_NUMERIC) trigger_error ($columnObj->getName()." is not a Number column", E_USER_ERROR);
  162. $s = $this->forceGetString($columnObj);
  163. if (!$s) return false;
  164. $s = str_replace(",",".",$s);
  165. return intval($s);
  166. }
  167. function getIndex($columnObj) {
  168. if ($columnObj->getType()!=DBFFIELD_TYPE_INDEX) trigger_error ($columnObj->getName()." is not an Index column", E_USER_ERROR);
  169. $s = $this->choppedData[$columnObj->getColIndex()];
  170. if (!$s) return false;
  171. $ret = ord($s[0]);
  172. for ($i = 1; $i < $columnObj->length; $i++) {
  173. $ret += $i * 256 * ord($s[$i]);
  174. }
  175. return $ret;
  176. }
  177. /**
  178. * -------------------------------------------------------------------------
  179. * Set data functions
  180. * -------------------------------------------------------------------------
  181. **/
  182. function copyFrom($record) {
  183. $this->choppedData = $record->choppedData;
  184. }
  185. function setDeleted($b) {
  186. $this->deleted=$b;
  187. }
  188. function setStringByName($columnName,$value) {
  189. $this->setString($this->table->getColumnByName($columnName),$value);
  190. }
  191. function setStringByIndex($columnIndex,$value) {
  192. $this->setString($this->table->getColumn($columnIndex),$value);
  193. }
  194. function setString($columnObj,$value) {
  195. if ($columnObj->getType()==DBFFIELD_TYPE_CHAR) {
  196. $this->forceSetString($columnObj,$value);
  197. } else {
  198. if ($columnObj->getType()==DBFFIELD_TYPE_DATETIME || $columnObj->getType()==DBFFIELD_TYPE_DATE) $value = strtotime($value);
  199. $this->setObject($columnObj,$value);
  200. }
  201. }
  202. function forceSetString($columnObj,$value) {
  203. $this->choppedData[$columnObj->getColIndex()] = str_pad(substr($value,0,$columnObj->getDataLength()),$columnObj->getDataLength()," ");
  204. }
  205. function setObjectByName($columnName,$value) {
  206. return $this->setObject($this->table->getColumnByName($columnName),$value);
  207. }
  208. function setObjectByIndex($columnIndex,$value) {
  209. return $this->setObject($this->table->getColumn($columnIndex),$value);
  210. }
  211. function setObject($columnObj,$value) {
  212. switch ($columnObj->getType()) {
  213. case DBFFIELD_TYPE_CHAR : $this->setString($columnObj,$value); return;
  214. case DBFFIELD_TYPE_DATE : $this->setDate($columnObj,$value); return;
  215. case DBFFIELD_TYPE_DATETIME : $this->setDateTime($columnObj,$value); return;
  216. case DBFFIELD_TYPE_FLOATING : $this->setFloat($columnObj,$value); return;
  217. case DBFFIELD_TYPE_LOGICAL : $this->setBoolean($columnObj,$value); return;
  218. case DBFFIELD_TYPE_MEMO : $this->setMemo($columnObj,$value); return;
  219. case DBFFIELD_TYPE_NUMERIC : $this->setInt($columnObj,$value); return;
  220. case DBFFIELD_IGNORE_0 : return;
  221. }
  222. trigger_error ("cannot handle datatype".$columnObj->getType(), E_USER_ERROR);
  223. }
  224. function setDate($columnObj,$value) {
  225. if ($columnObj->getType()!=DBFFIELD_TYPE_DATE) trigger_error ($columnObj->getName()." is not a Date column", E_USER_ERROR);
  226. if (strlen($value)==0) {
  227. $this->forceSetString($columnObj,"");
  228. return;
  229. }
  230. $this->forceSetString($columnObj,date("Ymd",$value));
  231. }
  232. function setDateTime($columnObj,$value) {
  233. if ($columnObj->getType()!=DBFFIELD_TYPE_DATETIME) trigger_error ($columnObj->getName()." is not a DateTime column", E_USER_ERROR);
  234. if (strlen($value)==0) {
  235. $this->forceSetString($columnObj,"");
  236. return;
  237. }
  238. $a = getdate($value);
  239. $d = $this->zerodate + (mktime(0,0,0,$a["mon"],$a["mday"],$a["year"]) / 86400);
  240. $d = pack("i",$d);
  241. $t = pack("i",mktime($a["hours"],$a["minutes"],$a["seconds"],0,0,0));
  242. $this->choppedData[$columnObj->getColIndex()] = $d.$t;
  243. }
  244. function setBoolean($columnObj,$value) {
  245. if ($columnObj->getType()!=DBFFIELD_TYPE_LOGICAL) trigger_error ($columnObj->getName()." is not a DateTime column", E_USER_ERROR);
  246. switch (strtoupper($value)) {
  247. case 'T':
  248. case 'Y':
  249. case 'J':
  250. case '1':
  251. case 'F':
  252. case 'N':
  253. case '0':
  254. $this->forceSetString($columnObj,$value);
  255. return;
  256. case true:
  257. $this->forceSetString($columnObj,"T");
  258. return;
  259. default: $this->forceSetString($columnObj,"F");
  260. }
  261. }
  262. function setMemo($columnObj,$value) {
  263. if ($columnObj->getType()!=DBFFIELD_TYPE_MEMO) trigger_error ($columnObj->getName()." is not a Memo column", E_USER_ERROR);
  264. return $this->forceSetString($columnObj,$value);
  265. }
  266. function setFloat($columnObj,$value) {
  267. if ($columnObj->getType()!=DBFFIELD_TYPE_FLOATING) trigger_error ($columnObj->getName()." is not a Float column", E_USER_ERROR);
  268. if (strlen($value)==0) {
  269. $this->forceSetString($columnObj,"");
  270. return;
  271. }
  272. $value = str_replace(",",".",$value);
  273. $s = $this->forceSetString($columnObj,$value);
  274. }
  275. function setInt($columnObj,$value) {
  276. if ($columnObj->getType()!=DBFFIELD_TYPE_NUMERIC) trigger_error ($columnObj->getName()." is not a Number column", E_USER_ERROR);
  277. if (strlen($value)==0) {
  278. $this->forceSetString($columnObj,"");
  279. return;
  280. }
  281. $value = str_replace(",",".",$value);
  282. //$this->forceSetString($columnObj,intval($value));
  283. /**
  284. * suggestion from Sergiu Neamt: treat number values as decimals
  285. **/
  286. $this->forceSetString($columnObj,number_format($value, $columnObj->decimalCount));
  287. }
  288. /**
  289. * -------------------------------------------------------------------------
  290. * Protected
  291. * -------------------------------------------------------------------------
  292. **/
  293. function serializeRawData() {
  294. return ($this->deleted?"*":" ").implode("",$this->choppedData);
  295. }
  296. }