# -*- coding: iso-8859-1 -*-
#-----------------------------------------------------------------------------
# Modeling Framework: an Object-Relational Bridge for python
#
# Copyright (c) 2001-2004 Sbastien Bigaret <sbigaret@users.sourceforge.net>
# All rights reserved.
#
# This file is part of the Modeling Framework.
#
# This code is distributed under a "3-clause BSD"-style license;
# see the LICENSE file for details.
#-----------------------------------------------------------------------------
"""
MySQLAdaptor
CVS information
$Id: MySQLAdaptorChannel.py 933 2004-08-02 19:58:54Z sbigaret $
"""
__version__='$Revision: 933 $'[11:-2]
from Modeling.logging import db_info,db_trace,db_error
import sys
# Framework
from Modeling.Adaptor import GeneralAdaptorException
from Modeling.DatabaseAdaptors.AbstractDBAPI2AdaptorLayer.AbstractDBAPI2AdaptorChannel import AbstractDBAPI2AdaptorChannel
class MySQLAdaptorChannel(AbstractDBAPI2AdaptorChannel):
"""
Concrete AdaptorChannel for MySQL [module MySQLdb]
"""
# for testing purposes only
_count_for_execute=0
def primaryKeysForNewRowsWithEntity(self, count, anEntity):
"""
Generates and returns a list of dictionaries, whose keys are
PK attributes' names and values the corresponding PK value.
If 'anEntity' defines more than one PK (compound primary key), returns an
empty list.
Parameters:
count -- the number of primary keys to generate
anEntity -- the entity for which PKs are generated
The method used here is described in
MySQLSchemaGeneration.primaryKeySupportStatementsForEntityGroup()
See also:
MySQLSchemaGeneration.primaryKeySupportStatementsForEntityGroup()
MySQLSchemaGeneration.dropPrimaryKeySupportStatementsForEntityGroup()
Modeling.AdaptorChannel.primaryKeysForNewRowsWithEntity()
"""
if len(anEntity.primaryKeyAttributes())!=1:
return ()
self._adaptorContext.beginTransaction()
self.openChannel()
try:
pkName=anEntity.primaryKeyAttributeNames()[0]
pkRootName=anEntity.primaryKeyRootName()
statements=["UPDATE PK_SEQ_%s SET id=LAST_INSERT_ID(id+1)"%pkRootName,
"select LAST_INSERT_ID()"]
result=[]
for idx in range(count):
for statement in statements:
db_info('Evaluating: %s'%statement)
try:
self.dbAPI_cursor().execute(statement)
except:
exctype, value = sys.exc_info()[:2]
msg="Couldn't evaluate expression %s. Reason: %s:%s"%(statement, exctype, value)
db_error(msg)
raise GeneralAdaptorException, msg
MySQLAdaptorChannel._count_for_execute+=1
result.append({pkName :self.dbAPI_cursor().fetchone()[0]})
return tuple(result)
finally:
self._adaptorContext.commitTransaction()
|