xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / inc / sql.inc.php (0df57c195c603c2fbd13113b41681ff5821ee09c) (9,455B) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
<?php
require_once($INC . "/log.inc.php");
require_once($INC . "/prof.inc.php");

// Some constants for sql error codes
define('RG_SQL_UNIQUE_VIOLATION', '23505');

if (!function_exists("pg_connect"))
	die("FATAL: php PostgreSQL is not installed!");

if (!isset($rg_sql_debug))
	$rg_sql_debug = 0;

$rg_sql_conn = array();

$rg_sql_error = "";


/*
 * Set error string
 */
function rg_sql_set_error($str)
{
	global $rg_sql_error;
	$rg_sql_error = $str;
	rg_log('sql_set_error: ' . $str);
}

function rg_sql_error()
{
	global $rg_sql_error;
	return $rg_sql_error;
}

/*
 * Set application name to be able to identify the scripts
 */
$rg_sql_app = "rg-unk";
function rg_sql_app($name)
{
	global $rg_sql_app;

	$rg_sql_app = $name;
}

/*
 * Connect to database
 */
function rg_sql_open_nodelay($h)
{
	global $rg_sql_debug;
	global $rg_sql_conn;

	if ($rg_sql_debug > 20)
		rg_log_enter('sql_open_nodelay');

	$ret = FALSE;
	while (1) {
		if (!isset($rg_sql_conn[$h])) {
			rg_internal_error('Handler [' . $h . '] not present!');
			break;
		}

		if ($rg_sql_debug > 40) {
			rg_log('My pid: ' . getmypid());
			rg_log_ml('DEBUG: rg_sql_conn: ' . print_r($rg_sql_conn, TRUE));
		}

		if (isset($rg_sql_conn[$h]['db'])) {
			if (getmypid() == $rg_sql_conn[$h]['pid']) {
				if ($rg_sql_debug > 30)
					rg_log('DB: Same pid, reuse connection');
				$ret = $rg_sql_conn[$h]['db'];
				break;
			}

			if ($rg_sql_debug > 25)
				rg_log('DB: pid is different, reconnecting...');
			unset($rg_sql_conn[$h]['db']);
		}

		putenv('PGAPPNAME=' . $rg_sql_conn[$h]['app']);

		$str = $rg_sql_conn[$h]['str'];
		if ($rg_sql_debug > 0)
			rg_log("DB: openning [$str]...");

		rg_prof_set(array('db_conn' => 1));

		// This is used to test if we forked
		$rg_sql_conn[$h]['pid'] = getmypid();

		$_s = microtime(TRUE);

		$tries = 0;
		while (1) {
			$db = @pg_pconnect($str);
			if ($db !== FALSE) {
				// reconnect if needed
				$x = @pg_ping($db);
				if ($x === TRUE)
					break;
			}

			if ($tries == 0)
				rg_log('Cannot connect to db. Keep trying...');

			$tries++;
			if ($tries > 30) {
				$db = FALSE;
				break;
			}
			sleep(1);
		}
		$diff = intval((microtime(TRUE) - $_s) * 1000);
		rg_prof_set(array('db_c_ms' => $diff));
		if ($db === FALSE) {
			$err = 'cannot connect to database';
			rg_sql_set_error($err);
			rg_internal_error($err);
			rg_prof_set(array('db_conn_errors' => 1));
			break;
		}

		$rg_sql_conn[$h]['db'] = $db;
		$ret = $db;
		break;
	}

	if ($rg_sql_debug > 20)
		rg_log_exit();
	return $ret;
}

/*
 * Prepare to connect to database (delayed connection).
 * Returns a special handler.
 */
function rg_sql_open($str)
{
	global $rg_sql_conn;
	global $rg_sql_app;

	$free_index = count($rg_sql_conn);
	$rg_sql_conn[$free_index] = array(
		'str' => $str,
		'app' => $rg_sql_app
	);

	//rg_log("Delay connection to [$str], index $free_index.");
	return $free_index;
}

/*
 * Escaping
 */
function rg_sql_escape($h, $str)
{
	$db = rg_sql_open_nodelay($h);
	if ($db === FALSE)
		return FALSE;

	return pg_escape_string($db, $str);
}

