Subversion Repositories Applications.gtt

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
Line 4... Line 4...
4
 
4
 
5
/**
5
/**
6
 * The PEAR DB driver for PHP's mssql extension
6
 * The PEAR DB driver for PHP's mssql extension
7
 * for interacting with Microsoft SQL Server databases
7
 * for interacting with Microsoft SQL Server databases
8
 *
8
 *
9
 * PHP versions 4 and 5
9
 * PHP version 5
10
 *
10
 *
11
 * LICENSE: This source file is subject to version 3.0 of the PHP license
11
 * LICENSE: This source file is subject to version 3.0 of the PHP license
12
 * that is available through the world-wide-web at the following URI:
12
 * that is available through the world-wide-web at the following URI:
13
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
13
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
Line 16... Line 16...
16
 *
16
 *
17
 * @category   Database
17
 * @category   Database
18
 * @package    DB
18
 * @package    DB
19
 * @author     Sterling Hughes <sterling@php.net>
19
 * @author     Sterling Hughes <sterling@php.net>
20
 * @author     Daniel Convissor <danielc@php.net>
20
 * @author     Daniel Convissor <danielc@php.net>
21
 * @copyright  1997-2005 The PHP Group
21
 * @copyright  1997-2007 The PHP Group
22
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
22
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
23
 * @version    CVS: $Id: mssql.php,v 1.83 2005/03/07 18:24:51 danielc Exp $
23
 * @version    CVS: $Id$
24
 * @link       http://pear.php.net/package/DB
24
 * @link       http://pear.php.net/package/DB
25
 */
25
 */
Line 26... Line 26...
26
 
26
 
27
/**
27
/**
Line 33... Line 33...
33
 * The methods PEAR DB uses to interact with PHP's mssql extension
33
 * The methods PEAR DB uses to interact with PHP's mssql extension
34
 * for interacting with Microsoft SQL Server databases
34
 * for interacting with Microsoft SQL Server databases
35
 *
35
 *
36
 * These methods overload the ones declared in DB_common.
36
 * These methods overload the ones declared in DB_common.
37
 *
37
 *
-
 
38
 * DB's mssql driver is only for Microsfoft SQL Server databases.
-
 
39
 *
-
 
40
 * If you're connecting to a Sybase database, you MUST specify "sybase"
-
 
41
 * as the "phptype" in the DSN.
-
 
42
 *
-
 
43
 * This class only works correctly if you have compiled PHP using
-
 
44
 * --with-mssql=[dir_to_FreeTDS].
-
 
45
 *
38
 * @category   Database
46
 * @category   Database
39
 * @package    DB
47
 * @package    DB
40
 * @author     Sterling Hughes <sterling@php.net>
48
 * @author     Sterling Hughes <sterling@php.net>
41
 * @author     Daniel Convissor <danielc@php.net>
49
 * @author     Daniel Convissor <danielc@php.net>
42
 * @copyright  1997-2005 The PHP Group
50
 * @copyright  1997-2007 The PHP Group
43
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
51
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
44
 * @version    Release: 1.7.6
52
 * @version    Release: 1.9.2
45
 * @link       http://pear.php.net/package/DB
53
 * @link       http://pear.php.net/package/DB
46
 */
54
 */
