' <fileinfo name="Northwind_Base.vb">
'       <copyright>
'           All rights reserved.
'       </copyright>
'       <remarks>
'           Do not change this source code manually. Changes to this file may 
'           cause incorrect behavior and will be lost if the code is regenerated.
'       </remarks>
'       <generator rewritefile="True" infourl="http://www.SharpPower.com">RapTier</generator>
' </fileinfo>

Option Strict Off
Option Explicit On

Imports System
Imports System.Data

''' <summary>
''' The base class for the <see cref="Northwind"/> class that 
''' represents a connection to the <c>Northwind</c> database. 
''' </summary>
''' <remarks>
''' Do not change this source code. Modify the Northwind class
''' if you need to add or change some functionality.
''' </remarks>
Public MustInherit Class Northwind_Base
        Implements IDisposable
    Private _connection As IDbConnection
    Private _transaction As IDbTransaction
    Private _storedProcedures As StoredProcedures

    ' Table fields
    Private _alphabeticallistofproducts As AlphabeticallistofproductsCollection
    Private _categories As CategoriesCollection
    Private _categorySalesfor1997 As CategorySalesfor1997Collection
    Private _currentProductList As CurrentProductListCollection
    Private _customerandSuppliersbyCity As CustomerandSuppliersbyCityCollection
    Private _customerCustomerDemo As CustomerCustomerDemoCollection
    Private _customerDemographics As CustomerDemographicsCollection
    Private _customers As CustomersCollection
    Private _employees As EmployeesCollection
    Private _employeeTerritories As EmployeeTerritoriesCollection
    Private _invoices As InvoicesCollection
    Private _orderDetails As OrderDetailsCollection
    Private _orderDetailsExtended As OrderDetailsExtendedCollection
    Private _orderSubtotals As OrderSubtotalsCollection
    Private _orders As OrdersCollection
    Private _ordersQry As OrdersQryCollection
    Private _productSalesfor1997 As ProductSalesfor1997Collection
    Private _products As ProductsCollection
    Private _productsAboveAveragePrice As ProductsAboveAveragePriceCollection
    Private _productsbyCategory As ProductsbyCategoryCollection
    Private _quarterlyOrders As QuarterlyOrdersCollection
    Private _region As RegionCollection
    Private _salesbyCategory As SalesbyCategoryCollection
    Private _salesTotalsbyAmount As SalesTotalsbyAmountCollection
    Private _shippers As ShippersCollection
    Private _summaryofSalesbyQuarter As SummaryofSalesbyQuarterCollection
    Private _summaryofSalesbyYear As SummaryofSalesbyYearCollection
    Private _suppliers As SuppliersCollection
    Private _territories As TerritoriesCollection

    ''' <summary>
    ''' Initializes a new instance of the <see cref="Northwind_Base"/> 
    ''' class and opens the database connection.
    ''' </summary>
    Protected Sub New()
        MyClass.New(True)
    End Sub
    
    ''' <summary>
    ''' Initializes a new instance of the <see cref="Northwind_Base"/> class.
    ''' </summary>
    ''' <param name="init">Specifies whether the constructor calls the
    ''' <see cref="InitConnection"/> method to initialize the database connection.</param>
    Protected Sub New(init As Boolean)
        MyBase.New()
        If init Then
            InitConnection()
        End If
    End Sub

    ''' <summary>
    ''' Initializes the database connection.
    ''' </summary>
    Protected Sub InitConnection()
        _connection = CreateConnection()
        _connection.Open()
    End Sub

    ''' <summary>
    ''' Creates a new connection to the database.
    ''' </summary>
    ''' <returns>A reference to the <see cref="System.Data.IDbConnection"/> object.</returns>
    Protected MustOverride Function CreateConnection() As IDbConnection

    ''' <summary>
    ''' Returns a SQL statement parameter name that is specific for the data provider.
    ''' For example it returns ? for OleDb provider, or @paramName for MS SQL provider.
    ''' </summary>
    ''' <param name="paramName">The data provider neutral SQL parameter name.</param>
    ''' <returns>The SQL statement parameter name.</returns>
    Protected Friend MustOverride Function CreateSqlParameterName(paramName As String) As String

    ''' <summary>
    ''' Creates <see cref="System.Data.IDataReader"/> for the specified DB command.
    ''' </summary>
    ''' <param name="command">The <see cref="System.Data.IDbCommand"/> object.</param>
    ''' <returns>A reference to the <see cref="System.Data.IDataReader"/> object.</returns>
    Protected Friend Overridable Function ExecuteReader(command As IDbCommand) As IDataReader
        Return command.ExecuteReader()
    End Function

    ''' <summary>
    ''' Adds a new parameter to the specified command. It is not recommended that 
    ''' you use this method directly from your custom code. Instead use the 
    ''' <c>AddParameter</c> method of the <TableCodeName>Collection_Base classes.
    ''' </summary>
    ''' <param name="cmd">The <see cref="System.Data.IDbCommand"/> object to add the parameter to.</param>
    ''' <param name="paramName">The name of the parameter.</param>
    ''' <param name="dbType">One of the <see cref="System.Data.DbType"/> values. </param>
    ''' <param name="value">The value of the parameter.</param>
    ''' <returns>A reference to the added parameter.</returns>
    Friend Function AddParameter(cmd As IDbCommand, paramName As String, _
                                    dbType As DbType, value As Object) As IDbDataParameter
        Dim parameter As IDbDataParameter = cmd.CreateParameter()
        parameter.ParameterName = CreateCollectionParameterName(paramName)
        parameter.DbType = dbType
        If value Is Nothing Then
            parameter.Value = DBNull.Value
        Else
            parameter.Value = value
        End If
        cmd.Parameters.Add(parameter)
        return parameter
    End Function

    ''' <summary>
    ''' Creates a .Net data provider specific name that is used by the 
    ''' <see cref="AddParameter"/> method.
    ''' </summary>
    ''' <param name="baseParamName">The base name of the parameter.</param>
    ''' <returns>The full data provider specific parameter name.</returns>
    Protected MustOverride Function CreateCollectionParameterName(baseParamName As String) As String

    ''' <summary>
    ''' Gets <see cref="System.Data.IDbConnection"/> associated with this object.
    ''' </summary>
    ''' <value>A reference to the <see cref="System.Data.IDbConnection"/> object.</value>
    Public ReadOnly Property Connection As IDbConnection
        Get
            Return _connection
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Alphabetical list of products</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="AlphabeticallistofproductsCollection"/> object.</value>
    Public ReadOnly Property AlphabeticallistofproductsCollection As AlphabeticallistofproductsCollection
        Get
            If _alphabeticallistofproducts Is Nothing Then
                _alphabeticallistofproducts = new AlphabeticallistofproductsCollection(CType(Me, Northwind))
            End If
            Return _alphabeticallistofproducts
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Categories</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="CategoriesCollection"/> object.</value>
    Public ReadOnly Property CategoriesCollection As CategoriesCollection
        Get
            If _categories Is Nothing Then
                _categories = new CategoriesCollection(CType(Me, Northwind))
            End If
            Return _categories
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Category Sales for 1997</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="CategorySalesfor1997Collection"/> object.</value>
    Public ReadOnly Property CategorySalesfor1997Collection As CategorySalesfor1997Collection
        Get
            If _categorySalesfor1997 Is Nothing Then
                _categorySalesfor1997 = new CategorySalesfor1997Collection(CType(Me, Northwind))
            End If
            Return _categorySalesfor1997
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Current Product List</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="CurrentProductListCollection"/> object.</value>
    Public ReadOnly Property CurrentProductListCollection As CurrentProductListCollection
        Get
            If _currentProductList Is Nothing Then
                _currentProductList = new CurrentProductListCollection(CType(Me, Northwind))
            End If
            Return _currentProductList
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Customer and Suppliers by City</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="CustomerandSuppliersbyCityCollection"/> object.</value>
    Public ReadOnly Property CustomerandSuppliersbyCityCollection As CustomerandSuppliersbyCityCollection
        Get
            If _customerandSuppliersbyCity Is Nothing Then
                _customerandSuppliersbyCity = new CustomerandSuppliersbyCityCollection(CType(Me, Northwind))
            End If
            Return _customerandSuppliersbyCity
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>CustomerCustomerDemo</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="CustomerCustomerDemoCollection"/> object.</value>
    Public ReadOnly Property CustomerCustomerDemoCollection As CustomerCustomerDemoCollection
        Get
            If _customerCustomerDemo Is Nothing Then
                _customerCustomerDemo = new CustomerCustomerDemoCollection(CType(Me, Northwind))
            End If
            Return _customerCustomerDemo
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>CustomerDemographics</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="CustomerDemographicsCollection"/> object.</value>
    Public ReadOnly Property CustomerDemographicsCollection As CustomerDemographicsCollection
        Get
            If _customerDemographics Is Nothing Then
                _customerDemographics = new CustomerDemographicsCollection(CType(Me, Northwind))
            End If
            Return _customerDemographics
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Customers</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="CustomersCollection"/> object.</value>
    Public ReadOnly Property CustomersCollection As CustomersCollection
        Get
            If _customers Is Nothing Then
                _customers = new CustomersCollection(CType(Me, Northwind))
            End If
            Return _customers
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Employees</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="EmployeesCollection"/> object.</value>
    Public ReadOnly Property EmployeesCollection As EmployeesCollection
        Get
            If _employees Is Nothing Then
                _employees = new EmployeesCollection(CType(Me, Northwind))
            End If
            Return _employees
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>EmployeeTerritories</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="EmployeeTerritoriesCollection"/> object.</value>
    Public ReadOnly Property EmployeeTerritoriesCollection As EmployeeTerritoriesCollection
        Get
            If _employeeTerritories Is Nothing Then
                _employeeTerritories = new EmployeeTerritoriesCollection(CType(Me, Northwind))
            End If
            Return _employeeTerritories
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Invoices</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="InvoicesCollection"/> object.</value>
    Public ReadOnly Property InvoicesCollection As InvoicesCollection
        Get
            If _invoices Is Nothing Then
                _invoices = new InvoicesCollection(CType(Me, Northwind))
            End If
            Return _invoices
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Order Details</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="OrderDetailsCollection"/> object.</value>
    Public ReadOnly Property OrderDetailsCollection As OrderDetailsCollection
        Get
            If _orderDetails Is Nothing Then
                _orderDetails = new OrderDetailsCollection(CType(Me, Northwind))
            End If
            Return _orderDetails
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Order Details Extended</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="OrderDetailsExtendedCollection"/> object.</value>
    Public ReadOnly Property OrderDetailsExtendedCollection As OrderDetailsExtendedCollection
        Get
            If _orderDetailsExtended Is Nothing Then
                _orderDetailsExtended = new OrderDetailsExtendedCollection(CType(Me, Northwind))
            End If
            Return _orderDetailsExtended
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Order Subtotals</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="OrderSubtotalsCollection"/> object.</value>
    Public ReadOnly Property OrderSubtotalsCollection As OrderSubtotalsCollection
        Get
            If _orderSubtotals Is Nothing Then
                _orderSubtotals = new OrderSubtotalsCollection(CType(Me, Northwind))
            End If
            Return _orderSubtotals
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Orders</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="OrdersCollection"/> object.</value>
    Public ReadOnly Property OrdersCollection As OrdersCollection
        Get
            If _orders Is Nothing Then
                _orders = new OrdersCollection(CType(Me, Northwind))
            End If
            Return _orders
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Orders Qry</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="OrdersQryCollection"/> object.</value>
    Public ReadOnly Property OrdersQryCollection As OrdersQryCollection
        Get
            If _ordersQry Is Nothing Then
                _ordersQry = new OrdersQryCollection(CType(Me, Northwind))
            End If
            Return _ordersQry
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Product Sales for 1997</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="ProductSalesfor1997Collection"/> object.</value>
    Public ReadOnly Property ProductSalesfor1997Collection As ProductSalesfor1997Collection
        Get
            If _productSalesfor1997 Is Nothing Then
                _productSalesfor1997 = new ProductSalesfor1997Collection(CType(Me, Northwind))
            End If
            Return _productSalesfor1997
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Products</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="ProductsCollection"/> object.</value>
    Public ReadOnly Property ProductsCollection As ProductsCollection
        Get
            If _products Is Nothing Then
                _products = new ProductsCollection(CType(Me, Northwind))
            End If
            Return _products
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Products Above Average Price</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="ProductsAboveAveragePriceCollection"/> object.</value>
    Public ReadOnly Property ProductsAboveAveragePriceCollection As ProductsAboveAveragePriceCollection
        Get
            If _productsAboveAveragePrice Is Nothing Then
                _productsAboveAveragePrice = new ProductsAboveAveragePriceCollection(CType(Me, Northwind))
            End If
            Return _productsAboveAveragePrice
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Products by Category</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="ProductsbyCategoryCollection"/> object.</value>
    Public ReadOnly Property ProductsbyCategoryCollection As ProductsbyCategoryCollection
        Get
            If _productsbyCategory Is Nothing Then
                _productsbyCategory = new ProductsbyCategoryCollection(CType(Me, Northwind))
            End If
            Return _productsbyCategory
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Quarterly Orders</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="QuarterlyOrdersCollection"/> object.</value>
    Public ReadOnly Property QuarterlyOrdersCollection As QuarterlyOrdersCollection
        Get
            If _quarterlyOrders Is Nothing Then
                _quarterlyOrders = new QuarterlyOrdersCollection(CType(Me, Northwind))
            End If
            Return _quarterlyOrders
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Region</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="RegionCollection"/> object.</value>
    Public ReadOnly Property RegionCollection As RegionCollection
        Get
            If _region Is Nothing Then
                _region = new RegionCollection(CType(Me, Northwind))
            End If
            Return _region
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Sales by Category</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="SalesbyCategoryCollection"/> object.</value>
    Public ReadOnly Property SalesbyCategoryCollection As SalesbyCategoryCollection
        Get
            If _salesbyCategory Is Nothing Then
                _salesbyCategory = new SalesbyCategoryCollection(CType(Me, Northwind))
            End If
            Return _salesbyCategory
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Sales Totals by Amount</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="SalesTotalsbyAmountCollection"/> object.</value>
    Public ReadOnly Property SalesTotalsbyAmountCollection As SalesTotalsbyAmountCollection
        Get
            If _salesTotalsbyAmount Is Nothing Then
                _salesTotalsbyAmount = new SalesTotalsbyAmountCollection(CType(Me, Northwind))
            End If
            Return _salesTotalsbyAmount
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Shippers</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="ShippersCollection"/> object.</value>
    Public ReadOnly Property ShippersCollection As ShippersCollection
        Get
            If _shippers Is Nothing Then
                _shippers = new ShippersCollection(CType(Me, Northwind))
            End If
            Return _shippers
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Summary of Sales by Quarter</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="SummaryofSalesbyQuarterCollection"/> object.</value>
    Public ReadOnly Property SummaryofSalesbyQuarterCollection As SummaryofSalesbyQuarterCollection
        Get
            If _summaryofSalesbyQuarter Is Nothing Then
                _summaryofSalesbyQuarter = new SummaryofSalesbyQuarterCollection(CType(Me, Northwind))
            End If
            Return _summaryofSalesbyQuarter
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Summary of Sales by Year</c> view.
    ''' </summary>
    ''' <value>A reference to the <see cref="SummaryofSalesbyYearCollection"/> object.</value>
    Public ReadOnly Property SummaryofSalesbyYearCollection As SummaryofSalesbyYearCollection
        Get
            If _summaryofSalesbyYear Is Nothing Then
                _summaryofSalesbyYear = new SummaryofSalesbyYearCollection(CType(Me, Northwind))
            End If
            Return _summaryofSalesbyYear
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Suppliers</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="SuppliersCollection"/> object.</value>
    Public ReadOnly Property SuppliersCollection As SuppliersCollection
        Get
            If _suppliers Is Nothing Then
                _suppliers = new SuppliersCollection(CType(Me, Northwind))
            End If
            Return _suppliers
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that represents the <c>Territories</c> table.
    ''' </summary>
    ''' <value>A reference to the <see cref="TerritoriesCollection"/> object.</value>
    Public ReadOnly Property TerritoriesCollection As TerritoriesCollection
        Get
            If _territories Is Nothing Then
                _territories = new TerritoriesCollection(CType(Me, Northwind))
            End If
            Return _territories
        End Get
    End Property

    ''' <summary>
    ''' Gets an object that wraps the database stored procedures.
    ''' </summary>
    ''' <value>A reference to the <see cref="StoredProcedures"/> object.</value>
    Public ReadOnly Property StoredProcedures As StoredProcedures
        Get
            If _storedProcedures Is Nothing Then
                _storedProcedures = new StoredProcedures(CType(Me, Northwind))
            End If
            return _storedProcedures
        End Get
    End Property

    ''' <summary>
    ''' Begins a new database transaction.
    ''' </summary>
    ''' <seealso cref="CommitTransaction"/>
    ''' <seealso cref="RollbackTransaction"/>
    ''' <returns>An object representing the new transaction.</returns>
    Public Function BeginTransaction() As IDbTransaction
        CheckTransactionState(false)
        _transaction = _connection.BeginTransaction()
        Return _transaction
    End Function

    ''' <summary>
    ''' Begins a new database transaction with the specified 
    ''' transaction isolation level.
    ''' <seealso cref="CommitTransaction"/>
    ''' <seealso cref="RollbackTransaction"/>
    ''' </summary>
    ''' <param name="isolationLevel">The transaction isolation level.</param>
    ''' <returns>An object representing the new transaction.</returns>
    Public Function BeginTransaction(isolationLevel As IsolationLevel) As IDbTransaction
        CheckTransactionState(false)
        _transaction = _connection.BeginTransaction(isolationLevel)
        Return _transaction
    End Function

    ''' <summary>
    ''' Commits the current database transaction.
    ''' <seealso cref="BeginTransaction"/>
    ''' <seealso cref="RollbackTransaction"/>
    ''' </summary>
    Public Sub CommitTransaction()
        CheckTransactionState(true)
        _transaction.Commit()
        _transaction = Nothing
    End Sub

    ''' <summary>
    ''' Rolls back the current transaction from a pending state.
    ''' <seealso cref="BeginTransaction"/>
    ''' <seealso cref="CommitTransaction"/>
    ''' </summary>
    Public Sub RollbackTransaction()
        CheckTransactionState(true)
        _transaction.Rollback()
        _transaction = Nothing
    End Sub

    ' Checks the state of the current transaction
    Private Sub CheckTransactionState(mustBeOpen As Boolean)
        If mustBeOpen Then
            If _transaction Is Nothing Then
                Throw New InvalidOperationException("Transaction is not open.")
            End If
        Else
            If Not (_transaction Is Nothing) Then
                Throw New InvalidOperationException("Transaction is already open.")
            End If
        End If
    End Sub

    ''' <summary>
    ''' Creates and returns a new <see cref="System.Data.IDbCommand"/> object.
    ''' </summary>
    ''' <param name="sqlText">The text of the query.</param>
    ''' <returns>An <see cref="System.Data.IDbCommand"/> object.</returns>
    Friend Function CreateCommand(sqlText As String) As IDbCommand
        Return CreateCommand(sqlText, false)
    End Function

    ''' <summary>
    ''' Creates and returns a new <see cref="System.Data.IDbCommand"/> object.
    ''' </summary>
    ''' <param name="sqlText">The text of the query.</param>
    ''' <param name="procedure">Specifies whether the sqlText parameter is 
    ''' the name of a stored procedure.</param>
    ''' <returns>An <see cref="System.Data.IDbCommand"/> object.</returns>
    Friend Function CreateCommand(sqlText As String, procedure As Boolean) As IDbCommand
        Dim cmd As IDbCommand = _connection.CreateCommand()
        cmd.CommandText = sqlText
        cmd.Transaction = _transaction
        If procedure Then
            cmd.CommandType = CommandType.StoredProcedure
        End If
        Return cmd
    End Function

    ''' <summary>
    ''' Rolls back any pending transactions and closes the DB connection.
    ''' An application can call the <c>Close</c> method more than
    ''' one time without generating an exception.
    ''' </summary>
    Public Overridable Sub Close()
        If Not _connection Is Nothing Then
            _connection.Close()
        End If
    End Sub

    ''' <summary>
    ''' Rolls back any pending transactions and closes the DB connection.
    ''' </summary>
    Public Overridable Sub Dispose() Implements IDisposable.Dispose
        Close()
        If Not _connection Is Nothing Then
            _connection.Dispose()
        End If
    End Sub
End Class