Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented #109 #110 #123

Merged
merged 1 commit into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/docs/api-ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ class Datasource(dsxml, filename=None)
class Connection(connxml)
```

The Connection class represents a tableau data connection. It can be from any type of connection found in `dbclass.py` via `is_valid_dbclass`

**Params:**

**Raises:**

**Methods:**

**Properities:**

`self.server:` Returns a string containing the server.

`self.dbname:` Returns a string containing the database name.

`self.username:` Returns a string containing the username.

`self.dbclass:` Returns a string containing the database class.

`self.port:` Returns a string containing the port.

`self.query_band:` Returns a string containing the query band.

`self.initial_sql:` Returns a string containing the initial sql.

## Fields
```python
class Workbook(column_xml=None, metadata_xml=None)
Expand Down
59 changes: 58 additions & 1 deletion tableaudocumentapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ def __init__(self, connxml):
self._authentication = connxml.get('authentication')
self._class = connxml.get('class')
self._port = connxml.get('port', None)
self._query_band = connxml.get('query-band-spec', None)
self._initial_sql = connxml.get('one-time-sql', None)

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

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

Expand All @@ -34,6 +37,8 @@ def from_attributes(cls, server, dbname, username, dbclass, port=None, authentic
xml.username = username
xml.dbclass = dbclass
xml.port = port
xml.query_band = query_band
xml.initial_sql = initial_sql

return xml

Expand Down Expand Up @@ -149,3 +154,55 @@ def port(self, value):
pass
else:
self._connectionXML.set('port', value)

@property
def query_band(self):
"""Query band passed on connection to database."""
return self._query_band

@query_band.setter
def query_band(self, value):
"""Set the connection's query_band property.

Args:
value: New query_band value. String.

Returns:
Nothing.
"""

self._query_band = value
# If query band is None we remove the element and don't write it to XML
if value is None:
try:
del self._connectionXML.attrib['query-band-spec']
except KeyError:
pass
else:
self._connectionXML.set('query-band-spec', value)

@property
def initial_sql(self):
"""Initial SQL to be run."""
return self._initial_sql

@initial_sql.setter
def initial_sql(self, value):
"""Set the connection's initial_sql property.

Args:
value: New initial_sql value. String.

Returns:
Nothing.
"""

self._initial_sql = value
# If initial_sql is None we remove the element and don't write it to XML
if value is None:
try:
del self._connectionXML.attrib['one-time-sql']
except KeyError:
pass
else:
self._connectionXML.set('one-time-sql', value)
2 changes: 1 addition & 1 deletion test/assets/CONNECTION.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username='' port='1433'></connection>
<connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012' username='' port='1433' query-band-spec=''></connection>
18 changes: 18 additions & 0 deletions test/bvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,42 @@ def test_can_read_attributes_from_connection(self):
self.assertEqual(conn.dbclass, 'sqlserver')
self.assertEqual(conn.authentication, 'sspi')
self.assertEqual(conn.port, '1433')
self.assertEqual(conn.initial_sql, '')
self.assertEqual(conn.query_band, '')

def test_can_write_attributes_to_connection(self):
conn = Connection(self.connection)
conn.dbname = 'BubblesInMyDrink'
conn.server = 'mssql2014'
conn.username = 'bob'
conn.port = '1337'
conn.initial_sql = "insert values (1, 'winning') into schema.table"
conn.query_band = 'TableauReport=<workbookname>'
self.assertEqual(conn.dbname, 'BubblesInMyDrink')
self.assertEqual(conn.username, 'bob')
self.assertEqual(conn.server, 'mssql2014')
self.assertEqual(conn.port, '1337')
self.assertEqual(conn.initial_sql, "insert values (1, 'winning') into schema.table")
self.assertEqual(conn.query_band, 'TableauReport=<workbookname>')

def test_can_delete_port_from_connection(self):
conn = Connection(self.connection)
conn.port = None
self.assertEqual(conn.port, None)
self.assertIsNone(conn._connectionXML.get('port'))

def test_can_delete_initial_sql_from_connection(self):
conn = Connection(self.connection)
conn.initial_sql = None
self.assertEqual(conn.initial_sql, None)
self.assertIsNone(conn._connectionXML.get('initial_sql'))

def test_can_delete_query_band_from_connection(self):
conn = Connection(self.connection)
conn.query_band = None
self.assertEqual(conn.query_band, None)
self.assertIsNone(conn._connectionXML.get('query_band'))

def test_bad_dbclass_rasies_attribute_error(self):
conn = Connection(self.connection)
conn.dbclass = 'sqlserver'
Expand Down