3
__version__="$Revision $"[11:-2]
5
__author__="Andy Todd <andy47@halfcooked.com>"
9
Description: Plug in for PythonCard application dbBrowse to provide simple CSV functionality
11
Constant/configuration values are currently maintained in the source code. If we are to optimise this application they should be split into seperate configuration files (as per PythonCard/Webware style guidelines)
13
The structure of this module should be replicated for different RDBMS so that they can be interchanged by dbBrowse - hopefully.
19
# Connection should be a dictionary with at least two keys,
20
# 'databasename' and 'directory'
21
# This is wildly different to other database modules
22
def __init__(self, connection):
23
"Setup the database connection"
24
self._system_tables=[]
26
name = os.path.join(connection["directory"], connection["databasename"])
27
self.reader = csv.reader(file(name, "rb"))
28
self.headers = self.reader.next()
34
# This one is used in getRow
35
self._tableName='TheOnlyTable'
38
"Return a list of all of the non-system tables in <database>"
39
return [ "TheOnlyTable" ]
41
def getColumns(self, tableName):
42
"Get the definition of the columns in tableName"
43
# format of dbBrowser column definitions is
44
# column name, data type, length (for display), nullable, key, default
46
for column in self.headers:
48
dataType, nullable, key, default = "varchar", "", "", ""
49
# Dodgy default, but it works for me
51
columnDefs.append((columnName, dataType, precision, nullable, key, default))
54
def getQueryString(self, tableName):
55
"Return a SQL statement which queries all of the columns in tableName"
56
# only used internally, not needed for csv files
59
def getRow(self, tableName):
60
"Get a row from tableName"
61
if tableName!=self._tableName:
62
self._tableName=tableName
64
result = self.reader.next()
69
def getRows(self, tableName):
70
"Get all of the rows from tableName"
71
if tableName!=self._tableName:
72
self._tableName=tableName
74
for row in self.reader:
78
if __name__ == '__main__':
79
# We are in an interactive session so run our test routines
80
# Connect to the database
81
connection={ 'databasename':'testfile.csv'
83
dbHolder = browse(connection)
84
# Return all of our table names into user_tables
85
user_tables = dbHolder.getTables()
87
# Print out the structure of each table and its first row
88
print "--------------------------------------------------"
89
for table in user_tables:
90
print dbHolder.getQueryString(table)
91
print dbHolder.getRow(table)
92
print "--------------------------------------------------"