Move all code for adjusting CREATE TABLE statements to a specific version from exportsql.pas to fixSQL(). Using SynEdit's nice regular expression object. Fixes bug #1843890 "Exportfile: YPE=MyISAM; (missing T)"

This commit is contained in:
Ansgar Becker
2007-12-17 23:12:35 +00:00
parent e32bd641eb
commit 1671ea2c19
2 changed files with 36 additions and 73 deletions

View File

@ -838,53 +838,6 @@ begin
Query.Close;
FreeAndNil(Query);
sql := fixNewlines(sql);
spos := Pos('DEFAULT CHARSET', sql);
if (spos > 0) then begin
epos := Pos2(' ', sql, spos + 14);
// For cases in which no whitespace is following:
if epos = 0 then
epos := Length(sql)+1;
Insert('*/', sql, epos);
Insert('/*!40100 ', sql, spos);
end;
// Mask USING {BTREE,HASH,RTREE} from older servers.
spos := 1;
while true do begin
spos := Pos2('USING', sql, spos);
if spos = 0 then break;
epos := Pos2(' ', sql, spos + 6);
Insert('*/', sql, epos);
Insert('/*!50100 ', sql, spos);
spos := epos + 11;
end;
if target_version = SQL_VERSION_ANSI then
begin
sql := StringReplace(sql, '`', '"', [rfReplaceAll]);
j := max(pos('TYPE=', sql), pos('ENGINE=', sql));
// Delphi's Pos() lacks a start-at parameter. Admittedly very ugly hack to achieve said effect.
k := 0;
while k <= j do k := k + 1 + Pos(' ', Copy(sql, k + 1, Length(sql)));
Delete(sql, j, k - j);
end
else if target_version < 40102 then
begin
{***
@note ansgarbecker
The ENGINE and TYPE options specify the storage engine for the table.
ENGINE was added in MySQL 4.0.18 (for 4.0) and 4.1.2 (for 4.1).
It is the preferred option name as of those versions, and TYPE has
become deprecated. TYPE is supported throughout the 4.x series, but
likely will be removed in the future.
So we use "TYPE" on target versions below 4.1.2 and "ENGINE" on all other versions.
@see http://dev.mysql.com/doc/refman/4.1/en/create-table.html
@see http://www.heidisql.com/forum/viewtopic.php?p=1226#1226
}
sql := stringreplace(sql, 'ENGINE=', 'TYPE=', [rfReplaceAll]);
end
else
begin
sql := stringreplace(sql, 'TYPE=', 'ENGINE=', [rfReplaceAll]);
end;
sql := fixSQL( sql, target_version );
end
{***

View File

@ -10,7 +10,7 @@ interface
uses Classes, SysUtils, Graphics, db, clipbrd, dialogs,
forms, controls, ShellApi, checklst, windows, ZDataset, ZAbstractDataset,
shlobj, ActiveX, StrUtils, VirtualTrees;
shlobj, ActiveX, StrUtils, VirtualTrees, SynRegExpr;
type
@ -1171,9 +1171,7 @@ end;
}
function fixSQL( sql: String; sql_version: Integer = SQL_VERSION_ANSI ): String;
var
p, i: Word;
const
REMSTR : String = 'TOBEREMOVED';
rx : TRegExpr;
begin
result := sql;
if sql_version > SQL_VERSION_ANSI then // For all MySQL-versions
@ -1184,29 +1182,41 @@ begin
// Detect if SQL is a CREATE TABLE statement
if copy( result, 1, 12 ) = 'CREATE TABLE' then
begin
// Strip COLLATE and CHARACTER SET for 4.0 and below servers
// see issue #1685835
if sql_version < 40100 then
begin
result := StringReplace(result, ' COLLATE ', ' '+REMSTR+' ', [rfReplaceAll, rfIgnoreCase]);
result := StringReplace(result, ' CHARACTER SET ', ' '+REMSTR+' ', [rfReplaceAll, rfIgnoreCase]);
result := StringReplace(result, ' CHARSET ', ' '+REMSTR+' ', [rfReplaceAll, rfIgnoreCase]);
while pos( ' '+REMSTR+' ', result ) > 0 do
begin
// Get position of placeholder-word (REMSTR)
p := pos( ' '+REMSTR+' ', result );
for i := p+Length(REMSTR)+2 to Length(result) do
begin
// delete both: placeholder-word + word after placeholder
if not (result[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
begin
Delete( result, p, i-p );
break;
end;
end;
end;
rx := TRegExpr.Create;
// Greedy: take as many chars as I can get
rx.ModifierG := True;
// Case insensitive
rx.ModifierI := True;
end
if sql_version < 40100 then begin
// Strip charset definition
// see issue #1685835
rx.Expression := '\s+(DEFAULT\s+)?(CHARSET|CHARACTER SET)=\w+';
result := rx.Replace(result, '' );
// Strip collation part
rx.Expression := '\s+COLLATE=\w+';
result := rx.Replace(result, '' );
end;
if sql_version = SQL_VERSION_ANSI then begin
// Switch quoting char
result := StringReplace(result, '`', '"', [rfReplaceAll]);
// Strip ENGINE|TYPE
rx.Expression := '\s+(ENGINE|TYPE)=\w+';
result := rx.Replace(result, '' );
end;
// Turn ENGINE to TYPE
if sql_version < 40102 then
result := StringReplace(result, 'ENGINE=', 'TYPE=', [rfReplaceAll])
else
result := StringReplace(result, 'TYPE=', 'ENGINE=', [rfReplaceAll]);
// Mask USING {BTREE,HASH,RTREE} from older servers.
rx.Expression := '\s(USING\s+\w+)';
result := rx.Replace(result, ' /*!50100 $1 */', True);
rx.Free;
end;
end;