Because I once had to work in a Windows environment where I was not allowed to install anything and had no access to a database, but needed a database anyway, I was forced to write such an SQLite wrapper so that I could store my data in a database.
classes.ps1 defines the two PowerShell classes sqliteDB and sqliteStmt.
It also defines two helper functions, charPtrToString and strToCharPtr which were needed because the SQLite API requires strings to be passed on the heap. (I am not sure if it was really necessary, but I found no other working way than this hack).
In order to read the version string from the DLL, I found no other way than to use GetModuleHandle and GetProcAddress which are found in the kernel32.dll.
add-type -typeDefinition @'
using System;
using System.Runtime.InteropServices;
public static class kernel32 {
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
}
'@
If [sqliteDB]::new(…) is invoked with a relative path, the database file is not found. Thus, relative paths should be merged with $pwd to obtain an absolute path.
The method bindArrayStepReset() currently needs to be called with one parameter (which is an array) $stmt.bindArrayStepReset( ( $val_1, $val_2, $val_3 ) ). It would be nice if the method could be called with multiples arguments: $stmt.bindArrayStepReset( $val_1, $val_2, $val_3 ).
The value of $handle in the sqliteStmt class might be set to 0 when its finalize() method is called.
The currently implemented blob interface only supports 32-bit lengths (sqlite3_bind_blob()). Sooner or later, sqlite3_bind_blob64() will probably be necessary.
However, it's not clear if this at all possible because there is only a sqlite3_column_bytes() function, and it returns an int. There should be, imho, a function that returns a 64-bit number when determining the size of a blob.
Done
2020-03-22: Implemented [sqliteDB]::version()
2020-03-28: Implemented sqlite3_column_count()
2020-04-02: Added the error message constant SQLITE_RANGE
2020-04-13: Implemented last_insert_rowid()
2020-04-16: Add support for 64-bit integers and doubles (reals)
2020-04-24: Implemented sqlite3_next_stmt() and the corresponding (hidden) method [sqliteStmt]::nextStmt. This allows to iterate over all open statements and then finalize them in the [sqliteDB]::close() method.