7
7
using System . Text ;
8
8
using System . Collections . ObjectModel ;
9
9
using System . Linq . Expressions ;
10
+ using System . Runtime . CompilerServices ;
10
11
11
12
namespace ServiceStack . OrmLite
12
13
{
@@ -21,8 +22,8 @@ public abstract partial class SqlExpression<T> : ISqlExpression, IHasUntypedSqlE
21
22
private string havingExpression ;
22
23
private string orderBy = string . Empty ;
23
24
24
- IList < string > updateFields = new List < string > ( ) ;
25
- IList < string > insertFields = new List < string > ( ) ;
25
+ public List < string > UpdateFields { get ; set ; }
26
+ public List < string > InsertFields { get ; set ; }
26
27
27
28
private string sep = string . Empty ;
28
29
protected bool useFieldName = false ;
@@ -41,9 +42,13 @@ protected string Sep
41
42
42
43
public SqlExpression ( IOrmLiteDialectProvider dialectProvider )
43
44
{
45
+ UpdateFields = new List < string > ( ) ;
46
+ InsertFields = new List < string > ( ) ;
47
+
44
48
modelDef = typeof ( T ) . GetModelDefinition ( ) ;
45
49
PrefixFieldWithTableName = false ;
46
50
WhereStatementWithoutWhereString = false ;
51
+
47
52
DialectProvider = dialectProvider ;
48
53
Params = new List < IDbDataParameter > ( ) ;
49
54
tableDefs . Add ( modelDef ) ;
@@ -66,8 +71,8 @@ protected virtual SqlExpression<T> CopyTo(SqlExpression<T> to)
66
71
to . groupBy = groupBy ;
67
72
to . havingExpression = havingExpression ;
68
73
to . orderBy = orderBy ;
69
- to . updateFields = updateFields ;
70
- to . insertFields = insertFields ;
74
+ to . UpdateFields = UpdateFields ;
75
+ to . InsertFields = InsertFields ;
71
76
to . modelDef = modelDef ;
72
77
to . PrefixFieldWithTableName = PrefixFieldWithTableName ;
73
78
to . WhereStatementWithoutWhereString = WhereStatementWithoutWhereString ;
@@ -662,9 +667,9 @@ public virtual SqlExpression<T> ClearLimits()
662
667
/// <param name='updatefields'>
663
668
/// IList<string> containing Names of properties to be updated
664
669
/// </param>
665
- public virtual SqlExpression < T > Update ( IList < string > updateFields )
670
+ public virtual SqlExpression < T > Update ( List < string > updateFields )
666
671
{
667
- this . updateFields = updateFields ;
672
+ this . UpdateFields = updateFields ;
668
673
return this ;
669
674
}
670
675
@@ -681,7 +686,7 @@ public virtual SqlExpression<T> Update<TKey>(Expression<Func<T, TKey>> fields)
681
686
{
682
687
sep = string . Empty ;
683
688
useFieldName = false ;
684
- updateFields = Visit ( fields ) . ToString ( ) . Split ( ',' ) . ToList ( ) ;
689
+ UpdateFields = Visit ( fields ) . ToString ( ) . Split ( ',' ) . ToList ( ) ;
685
690
return this ;
686
691
}
687
692
@@ -690,7 +695,7 @@ public virtual SqlExpression<T> Update<TKey>(Expression<Func<T, TKey>> fields)
690
695
/// </summary>
691
696
public virtual SqlExpression < T > Update ( )
692
697
{
693
- this . updateFields = new List < string > ( ) ;
698
+ this . UpdateFields = new List < string > ( ) ;
694
699
return this ;
695
700
}
696
701
@@ -707,7 +712,7 @@ public virtual SqlExpression<T> Insert<TKey>(Expression<Func<T, TKey>> fields)
707
712
{
708
713
sep = string . Empty ;
709
714
useFieldName = false ;
710
- insertFields = Visit ( fields ) . ToString ( ) . Split ( ',' ) . ToList ( ) ;
715
+ InsertFields = Visit ( fields ) . ToString ( ) . Split ( ',' ) . ToList ( ) ;
711
716
return this ;
712
717
}
713
718
@@ -717,9 +722,9 @@ public virtual SqlExpression<T> Insert<TKey>(Expression<Func<T, TKey>> fields)
717
722
/// <param name='insertFields'>
718
723
/// IList<string> containing Names of properties to be inserted
719
724
/// </param>
720
- public virtual SqlExpression < T > Insert ( IList < string > insertFields )
725
+ public virtual SqlExpression < T > Insert ( List < string > insertFields )
721
726
{
722
- this . insertFields = insertFields ;
727
+ this . InsertFields = insertFields ;
723
728
return this ;
724
729
}
725
730
@@ -728,7 +733,7 @@ public virtual SqlExpression<T> Insert(IList<string> insertFields)
728
733
/// </summary>
729
734
public virtual SqlExpression < T > Insert ( )
730
735
{
731
- this . insertFields = new List < string > ( ) ;
736
+ this . InsertFields = new List < string > ( ) ;
732
737
return this ;
733
738
}
734
739
@@ -901,30 +906,6 @@ public string OrderByExpression
901
906
public int ? Rows { get ; set ; }
902
907
public int ? Offset { get ; set ; }
903
908
904
- public IList < string > UpdateFields
905
- {
906
- get
907
- {
908
- return updateFields ;
909
- }
910
- set
911
- {
912
- updateFields = value ;
913
- }
914
- }
915
-
916
- public IList < string > InsertFields
917
- {
918
- get
919
- {
920
- return insertFields ;
921
- }
922
- set
923
- {
924
- insertFields = value ;
925
- }
926
- }
927
-
928
909
public ModelDefinition ModelDef
929
910
{
930
911
get
@@ -1733,6 +1714,10 @@ public IDbDataParameter CreateParam(string name,
1733
1714
p . Value = DialectProvider . GetParamValue ( value , value . GetType ( ) ) ;
1734
1715
DialectProvider . InitDbParam ( p , value . GetType ( ) ) ;
1735
1716
}
1717
+ else
1718
+ {
1719
+ p . Value = DBNull . Value ;
1720
+ }
1736
1721
1737
1722
if ( dbType != null )
1738
1723
p . DbType = dbType . Value ;
@@ -1800,17 +1785,34 @@ public static IDbDataParameter CreateParam(this IDbConnection db,
1800
1785
string name ,
1801
1786
object value = null ,
1802
1787
DbType ? dbType = null ,
1803
- bool ? isNullable = null ,
1804
1788
byte ? precision = null ,
1805
1789
byte ? scale = null ,
1806
1790
int ? size = null )
1807
1791
{
1808
- var dialectProvider = db . GetDialectProvider ( ) ;
1792
+ return db . GetDialectProvider ( ) . CreateParam ( name , value , dbType , precision , scale , size ) ;
1793
+ }
1809
1794
1795
+ public static IDbDataParameter CreateParam ( this IOrmLiteDialectProvider dialectProvider ,
1796
+ string name ,
1797
+ object value = null ,
1798
+ DbType ? dbType = null ,
1799
+ byte ? precision = null ,
1800
+ byte ? scale = null ,
1801
+ int ? size = null )
1802
+ {
1810
1803
var to = dialectProvider . CreateParam ( ) ;
1811
1804
1812
1805
to . ParameterName = dialectProvider . GetParam ( name ) ;
1813
- to . Value = value ;
1806
+
1807
+ if ( value != null )
1808
+ {
1809
+ to . Value = dialectProvider . GetParamValue ( value , value . GetType ( ) ) ;
1810
+ dialectProvider . InitDbParam ( to , value . GetType ( ) ) ;
1811
+ }
1812
+ else
1813
+ {
1814
+ to . Value = DBNull . Value ;
1815
+ }
1814
1816
1815
1817
var valueType = value != null ? value . GetType ( ) : typeof ( string ) ;
1816
1818
@@ -1828,6 +1830,15 @@ public static IDbDataParameter CreateParam(this IDbConnection db,
1828
1830
1829
1831
return to ;
1830
1832
}
1833
+
1834
+ public static IDbDataParameter AddParam ( this IOrmLiteDialectProvider dialectProvider , IDbCommand dbCmd , object value )
1835
+ {
1836
+ var paramName = dbCmd . Parameters . Count . ToString ( ) ;
1837
+
1838
+ var parameter = dialectProvider . CreateParam ( paramName , value ) ;
1839
+ dbCmd . Parameters . Add ( parameter ) ;
1840
+ return parameter ;
1841
+ }
1831
1842
}
1832
1843
}
1833
1844
0 commit comments