root/websites/planet.php.gr.jp/trunk/lib/creole/drivers/mssql/MSSQLConnection.php @ 12928

Revision 12928, 8.6 kB (checked in by ha1t, 6 years ago)

creoleを追加

Line 
1<?php
2
3/*
4 *  $Id: MSSQLConnection.php,v 1.25 2005/10/17 19:03:51 dlawson_mi Exp $
5 *
6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 *
18 * This software consists of voluntary contributions made by many individuals
19 * and is licensed under the LGPL. For more information please see
20 * <http://creole.phpdb.org>.
21 */
22
23
24require_once 'creole/Connection.php';
25require_once 'creole/common/ConnectionCommon.php';
26include_once 'creole/drivers/mssql/MSSQLResultSet.php';
27
28/**
29 * MS SQL Server implementation of Connection.
30 *
31 * If you have trouble with BLOB / CLOB support
32 * --------------------------------------------
33 *
34 * You may need to change some PHP ini settings.  In particular, the following settings
35 * set the text size to maximum which should get around issues with truncated data:
36 * <code>
37 *  ini_set('mssql.textsize', 2147483647);
38 *  ini_set('mssql.textlimit', 2147483647);
39 * </code>
40 * We do not set these by default (anymore) because they do not apply to cases where MSSQL
41 * is being used w/ FreeTDS.
42 *
43 * @author    Hans Lellelid <hans@xmpl.org>
44 * @author    Stig Bakken <ssb@fast.no>
45 * @author    Lukas Smith
46 * @version   $Revision: 1.25 $
47 * @package   creole.drivers.mssql
48 */
49class MSSQLConnection extends ConnectionCommon implements Connection {       
50   
51    /** Current database (used in mssql_select_db()). */
52    private $database;
53   
54    /**
55     * @see Connection::connect()
56     */
57    function connect($dsninfo, $flags = 0)
58    {               
59        if (!extension_loaded('mssql') && !extension_loaded('sybase') && !extension_loaded('sybase_ct')) {
60            throw new SQLException('mssql extension not loaded');
61        }
62
63        $this->dsn = $dsninfo;
64        $this->flags = $flags;
65               
66        $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT);
67
68        $user = $dsninfo['username'];
69        $pw = $dsninfo['password'];
70        $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
71       
72        if (PHP_OS == "WINNT" || PHP_OS == "WIN32") {
73            $portDelimiter = ",";
74        } else {
75            $portDelimiter = ":";
76        }
77       
78        if(!empty($dsninfo['port'])) {
79                $dbhost .= $portDelimiter.$dsninfo['port'];
80        } else {
81                $dbhost .= $portDelimiter.'1433';
82        }
83       
84        $connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect';
85
86        if ($dbhost && $user && $pw) {
87            $conn = @$connect_function($dbhost, $user, $pw);
88        } elseif ($dbhost && $user) {
89            $conn = @$connect_function($dbhost, $user);
90        } else {
91            $conn = @$connect_function($dbhost);
92        }
93        if (!$conn) {
94            throw new SQLException('connect failed', mssql_get_last_message());
95        }
96       
97        if ($dsninfo['database']) {
98            if (!@mssql_select_db($dsninfo['database'], $conn)) {
99                throw new SQLException('No database selected');               
100            }
101           
102            $this->database = $dsninfo['database'];
103        }
104       
105        $this->dblink = $conn;       
106    }   
107   
108    /**
109     * @see Connection::getDatabaseInfo()
110     */
111    public function getDatabaseInfo()
112    {
113        require_once 'creole/drivers/mssql/metadata/MSSQLDatabaseInfo.php';
114        return new MSSQLDatabaseInfo($this);
115    }
116   
117     /**
118     * @see Connection::getIdGenerator()
119     */
120    public function getIdGenerator()
121    {
122        require_once 'creole/drivers/mssql/MSSQLIdGenerator.php';
123        return new MSSQLIdGenerator($this);
124    }
125   
126    /**
127     * @see Connection::prepareStatement()
128     */
129    public function prepareStatement($sql)
130    {
131        require_once 'creole/drivers/mssql/MSSQLPreparedStatement.php';
132        return new MSSQLPreparedStatement($this, $sql);
133    }
134   
135    /**
136     * @see Connection::createStatement()
137     */
138    public function createStatement()
139    {
140        require_once 'creole/drivers/mssql/MSSQLStatement.php';
141        return new MSSQLStatement($this);
142    }
143   
144    /**
145     * Returns false since MSSQL doesn't support this method.
146     */
147    public function applyLimit(&$sql, $offset, $limit)
148    {
149        return false;
150    }
151   
152    /**
153     * @see Connection::close()
154     */
155    function close()
156    {
157        $ret = @mssql_close($this->dblink);
158        $this->dblink = null;
159        return $ret;
160    }
161   
162    /**
163     * @see Connection::executeQuery()
164     */
165    function executeQuery($sql, $fetchmode = null)
166    {           
167        $this->lastQuery = $sql;
168        if (!@mssql_select_db($this->database, $this->dblink)) {
169            throw new SQLException('No database selected');
170        }       
171        $result = @mssql_query($sql, $this->dblink);
172        if (!$result) {
173            throw new SQLException('Could not execute query', mssql_get_last_message());
174        }
175        return new MSSQLResultSet($this, $result, $fetchmode);
176    }
177
178    /**
179     * @see Connection::executeUpdate()
180     */
181    function executeUpdate($sql)
182    {   
183       
184        $this->lastQuery = $sql;
185        if (!mssql_select_db($this->database, $this->dblink)) {
186            throw new SQLException('No database selected');
187        }
188       
189        $result = @mssql_query($sql, $this->dblink);
190        if (!$result) {
191            throw new SQLException('Could not execute update', mssql_get_last_message(), $sql);
192        }
193       
194        return $this->getUpdateCount();
195    }
196
197    /**
198     * Start a database transaction.
199     * @throws SQLException
200     * @return void
201     */
202    protected function beginTrans()
203    {
204        $result = @mssql_query('BEGIN TRAN', $this->dblink);
205        if (!$result) {
206            throw new SQLException('Could not begin transaction', mssql_get_last_message());
207        }
208    }
209   
210    /**
211     * Commit the current transaction.
212     * @throws SQLException
213     * @return void
214     */
215    protected function commitTrans()
216    {
217        if (!@mssql_select_db($this->database, $this->dblink)) {
218            throw new SQLException('No database selected');
219        }
220        $result = @mssql_query('COMMIT TRAN', $this->dblink);
221        if (!$result) {
222            throw new SQLException('Could not commit transaction', mssql_get_last_message());
223        }
224    }
225
226    /**
227     * Roll back (undo) the current transaction.
228     * @throws SQLException
229     * @return void
230     */
231    protected function rollbackTrans()
232    {
233        if (!@mssql_select_db($this->database, $this->dblink)) {           
234            throw new SQLException('no database selected');
235        }
236        $result = @mssql_query('ROLLBACK TRAN', $this->dblink);
237        if (!$result) {
238            throw new SQLException('Could not rollback transaction', mssql_get_last_message());
239        }
240    }
241
242    /**
243     * Gets the number of rows affected by the last query.
244     * if the last query was a select, returns 0.
245     *
246     * @return int Number of rows affected by the last query
247     * @throws SQLException
248     */
249    function getUpdateCount()
250    {       
251        $res = @mssql_query('select @@rowcount', $this->dblink);
252        if (!$res) {
253            throw new SQLException('Unable to get affected row count', mssql_get_last_message());
254        }
255        $ar = @mssql_fetch_row($res);
256        if (!$ar) {
257            $result = 0;
258        } else {
259            @mssql_free_result($res);
260            $result = $ar[0];
261        }
262       
263        return $result;
264    }         
265   
266   
267    /**
268     * Creates a CallableStatement object for calling database stored procedures.
269     *
270     * @param string $sql
271     * @return CallableStatement
272     * @throws SQLException
273     */
274    function prepareCall($sql)
275    {             
276        require_once 'creole/drivers/mssql/MSSQLCallableStatement.php';
277        $stmt = mssql_init($sql);
278        if (!$stmt) {
279            throw new SQLException('Unable to prepare statement', mssql_get_last_message(), $sql);
280        }
281        return new MSSQLCallableStatement($this, $stmt);
282    }
283}
Note: See TracBrowser for help on using the browser.