Changeset 3500
- Timestamp:
- 05/20/09 10:27:33 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/branches/1.26_fixes/pykota/storages/pgstorage.py
r3184 r3500 14 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 15 # GNU General Public License for more details. 16 # 16 # 17 17 # You should have received a copy of the GNU General Public License 18 18 # along with this program; if not, write to the Free Software … … 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 self.database = pg. connect(host=host, port=port, dbname=dbname, user=user, passwd=passwd)58 except PGError, msg : 57 self.database = pg.DB(host=host, port=port, dbname=dbname, user=user, passwd=passwd) 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 61 61 self.closed = 0 62 62 try : 63 self.quote = self.database._quote 64 except AttributeError : # pg <v4.x 65 self.quote = pg._quote 66 try : 63 67 self.database.query("SET CLIENT_ENCODING TO 'UTF-8';") 64 except PGError, msg : 68 except PGError, msg : 65 69 self.tool.logdebug("Impossible to set database client encoding to UTF-8 : %s" % msg) 66 70 self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user)) 67 68 def close(self) : 71 72 def close(self) : 69 73 """Closes the database connection.""" 70 74 if not self.closed : … … 72 76 self.closed = 1 73 77 self.tool.logdebug("Database closed.") 74 75 def beginTransaction(self) : 78 79 def beginTransaction(self) : 76 80 """Starts a transaction.""" 77 81 self.before = time.time() 78 82 self.database.query("BEGIN;") 79 83 self.tool.logdebug("Transaction begins...") 80 81 def commitTransaction(self) : 84 85 def commitTransaction(self) : 82 86 """Commits a transaction.""" 83 87 self.database.query("COMMIT;") … … 85 89 self.tool.logdebug("Transaction committed.") 86 90 #self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) 87 88 def rollbackTransaction(self) : 91 92 def rollbackTransaction(self) : 89 93 """Rollbacks a transaction.""" 90 94 self.database.query("ROLLBACK;") … … 92 96 self.tool.logdebug("Transaction aborted.") 93 97 #self.tool.logdebug("Transaction duration : %.4f seconds" % (after - self.before)) 94 98 95 99 def doRawSearch(self, query) : 96 100 """Does a raw search query.""" 97 query = query.strip() 98 if not query.endswith(';') : 101 query = query.strip() 102 if not query.endswith(';') : 99 103 query += ';' 100 104 try : … … 102 106 self.tool.logdebug("QUERY : %s" % query) 103 107 result = self.database.query(query) 104 except PGError, msg : 108 except PGError, msg : 105 109 raise PyKotaStorageError, str(msg) 106 else : 110 else : 107 111 after = time.time() 108 112 #self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) 109 113 return result 110 111 def doSearch(self, query) : 114 115 def doSearch(self, query) : 112 116 """Does a search query.""" 113 117 result = self.doRawSearch(query) 114 if (result is not None) and (result.ntuples() > 0) : 118 if (result is not None) and (result.ntuples() > 0) : 115 119 return result.dictresult() 116 120 117 121 def doModify(self, query) : 118 122 """Does a (possibly multiple) modify query.""" 119 query = query.strip() 120 if not query.endswith(';') : 123 query = query.strip() 124 if not query.endswith(';') : 121 125 query += ';' 122 126 try : … … 124 128 self.tool.logdebug("QUERY : %s" % query) 125 129 result = self.database.query(query) 126 except PGError, msg : 130 except PGError, msg : 127 131 self.tool.logdebug("Query failed : %s" % repr(msg)) 128 132 raise PyKotaStorageError, str(msg) 129 else : 133 else : 130 134 after = time.time() 131 135 #self.tool.logdebug("Query Duration : %.4f seconds" % (after - before)) 132 136 return result 133 137 134 138 def doQuote(self, field) : 135 139 """Quotes a field for use as a string in SQL queries.""" 136 if type(field) == type(0.0) : 140 if type(field) == type(0.0) : 137 141 typ = "decimal" 138 elif type(field) == type(0) : 142 elif type(field) == type(0) : 139 143 typ = "int" 140 elif type(field) == type(0L) : 144 elif type(field) == type(0L) : 141 145 typ = "int" 142 else : 146 else : 143 147 typ = "text" 144 return pg._quote(field, typ)145 148 return self.quote(field, typ) 149 146 150 def prepareRawResult(self, result) : 147 151 """Prepares a raw result by including the headers.""" … … 155 159 field = fields[j] 156 160 if type(field) == StringType : 157 fields[j] = self.databaseToUserCharset(field) 158 entries[i] = tuple(fields) 161 fields[j] = self.databaseToUserCharset(field) 162 entries[i] = tuple(fields) 159 163 return entries 160 164