green_copper_runtime/modules/db/mysql/
mod.rs

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
//!
//! # The MYSQL module
//!
//! work in progress
//!
//! ## Example
//!
//! ```javascript
//!!
//! async function test_mysql(){
//!     let mysql_mod = await import('greco://mysql');
//!   
//!     let url = "localhost";
//!     let port = 3306;
//!     let user = null;
//!     let pass = null;
//!     let dbSchema = "esses";
//!     let con = new mysql_mod.Connection(url, port, user, pass, dbSchema);
//!     // params and batches
//!     await con.execute("insert into table1(name, lastName) values(:name, :lastName)", {name: 'Pete', lastName: 'Peterson'}, {name: 'Andrew', lastName: 'Anderson'});
//!
//!     let q1_objects = await con.query("select * from something where id > :id and age < :age", {id: 1, age: 123}, (name, age) => {
//!         console.log("a name = %s, age = %s", name, age);
//!         return {name, age};
//!     });
//!     // q1_objects is an array like [{name: 'n1', age: 21}, {name: 'n2', age: 37}, etc..]
//!
//!     let transaction = await con.transaction();
//!     try {
//!         // all methods return a promise so execution never blocks
//!         await transaction.execute("insert into table1(name) values(:name)", {name: 'Harry'}, {name: 'Henry'});
//!         let ct_rows = await transaction.query("select count(*) from table1", [], (ct) => {
//!             return {count: ct};
//!         });
//!         await transaction.commit();
//!     } finally {
//!         // returns a promise await for error handling.
//!         await transaction.close();
//!     }
//! }
//!
//! test_mysql().then(() => {
//!     console.log("done");
//! }).catch((ex) => {
//!     console.log("test failed: %s", ex);
//! });
//!
//! ```
//!

use crate::modules::db::mysql::connection::MysqlConnection;
use crate::modules::db::mysql::transaction::MysqlTransaction;
use hirofa_utils::auto_id_map::AutoIdMap;
use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use quickjs_runtime::jsutils::jsproxies::JsProxy;
use quickjs_runtime::jsutils::modules::NativeModuleLoader;
use quickjs_runtime::jsutils::JsError;
use quickjs_runtime::quickjsrealmadapter::QuickJsRealmAdapter;
use quickjs_runtime::quickjsvalueadapter::QuickJsValueAdapter;
use std::cell::RefCell;
use std::collections::HashMap;

pub mod connection;
pub mod transaction;

thread_local! {
    pub static CONNECTIONS: RefCell<HashMap< usize, MysqlConnection>> = RefCell::new(HashMap::new());
    pub static TRANSACTIONS: RefCell<AutoIdMap<MysqlTransaction>> = RefCell::new(AutoIdMap::new());
}

fn with_connection<R, C: FnOnce(&MysqlConnection) -> R>(
    proxy_instance_id: &usize,
    consumer: C,
) -> R {
    CONNECTIONS.with(|rc| {
        let map = &*rc.borrow();
        let con: &MysqlConnection = map.get(proxy_instance_id).expect("no such Connection");
        consumer(con)
    })
}

fn with_transaction<R, C: FnOnce(&MysqlTransaction) -> R>(
    proxy_instance_id: &usize,
    consumer: C,
) -> R {
    TRANSACTIONS.with(|rc| {
        let map = &*rc.borrow();
        let con: &MysqlTransaction = map.get(proxy_instance_id).expect("no such Transaction");
        consumer(con)
    })
}

fn with_transaction_mut<R, C: FnOnce(&mut MysqlTransaction) -> R>(
    proxy_instance_id: &usize,
    consumer: C,
) -> R {
    TRANSACTIONS.with(|rc| {
        let map = &mut *rc.borrow_mut();
        let con: &mut MysqlTransaction =
            map.get_mut(proxy_instance_id).expect("no such Transaction");
        consumer(con)
    })
}

fn store_connection(proxy_instance_id: usize, con: MysqlConnection) {
    CONNECTIONS.with(|rc| {
        let map = &mut *rc.borrow_mut();
        log::debug!("store_connection, len = {}", map.len());
        map.insert(proxy_instance_id, con);
    })
}

fn drop_connection(proxy_instance_id: &usize) {
    CONNECTIONS.with(|rc| {
        let map = &mut *rc.borrow_mut();
        log::debug!("drop_connection, len = {}", map.len());
        map.remove(proxy_instance_id);
    })
}

