using System;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data;
using System.Data.SqlClient;


class BData: ISqlReader, ISqlRecord
{
	// the previous number in the sequence
	int previous_;
	// the current number in the sequence
	int current_;
	int count_ ;
	int maxCount_;

	internal BData(int preStart, int start, int count)
	{
		previous_ = preStart;
		current_ = start;
		maxCount_ = count;
		this.count_ = 0;
	}
	int this[int pos]
	{
		get
		{
			if (pos == 0)
			{
				return current_;
			}

			return previous_;
		}
	}
	bool Closed = false;
	public int RecordsAffected
	{
		get
		{
			// Elided:  BData.RecordsAffected getter implementation
			return 0;
		}
	}
	public int Depth
	{
		get
		{
			// Elided:  BData.Depth getter implementation
			return 0;
		}
	}
	public bool NextResult()
	{
		// Elided:  BData.NextResult implementation
		throw new NotImplementedException();
	}
	public DataTable GetSchemaTable()
	{
		// Elided:  BData.GetSchemaTable implementation
		return null;
	}
	public bool IsClosed
	{
		get
		{
			// Elided:  BData.IsClosed getter implementation
			return this.Closed;
		}
	}
	public void Close()
	{
		Closed = true;
	}
	public bool HasRows
	{
		get
		{
			if (maxCount_ > 0)
			{
				return true;
			}
			return false;
		}
	}
	public bool Read()
	{
		// just leave things as they are
		// for the first time though
		if (count_ != 0)
		{
			int p = current_;
			current_ += previous_;
			previous_ = p;
		}
		// move to next number in sequence
		count_ = count_ + 1;
		if (count_ > maxCount_)
		{
			// done with sequence
			return false;
		}
		
		return true;
	}



	public void Dispose()
	{
		// Elided:  BData.Dispose implementation
	}



	public Type GetFieldType(int i)
	{
		switch (i)
		{
			case 0 :
				return typeof(int);
			
			case 1 :
				return typeof(int);

			default :
				return Type.EmptyTypes[0];
		}
	}
	public int GetSqlValues(object[] values)
	{
		values = new object[2];
		values[0] = this[0];
		values[1] = this[1];
		return 2;
	}
	public int GetOrdinal(string name)
	{
		if (name == "next")
		{
			return 0;
		}
		if (name == "previous")
		{
			return 1;
		}

		return 0;
	}
	public object this[string name]
	{
		get
		{
			if (name.ToLower() == "next")
			{
				return this[0];
			}
			if (name.ToLower() == "previous")
			{
				return this[1];
			}

			return null;
		}
	}
	object System.Data.Sql.ISqlRecord.this[int i]
	{
		get
		{
			if (i == 0)
			{
				return current_;
			}
			if (i == 1)
			{
				return previous_;
			}
			return null;
		}
	}
	public string GetDataTypeName(int i)
	{
		if (i == 0)
		{
			return current_.GetType().ToString();
		}
		if (i == 1)
		{
			return previous_.GetType().ToString();
		}
		return null;
	}
	public int FieldCount
	{
		get
		{
			return 2;
		}
	}
	public string GetName(int i)
	{
		if (i == 0)
		{
			return "next";
		}
		if (i == 1)
		{
			return "previous";
		}

		return null;
	}
	public int GetValues(object[] values)
	{
		values = new object[2];
		values[0] = this[0];
		values[1] = this[1];
		return 1;
	}


