|
| 1 | +Attribute VB_Name = "SQLiteCAdoDemo" |
| 2 | +Attribute VB_Description = "Illustrates typical workflows for SQLiteCAdo" |
| 3 | +'@Folder "SQLite.AADemo" |
| 4 | +'@ModuleDescription "Illustrates typical workflows for SQLiteCAdo" |
| 5 | +'@IgnoreModule |
| 6 | +Option Explicit |
| 7 | +Option Private Module |
| 8 | + |
| 9 | +Private Const LITE_LIB As String = "SQLiteCAdo" |
| 10 | +Private Const PATH_SEP As String = "\" |
| 11 | +Private Const LITE_RPREFIX As String = "Library" & PATH_SEP & LITE_LIB & PATH_SEP |
| 12 | + |
| 13 | +Private Type TSQLiteCAdoDemo |
| 14 | + DbPathName As String |
| 15 | + dbmC As SQLiteC |
| 16 | + dbmADO As LiteMan |
| 17 | + dbs As SQLiteCStatement |
| 18 | + dbq As ILiteADO |
| 19 | +End Type |
| 20 | +Private this As TSQLiteCAdoDemo |
| 21 | + |
| 22 | + |
| 23 | +Private Sub CleanUp() |
| 24 | + With this |
| 25 | + Set .dbq = Nothing |
| 26 | + Set .dbs = Nothing |
| 27 | + Set .dbmADO = Nothing |
| 28 | + Set .dbmC = Nothing |
| 29 | + End With |
| 30 | +End Sub |
| 31 | + |
| 32 | + |
| 33 | +Private Sub MainC() |
| 34 | + this.DbPathName = FixObjAdo.RandomTempFileName |
| 35 | + InitDBQC |
| 36 | + Debug.Print "Created blank db: " & this.dbq.MainDB |
| 37 | + |
| 38 | + DemoDBQ "C" |
| 39 | + |
| 40 | + CleanUp |
| 41 | +End Sub |
| 42 | + |
| 43 | + |
| 44 | +Private Sub MainADO() |
| 45 | + this.DbPathName = FixObjAdo.RandomTempFileName |
| 46 | + Set this.dbmADO = LiteMan(this.DbPathName, True) |
| 47 | + Set this.dbq = this.dbmADO.ExecADO |
| 48 | + Debug.Print "Created blank db: " & this.dbq.MainDB |
| 49 | + |
| 50 | + DemoDBQ "ADO" |
| 51 | + |
| 52 | + CleanUp |
| 53 | +End Sub |
| 54 | + |
| 55 | + |
| 56 | +Private Sub DemoDBQ(Optional ByVal Subpackage As String = "C") |
| 57 | + Dim dbq As ILiteADO |
| 58 | + Set dbq = this.dbq |
| 59 | + |
| 60 | + Dim SQLQuery As String |
| 61 | + Dim AffectedRows As Long |
| 62 | + |
| 63 | + SQLQuery = FixSQLFunc.Create |
| 64 | + AffectedRows = dbq.ExecuteNonQuery(SQLQuery) |
| 65 | + SQLQuery = FixSQLFunc.InsertData |
| 66 | + AffectedRows = dbq.ExecuteNonQuery(SQLQuery) |
| 67 | + |
| 68 | + Debug.Print "Number of inserted rows: " & CStr(AffectedRows) |
| 69 | + |
| 70 | + Dim QueryParams As Scripting.Dictionary |
| 71 | + If Subpackage = "C" Then |
| 72 | + SQLQuery = FixSQLFunc.SelectFilteredParamName |
| 73 | + Set QueryParams = FixSQLFunc.SelectFilteredParamNameValues |
| 74 | + Else |
| 75 | + SQLQuery = FixSQLFunc.SelectFilteredPlain |
| 76 | + Set QueryParams = Nothing |
| 77 | + End If |
| 78 | + |
| 79 | + Dim AdoRecordset As ADODB.Recordset |
| 80 | + Set AdoRecordset = dbq.GetAdoRecordset(SQLQuery, QueryParams) |
| 81 | + |
| 82 | + Dim RowSet2D As Variant |
| 83 | + RowSet2D = ArrayLib.TransposeArray(AdoRecordset.GetRows) |
| 84 | + |
| 85 | + |
| 86 | + Debug.Print "Number of selected records: " & CStr(AdoRecordset.RecordCount) |
| 87 | +End Sub |
| 88 | + |
| 89 | + |
| 90 | +Private Sub InitDBQC() |
| 91 | + '------------------------' |
| 92 | + '===== INIT MANAGER =====' |
| 93 | + '------------------------' |
| 94 | + Dim DllPath As String |
| 95 | + DllPath = LITE_RPREFIX & "dll\" & ARCH |
| 96 | + Dim DllNames As Variant |
| 97 | + #If Win64 Then |
| 98 | + DllNames = "sqlite3.dll" |
| 99 | + #Else |
| 100 | + DllNames = Array("icudt68.dll", "icuuc68.dll", "icuin68.dll", _ |
| 101 | + "icuio68.dll", "icutu68.dll", "sqlite3.dll") |
| 102 | + #End If |
| 103 | + Dim dbm As SQLiteC |
| 104 | + '@Ignore IndexedDefaultMemberAccess |
| 105 | + Set dbm = SQLiteC(DllPath, DllNames) |
| 106 | + If dbm Is Nothing Then |
| 107 | + Err.Raise ErrNo.UnknownClassErr, "SQLiteCExamples", _ |
| 108 | + "Failed to create an SQLiteC instance." |
| 109 | + Else |
| 110 | + Debug.Print "Database manager instance (SQLiteC class) is ready" |
| 111 | + End If |
| 112 | + |
| 113 | + '''' Test SQLite3.dll |
| 114 | + If Replace(dbm.Version(False), ".", "0") & "0" = CStr(dbm.Version) Then |
| 115 | + Debug.Print "Database engine version functionality test passed." |
| 116 | + Else |
| 117 | + Debug.Print "Database engine version functionality test failed." |
| 118 | + End If |
| 119 | + Set this.dbmC = dbm |
| 120 | + |
| 121 | + '---------------------------' |
| 122 | + '===== INIT CONNECTION =====' |
| 123 | + '---------------------------' |
| 124 | + Dim dbc As SQLiteCConnection |
| 125 | + Set dbc = dbm.CreateConnection(this.DbPathName, AllowNonExistent:=True) |
| 126 | + If dbc Is Nothing Then |
| 127 | + Err.Raise ErrNo.UnknownClassErr, "SQLiteCExamples", _ |
| 128 | + "Failed to create an SQLiteCConnection instance." |
| 129 | + Else |
| 130 | + Debug.Print "Database SQLiteCConnection instance is ready." |
| 131 | + End If |
| 132 | + |
| 133 | + '--------------------------' |
| 134 | + '===== INIT STATEMENT =====' |
| 135 | + '--------------------------' |
| 136 | + Dim DbStmtName As String |
| 137 | + DbStmtName = vbNullString |
| 138 | + Dim dbs As SQLiteCStatement |
| 139 | + Set dbs = dbc.CreateStatement(DbStmtName) |
| 140 | + Set this.dbs = dbs |
| 141 | + Dim dbq As ILiteADO |
| 142 | + Set dbq = dbs |
| 143 | + If dbq Is Nothing Then |
| 144 | + Err.Raise ErrNo.UnknownClassErr, "SQLiteCExamples", _ |
| 145 | + "Failed to create an SQLiteCStatement instance." |
| 146 | + Else |
| 147 | + Debug.Print "Database SQLiteCStatement instance is ready." |
| 148 | + End If |
| 149 | + '''' Maximum capapacity of 100x10 = 1000 rows |
| 150 | + dbs.DbExecutor.PageCount = 10 |
| 151 | + dbs.DbExecutor.PageSize = 100 |
| 152 | + Set this.dbq = dbq |
| 153 | +End Sub |
0 commit comments