/
sql.lib.php
2334 lines (2132 loc) · 81 KB
/
sql.lib.php
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
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* set of functions for the sql executor
*
* @package PhpMyAdmin
*/
if (!defined('PHPMYADMIN')) {
exit;
}
/**
* Get the database name inside a query
*
* @param string $sql SQL query
* @param array $databases array with all databases
*
* @return string $db new database name
*/
function PMA_getNewDatabase($sql, $databases)
{
$db = '';
// loop through all the databases
foreach ($databases as $database) {
if (strpos($sql, $database['SCHEMA_NAME']) !== false) {
$db = $database['SCHEMA_NAME'];
break;
}
}
return $db;
}
/**
* Get the table name in a sql query
* If there are several tables in the SQL query,
* first table wil lreturn
*
* @param string $sql SQL query
* @param array $tables array of names in current database
*
* @return string $table table name
*/
function PMA_getTableNameBySQL($sql, $tables)
{
$table = '';
// loop through all the tables in the database
foreach ($tables as $tbl) {
if (strpos($sql, $tbl)) {
$table .= ' ' . $tbl;
}
}
if (count(explode(' ', trim($table))) > 1) {
$tmp_array = explode(' ', trim($table));
return $tmp_array[0];
}
return trim($table);
}
/**
* Generate table html when SQL statement have multiple queries
* which return displayable results
*
* @param object $displayResultsObject PMA_DisplayResults object
* @param string $db database name
* @param array $sql_data information about SQL statement
* @param string $goto URL to go back in case of errors
* @param string $pmaThemeImage path for theme images directory
* @param string $printview whether printview is enabled
* @param string $url_query URL query
* @param array $disp_mode the display mode
* @param string $sql_limit_to_append limit clause
* @param bool $editable whether editable or not
*
* @return string $table_html html content
*/
function PMA_getTableHtmlForMultipleQueries(
$displayResultsObject, $db, $sql_data, $goto, $pmaThemeImage,
$printview, $url_query, $disp_mode, $sql_limit_to_append,
$editable
) {
$table_html = '';
$tables_array = $GLOBALS['dbi']->getTables($db);
$databases_array = $GLOBALS['dbi']->getDatabasesFull();
$multi_sql = implode(";", $sql_data['valid_sql']);
$querytime_before = array_sum(explode(' ', microtime()));
// Assignment for variable is not needed since the results are
// looping using the connection
@$GLOBALS['dbi']->tryMultiQuery($multi_sql);
$querytime_after = array_sum(explode(' ', microtime()));
$querytime = $querytime_after - $querytime_before;
$sql_no = 0;
do {
$analyzed_sql = array();
$is_affected = false;
$result = $GLOBALS['dbi']->storeResult();
$fields_meta = ($result !== false)
? $GLOBALS['dbi']->getFieldsMeta($result)
: array();
$fields_cnt = count($fields_meta);
// Initialize needed params related to each query in multiquery statement
if (isset($sql_data['valid_sql'][$sql_no])) {
// 'Use' query can change the database
if (stripos($sql_data['valid_sql'][$sql_no], "use ")) {
$db = PMA_getNewDatabase(
$sql_data['valid_sql'][$sql_no],
$databases_array
);
}
$table = PMA_getTableNameBySQL(
$sql_data['valid_sql'][$sql_no],
$tables_array
);
// for the use of the parse_analyze.inc.php
$sql_query = $sql_data['valid_sql'][$sql_no];
// Parse and analyze the query
include 'libraries/parse_analyze.inc.php';
$unlim_num_rows = PMA_Table::countRecords($db, $table, true);
$showtable = PMA_Table::sGetStatusInfo($db, $table, null, true);
$url_query = PMA_URL_getCommon($db, $table);
// Handle remembered sorting order, only for single table query
if ($GLOBALS['cfg']['RememberSorting']
&& ! ($is_count || $is_export || $is_func || $is_analyse)
&& isset($analyzed_sql[0]['select_expr'])
&& (count($analyzed_sql[0]['select_expr']) == 0)
&& isset($analyzed_sql[0]['queryflags']['select_from'])
&& count($analyzed_sql[0]['table_ref']) == 1
) {
PMA_handleSortOrder(
$db,
$table,
$analyzed_sql,
$sql_data['valid_sql'][$sql_no]
);
}
// Do append a "LIMIT" clause?
if (($_SESSION['tmpval']['max_rows'] != 'all')
&& ! ($is_count || $is_export || $is_func || $is_analyse)
&& isset($analyzed_sql[0]['queryflags']['select_from'])
&& ! isset($analyzed_sql[0]['queryflags']['offset'])
&& empty($analyzed_sql[0]['limit_clause'])
) {
$sql_limit_to_append = ' LIMIT '
. $_SESSION['tmpval']['pos']
. ', ' . $_SESSION['tmpval']['max_rows'] . " ";
$sql_data['valid_sql'][$sql_no] = PMA_getSqlWithLimitClause(
$sql_data['valid_sql'][$sql_no],
$analyzed_sql,
$sql_limit_to_append
);
}
// Set the needed properties related to executing sql query
$displayResultsObject->__set('db', $db);
$displayResultsObject->__set('table', $table);
$displayResultsObject->__set('goto', $goto);
}
if (! $is_affected) {
$num_rows = ($result) ? @$GLOBALS['dbi']->numRows($result) : 0;
} elseif (! isset($num_rows)) {
$num_rows = @$GLOBALS['dbi']->affectedRows();
}
if (isset($sql_data['valid_sql'][$sql_no])) {
$displayResultsObject->__set(
'sql_query',
$sql_data['valid_sql'][$sql_no]
);
$displayResultsObject->setProperties(
$unlim_num_rows, $fields_meta, $is_count, $is_export, $is_func,
$is_analyse, $num_rows, $fields_cnt, $querytime, $pmaThemeImage,
$GLOBALS['text_dir'], $is_maint, $is_explain, $is_show,
$showtable, $printview, $url_query, $editable
);
}
if ($num_rows == 0) {
continue;
}
// With multiple results, operations are limied
$disp_mode = 'nnnn000000';
$is_limited_display = true;
// Collect the tables
$table_html .= $displayResultsObject->getTable(
$result, $disp_mode, $analyzed_sql, $is_limited_display
);
// Free the result to save the memory
$GLOBALS['dbi']->freeResult($result);
$sql_no++;
} while ($GLOBALS['dbi']->moreResults() && $GLOBALS['dbi']->nextResult());
return $table_html;
}
/**
* Handle remembered sorting order, only for single table query
*
* @param string $db database name
* @param string $table table name
* @param array &$analyzed_sql_results the analyzed query results
* @param string &$full_sql_query SQL query
*
* @return void
*/
function PMA_handleSortOrder($db, $table, &$analyzed_sql_results, &$full_sql_query)
{
$pmatable = new PMA_Table($table, $db);
if (empty($analyzed_sql_results['analyzed_sql'][0]['order_by_clause'])) {
$sorted_col = $pmatable->getUiProp(PMA_Table::PROP_SORTED_COLUMN);
if ($sorted_col) {
// retrieve the remembered sorting order for current table
$sql_order_to_append = ' ORDER BY ' . $sorted_col . ' ';
$full_sql_query
= $analyzed_sql_results['analyzed_sql'][0]['section_before_limit']
. $sql_order_to_append
. $analyzed_sql_results['analyzed_sql'][0]['limit_clause']
. ' '
. $analyzed_sql_results['analyzed_sql'][0]['section_after_limit'];
// update the $analyzed_sql
$analyzed_sql_results['analyzed_sql'][0]['section_before_limit']
.= $sql_order_to_append;
$analyzed_sql_results['analyzed_sql'][0]['order_by_clause']
= $sorted_col;
}
} else {
// store the remembered table into session
$pmatable->setUiProp(
PMA_Table::PROP_SORTED_COLUMN,
$analyzed_sql_results['analyzed_sql'][0]['order_by_clause']
);
}
}
/**
* Append limit clause to SQL query
*
* @param string $full_sql_query SQL query
* @param array $analyzed_sql the analyzed query
* @param string $sql_limit_to_append clause to append
*
* @return string limit clause appended SQL query
*/
function PMA_getSqlWithLimitClause($full_sql_query, $analyzed_sql,
$sql_limit_to_append
) {
return $analyzed_sql[0]['section_before_limit'] . "\n"
. $sql_limit_to_append . $analyzed_sql[0]['section_after_limit'];
}
/**
* Get column name from a drop SQL statement
*
* @param string $sql SQL query
*
* @return string $drop_column Name of the column
*/
function PMA_getColumnNameInColumnDropSql($sql)
{
$tmpArray1 = explode('DROP', $sql);
$str_to_check = trim($tmpArray1[1]);
if (stripos($str_to_check, 'COLUMN') !== false) {
$tmpArray2 = explode('COLUMN', $str_to_check);
$str_to_check = trim($tmpArray2[1]);
}
$tmpArray3 = explode(' ', $str_to_check);
$str_to_check = trim($tmpArray3[0]);
$drop_column = str_replace(';', '', trim($str_to_check));
$drop_column = str_replace('`', '', $drop_column);
return $drop_column;
}
/**
* Verify whether the result set contains all the columns
* of at least one unique key
*
* @param string $db database name
* @param string $table table name
* @param string $fields_meta meta fields
*
* @return boolean whether the result set contains a unique key
*/
function PMA_resultSetContainsUniqueKey($db, $table, $fields_meta)
{
$resultSetColumnNames = array();
foreach ($fields_meta as $oneMeta) {
$resultSetColumnNames[] = $oneMeta->name;
}
foreach (PMA_Index::getFromTable($table, $db) as $index) {
if ($index->isUnique()) {
$indexColumns = $index->getColumns();
$numberFound = 0;
foreach ($indexColumns as $indexColumnName => $dummy) {
if (in_array($indexColumnName, $resultSetColumnNames)) {
$numberFound++;
}
}
if ($numberFound == count($indexColumns)) {
return true;
}
}
}
return false;
}
/**
* Get the HTML for relational column dropdown
* During grid edit, if we have a relational field, returns the html for the
* dropdown
*
* @param string $db current database
* @param string $table current table
* @param string $column current column
* @param string $curr_value current selected value
*
* @return string $dropdown html for the dropdown
*/
function PMA_getHtmlForRelationalColumnDropdown($db, $table, $column, $curr_value)
{
$foreigners = PMA_getForeigners($db, $table, $column);
$display_field = PMA_getDisplayField(
$foreigners[$column]['foreign_db'],
$foreigners[$column]['foreign_table']
);
$foreignData = PMA_getForeignData($foreigners, $column, false, '', '');
if ($foreignData['disp_row'] == null) {
//Handle the case when number of values
//is more than $cfg['ForeignKeyMaxLimit']
$_url_params = array(
'db' => $db,
'table' => $table,
'field' => $column
);
$dropdown = '<span class="curr_value">'
. htmlspecialchars($_REQUEST['curr_value'])
. '</span>'
. '<a href="browse_foreigners.php'
. PMA_URL_getCommon($_url_params) . '"'
. ' target="_blank" class="browse_foreign" ' .'>'
. __('Browse foreign values')
. '</a>';
} else {
$dropdown = PMA_foreignDropdown(
$foreignData['disp_row'],
$foreignData['foreign_field'],
$foreignData['foreign_display'],
$curr_value,
$GLOBALS['cfg']['ForeignKeyMaxLimit']
);
$dropdown = '<select>' . $dropdown . '</select>';
}
return $dropdown;
}
/**
* Get the HTML for the header of the page in print view if print view is selected.
* Otherwise returns null.
*
* @param string $db current database
* @param string $sql_query current sql query
* @param int $num_rows the number of rows in result
*
* @return string $header html for the header
*/
function PMA_getHtmlForPrintViewHeader($db, $sql_query, $num_rows)
{
$response = PMA_Response::getInstance();
$header = $response->getHeader();
if (isset($_REQUEST['printview']) && $_REQUEST['printview'] == '1') {
PMA_Util::checkParameters(array('db', 'sql_query'));
$header->enablePrintView();
$hostname = '';
if ( $GLOBALS['cfg']['Server']['verbose']) {
$hostname = $GLOBALS['cfg']['Server']['verbose'];
} else {
$hostname = $GLOBALS['cfg']['Server']['host'];
if (! empty( $GLOBALS['cfg']['Server']['port'])) {
$hostname .= $GLOBALS['cfg']['Server']['port'];
}
}
$versions = "phpMyAdmin " . PMA_VERSION;
$versions .= " / ";
$versions .= "MySQL " . PMA_MYSQL_STR_VERSION;
$print_view_header = '';
$print_view_header .= "<h1>" . __('SQL result') . "</h1>";
$print_view_header .= "<p>";
$print_view_header .= "<strong>" . __('Host:')
. "</strong> $hostname<br />";
$print_view_header .= "<strong>" . __('Database:') . "</strong> "
. htmlspecialchars($db) . "<br />";
$print_view_header .= "<strong>" . __('Generation Time:') . "</strong> "
. PMA_Util::localisedDate() . "<br />";
$print_view_header .= "<strong>" . __('Generated by:')
. "</strong> $versions<br />";
$print_view_header .= "<strong>" . __('SQL query:') . "</strong> "
. htmlspecialchars($sql_query) . ";";
if (isset($num_rows)) {
$print_view_header .= "<br />";
$print_view_header .= "<strong>" . __('Rows:') . "</strong> $num_rows";
}
$print_view_header .= "</p>";
} else {
$print_view_header = null;
}
return $print_view_header;
}
/**
* Get the HTML for the profiling table and accompanying chart if profiling is set.
* Otherwise returns null
*
* @param string $url_query url query
* @param string $db current database
* @param array $profiling_results array containing the profiling info
*
* @return string $profiling_table html for the profiling table and chart
*/
function PMA_getHtmlForProfilingChart($url_query, $db, $profiling_results)
{
if (isset($profiling_results)) {
$pma_token = $_SESSION[' PMA_token '];
$url_query = (isset($url_query) ? $url_query : PMA_URL_getCommon($db));
$profiling_table = '';
$profiling_table .= '<fieldset><legend>' . __('Profiling')
. '</legend>' . "\n";
$profiling_table .= '<div style="float: left;">';
$profiling_table .= '<h3>' . __('Detailed profile') . '</h3>';
$profiling_table .= '<table id="profiletable"><thead>' . "\n";
$profiling_table .= ' <tr>' . "\n";
$profiling_table .= ' <th>' . __('Order')
. '<div class="sorticon"></div></th>' . "\n";
$profiling_table .= ' <th>' . __('State')
. PMA_Util::showMySQLDocu('general-thread-states')
. '<div class="sorticon"></div></th>' . "\n";
$profiling_table .= ' <th>' . __('Time')
. '<div class="sorticon"></div></th>' . "\n";
$profiling_table .= ' </tr></thead><tbody>' . "\n";
list($detailed_table, $chart_json, $profiling_stats)
= PMA_analyzeAndGetTableHtmlForProfilingResults($profiling_results);
$profiling_table .= $detailed_table;
$profiling_table .= '</tbody></table>' . "\n";
$profiling_table .= '</div>';
$profiling_table .= '<div style="float: left; margin-left:10px;">';
$profiling_table .= '<h3>' . __('Summary by state') . '</h3>';
$profiling_table .= '<table id="profilesummarytable"><thead>' . "\n";
$profiling_table .= ' <tr>' . "\n";
$profiling_table .= ' <th>' . __('State')
. PMA_Util::showMySQLDocu('general-thread-states')
. '<div class="sorticon"></div></th>' . "\n";
$profiling_table .= ' <th>' . __('Total Time')
. '<div class="sorticon"></div></th>' . "\n";
$profiling_table .= ' <th>' . __('% Time')
. '<div class="sorticon"></div></th>' . "\n";
$profiling_table .= ' <th>' . __('Calls')
. '<div class="sorticon"></div></th>' . "\n";
$profiling_table .= ' <th>' . __('ø Time')
. '<div class="sorticon"></div></th>' . "\n";
$profiling_table .= ' </tr></thead><tbody>' . "\n";
$profiling_table .= PMA_getTableHtmlForProfilingSummaryByState(
$profiling_stats
);
$profiling_table .= '</tbody></table>' . "\n";
$profiling_table .= <<<EOT
<script type="text/javascript">
pma_token = '$pma_token';
url_query = '$url_query';
</script>
EOT;
$profiling_table .= "</div>";
//require_once 'libraries/chart.lib.php';
$profiling_table .= '<div id="profilingChartData" style="display:none;">';
$profiling_table .= json_encode($chart_json);
$profiling_table .= '</div>';
$profiling_table .= '<div id="profilingchart" style="display:none;">';
$profiling_table .= '</div>';
$profiling_table .= '<script type="text/javascript">';
$profiling_table .= 'makeProfilingChart();';
$profiling_table .= 'initProfilingTables();';
$profiling_table .= '</script>';
$profiling_table .= '</fieldset>' . "\n";
} else {
$profiling_table = null;
}
return $profiling_table;
}
/**
* Function to get HTML for detailed profiling results table, profiling stats, and
* $chart_json for displaying the chart.
*
* @param array $profiling_results profiling results
*
* @return mixed
*/
function PMA_analyzeAndGetTableHtmlForProfilingResults(
$profiling_results
) {
$profiling_stats = array(
'total_time' => 0,
'states' => array(),
);
$chart_json = Array();
$i = 1;
$table = '';
foreach ($profiling_results as $one_result) {
if (isset($profiling_stats['states'][ucwords($one_result['Status'])])) {
$states = $profiling_stats['states'];
$states[ucwords($one_result['Status'])]['time']
+= $one_result['Duration'];
$states[ucwords($one_result['Status'])]['calls']++;
} else {
$profiling_stats['states'][ucwords($one_result['Status'])] = array(
'total_time' => $one_result['Duration'],
'calls' => 1,
);
}
$profiling_stats['total_time'] += $one_result['Duration'];
$table .= ' <tr>' . "\n";
$table .= '<td>' . $i++ . '</td>' . "\n";
$table .= '<td>' . ucwords($one_result['Status'])
. '</td>' . "\n";
$table .= '<td class="right">'
. (PMA_Util::formatNumber($one_result['Duration'], 3, 1))
. 's<span style="display:none;" class="rawvalue">'
. $one_result['Duration'] . '</span></td>' . "\n";
if (isset($chart_json[ucwords($one_result['Status'])])) {
$chart_json[ucwords($one_result['Status'])]
+= $one_result['Duration'];
} else {
$chart_json[ucwords($one_result['Status'])]
= $one_result['Duration'];
}
}
return array($table, $chart_json, $profiling_stats);
}
/**
* Function to get HTML for summary by state table
*
* @param array $profiling_stats profiling stats
*
* @return string $table html for the table
*/
function PMA_getTableHtmlForProfilingSummaryByState($profiling_stats)
{
$table = '';
foreach ($profiling_stats['states'] as $name => $stats) {
$table .= ' <tr>' . "\n";
$table .= '<td>' . $name . '</td>' . "\n";
$table .= '<td align="right">'
. PMA_Util::formatNumber($stats['total_time'], 3, 1)
. 's<span style="display:none;" class="rawvalue">'
. $stats['total_time'] . '</span></td>' . "\n";
$table .= '<td align="right">'
. PMA_Util::formatNumber(
100 * ($stats['total_time'] / $profiling_stats['total_time']),
0, 2
)
. '%</td>' . "\n";
$table .= '<td align="right">' . $stats['calls'] . '</td>'
. "\n";
$table .= '<td align="right">'
. PMA_Util::formatNumber(
$stats['total_time'] / $stats['calls'], 3, 1
)
. 's<span style="display:none;" class="rawvalue">'
. number_format($stats['total_time'] / $stats['calls'], 8, '.', '')
. '</span></td>' . "\n";
$table .= ' </tr>' . "\n";
}
return $table;
}
/**
* Get the HTML for the enum column dropdown
* During grid edit, if we have a enum field, returns the html for the
* dropdown
*
* @param string $db current database
* @param string $table current table
* @param string $column current column
* @param string $curr_value currently selected value
*
* @return string $dropdown html for the dropdown
*/
function PMA_getHtmlForEnumColumnDropdown($db, $table, $column, $curr_value)
{
$values = PMA_getValuesForColumn($db, $table, $column);
$dropdown = '<option value=""> </option>';
$dropdown .= PMA_getHtmlForOptionsList($values, array($curr_value));
$dropdown = '<select>' . $dropdown . '</select>';
return $dropdown;
}
/**
* Get the HTML for the set column dropdown
* During grid edit, if we have a set field, returns the html for the
* dropdown
*
* @param string $db current database
* @param string $table current table
* @param string $column current column
* @param string $curr_value currently selected value
*
* @return string $dropdown html for the set column
*/
function PMA_getHtmlForSetColumn($db, $table, $column, $curr_value)
{
$values = PMA_getValuesForColumn($db, $table, $column);
$dropdown = '';
//converts characters of $curr_value to HTML entities
$converted_curr_value = htmlentities(
$curr_value, ENT_COMPAT, "UTF-8"
);
$selected_values = explode(',', $converted_curr_value);
$dropdown .= PMA_getHtmlForOptionsList($values, $selected_values);
$select_size = (sizeof($values) > 10) ? 10 : sizeof($values);
$dropdown = '<select multiple="multiple" size="' . $select_size . '">'
. $dropdown . '</select>';
return $dropdown;
}
/**
* Get all the values for a enum column or set column in a table
*
* @param string $db current database
* @param string $table current table
* @param string $column current column
*
* @return array $values array containing the value list for the column
*/
function PMA_getValuesForColumn($db, $table, $column)
{
$field_info_query = $GLOBALS['dbi']->getColumnsSql($db, $table, $column);
$field_info_result = $GLOBALS['dbi']->fetchResult(
$field_info_query, null, null, null, PMA_DatabaseInterface::QUERY_STORE
);
$values = PMA_Util::parseEnumSetValues($field_info_result[0]['Type']);
return $values;
}
/**
* Get HTML for options list
*
* @param array $values set of values
* @param array $selected_values currently selected values
*
* @return string $options HTML for options list
*/
function PMA_getHtmlForOptionsList($values, $selected_values)
{
$options = '';
foreach ($values as $value) {
$options .= '<option value="' . $value . '"';
if (in_array($value, $selected_values, true)) {
$options .= ' selected="selected" ';
}
$options .= '>' . $value . '</option>';
}
return $options;
}
/**
* Function to get html for bookmark support if bookmarks are enabled. Else will
* return null
*
* @param string $disp_mode display mode
* @param bool $cfgBookmark configuration setting for bookmarking
* @param string $sql_query sql query
* @param string $db current database
* @param string $table current table
* @param string $complete_query complete query
* @param string $bkm_user bookmarking user
*
* @return string $html
*/
function PMA_getHtmlForBookmark($disp_mode, $cfgBookmark, $sql_query, $db, $table,
$complete_query, $bkm_user
) {
if ($disp_mode[7] == '1'
&& (! empty($cfgBookmark) && empty($_GET['id_bookmark']))
&& ! empty($sql_query)
) {
$html = "\n";
$goto = 'sql.php'
. PMA_URL_getCommon(
array(
'db' => $db,
'table' => $table,
'sql_query' => $sql_query,
'id_bookmark'=> 1,
)
);
$bkm_sql_query = urlencode(
isset($complete_query) ? $complete_query : $sql_query
);
$html = '<form action="sql.php" method="post"'
. ' onsubmit="return ! emptyFormElements(this,'
. '\'bkm_fields[bkm_label]\');"'
. ' id="bookmarkQueryForm">';
$html .= PMA_URL_getHiddenInputs();
$html .= '<input type="hidden" name="goto" value="' . $goto . '" />';
$html .= '<input type="hidden" name="bkm_fields[bkm_database]"'
. ' value="' . htmlspecialchars($db) . '" />';
$html .= '<input type="hidden" name="bkm_fields[bkm_user]"'
. ' value="' . $bkm_user . '" />';
$html .= '<input type="hidden" name="bkm_fields[bkm_sql_query]"'
. ' value="'
. $bkm_sql_query
. '" />';
$html .= '<fieldset>';
$html .= '<legend>';
$html .= PMA_Util::getIcon(
'b_bookmark.png', __('Bookmark this SQL query'), true
);
$html .= '</legend>';
$html .= '<div class="formelement">';
$html .= '<label for="fields_label_">' . __('Label:') . '</label>';
$html .= '<input type="text" id="fields_label_"'
. ' name="bkm_fields[bkm_label]" value="" />';
$html .= '</div>';
$html .= '<div class="formelement">';
$html .= '<input type="checkbox" name="bkm_all_users"'
. ' id="bkm_all_users" value="true" />';
$html .= '<label for="bkm_all_users">'
. __('Let every user access this bookmark')
. '</label>';
$html .= '</div>';
$html .= '<div class="clearfloat"></div>';
$html .= '</fieldset>';
$html .= '<fieldset class="tblFooters">';
$html .= '<input type="hidden" name="store_bkm" value="1" />';
$html .= '<input type="submit"'
. ' value="' . __('Bookmark this SQL query') . '" />';
$html .= '</fieldset>';
$html .= '</form>';
} else {
$html = null;
}
return $html;
}
/**
* Function to check whether to remember the sorting order or not
*
* @param array $analyzed_sql_results the analyzed query and other varibles set
* after analyzing the query
*
* @return boolean
*/
function PMA_isRememberSortingOrder($analyzed_sql_results)
{
$select_from = isset(
$analyzed_sql_results['analyzed_sql'][0]['queryflags']['select_from']
);
if ($GLOBALS['cfg']['RememberSorting']
&& ! ($analyzed_sql_results['is_count']
|| $analyzed_sql_results['is_export']
|| $analyzed_sql_results['is_func']
|| $analyzed_sql_results['is_analyse'])
&& isset($analyzed_sql_results['analyzed_sql'][0]['select_expr'])
&& (count($analyzed_sql_results['analyzed_sql'][0]['select_expr']) == 0)
&& $select_from
&& count($analyzed_sql_results['analyzed_sql'][0]['table_ref']) == 1
) {
return true;
} else {
return false;
}
}
/**
* Function to check whether the LIMIT clause should be appended or not
*
* @param array $analyzed_sql_results the analyzed query and other varibles set
* after analyzing the query
*
* @return boolean
*/
function PMA_isAppendLimitClause($analyzed_sql_results)
{
$select_from = isset(
$analyzed_sql_results['analyzed_sql'][0]['queryflags']['select_from']
);
if (($_SESSION['tmpval']['max_rows'] != 'all')
&& ! ($analyzed_sql_results['is_count']
|| $analyzed_sql_results['is_export']
|| $analyzed_sql_results['is_func']
|| $analyzed_sql_results['is_analyse'])
&& $select_from
&& ! isset($analyzed_sql_results['analyzed_sql'][0]['queryflags']['offset'])
&& empty($analyzed_sql_results['analyzed_sql'][0]['limit_clause'])
) {
return true;
} else {
return false;
}
}
/**
* Function to check whether this query is for just browsing
*
* @param array $analyzed_sql_results the analyzed query and other varibles set
* after analyzing the query
* @param boolean $find_real_end whether the real end should be found
*
* @return boolean
*/
function PMA_isJustBrowsing($analyzed_sql_results, $find_real_end)
{
$distinct = isset(
$analyzed_sql_results['analyzed_sql'][0]['queryflags']['distinct']
);
$table_name = isset(
$analyzed_sql_results['analyzed_sql'][0]['table_ref'][1]['table_name']
);
if (! $analyzed_sql_results['is_group']
&& ! isset($analyzed_sql_results['analyzed_sql'][0]['queryflags']['union'])
&& ! $distinct
&& ! $table_name
&& (empty($analyzed_sql_results['analyzed_sql'][0]['where_clause'])
|| $analyzed_sql_results['analyzed_sql'][0]['where_clause'] == '1 ')
&& ! isset($find_real_end)
) {
return true;
} else {
return false;
}
}
/**
* Function to check whether the reated transformation information shoul be deleted
*
* @param array $analyzed_sql_results the analyzed query and other varibles set
* after analyzing the query
*
* @return boolean
*/
function PMA_isDeleteTransformationInfo($analyzed_sql_results)
{
if (!empty($analyzed_sql_results['analyzed_sql'][0]['querytype'])
&& (($analyzed_sql_results['analyzed_sql'][0]['querytype'] == 'ALTER')
|| ($analyzed_sql_results['analyzed_sql'][0]['querytype'] == 'DROP'))
) {
return true;
} else {
return false;
}
}
/**
* Function to check whether the user has rights to drop the database
*
* @param array $analyzed_sql_results the analyzed query and other varibles set
* after analyzing the query
* @param boolean $allowUserDropDatabase whether the user is allowed to drop db
* @param boolean $is_superuser whether this user is a superuser
*
* @return boolean
*/
function PMA_hasNoRightsToDropDatabase($analyzed_sql_results,
$allowUserDropDatabase, $is_superuser
) {
if (! defined('PMA_CHK_DROP')
&& ! $allowUserDropDatabase
&& isset ($analyzed_sql_results['drop_database'])
&& $analyzed_sql_results['drop_database'] == 1
&& ! $is_superuser
) {
return true;
} else {
return false;
}
}
/**
* Function to set the column order
*
* @param PMA_Table $pmatable PMA_Table instance
*
* @return boolean $retval
*/
function PMA_setColumnOrder($pmatable)
{
$col_order = explode(',', $_REQUEST['col_order']);
$retval = $pmatable->setUiProp(
PMA_Table::PROP_COLUMN_ORDER,
$col_order,
$_REQUEST['table_create_time']
);
if (gettype($retval) != 'boolean') {
$response = PMA_Response::getInstance();
$response->isSuccess(false);
$response->addJSON('message', $retval->getString());
exit;
}
return $retval;
}
/**
* Function to set the column visibility
*
* @param PMA_Table $pmatable PMA_Table instance
*
* @return boolean $retval
*/
function PMA_setColumnVisibility($pmatable)
{
$col_visib = explode(',', $_REQUEST['col_visib']);
$retval = $pmatable->setUiProp(
PMA_Table::PROP_COLUMN_VISIB, $col_visib,
$_REQUEST['table_create_time']
);
if (gettype($retval) != 'boolean') {
$response = PMA_Response::getInstance();
$response->isSuccess(false);
$response->addJSON('message', $retval->getString());
exit;
}
return $retval;
}
/**
* Function to check the request for setting the column order or visibility
*
* @param String $table the current table
* @param String $db the current database
*
* @return void
*/
function PMA_setColumnOrderOrVisibility($table, $db)
{
$pmatable = new PMA_Table($table, $db);
$retval = false;
// set column order
if (isset($_REQUEST['col_order'])) {
$retval = PMA_setColumnOrder($pmatable);
}
// set column visibility
if ($retval === true && isset($_REQUEST['col_visib'])) {
$retval = PMA_setColumnVisibility($pmatable);
}
$response = PMA_Response::getInstance();
$response->isSuccess($retval == true);
exit;
}