source: coopr.pyomo/trunk/coopr/pyomo/data/odbc_table.py @ 3008

Last change on this file since 3008 was 3008, checked in by wehart, 9 years ago

Adding a rough draft of an ODBC plugin.

File size: 2.3 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11import os.path
12from TableData import TableData
13from pyutilib.component.core import alias
14
15
16class odbcTable(TableData):
17
18    alias("odbc", "Manage IO with an ODBC interface.")
19
20    def __init__(self):
21        TableData.__init__(self)
22
23    def open(self):
24        # WEH - Do we specify a filename?  If not, we should use self.options.dburl
25        if self.filename is None:
26            raise IOError, "No filename specified"
27        if not os.path.exists(self.filename):
28            raise IOError, "Cannot find file '%s'" % self.filename
29        #
30        # Initialize self.db
31        #
32        self.db = None
33        if self._data is not None:
34            self.db = self._data
35        else:
36            try:
37                # WEH - Create a database here
38                #self.db = pyodbc.connect() #
39                pass
40            except pyutilib.ApplicationError:
41                raise
42
43    def read(self):
44        #
45        # Get the table from the database
46        #
47        if self.db is None:
48            return
49        tmp = self.db.get_table(self.options.table, raw=True)
50        #
51        # Process data from the table
52        #
53        if type(tmp) in (int,long,float):
54            if not self.options.param is None:
55                self._info = ["param",self.options.param,":=",tmp]
56            elif len(self.options.symbol_map) == 1:
57                self._info = ["param",self.options.symbol_map[self.options.symbol_map.keys()[0]],":=",tmp]
58            else:
59                raise IOError, "Data looks like a parameter, but multiple parameter names have been specified: %s" % str(self.options.symbol_map)
60        elif len(tmp) == 0:
61            raise IOError, "Empty range '%s'" % self.options.range
62        else:
63            self._set_data(tmp[0], tmp[1:])
64        return True
65
66    def close(self):
67        if self._data is None and not self.db is None:
68            del self.db
69
70
Note: See TracBrowser for help on using the repository browser.