• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            qiezi的學(xué)習(xí)園地

            AS/C/C++/D/Java/JS/Python/Ruby

              C++博客 :: 首頁(yè) :: 新隨筆 ::  ::  :: 管理 ::
            這是前段時(shí)間有人討論過(guò)的問(wèn)題:
            代碼:

            order = Order.find(1)
            order.update_attribute(:status, 'finished')

            假定orders表有10個(gè)字段,你只想更新其中一個(gè),但active record會(huì)生成一個(gè)更新全部字段的SQL語(yǔ)句,假定其中一個(gè)字段值長(zhǎng)度是20K,這個(gè)負(fù)擔(dān)可能會(huì)有些重。

            我嘗試解決這個(gè)問(wèn)題,寫了個(gè)簡(jiǎn)單的插件:
            代碼:

            module ActiveRecord
            ? class Base
            ? ? def update_attribute(name, value)
            ? ? ? update_attributes(name => value)
            ? ? end

            ? ? def update_attributes(new_attributes)
            ? ? ? return if new_attributes.nil?
            ? ? ? attributes = new_attributes.dup
            ? ? ? attributes.stringify_keys!
            ? ? ? self.attributes = attributes
            ? ? ? update(attributes)
            ? ? end

            ? ? private
            ? ? ? def update(attrs = nil)
            ? ? ? ? connection.update(
            ? ? ? ? ? "UPDATE #{self.class.table_name} " +
            ? ? ? ? ? "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false, attrs))} " +
            ? ? ? ? ? "WHERE #{self.class.primary_key} = #{quote(id)}",
            ? ? ? ? ? "#{self.class.name} Update"
            ? ? ? ? )
            ? ? ? ?
            ? ? ? ? return true
            ? ? ? end

            ? ? ? def attributes_with_quotes(include_primary_key = true, attrs = nil)
            ? ? ? ? (attrs || attributes).inject({}) do |quoted, (name, value)|
            ? ? ? ? ? if column = column_for_attribute(name)
            ? ? ? ? ? ? quoted[name] = quote(value, column) unless !include_primary_key && column.primary
            ? ? ? ? ? end
            ? ? ? ? ? quoted
            ? ? ? ? end
            ? ? ? end
            ? end
            end


            attributes_with_quotes函數(shù)的參數(shù)搞這么復(fù)雜,原因是我想即便是用這段代碼替換庫(kù)里面的部分,也不影響原有代碼的正常功能。

            可以簡(jiǎn)單測(cè)試一下上面的例子,它生成的SQL語(yǔ)句會(huì)簡(jiǎn)潔很多,大概是這樣子:
            UPDATE orders SET "status" = 'finished' WHERE id = 1

            已發(fā)現(xiàn)的BUG和修復(fù):

            1、沒(méi)有調(diào)用validation (by cookoo)。由于原有代碼調(diào)用save,而save被覆蓋成有驗(yàn)證的代碼,所以具有驗(yàn)證功能。解決辦法是增加一段代碼:

            module?ActiveRecord
            ??module?ValidationsFix
            ????
            def ?self.append_features(base)? # ?:nodoc:
            ??????super
            ??????base.class_eval?do
            ????????alias_method?:update_attributes_without_validation,?:update_attributes
            ????????alias_method?:update_attributes,?:update_attributes_with_validation
            ??????end
            ????end

            ????
            def ?update_attributes_with_validation(new_attributes)
            ??????
            return ? if ?new_attributes.nil?
            ????? attributes = new_attributes.dup
            ????? attributes.stringify_keys!
            ??????self.attributes?
            = ?attributes

            ??????
            if ?valid?
            ????????update_attributes_without_validation(attributes)
            ??????
            else
            ????????
            return ?false
            ??????end
            ????end
            ??end
            end

            ActiveRecord::Base.class_eval?do
            ??include?ActiveRecord::ValidationsFix
            end

            簡(jiǎn)單測(cè)試通過(guò)。

            posted on 2006-08-26 02:10 qiezi 閱讀(691) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Ruby
            久久精品国产免费观看三人同眠| 99久久精品国内| 国产亚洲综合久久系列| 国产午夜精品理论片久久| 久久人人添人人爽添人人片牛牛 | 久久美女人爽女人爽| 久久久精品波多野结衣| 热re99久久6国产精品免费| 99久久婷婷国产一区二区| 免费精品久久天干天干| 久久久久久久综合日本亚洲| 日日狠狠久久偷偷色综合免费 | 免费观看久久精彩视频| 一极黄色视频久久网站| 精品人妻久久久久久888| 欧美伊人久久大香线蕉综合69| 国产精品久久久久久吹潮| 青青久久精品国产免费看| 久久香蕉综合色一综合色88| 亚洲欧美成人综合久久久| 久久精品不卡| 久久国产精品一区二区| 亚洲国产另类久久久精品小说| 国产成人精品综合久久久久| 久久久精品久久久久久| 99热成人精品热久久669| 精品一二三区久久aaa片| 色99久久久久高潮综合影院| 人人狠狠综合久久亚洲婷婷| 亚洲国产精品无码久久98| 亚洲?V乱码久久精品蜜桃| 91麻豆精品国产91久久久久久| 色妞色综合久久夜夜| 伊人久久亚洲综合影院| 久久精品中文字幕第23页| 99久久国产主播综合精品| 99久久婷婷免费国产综合精品| 亚洲va久久久噜噜噜久久天堂| 欧美亚洲国产精品久久久久| 久久久久国产亚洲AV麻豆| 国产综合免费精品久久久|