diff --git a/source/apphelpers.pas b/source/apphelpers.pas index 5bf28d7f..651b2419 100644 --- a/source/apphelpers.pas +++ b/source/apphelpers.pas @@ -422,6 +422,7 @@ type procedure FindComponentInstances(BaseForm: TComponent; ClassType: TClass; var List: TObjectList); function WebColorStrToColorDef(WebColor: string; Default: TColor): TColor; function UserAgent(OwnerComponent: TComponent): String; + function CodeIndent(Steps: Integer=1): String; var AppSettings: TAppSettings; @@ -2999,6 +3000,16 @@ begin end; +function CodeIndent(Steps: Integer=1): String; +begin + // Provide tab or spaces for indentation, uniquely used for all SQL statements + if AppSettings.ReadBool(asTabsToSpaces) then + Result := StringOfChar(' ', AppSettings.ReadInt(asTabWidth) * Steps) + else + Result := StringOfChar(#9, Steps); +end; + + { Get SID of current Windows user, probably useful in the future function GetCurrentUserSID: string; type diff --git a/source/copytable.pas b/source/copytable.pas index 39f1022b..c19ab0b7 100644 --- a/source/copytable.pas +++ b/source/copytable.pas @@ -357,12 +357,12 @@ var Column: TTableColumn; Key: TTableKey; ForeignKey: TForeignKey; -const - ClausePattern: String = #9 + '%s,' + CRLF; + ClausePattern: String; begin // Compose and run CREATE query TargetTable := FConnection.QuotedDbAndTableName(comboDatabase.Text, editNewTablename.Text); + ClausePattern := CodeIndent + '%s,' + sLineBreak; // Watch out if target table exists try diff --git a/source/csv_detector.pas b/source/csv_detector.pas index 9eb424c7..61d611b3 100644 --- a/source/csv_detector.pas +++ b/source/csv_detector.pas @@ -359,7 +359,7 @@ begin TableName := FConnection.CleanIdent(TableName); Result := 'CREATE TABLE '+FConnection.QuoteIdent(FLoadDataFrm.comboDatabase.Text)+'.'+FConnection.QuoteIdent(TableName)+' (' + sLineBreak; for Col in Columns do begin - Result := Result + #9 + Col.SQLCode; + Result := Result + CodeIndent + Col.SQLCode; if Col <> Columns.Last then Result := Result + ','; Result := Result + sLineBreak; diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 4f69ea2d..280a7f2f 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -4077,25 +4077,25 @@ begin Result := 'CREATE TABLE '+QuoteIdent(Obj.Name)+' ('; TableCols := Obj.GetTableColumns; for TableCol in TableCols do begin - Result := Result + CRLF + #9 + TableCol.SQLCode + ','; + Result := Result + sLineBreak + CodeIndent + TableCol.SQLCode + ','; end; TableCols.Free; TableKeys := Obj.GetTableKeys; for TableKey in TableKeys do begin - Result := Result + CRLF + #9 + TableKey.SQLCode + ','; + Result := Result + sLineBreak + CodeIndent + TableKey.SQLCode + ','; end; TableKeys.Free; TableForeignKeys := Obj.GetTableForeignKeys; for TableForeignKey in TableForeignKeys do begin - Result := Result + CRLF + #9 + TableForeignKey.SQLCode(True) + ','; + Result := Result + sLineBreak + CodeIndent + TableForeignKey.SQLCode(True) + ','; end; TableForeignKeys.Free; TableCheckConstraints := Obj.GetTableCheckConstraints; for TableCheckConstraint in TableCheckConstraints do begin - Result := Result + CRLF + #9 + TableCheckConstraint.SQLCode + ','; + Result := Result + sLineBreak + CodeIndent + TableCheckConstraint.SQLCode + ','; end; TableCheckConstraints.Free; diff --git a/source/event_editor.pas b/source/event_editor.pas index c8940d75..82d7d12b 100644 --- a/source/event_editor.pas +++ b/source/event_editor.pas @@ -272,7 +272,7 @@ begin Result := CreateOrAlter + ' '; if comboDefiner.Text <> '' then Result := Result + 'DEFINER='+DBObject.Connection.QuoteIdent(comboDefiner.Text, True, '@')+' '; - Result := Result + 'EVENT ' + DBObject.Connection.QuoteIdent(ObjName) + CRLF + #9 + 'ON SCHEDULE' + CRLF + #9#9; + Result := Result + 'EVENT ' + DBObject.Connection.QuoteIdent(ObjName) + sLineBreak + CodeIndent + 'ON SCHEDULE' + sLineBreak + CodeIndent(2); if radioOnce.Checked then begin d := dateOnce.DateTime; ReplaceTime(d, timeOnce.DateTime); @@ -297,14 +297,14 @@ begin end; if chkDropAfterExpiration.Checked then - Result := Result + #9 + 'ON COMPLETION NOT PRESERVE' + Result := Result + CodeIndent + 'ON COMPLETION NOT PRESERVE' else - Result := Result + #9 + 'ON COMPLETION PRESERVE'; + Result := Result + CodeIndent + 'ON COMPLETION PRESERVE'; if ObjectExists and (DBObject.Name <> editName.Text) then - Result := Result + CRLF + #9 + 'RENAME TO ' + DBObject.Connection.QuoteIdent(editName.Text); - Result := Result + CRLF + #9 + UpperCase(grpState.Items[grpState.ItemIndex]); - Result := Result + CRLF + #9 + 'COMMENT ' + DBObject.Connection.EscapeString(editComment.Text); - Result := Result + CRLF + #9 + 'DO ' + SynMemoBody.Text; + Result := Result + sLineBreak + CodeIndent + 'RENAME TO ' + DBObject.Connection.QuoteIdent(editName.Text); + Result := Result + sLineBreak + CodeIndent + UpperCase(grpState.Items[grpState.ItemIndex]); + Result := Result + sLineBreak + CodeIndent + 'COMMENT ' + DBObject.Connection.EscapeString(editComment.Text); + Result := Result + sLineBreak + CodeIndent + 'DO ' + SynMemoBody.Text; end; diff --git a/source/exportgrid.pas b/source/exportgrid.pas index 5043f8b2..2e12aa4c 100644 --- a/source/exportgrid.pas +++ b/source/exportgrid.pas @@ -681,47 +681,46 @@ begin case ExportFormat of efHTML: begin Header := - '' + CRLF + CRLF + - '' + CRLF + - ' ' + CRLF + - ' ' + TableName + '' + CRLF + - ' ' + CRLF + - ' ' + CRLF + - ' ' + CRLF + - ' ' + CRLF + CRLF + - ' ' + CRLF + CRLF; + CodeIndent(2) + '' + sLineBreak + + CodeIndent + '' + sLineBreak + sLineBreak + + CodeIndent + '' + sLineBreak + sLineBreak; if chkIncludeQuery.Checked then Header := Header + '

