Skip to content

Commit 7c828fa

Browse files
Hadi FadlallahHadi Fadlallah
Hadi Fadlallah
authored and
Hadi Fadlallah
committed
Add project files.
1 parent 6493797 commit 7c828fa

File tree

9 files changed

+380
-0
lines changed

9 files changed

+380
-0
lines changed

SQLToArangoDB.sln

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30204.135
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLToArangoDB", "SQLToArangoDB\SQLToArangoDB.csproj", "{AE4D000C-D345-4F0D-89DF-2E409F0DBAA5}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLToArangoDBDemo", "SQLToArangoDBDemo\SQLToArangoDBDemo.csproj", "{033622B4-3DD0-4610-87F7-D6BF5882CA95}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{AE4D000C-D345-4F0D-89DF-2E409F0DBAA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{AE4D000C-D345-4F0D-89DF-2E409F0DBAA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{AE4D000C-D345-4F0D-89DF-2E409F0DBAA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{AE4D000C-D345-4F0D-89DF-2E409F0DBAA5}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{033622B4-3DD0-4610-87F7-D6BF5882CA95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{033622B4-3DD0-4610-87F7-D6BF5882CA95}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{033622B4-3DD0-4610-87F7-D6BF5882CA95}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{033622B4-3DD0-4610-87F7-D6BF5882CA95}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {52DBE607-FEEC-404D-9DD5-34E35A66E332}
30+
EndGlobalSection
31+
EndGlobal

SQLToArangoDB/ArrangoDbWriter.cs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using ArangoDB.Client;
5+
using System.Net;
6+
using Newtonsoft.Json;
7+
8+
namespace SqlToArangoDB
9+
{
10+
public class ArrangoDbWriter : IDisposable
11+
{
12+
public string url { get; set; }
13+
public string Database { get; set; }
14+
public string UserName { get; set; }
15+
public string Password { get; set; }
16+
public IArangoDatabase db { get; set; }
17+
public ArrangoDbWriter(string url, string Database, string UserName, string Password)
18+
{
19+
ArangoDatabase.ChangeSetting(s =>
20+
{
21+
s.Database = Database;
22+
s.Url = url;
23+
s.Credential = new NetworkCredential(UserName, Password);
24+
s.SystemDatabaseCredential = new NetworkCredential(UserName, Password);
25+
});
26+
27+
db = ArangoDatabase.CreateWithSetting();
28+
}
29+
30+
31+
public void ImportNodes(List<Node> nodes)
32+
{
33+
foreach (Node node in nodes)
34+
{
35+
36+
if(db.ListCollections().Where(x => x.Name == node.Label).Count() == 0)
37+
db.CreateCollection(node.Label, type: CollectionType.Document);
38+
39+
40+
var obj = node.Properties;
41+
obj.Add("_key", node.ID);
42+
string json = JsonConvert.SerializeObject(obj);
43+
db.Collection(node.Label).Insert(obj);
44+
}
45+
}
46+
47+
public void ImportEdges(List<Edge> edges)
48+
{
49+
foreach (Edge edge in edges)
50+
{
51+
if (db.ListCollections().Where(x => x.Name == edge.Label).Count() == 0)
52+
db.CreateCollection(edge.Label, type: CollectionType.Edge);
53+
54+
var obj = edge.Properties;
55+
obj.Add("_key", edge.ID);
56+
obj.Add("_from",edge.FromNode + @"/" + edge.FromNodeID);
57+
obj.Add("_to",edge.ToNode + @"/" + edge.ToNodeID);
58+
string json = JsonConvert.SerializeObject(obj);
59+
db.Collection(edge.Label).Insert(obj);
60+
61+
}
62+
}
63+
64+
public void Dispose()
65+
{
66+
url = null;
67+
Database = null;
68+
UserName = null;
69+
Password = null;
70+
}
71+
72+
}
73+
}

SQLToArangoDB/Column.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace SqlToArangoDB
2+
{
3+
public class Column
4+
{
5+
public string Name { get; set; }
6+
public string DataType { get; set; }
7+
public Column(string name, string type)
8+
{
9+
Name = name;
10+
DataType = type;
11+
}
12+
}
13+
}

SQLToArangoDB/Edge.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
3+
namespace SqlToArangoDB
4+
{
5+
public class Edge
6+
{
7+
public string ID { get; set; }
8+
public string Label { get; set; }
9+
public Dictionary<string, object> Properties { get; set; }
10+
public string FromNode { get; set; }
11+
public string FromNodeID { get; set; }
12+
public string ToNode { get; set; }
13+
public string ToNodeID { get; set; }
14+
public Edge(string id, string label, string from, string fromID, string to, string toID, Dictionary<string, object> properties = null)
15+
{
16+
ID = id;
17+
Label = label;
18+
FromNode = from;
19+
FromNodeID = fromID;
20+
ToNodeID = toID;
21+
ToNode = to;
22+
if (properties != null)
23+
Properties = properties;
24+
else
25+
Properties = new Dictionary<string, object>();
26+
27+
}
28+
29+
}
30+
}

SQLToArangoDB/Node.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Generic;
2+
3+
namespace SqlToArangoDB
4+
{
5+
public class Node
6+
{
7+
public string ID { get; set; }
8+
public string Label { get; set; }
9+
public Dictionary<string, object> Properties { get; set; }
10+
public Node(string id, string label, Dictionary<string, object> properties = null)
11+
{
12+
ID = id;
13+
Label = label;
14+
15+
if (properties != null)
16+
Properties = properties;
17+
else
18+
Properties = new Dictionary<string, object>();
19+
}
20+
}
21+
}

SQLToArangoDB/SQLReader.cs

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
}

SQLToArangoDB/SQLToArangoDB.csproj

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="ArangoDB.Client" Version="0.7.70" />
9+
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
10+
</ItemGroup>
11+
12+
</Project>

SQLToArangoDBDemo/Program.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using SqlToArangoDB;
3+
namespace SQLToArangoDBDemo
4+
{
5+
public class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
using (SQLReader reader = new SQLReader("Server=DESKTOP-KCL006K\\DATASERVER;Database=GraphPL;Trusted_Connection=yes;"))
10+
{
11+
reader.GetNodes();
12+
reader.GetEdges();
13+
14+
using (ArrangoDbWriter writer = new ArrangoDbWriter("http://localhost:8529", "TestGraph", "root", "123"))
15+
{
16+
writer.ImportNodes(reader.Nodes);
17+
writer.ImportEdges(reader.Edges);
18+
}
19+
20+
}
21+
22+
}
23+
}
24+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\SQLToArangoDB\SQLToArangoDB.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)