Python社区  »  DATABASE

使用Alembic的存储过程:MySQL语法错误

A23149577 • 1 周前 • 13 次点击  

我正在使用Alembic将存储过程加载到MySQL数据库中。跟随 cookbook ,我能够创建创建和删除过程所需的对象。现在我想升级版本以实际加载过程,我得到了 DELIMITER $$ 我需要程序定义。我也试过移除 DELIMITER 并替换 AS 开始手术,但也没用。我甚至在食谱中尝试了简单的示例函数,但这也给了我同样的语法错误 $ .

class ReplaceableObject:
    def __init__(self, name, sqltext):
        self.name = name
        self.sqltext = sqltext


class ReversibleOp(MigrateOperation):
    def __init__(self, target):
        self.target = target

    @classmethod
    def invoke_for_target(cls, operations, target):
        op = cls(target)
        return operations.invoke(op)

    def reverse(self):
        raise NotImplementedError()

    @classmethod
    def _get_object_from_version(cls, operations, ident):
        version, objname = ident.split(".")

        module = operations.get_context().script.get_revision(version).module
        obj = getattr(module, objname)
        return obj

    @classmethod
    def replace(cls, operations, target, replaces=None, replace_with=None):

        if replaces:
            old_obj = cls._get_object_from_version(operations, replaces)
            drop_old = cls(old_obj).reverse()
            create_new = cls(target)
        elif replace_with:
            old_obj = cls._get_object_from_version(operations, replace_with)
            drop_old = cls(target).reverse()
            create_new = cls(old_obj)
        else:
            raise TypeError("replaces or replace_with is required")

        operations.invoke(drop_old)
        operations.invoke(create_new)


@Operations.register_operation("create_sp", "invoke_for_target")
@Operations.register_operation("replace_sp", "replace")
class CreateSPOp(ReversibleOp):
    def reverse(self):
        return DropSPOp(self.target)


@Operations.register_operation("drop_sp", "invoke_for_target")
class DropSPOp(ReversibleOp):
    def reverse(self):
        return CreateSPOp(self.target)


@Operations.implementation_for(CreateSPOp)
def create_sp(operations, operation):
    operations.execute(
        """
        DELIMITER $$
        CREATE PROCEDURE %s %s
        DELIMITER ;
        """ % (
            operation.target.name, operation.target.sqltext
        )
    )


@Operations.implementation_for(DropSPOp)
def drop_sp(operations, operation):
    operations.execute("DROP PROCEDURE IF EXISTS %s" % operation.target.name)

这就是我试图创造它的方式:

my_procedure = ReplaceableObject(
    "my_procedure(IN arg1 TEXT, IN arg2 TEXT, OUT res TEXT)",
    """
    BEGIN
        -- procedure implementation
    END $$   
    """
)

def upgrade():
    op.create_sp(my_procedure)

def downgrade():
    op.drop_sp(my_procedure)

这就是错误(我正在使用 pymysql 作为客户):

sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$\n        CREATE PROCEDURE my_procedure(IN arg1 TEXT, IN arg2 TEXT, OU' at line 1")
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50716
 
13 次点击  
分享到微博
文章 [ 1 ]  |  最新文章 1 周前
Bill Karwin
Reply   •   1 楼
Bill Karwin    2 月前

你不需要 DELIMITER 除了在支持一系列分号分隔语句的环境中之外,MySQL客户端。

事实上 分隔符 命令是 mysql client builtin ,MySQL服务器无法识别。

你不能用 分隔符 当通过API向MySQL服务器提交语句时,即使 CREATE PROCEDURE .