| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672 |
- //------------------------------------------------------------------------------
- // <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.
- 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.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.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 (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 (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;
- 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.
- 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((IDataReader)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.
- 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.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.
- 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");
- // 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 (_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 = _metaData[i].metaType;
- 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))) {
- 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();
- }
-
- try {
- statistics = SqlStatistics.StartTimer(Statistics);
- SetTimeout(_defaultTimeoutMilliseconds);
- if ((_metaData[i].metaType.IsPlp) &&
- (IsCommandBehavior(CommandBehavior.SequentialAccess)) ) {
- if (length < 0) {
- throw ADP.InvalidDataLength(length);
- }
- // 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 ( _metaData[i].type == 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)) { // 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 */)
- {
- _parser.GetNullSqlValue(_data[_sharedState._nextColumnDataToRead], columnMetaData);
-
- 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)) { // 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
|