# -*- coding: Windows-31j -*-
# jwdb.rb joywatcher database
# 2016.11 Ver.1.11   2015.02 Ver.1.10  TEXCELL
require 'win32ole'
require 'date'

@arMDay = [
   [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ],
   [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
]
@msecpday = 24 * 60 * 60 * 1000

def fromJwDateTime(jwdt)

   ia = jwdt.to_i
   ib = (jwdt - ia) * 86400
   ih = (ib / 3600).to_i; ib -= ih * 3600
   im = (ib / 60).to_i; ib -= im * 60
   ic = ib * 10 + 5; is = (ic / 10).to_i
   if is == 60 then im += 1; is = 0 end
   if im == 60 then ih += 1; im = 0 end
   if ih == 24 then ih = 0 end
   dta = DateTime.jd(ia + 2415019,ih,im,is)
   return dta
end

def encodeJwDate(iy, im, id)

   dr = 0.0
   ir = 1
   iarDay = 0
   if ((iy % 4) == 0) && ((iy % 100) != 0 || (iy % 400) == 0) then
      iarDay = 1
   end
   if (iy >= 1) && (iy <= 9999) && (im >= 1) && (im <= 12) && (id >= 1) && (id <= @arMDay[iarDay][im]) then
      for ia in 0..(im - 2)
         id += @arMDay[iarDay][ia]
      end
      ii = iy - 1
      # 693594 1/1/1 -> 1899/12/31 days
      dr = ii * 365 + (ii / 4) - (ii / 100) + (ii / 400) + id - 693594
      ir = 0
   end
   return ir, dr
end

def encodeJwTime(ih, im, is, ims = 0)

   ir = 1
   da = 0.0
   if (ih < 24) && (im < 60) && (is < 60) && (ims < 1000) then
      da = (ih * (60 * 60 * 1000) + im * (60 * 1000) + is * 1000 + ims).to_f / @msecpday
      ir = 0
   end
   return ir, da
end

def encodeJwDateTime(iy, imn, id, ih, imi, is, ims = 0)

   dr = 0
   ir, dd = encodeJwDate(iy, imn, id)
   if ir == 0
      ir, dt = encodeJwTime(ih, imi, is, ims)
      if ir == 0
          dr = dd + dt
      end
   end
   return ir, dr
end

class Jwdbt

   CP_ACP = 0
   CP_UTF8 = 65001

   def initialize(ccode)

      @imcode = -1
      sa = ccode.to_s.upcase
      if sa == "SHIFT_JIS" || sa == "WINDOWS-31J"
         @imcode = CP_ACP
      elsif sa == "UTF-8"
         @imcode = CP_UTF8
      end
      @jwdb = WIN32OLE.new("JWDBApiXControl1.JWDBApi")
      @jwserver = "localhost"
      @colCount = 0
      @rowCount = 0
      @ierrrun = 0
   end

   def connect(jwsver)

      ir = 0
      if @imcode == CP_UTF8
         jwsver = jwsver.encode("windows-31j", :undef => :replace, :replace => '?')
      end
      sSQL = "SELECT datetime('NOW')"
      begin
         @jwdb.ExecSQL(jwsver,sSQL)
         @jwserver = jwsver
      rescue
         ir = 1
      end
      return ir
   end

   def execSQL(ssql)

      ir = 0
      if @imcode == CP_UTF8
         ssql = ssql.encode("windows-31j", :undef => :replace, :replace => '?')
      end
      @colCount = 0
      @rowCount = 0
      begin
         @jwdb.ExecSQL(@jwserver,ssql)
         @colCount = @jwdb.ColCount
         @rowCount = @jwdb.RowCount
      rescue => se
         print "jw DB error\r\n"
         print se.message + "\r\n"
         print "[" + ssql + "]\r\n"
         if @ierrrun == 0
            close
            disconnect
            exit
         end
         ir = 1
      end
      return ir
   end

   def close

      @jwdb.Close
   end

   def disconnect

      begin
         @jwdb.Disconnect
      rescue
      end
   end

   def columnText(iy,ix)

      sr = @jwdb.ColumnText(iy,ix)
      if @imcode == CP_UTF8
         sr = sr.encode("utf-8", :undef => :replace, :replace => '?')
      end
      return sr
   end

   def columnName(ix)

      sr = @jwdb.ColumnName(ix)
      if @imcode == CP_UTF8
         sr = sr.encode("utf-8", :undef => :replace, :replace => '?')
      end
      return sr
   end

   attr_reader :colCount
   attr_reader :rowCount
   attr_accessor :ierrrun

end
