-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdo_cfd1_db.c
163 lines (136 loc) · 3.71 KB
/
pdo_cfd1_db.c
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
static void cfd1_handle_closer(pdo_dbh_t *dbh)
{
// EM_ASM({ console.log('CLOSE', $0);}, dbh);
}
static bool cfd1_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
{
pdo_cfd1_db_handle *handle = dbh->driver_data;
pdo_cfd1_stmt *vStmt = emalloc(sizeof(pdo_cfd1_stmt));
stmt->methods = &vrzno_stmt_methods;
stmt->driver_data = vStmt;
const char *sqlString = ZSTR_VAL(sql);
EM_ASM({
const dbName = UTF8ToString($0);
const query = UTF8ToString($1);
const zv = $2;
const prepared = Module.cfd1[dbName].prepare(query);
Module.jsToZval(prepared, zv);
}, dbh->data_source, sqlString, &vStmt->prepared);
vStmt->db = handle;
return true;
}
static zend_long cfd1_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
{
return 1;
}
static zend_string *cfd1_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
{
const char *unquotedChar = ZSTR_VAL(unquoted);
zend_string *quoted = zend_string_init(unquotedChar, strlen(unquotedChar), 0);
return quoted;
}
static bool cfd1_handle_begin(pdo_dbh_t *dbh)
{
return (bool) EM_ASM_INT({
console.log('BEGIN TXN');
return true;
});
}
static bool cfd1_handle_commit(pdo_dbh_t *dbh)
{
return (bool) EM_ASM_INT({
console.log('COMMIT TXN', $0);
return true;
});
}
static bool cfd1_handle_rollback(pdo_dbh_t *dbh)
{
return (bool) EM_ASM_INT({
console.log('ROLLBACK TXN', $0);
return true;
});
}
static bool pdo_cfd1_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
{
return (bool) EM_ASM_INT({
console.log('SET ATTR', $1, $2);
return true;
}, attr, val);
}
static zend_string *pdo_cfd1_last_insert_id(pdo_dbh_t *dbh, const zend_string *name)
{
const char *nameStr = ZSTR_VAL(name);
#if PHP_MAJOR_VERSION >= 8 && PHP_MINOR_VERSION >= 1
return zend_ulong_to_str
#else
return zend_long_to_str
#endif
(EM_ASM_INT({
console.log('LAST INSERT ID', UTF8ToString($0));
return 0;
}, &nameStr));
}
static void pdo_cfd1_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
{
EM_ASM({
console.log('FETCH ERROR FUNC', $0, $1);
}, stmt, info);
}
static int pdo_cfd1_get_attr(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
{
return EM_ASM_INT({
console.log('GET ATTR', $0, $1);
return 0;
}, attr, return_value);
}
static void pdo_cfd1_request_shutdown(pdo_dbh_t *dbh)
{
EM_ASM({ console.log('SHUTDOWN'); });
}
static void pdo_cfd1_get_gc(pdo_dbh_t *dbh, zend_get_gc_buffer *gc_buffer)
{
EM_ASM({ console.log('GET GC', $0);}, gc_buffer);
}
static const struct pdo_dbh_methods cfd1_db_methods = {
cfd1_handle_closer,
cfd1_handle_preparer,
cfd1_handle_doer,
cfd1_handle_quoter,
cfd1_handle_begin,
cfd1_handle_commit,
cfd1_handle_rollback,
pdo_cfd1_set_attr,
pdo_cfd1_last_insert_id,
pdo_cfd1_fetch_error_func,
pdo_cfd1_get_attr,
NULL, /* check_liveness: not needed */
NULL, //get_driver_methods,
pdo_cfd1_request_shutdown,
NULL, /* in transaction, use PDO's internal tracking mechanism */
pdo_cfd1_get_gc
};
static int pdo_cfd1_db_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
{
pdo_cfd1_db_handle *handle;
handle = pecalloc(1, sizeof(pdo_cfd1_db_handle), dbh->is_persistent);
handle->einfo.errcode = 0;
handle->einfo.errmsg = NULL;
dbh->driver_data = handle;
dbh->methods = &cfd1_db_methods;
EM_ASM({
if(typeof Module.cfd1 !== 'object')
{
throw new Error("The `cfd1` object must be provided as a constructor arg to PHP to use pdo_cfd1.");
}
const dbName = UTF8ToString($0);
if(typeof Module.cfd1[dbName] !== 'object')
{
throw new Error(`The value provided at cfd1[${dbName}] does not exist or is not an object.`);
}
}, dbh->data_source);
return 1;
}
const pdo_driver_t pdo_cfd1_driver = {
PDO_DRIVER_HEADER(cfd1),
pdo_cfd1_db_handle_factory
};