2011-07-21 18:00:12 +00:00
|
|
|
#include "go-sqlite.h"
|
|
|
|
|
2011-07-17 21:57:53 +00:00
|
|
|
#include <stdlib.h>
|
2011-07-17 23:58:53 +00:00
|
|
|
#include <stdio.h>
|
2011-07-17 21:57:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-07-16 01:30:46 +00:00
|
|
|
#include "_cgo_export.h"
|
2011-07-17 21:57:53 +00:00
|
|
|
|
2011-07-17 23:12:02 +00:00
|
|
|
static sqlite3_io_methods g_file_methods;
|
|
|
|
|
2011-07-16 00:26:36 +00:00
|
|
|
typedef struct GoFile GoFile;
|
|
|
|
struct GoFile {
|
|
|
|
sqlite3_io_methods const *pMethod; /* Always the first entry */
|
2011-07-17 23:12:02 +00:00
|
|
|
int fd;
|
2011-07-16 00:26:36 +00:00
|
|
|
};
|
|
|
|
|
2011-07-17 23:58:53 +00:00
|
|
|
/* File methods */
|
|
|
|
|
2011-07-17 23:12:02 +00:00
|
|
|
static int go_file_close(sqlite3_file* file) {
|
2011-07-20 01:03:19 +00:00
|
|
|
GoFileClose(((GoFile*) file)->fd);
|
|
|
|
// Matching sqlite3's os_unix.c here.
|
|
|
|
memset(file, 0, sizeof(GoFile));
|
|
|
|
return SQLITE_OK;
|
2011-07-17 23:12:02 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:58:53 +00:00
|
|
|
static int go_file_read(sqlite3_file* file, void* dest, int iAmt, sqlite3_int64 iOfst) {
|
2011-07-19 22:24:58 +00:00
|
|
|
return GoFileRead(((GoFile*) file)->fd, dest, iAmt, iOfst);
|
2011-07-17 23:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int go_file_write(sqlite3_file* file, const void* src, int iAmt, sqlite3_int64 iOfst) {
|
2011-07-20 00:43:21 +00:00
|
|
|
return GoFileWrite(((GoFile*) file)->fd, src, iAmt, iOfst);
|
2011-07-17 23:58:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-18 00:49:54 +00:00
|
|
|
static int go_file_truncate(sqlite3_file* file, sqlite3_int64 size) {
|
2011-07-19 22:55:07 +00:00
|
|
|
const int fd = ((GoFile*) file)->fd;
|
2011-07-18 14:39:48 +00:00
|
|
|
fprintf(stderr, "TODO go_file_truncate(%d)\n", fd);
|
2011-07-18 00:49:54 +00:00
|
|
|
// TODO: implement
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_file_sync(sqlite3_file* file, int flags) {
|
2011-07-19 22:55:07 +00:00
|
|
|
const int fd = ((GoFile*) file)->fd;
|
2011-07-18 14:39:48 +00:00
|
|
|
fprintf(stderr, "TODO go_file_sync(%d)\n", fd);
|
2011-07-18 00:49:54 +00:00
|
|
|
// TODO: implement
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_file_file_size(sqlite3_file* file, sqlite3_int64* pSize) {
|
2011-07-19 22:55:07 +00:00
|
|
|
const int fd = ((GoFile*) file)->fd;
|
2011-07-18 14:39:48 +00:00
|
|
|
struct GoFileFileSize_return result = GoFileFileSize(fd);
|
2011-07-19 22:39:30 +00:00
|
|
|
fprintf(stderr, "go_file_file_size(%d) = %d, %lld\n", fd, result.r0, result.r1);
|
2011-07-18 14:39:48 +00:00
|
|
|
if (result.r0 != 0) {
|
2011-07-18 00:49:54 +00:00
|
|
|
return SQLITE_ERROR;
|
2011-07-18 14:39:48 +00:00
|
|
|
}
|
2011-07-18 00:49:54 +00:00
|
|
|
|
|
|
|
*pSize = result.r1;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-18 14:39:48 +00:00
|
|
|
static int go_file_lock(sqlite3_file* file, int flags) {
|
2011-07-19 22:55:07 +00:00
|
|
|
const int fd = ((GoFile*) file)->fd;
|
2011-07-18 14:39:48 +00:00
|
|
|
fprintf(stderr, "TODO go_file_lock(%d)\n", fd);
|
|
|
|
// TODO: implement
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_file_unlock(sqlite3_file* file, int flags) {
|
2011-07-19 22:55:07 +00:00
|
|
|
const int fd = ((GoFile*) file)->fd;
|
2011-07-18 14:39:48 +00:00
|
|
|
fprintf(stderr, "TODO go_file_unlock(%d)\n", fd);
|
|
|
|
// TODO: implement
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-19 22:55:07 +00:00
|
|
|
static int go_file_check_reserved_lock(sqlite3_file* file, int* pResOut) {
|
|
|
|
const int fd = ((GoFile*) file)->fd;
|
|
|
|
fprintf(stderr, "TODO go_file_check_reserved_lock(%d)\n", fd);
|
|
|
|
// TODO: implement
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_file_file_control(sqlite3_file* file, int op, void* pArg) {
|
|
|
|
const int fd = ((GoFile*) file)->fd;
|
|
|
|
fprintf(stderr, "TODO go_file_file_control(%d, %d)\n", fd, op);
|
|
|
|
// TODO: implement
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_file_sector_size(sqlite3_file* file) {
|
2011-07-20 01:03:19 +00:00
|
|
|
// Matching sqlite3's os_unix.c here; this is SQLITE_DEFAULT_SECTOR_SIZE.
|
2011-07-19 22:55:07 +00:00
|
|
|
return 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_file_device_characteristics(sqlite3_file* file) {
|
2011-07-20 01:03:19 +00:00
|
|
|
// Matching sqlite3's os_unix.c here.
|
2011-07-19 22:55:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:58:53 +00:00
|
|
|
/* VFS methods */
|
|
|
|
|
2011-07-16 01:30:46 +00:00
|
|
|
static int go_vfs_open(sqlite3_vfs* vfs,
|
|
|
|
const char* zName,
|
|
|
|
sqlite3_file* file,
|
|
|
|
int flags,
|
|
|
|
int* pOutFlags) {
|
2011-07-17 23:58:53 +00:00
|
|
|
fprintf(stderr, "go_vfs_open: %s\n", zName);
|
2011-07-17 23:12:02 +00:00
|
|
|
GoFile* go_file = (GoFile*) file;
|
|
|
|
memset(go_file, 0, sizeof(go_file));
|
|
|
|
|
|
|
|
const int fd = GoVFSOpen((char*) zName, flags);
|
|
|
|
if (fd == -1) {
|
2011-07-16 01:30:46 +00:00
|
|
|
return SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:12:02 +00:00
|
|
|
go_file->pMethod = &g_file_methods;
|
|
|
|
go_file->fd = fd;
|
|
|
|
return SQLITE_OK;
|
2011-07-16 01:30:46 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:58:53 +00:00
|
|
|
static int go_vfs_delete(sqlite3_vfs* vfs, const char* zName, int syncDir) {
|
2011-07-20 01:35:57 +00:00
|
|
|
return GoVFSDelete(zName, syncDir);
|
2011-07-17 23:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int go_vfs_access(sqlite3_vfs* vfs,
|
|
|
|
const char* zName,
|
|
|
|
int flags,
|
|
|
|
int* pResOut) {
|
2011-07-19 22:39:30 +00:00
|
|
|
*pResOut = GoVFSAccess(zName, flags);
|
2011-07-17 23:58:53 +00:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_vfs_full_pathname(sqlite3_vfs* vfs,
|
|
|
|
const char* zName,
|
|
|
|
int nOut,
|
|
|
|
char* zOut) {
|
2011-07-18 14:39:48 +00:00
|
|
|
fprintf(stderr, "TODO go_vfs_full_pathname: %s\n", zName);
|
2011-07-18 00:49:54 +00:00
|
|
|
// TODO: Actually implement this.
|
2011-07-17 23:58:53 +00:00
|
|
|
strncpy(zOut, zName, nOut);
|
|
|
|
zOut[nOut - 1] = '\0';
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* go_vfs_dl_open(sqlite3_vfs* vfs, const char* zFilename) {
|
2011-07-20 01:35:57 +00:00
|
|
|
fprintf(stderr, "TODO go_vfs_dl_open\n");
|
2011-07-17 23:58:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void go_vfs_dl_error(sqlite3_vfs* vfs, int nByte, char *zErrMsg) {
|
2011-07-20 01:35:57 +00:00
|
|
|
fprintf(stderr, "TODO go_vfs_dl_error\n");
|
2011-07-17 23:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* go_vfs_dl_sym(sqlite3_vfs* vfs,
|
|
|
|
void* handle,
|
|
|
|
const char* zSymbol) {
|
2011-07-20 01:35:57 +00:00
|
|
|
fprintf(stderr, "TODO go_vfs_dl_sym\n");
|
2011-07-17 23:58:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void go_vfs_dl_close(sqlite3_vfs* vfs, void* handle) {
|
2011-07-20 01:35:57 +00:00
|
|
|
fprintf(stderr, "TODO go_vfs_dl_close\n");
|
2011-07-17 23:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int go_vfs_randomness(sqlite3_vfs* vfs, int nByte, char *zOut) {
|
2011-07-20 01:35:57 +00:00
|
|
|
fprintf(stderr, "TODO go_vfs_randomness\n");
|
2011-07-17 23:58:53 +00:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_vfs_sleep(sqlite3_vfs* vfs, int microseconds) {
|
2011-07-20 01:35:57 +00:00
|
|
|
fprintf(stderr, "TODO go_vfs_sleep\n");
|
2011-07-17 23:58:53 +00:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_vfs_current_time(sqlite3_vfs* vfs, double* now) {
|
2011-07-20 01:35:57 +00:00
|
|
|
*now = GoVFSCurrentTimeInt64() / 86400000.0;
|
2011-07-17 23:58:53 +00:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int go_vfs_get_last_error(sqlite3_vfs* vfs, int foo, char* bar) {
|
|
|
|
// Unused, per sqlite3's os_unix.c.
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-20 01:35:57 +00:00
|
|
|
static int go_vfs_current_time_int64(sqlite3_vfs* vfs, sqlite3_int64* now) {
|
|
|
|
*now = GoVFSCurrentTimeInt64();
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-21 01:33:13 +00:00
|
|
|
int go_init_vfs(void) {
|
2011-07-16 00:26:36 +00:00
|
|
|
static sqlite3_vfs vfs;
|
|
|
|
memset(&vfs, 0, sizeof(vfs));
|
2011-07-20 01:35:57 +00:00
|
|
|
vfs.iVersion = 2;
|
2011-07-16 00:26:36 +00:00
|
|
|
vfs.szOsFile = sizeof(GoFile);
|
|
|
|
vfs.mxPathname = 512;
|
|
|
|
vfs.pNext = NULL;
|
|
|
|
vfs.zName = "go";
|
|
|
|
vfs.pAppData = NULL;
|
2011-07-17 23:58:53 +00:00
|
|
|
/* Version 1 methods */
|
2011-07-16 01:30:46 +00:00
|
|
|
vfs.xOpen = go_vfs_open;
|
2011-07-17 23:58:53 +00:00
|
|
|
vfs.xDelete = go_vfs_delete;
|
|
|
|
vfs.xAccess = go_vfs_access;
|
|
|
|
vfs.xFullPathname = go_vfs_full_pathname;
|
|
|
|
vfs.xDlOpen = go_vfs_dl_open;
|
|
|
|
vfs.xDlError = go_vfs_dl_error;
|
|
|
|
vfs.xDlSym = go_vfs_dl_sym;
|
|
|
|
vfs.xDlClose = go_vfs_dl_close;
|
|
|
|
vfs.xRandomness = go_vfs_randomness;
|
|
|
|
vfs.xSleep = go_vfs_sleep;
|
|
|
|
vfs.xCurrentTime = go_vfs_current_time;
|
|
|
|
vfs.xGetLastError = go_vfs_get_last_error;
|
2011-07-20 01:35:57 +00:00
|
|
|
/* Version 2 method */
|
|
|
|
vfs.xCurrentTimeInt64 = go_vfs_current_time_int64;
|
2011-07-16 00:26:36 +00:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
** The methods above are in versions 1 and 2 of the sqlite_vfs object.
|
|
|
|
** Those below are for version 3 and greater.
|
|
|
|
*/
|
|
|
|
int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
|
|
|
|
sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
|
|
|
|
const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
|
|
|
|
/*
|
|
|
|
** The methods above are in versions 1 through 3 of the sqlite_vfs object.
|
|
|
|
** New fields may be appended in figure versions. The iVersion
|
|
|
|
** value will increment whenever this happens.
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
sqlite3_vfs_register(&vfs, 1);
|
2011-07-17 23:12:02 +00:00
|
|
|
|
|
|
|
memset(&g_file_methods, 0, sizeof(g_file_methods));
|
|
|
|
g_file_methods.iVersion = 1;
|
|
|
|
g_file_methods.xClose = go_file_close;
|
2011-07-17 23:58:53 +00:00
|
|
|
g_file_methods.xRead = go_file_read;
|
|
|
|
g_file_methods.xWrite = go_file_write;
|
2011-07-18 00:49:54 +00:00
|
|
|
g_file_methods.xTruncate = go_file_truncate;
|
|
|
|
g_file_methods.xSync = go_file_sync;
|
|
|
|
g_file_methods.xFileSize = go_file_file_size;
|
2011-07-18 14:39:48 +00:00
|
|
|
g_file_methods.xLock = go_file_lock;
|
|
|
|
g_file_methods.xUnlock = go_file_unlock;
|
2011-07-19 22:55:07 +00:00
|
|
|
g_file_methods.xCheckReservedLock = go_file_check_reserved_lock;
|
|
|
|
g_file_methods.xFileControl = go_file_file_control;
|
|
|
|
g_file_methods.xSectorSize = go_file_sector_size;
|
|
|
|
g_file_methods.xDeviceCharacteristics = go_file_device_characteristics;
|
2011-07-17 23:12:02 +00:00
|
|
|
#if 0
|
|
|
|
/* Methods above are valid for version 1 */
|
|
|
|
int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
|
|
|
|
int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
|
|
|
|
void (*xShmBarrier)(sqlite3_file*);
|
|
|
|
int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
|
|
|
|
/* Methods above are valid for version 2 */
|
|
|
|
/* Additional methods may be added in future releases */
|
|
|
|
#endif
|
|
|
|
|
2011-07-16 00:26:36 +00:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-21 01:33:13 +00:00
|
|
|
#if SQLITE_OS_OTHER==1
|
|
|
|
int sqlite3_os_init(void) { return SQLITE_OK; }
|
2011-07-16 00:26:36 +00:00
|
|
|
int sqlite3_os_end(void) { return SQLITE_OK; }
|
2011-07-21 01:33:13 +00:00
|
|
|
#endif
|