Permalink
Browse files

Rename StringFixedLength to CharString and remove op_Implicit members

  • Loading branch information...
1 parent c96ae99 commit 1547578013d0ac660349b39e09096a1e02215a5a Toshihiro Nakamura committed Apr 3, 2012
Showing with 148 additions and 79 deletions.
  1. +4 −0 Soma.Core.IT.MsSql/CreateDb.sql
  2. +64 −0 Soma.Core.IT.MsSql/MappingTest.fs
  3. +41 −30 Soma.Core.UT/SqlTest.fs
  4. +30 −35 Soma.Core/Sql.fs
  5. +9 −14 Soma.Core/Sql.fsi
@@ -81,6 +81,10 @@ create table LobMapping (
VarcharStringCol varchar(max),
NVarcharStringCol nvarchar(max)
);
+create table CharMapping (
+ CharMappingId int primary key,
+ CharCol char(10)
+);
GO
insert into Department values (1, 'Account', 0);
insert into Department values (2, 'Sales', 0);
@@ -477,3 +477,67 @@ module MappingTest =
assert_equal [|1uy; 2uy; 3uy |] entity.VarBinaryCol
assert_equal "abc" entity.VarcharStringCol
assert_equal "あいう" entity.NVarcharStringCol
+
+ type CharMapping =
+ { [<Id>]
+ CharMappingId : int32
+ CharCol : CharString }
+
+ [<Test>]
+ let ``char mapping``() =
+ use tx = new TransactionScope ()
+ MsSql.insert<CharMapping>
+ { CharMappingId = 1
+ CharCol = CharString("abc") } |> ignore
+ let entity = MsSql.find<CharMapping> [1]
+ printfn "%A" entity
+ assert_equal (CharString("abc ")) entity.CharCol
+ assert_equal 10 entity.CharCol.Value.Length
+ let list =
+ MsSql.query
+ "select * from CharMapping where CharCol = /* CharCol */'' "
+ ["CharCol" @= CharString("abc")]
+ printfn "%A" list
+ assert_equal 1 list.Length
+ assert_equal (CharString("abc ")) list.Head.CharCol
+
+ [<Table(Name = "CharMapping")>]
+ type OptionCharMapping =
+ { [<Id>]
+ CharMappingId : int32
+ CharCol : CharString option}
+
+ [<Test>]
+ let ``option char mapping : some``() =
+ use tx = new TransactionScope ()
+ MsSql.insert<OptionCharMapping>
+ { CharMappingId = 1
+ CharCol = Some (CharString("abc")) } |> ignore
+ let entity = MsSql.find<OptionCharMapping> [1]
+ printfn "%A" entity
+ assert_equal (Some (CharString("abc "))) entity.CharCol
+ assert_equal 10 entity.CharCol.Value.Value.Length
+ let list =
+ MsSql.query<OptionCharMapping>
+ "select * from CharMapping where CharCol = /* CharCol */'' "
+ ["CharCol" @= CharString("abc")]
+ printfn "%A" list
+ assert_equal 1 list.Length
+ assert_equal (Some (CharString("abc "))) list.Head.CharCol
+
+ [<Test>]
+ let ``option char mapping : none``() =
+ use tx = new TransactionScope ()
+ MsSql.insert<OptionCharMapping>
+ { CharMappingId = 1
+ CharCol = None } |> ignore
+ let entity = MsSql.find<OptionCharMapping> [1]
+ printfn "%A" entity
+ assert_equal None entity.CharCol
+ let list =
+ MsSql.query<OptionCharMapping>
+ "select * from CharMapping where CharCol is null"
+ []
+ printfn "%A" list
+ assert_equal 1 list.Length
+ assert_equal None list.Head.CharCol
@@ -1218,14 +1218,14 @@ module SqlTest =
assert_equal (Nullable()) (dialect.ConvertFromDbToClr(Convert.DBNull, typeof<int Nullable>, null))
[<Test>]
- let ``MsSqlDialect : ConvertFromDbToClr : StringFixedLength`` () =
+ let ``MsSqlDialect : ConvertFromDbToClr : CharString`` () =
let dialect = MsSqlDialect()
// raw type
- assert_equal (StringFixedLength("abc")) (dialect.ConvertFromDbToClr(StringFixedLength("abc"), typeof<StringFixedLength>, null))
- assert_equal null (dialect.ConvertFromDbToClr(Convert.DBNull, typeof<StringFixedLength>, null))
+ assert_equal (CharString("abc")) (dialect.ConvertFromDbToClr("abc", typeof<CharString>, null))
+ assert_equal null (dialect.ConvertFromDbToClr(Convert.DBNull, typeof<CharString>, null))
// option type
- assert_equal (Some (StringFixedLength("abc"))) (dialect.ConvertFromDbToClr(StringFixedLength("abc"), typeof<StringFixedLength option>, null))
- assert_equal None (dialect.ConvertFromDbToClr(Convert.DBNull, typeof<StringFixedLength option>, null))
+ assert_equal (Some (CharString("abc"))) (dialect.ConvertFromDbToClr("abc", typeof<CharString option>, null))
+ assert_equal None (dialect.ConvertFromDbToClr(Convert.DBNull, typeof<CharString option>, null))
[<Test>]
let ``MsSqlDialect : ConvertFromDbToClr : enum type`` () =
@@ -1263,6 +1263,16 @@ module SqlTest =
assert_equal (Convert.DBNull, typeof<int>, DbType.Int32) (dialect.ConvertFromClrToDb(Nullable(), typeof<int Nullable>, null))
[<Test>]
+ let ``MsSqlDialect : ConvertFromClrToDb : CharString`` () =
+ let dialect = MsSqlDialect()
+ // raw type
+ assert_equal (box <| "abc", typeof<CharString>, DbType.StringFixedLength) (dialect.ConvertFromClrToDb(CharString("abc"), typeof<CharString>, null))
+ assert_equal (Convert.DBNull, typeof<CharString>, DbType.StringFixedLength) (dialect.ConvertFromClrToDb(null, typeof<CharString>, null))
+ // option type
+ assert_equal (box <| "abc", typeof<CharString>, DbType.StringFixedLength) (dialect.ConvertFromClrToDb(Some (CharString("abc")), typeof<CharString option>, null))
+ assert_equal (Convert.DBNull, typeof<CharString>, DbType.StringFixedLength) (dialect.ConvertFromClrToDb(None, typeof<CharString option>, null))
+
+ [<Test>]
let ``MsSqlDialect : ConvertFromClrToDb : enum type`` () =
let dialect = MsSqlDialect()
// raw type
@@ -1289,6 +1299,7 @@ module SqlTest =
let dialect = MsSqlDialect()
assert_equal "null" (dialect.FormatAsSqlLiteral(Convert.DBNull, typeof<int>, DbType.Int32))
assert_equal "N'aaa'" (dialect.FormatAsSqlLiteral("aaa", typeof<string>, DbType.String))
+ assert_equal "N'aaa'" (dialect.FormatAsSqlLiteral(CharString("aaa"), typeof<CharString>, DbType.StringFixedLength))
assert_equal "'12:34:56'" (dialect.FormatAsSqlLiteral(TimeSpan(12, 34, 56), typeof<TimeSpan>, DbType.Time))
assert_equal "'1234-01-23'" (dialect.FormatAsSqlLiteral(DateTime(1234, 01, 23), typeof<DateTime>, DbType.Date))
assert_equal "'1234-01-23 13:34:46.123'" (dialect.FormatAsSqlLiteral(DateTime(1234, 01, 23, 13, 34, 46, 123), typeof<DateTime>, DbType.DateTime))
@@ -1590,34 +1601,34 @@ module SqlTest =
assert_true (ex.Message.Contains "[SOMA1025]")
[<Test>]
- let ``StringFixedLength : Equals`` =
- assert_true (StringFixedLength("abc").Equals(StringFixedLength("abc")))
- assert_true (StringFixedLength(null).Equals(StringFixedLength(null)))
- assert_false (StringFixedLength("abc").Equals(StringFixedLength("123")))
- assert_false (StringFixedLength(null).Equals(StringFixedLength("abc")))
- assert_false (StringFixedLength("abc").Equals(StringFixedLength(null)))
+ let ``CharString : Equals`` =
+ assert_true (CharString("abc").Equals(CharString("abc")))
+ assert_true (CharString(null).Equals(CharString(null)))
+ assert_false (CharString("abc").Equals(CharString("123")))
+ assert_false (CharString(null).Equals(CharString("abc")))
+ assert_false (CharString("abc").Equals(CharString(null)))
[<Test>]
- let ``StringFixedLength : Equality`` =
- assert_true (StringFixedLength("abc") = (StringFixedLength("abc")))
- assert_true (StringFixedLength(null) = (StringFixedLength(null)))
- assert_false (StringFixedLength("abc") = (StringFixedLength("123")))
- assert_false (StringFixedLength(null) = (StringFixedLength("abc")))
- assert_false (StringFixedLength("abc") = (StringFixedLength(null)))
+ let ``CharString : Equality`` =
+ assert_true (CharString("abc") = (CharString("abc")))
+ assert_true (CharString(null) = (CharString(null)))
+ assert_false (CharString("abc") = (CharString("123")))
+ assert_false (CharString(null) = (CharString("abc")))
+ assert_false (CharString("abc") = (CharString(null)))
[<Test>]
- let ``StringFixedLength : Inequality`` =
- assert_false (StringFixedLength("abc") <> (StringFixedLength("abc")))
- assert_false (StringFixedLength(null) <> (StringFixedLength(null)))
- assert_true (StringFixedLength("abc") <> (StringFixedLength("123")))
- assert_true (StringFixedLength(null) <> (StringFixedLength("abc")))
- assert_true (StringFixedLength("abc") <> (StringFixedLength(null)))
+ let ``CharString : Inequality`` =
+ assert_false (CharString("abc") <> (CharString("abc")))
+ assert_false (CharString(null) <> (CharString(null)))
+ assert_true (CharString("abc") <> (CharString("123")))
+ assert_true (CharString(null) <> (CharString("abc")))
+ assert_true (CharString("abc") <> (CharString(null)))
[<Test>]
- let ``StringFixedLength : CompareTo`` =
- assert_equal 0 ((StringFixedLength("abc") :> IComparable<StringFixedLength>).CompareTo(StringFixedLength("abc")))
- assert_equal 0 ((StringFixedLength(null) :> IComparable<StringFixedLength>).CompareTo(StringFixedLength(null)))
- assert_equal -1 ((StringFixedLength("abc") :> IComparable<StringFixedLength>).CompareTo(StringFixedLength("def")))
- assert_equal 1 ((StringFixedLength("def") :> IComparable<StringFixedLength>).CompareTo(StringFixedLength("abc")))
- assert_equal 1 ((StringFixedLength("abc") :> IComparable<StringFixedLength>).CompareTo(StringFixedLength(null)))
- assert_equal -1 ((StringFixedLength(null) :> IComparable<StringFixedLength>).CompareTo(StringFixedLength("abc")))
+ let ``CharString : CompareTo`` =
+ assert_equal 0 ((CharString("abc") :> IComparable<CharString>).CompareTo(CharString("abc")))
+ assert_equal 0 ((CharString(null) :> IComparable<CharString>).CompareTo(CharString(null)))
+ assert_equal -1 ((CharString("abc") :> IComparable<CharString>).CompareTo(CharString("def")))
+ assert_equal 1 ((CharString("def") :> IComparable<CharString>).CompareTo(CharString("abc")))
+ assert_equal 1 ((CharString("abc") :> IComparable<CharString>).CompareTo(CharString(null)))
+ assert_equal -1 ((CharString(null) :> IComparable<CharString>).CompareTo(CharString("abc")))
View
@@ -48,44 +48,30 @@ exception NoUpdatablePropertyException of unit with
Unchecked.defaultof<_>
[<Class>]
+[<Sealed>]
[<AllowNullLiteral>]
-type StringFixedLength(value:string) =
- member this.Value = value
- override this.Equals(other) =
- if obj.ReferenceEquals(this, other) then
+type CharString(value:string) =
+ static let eq (a:obj) (b:obj) =
+ if obj.ReferenceEquals(a, b) then
true
else
- match other with
- | (:? StringFixedLength as other) -> this.Value = other.Value
+ match a, b with
+ | (:? CharString as a), (:? CharString as b) -> a.Value = b.Value
| _ -> false
- override this.GetHashCode() =
- if this.Value = null then 0 else this.Value.GetHashCode()
- override this.ToString() =
- if this.Value = null then String.Empty else this.Value
- static member op_Implicit(value:string) = StringFixedLength(value)
- static member op_Implicit(value:StringFixedLength) = value.Value
- static member op_Equality(a:StringFixedLength, b:StringFixedLength) =
- if obj.ReferenceEquals(a, b) then
- true
- elif a = null || b = null then
- false
- else
- a.Value = b.Value
- static member op_Inequality(a:StringFixedLength, b:StringFixedLength) =
- not (a = b)
-
- interface IComparable<StringFixedLength> with
+ member this.Value = if value = null then String.Empty else value
+ override this.Equals(other) = eq this other
+ override this.GetHashCode() = this.Value.GetHashCode()
+ override this.ToString() = this.Value
+ static member op_Equality(a:CharString, b:CharString) = eq a b
+ static member op_Inequality(a:CharString, b:CharString) = not <| eq a b
+
+ interface IComparable<CharString> with
member this.CompareTo(other) =
- if obj.ReferenceEquals(this, other) then
- 0
+ if eq this other then 0
else
match other with
| null -> 1
- | _ ->
- if this.Value = null && other.Value = null then 0
- elif this.Value = null then -1
- elif other.Value = null then 1
- else this.Value.CompareTo(other.Value)
+ | _ -> this.Value.CompareTo(other.Value)
type InsertOpt() =
let mutable exclude:seq<string> = null
@@ -951,8 +937,8 @@ type DialectBase() as this =
default this.ConvertFromDbToUnderlyingClr (dbValue:obj, destType:Type) =
if dbValue.GetType() = destType then
dbValue
- elif destType = typeof<StringFixedLength> then
- upcast StringFixedLength(Convert.ToString(dbValue))
+ elif destType = typeof<CharString> then
+ upcast CharString(Convert.ToString(dbValue))
else
Convert.ChangeType(dbValue, destType)
@@ -999,7 +985,7 @@ type DialectBase() as this =
| t when t = typeof<DateTime> || t = typeof<DateTime Nullable> -> DbType.DateTime
| t when t = typeof<TimeSpan> || t = typeof<TimeSpan Nullable> -> DbType.Time
| t when t = typeof<Boolean> || t = typeof<Boolean Nullable> -> DbType.Boolean
- | t when t = typeof<StringFixedLength> -> DbType.StringFixedLength
+ | t when t = typeof<CharString> -> DbType.StringFixedLength
| t when t = typeof<Byte> || t = typeof<Byte Nullable> -> DbType.Byte
| t when t = typeof<Byte[]> -> DbType.Binary
| t when t = typeof<DateTimeOffset> || t = typeof<DateTimeOffset Nullable> -> DbType.DateTimeOffset
@@ -1037,6 +1023,13 @@ type DialectBase() as this =
Reflection.getNullableElement (clrValue, typ)
else
clrValue, typ
+ let value =
+ if typ = typeof<CharString> then
+ match value with
+ | :? CharString as cs -> cs.Value :> obj
+ | _ -> value
+ else
+ value
if typ.IsEnum then
let text = Enum.Format(typ, value, "D")
match typ.GetEnumUnderlyingType() with
@@ -1060,7 +1053,7 @@ type DialectBase() as this =
else
let quote () = "'" + string dbValue + "'"
match dbType with
- | d when d = DbType.String ->
+ | d when d = DbType.String || d = DbType.StringFixedLength->
"N'" + string dbValue + "'"
| d when d = DbType.Time ->
match dbValue with
@@ -1447,6 +1440,7 @@ type MsSqlDialect() =
| d when d = DbType.Binary -> "varbinary"
| d when d = DbType.Double -> "float"
| d when d = DbType.String -> "nvarchar"
+ | d when d = DbType.StringFixedLength -> "nchar"
| d when d = DbType.Int16 -> "smallint"
| d when d = DbType.Time -> "time"
| d when d = DbType.Byte -> "tinyint"
@@ -1498,6 +1492,7 @@ type MsSqlDialect() =
match param.DbType with
| d when d = DbType.Binary -> dbParam.SqlDbType <- SqlDbType.VarBinary
| d when d = DbType.String -> dbParam.SqlDbType <- SqlDbType.NVarChar
+ | d when d = DbType.StringFixedLength -> dbParam.SqlDbType <- SqlDbType.NChar
| d when d = DbType.Date -> dbParam.SqlDbType <- SqlDbType.Date
| d when d = DbType.DateTimeOffset -> dbParam.SqlDbType <- SqlDbType.DateTimeOffset
| d when d = DbType.Time -> dbParam.SqlDbType <- SqlDbType.Time
@@ -1917,7 +1912,7 @@ type OracleDialect() =
else
let literal =
match dbType with
- | d when d = DbType.String ->
+ | d when d = DbType.String || d = DbType.StringFixedLength ->
Some ("'" + string dbValue + "'")
| d when d = DbType.Time ->
match dbValue with
View
@@ -25,26 +25,21 @@ exception NoUpdatablePropertyException of unit
/// <summary>Represents the fixed length string.</summary>
[<Class>]
-type StringFixedLength =
- interface IComparable<StringFixedLength>
+[<Sealed>]
+type CharString =
+ interface IComparable<CharString>
- /// <summary>Initializes a StringFixedLength instance.</summary>
- new : string -> StringFixedLength
+ /// <summary>Initializes a CharString instance.</summary>
+ new : string -> CharString
/// <summary>Gets the string value.</summary>
member Value : string with get
- /// <summary>Converts a string value to a fixed length string value.</summary>
- static member op_Implicit : string -> StringFixedLength
+ /// <summary>Determines whether two specified CharString objects have the same value.</summary>
+ static member op_Equality : CharString * CharString -> bool
- /// <summary>Converts a fixed length string value to a string value.</summary>
- static member op_Implicit : StringFixedLength -> string
-
- /// <summary>Determines whether two specified StringFixedLength objects have the same value.</summary>
- static member op_Equality : StringFixedLength * StringFixedLength -> bool
-
- /// <summary>Determines whether two specified StringFixedLength objects have the different value.</summary>
- static member op_Inequality : StringFixedLength * StringFixedLength -> bool
+ /// <summary>Determines whether two specified CharString objects have the different value.</summary>
+ static member op_Inequality : CharString * CharString -> bool
/// <summary>Represents the options of insert operation.</summary>
[<Class>]

0 comments on commit 1547578

Please sign in to comment.