Phpextensionformysqldocstorewebminar 1535625794008
Phpextensionformysqldocstorewebminar 1535625794008
|
Presented with
MySQL DevAPI
Building Modern Apps with Node.js
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
On This Call Today
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
NoSQL
• Most users/companies feel compelled to try NoSQL at some point in time
• Either they believe a RDBMS does not scale or because devs don’t like SQL
or something else
• Only later to find out about all its limitations (usually because there was not
enough time to learn a whole new tool in the first place)
• The result is usually one of the following:
– Use some RDBMS with JSON support (smaller companies/users)
– Implement some kind of NoSQL interface on top of a reliable storage engine (e.g. Uber
with Schemaless and Facebook with MyRocks/RocksDB)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
MySQL DocStore Components
Client App
MySQL
Router
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
X Protocol
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
X-Protocol
Mysqlx.Crud.Find {
collection { name: "collection_name", schema: "test" }
data_model: DOCUMENT
criteria {
type: OPERATOR
operator {
name: "=="
param {
type: IDENT,
identifier { name: "_id" }
}
param {
type: LITERAL,
literal {
type: V_STRING,
v_string: { value: "some_string" }
}
}
}
}
}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Documents and Collections
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
X DevAPI
A common API for Document and SQL
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Features
• Fluent API with flexible parameters
– query builder based on common expression language
– raw SQL interface is also available
• Secure by default (SSL/TLS and SHA256 authentication)
• Data-consistency with transactions, savepoints and row-locking
• Faster (in SQL mode) than alternative community drivers
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Fluent API
• Code becomes more readable, maintainable (and even testable)
• Operations encapsulated in specialized and semantic methods
• Nice scaffolding for refactor tasks
• First-class support for text-editor (or IDE) hints and auto-completion
• Smaller SQL injection surface area
• Common standard between different programming environments
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Flexible Parameters
• Most public API methods work with:
– Multiple individual parameters
– A single array of parameters
– An object with named properties (where it applies)
$sakila = ["name" => "Sakila", "age" => 17, "job" => "Singer"];
$coll->add($sakila)->execute();
$coll->add(["name" => "Sakila", "age" => 18, "job" => "Student"])->execute();
$coll->add('{"name": "Filip", "age": 24, "job": "Plumber"}')→execute();
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Expression Language
• Subset of SQL (easier to learn, yet still powerful)
• Expressive and human-readable
• Common standard between all the official connector implementations
(making it portable)
// PHP // Java
$coll->find('name like \‘foo\’ AND age > 42') collection.find("name = 'foo' AND age > 42")
->fields(['name', 'age']) .fields("name", "age")
->groupBy('name', 'age'); .groupBy("name", "age")
->sort('name ASC',’age DESC’) .sort("name ASC", "age DESC")
->limit(4) .limit(4)
->offset(2) .offset(2)
->execute(); .execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
SSL Modes
Secure by default
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Authentication Mechanisms
Secure by default (as well)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Transactions and Savepoints
Session-level atomic operations
$session→startTransaction();
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Row Locking
Document-level isolation
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Row Locking
Usage
Type/Modes DEFAULT NOWAIT SKIP_LOCKED
Exclusive Lock Transactions on different sessions Reads in one session fail if there is Reads in one session see inconsistent
are synchronized and reads see an ongoing transaction on a data when there is an ongoing
consistent data. different session. transaction on a different session.
Shared Lock Reads in one session wait until all Same as above. Same as above.
ongoing transactions on different
sessions are commited.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Raw SQL
// create a table
$session->sql('CREATE TABLE foo (bar VARCHAR(3))')->execute()
// add a unique constraint
$session->sql('ALTER TABLE foo ADD COLUMN bar VARCHAR(3) GENERATED ALWAYS AS doc->>"$.bar" VIRTUAL UNIQUE KEY NOT NULL')->execute()
// execute a JOIN query
$session->sql('SELECT DISTINCT t1.bar FROM foo t1 JOIN baz t2 ON t1.bar = t2.qux WHERE t1.qux = t2.quux')->execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Setting up and running the PHP Extension
for MySQLX
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Extension setup
.
.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Extension setup
fjaniszewski ~ : php -m
[PHP Modules]
Core
.
.
mysql_xdevapi
.
.
mysqli
Mysqlnd
.
.
[Zend Modules]
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Extension setup
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Configure MySQL
• For >= 8.0.12 there’s not too much to do, the plugin is installed and can be
verified with:
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Examples
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
How to create a collection
<?php
$session = mysql_xdevapi\getSession('mysqlx://fjanisze:pass@localhost:33060/?ssl-mode=disabled');
$session->createSchema('testx');
$schema = $session->getSchema('testx');
$schema->createCollection('test_collection');
$coll = $schema->getCollection('test_collection');
?>
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Adding Documents
.
.
.
.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Adding documents
• Different data types can be provided as input for the ‘add’ operation, those
data types are implicitly converted using json_encode()
.
.
$sakila = ["name" => "Sakila", "age" => 17, "job" => "Singer"];
$coll->add($sakila)->execute();
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
CollectionAdd
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Modify a document
.
.
$coll->modify("job in ('Programmatore','Programmatrice')")->arrayAppend('job','Tassinaro')->set('Overworked','Yes')->execute();
$coll->modify("age > 25 and age < 60")->patch('{"Hobby" : ["Swimming","Dancing"]}')->execute();
.
.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Modify a document
.
$coll->modify("job in ('Programmatore','Programmatrice')")->arrayAppend('job','Tassinaro')->set('Overworked','Yes')->execute();
$coll->modify("age > 25 and age < 60")->patch('{"Hobby" : ["Swimming","Dancing"]}')→execute();
.
$coll->modify("_id like 'ID1'")->patch('{"Overworked": null}')→execute();
$coll->modify("_id like 'ID1'")->unset('Overworked')->execute();
.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
CollectionModify
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Search for a document
.
$age = 30;
$res = $coll->find('age > :age_param')->bind([ 'age_param' => $age ])->execute();
$data = $res->fetchAll();
.
array(1) {
[0]=>
array(4) {
["_id"]=>
string(28) "00005b555b14000000000000001e"
["age"]=>
int(59)
["job"]=>
string(8) "Paninaro"
["name"]=>
string(7) "Lonardo"
}
}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
CollectionFind
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Removing documents
.
$coll->remove('age > :age_from and age < :age_to')->bind(['age_from' => 20, 'age_to' => 50])->limit(7)->execute();
$coll->remove('true')->sort('age desc')->limit(2)->execute();
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
CollectionRemove
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |