Skip to content

Commit 403a27d

Browse files
committed
优化 sql执行判断
1 parent 06283ee commit 403a27d

File tree

3 files changed

+102
-73
lines changed

3 files changed

+102
-73
lines changed

src/IoTSharp.Data.Taos/Driver/TDengineDriver.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,24 @@ public static List<TDengineMeta> FetchFields(IntPtr res)
402402
[DllImport("taos", EntryPoint = "taos_stmt_add_batch", CallingConvention = CallingConvention.Cdecl)]
403403
static extern public int StmtAddBatch(IntPtr stmt);
404404

405+
[DllImport("taos", EntryPoint = "taos_stmt_is_insert", CallingConvention = CallingConvention.Cdecl)]
406+
static extern private int StmtIsInsert(IntPtr stmt, IntPtr insert);
407+
408+
public static bool StmtIsInsert(IntPtr stmt)
409+
{
410+
bool result = false;
411+
IntPtr ptr = Marshal.AllocHGlobal(sizeof(int));
412+
int code = StmtIsInsert(stmt, ptr);
413+
if (code == 0)
414+
{
415+
result = Marshal.ReadInt32(ptr) == 1;
416+
}
417+
Marshal.FreeHGlobal(ptr);
418+
return result;
419+
}
420+
421+
[DllImport("taos", EntryPoint = "taos_stmt_affected_rows", CallingConvention = CallingConvention.Cdecl)]
422+
static extern public int StmtAffected_rows(IntPtr stmt);
405423
/// <summary>
406424
/// actually execute the INSERT/SELECT sql statement.
407425
/// User application can continue to bind new data after calling to this API.

src/IoTSharp.Data.Taos/TaosCommand.cs

