/inc/sql.inc.php (10abc865c9d5c395829c939b39139d50504d9eea) (7151 bytes) (mode 100644) (type blob)

<?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;
	rg_log($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 $php_errormsg;
	global $rg_sql_debug;
	global $rg_sql_conn;

	rg_prof_start("sql_open_nodelay");

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

		if (isset($rg_sql_conn[$h]['db'])) {
			$ret = $rg_sql_conn[$h]['db'];
			break;
		}

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

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

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

		$_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;
			}
		}
		$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;
	}

	rg_prof_end("sql_open_nodelay");
	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();
	$rg_sql_conn[$free_index]['str'] = $str;
	$rg_sql_conn[$free_index]['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, $res, $_s)
{
	global $rg_sql_debug;

	while (1) {
		if ($res === FALSE) {
			$err = "$sql: " . @pg_last_error($db);
			rg_sql_set_error($err);
			rg_internal_error($err);
			rg_prof_set(array('qerrors' => 1));
			// reconnect if needed
			@pg_ping($db);
			break;
		}

		$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("DB: Took " . $diff . "ms, $rows row(s), $arows affected");

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

	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;

		$_s = microtime(TRUE);
		$res = @pg_query($db, $sql);
		$ret = rg_sql_query0($db, $sql, $res, $_s);
		break;
	}

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

/*
 * Queries using params
 * @params - array of fields -> values
 * 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($h, $sql, $params)
{
	global $rg_sql_debug;

	if ($rg_sql_debug > 0)
		rg_log_enter("query_params: running [$sql] with [" . rg_array2string($params) . "]");

	$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));

		$_s = microtime(TRUE);
		$res = @pg_query_params($db, $sql, $params2);
		$ret = rg_sql_query0($db, $sql, $res, $_s);
		break;
	}

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

/*
 * Close database
 */
function rg_sql_close($h)
{
	// TODO: why should I connect before close?!
	$db = rg_sql_open_nodelay($h);
	if ($db === FALSE)
		return FALSE;

	return pg_close($db);
}

/*
 * 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($db, $rel)
{
	$sql = "SELECT 1 FROM pg_class"
		. " WHERE relname = '" . $rel . "'";
	$res = rg_sql_query($db, $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($db, $table)
{
	$params = array('table' => $table);
	$sql = 'SELECT column_name FROM information_schema.columns'
		. ' WHERE table_name = @@table@@';
	$res = rg_sql_query_params($db, $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;
}

?>


Mode Type Size Ref File
100644 blob 9 f3c7a7c5da68804a1bdf391127ba34aed33c3cca .exclude
100644 blob 102 eaeb7d777062c60a55cdd4b5734902cdf6e1790c .gitignore
100644 blob 289 fabbff669e768c05d6cfab4d9aeb651bf623e174 AUTHORS
100644 blob 1132 dd65951315f3de6d52d52a82fca59889d1d95187 Certs.txt
100644 blob 549 41c3bdbba8ec2523fe24b84bdd46777fc13e8345 History.txt
100644 blob 34520 dba13ed2ddf783ee8118c6a581dbf75305f816a3 LICENSE
100644 blob 3398 cf75b360b8a3e6ef86bc4a42648e353bd58c2a80 Makefile.in
100644 blob 5774 4a18249bf06d04d1e27d97623f12a7a2d51f83c0 README
100644 blob 118763 259c7b1871cb204da34bc36f9b635efb208c8a40 TODO
100644 blob 1294 f22911eb777f0695fcf81ad686eac133eb11fcc4 TODO-plans
100644 blob 203 a2863c67c3da44126b61a15a6f09738c25e0fbe0 TODO.perf
100644 blob 1044 9bb3652b3937eb624dba0f2d8efff7ce6c0ce0e2 TODO.vm
040000 tree - 21928e906ad2907a55c2e81c2a8b0502b586b8a0 artwork
100644 blob 4650 548f8c18609fa92b720aebfa5433f50a2c4ced78 compare.csv
100755 blob 30 92c4bc48245c00408cd7e1fd89bc1a03058f4ce4 configure
040000 tree - 69114e8648f8e0e7173c76e30ca6bbfcece7df31 debian
040000 tree - afac05b52ba7b5cd8de8a5141ab143b01cad8e46 docker
040000 tree - f67d3605efbd6422a8acdd953578991139266391 docs
100755 blob 16720 52405deef0d3708e7553022e1e9db73faa28d05c duilder
100644 blob 536 96c75f943c5bf93b54dbddf678e8a99d7ba4ff93 duilder.conf
040000 tree - c503cf29ce2337a771fdfdbf4225b35d8e81ab98 hooks
040000 tree - dddbdd6b3e3f98f12622b28a544c5e05febe1d24 inc
040000 tree - ab5cc695f620de9abecc84af49866a45612067c6 misc
100644 blob 3742 1d19fcac5abc96f5aa3412ee9ee4e7f0dfe0bc08 rocketgit.spec.in
040000 tree - 1b971d1b8f26cf4a14ee7ab74006cd4285e99ed6 root
040000 tree - edfd5fcdabcb2a987269d8167dfe8d02eebe3e19 samples
040000 tree - a2ceaf5273e4bd4d74691afae72ec570de83a3c0 scripts
040000 tree - 00c52dce99b99f5f59800512ffd8e145d5ffe2c9 selinux
100755 blob 256 462ccd108c431f54e380cdac2329129875a318b5 spell_check.sh
040000 tree - cb54e074b3ca35943edfcda9dd9cfcd281bcd9e7 techdocs
040000 tree - 5b464ef56a79806751442be1ddd01681571cd7fd tests
040000 tree - 63f68e921ac8d6a62ea9c3d180e072c7c4725b7d tools
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://rocketgit.com/user/catalinux/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/catalinux/rocketgit

Clone this repository using git:
git clone git://git.rocketgit.com/user/catalinux/rocketgit

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