2 com.noccy.pdo
Christopher Vagnetoft edited this page 2021-12-15 13:37:54 +01:00

PDO Plugin

Installation

Global installation (recommended, requires SPARK_PLUGINS environment):

  1. Extract the plug into your global plugin directory ($SPARK_PLUGINS)
  2. Go to your project directory and run spark plugin --enable com.noccy.pdo

Local installation:

  1. Extract the directory into .spark/plugins, like a savage.

Configuration

To define resorces using PHP, add this to the appropriate preloaded file:

create_resource($name, $type, $uri);

You can also use the config file .spark/resources.json:

{
  "resources": {}
}

Resources are defined with the name as the key, and the full connection URI as the value. The URI should contain the resource type as well as the resource URI separated by a plus sign, in this case pdo+mysql, pdo+sqlite etc.

...
  "db": "pdo+mysql://user:pass@host:port/dbname"
...

You can also create a temporary in-memory SQLite database by using pdo+sqlite:

Usage

Storing queries:

$ spark pdo:store --res otherdb \  # store resource with query
    "getuserid" \  # Query name
    "select id from users where username=:username" \  # query
    :username  # slot

List stored queries:

$ spark pdo:store

Delete a stored query:

$ spark pdo:store --remove getuserid

Recalling queries:

$ spark pdo:query --recall getuserid username=bob

Direct query:

$ spark pdo:query "select * from users"
$ spark pdo:query --res otherdb "select * from users"
$ spark pdo:query --vertical "select * from user where id=:id" id=42"
$ spark pdo:query --csv "select username from user where id=:id" id=42"
$ spark pdo:query --box --vertical "select name,value from config"

Execute without result:

$ spark pdo:exec "drop table foo"
$ spark pdo:exec --res otherdb "drop table bar"