IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

JavaScriptでSQLServerにアクセス

node-mssqlでアクセスできる模様。(試してない)

node-mssqlが便利なインターフェースを用意していて、ドライバがtediousjsとういう役割分担。

my program <--> node-mssql <--> tediousjs <--> SQLServer

node-mssql

GitHub - tediousjs/tedious: Node TDS module for connecting to SQL Server databases.

なんで、tediousっていう名前なのかとずっと疑問でしたが、シャレになっているようですね。

tediousの意味・使い方 - 英和辞典 Weblio辞書

SQLServer Management Studio上のクエリでプロシージャを実行してもSQLBatchとして実行される模様

サンプルのNorthwindで次のクエリをSQLServer Management Studioで実行したら、SQLBatchとして実行されていました。クエリ全体が文字列(下の例でいえば6行全体)として扱われているようです。PreparedStatementとして実行されることを期待していたのですが。。。

Declare @P1 int;  
Exec sp_prepare @P1 output,   
    N'@id int',  
    N'SELECT [EmployeeID],[LastName],[FirstName],[Title] FROM [Northwind].[dbo].[Employees] where EmployeeID = @id';  
Exec sp_execute @P1, 1;  
EXEC sp_unprepare @P1;

PutをBitPutにするとflushされてしまう

Binary処理が定義されているとします。

例えば、下記は1Byteの処理です。

 > :t example_ColFlags
example_ColFlags :: ColFlags
 > :t put example_ColFlags
put example_ColFlags :: Put
 >
 > BSL.writeFile "work\\colflags.bin" (runPut $ put example_ColFlags)
 >
  Offset: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F    
00000000: FF                                                 .

このようなPutが定義されているときに、前後にbit単位の処理を追加することを考えます。

joinPutを使うと、直掩までのBit処理がflushされてしまいます。

 > let foo = putBool True >> (joinPut $ put (example_ColFlags ::ColFlags)) >> putBool True
 > BSL.writeFile "work\\colflags.bin" (runPut $ runBitPut foo)
 >

ここでは、1bitを出しているので、ゼロフィルされて80が出力されてしまいます。この挙動はドキュメントに記載されています。

Run a Put inside BitPut. Any partially written bytes will be flushed before Put executes to ensure byte alignment.

  Offset: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F    
00000000: 80 FF 80                                           ...

flushを避けるため、仕方がないので、バイト単位の処理はいったんByteStringにして、それをBit処理しました。

 > let foo = putBool True >> (Bit.putByteString $ BSL.toStrict $ runPut $ put (example_ColFlags ::ColFlags)) >> putBool True
 > BSL.writeFile "work\\colflags.bin" (runPut $ runBitPut foo)
  Offset: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F    
00000000: FF C0                                              .@

いつも忘れるのでHaskellのテストの最小のひな型をメモ

package.yaml

tastyだけでなくtastyから利用するテストパッケージもdependenciesに追加する。(これを忘れていつも嵌る)

tests:
  testproj-test:
    main:                Test.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - testproj
    - tasty
    - tasty-discover
    - tasty-hunit
    - tasty-hspec
    - tasty-quickcheck
    - tasty-smallcheck
    - bytestring 

Hspecのひな型

テストされる側のファイルとテストファイルは同じ名前にはできない。 sは小文字

module Foo.Bar.TestBuzz

where

-- test
import Test.Tasty
import Test.Tasty.Hspec

-- my library
import Foo.Bar.Buzz

getContext = return "dummy"

spec_template = do
    before getContext $ do
      describe "some descriptions" $ do
        it "descripe it" $ \_-> True `shouldBe` True

ghciをテスト側で呼び出す

stack ghci :testproj-test

実行方法

*> hspec spec_template

some descriptions
  descripe it FAILED [1]

Failures:

  test\Foo\Bar\TestBuzz.hs:17:
  1) some descriptions descripe it
       expected: True
        but got: False

Randomized with seed 285726028

Finished in 0.0081 seconds
1 example, 1 failure
*** Exception: ExitFailure 1