-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOracleTests.cs
160 lines (145 loc) · 6.26 KB
/
OracleTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.ManagedDataAccess.Client;
using Utils;
using OracleTest;
namespace Utils
{
class OracleTests
{
public static string ConnectionTest(string username, string passwd, bool connectAsDba)
{
OracleConnection conn = OracleHelpers.GetOracleConnection(username, passwd, connectAsDba);
conn.Open();
string testResult = "Connection state: " + conn.State;
conn.Close();
return testResult;
}
public static List<WktWithName> WktPullTest(string username, string passwd, string tableName, int rowCount)
{
// conn to use
OracleConnection conn = OracleHelpers.GetOracleConnection(username, passwd, false);
// List to return
List<WktWithName> contents = new List<WktWithName>();
string testQueryString = ("select SDO_GEOMETRY.get_wkt(geom) from nemo." + tableName + " where rownum < " + (rowCount + 1).ToString()).ToString();
// has geom?
bool hasGeom = OracleHelpers.IsColumnNameExistInTableName(conn, tableName, "GEOM".ToString());
if (!hasGeom)
{
return contents;
}
// has name?
bool hasName = OracleHelpers.IsColumnNameExistInTableName(conn, tableName, "NAME".ToString());
if (hasName)
{
// also grab the name.
testQueryString = ("select SDO_GEOMETRY.get_wkt(geom), NAME from nemo." + tableName + " where rownum < " + (rowCount + 1).ToString()).ToString();
}
Logging.Info("WktPullTest", "Constructed query: " + testQueryString);
// constructs command from string
OracleCommand command = new OracleCommand(testQueryString, conn);
// open db connection
conn.Open();
// then, executes the data reader
OracleDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
// Console.WriteLine(reader.GetString(0));
string wkt = reader.GetString(0);
string name = null;
if (hasName)
{
// some data still has null string even there is name column
// also, this proves to be useful when we didn't select name from the table.
try
{
name = reader.GetString(1);
}
catch (System.InvalidCastException e)
{
Logging.Warning("WktPullTest", "Experienced an InvalidCastException");
if (e.ToString().Split(new[] { '\r', '\n' }).FirstOrDefault() == "System.InvalidCastException: Column contains NULL data")
{
Logging.Warning("WktPullTest", "Experienced a null string");
}
name = null;
}
}
WktWithName temp = new WktWithName();
temp.wkt = wkt;
temp.name = name;
contents.Add(temp);
}
}
finally
{
// always call Close when done reading.
reader.Close();
}
conn.Close();
return contents;
}
public static List<WktWithName> NameSearchTest(string username, string passwd, string tableName, string nameOfPlace)
{
// conn to use
OracleConnection conn = OracleHelpers.GetOracleConnection(username, passwd, false);
// List to return
List<WktWithName> contents = new List<WktWithName>();
// has geom?
bool hasGeom = OracleHelpers.IsColumnNameExistInTableName(conn, tableName, "GEOM".ToString());
bool hasName = OracleHelpers.IsColumnNameExistInTableName(conn, tableName, "NAME".ToString());
if (!hasGeom||!hasName)
{
return contents;
}
string testQueryString = ("select SDO_GEOMETRY.get_wkt(geom) from nemo." + tableName + " where NAME like '%" + nameOfPlace + "%'").ToString();
Logging.Info("NameSearchTest", "Constructed query: " + testQueryString);
// constructs command from string
OracleCommand command = new OracleCommand(testQueryString, conn);
// open db connection
conn.Open();
// then, executes the data reader
OracleDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
// Console.WriteLine(reader.GetString(0));
string wkt = reader.GetString(0);
string name = null;
// some data still has null string even there is name column
// also, this proves to be useful when we didn't select name from the table.
try
{
name = reader.GetString(1);
}
catch (System.InvalidCastException e)
{
Logging.Warning("WktPullTest", "Experienced an InvalidCastException");
if (e.ToString().Split(new[] { '\r', '\n' }).FirstOrDefault() == "System.InvalidCastException: Column contains NULL data")
{
Logging.Warning("WktPullTest", "Experienced a null string");
}
name = null;
}
WktWithName temp = new WktWithName();
temp.wkt = wkt;
temp.name = name;
contents.Add(temp);
}
}
finally
{
// always call Close when done reading.
reader.Close();
}
conn.Close();
return contents;
}
}
}