/*
 * Helper for sql_query and sql_query_params
 */
function rg_sql_query0($db, $sql, $r, $start_ts, $ignore, &$ignore_kicked)
{
	global $rg_sql_debug;

	$ignore_kicked = FALSE;
	while (1) {
		if ($r !== TRUE) {
			$err = "$sql: send: " . @pg_last_error($db);
			$res = FALSE;
			break;
		}

		$res = @pg_get_result($db);
		if ($res === FALSE) {
			$err = $sql . ': get: no pending query';
			break;
		}

		$state = rg_sql_last_error_code($res);
		if ($state === FALSE) {
			$err = $sql . ': get: pg_result_error_field error';
			break;
		}
		if (($state !== NULL) && (strcmp($state, '00000') !== 0)) {
			foreach ($ignore as $code) {
				if (strcmp($code, $state) == 0) {
					$ignore_kicked = TRUE;
					break;
				}
			}

			if ($ignore_kicked)
				if ($rg_sql_debug > 50)
					rg_log('DB: We should ignore the error!');

			$err = $sql . ': ' . @pg_last_error($db) . ' (' . $state . ')';
			@pg_free_result($res);
			$res = FALSE;
			break;
		}

		$diff = sprintf("%u", (microtime(TRUE) - $start_ts) * 1000);
		$rows = rg_sql_num_rows($res);
		if ($rows == 0)
			$arows = rg_sql_affected_rows($res);
		else
			$arows = 0;

		if ($rg_sql_debug > 0)
			rg_log("DB: Took " . $diff . "ms, $rows row(s), $arows affected");

		rg_prof_set(array("q" => 1,
			"rows" => $rows,
			"arows" => $arows,
			"q_ms" => $diff));
		break;
	}

	if ($res === FALSE) {
		rg_sql_set_error($err);
		if (!$ignore_kicked) {
			rg_internal_error($err);
			rg_prof_set(array('qerrors' => 1));
		}
		// reconnect if needed
		@pg_ping($db);
	}

	return $res;

}

/*
 * Do a query
 */
function rg_sql_query($h, $sql)
{
	global $rg_sql_debug;

	if ($rg_sql_debug > 0)
		rg_log_enter("sql_query: sql=$sql");

	$ret = FALSE;
	while (1) {
		$db = rg_sql_open_nodelay($h);
		if ($db === FALSE)
			break;

		$ignore = array();
		$start_ts = microtime(TRUE);
		$r = @pg_send_query($db, $sql);
		$ret = rg_sql_query0($db, $sql, $r, $start_ts,
			$ignore, $ignore_kicked);
		break;
	}

	if ($rg_sql_debug > 0)
		rg_log_exit();
	return $ret;
}

/*
 * Queries using params
 * @params - array of fields -> values
 * @ignore - array of strings with errors we should not log as internal errors
 *	See https://www.postgresql.org/docs/current/static/errcodes-appendix.html
 * @ignore_kicked will be set to true if the error is in @ignore array
 * Examples: $params = array("id" => "1", "name" = "bau")
 *	$sql = "UPDATE x SET name = @@name@@ WHERE id = @@id@@ AND @@name@@ = @@name@@"
 *	=> $sql2 = "UPDATE x SET name = $1 WHERE id = $2 AND name = $1"
 */
function rg_sql_query_params_ignore($h, $sql, $params, $ignore, &$ignore_kicked)
{
	global $rg_sql_debug;

	if ($rg_sql_debug > 0)
		rg_log_enter('sql_query_params: sql=[' . $sql . ']'
			. ' params=[' . rg_array2string($params) . ']'
			. ' ignore=' . implode(',', $ignore));

	$ret = FALSE;
	while (1) {
		$db = rg_sql_open_nodelay($h);
		if ($db === FALSE)
			break;

		// Transforms @params into $x system
		$params2 = array();
		$i = 1;
		foreach ($params as $k => $v) {
			$what = '@@' . $k . '@@';
			$value = '$' . $i;
			$sql = str_replace($what, $value, $sql, $count);

			//rg_log("rg_sql_query_params: k=[$k] value=$value count=$count");
			if ($count > 0) {
				$params2[] = $v;
				$i++;
			}
		}
		//rg_log("new sql: $sql");
		//rg_log("params2: " . rg_array2string($params2));

		$start_ts = microtime(TRUE);
		$r = @pg_send_query_params($db, $sql, $params2);
		$ret = rg_sql_query0($db, $sql, $r, $start_ts, $ignore, $ignore_kicked);
		break;
	}

	if ($rg_sql_debug > 0)
		rg_log_exit();
	return $ret;
}

