Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
Added meat of odbc example
Browse files Browse the repository at this point in the history
  • Loading branch information
Wesley Mason committed Jul 14, 2010
1 parent a1d9ebc commit 0f441ac
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
13 changes: 10 additions & 3 deletions examples/litecli.py
@@ -1,3 +1,12 @@
#-*- coding: utf-8 -*-

"""Example pyDBCLI.utils.Utility app, simple replacement for sqlite3's
CLI utility.
"""
__author__ = 'Wes Mason <wes[at]1stvamp[dot]org>'
__docformat__ = 'restructuredtext en'
__version__ = '0.1'

import sqlite3
import getopt
import sys
Expand Down Expand Up @@ -76,11 +85,9 @@ def main(argv):

# Setup cursor
conn = sqlite3.connect(filepath)
cursor = conn.cursor()

u = LiteUtility()
u.cursor = cursor
u.system_cursor = cursor
u.cursor = u.system_cursor = conn.cursor()
u.cmdloop()

if __name__ == "__main__":
Expand Down
113 changes: 113 additions & 0 deletions examples/odbc.py
@@ -0,0 +1,113 @@
#-*- coding: utf-8 -*-

"""Example pyDBCLI.utils.Utility app, using PyODBC to wrap
DBAPI cursor calls, allowing the use an ODBC DSN and driver
for connections and to send queries.
"""
__author__ = 'Wes Mason <wes[at]1stvamp[dot]org>'
__docformat__ = 'restructuredtext en'
__version__ = '0.1'

import pyodbc
import getopt
import sys
from urlparse import urlparse
from pyDBCLI.utils import Utility
from pyDBCLI.helpers import error, usage, memoized

class ODBCUtility(Utility):
prompt = 'odbc-qt# '
intro = 'ODBC Query Tool v%s' % (__version__,)
query = ''
dsn = {}

@memoized()
def get_tables(self):
tables = [['Table name']]
for row in self.cursor.tables():
tables.append([row])
return tables

@memoized()
def get_columns(self, name):
columns = [['Column name', 'Type', 'Size',]]
r = self.connect.execute('SELECT * FROM %s LIMIT 1' % (table,))
for row in r.cursor.description:
columns.append([row[0], self.db_types[row[1]], row[3],])
return columns

def connect(self, schema):
self.dsn['schema'] = schema
try:
conn = pyodbc.connect(self.query, **self.dsn)
self.cursor = self.system_cursor = conn.cursor()
except:
error(e, False)
return False
else:
return True

# Shell script usage instrunctions, pushed to stdout
# by usage()
USAGE_MESSAGE = """ODBC Query Tool
Usage: odbc.py -d <dsn url>
Options:
-d, --dsn : ODBC DSN, in RFC1738 URL format:
driver://username:password@host:port/schema
driver://username:password@host:port/schema?extraProperties=here&another=too
"""

def url_to_dsn(url):
res = urlparse(url)
return res.query.replace('&', ';'), {
'driver': res.scheme,
'server': res.netloc,
'database': res.path,
'uid': res.username,
'pwd': res.password,
}


def main(argv):
"""Main entry function for CLI script, should be
passed CLI args tuple, normally from sys.argv
"""
# Get CLI options
try:
opts, args = getopt.getopt(
argv,
"d:",
[
"dsn=",
]
)
except getopt.GetoptError:
error("Unknown options", True, USAGE_MESSAGE)

if not opts:
usage(USAGE_MESSAGE)
sys.exit(0)

dsn = None
query = None

# Parse CLI options
for opt, arg in opts:
if opt in ("-d", "--dsn"):
query, dsn = url_to_dsn(arg)
if not dsn:
error("Please provide a DSN URL", True, USAGE_MESSAGE)

# Setup cursor
conn = pyodbc.connect(query, **dsn)

u = ODBCUtility()
u.cursor = u.system_cursor = conn.cursor()
u.cmdloop()

if __name__ == "__main__":
# Pass all CLI args except the first one, which is normally
# the scripts filename
main(sys.argv[1:])

0 comments on commit 0f441ac

Please sign in to comment.