@@ -26,7 +26,77 @@ If you don't use composer, you can skip the autoloader and include `src/SQLParse
2626
2727## Usage
2828
29- TODO
29+ To extract the tables defined in SQL:
30+
31+ $parser = new SQLParser();
32+ $parser->parse($sql);
33+
34+ print_r($parser->tables);
35+
36+ The ` tables ` property is an array of tables, each of which is a nested array structure defining the
37+ table's structure:
38+
39+ CREATE TABLE `achievements_counts` (
40+ `achievement_id` int(10) unsigned NOT NULL,
41+ `num_players` int(10) unsigned NOT NULL,
42+ `date_updated` int(10) unsigned NOT NULL,
43+ PRIMARY KEY (`achievement_id`)
44+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
45+
46+
47+ [
48+ 'achievements_counts' => [
49+ 'name' => 'achievements_counts',
50+ 'fields' => [
51+ [
52+ 'name' => 'achievement_id',
53+ 'type' => 'INT',
54+ 'length' => '10',
55+ 'unsigned' => true,
56+ 'null' => false,
57+ ],
58+ [
59+ 'name' => 'num_players',
60+ 'type' => 'INT',
61+ 'length' => '10',
62+ 'unsigned' => true,
63+ 'null' => false,
64+ ],
65+ [
66+ 'name' => 'date_updated',
67+ 'type' => 'INT',
68+ 'length' => '10',
69+ 'unsigned' => true,
70+ 'null' => false,
71+ ],
72+ ],
73+ 'indexes' => [
74+ [
75+ 'type' => 'PRIMARY',
76+ 'cols' => [
77+ [
78+ 'name' => 'achievement_id',
79+ ],
80+ ],
81+ ],
82+ ],
83+ 'props' => [
84+ 'ENGINE' => 'InnoDB',
85+ 'CHARSET' => 'utf8',
86+ ],
87+ ],
88+ ]
89+
90+ You can also use the lexer directly to work with other piece of SQL:
91+
92+ $parser = new SQLParser();
93+ $parser->lex($sql);
94+
95+ print($parser->tokens);
96+
97+ The ` tokens ` property contains an array of tokens. SQL keywords are returned as uppercase,
98+ with multi-word terms (e.g. ` DEFAULT CHARACTER SET ` ) as a single token. Strings and escaped
99+ identifiers are not further processed; they are returned exactly as expressed in the input SQL.
30100
31101
32102## Performance
0 commit comments