function rg_sql_query_params($h, $sql, $params)
{
	$ignore = array();
	return rg_sql_query_params_ignore($h, $sql, $params,
		$ignore, $ignore_kicked);
}

/*
 * Close database
 */
function rg_sql_close($h)
{
	global $rg_sql_conn;

	if (!isset($rg_sql_conn[$h])) {
		rg_internal_error('Handler ' . $h . ' was not allocated!');
		return FALSE;
	}

	if (!isset($rg_sql_conn[$h]['db'])) {
		// was not opened
		return TRUE;
	}

	$r = pg_close($rg_sql_conn[$h]['db']);
	if ($r === FALSE)
		return FALSE;

	unset($rg_sql_conn[$h]['db']);

	return TRUE;
}

/*
 * Free results
 */
function rg_sql_free_result($res)
{
	if (!is_resource($res)) {
		rg_internal_error("res is not resource!");
		return;
	}

	pg_free_result($res);
}

/*
 * Returns a row as an associated array
 */
function rg_sql_fetch_array($res)
{
	if (!is_resource($res)) {
		rg_internal_error("res is not resource!");
		return FALSE;
	}

	return pg_fetch_assoc($res);
}

function rg_sql_last_id($h)
{
	$sql = "SELECT lastval() AS id";
	$res = rg_sql_query($h, $sql);
	if ($res === FALSE)
		return FALSE;

	$row = rg_sql_fetch_array($res);
	rg_sql_free_result($res);
	return $row['id'];
}

function rg_sql_num_rows($res)
{
	if (!is_resource($res)) {
		rg_internal_error("res is not resource!");
		return FALSE;
	}

	return pg_num_rows($res);
}

function rg_sql_affected_rows($res)
{
	if (!is_resource($res)) {
		rg_internal_error("res is not resource!");
		return FALSE;
	}

	return pg_affected_rows($res);
}

function rg_sql_begin($h)
{
	$res = rg_sql_query($h, "BEGIN");
	if ($res === FALSE)
		return FALSE;

	rg_sql_free_result($res);
	return TRUE;
}

function rg_sql_commit($h)
{
	$res = rg_sql_query($h, "COMMIT");
	if ($res === FALSE)
		return FALSE;

	rg_sql_free_result($res);
	return TRUE;
}

function rg_sql_rollback($h)
{
	$res = rg_sql_query($h, "ROLLBACK");
	if ($res === FALSE)
		return FALSE;

	rg_sql_free_result($res);
	return TRUE;
}

/*
 * Test if a table exists
 * Returns FALSE on error, 0 if does not exists, 1 if exists
 */
function rg_sql_rel_exists($h, $rel)
{
	$sql = "SELECT 1 FROM pg_class"
		. " WHERE relname = '" . $rel . "'";
	$res = rg_sql_query($h, $sql);
	if ($res === FALSE)
		return FALSE;

	$rows = rg_sql_num_rows($res);
	rg_sql_free_result($res);

	return $rows;
}

/*
 * Returns the fileds names of a table
 */
function rg_sql_fields($h, $table)
{
	$params = array('table' => $table);
	$sql = 'SELECT column_name FROM information_schema.columns'
		. ' WHERE table_name = @@table@@';
	$res = rg_sql_query_params($h, $sql, $params);
	if ($res === FALSE)
		return FALSE;

	$ret = array();
	while (($row = rg_sql_fetch_array($res))) {
		$f = $row['column_name'];
		$ret[$f] = 1;
	}
	rg_sql_free_result($res);

	return $ret;
}

/*
 * Returns the last error codes
 */
function rg_sql_last_error_code($res)
{
	return @pg_result_error_field($res, PGSQL_DIAG_SQLSTATE);
}

?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master