[轉載]Openerp 使用 Postgresql 預存程序和視圖

來源:互聯網
上載者:User

原文地址:Openerp 使用 Postgresql 預存程序和視圖

 

OpenERP 使用 postgresql 預存程序和試圖,步驟如下:

STEP1: 在模組的 init 函數中定義預存程序

    def init(self, cr):        ''' create stored procedure '''        cr.execute("""CREATE OR REPLACE FUNCTION fn_fi_report_childs(int)        RETURNS TABLE(id int) AS $$            WITH RECURSIVE t AS (                SELECT id,parent_id  FROM fi_report WHERE id = $1              UNION ALL                SELECT fi_report.id, fi_report.parent_id FROM fi_report, t WHERE fi_report.parent_id = t.id            )            SELECT id FROM t;        $$ LANGUAGE SQL            """)

或者定義視圖

    def init(self, cr):        tools.drop_view_if_exists(cr, 'analytic_entries_report')        cr.execute("""            create or replace view analytic_entries_report as (                 select                     min(a.id) as id,                     count(distinct a.id) as nbr,                     a.date as date,                     to_char(a.date, 'YYYY') as year,                     to_char(a.date, 'MM') as month,                     to_char(a.date, 'YYYY-MM-DD') as day,                     a.user_id as user_id,                     a.name as name,                     analytic.partner_id as partner_id,                     a.company_id as company_id,                     a.currency_id as currency_id,                     a.account_id as account_id,                     a.general_account_id as general_account_id,                     a.journal_id as journal_id,                     a.move_id as move_id,                     a.product_id as product_id,                     a.product_uom_id as product_uom_id,                     sum(a.amount) as amount,                     sum(a.unit_amount) as unit_amount                 from                     account_analytic_line a, account_analytic_account analytic                 where analytic.id = a.account_id                 group by                     a.date, a.user_id,a.name,analytic.partner_id,a.company_id,a.currency_id,                     a.account_id,a.general_account_id,a.journal_id,                     a.move_id,a.product_id,a.product_uom_id            )        """)

STEP2: 在模組的函數中使用預存程序

def get_amount(self,cr,uid,id,period_id,context=None):        cr.execute('SELECT * FROM fn_fi_report_childs(%s)', (id,))

而視圖的話,則如普通的表一樣使用。

STEP3: 完成!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.