Skip to content

Commit 881a14f

Browse files
committed
Merge pull request pymssql#197 from pymssql/variant-azure
Support SQL Azure
2 parents bbc2eca + 6683847 commit 881a14f

File tree

9 files changed

+78
-10
lines changed

9 files changed

+78
-10
lines changed

ChangeLog

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
Change Log
22
==========
33

4+
Version 2.x.y - To be released
5+
==============================
6+
7+
Features
8+
--------
9+
10+
- Compatibility with Azure
11+
12+
It is now possible to transparently connect to `SQL Server instances`_
13+
accessible as part of the Azure_ cloud services.
14+
15+
.. note:: If you need to connect to Azure make sure you use FreeTDS 0.91 or newer.
16+
17+
.. _SQL Server instances: http://www.windowsazure.com/en-us/services/sql-database/
18+
.. _Azure: https://www.windowsazure.com/
19+
420
Version 2.1.0 - 2014-02-25 - `Marc Abramowitz <http://marc-abramowitz.com/>`_
521
=============================================================================
622

_mssql.pyx

+15
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ from cpython.tuple cimport PyTuple_New, PyTuple_SetItem
6666
cdef extern from "pymssql_version.h":
6767
const char *PYMSSQL_VERSION
6868

69+
cdef extern from "cpp_helpers.h":
70+
cdef bint FREETDS_SUPPORTS_DBSETLDBNAME
71+
6972
# Vars to store messages from the server in
7073
cdef int _mssql_last_msg_no = 0
7174
cdef int _mssql_last_msg_severity = 0
@@ -578,6 +581,18 @@ cdef class MSSQLConnection:
578581
strncpy(self._charset, _charset, PYMSSQL_CHARSETBUFSIZE)
579582
DBSETLCHARSET(login, self._charset)
580583

584+
# For Python 3, we need to convert unicode to byte strings
585+
cdef bytes dbname_bytes
586+
cdef char *dbname_cstr
587+
# Put the DB name in the login LOGINREC because it helps with connections to Azure
588+
if database:
589+
if FREETDS_SUPPORTS_DBSETLDBNAME:
590+
dbname_bytes = database.encode('ascii')
591+
dbname_cstr = dbname_bytes
592+
DBSETLDBNAME(login, dbname_cstr)
593+
else:
594+
log("_mssql.MSSQLConnection.__init__(): Warning: This version of FreeTDS doesn't support selecting the DB name when setting up the connection. This will keep connections to Azure from working.")
595+
581596
# Set the login timeout
582597
dbsetlogintime(login_timeout)
583598

cpp_helpers.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3+
cpp_helpers.h
4+
5+
Copyright (C) 2014 pymssql development team.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20+
MA 02110-1301 USA
21+
*/
22+
23+
/*
24+
25+
Helper module to aid with the fact that Cython doesn't support C pre-procesor
26+
macros yet. See:
27+
28+
https://groups.google.com/forum/#!topic/cython-users/HASNraxsGGw
29+
https://github.com/denik/cython-ifdef
30+
http://grokbase.com/t/gg/cython-users/128rn1399p/equivalent-of-ifdef-preprocessor
31+
32+
*/
33+
34+
#include "sybdb.h"
35+
36+
#ifdef DBSETLDBNAME
37+
#define FREETDS_SUPPORTS_DBSETLDBNAME 1
38+
#else
39+
#define FREETDS_SUPPORTS_DBSETLDBNAME 0
40+
#endif

docs/freetds.rst

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
FreeTDS configuration
33
=====================
44

5-
Basic info on setting up connections with FreeTDS.
6-
75
pymssql uses FreeTDS package to connect to SQL Server instances. You have to
86
tell it how to find your database servers. The most basic info is host name,
97
port number, and protocol version to use.

docs/ref/_mssql.rst

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Functions
8888
SET QUOTED_IDENTIFIER ON;
8989
SET TEXTSIZE 2147483647; -- http://msdn.microsoft.com/en-us/library/aa259190%28v=sql.80%29.aspx
9090
91+
.. note:: If you need to connect to Azure make sure you use FreeTDS 0.91 or newer.
92+
9193
``MSSQLConnection`` object properties
9294
-------------------------------------
9395

docs/ref/pymssql.rst

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Functions
5454
:keyword port: the TCP port to use to connect to the server
5555
:type port: string
5656

57+
.. note:: If you need to connect to Azure make sure you use FreeTDS 0.91 or newer.
58+
5759
.. function:: get_dbversion()
5860

5961
TBD

pymssql.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ def connect(server='.', user='', password='', database='', timeout=0,
572572
:keyword appname: Set the application name to use for the connection
573573
:type appname: string
574574
:keyword port: the TCP port to use to connect to the server
575-
:type appname: string
575+
:type port: string
576576
"""
577577

578578
_mssql.login_timeout = login_timeout

sqlfront.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -651,5 +651,6 @@ cdef extern from "sqlfront.h":
651651
RETCODE DBSETLUSER(LOGINREC *x, char *y)
652652
RETCODE DBSETLCHARSET(LOGINREC *x, char *y)
653653
RETCODE DBSETLVERSION(LOGINREC *login, BYTE version)
654+
RETCODE DBSETLDBNAME(LOGINREC *x, char *y)
654655

655656
ctypedef int LINE_T

tests/test_config.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,14 @@ def test_config_values(self):
3737
database = 'tempdb',
3838
)
3939
assert b'user_name = bob' in config_dump
40-
# it would be nice if this was the DB name, see test_dbsetldbname()
41-
assert b'database = \n' in config_dump
40+
assert b'database = tempdb\n' in config_dump
4241
# test default port
4342
assert b'port = 1433' in config_dump
4443
# not sure why 7.1 version is used instead of 8.0 which is the
4544
# default
4645
assert b'major_version = 7' in config_dump
4746
assert b'minor_version = 1' in config_dump
4847

49-
def test_dbsetldbname(self):
50-
# sybdb.h defines DBSETLDBNAME, we should try to use that to get
51-
# the DB in the config dump for debugging purposes
52-
skip_test() # test_dbsetldbname
53-
5448
def test_tds_protocol_version_42(self):
5549
config_dump = self.connect(
5650
server='dontnameyourserverthis',

0 commit comments

Comments
 (0)