PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Turbo4/TpDb.php

http://turbophp.googlecode.com/
PHP | 382 lines | 340 code | 16 blank | 26 comment | 60 complexity | 1ed49a34d626554e4a8f0623d601fe31 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. # +----------------------------------------------------------------------+
  3. # | TurboPhp 4 - TpDb
  4. # | Version 4.0 Beta |
  5. # +----------------------------------------------------------------------+
  6. # | Copyright (c) 2004 Least-Resistance Software |
  7. # +----------------------------------------------------------------------+
  8. # | This source file is subject to the terms of the TurboPhp license, |
  9. # | that is bundled with this package in the file LICENSE.TXT, and is |
  10. # | available at through the world-wide-web at |
  11. # | http://www.turbophp.com/turbophp/license.txt |
  12. # +----------------------------------------------------------------------+
  13. TpRegisterClass('dbtext', 'TTpDbText');
  14. TpRegisterClass('dbedit', 'TTpDbEdit');
  15. TpRegisterClass('dbtextarea', 'TTpDbTextArea');
  16. TpRegisterClass('dblistsource', 'TTpDbListSource');
  17. define('P_QUERY_PARAM', '#{%([^}]*)}#Usm');
  18. class TTpDbConnect extends TTpSilentObj
  19. {
  20. var $ConfigFile;
  21. var $Connection;
  22. var $ConnectManually;
  23. var $Database;
  24. var $Host;
  25. var $OnConnect;
  26. var $Password;
  27. var $Persistent;
  28. var $UserName;
  29. function Init()
  30. {
  31. parent::Init();
  32. $this->Host = '';
  33. }
  34. function StoreAttr($inName, $inValue)
  35. {
  36. if ($inName == 'tphost')
  37. $this->Host = $inValue;
  38. else if ($inName == 'tpuser')
  39. $this->UserName = $inValue;
  40. else if ($inName == 'tppassword')
  41. $this->Password = $inValue;
  42. else if ($inName == 'tpdatabase')
  43. $this->Database = $inValue;
  44. else
  45. parent::StoreAttr($inName, $inValue);
  46. }
  47. function AfterParse()
  48. {
  49. global $tpSupportPath;
  50. if ($this->ConfigFile == '')
  51. $this->ConfigFile = $tpSupportPath . "$this->Name.conf.php";
  52. if (!$this->ConnectManually)
  53. $this->AutoConnect();
  54. }
  55. function LoadConfig()
  56. {
  57. if (file_exists($this->ConfigFile))
  58. {
  59. $conf = parse_ini_file($this->ConfigFile);
  60. $this->Host = TpSafeArrayGet('Host', $conf);
  61. $this->UserName = TpSafeArrayGet('User', $conf);
  62. $this->Password = TpSafeArrayGet('Password', $conf);
  63. }
  64. }
  65. function SelectDatabase()
  66. {
  67. if ($this->Connection && $this->Database <> '')
  68. return $this->DoSelectDb(); // abstract: must be provided in subclass
  69. else if (!$this->App->Debug)
  70. return false;
  71. else if ($this->Connection)
  72. echo $this->DebugId()." no database selected (property is empty).<br>";
  73. else
  74. echo $this->DebugId()." no connection available.<br>";
  75. }
  76. function Connect()
  77. {
  78. if (!$this->Connection)
  79. {
  80. $this->DoEvent($this->OnConnect);
  81. $this->LoadConfig();
  82. $this->DoConnect(); // abstract: must be provided in subclass
  83. }
  84. return $this->Connection;
  85. }
  86. function AutoConnect()
  87. {
  88. $this->Connect();
  89. $this->SelectDatabase();
  90. }
  91. function ConnectOrDie($inMessage = '')
  92. {
  93. if ($inMessage == '')
  94. $inMessage = 'Could not connect: ';
  95. $this->Connect() or die($inMessage . mysql_error() . '<br>');
  96. }
  97. function Close()
  98. {
  99. $this->Connection = false;
  100. }
  101. function Dump()
  102. {
  103. parent::Dump();
  104. echo "Host: [$this->Host]<br>";
  105. echo "User: [$this->UserName]<br>";
  106. echo "Password: <hidden><br>";
  107. echo "Persistent: [$this->Persistent]<br>";
  108. echo "Database: [$this->Database]<br>";
  109. }
  110. }
  111. class TTpDataSource extends TTpSilentObj
  112. {
  113. var $Result;
  114. var $Row;
  115. var $OnBeforeExecute;
  116. var $OnFailure;
  117. var $OnSuccess;
  118. function Init()
  119. {
  120. parent::Init();
  121. $this->Row = false;
  122. }
  123. function ExecuteQuery()
  124. {
  125. // Abstract. Must be provided by subclass.
  126. }
  127. function Execute()
  128. {
  129. if ($this->App->Debug)
  130. echo $this->DebugId()."Execute()<br>";
  131. $this->DoEvent($this->OnBeforeExecute);
  132. $this->ExecuteQuery();
  133. if (!$this->Row)
  134. $this->DoEvent($this->OnFailure);
  135. else
  136. $this->DoEvent($this->OnSuccess);
  137. }
  138. function NeedData()
  139. {
  140. if ($this->App->Debug)
  141. echo $this->DebugId()."NeedData() ...";
  142. if (!$this->Result)
  143. {
  144. if ($this->App->Debug)
  145. echo "calling Execute()<br>";
  146. $this->Execute();
  147. }
  148. else if ($this->App->Debug)
  149. echo "using cache.<br>";
  150. }
  151. function EOF()
  152. {
  153. return !$this->Row;
  154. }
  155. function Rewind()
  156. {
  157. $this->NeedData();
  158. }
  159. function Next()
  160. {
  161. $this->NeedData();
  162. }
  163. function GetField($inFieldId)
  164. {
  165. $this->NeedData();
  166. if (!$this->Row)
  167. return '(bad row)';
  168. else
  169. return TpSafeArrayGet($inFieldId, $this->Row);
  170. }
  171. }
  172. class TTpQuery extends TTpDataSource
  173. {
  174. var $SQL;
  175. var $Params;
  176. function Init()
  177. {
  178. parent::Init();
  179. $this->Params = array();
  180. }
  181. function AfterParse()
  182. {
  183. parent::AfterParse();
  184. $this->SQL = base64_decode($this->SQL);
  185. }
  186. function InterpolateParam(&$matches)
  187. {
  188. $obj = $this->App->GetObject($matches[1]);
  189. if ($obj)
  190. $p = $obj->GetDefaultValue();
  191. else
  192. $p = TpSafeArrayGet($matches[1], $this->Params);
  193. return $this->Interpolate($p);
  194. }
  195. function Interpolate(&$inText)
  196. {
  197. return preg_replace_callback(
  198. P_QUERY_PARAM,
  199. array(&$this, 'InterpolateParam'),
  200. $inText
  201. );
  202. }
  203. function InterpolatedQuery()
  204. {
  205. return $this->Interpolate($this->SQL);
  206. }
  207. function Dump()
  208. {
  209. parent::Dump();
  210. echo "SQL: [$this->SQL]<br>";
  211. if (count($this->Params) > 0)
  212. {
  213. print_r($this->Params);
  214. echo "<br>";
  215. }
  216. }
  217. }
  218. class TTpDbText extends TTpObj
  219. {
  220. var $DataSource;
  221. var $Field;
  222. function AfterParse()
  223. {
  224. if ($this->App->Debug)
  225. {
  226. if (!$this->DataSource)
  227. echo $this->DebugId()."AfterParse(): parsed empty DataSource [$this->DataSource]<br>";
  228. $s = $this->DataSource;
  229. }
  230. //
  231. if (is_string($this->DataSource))
  232. $this->DataSource = &$this->App->GetObject($this->DataSource);
  233. //
  234. if ($this->App->Debug)
  235. {
  236. if (!$this->DataSource)
  237. echo $this->DebugId()."AfterParse(): failed to get DataSource object [$s]<br>";
  238. else
  239. echo $this->DebugId()."AfterParse(): DataSource object [$s => ".$this->DataSource->Name."]<br>";
  240. $this->App->DumpObjects();
  241. }
  242. }
  243. function BeforeGenerate()
  244. {
  245. if ($this->App->Debug)
  246. echo $this->DebugId()."BeforeGenerate(): DataSource object [$this->DataSource]<br>";
  247. parent::BeforeGenerate();
  248. if ($this->DataSource)
  249. $this->Content = $this->DataSource->GetField($this->Field);
  250. else if ($this->App->Debug)
  251. echo $this->DebugId()."Bad DataSource<br>";
  252. }
  253. function Dump()
  254. {
  255. parent::Dump();
  256. echo "DataSource: [".$this->DataSource->Name."]<br>";
  257. echo "Field: [$this->Field]<br>";
  258. }
  259. }
  260. class TTpDbInput extends TTpInput
  261. {
  262. var $DataSource;
  263. var $Field;
  264. function AfterParse()
  265. {
  266. parent::AfterParse();
  267. if ($this->App->Debug)
  268. echo $this->DebugId()."DataSource = $this->DataSource<br>";
  269. if (is_string($this->DataSource))
  270. $this->DataSource = &$this->App->GetObject($this->DataSource);
  271. }
  272. function GetData()
  273. {
  274. if ($this->DataSource)
  275. return $this->DataSource->GetField($this->Field);
  276. else if ($this->App->Debug)
  277. echo $this->DebugId()."Bad DataSource<br>";
  278. return '';
  279. }
  280. function Dump()
  281. {
  282. parent::Dump();
  283. echo "DataSource: [".$this->DataSource->Name."]<br>";
  284. echo "Field: [$this->Field]<br>";
  285. }
  286. }
  287. class TTpDbEdit extends TTpDbInput
  288. {
  289. function BeforeGenerate()
  290. {
  291. parent::BeforeGenerate();
  292. if ($this->DataSource)
  293. $this->Value = $this->DataSource->GetField($this->Field);
  294. else if ($this->App->Debug)
  295. echo $this->DebugId()."Bad DataSource<br>";
  296. }
  297. }
  298. class TTpDbTextArea extends TTpDbInput
  299. {
  300. function StoreValue()
  301. {
  302. $this->Content = $this->NewValue;
  303. }
  304. function GetDefaultValue()
  305. {
  306. return $this->Content;
  307. }
  308. function BeforeGenerate()
  309. {
  310. parent::BeforeGenerate();
  311. if ($this->DataSource)
  312. $this->Content = $this->DataSource->GetField($this->Field);
  313. else if ($this->App->Debug)
  314. echo $this->DebugId()."Bad DataSource<br>";
  315. }
  316. }
  317. class TTpDbListSource extends TTpListSource
  318. {
  319. var $DataSource;
  320. var $Field;
  321. function AfterParse()
  322. {
  323. //echo "[$this->DataSource]<br>";
  324. //echo ($this->App->IsObject($this->DataSource) ? 'Found' : 'Not found')."<br>";
  325. if (is_string($this->DataSource))
  326. $this->DataSource = &$this->App->GetObject($this->DataSource);
  327. // BAD
  328. // if (is_string($this->DataSource))
  329. // $this->DataSource &= $this->App->GetObject($this->DataSource);
  330. // Good
  331. // if (is_string($this->DataSource))
  332. // if ($this->App->IsObject($this->DataSource))
  333. // $this->DataSource = &$this->App->Objects[$this->DataSource];
  334. // echo "[".$this->DataSource->Name."]<br>";
  335. }
  336. function GetItems(&$outItems)
  337. {
  338. $outItems = array();
  339. if ($this->DataSource)
  340. {
  341. $this->DataSource->Rewind();
  342. while ($this->DataSource->Next())
  343. {
  344. $field = $this->DataSource->GetField($this->Field);
  345. //if ($this->NumericKeys)
  346. //$outItems[] = $this->DataSource->GetField($this->Field);
  347. $outItems[$field] = $field;
  348. }
  349. if ($this->App->Debug)
  350. {
  351. echo $this->DebugId()."GetItems:<br><pre>";
  352. print_r($outItems);
  353. echo "</pre><br>";
  354. }
  355. }
  356. else if ($this->App->Debug)
  357. echo $this->DebugId()."GetItems: bad DataSource<br>";
  358. }
  359. function Dump()
  360. {
  361. parent::Dump();
  362. echo "DataSource: [".$this->DataSource->Name."]<br>";
  363. echo "Field: [$this->Field]<br>";
  364. }
  365. }
  366. ?>