-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmysqlx_schema.cc
575 lines (470 loc) · 17.9 KB
/
mysqlx_schema.cc
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
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andrey Hristov <andrey@php.net> |
+----------------------------------------------------------------------+
*/
#include "php_api.h"
#include "mysqlnd_api.h"
#include "xmysqlnd/xmysqlnd.h"
#include "xmysqlnd/xmysqlnd_schema.h"
#include "xmysqlnd/xmysqlnd_session.h"
#include "xmysqlnd/xmysqlnd_collection.h"
#include "xmysqlnd/xmysqlnd_table.h"
#include "php_mysqlx.h"
#include "mysqlx_class_properties.h"
#include "mysqlx_database_object.h"
#include "mysqlx_exception.h"
#include "mysqlx_collection.h"
#include "mysqlx_table.h"
#include "mysqlx_schema.h"
#include "mysqlx_session.h"
#include "util/allocator.h"
#include "util/object.h"
#include "util/string_utils.h"
#include "util/functions.h"
namespace mysqlx {
namespace devapi {
namespace {
using namespace drv;
static zend_class_entry *mysqlx_schema_class_entry;
/************************************** INHERITED START ****************************************/
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__construct, 0, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__get_session, 0, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__get_name, 0, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__exists_in_database, 0, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
/************************************** INHERITED END ****************************************/
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__create_collection, 0, ZEND_RETURN_VALUE, 1)
ZEND_ARG_TYPE_INFO(no_pass_by_ref, name, IS_STRING, dont_allow_null)
ZEND_ARG_TYPE_INFO(no_pass_by_ref, options, IS_STRING, dont_allow_null)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema_modify_collection, 0, ZEND_RETURN_VALUE, 2)
ZEND_ARG_TYPE_INFO(no_pass_by_ref, name, IS_STRING, dont_allow_null)
ZEND_ARG_TYPE_INFO(no_pass_by_ref, options, IS_STRING, dont_allow_null)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__drop_collection, 0, ZEND_RETURN_VALUE, 1)
ZEND_ARG_TYPE_INFO(no_pass_by_ref, collection_name, IS_STRING, dont_allow_null)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__get_collection, 0, ZEND_RETURN_VALUE, 1)
ZEND_ARG_TYPE_INFO(no_pass_by_ref, collection_name, IS_STRING, dont_allow_null)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__get_collections, 0, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__get_table, 0, ZEND_RETURN_VALUE, 1)
ZEND_ARG_TYPE_INFO(no_pass_by_ref, table_name, IS_STRING, dont_allow_null)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__get_tables, 0, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysqlx_schema__get_collection_as_table, 0, ZEND_RETURN_VALUE, 1)
ZEND_ARG_TYPE_INFO(no_pass_by_ref, collection_name, IS_STRING, dont_allow_null)
ZEND_END_ARG_INFO()
struct st_mysqlx_schema : public util::custom_allocable
{
~st_mysqlx_schema();
xmysqlnd_schema* schema;
};
st_mysqlx_schema::~st_mysqlx_schema()
{
xmysqlnd_schema_free(schema, nullptr, nullptr);
if (schema->get_counter() == 0) {
mnd_efree(schema);
}
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, __construct)
{
UNUSED_INTERNAL_FUNCTION_PARAMETERS();
}
/************************************** INHERITED START ****************************************/
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, getSession)
{
DBG_ENTER("mysqlx_schema::getSession");
util::raw_zval* object_zv{nullptr};
if (FAILURE == util::get_method_arguments(
execute_data,
getThis(),
"O",
&object_zv,
mysqlx_schema_class_entry))
{
DBG_VOID_RETURN;
}
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
XMYSQLND_SESSION session{ data_object.schema->get_session() };
create_session(session).move_to(return_value);
DBG_VOID_RETURN;
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, getName)
{
DBG_ENTER("mysqlx_schema::getName");
util::raw_zval* object_zv{nullptr};
if (FAILURE == util::get_method_arguments(execute_data, getThis(), "O",
&object_zv, mysqlx_schema_class_entry))
{
DBG_VOID_RETURN;
}
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
const auto& schema_name{ data_object.schema->get_name() };
RETVAL_STRINGL(schema_name.data(), schema_name.length());
DBG_VOID_RETURN;
}
static const enum_hnd_func_status
mysqlx_scheme_on_error(
void* /*context*/,
XMYSQLND_SESSION session,
xmysqlnd_stmt* const /*stmt*/,
const unsigned int code,
const util::string_view& sql_state,
const util::string_view& message)
{
DBG_ENTER("mysqlx_scheme_on_error");
create_exception(code, sql_state, message);
DBG_RETURN(HND_PASS_RETURN_FAIL);
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, existsInDatabase)
{
DBG_ENTER("mysqlx_schema::existsInDatabase");
util::raw_zval* object_zv{nullptr};
if (FAILURE == util::get_method_arguments(execute_data, getThis(), "O",
&object_zv, mysqlx_schema_class_entry))
{
DBG_VOID_RETURN;
}
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
xmysqlnd_schema* schema = data_object.schema;
const st_xmysqlnd_session_on_error_bind on_error{ mysqlx_scheme_on_error, nullptr };
schema->exists_in_database(on_error, return_value);
DBG_VOID_RETURN;
}
/************************************** INHERITED END ****************************************/
static const enum_hnd_func_status
mysqlx_schema_on_error(
void* /*context*/,
const xmysqlnd_schema* const /*schema*/,
const unsigned int code,
const util::string_view& sql_state,
const util::string_view& message)
{
DBG_ENTER("mysqlx_schema_on_error");
create_exception(code, sql_state, message);
DBG_RETURN(HND_PASS_RETURN_FAIL);
}
const enum_hnd_func_status on_drop_db_object_error(
void* /*context*/,
const xmysqlnd_schema * const /*schema*/,
const unsigned int code,
const util::string_view& sql_state,
const util::string_view& message)
{
throw util::xdevapi_exception(code, sql_state, message);
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, createCollection)
{
DBG_ENTER("mysqlx_schema::createCollection");
util::raw_zval* object_zv{nullptr};
util::arg_string collection_name;
const util::arg_string Empty_collection_options = "{}";
util::arg_string collection_options(Empty_collection_options);
if (FAILURE == util::get_method_arguments(
execute_data, getThis(), "Os|s",
&object_zv, mysqlx_schema_class_entry,
&(collection_name.str), &(collection_name.len),
&collection_options.str, &collection_options.len))
{
DBG_VOID_RETURN;
}
RETVAL_NULL();
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
if (!collection_name.empty()) {
const st_xmysqlnd_schema_on_error_bind on_error{ mysqlx_schema_on_error, nullptr };
xmysqlnd_collection* const collection{
data_object.schema->create_collection(
collection_name.to_view(),
collection_options.to_view(),
on_error) };
DBG_INF_FMT("collection=%p", collection);
if (collection) {
create_collection(collection).move_to(return_value);
}
}
DBG_VOID_RETURN;
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, modifyCollection)
{
DBG_ENTER("mysqlx_schema::modifyCollection");
util::raw_zval* object_zv{nullptr};
util::arg_string collection_name;
util::arg_string collection_options;
if (FAILURE == util::get_method_arguments(
execute_data, getThis(), "Oss",
&object_zv, mysqlx_schema_class_entry,
&collection_name.str, &collection_name.len,
&collection_options.str, &collection_options.len))
{
DBG_VOID_RETURN;
}
RETVAL_FALSE;
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
if (!collection_name.empty()) {
const st_xmysqlnd_schema_on_error_bind on_error{ mysqlx_schema_on_error, nullptr };
if (data_object.schema->modify_collection(
collection_name.to_view(),
collection_options.to_view(),
on_error)) {
RETVAL_TRUE;
}
}
DBG_VOID_RETURN;
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, dropCollection)
{
DBG_ENTER("mysqlx_schema::dropCollection");
util::raw_zval* object_zv{nullptr};
util::arg_string collection_name;
if (FAILURE == util::get_method_arguments(
execute_data, getThis(), "Os",
&object_zv, mysqlx_schema_class_entry,
&collection_name.str, &collection_name.len))
{
DBG_VOID_RETURN;
}
auto& data_object = util::fetch_data_object<st_mysqlx_schema>(object_zv);
try {
const st_xmysqlnd_schema_on_error_bind on_error{ on_drop_db_object_error, nullptr };
RETVAL_BOOL(PASS == data_object.schema->drop_collection(collection_name.to_view(), on_error));
} catch(std::exception& e) {
util::log_warning(e.what());
RETVAL_FALSE;
}
DBG_VOID_RETURN;
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, getCollection)
{
DBG_ENTER("mysqlx_schema::getCollection");
util::raw_zval* object_zv{nullptr};
util::arg_string collection_name;
if (FAILURE == util::get_method_arguments(execute_data, getThis(), "Os",
&object_zv, mysqlx_schema_class_entry,
&(collection_name.str), &(collection_name.len)))
{
DBG_VOID_RETURN;
}
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
RETVAL_NULL();
if ( !collection_name.empty() && data_object.schema) {
xmysqlnd_collection* const collection = data_object.schema->create_collection_object(collection_name.to_view());
if (collection) {
create_collection(collection).move_to(return_value);
}
}
DBG_VOID_RETURN;
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, getTable)
{
DBG_ENTER("mysqlx_schema::getTable");
util::raw_zval* object_zv{nullptr};
util::arg_string table_name;
if (FAILURE == util::get_method_arguments(execute_data, getThis(), "Os",
&object_zv, mysqlx_schema_class_entry,
&(table_name.str), &(table_name.len)))
{
DBG_VOID_RETURN;
}
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
RETVAL_NULL();
if ( !table_name.empty() && data_object.schema) {
xmysqlnd_table* const table = data_object.schema->create_table_object(table_name.to_view());
create_table(table).move_to(return_value);
}
DBG_VOID_RETURN;
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, getCollectionAsTable)
{
DBG_ENTER("mysqlx_schema::getCollectionAsTable");
util::raw_zval* object_zv{nullptr};
util::arg_string collection_name;
if (FAILURE == util::get_method_arguments(execute_data, getThis(), "Os",
&object_zv, mysqlx_schema_class_entry,
&(collection_name.str), &(collection_name.len)))
{
DBG_VOID_RETURN;
}
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
RETVAL_NULL();
if (!collection_name.empty() && data_object.schema) {
xmysqlnd_table* const table = data_object.schema->create_table_object(collection_name.to_view());
create_table(table).move_to(return_value);
}
DBG_VOID_RETURN;
}
struct st_mysqlx_on_db_object_ctx
{
util::zvalue* list;
};
static void
mysqlx_on_db_object(void* context, xmysqlnd_schema* const schema, const util::string_view& object_name, const util::string_view& object_type)
{
DBG_ENTER("mysqlx_on_db_object");
st_mysqlx_on_db_object_ctx* ctx = static_cast<st_mysqlx_on_db_object_ctx*>(context);
util::zvalue db_obj;
if ((object_type[0] == 'T') || (object_type[0] == 'V')) {
xmysqlnd_table* const table = schema->create_table_object(object_name);
if (table) {
db_obj = create_table(table);
ctx->list->insert(object_name, db_obj);
}
} else if (object_type[0] == 'C') {
xmysqlnd_collection* const collection = schema->create_collection_object(object_name);
if (collection) {
db_obj = create_collection(collection);
ctx->list->insert(object_name, db_obj);
}
}
DBG_VOID_RETURN;
}
static util::zvalue
mysqlx_get_database_objects(
xmysqlnd_schema* schema,
const db_object_type_filter object_type_filter)
{
DBG_ENTER("mysqlx_get_database_objects");
util::zvalue list = util::zvalue::create_array();
if (schema) {
st_mysqlx_on_db_object_ctx context{ &list };
const st_xmysqlnd_schema_on_database_object_bind on_object{ mysqlx_on_db_object, &context };
const st_xmysqlnd_schema_on_error_bind handler_on_error{ mysqlx_schema_on_error, nullptr };
if (PASS != schema->get_db_objects(schema->get_name(), object_type_filter, on_object, handler_on_error)) {
list.clear();
}
}
DBG_RETURN(list);
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, getTables)
{
DBG_ENTER("mysqlx_schema::getTables");
util::raw_zval* object_zv{nullptr};
if (util::get_method_arguments(execute_data, getThis(), "O", &object_zv, mysqlx_schema_class_entry) == FAILURE) {
DBG_VOID_RETURN;
}
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
mysqlx_get_database_objects(data_object.schema, drv::db_object_type_filter::table_or_view).move_to(return_value);
DBG_VOID_RETURN;
}
MYSQL_XDEVAPI_PHP_METHOD(mysqlx_schema, getCollections)
{
DBG_ENTER("mysqlx_schema::getCollections");
util::raw_zval* object_zv{nullptr};
if (util::get_method_arguments(execute_data, getThis(), "O", &object_zv, mysqlx_schema_class_entry) == FAILURE) {
DBG_VOID_RETURN;
}
auto& data_object{ util::fetch_data_object<st_mysqlx_schema>(object_zv) };
mysqlx_get_database_objects(data_object.schema, drv::db_object_type_filter::collection).move_to(return_value);
DBG_VOID_RETURN;
}
static const zend_function_entry mysqlx_schema_methods[] = {
PHP_ME(mysqlx_schema, __construct, arginfo_mysqlx_schema__construct, ZEND_ACC_PRIVATE)
/************************************** INHERITED START ****************************************/
PHP_ME(mysqlx_schema, getSession, arginfo_mysqlx_schema__get_session, ZEND_ACC_PUBLIC)
PHP_ME(mysqlx_schema, getName, arginfo_mysqlx_schema__get_name, ZEND_ACC_PUBLIC)
PHP_ME(mysqlx_schema, existsInDatabase, arginfo_mysqlx_schema__exists_in_database, ZEND_ACC_PUBLIC)
/************************************** INHERITED END ****************************************/
PHP_ME(mysqlx_schema, createCollection, arginfo_mysqlx_schema__create_collection, ZEND_ACC_PUBLIC)
PHP_ME(mysqlx_schema, modifyCollection, arginfo_mysqlx_schema_modify_collection, ZEND_ACC_PUBLIC)
PHP_ME(mysqlx_schema, dropCollection, arginfo_mysqlx_schema__drop_collection, ZEND_ACC_PUBLIC)
PHP_ME(mysqlx_schema, getCollection, arginfo_mysqlx_schema__get_collection, ZEND_ACC_PUBLIC)
PHP_ME(mysqlx_schema, getCollections, arginfo_mysqlx_schema__get_collections, ZEND_ACC_PUBLIC)
PHP_ME(mysqlx_schema, getTable, arginfo_mysqlx_schema__get_table, ZEND_ACC_PUBLIC)
PHP_ME(mysqlx_schema, getTables, arginfo_mysqlx_schema__get_tables, ZEND_ACC_PUBLIC)
PHP_ME(mysqlx_schema, getCollectionAsTable,arginfo_mysqlx_schema__get_collection_as_table, ZEND_ACC_PUBLIC)
{nullptr, nullptr, nullptr}
};
static util::raw_zval*
mysqlx_schema_property__name(const st_mysqlx_object* obj, util::raw_zval* return_value)
{
const st_mysqlx_schema* object = static_cast<const st_mysqlx_schema*>(obj->ptr);
DBG_ENTER("mysqlx_schema_property__name");
if (object->schema && !object->schema->get_name().empty()) {
ZVAL_STRINGL(return_value, object->schema->get_name().data(), object->schema->get_name().length());
} else {
/*
This means EG(uninitialized_value). If we return just return_value, this is an UNDEF-ed value
and ISSET will say 'true' while for EG(unin) it is false.
In short:
return nullptr; -> isset()===false, value is nullptr
return return_value; (without doing ZVAL_XXX)-> isset()===true, value is nullptr
*/
return_value = nullptr;
}
DBG_RETURN(return_value);
}
static zend_object_handlers mysqlx_object_schema_handlers;
static HashTable mysqlx_schema_properties;
const st_mysqlx_property_entry mysqlx_schema_property_entries[] =
{
{std::string_view("name"), mysqlx_schema_property__name, nullptr},
{std::string_view{}, nullptr, nullptr}
};
static void
mysqlx_schema_free_storage(zend_object* object)
{
util::free_object<st_mysqlx_schema>(object);
}
static zend_object *
php_mysqlx_schema_object_allocator(zend_class_entry* class_type)
{
DBG_ENTER("php_mysqlx_schema_object_allocator");
st_mysqlx_object* mysqlx_object = util::alloc_object<st_mysqlx_schema>(
class_type,
&mysqlx_object_schema_handlers,
&mysqlx_schema_properties);
DBG_RETURN(&mysqlx_object->zo);
}
} // anonymous namespace
void
mysqlx_register_schema_class(UNUSED_INIT_FUNC_ARGS, zend_object_handlers* mysqlx_std_object_handlers)
{
MYSQL_XDEVAPI_REGISTER_CLASS(
mysqlx_schema_class_entry,
"Schema",
mysqlx_std_object_handlers,
mysqlx_object_schema_handlers,
php_mysqlx_schema_object_allocator,
mysqlx_schema_free_storage,
mysqlx_schema_methods,
mysqlx_schema_properties,
mysqlx_schema_property_entries,
mysqlx_database_object_interface_entry);
/* The following is needed for the Reflection API */
zend_declare_property_null(mysqlx_schema_class_entry, "name", sizeof("name") - 1, ZEND_ACC_PUBLIC);
}
void
mysqlx_unregister_schema_class(UNUSED_SHUTDOWN_FUNC_ARGS)
{
zend_hash_destroy(&mysqlx_schema_properties);
}
util::zvalue
create_schema(xmysqlnd_schema* schema)
{
DBG_ENTER("create_schema");
util::zvalue schema_obj;
st_mysqlx_schema& data_object{
util::init_object<st_mysqlx_schema>(mysqlx_schema_class_entry, schema_obj) };
data_object.schema = schema;
DBG_RETURN(schema_obj);
}
} // namespace devapi
} // namespace mysqlx