Changeset 3413 for pykota/trunk/pykota/storages/pgstorage.py
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/storages/pgstorage.py
r3411 r3413 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 29 29 from pykota.storages.sql import SQLStorage 30 30 31 from pykota.utils import * 31 from pykota.utils import * 32 32 33 33 try : 34 34 import pg 35 except ImportError : 35 except ImportError : 36 36 import sys 37 37 # TODO : to translate or not to translate ? 38 38 raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0] 39 else : 39 else : 40 40 try : 41 41 PGError = pg.Error 42 except AttributeError : 42 except AttributeError : 43 43 PGError = pg.error 44 44 … … 50 50 (host, port) = host.split(":") 51 51 port = int(port) 52 except ValueError : 52 except ValueError : 53 53 port = 5432 # Use PostgreSQL's default tcp/ip port (5432). 54 54 55 55 self.tool.logdebug("Trying to open database (host=%s, port=%s, dbname=%s, user=%s)..." % (host, port, dbname, user)) 56 56 try : 57 57 self.database = pg.connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd) 58 except PGError, msg : 58 except PGError, msg : 59 59 msg = "%(msg)s --- the most probable cause of your problem is that PostgreSQL is down, or doesn't accept incoming connections because you didn't configure it as explained in PyKota's documentation." % locals() 60 60 raise PGError, msg … … 62 62 try : 63 63 self.database.query("SET CLIENT_ENCODING TO 'UTF-8';") 64 except PGError, msg : 64 except PGError, msg : 65 65 self.tool.logdebug("Impossible to set database client encoding to UTF-8 : %s" % msg) 66 66 self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) 67 68 def close(self) : 67 68 def close(self) : 69 69 """Closes the database connection.""" 70 70 if not self.closed : … … 72 72 self.closed = 1 73 73 self.tool.logdebug("Database closed.") 74 75 def beginTransaction(self) : 74 75 def beginTransaction(self) : 76 76 """Starts a transaction.""" 77 77 self.database.query("BEGIN;") 78 78 self.tool.logdebug("Transaction begins...") 79 80 def commitTransaction(self) : 79 80 def commitTransaction(self) : 81 81 """Commits a transaction.""" 82 82 self.database.query("COMMIT;") 83 83 self.tool.logdebug("Transaction committed.") 84 85 def rollbackTransaction(self) : 84 85 def rollbackTransaction(self) : 86 86 """Rollbacks a transaction.""" 87 87 self.database.query("ROLLBACK;") 88 88 self.tool.logdebug("Transaction aborted.") 89 89 90 90 def doRawSearch(self, query) : 91 91 """Does a raw search query.""" 92 query = query.strip() 93 if not query.endswith(';') : 92 query = query.strip() 93 if not query.endswith(';') : 94 94 query += ';' 95 95 try : 96 96 self.querydebug("QUERY : %s" % query) 97 97 return self.database.query(query) 98 except PGError, msg : 98 except PGError, msg : 99 99 raise PyKotaStorageError, repr(msg) 100 101 def doSearch(self, query) : 100 101 def doSearch(self, query) : 102 102 """Does a search query.""" 103 103 result = self.doRawSearch(query) 104 if (result is not None) and (result.ntuples() > 0) : 104 if (result is not None) and (result.ntuples() > 0) : 105 105 return result.dictresult() 106 106 107 107 def doModify(self, query) : 108 108 """Does a (possibly multiple) modify query.""" 109 query = query.strip() 110 if not query.endswith(';') : 109 query = query.strip() 110 if not query.endswith(';') : 111 111 query += ';' 112 112 try : 113 113 self.querydebug("QUERY : %s" % query) 114 114 return self.database.query(query) 115 except PGError, msg : 115 except PGError, msg : 116 116 self.tool.logdebug("Query failed : %s" % repr(msg)) 117 117 raise PyKotaStorageError, repr(msg) 118 118 119 119 def doQuote(self, field) : 120 120 """Quotes a field for use as a string in SQL queries.""" 121 if type(field) == type(0.0) : 121 if type(field) == type(0.0) : 122 122 typ = "decimal" 123 elif type(field) == type(0) : 123 elif type(field) == type(0) : 124 124 typ = "int" 125 elif type(field) == type(0L) : 125 elif type(field) == type(0L) : 126 126 typ = "int" 127 else : 127 else : 128 128 typ = "text" 129 129 return pg._quote(field, typ) 130 130 131 131 def prepareRawResult(self, result) : 132 132 """Prepares a raw result by including the headers.""" … … 140 140 field = fields[j] 141 141 if type(field) == StringType : 142 fields[j] = databaseToUnicode(field) 143 entries[i] = tuple(fields) 142 fields[j] = databaseToUnicode(field) 143 entries[i] = tuple(fields) 144 144 return entries 145 145