|
| 1 | +Contributing to usql |
| 2 | +==================== |
| 3 | + |
| 4 | +Any contributions are welcome. If you found a bug, or a missing feature, |
| 5 | +take a look at existing [issues](https://github.com/xo/usql/issues) |
| 6 | +and create a new one if needed. |
| 7 | + |
| 8 | +You can also open up a [pull request](https://github.com/xo/usql/pulls) (PR) |
| 9 | +with code or documentation changes. |
| 10 | + |
| 11 | +# Adding a new driver |
| 12 | + |
| 13 | +1. Add a new schema in [dburl](https://github.com/xo/dburl). |
| 14 | +1. Create a new go package in `drivers`. It should have an `init()` function, that would call `drivers.Register()`. |
| 15 | +1. Regenerate code in the `internal` package by running `internal/gen.sh`. |
| 16 | +1. Add any new required modules using `go get` or by editing `go.mod` manually and running `go mod tidy`. |
| 17 | +1. Run all tests, build `usql` and see if the new driver works. |
| 18 | +1. Update `README.md`. |
| 19 | + |
| 20 | +> Tip: check out closed PRs for examples, and/or search the codebase |
| 21 | +for names of databases you're familiar with. |
| 22 | + |
| 23 | +# Enabling metadata introspection for a driver |
| 24 | + |
| 25 | +For `\d*` commands to work, `usql` needs to know how to read the structure of a database. |
| 26 | +A driver must provide a metadata reader, by setting the `NewMetadataReader` property |
| 27 | +in the `drivers.Driver` structure passed to `drivers.Register()`. This needs to be a function |
| 28 | +that given a database and reader options, returns a reader instance for this particular driver. |
| 29 | + |
| 30 | +If the database has a `information_schema` schema, with standard tables like `tables` and `columns`, |
| 31 | +you can use an existing reader from the `drivers/informationschema` package. |
| 32 | +Since there are usually minor difference in objects defined in that schema in different databases, |
| 33 | +there's a set of options to configure this reader. Refer to |
| 34 | +the [package docs](https://pkg.go.dev/github.com/xo/usql/drivers/metadata/informationschema) for details. |
| 35 | + |
| 36 | +If you can't use the `informationschema` reader, consider implementing a new one. |
| 37 | +It should implement at least one of the following reader interfaces: |
| 38 | +* CatalogReader |
| 39 | +* SchemaReader |
| 40 | +* TableReader |
| 41 | +* ColumnReader |
| 42 | +* IndexReader |
| 43 | +* IndexColumnReader |
| 44 | +* FunctionReader |
| 45 | +* FunctionColumnReader |
| 46 | +* SequenceReader |
| 47 | + |
| 48 | +Every of these interfaces consist of a single function, that takes a `Filter` structure as an argument, |
| 49 | +and returns a set of results and an error. |
| 50 | + |
| 51 | +Example drivers using their own readers include: |
| 52 | +* `sqlite3` |
| 53 | +* `oracle` and `godror` sharing the same reader |
| 54 | + |
| 55 | +If you want to use the `informationschema` reader, but need to override one or more readers, |
| 56 | +use the `metadata.NewPluginReader(readers ...Reader)` function. It returns an object calling |
| 57 | +reader functions from the last reader passed in the arguments, that implements it. |
| 58 | + |
| 59 | +Example drivers extending an `informationschema` reader using a plugin reader: |
| 60 | +* `postgres` |
| 61 | + |
| 62 | +`\d*` commands are actually implemented by a metadata writer. There's currently only one, |
| 63 | +but it too can be replaced and/or extended. |
| 64 | + |
| 65 | +# Enabling autocomplete for a driver |
| 66 | + |
| 67 | +If a driver provides a metadata reader, the default completer will use it. |
| 68 | +A driver can provide it's own completer, by setting the `NewCompleter` property |
| 69 | +in the `drivers.Driver` structure passed to `drivers.Register()`. |
0 commit comments