1<?php 2namespace DALMP\Queue; 3 4/** 5 * SQLite 6 * 7 * @author Nicolas de Bari Embriz <nbari@dalmp.com> 8 * @package DALMP 9 * @license BSD License 10 * @version 3.0.3 11 */ 12class SQLite implements QueueInterface 13{ 14 /** 15 * filename - Path to the SQLite database, or :memory: to use in-memory 16 * database. 17 * 18 * @var string 19 */ 20 private $filename; 21 22 /** 23 * queue name 24 * 25 * @var string 26 */ 27 private $queue_name; 28 29 /** 30 * enc_key 31 * 32 * @var string 33 */ 34 private $enc_key = false; 35 36 /** 37 * Constructor 38 * 39 * @param string $filename 40 * @param string $queue 41 * @param string $enc_key 42 */ 43 public function __construct($filename, $queue_name = 'default', $enc_key = null) 44 { 45 $sdb = new \SQLite3($filename); 46 $sdb->busyTimeout(2000); 47 48 $this->filename = $filename; 49 $this->queue_name = $queue_name; 50 51 if ($enc_key) { 52 if ($this->sdb->exec(sprintf("PRAGMA key='%s'", $enc_key))) { 53 $this->enc_key = $enc_key; 54 } 55 } 56 57 $sdb->exec('PRAGMA synchronous=OFF; PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY'); 58 $sdb->exec('CREATE TABLE IF NOT EXISTS queues (id INTEGER PRIMARY KEY, queue VARCHAR (64) NOT null, data TEXT, cdate DATE)'); 59 $sdb->busyTimeout(0); 60 $sdb->close(); 61 } 62 63 /** 64 * enqueue 65 * 66 * @param string $value 67 * @return boolean 68 */ 69 public function enqueue($value) 70 { 71 $sdb = new \SQLite3($this->filename); 72 $sdb->busyTimeout(2000); 73 if ($this->enc_key) { 74 $sdb->exec(sprintf("PRAGMA key='%s'", $enc_key)); 75 } 76 77 $stmt = $sdb->prepare('INSERT INTO queues VALUES (null, ?, ?, ?)'); 78 $stmt->bindValue(1, $this->queue_name, SQLITE3_TEXT); 79 $stmt->bindValue(2, base64_encode($value), SQLITE3_TEXT); 80 $stmt->bindValue(3, @date('Y-m-d H:i:s'), SQLITE3_BLOB); 81 82 if (!$stmt->execute()) { 83 throw new \ErrorException(sprintf('Could not save: [ %s ] on queue [ %s ] in [ %s ]', $value, $this->queue_name, $this->filename)); 84 } 85 86 $sdb->busyTimeout(0); 87 $sdb->close(); 88 89 return true; 90 } 91 92 /** 93 * dequeue 94 * 95 * @param string $key 96 */ 97 public function dequeue($limit = false) 98 { 99 $sdb = new \SQLite3($this->filename); 100 $sdb->busyTimeout(2000); 101 if ($this->enc_key) { 102 $sdb->exec(sprintf("PRAGMA key='%s'", $enc_key)); 103 } 104 105 if ($limit) { 106 $stmt = $sdb->prepare('SELECT * FROM queues WHERE queue = ? LIMIT ?'); 107 $stmt->bindValue(1, $this->queue_name, SQLITE3_TEXT); 108 $stmt->bindValue(2, $limit, SQLITE3_INTEGER); 109 } else { 110 $stmt = $sdb->prepare('SELECT * FROM queues WHERE queue = ?'); 111 $stmt->bindValue(1, $this->queue_name, SQLITE3_TEXT); 112 } 113 114 $rs = $stmt->execute(); 115 116 $rows = array(); 117 118 if ($rs) { 119 while ($row = $rs->fetchArray(SQLITE3_ASSOC)) { 120 $rows[$row['id']] = array('id' => $row['id'], 'queue' => $row['queue'], 'data' => $row['data'], 'cdate' => $row['cdate']); 121 } 122 } 123 124 return $rows; 125 } 126 127 /** 128 * delete element from queue 129 * 130 * @param string $value 131 */ 132 public function delete($key) 133 { 134 $sdb = new \SQLite3($this->filename); 135 $sdb->busyTimeout(2000); 136 if ($this->enc_key) { 137 $sdb->exec(sprintf("PRAGMA key='%s'", $enc_key)); 138 } 139 140 $stmt = $sdb->prepare('DELETE FROM queues WHERE queue = ? AND id = ?'); 141 $stmt->bindValue(1, $this->queue_name, SQLITE3_TEXT); 142 $stmt->bindValue(2, $key, SQLITE3_INTEGER); 143 144 $sdb->busyTimeout(0); 145 146 return (bool) $stmt->execute(); 147 } 148 149 /** 150 * 151 * X execute/call custom methods 152 * 153 * @return queue object 154 */ 155 public function X() {} 156 157} 158