Ruby - RSS 作成 - テーブルアクセス

 クラウディア
1. 概要
2. ライブラリのインストール
3. モジュール

1. 概要

 当初、定義をコンフィグレーションファイルに書いて、それを読みこむような考えでおりましたが・・・。  サイト定義テーブルというものを持って、そこから定義を持ってくるように方針変更しました。

2. ライブラリのインストール

 gem で pg というライブラリをインストールすればいいそうな。  本項は「rubyからpostgresqlにアクセスする。 - rderaログ」を参考にさせていただきました。  環境は FreeBSD ですので root ユーザ権限で

> gem install pg
Fetching: pg-1.1.3.gem (100%)
Building native extensions. This could take a while...
Successfully installed pg-1.1.3
Parsing documentation for pg-1.1.3
Installing ri documentation for pg-1.1.3
Done installing documentation for pg after 2 seconds
1 gem installed
 すぐに終わります。

3. モジュール

=begin

=end

require('pry')
require('pg')
require('ExceptLog')
require('BaseClass')

class DBaccess < BaseClass
  @@connection = nil                                          # メンバ変数 データベースとのコネクション

  def DBaccess.callback                                       # ファイナライザーで切断
    proc {
      @@connection.finish if @@connection
    }
  end

  def initialize
    ObjectSpace.define_finalizer(self, DBaccess.callback)
  end

  def connect(dbparam)                                        # 接続
    begin
      @@connection = PG::connect(dbparam)

    rescue PGError => error
      exeptlog = ExceptLog.new
      exeptlog.execute(self, error)
    rescue => error
      exeptlog = ExceptLog.new
      exeptlog.execute(self, error)
    ensure

    end
  end

  def feed                                                    # サイト定義テーブルの読込
    begin
      hash = Hash.new
      results = @@connection.exec("SELECT * FROM FEED")

      results.fields.each do | column |
        hash[column] = results.field_values(column)[0]
      end

    rescue PGError => error
      exeptlog = ExceptLog.new
      exeptlog.execute(self, error)
    rescue => error
      exeptlog = ExceptLog.new
      exeptlog.execute(self, error)
    ensure
      return hash
    end
  end

  def content=(detail)
    begin

      detail.each do | record |
        #puts("[#{self.class.name}.#{__method__}] insert=[#{record[:uri]}]")

        if (record[:title] == nil)
          puts("[#{self.class.name}.#{__method__}] URI[#{record[:uri]}] のタイトルが nill です")
          next
        end

        if (record[:description] == nil)
          puts("[#{self.class.name}.#{__method__}] URI[#{record[:description]}] の要約(description)が nill です")
          next
        end

        begin
          contentstr  = @@connection.escape_literal(record[:content])
          title       = @@connection.escape_literal(record[:title])
          description = @@connection.escape_literal(record[:description])
        rescue => error
          exeptlog = ExceptLog.new
          exeptlog.execute(self, error)
          binding.pry
          exit 1
        ensure
        end

        insert = "INSERT INTO CONTENTS "
        insert << "(INDEX, URI, DOCUMENT, TITLE, MODIFIED, DESCRIPTION, CONTENT) "
        insert << "VALUES "
        insert << "("+record[:index]+", '"+record[:uri]+"', '"+record[:document]+"', "+title+", TO_TIMESTAMP('"+record[:modified].to_s[0, 19]+"', 'YYYY-MM-DD HH24:MI:SS'), "+description+", "+contentstr+") "
        insert << "ON CONFLICT (INDEX, URI) "
        insert << "DO UPDATE "
        insert << "SET DOCUMENT='"+record[:document]+"', TITLE="+title+", MODIFIED=TO_TIMESTAMP('"+record[:modified].to_s[0, 19]+"', 'YYYY-MM-DD HH24:MI:SS'), DESCRIPTION="+description+", CONTENT="+contentstr+" "

        #binding.pry
        #puts("[#{self.class.name}.#{__method__}] insert=[#{insert}]")

        results = @@connection.exec(insert)
      end
    rescue => error
      exeptlog = ExceptLog.new
      exeptlog.execute(self, error)
      exit 1
    ensure
    end
  end
end



 「feed」メソッドで、「FEED」テーブルの内容を取得します。  「content」メソッドで、「CONTENTS」テーブルへ内容を追加したり更新したりしています。
earthcar(アースカー)