44using Thrift ;
55using Thrift . Protocol ;
66using Thrift . Transport . Client ;
7+ using System . Net ;
8+ using System . Collections . Generic ;
9+ using System . Linq ;
10+ using System . Reflection ;
11+ using Nebula . Common ;
12+ using System . Threading ;
713
814namespace NebulaNet
915{
@@ -17,18 +23,23 @@ public class GraphClient
1723 public GraphClient ( string ip , int port )
1824 {
1925 var tConfiguration = new TConfiguration ( ) ;
20- socketTransport = new TSocketTransport ( ip , port , tConfiguration ) ;
2126
22- socketTransport . OpenAsync ( default ) ;
27+ if ( IPAddress . TryParse ( ip , out IPAddress iPAddress ) )
28+ socketTransport = new TSocketTransport ( iPAddress , port , tConfiguration ) ;
29+ else
30+ socketTransport = new TSocketTransport ( ip , port , tConfiguration ) ;
31+
32+ socketTransport . OpenAsync ( default ) . Wait ( ) ;
2333
2434 protocolProtocol = new TBinaryProtocol ( socketTransport ) ;
2535
2636 Client = new GraphService . Client ( protocolProtocol ) ;
2737 }
28-
2938 public void Authenticate ( string user , string passwd )
3039 {
31- var authResponse = Client . authenticate ( System . Text . Encoding . Default . GetBytes ( user ) , System . Text . Encoding . Default . GetBytes ( passwd ) ) . Result ;
40+
41+ var authResponse = Client . authenticate ( System . Text . Encoding . ASCII . GetBytes ( user ) , System . Text . Encoding . ASCII . GetBytes ( passwd ) ) . Result ;
42+
3243 if ( authResponse . Error_code != 0 )
3344 {
3445 throw new Exception ( "error auth!" ) ;
@@ -44,12 +55,42 @@ public async Task<ExecutionResponse> Execute(string statement)
4455 var executionResponse = await Client . execute ( sessionId , System . Text . Encoding . Default . GetBytes ( statement ) ) ;
4556 if ( executionResponse . Error_code != 0 )
4657 {
47- throw new Exception ( System . Text . Encoding . ASCII . GetString ( executionResponse . Error_msg ) + statement + " execute failed." ) ;
58+ throw new Exception ( System . Text . Encoding . UTF8 . GetString ( executionResponse . Error_msg ) + statement + " execute failed." ) ;
4859 }
4960
5061 return executionResponse ;
5162 }
5263
64+ public async Task < List < T > > Execute < T > ( string statement ) where T : class
65+ {
66+ var executionResponse = await Execute ( statement ) ;
67+ if ( executionResponse . Data == null )
68+ return default ;
69+
70+ //获取列名
71+ var columnNames = executionResponse . Data . Column_names
72+ . Select ( x => System . Text . Encoding . UTF8 . GetString ( x ) . ToLower ( ) ) . ToArray ( ) ;
73+
74+ //查找可用属性和数据索引
75+ var indexAndProps = typeof ( T ) . GetProperties ( BindingFlags . Public | BindingFlags . Instance ) //BindingFlags.IgnoreCase
76+ . Select ( x => new { Index = Array . IndexOf ( columnNames , x . Name . ToLower ( ) ) , Prop = x } )
77+ . Where ( x => x . Index >= 0 ) ;
78+
79+ //映射对象
80+ var result = new List < T > ( ) ;
81+ foreach ( var row in executionResponse . Data . Rows )
82+ {
83+ var o = Activator . CreateInstance < T > ( ) ;
84+ foreach ( var item in indexAndProps )
85+ {
86+ item . Prop . SetValue ( o , row . Values [ item . Index ] . GetValue ( item . Prop . PropertyType ) ) ;
87+ }
88+ result . Add ( o ) ;
89+ }
90+
91+ return result ;
92+ }
93+
5394 public async Task SignOff ( )
5495 {
5596 await Client . signout ( sessionId ) ;
0 commit comments