Skip to content

Commit 9970666

Browse files
committed
fix bug : run Multi-line script get syntax error
1 parent 9aa7763 commit 9970666

File tree

2 files changed

+159
-14
lines changed

2 files changed

+159
-14
lines changed

dolphindb_csharpapi/DBConnection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ public virtual IEntity run(string script, ProgressListener listener)
304304
@out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
305305
}
306306
}
307+
// "\r\n" replace to "\n" for windows
308+
script = script.Replace(Environment.NewLine, "\n");
307309

308310
string body = "script\n" + script;
309311
ExtendedDataInput @in = null;

dolphindb_csharpapi_test/DBConnection_test.cs

Lines changed: 157 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Threading;
1212
using System.Collections;
13+
using System.Threading.Tasks;
1314

1415
namespace dolphindb_csharpapi_test
1516
{
@@ -18,6 +19,8 @@ public class DBConnection_test
1819
{
1920
private readonly string SERVER = "115.239.209.189";
2021
private readonly int PORT = 18531;
22+
private readonly string USER = "admin";
23+
private readonly string PASSWORD = "123456";
2124

2225
[TestMethod]
2326
public void Test_chinese_Table()
@@ -72,7 +75,6 @@ private void prepareChineseTable(DBConnection conn)
7275
conn.run("share t as sharedTable");
7376
conn.run("sharedTable.append!(table(symbol(take(`GGG`MMS`FABB`APPL, 8000)) as 股票代码, take(today(), 8000) as 股票日期, norm(40, 5, 8000) as 买方报价, norm(45, 5, 8000) as 卖方报价, take(now(), 8000) as 时间戳,'备注' + string(1..8000) as 备注)) ");
7477
}
75-
7678
[TestMethod]
7779
public void Test_Connect()
7880
{
@@ -120,6 +122,7 @@ public void getConn(object db)
120122
Assert.IsFalse(b);
121123
}
122124

125+
[TestMethod]
123126
public void Test_Upload_DataTable()
124127
{
125128
DataTable dt = new DataTable();
@@ -139,24 +142,27 @@ public void Test_Upload_DataTable()
139142
dt.Columns.Add(dc);
140143
dc = new DataColumn("dt_byte", Type.GetType("System.Byte"));
141144
dt.Columns.Add(dc);
142-
DataRow dr = dt.NewRow();
143-
dr["dt_short"] = 1;
144-
dr["dt_int"] = 2147483646;
145-
dr["dt_long"] = 2147483649;
146-
dr["dt_double"] = 3.14159893984;
147-
dr["dt_datetime"] = new DateTime(2018, 03, 30, 14, 59, 02, 111);
148-
dr["dt_bool"] = false;
149-
dr["dt_byte"] = (byte)97;
150-
dr["dt_string"] = "test_string";
151-
dt.Rows.Add(dr);
145+
for(int i = 0; i < 2000000; i++)
146+
{
147+
DataRow dr = dt.NewRow();
148+
dr["dt_short"] = 1;
149+
dr["dt_int"] = 2147483646;
150+
dr["dt_long"] = 2147483649;
151+
dr["dt_double"] = 3.14159893984;
152+
dr["dt_datetime"] = new DateTime(2018, 03, 30, 14, 59, 02, 111);
153+
dr["dt_bool"] = false;
154+
dr["dt_byte"] = (byte)97;
155+
dr["dt_string"] = "test_string";
156+
dt.Rows.Add(dr);
157+
}
152158
DBConnection db = new DBConnection();
153159
db.connect(SERVER, PORT);
154160
BasicTable bt = new BasicTable(dt);
155161
Dictionary<string, IEntity> obj = new Dictionary<string, IEntity>();
156162
obj.Add("up_datatable", (IEntity)bt);
157163
db.upload(obj);
158164
BasicIntVector v = (BasicIntVector)db.run("up_datatable.dt_int");
159-
Assert.AreEqual(2147483646, v.get(0));
165+
Assert.AreEqual(2147483646, ((BasicInt)v.get(0)).getValue());
160166

161167
}
162168

@@ -1383,8 +1389,7 @@ public void Test_Module()
13831389
public void Test_udf()
13841390
{
13851391
DBConnection conn = new DBConnection();
1386-
//conn.connect("115.239.209.223", 8951, "admin", "123456");
1387-
conn.connect("192.168.1.135", 8981, "admin", "123456");
1392+
conn.connect(SERVER, PORT, "admin", "123456");
13881393
conn.run("def Foo5(a,b){f=file('testsharp.csv','w');f.writeLine(string(a));}");
13891394

13901395
var args = new List<IEntity>();
@@ -1424,6 +1429,60 @@ public void Test_null_Table_upload()
14241429

14251430
}
14261431

1432+
1433+
[TestMethod]
1434+
public void Test_BasicTable_upload()
1435+
{
1436+
DBConnection conn = new DBConnection();
1437+
conn.connect(SERVER, PORT, "admin", "123456");
1438+
var cols = new List<IVector>() {};
1439+
var colNames = new List<String>() { "Symbol", "TradingDate", "TradingTime", "RecID", "TradeChannel", "TradePrice", "TradeVolume", "TradeAmount", "UNIX", "Market", "BuyRecID", "SellRecID", "BuySellFlag", "SecurityID" };
1440+
int rowNum = 1;
1441+
cols.Add(new BasicStringVector(rowNum));
1442+
cols.Add(new BasicIntVector(rowNum));
1443+
cols.Add(new BasicDateTimeVector(rowNum));
1444+
cols.Add(new BasicIntVector(rowNum));
1445+
cols.Add(new BasicIntVector(rowNum));
1446+
cols.Add(new BasicDoubleVector(rowNum));
1447+
cols.Add(new BasicDoubleVector(rowNum));
1448+
cols.Add(new BasicDoubleVector(rowNum));
1449+
cols.Add(new BasicDoubleVector(rowNum));
1450+
cols.Add(new BasicStringVector(rowNum));
1451+
cols.Add(new BasicIntVector(rowNum));
1452+
cols.Add(new BasicIntVector(rowNum));
1453+
cols.Add(new BasicStringVector(rowNum));
1454+
cols.Add(new BasicDoubleVector(rowNum));
1455+
int i = 0;
1456+
//for(int i = 0;i< rowNum; i++) {
1457+
cols[0].set(i, new BasicString("995000"));
1458+
cols[1].set(i, new BasicInt(9));
1459+
cols[2].set(i, new BasicDateTime(DateTime.Now));
1460+
cols[3].set(i, new BasicInt(1));
1461+
cols[4].set(i, new BasicInt(1));
1462+
cols[5].set(i, new BasicDouble(995000.21));
1463+
cols[6].set(i, new BasicDouble(995000.21));
1464+
cols[7].set(i, new BasicDouble(995000.21));
1465+
cols[8].set(i, new BasicDouble(995000.21));
1466+
cols[9].set(i, new BasicString("SZ"));
1467+
cols[10].set(i, new BasicInt(995000));
1468+
cols[11].set(i, new BasicInt(77755));
1469+
cols[12].set(i, new BasicString("995000"));
1470+
cols[13].set(i, new BasicDouble(995000.22));
1471+
//}
1472+
1473+
var bt = new BasicTable(colNames ,cols);
1474+
var variable = new Dictionary<string, IEntity>();
1475+
variable.Add("table1", bt);
1476+
try
1477+
{
1478+
conn.upload(variable);
1479+
}
1480+
catch (Exception ex)
1481+
{
1482+
Assert.AreEqual("ex", ex.Message);
1483+
}
1484+
}
1485+
14271486
[TestMethod]
14281487
public void Test_up_down()
14291488
{
@@ -1465,5 +1524,89 @@ public void Test_up_down()
14651524
BasicDateTime rtdm = (BasicDateTime)conn.run("Foo5", args);
14661525
Assert.AreEqual(dt.ToString(), rtdm.getValue().ToString());
14671526
}
1527+
1528+
[TestMethod]
1529+
public void Test_MultiTask_SaveData()
1530+
{
1531+
DBConnection db = new DBConnection();
1532+
db.connect(SERVER, PORT, USER, PASSWORD);
1533+
string script = "login('admin','123456'); t = table(100:0, `symbol`price , [SYMBOL,INT]);db = database('dfs://testMultiTaskSaveData', VALUE, `A`B`C);db.createPartitionedTable(t,'table1',`symbol)";
1534+
db.run(script);
1535+
var colNames = new List<String>() { "symbol", "price" };
1536+
var cols1 = new List<IVector>();
1537+
int size = 100000;
1538+
var bs = new BasicStringVector(size);
1539+
var ints = new BasicIntVector(size);
1540+
for (int i = 0; i < size; i++)
1541+
{
1542+
bs.setString(i, "A");
1543+
ints.setInt(i, i);
1544+
}
1545+
cols1.Add(bs);
1546+
cols1.Add(ints);
1547+
BasicTable bt1 = new BasicTable(colNames, cols1);
1548+
1549+
1550+
var cols2 = new List<IVector>();
1551+
bs = new BasicStringVector(size);
1552+
ints = new BasicIntVector(size);
1553+
for (int i = size; i < size *2; i++)
1554+
{
1555+
int pos = i - size;
1556+
bs.setString(pos, "B");
1557+
ints.setInt(pos, i);
1558+
}
1559+
cols2.Add(bs);
1560+
cols2.Add(ints);
1561+
BasicTable bt2 = new BasicTable(colNames, cols2);
1562+
1563+
var cols3 = new List<IVector>();
1564+
bs = new BasicStringVector(size);
1565+
ints = new BasicIntVector(size);
1566+
for (int i = size *2; i < size *3; i++)
1567+
{
1568+
int pos = i - size *2;
1569+
bs.setString(pos, "C");
1570+
ints.setInt(pos, i);
1571+
}
1572+
cols3.Add(bs);
1573+
cols3.Add(ints);
1574+
BasicTable bt3 = new BasicTable(colNames, cols3);
1575+
1576+
Task t1 = Task.Factory.StartNew(delegate { saveData(bt1, "bt1.csv"); });
1577+
Task t2 = Task.Factory.StartNew(delegate { saveData(bt2, "bt2.csv"); });
1578+
Task t3 = Task.Factory.StartNew(delegate { saveData(bt3, "bt3.csv"); });
1579+
Task.WaitAll(t1, t2, t3);
1580+
System.Threading.Thread.Sleep(2000);
1581+
BasicTable btResult = (BasicTable)db.run("select * from loadTable('dfs://testMultiTaskSaveData','table1')");
1582+
db.run("dropDatabase('dfs://testMultiTaskSaveData')");
1583+
Assert.AreEqual(size * 3, btResult.rows());
1584+
db.close();
1585+
}
1586+
1587+
private void saveData(BasicTable bt,string filename)
1588+
{
1589+
DBConnection db = new DBConnection();
1590+
string InitScript = "def saveData(t){loadTable('dfs://testMultiTaskSaveData','table1').append!(t)}";
1591+
db.connect(SERVER, PORT, USER, PASSWORD, InitScript);
1592+
1593+
List<IEntity> args = new List<IEntity>(1);
1594+
args.Add(bt);
1595+
db.run("submitJob{'saveData','test csharp', saveData}", args);
1596+
1597+
}
1598+
1599+
[TestMethod]
1600+
public void Test_FirstLineIsEmpty()
1601+
{
1602+
DBConnection db = new DBConnection();
1603+
db.connect(SERVER, PORT, USER, PASSWORD);
1604+
string sql = @"a = 1+1
1605+
a";
1606+
1607+
BasicInt re = (BasicInt)db.run(sql);
1608+
Assert.AreEqual(2, re.getValue());
1609+
}
1610+
14681611
}
14691612
}

0 commit comments

Comments
 (0)