go-sqlite / sqlite3

pure-Go sqlite3 file reader
BSD 3-Clause "New" or "Revised" License
140 stars 19 forks source link

read column names #17

Closed adamdecaf closed 6 years ago

adamdecaf commented 6 years ago

Cool project. I want to read browser (firefox, chrome, etc) history sqlite databases and don't want to import a cgo library for the simple read. (It'll also complicate my build process)

To get my feet wet I found out a way to read column names, but I'm unsure if this works well enough.

My use case doesn't need to be "fast" and I only need one column's data from one table in these files.

I'm willing to help read tables/column data more.

zellyn commented 6 years ago

Your use-case is very similar to mine, which is reading browser cookies. Wanting to avoid the heavyweight C-linked sqlite libraries is exactly why I started contributing to this project. For now, I just punted on column names completely, and trusted that I could rely on the number and order of column data, and the type information attached to it. Here's the code in question: https://github.com/zellyn/kooky/blob/master/chrome.go#L26

Your code (and the existing comma-splitting code to count columns) will sort-of work, at least for simple table definitions. However, for more complex definitions, it will break down: you can see that table-constraint clauses are comma-separated here: https://www.sqlite.org/lang_createtable.html.

I was planning on writing a simple-as-possible parser that could handle the full sqlite create table syntax, and using that to extract structured information about the table. But then, you know, my cookie-reading code started working perfectly, and … well ¯\_(ツ)_/¯

sbinet commented 6 years ago

one could perhaps tap into a more robust SQL parser e.g.

zellyn commented 6 years ago

Yeah, I thought of that. But cznic/ql doesn't seem to support the full range of create table syntax, vitess is mysql-alike, and cockroach's parser is postgres-based (originally).

adamdecaf commented 6 years ago

Your code (and the existing comma-splitting code to count columns) will sort-of work, at least for simple table definitions.

@zellyn yea. I figured strings.Split was too easy..

@sbinet Thanks. I'm gonna check those out (and the zellyn/kooky link).

zellyn commented 6 years ago

Well, splitting on commas is (a) better than nothing, and (b) completely consistent with the existing code. @sbinet may have opinions, but I say merge it on in :-)

adamdecaf commented 6 years ago

Works for me!

sbinet commented 6 years ago

I agree: getting something to start playing with is at least something :)

zellyn commented 6 years ago

lgtm. @sbinet I don't think I have merge permission…

sbinet commented 6 years ago

LGTM too.

@adamdecaf could you send another PR adding yourself to the list of AUTHORS and CONTRIBUTORS? I'll accept it and then this one.

adamdecaf commented 6 years ago

Sure thing! https://github.com/go-sqlite/sqlite3/pull/20

sbinet commented 6 years ago

thanks and welcome to the pod :)