File: database-mssql.inc.php

package info (click to toggle)
webissues-server 0.8.3-2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 392 kB
  • ctags: 610
  • sloc: php: 2,206; sql: 458; sh: 44; makefile: 9
file content (131 lines) | stat: -rw-r--r-- 3,281 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**************************************************************************
* This file is part of the WebIssues Server program
* Copyright (C) 2006 Michał Męciński
* Copyright (C) 2007-2008 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 )
{
    global $db_link;

    try {
        $db_link = new COM( 'ADODB.Connection', null, CP_UTF8 );
    } catch ( com_exception $ex ) {
        trigger_error( 'Could not create the ADODB.Connection object', E_USER_WARNING );
        return false;
    }

    $string = "Provider=sqloledb; Data Source=$host; Initial Catalog=$database";
    if ( $user != '' )
        $string .= "; User ID=$user; Password=$password";
    else
        $string .= "; Trusted_Connection=yes";

    try {
        $db_link->Open( $string );
    } catch ( com_exception $ex ) {
        trigger_error( $db_link->Errors[ 0 ], E_USER_WARNING );
        return false;
    }

    return true;
}

function wi_db_escape_arg( $arg, $type )
{
    switch( $type ) {
        case 'd':
            return (int)$arg;
        case 's':
            return "N'" . str_replace( "'", "''", $arg ) . "'";
        case 'b':
            return '0x' . bin2hex( $arg );
    }
}

function wi_db_query( $query )
{
    global $db_link;

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

    try {
        $rs = $db_link->Execute( $query );
    } catch ( com_exception $ex ) {
        $msg = $db_link->Errors[ 0 ];
        trigger_error( $msg, E_USER_WARNING );
        if ( $log )
            fwrite( $log, "ERROR: $msg\n" );
        return false;
    }

    if ( $log ) {
        if ( $rs->State == 0 )
            fwrite( $log, "(no result)\n");
        else if ( $rs->EOF )
            fwrite( $log, "(empty result)\n");
        else
            fwrite( $log, "(one or more rows returned)\n");
    }

    if ( $rs->State == 0 )
        return true;

    return $rs;
}

function wi_db_fetch_assoc( $rs )
{
    if( $rs->EOF )
        return false;

    $row = array();

    for( $i = 0; $i < $rs->Fields->Count; $i++ )
        $row[ $rs->Fields[ $i ]->Name ] = $rs->Fields[ $i ]->Value;

    $rs->MoveNext();

    return $row;
}

function wi_db_unescape_blob( $data )
{
    // NOTE: $data is a VARIANT of type VT_ARRAY | VT_UI1
    // casting it to a string will truncate it on the first \0 character
    // so the string must be built from individual characters

    $length = count( $data );
    $string = '';

    for ( $i = 0; $i < $length; $i++ )
        $string .= chr( $data[ $i ] );

    return $string;
}

function wi_db_insert_id( $table, $column )
{
    global $db_link;

    $query = "SELECT @@IDENTITY";
    $rs = $db_link->Execute( $query );
    return $rs->Fields[ 0 ]->Value;
}

function wi_db_table_exists( $table )
{
    global $db_link;

    $query = "SELECT OBJECT_ID('$table', 'U')";
    $rs = $db_link->Execute( $query );
    return $rs->Fields[ 0 ]->Value != null;
}