fn store_transaction(tx: MysqlTransaction) -> usize {
    TRANSACTIONS.with(|rc| {
        let map = &mut *rc.borrow_mut();
        log::debug!("store_transaction, len = {}", map.len());
        map.insert(tx)
    })
}

fn drop_transaction(proxy_instance_id: &usize) {
    TRANSACTIONS.with(|rc| {
        let map = &mut *rc.borrow_mut();
        log::debug!("drop_transaction, len = {}", map.len());
        map.remove(proxy_instance_id);
    })
}

struct MysqlModuleLoader {}

impl NativeModuleLoader for MysqlModuleLoader {
    fn has_module(&self, _realm: &QuickJsRealmAdapter, module_name: &str) -> bool {
        module_name.eq("greco://mysql")
    }

    fn get_module_export_names(
        &self,
        _realm: &QuickJsRealmAdapter,
        _module_name: &str,
    ) -> Vec<&str> {
        vec!["Connection"]
    }

    fn get_module_exports(
        &self,
        realm: &QuickJsRealmAdapter,
        _module_name: &str,
    ) -> Vec<(&str, QuickJsValueAdapter)> {
        init_exports(realm).expect("init mysql exports failed")
    }
}

pub(crate) fn init(builder: QuickJsRuntimeBuilder) -> QuickJsRuntimeBuilder {
    builder.native_module_loader(MysqlModuleLoader {})
}

fn init_exports(
    realm: &QuickJsRealmAdapter,
) -> Result<Vec<(&'static str, QuickJsValueAdapter)>, JsError> {
    let mysql_connection_proxy_class = create_mysql_connection_proxy(realm);
    let mysql_transaction_proxy_class = create_mysql_transaction_proxy(realm);
    let con_res = realm.install_proxy(mysql_connection_proxy_class, false)?;
    let tx_res = realm.install_proxy(mysql_transaction_proxy_class, false)?;

    Ok(vec![("Connection", con_res), ("Transaction", tx_res)])
}

pub(crate) fn create_mysql_transaction_proxy(_realm: &QuickJsRealmAdapter) -> JsProxy {
    JsProxy::new().namespace(&["greco", "db", "mysql"]).name("Transaction")
        .event_target()
        .method("commit", |runtime, realm, id, _args| {
            with_transaction_mut( id, |tx| {
                tx.commit(runtime, realm, *id)
            })
        })
        .method("rollback", |runtime, realm, id, _args| {
            with_transaction_mut( id, |tx| {
                tx.rollback(runtime, realm)
            })
        })
        .method("close", |runtime, realm, id, _args| {
            with_transaction( id, |tx| {
                tx.close_tx(runtime, realm)
            })
        })
        .method("query", |runtime, realm, id, args| {

            // todo think up a macro for this?
            // 3 args, second may be null
            if args.len() != 3 {
                Err(JsError::new_str("query requires 3 arguments (query: String, params: Object, rowConsumer: Function)"))
            } else {
                // todo

                let query = args[0].to_string()?;

                let params = &args[1];
                let row_consumer = &args[2];

                with_transaction( id, |tx| {
                    tx.query(runtime, realm, query.as_str(), params, row_consumer)
                })
            }

        })
        .method("execute", |runtime, realm, id, args| {
            // todo think up a macro for this?
            // 3 args, second may be null
            if args.len() < 2 {
                Err(JsError::new_str("execute requires at least 2 arguments (query: String, ...params: Array<Object>)"))
            } else {
                // todo

                let query = args[0].to_string()?;

                let params: Vec<&QuickJsValueAdapter> = args[1..args.len()].iter().collect();

                with_transaction( id, |tx| {
                    tx.execute(runtime, realm, query.as_str(), &params)
                })
            }

        })
        .finalizer(|_rt, _realm, id| {
            drop_transaction(&id);
        })
}

