From 91d2ebfc64c9123bc81f4d1b818619eeaccafc14 Mon Sep 17 00:00:00 2001 From: jpaint Date: Tue, 27 Jun 2000 03:09:52 +0000 Subject: [PATCH] move the dbi abstraction to dbi.py; I'll deal with various errors and incompatibilities in different versions of MySQLdb here... git-svn-id: http://viewvc.tigris.org/svn/viewvc/trunk@135 8cb11bc2-c004-0410-86c3-e597b4017df7 --- lib/database.py | 24 +++++---------------- lib/dbi.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/query.py | 6 +----- viewcvs-install | 1 + 4 files changed, 63 insertions(+), 24 deletions(-) create mode 100644 lib/dbi.py diff --git a/lib/database.py b/lib/database.py index 9bae277d..cc520ffc 100644 --- a/lib/database.py +++ b/lib/database.py @@ -18,11 +18,7 @@ import sys import string import time -## imports from the database API; we re-assign the namespace here so it -## is easier to switch databases -import MySQLdb -DBI = MySQLdb - +import dbi from commit import CreateCommit, PrintCommit @@ -63,7 +59,8 @@ class CheckinDatabase: self.dbDescriptionIDCache = {} def Connect(self): - self.dbConn = self.SQLConnect() + self.dbConn = dbi.connect( + self.dbHost, self.dbUser, self.dbPasswd, self.dbDatabase) def SQLGetID(self, table, field, identifier, auto_set): sql = 'SELECT id FROM %s x WHERE x.%s="%s"' % ( @@ -244,7 +241,7 @@ class CheckinDatabase: ## module to do the conversion temp = time.localtime(commit.GetTime()) - dbCI_When = DBI.Timestamp( + dbCI_When = dbi.Timestamp( temp[0], temp[1], temp[2], temp[3], temp[4], temp[5]) dbWhoID = self.GetAuthorID(commit.GetAuthor()) @@ -404,19 +401,8 @@ class CheckinDatabase: return None return commit - - -class MySQLCheckinDatabase(CheckinDatabase): - def SQLConnect(self): - return MySQLdb.connect( - host = self.dbHost, - user = self.dbUser, - passwd = self.dbPasswd, - db = self.dbDatabase) - ## entrypoints - def CreateCheckinDatabase(host, user, passwd, database): - return MySQLCheckinDatabase(host, user, passwd, database) + return CheckinDatabase(host, user, passwd, database) diff --git a/lib/dbi.py b/lib/dbi.py new file mode 100644 index 00000000..e9b7a8fa --- /dev/null +++ b/lib/dbi.py @@ -0,0 +1,56 @@ +# -*- Mode: python -*- +# +# Copyright (C) 2000 The ViewCVS Group. All Rights Reserved. +# +# By using this file, you agree to the terms and conditions set forth in +# the LICENSE.html file which can be found at the top level of the ViewCVS +# distribution or at http://www.lyra.org/viewcvs/license-1.html. +# +# Contact information: +# Greg Stein, PO Box 760, Palo Alto, CA, 94302 +# gstein@lyra.org, http://www.lyra.org/viewcvs/ +# +# ----------------------------------------------------------------------- +# + +import MySQLdb + + +dbi_error = "dbi error" + + +class Cursor: + def __init__(self, mysql_cursor): + self.__cursor = mysql_cursor + + def execute(self, *args): + apply(self.__cursor.execute, args) + + def fetchone(self): + try: + row = self.__cursor.fetchone() + except IndexError: + row = None + + return row + + +class Connection: + def __init__(self, host, user, passwd, db): + self.__mysql = MySQLdb.connect( + host=host, user=user, passwd=passwd, db=db) + + def cursor(self): + return Cursor(self.__mysql.cursor()) + + +def Timestamp(year, month, date, hour, minute, second): + return MySQLdb.Timestamp(year, month, date, hour, minute, second) + + +def TimestampFromTicks(ticks): + return MySQLdb.TimestampFromTicks(ticks) + + +def connect(host, user, passwd, db): + return Connection(host, user, passwd, db) diff --git a/lib/query.py b/lib/query.py index 5353da4c..796a5903 100644 --- a/lib/query.py +++ b/lib/query.py @@ -14,11 +14,7 @@ # import time - -## imports from the database API; we re-assign the namespace here so it -## is easier to switch databases -import MySQLdb -DBI = MySQLdb +import dbi ## QueryEntry holds data on one match-type in the SQL database diff --git a/viewcvs-install b/viewcvs-install index f154da36..d64671a1 100755 --- a/viewcvs-install +++ b/viewcvs-install @@ -66,6 +66,7 @@ FILE_INFO_LIST = [ ("lib/config.py", "lib/config.py", 0644, 0, 0, 1), ("lib/cvsdbapi.py", "lib/cvsdbapi.py", 0644, 1, 0, 1), ("lib/database.py", "lib/database.py", 0644, 0, 0, 1), + ("lib/dbi.py", "lib/dbi.py", 0644, 0, 0, 1), ("lib/popen.py", "lib/popen.py", 0644, 0, 0, 1), ("lib/py2html.py", "lib/py2html.py", 0644, 0, 0, 1), ("lib/rlog.py", "lib/rlog.py", 0644, 1, 0, 1),