-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql.html
833 lines (821 loc) · 44.2 KB
/
mysql.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title> mysql - mysql database client </title>
<script src="jquery.js"></script>
<script src="jquery-cookie.js"></script>
<script src="jquery-tablesorter.js"></script>
<script src="strftime.js"></script>
<script src="mustache.js"></script>
<script src="config.js"></script>
<script src="main.js"></script>
<link rel="stylesheet" type="text/css" href="templates/reset.css" />
<link rel="stylesheet" type="text/css" href="templates/hack.css" />
<link rel="stylesheet" type="text/css" id="lights_css" />
<script>
var DOCNAME = 'mysql'
var PROJECT = 'mysql'
//set the lights before rendering starts
set_lights()
</script>
</head>
<body>
<header>
<div class="container">
<div class="btn-container btn-lights-container">
<a href="#" class="btn btn-lights" id="lights">lights</a>
</div>
<div class="btn-container btn-home-container">
<a href="/" class="btn btn-home">luapower</a>
</div>
<table id="header_table">
<tr>
<td style="vertical-align: middle;" width="100%">
<h1> mysql </h1>
<h2> mysql database client </h2>
</td>
<td style="vertical-align: middle;" align="right" style="height: 150px">
<table><tr><td>
<div class="doc" id="package_info_container">
<div id="package_info"> </div>
<div id="commit_log"> </div>
</div>
<a href="https://github.com/luapower/mysql" class="btn btn-rightside btn-github"><span class="icon"></span>View on GitHub</a>
</td></tr><tr><td>
<a href="https://github.com/luapower/mysql/tarball/master" class="btn btn-rightside">Download as .tar.gz</a>
</td></tr><tr><td>
<a href="https://github.com/luapower/mysql/zipball/master" class="btn btn-rightside">Download as .zip</a>
</td></tr></table>
</td>
</tr>
</table>
<div class="btn-container btn-discuss-container">
<a href="https://github.com/luapower/mysql/issues/new" target="_blank"
class="btn btn-rightside btn-discuss"><span class="icon"></span>Discuss</a>
</div>
</div>
</header>
<div class="bg-container">
<div class="bg-center-container">
<div class="bg bg-mysql" style="background-image: url('bg/mysql.png');"></div>
</div>
</div>
<div class="under-header">
<div class="container">
<div id="toc_container" class="toc_container doc"></div>
<button id=tab1_button class="tab_button hidden">Documentation</button>
<button id=tab2_button class="tab_button hidden">Package Info</button>
<div id="tabs_cointainer">
<div id="tab1_container">
<section class="doc">
<span id="module_info"></span>
<h2 id="local-mysql-requiremysql"><code>local mysql = require'mysql'</code></h2>
<p>A complete, lightweight ffi binding of the mysql client library.</p>
<blockquote>
<p>NOTE: binaries are in separate packages <a href="libmysql.html">libmysql</a> and <a href="libmariadb.html">libmariadb</a>.</p>
</blockquote>
<h2 id="summary">Summary</h2>
<table>
<tbody>
<tr class="odd">
<td align="left"><strong><a href="#connections">Connections</a></strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>mysql.connect(host, [user], [pass], [db], [charset], [port]) -> conn</code></td>
<td align="left">connect to a mysql server</td>
</tr>
<tr class="odd">
<td align="left"><code>mysql.connect(options_t) -> conn</code></td>
<td align="left">connect to a mysql server</td>
</tr>
<tr class="even">
<td align="left"><code>conn:close()</code></td>
<td align="left">close the connection</td>
</tr>
<tr class="odd">
<td align="left"><strong><a href="#queries">Queries</a></strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>conn:query(s)</code></td>
<td align="left">execute a query</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:escape(s) -> s</code></td>
<td align="left">escape an SQL string</td>
</tr>
<tr class="even">
<td align="left"><strong><a href="#fetching-results">Fetching results</a></strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>conn:store_result() -> result</code></td>
<td align="left">get a cursor for buffered read (<a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-store-result.html">manual</a>)</td>
</tr>
<tr class="even">
<td align="left"><code>conn:use_result() -> result</code></td>
<td align="left">get a cursor for unbuffered read (<a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-use-result.html">manual</a>)</td>
</tr>
<tr class="odd">
<td align="left"><code>result:fetch([mode[, row_t]]) -> true, v1, v2, ... | row_t | nil</code></td>
<td align="left">fetch the next row from the result</td>
</tr>
<tr class="even">
<td align="left"><code>result:rows([mode[, row_t]]) -> iterator() -> row_num, val1, val2, ...</code></td>
<td align="left">row iterator</td>
</tr>
<tr class="odd">
<td align="left"><code>result:rows([mode[, row_t]]) -> iterator() -> row_num, row_t</code></td>
<td align="left">row iterator</td>
</tr>
<tr class="even">
<td align="left"><code>result:free()</code></td>
<td align="left">free the cursor</td>
</tr>
<tr class="odd">
<td align="left"><code>result:row_count() -> n</code></td>
<td align="left">number of rows</td>
</tr>
<tr class="even">
<td align="left"><code>result:eof() -> true | false</code></td>
<td align="left">check if no more rows</td>
</tr>
<tr class="odd">
<td align="left"><code>result:seek(row_number)</code></td>
<td align="left">seek to row number</td>
</tr>
<tr class="even">
<td align="left"><strong><a href="#query-info">Query info</a></strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>conn:field_count() -> n</code></td>
<td align="left">number of result fields in the executed query</td>
</tr>
<tr class="even">
<td align="left"><code>conn:affected_rows() -> n</code></td>
<td align="left">number of affected rows in the executed query</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:insert_id() -> n</code></td>
<td align="left">the id of the autoincrement column in the executed query</td>
</tr>
<tr class="even">
<td align="left"><code>conn:errno() -> n</code></td>
<td align="left">mysql error code (0 if no error) from the executed query</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:sqlstate() -> s</code></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>conn:warning_count() -> n</code></td>
<td align="left">number of errors, warnings, and notes from executed query</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:info() -> s</code></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><strong><a href="#field-info">Field info</a></strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>result:field_count() -> n</code></td>
<td align="left">number of fields in the result</td>
</tr>
<tr class="even">
<td align="left"><code>result:field_name(field_number) -> s</code></td>
<td align="left">field name given field index</td>
</tr>
<tr class="odd">
<td align="left"><code>result:field_type(field_number) -> type, length, unsigned, decimals</code></td>
<td align="left">field type given field index</td>
</tr>
<tr class="even">
<td align="left"><code>result:field_info(field_number) -> info_t</code></td>
<td align="left">field info table</td>
</tr>
<tr class="odd">
<td align="left"><code>result:fields() -> iterator() -> i, info_t</code></td>
<td align="left">field info iterator</td>
</tr>
<tr class="even">
<td align="left"><strong><a href="#result-bookmarks">Result bookmarks</a></strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>result:tell() -> bookmark</code></td>
<td align="left">bookmark the current row for later seek</td>
</tr>
<tr class="even">
<td align="left"><code>result:seek(bookmark)</code></td>
<td align="left">seek to a row bookmark</td>
</tr>
<tr class="odd">
<td align="left"><strong><a href="#multiple-statement-queries">Multiple statement queries</a></strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>conn:next_result() -> true | false</code></td>
<td align="left">skip to the next result set (<a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-next-result.html">manual</a>)</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:more_results() -> true | false</code></td>
<td align="left">are there more result sets?</td>
</tr>
<tr class="even">
<td align="left"><strong><a href="#prepared-statements">Prepared statements</a></strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>conn:prepare(query) -> stmt</code></td>
<td align="left">prepare a query for multiple executions</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:param_count() -> n</code></td>
<td align="left">number of params</td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:exec()</code></td>
<td align="left">execute a prepared statement</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:store_result()</code></td>
<td align="left">store all the resulted rows to the client</td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:fetch() -> true | false | true, 'truncated'</code></td>
<td align="left">fetch the next row</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:free_result()</code></td>
<td align="left">free the current result buffers</td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:close()</code></td>
<td align="left">close the statement</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:next_result()</code></td>
<td align="left">skip to the next result set</td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:row_count() -> n</code></td>
<td align="left">number of rows in the result, if the result was stored</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:affected_rows() -> n</code></td>
<td align="left">number of affected rows after execution</td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:insert_id() -> n</code></td>
<td align="left">the id of the autoincrement column after execution</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:field_count() -> n</code></td>
<td align="left">number of fields in the result after execution</td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:errno() -> n</code></td>
<td align="left">mysql error code, if any, from the executed statement</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:sqlstate() -> s</code></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:result_metadata() -> result</code></td>
<td align="left">get a result for accessing the field info</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:fields() -> iterator() -> i, info_t</code></td>
<td align="left">iterate the result fields info</td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:reset()</code></td>
<td align="left">see <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-reset.html">manual</a></td>
</tr>
<tr class="even">
<td align="left"><code>stmt:seek(row_number)</code></td>
<td align="left">seek to row number</td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:tell() -> bookmark</code></td>
<td align="left">get a bookmark in the current result</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:seek(bookmark)</code></td>
<td align="left">seek to a row bookmark in the current result</td>
</tr>
<tr class="odd">
<td align="left"><strong><a href="#prepared-statements-io">Prepared statements I/O</a></strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>stmt:bind_params(type1, ... | types_t) -> params</code></td>
<td align="left">bind query parameters based on type definitions</td>
</tr>
<tr class="odd">
<td align="left"><code>params:set(i, number | int64_t | uint64_t | true | false)</code></td>
<td align="left">set an integer, float or bit param</td>
</tr>
<tr class="even">
<td align="left"><code>params:set(i, s[, size])</code></td>
<td align="left">set a variable sized param</td>
</tr>
<tr class="odd">
<td align="left"><code>params:set(i, cdata, size)</code></td>
<td align="left">set a variable sized param</td>
</tr>
<tr class="even">
<td align="left"><code>params:set(i, {year=, month=, ...})</code></td>
<td align="left">set a time/date param</td>
</tr>
<tr class="odd">
<td align="left"><code>params:set_date(i, [year], [month], [day], [hour], [min], [sec], [frac])</code></td>
<td align="left">set a time/date param</td>
</tr>
<tr class="even">
<td align="left"><code>stmt:write(param_number, data[, size])</code></td>
<td align="left">send a long param in chunks</td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:bind_result([type1, ... | types_t | maxsize]) -> fields</code></td>
<td align="left">bind query result fields based on type definitions</td>
</tr>
<tr class="even">
<td align="left"><code>fields:get(i) -> value</code></td>
<td align="left">get the current row value of a field</td>
</tr>
<tr class="odd">
<td align="left"><code>fields:get_datetime(i) -> year, month, day, hour, min, sec, frac</code></td>
<td align="left">get the value of a date/time field directly</td>
</tr>
<tr class="even">
<td align="left"><code>fields:is_null(i) -> true | false</code></td>
<td align="left">is field null?</td>
</tr>
<tr class="odd">
<td align="left"><code>fields:is_truncated(i) -> true | false</code></td>
<td align="left">was field value truncated?</td>
</tr>
<tr class="even">
<td align="left"><strong><a href="#prepared-statements-settings">Prepared statements settings</a></strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:update_max_length() -> true | false</code></td>
<td align="left">see <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-attr-set.html">manual</a></td>
</tr>
<tr class="even">
<td align="left"><code>stmt:set_update_max_length(true | false)</code></td>
<td align="left">see <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-attr-set.html">manual</a></td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:cursor_type() -> mysql.C.MYSQL_CURSOR_TYPE_*</code></td>
<td align="left">see <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-attr-set.html">manual</a></td>
</tr>
<tr class="even">
<td align="left"><code>stmt:set_cursor_type('CURSOR_TYPE_...')</code></td>
<td align="left">see <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-attr-set.html">manual</a></td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:set_cursor_type(mysql.C.MYSQL_CURSOR_TYPE_...)</code></td>
<td align="left">see <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-attr-set.html">manual</a></td>
</tr>
<tr class="even">
<td align="left"><code>stmt:prefetch_rows() -> n</code></td>
<td align="left">see <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-attr-set.html">manual</a></td>
</tr>
<tr class="odd">
<td align="left"><code>stmt:set_prefetch_rows(stmt, n)</code></td>
<td align="left">see <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-attr-set.html">manual</a></td>
</tr>
<tr class="even">
<td align="left"><strong><a href="#connection-info">Connection info</a></strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>conn:set_charset(charset)</code></td>
<td align="left">change the current charset</td>
</tr>
<tr class="even">
<td align="left"><code>conn:select_db(dbname)</code></td>
<td align="left">change the current database</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:change_user(user, [pass], [db])</code></td>
<td align="left">change the current user (and database)</td>
</tr>
<tr class="even">
<td align="left"><code>conn:set_multiple_statements(true | false)</code></td>
<td align="left">enable/disable support for multiple statements</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:charset() -> s</code></td>
<td align="left">get current charset's name</td>
</tr>
<tr class="even">
<td align="left"><code>conn:charset_info() -> info_t</code></td>
<td align="left">get info about the current charset</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:ping() -> true | false</code></td>
<td align="left">check if the connection is still alive</td>
</tr>
<tr class="even">
<td align="left"><code>conn:thread_id() -> id</code></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>conn:stat() -> s</code></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>conn:server_info() -> s</code></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>conn:host_info() -> s</code></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>conn:server_version() -> n</code></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>conn:proto_info() -> n</code></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>conn:ssl_cipher() -> s</code></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><strong><a href="#transactions">Transactions</a></strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>conn:commit()</code></td>
<td align="left">commit the current transaction</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:rollback()</code></td>
<td align="left">rollback the current transaction</td>
</tr>
<tr class="even">
<td align="left"><code>conn:set_autocommit([true | false])</code></td>
<td align="left">enable/disable autocommit on the current connection</td>
</tr>
<tr class="odd">
<td align="left"><strong><a href="#reflection">Reflection</a></strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>conn:list_dbs([wildcard]) -> result</code></td>
<td align="left">return info about databases as a result object</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:list_tables([wildcard]) -> result</code></td>
<td align="left">return info about tables as a result object</td>
</tr>
<tr class="even">
<td align="left"><code>conn:list_processes() -> result</code></td>
<td align="left">return info about processes as a result object</td>
</tr>
<tr class="odd">
<td align="left"><strong><a href="#remote-control">Remote control</a></strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>conn:kill(pid)</code></td>
<td align="left">kill a connection based on process id</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:shutdown([level])</code></td>
<td align="left">shutdown the server</td>
</tr>
<tr class="even">
<td align="left"><code>conn:refresh(options)</code></td>
<td align="left">flush tables or caches</td>
</tr>
<tr class="odd">
<td align="left"><code>conn:dump_debug_info()</code></td>
<td align="left">dump debug info in the log file</td>
</tr>
<tr class="even">
<td align="left"><strong><a href="#client-library-info">Client library info</a></strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>mysql.thread_safe() -> true | false</code></td>
<td align="left">was the client library compiled as thread-safe?</td>
</tr>
<tr class="even">
<td align="left"><code>mysql.client_info() -> s</code></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>mysql.client_version() -> n</code></td>
<td align="left"></td>
</tr>
</tbody>
</table>
<h2 id="features">Features</h2>
<ul>
<li>covers all of the functionality provided by the mysql C API</li>
<li>all data types are supported with options for conversion</li>
<li>prepared statements, avoiding dynamic allocations and format conversions when fetching rows</li>
<li>all C calls are checked for errors and Lua errors are raised</li>
<li>all C objects are tied to Lua's garbage collector</li>
<li>lightweight OOP-style API using only <code>ffi.metatype</code></li>
<li>no external dependencies</li>
</ul>
<h2 id="example">Example</h2>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">function</span> print_help<span class="ot">(</span><span class="kw">search</span><span class="ot">)</span>
<span class="kw">local</span> <span class="kw">mysql</span> <span class="ot">=</span> require<span class="st">'mysql'</span>
<span class="kw">local</span> <span class="kw">conn</span> <span class="ot">=</span> <span class="kw">mysql</span><span class="ot">.</span>connect<span class="ot">(</span><span class="st">'localhost'</span><span class="ot">,</span> <span class="st">'root'</span><span class="ot">,</span> <span class="kw">nil</span><span class="ot">,</span> <span class="st">'mysql'</span><span class="ot">,</span> <span class="st">'utf8'</span><span class="ot">)</span>
<span class="kw">conn</span>:query<span class="ot">(</span><span class="st">"select name, description, example from help_topic where name like '"</span> <span class="ot">..</span>
<span class="kw">conn</span>:escape<span class="ot">(</span><span class="kw">search</span><span class="ot">)</span> <span class="ot">..</span> <span class="st">"'"</span><span class="ot">)</span>
<span class="kw">local</span> <span class="kw">result</span> <span class="ot">=</span> <span class="kw">conn</span>:store_result<span class="ot">()</span>
<span class="fu">print</span><span class="ot">(</span><span class="st">'Found:'</span><span class="ot">)</span>
<span class="kw">for</span> <span class="kw">i</span><span class="ot">,</span><span class="kw">name</span> <span class="kw">in</span> <span class="kw">result</span>:rows<span class="ot">()</span> <span class="kw">do</span>
<span class="fu">print</span><span class="ot">(</span><span class="st">' '</span> <span class="ot">..</span> <span class="kw">name</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="fu">print</span><span class="ot">()</span>
<span class="kw">for</span> <span class="kw">i</span><span class="ot">,</span> <span class="kw">name</span><span class="ot">,</span> <span class="kw">description</span><span class="ot">,</span> <span class="kw">example</span> <span class="kw">in</span> <span class="kw">result</span>:rows<span class="ot">()</span> <span class="kw">do</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">name</span><span class="ot">)</span>
print<span class="st">'-------------------------------------------'</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">description</span><span class="ot">)</span>
print<span class="st">'Example:'</span>
print<span class="st">'-------------------------------------------'</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">example</span><span class="ot">)</span>
<span class="fu">print</span><span class="ot">()</span>
<span class="kw">end</span>
<span class="kw">result</span>:free<span class="ot">()</span>
<span class="kw">conn</span>:<span class="fu">close</span><span class="ot">()</span>
<span class="kw">end</span>
print_help<span class="st">'CONCAT%'</span></code></pre>
<h2 id="connections">Connections</h2>
<h2 id="mysql.connecthost-user-pass-db-charset-port---conn"><code>mysql.connect(host, [user], [pass], [db], [charset], [port]) -> conn</code></h2>
<h2 id="mysql.connectoptions_t---conn"><code>mysql.connect(options_t) -> conn</code></h2>
<p>Connect to a mysql server, optionally selecting a working database and charset.</p>
<p>In the second form, <code>options_t</code> is a table that besides <code>host</code>, <code>user</code>, <code>pass</code>, <code>db</code>, <code>charset</code>, <code>port</code> can have the following fields:</p>
<ul>
<li><code>unix_socket</code>: specify a unix socket filename to connect to</li>
<li><code>flags</code>: bit field corresponding to mysql <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-real-connect.html">client_flag</a> parameter
<ul>
<li>can be a table of form <code>{CLIENT_... = true | false, ...}</code>, or</li>
<li>a number of form <code>bit.bor(mysql.C.CLIENT_..., ...)</code></li>
</ul></li>
<li><code>options</code>: a table of form <code>{MYSQL_OPT_... = value, ...}</code>, containing options per <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-options.html">mysql_options()</a> (values are properly converted from Lua types)</li>
<li><code>attrs</code>: a table of form <code>{attr = value, ...}</code> containing attributes to be passed to the server per <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-options4.html">mysql_options4()</a></li>
<li><code>key</code>, <code>cert</code>, <code>ca</code>, <code>cpath</code>, <code>cipher</code>: parameters used to establish a <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-ssl-set.html">SSL connection</a></li>
</ul>
<h2 id="connclose"><code>conn:close()</code></h2>
<p>Close a mysql connection freeing all associated resources (otherwise called when <code>conn</code> is garbage collected).</p>
<h2 id="queries">Queries</h2>
<h2 id="connquerys"><code>conn:query(s)</code></h2>
<p>Execute a query. If the query string contains multiple statements, only the first statement is executed (see the section on multiple statements).</p>
<h2 id="connescapes---s"><code>conn:escape(s) -> s</code></h2>
<p>Escape a value to be safely embedded in SQL queries. Assumes the current charset.</p>
<h2 id="fetching-results">Fetching results</h2>
<h2 id="connstore_result---result"><code>conn:store_result() -> result</code></h2>
<p>Fetch all the rows in the current result set from the server and return a result object to read them one by one.</p>
<h2 id="connuse_result---result"><code>conn:use_result() -> result</code></h2>
<p>Return a result object that will fetch the rows in the current result set from the server on demand.</p>
<h2 id="resultfetchmode-row_t---true-v1-v2-...-row_t-nil"><code>result:fetch([mode[, row_t]]) -> true, v1, v2, ... | row_t | nil</code></h2>
<p>Fetch and return the next row of values from the current result set. Returns nil if there are no more rows to fetch.</p>
<ul>
<li>the <code>mode</code> arg can contain any combination of the following letters:
<ul>
<li><code>"n"</code> - return values in a table with numeric indices as keys.</li>
<li><code>"a"</code> - return values in a table with field names as keys.</li>
<li><code>"s"</code> - do not convert numeric and time values to Lua types.</li>
</ul></li>
<li>the <code>row_t</code> arg is an optional table to store the row values in, instead of creating a new one on each fetch.</li>
<li>options "a" and "n" can be combined to get a table with both numeric and field name indices.</li>
<li>if <code>mode</code> is missing or if neither "a" nor "n" is specified, the values are returned to the caller unpacked, after a first value that is always true, to make it easy to distinguish between a valid <code>NULL</code> value in the first column and eof.</li>
<li>in "n" mode, the result table may contain <code>nil</code> values so <code>#row_t</code> and <code>ipairs(row_t)</code> are out; instead iterate from 1 to <code>result:field_count()</code>.</li>
<li>in "a" mode, for fields with duplicate names only the last field will be present.</li>
<li>if <code>mode</code> does not specify <code>"s"</code>, the following conversions are applied on the returned values:
<ul>
<li>integer types are returned as Lua numbers, except bigint which is returned as an <code>int64_t</code> cdata (or <code>uint64</code> if unsigned).</li>
<li>date/time types are returned as tables in the usual <code>os.date"*t"</code> format (date fields are missing for time-only types and viceversa).</li>
<li>decimal/numeric types are returned as Lua strings.</li>
<li>bit types are returned as Lua numbers, and as <code>uint64_t</code> for bit types larger than 48 bits.</li>
<li>enum and set types are always returned as strings.</li>
</ul></li>
</ul>
<h2 id="resultrowsmode-row_t---iterator---row_num-val1-val2-..."><code>result:rows([mode[, row_t]]) -> iterator() -> row_num, val1, val2, ...</code></h2>
<h2 id="resultrowsmode-row_t---iterator---row_num-row_t"><code>result:rows([mode[, row_t]]) -> iterator() -> row_num, row_t</code></h2>
<p>Convenience iterator for fetching (or refetching) all the rows from the current result set. The <code>mode</code> arg is the same as for <code>result:fetch()</code>, with the exception that in unpacked mode, the first <code>true</code> value is not present.</p>
<h2 id="resultfree"><code>result:free()</code></h2>
<p>Free the result buffer (otherwise called when <code>result</code> is garbage collected).</p>
<h2 id="resultrow_count---n"><code>result:row_count() -> n</code></h2>
<p>Return the number of rows in the current result set . This value is only correct if <code>result:store_result()</code> was previously called or if all the rows were fetched, in other words if <code>result:eof()</code> is true.</p>
<h2 id="resulteof---true-false"><code>result:eof() -> true | false</code></h2>
<p>Check if there are no more rows to fetch. If <code>result:store_result()</code> was previously called, then all rows were already fetched, so <code>result:eof()</code> always returns <code>true</code> in this case.</p>
<h2 id="resultseekrow_number"><code>result:seek(row_number)</code></h2>
<p>Seek back to a particular row number to refetch the rows from there.</p>
<h2 id="query-info">Query info</h2>
<h2 id="connfield_count---n"><code>conn:field_count() -> n</code></h2>
<h2 id="connaffected_rows---n"><code>conn:affected_rows() -> n</code></h2>
<h2 id="conninsert_id---n"><code>conn:insert_id() -> n</code></h2>
<h2 id="connerrno---n"><code>conn:errno() -> n</code></h2>
<h2 id="connsqlstate---s"><code>conn:sqlstate() -> s</code></h2>
<h2 id="connwarning_count---n"><code>conn:warning_count() -> n</code></h2>
<h2 id="conninfo---s"><code>conn:info() -> s</code></h2>
<p>Return various pieces of information about the previously executed query.</p>
<h2 id="field-info">Field info</h2>
<h2 id="resultfield_count---n"><code>result:field_count() -> n</code></h2>
<h2 id="resultfield_namefield_number---s"><code>result:field_name(field_number) -> s</code></h2>
<h2 id="resultfield_typefield_number---type-length-decimals-unsigned"><code>result:field_type(field_number) -> type, length, decimals, unsigned</code></h2>
<h2 id="resultfield_infofield_number---info_t"><code>result:field_info(field_number) -> info_t</code></h2>
<h2 id="resultfields---iterator---i-info_t"><code>result:fields() -> iterator() -> i, info_t</code></h2>
<p>Return information about the fields (columns) in the current result set.</p>
<h2 id="result-bookmarks">Result bookmarks</h2>
<h2 id="resulttell---bookmark"><code>result:tell() -> bookmark</code></h2>
<p>Get a bookmark to the current row to be later seeked into with <code>seek()</code>.</p>
<h2 id="resultseekbookmark"><code>result:seek(bookmark)</code></h2>
<p>Seek to a previous saved row bookmark, or to a specific row number, fetching more rows as needed.</p>
<h2 id="multiple-statement-queries">Multiple statement queries</h2>
<h2 id="connnext_result---true-false"><code>conn:next_result() -> true | false</code></h2>
<p>Skip over to the next result set in a multiple statement query, and make that the current result set. Return true if there more result sets after this one.</p>
<h2 id="connmore_results---true-false"><code>conn:more_results() -> true | false</code></h2>
<p>Check if there are more result sets after this one.</p>
<h2 id="prepared-statements">Prepared statements</h2>
<p>Prepared statements are a way to run queries and retrieve results more efficiently from the database, in particular:</p>
<ul>
<li>parametrized queries allow sending query parameters in their native format, avoiding having to convert values into strings and escaping those strings.</li>
<li>running the same query multiple times with different parameters each time allows the server to reuse the parsed query and possibly the query plan between runs.</li>
<li>fetching the result rows in preallocated buffers avoids dynamic allocation on each row fetch.</li>
</ul>
<p>The flow for prepared statements is like this:</p>
<ul>
<li>call <code>conn:prepare()</code> to prepare a query and get a statement object.</li>
<li>call <code>stmt:bind_params()</code> and <code>stmt:bind_result()</code> to get the buffer objects for setting params and getting row values.</li>
<li>run the query multiple times; each time:
<ul>
<li>call <code>params:set()</code> for each param to set param values.</li>
<li>call <code>stmt:exec()</code> to run the query.</li>
<li>fetch the resulting rows one by one; for each row:
<ul>
<li>call <code>stmt:fetch()</code> to get the next row (it returns false if it was the last row).</li>
<li>call <code>fields:get()</code> to read the values of the fetched row.</li>
</ul></li>
</ul></li>
<li>call <code>stmt:close()</code> to free the statement object and all the associated resources from the server and client.</li>
</ul>
<h2 id="connpreparequery---stmt-params"><code>conn:prepare(query) -> stmt, params</code></h2>
<p>Prepare a query for multiple execution and return a statement object.</p>
<h2 id="stmtparam_count---n"><code>stmt:param_count() -> n</code></h2>
<p>Number of parameters.</p>
<h2 id="stmtexec"><code>stmt:exec()</code></h2>
<p>Execute a prepared statement.</p>
<h2 id="stmtstore_result"><code>stmt:store_result()</code></h2>
<p>Fetch all the rows in the current result set from the server, otherwise the rows are fetched on demand.</p>
<h2 id="stmtfetch---true-false-true-truncated"><code>stmt:fetch() -> true | false | true, 'truncated'</code></h2>
<p>Fetch the next row from the current result set. Use a binding buffer (see prepared statements I/O section) to get the row values. If present, second value indicates that at least one of the rows were truncated because the receiving buffer was too small for it.</p>
<h2 id="stmtfree_result"><code>stmt:free_result()</code></h2>
<p>Free the current result and all associated resources (otherwise the result is closed when the statement is closed).</p>
<h2 id="stmtclose"><code>stmt:close()</code></h2>
<p>Close a prepared statement and free all associated resources (otherwise the statement is closed when garbage collected).</p>
<h2 id="stmtnext_result"><code>stmt:next_result()</code></h2>
<p>Skip over to the next result set in a multiple statement query.</p>
<h2 id="stmtrow_count---n"><code>stmt:row_count() -> n</code></h2>
<h2 id="stmtaffected_rows---n"><code>stmt:affected_rows() -> n</code></h2>
<h2 id="stmtinsert_id---n"><code>stmt:insert_id() -> n</code></h2>
<h2 id="stmtfield_count---n"><code>stmt:field_count() -> n</code></h2>
<h2 id="stmterrno---n"><code>stmt:errno() -> n</code></h2>
<h2 id="stmtsqlstate---s"><code>stmt:sqlstate() -> s</code></h2>
<h2 id="stmtresult_metadata---result"><code>stmt:result_metadata() -> result</code></h2>
<h2 id="stmtfields---iterator---i-info_t"><code>stmt:fields() -> iterator() -> i, info_t</code></h2>
<p>Return various pieces of information on the executed statement.</p>
<h2 id="stmtreset"><code>stmt:reset()</code></h2>
<p>See <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-reset.html">manual</a>.</p>
<h2 id="stmtseekrow_number"><code>stmt:seek(row_number)</code></h2>
<h2 id="stmttell---bookmark"><code>stmt:tell() -> bookmark</code></h2>
<h2 id="stmtseekbookmark"><code>stmt:seek(bookmark)</code></h2>
<p>Seek into the current result set.</p>
<h2 id="prepared-statements-io">Prepared statements I/O</h2>
<h2 id="stmtbind_paramstype1-...-types_t---params"><code>stmt:bind_params(type1, ... | types_t) -> params</code></h2>
<p>Bind query parameters according to a list of type definitions (which can be given either packed or unpacked). Return a binding buffer object to be used for setting parameters.</p>
<p>The types must be valid, fully specified SQL types, eg.</p>
<ul>
<li><code>smallint unsigned</code> specifies a 16bit unsigned integer</li>
<li><code>bit(32)</code> specifies a 32bit bit field</li>
<li><code>varchar(200)</code> specifies a 200 byte varchar.</li>
</ul>
<h2 id="paramsseti-number-int64_t-uint64_t-true-false"><code>params:set(i, number | int64_t | uint64_t | true | false)</code></h2>
<h2 id="paramsseti-s-size"><code>params:set(i, s[, size])</code></h2>
<h2 id="paramsseti-cdata-size"><code>params:set(i, cdata, size)</code></h2>
<h2 id="paramsseti-year-month-..."><code>params:set(i, {year=, month=, ...})</code></h2>
<h2 id="paramsset_datei-year-month-day-hour-min-sec-frac"><code>params:set_date(i, [year], [month], [day], [hour], [min], [sec], [frac])</code></h2>
<p>Set a parameter value.</p>
<ul>
<li>the first form is for setting integers and bit fields.</li>
<li>the second and third forms are for setting variable-sized fields and decimal/numeric fields.</li>
<li>the last forms are for setting date/time/datetime/timestamp fields.</li>
<li>the null type cannot be set (raises an error if attempted).</li>
</ul>
<h2 id="stmtwriteparam_number-data-size"><code>stmt:write(param_number, data[, size])</code></h2>
<p>Send a parameter value in chunks (for long, var-sized values).</p>
<h2 id="stmtbind_resulttype1-...-types_t-maxsize---fields"><code>stmt:bind_result([type1, ... | types_t | maxsize]) -> fields</code></h2>
<p>Bind result fields according to a list of type definitions (same as for params). Return a binding buffer object to be used for getting row values. If no types are specified, appropriate type definitions will be created automatically as to minimize type conversions. Variable-sized fields will get a buffer sized according to data type's maximum allowed size and <code>maxsize</code> (which defaults to 64k).</p>
<h2 id="fieldsgeti---value"><code>fields:get(i) -> value</code></h2>
<h2 id="fieldsget_datetimei---year-month-day-hour-min-sec-frac"><code>fields:get_datetime(i) -> year, month, day, hour, min, sec, frac</code></h2>
<p>Get a row value from the last fetched row. The same type conversions as for <code>result:fetch()</code> apply.</p>
<h2 id="fieldsis_nulli---true-false"><code>fields:is_null(i) -> true | false</code></h2>
<p>Check if a value is null without having to get it if it's not.</p>
<h2 id="fieldsis_truncatedi---true-false"><code>fields:is_truncated(i) -> true | false</code></h2>
<p>Check if a value was truncated due to insufficient buffer space.</p>
<h2 id="stmtbind_result_typesmaxsize---types_t"><code>stmt:bind_result_types([maxsize]) -> types_t</code></h2>
<p>Return the list of type definitions that describe the result of a prepared statement.</p>
<h2 id="prepared-statements-settings">Prepared statements settings</h2>
<h2 id="stmtupdate_max_length---true-false"><code>stmt:update_max_length() -> true | false</code></h2>
<h2 id="stmtset_update_max_lengthtrue-false"><code>stmt:set_update_max_length(true | false)</code></h2>
<h2 id="stmtcursor_type---mysql.c.mysql_cursor_type_"><code>stmt:cursor_type() -> mysql.C.MYSQL_CURSOR_TYPE_*</code></h2>
<h2 id="stmtset_cursor_typecursor_type_..."><code>stmt:set_cursor_type('CURSOR_TYPE_...')</code></h2>
<h2 id="stmtset_cursor_typemysql.c.mysql_cursor_type_..."><code>stmt:set_cursor_type(mysql.C.MYSQL_CURSOR_TYPE_...)</code></h2>
<h2 id="stmtprefetch_rows---n"><code>stmt:prefetch_rows() -> n</code></h2>
<h2 id="stmtset_prefetch_rowsstmt-n"><code>stmt:set_prefetch_rows(stmt, n)</code></h2>
<p>See <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-attr-set.html">manual</a> for these.</p>
<h2 id="connection-info">Connection info</h2>
<h2 id="connset_charsetcharset"><code>conn:set_charset(charset)</code></h2>
<p>Change the current charset.</p>
<h2 id="connselect_dbdbname"><code>conn:select_db(dbname)</code></h2>
<p>Change the current database.</p>
<h2 id="connchange_useruser-pass-db"><code>conn:change_user(user, [pass], [db])</code></h2>
<p>Change the current user and optionally select a database.</p>
<h2 id="connset_multiple_statementstrue-false"><code>conn:set_multiple_statements(true | false)</code></h2>
<p>Enable or disable support for query strings containing multiple statements separated by a semi-colon.</p>
<h2 id="conncharset---s"><code>conn:charset() -> s</code></h2>
<p>Get the current charset.</p>
<h2 id="conncharset_info---info_t"><code>conn:charset_info() -> info_t</code></h2>
<p>Return a table of information about the current charset.</p>
<h2 id="connping---true-false"><code>conn:ping() -> true | false</code></h2>
<p>Check if the connection to the server is still alive.</p>
<h2 id="connthread_id---id"><code>conn:thread_id() -> id</code></h2>
<h2 id="connstat---s"><code>conn:stat() -> s</code></h2>
<h2 id="connserver_info---s"><code>conn:server_info() -> s</code></h2>
<h2 id="connhost_info---s"><code>conn:host_info() -> s</code></h2>
<h2 id="connserver_version---n"><code>conn:server_version() -> n</code></h2>
<h2 id="connproto_info---n"><code>conn:proto_info() -> n</code></h2>
<h2 id="connssl_cipher---s"><code>conn:ssl_cipher() -> s</code></h2>
<p>Return various pieces of information about the connection and server.</p>
<h2 id="transactions">Transactions</h2>
<h2 id="conncommit"><code>conn:commit()</code></h2>
<h2 id="connrollback"><code>conn:rollback()</code></h2>
<p>Commit/rollback the current transaction.</p>
<h2 id="connset_autocommittrue-false"><code>conn:set_autocommit([true | false])</code></h2>
<p>Set autocommit on the connection (set to true if no argument is given).</p>
<h2 id="reflection">Reflection</h2>
<h2 id="connlist_dbswildcard---result"><code>conn:list_dbs([wildcard]) -> result</code></h2>
<h2 id="connlist_tableswildcard---result"><code>conn:list_tables([wildcard]) -> result</code></h2>
<h2 id="connlist_processes---result"><code>conn:list_processes() -> result</code></h2>
<p>Return information about databases, tables and proceses as a stored result object that can be iterated etc. using the methods of result objects. The optional <code>wild</code> parameter may contain the wildcard characters <code>"%"</code> or <code>"_"</code>, similar to executing the query <code>SHOW DATABASES [LIKE wild]</code>.</p>
<h2 id="remote-control">Remote control</h2>
<h2 id="connkillpid"><code>conn:kill(pid)</code></h2>
<p>Kill a connection with a specific <code>pid</code>.</p>
<h2 id="connshutdownlevel"><code>conn:shutdown([level])</code></h2>
<p>Shutdown the server. <code>SHUTDOWN</code> priviledge needed. The level argument is reserved for future versions of mysql.</p>
<h2 id="connrefreshoptions"><code>conn:refresh(options)</code></h2>
<p>Flush tables or caches, or resets replication server information. <code>RELOAD</code> priviledge needed. Options are either a table of form <code>{REFRESH_... = true | false, ...}</code> or a number of form <code>bit.bor(mysql.C.MYSQL_REFRESH_*, ...)</code> and they are as described in the <a href="http://dev.mysql.com/doc/refman/5.7/en/mysql-refresh.html">mysql manual</a>.</p>
<h2 id="conndump_debug_info"><code>conn:dump_debug_info()</code></h2>
<p>Instruct the server to dump debug info in the log file. <code>SUPER</code> priviledge needed.</p>
<h2 id="client-library-info">Client library info</h2>
<h2 id="mysql.thread_safe---true-false"><code>mysql.thread_safe() -> true | false</code></h2>
<h2 id="mysql.client_info---s"><code>mysql.client_info() -> s</code></h2>
<h2 id="mysql.client_version---n"><code>mysql.client_version() -> n</code></h2>
<hr />
<h2 id="todo">TODO</h2>
<ul>
<li>reader function for getting large blobs in chunks using mysql_stmt_fetch_column: <code>stmt:chunks(i[, bufsize])</code> or <code>stmt:read()</code> ?</li>
<li>support connecting against different runtimes (client_library option)</li>
</ul>
</section>
</div>
<div id="tab2_container" class="doc"></div>
</div>
</div>
<div class="container">
<footer>
<div id="disqus_thread"></div>
<div class="faint">[email protected] | <a href="http://unlicense.org/">public domain</a></div>
</footer>
</div>
</div>
<script type="text/x-mustache" id=info_tab_template>
<h3>Modules</h3>
<ul>
{{#module_array}}
<li>{{name}}</li>
{{/module_array}}
</ul>
</script>
</body>
</html>