Skip to content

bool に対しての DirectValue の結果を 0 or 1 にして欲しい #8

Description

@OdaShinsuke

SQL Server bit (Transact-SQL)
PostgreSQL11.4 - 8.6. 論理値データ型
MySQL 9.1.6 Boolean Literals
メジャーな DB は false = 0、true = 1 で問題なさそう。

bool 型のプロパティ == 1.DirectValue() とするとコンパイルエラーになる。
なので、bool 型のプロパティ == true.DirectValue() とするが、
そうすると SQL Server では、構文エラーになるクエリが出力される。
※PostgreSQL、MySQL は構文エラーにならないはず(true / false リテラル がある)

サンプルコード

public class table
{
  public int foo { get; set; }
  public int bar { get; set; }
  public bool IsValid { get; set; }
}
public class DB
{
  public table table { get; set; }
}

class Program
{

  static void Main(string[] args)
  {
    var query = Db<DB>.Sql(db => Select(new table
    {
      foo = db.table.foo,
      bar = db.table.bar
    }).
    From(db.table).
    Where(db.table.IsValid == true.DirectValue()));

    var qs = query.Build(typeof(SqlConnection)).Text;

    Console.WriteLine(qs);
/*
SELECT
   table.foo AS foo,
   table.bar AS bar
FROM table
WHERE table.IsValid = True
*/
  }
}

SQL Server で↑のクエリを実行するとエラーになる。

列名 'True' が無効です。

回避コードは、1.DirectValue() の結果を無理やり bool にキャストする。

var query = Db<DB>.Sql(db => Select(new table
    {
      foo = db.table.foo,
      bar = db.table.bar
    }).
    From(db.table).
    Where(db.table.IsValid == (bool)(object)1.DirectValue()));

しかしこれは書きづらいので変更して欲しい。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions