21 declare(strict_types=1);
54 private $loggingQueries;
56 private $queries = [];
57 private $handlers = [];
69 public function __construct(Plugin $plugin,
SqlThread $thread, ?
string $placeHolder,
bool $logQueries =
false){
70 $this->plugin = $plugin;
71 $this->thread = $thread;
72 $this->loggingQueries = $logQueries;
73 $this->placeHolder = $placeHolder;
76 $this->plugin->getScheduler()->scheduleRepeatingTask($this->task, 1);
84 return $this->loggingQueries;
90 foreach($parser->getResults() as $result){
96 if(isset($this->queries[$stmt->
getName()])){
99 $this->queries[$stmt->
getName()] = $stmt;
102 public function executeGeneric(
string $queryName, array $args = [], ?callable $onSuccess = null, ?callable $onError = null) : void{
104 if($onSuccess !== null){
110 public function executeGenericRaw(
string $query, array $args = [], ?callable $onSuccess = null, ?callable $onError = null) : void{
112 if($onSuccess !== null){
118 public function executeChange(
string $queryName, array $args = [], ?callable $onSuccess = null, ?callable $onError = null) : void{
120 if($onSuccess !== null){
126 public function executeChangeRaw(
string $query, array $args = [], ?callable $onSuccess = null, ?callable $onError = null) : void{
128 if($onSuccess !== null){
134 public function executeInsert(
string $queryName, array $args = [], ?callable $onInserted = null, ?callable $onError = null) : void{
136 if($onInserted !== null){
142 public function executeInsertRaw(
string $query, array $args = [], ?callable $onInserted = null, ?callable $onError = null) : void{
144 if($onInserted !== null){
150 public function executeSelect(
string $queryName, array $args = [], ?callable $onSelect = null, ?callable $onError = null) : void{
152 if($onSelect !== null){
158 public function executeSelectRaw(
string $query, array $args = [], ?callable $onSelect = null, ?callable $onError = null) : void{
160 if($onSelect !== null){
166 private function executeImpl(
string $queryName, array $args,
int $mode, callable $handler, ?callable $onError) : void{
167 if(!isset($this->queries[$queryName])){
170 $query = $this->queries[$queryName]->format($args, $this->placeHolder, $outArgs);
172 $this->executeImplRaw($query, $outArgs, $mode, $handler, $onError);
175 private function executeImplRaw(
string $query, array $args,
int $mode, callable $handler, ?callable $onError) : void{
176 $queryId = $this->queryId++;
178 $this->handlers[$queryId] =
function($result) use ($handler, $onError, $trace){
179 if($result instanceof SqlError){
180 $this->reportError($onError, $result, $trace);
187 $prop->setAccessible(
true);
188 $newTrace = $prop->getValue($e);
189 $oldTrace = $prop->getValue($trace);
190 for($i =
count($newTrace) - 1, $j =
count($oldTrace) - 1; $i >= 0 && $j >= 0 && $newTrace[$i] === $oldTrace[$j]; --$i, --$j){
196 "function" => Terminal::$COLOR_YELLOW .
"--- below is the original stack trace ---" . Terminal::$FORMAT_RESET,
203 $exceptionProperty = (
new ReflectionClass(Exception::class))->getProperty(
"trace");
204 $exceptionProperty->setAccessible(
true);
205 $oldTrace = $exceptionProperty->getValue($trace);
207 $errorProperty = (
new ReflectionClass(Error::class))->getProperty(
"trace");
208 $errorProperty->setAccessible(
true);
209 $newTrace = $errorProperty->getValue($e);
211 for($i =
count($newTrace) - 1, $j =
count($oldTrace) - 1; $i >= 0 && $j >= 0 && $newTrace[$i] === $oldTrace[$j]; --$i, --$j){
215 $errorProperty->setValue($e,
array_merge($newTrace, [
217 "function" => Terminal::$COLOR_YELLOW .
"--- below is the original stack trace ---" . Terminal::$FORMAT_RESET,
225 if($this->loggingQueries){
226 $this->plugin->getLogger()->debug(
"Queuing mode-$mode query: " .
str_replace([
"\r\n",
"\n"],
"\\n ", $query) .
" | Args: " .
json_encode($args));
228 $this->thread->addQuery($queryId, $mode, $query, $args);
231 private function reportError(?callable $default, SqlError $error, ?
Exception $trace) : void{
232 if($default !== null){
234 $default($error, $trace);
236 }
catch(SqlError $err){
241 $this->plugin->getLogger()->error($error->getMessage());
242 if($error->getQuery() !== null){
243 $this->plugin->getLogger()->debug(
"Query: " . $error->getQuery());
245 if($error->getArgs() !== null){
246 $this->plugin->getLogger()->debug(
"Args: " .
json_encode($error->getArgs()));
249 $this->plugin->getLogger()->debug(
"Stack trace: " . $trace->getTraceAsString());
255 while(!empty($this->handlers)){
262 $this->thread->readResults($this->handlers);
266 $this->thread->stopRunning();
267 $this->plugin->getScheduler()->cancelTask($this->task->getTaskId());
loadQuery(GenericStatement $stmt)
setLoggingQueries(bool $loggingQueries)
executeSelect(string $queryName, array $args=[], ?callable $onSelect=null, ?callable $onError=null)
executeChangeRaw(string $query, array $args=[], ?callable $onSuccess=null, ?callable $onError=null)
executeGenericRaw(string $query, array $args=[], ?callable $onSuccess=null, ?callable $onError=null)
executeInsert(string $queryName, array $args=[], ?callable $onInserted=null, ?callable $onError=null)
__construct(Plugin $plugin, SqlThread $thread, ?string $placeHolder, bool $logQueries=false)
executeGeneric(string $queryName, array $args=[], ?callable $onSuccess=null, ?callable $onError=null)
loadQueryFile($fh, string $fileName=null)
executeInsertRaw(string $query, array $args=[], ?callable $onInserted=null, ?callable $onError=null)
executeSelectRaw(string $query, array $args=[], ?callable $onSelect=null, ?callable $onError=null)
executeChange(string $queryName, array $args=[], ?callable $onSuccess=null, ?callable $onError=null)