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