' + GridData.SQL + '

' + CRLF + CRLF; - Header := Header + ' ' + CRLF; + Header := Header + CodeIndent(2) + '
' + sLineBreak; if chkIncludeColumnNames.Checked then begin Header := Header + - ' ' + CRLF + - ' ' + CRLF; + CodeIndent(3) + '' + sLineBreak + + CodeIndent(4) + '' + sLineBreak; Col := Grid.Header.Columns.GetFirstVisibleColumn(True); while Col > NoColumn do begin if Col <> ExcludeCol then - Header := Header + ' ' + CRLF; + Header := Header + CodeIndent(5) + '' + sLineBreak; Col := Grid.Header.Columns.GetNextVisibleColumn(Col); end; Header := Header + - ' ' + CRLF + - ' ' + CRLF; + CodeIndent(4) + '' + sLineBreak + + CodeIndent(3) + '' + sLineBreak; end; - Header := Header + - ' ' + CRLF; + Header := Header + CodeIndent(3) + '' + sLineBreak; end; efExcel, efCSV: begin @@ -847,10 +846,10 @@ begin // JavaScript Object Notation Header := '{' + CRLF; if chkIncludeQuery.Checked then - Header := Header + #9 + '"query": '+FormatPhp(GridData.SQL)+',' + CRLF + Header := Header + CodeIndent + '"query": '+FormatPhp(GridData.SQL)+',' + sLineBreak else - Header := Header + #9 + '"table": '+FormatPhp(TableName)+',' + CRLF ; - Header := Header + #9 + '"rows":' + CRLF + #9 + '['; + Header := Header + CodeIndent + '"table": '+FormatPhp(TableName)+',' + sLineBreak ; + Header := Header + CodeIndent + '"rows":' + sLineBreak + CodeIndent + '['; end; end; @@ -873,9 +872,9 @@ begin // Row preamble case ExportFormat of - efHTML: tmp := ' ' + CRLF; + efHTML: tmp := CodeIndent(4) + '' + sLineBreak; - efXML: tmp := #9'' + CRLF; + efXML: tmp := CodeIndent + '' + sLineBreak; efSQLUpdate: begin tmp := ''; @@ -891,7 +890,7 @@ begin if ExportFormat in [efSQLInsert, efSQLDeleteInsert] then tmp := tmp + 'INSERT' else if ExportFormat = efSQLInsertIgnore then - tmp := tmp + 'INSERT IGNORE' + tmp := tmp + 'INSERT IGNORE' else tmp := tmp + 'REPLACE'; tmp := tmp + ' INTO '+GridData.Connection.QuoteIdent(Tablename); @@ -912,15 +911,15 @@ begin efTextile, efJiraTextile: tmp := TrimLeft(Separator); - efPHPArray: tmp := #9 + 'array('+CRLF; + efPHPArray: tmp := CodeIndent + 'array(' + sLineBreak; efMarkDown: tmp := '| '; efJSON: begin if chkIncludeColumnNames.Checked then - tmp := CRLF + #9#9 + '{' + CRLF + tmp := sLineBreak + CodeIndent(2) + '{' + sLineBreak else - tmp := CRLF + #9#9 + '[' + CRLF + tmp := sLineBreak + CodeIndent(2) + '[' + sLineBreak end; efJSONLines: begin @@ -961,7 +960,7 @@ begin efHTML: begin // Escape HTML control characters in data. Data := HTMLSpecialChars(Data); - tmp := tmp + ' ' + CRLF; + tmp := tmp + CodeIndent(5) + '' + sLineBreak; end; efExcel, efCSV: begin @@ -994,7 +993,7 @@ begin efXML: begin // Print cell start tag. - tmp := tmp + #9#9' ' + Data + ','+CRLF + tmp := tmp + CodeIndent(2) + FormatPhp(Grid.Header.Columns[Col].Text) + ' => ' + Data + ',' + sLineBreak else - tmp := tmp + #9#9 + Data + ','+CRLF; + tmp := tmp + CodeIndent(2) + Data + ',' + sLineBreak; end; efJSON: begin - tmp := tmp + #9#9#9; + tmp := tmp + CodeIndent(3); if chkIncludeColumnNames.Checked then tmp := tmp + FormatPhp(Grid.Header.Columns[Col].Text) + ': '; if GridData.IsNull(ResultCol) then @@ -1087,13 +1086,13 @@ begin // Row epilogue case ExportFormat of efHTML: - tmp := tmp + ' ' + CRLF; + tmp := tmp + CodeIndent(4) + '' + sLineBreak; efExcel, efCSV, efLaTeX, efTextile, efJiraTextile: begin Delete(tmp, Length(tmp)-Length(Separator)+1, Length(Separator)); tmp := tmp + Terminator; end; efXML: - tmp := tmp + #9'' + CRLF; + tmp := tmp + CodeIndent + '' + sLineBreak; efSQLInsert, efSQLInsertIgnore, efSQLReplace, efSQLDeleteInsert: begin Delete(tmp, Length(tmp)-1, 2); tmp := tmp + ');' + CRLF; @@ -1103,15 +1102,15 @@ begin tmp := tmp + ' WHERE' + GridData.GetWhereClause + ';' + sLineBreak; end; efPHPArray: - tmp := tmp + #9 + '),' + CRLF; + tmp := tmp + CodeIndent + '),' + sLineBreak; efMarkDown: tmp := tmp + Terminator; efJSON: begin Delete(tmp, length(tmp)-2,2); if chkIncludeColumnNames.Checked then - tmp := tmp + #9#9 + '},' + tmp := tmp + CodeIndent(2) + '},' else - tmp := tmp + #9#9 + '],'; + tmp := tmp + CodeIndent(2) + '],'; end; efJSONLines: begin Delete(tmp, length(tmp)-1,2); @@ -1130,14 +1129,14 @@ begin case ExportFormat of efHTML: begin tmp := - ' ' + CRLF + - '
' + Grid.Header.Columns[Col].Text + '' + Grid.Header.Columns[Col].Text + '
' + Data + '' + Data + '
' + CRLF + CRLF + - '

' + CRLF + - ' generated ' + DateToStr(now) + ' ' + TimeToStr(now) + - ' by ' + APPNAME + ' ' + Mainform.AppVersion + '' + CRLF + - '

' + CRLF + CRLF + - ' ' + CRLF + - '' + CRLF; + CodeIndent(3) + '' + sLineBreak + + CodeIndent(2) + '' + sLineBreak + sLineBreak + + CodeIndent(2) + '

' + sLineBreak + + CodeIndent(3) + 'generated ' + DateToStr(now) + ' ' + TimeToStr(now) + + CodeIndent(3) + 'by ' + APPNAME + ' ' + Mainform.AppVersion + '' + sLineBreak + + CodeIndent(2) + '

' + sLineBreak + sLineBreak + + CodeIndent + '' + sLineBreak + + '' + sLineBreak; end; efXML: begin if chkIncludeQuery.Checked then @@ -1152,7 +1151,7 @@ begin end; efJSON: begin S.Size := S.Size - 1; - tmp := CRLF + #9 + ']' + CRLF + '}'; + tmp := sLineBreak + CodeIndent + ']' + sLineBreak + '}'; end; else tmp := ''; diff --git a/source/main.pas b/source/main.pas index fd3ab1dc..46497bd1 100644 --- a/source/main.pas +++ b/source/main.pas @@ -13254,24 +13254,24 @@ begin if MenuItem = menuQueryHelpersGenerateSelect then begin - sql := 'SELECT '+Implode(', ', ColumnNames)+CRLF+ - #9'FROM '+ActiveDbObj.QuotedName(False); + sql := 'SELECT ' + Implode(', ', ColumnNames) + SLineBreak + + CodeIndent + 'FROM '+ActiveDbObj.QuotedName(False); end else if MenuItem = menuQueryHelpersGenerateInsert then begin - sql := 'INSERT INTO '+ActiveDbObj.QuotedName(False)+CRLF+ - #9'('+Implode(', ', ColumnNames)+')'+CRLF+ - #9'VALUES ('+Implode(', ', DefaultValues)+')'; + sql := 'INSERT INTO ' + ActiveDbObj.QuotedName(False) + SLineBreak + + CodeIndent + '(' + Implode(', ', ColumnNames) + ')' + SLineBreak + + CodeIndent + 'VALUES (' + Implode(', ', DefaultValues) + ')'; end else if MenuItem = menuQueryHelpersGenerateUpdate then begin - sql := 'UPDATE '+ActiveDbObj.QuotedName(False)+CRLF+#9'SET'+CRLF; + sql := 'UPDATE ' + ActiveDbObj.QuotedName(False) + SLineBreak + CodeIndent + 'SET' + SLineBreak; if ColumnNames.Count > 0 then begin for i:=0 to ColumnNames.Count-1 do begin - sql := sql + #9#9 + ColumnNames[i] + '=' + DefaultValues[i] + ',' + CRLF; + sql := sql + CodeIndent(2) + ColumnNames[i] + '=' + DefaultValues[i] + ',' + SLineBreak; end; Delete(sql, Length(sql)-2, 1); end else - sql := sql + #9#9'??? # No column names selected!'+CRLF; - sql := sql + #9'WHERE ' + WhereClause; + sql := sql + CodeIndent(2) + '??? # No column names selected!' + SLineBreak; + sql := sql + CodeIndent + 'WHERE ' + WhereClause; end else if MenuItem = menuQueryHelpersGenerateDelete then begin sql := 'DELETE FROM '+ActiveDbObj.QuotedName(False)+' WHERE ' + WhereClause; diff --git a/source/preferences.pas b/source/preferences.pas index 0edd1e81..8e6aeabc 100644 --- a/source/preferences.pas +++ b/source/preferences.pas @@ -560,18 +560,18 @@ begin comboSQLFontName.Sorted := True; updownCompletionProposalInterval.Min := 0; updownCompletionProposalInterval.Max := MaxInt; - SynMemoSQLSample.Text := 'SELECT DATE_SUB(NOW(), INTERVAL 1 DAY),' + CRLF + - #9'''String literal'' AS lit' + CRLF + - 'FROM tableA AS ta' + CRLF + - 'WHERE `columnA` IS NULL;' + CRLF + - CRLF + - '-- A comment' + CRLF + - '# Old style comment' + CRLF + - '/* Multi line comment */' + CRLF + - CRLF + - 'CREATE TABLE /*!32312 IF NOT EXISTS*/ tableB (' + CRLF + - #9'id INT,' + CRLF + - #9'name VARCHAR(30) DEFAULT "standard"' + CRLF + + SynMemoSQLSample.Text := 'SELECT DATE_SUB(NOW(), INTERVAL 1 DAY),' + sLineBreak + + CodeIndent + '''String literal'' AS lit' + sLineBreak + + 'FROM tableA AS ta' + sLineBreak + + 'WHERE `columnA` IS NULL;' + sLineBreak + + sLineBreak + + '-- A comment' + sLineBreak + + '# Old style comment' + sLineBreak + + '/* Multi line comment */' + sLineBreak + + sLineBreak + + 'CREATE TABLE /*!32312 IF NOT EXISTS*/ tableB (' + sLineBreak + + CodeIndent + 'id INT,' + sLineBreak + + CodeIndent + 'name VARCHAR(30) DEFAULT "standard"' + sLineBreak + ')'; SynSQLSynSQLSample.TableNames.CommaText := 'tableA,tableB'; for i:=0 to SynSQLSynSQLSample.AttrCount - 1 do begin diff --git a/source/reformatter.pas b/source/reformatter.pas index aa9ce09d..525f6fbc 100644 --- a/source/reformatter.pas +++ b/source/reformatter.pas @@ -241,10 +241,7 @@ begin HttpReq.Request.CharSet := 'utf-8'; HttpReq.Request.UserAgent := apphelpers.UserAgent(Self); Parameters := TStringList.Create; - if AppSettings.ReadBool(asTabsToSpaces) then - Parameters.AddPair('indent', StringOfChar(' ', AppSettings.ReadInt(asTabWidth))) - else - Parameters.AddPair('indent', #9); + Parameters.AddPair('indent', CodeIndent); Parameters.AddPair('input', FInputCode); Result := HttpReq.Post(APPDOMAIN + 'sql-formatter.php', Parameters); if Result.IsEmpty then diff --git a/source/routine_editor.pas b/source/routine_editor.pas index 6d08eb63..b7f921c5 100644 --- a/source/routine_editor.pas +++ b/source/routine_editor.pas @@ -558,7 +558,7 @@ begin Params.Add(tmp); end; if Params.Count > 0 then - Result := Result + CRLF + #9 + Implode(','+CRLF+#9, Params) + CRLF; + Result := Result + sLineBreak + CodeIndent + Implode(',' + sLineBreak + CodeIndent, Params) + sLineBreak; Result := Result + ')'+CRLF; if comboReturns.Enabled then Result := Result + 'RETURNS '+comboReturns.Text+CRLF; diff --git a/source/table_editor.pas b/source/table_editor.pas index 243b875e..6f02ef0f 100644 --- a/source/table_editor.pas +++ b/source/table_editor.pas @@ -575,7 +575,8 @@ var procedure FinishSpecs; begin if Specs.Count > 0 then begin - SQL := SQL + Trim('ALTER TABLE '+DBObject.QuotedName + CRLF + #9 + Implode(',' + CRLF + #9, Specs)) + ';' + CRLF; + SQL := SQL + Trim('ALTER TABLE '+DBObject.QuotedName + sLineBreak + + CodeIndent + Implode(',' + sLineBreak + CodeIndent, Specs)) + ';' + sLineBreak; Specs.Clear; end; end; @@ -847,7 +848,7 @@ begin Node := listColumns.GetFirst; while Assigned(Node) do begin Col := listColumns.GetNodeData(Node); - SQL := SQL + #9 + Col.SQLCode + ','+CRLF; + SQL := SQL + CodeIndent + Col.SQLCode + ',' + sLineBreak; Node := listColumns.GetNextSibling(Node); end; @@ -855,17 +856,17 @@ begin for i:=0 to FKeys.Count-1 do begin tmp := FKeys[i].SQLCode; if tmp <> '' then begin - SQL := SQL + #9 + tmp + ','+CRLF; + SQL := SQL + CodeIndent + tmp + ',' + sLineBreak; Inc(IndexCount); end; end; for i:=0 to FForeignKeys.Count-1 do - SQL := SQL + #9 + FForeignKeys[i].SQLCode(True) + ','+CRLF; + SQL := SQL + CodeIndent + FForeignKeys[i].SQLCode(True) + ',' + sLineBreak; // Check constraints for Constraint in FCheckConstraints do begin - SQL := SQL + #9 + Constraint.SQLCode + ',' + sLineBreak; + SQL := SQL + CodeIndent + Constraint.SQLCode + ',' + sLineBreak; end; if Integer(listColumns.RootNodeCount) + IndexCount + FForeignKeys.Count > 0 then diff --git a/source/tabletools.pas b/source/tabletools.pas index 1e8718ca..8ebe06e2 100644 --- a/source/tabletools.pas +++ b/source/tabletools.pas @@ -1863,7 +1863,7 @@ begin // Prevent DEFAULT value from coming in, to fix errors due to multiple CURRENT_TIMESTAMP values // See issue #2748 Column.DefaultType := cdtNothing; - Struc := Struc + CRLF + #9 + Column.SQLCode + ','; + Struc := Struc + sLineBreak + CodeIndent + Column.SQLCode + ','; end; Delete(Struc, Length(Struc), 1); Struc := Struc + CRLF + ') ENGINE=MyISAM'; @@ -1983,7 +1983,7 @@ begin BaseInsert := BaseInsert + Quoter.QuoteIdent(Data.ColumnNames[i]) + ', '; end; Delete(BaseInsert, Length(BaseInsert)-1, 2); - BaseInsert := BaseInsert + ') VALUES'+CRLF+#9+'('; + BaseInsert := BaseInsert + ') VALUES' + sLineBreak + CodeIndent + '('; while true do begin Output(BaseInsert, False, True, True, True, True); RowCountInChunk := 0; @@ -1991,7 +1991,7 @@ begin while not Data.Eof do begin Row := ''; if RowCountInChunk > 0 then - Row := Row + ','+CRLF+#9+'('; + Row := Row + ',' + sLineBreak + CodeIndent + '('; for i:=0 to Data.ColumnCount-1 do begin if Data.ColIsVirtual(i) then Continue;