| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791 |
- //------------------------------------------------------------------------------
- // <copyright file="SqlDataReader.cs" company="Microsoft">
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
- // <owner current="true" primary="true">[....]</owner>
- // <owner current="true" primary="false">[....]</owner>
- //------------------------------------------------------------------------------
- namespace System.Data.SqlClient {
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.ComponentModel;
- using System.Data;
- using System.Data.Sql;
- using System.Data.SqlTypes;
- using System.Data.Common;
- using System.Data.ProviderBase;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Threading;
- using System.Xml;
-
- using Microsoft.SqlServer.Server;
- using System.Threading.Tasks;
- public class SqlDataReader : DbDataReader, IDataReader {
- private enum ALTROWSTATUS {
- Null = 0, // default and after Done
- AltRow, // after calling NextResult and the first AltRow is available for read
- Done, // after consuming the value (GetValue -> GetValueInternal)
- }
- internal class SharedState { // parameters needed to execute cleanup from parser
- internal int _nextColumnHeaderToRead;
- internal int _nextColumnDataToRead;
- internal long _columnDataBytesRemaining;
- internal bool _dataReady; // ready to ProcessRow
- }
- internal SharedState _sharedState = new SharedState();
- private TdsParser _parser; // TODO: Probably don't need this, since it's on the stateObj
- private TdsParserStateObject _stateObj;
- private SqlCommand _command;
- private SqlConnection _connection;
- private int _defaultLCID;
- private bool _haltRead; // bool to denote whether we have read first row for single row behavior
- private bool _metaDataConsumed;
- private bool _browseModeInfoConsumed;
- private bool _isClosed;
- private bool _isInitialized; // Webdata 104560
- private bool _hasRows;
- private ALTROWSTATUS _altRowStatus;
- private int _recordsAffected = -1;
- private long _defaultTimeoutMilliseconds;
- private SqlConnectionString.TypeSystem _typeSystem;
- // SQLStatistics support
- private SqlStatistics _statistics;
- private SqlBuffer[] _data; // row buffer, filled in by ReadColumnData()
- private SqlStreamingXml _streamingXml; // Used by Getchars on an Xml column for sequential access
- // buffers and metadata
- private _SqlMetaDataSet _metaData; // current metaData for the stream, it is lazily loaded
- private _SqlMetaDataSetCollection _altMetaDataSetCollection;
- private FieldNameLookup _fieldNameLookup;
- private CommandBehavior _commandBehavior;
- private static int _objectTypeCount; // Bid counter
- internal readonly int ObjectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);
- // context
- // undone: we may still want to do this...it's nice to pass in an lpvoid (essentially) and just have the reader keep the state
- // private object _context = null; // this is never looked at by the stream object. It is used by upper layers who wish
- // to remain stateless
- // metadata (no explicit table, use 'Table')
- private MultiPartTableName[] _tableNames = null;
- private string _resetOptionsString;
- private int _lastColumnWithDataChunkRead;
- private long _columnDataBytesRead; // last byte read by user
- private long _columnDataCharsRead; // last char read by user
- private char[] _columnDataChars;
- private int _columnDataCharsIndex; // Column index that is currently loaded in _columnDataChars
- private Task _currentTask;
- private Snapshot _snapshot;
- private CancellationTokenSource _cancelAsyncOnCloseTokenSource;
- private CancellationToken _cancelAsyncOnCloseToken;
- // Used for checking if the Type parameter provided to GetValue<T> is an INullable
- internal static readonly Type _typeofINullable = typeof(INullable);
- private static readonly Type _typeofSqlString = typeof(SqlString);
- private SqlSequentialStream _currentStream;
- private SqlSequentialTextReader _currentTextReader;
-
- internal SqlDataReader(SqlCommand command, CommandBehavior behavior)
- {
- SqlConnection.VerifyExecutePermission();
-
- _command = command;
- _commandBehavior = behavior;
- if (_command != null) {
- _defaultTimeoutMilliseconds = (long)command.CommandTimeout * 1000L;
- _connection = command.Connection;
- if (_connection != null) {
- _statistics = _connection.Statistics;
- _typeSystem = _connection.TypeSystem;
- }
- }
- _sharedState._dataReady = false;
- _metaDataConsumed = false;
- _hasRows = false;
- _browseModeInfoConsumed = false;
- _currentStream = null;
- _currentTextReader = null;
- _cancelAsyncOnCloseTokenSource = new CancellationTokenSource();
- _cancelAsyncOnCloseToken = _cancelAsyncOnCloseTokenSource.Token;
- _columnDataCharsIndex = -1;
- }
-
- internal bool BrowseModeInfoConsumed {
- set {
- _browseModeInfoConsumed = value;
- }
- }
- internal SqlCommand Command {
- get {
- return _command;
- }
- }
- protected SqlConnection Connection {
- get {
- return _connection;
- }
- }
- override public int Depth {
- get {
- if (this.IsClosed) {
- throw ADP.DataReaderClosed("Depth");
- }
- return 0;
- }
- }
- // fields/attributes collection
- override public int FieldCount {
- get {
- if (this.IsClosed) {
- throw ADP.DataReaderClosed("FieldCount");
- }
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- if (MetaData == null) {
- return 0;
- }
- return _metaData.Length;
- }
- }
- override public bool HasRows {
- get {
- if (this.IsClosed) {
- throw ADP.DataReaderClosed("HasRows");
- }
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- return _hasRows;
- }
- }
- override public bool IsClosed {
- get {
- return _isClosed;
- }
- }
- internal bool IsInitialized {
- get {
- return _isInitialized;
- }
- set {
- Debug.Assert(value, "attempting to uninitialize a data reader?");
- _isInitialized = value;
- }
- }
- // NOTE: For PLP values this indicates the amount of data left in the current chunk (or 0 if there are no more chunks left)
- internal long ColumnDataBytesRemaining() {
- // If there are an unknown (-1) number of bytes left for a PLP, read its size
- if (-1 == _sharedState._columnDataBytesRemaining) {
- _sharedState._columnDataBytesRemaining = (long)_parser.PlpBytesLeft(_stateObj);
- }
- return _sharedState._columnDataBytesRemaining;
- }
- internal _SqlMetaDataSet MetaData {
- get {
- if (IsClosed) {
- throw ADP.DataReaderClosed("MetaData");
- }
- // metaData comes in pieces: colmetadata, tabname, colinfo, etc
- // if we have any metaData, return it. If we have none,
- // then fetch it
- if (_metaData == null && !_metaDataConsumed) {
- if (_currentTask != null) {
- throw SQL.PendingBeginXXXExists();
- }
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #else
- {
- #endif //DEBUG
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- if (!TryConsumeMetaData())
- {
- throw SQL.SynchronousCallMayNotPend();
- }
- }
- #if DEBUG
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- }
- return _metaData;
- }
- }
- internal virtual SmiExtendedMetaData[] GetInternalSmiMetaData() {
- SmiExtendedMetaData[] metaDataReturn = null;
- _SqlMetaDataSet metaData = this.MetaData;
- if ( null != metaData && 0 < metaData.Length ) {
- metaDataReturn = new SmiExtendedMetaData[metaData.visibleColumns];
- for( int index=0; index < metaData.Length; index++ ) {
- _SqlMetaData colMetaData = metaData[index];
- if ( !colMetaData.isHidden ) {
- SqlCollation collation = colMetaData.collation;
- string typeSpecificNamePart1 = null;
- string typeSpecificNamePart2 = null;
- string typeSpecificNamePart3 = null;
- if (SqlDbType.Xml == colMetaData.type) {
- typeSpecificNamePart1 = colMetaData.xmlSchemaCollectionDatabase;
- typeSpecificNamePart2 = colMetaData.xmlSchemaCollectionOwningSchema;
- typeSpecificNamePart3 = colMetaData.xmlSchemaCollectionName;
- }
- else if (SqlDbType.Udt == colMetaData.type) {
- Connection.CheckGetExtendedUDTInfo(colMetaData, true); // SQLBUDT #370593 ensure that colMetaData.udtType is set
- typeSpecificNamePart1 = colMetaData.udtDatabaseName;
- typeSpecificNamePart2 = colMetaData.udtSchemaName;
- typeSpecificNamePart3 = colMetaData.udtTypeName;
- }
- int length = colMetaData.length;
- if ( length > TdsEnums.MAXSIZE ) {
- length = (int) SmiMetaData.UnlimitedMaxLengthIndicator;
- }
- else if (SqlDbType.NChar == colMetaData.type
- ||SqlDbType.NVarChar == colMetaData.type) {
- length /= ADP.CharSize;
- }
- metaDataReturn[index] = new SmiQueryMetaData(
- colMetaData.type,
- length,
- colMetaData.precision,
- colMetaData.scale,
- (null != collation) ? collation.LCID : _defaultLCID,
- (null != collation) ? collation.SqlCompareOptions : SqlCompareOptions.None,
- colMetaData.udtType,
- false, // isMultiValued
- null, // fieldmetadata
- null, // extended properties
- colMetaData.column,
- typeSpecificNamePart1,
- typeSpecificNamePart2,
- typeSpecificNamePart3,
- colMetaData.isNullable,
- colMetaData.serverName,
- colMetaData.catalogName,
- colMetaData.schemaName,
- colMetaData.tableName,
- colMetaData.baseColumn,
- colMetaData.isKey,
- colMetaData.isIdentity,
- 0==colMetaData.updatability,
- colMetaData.isExpression,
- colMetaData.isDifferentName,
- colMetaData.isHidden
- );
- }
- }
- }
- return metaDataReturn;
- }
- override public int RecordsAffected {
- get {
- if (null != _command)
- return _command.InternalRecordsAffected;
- // cached locally for after Close() when command is nulled out
- return _recordsAffected;
- }
- }
- internal string ResetOptionsString {
- set {
- _resetOptionsString = value;
- }
- }
- private SqlStatistics Statistics {
- get {
- return _statistics;
- }
- }
- internal MultiPartTableName[] TableNames {
- get {
- return _tableNames;
- }
- set {
- _tableNames = value;
- }
- }
- override public int VisibleFieldCount {
- get {
- if (this.IsClosed) {
- throw ADP.DataReaderClosed("VisibleFieldCount");
- }
- _SqlMetaDataSet md = this.MetaData;
- if (md == null) {
- return 0;
- }
- return (md.visibleColumns);
- }
- }
- // this operator
- override public object this[int i] {
- get {
- return GetValue(i);
- }
- }
- override public object this[string name] {
- get {
- return GetValue(GetOrdinal(name));
- }
- }
- internal void Bind(TdsParserStateObject stateObj) {
- Debug.Assert(null != stateObj, "null stateobject");
- Debug.Assert(null == _snapshot, "Should not change during execution of asynchronous command");
- stateObj.Owner = this;
- _stateObj = stateObj;
- _parser = stateObj.Parser;
- _defaultLCID = _parser.DefaultLCID;
- }
- // Fills in a schema table with meta data information. This function should only really be called by
- //
- internal DataTable BuildSchemaTable() {
- _SqlMetaDataSet md = this.MetaData;
- Debug.Assert(null != md, "BuildSchemaTable - unexpected null metadata information");
- DataTable schemaTable = new DataTable("SchemaTable");
- schemaTable.Locale = CultureInfo.InvariantCulture;
- schemaTable.MinimumCapacity = md.Length;
- DataColumn ColumnName = new DataColumn(SchemaTableColumn.ColumnName, typeof(System.String));
- DataColumn Ordinal = new DataColumn(SchemaTableColumn.ColumnOrdinal, typeof(System.Int32));
- DataColumn Size = new DataColumn(SchemaTableColumn.ColumnSize, typeof(System.Int32));
- DataColumn Precision = new DataColumn(SchemaTableColumn.NumericPrecision, typeof(System.Int16));
- DataColumn Scale = new DataColumn(SchemaTableColumn.NumericScale, typeof(System.Int16));
- DataColumn DataType = new DataColumn(SchemaTableColumn.DataType, typeof(System.Type));
- DataColumn ProviderSpecificDataType = new DataColumn(SchemaTableOptionalColumn.ProviderSpecificDataType, typeof(System.Type));
- DataColumn NonVersionedProviderType = new DataColumn(SchemaTableColumn.NonVersionedProviderType, typeof(System.Int32));
- DataColumn ProviderType = new DataColumn(SchemaTableColumn.ProviderType, typeof(System.Int32));
- DataColumn IsLong = new DataColumn(SchemaTableColumn.IsLong, typeof(System.Boolean));
- DataColumn AllowDBNull = new DataColumn(SchemaTableColumn.AllowDBNull, typeof(System.Boolean));
- DataColumn IsReadOnly = new DataColumn(SchemaTableOptionalColumn.IsReadOnly, typeof(System.Boolean));
- DataColumn IsRowVersion = new DataColumn(SchemaTableOptionalColumn.IsRowVersion, typeof(System.Boolean));
- DataColumn IsUnique = new DataColumn(SchemaTableColumn.IsUnique, typeof(System.Boolean));
- DataColumn IsKey = new DataColumn(SchemaTableColumn.IsKey, typeof(System.Boolean));
- DataColumn IsAutoIncrement = new DataColumn(SchemaTableOptionalColumn.IsAutoIncrement, typeof(System.Boolean));
- DataColumn IsHidden = new DataColumn(SchemaTableOptionalColumn.IsHidden, typeof(System.Boolean));
- DataColumn BaseCatalogName = new DataColumn(SchemaTableOptionalColumn.BaseCatalogName, typeof(System.String));
- DataColumn BaseSchemaName = new DataColumn(SchemaTableColumn.BaseSchemaName, typeof(System.String));
- DataColumn BaseTableName = new DataColumn(SchemaTableColumn.BaseTableName, typeof(System.String));
- DataColumn BaseColumnName = new DataColumn(SchemaTableColumn.BaseColumnName, typeof(System.String));
- // unique to SqlClient
- DataColumn BaseServerName = new DataColumn(SchemaTableOptionalColumn.BaseServerName, typeof(System.String));
- DataColumn IsAliased = new DataColumn(SchemaTableColumn.IsAliased, typeof(System.Boolean));
- DataColumn IsExpression = new DataColumn(SchemaTableColumn.IsExpression, typeof(System.Boolean));
- DataColumn IsIdentity = new DataColumn("IsIdentity", typeof(System.Boolean));
- DataColumn DataTypeName = new DataColumn("DataTypeName", typeof(System.String));
- DataColumn UdtAssemblyQualifiedName = new DataColumn("UdtAssemblyQualifiedName", typeof(System.String));
- // Xml metadata specific
- DataColumn XmlSchemaCollectionDatabase = new DataColumn("XmlSchemaCollectionDatabase", typeof(System.String));
- DataColumn XmlSchemaCollectionOwningSchema = new DataColumn("XmlSchemaCollectionOwningSchema", typeof(System.String));
- DataColumn XmlSchemaCollectionName = new DataColumn("XmlSchemaCollectionName", typeof(System.String));
- // SparseColumnSet
- DataColumn IsColumnSet = new DataColumn("IsColumnSet", typeof(System.Boolean));
- Ordinal.DefaultValue = 0;
- IsLong.DefaultValue = false;
- DataColumnCollection columns = schemaTable.Columns;
- // must maintain order for backward compatibility
- columns.Add(ColumnName);
- columns.Add(Ordinal);
- columns.Add(Size);
- columns.Add(Precision);
- columns.Add(Scale);
- columns.Add(IsUnique);
- columns.Add(IsKey);
- columns.Add(BaseServerName);
- columns.Add(BaseCatalogName);
- columns.Add(BaseColumnName);
- columns.Add(BaseSchemaName);
- columns.Add(BaseTableName);
- columns.Add(DataType);
- columns.Add(AllowDBNull);
- columns.Add(ProviderType);
- columns.Add(IsAliased);
- columns.Add(IsExpression);
- columns.Add(IsIdentity);
- columns.Add(IsAutoIncrement);
- columns.Add(IsRowVersion);
- columns.Add(IsHidden);
- columns.Add(IsLong);
- columns.Add(IsReadOnly);
- columns.Add(ProviderSpecificDataType);
- columns.Add(DataTypeName);
- columns.Add(XmlSchemaCollectionDatabase);
- columns.Add(XmlSchemaCollectionOwningSchema);
- columns.Add(XmlSchemaCollectionName);
- columns.Add(UdtAssemblyQualifiedName);
- columns.Add(NonVersionedProviderType);
- columns.Add(IsColumnSet);
- for (int i = 0; i < md.Length; i++) {
- _SqlMetaData col = md[i];
- DataRow schemaRow = schemaTable.NewRow();
- schemaRow[ColumnName] = col.column;
- schemaRow[Ordinal] = col.ordinal;
- //
- // be sure to return character count for string types, byte count otherwise
- // col.length is always byte count so for unicode types, half the length
- //
- // For MAX and XML datatypes, we get 0x7fffffff from the server. Do not divide this.
- if (col.cipherMD != null) {
- Debug.Assert(col.baseTI != null && col.baseTI.metaType != null, "col.baseTI and col.baseTI.metaType should not be null.");
- schemaRow[Size] = (col.baseTI.metaType.IsSizeInCharacters && (col.baseTI.length != 0x7fffffff)) ? (col.baseTI.length / 2) : col.baseTI.length;
- }
- else {
- schemaRow[Size] = (col.metaType.IsSizeInCharacters && (col.length != 0x7fffffff)) ? (col.length / 2) : col.length;
- }
- schemaRow[DataType] = GetFieldTypeInternal(col);
- schemaRow[ProviderSpecificDataType] = GetProviderSpecificFieldTypeInternal(col);
- schemaRow[NonVersionedProviderType] = (int) (col.cipherMD != null ? col.baseTI.type : col.type); // SqlDbType enum value - does not change with TypeSystem.
- schemaRow[DataTypeName] = GetDataTypeNameInternal(col);
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && col.IsNewKatmaiDateTimeType) {
- schemaRow[ProviderType] = SqlDbType.NVarChar;
- switch (col.type) {
- case SqlDbType.Date:
- schemaRow[Size] = TdsEnums.WHIDBEY_DATE_LENGTH;
- break;
- case SqlDbType.Time:
- Debug.Assert(TdsEnums.UNKNOWN_PRECISION_SCALE == col.scale || (0 <= col.scale && col.scale <= 7), "Invalid scale for Time column: " + col.scale);
- schemaRow[Size] = TdsEnums.WHIDBEY_TIME_LENGTH[TdsEnums.UNKNOWN_PRECISION_SCALE != col.scale ? col.scale : col.metaType.Scale];
- break;
- case SqlDbType.DateTime2:
- Debug.Assert(TdsEnums.UNKNOWN_PRECISION_SCALE == col.scale || (0 <= col.scale && col.scale <= 7), "Invalid scale for DateTime2 column: " + col.scale);
- schemaRow[Size] = TdsEnums.WHIDBEY_DATETIME2_LENGTH[TdsEnums.UNKNOWN_PRECISION_SCALE != col.scale ? col.scale : col.metaType.Scale];
- break;
- case SqlDbType.DateTimeOffset:
- Debug.Assert(TdsEnums.UNKNOWN_PRECISION_SCALE == col.scale || (0 <= col.scale && col.scale <= 7), "Invalid scale for DateTimeOffset column: " + col.scale);
- schemaRow[Size] = TdsEnums.WHIDBEY_DATETIMEOFFSET_LENGTH[TdsEnums.UNKNOWN_PRECISION_SCALE != col.scale ? col.scale : col.metaType.Scale];
- break;
- }
- }
- else if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && col.IsLargeUdt) {
- if (_typeSystem == SqlConnectionString.TypeSystem.SQLServer2005) {
- schemaRow[ProviderType] = SqlDbType.VarBinary;
- }
- else {
- // TypeSystem.SQLServer2000
- schemaRow[ProviderType] = SqlDbType.Image;
- }
- }
- else if (_typeSystem != SqlConnectionString.TypeSystem.SQLServer2000) {
- // TypeSystem.SQLServer2005 and above
- // SqlDbType enum value - always the actual type for SQLServer2005.
- schemaRow[ProviderType] = (int) (col.cipherMD != null ? col.baseTI.type : col.type);
- if (col.type == SqlDbType.Udt) { // Additional metadata for UDTs.
- Debug.Assert(Connection.IsYukonOrNewer, "Invalid Column type received from the server");
- schemaRow[UdtAssemblyQualifiedName] = col.udtAssemblyQualifiedName;
- }
- else if (col.type == SqlDbType.Xml) { // Additional metadata for Xml.
- Debug.Assert(Connection.IsYukonOrNewer, "Invalid DataType (Xml) for the column");
- schemaRow[XmlSchemaCollectionDatabase] = col.xmlSchemaCollectionDatabase;
- schemaRow[XmlSchemaCollectionOwningSchema] = col.xmlSchemaCollectionOwningSchema;
- schemaRow[XmlSchemaCollectionName] = col.xmlSchemaCollectionName;
- }
- }
- else {
- // TypeSystem.SQLServer2000
-
- // SqlDbType enum value - variable for certain types when SQLServer2000.
- schemaRow[ProviderType] = GetVersionedMetaType(col.metaType).SqlDbType;
- }
-
- if (col.cipherMD != null) {
- Debug.Assert(col.baseTI != null, @"col.baseTI should not be null.");
- if (TdsEnums.UNKNOWN_PRECISION_SCALE != col.baseTI.precision) {
- schemaRow[Precision] = col.baseTI.precision;
- }
- else {
- schemaRow[Precision] = col.baseTI.metaType.Precision;
- }
- }
- else if (TdsEnums.UNKNOWN_PRECISION_SCALE != col.precision) {
- schemaRow[Precision] = col.precision;
- }
- else {
- schemaRow[Precision] = col.metaType.Precision;
- }
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && col.IsNewKatmaiDateTimeType) {
- schemaRow[Scale] = MetaType.MetaNVarChar.Scale;
- }
- else if (col.cipherMD != null) {
- Debug.Assert(col.baseTI != null, @"col.baseTI should not be null.");
- if (TdsEnums.UNKNOWN_PRECISION_SCALE != col.baseTI.scale) {
- schemaRow[Scale] = col.baseTI.scale;
- }
- else {
- schemaRow[Scale] = col.baseTI.metaType.Scale;
- }
- }
- else if (TdsEnums.UNKNOWN_PRECISION_SCALE != col.scale) {
- schemaRow[Scale] = col.scale;
- }
- else {
- schemaRow[Scale] = col.metaType.Scale;
- }
- schemaRow[AllowDBNull] = col.isNullable;
- // If no ColInfo token received, do not set value, leave as null.
- if (_browseModeInfoConsumed) {
- schemaRow[IsAliased] = col.isDifferentName;
- schemaRow[IsKey] = col.isKey;
- schemaRow[IsHidden] = col.isHidden;
- schemaRow[IsExpression] = col.isExpression;
- }
- schemaRow[IsIdentity] = col.isIdentity;
- schemaRow[IsAutoIncrement] = col.isIdentity;
- if (col.cipherMD != null) {
- Debug.Assert(col.baseTI != null, @"col.baseTI should not be null.");
- Debug.Assert(col.baseTI.metaType != null, @"col.baseTI.metaType should not be null.");
- schemaRow[IsLong] = col.baseTI.metaType.IsLong;
- }
- else {
- schemaRow[IsLong] = col.metaType.IsLong;
- }
- // mark unique for timestamp columns
- if (SqlDbType.Timestamp == col.type) {
- schemaRow[IsUnique] = true;
- schemaRow[IsRowVersion] = true;
- }
- else {
- schemaRow[IsUnique] = false;
- schemaRow[IsRowVersion] = false;
- }
- schemaRow[IsReadOnly] = (0 == col.updatability);
- schemaRow[IsColumnSet] = col.isColumnSet;
- if (!ADP.IsEmpty(col.serverName)) {
- schemaRow[BaseServerName] = col.serverName;
- }
- if (!ADP.IsEmpty(col.catalogName)) {
- schemaRow[BaseCatalogName] = col.catalogName;
- }
- if (!ADP.IsEmpty(col.schemaName)) {
- schemaRow[BaseSchemaName] = col.schemaName;
- }
- if (!ADP.IsEmpty(col.tableName)) {
- schemaRow[BaseTableName] = col.tableName;
- }
- if (!ADP.IsEmpty(col.baseColumn)) {
- schemaRow[BaseColumnName] = col.baseColumn;
- }
- else if (!ADP.IsEmpty(col.column)) {
- schemaRow[BaseColumnName] = col.column;
- }
- schemaTable.Rows.Add(schemaRow);
- schemaRow.AcceptChanges();
- }
- // mark all columns as readonly
- foreach(DataColumn column in columns) {
- column.ReadOnly = true; // MDAC 70943
- }
- return schemaTable;
- }
- internal void Cancel(int objectID) {
- TdsParserStateObject stateObj = _stateObj;
- if (null != stateObj) {
- stateObj.Cancel(objectID);
- }
- }
- // wipe any data off the wire from a partial read
- // and reset all pointers for sequential access
- private bool TryCleanPartialRead() {
- AssertReaderState(requireData: true, permitAsync: true);
- // VSTS DEVDIV2 380446: It is possible that read attempt we are cleaning after ended with partially
- // processed header (if it falls between network packets). In this case the first thing to do is to
- // finish reading the header, otherwise code will start treating unread header as TDS payload.
- if (_stateObj._partialHeaderBytesRead > 0) {
- if (!_stateObj.TryProcessHeader()) {
- return false;
- }
- }
- // following cases for sequential read
- // i. user called read but didn't fetch anything
- // iia. user called read and fetched a subset of the columns
- // iib. user called read and fetched a subset of the column data
- // Wipe out any Streams or TextReaders
- if (-1 != _lastColumnWithDataChunkRead) {
- CloseActiveSequentialStreamAndTextReader();
- }
- // i. user called read but didn't fetch anything
- if (0 == _sharedState._nextColumnHeaderToRead) {
- if (!_stateObj.Parser.TrySkipRow(_metaData, _stateObj)) {
- return false;
- }
- }
- else {
- // iia. if we still have bytes left from a partially read column, skip
- if (!TryResetBlobState()) {
- return false;
- }
- // iib.
- // now read the remaining values off the wire for this row
- if (!_stateObj.Parser.TrySkipRow(_metaData, _sharedState._nextColumnHeaderToRead, _stateObj)) {
- return false;
- }
- }
- #if DEBUG
- if (_stateObj._pendingData) {
- byte token;
- if (!_stateObj.TryPeekByte(out token)) {
- return false;
- }
- Debug.Assert(TdsParser.IsValidTdsToken(token), string.Format("Invalid token after performing CleanPartialRead: {0,-2:X2}", token));
- }
- #endif
- _sharedState._dataReady = false;
- return true;
- }
- private void CleanPartialReadReliable() {
- AssertReaderState(requireData: true, permitAsync: false);
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #else
- {
- #endif //DEBUG
- bool result = TryCleanPartialRead();
- Debug.Assert(result, "Should not pend on sync call");
- Debug.Assert(!_sharedState._dataReady, "_dataReady should be cleared");
- }
- #if DEBUG
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- if (_connection != null) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- if (_connection != null) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- if (_connection != null) {
- _connection.Abort(e);
- }
- throw;
- }
- }
- override public void Close() {
- SqlStatistics statistics = null;
- IntPtr hscp;
- Bid.ScopeEnter(out hscp, "<sc.SqlDataReader.Close|API> %d#", ObjectID);
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- TdsParserStateObject stateObj = _stateObj;
- // Request that the current task is stopped
- _cancelAsyncOnCloseTokenSource.Cancel();
- var currentTask = _currentTask;
- if ((currentTask != null) && (!currentTask.IsCompleted)) {
- try {
- // Wait for the task to complete
- ((IAsyncResult)currentTask).AsyncWaitHandle.WaitOne();
- // Ensure that we've finished reading any pending data
- var networkPacketTaskSource = stateObj._networkPacketTaskSource;
- if (networkPacketTaskSource != null) {
- ((IAsyncResult)networkPacketTaskSource.Task).AsyncWaitHandle.WaitOne();
- }
- }
- catch (Exception) {
- // If we receive any exceptions while waiting, something has gone horribly wrong and we need to doom the connection and fast-fail the reader
- _connection.InnerConnection.DoomThisConnection();
- _isClosed = true;
- if (stateObj != null) {
- lock (stateObj) {
- _stateObj = null;
- _command = null;
- _connection = null;
- }
- }
- throw;
- }
- }
-
- // Close down any active Streams and TextReaders (this will also wait for them to finish their async tasks)
- // NOTE: This must be done outside of the lock on the stateObj otherwise it will deadlock with CleanupAfterAsyncInvocation
- CloseActiveSequentialStreamAndTextReader();
- if (stateObj != null) {
- // protect against concurrent close and cancel
- lock (stateObj) {
-
- if (_stateObj != null ) { // reader not closed while we waited for the lock
- // TryCloseInternal will clear out the snapshot when it is done
- if (_snapshot != null) {
- #if DEBUG
- // The stack trace for replays will differ since they weren't captured during close
- stateObj._permitReplayStackTraceToDiffer = true;
- #endif
- PrepareForAsyncContinuation();
- }
- SetTimeout(_defaultTimeoutMilliseconds);
- // Close can be called from async methods in error cases,
- // in which case we need to switch to syncOverAsync
- stateObj._syncOverAsync = true;
- if (!TryCloseInternal(true /*closeReader*/)) {
- throw SQL.SynchronousCallMayNotPend();
- }
- // DO NOT USE stateObj after this point - it has been returned to the TdsParser's session pool and potentially handed out to another thread
- }
- }
- }
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- Bid.ScopeLeave(ref hscp);
- }
- }
- private bool TryCloseInternal(bool closeReader) {
- TdsParser parser = _parser;
- TdsParserStateObject stateObj = _stateObj;
- bool closeConnection = (IsCommandBehavior(CommandBehavior.CloseConnection));
- bool aborting = false;
- bool cleanDataFailed = false;
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #else
- {
- #endif //DEBUG
- if ((!_isClosed) && (parser != null) && (stateObj != null) && (stateObj._pendingData)) {
- // It is possible for this to be called during connection close on a
- // broken connection, so check state first.
- if (parser.State == TdsParserState.OpenLoggedIn) {
- // if user called read but didn't fetch any values, skip the row
- // same applies after NextResult on ALTROW because NextResult starts rowconsumption in that case ...
- Debug.Assert(SniContext.Snix_Read==stateObj.SniContext, String.Format((IFormatProvider)null, "The SniContext should be Snix_Read but it actually is {0}", stateObj.SniContext));
- if (_altRowStatus == ALTROWSTATUS.AltRow) {
- _sharedState._dataReady = true; // set _sharedState._dataReady to not confuse CleanPartialRead
- }
- _stateObj._internalTimeout = false;
- if (_sharedState._dataReady) {
- cleanDataFailed = true;
- if (TryCleanPartialRead()) {
- cleanDataFailed = false;
- }
- else {
- return false;
- }
- }
- #if DEBUG
- else {
- byte token;
- if (!_stateObj.TryPeekByte(out token)) {
- return false;
- }
- Debug.Assert(TdsParser.IsValidTdsToken(token), string.Format("DataReady is false, but next token is invalid: {0,-2:X2}", token));
- }
- #endif
- bool ignored;
- if (!parser.TryRun(RunBehavior.Clean, _command, this, null, stateObj, out ignored)) {
- return false;
- }
- }
- }
- RestoreServerSettings(parser, stateObj);
- return true;
- }
- #if DEBUG
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- aborting = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- aborting = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- aborting = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- finally {
- if (aborting) {
- _isClosed = true;
- _command = null; // we are done at this point, don't allow navigation to the connection
- _connection = null;
- _statistics = null;
- _stateObj = null;
- _parser = null;
- }
- else if (closeReader) {
- bool wasClosed = _isClosed;
- _isClosed = true;
- _parser = null;
- _stateObj = null;
- _data = null;
- if (_snapshot != null) {
- CleanupAfterAsyncInvocationInternal(stateObj);
- }
- // SQLBUDT #284712 - Note the order here is extremely important:
- //
- // (1) First, we remove the reader from the reference collection
- // to prevent it from being forced closed by the parser if
- // any future work occurs.
- //
- // (2) Next, we ensure that cancellation can no longer happen by
- // calling CloseSession.
- if (Connection != null) {
- Connection.RemoveWeakReference(this); // This doesn't catch everything -- the connection may be closed, but it prevents dead readers from clogging the collection
- }
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #else
- {
- #endif //DEBUG
- // IsClosed may be true if CloseReaderFromConnection was called - in which case, the session has already been closed
- if ((!wasClosed) && (null != stateObj)) {
- if (!cleanDataFailed) {
- stateObj.CloseSession();
- }
- else {
- if (parser != null) {
- parser.State = TdsParserState.Broken; // We failed while draining data, so TDS pointer can be between tokens - cannot recover
- parser.PutSession(stateObj);
- parser.Connection.BreakConnection();
- }
- }
- }
- // DO NOT USE stateObj after this point - it has been returned to the TdsParser's session pool and potentially handed out to another thread
- }
- #if DEBUG
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- // do not retry here
- bool result = TrySetMetaData(null, false);
- Debug.Assert(result, "Should not pend a synchronous request");
- _fieldNameLookup = null;
- // if the user calls ExecuteReader(CommandBehavior.CloseConnection)
- // then we close down the connection when we are done reading results
- if (closeConnection) {
- if (Connection != null) {
- Connection.Close();
- }
- }
- if (_command != null) {
- // cache recordsaffected to be returnable after DataReader.Close();
- _recordsAffected = _command.InternalRecordsAffected;
- }
- _command = null; // we are done at this point, don't allow navigation to the connection
- _connection = null;
- _statistics = null;
- }
- }
- }
- virtual internal void CloseReaderFromConnection() {
- var parser = _parser;
- Debug.Assert(parser == null || parser.State != TdsParserState.OpenNotLoggedIn, "Reader on a connection that is not logged in");
- if ((parser != null) && (parser.State == TdsParserState.OpenLoggedIn)) {
- // Connection is ok - proper cleanup
- // NOTE: This is NOT thread-safe
- Close();
- }
- else {
- // Connection is broken - quick cleanup
- // NOTE: This MUST be thread-safe as a broken connection can happen at any time
- var stateObj = _stateObj;
- _isClosed = true;
- // Request that the current task is stopped
- _cancelAsyncOnCloseTokenSource.Cancel();
- if (stateObj != null) {
- var networkPacketTaskSource = stateObj._networkPacketTaskSource;
- if (networkPacketTaskSource != null) {
- // If the connection is closed or broken, this will never complete
- networkPacketTaskSource.TrySetException(ADP.ClosedConnectionError());
- }
- if (_snapshot != null) {
- // CleanWire will do cleanup - so we don't really care about the snapshot
- CleanupAfterAsyncInvocationInternal(stateObj, resetNetworkPacketTaskSource: false);
- }
- // Switch to [....] to prepare for cleanwire
- stateObj._syncOverAsync = true;
- // Remove owner (this will allow the stateObj to be disposed after the connection is closed)
- stateObj.RemoveOwner();
- }
- }
- }
- private bool TryConsumeMetaData() {
- // warning: Don't check the MetaData property within this function
- // warning: as it will be a reentrant call
- while (_parser != null && _stateObj != null && _stateObj._pendingData && !_metaDataConsumed) {
- if (_parser.State == TdsParserState.Broken || _parser.State == TdsParserState.Closed) {
- // Happened for DEVDIV2:180509 (SqlDataReader.ConsumeMetaData Hangs In 100% CPU Loop Forever When TdsParser._state == TdsParserState.Broken)
- // during request for DTC address.
- // NOTE: We doom connection for TdsParserState.Closed since it indicates that it is in some abnormal and unstable state, probably as a result of
- // closing from another thread. In general, TdsParserState.Closed does not necessitate dooming the connection.
- if (_parser.Connection != null)
- _parser.Connection.DoomThisConnection();
- throw SQL.ConnectionDoomed();
- }
- bool ignored;
- if (!_parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out ignored)) {
- return false;
- }
- Debug.Assert(!ignored, "Parser read a row token while trying to read metadata");
- }
- // we hide hidden columns from the user so build an internal map
- // that compacts all hidden columns from the array
- if (null != _metaData) {
- if (_snapshot != null && object.ReferenceEquals(_snapshot._metadata, _metaData)) {
- _metaData = (_SqlMetaDataSet)_metaData.Clone();
- }
- _metaData.visibleColumns = 0;
- Debug.Assert(null == _metaData.indexMap, "non-null metaData indexmap");
- int[] indexMap = new int[_metaData.Length];
- for (int i = 0; i < indexMap.Length; ++i) {
- indexMap[i] = _metaData.visibleColumns;
- if (!(_metaData[i].isHidden)) {
- _metaData.visibleColumns++;
- }
- }
- _metaData.indexMap = indexMap;
- }
- return true;
- }
- override public string GetDataTypeName(int i) {
- SqlStatistics statistics = null;
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- CheckMetaDataIsReady(columnIndex: i);
- return GetDataTypeNameInternal(_metaData[i]);
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- }
- private string GetDataTypeNameInternal(_SqlMetaData metaData) {
- string dataTypeName = null;
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsNewKatmaiDateTimeType) {
- dataTypeName = MetaType.MetaNVarChar.TypeName;
- }
- else if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsLargeUdt) {
- if (_typeSystem == SqlConnectionString.TypeSystem.SQLServer2005) {
- dataTypeName = MetaType.MetaMaxVarBinary.TypeName;
- }
- else {
- // TypeSystem.SQLServer2000
- dataTypeName = MetaType.MetaImage.TypeName;
- }
- }
- else if (_typeSystem != SqlConnectionString.TypeSystem.SQLServer2000) {
- // TypeSystem.SQLServer2005 and above
- if (metaData.type == SqlDbType.Udt) {
- Debug.Assert(Connection.IsYukonOrNewer, "Invalid Column type received from the server");
- dataTypeName = metaData.udtDatabaseName + "." + metaData.udtSchemaName + "." + metaData.udtTypeName;
- }
- else { // For all other types, including Xml - use data in MetaType.
- if (metaData.cipherMD != null) {
- Debug.Assert(metaData.baseTI != null && metaData.baseTI.metaType != null, "metaData.baseTI and metaData.baseTI.metaType should not be null.");
- dataTypeName = metaData.baseTI.metaType.TypeName;
- }
- else {
- dataTypeName = metaData.metaType.TypeName;
- }
- }
- }
- else {
- // TypeSystem.SQLServer2000
- dataTypeName = GetVersionedMetaType(metaData.metaType).TypeName;
- }
- return dataTypeName;
- }
- virtual internal SqlBuffer.StorageType GetVariantInternalStorageType(int i) {
- Debug.Assert(null != _data, "Attempting to get variant internal storage type");
- Debug.Assert(i < _data.Length, "Reading beyond data length?");
- return _data[i].VariantInternalStorageType;
- }
- override public IEnumerator GetEnumerator() {
- return new DbEnumerator(this, IsCommandBehavior(CommandBehavior.CloseConnection));
- }
-
- override public Type GetFieldType(int i) {
- SqlStatistics statistics = null;
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- CheckMetaDataIsReady(columnIndex: i);
- return GetFieldTypeInternal(_metaData[i]);
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- }
- private Type GetFieldTypeInternal(_SqlMetaData metaData) {
- Type fieldType = null;
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsNewKatmaiDateTimeType) {
- // Return katmai types as string
- fieldType = MetaType.MetaNVarChar.ClassType;
- }
- else if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsLargeUdt) {
- if (_typeSystem == SqlConnectionString.TypeSystem.SQLServer2005) {
- fieldType = MetaType.MetaMaxVarBinary.ClassType;
- }
- else {
- // TypeSystem.SQLServer2000
- fieldType = MetaType.MetaImage.ClassType;
- }
- }
- else if (_typeSystem != SqlConnectionString.TypeSystem.SQLServer2000) {
- // TypeSystem.SQLServer2005 and above
- if (metaData.type == SqlDbType.Udt) {
- Debug.Assert(Connection.IsYukonOrNewer, "Invalid Column type received from the server");
- Connection.CheckGetExtendedUDTInfo(metaData, false);
- fieldType = metaData.udtType;
- }
- else { // For all other types, including Xml - use data in MetaType.
- if (metaData.cipherMD != null) {
- Debug.Assert(metaData.baseTI != null && metaData.baseTI.metaType != null, "metaData.baseTI and metaData.baseTI.metaType should not be null.");
- fieldType = metaData.baseTI.metaType.ClassType;
- }
- else {
- fieldType = metaData.metaType.ClassType; // Com+ type.
- }
- }
- }
- else {
- // TypeSystem.SQLServer2000
-
- fieldType = GetVersionedMetaType(metaData.metaType).ClassType; // Com+ type.
- }
-
- return fieldType;
- }
- virtual internal int GetLocaleId(int i) {
- _SqlMetaData sqlMetaData = MetaData[i];
- int lcid;
- if (sqlMetaData.cipherMD != null) {
- // If this column is encrypted, get the collation from baseTI
- //
- if (sqlMetaData.baseTI.collation != null) {
- lcid = sqlMetaData.baseTI.collation.LCID;
- }
- else {
- lcid = 0;
- }
- }
- else {
- if (sqlMetaData.collation != null) {
- lcid = sqlMetaData.collation.LCID;
- }
- else {
- lcid = 0;
- }
- }
- return lcid;
- }
-
- override public string GetName(int i) {
- CheckMetaDataIsReady(columnIndex: i);
- Debug.Assert(null != _metaData[i].column, "MDAC 66681");
- return _metaData[i].column;
- }
- override public Type GetProviderSpecificFieldType(int i) {
- SqlStatistics statistics = null;
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- CheckMetaDataIsReady(columnIndex: i);
- return GetProviderSpecificFieldTypeInternal(_metaData[i]);
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- }
- private Type GetProviderSpecificFieldTypeInternal(_SqlMetaData metaData) {
- Type providerSpecificFieldType = null;
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsNewKatmaiDateTimeType) {
- providerSpecificFieldType = MetaType.MetaNVarChar.SqlType;
- }
- else if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsLargeUdt) {
- if (_typeSystem == SqlConnectionString.TypeSystem.SQLServer2005) {
- providerSpecificFieldType = MetaType.MetaMaxVarBinary.SqlType;
- }
- else {
- // TypeSystem.SQLServer2000
- providerSpecificFieldType = MetaType.MetaImage.SqlType;
- }
- }
- else if (_typeSystem != SqlConnectionString.TypeSystem.SQLServer2000) {
- // TypeSystem.SQLServer2005 and above
- if (metaData.type == SqlDbType.Udt) {
- Debug.Assert(Connection.IsYukonOrNewer, "Invalid Column type received from the server");
- Connection.CheckGetExtendedUDTInfo(metaData, false);
- providerSpecificFieldType = metaData.udtType;
- }
- else {
- // For all other types, including Xml - use data in MetaType.
- if (metaData.cipherMD != null) {
- Debug.Assert(metaData.baseTI != null && metaData.baseTI.metaType != null,
- "metaData.baseTI and metaData.baseTI.metaType should not be null.");
- providerSpecificFieldType = metaData.baseTI.metaType.SqlType; // SqlType type.
- }
- else {
- providerSpecificFieldType = metaData.metaType.SqlType; // SqlType type.
- }
- }
- }
- else {
- // TypeSystem.SQLServer2000
-
- providerSpecificFieldType = GetVersionedMetaType(metaData.metaType).SqlType; // SqlType type.
- }
- return providerSpecificFieldType;
- }
-
- // named field access
- override public int GetOrdinal(string name) {
- SqlStatistics statistics = null;
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- if (null == _fieldNameLookup) {
- CheckMetaDataIsReady();
- _fieldNameLookup = new FieldNameLookup(this, _defaultLCID);
- }
- return _fieldNameLookup.GetOrdinal(name); // MDAC 71470
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- }
- override public object GetProviderSpecificValue(int i) {
- return GetSqlValue(i);
- }
- override public int GetProviderSpecificValues(object[] values) {
- return GetSqlValues(values);
- }
- override public DataTable GetSchemaTable() {
- SqlStatistics statistics = null;
- IntPtr hscp;
- Bid.ScopeEnter(out hscp, "<sc.SqlDataReader.GetSchemaTable|API> %d#", ObjectID);
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- if (null == _metaData || null == _metaData.schemaTable) {
- if (null != this.MetaData) {
- _metaData.schemaTable = BuildSchemaTable();
- Debug.Assert(null != _metaData.schemaTable, "No schema information yet!");
- // filter table?
- }
- }
- if (null != _metaData) {
- return _metaData.schemaTable;
- }
- return null;
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- Bid.ScopeLeave(ref hscp);
- }
- }
- override public bool GetBoolean(int i) {
- ReadColumn(i);
- return _data[i].Boolean;
- }
- virtual public XmlReader GetXmlReader(int i) {
- // NOTE: sql_variant can not contain a XML data type: http://msdn.microsoft.com/en-us/library/ms173829.aspx
- // If this ever changes, the following code should be changed to be like GetStream\GetTextReader
- CheckDataIsReady(columnIndex: i, methodName: "GetXmlReader");
- MetaType mt = _metaData[i].metaType;
- // XmlReader only allowed on XML types
- if (mt.SqlDbType != SqlDbType.Xml) {
- throw SQL.XmlReaderNotSupportOnColumnType(_metaData[i].column);
- }
- if (IsCommandBehavior(CommandBehavior.SequentialAccess)) {
- // Wrap the sequential stream in an XmlReader
- _currentStream = new SqlSequentialStream(this, i);
- _lastColumnWithDataChunkRead = i;
- return SqlXml.CreateSqlXmlReader(_currentStream, closeInput: true);
- }
- else {
- // Need to call ReadColumn, since we want to access the internal data structures (i.e. SqlBinary) rather than calling anther Get*() method
- ReadColumn(i);
- if (_data[i].IsNull) {
- // A 'null' stream
- return SqlXml.CreateSqlXmlReader(new MemoryStream(new byte[0], writable: false), closeInput: true);
- }
- else {
- // Grab already read data
- return _data[i].SqlXml.CreateReader();
- }
- }
- }
- override public Stream GetStream(int i) {
- CheckDataIsReady(columnIndex: i, methodName: "GetStream");
- // Streaming is not supported on encrypted columns.
- if (_metaData[i] != null && _metaData[i].cipherMD != null) {
- throw SQL.StreamNotSupportOnEncryptedColumn(_metaData[i].column);
- }
- // Stream is only for Binary, Image, VarBinary, Udt and Xml types
- // NOTE: IsBinType also includes Timestamp for some reason...
- MetaType mt = _metaData[i].metaType;
- if (((!mt.IsBinType) || (mt.SqlDbType == SqlDbType.Timestamp)) && (mt.SqlDbType != SqlDbType.Variant)) {
- throw SQL.StreamNotSupportOnColumnType(_metaData[i].column);
- }
- // For non-variant types with sequential access, we support proper streaming
- if ((mt.SqlDbType != SqlDbType.Variant) && (IsCommandBehavior(CommandBehavior.SequentialAccess))) {
- _currentStream = new SqlSequentialStream(this, i);
- _lastColumnWithDataChunkRead = i;
- return _currentStream;
- }
- else {
- // Need to call ReadColumn, since we want to access the internal data structures (i.e. SqlBinary) rather than calling anther Get*() method
- ReadColumn(i);
- byte[] data;
- if (_data[i].IsNull) {
- // A 'null' stream
- data = new byte[0];
- }
- else {
- // Grab already read data
- data = _data[i].SqlBinary.Value;
- }
- // If non-sequential then we just have a read-only MemoryStream
- return new MemoryStream(data, writable: false);
- }
- }
- override public byte GetByte(int i) {
- ReadColumn(i);
- return _data[i].Byte;
- }
- override public long GetBytes(int i, long dataIndex, byte[] buffer, int bufferIndex, int length) {
- SqlStatistics statistics = null;
- long cbBytes = 0;
- CheckDataIsReady(columnIndex: i, allowPartiallyReadColumn: true, methodName: "GetBytes");
- // don't allow get bytes on non-long or non-binary columns
- MetaType mt = _metaData[i].metaType;
- if (!(mt.IsLong || mt.IsBinType) || (SqlDbType.Xml == mt.SqlDbType)) {
- throw SQL.NonBlobColumn(_metaData[i].column);
- }
-
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- SetTimeout(_defaultTimeoutMilliseconds);
- cbBytes = GetBytesInternal(i, dataIndex, buffer, bufferIndex, length);
- _lastColumnWithDataChunkRead = i;
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- return cbBytes;
- }
- // Used (indirectly) by SqlCommand.CompleteXmlReader
- virtual internal long GetBytesInternal(int i, long dataIndex, byte[] buffer, int bufferIndex, int length) {
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- long value;
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- bool result = TryGetBytesInternal(i, dataIndex, buffer, bufferIndex, length, out value);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- return value;
- }
- private bool TryGetBytesInternal(int i, long dataIndex, byte[] buffer, int bufferIndex, int length, out long remaining) {
- remaining = 0;
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #else
- {
- #endif //DEBUG
- int cbytes = 0;
- AssertReaderState(requireData: true, permitAsync: true, columnIndex: i, enforceSequentialAccess: true);
- // sequential reading
- if (IsCommandBehavior(CommandBehavior.SequentialAccess)) {
- Debug.Assert(!HasActiveStreamOrTextReaderOnColumn(i), "Column has an active Stream or TextReader");
- if (_metaData[i] != null && _metaData[i].cipherMD != null) {
- throw SQL.SequentialAccessNotSupportedOnEncryptedColumn(_metaData[i].column);
- }
- if (_sharedState._nextColumnHeaderToRead <= i) {
- if (!TryReadColumnHeader(i)) {
- return false;
- }
- }
- // If data is null, ReadColumnHeader sets the data.IsNull bit.
- if (_data[i] != null && _data[i].IsNull) {
- throw new SqlNullValueException();
- }
- // If there are an unknown (-1) number of bytes left for a PLP, read its size
- if ((-1 == _sharedState._columnDataBytesRemaining) && (_metaData[i].metaType.IsPlp)) {
- ulong left;
- if (!_parser.TryPlpBytesLeft(_stateObj, out left)) {
- return false;
- }
- _sharedState._columnDataBytesRemaining = (long)left;
- }
- if (0 == _sharedState._columnDataBytesRemaining) {
- return true; // We've read this column to the end
- }
- // if no buffer is passed in, return the number total of bytes, or -1
- if (null == buffer) {
- if (_metaData[i].metaType.IsPlp) {
- remaining = (long) _parser.PlpBytesTotalLength(_stateObj);
- return true;
- }
- remaining = _sharedState._columnDataBytesRemaining;
- return true;
- }
-
- if (dataIndex < 0)
- throw ADP.NegativeParameter("dataIndex");
-
- if (dataIndex < _columnDataBytesRead) {
- throw ADP.NonSeqByteAccess(dataIndex, _columnDataBytesRead, ADP.GetBytes);
- }
- // if the dataIndex is not equal to bytes read, then we have to skip bytes
- long cb = dataIndex - _columnDataBytesRead;
- // if dataIndex is outside of the data range, return 0
- if ((cb > _sharedState._columnDataBytesRemaining) && !_metaData[i].metaType.IsPlp) {
- return true;
- }
-
- // if bad buffer index, throw
- if (bufferIndex < 0 || bufferIndex >= buffer.Length)
- throw ADP.InvalidDestinationBufferIndex(buffer.Length, bufferIndex, "bufferIndex");
- // if there is not enough room in the buffer for data
- if (length + bufferIndex > buffer.Length)
- throw ADP.InvalidBufferSizeOrIndex(length, bufferIndex);
- if (length < 0)
- throw ADP.InvalidDataLength(length);
- // Skip if needed
- if (cb > 0) {
- if (_metaData[i].metaType.IsPlp) {
- ulong skipped;
- if (!_parser.TrySkipPlpValue((ulong) cb, _stateObj, out skipped)) {
- return false;
- }
- _columnDataBytesRead += (long) skipped;
- }
- else {
- if (!_stateObj.TrySkipLongBytes(cb)) {
- return false;
- }
- _columnDataBytesRead += cb;
- _sharedState._columnDataBytesRemaining -= cb;
- }
- }
- int bytesRead;
- bool result = TryGetBytesInternalSequential(i, buffer, bufferIndex, length, out bytesRead);
- remaining = (int)bytesRead;
- return result;
- }
- // random access now!
- // note that since we are caching in an array, and arrays aren't 64 bit ready yet,
- // we need can cast to int if the dataIndex is in range
- if (dataIndex < 0)
- throw ADP.NegativeParameter("dataIndex");
-
- if (dataIndex > Int32.MaxValue) {
- throw ADP.InvalidSourceBufferIndex(cbytes, dataIndex, "dataIndex");
- }
-
- int ndataIndex = (int)dataIndex;
- byte[] data;
- // WebData 99342 - in the non-sequential case, we need to support
- // the use of GetBytes on string data columns, but
- // GetSqlBinary isn't supposed to. What we end up
- // doing isn't exactly pretty, but it does work.
- if (_metaData[i].metaType.IsBinType) {
- data = GetSqlBinary(i).Value;
- }
- else {
- Debug.Assert(_metaData[i].metaType.IsLong, "non long type?");
- Debug.Assert(_metaData[i].metaType.IsCharType, "non-char type?");
- SqlString temp = GetSqlString(i);
- if (_metaData[i].metaType.IsNCharType) {
- data = temp.GetUnicodeBytes();
- }
- else {
- data = temp.GetNonUnicodeBytes();
- }
- }
- cbytes = data.Length;
- // if no buffer is passed in, return the number of characters we have
- if (null == buffer) {
- remaining = cbytes;
- return true;
- }
- // if dataIndex is outside of data range, return 0
- if (ndataIndex < 0 || ndataIndex >= cbytes) {
- return true;
- }
- try {
- if (ndataIndex < cbytes) {
- // help the user out in the case where there's less data than requested
- if ((ndataIndex + length) > cbytes)
- cbytes = cbytes - ndataIndex;
- else
- cbytes = length;
- }
- Array.Copy(data, ndataIndex, buffer, bufferIndex, cbytes);
- }
- catch (Exception e) {
- //
- if (!ADP.IsCatchableExceptionType(e)) {
- throw;
- }
- cbytes = data.Length;
- if (length < 0)
- throw ADP.InvalidDataLength(length);
- // if bad buffer index, throw
- if (bufferIndex < 0 || bufferIndex >= buffer.Length)
- throw ADP.InvalidDestinationBufferIndex(buffer.Length, bufferIndex, "bufferIndex");
- // if there is not enough room in the buffer for data
- if (cbytes + bufferIndex > buffer.Length)
- throw ADP.InvalidBufferSizeOrIndex(cbytes, bufferIndex);
- throw;
- }
- remaining = cbytes;
- return true;
- }
- #if DEBUG
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- }
- internal int GetBytesInternalSequential(int i, byte[] buffer, int index, int length, long? timeoutMilliseconds = null) {
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- int value;
- SqlStatistics statistics = null;
- Debug.Assert(_stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- SetTimeout(timeoutMilliseconds ?? _defaultTimeoutMilliseconds);
- bool result = TryReadColumnHeader(i);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- result = TryGetBytesInternalSequential(i, buffer, index, length, out value);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
-
- return value;
- }
- // This is meant to be called from other internal methods once we are at the column to read
- // NOTE: This method must be retriable WITHOUT replaying a snapshot
- // Every time you call this method increment the index and decrease length by the value of bytesRead
- internal bool TryGetBytesInternalSequential(int i, byte[] buffer, int index, int length, out int bytesRead) {
- AssertReaderState(requireData: true, permitAsync: true, columnIndex: i, enforceSequentialAccess: true);
- Debug.Assert(_sharedState._nextColumnHeaderToRead == i + 1 && _sharedState._nextColumnDataToRead == i, "Non sequential access");
- Debug.Assert(buffer != null, "Null buffer");
- Debug.Assert(index >= 0, "Invalid index");
- Debug.Assert(length >= 0, "Invalid length");
- Debug.Assert(index + length <= buffer.Length, "Buffer too small");
-
- bytesRead = 0;
- RuntimeHelpers.PrepareConstrainedRegions();
- try
- {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #endif //DEBUG
- if ((_sharedState._columnDataBytesRemaining == 0) || (length == 0)) {
- // No data left or nothing requested, return 0
- bytesRead = 0;
- return true;
- }
- else {
- // if plp columns, do partial reads. Don't read the entire value in one shot.
- if (_metaData[i].metaType.IsPlp) {
- // Read in data
- bool result = _stateObj.TryReadPlpBytes(ref buffer, index, length, out bytesRead);
- _columnDataBytesRead += bytesRead;
- if (!result) {
- return false;
- }
- // Query for number of bytes left
- ulong left;
- if (!_parser.TryPlpBytesLeft(_stateObj, out left)) {
- _sharedState._columnDataBytesRemaining = -1;
- return false;
- }
- _sharedState._columnDataBytesRemaining = (long)left;
- return true;
- }
- else {
- // Read data (not exceeding the total amount of data available)
- int bytesToRead = (int)Math.Min((long)length, _sharedState._columnDataBytesRemaining);
- bool result = _stateObj.TryReadByteArray(buffer, index, bytesToRead, out bytesRead);
- _columnDataBytesRead += bytesRead;
- _sharedState._columnDataBytesRemaining -= bytesRead;
- return result;
- }
- }
- #if DEBUG
- }
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- }
- override public TextReader GetTextReader(int i) {
- CheckDataIsReady(columnIndex: i, methodName: "GetTextReader");
-
- // Xml type is not supported
- MetaType mt = null;
- if (_metaData[i].cipherMD != null) {
- Debug.Assert(_metaData[i].baseTI != null, "_metaData[i].baseTI should not be null.");
- mt = _metaData[i].baseTI.metaType;
- }
- else {
- mt = _metaData[i].metaType;
- }
- Debug.Assert(mt != null, @"mt should not be null.");
- if (((!mt.IsCharType) && (mt.SqlDbType != SqlDbType.Variant)) || (mt.SqlDbType == SqlDbType.Xml)) {
- throw SQL.TextReaderNotSupportOnColumnType(_metaData[i].column);
- }
- // For non-variant types with sequential access, we support proper streaming
- if ((mt.SqlDbType != SqlDbType.Variant) && (IsCommandBehavior(CommandBehavior.SequentialAccess))) {
- if (_metaData[i].cipherMD != null) {
- throw SQL.SequentialAccessNotSupportedOnEncryptedColumn(_metaData[i].column);
- }
- System.Text.Encoding encoding;
- if (mt.IsNCharType)
- {
- // NChar types always use unicode
- encoding = SqlUnicodeEncoding.SqlUnicodeEncodingInstance;
- }
- else
- {
- encoding = _metaData[i].encoding;
- }
-
- _currentTextReader = new SqlSequentialTextReader(this, i, encoding);
- _lastColumnWithDataChunkRead = i;
- return _currentTextReader;
- }
- else {
- // Need to call ReadColumn, since we want to access the internal data structures (i.e. SqlString) rather than calling anther Get*() method
- ReadColumn(i);
- string data;
- if (_data[i].IsNull) {
- // A 'null' stream
- data = string.Empty;
- }
- else {
- // Grab already read data
- data = _data[i].SqlString.Value;
- }
- // We've already read the data, so just wrap it in a StringReader
- return new StringReader(data);
- }
- }
- [ EditorBrowsableAttribute(EditorBrowsableState.Never) ] // MDAC 69508
- override public char GetChar(int i) {
- throw ADP.NotSupported();
- }
- override public long GetChars(int i, long dataIndex, char[] buffer, int bufferIndex, int length) {
- SqlStatistics statistics = null;
- CheckMetaDataIsReady(columnIndex: i);
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- MetaType mt = null;
- if (_metaData[i].cipherMD != null) {
- Debug.Assert(_metaData[i].baseTI != null, @"_metaData[i].baseTI should not be null.");
- mt = _metaData[i].baseTI.metaType;
- }
- else {
- mt = _metaData[i].metaType;
- }
- Debug.Assert(mt != null, "mt should not be null.");
- SqlDbType sqlDbType;
- if (_metaData[i].cipherMD != null) {
- Debug.Assert(_metaData[i].baseTI != null, @"_metaData[i].baseTI should not be null.");
- sqlDbType = _metaData[i].baseTI.type;
- }
- else {
- sqlDbType = _metaData[i].type;
- }
-
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- SetTimeout(_defaultTimeoutMilliseconds);
- if ((mt.IsPlp) &&
- (IsCommandBehavior(CommandBehavior.SequentialAccess)) ) {
- if (length < 0) {
- throw ADP.InvalidDataLength(length);
- }
- if (_metaData[i].cipherMD != null) {
- throw SQL.SequentialAccessNotSupportedOnEncryptedColumn(_metaData[i].column);
- }
- // if bad buffer index, throw
- if ((bufferIndex < 0) || (buffer != null && bufferIndex >= buffer.Length)) {
- throw ADP.InvalidDestinationBufferIndex(buffer.Length, bufferIndex, "bufferIndex");
- }
-
- // if there is not enough room in the buffer for data
- if (buffer != null && (length + bufferIndex > buffer.Length)) {
- throw ADP.InvalidBufferSizeOrIndex(length, bufferIndex);
- }
- long charsRead = 0;
- if ( sqlDbType == SqlDbType.Xml ) {
- try {
- CheckDataIsReady(columnIndex: i, allowPartiallyReadColumn: true, methodName: "GetChars");
- }
- catch (Exception ex) {
- // Dev11 Bug #315513: Exception type breaking change from 4.0 RTM when calling GetChars on null xml
- // We need to wrap all exceptions inside a TargetInvocationException to simulate calling CreateSqlReader via MethodInfo.Invoke
- if (ADP.IsCatchableExceptionType(ex)) {
- throw new TargetInvocationException(ex);
- }
- else {
- throw;
- }
- }
- charsRead = GetStreamingXmlChars(i, dataIndex, buffer, bufferIndex, length);
- }
- else {
- CheckDataIsReady(columnIndex: i, allowPartiallyReadColumn: true, methodName: "GetChars");
- charsRead = GetCharsFromPlpData(i, dataIndex, buffer, bufferIndex, length);
- }
- _lastColumnWithDataChunkRead = i;
- return charsRead;
- }
- // Did we start reading this value yet?
- if ((_sharedState._nextColumnDataToRead == (i+1)) && (_sharedState._nextColumnHeaderToRead == (i+1)) && (_columnDataChars != null) && (IsCommandBehavior(CommandBehavior.SequentialAccess)) && (dataIndex < _columnDataCharsRead)) {
- // Don't allow re-read of same chars in sequential access mode
- throw ADP.NonSeqByteAccess(dataIndex, _columnDataCharsRead, ADP.GetChars);
- }
- if (_columnDataCharsIndex != i) {
- // if the object doesn't contain a char[] then the user will get an exception
- string s = GetSqlString(i).Value;
- _columnDataChars = s.ToCharArray();
- _columnDataCharsRead = 0;
- _columnDataCharsIndex = i;
- }
- int cchars = _columnDataChars.Length;
- // note that since we are caching in an array, and arrays aren't 64 bit ready yet,
- // we need can cast to int if the dataIndex is in range
- if (dataIndex > Int32.MaxValue) {
- throw ADP.InvalidSourceBufferIndex(cchars, dataIndex, "dataIndex");
- }
- int ndataIndex = (int)dataIndex;
- // if no buffer is passed in, return the number of characters we have
- if (null == buffer)
- return cchars;
- // if dataIndex outside of data range, return 0
- if (ndataIndex < 0 || ndataIndex >= cchars)
- return 0;
- try {
- if (ndataIndex < cchars) {
- // help the user out in the case where there's less data than requested
- if ((ndataIndex + length) > cchars)
- cchars = cchars - ndataIndex;
- else
- cchars = length;
- }
- Array.Copy(_columnDataChars, ndataIndex, buffer, bufferIndex, cchars);
- _columnDataCharsRead += cchars;
- }
- catch (Exception e) {
- //
- if (!ADP.IsCatchableExceptionType(e)) {
- throw;
- }
- cchars = _columnDataChars.Length;
- if (length < 0)
- throw ADP.InvalidDataLength(length);
- // if bad buffer index, throw
- if (bufferIndex < 0 || bufferIndex >= buffer.Length)
- throw ADP.InvalidDestinationBufferIndex(buffer.Length, bufferIndex, "bufferIndex");
- // if there is not enough room in the buffer for data
- if (cchars + bufferIndex > buffer.Length)
- throw ADP.InvalidBufferSizeOrIndex(cchars, bufferIndex);
- throw;
- }
- return cchars;
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- }
- private long GetCharsFromPlpData(int i, long dataIndex, char[] buffer, int bufferIndex, int length) {
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #else
- {
- #endif //DEBUG
- long cch;
- AssertReaderState(requireData: true, permitAsync: false, columnIndex: i, enforceSequentialAccess: true);
- Debug.Assert(!HasActiveStreamOrTextReaderOnColumn(i), "Column has active Stream or TextReader");
- // don't allow get bytes on non-long or non-binary columns
- Debug.Assert(_metaData[i].metaType.IsPlp, "GetCharsFromPlpData called on a non-plp column!");
- // Must be sequential reading
- Debug.Assert (IsCommandBehavior(CommandBehavior.SequentialAccess), "GetCharsFromPlpData called for non-Sequential access");
-
- if (!_metaData[i].metaType.IsCharType) {
- throw SQL.NonCharColumn(_metaData[i].column);
- }
-
- if (_sharedState._nextColumnHeaderToRead <= i) {
- ReadColumnHeader(i);
- }
-
- // If data is null, ReadColumnHeader sets the data.IsNull bit.
- if (_data[i] != null && _data[i].IsNull) {
- throw new SqlNullValueException();
- }
- if (dataIndex < _columnDataCharsRead) {
- // Don't allow re-read of same chars in sequential access mode
- throw ADP.NonSeqByteAccess(dataIndex, _columnDataCharsRead, ADP.GetChars);
- }
-
- // If we start reading the new column, either dataIndex is 0 or
- // _columnDataCharsRead is 0 and dataIndex > _columnDataCharsRead is true below.
- // In both cases we will clean decoder
- if (dataIndex == 0)
- _stateObj._plpdecoder = null;
- bool isUnicode = _metaData[i].metaType.IsNCharType;
- // If there are an unknown (-1) number of bytes left for a PLP, read its size
- if (-1 == _sharedState._columnDataBytesRemaining) {
- _sharedState._columnDataBytesRemaining = (long)_parser.PlpBytesLeft(_stateObj);
- }
- if (0 == _sharedState._columnDataBytesRemaining) {
- _stateObj._plpdecoder = null;
- return 0; // We've read this column to the end
- }
- // if no buffer is passed in, return the total number of characters or -1
- //
- if (null == buffer) {
- cch = (long) _parser.PlpBytesTotalLength(_stateObj);
- return (isUnicode && (cch > 0)) ? cch >> 1 : cch;
- }
- if (dataIndex > _columnDataCharsRead) {
- // Skip chars
- // Clean decoder state: we do not reset it, but destroy to ensure
- // that we do not start decoding the column with decoder from the old one
- _stateObj._plpdecoder = null;
- //
- cch = dataIndex - _columnDataCharsRead;
- cch = isUnicode ? (cch << 1 ) : cch;
- cch = (long) _parser.SkipPlpValue((ulong)(cch), _stateObj);
- _columnDataBytesRead += cch;
- _columnDataCharsRead += (isUnicode && (cch > 0)) ? cch >> 1 : cch;
- }
- cch = length;
-
- if (isUnicode) {
- cch = (long) _parser.ReadPlpUnicodeChars(ref buffer, bufferIndex, length, _stateObj);
- _columnDataBytesRead += (cch << 1);
- }
- else {
- cch = (long) _parser.ReadPlpAnsiChars(ref buffer, bufferIndex, length, _metaData[i], _stateObj);
- _columnDataBytesRead += cch << 1;
- }
- _columnDataCharsRead += cch;
- _sharedState._columnDataBytesRemaining = (long)_parser.PlpBytesLeft(_stateObj);
- return cch;
- }
- #if DEBUG
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- }
- internal long GetStreamingXmlChars(int i, long dataIndex, char[] buffer, int bufferIndex, int length) {
- SqlStreamingXml localSXml = null;
- if ((_streamingXml != null) && ( _streamingXml.ColumnOrdinal != i)) {
- _streamingXml.Close();
- _streamingXml = null;
- }
- if (_streamingXml == null) {
- localSXml = new SqlStreamingXml(i, this);
- }
- else {
- localSXml = _streamingXml;
- }
- long cnt = localSXml.GetChars(dataIndex, buffer, bufferIndex, length);
- if (_streamingXml == null) {
- // Data is read through GetBytesInternal which may dispose _streamingXml if it has to advance the column ordinal.
- // Therefore save the new SqlStreamingXml class after the read succeeds.
- _streamingXml = localSXml;
- }
- return cnt;
- }
- [ EditorBrowsableAttribute(EditorBrowsableState.Never) ] // MDAC 69508
- IDataReader IDataRecord.GetData(int i) {
- throw ADP.NotSupported();
- }
- override public DateTime GetDateTime(int i) {
- ReadColumn(i);
- DateTime dt = _data[i].DateTime;
- // This accessor can be called for regular DateTime column. In this case we should not throw
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && _metaData[i].IsNewKatmaiDateTimeType) {
- // TypeSystem.SQLServer2005 or less
- // If the above succeeds, then we received a valid DateTime instance, now we need to force
- // an InvalidCastException since DateTime is not exposed with the version knob in this setting.
- // To do so, we simply force the exception by casting the string representation of the value
- // To DateTime.
- object temp = (object) _data[i].String;
- dt = (DateTime) temp;
- }
- return dt;
- }
- override public Decimal GetDecimal(int i) {
- ReadColumn(i);
- return _data[i].Decimal;
- }
- override public double GetDouble(int i) {
- ReadColumn(i);
- return _data[i].Double;
- }
- override public float GetFloat(int i) {
- ReadColumn(i);
- return _data[i].Single;
- }
- override public Guid GetGuid(int i) {
- ReadColumn(i);
- return _data[i].SqlGuid.Value;
- }
- override public Int16 GetInt16(int i) {
- ReadColumn(i);
- return _data[i].Int16;
- }
- override public Int32 GetInt32(int i) {
- ReadColumn(i);
- return _data[i].Int32;
- }
- override public Int64 GetInt64(int i) {
- ReadColumn(i);
- return _data[i].Int64;
- }
- virtual public SqlBoolean GetSqlBoolean(int i) {
- ReadColumn(i);
- return _data[i].SqlBoolean;
- }
- virtual public SqlBinary GetSqlBinary(int i) {
- ReadColumn(i, setTimeout: true, allowPartiallyReadColumn: true);
- return _data[i].SqlBinary;
- }
- virtual public SqlByte GetSqlByte(int i) {
- ReadColumn(i);
- return _data[i].SqlByte;
- }
- virtual public SqlBytes GetSqlBytes(int i) {
- ReadColumn(i);
- SqlBinary data = _data[i].SqlBinary;
- return new SqlBytes(data);
- }
- virtual public SqlChars GetSqlChars(int i) {
- ReadColumn(i);
- SqlString data;
- // Convert Katmai types to string
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && _metaData[i].IsNewKatmaiDateTimeType)
- {
- data = _data[i].KatmaiDateTimeSqlString;
- } else {
- data = _data[i].SqlString;
- }
- return new SqlChars(data);
- }
- virtual public SqlDateTime GetSqlDateTime(int i) {
- ReadColumn(i);
- return _data[i].SqlDateTime;
- }
- virtual public SqlDecimal GetSqlDecimal(int i) {
- ReadColumn(i);
- return _data[i].SqlDecimal;
- }
- virtual public SqlGuid GetSqlGuid(int i) {
- ReadColumn(i);
- return _data[i].SqlGuid;
- }
- virtual public SqlDouble GetSqlDouble(int i) {
- ReadColumn(i);
- return _data[i].SqlDouble;
- }
- virtual public SqlInt16 GetSqlInt16(int i) {
- ReadColumn(i);
- return _data[i].SqlInt16;
- }
- virtual public SqlInt32 GetSqlInt32(int i) {
- ReadColumn(i);
- return _data[i].SqlInt32;
- }
- virtual public SqlInt64 GetSqlInt64(int i) {
- ReadColumn(i);
- return _data[i].SqlInt64;
- }
- virtual public SqlMoney GetSqlMoney(int i) {
- ReadColumn(i);
- return _data[i].SqlMoney;
- }
- virtual public SqlSingle GetSqlSingle(int i) {
- ReadColumn(i);
- return _data[i].SqlSingle;
- }
- //
- virtual public SqlString GetSqlString(int i) {
- ReadColumn(i);
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && _metaData[i].IsNewKatmaiDateTimeType) {
- return _data[i].KatmaiDateTimeSqlString;
- }
- return _data[i].SqlString;
- }
- virtual public SqlXml GetSqlXml(int i){
- ReadColumn(i);
- SqlXml sx = null;
- if (_typeSystem != SqlConnectionString.TypeSystem.SQLServer2000) {
- // TypeSystem.SQLServer2005
- sx = _data[i].IsNull ? SqlXml.Null : _data[i].SqlCachedBuffer.ToSqlXml();
- }
- else {
- // TypeSystem.SQLServer2000
- // First, attempt to obtain SqlXml value. If not SqlXml, we will throw the appropriate
- // cast exception.
- sx = _data[i].IsNull ? SqlXml.Null : _data[i].SqlCachedBuffer.ToSqlXml();
- // If the above succeeds, then we received a valid SqlXml instance, now we need to force
- // an InvalidCastException since SqlXml is not exposed with the version knob in this setting.
- // To do so, we simply force the exception by casting the string representation of the value
- // To SqlXml.
- object temp = (object) _data[i].String;
- sx = (SqlXml) temp;
- }
- return sx;
- }
- virtual public object GetSqlValue(int i) {
- SqlStatistics statistics = null;
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- SetTimeout(_defaultTimeoutMilliseconds);
- return GetSqlValueInternal(i);
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- }
- private object GetSqlValueInternal(int i) {
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- bool result = TryReadColumn(i, setTimeout: false);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- return GetSqlValueFromSqlBufferInternal(_data[i], _metaData[i]);
- }
- // NOTE: This method is called by the fast-paths in Async methods and, therefore, should be resilient to the DataReader being closed
- // Always make sure to take reference copies of anything set to null in TryCloseInternal()
- private object GetSqlValueFromSqlBufferInternal(SqlBuffer data, _SqlMetaData metaData) {
- // Dev11 Bug #336820, Dev10 Bug #479607 (SqlClient: IsDBNull always returns false for timestamp datatype)
- // Due to a bug in TdsParser.GetNullSqlValue, Timestamps' IsNull is not correctly set - so we need to bypass the following check
- Debug.Assert(!data.IsEmpty || data.IsNull || metaData.type == SqlDbType.Timestamp, "Data has been read, but the buffer is empty");
- // Convert Katmai types to string
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsNewKatmaiDateTimeType) {
- return data.KatmaiDateTimeSqlString;
- }
- else if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsLargeUdt) {
- return data.SqlValue;
- }
- else if (_typeSystem != SqlConnectionString.TypeSystem.SQLServer2000) {
- // TypeSystem.SQLServer2005
- if (metaData.type == SqlDbType.Udt) {
- var connection = _connection;
- if (connection != null) {
- connection.CheckGetExtendedUDTInfo(metaData, true);
- return connection.GetUdtValue(data.Value, metaData, false);
- }
- else {
- throw ADP.DataReaderClosed("GetSqlValueFromSqlBufferInternal");
- }
- }
- else {
- return data.SqlValue;
- }
- }
- else {
- // TypeSystem.SQLServer2000
- if (metaData.type == SqlDbType.Xml) {
- return data.SqlString;
- }
- else {
- return data.SqlValue;
- }
- }
- }
- virtual public int GetSqlValues(object[] values){
- SqlStatistics statistics = null;
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- CheckDataIsReady();
- if (null == values) {
- throw ADP.ArgumentNull("values");
- }
- SetTimeout(_defaultTimeoutMilliseconds);
- int copyLen = (values.Length < _metaData.visibleColumns) ? values.Length : _metaData.visibleColumns;
- for (int i = 0; i < copyLen; i++) {
- values[_metaData.indexMap[i]] = GetSqlValueInternal(i);
- }
- return copyLen;
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- }
- override public string GetString(int i) {
- ReadColumn(i);
- // Convert katmai value to string if type system knob is 2005 or earlier
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && _metaData[i].IsNewKatmaiDateTimeType) {
- return _data[i].KatmaiDateTimeString;
- }
- return _data[i].String;
- }
-
- override public T GetFieldValue<T>(int i) {
- SqlStatistics statistics = null;
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- SetTimeout(_defaultTimeoutMilliseconds);
- return GetFieldValueInternal<T>(i);
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- }
- override public object GetValue(int i) {
- SqlStatistics statistics = null;
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- SetTimeout(_defaultTimeoutMilliseconds);
- return GetValueInternal(i);
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- }
- }
- virtual public TimeSpan GetTimeSpan(int i) {
- ReadColumn(i);
- TimeSpan t = _data[i].Time;
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005) {
- // TypeSystem.SQLServer2005 or less
- // If the above succeeds, then we received a valid TimeSpan instance, now we need to force
- // an InvalidCastException since TimeSpan is not exposed with the version knob in this setting.
- // To do so, we simply force the exception by casting the string representation of the value
- // To TimeSpan.
- object temp = (object) _data[i].String;
- t = (TimeSpan) temp;
- }
- return t;
- }
- virtual public DateTimeOffset GetDateTimeOffset(int i) {
- ReadColumn(i);
- DateTimeOffset dto = _data[i].DateTimeOffset;
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005) {
- // TypeSystem.SQLServer2005 or less
- // If the above succeeds, then we received a valid DateTimeOffset instance, now we need to force
- // an InvalidCastException since DateTime is not exposed with the version knob in this setting.
- // To do so, we simply force the exception by casting the string representation of the value
- // To DateTimeOffset.
- object temp = (object) _data[i].String;
- dto = (DateTimeOffset) temp;
- }
- return dto;
- }
- private object GetValueInternal(int i) {
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- bool result = TryReadColumn(i, setTimeout: false);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- return GetValueFromSqlBufferInternal(_data[i], _metaData[i]);
- }
- // NOTE: This method is called by the fast-paths in Async methods and, therefore, should be resilient to the DataReader being closed
- // Always make sure to take reference copies of anything set to null in TryCloseInternal()
- private object GetValueFromSqlBufferInternal(SqlBuffer data, _SqlMetaData metaData) {
- // Dev11 Bug #336820, Dev10 Bug #479607 (SqlClient: IsDBNull always returns false for timestamp datatype)
- // Due to a bug in TdsParser.GetNullSqlValue, Timestamps' IsNull is not correctly set - so we need to bypass the following check
- Debug.Assert(!data.IsEmpty || data.IsNull || metaData.type == SqlDbType.Timestamp, "Data has been read, but the buffer is empty");
- if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsNewKatmaiDateTimeType) {
- if (data.IsNull) {
- return DBNull.Value;
- }
- else {
- return data.KatmaiDateTimeString;
- }
- }
- else if (_typeSystem <= SqlConnectionString.TypeSystem.SQLServer2005 && metaData.IsLargeUdt) {
- return data.Value;
- }
- else if (_typeSystem != SqlConnectionString.TypeSystem.SQLServer2000) {
- // TypeSystem.SQLServer2005
- if (metaData.type != SqlDbType.Udt) {
- return data.Value;
- }
- else {
- var connection = _connection;
- if (connection != null) {
- connection.CheckGetExtendedUDTInfo(metaData, true);
- return connection.GetUdtValue(data.Value, metaData, true);
- }
- else {
- throw ADP.DataReaderClosed("GetValueFromSqlBufferInternal");
- }
- }
- }
- else {
- // TypeSystem.SQLServer2000
- return data.Value;
- }
- }
- private T GetFieldValueInternal<T>(int i) {
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- bool result = TryReadColumn(i, setTimeout: false);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- return GetFieldValueFromSqlBufferInternal<T>(_data[i], _metaData[i]);
- }
- private T GetFieldValueFromSqlBufferInternal<T>(SqlBuffer data, _SqlMetaData metaData) {
- Type typeofT = typeof(T);
- if (_typeofINullable.IsAssignableFrom(typeofT)) {
- // If its a SQL Type or Nullable UDT
- object rawValue = GetSqlValueFromSqlBufferInternal(data, metaData);
- // Special case: User wants SqlString, but we have a SqlXml
- // SqlXml can not be typecast into a SqlString, but we need to support SqlString on XML Types - so do a manual conversion
- if (typeofT == _typeofSqlString) {
- SqlXml xmlValue = rawValue as SqlXml;
- if (xmlValue != null) {
- if (xmlValue.IsNull) {
- rawValue = SqlString.Null;
- }
- else {
- rawValue = new SqlString(xmlValue.Value);
- }
- }
- }
- return (T)rawValue;
- }
- else {
- // Otherwise Its a CLR or non-Nullable UDT
- try {
- return (T)GetValueFromSqlBufferInternal(data, metaData);
- }
- catch (InvalidCastException) {
- if (data.IsNull) {
- // If the value was actually null, then we should throw a SqlNullValue instead
- throw SQL.SqlNullValue();
- }
- else {
- // Legitmate InvalidCast, rethrow
- throw;
- }
- }
- }
- }
- override public int GetValues(object[] values) {
- SqlStatistics statistics = null;
- bool sequentialAccess = IsCommandBehavior(CommandBehavior.SequentialAccess);
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- if (null == values) {
- throw ADP.ArgumentNull("values");
- }
- CheckMetaDataIsReady();
- int copyLen = (values.Length < _metaData.visibleColumns) ? values.Length : _metaData.visibleColumns;
- int maximumColumn = copyLen - 1;
- SetTimeout(_defaultTimeoutMilliseconds);
- // Temporarily disable sequential access
- _commandBehavior &= ~CommandBehavior.SequentialAccess;
- // Read in all of the columns in one TryReadColumn call
- bool result = TryReadColumn(maximumColumn, setTimeout: false);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- for (int i = 0; i < copyLen; i++) {
- // Get the usable, TypeSystem-compatible value from the iternal buffer
- values[_metaData.indexMap[i]] = GetValueFromSqlBufferInternal(_data[i], _metaData[i]);
- // If this is sequential access, then we need to wipe the internal buffer
- if ((sequentialAccess) && (i < maximumColumn)) {
- _data[i].Clear();
- }
- }
- return copyLen;
- }
- finally {
- // Restore sequential access
- if (sequentialAccess) {
- _commandBehavior |= CommandBehavior.SequentialAccess;
- }
- SqlStatistics.StopTimer(statistics);
- }
- }
- private MetaType GetVersionedMetaType(MetaType actualMetaType) {
- Debug.Assert(_typeSystem == SqlConnectionString.TypeSystem.SQLServer2000, "Should not be in this function under anything else but SQLServer2000");
- MetaType metaType = null;
- if (actualMetaType == MetaType.MetaUdt) {
- metaType = MetaType.MetaVarBinary;
- }
- else if (actualMetaType == MetaType.MetaXml) {
- metaType = MetaType.MetaNText;
- }
- else if (actualMetaType == MetaType.MetaMaxVarBinary) {
- metaType = MetaType.MetaImage;
- }
- else if (actualMetaType == MetaType.MetaMaxVarChar) {
- metaType = MetaType.MetaText;
- }
- else if (actualMetaType == MetaType.MetaMaxNVarChar) {
- metaType = MetaType.MetaNText;
- }
- else {
- metaType = actualMetaType;
- }
- return metaType;
- }
- private bool TryHasMoreResults(out bool moreResults) {
- if(null != _parser) {
- bool moreRows;
- if (!TryHasMoreRows(out moreRows)) {
- moreResults = false;
- return false;
- }
- if(moreRows) {
- // When does this happen? This is only called from NextResult(), which loops until Read() false.
- moreResults = false;
- return true;
- }
- Debug.Assert(null != _command, "unexpected null command from the data reader!");
- while(_stateObj._pendingData) {
- byte token;
- if (!_stateObj.TryPeekByte(out token)) {
- moreResults = false;
- return false;
- }
- switch(token) {
- case TdsEnums.SQLALTROW:
- if(_altRowStatus == ALTROWSTATUS.Null) {
- // cache the regular metadata
- _altMetaDataSetCollection.metaDataSet = _metaData;
- _metaData = null;
- }
- else {
- Debug.Assert(_altRowStatus == ALTROWSTATUS.Done, "invalid AltRowStatus");
- }
- _altRowStatus = ALTROWSTATUS.AltRow;
- _hasRows = true;
- moreResults = true;
- return true;
- case TdsEnums.SQLROW:
- case TdsEnums.SQLNBCROW:
- // always happens if there is a row following an altrow
- moreResults = true;
- return true;
-
- // VSTFDEVDIV 926281: DONEINPROC case is missing here; we have decided to reject this bug as it would result in breaking change
- // from Orcas RTM/SP1 and Dev10 RTM. See the bug for more details.
- // case TdsEnums.DONEINPROC:
- case TdsEnums.SQLDONE:
- Debug.Assert(_altRowStatus == ALTROWSTATUS.Done || _altRowStatus == ALTROWSTATUS.Null, "invalid AltRowStatus");
- _altRowStatus = ALTROWSTATUS.Null;
- _metaData = null;
- _altMetaDataSetCollection = null;
- moreResults = true;
- return true;
- case TdsEnums.SQLCOLMETADATA:
- moreResults = true;
- return true;
- }
- // Dev11 Bug 316483:Hang on SqlDataReader::TryHasMoreResults using MARS
- // http://vstfdevdiv:8080/web/wi.aspx?pcguid=22f9acc9-569a-41ff-b6ac-fac1b6370209&id=316483
- // TryRun() will immediately return if the TdsParser is closed\broken, causing us to enter an infinite loop
- // Instead, we will throw a closed connection exception
- if (_parser.State == TdsParserState.Broken || _parser.State == TdsParserState.Closed) {
- throw ADP.ClosedConnectionError();
- }
- bool ignored;
- if (!_parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out ignored)) {
- moreResults = false;
- return false;
- }
- }
- }
- moreResults = false;
- return true;
- }
- private bool TryHasMoreRows(out bool moreRows) {
- if (null != _parser) {
- if (_sharedState._dataReady) {
- moreRows = true;
- return true;
- }
- // NextResult: previous call to NextResult started to process the altrowpackage, can't peek anymore
- // Read: Read prepared for final processing of altrow package, No more Rows until NextResult ...
- // Done: Done processing the altrow, no more rows until NextResult ...
- switch (_altRowStatus) {
- case ALTROWSTATUS.AltRow:
- moreRows = true;
- return true;
- case ALTROWSTATUS.Done:
- moreRows = false;
- return true;
- }
- if (_stateObj._pendingData) {
- // Consume error's, info's, done's on HasMoreRows, so user obtains error on Read.
- // Previous bug where Read() would return false with error on the wire in the case
- // of metadata and error immediately following. See MDAC 78285 and 75225.
- //
- // process any done, doneproc and doneinproc token streams and
- // any order, error or info token preceeding the first done, doneproc or doneinproc token stream
- byte b;
- if (!_stateObj.TryPeekByte(out b)) {
- moreRows = false;
- return false;
- }
- bool ParsedDoneToken = false;
- while ( b == TdsEnums.SQLDONE ||
- b == TdsEnums.SQLDONEPROC ||
- b == TdsEnums.SQLDONEINPROC ||
- !ParsedDoneToken && (
- b == TdsEnums.SQLSESSIONSTATE ||
- b == TdsEnums.SQLENVCHANGE ||
- b == TdsEnums.SQLORDER ||
- b == TdsEnums.SQLERROR ||
- b == TdsEnums.SQLINFO ) ) {
- if (b == TdsEnums.SQLDONE ||
- b == TdsEnums.SQLDONEPROC ||
- b == TdsEnums.SQLDONEINPROC) {
- ParsedDoneToken = true;
- }
- // Dev11 Bug 316483:Hang on SqlDataReader::TryHasMoreResults using MARS
- // http://vstfdevdiv:8080/web/wi.aspx?pcguid=22f9acc9-569a-41ff-b6ac-fac1b6370209&id=316483
- // TryRun() will immediately return if the TdsParser is closed\broken, causing us to enter an infinite loop
- // Instead, we will throw a closed connection exception
- if (_parser.State == TdsParserState.Broken || _parser.State == TdsParserState.Closed) {
- throw ADP.ClosedConnectionError();
- }
- bool ignored;
- if (!_parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out ignored)) {
- moreRows = false;
- return false;
- }
- if ( _stateObj._pendingData) {
- if (!_stateObj.TryPeekByte(out b)) {
- moreRows = false;
- return false;
- }
- }
- else {
- break;
- }
- }
- // Only return true when we are positioned on a row token.
- if (IsRowToken(b)) {
- moreRows = true;
- return true;
- }
- }
- }
- moreRows = false;
- return true;
- }
-
- private bool IsRowToken(byte token) {
- return TdsEnums.SQLROW == token || TdsEnums.SQLNBCROW == token;
- }
- override public bool IsDBNull(int i) {
- if ((IsCommandBehavior(CommandBehavior.SequentialAccess)) && ((_sharedState._nextColumnHeaderToRead > i + 1) || (_lastColumnWithDataChunkRead > i))) {
- // Bug 447026 : A breaking change in System.Data .NET 4.5 for calling IsDBNull on commands in SequentialAccess mode
- // http://vstfdevdiv:8080/web/wi.aspx?pcguid=22f9acc9-569a-41ff-b6ac-fac1b6370209&id=447026
- // In .Net 4.0 and previous, it was possible to read a previous column using IsDBNull when in sequential mode
- // However, since it had already gone past the column, the current IsNull value is simply returned
- // To replicate this behavior we will skip CheckHeaderIsReady\ReadColumnHeader and instead just check that the reader is ready and the column is valid
- CheckMetaDataIsReady(columnIndex: i);
- }
- else {
- CheckHeaderIsReady(columnIndex: i, methodName: "IsDBNull");
-
- SetTimeout(_defaultTimeoutMilliseconds);
- ReadColumnHeader(i); // header data only
- }
- return _data[i].IsNull;
- }
- protected internal bool IsCommandBehavior(CommandBehavior condition) {
- return (condition == (condition & _commandBehavior));
- }
- override public bool NextResult() {
- if (_currentTask != null) {
- throw SQL.PendingBeginXXXExists();
- }
- bool more;
- bool result;
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- result = TryNextResult(out more);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- return more;
- }
- // recordset is automatically positioned on the first result set
- private bool TryNextResult(out bool more) {
- SqlStatistics statistics = null;
- IntPtr hscp;
- Bid.ScopeEnter(out hscp, "<sc.SqlDataReader.NextResult|API> %d#", ObjectID);
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #else
- {
- #endif //DEBUG
- statistics = SqlStatistics.StartTimer(Statistics);
- SetTimeout(_defaultTimeoutMilliseconds);
-
- if (IsClosed) {
- throw ADP.DataReaderClosed("NextResult");
- }
- _fieldNameLookup = null;
- bool success = false; // WebData 100390
- _hasRows = false; // reset HasRows
- // if we are specifically only processing a single result, then read all the results off the wire and detach
- if (IsCommandBehavior(CommandBehavior.SingleResult)) {
- if (!TryCloseInternal(false /*closeReader*/)) {
- more = false;
- return false;
- }
- // In the case of not closing the reader, null out the metadata AFTER
- // CloseInternal finishes - since CloseInternal may go to the wire
- // and use the metadata.
- ClearMetaData();
- more = success;
- return true;
- }
- if (null != _parser) {
- // if there are more rows, then skip them, the user wants the next result
- bool moreRows = true;
- while (moreRows) {
- if (!TryReadInternal(false, out moreRows)) { // don't reset set the timeout value
- more = false;
- return false;
- }
- }
- }
- // we may be done, so continue only if we have not detached ourselves from the parser
- if (null != _parser) {
- bool moreResults;
- if (!TryHasMoreResults(out moreResults)) {
- more = false;
- return false;
- }
- if (moreResults) {
- _metaDataConsumed = false;
- _browseModeInfoConsumed = false;
- switch (_altRowStatus) {
- case ALTROWSTATUS.AltRow:
- int altRowId;
- if (!_parser.TryGetAltRowId(_stateObj, out altRowId)) {
- more = false;
- return false;
- }
- _SqlMetaDataSet altMetaDataSet = _altMetaDataSetCollection.GetAltMetaData(altRowId);
- if (altMetaDataSet != null) {
- _metaData = altMetaDataSet;
- }
- Debug.Assert ((_metaData != null), "Can't match up altrowmetadata");
- break;
- case ALTROWSTATUS.Done:
- // restore the row-metaData
- _metaData = _altMetaDataSetCollection.metaDataSet;
- Debug.Assert (_altRowStatus == ALTROWSTATUS.Done, "invalid AltRowStatus");
- _altRowStatus = ALTROWSTATUS.Null;
- break;
- default:
- if (!TryConsumeMetaData()) {
- more = false;
- return false;
- }
- if (_metaData == null) {
- more = false;
- return true;
- }
- break;
- }
- success = true;
- }
- else {
- // detach the parser from this reader now
- if (!TryCloseInternal(false /*closeReader*/)) {
- more = false;
- return false;
- }
- // In the case of not closing the reader, null out the metadata AFTER
- // CloseInternal finishes - since CloseInternal may go to the wire
- // and use the metadata.
- if (!TrySetMetaData(null, false)) {
- more = false;
- return false;
- }
- }
- }
- else {
- // Clear state in case of Read calling CloseInternal() then user calls NextResult()
- // MDAC 81986. Or, also the case where the Read() above will do essentially the same
- // thing.
- ClearMetaData();
- }
- more = success;
- return true;
- }
- #if DEBUG
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- Bid.ScopeLeave(ref hscp);
- }
- }
- // user must call Read() to position on the first row
- override public bool Read() {
- if (_currentTask != null) {
- throw SQL.PendingBeginXXXExists();
- }
- bool more;
- bool result;
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- result = TryReadInternal(true, out more);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- return more;
- }
- // user must call Read() to position on the first row
- private bool TryReadInternal(bool setTimeout, out bool more) {
- SqlStatistics statistics = null;
- IntPtr hscp;
- Bid.ScopeEnter(out hscp, "<sc.SqlDataReader.Read|API> %d#", ObjectID);
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #else
- {
- #endif //DEBUG
- statistics = SqlStatistics.StartTimer(Statistics);
- if (null != _parser) {
- if (setTimeout) {
- SetTimeout(_defaultTimeoutMilliseconds);
- }
- if (_sharedState._dataReady) {
- if (!TryCleanPartialRead()) {
- more = false;
- return false;
- }
- }
-
- // clear out our buffers
- SqlBuffer.Clear(_data);
- _sharedState._nextColumnHeaderToRead = 0;
- _sharedState._nextColumnDataToRead = 0;
- _sharedState._columnDataBytesRemaining = -1; // unknown
- _lastColumnWithDataChunkRead = -1;
- if (!_haltRead) {
- bool moreRows;
- if (!TryHasMoreRows(out moreRows)) {
- more = false;
- return false;
- }
- if (moreRows) {
- // read the row from the backend (unless it's an altrow were the marker is already inside the altrow ...)
- while (_stateObj._pendingData) {
- if (_altRowStatus != ALTROWSTATUS.AltRow) {
- // if this is an ordinary row we let the run method consume the ROW token
- if (!_parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out _sharedState._dataReady)) {
- more = false;
- return false;
- }
- if (_sharedState._dataReady) {
- break;
- }
- }
- else {
- // ALTROW token and AltrowId are already consumed ...
- Debug.Assert (_altRowStatus == ALTROWSTATUS.AltRow, "invalid AltRowStatus");
- _altRowStatus = ALTROWSTATUS.Done;
- _sharedState._dataReady = true;
- break;
- }
- }
- if (_sharedState._dataReady) {
- _haltRead = IsCommandBehavior(CommandBehavior.SingleRow);
- more = true;
- return true;
- }
- }
- if (!_stateObj._pendingData) {
- if (!TryCloseInternal(false /*closeReader*/)) {
- more = false;
- return false;
- }
- }
- }
- else {
- // if we did not get a row and halt is true, clean off rows of result
- // success must be false - or else we could have just read off row and set
- // halt to true
- bool moreRows;
- if (!TryHasMoreRows(out moreRows)) {
- more = false;
- return false;
- }
- while (moreRows) {
- // if we are in SingleRow mode, and we've read the first row,
- // read the rest of the rows, if any
- while (_stateObj._pendingData && !_sharedState._dataReady) {
- if (!_parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out _sharedState._dataReady)) {
- more = false;
- return false;
- }
- }
- if (_sharedState._dataReady) {
- if (!TryCleanPartialRead()) {
- more = false;
- return false;
- }
- }
- // clear out our buffers
- SqlBuffer.Clear(_data);
- _sharedState._nextColumnHeaderToRead = 0;
- if (!TryHasMoreRows(out moreRows)) {
- more = false;
- return false;
- }
- }
- // reset haltRead
- _haltRead = false;
- }
- }
- else if (IsClosed) {
- throw ADP.DataReaderClosed("Read");
- }
- more = false;
- #if DEBUG
- if ((!_sharedState._dataReady) && (_stateObj._pendingData)) {
- byte token;
- if (!_stateObj.TryPeekByte(out token)) {
- return false;
- }
- Debug.Assert(TdsParser.IsValidTdsToken(token), string.Format("DataReady is false, but next token is invalid: {0,-2:X2}", token));
- }
- #endif
- return true;
- }
- #if DEBUG
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- SqlConnection con = _connection;
- if (con != null) {
- con.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- SqlConnection con = _connection;
- if (con != null) {
- con.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- SqlConnection con = _connection;
- if (con != null) {
- con.Abort(e);
- }
- throw;
- }
- finally {
- SqlStatistics.StopTimer(statistics);
- Bid.ScopeLeave(ref hscp);
- }
- }
- private void ReadColumn(int i, bool setTimeout = true, bool allowPartiallyReadColumn = false) {
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- bool result = TryReadColumn(i, setTimeout, allowPartiallyReadColumn);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- }
- private bool TryReadColumn(int i, bool setTimeout, bool allowPartiallyReadColumn = false) {
- CheckDataIsReady(columnIndex: i, permitAsync: true, allowPartiallyReadColumn: allowPartiallyReadColumn);
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #else
- {
- #endif //DEBUG
- Debug.Assert(_sharedState._nextColumnHeaderToRead <= _metaData.Length, "_sharedState._nextColumnHeaderToRead too large");
- Debug.Assert(_sharedState._nextColumnDataToRead <= _metaData.Length, "_sharedState._nextColumnDataToRead too large");
- if (setTimeout) {
- SetTimeout(_defaultTimeoutMilliseconds);
- }
-
- if (!TryReadColumnInternal(i, readHeaderOnly: false)) {
- return false;
- }
-
- Debug.Assert(null != _data[i], " data buffer is null?");
- }
- #if DEBUG
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- return true;
- }
- private bool TryReadColumnData() {
- // If we've already read the value (because it was NULL) we don't
- // bother to read here.
- if (!_data[_sharedState._nextColumnDataToRead].IsNull) {
- _SqlMetaData columnMetaData = _metaData[_sharedState._nextColumnDataToRead];
- if (!_parser.TryReadSqlValue(_data[_sharedState._nextColumnDataToRead], columnMetaData, (int)_sharedState._columnDataBytesRemaining, _stateObj,
- _command != null ? _command.ColumnEncryptionSetting : SqlCommandColumnEncryptionSetting.UseConnectionSetting,
- columnMetaData.column)) { // will read UDTs as VARBINARY.
- return false;
- }
- _sharedState._columnDataBytesRemaining = 0;
- }
- _sharedState._nextColumnDataToRead++;
- return true;
- }
- private void ReadColumnHeader(int i) {
- Debug.Assert(_stateObj == null || _stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
- bool result = TryReadColumnHeader(i);
- if (!result) { throw SQL.SynchronousCallMayNotPend(); }
- }
- private bool TryReadColumnHeader(int i) {
- if (!_sharedState._dataReady) {
- throw SQL.InvalidRead();
- }
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- #if DEBUG
- TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- tdsReliabilitySection.Start();
- #endif //DEBUG
- return TryReadColumnInternal(i, readHeaderOnly: true);
- #if DEBUG
- }
- finally {
- tdsReliabilitySection.Stop();
- }
- #endif //DEBUG
- }
- catch (System.OutOfMemoryException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.StackOverflowException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- catch (System.Threading.ThreadAbortException e) {
- _isClosed = true;
- if (null != _connection) {
- _connection.Abort(e);
- }
- throw;
- }
- }
- private bool TryReadColumnInternal(int i, bool readHeaderOnly = false) {
- AssertReaderState(requireData: true, permitAsync: true, columnIndex: i);
- // Check if we've already read the header already
- if (i < _sharedState._nextColumnHeaderToRead) {
- // Read the header, but we need to read the data
- if ((i == _sharedState._nextColumnDataToRead) && (!readHeaderOnly)) {
- return TryReadColumnData();
- }
- // Else we've already read the data, or we're reading the header only
- else {
- // Ensure that, if we've read past the column, then we did store its data
- Debug.Assert(i == _sharedState._nextColumnDataToRead || // Either we haven't read the column yet
- ((i + 1 < _sharedState._nextColumnDataToRead) && (IsCommandBehavior(CommandBehavior.SequentialAccess))) || // Or we're in sequential mode and we've read way past the column (i.e. it was not the last column we read)
- (!_data[i].IsEmpty || _data[i].IsNull) || // Or we should have data stored for the column (unless the column was null)
- (_metaData[i].type == SqlDbType.Timestamp), // Or Dev11 Bug #336820, Dev10 Bug #479607 (SqlClient: IsDBNull always returns false for timestamp datatype)
- // Due to a bug in TdsParser.GetNullSqlValue, Timestamps' IsNull is not correctly set - so we need to bypass the check
- "Gone past column, be we have no data stored for it");
- return true;
- }
- }
- Debug.Assert(_data[i].IsEmpty || _data[i].IsNull, "re-reading column value?");
-
- // If we're in sequential access mode, we can safely clear out any
- // data from the previous column.
- bool isSequentialAccess = IsCommandBehavior(CommandBehavior.SequentialAccess);
- if (isSequentialAccess) {
- if (0 < _sharedState._nextColumnDataToRead) {
- _data[_sharedState._nextColumnDataToRead - 1].Clear();
- }
- // Only wipe out the blob objects if they aren't for a 'future' column (i.e. we haven't read up to them yet)
- if ((_lastColumnWithDataChunkRead > -1) && (i > _lastColumnWithDataChunkRead)) {
- CloseActiveSequentialStreamAndTextReader();
- }
- }
- else if (_sharedState._nextColumnDataToRead < _sharedState._nextColumnHeaderToRead) {
- // We read the header but not the column for the previous column
- if (!TryReadColumnData()) {
- return false;
- }
- Debug.Assert(_sharedState._nextColumnDataToRead == _sharedState._nextColumnHeaderToRead);
- }
- // if we still have bytes left from the previous blob read, clear the wire and reset
- if (!TryResetBlobState()) {
- return false;
- }
- do {
- _SqlMetaData columnMetaData = _metaData[_sharedState._nextColumnHeaderToRead];
- if ((isSequentialAccess) && (_sharedState._nextColumnHeaderToRead < i)) {
- // SkipValue is no-op if the column appears in NBC bitmask
- // if not, it skips regular and PLP types
- if (!_parser.TrySkipValue(columnMetaData, _sharedState._nextColumnHeaderToRead, _stateObj)) {
- return false;
- }
- _sharedState._nextColumnDataToRead = _sharedState._nextColumnHeaderToRead;
- _sharedState._nextColumnHeaderToRead++;
- }
- else {
- bool isNull;
- ulong dataLength;
- if (!_parser.TryProcessColumnHeader(columnMetaData, _stateObj, _sharedState._nextColumnHeaderToRead, out isNull, out dataLength)) {
- return false;
- }
- _sharedState._nextColumnDataToRead = _sharedState._nextColumnHeaderToRead;
- _sharedState._nextColumnHeaderToRead++; // We read this one
- if (isNull && columnMetaData.type != SqlDbType.Timestamp /* Maintain behavior for known bug (Dev10 479607) rejected as breaking change - See comments in GetNullSqlValue for timestamp */)
- {
- TdsParser.GetNullSqlValue(_data[_sharedState._nextColumnDataToRead],
- columnMetaData,
- _command != null ? _command.ColumnEncryptionSetting : SqlCommandColumnEncryptionSetting.UseConnectionSetting,
- _parser.Connection);
-
- if (!readHeaderOnly) {
- _sharedState._nextColumnDataToRead++;
- }
- }
- else {
- if ((i > _sharedState._nextColumnDataToRead) || (!readHeaderOnly)) {
- // If we're not in sequential access mode, we have to
- // save the data we skip over so that the consumer
- // can read it out of order
- if (!_parser.TryReadSqlValue(_data[_sharedState._nextColumnDataToRead], columnMetaData, (int)dataLength, _stateObj,
- _command != null ? _command.ColumnEncryptionSetting : SqlCommandColumnEncryptionSetting.UseConnectionSetting,
- columnMetaData.column)) { // will read UDTs as VARBINARY.
- return false;
- }
- _sharedState._nextColumnDataToRead++;
- }
- else {
- _sharedState._columnDataBytesRemaining = (long)dataLength;
- }
- }
- }
- if (_snapshot != null) {
- // reset snapshot to save memory use. We can safely do that here because all SqlDataReader values are stable.
- // The retry logic can use the current values to get back to the right state.
- _snapshot = null;
- PrepareAsyncInvocation(useSnapshot: true);
- }
- } while (_sharedState._nextColumnHeaderToRead <= i);
- return true;
- }
- // Estimates if there is enough data available to read the number of columns requested
- private bool WillHaveEnoughData(int targetColumn, bool headerOnly = false) {
- AssertReaderState(requireData: true, permitAsync: true, columnIndex: targetColumn);
-
- if ((_lastColumnWithDataChunkRead == _sharedState._nextColumnDataToRead) && (_metaData[_lastColumnWithDataChunkRead].metaType.IsPlp)) {
- // In the middle of reading a Plp - no idea how much is left
- return false;
- }
- int bytesRemaining = Math.Min(checked(_stateObj._inBytesRead - _stateObj._inBytesUsed), _stateObj._inBytesPacket);
- // There are some parts of our code that peeks at the next token after doing its read
- // So we will make sure that there is always a spare byte for it to look at
- bytesRemaining--;
-
- if ((targetColumn >= _sharedState._nextColumnDataToRead) && (_sharedState._nextColumnDataToRead < _sharedState._nextColumnHeaderToRead)) {
- if (_sharedState._columnDataBytesRemaining > bytesRemaining) {
- // The current column needs more data than we currently have
- // NOTE: Since the Long data types (TEXT, IMAGE, NTEXT) can have a size of Int32.MaxValue we cannot simply subtract
- // _columnDataBytesRemaining from bytesRemaining and then compare it to zero as this may lead to an overflow
- return false;
- }
- else {
- // Already read the header, so subtract actual data size
- bytesRemaining = checked(bytesRemaining - (int)_sharedState._columnDataBytesRemaining);
- }
- }
-
- // For each column that we need to read, subtract the size of its header and the size of its data
- int currentColumn = _sharedState._nextColumnHeaderToRead;
- while ((bytesRemaining >= 0) && (currentColumn <= targetColumn)) {
- // Check NBC first
- if (!_stateObj.IsNullCompressionBitSet(currentColumn)) {
- // NOTE: This is mostly duplicated from TryProcessColumnHeaderNoNBC and TryGetTokenLength
- var metaType = _metaData[currentColumn].metaType;
- if ((metaType.IsLong) || (metaType.IsPlp) || (metaType.SqlDbType == SqlDbType.Udt) || (metaType.SqlDbType == SqlDbType.Structured)) {
- // Plp, Udt and TVP types have an unknownable size - so return that the estimate failed
- return false;
- }
- int maxHeaderSize;
- byte typeAndMask = (byte)(_metaData[currentColumn].tdsType & TdsEnums.SQLLenMask);
- if ((typeAndMask == TdsEnums.SQLVarLen) || (typeAndMask == TdsEnums.SQLVarCnt)) {
- if (0 != (_metaData[currentColumn].tdsType & 0x80)) {
- // UInt16 represents size
- maxHeaderSize = 2;
- }
- else if (0 == (_metaData[currentColumn].tdsType & 0x0c)) {
- // UInt32 represents size
- maxHeaderSize = 4;
- }
- else {
- // Byte represents size
- maxHeaderSize = 1;
- }
- }
- else
- {
- maxHeaderSize = 0;
- }
- bytesRemaining = checked(bytesRemaining - maxHeaderSize);
- if ((currentColumn < targetColumn) || (!headerOnly)) {
- bytesRemaining = checked(bytesRemaining - _metaData[currentColumn].length);
- }
- }
- currentColumn++;
- }
- return (bytesRemaining >= 0);
- }
- // clean remainder bytes for the column off the wire
- private bool TryResetBlobState() {
- Debug.Assert(null != _stateObj, "null state object"); // _parser may be null at this point
- AssertReaderState(requireData: true, permitAsync: true);
- Debug.Assert(_sharedState._nextColumnHeaderToRead <= _metaData.Length, "_sharedState._nextColumnHeaderToRead too large");
- // If we haven't already entirely read the column
- if (_sharedState._nextColumnDataToRead < _sharedState._nextColumnHeaderToRead) {
- if ((_sharedState._nextColumnHeaderToRead > 0) && (_metaData[_sharedState._nextColumnHeaderToRead - 1].metaType.IsPlp)) {
- if (_stateObj._longlen != 0) {
- ulong ignored;
- if (!_stateObj.Parser.TrySkipPlpValue(UInt64.MaxValue, _stateObj, out ignored)) {
- return false;
- }
- }
- if (_streamingXml != null) {
- SqlStreamingXml localSXml = _streamingXml;
- _streamingXml = null;
- localSXml.Close();
- }
- }
- else if (0 < _sharedState._columnDataBytesRemaining) {
- if (!_stateObj.TrySkipLongBytes(_sharedState._columnDataBytesRemaining)) {
- return false;
- }
- }
- }
- #if DEBUG
- else {
- Debug.Assert((_sharedState._columnDataBytesRemaining == 0 || _sharedState._columnDataBytesRemaining == -1) && _stateObj._longlen == 0, "Haven't read header yet, but column is partially read?");
- }
- #endif
- _sharedState._columnDataBytesRemaining = 0;
- _columnDataBytesRead = 0;
- _columnDataCharsRead = 0;
- _columnDataChars = null;
- _columnDataCharsIndex = -1;
- _stateObj._plpdecoder = null;
- return true;
- }
- private void CloseActiveSequentialStreamAndTextReader() {
- if (_currentStream != null) {
- _currentStream.SetClosed();
- _currentStream = null;
- }
- if (_currentTextReader != null) {
- _currentTextReader.SetClosed();
- _currentStream = null;
- }
- }
- private void RestoreServerSettings(TdsParser parser, TdsParserStateObject stateObj) {
- // turn off any set options
- if (null != parser && null != _resetOptionsString) {
- // It is possible for this to be called during connection close on a
- // broken connection, so check state first.
- if (parser.State == TdsParserState.OpenLoggedIn) {
- Bid.CorrelationTrace("<sc.SqlDataReader.RestoreServerSettings|Info|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID);
- Task executeTask = parser.TdsExecuteSQLBatch(_resetOptionsString, (_command != null) ? _command.CommandTimeout : 0, null, stateObj, sync: true);
- Debug.Assert(executeTask == null, "Shouldn't get a task when doing sync writes");
-
- // must execute this one synchronously as we can't retry
- parser.Run(RunBehavior.UntilDone, _command, this, null, stateObj);
- }
- _resetOptionsString = null;
- }
- }
- internal bool TrySetAltMetaDataSet(_SqlMetaDataSet metaDataSet, bool metaDataConsumed) {
- if (_altMetaDataSetCollection == null) {
- _altMetaDataSetCollection = new _SqlMetaDataSetCollection();
- }
- else if (_snapshot != null && object.ReferenceEquals(_snapshot._altMetaDataSetCollection, _altMetaDataSetCollection)) {
- _altMetaDataSetCollection = (_SqlMetaDataSetCollection)_altMetaDataSetCollection.Clone();
- }
- _altMetaDataSetCollection.SetAltMetaData(metaDataSet);
- _metaDataConsumed = metaDataConsumed;
- if (_metaDataConsumed && null != _parser) {
- byte b;
- if (!_stateObj.TryPeekByte(out b)) {
- return false;
- }
- if (TdsEnums.SQLORDER == b) {
- bool ignored;
- if (!_parser.TryRun(RunBehavior.ReturnImmediately, _command, this, null, _stateObj, out ignored)) {
- return false;
- }
- if (!_stateObj.TryPeekByte(out b)) {
- return false;
- }
- }
- if (b == TdsEnums.SQLINFO) {
- try {
- _stateObj._accumulateInfoEvents = true;
- bool ignored;
- if (!_parser.TryRun(RunBehavior.ReturnImmediately, _command, null, null, _stateObj, out ignored)) {
- return false;
- }
- }
- finally {
- _stateObj._accumulateInfoEvents = false;
- }
- if (!_stateObj.TryPeekByte(out b)) {
- return false;
- }
- }
- _hasRows = IsRowToken(b);
- }
- if (metaDataSet != null) {
- if (_data == null || _data.Length<metaDataSet.Length) {
- _data = SqlBuffer.CreateBufferArray(metaDataSet.Length);
- }
- }
- return true;
- }
- private void ClearMetaData() {
- _metaData = null;
- _tableNames = null;
- _fieldNameLookup = null;
- _metaDataConsumed = false;
- _browseModeInfoConsumed = false;
- }
- internal bool TrySetMetaData(_SqlMetaDataSet metaData, bool moreInfo) {
- _metaData = metaData;
- // get rid of cached metadata info as well
- _tableNames = null;
- if (_metaData != null) {
- _metaData.schemaTable = null;
- _data = SqlBuffer.CreateBufferArray(metaData.Length);
- }
- _fieldNameLookup = null;
- if (null != metaData) {
- // we are done consuming metadata only if there is no moreInfo
- if (!moreInfo) {
- _metaDataConsumed = true;
- if (_parser != null) { // There is a valid case where parser is null
- // Peek, and if row token present, set _hasRows true since there is a
- // row in the result
- byte b;
- if (!_stateObj.TryPeekByte(out b)) {
- return false;
- }
- //
- // simply rip the order token off the wire
- if (b == TdsEnums.SQLORDER) { // same logic as SetAltMetaDataSet
- // Devnote: That's not the right place to process TDS
- // Can this result in Reentrance to Run?
- //
- bool ignored;
- if (!_parser.TryRun(RunBehavior.ReturnImmediately, null, null, null, _stateObj, out ignored)) {
- return false;
- }
- if (!_stateObj.TryPeekByte(out b)) {
- return false;
- }
- }
- if (b == TdsEnums.SQLINFO)
- {
- // VSTFDEVDIV713926
- // We are accumulating informational events and fire them at next
- // TdsParser.Run purely to avoid breaking change
- try {
- _stateObj._accumulateInfoEvents = true;
- bool ignored;
- if (!_parser.TryRun(RunBehavior.ReturnImmediately, null, null, null, _stateObj, out ignored)) {
- return false;
- }
- }
- finally {
- _stateObj._accumulateInfoEvents = false;
- }
- if (!_stateObj.TryPeekByte(out b)) {
- return false;
- }
- }
- _hasRows = IsRowToken(b);
- if (TdsEnums.SQLALTMETADATA == b)
- {
- _metaDataConsumed = false;
- }
- }
- }
- }
- else {
- _metaDataConsumed = false;
- }
- _browseModeInfoConsumed = false;
- return true;
- }
- private void SetTimeout(long timeoutMilliseconds) {
- // WebData 111653,112003 -- we now set timeouts per operation, not
- // per command (it's not supposed to be a cumulative per command).
- TdsParserStateObject stateObj = _stateObj;
- if (null != stateObj) {
- stateObj.SetTimeoutMilliseconds(timeoutMilliseconds);
- }
- }
- private bool HasActiveStreamOrTextReaderOnColumn(int columnIndex) {
- bool active = false;
- active |= ((_currentStream != null) && (_currentStream.ColumnIndex == columnIndex));
- active |= ((_currentTextReader != null) && (_currentTextReader.ColumnIndex == columnIndex));
- return active;
- }
- private void CheckMetaDataIsReady() {
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- if (MetaData == null) {
- throw SQL.InvalidRead();
- }
- }
- private void CheckMetaDataIsReady(int columnIndex, bool permitAsync = false) {
- if ((!permitAsync) && (_currentTask != null)) {
- throw ADP.AsyncOperationPending();
- }
- if (MetaData == null) {
- throw SQL.InvalidRead();
- }
- if ((columnIndex < 0) || (columnIndex >= _metaData.Length)) {
- throw ADP.IndexOutOfRange();
- }
- }
- private void CheckDataIsReady() {
- if (_currentTask != null) {
- throw ADP.AsyncOperationPending();
- }
- Debug.Assert(!_sharedState._dataReady || _metaData != null, "Data is ready, but there is no metadata?");
- if ((!_sharedState._dataReady) || (_metaData == null)) {
- throw SQL.InvalidRead();
- }
- }
-
- private void CheckHeaderIsReady(int columnIndex, bool permitAsync = false, string methodName = null) {
- if (_isClosed) {
- throw ADP.DataReaderClosed(methodName ?? "CheckHeaderIsReady");
- }
- if ((!permitAsync) && (_currentTask != null)) {
- throw ADP.AsyncOperationPending();
- }
- Debug.Assert(!_sharedState._dataReady || _metaData != null, "Data is ready, but there is no metadata?");
- if ((!_sharedState._dataReady) || (_metaData == null)) {
- throw SQL.InvalidRead();
- }
- if ((columnIndex < 0) || (columnIndex >= _metaData.Length)) {
- throw ADP.IndexOutOfRange();
- }
- if ((IsCommandBehavior(CommandBehavior.SequentialAccess)) && // Only for sequential access
- ((_sharedState._nextColumnHeaderToRead > columnIndex + 1) || (_lastColumnWithDataChunkRead > columnIndex))) { // Read past column
- throw ADP.NonSequentialColumnAccess(columnIndex, Math.Max(_sharedState._nextColumnHeaderToRead - 1, _lastColumnWithDataChunkRead));
- }
- }
- private void CheckDataIsReady(int columnIndex, bool allowPartiallyReadColumn = false, bool permitAsync = false, string methodName = null) {
- if (_isClosed) {
- throw ADP.DataReaderClosed(methodName ?? "CheckDataIsReady");
- }
- if ((!permitAsync) && (_currentTask != null)) {
- throw ADP.AsyncOperationPending();
- }
- Debug.Assert(!_sharedState._dataReady || _metaData != null, "Data is ready, but there is no metadata?");
- if ((!_sharedState._dataReady) || (_metaData == null)) {
- throw SQL.InvalidRead();
- }
- if ((columnIndex < 0) || (columnIndex >= _metaData.Length)) {
- throw ADP.IndexOutOfRange();
- }
- if ((IsCommandBehavior(CommandBehavior.SequentialAccess)) && // Only for sequential access
- ((_sharedState._nextColumnDataToRead > columnIndex) || (_lastColumnWithDataChunkRead > columnIndex) || // Read past column
- ((!allowPartiallyReadColumn) && (_lastColumnWithDataChunkRead == columnIndex)) || // Partially read column
- ((allowPartiallyReadColumn) && (HasActiveStreamOrTextReaderOnColumn(columnIndex))))) { // Has a Stream or TextReader on a partially-read column
- throw ADP.NonSequentialColumnAccess(columnIndex, Math.Max(_sharedState._nextColumnDataToRead, _lastColumnWithDataChunkRead + 1));
- }
- }
- [Conditional("DEBUG")]
- private void AssertReaderState(bool requireData, bool permitAsync, int? columnIndex = null, bool enforceSequentialAccess = false) {
- Debug.Assert(!_sharedState._dataReady || _metaData != null, "Data is ready, but there is no metadata?");
- Debug.Assert(permitAsync || _currentTask == null, "Call while async operation is pending");
- Debug.Assert(_metaData != null, "_metaData is null, check MetaData before calling this method");
- Debug.Assert(!requireData || _sharedState._dataReady, "No data is ready to be read");
- if (columnIndex.HasValue) {
- Debug.Assert(columnIndex.Value >= 0 && columnIndex.Value < _metaData.Length, "Invalid column index");
- Debug.Assert((!enforceSequentialAccess) || (!IsCommandBehavior(CommandBehavior.SequentialAccess)) || ((_sharedState._nextColumnDataToRead <= columnIndex) && (_lastColumnWithDataChunkRead <= columnIndex)), "Already read past column");
- }
- }
- public override Task<bool> NextResultAsync(CancellationToken cancellationToken) {
- IntPtr hscp;
- Bid.ScopeEnter(out hscp, "<sc.SqlDataReader.NextResultAsync|API> %d#", ObjectID);
- try {
- TaskCompletionSource<bool> source = new TaskCompletionSource<bool>();
- if (IsClosed) {
- source.SetException(ADP.ExceptionWithStackTrace(ADP.DataReaderClosed("NextResultAsync")));
- return source.Task;
- }
- IDisposable registration = null;
- if (cancellationToken.CanBeCanceled) {
- if (cancellationToken.IsCancellationRequested) {
- source.SetCanceled();
- return source.Task;
- }
- registration = cancellationToken.Register(_command.CancelIgnoreFailure);
- }
- Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null);
- if (original != null) {
- source.SetException(ADP.ExceptionWithStackTrace(SQL.PendingBeginXXXExists()));
- return source.Task;
- }
- // Check if cancellation due to close is requested (this needs to be done after setting _currentTask)
- if (_cancelAsyncOnCloseToken.IsCancellationRequested) {
- source.SetCanceled();
- _currentTask = null;
- return source.Task;
- }
- PrepareAsyncInvocation(useSnapshot: true);
- Func<Task, Task<bool>> moreFunc = null;
- moreFunc = (t) => {
- if (t != null) {
- Bid.Trace("<sc.SqlDataReader.NextResultAsync> attempt retry %d#\n", ObjectID);
- PrepareForAsyncContinuation();
- }
- bool more;
- if (TryNextResult(out more)) {
- // completed
- return more ? ADP.TrueTask : ADP.FalseTask;
- }
- return ContinueRetryable(moreFunc);
- };
- return InvokeRetryable(moreFunc, source, registration);
- }
- finally {
- Bid.ScopeLeave(ref hscp);
- }
- }
- // NOTE: This will return null if it completed sequentially
- // If this returns null, then you can use bytesRead to see how many bytes were read - otherwise bytesRead should be ignored
- internal Task<int> GetBytesAsync(int i, byte[] buffer, int index, int length, int timeout, CancellationToken cancellationToken, out int bytesRead) {
- AssertReaderState(requireData: true, permitAsync: true, columnIndex: i, enforceSequentialAccess: true);
- Debug.Assert(IsCommandBehavior(CommandBehavior.SequentialAccess));
- bytesRead = 0;
- if (IsClosed) {
- TaskCompletionSource<int> source = new TaskCompletionSource<int>();
- source.SetException(ADP.ExceptionWithStackTrace(ADP.DataReaderClosed("GetBytesAsync")));
- return source.Task;
- }
- if (_currentTask != null) {
- TaskCompletionSource<int> source = new TaskCompletionSource<int>();
- source.SetException(ADP.ExceptionWithStackTrace(ADP.AsyncOperationPending()));
- return source.Task;
- }
-
- if (cancellationToken.CanBeCanceled) {
- if (cancellationToken.IsCancellationRequested) {
- return null;
- }
- }
- // Check if we need to skip columns
- Debug.Assert(_sharedState._nextColumnDataToRead <= _lastColumnWithDataChunkRead, "Non sequential access");
- if ((_sharedState._nextColumnHeaderToRead <= _lastColumnWithDataChunkRead) || (_sharedState._nextColumnDataToRead < _lastColumnWithDataChunkRead)) {
- TaskCompletionSource<int> source = new TaskCompletionSource<int>();
- Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null);
- if (original != null) {
- source.SetException(ADP.ExceptionWithStackTrace(ADP.AsyncOperationPending()));
- return source.Task;
- }
- PrepareAsyncInvocation(useSnapshot: true);
- Func<Task, Task<int>> moreFunc = null;
- // Timeout
- CancellationToken timeoutToken = CancellationToken.None;
- CancellationTokenSource timeoutCancellationSource = null;
- if (timeout > 0) {
- timeoutCancellationSource = new CancellationTokenSource();
- timeoutCancellationSource.CancelAfter(timeout);
- timeoutToken = timeoutCancellationSource.Token;
- }
- moreFunc = (t) => {
- if (t != null) {
- Bid.Trace("<sc.SqlDataReader.GetBytesAsync> attempt retry %d#\n", ObjectID);
- PrepareForAsyncContinuation();
- }
- // Prepare for stateObj timeout
- SetTimeout(_defaultTimeoutMilliseconds);
- if (TryReadColumnHeader(i)) {
- // Only once we have read upto where we need to be can we check the cancellation tokens (otherwise we will be in an unknown state)
- if (cancellationToken.IsCancellationRequested) {
- // User requested cancellation
- return ADP.CreatedTaskWithCancellation<int>();
- }
- else if (timeoutToken.IsCancellationRequested) {
- // Timeout
- return ADP.CreatedTaskWithException<int>(ADP.ExceptionWithStackTrace(ADP.IO(SQLMessage.Timeout())));
- }
- else {
- // Upto the correct column - continue to read
- SwitchToAsyncWithoutSnapshot();
- int totalBytesRead;
- var readTask = GetBytesAsyncReadDataStage(i, buffer, index, length, timeout, true, cancellationToken, timeoutToken, out totalBytesRead);
- if (readTask == null) {
- // Completed synchronously
- return Task.FromResult<int>(totalBytesRead);
- }
- else {
- return readTask;
- }
- }
- }
- else {
- return ContinueRetryable(moreFunc);
- }
- };
- return InvokeRetryable(moreFunc, source, timeoutCancellationSource);
- }
- else {
- // We're already at the correct column, just read the data
- // Switch to async
- PrepareAsyncInvocation(useSnapshot: false);
- try {
- return GetBytesAsyncReadDataStage(i, buffer, index, length, timeout, false, cancellationToken, CancellationToken.None, out bytesRead);
- }
- catch {
- CleanupAfterAsyncInvocation();
- throw;
- }
- }
- }
- private Task<int> GetBytesAsyncReadDataStage(int i, byte[] buffer, int index, int length, int timeout, bool isContinuation, CancellationToken cancellationToken, CancellationToken timeoutToken, out int bytesRead) {
- _lastColumnWithDataChunkRead = i;
- TaskCompletionSource<int> source = null;
- CancellationTokenSource timeoutCancellationSource = null;
-
- // Prepare for stateObj timeout
- SetTimeout(_defaultTimeoutMilliseconds);
- // Try to read without any continuations (all the data may already be in the stateObj's buffer)
- if (!TryGetBytesInternalSequential(i, buffer, index, length, out bytesRead)) {
- // This will be the 'state' for the callback
- int totalBytesRead = bytesRead;
- if (!isContinuation) {
- // This is the first async operation which is happening - setup the _currentTask and timeout
- source = new TaskCompletionSource<int>();
- Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null);
- if (original != null) {
- source.SetException(ADP.ExceptionWithStackTrace(ADP.AsyncOperationPending()));
- return source.Task;
- }
- // Check if cancellation due to close is requested (this needs to be done after setting _currentTask)
- if (_cancelAsyncOnCloseToken.IsCancellationRequested) {
- source.SetCanceled();
- _currentTask = null;
- return source.Task;
- }
- // Timeout
- Debug.Assert(timeoutToken == CancellationToken.None, "TimeoutToken is set when GetBytesAsyncReadDataStage is not a continuation");
- if (timeout > 0) {
- timeoutCancellationSource = new CancellationTokenSource();
- timeoutCancellationSource.CancelAfter(timeout);
- timeoutToken = timeoutCancellationSource.Token;
- }
- }
-
- Func<Task, Task<int>> moreFunc = null;
- moreFunc = (_ => {
- PrepareForAsyncContinuation();
- if (cancellationToken.IsCancellationRequested) {
- // User requested cancellation
- return ADP.CreatedTaskWithCancellation<int>();
- }
- else if (timeoutToken.IsCancellationRequested) {
- // Timeout
- return ADP.CreatedTaskWithException<int>(ADP.ExceptionWithStackTrace(ADP.IO(SQLMessage.Timeout())));
- }
- else {
- // Prepare for stateObj timeout
- SetTimeout(_defaultTimeoutMilliseconds);
- int bytesReadThisIteration;
- bool result = TryGetBytesInternalSequential(i, buffer, index + totalBytesRead, length - totalBytesRead, out bytesReadThisIteration);
- totalBytesRead += bytesReadThisIteration;
- Debug.Assert(totalBytesRead <= length, "Read more bytes than required");
- if (result) {
- return Task.FromResult<int>(totalBytesRead);
- }
- else {
- return ContinueRetryable(moreFunc);
- }
- }
- });
- Task<int> retryTask = ContinueRetryable(moreFunc);
- if (isContinuation) {
- // Let the caller handle cleanup\completing
- return retryTask;
- }
- else {
- // setup for cleanup\completing
- retryTask.ContinueWith((t) => CompleteRetryable(t, source, timeoutCancellationSource), TaskScheduler.Default);
- return source.Task;
- }
- }
- if (!isContinuation) {
- // If this is the first async op, we need to cleanup
- CleanupAfterAsyncInvocation();
- }
- // Completed synchronously, return null
- return null;
- }
- public override Task<bool> ReadAsync(CancellationToken cancellationToken) {
- IntPtr hscp;
- Bid.ScopeEnter(out hscp, "<sc.SqlDataReader.ReadAsync|API> %d#", ObjectID);
- try {
- if (IsClosed) {
- return ADP.CreatedTaskWithException<bool>(ADP.ExceptionWithStackTrace(ADP.DataReaderClosed("ReadAsync")));
- }
- // If user's token is canceled, return a canceled task
- if (cancellationToken.IsCancellationRequested) {
- return ADP.CreatedTaskWithCancellation<bool>();
- }
- // Check for existing async
- if (_currentTask != null) {
- return ADP.CreatedTaskWithException<bool>(ADP.ExceptionWithStackTrace(SQL.PendingBeginXXXExists()));
- }
-
- // These variables will be captured in moreFunc so that we can skip searching for a row token once one has been read
- bool rowTokenRead = false;
- bool more = false;
- // Shortcut, do we have enough data to immediately do the ReadAsync?
- try {
- // First, check if we can finish reading the current row
- // NOTE: If we are in SingleRow mode and we've read that single row (i.e. _haltRead == true), then skip the shortcut
- if ((!_haltRead) && ((!_sharedState._dataReady) || (WillHaveEnoughData(_metaData.Length - 1)))) {
- #if DEBUG
- try {
- _stateObj._shouldHaveEnoughData = true;
- #endif
- if (_sharedState._dataReady) {
- // Clean off current row
- CleanPartialReadReliable();
- }
- // If there a ROW token ready (as well as any metadata for the row)
- if (_stateObj.IsRowTokenReady()) {
- // Read the ROW token
- bool result = TryReadInternal(true, out more);
- Debug.Assert(result, "Should not have run out of data");
- rowTokenRead = true;
- if (more) {
- // Sequential mode, nothing left to do
- if (IsCommandBehavior(CommandBehavior.SequentialAccess)) {
- return ADP.TrueTask;
- }
- // For non-sequential, check if we can read the row data now
- else if (WillHaveEnoughData(_metaData.Length - 1)) {
- // Read row data
- result = TryReadColumn(_metaData.Length - 1, setTimeout: true);
- Debug.Assert(result, "Should not have run out of data");
- return ADP.TrueTask;
- }
- }
- else {
- // No data left, return
- return ADP.FalseTask;
- }
- }
- #if DEBUG
- }
- finally {
- _stateObj._shouldHaveEnoughData = false;
- }
- #endif
- }
- }
- catch (Exception ex) {
- if (!ADP.IsCatchableExceptionType(ex)) {
- throw;
- }
- return ADP.CreatedTaskWithException<bool>(ex);
- }
- TaskCompletionSource<bool> source = new TaskCompletionSource<bool>();
- Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null);
- if (original != null) {
- source.SetException(ADP.ExceptionWithStackTrace(SQL.PendingBeginXXXExists()));
- return source.Task;
- }
- // Check if cancellation due to close is requested (this needs to be done after setting _currentTask)
- if (_cancelAsyncOnCloseToken.IsCancellationRequested) {
- source.SetCanceled();
- _currentTask = null;
- return source.Task;
- }
- IDisposable registration = null;
- if (cancellationToken.CanBeCanceled) {
- registration = cancellationToken.Register(_command.CancelIgnoreFailure);
- }
- PrepareAsyncInvocation(useSnapshot: true);
- Func<Task, Task<bool>> moreFunc = null;
- moreFunc = (t) => {
- if (t != null) {
- Bid.Trace("<sc.SqlDataReader.ReadAsync> attempt retry %d#\n", ObjectID);
- PrepareForAsyncContinuation();
- }
- if (rowTokenRead || TryReadInternal(true, out more)) {
- // If there are no more rows, or this is Sequential Access, then we are done
- if (!more || (_commandBehavior & CommandBehavior.SequentialAccess) == CommandBehavior.SequentialAccess) {
- // completed
- return more ? ADP.TrueTask : ADP.FalseTask;
- }
- else {
- // First time reading the row token - update the snapshot
- if (!rowTokenRead) {
- rowTokenRead = true;
- _snapshot = null;
- PrepareAsyncInvocation(useSnapshot: true);
- }
- // if non-sequentialaccess then read entire row before returning
- if (TryReadColumn(_metaData.Length - 1, true)) {
- // completed
- return ADP.TrueTask;
- }
- }
- }
- return ContinueRetryable(moreFunc);
- };
- return InvokeRetryable(moreFunc, source, registration);
- }
- finally {
- Bid.ScopeLeave(ref hscp);
- }
- }
- override public Task<bool> IsDBNullAsync(int i, CancellationToken cancellationToken) {
- try {
- CheckHeaderIsReady(columnIndex: i, methodName: "IsDBNullAsync");
- }
- catch (Exception ex) {
- if (!ADP.IsCatchableExceptionType(ex)) {
- throw;
- }
- return ADP.CreatedTaskWithException<bool>(ex);
- }
- // Shortcut - if there are no issues and the data is already read, then just return the value
- if ((_sharedState._nextColumnHeaderToRead > i) && (!cancellationToken.IsCancellationRequested) && (_currentTask == null)) {
- var data = _data;
- if (data != null) {
- return data[i].IsNull ? ADP.TrueTask : ADP.FalseTask;
- }
- else {
- // Reader was closed between the CheckHeaderIsReady and accessing _data - throw closed exception
- return ADP.CreatedTaskWithException<bool>(ADP.ExceptionWithStackTrace(ADP.DataReaderClosed("IsDBNullAsync")));
- }
- }
- else {
- // Throw if there is any current task
- if (_currentTask != null) {
- return ADP.CreatedTaskWithException<bool>(ADP.ExceptionWithStackTrace(ADP.AsyncOperationPending()));
- }
-
- // If user's token is canceled, return a canceled task
- if (cancellationToken.IsCancellationRequested) {
- return ADP.CreatedTaskWithCancellation<bool>();
- }
-
- // Shortcut - if we have enough data, then run [....]
- try {
- if (WillHaveEnoughData(i, headerOnly: true)) {
- #if DEBUG
- try {
- _stateObj._shouldHaveEnoughData = true;
- #endif
- ReadColumnHeader(i);
- return _data[i].IsNull ? ADP.TrueTask : ADP.FalseTask;
- #if DEBUG
- }
- finally {
- _stateObj._shouldHaveEnoughData = false;
- }
- #endif
- }
- }
- catch (Exception ex) {
- if (!ADP.IsCatchableExceptionType(ex)) {
- throw;
- }
- return ADP.CreatedTaskWithException<bool>(ex);
- }
-
- // Setup and check for pending task
- TaskCompletionSource<bool> source = new TaskCompletionSource<bool>();
- Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null);
- if (original != null) {
- source.SetException(ADP.ExceptionWithStackTrace(ADP.AsyncOperationPending()));
- return source.Task;
- }
- // Check if cancellation due to close is requested (this needs to be done after setting _currentTask)
- if (_cancelAsyncOnCloseToken.IsCancellationRequested) {
- source.SetCanceled();
- _currentTask = null;
- return source.Task;
- }
- // Setup cancellations
- IDisposable registration = null;
- if (cancellationToken.CanBeCanceled) {
- registration = cancellationToken.Register(_command.CancelIgnoreFailure);
- }
- // Setup async
- PrepareAsyncInvocation(useSnapshot: true);
- // Setup the retryable function
- Func<Task, Task<bool>> moreFunc = null;
- moreFunc = (t) => {
- if (t != null) {
- PrepareForAsyncContinuation();
- }
- if (TryReadColumnHeader(i)) {
- return _data[i].IsNull ? ADP.TrueTask : ADP.FalseTask;
- }
- else {
- return ContinueRetryable(moreFunc);
- }
- };
- // Go!
- return InvokeRetryable(moreFunc, source, registration);
- }
- }
- override public Task<T> GetFieldValueAsync<T>(int i, CancellationToken cancellationToken) {
- try {
- CheckDataIsReady(columnIndex: i, methodName: "GetFieldValueAsync");
- // Shortcut - if there are no issues and the data is already read, then just return the value
- if ((!IsCommandBehavior(CommandBehavior.SequentialAccess)) && (_sharedState._nextColumnDataToRead > i) && (!cancellationToken.IsCancellationRequested) && (_currentTask == null)) {
- var data = _data;
- var metaData =_metaData;
- if ((data != null) && (metaData != null)) {
- return Task.FromResult<T>(GetFieldValueFromSqlBufferInternal<T>(data[i], metaData[i]));
- }
- else {
- // Reader was closed between the CheckDataIsReady and accessing _data\_metaData - throw closed exception
- return ADP.CreatedTaskWithException<T>(ADP.ExceptionWithStackTrace(ADP.DataReaderClosed("GetFieldValueAsync")));
- }
- }
- } catch (Exception ex) {
- if (!ADP.IsCatchableExceptionType(ex)) {
- throw;
- }
- return ADP.CreatedTaskWithException<T>(ex);
- }
- // Throw if there is any current task
- if (_currentTask != null) {
- return ADP.CreatedTaskWithException<T>(ADP.ExceptionWithStackTrace(ADP.AsyncOperationPending()));
- }
- // If user's token is canceled, return a canceled task
- if (cancellationToken.IsCancellationRequested) {
- return ADP.CreatedTaskWithCancellation<T>();
- }
- // Shortcut - if we have enough data, then run [....]
- try {
- if (WillHaveEnoughData(i)) {
- #if DEBUG
- try {
- _stateObj._shouldHaveEnoughData = true;
- #endif
- return Task.FromResult(GetFieldValueInternal<T>(i));
- #if DEBUG
- }
- finally {
- _stateObj._shouldHaveEnoughData = false;
- }
- #endif
- }
- }
- catch (Exception ex) {
- if (!ADP.IsCatchableExceptionType(ex)) {
- throw;
- }
- return ADP.CreatedTaskWithException<T>(ex);
- }
- // Setup and check for pending task
- TaskCompletionSource<T> source = new TaskCompletionSource<T>();
- Task original = Interlocked.CompareExchange(ref _currentTask, source.Task, null);
- if (original != null) {
- source.SetException(ADP.ExceptionWithStackTrace(ADP.AsyncOperationPending()));
- return source.Task;
- }
- // Check if cancellation due to close is requested (this needs to be done after setting _currentTask)
- if (_cancelAsyncOnCloseToken.IsCancellationRequested) {
- source.SetCanceled();
- _currentTask = null;
- return source.Task;
- }
- // Setup cancellations
- IDisposable registration = null;
- if (cancellationToken.CanBeCanceled) {
- registration = cancellationToken.Register(_command.CancelIgnoreFailure);
- }
- // Setup async
- PrepareAsyncInvocation(useSnapshot: true);
- // Setup the retryable function
- Func<Task, Task<T>> moreFunc = null;
- moreFunc = (t) => {
- if (t != null) {
- PrepareForAsyncContinuation();
- }
- if (TryReadColumn(i, setTimeout: false)) {
- return Task.FromResult<T>(GetFieldValueFromSqlBufferInternal<T>(_data[i], _metaData[i]));
- }
- else {
- return ContinueRetryable(moreFunc);
- }
- };
- // Go!
- return InvokeRetryable(moreFunc, source, registration);
- }
- #if DEBUG
- internal void CompletePendingReadWithSuccess(bool resetForcePendingReadsToWait) {
- var stateObj = _stateObj;
- if (stateObj != null) {
- stateObj.CompletePendingReadWithSuccess(resetForcePendingReadsToWait);
- }
- }
- internal void CompletePendingReadWithFailure(int errorCode, bool resetForcePendingReadsToWait) {
- var stateObj = _stateObj;
- if (stateObj != null) {
- stateObj.CompletePendingReadWithFailure(errorCode, resetForcePendingReadsToWait);
- }
- }
- #endif
- class Snapshot {
- public bool _dataReady;
- public bool _haltRead;
- public bool _metaDataConsumed;
- public bool _browseModeInfoConsumed;
- public bool _hasRows;
- public ALTROWSTATUS _altRowStatus;
- public int _nextColumnDataToRead;
- public int _nextColumnHeaderToRead;
- public long _columnDataBytesRead;
- public long _columnDataBytesRemaining;
- public _SqlMetaDataSet _metadata;
- public _SqlMetaDataSetCollection _altMetaDataSetCollection;
- public MultiPartTableName[] _tableNames;
- public SqlSequentialStream _currentStream;
- public SqlSequentialTextReader _currentTextReader;
- }
- private Task<T> ContinueRetryable<T>(Func<Task, Task<T>> moreFunc) {
- // _networkPacketTaskSource could be null if the connection was closed
- // while an async invocation was outstanding.
- TaskCompletionSource<object> completionSource = _stateObj._networkPacketTaskSource;
- if (_cancelAsyncOnCloseToken.IsCancellationRequested || completionSource == null) {
- // Cancellation requested due to datareader being closed
- TaskCompletionSource<T> source = new TaskCompletionSource<T>();
- source.TrySetException(ADP.ExceptionWithStackTrace(ADP.ClosedConnectionError()));
- return source.Task;
- }
- else {
- return completionSource.Task.ContinueWith((retryTask) => {
- if (retryTask.IsFaulted) {
- // Somehow the network task faulted - return the exception
- TaskCompletionSource<T> exceptionSource = new TaskCompletionSource<T>();
- exceptionSource.TrySetException(retryTask.Exception.InnerException);
- return exceptionSource.Task;
- }
- else if (!_cancelAsyncOnCloseToken.IsCancellationRequested) {
- TdsParserStateObject stateObj = _stateObj;
- if (stateObj != null) {
- // protect continuations against concurrent
- // close and cancel
- lock (stateObj) {
- if (_stateObj != null) { // reader not closed while we waited for the lock
- if (retryTask.IsCanceled) {
- if (_parser != null) {
- _parser.State = TdsParserState.Broken; // We failed to respond to attention, we have to quit!
- _parser.Connection.BreakConnection();
- _parser.ThrowExceptionAndWarning(_stateObj);
- }
- }
- else {
- if (!IsClosed) {
- try {
- return moreFunc(retryTask);
- }
- catch (Exception) {
- CleanupAfterAsyncInvocation();
- throw;
- }
- }
- }
- }
- }
- }
- }
- // if stateObj is null, or we closed the connection or the connection was already closed,
- // then mark this operation as cancelled.
- TaskCompletionSource<T> source = new TaskCompletionSource<T>();
- source.SetException(ADP.ExceptionWithStackTrace(ADP.ClosedConnectionError()));
- return source.Task;
- }, TaskScheduler.Default).Unwrap();
- }
- }
- private Task<T> InvokeRetryable<T>(Func<Task, Task<T>> moreFunc, TaskCompletionSource<T> source, IDisposable objectToDispose = null) {
- try {
- Task<T> task;
- try {
- task = moreFunc(null);
- }
- catch (Exception ex) {
- task = ADP.CreatedTaskWithException<T>(ex);
- }
- if (task.IsCompleted) {
- // If we've completed [....], then don't bother handling the TaskCompletionSource - we'll just return the completed task
- CompleteRetryable(task, source, objectToDispose);
- return task;
- }
- else {
- task.ContinueWith((t) => CompleteRetryable(t, source, objectToDispose), TaskScheduler.Default);
- }
- }
- catch (AggregateException e) {
- source.TrySetException(e.InnerException);
- }
- catch (Exception e) {
- source.TrySetException(e);
- }
- // Fall through for exceptions\completing async
- return source.Task;
- }
-
- private void CompleteRetryable<T>(Task<T> task, TaskCompletionSource<T> source, IDisposable objectToDispose) {
- if (objectToDispose != null) {
- objectToDispose.Dispose();
- }
- // If something has forced us to switch to SyncOverAsync mode while in an async task then we need to guarantee that we do the cleanup
- // This avoids us replaying non-replayable data (such as DONE or ENV_CHANGE tokens)
- var stateObj = _stateObj;
- bool ignoreCloseToken = ((stateObj != null) && (stateObj._syncOverAsync));
- CleanupAfterAsyncInvocation(ignoreCloseToken);
- Task current = Interlocked.CompareExchange(ref _currentTask, null, source.Task);
- Debug.Assert(current == source.Task, "Should not be able to change the _currentTask while an asynchronous operation is pending");
- if (task.IsFaulted) {
- Exception e = task.Exception.InnerException;
- source.TrySetException(e);
- }
- else if (task.IsCanceled) {
- source.TrySetCanceled();
- }
- else {
- source.TrySetResult(task.Result);
- }
- }
- private void PrepareAsyncInvocation(bool useSnapshot) {
- // if there is already a snapshot, then the previous async command
- // completed with exception or cancellation. We need to continue
- // with the old snapshot.
- if (useSnapshot) {
- Debug.Assert(!_stateObj._asyncReadWithoutSnapshot, "Can't prepare async invocation with snapshot if doing async without snapshots");
- if (_snapshot == null) {
- _snapshot = new Snapshot {
- _dataReady = _sharedState._dataReady,
- _haltRead = _haltRead,
- _metaDataConsumed = _metaDataConsumed,
- _browseModeInfoConsumed = _browseModeInfoConsumed,
- _hasRows = _hasRows,
- _altRowStatus = _altRowStatus,
- _nextColumnDataToRead = _sharedState._nextColumnDataToRead,
- _nextColumnHeaderToRead = _sharedState._nextColumnHeaderToRead,
- _columnDataBytesRead = _columnDataBytesRead,
- _columnDataBytesRemaining = _sharedState._columnDataBytesRemaining,
- // _metadata and _altaMetaDataSetCollection must be Cloned
- // before they are updated
- _metadata = _metaData,
- _altMetaDataSetCollection = _altMetaDataSetCollection,
- _tableNames = _tableNames,
-
- _currentStream = _currentStream,
- _currentTextReader = _currentTextReader,
- };
- _stateObj.SetSnapshot();
- }
- }
- else {
- Debug.Assert(_snapshot == null, "Can prepare async invocation without snapshot if there is currently a snapshot");
- _stateObj._asyncReadWithoutSnapshot = true;
- }
- _stateObj._syncOverAsync = false;
- _stateObj._executionContext = ExecutionContext.Capture();
- }
- private void CleanupAfterAsyncInvocation(bool ignoreCloseToken = false) {
- var stateObj = _stateObj;
- if (stateObj != null) {
- // If close requested cancellation and we have a snapshot, then it will deal with cleaning up
- // NOTE: There are some cases where we wish to ignore the close token, such as when we've read some data that is not replayable (e.g. DONE or ENV_CHANGE token)
- if ((ignoreCloseToken) || (!_cancelAsyncOnCloseToken.IsCancellationRequested) || (stateObj._asyncReadWithoutSnapshot)) {
- // Prevent race condition between the DataReader being closed (e.g. when another MARS thread has an error)
- lock(stateObj) {
- if (_stateObj != null) { // reader not closed while we waited for the lock
- CleanupAfterAsyncInvocationInternal(_stateObj);
- Debug.Assert(_snapshot == null && !_stateObj._asyncReadWithoutSnapshot, "Snapshot not null or async without snapshot still enabled after cleaning async state");
- }
- }
- }
- }
- }
- // This function is called directly if calling function already closed the reader, so _stateObj is null,
- // in other cases parameterless version should be called
- private void CleanupAfterAsyncInvocationInternal(TdsParserStateObject stateObj, bool resetNetworkPacketTaskSource = true)
- {
- if (resetNetworkPacketTaskSource) {
- stateObj._networkPacketTaskSource = null;
- }
- stateObj.ResetSnapshot();
- stateObj._syncOverAsync = true;
- stateObj._executionContext = null;
- stateObj._asyncReadWithoutSnapshot = false;
- #if DEBUG
- stateObj._permitReplayStackTraceToDiffer = false;
- #endif
- // We are setting this to null inside the if-statement because stateObj==null means that the reader hasn't been initialized or has been closed (either way _snapshot should already be null)
- _snapshot = null;
- }
- private void PrepareForAsyncContinuation() {
- Debug.Assert(((_snapshot != null) || (_stateObj._asyncReadWithoutSnapshot)), "Can not prepare for an async continuation if no async if setup");
- if (_snapshot != null) {
- _sharedState._dataReady = _snapshot._dataReady;
- _haltRead = _snapshot._haltRead;
- _metaDataConsumed = _snapshot._metaDataConsumed;
- _browseModeInfoConsumed = _snapshot._browseModeInfoConsumed;
- _hasRows = _snapshot._hasRows;
- _altRowStatus = _snapshot._altRowStatus;
- _sharedState._nextColumnDataToRead = _snapshot._nextColumnDataToRead;
- _sharedState._nextColumnHeaderToRead = _snapshot._nextColumnHeaderToRead;
- _columnDataBytesRead = _snapshot._columnDataBytesRead;
- _sharedState._columnDataBytesRemaining = _snapshot._columnDataBytesRemaining;
- _metaData = _snapshot._metadata;
- _altMetaDataSetCollection = _snapshot._altMetaDataSetCollection;
- _tableNames = _snapshot._tableNames;
- _currentStream = _snapshot._currentStream;
- _currentTextReader = _snapshot._currentTextReader;
- _stateObj.PrepareReplaySnapshot();
- }
- _stateObj._executionContext = ExecutionContext.Capture();
- }
- private void SwitchToAsyncWithoutSnapshot() {
- Debug.Assert(_snapshot != null, "Should currently have a snapshot");
- Debug.Assert(_stateObj != null && !_stateObj._asyncReadWithoutSnapshot, "Already in async without snapshot");
- _snapshot = null;
- _stateObj.ResetSnapshot();
- _stateObj._asyncReadWithoutSnapshot = true;
- }
- }// SqlDataReader
- }// namespace
|