8000 Add `initial sql` and `query band` support (#123) · onware/document-api-python@cc97fc4 · GitHub
[go: up one dir, main page]

Skip to content

Commit cc97fc4

Browse files
r-richmondt8y8
authored andcommitted
Add initial sql and query band support (tableau#123)
Addresses tableau#109 and tableau#110
1 parent 3ab99f4 commit cc97fc4

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

docs/docs/api-ref.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ class Datasource(dsxml, filename=None)
4848
class Connection(connxml)
4949
```
5050

51+
The Connection class represents a tableau data connection. It can be from any type of connection found in `dbclass.py` via `is_valid_dbclass`
52+
53+
**Params:**
54+
55+
**Raises:**
56+
57+
**Methods:**
58+
59+
**Properities:**
60+
61+
`self.server:` Returns a string containing the server.
62+
63+
`self.dbname:` Returns a string containing the database name.
64+
65+
`self.username:` Returns a string containing the username.
66+
67+
`self.dbclass:` Returns a string containing the database class.
68+
69+
`self.port:` Returns a string containing the port.
70+
71+
`self.query_band:` Returns a string containing the query band.
72+
73+
`self.initial_sql:` Returns a string containing the initial sql.
74+
5175
## Fields
5276
```python
5377
class Workbook(column_xml=None, metadata_xml=None)

tableaudocumentapi/connection.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ def __init__(self, connxml):
1818
self._authentication = connxml.get('authentication')
1919
self._class = connxml.get('class')
2020
self._port = connxml.get('port', None)
21+
self._query_band = connxml.get('query-band-spec', None)
22+
self._initial_sql = connxml.get('one-time-sql', None)
2123

2224
def __repr__(self):
2325
return "'<Connection server='{}' dbname='{}' @ {}>'".format(self._server, self._dbname, hex(id(self)))
2426

2527
@classmethod
26-
def from_attributes(cls, server, dbname, username, dbclass, port=None, authentication=''):
28+
def from_attributes(cls, server, dbname, username, dbclass, port=None, query_band=None,
29+
initial_sql=None, authentication=''):
2730
"""Creates a new connection that can be added into a Data Source.
2831
defaults to `''` which will be treated as 'prompt' by Tableau."""
2932

@@ -34,6 +37,8 @@ def from_attributes(cls, server, dbname, username, dbclass, port=None, authentic
3437
xml.username = username
3538
xml.dbclass = dbclass
3639
xml.port = port
40+
xml.query_band = query_band
41+
xml.initial_sql = initial_sql
3742

3843
return xml
3944

@@ -149,3 +154,55 @@ def port(self, value):
149154
pass
150155
else:
151156
self._connectionXML.set('port', value)
157+
158+
@property
159+
def query_band(self):
160+
"""Query band passed on connection to database."""
161+
return self._query_band
162+
163+
@query_band.setter
164+
def query_band(self, value):
165+
"""Set the connection's query_band property.
166+
167+
Args:
168+
value: New query_band value. String.
169+
170+
Returns:
171+
Nothing.
172+
"""
173+
174+
self._query_band = value
175+
# If query band is None we remove the element and don't write it to XML
176+
if value is None:
177+
try:
178+
del self._connectionXML.attrib['query-band-spec']
179+
except KeyError:
180+
pass
181+
else:
182+
self._connectionXML.set('query-band-spec', value)
183+
184+
@property
185+
def initial_sql(self):
186+
"""Initial SQL to be run."""
187+
return self._initial_sql
188+
189+
@initial_sql.setter
190+
def initial_sql(self, value):
191+
"""Set the connection's initial_sql property.
192+
193+
Args:
194+
value: New initial_sql value. String.
195+
196+
Returns:
197+
Nothing.
198+
"""
199+
200+
self._initial_sql = value
201+
# If initial_sql is None we remove the element and don't write it to XML
202+
if value is None:
203+
try:
204+
del self._connectionXML.attrib['one-time-sql']
205+
except KeyError:
206+
pass
207+
else:
208+
self._connectionXML.set('one-time-sql', value)

test/assets/CONNECTION.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username='' port='1433'></connection>
1+
<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username='' port='1433' query-band-spec=''></connection>

test/bvt.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,42 @@ def test_can_read_attributes_from_connection(self):
6060
self.assertEqual(conn.dbclass, 'sqlserver')
6161
self.assertEqual(conn.authentication, 'sspi')
6262
self.assertEqual(conn.port, '1433')
63+
self.assertEqual(conn.initial_sql, '')
64+
self.assertEqual(conn.query_band, '')
6365

6466
def test_can_write_attributes_to_connection(self):
6567
conn = Connection(self.connection)
6668
conn.dbname = 'BubblesInMyDrink'
6769
conn.server = 'mssql2014'
6870
conn.username = 'bob'
6971
conn.port = '1337'
72+
conn.initial_sql = "insert values (1, 'winning') into schema.table"
73+
conn.query_band = 'TableauReport=<workbookname>'
7074
self.assertEqual(conn.dbname, 'BubblesInMyDrink')
7175
self.assertEqual(conn.username, 'bob')
7276
self.assertEqual(conn.server, 'mssql2014')
7377
self.assertEqual(conn.port, '1337')
78+
self.assertEqual(conn.initial_sql, "insert values (1, 'winning') into schema.table")
79+
self.assertEqual(conn.query_band, 'TableauReport=<workbookname>')
7480

7581
def test_can_delete_port_from_connection(self):
7682
conn = Connection(self.connection)
7783
conn.port = None
7884
self.assertEqual(conn.port, None)
7985
self.assertIsNone(conn._connectionXML.get('port'))
8086

87+
def test_can_delete_initial_sql_from_connection(self):
88+
conn = Connection(self.connection)
89+
conn.initial_sql = None
90+
self.assertEqual(conn.initial_sql, None)
91+
self.assertIsNone(conn._connectionXML.get('initial_sql'))
92+
93+
def test_can_delete_query_band_from_connection(self):
94+
conn = Connection(self.connection)
95+
conn.query_band = None
96+
self.assertEqual(conn.query_band, None)
97+
self.assertIsNone(conn._connectionXML.get('query_band'))
98+
8199
def test_bad_dbclass_rasies_attribute_error(self):
82100
conn = Connection(self.connection)
83101
conn.dbclass = 'sqlserver'

0 commit comments

Comments
 (0)
0