mORMot and Open Source friends
Check-in [c0e6012df8]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:enhanced TSQLDBConnectionProperties.SQLTableName() method to support several identified SQL dialects
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c0e6012df8550d5525f0d3280d010d6cb3a49529
User & Date: abouchez 2013-11-29 14:26:25
Context
2013-11-29
14:27
enhanced TSQLDBZEOSConnectionProperties.GetFields() to support database schema information check-in: e02f768d51 user: abouchez tags: trunk
14:26
enhanced TSQLDBConnectionProperties.SQLTableName() method to support several identified SQL dialects check-in: c0e6012df8 user: abouchez tags: trunk
2013-11-27
22:10
fixed issue in case of filling an existing TClientDataSet check-in: e3fd3c3595 user: User tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to SynDB.pas.

4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
....
4226
4227
4228
4229
4230
4231
4232


4233
4234
4235
4236
4237
4238
4239














4240
4241
4242
4243
4244
4245
4246
  result := '';
  if (self=nil) or (aTableName='') or (high(aFieldNames)<0) then
    exit;
  if aUnique then
    result := 'UNIQUE ';
  if aIndexName='' then begin
    SQLSplitTableName(aTableName,Owner,Table);
    if (Owner<>'') and not (fDBMS in [dMSSQL,dPostgreSQL]) then
      // MSSQL/PostgreSQL does not expect any schema in the index name
      IndexName := Owner+'.';
    IndexName := IndexName+'Index'+Table+RawUTF8ArrayToCSV(aFieldNames,'');
  end else
    IndexName := aIndexName;
  if aDescending then
    case DB_SQLDESENDINGINDEXPOS[DBMS] of
    posGlobalBefore:
................................................................................
    ColsDesc := RawUTF8ArrayToCSV(aFieldNames,',');
  result := FormatUTF8(CREATNDX,
    [result,CREATNDXIFNE[DBMS in DB_HANDLECREATEINDEXIFNOTEXISTS],
     IndexName,aTableName,ColsDesc]);
end;

function TSQLDBConnectionProperties.SQLTableName(const aTableName: RawUTF8): RawUTF8;


begin
  if (PosEx('"',aTableName)>0) and (aTableName[1]<>'"') then
    result := QuotedStr(aTableName,'"') else
  if (PosEx('''',aTableName)>0) and (aTableName[1]<>'''') then
    result := QuotedStr(aTableName,'''') else
  if PosEx(' ',aTableName)>0 then
    result := '`'+aTableName+'`' else














    result := aTableName;
end;

procedure TSQLDBConnectionProperties.GetIndexesAndSetFieldsColumnIndexed(
  const aTableName: RawUTF8; var Fields: TSQLDBColumnDefineDynArray);
var i,j: integer;
    ColName: RawUTF8;






|
|







 







>
>

<
<
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>







4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
....
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235


4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
  result := '';
  if (self=nil) or (aTableName='') or (high(aFieldNames)<0) then
    exit;
  if aUnique then
    result := 'UNIQUE ';
  if aIndexName='' then begin
    SQLSplitTableName(aTableName,Owner,Table);
    if (Owner<>'') and not (fDBMS in [dMSSQL,dPostgreSQL,dMySQL]) then
      // MSSQL/PostgreSQL/MySQL do not expect any schema in the index name
      IndexName := Owner+'.';
    IndexName := IndexName+'Index'+Table+RawUTF8ArrayToCSV(aFieldNames,'');
  end else
    IndexName := aIndexName;
  if aDescending then
    case DB_SQLDESENDINGINDEXPOS[DBMS] of
    posGlobalBefore:
................................................................................
    ColsDesc := RawUTF8ArrayToCSV(aFieldNames,',');
  result := FormatUTF8(CREATNDX,
    [result,CREATNDXIFNE[DBMS in DB_HANDLECREATEINDEXIFNOTEXISTS],
     IndexName,aTableName,ColsDesc]);
end;

function TSQLDBConnectionProperties.SQLTableName(const aTableName: RawUTF8): RawUTF8;
var BeginQuoteChar, EndQuoteChar: RawUTF8;
    UseQuote: boolean;
begin


  BeginQuoteChar := '"';
  EndQuoteChar := '"';
  UseQuote := PosEx(' ',aTableName)>0;
  case fDBMS of
    dPostgresql:
      if PosEx('.',aTablename)=0 then
        UseQuote := true; // quote if not schema.identifier format
    dMySQL: begin
      BeginQuoteChar := '`';  // backtick/grave accent
      EndQuoteChar := '`';
    end;
    dJet: begin  // note: dMSSQL may SET IDENTIFIER ON to use doublequotes
      BeginQuotechar := '[';
      EndQuoteChar := ']';
    end;
  end;
  if UseQuote and (PosEx(BeginQuoteChar,aTableName)=0) then
    result := BeginQuoteChar+aTableName+EndQuoteChar else
    result := aTableName;
end;

procedure TSQLDBConnectionProperties.GetIndexesAndSetFieldsColumnIndexed(
  const aTableName: RawUTF8; var Fields: TSQLDBColumnDefineDynArray);
var i,j: integer;
    ColName: RawUTF8;