-
Notifications
You must be signed in to change notification settings - Fork 4
SQL interop
Charm gives special privileges to SQL. In particular, it allows you to configure the hub so as to make all its services into clients, by default, of a given SQL database.
Here is the process as I just carried it out in my REPL.
→ hub config db
The following SQL drivers are available:
[0] Firebird SQL
[1] MariaDB
[2] MySQL
[3] Oracle
[4] Postgres
[5] SQLite
Pick a number: 4
Host: localhost
Port: 5432
Database: postgres
Username for database access: tim
Password for database access: ▪▪▪▪▪
ok
→
Having done that, the hub assumes by default that a service trying to talk to SQL is trying to talk to that database in particular.
An example is given in examples/sql.ch. The following definitions and commands are only excerpts from that file, which you should look at in full.
def
Person = struct(name varchar(32), age int)
cmd
main :
put SQL --- CREATE TABLE IF NOT EXISTS People |Person|
add (name string, age int) :
put SQL ---
INSERT INTO People
VALUES |name, age|
show (name string) :
get personList as Person from SQL ---
SELECT * FROM People
WHERE name=|name|
post prettyFmt(personList) to Output()
Things to note:
-
As with other forms of IO, as discussed on the previous page, we overload the
get,put,postanddeletecommands. Note that Charm has no way of checking whether you are in fact putting, posting, or deleting, and which of these to use is up to your judgement, just as when you use the same words in HTTP.get, on the other hand, is compulsory if you want to get a value back. -
Charm values are inserted into SQL code using
|as a delimiter. This doesn't just allow you to pass variables, you can also use Charm expressions. -
|<TypeName>|converts the name of a struct type to a SQL table definition. (This is why we have abstract types of the formvarchar(n), so that we can have datatypes that correspond exactly to SQL datatypes.)
🧿 Pipefish is distributed under the MIT license. Please steal my code and ideas.
- Getting started
- Language basics
- The type system and built-in functions
- Functional Pipefish
- Encapsulation
- Imperative Pipefish
-
Imports and libraries
- The crypto/aes library
- The crypto/bcrypt library
- The crypto/rand library
- The crypto/rsa library
- The crypto/sha_256 library
- The crypto/sha_512 library
- The database/sql library
- The encoding/base_32 library
- The encoding/base_64 library
- The encoding/csv library
- The encoding/json library
- The files library
- The fmt library
- The html library
- The image library
- The image/bmp library
- The image/color library
- The image/jpeg library
- The image/png library
- The lists library
- The markdown library
- The math library
- The math/big library
- The math/cmplx library
- The math/rand library
- The net/http library
- The net/mail library
- The net/smtp library
- The net/url library
- The os/exec library
- The path library
- The path/filepath library
- The reflect library
- The regexp library
- The strconv library
- The strings library
- The terminal library
- The time library
- The unicode library
- Advanced Pipefish
- Developing in Pipefish
- Deployment
- Appendices