1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Data ;
5
+ using System . Data . SqlClient ;
6
+
7
+ namespace SqlToArangoDB
8
+ {
9
+ public class SQLReader : IDisposable
10
+ {
11
+ public List < Node > Nodes { get ; set ; }
12
+ public List < Edge > Edges { get ; set ; }
13
+
14
+ public string ConnectionString { get ; set ; }
15
+
16
+ public SQLReader ( string connection )
17
+ {
18
+ ConnectionString = connection ;
19
+ }
20
+ public void GetNodes ( )
21
+ {
22
+ Nodes = new List < Node > ( ) ;
23
+ //initialize connection
24
+ using ( SqlConnection sqlcon = new SqlConnection ( ConnectionString ) )
25
+ {
26
+ sqlcon . Open ( ) ;
27
+ //retrieving nodes tables
28
+ using ( SqlCommand sqlnodes = new SqlCommand ( "select name from sys.tables where is_node = 1" , sqlcon ) )
29
+ {
30
+ SqlDataReader tablesreader ;
31
+ tablesreader = sqlnodes . ExecuteReader ( ) ;
32
+
33
+ List < string > tables = new List < string > ( ) ;
34
+
35
+ //get nodes tables
36
+ while ( tablesreader . Read ( ) )
37
+ tables . Add ( tablesreader [ 0 ] . ToString ( ) ) ;
38
+
39
+ tablesreader . Close ( ) ;
40
+ foreach ( string tablename in tables )
41
+ {
42
+ //Get columns with data types
43
+ string cmdColumns = String . Format ( "select column_name,data_type from information_schema.columns columns where table_name = '{0}'" +
44
+ "and exists(select 1 from information_schema.columns temp where temp.column_name like 'graph_id_%'" +
45
+ "and temp.TABLE_SCHEMA = columns.table_schema and temp.TABLE_NAME = columns.TABLE_NAME)" , tablename ) ;
46
+ List < Column > columns = new List < Column > ( ) ;
47
+ using ( SqlCommand sqlcmd = new SqlCommand ( cmdColumns , sqlcon ) )
48
+ {
49
+ SqlDataReader columnsreader = sqlcmd . ExecuteReader ( ) ;
50
+ while ( columnsreader . Read ( ) )
51
+ {
52
+ columns . Add ( new Column ( columnsreader [ 0 ] . ToString ( ) , columnsreader [ 1 ] . ToString ( ) ) ) ;
53
+ }
54
+ columnsreader . Close ( ) ;
55
+ }
56
+
57
+ string idcolumn = columns . Where ( x => x . Name . StartsWith ( "$node_id" ) ) . FirstOrDefault ( ) . Name ;
58
+ string propColumns = string . Join ( "," , columns . Select ( x => x . Name ) . Where ( y => ! y . StartsWith ( "$" ) && ! y . StartsWith ( "graph_id_" ) ) ) ;
59
+ string cmdNodes = "select JSON_VALUE([" + idcolumn + "],'$.id') as node_id "
60
+ + ( propColumns == "" ? "" : "," + propColumns ) ;
61
+ cmdNodes = cmdNodes + string . Format ( " from {0}" , tablename ) ;
62
+ //get nodes
63
+ using ( SqlCommand sqlcmd = new SqlCommand ( cmdNodes , sqlcon ) )
64
+ {
65
+ SqlDataReader nodesreader = sqlcmd . ExecuteReader ( ) ;
66
+ //Get properties
67
+
68
+ while ( nodesreader . Read ( ) )
69
+ {
70
+ Dictionary < string , object > properties = new Dictionary < string , object > ( ) ;
71
+ foreach ( Column col in columns . Where ( x => ! x . Name . StartsWith ( "$" ) && ! x . Name . StartsWith ( "graph_id_" ) ) )
72
+ {
73
+ properties . Add ( col . Name , nodesreader [ col . Name ] ) ;
74
+ }
75
+ properties . Add ( "node_id" , nodesreader [ "node_id" ] . ToString ( ) ) ;
76
+ Nodes . Add ( new Node ( nodesreader [ "node_id" ] . ToString ( ) , tablename , properties ) ) ;
77
+ }
78
+ nodesreader . Close ( ) ;
79
+ }
80
+ }
81
+ }
82
+ }
83
+ }
84
+ public void GetEdges ( )
85
+ {
86
+ Edges = new List < Edge > ( ) ;
87
+ //initialize connection
88
+ using ( SqlConnection sqlcon = new SqlConnection ( ConnectionString ) )
89
+ {
90
+ sqlcon . Open ( ) ;
91
+ //retrieving nodes tables
92
+ using ( SqlCommand sqlnodes = new SqlCommand ( "select name from sys.tables where is_edge = 1" , sqlcon ) )
93
+ {
94
+ SqlDataReader tablesreader ;
95
+ tablesreader = sqlnodes . ExecuteReader ( ) ;
96
+
97
+ List < string > tables = new List < string > ( ) ;
98
+
99
+ //get edges tables
100
+ while ( tablesreader . Read ( ) )
101
+ tables . Add ( tablesreader [ 0 ] . ToString ( ) ) ;
102
+
103
+ tablesreader . Close ( ) ;
104
+ foreach ( string tablename in tables )
105
+ {
106
+ //Get columns with data types
107
+ string cmdColumns = String . Format ( "select column_name,data_type from information_schema.columns columns where table_name = '{0}'" +
108
+ "and exists(select 1 from information_schema.columns temp where temp.column_name like 'graph_id_%'" +
109
+ "and temp.TABLE_SCHEMA = columns.table_schema and temp.TABLE_NAME = columns.TABLE_NAME)" , tablename ) ;
110
+ List < Column > columns = new List < Column > ( ) ;
111
+ using ( SqlCommand sqlcmd = new SqlCommand ( cmdColumns , sqlcon ) )
112
+ {
113
+ SqlDataReader columnsreader = sqlcmd . ExecuteReader ( ) ;
114
+ while ( columnsreader . Read ( ) )
115
+ {
116
+ columns . Add ( new Column ( columnsreader [ 0 ] . ToString ( ) , columnsreader [ 1 ] . ToString ( ) ) ) ;
117
+ }
118
+ columnsreader . Close ( ) ;
119
+ }
120
+
121
+ string idcolumn = columns . Where ( x => x . Name . StartsWith ( "$edge_id" ) ) . FirstOrDefault ( ) . Name ;
122
+ string fromid = columns . Where ( x => x . Name . StartsWith ( "$from_id" ) ) . FirstOrDefault ( ) . Name ;
123
+ string toid = columns . Where ( x => x . Name . StartsWith ( "$to_id" ) ) . FirstOrDefault ( ) . Name ;
124
+ string propColumns = string . Join ( "," , columns . Select ( x => x . Name ) . Where ( y => ! y . StartsWith ( "$" ) && ! y . StartsWith ( "graph_id_" ) && ! y . StartsWith ( "from_" ) && ! y . StartsWith ( "to_" ) ) ) ;
125
+ string cmdNodes = "select JSON_VALUE([" + idcolumn + "],'$.id') as edge_id " +
126
+ ",JSON_VALUE([" + fromid + "],'$.id') as from_id " +
127
+ ",JSON_VALUE([" + fromid + "],'$.table') as from_table " +
128
+ ",JSON_VALUE([" + toid + "],'$.id') as to_id " +
129
+ ",JSON_VALUE([" + toid + "],'$.table') as to_table" +
130
+ ( propColumns == "" ? "" : "," + propColumns ) ;
131
+ cmdNodes = cmdNodes + string . Format ( " from {0}" , tablename ) ;
132
+
133
+ using ( SqlCommand sqlcmd = new SqlCommand ( cmdNodes , sqlcon ) )
134
+ {
135
+ SqlDataReader edgesreader = sqlcmd . ExecuteReader ( ) ;
136
+ //Get properties
137
+
138
+ while ( edgesreader . Read ( ) )
139
+ {
140
+ Dictionary < string , object > properties = new Dictionary < string , object > ( ) ;
141
+ foreach ( Column col in columns . Where ( y => ! y . Name . StartsWith ( "$" ) && ! y . Name . StartsWith ( "graph_id_" ) && ! y . Name . StartsWith ( "from_" ) && ! y . Name . StartsWith ( "to_" ) ) )
142
+ {
143
+ properties . Add ( col . Name , edgesreader [ col . Name ] ) ;
144
+ }
145
+ Edges . Add ( new Edge ( edgesreader [ "edge_id" ] . ToString ( ) , tablename ,
146
+ edgesreader [ "from_table" ] . ToString ( ) , edgesreader [ "from_id" ] . ToString ( ) ,
147
+ edgesreader [ "to_table" ] . ToString ( ) , edgesreader [ "to_id" ] . ToString ( ) , properties ) ) ;
148
+ }
149
+ edgesreader . Close ( ) ;
150
+ }
151
+
152
+ }
153
+ }
154
+ }
155
+
156
+ }
157
+ public void Dispose ( )
158
+ {
159
+ Nodes = null ;
160
+ Edges = null ;
161
+ ConnectionString = null ;
162
+ }
163
+ }
164
+ }
0 commit comments