Location: PHPKode > projects > Course-manager > courseman-0.1.1/sql.php
<?php
  $prestring = $postgresql_8_strings ? 'E' : '';
  function database_connection($failure_notice) {
    global $database, $database_user, $debug, $debug_connection;
    $connection = pg_connect("dbname=$database user=$database_user");
    if (!$connection) {
      print($failure_notice);
      exit;
    }
    if ($debug['sql']) {
      $debug_connection = pg_connect("user=$database_user dbname=$database");
    }
    return $connection;
  }
  function db_exec($connection, $query) {
    global $debug, $database, $database_user, $debug_connection;
    if ($debug['sql']) {
      pg_exec($debug_connection, "INSERT INTO sql_log (query) "
      		. "SELECT $prestring'" . pg_escape_string($query) . "';");
    }
    return pg_exec($connection, $query);
  }
  function db_close($connection) {
    global $debug, $debug_connection;
    pg_close($connection);
    if ($debug['sql']) {
      pg_close($debug_connection);
    }
  }
  function column_constrain($field, $value) {
    if ($value) {
      return "$field=$prestring'" . pg_escape_string($value) . "'";
    } else {
      return "$field IS null";
    }
  }
  function exact_join($ignored, $table_data) {
    return delimited_array_elements(
		    map_function_using_key(
				column_constrain, $table_data), "
			AND ");
  }
  function lock_table_key_with_registration($table, $ignored) {
    global $verbindung, $table_registration, $debug;
    db_exec($verbindung, "LOCK TABLE $table;");
    if ($debug['sql']) {
?>
<pre>
<?="LOCK TABLE "
				. $table_registration[$table]['table']
				. ";"?>
</pre>
<?php
    }
    db_exec($verbindung, "LOCK TABLE "
    			       . $table_registration[$table]['table']
    			       . ";");
  }
  function insert_data_simple($table_name, $table_data) {
    global $verbindung;
    $fields = array();
    $values = array();
    foreach ($table_data as $field => $value) {
      if ($value) {
        array_push($fields, $field);
        array_push($values, "$prestring'" . pg_escape_string($value) . "'");
      }
    }
    if ($fields) {
      db_exec($verbindung, "INSERT
	INTO $table_name (" . delimited_array_elements($fields, ", ") . ")
	SELECT " . delimited_array_elements($values, ", ") . "
	WHERE NOT EXISTS (
		SELECT true FROM $table_name
			WHERE " . exact_join($table_name, $table_data) . ");");
    }
  }
  function insert_data_predefined($table_name, $table_data) {
  }
  function insert_data($table_name, $table_data) {
    global $table_registration;
    $table_registration[$table_name]['insertion']($table_name, $table_data);
  }
  function register_data_predefined($table_name,
			            $registration_column,
			            $registration_table_name,
			            $table_column,
			            $data_registration_column,
			            $registration_id,
			            $table_data) {
    global $verbindung;
    $fields = array();
    $values = array();
    foreach ($table_data as $field => $value) {
      array_push($fields, $field);
      array_push($values, intval($value));
    }
    db_exec($verbindung, "INSERT
	INTO $registration_table_name ($registration_column,
                                       " . delimited_array_elements($fields, ",
                                       ") . ")
	SELECT $registration_id,
               " . delimited_array_elements($values, ",
               ") . ";");
  }
  function register_data_simple($table_name,
			        $registration_column,
			        $registration_table_name,
			        $table_column,
			        $data_registration_column,
			        $registration_id,
			        $table_data) {
    global $verbindung;
    db_exec($verbindung, "INSERT
	INTO $registration_table_name ($registration_column,
                                       $data_registration_column)
	SELECT $registration_id, $table_column
		FROM $table_name
		WHERE " . delimited_array_elements(
					map_function_using_key(
						column_constrain,
						$table_data), "
		AND ") . ";");
  }
  function register_data($table_name,
                         $registration_column,
                         $registration_table_name,
                         $table_column,
                         $data_registration_column,
                         $registration_id,
                         $table_data) {
    global $table_registration;
    $table_registration[$table_name]['registration_function'](
                                                     $table_name,
                                                     $registration_column,
                                                     $registration_table_name,
                                                     $table_column,
                                                     $data_registration_column,
                                                     $registration_id,
                                                     $table_data);
  }
?>
Return current item: Course-manager