backend/server/rhnSQL/__init__.py | 74 ++++++++++++++++++-----
backend/server/rhnSQL/const.py | 5 -
backend/server/rhnSQL/dbi.py | 6 +
backend/server/rhnSQL/driver_cx_Oracle.py | 76 ++++++++++++------------
backend/server/rhnSQL/driver_postgresql.py | 74 +++++++++++++----------
backend/server/rhnSQL/sql_base.py | 53 +++++++---------
backend/server/rhnSQL/sql_lib.py | 11 +--
backend/server/rhnSQL/sql_row.py | 39 ++++++------
backend/server/rhnSQL/sql_sequence.py | 23 +++----
backend/server/rhnSQL/sql_table.py | 91 +++++++++++++++--------------
backend/server/rhnSQL/sql_types.py | 7 +-
rel-eng/download-koji.sh | 8 +-
12 files changed, 265 insertions(+), 202 deletions(-)
New commits:
commit a52ac21e56b83776ed40bf440ac6aa73a9998c51
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Thu Oct 24 18:16:04 2013 +0200
Small changes
diff --git a/rel-eng/download-koji.sh b/rel-eng/download-koji.sh
index 6c81d9b..e95f7c7 100755
--- a/rel-eng/download-koji.sh
+++ b/rel-eng/download-koji.sh
@@ -1,6 +1,6 @@
-#!/bin/sh
+#!/bin/bash
-KOJI=$(echo "$1" | sed 's,\(https*://.*\)/[^/]*$,\1/,')
+KOJI=$( echo "$1" | sed 's,\(https\?://.*\)/[^/]*$,\1/,' )
WGET='wget --no-check-certificate -nv'
$WGET -O - $1 \
@@ -13,4 +13,6 @@ $WGET -O - $1 \
print "'"$KOJI"'" $2 " -O "
basename($2)}
/"http:\/\/download.*\.rpm/ && ! /src\.rpm/ {
print $2 " -O " basename($2)} ' \
- | xargs -n 3 $WGET
+ | xargs --no-run-if-empty -n 3 $WGET
+
+# rpm -Fvh *.rpm
commit 01afc927f1fb519884cfe900c4169360fcbf243c
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Fri Oct 18 17:33:31 2013 +0200
1020952 - SSL for Postgresql: Backend (Python)
diff --git a/backend/server/rhnSQL/__init__.py b/backend/server/rhnSQL/__init__.py
index f3e744b..f3f4dbf 100644
--- a/backend/server/rhnSQL/__init__.py
+++ b/backend/server/rhnSQL/__init__.py
@@ -41,7 +41,7 @@ from sql_base import SQLError, SQLSchemaError, SQLConnectError, \
# EVER be exposed to the calling applications.
-def __init__DB(backend, host, port, username, password, database):
+def __init__DB(backend, host, port, username, password, database, sslmode):
"""
Establish and check the connection so we can wrap it and handle
exceptions.
@@ -52,14 +52,14 @@ def __init__DB(backend, host, port, username, password, database):
my_db = __DB
except NameError: # __DB has not been set up
db_class = dbi.get_database_class(backend=backend)
- __DB = db_class(host, port, username, password, database)
+ __DB = db_class(host, port, username, password, database, sslmode)
__DB.connect()
return
else:
del my_db
if __DB.is_connected_to(backend, host, port, username, password,
- database):
+ database, sslmode):
__DB.check_connection()
return
@@ -67,13 +67,13 @@ def __init__DB(backend, host, port, username, password, database):
__DB.close()
# now we have to get a different connection
__DB = dbi.get_database_class(backend=backend)(
- host, port, username, password, database)
+ host, port, username, password, database, sslmode)
__DB.connect()
return 0
def initDB(backend=None, host=None, port=None, username=None,
- password=None, database=None):
+ password=None, database=None, sslmode=None):
"""
Initialize the database.
@@ -91,6 +91,7 @@ def initDB(backend=None, host=None, port=None, username=None,
database = CFG.DB_NAME
username = CFG.DB_USER
password = CFG.DB_PASSWORD
+ sslmode = CFG.DB_SSLMODE
if backend not in SUPPORTED_BACKENDS:
raise rhnException("Unsupported database backend", backend)
@@ -101,7 +102,7 @@ def initDB(backend=None, host=None, port=None, username=None,
# Hide the password
add_to_seclist(password)
try:
- __init__DB(backend, host, port, username, password, database)
+ __init__DB(backend, host, port, username, password, database, sslmode)
# except (rhnException, SQLError):
# raise # pass on, we know those ones
# except (KeyboardInterrupt, SystemExit):
diff --git a/backend/server/rhnSQL/driver_cx_Oracle.py
b/backend/server/rhnSQL/driver_cx_Oracle.py
index 8d5724b..8bad31d 100644
--- a/backend/server/rhnSQL/driver_cx_Oracle.py
+++ b/backend/server/rhnSQL/driver_cx_Oracle.py
@@ -358,11 +358,13 @@ class Database(sql_base.Database):
OracleError = cx_Oracle.DatabaseError
def __init__(self, host=None, port=None, username=None,
- password=None, database=None):
+ password=None, database=None, sslmode=None):
# Oracle requires enough info to connect
if not (username and password and database):
raise AttributeError("A valid Oracle username, password, and SID are
required.")
+ if sslmode is not None:
+ raise AttributeError("Option sslmode is not supported for Oracle
database backend.")
sql_base.Database.__init__(self)
@@ -417,7 +419,7 @@ class Database(sql_base.Database):
return dbh
def is_connected_to(self, backend, host, port, username, password,
- database):
+ database, sslmode):
# NOTE: host and port are unused for Oracle:
return (backend == ORACLE) and (self.username == username) and \
(self.password == password) and (self.database == database)
diff --git a/backend/server/rhnSQL/driver_postgresql.py
b/backend/server/rhnSQL/driver_postgresql.py
index ce7c97a..3a9eb8d 100644
--- a/backend/server/rhnSQL/driver_postgresql.py
+++ b/backend/server/rhnSQL/driver_postgresql.py
@@ -137,7 +137,16 @@ class Database(sql_base.Database):
""" Class for PostgreSQL database operations. """
def __init__(self, host=None, port=None, username=None,
- password=None, database=None):
+ password=None, database=None, sslmode=None):
+
+ self.username = username
+ self.password = password
+ self.database = database
+ self.sslmode = sslmode
+
+ # Minimum requirements to connect to a PostgreSQL db:
+ if not (self.username and self.database):
+ raise AttributeError("PostgreSQL requires at least a user and database
name.")
if host is None or host == '' or host == 'localhost':
self.host = None
@@ -150,40 +159,40 @@ class Database(sql_base.Database):
if not self.port:
self.port = -1
- self.username = username
- self.password = password
- self.database = database
-
- # Minimum requirements to connect to a PostgreSQL db:
- if not (self.username and self.database):
- raise AttributeError("PostgreSQL requires at least a user and database
name.")
-
sql_base.Database.__init__(self)
def connect(self, reconnect=1):
try:
- if self.host is None:
- self.dbh = psycopg2.connect(database=str(self.database),
user=str(self.username),
- password=str(self.password))
- else:
- self.dbh = psycopg2.connect(database=str(self.database),
user=str(self.username),
- password=str(self.password), host=self.host,
port=self.port)
+ kwargs = {
+ 'database': str(self.database),
+ 'user': str(self.username),
+ 'password': str(self.password)}
+ if self.host is not None:
+ kwargs['host'] = self.host
+ kwargs['port'] = self.port
+ if self.sslmode is not None and self.sslmode == 'verify-full':
+ kwargs['sslmode'] = str(self.sslmode)
+ elif self.sslmode is not None:
+ raise AttributeError("Only sslmode=verify-full is supported.")
+
+ self.dbh = psycopg2.connect(**kwargs)
+
# convert all DECIMAL types to float (let Python to choose one)
DEC2INTFLOAT =
psycopg2.extensions.new_type(psycopg2._psycopg.DECIMAL.values,
- 'DEC2INTFLOAT',
decimal2intfloat)
+ 'DEC2INTFLOAT',
decimal2intfloat)
psycopg2.extensions.register_type(DEC2INTFLOAT)
except Exception, e:
- if reconnect:
+ if reconnect > 0:
# Try one more time:
- return self.connect(reconnect=0)
+ return self.connect(reconnect=reconnect - 1)
# Failed reconnect, time to error out:
raise sql_base.SQLConnectError(
- self.database, e.pgcode, e.pgerror,
- "Attempting Re-Connect to the database failed"), None,
sys.exc_info()[2]
+ self.database, e.pgcode, e.pgerror,
+ "Attempting Re-Connect to the database failed"), None,
sys.exc_info()[2]
def is_connected_to(self, backend, host, port, username, password,
- database):
+ database, sslmode):
if host is None or host == '' or host == 'localhost':
host = None
port = None
@@ -191,7 +200,8 @@ class Database(sql_base.Database):
port = -1
return (backend == POSTGRESQL) and (self.host == host) and \
(self.port == port) and (self.username == username) and \
- (self.password == password) and (self.database == database)
+ (self.password == password) and (self.database == database) and \
+ (self.sslmode == sslmode)
def check_connection(self):
try:
diff --git a/backend/server/rhnSQL/sql_base.py b/backend/server/rhnSQL/sql_base.py
index 4650582..071f717 100644
--- a/backend/server/rhnSQL/sql_base.py
+++ b/backend/server/rhnSQL/sql_base.py
@@ -356,7 +356,7 @@ class Database:
return None
def is_connected_to(self, backend, host, port, username, password,
- database):
+ database, sslmode):
"""
Check if this database matches the given connection parameters.
"""
commit 5f924106873af856a7c3d802a1ed4b9e1316d7d3
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Thu Oct 17 20:46:19 2013 +0200
Simplification
diff --git a/backend/server/rhnSQL/sql_base.py b/backend/server/rhnSQL/sql_base.py
index ad17c1c..4650582 100644
--- a/backend/server/rhnSQL/sql_base.py
+++ b/backend/server/rhnSQL/sql_base.py
@@ -52,9 +52,7 @@ def __oci_name_value(names, value):
# this is for when an execute statement went bad...
class SQLError(Exception):
- def __init__(self, *args):
- apply(Exception.__init__, (self, ) + args)
-
+ pass
# other Schema Errors
diff --git a/backend/server/rhnSQL/sql_row.py b/backend/server/rhnSQL/sql_row.py
index 444572e..d0a5419 100644
--- a/backend/server/rhnSQL/sql_row.py
+++ b/backend/server/rhnSQL/sql_row.py
@@ -81,9 +81,9 @@ class Row(UserDictCase):
def reset(self, val=0):
""" reset the changed status for these entries """
- for k in self.data.keys():
+ for k, v in self.data.items():
# tuples do not support item assignement
- self.data[k] = (self.data[k][0], val)
+ self.data[k] = (v[0], val)
def create(self, hashval):
""" create it as a new entry """
@@ -104,8 +104,8 @@ class Row(UserDictCase):
if not ret:
self.real = 0
return 0
- for k in ret.keys():
- self.data[k] = (ret[k], 0)
+ for k, v in ret.items():
+ self.data[k] = (v, 0)
self.real = 1
return 1
commit 34ba98264c70235e8b965aa92f1f82e4e776fb5f
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Thu Oct 17 20:44:00 2013 +0200
Simplification: use isinstance
instead of complicated construction with "type"
diff --git a/backend/server/rhnSQL/sql_sequence.py
b/backend/server/rhnSQL/sql_sequence.py
index df6b073..539a88c 100644
--- a/backend/server/rhnSQL/sql_sequence.py
+++ b/backend/server/rhnSQL/sql_sequence.py
@@ -24,7 +24,7 @@ import sql_base
# XXX: this is still Oracle specific, but it shouldn't be
class Sequence:
def __init__(self, db, seq):
- if not seq or type(seq) != type(""):
+ if not seq or not isinstance(seq, str):
raise rhnException("First argument needs to be a sequence name",
seq)
self.__seq = seq
if not isinstance(db, sql_base.Database):
diff --git a/backend/server/rhnSQL/sql_table.py b/backend/server/rhnSQL/sql_table.py
index fd8ae21..5143819 100644
--- a/backend/server/rhnSQL/sql_table.py
+++ b/backend/server/rhnSQL/sql_table.py
@@ -61,11 +61,11 @@ class RowData(UserDictCase):
# Some day we'll figure out how to reduce confusion...
class Table:
def __init__(self, db, table, hashid, cache=False):
- if not table or not type(table) == type(""):
+ if not table or not isinstance(table, str):
raise rhnException("First argument needs to be a table name",
table)
self.__table = table
- if not hashid or not type(hashid) == type(""):
+ if not hashid or not isinstance(hashid, str):
raise rhnException("Second argument needs to be the name of the unique
index column",
hashid)
self.__hashid = hashid
@@ -91,9 +91,9 @@ class Table:
if self.__cache is not None:
self.__cache[row[self.__hashid]] = row
return self.__setitem__(None, row)
- if type(rows) == type({}) or isinstance(rows, UserDictCase):
+ if isinstance(rows, dict) or isinstance(rows, UserDictCase):
return insert_row(rows)
- if type(rows) == type([]):
+ if isinstance(rows, list):
for x in rows:
insert_row(x)
return None
@@ -102,7 +102,7 @@ class Table:
# select from the whole table all the entries that match the
# valuies of the hash provided (kind of a complex select)
def select(self, row):
- if not type(row) == type({}) and not isinstance(row, UserDictCase):
+ if not isinstance(row, dict) and not isinstance(row, UserDictCase):
raise rhnException("Expecting hash argument. %s is invalid" %
type(row),
row)
if row == {}:
@@ -166,7 +166,7 @@ class Table:
# values for all columns except the one that functions as the
# primary key identifier
def __setitem__(self, key, value):
- if not type(value) == type({}) and not isinstance(value, UserDictCase):
+ if not isinstance(value, dict) and not isinstance(value, UserDictCase):
raise TypeError("Expected value to be a hash")
if self.__hashid in value: # we don't need that
if key is None:
commit 1ae50700dfaffed1bcf031815b1c5555e244bff1
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Thu Oct 17 20:27:16 2013 +0200
Various small coding convetions (PEP8)
I.e.
* visual indentation
* indendtation multiple of four
* spaces around operator
* 2 spaces before inline comment
* 2 empty lines between top level definitions
* 1 empty line between local definitions
* ...
Conflicts:
backend/server/rhnSQL/driver_cx_Oracle.py
backend/server/rhnSQL/sql_base.py
diff --git a/backend/server/rhnSQL/__init__.py b/backend/server/rhnSQL/__init__.py
index 7c4b2e9..f3e744b 100644
--- a/backend/server/rhnSQL/__init__.py
+++ b/backend/server/rhnSQL/__init__.py
@@ -40,6 +40,7 @@ from sql_base import SQLError, SQLSchemaError, SQLConnectError, \
# instantiated by the initDB call. This object/instance should NEVER,
# EVER be exposed to the calling applications.
+
def __init__DB(backend, host, port, username, password, database):
"""
Establish and check the connection so we can wrap it and handle
@@ -49,7 +50,7 @@ def __init__DB(backend, host, port, username, password, database):
global __DB
try:
my_db = __DB
- except NameError: # __DB has not been set up
+ except NameError: # __DB has not been set up
db_class = dbi.get_database_class(backend=backend)
__DB = db_class(host, port, username, password, database)
__DB.connect()
@@ -58,20 +59,21 @@ def __init__DB(backend, host, port, username, password, database):
del my_db
if __DB.is_connected_to(backend, host, port, username, password,
- database):
+ database):
__DB.check_connection()
return
__DB.commit()
__DB.close()
# now we have to get a different connection
- __DB = dbi.get_database_class(backend=backend)(host, port, username,
- password, database)
+ __DB = dbi.get_database_class(backend=backend)(
+ host, port, username, password, database)
__DB.connect()
return 0
+
def initDB(backend=None, host=None, port=None, username=None,
- password=None, database=None):
+ password=None, database=None):
"""
Initialize the database.
@@ -101,7 +103,7 @@ def initDB(backend=None, host=None, port=None, username=None,
try:
__init__DB(backend, host, port, username, password, database)
# except (rhnException, SQLError):
-# raise # pass on, we know those ones
+# raise # pass on, we know those ones
# except (KeyboardInterrupt, SystemExit):
# raise
except:
@@ -111,6 +113,7 @@ def initDB(backend=None, host=None, port=None, username=None,
# str(e_type), str(e_value))
return 0
+
# close the database
def closeDB():
global __DB
@@ -125,6 +128,7 @@ def closeDB():
del __DB
return
+
# common function for testing the connection state (ie, __DB defined
def __test_DB():
global __DB
@@ -139,73 +143,105 @@ def Procedure(name):
db = __test_DB()
return db.procedure(name)
+
# wrapper for a Procedure callable class
def Function(name, ret_type):
db = __test_DB()
return db.function(name, ret_type)
+
# Wrapper for the Sequence class
def Sequence(seq):
db = __test_DB()
return sql_sequence.Sequence(db, seq)
+
# Wrapper for the Row class
def Row(table, hash_name, hash_value=None):
db = __test_DB()
return sql_row.Row(db, table, hash_name, hash_value)
+
# Wrapper for the Table class
def Table(table, hash_name, local_cache=0):
db = __test_DB()
return sql_table.Table(db, table, hash_name, local_cache)
+
+###########################
# Functions points of entry
+###########################
+
+
def cursor():
db = __test_DB()
return db.cursor()
+
+
def prepare(sql, blob_map=None):
db = __test_DB()
if isinstance(sql, Statement):
sql = sql.statement
return db.prepare(sql, blob_map=blob_map)
+
+
def execute(sql, *args, **kwargs):
db = __test_DB()
return apply(db.execute, (sql, ) + args, kwargs)
+
+
def fetchall_dict(sql, *args, **kwargs):
h = prepare(sql)
h.execute(sql, *args, **kwargs)
return h.fetchall_dict()
+
+
def fetchone_dict(sql, *args, **kwargs):
h = prepare(sql)
h.execute(sql, *args, **kwargs)
return h.fetchone_dict()
+
+
def commit():
db = __test_DB()
return db.commit()
+
+
def rollback(name=None):
db = __test_DB()
return db.rollback(name)
+
+
def transaction(name):
db = __test_DB()
return db.transaction(name)
+
+
def TimestampFromTicks(*args, **kwargs):
db = __test_DB()
return apply(db.TimestampFromTicks, args, kwargs)
+
+
def DateFromTicks(*args, **kwargs):
db = __test_DB()
return apply(db.DateFromTicks, args, kwargs)
+
+
def Date(*args, **kwargs):
db = __test_DB()
return apply(db.Date, args, kwargs)
+
def clear_log_id():
clear_log_id = Procedure("logging.clear_log_id")
clear_log_id()
+
def set_log_auth(user_id):
set_log_auth = Procedure("logging.set_log_auth")
set_log_auth(user_id)
+
def set_log_auth_login(login):
h = prepare("select id from web_contact_all where login = :login")
h.execute(login=login)
@@ -216,6 +252,7 @@ def set_log_auth_login(login):
else:
raise rhnException("No such log user", login)
+
def read_lob(lob):
if not lob:
return None
@@ -238,9 +275,10 @@ class _Callable(object):
class _Procedure(_Callable):
- def __init__(self, name):
- _Callable.__init__(self, name)
- self._implementor = Procedure
+ def __init__(self, name):
+ _Callable.__init__(self, name)
+ self._implementor = Procedure
+
class _Function(_Callable):
def __init__(self, name):
diff --git a/backend/server/rhnSQL/const.py b/backend/server/rhnSQL/const.py
index 9dc9fdb..4114efc 100644
--- a/backend/server/rhnSQL/const.py
+++ b/backend/server/rhnSQL/const.py
@@ -20,7 +20,6 @@ POSTGRESQL = "postgresql"
# Backend constants:
SUPPORTED_BACKENDS = {
- ORACLE: "",
- POSTGRESQL: "",
+ ORACLE: "",
+ POSTGRESQL: "",
}
-
diff --git a/backend/server/rhnSQL/dbi.py b/backend/server/rhnSQL/dbi.py
index 50d315c..cb5e35a 100644
--- a/backend/server/rhnSQL/dbi.py
+++ b/backend/server/rhnSQL/dbi.py
@@ -18,10 +18,11 @@ from const import ORACLE, POSTGRESQL
# Map supported backend constants to a specific Python driver:
BACKEND_DRIVERS = {
- ORACLE: "cx_Oracle",
- POSTGRESQL: "postgresql",
+ ORACLE: "cx_Oracle",
+ POSTGRESQL: "postgresql",
}
+
def get_database_module(backend=None):
"""Loads the database driver module, performing autodetection if
needed"""
@@ -41,6 +42,7 @@ def get_database_module(backend=None):
return module
+
def get_database_class(backend=None):
"""Returns the database class"""
module = get_database_module(backend=backend)
diff --git a/backend/server/rhnSQL/driver_cx_Oracle.py
b/backend/server/rhnSQL/driver_cx_Oracle.py
index 547adbc..8d5724b 100644
--- a/backend/server/rhnSQL/driver_cx_Oracle.py
+++ b/backend/server/rhnSQL/driver_cx_Oracle.py
@@ -44,6 +44,7 @@ ORACLE_TYPE_MAPPING = [
(sql_types.LONG_BINARY, cx_Oracle.LONG_BINARY),
]
+
class Cursor(sql_base.Cursor):
"""
Wrapper that should just transform Oracle specific exceptions into
@@ -55,7 +56,7 @@ class Cursor(sql_base.Cursor):
try:
sql_base.Cursor.__init__(self, dbh=dbh, sql=sql,
- force=force)
+ force=force)
self._type_mapping = ORACLE_TYPE_MAPPING
self.blob_map = blob_map
except sql_base.SQLSchemaError, e:
@@ -89,19 +90,19 @@ class Cursor(sql_base.Cursor):
return cursor
def _execute_wrapper(self, function, *p, **kw):
- params = ','.join(["%s: %s" % (repr(key), repr(value)) for
key, value \
- in kw.items()])
+ params = ','.join(["%s: %s" % (repr(key), repr(value)) for key,
value
+ in kw.items()])
log_debug(5, "Executing SQL: \"%s\" with bind params: {%s}"
- % (self.sql, params))
+ % (self.sql, params))
if self.sql is None:
raise rhnException("Cannot execute empty cursor")
if self.blob_map:
blob_content = {}
for orig_blob_var in self.blob_map.keys():
- new_blob_var = orig_blob_var + '_blob'
- blob_content[new_blob_var] = kw[orig_blob_var]
- kw[new_blob_var] = self.var(cx_Oracle.BLOB)
- del kw[orig_blob_var]
+ new_blob_var = orig_blob_var + '_blob'
+ blob_content[new_blob_var] = kw[orig_blob_var]
+ kw[new_blob_var] = self.var(cx_Oracle.BLOB)
+ del kw[orig_blob_var]
modified_params = self._munge_args(kw)
try:
@@ -114,26 +115,26 @@ class Cursor(sql_base.Cursor):
if 900 <= errno <= 999:
# Per Oracle's documentation, SQL parsing error
raise sql_base.SQLStatementPrepareError(errno, errmsg, self.sql), None,
sys.exc_info()[2]
- if errno == 1475: # statement needs to be reparsed; force a prepare again
- if self.reparsed: # useless, tried that already. give up
+ if errno == 1475: # statement needs to be reparsed; force a prepare again
+ if self.reparsed: # useless, tried that already. give up
log_error("Reparsing cursor did not fix it", self.sql)
args = ("Reparsing tried and still got this",) +
tuple(ret)
raise sql_base.SQLError(*args), None, sys.exc_info()[2]
self._real_cursor = self.dbh.prepare(self.sql)
self.reparsed = 1
apply(self._execute_wrapper, (function, ) + p, kw)
- elif 20000 <= errno <= 20999: # error codes we know we raise as schema
errors
+ elif 20000 <= errno <= 20999: # error codes we know we raise as schema
errors
raise sql_base.SQLSchemaError(*ret), None, sys.exc_info()[2]
raise apply(sql_base.SQLError, ret), None, sys.exc_info()[2]
except ValueError:
# this is not good.Let the user know
raise
else:
- self.reparsed = 0 # reset the reparsed counter
+ self.reparsed = 0 # reset the reparsed counter
if self.blob_map:
for blob_var, content in blob_content.items():
- kw[blob_var].getvalue().write(content)
+ kw[blob_var].getvalue().write(content)
# Munge back the values
self._unmunge_args(kw, modified_params)
return retval
@@ -194,7 +195,7 @@ class Cursor(sql_base.Cursor):
for i in xrange(item_count):
pdict = arr[i]
for k, v in kwargs.iteritems():
- pdict[k] = to_string(v[start+i])
+ pdict[k] = to_string(v[start + i])
# We clear self->bindVariables so that list of all nulls
# in the previous chunk which caused the type to be set to
@@ -264,10 +265,10 @@ class Cursor(sql_base.Cursor):
return modified
def update_blob(self, table_name, column_name, where_clause, data,
- **kwargs):
+ **kwargs):
sql = "SELECT %s FROM %s %s FOR update of %s" % \
(column_name, table_name, where_clause, column_name)
- c= rhnSQL.prepare(sql)
+ c = rhnSQL.prepare(sql)
apply(c.execute, (), kwargs)
row = c.fetchone_dict()
blob = row[column_name]
@@ -293,9 +294,9 @@ class Procedure(sql_base.Procedure):
except cx_Oracle.DatabaseError, e:
if not hasattr(e, "args"):
raise sql_base.SQLError(self.name, args), None, sys.exc_info()[2]
- elif 20000 <= e[0].code <= 20999: # error codes we know we raise as
schema errors
+ elif 20000 <= e[0].code <= 20999: # error codes we know we raise as
schema errors
- raise sql_base.SQLSchemaError(e[0].code, str(e[0])), None,
sys.exc_info()[2]
+ raise sql_base.SQLSchemaError(e[0].code, str(e[0])), None,
sys.exc_info()[2]
raise sql_base.SQLError(e[0].code, str(e[0])), None, sys.exc_info()[2]
except cx_Oracle.NotSupportedError, error:
raise sql_base.SQLError(*error.args), None, sys.exc_info()[2]
@@ -341,7 +342,6 @@ class Procedure(sql_base.Procedure):
return self.cursor.callproc(self.name, args)
-
class Function(Procedure):
def __init__(self, name, proc, ret_type):
Procedure.__init__(self, name, proc)
@@ -351,7 +351,6 @@ class Function(Procedure):
return self._call_proc_ret(args, self.ret_type)
-
class Database(sql_base.Database):
_cursor_class = Cursor
_procedure_class = Procedure
@@ -359,7 +358,7 @@ class Database(sql_base.Database):
OracleError = cx_Oracle.DatabaseError
def __init__(self, host=None, port=None, username=None,
- password=None, database=None):
+ password=None, database=None):
# Oracle requires enough info to connect
if not (username and password and database):
@@ -396,7 +395,7 @@ class Database(sql_base.Database):
# we don't try to reconnect blindly. We have a list of
# known "good" failure codes that warrant a reconnect
# attempt
- if errno in [ 12547 ] : # lost contact
+ if errno in [12547]: # lost contact
return self.connect(reconnect=0)
err_args = [self.dbtxt, errno, errmsg]
err_args.extend(list(ret[2:]))
@@ -412,13 +411,13 @@ class Database(sql_base.Database):
def _connect(self):
dbh = cx_Oracle.Connection(self.username, self.password, self.database)
if hasattr(sys, "argv"):
- dbh.cursor().execute(
- "BEGIN DBMS_APPLICATION_INFO.SET_MODULE('%s',NULL);
END;"
- % sys.argv[0])
+ dbh.cursor().execute(
+ "BEGIN DBMS_APPLICATION_INFO.SET_MODULE('%s',NULL);
END;"
+ % sys.argv[0])
return dbh
def is_connected_to(self, backend, host, port, username, password,
- database):
+ database):
# NOTE: host and port are unused for Oracle:
return (backend == ORACLE) and (self.username == username) and \
(self.password == password) and (self.database == database)
@@ -459,11 +458,11 @@ class Database(sql_base.Database):
col_list = []
bind_list = []
for bind_var, column in blob_map.items():
- r=re.compile(":%s" % bind_var)
+ r = re.compile(":%s" % bind_var)
sql = re.sub(r, 'empty_blob()', sql)
col_list.append(column)
bind_list.append(":%s_blob" % bind_var)
- sql += " returning %s into %s" %(','.join(col_list),
','.join(bind_list))
+ sql += " returning %s into %s" % (','.join(col_list),
','.join(bind_list))
# this way we only hit the network once for each sql statement
return self._cursor_class(dbh=self.dbh, sql=sql, force=force, blob_map=blob_map)
@@ -502,7 +501,7 @@ class Database(sql_base.Database):
def rollback(self, name=None):
log_debug(3, self.dbtxt, name)
- if name: # we need to roll back to a savepoint
+ if name: # we need to roll back to a savepoint
return self.execute("rollback to savepoint %s" % name)
return self.dbh.rollback()
@@ -510,10 +509,10 @@ class Database(sql_base.Database):
try:
h = self.prepare("select 1 from dual")
h.execute()
- except: # try to reconnect, that one MUST WORK always
+ except: # try to reconnect, that one MUST WORK always
log_error("DATABASE CONNECTION TO '%s' LOST" % self.dbtxt,
"Exception information: %s" % sys.exc_info()[1])
- self.connect() # only allow one try
+ self.connect() # only allow one try
return 0
# function that attempts to fix the environment variables
@@ -552,4 +551,3 @@ class Database(sql_base.Database):
def DatetimeFromTicks(self, ticks):
return cx_Oracle.DatetimeFromTicks(ticks)
-
diff --git a/backend/server/rhnSQL/driver_postgresql.py
b/backend/server/rhnSQL/driver_postgresql.py
index 326a27b..ce7c97a 100644
--- a/backend/server/rhnSQL/driver_postgresql.py
+++ b/backend/server/rhnSQL/driver_postgresql.py
@@ -36,6 +36,7 @@ from spacewalk.common.rhnLog import log_debug, log_error
from spacewalk.common.rhnException import rhnException
from const import POSTGRESQL
+
def convert_named_query_params(query):
"""
Convert a query with named parameters (i.e. :id, :name, etc) into one
@@ -131,6 +132,7 @@ def decimal2intfloat(dec, cursor):
except ValueError:
return float(dec)
+
class Database(sql_base.Database):
""" Class for PostgreSQL database operations. """
@@ -188,17 +190,17 @@ class Database(sql_base.Database):
if not port:
port = -1
return (backend == POSTGRESQL) and (self.host == host) and \
- (self.port == port) and (self.username == username) \
- and (self.password == password) and (self.database == database)
+ (self.port == port) and (self.username == username) and \
+ (self.password == password) and (self.database == database)
def check_connection(self):
try:
c = self.prepare("select 1")
c.execute()
- except: # try to reconnect, that one MUST WORK always
+ except: # try to reconnect, that one MUST WORK always
log_error("DATABASE CONNECTION TO '%s' LOST" %
self.database,
"Exception information: %s" % sys.exc_info()[1])
- self.connect() # only allow one try
+ self.connect() # only allow one try
def prepare(self, sql, force=0, blob_map=None):
return Cursor(dbh=self.dbh, sql=sql, force=force, blob_map=blob_map)
@@ -240,7 +242,6 @@ class Database(sql_base.Database):
return str(lob)
-
class Cursor(sql_base.Cursor):
""" PostgreSQL specific wrapper over sql_base.Cursor.
"""
@@ -261,15 +262,15 @@ class Cursor(sql_base.Cursor):
return cursor
def _execute_wrapper(self, function, *p, **kw):
- params = ','.join(["%s: %s" % (key, value) for key, value \
- in kw.items()])
+ params = ','.join(["%s: %s" % (key, value) for key, value
+ in kw.items()])
log_debug(5, "Executing SQL: \"%s\" with bind params: {%s}"
% (self.sql, params))
if self.sql is None:
raise rhnException("Cannot execute empty cursor")
if self.blob_map:
for blob_var in self.blob_map.keys():
- kw[blob_var] = buffer(kw[blob_var])
+ kw[blob_var] = buffer(kw[blob_var])
try:
retval = apply(function, p, kw)
@@ -277,7 +278,7 @@ class Cursor(sql_base.Cursor):
error_code = 99999
m = re.match('ERROR: +-([0-9]+)', e.pgerror)
if m:
- error_code = int(m.group(1))
+ error_code = int(m.group(1))
raise sql_base.SQLSchemaError(error_code, e.pgerror, e)
except psycopg2.ProgrammingError, e:
raise sql_base.SQLStatementPrepareError(self.dbh, e.pgerror, self.sql)
diff --git a/backend/server/rhnSQL/sql_base.py b/backend/server/rhnSQL/sql_base.py
index 5fd3c38..ad17c1c 100644
--- a/backend/server/rhnSQL/sql_base.py
+++ b/backend/server/rhnSQL/sql_base.py
@@ -28,6 +28,7 @@ import sys
import sql_types
import types
+
def ociDict(names=None, row=None):
""" Create a dictionary from a row description and its values.
"""
data = {}
@@ -40,6 +41,7 @@ def ociDict(names=None, row=None):
data[name] = value
return data
+
def __oci_name_value(names, value):
""" Extract the name, value pair needed by ociDict function.
"""
# the format of the names is
@@ -47,6 +49,7 @@ def __oci_name_value(names, value):
name = name.lower()
return name, value
+
# this is for when an execute statement went bad...
class SQLError(Exception):
def __init__(self, *args):
@@ -58,14 +61,13 @@ class SQLError(Exception):
class SQLSchemaError(SQLError):
def __init__(self, errno, errmsg, *args):
self.errno = errno
- (self.errmsg, errmsg) = string.split(errmsg, '\n', 1)
+ (self.errmsg, errmsg) = string.split(errmsg, '\n', 1)
if len(args):
apply(SQLError.__init__, (self, self.errno, self.errmsg, errmsg) + args)
else:
apply(SQLError.__init__, (self, errno, self.errmsg) + (errmsg,))
-
# SQL connect error
class SQLConnectError(SQLError):
def __init__(self, db, errno, errmsg, *args):
@@ -78,7 +80,6 @@ class SQLConnectError(SQLError):
SQLError.__init__(self, errno, errmsg, db)
-
# Cannot prepare statement
class SQLStatementPrepareError(SQLError):
def __init__(self, db, errmsg, *args):
@@ -87,12 +88,10 @@ class SQLStatementPrepareError(SQLError):
apply(SQLError.__init__, (self, errmsg, db) + args)
-
class ModifiedRowError(SQLError):
pass
-
class Cursor:
""" A class to implement generic SQL Cursor operations.
"""
@@ -220,7 +219,6 @@ class Cursor:
# Please note: these functions return None if no data is available,
# not an empty tuple or a list of empty tuples, or an empty list
# or any other combination you can imagine with the word "empty" in it.
-
def fetchone(self):
return self._real_cursor.fetchone()
@@ -260,7 +258,6 @@ class Cursor:
return 0
-
class Procedure:
"""
Class for calling out to stored procedures.
@@ -280,7 +277,6 @@ class Procedure:
self.cursor = None
-
class Database:
"""
Base class for handling database operations.
@@ -362,7 +358,7 @@ class Database:
return None
def is_connected_to(self, backend, host, port, username, password,
- database):
+ database):
"""
Check if this database matches the given connection parameters.
"""
diff --git a/backend/server/rhnSQL/sql_lib.py b/backend/server/rhnSQL/sql_lib.py
index ae376a3..6540a30 100644
--- a/backend/server/rhnSQL/sql_lib.py
+++ b/backend/server/rhnSQL/sql_lib.py
@@ -23,6 +23,7 @@
import string
+
def build_sql_insert(table, hash_name, items):
""" This statement builds a sql statement for an insert
of 'items' into "table" indexed by "hash_name"
@@ -31,10 +32,11 @@ def build_sql_insert(table, hash_name, items):
table, hash_name,
string.join(map(lambda a: a[0], items), ", "),
string.join(map(lambda a: ":p_%s" % a[0], items), ", "))
- pdict = { "p0" : None } # This must be reset after we return from this
call
- map(pdict.update, map(lambda a : { "p_%s" % a[0] : a[1] }, items))
+ pdict = {"p0": None} # This must be reset after we return from this call
+ map(pdict.update, map(lambda a: {"p_%s" % a[0]: a[1]}, items))
return sql, pdict
+
def build_sql_update(table, hash_name, items):
""" This statement builds a sql statement for an update
of 'items' into "table" indexed by "hash_name"
@@ -45,7 +47,6 @@ def build_sql_update(table, hash_name, items):
map(lambda a: a[0], items)),
", "),
hash_name)
- pdict = { "p0" : None } # This must be reset after we return from this
call
- map(pdict.update, map(lambda a : { "p_%s" % a[0] : a[1] }, items))
+ pdict = {"p0": None} # This must be reset after we return from this call
+ map(pdict.update, map(lambda a: {"p_%s" % a[0]: a[1]}, items))
return sql, pdict
-
diff --git a/backend/server/rhnSQL/sql_row.py b/backend/server/rhnSQL/sql_row.py
index bc0653d..444572e 100644
--- a/backend/server/rhnSQL/sql_row.py
+++ b/backend/server/rhnSQL/sql_row.py
@@ -23,6 +23,7 @@ from spacewalk.common.rhnException import rhnException
import sql_base
import sql_lib
+
class Row(UserDictCase):
""" This class allows one to work with the columns of a particular row
in a more
convenient manner (ie, using a disctionary interface). It allows for the row
@@ -45,7 +46,7 @@ class Row(UserDictCase):
self.data = {}
# is this a real entry (ie, use insert or update)
self.real = 0
- if hashval is not None: # if we have to load an entry already...
+ if hashval is not None: # if we have to load an entry already...
self.load(hashval)
def __repr__(self):
@@ -65,6 +66,7 @@ class Row(UserDictCase):
raise AttributeError("Can not reset the value of the hash key")
if x not in self.data or self.data[x][0] != value:
self.data[x] = (value, 1)
+
def __getitem__(self, name):
x = string.lower(name)
if x in self.data:
@@ -114,10 +116,10 @@ class Row(UserDictCase):
# get a list of fields to be set
items = map(lambda a: (a[0], a[1][0]),
filter(lambda b: b[1][1] == 1, self.data.items()))
- if not items: # if there is nothing for us to do, avoid doing it.
+ if not items: # if there is nothing for us to do, avoid doing it.
return
# and now build the SQL statements
- if self.real: # Update
+ if self.real: # Update
if not with_updates:
raise sql_base.ModifiedRowError()
sql, pdict = sql_lib.build_sql_update(self.table, self.hashname, items)
@@ -129,4 +131,3 @@ class Row(UserDictCase):
apply(h.execute, (), pdict)
self.real = 1
return
-
diff --git a/backend/server/rhnSQL/sql_sequence.py
b/backend/server/rhnSQL/sql_sequence.py
index d724866..df6b073 100644
--- a/backend/server/rhnSQL/sql_sequence.py
+++ b/backend/server/rhnSQL/sql_sequence.py
@@ -19,6 +19,7 @@ from spacewalk.common.rhnException import rhnException
import sql_base
+
# A class to handle sequences
# XXX: this is still Oracle specific, but it shouldn't be
class Sequence:
@@ -35,7 +36,7 @@ class Sequence:
cursor = self.__db.prepare(sql)
cursor.execute()
ret = cursor.fetchone_dict()
- if ret is None: # how the hell can this happen?
+ if ret is None: # how the hell can this happen?
return ret
return int(ret['id'])
diff --git a/backend/server/rhnSQL/sql_table.py b/backend/server/rhnSQL/sql_table.py
index f1a7945..fd8ae21 100644
--- a/backend/server/rhnSQL/sql_table.py
+++ b/backend/server/rhnSQL/sql_table.py
@@ -43,9 +43,12 @@ class RowData(UserDictCase):
h.execute(new_val=value, row_id=self.__rowid)
# keep self.data in sync
self.data[key] = value
- if self.__cache: # maintain cache consistency
- try: self.__cache[self.__rowid][key] = value
- except: pass
+ if self.__cache: # maintain cache consistency
+ try:
+ self.__cache[self.__rowid][key] = value
+ except:
+ pass
+
# A class to handle operations on a table.
#
@@ -77,7 +80,7 @@ class Table:
if not value:
self.__cache = None
return
- if self.__cache is not None: # already enabled
+ if self.__cache is not None: # already enabled
return
self.__cache = {}
@@ -173,7 +176,7 @@ class Table:
if key is None:
raise KeyError("Can not insert entry with NULL key")
items = value.items()
- if items == []: # quick check for noop
+ if items == []: # quick check for noop
return
sql = None
if self.has_key(key):
@@ -181,7 +184,7 @@ class Table:
else:
sql, pdict = sql_lib.build_sql_insert(self.__table, self.__hashid, items)
# import the value of the hash key
- pdict["p0"] = key
+ pdict["p0"] = key
h = self.__db.prepare(sql)
apply(h.execute, (), pdict)
try:
@@ -204,8 +207,10 @@ class Table:
h = self.__db.prepare("delete from %s where %s = :p1" % (
self.__table, self.__hashid))
h.execute(p1=key)
- try: del self.__cache[key]
- except: pass
+ try:
+ del self.__cache[key]
+ except:
+ pass
return 0
# get all keys
@@ -240,12 +245,13 @@ class Table:
# flush the cache. if cache is off, then noop
def flush(self):
- if self.__cache is not None: # avoid turning caching on when flushing
+ if self.__cache is not None: # avoid turning caching on when flushing
self.__cache = {}
# passthrough commit
def commit(self):
return self.__db.commit()
+
# passthrough rollback
def rollback(self):
self.flush()
diff --git a/backend/server/rhnSQL/sql_types.py b/backend/server/rhnSQL/sql_types.py
index 1a6057b..7898149 100644
--- a/backend/server/rhnSQL/sql_types.py
+++ b/backend/server/rhnSQL/sql_types.py
@@ -16,9 +16,11 @@
# Database types we support for out variables
#
+
# Data types
class DatabaseDataType:
type_name = None
+
def __init__(self, value=None, size=None):
self.size = size or 1
self.set_value(value)
@@ -36,18 +38,21 @@ class DatabaseDataType:
class NUMBER(DatabaseDataType):
type_name = "NUMBER"
+
class STRING(DatabaseDataType):
type_name = "STRING"
+
def __init__(self, value=None, size=None):
DatabaseDataType.__init__(self, value=value, size=size)
if not size:
self.size = 4000
+
class BINARY(DatabaseDataType):
type_name = "BINARY"
+
class LONG_BINARY(DatabaseDataType):
type_name = "LONG_BINARY"
# XXX More data types to be added as we find need for them
-
commit 8ac869af32eeea8862bc484f2c7466582c890d82
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Thu Oct 17 20:16:59 2013 +0200
Test for None with `is`
diff --git a/backend/server/rhnSQL/driver_postgresql.py
b/backend/server/rhnSQL/driver_postgresql.py
index beada98..326a27b 100644
--- a/backend/server/rhnSQL/driver_postgresql.py
+++ b/backend/server/rhnSQL/driver_postgresql.py
@@ -91,7 +91,7 @@ class Function(sql_base.Procedure):
error_code = int(m.group(1))
raise sql_base.SQLSchemaError(error_code, e.pgerror, e)
- if self.ret_type == None:
+ if self.ret_type is None:
return ret
else:
return self.cursor.fetchone()[0]
commit 983455878c7e46d2028333cf48aab461e6ccad86
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Thu Oct 17 20:14:12 2013 +0200
Older Class to newer Instance exceptions
diff --git a/backend/server/rhnSQL/__init__.py b/backend/server/rhnSQL/__init__.py
index d632086..7c4b2e9 100644
--- a/backend/server/rhnSQL/__init__.py
+++ b/backend/server/rhnSQL/__init__.py
@@ -131,7 +131,8 @@ def __test_DB():
try:
return __DB
except NameError:
- raise SystemError, "Not connected to any database!", sys.exc_info()[2]
+ raise SystemError("Not connected to any database!"), None,
sys.exc_info()[2]
+
# wrapper for a Procedure callable class
def Procedure(name):
diff --git a/backend/server/rhnSQL/driver_cx_Oracle.py
b/backend/server/rhnSQL/driver_cx_Oracle.py
index 0bfa6b6..547adbc 100644
--- a/backend/server/rhnSQL/driver_cx_Oracle.py
+++ b/backend/server/rhnSQL/driver_cx_Oracle.py
@@ -224,7 +224,7 @@ class Cursor(sql_base.Cursor):
def __getattr__(self, name):
if hasattr(self._real_cursor, name):
return getattr(self._real_cursor, name)
- raise AttributeError, name
+ raise AttributeError(name)
# deletion of the object
def __del__(self):
@@ -363,7 +363,7 @@ class Database(sql_base.Database):
# Oracle requires enough info to connect
if not (username and password and database):
- raise AttributeError, "A valid Oracle username, password, and SID are
required."
+ raise AttributeError("A valid Oracle username, password, and SID are
required.")
sql_base.Database.__init__(self)
diff --git a/backend/server/rhnSQL/driver_postgresql.py
b/backend/server/rhnSQL/driver_postgresql.py
index 62b1057..beada98 100644
--- a/backend/server/rhnSQL/driver_postgresql.py
+++ b/backend/server/rhnSQL/driver_postgresql.py
@@ -154,7 +154,7 @@ class Database(sql_base.Database):
# Minimum requirements to connect to a PostgreSQL db:
if not (self.username and self.database):
- raise AttributeError, "PostgreSQL requires at least a user and database
name."
+ raise AttributeError("PostgreSQL requires at least a user and database
name.")
sql_base.Database.__init__(self)
diff --git a/backend/server/rhnSQL/sql_base.py b/backend/server/rhnSQL/sql_base.py
index 0a56ddd..5fd3c38 100644
--- a/backend/server/rhnSQL/sql_base.py
+++ b/backend/server/rhnSQL/sql_base.py
@@ -32,7 +32,7 @@ def ociDict(names=None, row=None):
""" Create a dictionary from a row description and its values.
"""
data = {}
if not names:
- raise AttributeError, "Class initialization requires a description
hash"
+ raise AttributeError("Class initialization requires a description
hash")
if row is None:
return data
for x in range(len(names)):
@@ -118,7 +118,7 @@ class Cursor:
self._real_cursor = self._prepare(force=force)
def _prepare_sql(self):
- raise NotImplementedError
+ raise NotImplementedError()
def _prepare(self, force=None):
if self.sql:
@@ -149,7 +149,7 @@ class Cursor:
Abstraction for the update of a blob column which can vary wildly
between different database implementations.
"""
- raise NotImplementedError
+ raise NotImplementedError()
def execute(self, *p, **kw):
""" Execute a single query. """
@@ -200,7 +200,7 @@ class Cursor:
Must be subclasses by database specific drivers.
"""
- raise NotImplementedError
+ raise NotImplementedError()
def _execute(self, *args, **kwargs):
if kwargs:
@@ -210,12 +210,11 @@ class Cursor:
return self._execute_(args, kwargs)
def _executemany(self, *args, **kwargs):
- raise NotImplementedError
+ raise NotImplementedError()
def _execute_(self, args, kwargs):
""" Database specific execution of the query. """
- raise NotImplementedError
-
+ raise NotImplementedError()
# DATA RETRIEVAL
# Please note: these functions return None if no data is available,
@@ -296,21 +295,21 @@ class Database:
def connect(self, reconnect=1):
""" Opens a connection to the database. """
- raise NotImplementedError
+ raise NotImplementedError()
def check_connection(self):
""" Check that this connection is still valid. """
# Delegates to sub-classes as this is usually done with a DB specific
# query:
- raise NotImplementedError
+ raise NotImplementedError()
def prepare(self, sql, force=0):
""" Prepare an SQL statement. """
- raise NotImplementedError
+ raise NotImplementedError()
def commit(self):
""" Commit changes """
- raise NotImplementedError
+ raise NotImplementedError()
def procedure(self, name):
"""Return a pointer to a callable instance for a given stored
@@ -336,8 +335,7 @@ class Database:
return self._function(name, ret_type)
def _function(self, name, ret_type):
- raise NotImplementedError
-
+ raise NotImplementedError()
def transaction(self, name):
"set a transaction point to which we can rollback to"
@@ -368,16 +366,15 @@ class Database:
"""
Check if this database matches the given connection parameters.
"""
- raise NotImplementedError
+ raise NotImplementedError()
def Date(self, year, month, day):
"Returns a Date object"
- raise NotImplementedError
+ raise NotImplementedError()
def DateFromTicks(self, ticks):
"Returns a Date object"
- raise NotImplementedError
-
+ raise NotImplementedError()
# Class that we use just as a markup for queries/statements; if the statement
diff --git a/backend/server/rhnSQL/sql_row.py b/backend/server/rhnSQL/sql_row.py
index cbee06b..bc0653d 100644
--- a/backend/server/rhnSQL/sql_row.py
+++ b/backend/server/rhnSQL/sql_row.py
@@ -62,14 +62,15 @@ class Row(UserDictCase):
# "load from new id"?). We provide interfaces for load, save
# and create instead.
if x == self.hashname:
- raise AttributeError, "Can not reset the value of the hash key"
+ raise AttributeError("Can not reset the value of the hash key")
if x not in self.data or self.data[x][0] != value:
self.data[x] = (value, 1)
def __getitem__(self, name):
x = string.lower(name)
if x in self.data:
return self.data[x][0]
- raise KeyError, "Key %s not found in the Row dictionary" % name
+ raise KeyError("Key %s not found in the Row dictionary" % name)
+
def get(self, name):
x = string.lower(name)
if x in self.data:
@@ -109,7 +110,7 @@ class Row(UserDictCase):
def save(self, with_updates=1):
""" now save an entry """
if self.hashname not in self.data:
- raise AttributeError, "Table does not have a hash `%s' key" %
self.hashname
+ raise AttributeError("Table does not have a hash `%s' key" %
self.hashname)
# get a list of fields to be set
items = map(lambda a: (a[0], a[1][0]),
filter(lambda b: b[1][1] == 1, self.data.items()))
diff --git a/backend/server/rhnSQL/sql_table.py b/backend/server/rhnSQL/sql_table.py
index f303693..f1a7945 100644
--- a/backend/server/rhnSQL/sql_table.py
+++ b/backend/server/rhnSQL/sql_table.py
@@ -24,12 +24,13 @@ from spacewalk.common.rhnException import rhnException
import sql_base
import sql_lib
+
# A class to handle row updates transparently
class RowData(UserDictCase):
def __init__(self, dict, db, sql, rowid, cache=None):
UserDictCase.__init__(self, dict)
if not isinstance(db, sql_base.Database):
- raise TypeError, "Second argument needs to be a database handle"
+ raise TypeError("Second argument needs to be a database handle")
self.__db = db
self.__sql = sql
self.__rowid = rowid
@@ -163,14 +164,14 @@ class Table:
# primary key identifier
def __setitem__(self, key, value):
if not type(value) == type({}) and not isinstance(value, UserDictCase):
- raise TypeError, "Expected value to be a hash"
+ raise TypeError("Expected value to be a hash")
if self.__hashid in value: # we don't need that
if key is None:
key = value[self.__hashid]
del value[self.__hashid]
if key is None:
- raise KeyError, "Can not insert entry with NULL key"
+ raise KeyError("Can not insert entry with NULL key")
items = value.items()
if items == []: # quick check for noop
return
commit 6083f8ca7323eadb369fe9b3342f4137476ccb41
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Thu Oct 17 18:41:03 2013 +0200
Small coding conventions
diff --git a/backend/server/rhnSQL/__init__.py b/backend/server/rhnSQL/__init__.py
index fc74642..d632086 100644
--- a/backend/server/rhnSQL/__init__.py
+++ b/backend/server/rhnSQL/__init__.py
@@ -149,12 +149,12 @@ def Sequence(seq):
return sql_sequence.Sequence(db, seq)
# Wrapper for the Row class
-def Row(table, hash_name, hash_value = None):
+def Row(table, hash_name, hash_value=None):
db = __test_DB()
return sql_row.Row(db, table, hash_name, hash_value)
# Wrapper for the Table class
-def Table(table, hash_name, local_cache = 0):
+def Table(table, hash_name, local_cache=0):
db = __test_DB()
return sql_table.Table(db, table, hash_name, local_cache)
@@ -181,7 +181,7 @@ def fetchone_dict(sql, *args, **kwargs):
def commit():
db = __test_DB()
return db.commit()
-def rollback(name = None):
+def rollback(name=None):
db = __test_DB()
return db.rollback(name)
def transaction(name):
diff --git a/backend/server/rhnSQL/driver_cx_Oracle.py
b/backend/server/rhnSQL/driver_cx_Oracle.py
index 584955d..0bfa6b6 100644
--- a/backend/server/rhnSQL/driver_cx_Oracle.py
+++ b/backend/server/rhnSQL/driver_cx_Oracle.py
@@ -448,7 +448,7 @@ class Database(sql_base.Database):
return self._cursor_class(dbh=self.dbh)
# pass-through functions for when you want to do SQL yourself
- def prepare(self, sql, force=0, blob_map = None):
+ def prepare(self, sql, force=0, blob_map=None):
# Abuse the map calls to get rid of SQL comments and extra spaces
sql = string.join(filter(lambda a: len(a),
map(string.strip,
@@ -500,7 +500,7 @@ class Database(sql_base.Database):
log_debug(3, self.dbtxt)
return self.dbh.commit()
- def rollback(self, name = None):
+ def rollback(self, name=None):
log_debug(3, self.dbtxt, name)
if name: # we need to roll back to a savepoint
return self.execute("rollback to savepoint %s" % name)
diff --git a/backend/server/rhnSQL/sql_base.py b/backend/server/rhnSQL/sql_base.py
index 3925848..0a56ddd 100644
--- a/backend/server/rhnSQL/sql_base.py
+++ b/backend/server/rhnSQL/sql_base.py
@@ -343,7 +343,7 @@ class Database:
"set a transaction point to which we can rollback to"
pass
- def rollback(self, name = None):
+ def rollback(self, name=None):
"rollback changes, optionally to a previously set transaction point"
pass
diff --git a/backend/server/rhnSQL/sql_row.py b/backend/server/rhnSQL/sql_row.py
index 4fd7d9c..cbee06b 100644
--- a/backend/server/rhnSQL/sql_row.py
+++ b/backend/server/rhnSQL/sql_row.py
@@ -32,7 +32,8 @@ class Row(UserDictCase):
The easiest way to separate what these things are for is to remember that
the Table class indexes by KEY, while the Row class indexes by column
"""
- def __init__(self, db, table, hashname, hashval = None):
+
+ def __init__(self, db, table, hashname, hashval=None):
UserDictCase.__init__(self)
if not isinstance(db, sql_base.Database):
raise rhnException("Argument db is not a database instance", db)
@@ -75,7 +76,7 @@ class Row(UserDictCase):
return self.data[x][0]
return None
- def reset(self, val = 0):
+ def reset(self, val=0):
""" reset the changed status for these entries """
for k in self.data.keys():
# tuples do not support item assignement
@@ -91,7 +92,7 @@ class Row(UserDictCase):
""" load an entry """
return self.load_sql("%s = :hashval" % self.hashname,
{'hashval': hashval})
- def load_sql(self, sql, pdict = {}):
+ def load_sql(self, sql, pdict={}):
""" load from a sql clause """
h = self.db.prepare("select * from %s where %s" % (self.table, sql))
apply(h.execute, (), pdict)
diff --git a/backend/server/rhnSQL/sql_table.py b/backend/server/rhnSQL/sql_table.py
index 10b6d69..f303693 100644
--- a/backend/server/rhnSQL/sql_table.py
+++ b/backend/server/rhnSQL/sql_table.py
@@ -26,7 +26,7 @@ import sql_lib
# A class to handle row updates transparently
class RowData(UserDictCase):
- def __init__(self, dict, db, sql, rowid, cache = None):
+ def __init__(self, dict, db, sql, rowid, cache=None):
UserDictCase.__init__(self, dict)
if not isinstance(db, sql_base.Database):
raise TypeError, "Second argument needs to be a database handle"
@@ -56,7 +56,7 @@ class RowData(UserDictCase):
#
# Some day we'll figure out how to reduce confusion...
class Table:
- def __init__(self, db, table, hashid, cache = 0):
+ def __init__(self, db, table, hashid, cache=False):
if not table or not type(table) == type(""):
raise rhnException("First argument needs to be a table name",
table)
@@ -83,7 +83,7 @@ class Table:
# insert row(s) into the table
def insert(self, rows):
# insert a single row into the table
- def insert_row(row, self = self):
+ def insert_row(row, self=self):
if self.__cache is not None:
self.__cache[row[self.__hashid]] = row
return self.__setitem__(None, row)
commit 08079f0d816fc8217cdd163be079c3f0ae59281f
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Thu Oct 17 18:12:26 2013 +0200
Change deprecated 'has_key' to 'in'
where it was possible/I dared to do so.
diff --git a/backend/server/rhnSQL/__init__.py b/backend/server/rhnSQL/__init__.py
index b1fe23f..fc74642 100644
--- a/backend/server/rhnSQL/__init__.py
+++ b/backend/server/rhnSQL/__init__.py
@@ -90,7 +90,7 @@ def initDB(backend=None, host=None, port=None, username=None,
username = CFG.DB_USER
password = CFG.DB_PASSWORD
- if not SUPPORTED_BACKENDS.has_key(backend):
+ if backend not in SUPPORTED_BACKENDS:
raise rhnException("Unsupported database backend", backend)
if port:
diff --git a/backend/server/rhnSQL/driver_cx_Oracle.py
b/backend/server/rhnSQL/driver_cx_Oracle.py
index a92d341..584955d 100644
--- a/backend/server/rhnSQL/driver_cx_Oracle.py
+++ b/backend/server/rhnSQL/driver_cx_Oracle.py
@@ -522,7 +522,7 @@ class Database(sql_base.Database):
# an invalid pointer if we set an environment variable.
# If the environment variables are already set, this will be a noop
- if not os.environ.has_key("NLS_LANG"):
+ if "NLS_LANG" not in os.environ:
value = None
# Do we have a config object?
if rhnConfig.CFG.is_initialized():
diff --git a/backend/server/rhnSQL/sql_base.py b/backend/server/rhnSQL/sql_base.py
index e1ce75a..3925848 100644
--- a/backend/server/rhnSQL/sql_base.py
+++ b/backend/server/rhnSQL/sql_base.py
@@ -111,7 +111,7 @@ class Cursor:
self.description = None
- if not self._cursor_cache.has_key(self._dbh_id):
+ if self._dbh_id not in self._cursor_cache:
self._cursor_cache[self._dbh_id] = {}
# Store a reference to the underlying Python DB API Cursor:
diff --git a/backend/server/rhnSQL/sql_row.py b/backend/server/rhnSQL/sql_row.py
index 9055230..4fd7d9c 100644
--- a/backend/server/rhnSQL/sql_row.py
+++ b/backend/server/rhnSQL/sql_row.py
@@ -62,16 +62,16 @@ class Row(UserDictCase):
# and create instead.
if x == self.hashname:
raise AttributeError, "Can not reset the value of the hash key"
- if not self.data.has_key(x) or self.data[x][0] != value:
+ if x not in self.data or self.data[x][0] != value:
self.data[x] = (value, 1)
def __getitem__(self, name):
x = string.lower(name)
- if self.data.has_key(x):
+ if x in self.data:
return self.data[x][0]
raise KeyError, "Key %s not found in the Row dictionary" % name
def get(self, name):
x = string.lower(name)
- if self.data.has_key(x):
+ if x in self.data:
return self.data[x][0]
return None
@@ -107,7 +107,7 @@ class Row(UserDictCase):
def save(self, with_updates=1):
""" now save an entry """
- if not self.data.has_key(self.hashname):
+ if self.hashname not in self.data:
raise AttributeError, "Table does not have a hash `%s' key" %
self.hashname
# get a list of fields to be set
items = map(lambda a: (a[0], a[1][0]),
diff --git a/backend/server/rhnSQL/sql_table.py b/backend/server/rhnSQL/sql_table.py
index 9980e98..10b6d69 100644
--- a/backend/server/rhnSQL/sql_table.py
+++ b/backend/server/rhnSQL/sql_table.py
@@ -132,7 +132,7 @@ class Table:
# make this table look like a dictionary
def __getitem__(self, key):
- if self.__cache and self.__cache.has_key(key):
+ if self.__cache and key in self.__cache:
return self.__cache[key]
h = self.__db.prepare("select * from %s where %s = :p1" % (
self.__table, self.__hashid))
@@ -152,7 +152,7 @@ class Table:
# be modified.
def get(self, key):
ret = self.__getitem__(key)
- if self.__cache and self.__cache.has_key(key):
+ if self.__cache and key in self.__cache:
del self.__cache[key]
sql = "update %s set %%s = :new_val where %s = :row_id" % (
self.__table, self.__hashid)
@@ -164,7 +164,7 @@ class Table:
def __setitem__(self, key, value):
if not type(value) == type({}) and not isinstance(value, UserDictCase):
raise TypeError, "Expected value to be a hash"
- if value.has_key(self.__hashid): # we don't need that
+ if self.__hashid in value: # we don't need that
if key is None:
key = value[self.__hashid]
del value[self.__hashid]
commit dc9f5258e69b92df862eee58ad44d3e7cab0516b
Author: Matej Kollar <mkollar(a)redhat.com>
Date: Thu Oct 17 16:33:05 2013 +0200
Tab vs. Space War
diff --git a/backend/server/rhnSQL/driver_cx_Oracle.py
b/backend/server/rhnSQL/driver_cx_Oracle.py
index 0af3df6..a92d341 100644
--- a/backend/server/rhnSQL/driver_cx_Oracle.py
+++ b/backend/server/rhnSQL/driver_cx_Oracle.py
@@ -504,7 +504,7 @@ class Database(sql_base.Database):
log_debug(3, self.dbtxt, name)
if name: # we need to roll back to a savepoint
return self.execute("rollback to savepoint %s" % name)
- return self.dbh.rollback()
+ return self.dbh.rollback()
def check_connection(self):
try:
diff --git a/backend/server/rhnSQL/driver_postgresql.py
b/backend/server/rhnSQL/driver_postgresql.py
index caa5e25..62b1057 100644
--- a/backend/server/rhnSQL/driver_postgresql.py
+++ b/backend/server/rhnSQL/driver_postgresql.py
@@ -117,6 +117,7 @@ class Procedure(Function):
#if not (type(result) == 'tuple' and result[0] == ''):
#raise rhnSQL.SQLError("Unexpected result returned by procedure %s:
%s" % (self.name, str(result)))
+
def decimal2intfloat(dec, cursor):
"Convert a Decimal to an int or a float with no loss of information."
"The dec is passed in as str (not Decimal) so we cannot check its type."
diff --git a/backend/server/rhnSQL/sql_sequence.py
b/backend/server/rhnSQL/sql_sequence.py
index 297680c..d724866 100644
--- a/backend/server/rhnSQL/sql_sequence.py
+++ b/backend/server/rhnSQL/sql_sequence.py
@@ -23,24 +23,24 @@ import sql_base
# XXX: this is still Oracle specific, but it shouldn't be
class Sequence:
def __init__(self, db, seq):
- if not seq or type(seq) != type(""):
- raise rhnException("First argument needs to be a sequence name", seq)
- self.__seq = seq
+ if not seq or type(seq) != type(""):
+ raise rhnException("First argument needs to be a sequence name",
seq)
+ self.__seq = seq
if not isinstance(db, sql_base.Database):
raise rhnException("Argument db is not a database instance", db)
self.__db = db
def next(self):
- sql = "select sequence_nextval('%s') as ID from dual" % self.__seq
- cursor = self.__db.prepare(sql)
- cursor.execute()
- ret = cursor.fetchone_dict()
- if ret is None: # how the hell can this happen?
- return ret
- return int(ret['id'])
+ sql = "select sequence_nextval('%s') as ID from dual" %
self.__seq
+ cursor = self.__db.prepare(sql)
+ cursor.execute()
+ ret = cursor.fetchone_dict()
+ if ret is None: # how the hell can this happen?
+ return ret
+ return int(ret['id'])
def __call__(self):
- return self.next()
+ return self.next()
def __del__(self):
self.__seq = self.__db = None
diff --git a/backend/server/rhnSQL/sql_table.py b/backend/server/rhnSQL/sql_table.py
index ca7cbce..9980e98 100644
--- a/backend/server/rhnSQL/sql_table.py
+++ b/backend/server/rhnSQL/sql_table.py
@@ -92,7 +92,7 @@ class Table:
if type(rows) == type([]):
for x in rows:
insert_row(x)
- return None
+ return None
raise rhnException("Invalid data %s passed" % type(rows), rows)
# select from the whole table all the entries that match the
@@ -102,7 +102,7 @@ class Table:
raise rhnException("Expecting hash argument. %s is invalid" %
type(row),
row)
if row == {}:
- raise rhnException("The hash argument is empty", row)
+ raise rhnException("The hash argument is empty", row)
keys = row.keys()
# Sort the list of keys, to always get the same list of arguments
keys.sort()
@@ -113,10 +113,10 @@ class Table:
else:
clause = "%s = :%s" % (col, col)
args.append(clause)
- sql = "select * from %s where " % self.__table
- cursor = self.__db.prepare(sql + string.join(args, " and "))
- apply(cursor.execute, (), row)
- rows = cursor.fetchall_dict()
+ sql = "select * from %s where " % self.__table
+ cursor = self.__db.prepare(sql + string.join(args, " and "))
+ apply(cursor.execute, (), row)
+ rows = cursor.fetchall_dict()
if rows is None:
return None
# fill up the cache
@@ -133,14 +133,14 @@ class Table:
# make this table look like a dictionary
def __getitem__(self, key):
if self.__cache and self.__cache.has_key(key):
- return self.__cache[key]
+ return self.__cache[key]
h = self.__db.prepare("select * from %s where %s = :p1" % (
self.__table, self.__hashid))
h.execute(p1=key)
ret = h.fetchone_dict()
if ret is None:
- if self.__cache is not None:
- self.__cache[key] = None
+ if self.__cache is not None:
+ self.__cache[key] = None
return None
xret = UserDictCase(ret)
if self.__cache is not None:
@@ -153,7 +153,7 @@ class Table:
def get(self, key):
ret = self.__getitem__(key)
if self.__cache and self.__cache.has_key(key):
- del self.__cache[key]
+ del self.__cache[key]
sql = "update %s set %%s = :new_val where %s = :row_id" % (
self.__table, self.__hashid)
return RowData(ret, self.__db, sql, key, self.__cache)
@@ -174,7 +174,7 @@ class Table:
items = value.items()
if items == []: # quick check for noop
return
- sql = None
+ sql = None
if self.has_key(key):
sql, pdict = sql_lib.build_sql_update(self.__table, self.__hashid, items)
else:
@@ -183,11 +183,11 @@ class Table:
pdict["p0"] = key
h = self.__db.prepare(sql)
apply(h.execute, (), pdict)
- try:
- value[self.__hashid] = key
- self.__cache[key] = value
- except:
- pass
+ try:
+ value[self.__hashid] = key
+ self.__cache[key] = value
+ except:
+ pass
# length
def __len__(self):
@@ -203,8 +203,8 @@ class Table:
h = self.__db.prepare("delete from %s where %s = :p1" % (
self.__table, self.__hashid))
h.execute(p1=key)
- try: del self.__cache[key]
- except: pass
+ try: del self.__cache[key]
+ except: pass
return 0
# get all keys
@@ -247,9 +247,9 @@ class Table:
return self.__db.commit()
# passthrough rollback
def rollback(self):
- self.flush()
+ self.flush()
return self.__db.rollback()
def printcache(self):
- print self.__cache
+ print self.__cache
return