pub(crate) fn create_mysql_connection_proxy(_realm: &QuickJsRealmAdapter) -> JsProxy {
    JsProxy::new().namespace(&["greco", "db", "mysql"]).name("Connection")
        .constructor(|runtime, realm, instance_id, args| {
            let con = MysqlConnection::new(runtime, realm, args)?;
            store_connection(instance_id, con);
            Ok(())
        })
        .method("transaction", |_runtime, realm, id, _args| {
            // todo options like isolation/readonly
            with_connection( id, |con| {
                 con.start_transaction(realm)
            })
        })
        .method("query", |runtime, realm, id, args| {
            // todo think up a macro for this?
            // 3 args, second may be null
            if args.len() != 3 {
                Err(JsError::new_str("query requires 3 arguments (query: String, params: Object, rowConsumer: Function)"))
            } else {
                // todo

                let query = args[0].to_string()?;

                let params = &args[1];
                let row_consumer = &args[2];

                let con = with_connection( id, |con| con.clone());
                con.query(runtime, realm, query.as_str(), params, row_consumer)
            }

        })
        .method("execute", |runtime, realm, id, args| {
            // todo think up a macro for this?
            // 3 args, second may be null
            if args.len() < 2 {
                Err(JsError::new_str("execute requires at least 2 arguments (query: String, ...params: Array<Object>)"))
            } else {
                // todo

                let query = args[0].to_string()?;

                let params: Vec<&QuickJsValueAdapter> = args[1..args.len()].iter().collect();

                with_connection( id, |con| {
                    con.execute(runtime, realm, query.as_str(), &params)
                })
            }

        })
        .finalizer(|_rt, _realm, id| {
            drop_connection(&id);
        })
}

#[cfg(test)]
pub mod tests {
    use futures::executor::block_on;
    //use log::LevelFilter;
    use quickjs_runtime::builder::QuickJsRuntimeBuilder;
    use quickjs_runtime::jsutils::Script;
    use quickjs_runtime::values::{JsValueFacade, TypedArrayType};

    //#[test]
    fn _test_params() {
        //simple_logging::log_to_stderr(log::LevelFilter::Info);

        let builder = QuickJsRuntimeBuilder::new();
        let builder = crate::init_greco_rt(builder);
        let rt = builder.build();

        //simple_logging::log_to_stderr(log::LevelFilter::Trace);

        let script = Script::new(
            "test_mysql.js",
            r#"
        
        async function test() {
            let mysqlMod = await import('greco://mysql');
            let host = '192.168.10.43';
            let port = 3306;
            let user = 'hirofa';
            let pass = 'hirofa';
            let db = 'hirofa_testdb';
            let con = new mysqlMod.Connection(host, port, user, pass, db);
            
            await con.execute(`DROP TABLE IF EXISTS test`, []);
            await con.execute(`
                CREATE TABLE test(
                    \`id\` INT auto_increment PRIMARY KEY,
                    \`test\` VARCHAR(32),
                    \`uuid\` UUID,
                    \`when\` DATE,
                    \`data\` LONGBLOB
                )
            `, []);
            
            let grecoDom = await import("greco://htmldom");
            let ParserClass = grecoDom.DOMParser;
            let parser = new ParserClass();
            let ps = [];
            for (let x = 0; x < 500000; x++) {
                ps.push("<p>hello world</p>");
            }
            let htmlDoc = parser.parseFromString(`<html><body>${ps.join("\n")}</body></html>`);

            for (let x = 0; x < 50; x++) {
            
                const bytes = htmlDoc.encodeHTML();
            
                let tx = await con.transaction();
                
                await tx.execute(`
                    INSERT INTO test(\`test\`, \`uuid\`, \`when\`, \`data\`) VALUES('hi1', '0000-0000-0000-00000000-000000000000', CURDATE(), :data) ON DUPLICATE KEY UPDATE data = :data, \`when\` = CURDATE()
                `, {data: bytes});
                
                let returnData = null;
                await tx.query("SELECT data from test where test='hi1' LIMIT 0,1", [], (data) => {
                    returnData = data;
                });
                console.log("got data %s", returnData.length);
                
                await tx.commit();
                console.log("x=%s", x);
                
            }
            
            
            await con.execute(`
                INSERT into test(\`test\`, \`uuid\`, \`when\`, \`data\`) VALUES('hi2', '0000-0000-0000-00000000-000000000000', CURDATE(), ?)
            `, [bytes]);
            
            
            await con.query('select * from test where \`test\` = ?', ['hi2'], (...row) => {
                for (let x = 0; x < row.length; x++) {
                    console.log('col %s = %s (typeof = %s)', x, row[x].length, typeof row[x]);
                }
            });
            
            await con.query('select * from test where \`test\` = :a', {a: 'hi2'}, (...row) => {
                for (let x = 0; x < row.length; x++) {
                    console.log('named col %s = %s', x, row[x].length);
                }
            });
            
            await con.query('select * from test', null, (...row) => {
                for (let x = 0; x < row.length; x++) {
                    console.log('noparams col %s = %s', x, row[x].length);
                }
            });
            
            let ct = await con.execute('delete from test', []);
            console.log('deleted rows %s', ct);
            ct = await con.execute('delete from test', []);
            console.log('deleted rows %s', ct);
            
            await con.execute('insert into test(id) values(?)', [4], [8], [12]);
            
            let tables = await con.query('show tables', null, (row1) => {
                return row1;
            });
            
            for (let table of tables) {
                console.log('found table %s', table);            
            }
            
        }
        
        test()
        
        "#,
        );
        let res: JsValueFacade = block_on(rt.eval(None, script))
            .map_err(|e| {
                println!("{}", e);
                e
            })
            .map_err(|e| {
                println!("{}", e);
                e
            })
            .ok()
            .expect("script failed");

        println!("{}", res.stringify());
        if let JsValueFacade::JsPromise { cached_promise } = res {
            let p_res = block_on(cached_promise.get_promise_result())
                .ok()
                .expect("get prom res failed");
            match p_res {
                Ok(jsvf) => {
                    println!("prom resolved to {}", jsvf.stringify());
                }
                Err(e) => {
                    panic!("prom rejected: {}", e.stringify());
                }
            }
        } else {
            panic!("did not get a promise");
        }
    }