Lines changed: 78 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -326,91 +326,33 @@ internal long GetDateTimeFrom(DateTime dt,IntPtr _taos)
326326
#if DEBUG
327327
Debug.WriteLine($"_commandText:{_commandText}");
328328
#endif
329-
329+
int _affectRows = 0;
330330
Task<IntPtr> code = null;
331331
bool isok = false;
332332
if (_parameters.IsValueCreated)
333333
{
334334
var stmt = TDengine.StmtInit(_taos);
335335
if (stmt != IntPtr.Zero)
336336
{
337-
338337
var pms = _parameters.Value;
339-
List<TAOS_BIND> binds = new List<TAOS_BIND>();
340-
for (int i = 0; i < pms.Count; i++)
341-
{
342-
343-
var tp = pms[i];
344-
_commandText = _commandText.Replace(tp.ParameterName, "?");
345-
switch (TypeInfo.GetTypeCode(tp.Value?.GetType()))
346-
{
347-
case TypeCode.Boolean:
348-
binds.Add(TaosBind.BindBool((tp.Value as bool?).GetValueOrDefault()));
349-
break;
350-
case TypeCode.Char:
351-
binds.Add(TaosBind.BindNchar(tp.Value as string));
352-
break;
353-
case TypeCode.Byte:
354-
case TypeCode.SByte:
355-
binds.Add(TaosBind.BindUTinyInt((tp.Value as byte?).GetValueOrDefault()));
356-
break;
357-
case TypeCode.DateTime:
358-
var t0 = tp.Value as DateTime?;
359-
if (!t0.HasValue)
360-
{
361-
throw new ArgumentException($"InvalidArgumentOfDateTime{tp.Value}");
362-
}
363-
binds.Add(TaosBind.BindTimestamp(GetDateTimeFrom(t0.GetValueOrDefault(), _taos)));
364-
break;
365-
case TypeCode.DBNull:
366-
binds.Add(TaosBind.BindNil());
367-
break;
368-
case TypeCode.Single:
369-
binds.Add(TaosBind.BindFloat((tp.Value as float?).GetValueOrDefault()));
370-
break;
371-
case TypeCode.Decimal:
372-
case TypeCode.Double:
373-
binds.Add(TaosBind.BindDouble((tp.Value as double?).GetValueOrDefault()));
374-
break;
375-
case TypeCode.Int16:
376-
binds.Add(TaosBind.BindSmallInt((tp.Value as short?).GetValueOrDefault()));
377-
break;
378-
case TypeCode.Int32:
379-
binds.Add(TaosBind.BindInt((tp.Value as int?).GetValueOrDefault()));
380-
break;
381-
case TypeCode.Int64:
382-
binds.Add(TaosBind.BindBigInt((tp.Value as long?).GetValueOrDefault()));
383-
break;
384-
case TypeCode.UInt16:
385-
binds.Add(TaosBind.BindSmallInt((tp.Value as short?).GetValueOrDefault()));
386-
break;
387-
case TypeCode.UInt32:
388-
binds.Add(TaosBind.BindUInt((tp.Value as uint?).GetValueOrDefault()));
389-
break;
390-
case TypeCode.UInt64:
391-
binds.Add(TaosBind.BindUBigInt((tp.Value as ulong?).GetValueOrDefault()));
392-
break;
393-
case TypeCode.String:
394-
default:
395-
binds.Add(TaosBind.BindBinary(tp.Value as string));
396-
break;
397-
}
398-
}
399-
338+
List<TAOS_BIND> binds = BindParamters(pms);
400339
int res = TDengine.StmtPrepare(stmt, _commandText);
401340
if (res == 0)
402341
{
403342
int ret = TDengine.StmtBindParam(stmt, binds.ToArray());
404343
if (ret == 0)
405344
{
406-
int addbech= TDengine.StmtAddBatch(stmt);
407-
345+
if (TDengine.StmtIsInsert(stmt))
346+
{
347+
int addbech = TDengine.StmtAddBatch(stmt);
348+
}
408349
code = Task.Run(() =>
409350
{
410351
IntPtr ptr = IntPtr.Zero;
411352
int re = TDengine.StmtExecute(stmt);
412353
if (re == 0)
413354
{
355+
_affectRows = TDengine.StmtAffected_rows(stmt);
414356
ptr = TDengine.StmtUseResult(stmt);
415357
}
416358
else
@@ -421,7 +363,7 @@ internal long GetDateTimeFrom(DateTime dt,IntPtr _taos)
421363
TaosException.ThrowExceptionForRC(-10010, $"stmt execute failed,{ TDengine.StmtErrorStr(stmt)}", null);
422364
}
423365
return ptr;
424-
366+
425367
});
426368
isok = code.Wait(TimeSpan.FromSeconds(CommandTimeout));
427369
if (isok == false)
@@ -439,9 +381,9 @@ internal long GetDateTimeFrom(DateTime dt,IntPtr _taos)
439381
TaosBind.FreeTaosBind(binds.ToArray());
440382
TaosException.ThrowExceptionForRC(-10008, $"stmt prepare failed,{ TDengine.StmtErrorStr(stmt)}", null);
441383
}
442-
384+
443385
}
444-
386+
445387
}
446388
else
447389
{
@@ -456,6 +398,7 @@ internal long GetDateTimeFrom(DateTime dt,IntPtr _taos)
456398
{
457399
TDengine.StopQuery(_taos);
458400
}
401+
_affectRows = TDengine.AffectRows(_taos);
459402
}
460403

461404
if (isok && code !=null && TDengine.ErrorNo(code.Result) == 0)
@@ -468,7 +411,7 @@ internal long GetDateTimeFrom(DateTime dt,IntPtr _taos)
468411
Debug.WriteLine("index:" + j + ", type:" + meta.type + ", typename:" + meta.TypeName() + ", name:" + meta.name + ", size:" + meta.size);
469412
#endif
470413
}
471-
dataReader = new TaosDataReader(this, metas, closeConnection, code.Result);
414+
dataReader = new TaosDataReader(this, metas, closeConnection, code.Result, _affectRows, metas.Count);
472415
}
473416
else if (isok && TDengine.ErrorNo(code.Result) != 0)
474417
{
@@ -502,6 +445,72 @@ internal long GetDateTimeFrom(DateTime dt,IntPtr _taos)
502445
return dataReader;
503446
}
504447

