-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsql_parser.module
137 lines (127 loc) · 3.75 KB
/
sql_parser.module
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
<?php
// $Id: sql_parser.module,v 1.8 2010/09/04 19:11:05 duellj Exp $
/**
* @file
* Provides parsing ability from sql strings into Drupal DBTNG objects.
*/
/**
* Implements hook_menu().
*/
function sql_parser_menu() {
$items['admin/config/development/sql-parser'] = array(
'title' => 'SQL Parser',
'description' => 'Parse SQL strings and convert to Drupal DBTNG objects.',
'page callback' => 'drupal_get_form',
'page arguments' => array('sql_parser_parser_form'),
'access arguments' => array('access content'),
);
return $items;
}
/**
* Page Callback.
*/
function sql_parser_parser_form($form, &$form_state) {
if (isset($form_state['input']['sql'])) {
$parsed_sql = sql_parser_parse($form_state['input']['sql']);
if ($parsed_sql) {
// $raw_query_string = (string) $parsed_sql; // @todo Not all objects have _toString() defined.
$dbtng_query_string = sql_parser_convert_dbtng($parsed_sql);
}
drupal_set_message('Peak memory usage = ' . number_format(memory_get_peak_usage(TRUE), 0, '.', ',') . " bytes");
}
// Move button here while testing.
$form['submit'] = array(
'#type' => 'button',
'#value' => t('Convert'),
);
$form['output'] = array(
'#title' => 'Database object syntax',
'#type' => 'textarea',
'#rows' => min(35, max(15, isset($dbtng_query_string) ? count(explode("\n", $dbtng_query_string)) : 0)), // @todo For testing.
'#attributes' => array(
'readonly' => 'readonly',
),
'#value' => isset($dbtng_query_string) ? $dbtng_query_string : '',
);
$form['sql'] = array(
'#title' => 'Original SQL string',
'#type' => 'textarea',
'#rows' => 15, // @todo For testing.
'#default_value' => isset($form_state['input']['sql']) ? $form_state['input']['sql'] : '',
);
// $form['parsed_sql'] = array(
// '#title' => 'Parsed SQL string',
// '#type' => 'textarea',
// '#attributes' => array(
// 'readonly' => 'readonly',
// ),
// '#value' => isset($raw_query_string) ? $raw_query_string : '',
// );
return $form;
}
/**
* Loads sql parser code files.
*/
function sql_parser_load_parser() {
static $parser_loaded = FALSE;
if (!$parser_loaded) {
$parser_loaded = TRUE;
// Include parser files.
module_load_include('inc', 'sql_parser', 'includes/sql_parser');
module_load_include('inc', 'sql_parser', 'includes/sql_lexer');
module_load_include('inc', 'sql_parser', 'includes/sql_dialect');
module_load_include('inc', 'sql_parser', 'includes/sql_object');
module_load_include('inc', 'sql_parser', 'includes/sql_converter');
}
}
/**
* Parses a raw SQL string into a SqlObject.
*
* @param $sql
* Raw SQL string.
*
* @return SqlObject
* The parsed sql object.
*/
function sql_parser_parse($sql) {
sql_parser_load_parser();
$parser = new SqlParser($sql);
$sql_object = $parser->parse();
// Check if errors have been found in the parsing process.
if ($parser->isException()) {
$e = $parser->getException();
drupal_set_message('<pre>' . $e . '</pre>');
return FALSE;
}
else {
return $sql_object;
}
}
/**
* Converts a SqlObject into a Drupal 7 DBNTG query string.
*
* @param SqlObject $parsed_sql
* Parsed SQL object.
*
* @return string
* Converted DBTNG string.
*/
function sql_parser_convert_dbtng(SqlObject $parsed_sql) {
sdp(__FUNCTION__);
sdp($parsed_sql, '$parsed_sql');
$converter = new SqlDBTNGConverter($parsed_sql);
return $converter->convert();
}
/**
* Converts a SqlObject into a raw Drupal 7 query string.
*
* @param SqlObject $parsed_sql
* Parsed SQL object.
*
* @return string
* Converted query string.
*/
function sql_parser_convert_string(SqlObject $parsed_sql) {
$converter = new SqlStringConverter($parsed_sql);
return $converter->convert();
}