SQLite.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. namespace netico\Bones;
  4. /**
  5. * SQLite wrapper.
  6. *
  7. * This is just another SQLite database access wrapper class.
  8. * It can establish connections to the SQLite database, execute SQL queries, and return results.
  9. * Native drivers are great if you are only using one database in your application,
  10. * so this class uses the native SQLite driver.
  11. *
  12. * $db = new \netico\Bones\SQLite();
  13. * $db->sql("SELECT * FROM pages ORDER BY [rowid] ASC;");
  14. * $db->close();
  15. *
  16. * var_dump($db->data);
  17. *
  18. * @package bones
  19. * @link https://git.lattuga.net/netico/code-library/src/master/Framework
  20. * @copyright Copyright (c) 2016, 2022 netico <netico@riseup.net>
  21. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License
  22. * @author netico <netico@riseup.net>
  23. *
  24. */
  25. class SQLite
  26. {
  27. /**
  28. * This array contains the results of the query.
  29. *
  30. * @var array[] $data
  31. */
  32. public $data;
  33. private $schema;
  34. private $conn;
  35. /**
  36. * This method retrieves the SQLite database path from the configuration file
  37. * and then opens a connection in read mode.
  38. */
  39. public function __construct()
  40. {
  41. $c = new \netico\Bones\Configuration(CONFIG);
  42. $this->schema = $c->getValue("database", "sqlite");
  43. // FIXME: Exception
  44. $this->conn = $this->SQLiteOpen($this->schema);
  45. }
  46. /**
  47. * This method closes the connection to the database.
  48. *
  49. * @return void
  50. */
  51. public function close()
  52. {
  53. $this->SQLiteClose($this->conn);
  54. }
  55. /**
  56. * It runs a SQL query on a given database and builds an array of the results.
  57. *
  58. * @param string $query
  59. * @return void
  60. */
  61. public function SQL($query)
  62. {
  63. $r = $this->SQLiteQuery($this->conn, $query);
  64. if (is_array($this->data) === true) {
  65. unset($this->data);
  66. }
  67. // FIXME: Exception
  68. if (is_bool($r) === false) {
  69. while ($row = $this->SQLiteFetchArray($r)) {
  70. $this->data[] = $row;
  71. }
  72. }
  73. return $this->data;
  74. }
  75. private function SQLiteOpen($path)
  76. {
  77. $handle = new \SQLite3($path, SQLITE3_OPEN_READONLY);
  78. return $handle;
  79. }
  80. private function SQLiteClose($handle)
  81. {
  82. $handle->close();
  83. }
  84. private function SQLiteQuery($dbhandle, $query)
  85. {
  86. $res = $dbhandle->query($query);
  87. return $res;
  88. }
  89. private function SQLiteFetchArray(&$r)
  90. {
  91. # Get Columns
  92. $i = 0;
  93. while ($r->columnName($i)) {
  94. $columns[] = $r->columnName($i);
  95. $i++;
  96. }
  97. $res = $r->fetchArray(SQLITE3_ASSOC);
  98. return $res;
  99. }
  100. }