Skip to content

Commit d28aaae

Browse files
committed
Code and comment enhancements.
2 parents 7640511 + 400d55b commit d28aaae

File tree

9 files changed

+532
-3
lines changed

9 files changed

+532
-3
lines changed

.gitignore

100644100755
File mode changed.

composer.json

100644100755
File mode changed.

lib/MySqlFix.php

100644100755
Lines changed: 139 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MySqlFix
2626
*
2727
* @return string
2828
*/
29-
static public function fixComments( $theSourceCode )
29+
static public function fixColumnComments( $theSourceCode )
3030
{
3131
$source_lines = explode( "\n", $theSourceCode );
3232

@@ -61,7 +61,7 @@ static public function fixComments( $theSourceCode )
6161
{
6262
if (preg_match( '/^COMMENT ON COLUMN `(\w+)`.`(\w+)`/', $line, $matches ))
6363
{
64-
$comments[$matches[1]][$matches[2]] = $source_lines[$i+1];
64+
$comments[$matches[1]][$matches[2]] = trim($source_lines[$i+1]);
6565
}
6666
}
6767

@@ -88,7 +88,7 @@ static public function fixComments( $theSourceCode )
8888
}
8989

9090
// Enhance the column definition with comment.
91-
$source_lines[$line_number] = mb_substr( $source_lines[$line_number], 0, -1 );
91+
$source_lines[$line_number] = mb_substr( rtrim($source_lines[$line_number]), 0, -1 );
9292
$source_lines[$line_number] .= " COMMENT '".self::escapeMysqlString( $comment )."',";
9393
}
9494
}
@@ -98,6 +98,142 @@ static public function fixComments( $theSourceCode )
9898
return $new_source_code;
9999
}
100100

