-
Notifications
You must be signed in to change notification settings - Fork 790
/
Copy pathplugin.xml
1437 lines (1418 loc) · 42.6 KB
/
plugin.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="mysqlnd.plugin" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>MySQL Native Driver Plugin API</title>
<para>
The MySQL Native Driver Plugin API is a feature of MySQL Native
Driver, or <literal>mysqlnd</literal>. <literal>Mysqlnd</literal>
plugins operate in the layer between PHP applications and the MySQL
server. This is comparable to MySQL Proxy. MySQL Proxy operates on a
layer between any MySQL client application, for example, a PHP
application and, the MySQL server. <literal>Mysqlnd</literal> plugins
can undertake typical MySQL Proxy tasks such as load balancing,
monitoring and performance optimizations. Due to the different
architecture and location, <literal>mysqlnd</literal> plugins do not
have some of MySQL Proxy's disadvantages. For example, with plugins,
there is no single point of failure, no dedicated proxy server to
deploy, and no new programming language to learn (Lua).
</para>
<para>
A <literal>mysqlnd</literal> plugin can be thought of as an extension
to <literal>mysqlnd</literal>. Plugins can intercept the majority of
<literal>mysqlnd</literal> functions. The <literal>mysqlnd</literal>
functions are called by the PHP MySQL extensions such as
<literal>ext/mysql</literal>, <literal>ext/mysqli</literal>, and
<literal>PDO_MYSQL</literal>. As a result, it is possible for a
<literal>mysqlnd</literal> plugin to intercept all calls made to these
extensions from the client application.
</para>
<para>
Internal <literal>mysqlnd</literal> function calls can also be
intercepted, or replaced. There are no restrictions on manipulating
<literal>mysqlnd</literal> internal function tables. It is possible to
set things up so that when certain <literal>mysqlnd</literal>
functions are called by the extensions that use
<literal>mysqlnd</literal>, the call is directed to the appropriate
function in the <literal>mysqlnd</literal> plugin. The ability to
manipulate <literal>mysqlnd</literal> internal function tables in this
way allows maximum flexibility for plugins.
</para>
<para>
<literal>Mysqlnd</literal> plugins are in fact PHP Extensions, written
in C, that use the <literal>mysqlnd</literal> plugin API (which is
built into MySQL Native Driver, <literal>mysqlnd</literal>). Plugins
can be made 100% transparent to PHP applications. No application
changes are needed because plugins operate on a different layer. The
<literal>mysqlnd</literal> plugin can be thought of as operating in a
layer below <literal>mysqlnd</literal>.
</para>
<para>
The following list represents some possible applications of
<literal>mysqlnd</literal> plugins.
</para>
<itemizedlist>
<listitem>
<para>
Load Balancing
</para>
<itemizedlist>
<listitem>
<para>
Read/Write Splitting. An example of this is the PECL/mysqlnd_ms
(Master Slave) extension. This extension splits read/write queries
for a replication setup.
</para>
</listitem>
<listitem>
<para>
Failover
</para>
</listitem>
<listitem>
<para>
Round-Robin, least loaded
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>
Monitoring
</para>
<itemizedlist>
<listitem>
<para>
Query Logging
</para>
</listitem>
<listitem>
<para>
Query Analysis
</para>
</listitem>
<listitem>
<para>
Query Auditing. An example of this is the PECL/mysqlnd_sip (SQL
Injection Protection) extension. This extension inspects queries
and executes only those that are allowed according to a ruleset.
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>
Performance
</para>
<itemizedlist>
<listitem>
<para>
Caching. An example of this is the PECL/mysqlnd_qc (Query Cache)
extension.
</para>
</listitem>
<listitem>
<para>
Throttling
</para>
</listitem>
<listitem>
<para>
Sharding. An example of this is the PECL/mysqlnd_mc (Multi
Connect) extension. This extension will attempt to split a SELECT
statement into n-parts, using SELECT ... LIMIT part_1, SELECT
LIMIT part_n. It sends the queries to distinct MySQL servers and
merges the result at the client.
</para>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<para>
<emphasis role="bold">MySQL Native Driver Plugins Available</emphasis>
</para>
<para>
There are a number of mysqlnd plugins already available. These
include:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="bold">PECL/mysqlnd_mc</emphasis> - Multi Connect
plugin.
</para>
</listitem>
<listitem>
<para>
<emphasis role="bold">PECL/mysqlnd_ms</emphasis> - Master Slave
plugin.
</para>
</listitem>
<listitem>
<para>
<emphasis role="bold">PECL/mysqlnd_qc</emphasis> - Query Cache
plugin.
</para>
</listitem>
<listitem>
<para>
<emphasis role="bold">PECL/mysqlnd_pscache</emphasis> - Prepared
Statement Handle Cache plugin.
</para>
</listitem>
<listitem>
<para>
<emphasis role="bold">PECL/mysqlnd_sip</emphasis> - SQL Injection
Protection plugin.
</para>
</listitem>
<listitem>
<para>
<emphasis role="bold">PECL/mysqlnd_uh</emphasis> - User Handler
plugin.
</para>
</listitem>
</itemizedlist>
<section xml:id="mysqlnd.plugin.mysql-proxy">
<title>A comparison of mysqlnd plugins with MySQL Proxy</title>
<para>
<literal>Mysqlnd</literal> plugins and MySQL Proxy are different
technologies using different approaches. Both are valid tools for
solving a variety of common tasks such as load balancing, monitoring,
and performance enhancements. An important difference is that MySQL
Proxy works with all MySQL clients, whereas
<literal>mysqlnd</literal> plugins are specific to PHP applications.
</para>
<para>
As a PHP Extension, a <literal>mysqlnd</literal> plugin gets
installed on the PHP application server, along with the rest of PHP.
MySQL Proxy can either be run on the PHP application server or can be
installed on a dedicated machine to handle multiple PHP application
servers.
</para>
<para>
Deploying MySQL Proxy on the application server has two advantages:
</para>
<orderedlist>
<listitem>
<para>
No single point of failure
</para>
</listitem>
<listitem>
<para>
Easy to scale out (horizontal scale out, scale by client)
</para>
</listitem>
</orderedlist>
<para>
MySQL Proxy (and <literal>mysqlnd</literal> plugins) can solve
problems easily which otherwise would have required changes to
existing applications.
</para>
<para>
However, MySQL Proxy does have some disadvantages:
</para>
<itemizedlist>
<listitem>
<para>
MySQL Proxy is a new component and technology to master and deploy.
</para>
</listitem>
<listitem>
<para>
MySQL Proxy requires knowledge of the Lua scripting language.
</para>
</listitem>
</itemizedlist>
<para>
MySQL Proxy can be customized with C and Lua programming. Lua is the
preferred scripting language of MySQL Proxy. For most PHP experts Lua
is a new language to learn. A <literal>mysqlnd</literal> plugin can
be written in C. It is also possible to write plugins in PHP using
<link xlink:href="&url.pecl.package;mysqlnd_uh">PECL/mysqlnd_uh</link>.
</para>
<para>
MySQL Proxy runs as a daemon - a background process. MySQL Proxy can
recall earlier decisions, as all state can be retained. However, a
<literal>mysqlnd</literal> plugin is bound to the request-based
lifecycle of PHP. MySQL Proxy can also share one-time computed
results among multiple application servers. A
<literal>mysqlnd</literal> plugin would need to store data in a
persistent medium to be able to do this. Another daemon would need to
be used for this purpose, such as Memcache. This gives MySQL Proxy an
advantage in this case.
</para>
<para>
MySQL Proxy works on top of the wire protocol. With MySQL Proxy you
have to parse and reverse engineer the MySQL Client Server Protocol.
Actions are limited to those that can be achieved by manipulating the
communication protocol. If the wire protocol changes (which happens
very rarely) MySQL Proxy scripts would need to be changed as well.
</para>
<para>
<literal>Mysqlnd</literal> plugins work on top of the C API, which
mirrors the <literal>libmysqlclient</literal> client.
This C API is basically a wrapper around the MySQL Client Server
protocol, or wire protocol, as it is sometimes called. You can
intercept all C API calls. PHP makes use of the C API, therefore you
can hook all PHP calls, without the need to program at the level of
the wire protocol.
</para>
<para>
<literal>Mysqlnd</literal> implements the wire protocol. Plugins can
therefore parse, reverse engineer, manipulate and even replace the
communication protocol. However, this is usually not required.
</para>
<para>
As plugins allow you to create implementations that use two levels (C
API and wire protocol), they have greater flexibility than MySQL
Proxy. If a <literal>mysqlnd</literal> plugin is implemented using
the C API, any subsequent changes to the wire protocol do not require
changes to the plugin itself.
</para>
</section>
<section xml:id="mysqlnd.plugin.obtaining">
<title>Obtaining the mysqlnd plugin API</title>
<para>
The <literal>mysqlnd</literal> plugin API is simply part of the MySQL
Native Driver PHP extension, <literal>ext/mysqlnd</literal>.
Development started on the <literal>mysqlnd</literal> plugin API in
December 2009. It is developed as part of the PHP source repository,
and as such is available to the public either via Git, or through
source snapshot downloads.
</para>
<para>
Plugin developers can determine the <literal>mysqlnd</literal>
version through accessing <literal>MYSQLND_VERSION</literal>, which
is a string of the format <quote>mysqlnd 8.3.17</quote>, or through
<literal>MYSQLND_VERSION_ID</literal>, which is an integer such as
50007. Developers can calculate the version number as follows:
</para>
<table xml:id="mysqlnd.plugin.version-id">
<title>MYSQLND_VERSION_ID calculation table</title>
<tgroup cols="2">
<thead>
<row>
<entry>Version (part)</entry>
<entry>Example</entry>
</row>
</thead>
<tbody>
<row>
<entry>Major*10000</entry>
<entry>5*10000 = 50000</entry>
</row>
<row>
<entry>Minor*100</entry>
<entry>0*100 = 0</entry>
</row>
<row>
<entry>Patch</entry>
<entry>7 = 7</entry>
</row>
<row>
<entry>MYSQLND_VERSION_ID</entry>
<entry>50007</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
During development, developers should refer to the
<literal>mysqlnd</literal> version number for compatibility and
version tests, as several iterations of <literal>mysqlnd</literal>
could occur during the lifetime of a PHP development branch with a
single PHP version number.
</para>
</section>
<section xml:id="mysqlnd.plugin.architecture">
<title>MySQL Native Driver Plugin Architecture</title>
<para>
This section provides an overview of the <literal>mysqlnd</literal>
plugin architecture.
</para>
<para>
<emphasis role="bold">MySQL Native Driver Overview</emphasis>
</para>
<para>
Before developing <literal>mysqlnd</literal> plugins, it is useful to
know a little of how <literal>mysqlnd</literal> itself is organized.
<literal>Mysqlnd</literal> consists of the following modules:
</para>
<table xml:id="mysqlnd.plugin.orgchart">
<title>The mysqlnd organization chart, per module</title>
<tgroup cols="2">
<thead>
<row>
<entry>Modules Statistics</entry>
<entry>mysqlnd_statistics.c</entry>
</row>
</thead>
<tbody>
<row>
<entry>Connection</entry>
<entry>mysqlnd.c</entry>
</row>
<row>
<entry>Resultset</entry>
<entry>mysqlnd_result.c</entry>
</row>
<row>
<entry>Resultset Metadata</entry>
<entry>mysqlnd_result_meta.c</entry>
</row>
<row>
<entry>Statement</entry>
<entry>mysqlnd_ps.c</entry>
</row>
<row>
<entry>Network</entry>
<entry>mysqlnd_net.c</entry>
</row>
<row>
<entry>Wire protocol</entry>
<entry>mysqlnd_wireprotocol.c</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
<emphasis role="bold">C Object-Oriented Paradigm</emphasis>
</para>
<para>
At the code level, <literal>mysqlnd</literal> uses a C pattern for
implementing object orientation.
</para>
<para>
In C you use a <literal>struct</literal> to represent an object.
Members of the struct represent object properties. Struct members
pointing to functions represent methods.
</para>
<para>
Unlike with other languages such as C++ or Java, there are no fixed
rules on inheritance in the C object-oriented paradigm. However,
there are some conventions that need to be followed that will be
discussed later.
</para>
<para>
<emphasis role="bold">The PHP Life Cycle</emphasis>
</para>
<para>
When considering the PHP life cycle there are two basic cycles:
</para>
<itemizedlist>
<listitem>
<para>
PHP engine startup and shutdown cycle
</para>
</listitem>
<listitem>
<para>
Request cycle
</para>
</listitem>
</itemizedlist>
<para>
When the PHP engine starts up it will call the module initialization
(MINIT) function of each registered extension. This allows each
module to setup variables and allocate resources that will exist for
the lifetime of the PHP engine process. When the PHP engine shuts
down it will call the module shutdown (MSHUTDOWN) function of each
extension.
</para>
<para>
During the lifetime of the PHP engine it will receive a number of
requests. Each request constitutes another life cycle. On each
request the PHP engine will call the request initialization function
of each extension. The extension can perform any variable setup and
resource allocation required for request processing. As the request
cycle ends the engine calls the request shutdown (RSHUTDOWN) function
of each extension so the extension can perform any cleanup required.
</para>
<para>
<emphasis role="bold">How a plugin works</emphasis>
</para>
<para>
A <literal>mysqlnd</literal> plugin works by intercepting calls made
to <literal>mysqlnd</literal> by extensions that use
<literal>mysqlnd</literal>. This is achieved by obtaining the
<literal>mysqlnd</literal> function table, backing it up, and
replacing it by a custom function table, which calls the functions of
the plugin as required.
</para>
<para>
The following code shows how the <literal>mysqlnd</literal> function
table is replaced:
</para>
<programlisting>
<![CDATA[
/* a place to store original function table */
struct st_mysqlnd_conn_methods org_methods;
void minit_register_hooks(TSRMLS_D) {
/* active function table */
struct st_mysqlnd_conn_methods * current_methods
= mysqlnd_conn_get_methods();
/* backup original function table */
memcpy(&org_methods, current_methods,
sizeof(struct st_mysqlnd_conn_methods);
/* install new methods */
current_methods->query = MYSQLND_METHOD(my_conn_class, query);
}
]]>
</programlisting>
<para>
Connection function table manipulations must be done during Module
Initialization (MINIT). The function table is a global shared
resource. In an multi-threaded environment, with a TSRM build, the
manipulation of a global shared resource during the request
processing will almost certainly result in conflicts.
</para>
<note>
<para>
Do not use any fixed-size logic when manipulating the
<literal>mysqlnd</literal> function table: new methods may be added
at the end of the function table. The function table may change at
any time in the future.
</para>
</note>
<para>
<emphasis role="bold">Calling parent methods</emphasis>
</para>
<para>
If the original function table entries are backed up, it is still
possible to call the original function table entries - the parent
methods.
</para>
<para>
In some cases, such as for
<literal>Connection::stmt_init()</literal>, it is vital to call the
parent method prior to any other activity in the derived method.
</para>
<programlisting>
<![CDATA[
MYSQLND_METHOD(my_conn_class, query)(MYSQLND *conn,
const char *query, unsigned int query_len TSRMLS_DC) {
php_printf("my_conn_class::query(query = %s)\n", query);
query = "SELECT 'query rewritten' FROM DUAL";
query_len = strlen(query);
return org_methods.query(conn, query, query_len); /* return with call to parent */
}
]]>
</programlisting>
<para>
<emphasis role="bold">Extending properties</emphasis>
</para>
<para>
A <literal>mysqlnd</literal> object is represented by a C struct. It
is not possible to add a member to a C struct at run time. Users of
<literal>mysqlnd</literal> objects cannot simply add properties to
the objects.
</para>
<para>
Arbitrary data (properties) can be added to a
<literal>mysqlnd</literal> objects using an appropriate function of
the
<literal>mysqlnd_plugin_get_plugin_<object>_data()</literal>
family. When allocating an object <literal>mysqlnd</literal> reserves
space at the end of the object to hold a <literal>void *</literal>
pointer to arbitrary data. <literal>mysqlnd</literal> reserves space
for one <literal>void *</literal> pointer per plugin.
</para>
<para>
The following table shows how to calculate the position of the
pointer for a specific plugin:
</para>
<table xml:id="mysqlnd.plugin.pointercalc">
<title>Pointer calculations for mysqlnd</title>
<tgroup cols="2">
<thead>
<row>
<entry>Memory address</entry>
<entry>Contents</entry>
</row>
</thead>
<tbody>
<row>
<entry>0</entry>
<entry>Beginning of the mysqlnd object C struct</entry>
</row>
<row>
<entry>n</entry>
<entry>End of the mysqlnd object C struct</entry>
</row>
<row>
<entry>n + (m x sizeof(void*))</entry>
<entry>void* to object data of the m-th plugin</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
If you plan to subclass any of the <literal>mysqlnd</literal> object
constructors, which is allowed, you must keep this in mind!
</para>
<para>
The following code shows extending properties:
</para>
<programlisting>
<![CDATA[
/* any data we want to associate */
typedef struct my_conn_properties {
unsigned long query_counter;
} MY_CONN_PROPERTIES;
/* plugin id */
unsigned int my_plugin_id;
void minit_register_hooks(TSRMLS_D) {
/* obtain unique plugin ID */
my_plugin_id = mysqlnd_plugin_register();
/* snip - see Extending Connection: methods */
}
static MY_CONN_PROPERTIES** get_conn_properties(const MYSQLND *conn TSRMLS_DC) {
MY_CONN_PROPERTIES** props;
props = (MY_CONN_PROPERTIES**)mysqlnd_plugin_get_plugin_connection_data(
conn, my_plugin_id);
if (!props || !(*props)) {
*props = mnd_pecalloc(1, sizeof(MY_CONN_PROPERTIES), conn->persistent);
(*props)->query_counter = 0;
}
return props;
}
]]>
</programlisting>
<para>
The plugin developer is responsible for the management of plugin data
memory.
</para>
<para>
Use of the <literal>mysqlnd</literal> memory allocator is recommended
for plugin data. These functions are named using the convention:
<literal>mnd_*loc()</literal>. The <literal>mysqlnd</literal>
allocator has some useful features, such as the ability to use a
debug allocator in a non-debug build.
</para>
<table xml:id="mysqlnd.plugin.subclass">
<title>When and how to subclass</title>
<tgroup cols="4">
<thead>
<row>
<entry></entry>
<entry>When to subclass?</entry>
<entry>Each instance has its own private function table?</entry>
<entry>How to subclass?</entry>
</row>
</thead>
<tbody>
<row>
<entry>Connection (MYSQLND)</entry>
<entry>MINIT</entry>
<entry>No</entry>
<entry>mysqlnd_conn_get_methods()</entry>
</row>
<row>
<entry>Resultset (MYSQLND_RES)</entry>
<entry>MINIT or later</entry>
<entry>Yes</entry>
<entry>mysqlnd_result_get_methods() or object method function table
manipulation</entry>
</row>
<row>
<entry>Resultset Meta (MYSQLND_RES_METADATA)</entry>
<entry>MINIT</entry>
<entry>No</entry>
<entry>mysqlnd_result_metadata_get_methods()</entry>
</row>
<row>
<entry>Statement (MYSQLND_STMT)</entry>
<entry>MINIT</entry>
<entry>No</entry>
<entry>mysqlnd_stmt_get_methods()</entry>
</row>
<row>
<entry>Network (MYSQLND_NET)</entry>
<entry>MINIT or later</entry>
<entry>Yes</entry>
<entry>mysqlnd_net_get_methods() or object method function table manipulation</entry>
</row>
<row>
<entry>Wire protocol (MYSQLND_PROTOCOL)</entry>
<entry>MINIT or later</entry>
<entry>Yes</entry>
<entry>mysqlnd_protocol_get_methods() or object method function table
manipulation</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
You must not manipulate function tables at any time later than MINIT
if it is not allowed according to the above table.
</para>
<para>
Some classes contain a pointer to the method function table. All
instances of such a class will share the same function table. To
avoid chaos, in particular in threaded environments, such function
tables must only be manipulated during MINIT.
</para>
<para>
Other classes use copies of a globally shared function table. The
class function table copy is created together with the object. Each
object uses its own function table. This gives you two options: you
can manipulate the default function table of an object at MINIT, and
you can additionally refine methods of an object without impacting
other instances of the same class.
</para>
<para>
The advantage of the shared function table approach is performance.
There is no need to copy a function table for each and every object.
</para>
<table xml:id="mysqlnd.plugin.constatus">
<title>Constructor status</title>
<tgroup cols="4">
<thead>
<row>
<entry>Type</entry>
<entry>Allocation, construction, reset</entry>
<entry>Can be modified?</entry>
<entry>Caller</entry>
</row>
</thead>
<tbody>
<row>
<entry>Connection (MYSQLND)</entry>
<entry>mysqlnd_init()</entry>
<entry>No</entry>
<entry>mysqlnd_connect()</entry>
</row>
<row>
<entry>Resultset(MYSQLND_RES)</entry>
<entry><para>
Allocation:
</para>
<itemizedlist>
<listitem>
<para>
Connection::result_init()
</para>
</listitem>
</itemizedlist>
<para>
Reset and re-initialized during:
</para>
<itemizedlist>
<listitem>
<para>
Result::use_result()
</para>
</listitem>
<listitem>
<para>
Result::store_result
</para>
</listitem>
</itemizedlist></entry>
<entry>Yes, but call parent!</entry>
<entry><itemizedlist>
<listitem>
<para>
Connection::list_fields()
</para>
</listitem>
<listitem>
<para>
Statement::get_result()
</para>
</listitem>
<listitem>
<para>
Statement::prepare() (Metadata only)
</para>
</listitem>
<listitem>
<para>
Statement::resultMetaData()
</para>
</listitem>
</itemizedlist></entry>
</row>
<row>
<entry>Resultset Meta (MYSQLND_RES_METADATA)</entry>
<entry>Connection::result_meta_init()</entry>
<entry>Yes, but call parent!</entry>
<entry>Result::read_result_metadata()</entry>
</row>
<row>
<entry>Statement (MYSQLND_STMT)</entry>
<entry>Connection::stmt_init()</entry>
<entry>Yes, but call parent!</entry>
<entry>Connection::stmt_init()</entry>
</row>
<row>
<entry>Network (MYSQLND_NET)</entry>
<entry>mysqlnd_net_init()</entry>
<entry>No</entry>
<entry>Connection::init()</entry>
</row>
<row>
<entry>Wire protocol (MYSQLND_PROTOCOL)</entry>
<entry>mysqlnd_protocol_init()</entry>
<entry>No</entry>
<entry>Connection::init()</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
It is strongly recommended that you do not entirely replace a
constructor. The constructors perform memory allocations. The memory
allocations are vital for the <literal>mysqlnd</literal> plugin API
and the object logic of <literal>mysqlnd</literal>. If you do not
care about warnings and insist on hooking the constructors, you
should at least call the parent constructor before doing anything in
your constructor.
</para>
<para>
Regardless of all warnings, it can be useful to subclass
constructors. Constructors are the perfect place for modifying the
function tables of objects with non-shared object tables, such as
Resultset, Network, Wire Protocol.
</para>
<table xml:id="mysqlnd.plugin.deststatus">
<title>Destruction status</title>
<tgroup cols="3">
<thead>
<row>
<entry>Type</entry>
<entry>Derived method must call parent?</entry>
<entry>Destructor</entry>
</row>
</thead>
<tbody>
<row>
<entry>Connection</entry>
<entry>yes, after method execution</entry>
<entry>free_contents(), end_psession()</entry>
</row>
<row>
<entry>Resultset</entry>
<entry>yes, after method execution</entry>
<entry>free_result()</entry>
</row>
<row>
<entry>Resultset Meta</entry>
<entry>yes, after method execution</entry>
<entry>free()</entry>
</row>
<row>
<entry>Statement</entry>
<entry>yes, after method execution</entry>
<entry>dtor(), free_stmt_content()</entry>
</row>
<row>
<entry>Network</entry>
<entry>yes, after method execution</entry>
<entry>free()</entry>
</row>
<row>
<entry>Wire protocol</entry>
<entry>yes, after method execution</entry>
<entry>free()</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
The destructors are the appropriate place to free properties,
<literal>mysqlnd_plugin_get_plugin_<replaceable><object></replaceable>_data()</literal>.
</para>
<para>
The listed destructors may not be equivalent to the actual
<literal>mysqlnd</literal> method freeing the object itself. However,
they are the best possible place for you to hook in and free your
plugin data. As with constructors you may replace the methods
entirely but this is not recommended. If multiple methods are listed
in the above table you will need to hook all of the listed methods
and free your plugin data in whichever method is called first by
<literal>mysqlnd</literal>.
</para>
<para>
The recommended method for plugins is to simply hook the methods,
free your memory and call the parent implementation immediately
following this.
</para>
</section>
<section xml:id="mysqlnd.plugin.api">
<title>The mysqlnd plugin API</title>
<para>
The following is a list of functions provided in the
<literal>mysqlnd</literal> plugin API:
</para>
<itemizedlist>
<listitem>
<para>
mysqlnd_plugin_register()
</para>
</listitem>
<listitem>
<para>
mysqlnd_plugin_count()
</para>
</listitem>
<listitem>
<para>
mysqlnd_plugin_get_plugin_connection_data()
</para>
</listitem>
<listitem>
<para>
mysqlnd_plugin_get_plugin_result_data()
</para>
</listitem>
<listitem>
<para>
mysqlnd_plugin_get_plugin_stmt_data()
</para>
</listitem>
<listitem>
<para>
mysqlnd_plugin_get_plugin_net_data()
</para>
</listitem>
<listitem>
<para>
mysqlnd_plugin_get_plugin_protocol_data()
</para>
</listitem>
<listitem>
<para>
mysqlnd_conn_get_methods()
</para>
</listitem>
<listitem>
<para>
mysqlnd_result_get_methods()
</para>
</listitem>
<listitem>
<para>
mysqlnd_result_meta_get_methods()
</para>
</listitem>
<listitem>
<para>
mysqlnd_stmt_get_methods()
</para>
</listitem>
<listitem>
<para>
mysqlnd_net_get_methods()
</para>
</listitem>
<listitem>
<para>
mysqlnd_protocol_get_methods()
</para>
</listitem>
</itemizedlist>
<para>
There is no formal definition of what a plugin is and how a plugin
mechanism works.
</para>
<para>
Components often found in plugins mechanisms are:
</para>
<itemizedlist>
<listitem>
<para>
A plugin manager
</para>
</listitem>
<listitem>
<para>
A plugin API
</para>
</listitem>
<listitem>
<para>
Application services (or modules)
</para>
</listitem>
<listitem>
<para>
Application service APIs (or module APIs)
</para>
</listitem>
</itemizedlist>
<para>
The <literal>mysqlnd</literal> plugin concept employs these features,
and additionally enjoys an open architecture.
</para>
<para>
<emphasis role="bold">No Restrictions</emphasis>
</para>
<para>
A plugin has full access to the inner workings of
<literal>mysqlnd</literal>. There are no security limits or
restrictions. Everything can be overwritten to implement friendly or
hostile algorithms. It is recommended you only deploy plugins from a
trusted source.
</para>
<para>
As discussed previously, plugins can use pointers freely. These
pointers are not restricted in any way, and can point into another
plugin's data. Simple offset arithmetic can be used to read another
plugin's data.
</para>
<para>
It is recommended that you write cooperative plugins, and that you
always call the parent method. The plugins should always cooperate
with <literal>mysqlnd</literal> itself.
</para>
<table xml:id="mysqlnd.plugin.chaining">
<title>Issues: an example of chaining and cooperation</title>
<tgroup cols="3">
<thead>
<row>
<entry>Extension</entry>
<entry>mysqlnd.query() pointer</entry>
<entry>call stack if calling parent</entry>
</row>
</thead>
<tbody>
<row>
<entry>ext/mysqlnd</entry>
<entry>mysqlnd.query()</entry>
<entry>mysqlnd.query</entry>
</row>
<row>
<entry>ext/mysqlnd_cache</entry>
<entry>mysqlnd_cache.query()</entry>
<entry><orderedlist>
<listitem>
<para>