Simulating SQL Server (Part 2)

If you haven’t read it already, why not start with Simulating SQL Server (Part 1)?

Last time we looked at creating a RecordSetDefinition  as a collection of ColumnDefinition s.  Once we had a way of defining what a recordset looks like, we then were able to create an ObjectRecord .

The next step is to collect those ObectRecord s together into a record set implementation.  For this I’ve added and ObjectSet .



Interestingly the BCL don’t expose a ‘recordset’ independently, instead, it’s always accessed through an IDataReader .  We want to be able to manipulate our recordsets though, so I added an IObjectSet  interface, and we can use the collection operators to make our sets, including Collection Initializers.

As we can create records independently of a record set (again great for testing a method that accepts an IDataRecord) then we need to be sure that any records we add to our record set implement the same RecordSetDefinition .  The above example adds two records, the second of which is random, of course, we can add and remove objects as we like –

But in many cases, we may not care about the actual data (or we want to test how well it copes with random data).  We already showed how to create a random record, so what we need is a fully random record set, and thankfully I added one of them too-

Couldn’t be easier – using the information from the recordSetDefinition  this creates between 0 and 1000 rows of random data.  There are lots of useful optional parameters here as well –

Which compares quite favourably to the full signature for creating a record –

Two useful parameters here include nullProbability  which determines how likely nulls will appear in columns (that are defined as allowing nulls) and columnGenerators  which allow a function to optionally be specified for columns that you want to generate specific values for (e.g. lookup out of an array). An example is shown below.

Now we have a recordset, it’s only used in an IDataReader , so for this, we create another Collection, ObjectReader  which implements IDataReader  as well as ICollection<IObjectSet> . Again we can use Collection Initializers

We now have a reader with one record set in it but more, can be added, e.g. a random set –

And finally, we can use this reader just like a normal data reader –

Again the full project can be viewed on GitHub or use the download link below to see the complete project.  Next time we will look in more detail at the other functions of the ObjectReader, and also we’ll attempt to simulate realistic SQL Server errors.

Comments 2

Leave a Reply

%d bloggers like this: