1+ using System ;
2+ using NUnit . Framework ;
3+ using ServiceStack . DataAnnotations ;
4+
5+ namespace ServiceStack . OrmLite . Tests . Issues
6+ {
7+ public class DynamicResultsNamingStrategyTest : OrmLiteTestBase
8+ {
9+ public class Menu : EntityBase < Menu >
10+ {
11+ [ AutoIncrement ]
12+ public int Id { get ; set ; }
13+
14+ [ ForeignKey ( typeof ( Menu ) ) ]
15+ public int ? ParentId { get ; set ; }
16+
17+ // [Required]
18+ // public MenuType Type { get; set; }
19+
20+ [ Required , StringLength ( 100 ) ]
21+ public string Name { get ; set ; }
22+
23+ [ StringLength ( 100 ) ]
24+ public string Form { get ; set ; }
25+
26+ [ StringLength ( 50 ) ]
27+ public string Icon { get ; set ; }
28+
29+ [ StringLength ( 1000 ) ]
30+ public string Style { get ; set ; }
31+
32+ // [ForeignKey(typeof(User))]
33+ public int ? UserId { get ; set ; }
34+ }
35+
36+ public abstract class EntityBase < T > // : IEntity<T>
37+ {
38+ [ Required , Default ( typeof ( bool ) , "0" ) ]
39+ public bool IsDeleted { get ; set ; }
40+ [ Required , Default ( typeof ( bool ) , "1" ) ]
41+ public bool IsActive { get ; set ; } = true ;
42+ public int ? Position { get ; set ; }
43+ //public ulong RowVersion { get; set; }
44+ public Guid RecId { get ; set ; }
45+ }
46+
47+ public class DatabaseNamingStrategy : OrmLiteNamingStrategyBase
48+ {
49+ public override string GetTableName ( string name )
50+ {
51+ return ToUnderscoreSeparated ( name ) ;
52+ }
53+
54+ public override string GetColumnName ( string name )
55+ {
56+ return ToUnderscoreSeparated ( name ) ;
57+ }
58+
59+
60+ string ToUnderscoreSeparated ( string name )
61+ {
62+
63+ string r = char . ToLower ( name [ 0 ] ) . ToString ( ) ;
64+
65+ for ( int i = 1 ; i < name . Length ; i ++ )
66+ {
67+ char c = name [ i ] ;
68+ if ( char . IsUpper ( name [ i ] ) )
69+ {
70+ r += "_" ;
71+ r += char . ToLower ( name [ i ] ) ;
72+ }
73+ else
74+ {
75+ r += name [ i ] ;
76+ }
77+ }
78+ return r ;
79+ }
80+ }
81+
82+ [ Test ]
83+ public void Can_select_dynamic_results_from_custom_NamingStrategy ( )
84+ {
85+ OrmLiteConfig . BeforeExecFilter = dbCmd => Console . WriteLine ( dbCmd . GetDebugString ( ) ) ;
86+
87+ var hold = SqliteDialect . Provider . NamingStrategy ;
88+ SqliteDialect . Provider . NamingStrategy = new DatabaseNamingStrategy ( ) ;
89+
90+ using ( var db = OpenDbConnection ( ) )
91+ {
92+ db . DropAndCreateTable < Menu > ( ) ;
93+
94+ var rows = new [ ] {
95+ new Menu {
96+ Name = "Test List" ,
97+ RecId = new Guid ( "2F96233B-152E-4D20-BE08-5633431A9EBC" )
98+ }
99+ } ;
100+
101+ db . InsertAll ( rows ) ;
102+
103+ var q = db . From < Menu > ( ) . Select ( x => new { x . Id , x . RecId , x . Name } ) ;
104+ var results = db . Select < ( int id , Guid recId , string name ) > ( q ) ;
105+
106+ var expected = rows [ 0 ] ;
107+ Assert . That ( results [ 0 ] . id , Is . EqualTo ( 1 ) ) ;
108+ Assert . That ( results [ 0 ] . recId , Is . EqualTo ( expected . RecId ) ) ;
109+ Assert . That ( results [ 0 ] . name , Is . EqualTo ( expected . Name ) ) ;
110+ }
111+
112+ SqliteDialect . Provider . NamingStrategy = hold ;
113+ }
114+ }
115+ }
0 commit comments