101+
102+
//--------------------------------------------------------------------------------------------------------------------
103+
/**
104+
* Add comments to table definitions based on commented comments.
105+
*
106+
* @param string $theSourceCode The SQL code generated by ERD concepts.
107+
*
108+
* @return string
109+
*/
110+
static public function fixTableComments( $theSourceCode )
111+
{
112+
$source_lines = explode( "\n", $theSourceCode );
113+
114+
// Map from (table_name,column_name) to line number
115+
$map = array();
116+
117+
// Scan the source for column definitions.
118+
$table_name = null;
119+
$level = 0;
120+
foreach ($source_lines as $i => $line)
121+
{
122+
if ($table_name)
123+
{
124+
if (preg_match( '/\)|\(/', $source_lines[$i], $matches))
125+
{
126+
if($matches[0] == '(') $level =+ 1;
127+
if($matches[0] == ')') $level =- 1;
128+
129+
if($level < 0)
130+
{
131+
$map[$table_name] = $i;
132+
$table_name = null;
133+
}
134+
}
135+
}
136+
137+
if (!$table_name && preg_match( '/^CREATE TABLE `(\w+)`(\s*\()?/', $line, $matches ))
138+
{
139+
$table_name = $matches[1];
140+
if($matches[2]) $level = 1;
141+
}
142+
}
143+
144+
// Scan the source for comments.
145+
$comments = array();
146+
foreach ($source_lines as $i => $line)
147+
{
148+
if (preg_match( '/^COMMENT ON TABLE `(\w+)`/', $line, $matches ))
149+
{
150+
$comments[$matches[1]] = trim($source_lines[$i+1]);
151+
}
152+
}
153+
154+
// Enhance the column definitions with comments.
155+
foreach ($comments as $table_name => $comment)
156+
{
157+
if (!isset($map[$table_name])) Affirm::assertFailed( "Table '%s' is not defined.", $table_name );
158+
159+
$line_number = $map[$table_name];
160+
161+
// Truncate comments longer than 60 characters.
162+
if (strlen( $comment )>self::MAX_COLUMN_COMMENT_LENGTH)
163+
{
164+
$comment = trim( mb_substr( $comment, 0, self::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'...';
165+
}
166+
167+
// Enhance the column definition with comment.
168+
$source_lines[$line_number] = mb_substr( rtrim($source_lines[$line_number]), 0, -1 );
169+
$source_lines[$line_number] .= " COMMENT '".self::escapeMysqlString( $comment )."';";
170+
}
171+
172+
$new_source_code = implode( "\n", $source_lines );
173+
174+
175+
return $new_source_code;
176+
}
177+
178+
//--------------------------------------------------------------------------------------------------------------------
179+
/**
180+
* Add comments to index definitions based on commented comments.
181+
*
182+
* @param string $theSourceCode The SQL code generated by ERD concepts.
183+
*
184+
* @return string
185+
*/
186+
static public function fixIndexComments( $theSourceCode )
187+
{
188+
$source_lines = explode( "\n", $theSourceCode );
189+
190+
// Map from (table_name,column_name) to line number
191+
$map = array();
192+
193+
// Scan the source for column definitions.
194+
$index_name = null;
195+
foreach ($source_lines as $i => $line)
196+
{
197+
if (preg_match( '/^CREATE INDEX `(\w+)`(\s*\()?/', $line, $matches ))
198+
{
199+
$map[$matches[1]] = $i;
200+
}
201+
}
202+
203+
// Scan the source for comments.
204+
$comments = array();
205+
foreach ($source_lines as $i => $line)
206+
{
207+
if (preg_match( '/^COMMENT ON INDEX `(\w+)`/', $line, $matches ))
208+
{
209+
$comments[$matches[1]] = trim($source_lines[$i+1]);
210+
}
211+
}
212+
213+
// Enhance the column definitions with comments.
214+
foreach ($comments as $index_name => $comment)
215+
{
216+
if (!isset($map[$index_name])) Affirm::assertFailed( "Table '%s' is not defined.", $index_name );
217+
218+
$line_number = $map[$index_name];
219+
220+
// Truncate comments longer than 60 characters.
221+
if (strlen( $comment )>self::MAX_COLUMN_COMMENT_LENGTH)
222+
{
223+
$comment = trim( mb_substr( $comment, 0, self::MAX_COLUMN_COMMENT_LENGTH - 3 ) ).'...';
224+
}
225+
226+
// Enhance the column definition with comment.
227+
$source_lines[$line_number] = mb_substr( rtrim($source_lines[$line_number]), 0, -1 );
228+
$source_lines[$line_number] .= " COMMENT '".self::escapeMysqlString( $comment )."';";
229+
}
230+
231+
$new_source_code = implode( "\n", $source_lines );
232+
233+
234+
return $new_source_code;
235+
}
236+
101237
//--------------------------------------------------------------------------------------------------------------------
102238
/**
103239
* Escapes special characters in a string for use in an SQL statement.

test/ErdConceptMySQLTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
//----------------------------------------------------------------------------------------------------------------------
3+
use SetBased\ErdConcepts\MySqlFix;
4+
5+
//----------------------------------------------------------------------------------------------------------------------
6+
class ErdConceptMySQLTest extends PHPUnit_Framework_TestCase
7+
{
8+
private $mySource;
9+
10+
public function setUp()
11+
{
12+
$this->mySource = file_get_contents(realpath( __DIR__ ).'/php-erd-concepts_create.ddl');
13+
}
14+
15+
//--------------------------------------------------------------------------------------------------------------------
16+
/**
17+
*/
18+
public function testColumns()
19+
{
20+
$template = file_get_contents(realpath( __DIR__ ).'/templates/column_template.ddl');
21+
22+
$result = MySqlFix::fixColumnComments($this->mySource);
23+
$this->assertEquals($template, $result);
24+
}
25+
26+
//--------------------------------------------------------------------------------------------------------------------
27+
/**
28+
*/
29+
public function testTables()
30+
{
31+
$template = file_get_contents(realpath( __DIR__ ).'/templates/table_template.ddl');
32+
33+
$result = MySqlFix::fixTableComments($this->mySource);
34+
$this->assertEquals($template, $result);
35+
}
36+
37+
//--------------------------------------------------------------------------------------------------------------------
38+
/**
39+
*/
40+
public function testIndex()
41+
{
42+
$template = file_get_contents(realpath( __DIR__ ).'/templates/index_template.ddl');
43+
44+
$result = MySqlFix::fixIndexComments($this->mySource);
45+
$this->assertEquals($template, $result);
46+
}
47+
48+
//--------------------------------------------------------------------------------------------------------------------
49+
}
50+
51+
//----------------------------------------------------------------------------------------------------------------------

test/bootstrap.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
//----------------------------------------------------------------------------------------------------------------------
3+
/**
4+
* phpStratum
5+
*
6+
* @copyright 2005-2015 Paul Water / Set Based IT Consultancy (https://www.setbased.nl)
7+
* @license http://www.opensource.org/licenses/mit-license.php MIT
8+
* @link
9+
*/
10+
//----------------------------------------------------------------------------------------------------------------------
11+
error_reporting(E_ALL);
12+
date_default_timezone_set( 'Europe/Amsterdam' );
13+
14+
ini_set('memory_limit', '10000M');
15+
16+
require_once( __DIR__.'/../vendor/autoload.php' );
17+
18+
//----------------------------------------------------------------------------------------------------------------------

test/php-erd-concepts_create.ddl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*================================================================================*/
2+
/* DDL SCRIPT */
3+
/*================================================================================*/
4+
/* Title : */
5+
/* FileName : php-erd-concepts.ecm */
6+
/* Platform : MySQL 5 */
7+
/* Version : Concept */
8+
/* Date : vrijdag 27 februari 2015 */
9+
/*================================================================================*/
10+
/*================================================================================*/
11+
/* CREATE TABLES */
12+
/*================================================================================*/
13+
14+
CREATE TABLE `BAR1` (
15+
`c1` VARCHAR(40) NOT NULL,
16+
`c2` VARCHAR(40),
17+
`c3` VARCHAR(40),
18+
`c4` VARCHAR(40),
19+
`c5` VARCHAR(40),
20+
CONSTRAINT `PK_BAR1` PRIMARY KEY (`c1`)
21+
);
22+
23+
/*
24+
COMMENT ON TABLE `BAR1`
25+
This table is table BAR1.
26+
*/
27+
28+
/*
29+
COMMENT ON COLUMN `BAR1`.`c1`
30+
This column 1. Same name as in table FOO1.
31+
*/
32+
33+
/*
34+
COMMENT ON COLUMN `BAR1`.`c2`
35+
This column 2. Same name as in table FOO1.
36+
*/
37+
38+
CREATE TABLE `FOO1` (
39+
`c1` VARCHAR(40) NOT NULL,
40+
`c2` VARCHAR(40),
41+
`c3` VARCHAR(40),
42+
CONSTRAINT `PK_FOO1` PRIMARY KEY (`c1`)
43+
);
44+
45+
/*
46+
COMMENT ON TABLE `FOO1`
47+
This is table FOO1.
48+
*/
49+
50+
/*
51+
COMMENT ON COLUMN `FOO1`.`c1`
52+
Column 1.
53+
*/
54+
55+
/*
56+
COMMENT ON COLUMN `FOO1`.`c2`
57+
Column 2.
58+
*/
59+
60+
/*
61+
COMMENT ON COLUMN `FOO1`.`c3`
62+
Column 3.
63+
*/
64+
65+
/*================================================================================*/
66+
/* CREATE INDEXES */
67+
/*================================================================================*/
68+
69+
CREATE INDEX `IX_BAR11` ON `BAR1` (`c2`);
70+
71+
/*
72+
COMMENT ON INDEX `IX_BAR11`
73+
Indexes can have comments to.
74+
*/
75+
76+
CREATE INDEX `IX_BAR12` ON `BAR1` (`c3`, `c4`, `c5`);
77+
78+
/*
79+
COMMENT ON INDEX `IX_BAR12`
80+
This is a multi column index.
81+
*/

0 commit comments

Comments
 (0)