<commit_before><?php

require_once "config.php";

function db_connect($host, $user, $pass, $db) {
	if (DB_TYPE == "pgsql") {

		$string = "dbname=$db user=$user";

		if ($pass) {
			$string .= " password=$pass";
		}

		if ($host) {
			$string .= " host=$host";
		}

		if (defined('DB_PORT') && DB_PORT) {
			$string = "$string port=" . DB_PORT;
		}

		$link = pg_connect($string);

		if (!$link) {
			die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
		}

		return $link;

	} else if (DB_TYPE == "mysql") {
		$link = mysql_connect($host, $user, $pass);
		if ($link) {
			$result = mysql_select_db($db, $link);
			if (!$result) {
				die("Can't select DB: " . mysql_error($link));
			}
			return $link;
		} else {
			die("Unable to connect to database (as $user to $host, database $db): " . mysql_error());
		}
	}
}

function db_escape_string($link, $s, $strip_tags = true) {
	if ($strip_tags) $s = strip_tags($s);

	if (DB_TYPE == "pgsql") {
		return pg_escape_string($link, $s);
	} else {
		return mysql_real_escape_string($s, $link);
	}
}

function db_query($link, $query, $die_on_error = true) {
	if (DB_TYPE == "pgsql") {
		$result = pg_query($link, $query);
		if (!$result) {
			$query = htmlspecialchars($query); // just in case
			if ($die_on_error) {
				die("Query <i>$query</i> failed [$result]: " . ($link ? pg_last_error($link) : "No connection"));
			}
		}
		return $result;
	} else if (DB_TYPE == "mysql") {
		$result = mysql_query($query, $link);
		if (!$result) {
			$query = htmlspecialchars($query);
			if ($die_on_error) {
				die("Query <i>$query</i> failed: " . ($link ? mysql_error($link) : "No connection"));
			}
		}
		return $result;
	}
}

function db_fetch_assoc($result) {
	if (DB_TYPE == "pgsql") {
		return pg_fetch_assoc($result);
	} else if (DB_TYPE == "mysql") {
		return mysql_fetch_assoc($result);
	}
}


function db_num_rows($result) {
	if (DB_TYPE == "pgsql") {
		return pg_num_rows($result);
	} else if (DB_TYPE == "mysql") {
		return mysql_num_rows($result);
	}
}

function db_fetch_result($result, $row, $param) {
	if (DB_TYPE == "pgsql") {
		return pg_fetch_result($result, $row, $param);
	} else if (DB_TYPE == "mysql") {
		// I hate incoherent naming of PHP functions
		return mysql_result($result, $row, $param);
	}
}

function db_unescape_string($str) {
	$tmp = str_replace("\\\"", "\"", $str);
	$tmp = str_replace("\\'", "'", $tmp);
	return $tmp;
}

function db_close($link) {
	if (DB_TYPE == "pgsql") {

		return pg_close($link);

	} else if (DB_TYPE == "mysql") {
		return mysql_close($link);
	}
}

function db_affected_rows($link, $result) {
	if (DB_TYPE == "pgsql") {
		return pg_affected_rows($result);
	} else if (DB_TYPE == "mysql") {
		return mysql_affected_rows($link);
	}
}

function db_last_error($link) {
	if (DB_TYPE == "pgsql") {
		return pg_last_error($link);
	} else if (DB_TYPE == "mysql") {
		return mysql_error($link);
	}
}

function db_quote($str){
	return("'$str'");
}

?><commit_msg>Add support for mysqli native PHP driver<commit_after><?php

require_once "config.php";

function db_connect($host, $user, $pass, $db) {
	switch(DB_TYPE) {
		case "pgsql":
			$string = "dbname=$db user=$user";
			
			if ($pass) {
				$string .= " password=$<PASSWORD>";
			}
			
			if ($host) {
				$string .= " host=$host";
			}
			
			if (defined('DB_PORT') && DB_PORT) {
				$string = "$string port=" . DB_PORT;
			}
			
			$link = pg_connect($string);
			
			if (!$link) {
				die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
			}
			
			return $link;
		break;
		case "mysql":
			$link = mysql_connect($host, $user, $pass);
			if ($link) {
				$result = mysql_select_db($db, $link);
				if (!$result) {
					die("Can't select DB: " . mysql_error($link));
				}
				return $link;
			} else {
				die("Unable to connect to database (as $user to $host, database $db): " . mysql_error());
			}
		break;
		case "mysqli":
			// The hostname localhost has a special meaning. It is bound to the
			// use of Unix domain sockets. It is not possible to open a TCP/IP
			// connection using the hostname localhost you must use 127.0.0.1
			// instead.
			$link = new mysqli($host, $user, $pass, $db, 3306); // @todo Declare $port = 3306; and avoid hard coding here
			$link = mysql_connect($host, $user, $pass);
			
			$link->report_mode = MYSQLI_REPORT_ALL; // MYSQLI_REPORT_STRICT
			
			if ($link->connect_errno) {
				die("Unable to connect to database (as $user to $host, database $db): ERROR[" . $link->connect_errno . "] " . $link->connect_error);
			} else {
				return $link;
			}
		break;
		case "pdo": // @todo
		break;
	}
}

