PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/web/editor/test/sqlite/play.php

https://bitbucket.org/scriptoid/diagramo-alex
PHP | 229 lines | 122 code | 25 blank | 82 comment | 11 complexity | 3e09856787184535aa5225a17c62840d MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. /**
  3. * http://zetcode.com/databases/sqlitephptutorial/
  4. */
  5. $dbhandle = sqlite_open('test.db', 0666, $error);
  6. //$stm = "DROP TABLE diagram";
  7. //$ok = sqlite_exec($dbhandle, $stm, $error);
  8. //
  9. //
  10. //$stm = "CREATE TABLE diagram (
  11. //id INTEGER PRIMARY KEY,
  12. //title VARCHAR(255),
  13. //description TEXT,
  14. //public BOOL,
  15. //createdDate DATETIME NOT NULL,
  16. //lastUpdate DATETIME NOT NULL,
  17. //size INT UNSIGNED COMMENT 'The size of diagram in bytes'
  18. //)";
  19. //
  20. ////$stm = "CREATE TABLE Friends(Id integer PRIMARY KEY,
  21. //// Name text UNIQUE NOT NULL, Sex text CHECK(Sex IN ('M', 'F')))";
  22. //$ok = sqlite_exec($dbhandle, $stm, $error);
  23. //
  24. //if (!$ok)
  25. // die("Cannot execute query. $error");
  26. //
  27. //$stm1 = "INSERT INTO diagram (title, description, createdDate, lastUpdate, size)
  28. // VALUES('test', 'Test description', '2012-11-23 12:34:56', '2012-11-24 13:34:56', 1023)";
  29. //$ok1 = sqlite_exec($dbhandle, $stm1);
  30. //if (!$ok1) die("Cannot execute statement.");
  31. //
  32. //$stm1 = "INSERT INTO diagram (title, description, createdDate, lastUpdate, size)
  33. // VALUES('test2', 'Test description2', '2012-11-23 12:34:56', '2012-11-24 13:34:56', 1023)";
  34. //$ok1 = sqlite_exec($dbhandle, $stm1);
  35. //if (!$ok1) die("Cannot execute statement.");
  36. //
  37. //echo "Table diagram created successfully";
  38. //
  39. //
  40. //$query = "SELECT * FROM diagram";
  41. //$result = sqlite_query($dbhandle, $query);
  42. //if (!$result) die("Cannot execute query.");
  43. //
  44. //while ($row = sqlite_fetch_array($result, SQLITE_ASSOC)) {
  45. // echo "\n" . $row['id'] . " : " . $row['title'];
  46. //}
  47. class Diagram {
  48. public $id;
  49. public $title;
  50. public $description;
  51. public $public;
  52. public $createdDate;
  53. public $lastUpdate;
  54. function loadFromSQL($row) {
  55. $this->id = is_null($row['id']) ? null : $row['id'];
  56. $this->title = is_null($row['title']) ? null : $row['title'];
  57. $this->description = is_null($row['description']) ? null : $row['description'];
  58. $this->public = is_null($row['public']) ? null : $row['public'];
  59. $this->createdDate = is_null($row['createdDate']) ? null : $row['createdDate'];
  60. $this->lastUpdate = is_null($row['lastUpdate']) ? null : $row['lastUpdate'];
  61. }
  62. }
  63. class User {
  64. public $id;
  65. public $email;
  66. public $password;
  67. public $name;
  68. public $createdDate;
  69. public $lastLoginDate;
  70. public $lastLoginIP;
  71. public $lastBrowserType;
  72. public $admin;
  73. function loadFromSQL($row) {
  74. $this->id = is_null($row['id']) ? null : $row['id'];
  75. $this->email = is_null($row['email']) ? null : $row['email'];
  76. $this->password = is_null($row['password']) ? null : $row['password'];
  77. $this->name = is_null($row['name']) ? null : $row['name'];
  78. $this->createdDate = is_null($row['createdDate']) ? null : $row['createdDate'];
  79. $this->lastLoginDate = is_null($row['lastLoginDate']) ? null : $row['lastLoginDate'];
  80. $this->lastLoginIP = is_null($row['lastLoginIP']) ? null : $row['lastLoginIP'];
  81. $this->lastBrowserType = is_null($row['lastBrowserType']) ? null : $row['lastBrowserType'];
  82. $this->admin = is_null($row['admin']) ? null : $row['admin'];
  83. }
  84. }
  85. function diagramCreate($dbhandle, $title, $description, $public){
  86. $stm1 = sprintf("INSERT INTO diagram (title, description, public, createdDate, lastUpdate)
  87. VALUES('%s', '%s', '%s', '%s', '%s')",
  88. $title, $description, $public, gmdate('Y-m-d H:i:s'), gmdate('Y-m-d H:i:s'));
  89. #print($stm1);
  90. $ok1 = sqlite_exec($dbhandle, $stm1);
  91. if (!$ok1) die("Cannot execute statement.");
  92. }
  93. function diagramGetById($dbhandle, $diagramId) {
  94. $d = false;
  95. $query = sprintf("SELECT * FROM diagram where id=%d", $diagramId);
  96. $result = sqlite_query($dbhandle, $query);
  97. if ($result) {
  98. $row = sqlite_fetch_array($result, SQLITE_ASSOC);
  99. if($row){
  100. $d = new Diagram();
  101. $d->loadFromSQL($row);
  102. }
  103. }
  104. return $d;
  105. }
  106. function diagramGetAll($dbhandle) {
  107. $diagrams = array();
  108. $query = "SELECT * FROM diagram ORDER BY title";
  109. $result = sqlite_query($dbhandle, $query);
  110. if ($result) {
  111. while($row = sqlite_fetch_array($result, SQLITE_ASSOC)){
  112. #print_r($row);
  113. $d = new Diagram();
  114. $d->loadFromSQL($row);
  115. $diagrams[] = $d;
  116. }
  117. }
  118. return $diagrams;
  119. }
  120. function diagramDeleteById($dbhandle, $diagramId){
  121. $query = sprintf("delete FROM diagram where id=%d", $diagramId);
  122. $result = sqlite_query($dbhandle, $query);
  123. if ($result) {
  124. }
  125. }
  126. if($_REQUEST['action'] == 'init'){
  127. /*
  128. $stm = "DROP TABLE diagram";
  129. $ok = sqlite_exec($dbhandle, $stm, $error);
  130. if (!$ok) die("Cannot execute statement.");
  131. */
  132. // $stm = "CREATE TABLE diagram (
  133. // id INTEGER PRIMARY KEY,
  134. // title VARCHAR(255),
  135. // description TEXT,
  136. // public BOOL,
  137. // createdDate DATETIME NOT NULL,
  138. // lastUpdate DATETIME NOT NULL,
  139. // size INT UNSIGNED COMMENT 'The size of diagram in bytes'
  140. // )";
  141. // $ok = sqlite_exec($dbhandle, $stm, $error);
  142. // if (!$ok) die("Cannot execute statement.");
  143. //
  144. // $stm1 = "INSERT INTO diagram (title, description, createdDate, lastUpdate, size)
  145. // VALUES('test', 'Test description', '2012-11-23 12:34:56', '2012-11-24 13:34:56', 1023)";
  146. // $ok1 = sqlite_exec($dbhandle, $stm1);
  147. // if (!$ok1) die("Cannot execute statement.");
  148. //
  149. // $stm1 = "INSERT INTO diagram (title, description, createdDate, lastUpdate, size)
  150. // VALUES('test2', 'Test description2', '2012-11-23 12:34:56', '2012-11-24 13:34:56', 1023)";
  151. // $ok1 = sqlite_exec($dbhandle, $stm1);
  152. // if (!$ok1) die("Cannot execute statement.");
  153. #echo "Table diagram created successfully";
  154. header('Location: ./play.php');
  155. }else if($_REQUEST['action'] == 'populate'){
  156. // $stm1 = "INSERT INTO diagram (title, description, createdDate, lastUpdate, size)
  157. // VALUES('test', 'Test description', '2012-11-23 12:34:56', '2012-11-24 13:34:56', 1023)";
  158. // $ok1 = sqlite_exec($dbhandle, $stm1);
  159. // if (!$ok1) die("Cannot execute statement.");
  160. //
  161. // $stm1 = "INSERT INTO diagram (title, description, createdDate, lastUpdate, size)
  162. // VALUES('test2', 'Test description2', '2012-11-23 12:34:56', '2012-11-24 13:34:56', 1023)";
  163. // $ok1 = sqlite_exec($dbhandle, $stm1);
  164. // if (!$ok1) die("Cannot execute statement.");
  165. diagramCreate($dbhandle, 'Test', 'Simple description', true);
  166. header('Location: ./play.php');
  167. }else if($_REQUEST['action'] == 'delete'){
  168. diagramDeleteById($dbhandle, $_REQUEST['diagramId']);
  169. header('Location: ./play.php');
  170. }else{//default view?>
  171. <html>
  172. <body>
  173. <table border="1">
  174. <tr>
  175. <td>id</td>
  176. <td>title</td>
  177. <td>description</td>
  178. <td>public</td>
  179. <td>createdDate</td>
  180. <td>lastUpdate</td>
  181. <td>delete</td>
  182. </tr>
  183. <?
  184. $diagrams = diagramGetAll($dbhandle);
  185. foreach($diagrams as $diagram){?>
  186. <tr>
  187. <td><?=$diagram->id?></td>
  188. <td><?=$diagram->title?></td>
  189. <td><?=$diagram->description?></td>
  190. <td><?=$diagram->public?></td>
  191. <td><?=$diagram->createdDate?></td>
  192. <td><?=$diagram->lastUpdate?></td>
  193. <td><a href="?action=delete&diagramId=<?=$diagram->id?>">delete</a></td>
  194. </tr>
  195. <?}?>
  196. </table>
  197. <a href="?action=init">Init</a> |
  198. <a href="?action=populate">Populate</a>
  199. </body>
  200. </html>
  201. <?}?>
  202. <?
  203. sqlite_close($dbhandle);
  204. ?>