go-sqlite3 icon indicating copy to clipboard operation
go-sqlite3 copied to clipboard

It is not possible to change driver name from sqlite3 to some another

Open borzovplus opened this issue 3 years ago • 11 comments

It is not possible to change driver name from sqlite3 to some another. It's critical if another lib also use sqlite3 but another version with the same driver name

borzovplus avatar Jan 06 '22 15:01 borzovplus

This is ultimately a flaw in the original design of Go's database/sql package. What we can do is move the call to sql.Register to a separate file that can be excluded via some build tag. Will that suffice?

rittneje avatar Jan 06 '22 16:01 rittneje

This is ultimately a flaw in the original design of Go's database/sql package. What we can do is move the call to sql.Register to a separate file that can be excluded via some build tag. Will that suffice?

I tired that. Unfortunately it is not help to me, problem is not in driver name =( May be do you have any ideas about that? tdlib/td#1815

borzovplus avatar Jan 06 '22 16:01 borzovplus

It looks like that td library you are trying to use compiles in its own version of SQLite, which has the sqlcipher extension built in. My guess is this conflicts with the version of SQLite that gets compiled in by this library, resulting in the issue you are seeing.

The only way I can think of to fix this issue is to tell this library not to compile in its own copy of SQLite, or to use the system-provided version, but rather use one that will be statically compiled in elsewhere. That is, you basically want the same thing as what the libsqlite3 build tag does, except for these lines: https://github.com/mattn/go-sqlite3/blob/98c52198ca55ae44ef9bb3107ff71af4c9d6e9eb/sqlite3_libsqlite3.go#L12-L17

You will also need to set CFLAGS appropriately at the command line so that it uses the sqlite3.h header file from td.

rittneje avatar Jan 06 '22 18:01 rittneje

It looks like that td library you are trying to use compiles in its own version of SQLite, which has the sqlcipher extension built in. My guess is this conflicts with the version of SQLite that gets compiled in by this library, resulting in the issue you are seeing.

The only way I can think of to fix this issue is to tell this library not to compile in its own copy of SQLite, or to use the system-provided version, but rather use one that will be statically compiled in elsewhere. That is, you basically want the same thing as what the libsqlite3 build tag does, except for these lines:

https://github.com/mattn/go-sqlite3/blob/98c52198ca55ae44ef9bb3107ff71af4c9d6e9eb/sqlite3_libsqlite3.go#L12-L17

You will also need to set CFLAGS appropriately at the command line so that it uses the sqlite3.h header file from td.

Thank you so much for diving into my problem!!! But I'm afraid my immature brain of a beginner in the golang world is not able to understand exactly what I need to do and where =(

Did I understand correctly what I need in my fork of go-sqlite3 remove next lines

#cgo linux LDFLAGS: -lsqlite3 #cgo darwin LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3 #cgo darwin CFLAGS: -I/usr/local/opt/sqlite/include #cgo openbsd LDFLAGS: -lsqlite3 #cgo solaris LDFLAGS: -lsqlite3 #cgo windows LDFLAGS: -lsqlite3

And then when I compile my golang application specify CFLAGS:

CFLAGS=[path to tdlib sqlite3.h] go build

I apologize in advance that I'm so stupid, I've been studying golang for only a week

borzovplus avatar Jan 06 '22 18:01 borzovplus

Something like that. I haven't dealt with this level of cgo that often, so it may take a few tries to get right. Please note you want CFLAGS to be -I/path/to/tdlib/sqlite3/dir. That is, the path to the folder, not the path of the file itself. You will also want to compile with the libsqlite3 build tag.

I apologize in advance that I'm so stupid, I've been studying golang for only a week

No need to apologize. You are not stupid, you just happened to hit upon what seems to be a fairly uncommon use case. :)

rittneje avatar Jan 06 '22 18:01 rittneje

Something like that. I haven't dealt with this level of cgo that often, so it may take a few tries to get right. Please note you want CFLAGS to be -I/path/to/tdlib/sqlite3/dir. That is, the path to the folder, not the path of the file itself. You will also want to compile with the libsqlite3 build tag.

I apologize in advance that I'm so stupid, I've been studying golang for only a week

No need to apologize. You are not stupid, you just happened to hit upon what seems to be a fairly uncommon use case. :)

thanks)) my tires (files sqlite3.h and other *.h are in /td/sqlite/sqlite) I run:

CGO_CFLAGS="-I/td/sqlite/sqlite" go build --tags "libsqlite3"

And I got alot of errors:

github.com/borzovplus/go-sqlite3 Undefined symbols for architecture x86_64: "_sqlite3_aggregate_context", referenced from: __cgo_8cada9c70ee3_Cfunc_sqlite3_aggregate_context in _x005.o (maybe you meant: __cgo_8cada9c70ee3_Cfunc_sqlite3_aggregate_context) "_sqlite3_backup_finish", referenced from: __cgo_8cada9c70ee3_Cfunc_sqlite3_backup_finish in _x002.o (maybe you meant: __cgo_8cada9c70ee3_Cfunc_sqlite3_backup_finish) "_sqlite3_backup_init", referenced from: __cgo_8cada9c70ee3_Cfunc_sqlite3_backup_init in _x002.o (maybe you meant: __cgo_8cada9c70ee3_Cfunc_sqlite3_backup_init) ....

ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

borzovplus avatar Jan 06 '22 18:01 borzovplus

Hmm, that is unfortunate. One other thing you could try is compiling td as a library first, and then linking with it in both places.

rittneje avatar Jan 06 '22 19:01 rittneje

Hmm, that is unfortunate. One other thing you could try is compiling td as a library first, and then linking with it in both places.

tdlib is already compiled in /usr/local/lib, what should I do? I have libtdjson.dylib

borzovplus avatar Jan 06 '22 19:01 borzovplus

If we provide variable driverName, you may be possible to replace the name. How about this?

go build -ldflags="-X 'github.com/mattn/go-sqlite3.driverName=my-sqlite3'"

mattn avatar Jan 09 '22 13:01 mattn

Ah, sorry. I misunderstood the purpose of this issue.

mattn avatar Jan 09 '22 13:01 mattn