Class: CatalogDb::Metal::LeaguePeriod

Inherits:
Record show all
Defined in:
lib/sportdb/catalogs/league_period.rb

Class Method Summary collapse

Methods inherited from Record

_city, _league, _to_city, _to_country, _to_league, database, database=, database?

Methods inherited from BaseRecord

_country, _to_bool, columns, columns=, count, execute, tablename, tablename=

Class Method Details

._build_league_period(row) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sportdb/catalogs/league_period.rb', line 40

def self._build_league_period( row )
   ## get league record by ky
   ## league = League._record( row[1] )

   ## note: cache structs by key (do NOT rebuild duplicates; reuse)
   cache[ row[0] ] ||= Sports::LeaguePeriod.new(
                           key: row[2],  ## tier key
                           name: row[3],
                           qname: row[4],
                           slug: row[5],
                           start_season: row[6] ? Season.parse( row[6]) : nil,
                           end_season:   row[7] ? Season.parse( row[7]) : nil
                        )
end

._calc_yyyymm(season) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sportdb/catalogs/league_period.rb', line 56

def self._calc_yyyymm( season )
  start_yyyymm =     if season.calendar?
                        "#{season.start_year}01".to_i
                     else
                        "#{season.start_year}07".to_i
                     end

  end_yyyymm   =     if season.calendar?
                        "#{season.end_year}12".to_i
                     else
                        "#{season.end_year}06".to_i
                     end

[start_yyyymm, end_yyyymm]
end

._record(id) ⇒ Object

use _record! as name - why? why not?



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sportdb/catalogs/league_period.rb', line 20

def self._record( id )  ## use _record! as name - why? why not?
   if (rec = cache[ id ])
     rec   ## return cached
   else  ## query and cache and return
   rows = execute( <<-SQL )
 SELECT #{self.columns.join(', ')}
 FROM league_periods
 WHERE leagues_periods.id = #{id}
SQL

     ## todo/fix: also assert for rows == 1 AND NOT MULTIPLE records - why? why not?
     if rows.empty?
       raise ArgumentError, "league period record with id #{id} not found"
     else
       _build_league_period( rows[0] )
     end
   end
end

.cacheObject



17
# File 'lib/sportdb/catalogs/league_period.rb', line 17

def self.cache() @cache ||= Hash.new; end

.match_by_code(code, season:) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sportdb/catalogs/league_period.rb', line 73

def self.match_by_code( code, season: )
  code = normalize( code )

  season = Season( season )
  start_yyyymm, end_yyyymm = _calc_yyyymm( season )

  rows = nil
      ## note: returns empty array if no match and NOT nil
       rows =  execute( <<-SQL )
  SELECT #{self.columns.join(', ')}
  FROM league_periods
  INNER JOIN league_period_codes ON league_periods.id  = league_period_codes.league_period_id
  WHERE league_period_codes.code = '#{code}' AND
        league_period_codes.start_yyyymm <=  #{start_yyyymm} AND
        league_period_codes.end_yyyymm  >= #{end_yyyymm}
SQL

   ## wrap results array into struct records
   rows.map {|row| _build_league_period( row )}
end

.match_by_name(name, season:) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sportdb/catalogs/league_period.rb', line 95

def self.match_by_name( name, season: )
  ## note: match must for now always include name
  name = normalize( unaccent(name) )

  season = Season( season )
  start_yyyymm, end_yyyymm = _calc_yyyymm( season )

  rows = nil
      ## note: returns empty array if no match and NOT nil
       rows =  execute( <<-SQL )
  SELECT #{self.columns.join(', ')}
  FROM league_periods
  INNER JOIN league_period_names ON league_periods.id  = league_period_names.league_period_id
  WHERE league_period_names.name = '#{name}'  AND
        league_period_names.start_yyyymm <=  #{start_yyyymm} AND
        league_period_names.end_yyyymm  >= #{end_yyyymm}
SQL

   ## wrap results array into struct records
   rows.map {|row| _build_league_period( row )}
end

.match_by_name_or_code(q, season:) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sportdb/catalogs/league_period.rb', line 118

def self.match_by_name_or_code( q, season: )
  name = normalize( unaccent(q) )
  code = normalize( q )

  season = Season( season )
  start_yyyymm, end_yyyymm = _calc_yyyymm( season )

  rows = nil
      ## note: returns empty array if no match and NOT nil
       rows =  execute( <<-SQL )
  SELECT #{self.columns.join(', ')}
  FROM league_periods
  INNER JOIN league_period_names ON league_periods.id  = league_period_names.league_period_id
  WHERE league_period_names.name = '#{name}'  AND
        league_period_names.start_yyyymm <=  #{start_yyyymm} AND
        league_period_names.end_yyyymm  >= #{end_yyyymm}
  UNION
  SELECT #{self.columns.join(', ')}
  FROM league_periods
  INNER JOIN league_period_codes ON league_periods.id  = league_period_codes.league_period_id
  WHERE league_period_codes.code = '#{code}' AND
        league_period_codes.start_yyyymm <=  #{start_yyyymm} AND
        league_period_codes.end_yyyymm  >= #{end_yyyymm}
SQL

   ## wrap results array into struct records
   rows.map {|row| _build_league_period( row )}
end