zoukankan      html  css  js  c++  java
  • XPO (Gary's post)Stored Procedure Support coming in V2010 Vol 2 (Part 2)

     

    原文:http://community.devexpress.com/blogs/garyshort/archive/2010/09/28/xpo-stored-procedure-support-coming-in-v2010-vol-2-part-2.aspx

    XPO - Stored Procedure Support coming in V2010 Vol 2 (Part 2)

     

    You’ll recall in my previous post on this in the forth coming topic I told you that there would be two ways of working with stored procedures 2010.2 release, those were:
    你回想一下,在我以前关于这个主题的文章中,我告诉你有两种方法使用存储过程工作,在未来发布的2010.2中,这些是:

    1. Direct calling of existing stored procedures and the ability to handle returned data;
      直接调用已经存在的存储过程,能处理返回的数据;
    2. Mapping of persistent classes on views in the database with help of INSTEAD-OF triggers and stored procedures.
      用数据库中视图持映射久类代替触发器和存储过程。

    I then went on to demonstrate how to use the first version. Well guess what? I’m back, and this time we’re going to take a look at that second variant, which works by mapping persistent classes to views in the database, which in turn query data from related tables in the database. Clearly, it’s not possible to add, edit or delete records from a database view and so to solve this problem, we’ll use INSTEAD-OF triggers to redirect the required operations to the stored procedures. To do this we’ll have to perform the following set of tasks for each database table:
    我接着演示如何使用第一个版本。那么你猜会怎么着?我回来了,此次,我们继续看第二种方法,通过映射数据库中的视图的持久类工作,返回数据库中相关表的查询数据。很明显,用数据库视图不可能添加,编辑或删除记录,因此要解决这个问题,我们将用替代触发器重定向到需求操作存储过程。要做到这一点,我们要为每个数据库表不得不执行下面设置任务:

    1. Create a view.
      创建一个新的视图。
    2. Map the persistent class to the corresponding View;
      映射视图对应的持久类。
    3. Create INSTEAD-OF triggers for INSERT, UPDATE and DELETE functionality.
      创建替代触发器的插入,更新和删除功能。
    4. Create stored procedure for INSERT, UPDATE and DELETE functionality.
      为插入,更新和删除功能创建存储过程。

    In the first blog post I demonstrated how to use the Persistent Classes Wizard to get started with stored procedures. Here we will use the same technique, with one difference. In the Generating Persistent Classes for an existing Database dialog, we will check the Generate views and stored procedures for tables access button.
    在第一篇博客中,我演示如何使用持久类导航开始使用存储过程。这里,我们将用相同的技术,用一个不同的方式。在这个为已存在数据库生成持久类的对话框,我们检查为访问表生成视图和存储过程按钮。

    The remaining steps are the same as in the previous post. As a result, the wizard will generate the code shown in the first post as well as the DDL code to generate views, triggers and stored procedures in the database.
    剩余几步与前一篇博客相同。结果,向导将生成的代码显示在前一篇博客中以及数据库描述代码生成的视图,触发器和存储过程在数据库中。

    For example, this code generates an EmployeeSplit_xpoView view:
    例如,这个代码生成了一个EmployeeSplit_xpoView视图:

    CREATE VIEW [EmployeeSplit_xpoView] AS
        SELECT
            [ID],
            [Extension],
            [PhotoPath]
        FROM [EmployeeSplit]
    GO
    CREATE PROCEDURE [sp_EmployeeSplit_xpoView_insert]
        @ID int,
        @Extension nvarchar(4),
        @PhotoPath nvarchar(255)
    AS
    BEGIN
        BEGIN TRY
            INSERT INTO [EmployeeSplit](
                [ID],
                [Extension],
                [PhotoPath]
            )
            VALUES(
                @ID,
                @Extension,
                @PhotoPath
            );
        END TRY
        BEGIN CATCH
            DECLARE @ErrorMessage NVARCHAR(4000);
            DECLARE @ErrorSeverity INT;
            DECLARE @ErrorState INT;
            SELECT @ErrorMessage = ERROR_MESSAGE(),
                @ErrorSeverity = ERROR_SEVERITY(),
                @ErrorState = ERROR_STATE();
            RAISERROR(
                @ErrorMessage,
                @ErrorSeverity,
                @ErrorState
            );
        END CATCH
    END
    GO
    CREATE PROCEDURE [sp_EmployeeSplit_xpoView_update]
        @ID int,
        @old_Extension nvarchar(4),
        @Extension nvarchar(4),
        @old_PhotoPath nvarchar(255),
        @PhotoPath nvarchar(255)
    AS
        UPDATE [EmployeeSplit] SET
            [Extension]=@Extension,
            [PhotoPath]=@PhotoPath
        WHERE
            [ID] = @ID
    GO
    CREATE PROCEDURE [sp_EmployeeSplit_xpoView_delete]
        @ID int,
        @old_Extension nvarchar(4),
        @old_PhotoPath nvarchar(255)
    AS
        DELETE FROM [EmployeeSplit] WHERE
            [ID] = @ID
    GO

    And this code generates the respective triggers.
    此代码分别生成了触发器。

    CREATE TRIGGER [t_EmployeeSplit_xpoView_insert]
    ON [EmployeeSplit_xpoView]
    INSTEAD OF INSERT AS
    BEGIN
        DECLARE @cur CURSOR
        SET @cur = CURSOR FOR
            SELECT
                [ID],
                [Extension],
                [PhotoPath]
            FROM inserted
        OPEN @cur
        DECLARE @ID int
        DECLARE @Extension nvarchar(4)
        DECLARE @PhotoPath nvarchar(255)
        FETCH NEXT FROM @cur INTO
            @ID,
            @Extension,
            @PhotoPath
        WHILE(@@fetch_status <> -1)
        BEGIN
            EXEC [sp_EmployeeSplit_xpoView_insert]
                @ID,
                @Extension,
                @PhotoPath
            FETCH NEXT FROM @cur INTO
                @ID,
                @Extension,
                @PhotoPath
        END
        CLOSE @cur
        DEALLOCATE @cur
    END
    GO
    CREATE TRIGGER [t_EmployeeSplit_xpoView_update]
    ON [EmployeeSplit_xpoView]
    INSTEAD OF UPDATE AS
    BEGIN
        DECLARE @cur CURSOR
        SET @cur = CURSOR FOR
            SELECT
                i.[ID],
                d.[Extension] as [old_Extension],
                i.[Extension],
                d.[PhotoPath] as [old_PhotoPath],
                i.[PhotoPath]
            FROM
                inserted i
                INNER JOIN
                deleted d
                ON
                    i.[ID] = d.[ID]
        OPEN @cur
        DECLARE @ID int
        DECLARE @old_Extension nvarchar(4)
        DECLARE @Extension nvarchar(4)
        DECLARE @old_PhotoPath nvarchar(255)
        DECLARE @PhotoPath nvarchar(255)
        FETCH NEXT FROM @cur INTO
            @ID,
            @old_Extension,
            @Extension,
            @old_PhotoPath,
            @PhotoPath
        WHILE(@@fetch_status <> -1)
        BEGIN
            EXEC [sp_EmployeeSplit_xpoView_update]
                @ID,
                @old_Extension,
                @Extension,
                @old_PhotoPath,
                @PhotoPath
            FETCH NEXT FROM @cur INTO
                @ID,
                @old_Extension,
                @Extension,
                @old_PhotoPath,
                @PhotoPath
        END
        CLOSE @cur
        DEALLOCATE @cur
    END
    GO
    CREATE TRIGGER [t_EmployeeSplit_xpoView_delete]
    ON [EmployeeSplit_xpoView]
    INSTEAD OF DELETE AS
    BEGIN
        DECLARE @cur CURSOR
        SET @cur = CURSOR FOR
            SELECT
                [ID],
                [Extension],
                [PhotoPath]
            FROM deleted
        OPEN @cur
        DECLARE @ID int
        DECLARE @Extension nvarchar(4)
        DECLARE @PhotoPath nvarchar(255)
        FETCH NEXT FROM @cur INTO
            @ID,
            @Extension,
            @PhotoPath
        WHILE(@@fetch_status <> -1)
        BEGIN
            EXEC [sp_EmployeeSplit_xpoView_delete]
                @ID,
                @Extension,
                @PhotoPath
            FETCH NEXT FROM @cur INTO
                @ID,
                @Extension,
                @PhotoPath
        END
        CLOSE @cur
        DEALLOCATE @cur
    END
    GO
     
     
    DROP TRIGGER [t_EmployeeSplit_xpoView_delete]
    GO
    DROP TRIGGER [t_EmployeeSplit_xpoView_update]
    GO
    DROP TRIGGER [t_EmployeeSplit_xpoView_insert]
    GO
    DROP PROCEDURE [sp_EmployeeSplit_xpoView_delete]
    GO
    DROP PROCEDURE [sp_EmployeeSplit_xpoView_update]
    GO
    DROP PROCEDURE [sp_EmployeeSplit_xpoView_insert]
    GO
    DROP VIEW [EmployeeSplit_xpoView]
    GO

    In addition to the above generated code, the corresponding persistent classes will be decorated with the PersistentAttribute, mapping them to the corresponding views in the database:
    吃了上面生成的代码,用PersistentAttribute修饰对应的持久类,映射他们到对应的数据库视图:

    using System;
    using DevExpress.Xpo;
    namespace Northwind {
     
        [Persistent("EmployeeSplit_xpoView")]
        public class EmployeeSplit : XPLiteObject {
            int fID;
            [Key]
            public int ID {
                get { return fID; }
                set { SetPropertyValue<int>("ID", ref fID, value); }
            }
            string fExtension;
            [Size(4)]
            public string Extension {
                get { return fExtension; }
                set { SetPropertyValue<string>("Extension", ref fExtension, value); }
            }
            string fPhotoPath;
            [Size(255)]
            public string PhotoPath {
                get { return fPhotoPath; }
                set { SetPropertyValue<string>("PhotoPath", ref fPhotoPath, value); }
            }
            public EmployeeSplit(Session session) : base(session) { }
            public EmployeeSplit() : base(Session.DefaultSession) { }
            public override void AfterConstruction() { base.AfterConstruction(); }
        }
     
        [NonPersistent]
        public class CustOrdersOrders : PersistentBase {
            int fOrderID;
            public int OrderID {
                get { return fOrderID; }
                set { SetPropertyValue<int>("OrderID", ref fOrderID, value); }
            }
            DateTime fOrderDate;
            public DateTime OrderDate {
                get { return fOrderDate; }
                set { SetPropertyValue<DateTime>("OrderDate", ref fOrderDate, value); }
            }
            DateTime fRequiredDate;
            public DateTime RequiredDate {
                get { return fRequiredDate; }
                set { SetPropertyValue<DateTime>("RequiredDate", ref fRequiredDate, value); }
            }
            public CustOrdersOrders(Session session) : base(session) { }
            public CustOrdersOrders() : base(Session.DefaultSession) { }
            public override void AfterConstruction() { base.AfterConstruction(); }
        }
        public static class NorthwindSprocHelper {
     
            public static DevExpress.Xpo.DB.SelectedData ExecCustOrdersOrders(Session session, string CustomerID){
                return session.ExecuteSproc("CustOrdersOrders", CustomerID);
            }
     
            static LoadDataMemberOrderItem[] CustOrdersOrdersOrderArray = {new LoadDataMemberOrderItem(0, "OrderID"), new LoadDataMemberOrderItem(1, "OrderDate"), new LoadDataMemberOrderItem(2, "RequiredDate")};
     
            public static System.Collections.Generic.ICollection<CustOrdersOrders> ExecCustOrdersOrdersIntoObjects(Session session, string CustomerID){
                return session.GetObjectsFromSproc<CustOrdersOrders>(CustOrdersOrdersOrderArray, "CustOrdersOrders", CustomerID);
            }
     
            public static XPDataView ExecCustOrdersOrdersIntoDataView(Session session, string CustomerID){
                DevExpress.Xpo.DB.SelectedData sprocData = session.ExecuteSproc("CustOrdersOrders", CustomerID);
                return new XPDataView(session.Dictionary, session.GetClassInfo(typeof(CustOrdersOrders)), CustOrdersOrdersOrderArray, sprocData);
            }
            public static void ExecCustOrdersOrdersIntoDataView(XPDataView dataView, Session session, string CustomerID){
                DevExpress.Xpo.DB.SelectedData sprocData = session.ExecuteSproc("CustOrdersOrders", CustomerID);
                dataView.PopulatePropertiesOrdered(session.GetClassInfo(typeof(CustOrdersOrders)), CustOrdersOrdersOrderArray);
                dataView.LoadOrderedData(CustOrdersOrdersOrderArray, sprocData);
            }
        }
    }

     

    欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/

  • 相关阅读:
    C++指针笔记
    破解入门【OllyDebug爆破程序】
    c++类的定义《一》
    数组
    while循环语句的使用
    MS10-046漏洞测试
    For循环语句的使用
    C++Builder编写计算器
    C++自定义函数
    SQLyog简介
  • 原文地址:https://www.cnblogs.com/Tonyyang/p/1945921.html
Copyright © 2011-2022 走看看