448+
private List<TAOS_BIND> BindParamters(TaosParameterCollection pms)
449+
{
450+
List<TAOS_BIND> binds = new List<TAOS_BIND>();
451+
for (int i = 0; i < pms.Count; i++)
452+
{
453+
454+
var tp = pms[i];
455+
_commandText = _commandText.Replace(tp.ParameterName, "?");
456+
switch (TypeInfo.GetTypeCode(tp.Value?.GetType()))
457+
{
458+
case TypeCode.Boolean:
459+
binds.Add(TaosBind.BindBool((tp.Value as bool?).GetValueOrDefault()));
460+
break;
461+
case TypeCode.Char:
462+
binds.Add(TaosBind.BindNchar(tp.Value as string));
463+
break;
464+
case TypeCode.Byte:
465+
case TypeCode.SByte:
466+
binds.Add(TaosBind.BindUTinyInt((tp.Value as byte?).GetValueOrDefault()));
467+
break;
468+
case TypeCode.DateTime:
469+
var t0 = tp.Value as DateTime?;
470+
if (!t0.HasValue)
471+
{
472+
throw new ArgumentException($"InvalidArgumentOfDateTime{tp.Value}");
473+
}
474+
binds.Add(TaosBind.BindTimestamp(GetDateTimeFrom(t0.GetValueOrDefault(), _taos)));
475+
break;
476+
case TypeCode.DBNull:
477+
binds.Add(TaosBind.BindNil());
478+
break;
479+
case TypeCode.Single:
480+
binds.Add(TaosBind.BindFloat((tp.Value as float?).GetValueOrDefault()));
481+
break;
482+
case TypeCode.Decimal:
483+
case TypeCode.Double:
484+
binds.Add(TaosBind.BindDouble((tp.Value as double?).GetValueOrDefault()));
485+
break;
486+
case TypeCode.Int16:
487+
binds.Add(TaosBind.BindSmallInt((tp.Value as short?).GetValueOrDefault()));
488+
break;
489+
case TypeCode.Int32:
490+
binds.Add(TaosBind.BindInt((tp.Value as int?).GetValueOrDefault()));
491+
break;
492+
case TypeCode.Int64:
493+
binds.Add(TaosBind.BindBigInt((tp.Value as long?).GetValueOrDefault()));
494+
break;
495+
case TypeCode.UInt16:
496+
binds.Add(TaosBind.BindSmallInt((tp.Value as short?).GetValueOrDefault()));
497+
break;
498+
case TypeCode.UInt32:
499+
binds.Add(TaosBind.BindUInt((tp.Value as uint?).GetValueOrDefault()));
500+
break;
501+
case TypeCode.UInt64:
502+
binds.Add(TaosBind.BindUBigInt((tp.Value as ulong?).GetValueOrDefault()));
503+
break;
504+
case TypeCode.String:
505+
default:
506+
binds.Add(TaosBind.BindBinary(tp.Value as string));
507+
break;
508+
}
509+
}
510+
511+
return binds;
512+
}
513+
505514
/// <summary>
506515
/// Executes the <see cref="CommandText" /> against the database and returns a data reader.
507516
/// </summary>

src/IoTSharp.Data.Taos/TaosDataReader.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class TaosDataReader : DbDataReader
2121
{
2222
private readonly TaosCommand _command;
2323
private bool _hasRows;
24+
private readonly int _recordsAffected;
2425
private bool _closed;
2526
private bool _closeConnection;
2627
private IntPtr _taosResult;
@@ -31,13 +32,14 @@ public class TaosDataReader : DbDataReader
3132
private double _date_max_1970;
3233
private DateTime _dt1970;
3334

34-
internal TaosDataReader(TaosCommand taosCommand, List<TDengineMeta> metas, bool closeConnection, IntPtr res)
35+
internal TaosDataReader(TaosCommand taosCommand, List<TDengineMeta> metas, bool closeConnection, IntPtr res,int recordsAffected,int fieldcount)
3536
{
3637
_taos = taosCommand.Connection._taos;
3738
_command = taosCommand;
3839
_closeConnection = closeConnection;
39-
_fieldCount = TDengine.FieldCount(res);
40-
_hasRows = TDengine.AffectRows(res) > 0;
40+
_fieldCount = fieldcount;
41+
_hasRows = recordsAffected > 0;
42+
_recordsAffected = recordsAffected;
4143
_closed = _closeConnection;
4244
_taosResult = res;
4345
_metas = metas;
@@ -82,7 +84,7 @@ public override int RecordsAffected
8284
{
8385
get
8486
{
85-
return TDengine.AffectRows(_taosResult);
87+
return _recordsAffected;;
8688
}
8789
}
8890

0 commit comments

Comments
 (0)