    //#[test]
    fn _test_params_2() {
        //simple_logging::log_to_stderr(log::LevelFilter::Info);

        let builder = QuickJsRuntimeBuilder::new();
        let builder = crate::init_greco_rt(builder);
        let rt = builder.build();

        //simple_logging::log_to_stderr(log::LevelFilter::Trace);

        let script = Script::new(
            "test_mysql.js",
            r#"
        
        globalThis.test = async function(bytes) {
            let mysqlMod = await import('greco://mysql');
            let host = '192.168.10.43';
            let port = 3306;
            let user = 'hirofa';
            let pass = 'hirofa';
            let db = 'hirofa_testdb';
            let con = new mysqlMod.Connection(host, port, user, pass, db);
            
            await con.execute(`DROP TABLE IF EXISTS test`, []);
            await con.execute(`
                CREATE TABLE test(
                    \`id\` INT auto_increment PRIMARY KEY,
                    \`test\` VARCHAR(32),
                    \`uuid\` UUID,
                    \`when\` DATE,
                    \`data\` LONGBLOB
                )
            `, []);
            
            
            await con.execute(`
                INSERT into test(\`test\`, \`uuid\`, \`when\`, \`data\`) VALUES('hi1', '0000-0000-0000-00000000-000000000000', CURDATE(), ?)
            `, [bytes]);
            
            await con.execute(`
                INSERT into test(\`test\`, \`uuid\`, \`when\`, \`data\`) VALUES('hi2', '0000-0000-0000-00000000-000000000000', CURDATE(), ?)
            `, [bytes]);
            
            let rows = await con.query('select data from test where \`test\` = ?', ['hi2'], (row) => {
                return row;
            });
            
            let foo = await con.query('select data from test where \`test\` = \'hi2\' LIMIT :offset, :limit', {offset: 0, limit: 5}, (row) => {
                return row;
            });
            
            return rows;
            
        }
                
        "#,
        );
        let res: JsValueFacade = block_on(rt.eval(None, script))
            .map_err(|e| {
                println!("{}", e);
                e
            })
            .map_err(|e| {
                println!("{}", e);
                e
            })
            .ok()
            .expect("script failed");
        println!("{}", res.stringify());
        for i in 0..2 {
            log::info!("{i}");
            let mut buffer = vec![];
            for x in 0..(20 * 1024 * 1024) {
                buffer.push(x as u8);
            }
            let bytes = JsValueFacade::TypedArray {
                buffer,
                array_type: TypedArrayType::Uint8,
            };
            let res = rt
                .invoke_function_sync(None, &[], "test", vec![bytes])
                .expect("script failed");

            if let JsValueFacade::JsPromise { cached_promise } = res {
                let p_res = block_on(cached_promise.get_promise_result())
                    .ok()
                    .expect("get prom res failed");
                match p_res {
                    Ok(jsvf) => {
                        log::info!("prom {i} resolved to {}", jsvf.stringify());
                    }
                    Err(e) => {
                        panic!("prom rejected: {}", e.stringify());
                    }
                }
            } else {
                panic!("did not get a promise");
            }
        }
    }
}