function db_escape_string($link, $s, $strip_tags = true) {
	if ($strip_tags) $s = strip_tags($s);

	switch(DB_TYPE) {
		case "pgsql":
			return pg_escape_string($link, $s);
		break;
		case "mysql":
			return mysql_real_escape_string($s, $link);
		break;
		case "mysqli":
			return $link->real_escape_string($s);
		break;
		case "pdo": // @todo
		break;
	}
}

function db_query($link, $query, $die_on_error = true) {
	switch(DB_TYPE) {
		case "pgsql":
			$result = pg_query($link, $query);
			if (!$result) {
				$query = htmlspecialchars($query); // just in case
				if ($die_on_error) {
					die("Query <i>$query</i> failed [$result]: " . ($link ? pg_last_error($link) : "No connection"));
				}
			}
			return $result;
		break;
		case "mysql":
			$result = mysql_query($query, $link);
			if (!$result) {
				$query = htmlspecialchars($query);
				if ($die_on_error) {
					die("Query <i>$query</i> failed: " . ($link ? mysql_error($link) : "No connection"));
				}
			}
			return $result;
		break;
		case "mysqli":
			if(!$result) {
				$query = htmlspecialchars($query);
				if ($die_on_error) {
					die("Query <i>$query</i> failed: Error[" . $link->errno . "]: " . $link->error);
				}
			}

			return $result;
		break;
		case "pdo": // @todo
		break;
	}
}

function db_fetch_assoc($result) {
	switch(DB_TYPE) {
		case "pgsql":
			return pg_fetch_assoc($result);
		break;
		case "mysql":
			return mysql_fetch_assoc($result);
		break;
		case "mysqli":
			return $result->fetch_assoc();
		break;
		case "pdo": // @todo
		break;
	}
}

function db_num_rows($result) {
	switch(DB_TYPE) {
		case "pgsql":
			return pg_num_rows($result);
		break;
		case "mysql":
			return mysql_num_rows($result);
		break;
		case "mysqli":
			return $result->num_rows;
		break;
		case "pdo": // @todo
		break;
	}
}

function db_fetch_result($result, $row, $param) {
	switch(DB_TYPE) {
		case "pgsql":
			return pg_fetch_result($result, $row, $param);
		break;
		case "mysql":
			// I hate incoherent naming of PHP functions
			return mysql_result($result, $row, $param);
		break;
		case "mysqli":
			if($result->num_rows == 0) {
				return FALSE;
			}
			$result->data_seek($row);
			$data = $result->fetch_assoc();
			return $data[$param];
		break;
		case "pdo": // @todo
		break;
	}
}

function db_unescape_string($str) {
	$tmp = str_replace("\\\"", "\"", $str);
	$tmp = str_replace("\\'", "'", $tmp);
	return $tmp;
}

function db_close($link) {
	switch(DB_TYPE) {
		case "pgsql":
			return pg_close($link);
		break;
		case "mysql":
			return mysql_close($link);
		break;
		case "mysqli":
			// Per process for PHP 5.3, no needed PHP 5.4
			$link->report_mode = MYSQLI_REPORT_OFF;
			
			$link->close();
		break;
		case "pdo": // @todo
		break;
	}
}

function db_affected_rows($link, $result) {
	switch(DB_TYPE) {
		case "pgsql":
			return pg_affected_rows($result);
		break;
		case "mysql":
			return mysql_affected_rows($link);
		break;
		case "mysqli":
			return $link->affected_rows;
		break;
		case "pdo": // @todo
		break;
	}
}

function db_last_error($link) {
	switch(DB_TYPE) {
		case "pgsql":
			return pg_last_error($link);
		break;
		case "mysql":
			return mysql_error($link);
		break;
		case "mysqli":
			return $link->error;
		break;
		case "pdo": // @todo
		break;
	}
}

function db_quote($str){
	return("'$str'");
}
{'fraction_non_alphanumeric': 0.1832309813259807, 'fraction_numerical': 0.0023812507833061787, 'mean_word_length': 3.455611390284757, 'pattern_counts': {'":': 36, '<': 16, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1146864', 'n_tokens_mistral': 3477, 'n_tokens_neox': 3048, 'n_words': 707}