<?php
/**************************************************************************
* This file is part of the WebIssues Server program
* Copyright (C) 2006 Michał Męciński
* Copyright (C) 2007-2009 WebIssues Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
**************************************************************************/

function wi_db_connect( $host, $database, $user, $password )
{
    if ( !mysql_connect( $host, $user, $password ) )
        return false;

    if ( !mysql_select_db( $database ) ) {
        trigger_error( 'mysql_select_db(): ' . mysql_error(), E_USER_WARNING );
        return false;
    }

    if ( version_compare( mysql_get_server_info(), '4.1', '>=' ) )
        mysql_query( "SET NAMES 'utf8'" );

    return true;
}

function wi_db_escape_arg( $arg, $type, &$params )
{
    switch( $type ) {
        case 'd':
            return (int)$arg;
        case 's':
        case 'b':
            return "'" . mysql_escape_string( $arg ) . "'";
    }
}

function wi_db_query( $query, $params )
{
    $log = wi_log_open( 'sql' );
    if ( $log )
        fwrite( $log, "> $query\n" );

    $rs = mysql_query( $query );

    if ( !$rs ) {
        $error = mysql_error();
        trigger_error( 'mysql_query(): ' . $error, E_USER_WARNING );
        if ( $log )
            fwrite( $log, "ERROR: $error\n" );
        return false;
    }

    if ( $log ) {
        if ( $rs === true ) {
            $num = mysql_affected_rows();
            fwrite( $log, "($num rows affected)\n" );
        } else {
            $num = mysql_num_rows( $rs );
            fwrite( $log, "($num rows returned)\n" );
        }
    }

    return $rs;
}

function wi_db_fetch_assoc( $rs )
{
    return mysql_fetch_assoc( $rs );
}

function wi_db_unescape_blob( $data )
{
    return $data;
}

function wi_db_insert_id( $table, $column )
{
    return mysql_insert_id();
}

function wi_db_table_exists( $table )
{
    $query = "SHOW TABLES LIKE '$table'";
    $rs = mysql_query( $query );
    return mysql_num_rows( $rs ) > 0;
}
