Guides/OfflineDriver-example.md
... ...
@@ -0,0 +1,197 @@
1
+#Offline Driver
2
+
3
+[[_TOC_]]
4
+
5
+##creating the driver
6
+
7
+In order to implement a driver that operates completely independent of a database, it is necessary to override the following methods at a minimum:
8
+
9
+* GetAllTables
10
+* GetColumns
11
+* GetDataSet
12
+* DetectVersion
13
+* CheckConnection
14
+* GetAllStoredProcedures
15
+
16
+This will allow Izenda to perform the operations it needs to in order to get database schema information and produce datasets in a completely custom manner. Below is an example of how one might override these to create a driver that does not connect to a database.
17
+
18
+##C♯ Example
19
+
20
+```csharp
21
+using System;
22
+using System.Collections.Generic;
23
+using Izenda.AdHoc;
24
+using Izenda.AdHoc.Database;
25
+using System.Data;
26
+
27
+[Serializable]
28
+public class OfflineDriver : MSSQLDriver
29
+{
30
+ Dictionary<string, Type> colTypes = new Dictionary<string, Type>()
31
+ { {"[dbo].[Fruits].[Price]", typeof(double)},
32
+ {"[dbo].[Drinks].[Price]", typeof(double)},
33
+ {"[dbo].[Fruits].[Name]", typeof(string)},
34
+ {"[dbo].[Drinks].[Name]", typeof(string)},
35
+ {"[dbo].[Fruits].[Color]", typeof(string)},
36
+ {"[dbo].[Drinks].[Alcohol]", typeof(string)}};
37
+
38
+ Dictionary<string, object[]> colData = new Dictionary<string, object[]>()
39
+ { {"[dbo].[Fruits].[Price]", new object[] {1.24, 2.61, 1.38, 3.21}},
40
+ {"[dbo].[Drinks].[Price]", new object[] {9.56, 7.11, 9.41, 7.42}},
41
+ {"[dbo].[Fruits].[Name]", new object[] {"Apple", "Pear", "Plum", "Apricot"}},
42
+ {"[dbo].[Drinks].[Name]", new object[] {"Beer", "Tea", "Whiskey", "Coffee"}},
43
+ {"[dbo].[Fruits].[Color]", new object[] {"Green", "Yellow", "Black", "Orange"}},
44
+ {"[dbo].[Drinks].[Alcohol]", new string[] {"Yes", "No", "Yes", "No"}}};
45
+
46
+ public override Table[] GetAllTables()
47
+ {
48
+ List<Table> tables = new List<Table>();
49
+ tables.Add(new Table("Fruits", "dbo", TableType.Table));
50
+ tables.Add(new Table("Drinks", "dbo", TableType.Table));
51
+ return tables.ToArray();
52
+ }
53
+
54
+ public override Column[] GetColumns(Table table)
55
+ {
56
+ List<Column> columns = new List<Column>();
57
+ columns.Add(new Column("Price", SqlType.Money));
58
+ columns.Add(new Column("Name", SqlType.VarChar));
59
+ if (table.Name == "Fruits")
60
+ columns.Add(new Column("Color", SqlType.VarChar));
61
+ if (table.Name == "Drinks")
62
+ columns.Add(new Column("Alcohol", SqlType.VarChar));
63
+ return columns.ToArray();
64
+ }
65
+
66
+ protected override DataSet GetDataSet(IDbCommand command, Report report, string reportPart, string uniqueID)
67
+ {
68
+ if (report == null || String.IsNullOrEmpty(reportPart))
69
+ return base.GetDataSet(command, report, reportPart, uniqueID);
70
+ DataSet ds = new DataSet();
71
+ DataTable dt = new DataTable();
72
+ ds.Tables.Add(dt);
73
+ foreach (Field f in report.Fields)
74
+ dt.Columns.Add(new DataColumn(f.Description, colTypes[f.DbColumn.FullName]));
75
+ int top = report.Top;
76
+ if (top <= 0 || top > 4)
77
+ top = 4;
78
+ for (int i = 0; i < top; i++)
79
+ {
80
+ DataRow dr = dt.NewRow();
81
+ for (int cc = 0; cc < report.Fields.Count; cc++)
82
+ dr[cc] = colData[report.Fields[cc].DbColumn.FullName][i];
83
+ dt.Rows.Add(dr);
84
+ }
85
+ return ds;
86
+ }
87
+
88
+ public override Version DetectVersion()
89
+ {
90
+ return new Version(1, 0);
91
+ }
92
+
93
+ public override void CheckConnection()
94
+ {
95
+ }
96
+
97
+ public override StoredProcedure[] GetAllStoredProcedures()
98
+ {
99
+ return new StoredProcedure[0];
100
+ }
101
+
102
+
103
+ public OfflineDriver()
104
+ {
105
+ }
106
+}
107
+```
108
+
109
+##VB.NET Example
110
+
111
+```visualbasic
112
+Imports System
113
+Imports System.Collections.Generic
114
+Imports Izenda.AdHoc
115
+Imports Izenda.AdHoc.Database
116
+Imports System.Data
117
+
118
+<Serializable>
119
+Public Class OfflineDriver
120
+ Inherits MSSQLDriver
121
+ Dim colTypes As Dictionary(Of String, Type) = New Dictionary(Of String, Type)() From
122
+ {{"[dbo].[Fruits].[Price]", GetType(Double)},
123
+ {"[dbo].[Drinks].[Price]", GetType(Double)},
124
+ {"[dbo].[Fruits].[Name]", GetType(String)},
125
+ {"[dbo].[Drinks].[Name]", GetType(String)},
126
+ {"[dbo].[Fruits].[Color]", GetType(String)},
127
+ {"[dbo].[Drinks].[Alcohol]", GetType(String)}}
128
+
129
+ Dim colData As Dictionary(Of String, Object()) = New Dictionary(Of String, Object())() From
130
+ {{"[dbo].[Fruits].[Price]", New Object() {1.24, 2.61, 1.38, 3.21}},
131
+ {"[dbo].[Drinks].[Price]", New Object() {9.56, 7.11, 9.41, 7.42}},
132
+ {"[dbo].[Fruits].[Name]", New Object() {"Apple", "Pear", "Plum", "Apricot"}},
133
+ {"[dbo].[Drinks].[Name]", New Object() {"Beer", "Tea", "Whiskey", "Coffee"}},
134
+ {"[dbo].[Fruits].[Color]", New Object() {"Green", "Yellow", "Black", "Orange"}},
135
+ {"[dbo].[Drinks].[Alcohol]", New String() {"Yes", "No", "Yes", "No"}}}
136
+
137
+ Public Overrides Function GetAllTables() As Table()
138
+ Dim tables = New List(Of Table)
139
+ tables.Add(New Table("Fruits", "dbo", TableType.Table))
140
+ tables.Add(New Table("Drinks", "dbo", TableType.Table))
141
+ Return tables.ToArray()
142
+ End function
143
+
144
+ Public Overrides Function GetColumns(table As Table) As Column()
145
+ Dim columns = New List(Of Column)
146
+ columns.Add(New Column("Price", SqlType.Money))
147
+ columns.Add(New Column("Name", SqlType.VarChar))
148
+ If table.Name.Equals("Fruits") Then
149
+ columns.Add(New Column("Color", SqlType.VarChar))
150
+ End If
151
+ If table.Name.Equals("Drinks") Then
152
+ columns.Add(New Column("Alcohol", SqlType.VarChar))
153
+ End If
154
+ Return columns.ToArray()
155
+ End function
156
+
157
+ Protected Overrides Function GetDataSet(command As IDbCommand, report As Report, reportPart As String, uniqueID As String) As DataSet
158
+ If report Is Nothing OrElse String.IsNullOrEmpty(reportPart) Then
159
+ Return MyBase.GetDataSet(command, report, reportPart, uniqueID)
160
+ End If
161
+ Dim ds As New DataSet()
162
+ Dim dt As New DataTable()
163
+ ds.Tables.Add(dt)
164
+ For Each f As Field In report.Fields
165
+ dt.Columns.Add(New DataColumn(f.Description, colTypes(f.DbColumn.FullName)))
166
+ Next
167
+ Dim top = report.Top
168
+ If top <= 0 OrElse top > 4 Then
169
+ top = 4
170
+ End If
171
+ For i As Integer = 0 To top - 1 Step 1
172
+ Dim dr = dt.NewRow()
173
+ For cc As Integer = 0 To report.Fields.Count - 1
174
+ dr(cc) = colData(report.Fields(cc).DbColumn.FullName)(i)
175
+ Next
176
+ dt.Rows.Add(dr)
177
+ Next
178
+ Return ds
179
+ End Function
180
+
181
+ Public Overrides Function DetectVersion() As Version
182
+ Return New Version(1, 0)
183
+ End Function
184
+
185
+ Public Overrides Sub CheckConnection()
186
+
187
+ End Sub
188
+
189
+ Public Overrides Function GetAllStoredProcedures() As StoredProcedure()
190
+ Return New StoredProcedure() {}
191
+ End Function
192
+
193
+ Public Sub New()
194
+
195
+ End Sub
196
+End Class
197
+```
... ...
\ No newline at end of file