KD2 Framework  Check-in [0966976430]

Overview
Comment:Make sure EXPLAIN QUERY PLAN statements are treated as readonly
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 7.3
Files: files | file ages | folders
SHA1: 0966976430c91ca31bd66031e587ea6bd3727f6d
User & Date: bohwaz on 2022-02-16 22:06:35
Other Links: branch diff | manifest | tags
Context
2022-02-26
10:24
Return DB_Exception when a statement fails check-in: 9d2f348513 user: bohwaz tags: 7.3
2022-02-16
22:06
Make sure EXPLAIN QUERY PLAN statements are treated as readonly check-in: 0966976430 user: bohwaz tags: 7.3
21:38
DB+SQLite3: implement callback to be able to log or do anything else really check-in: 87654be90b user: bohwaz tags: 7.3
Changes

Modified src/lib/KD2/DB/SQLite3.php from [42c50af287] to [bdd714fc18].

477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492













493
494
495
496
497
498
499
500
					$type = $this->getArgType($value, $key);
					$statement->bindValue(':' . $key, $value, $type);
				}
			}
		}

		try {
			// Return a boolean for write queries to avoid accidental duplicate execution
			// see https://bugs.php.net/bug.php?id=64531

			$result = $statement->execute();

			if ($this->callback) {
				call_user_func($this->callback, __FUNCTION__, 'after', $this, ... func_get_args());
			}














			return $statement->readOnly() ? $result : (bool) $result;
		}
		catch (\Exception $e)
		{
			throw new \RuntimeException($e->getMessage() . "\n" . json_encode($args, true), 0, $e);
		}
	}








<
<
<






>
>
>
>
>
>
>
>
>
>
>
>
>
|







477
478
479
480
481
482
483



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
					$type = $this->getArgType($value, $key);
					$statement->bindValue(':' . $key, $value, $type);
				}
			}
		}

		try {



			$result = $statement->execute();

			if ($this->callback) {
				call_user_func($this->callback, __FUNCTION__, 'after', $this, ... func_get_args());
			}

			$is_readonly = $statement->readOnly();

			// Make sure the statement is actually not readonly and not an EXPLAIN statement
			// see https://sqlite.org/forum/forumpost/8f8453aa37
			if (!$is_readonly
				&& ($sql = trim($statement->getSQL()))
				&& stristr(substr($sql, 0, 7), 'EXPLAIN')
				&& preg_match('/^EXPLAIN\s+QUERY\s+PLAN\s+[^;]+;?$/', $sql)) {
				$is_readonly = true;
			}

			// Return a boolean for write queries to avoid accidental duplicate execution
			// see https://bugs.php.net/bug.php?id=64531
			return $is_readonly ? $result : (bool) $result;
		}
		catch (\Exception $e)
		{
			throw new \RuntimeException($e->getMessage() . "\n" . json_encode($args, true), 0, $e);
		}
	}