~ubuntu-branches/ubuntu/karmic/tdiary/karmic-updates

« back to all changes in this revision

Viewing changes to contrib2/io/dbi_io/dbi_io.rb

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2006-05-09 22:09:33 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060509220933-0dt10j3wn8fnuhmq
Tags: 2.0.2+20060303-4
Eliminate the dependency to ruby1.6. 
Thanks to Fumitoshi UKAI. (Closes: #366489)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
 
# dbi_io.rb: DBI IO for tDiary 2.x. $Revision: 1.1 $
 
2
# dbi_io.rb: DBI IO for tDiary 2.x. $Revision: 1.5 $
3
3
#
4
4
# NAME             dbi_io
5
5
#
8
8
#
9
9
# Copyright        (C) 2003 ma2tak <ma2tak@ma2tak.dyndns.org>
10
10
#                  (C) 2004 moonwolf <moonwolf@mooonwolf.com>
 
11
#                  (C) 2005 Kazuhiko <kazuhiko@fdiary.net>
11
12
#
12
13
# You can distribute this under GPL.
13
14
require 'dbi'
41
42
                @dbh.execute("INSERT INTO commentdata (name, mail, last_modified, visible, comment, author, diary_id, no) VALUES (?,?,?,?,?,?,?,?);", *param)
42
43
              end
43
44
            }
 
45
            @dbh.execute("DELETE FROM commentdata where author=? AND diary_id=? AND no>?", @dbi_author, date, no)
44
46
          }
45
47
        rescue Errno::ENOENT
46
48
        end
51
53
      def restore_referer(diaries)
52
54
        begin
53
55
          diaries.each {|date, diary_object|
54
 
            sql = "SELECT diary_id, count, ref FROM refererdata WHERE author='#{@dbi_author}' AND diary_id=#{date};"
55
 
            num = 0
56
 
            @dbh.select_all(sql) {|diary_id, count, ref|
57
 
              num += 1
 
56
            sql = "SELECT diary_id, count, ref FROM refererdata WHERE author=? AND diary_id=?;"
 
57
            @dbh.select_all(sql, @dbi_author, date) {|diary_id, count, ref|
58
58
              diary_object.add_referer(ref.chomp, count.to_i)
59
59
            }
60
60
          }
65
65
      def store_referer(diaries)
66
66
        begin
67
67
          diaries.each {|date, diary|
68
 
            no = 0
69
 
            diary.each_referer(diary.count_referers) {|count,ref|
70
 
              no += 1
71
 
              param = [count, ref, @dbi_author, date, no]
72
 
              sth = @dbh.execute("UPDATE refererdata SET count=?, ref=? WHERE author=? AND diary_id=? AND no=?;", *param)
73
 
              if sth.rows==0
74
 
                @dbh.execute("INSERT INTO refererdata (count, ref, author, diary_id, no ) VALUES (?,?,?,?,?);", *param)
 
68
            referers_diff = []
 
69
            referers = diary.instance_variable_get('@referers')
 
70
            referers.each_pair {|k, v|
 
71
              begin
 
72
                next if (v - @old_referers[date][k]).empty?
 
73
              rescue
 
74
              end
 
75
              referers_diff << v
 
76
            }
 
77
            next if referers_diff.empty?
 
78
            referers_diff.each {|count,ref|
 
79
              param = [count, @dbi_author, date, ref]
 
80
              begin
 
81
                sth = @dbh.execute("UPDATE refererdata SET count=? WHERE author=? AND diary_id=? AND ref=?;", *param)
 
82
                if sth.rows==0
 
83
                  no = @dbh.select_one("SELECT MAX(no) from refererdata where author=? AND diary_id=?", @dbi_author, date).first.to_i + 1
 
84
                  param << no
 
85
                  @dbh.execute("INSERT INTO refererdata (count, author, diary_id, ref, no ) VALUES (?,?,?,?,?);", *param)
 
86
                end
 
87
              rescue DBI::ProgrammingError
 
88
                $stderr.puts "invalid referer:#{ref}"
75
89
              end
76
90
            }
77
91
          }
94
108
    end
95
109
    
96
110
    def calendar
97
 
      calendar = {}
98
 
      sql = "SELECT DISTINCT year, month FROM diarydata GROUP BY year, month ORDER BY year, month;"
99
 
      @dbh.select_all(sql) {|year, month|
100
 
        ary = calendar[year] || Array.new
101
 
        ary << month
102
 
        calendar[year] = ary
 
111
      calendar = Hash.new{|hash, key| hash[key] = []}
 
112
      sql = "SELECT year, month FROM diarydata WHERE author=? GROUP BY year, month ORDER BY year, month;"
 
113
      @dbh.select_all(sql, @dbi_author) {|year, month|
 
114
        calendar[year] << month
103
115
      }
104
 
      
105
116
      calendar
106
117
    end
107
118
    
109
120
    # block must be return boolean which dirty diaries.
110
121
    #
111
122
    def transaction(date)
112
 
      @dbh.transaction {
113
 
        date_string = date.strftime("%Y%m%d")
114
 
        diaries = {}
115
 
        cache = @tdiary.restore_parser_cache(date, 'defaultio')
116
 
        unless cache
117
 
          restore(date_string, diaries)
118
 
          restore_comment(diaries)
119
 
          restore_referer(diaries)
120
 
        else
121
 
          diaries.update(cache)
122
 
        end
123
 
        dirty = yield(diaries) if iterator?
124
 
        store(diaries)  if dirty & TDiary::TDiaryBase::DIRTY_DIARY != 0
125
 
        store_comment(diaries)  if dirty & TDiary::TDiaryBase::DIRTY_COMMENT != 0
126
 
        store_referer(diaries)  if dirty & TDiary::TDiaryBase::DIRTY_REFERER != 0
127
 
        if dirty or not cache
128
 
          @tdiary.store_parser_cache(date, 'defaultio', diaries)
129
 
        end
 
123
      File.open("#{@tdiary.conf.data_path}/dbi_io.lock", 'w') {|file|
 
124
        file.flock(File::LOCK_EX)
 
125
        @dbh.transaction {
 
126
          date_string = date.strftime("%Y%m%d")
 
127
          diaries = {}
 
128
          cache = @tdiary.restore_parser_cache(date, 'defaultio')
 
129
          if cache
 
130
            diaries.update(cache)
 
131
          else
 
132
            restore(date_string, diaries)
 
133
            restore_comment(diaries)
 
134
            restore_referer(diaries)
 
135
          end
 
136
          @old_referers = {}
 
137
          diaries.each_pair{|k,v| @old_referers[k] = v.instance_variable_get('@referers').dup}
 
138
          dirty = yield(diaries) if iterator?
 
139
          store(diaries)  if dirty & TDiary::TDiaryBase::DIRTY_DIARY != 0
 
140
          store_comment(diaries)  if dirty & TDiary::TDiaryBase::DIRTY_COMMENT != 0
 
141
          store_referer(diaries)  if dirty & TDiary::TDiaryBase::DIRTY_REFERER != 0
 
142
          if dirty or not cache
 
143
            @tdiary.store_parser_cache(date, 'defaultio', diaries)
 
144
          end
 
145
        }
130
146
      }
131
147
    end
132
148