-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathRecordSetSqlLite.ahk
165 lines (128 loc) · 3.36 KB
/
RecordSetSqlLite.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;namespace DBA
/*
Represents a result set of an SQLite Query
*/
class RecordSetSqlLite extends DBA.RecordSet
{
_currentRow := 0 ; Row
_colNames := 0 ; Collection<ColumnNames>
_query := 0 ; int Handle to the Query
_db := 0 ; SQLiteDataBase
_eof := false ; bool
__New(db, query){
if(!is(db, DBA.DataBaseSQLLite)){
throw Exception("ArgumentException: db must be a DataBaseSQLLite Object",-1)
}
this._db := db
this._query := query
if(query != 0){
SQLite_FetchNames(this._query, names)
this._colNames := new Collection(names)
this.MoveNext() ; move to the first record
}
}
Test(){
return "I'm a RecordSetSqlLite instance. For sure."
}
/*
Is this RecordSet valid?
*/
IsValid(){
return (this._query != 0)
}
/*
Returns an Array with all Column Names
*/
getColumnNames(){
return this._colNames
}
getEOF(){
return this._eof
}
MoveNext() {
static EOR := -1
this.ErrorMsg := ""
this.ErrorCode := 0
this._currentRow := 0
if (!this._query) {
this.ErrorMsg := "Invalid query handle!"
this._eof := true
return false
}
rc := DllCall("SQlite3\sqlite3_step", "UInt", this._query, "Cdecl Int")
if (rc != this._db.ReturnCode("SQLITE_ROW")) {
if (rc = this._db.ReturnCode("SQLITE_DONE")) {
this.ErrorMsg := "EOR"
this.ErrorCode := rc
this._eof := true
return EOR
}
this.ErrorMessage := this._db.ErrMsg()
this.ErrorCode := rc
this._eof := true
return false
}
rc := DllCall("SQlite3\sqlite3_data_count", UInt, this._query, "Cdecl Int")
if (rc < 1) {
this.ErrorMsg := "RecordSet is empty!"
this.ErrorCode := this._db.ReturnCode("SQLITE_EMPTY")
this._eof := true
return false
}
; fill the internal row structure
;_currentRow := new Row()
fields := new Collection()
Loop, %rc% {
ctype := DllCall("SQlite3\sqlite3_column_type", UInt, this._query, Int, A_Index - 1, "Cdecl Int")
if (ctype == SQLiteDataType.SQLITE_NULL) {
fields[A_Index] := DBA.DataBase.NULL ;""
}else if(ctype == SQLiteDataType.SQLITE_BLOB){
blobSize := DllCall("SQlite3\sqlite3_column_bytes", UInt, this._query, Int, A_Index -1, "Cdecl UInt")
blobPtr := DllCall("SQlite3\sqlite3_column_blob", UInt, this._query, Int, A_Index - 1, "Cdecl Ptr")
if ( blobPtr )
{
fields[A_Index] := MemoryBuffer.Create( blobPtr, blobSize )
}else{
fields[A_Index] := DBA.DataBase.NULL
}
} else {
strPtr := DllCall("SQlite3\sqlite3_column_text", UInt, this._query, Int, A_Index - 1, "Cdecl UInt")
fields[A_Index] := StrGet(strPtr, "UTF-8")
}
}
this._currentRow := new DBA.Row(this.getColumnNames(), fields)
this.CurrentRow++
return true
}
Reset() {
this.ErrorMsg := ""
this.ErrorCode := 0
if (!this._query) {
this.ErrorMsg := "Invalid query handle!"
return false
}
rc := DllCall("SQlite3\sqlite3_reset", UInt, this._query, "Cdecl Int")
if (rc) {
this.ErrorMsg := This._db.ErrMsg()
this.ErrorCode := rc
return false
}
this.CurrentRow := 0
this.MoveNext()
return true
}
Close() {
this.ErrorMsg := ""
this.ErrorCode := 0
if(this._query == 0)
return true
rc := DllCall("SQlite3\sqlite3_finalize", "UInt", this._query, "Cdecl Int")
if (rc) {
this.ErrorMsg := this._db.ErrMsg()
this.ErrorCode := rc
return false
}
this._query := 0
return true
}
}