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 (dbbd92bb3dd984151bdcf645fd1a7fd8dc7b4604) (2,989B) (mode 100644) [raw]
<?php
require_once($INC . "/log.inc.php");
require_once($INC . "/prof.inc.php");

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

$rg_sql_conn = array();

$rg_sql_error = "";

/*
 * Set error string
 */
function rg_sql_set_error($str)
{
	global $rg_sql_error;
	$rg_sql_error = $str;
}

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

/*
 * Connect to database
 */
function rg_sql_open($str)
{
	global $rg_sql_debug;
	global $rg_sql_conn;

	if (isset($rg_sql_conn[$str]))
		return $rg_sql_conn[$str];

	if ($rg_sql_debug > 0)
		rg_log("DB: opening [$str]...");

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

	$_s = microtime(TRUE);
	$db = @pg_pconnect($str);
	$diff = sprintf("%u", (microtime(TRUE) - $_s) * 1000);
	rg_prof_set(array("db_conn_time_ms" => $diff));
	if ($db === FALSE) {
		rg_sql_set_error("cannot connect to database (" . $php_errormsg . ")");
		rg_prof_set(array("db_conn_errors" => 1));
		return FALSE;
	}

	$rg_sql_conn[$str] = $db;

	return $db;
}

/*
 * Escaping
 */
function rg_sql_escape($db, $str)
{
	return pg_escape_string($db, $str);
}

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

	if ($rg_sql_debug > 0)
		rg_log("\tDB: running [$sql]...");

	$_s = microtime(TRUE);
	$res = @pg_query($db, $sql);
	if ($res === FALSE) {
		rg_sql_set_error("$sql: " . @pg_last_error($db));
		rg_prof_set(array("query_errors" => 1));
		return FALSE;
	}

	$diff = sprintf("%u", (microtime(TRUE) - $_s) * 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("\tDB: Took " . $diff . "ms, $rows row(s), $arows affected");

	rg_prof_set(array("queries" => 1,
		"rows" => $rows,
		"affected_rows" => $arows,
		"query_time_ms" => $diff));

	return $res;
}

/*
 * Close database
 */
function rg_sql_close($db)
{
	pg_close($db);
}

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

/*
 * Returns a row as an associated array
 */
function rg_sql_fetch_array($res)
{
	return pg_fetch_assoc($res);
}

function rg_sql_last_id($db)
{
	$sql = "SELECT lastval() AS id";
	$res = rg_sql_query($db, $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)
{
	return pg_num_rows($res);
}

function rg_sql_affected_rows($res)
{
	return pg_affected_rows($res);
}

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

	rg_sql_free_result($res);
	return TRUE;
}

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

	rg_sql_free_result($res);
	return TRUE;
}

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

	rg_sql_free_result($res);
	return TRUE;
}

?>
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