1+ using System ;
2+ using System . Net ;
3+ using NUnit . Framework ;
4+ using ServiceStack . DataAnnotations ;
5+
6+ namespace ServiceStack . OrmLite . Tests . Issues
7+ {
8+ public class InsertIntoSource
9+ {
10+ [ AutoIncrement ]
11+ public long Id { get ; set ; }
12+
13+ public string Url { get ; set ; }
14+ public string Provider { get ; set ; }
15+ public string DomainKey { get ; set ; }
16+ public bool NoFollow { get ; set ; }
17+ public HttpStatusCode HttpStatusCode { get ; set ; }
18+ public DateTime ? LastScanTime { get ; set ; }
19+ public string Anchors { get ; set ; }
20+ public int ? OutboundLinks { get ; set ; }
21+ public long TargetDomainRecordId { get ; set ; }
22+ public long UserAuthCustomId { get ; set ; }
23+ }
24+
25+ public class InertIntoTarget
26+ {
27+ [ PrimaryKey ]
28+ public long WatchedUrlRecordId { get ; set ; }
29+
30+ public string Url { get ; set ; }
31+ public string DomainKey { get ; set ; }
32+ public long TargetDomainRecordId { get ; set ; }
33+ public string TargetDomainKey { get ; set ; }
34+ public DateTime CreateDate { get ; set ; } = DateTime . UtcNow ;
35+ public int Tries { get ; set ; }
36+ public DateTime ? DeferUntil { get ; set ; }
37+ public long UserAuthCustomId { get ; set ; }
38+ public bool FirstScan { get ; set ; }
39+ }
40+
41+ public class InsertIntoJoin
42+ {
43+ [ AutoIncrement ]
44+ public long Id { get ; set ; }
45+
46+ public string Url { get ; set ; }
47+ public string DomainKey { get ; set ; }
48+ public DateTime CreateDate { get ; set ; } = DateTime . Now ;
49+ public DateTime ? DeleteDate { get ; set ; }
50+ public bool IsDeleted { get ; set ; }
51+ public bool Active { get ; set ; } = true ;
52+ public long UserAuthCustomId { get ; set ; }
53+ }
54+
55+ public class CustomInsertIntoSelectJoinIssue : OrmLiteTestBase
56+ {
57+ [ Test ]
58+ public void Can_custom_join_into_select ( )
59+ {
60+ using var db = OpenDbConnection ( ) ;
61+
62+ db . DropAndCreateTable < InsertIntoSource > ( ) ;
63+ db . DropAndCreateTable < InsertIntoJoin > ( ) ;
64+ db . DropAndCreateTable < InertIntoTarget > ( ) ;
65+
66+ var ids = new [ ] { 1 , 2 , 3 } ;
67+ // OrmLiteUtils.PrintSql();
68+
69+ var q = db . From < InsertIntoSource > ( )
70+ . Join < InsertIntoJoin > ( ( w , t ) => w . TargetDomainRecordId == t . Id )
71+ . Where ( x => Sql . In ( x . Id , ids ) )
72+ . Select < InsertIntoSource , InsertIntoJoin > ( ( w , t ) => new {
73+ UserAuthCustomId = w . UserAuthCustomId ,
74+ DomainKey = w . DomainKey ,
75+ CreateDate = DateTime . UtcNow ,
76+ TargetDomainKey = t . DomainKey ,
77+ Tries = 0 ,
78+ TargetDomainRecordId = w . TargetDomainRecordId ,
79+ Url = w . Url ,
80+ WatchedUrlRecordId = w . Id
81+ } ) ;
82+
83+ var inserted = db . InsertIntoSelect < InertIntoTarget > ( q , dbCmd => dbCmd . OnConflictIgnore ( ) ) ;
84+ }
85+ }
86+ }
0 commit comments