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
|
using System;
namespace ByteFX.Data.MySqlClient
{
/// <summary>
/// Summary description for CommandResult.
/// </summary>
internal class CommandResult
{
private Driver driver;
private Packet packet;
private int affectedRows = -1;
private int fieldCount = -1;
private int fieldsRead = 0;
private long fieldLength = 0;
private bool readSchema = false;
private bool readRows = false;
public CommandResult(Packet p, Driver d)
{
driver = d;
packet = p;
fieldCount = (int)p.ReadLenInteger();
if (fieldCount == 0)
affectedRows =(int)p.ReadLenInteger();
}
#region Properties
public bool IsResultSet
{
get { return fieldCount > 0; }
}
public int ColumnCount
{
get { return fieldCount; }
}
public int RowsAffected
{
get { return affectedRows; }
}
#endregion
public MySqlField GetField()
{
MySqlField f = new MySqlField( driver.Encoding );
packet = driver.ReadPacket();
f.TableName = packet.ReadLenString();
f.ColumnName = packet.ReadLenString();
f.ColumnLength = (int)packet.ReadNBytes();
f.Type = (MySqlDbType)packet.ReadNBytes();
packet.ReadByte(); // this is apparently 2 -- not sure what it is for
f.Flags = (ColumnFlags)packet.ReadInteger(2); //(short)(d.ReadByte() & 0xff);
f.NumericScale = packet.ReadByte();
fieldsRead++;
return f;
}
public byte[] GetFieldBuffer()
{
return packet.GetBuffer();
}
public int GetFieldIndex()
{
return (int)packet.Position;
}
public long GetFieldLength()
{
return fieldLength;
}
public bool NextField()
{
if (fieldLength >= 0)
packet.Position += fieldLength;
if (! packet.HasMoreData) return false;
fieldLength = packet.ReadLenInteger();
fieldsRead++;
return true;
}
/// <summary>
/// Checks to see if there are any row packets coming
/// </summary>
/// <returns>True if there are row packets available, false if not</returns>
public bool CheckForRows()
{
// first read off any unread field defs
while (fieldsRead < fieldCount)
GetField();
// read off the end of schema packet
packet = driver.ReadPacket();
if ( ! packet.IsLastPacket())
throw new MySqlException("Expected end of schema packet");
readSchema = true;
packet = driver.PeekPacket();
return ! packet.IsLastPacket();
}
public bool ReadDataRow()
{
packet = driver.ReadPacket();
if (packet.IsLastPacket())
{
readRows = true;
return false;
}
fieldsRead = 0;
fieldLength = 0;
NextField();
return true;
}
public void Clear()
{
Packet p;
if (! readSchema)
{
do
{
p = driver.ReadPacket();
} while (! p.IsLastPacket());
}
if (! readRows)
do
{
p = driver.ReadPacket();
} while (! p.IsLastPacket());
}
}
}
|