Sha256: b4ccc50ddea934e12fa67fe1af87815bfbbf3be52212eacf7785c179630d4087

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'nssql/version'
require 'nssql/settings'
require 'nssql/table'

require 'odbc_utf8'
require 'tempfile'

# NSSQL module.
#
module NSSQL
  class Error < StandardError; end

  class << self
    def select_array(query)
      execute(query, &:fetch_all)
    end

    def select_to_file(query)
      query = one_line_query(query)

      isql_command = "isql -v Netsuite #{NSSQL::Settings.user} '#{NSSQL::Settings.password}' -b -q -d,"
      iconv_command = 'iconv -f iso-8859-1 -t utf-8'

      Tempfile.new.tap do |file|
        system_call("echo \"#{query}\" | #{isql_command} | #{iconv_command} > #{file.path}")
      end
    end

    def configure
      NSSQL::Settings.configure
    end

    private

    def execute(query)
      statement = netsuite_connection.prepare(query).execute
      result = yield statement

      statement.drop

      result
    end

    def system_call(command)
      `#{command}`
    end

    def one_line_query(query)
      query.tr("\n", ' ').gsub(/\s+/, ' ').strip
    end

    def netsuite_connection
      ODBC.connect('NetSuite', NSSQL::Settings.user, NSSQL::Settings.password)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nssql-1.1.0 lib/nssql.rb
nssql-1.0.0 lib/nssql.rb
nssql-0.1.1 lib/nssql.rb