47
class DB_mssql extends DB_common
55
class DB_mssql extends DB_common
48
{
56
{
49
    // {{{ properties
57
    // {{{ properties
Line 87... Line 95...
87
     * A mapping of native error codes to DB error codes
95
     * A mapping of native error codes to DB error codes
88
     * @var array
96
     * @var array
89
     */
97
     */
90
    // XXX Add here error codes ie: 'S100E' => DB_ERROR_SYNTAX
98
    // XXX Add here error codes ie: 'S100E' => DB_ERROR_SYNTAX
91
    var $errorcode_map = array(
99
    var $errorcode_map = array(
-
 
100
        102   => DB_ERROR_SYNTAX,
92
        110   => DB_ERROR_VALUE_COUNT_ON_ROW,
101
        110   => DB_ERROR_VALUE_COUNT_ON_ROW,
93
        155   => DB_ERROR_NOSUCHFIELD,
102
        155   => DB_ERROR_NOSUCHFIELD,
-
 
103
        156   => DB_ERROR_SYNTAX,
94
        170   => DB_ERROR_SYNTAX,
104
        170   => DB_ERROR_SYNTAX,
95
        207   => DB_ERROR_NOSUCHFIELD,
105
        207   => DB_ERROR_NOSUCHFIELD,
96
        208   => DB_ERROR_NOSUCHTABLE,
106
        208   => DB_ERROR_NOSUCHTABLE,
97
        245   => DB_ERROR_INVALID_NUMBER,
107
        245   => DB_ERROR_INVALID_NUMBER,
-
 
108
        319   => DB_ERROR_SYNTAX,
-
 
109
        321   => DB_ERROR_NOSUCHFIELD,
-
 
110
        325   => DB_ERROR_SYNTAX,
-
 
111
        336   => DB_ERROR_SYNTAX,
98
        515   => DB_ERROR_CONSTRAINT_NOT_NULL,
112
        515   => DB_ERROR_CONSTRAINT_NOT_NULL,
99
        547   => DB_ERROR_CONSTRAINT,
113
        547   => DB_ERROR_CONSTRAINT,
-
 
114
        1018  => DB_ERROR_SYNTAX,
-
 
115
        1035  => DB_ERROR_SYNTAX,
100
        1913  => DB_ERROR_ALREADY_EXISTS,
116
        1913  => DB_ERROR_ALREADY_EXISTS,
-
 
117
        2209  => DB_ERROR_SYNTAX,
-
 
118
        2223  => DB_ERROR_SYNTAX,
-
 
119
        2248  => DB_ERROR_SYNTAX,
-
 
120
        2256  => DB_ERROR_SYNTAX,
-
 
121
        2257  => DB_ERROR_SYNTAX,
101
        2627  => DB_ERROR_CONSTRAINT,
122
        2627  => DB_ERROR_CONSTRAINT,
102
        2714  => DB_ERROR_ALREADY_EXISTS,
123
        2714  => DB_ERROR_ALREADY_EXISTS,
-
 
124
        3607  => DB_ERROR_DIVZERO,
103
        3701  => DB_ERROR_NOSUCHTABLE,
125
        3701  => DB_ERROR_NOSUCHTABLE,
-
 
126
        7630  => DB_ERROR_SYNTAX,
104
        8134  => DB_ERROR_DIVZERO,
127
        8134  => DB_ERROR_DIVZERO,
-
 
128
        9303  => DB_ERROR_SYNTAX,
-
 
129
        9317  => DB_ERROR_SYNTAX,
-
 
130
        9318  => DB_ERROR_SYNTAX,
-
 
131
        9331  => DB_ERROR_SYNTAX,
-
 
132
        9332  => DB_ERROR_SYNTAX,
-
 
133
        15253 => DB_ERROR_SYNTAX,
105
    );
134
    );
Line 106... Line 135...
106
 
135
 
107
    /**
136
    /**
108
     * The raw database connection created by PHP
137
     * The raw database connection created by PHP
Line 148... Line 177...
148
 
177
 
149
    // }}}
178
    // }}}
Line 150... Line 179...
150
    // {{{ constructor
179
    // {{{ constructor
151
 
180
 
152
    /**
181
    /**
153
     * This constructor calls <kbd>$this->DB_common()</kbd>
182
     * This constructor calls <kbd>parent::__construct()</kbd>
154
     *
183
     *
155
     * @return void
184
     * @return void
156
     */
185
     */
157
    function DB_mssql()
186
    function __construct()
158
    {
187
    {
Line 159... Line 188...
159
        $this->DB_common();
188
        parent::__construct();
160
    }
189
    }
Line 242... Line 271...
242
     *                + the DB_OK constant for other successful queries
271
     *                + the DB_OK constant for other successful queries
243
     *                + a DB_Error object on failure
272
     *                + a DB_Error object on failure
244
     */
273
     */
245
    function simpleQuery($query)
274
    function simpleQuery($query)
246
    {
275
    {
247
        $ismanip = DB::isManip($query);
276
        $ismanip = $this->_checkManip($query);
248
        $this->last_query = $query;
277
        $this->last_query = $query;
249
        if (!@mssql_select_db($this->_db, $this->connection)) {
278
        if (!@mssql_select_db($this->_db, $this->connection)) {
250
            return $this->mssqlRaiseError(DB_ERROR_NODBSELECTED);
279
            return $this->mssqlRaiseError(DB_ERROR_NODBSELECTED);
251
        }
280
        }
252
        $query = $this->modifyQuery($query);
281
        $query = $this->modifyQuery($query);
Line 314... Line 343...
314
            if (!@mssql_data_seek($result, $rownum)) {
343
            if (!@mssql_data_seek($result, $rownum)) {
315
                return null;
344
                return null;
316
            }
345
            }
317
        }
346
        }
318
        if ($fetchmode & DB_FETCHMODE_ASSOC) {
347
        if ($fetchmode & DB_FETCHMODE_ASSOC) {
319
            $arr = @mssql_fetch_array($result, MSSQL_ASSOC);
348
            $arr = @mssql_fetch_assoc($result);
320
            if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
349
            if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
321
                $arr = array_change_key_case($arr, CASE_LOWER);
350
                $arr = array_change_key_case($arr, CASE_LOWER);
322
            }
351
            }
323
        } else {
352
        } else {
324
            $arr = @mssql_fetch_row($result);
353
            $arr = @mssql_fetch_row($result);
Line 351... Line 380...
351
     *
380
     *
352
     * @see DB_result::free()
381
     * @see DB_result::free()
353
     */
382
     */
354
    function freeResult($result)
383
    function freeResult($result)
355
    {
384
    {
356
        return @mssql_free_result($result);
385
        return is_resource($result) ? mssql_free_result($result) : false;
357
    }
386
    }
Line 358... Line 387...
358
 
387
 
359
    // }}}
388
    // }}}
Line 481... Line 510...
481
     *
510
     *
482
     * @return int  the number of rows.  A DB_Error object on failure.
511
     * @return int  the number of rows.  A DB_Error object on failure.
483
     */
512
     */
484
    function affectedRows()
513
    function affectedRows()
485
    {
514
    {
486
        if (DB::isManip($this->last_query)) {
515
        if ($this->_last_query_manip) {
487
            $res = @mssql_query('select @@rowcount', $this->connection);
516
            $res = @mssql_query('select @@rowcount', $this->connection);
488
            if (!$res) {
517
            if (!$res) {
489
                return $this->mssqlRaiseError();
518
                return $this->mssqlRaiseError();
490
            }
519
            }
491
            $ar = @mssql_fetch_row($res);
520
            $ar = @mssql_fetch_row($res);
Line 535... Line 564...
535
                $result = $this->createSequence($seq_name);
564
                $result = $this->createSequence($seq_name);
536
                if (DB::isError($result)) {
565
                if (DB::isError($result)) {
537
                    return $this->raiseError($result);
566
                    return $this->raiseError($result);
538
                }
567
                }
539
            } elseif (!DB::isError($result)) {
568
            } elseif (!DB::isError($result)) {
-
 
569
                $result = $this->query("SELECT IDENT_CURRENT('$seqname')");
-
 
570
                if (DB::isError($result)) {
-
 
571
                    /* Fallback code for MS SQL Server 7.0, which doesn't have
-
 
572
                     * IDENT_CURRENT. This is *not* safe for concurrent
-
 
573
                     * requests, and really, if you're using it, you're in a
-
 
574
                     * world of hurt. Nevertheless, it's here to ensure BC. See
-
 
575
                     * bug #181 for the gory details.*/
540
                $result =& $this->query("SELECT @@IDENTITY FROM $seqname");
576
                    $result = $this->query("SELECT @@IDENTITY FROM $seqname");
-
 
577
                }
541
                $repeat = 0;
578
                $repeat = 0;
542
            } else {
579
            } else {
543
                $repeat = false;
580
                $repeat = false;
544
            }
581
            }
545
        } while ($repeat);
582
        } while ($repeat);
Line 585... Line 622...
585
    {
622
    {
586
        return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
623
        return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
587
    }
624
    }
Line 588... Line 625...
588
 
625
 
-
 
626
    // }}}
-
 
627
    // {{{ escapeSimple()
-
 
628
 
-
 
629
    /**
-
 
630
     * Escapes a string in a manner suitable for SQL Server.
-
 
631
     *
-
 
632
     * @param string $str  the string to be escaped
-
 
633
     * @return string  the escaped string
-
 
634
     *
-
 
635
     * @see DB_common::quoteSmart()
-
 
636
     * @since Method available since Release 1.6.0
-
 
637
     */
-
 
638
    function escapeSimple($str)
-
 
639
    {
-
 
640
        return str_replace(
-
 
641
            array("'", "\\\r\n", "\\\n"),
-
 
642
            array("''", "\\\\\r\n\r\n", "\\\\\n\n"),
-
 
643
            $str
-
 
644
        );
-
 
645
    }
-
 
646
 
589
    // }}}
647
    // }}}
Line 590... Line 648...
590
    // {{{ quoteIdentifier()
648
    // {{{ quoteIdentifier()
591
 
649
 
592
    /**
650
    /**
Line 743... Line 801...
743
        if ($mode) {
801
        if ($mode) {
744
            $res['num_fields'] = $count;
802
            $res['num_fields'] = $count;
745
        }
803
        }
Line 746... Line 804...
746
 
804
 
-
 
805
        for ($i = 0; $i < $count; $i++) {
-
 
806
            if ($got_string) {
-
 
807
                $flags = $this->_mssql_field_flags($result,
-
 
808
                        @mssql_field_name($id, $i));
-
 
809
                if (DB::isError($flags)) {
-
 
810
                    return $flags;
-
 
811
                }
-
 
812
            } else {
-
 
813
                $flags = '';
-
 
814
            }
747
        for ($i = 0; $i < $count; $i++) {
815
 
748
            $res[$i] = array(
816
            $res[$i] = array(
749
                'table' => $got_string ? $case_func($result) : '',
817
                'table' => $got_string ? $case_func($result) : '',
750
                'name'  => $case_func(@mssql_field_name($id, $i)),
818
                'name'  => $case_func(@mssql_field_name($id, $i)),
751
                'type'  => @mssql_field_type($id, $i),
819
                'type'  => @mssql_field_type($id, $i),
752
                'len'   => @mssql_field_length($id, $i),
-
 
753
                // We only support flags for table
820
                'len'   => @mssql_field_length($id, $i),
754
                'flags' => $got_string
-
 
755
                           ? $this->_mssql_field_flags($result,
-
 
756
                                                       @mssql_field_name($id, $i))
-
 
757
                           : '',
821
                'flags' => $flags,
758
            );
822
            );
759
            if ($mode & DB_TABLEINFO_ORDER) {
823
            if ($mode & DB_TABLEINFO_ORDER) {
760
                $res['order'][$res[$i]['name']] = $i;
824
                $res['order'][$res[$i]['name']] = $i;
761
            }
825
            }
Line 803... Line 867...
803
 
867
 
804
            $flags = array();
868
            $flags = array();
Line 805... Line 869...
805
            $tableName = $table;
869
            $tableName = $table;
806
 
870
 
-
 
871
            // get unique and primary keys
-
 
872
            $res = $this->getAll("EXEC SP_HELPINDEX $table", DB_FETCHMODE_ASSOC);
-
 
873
            if (DB::isError($res)) {
Line 807... Line 874...
807
            // get unique and primary keys
874
                return $res;
808
            $res = $this->getAll("EXEC SP_HELPINDEX[$table]", DB_FETCHMODE_ASSOC);
875
            }
Line 809... Line 876...
809
 
876
 
Line 826... Line 893...
826
                    }
893
                    }
827
                }
894
                }
828
            }
895
            }
Line 829... Line 896...
829
 
896
 
830
            // get auto_increment, not_null and timestamp
897
            // get auto_increment, not_null and timestamp
-
 
898
            $res = $this->getAll("EXEC SP_COLUMNS $table", DB_FETCHMODE_ASSOC);
-
 
899
            if (DB::isError($res)) {
-
 
900
                return $res;
Line 831... Line 901...
831
            $res = $this->getAll("EXEC SP_COLUMNS[$table]", DB_FETCHMODE_ASSOC);
901
            }
832
 
902
 
833
            foreach ($res as $val) {
903
            foreach ($res as $val) {
834
                $val = array_change_key_case($val, CASE_LOWER);
904
                $val = array_change_key_case($val, CASE_LOWER);