	public SqlBytes GetSqlBytes(int i)
	{
		return SqlBytes.Null;
	}
	public SqlChars GetSqlChars(int i)
	{
		return SqlChars.Null;
	}
	public SqlInt64 GetSqlInt64(int i)
	{
		return new SqlInt64(this[i]);
	}
	public SqlGuid GetSqlGuid(int i)
	{
		// Elided:  BData.GetSqlGuid implementation
		return new SqlGuid();
	}
	public SqlMoney GetSqlMoney(int i)
	{
		// Elided:  BData.GetSqlMoney implementation
		return new SqlMoney(current_);
	}
	public SqlBoolean GetSqlBoolean(int i)
	{
		// Elided:  BData.GetSqlBoolean implementation
		return new SqlBoolean();
	}
	public SqlByte GetSqlByte(int i)
	{
		// Elided:  BData.GetSqlByte implementation
		return new SqlByte();
	}
	public SqlInt16 GetSqlInt16(int i)
	{
		// Elided:  BData.GetSqlInt16 implementation
		return new SqlInt16((short)this[i]);
	}
	public SqlBinary GetSqlBinary(int i)
	{
		// Elided:  BData.GetSqlBinary implementation
		return new SqlBinary();
	}
	public SqlDouble GetSqlDouble(int i)
	{
		// Elided:  BData.GetSqlDouble implementation
		return new SqlDouble(this[i]);
	}
	public SqlInt32 GetSqlInt32(int i)
	{
		return new SqlInt32(this[i]);
	}
	public SqlString GetSqlString(int i)
	{
		return new SqlString(this[i].ToString());
	}
	public SqlChars GetSqlCharsRef(int i)
	{
		return SqlChars.Null;
	}
	public SqlSingle GetSqlSingle(int i)
	{
		return new SqlSingle(this[i]);
	}
	public SqlXml GetSqlXml(int i)
	{
		return new System.Data.SqlTypes.SqlXml((System.IO.Stream)null);
	}
	public SqlMetaData GetSqlMetaData(int i)
	{
		SqlMetaData md =  new SqlMetaData("Data", SqlDbType.Int);
		return md;
	}
	public SqlBytes GetSqlBytesRef(int i)
	{
		return SqlBytes.Null;
	}
	public SqlDateTime GetSqlDateTime(int i)
	{
		return new SqlDateTime();
	}
	public SqlDecimal GetSqlDecimal(int i)
	{
		return new SqlDecimal(this[i]);
	}
	public object GetSqlValue(int i)
	{
		return this[i];
	}

	public char GetChar(int i)
	{
		// Elided:  BData.GetChar implementation
		return '\0';
	}
	public short GetInt16(int i)
	{
		// Elided:  BData.GetInt16 implementation
		return (Int16)this[i];
	}
	public Guid GetGuid(int i)
	{
		// Elided:  BData.GetGuid implementation
		return new Guid();
	}
	public DateTime GetDateTime(int i)
	{
		// Elided:  BData.GetDateTime implementation
		return new DateTime();
	}
	public bool GetBoolean(int i)
	{
		// Elided:  BData.GetBoolean implementation
		return false;
	}
	public float GetFloat(int i)
	{
		// Elided:  BData.GetFloat implementation
		return this[i];
	}
	public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
	{
		// Elided:  BData.GetChars implementation
		return 0;
	}
	public object GetValue(int i)
	{
		// Elided:  BData.GetValue implementation
		return this[i];
	}
	public int GetInt32(int i)
	{
		// Elided:  BData.GetInt32 implementation
		return this[i];
	}
	public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
	{
		// Elided:  BData.GetBytes implementation
		return 0;
	}
	public bool IsSetAsDefault(int i)
	{
		// Elided:  BData.IsSetAsDefault implementation
		return false;
	}
	public object GetObjectRef(int i)
	{
		// Elided:  BData.GetObjectRef implementation
		return null;
	}
	public long GetInt64(int i)
	{
		// Elided:  BData.GetInt64 implementation
		return this[i];
	}
	public decimal GetDecimal(int i)
	{
		// Elided:  BData.GetDecimal implementation
		return this[i];
	}
	public double GetDouble(int i)
	{
		// Elided:  BData.GetDouble implementation
		return this[i];
	}
	public byte GetByte(int i)
	{
		// Elided:  BData.GetByte implementation
		return (byte)this[i];
	}
	public string GetString(int i)
	{
		// Elided:  BData.GetString implementation
		return this[i].ToString();
	}
	public bool IsDBNull(int i)
	{
		// Elided:  BData.IsDBNull implementation
		return false;
	}

}


public class CBernoulli
{
	[SqlFunction(DataAccess = DataAccessKind.None,
		SystemDataAccess = SystemDataAccessKind.None, 
		IsDeterministic = true,
		IsPrecise = true)]
    public static ISqlReader Bernoulli(int preStart, int start, int count)
    {
		BData b = new BData(preStart,
					  start,
					  count);
		ISqlReader dr = b;
        // Put your code here
		return dr;
    }
};
