-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.SQLConnectionProp.pas
More file actions
169 lines (136 loc) · 5.29 KB
/
Base.SQLConnectionProp.pas
File metadata and controls
169 lines (136 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
unit Base.SQLConnectionProp;
{$WARN IMPLICIT_STRING_CAST OFF}
{$WARN IMPLICIT_STRING_CAST_LOSS OFF}
interface
uses
Classes, SysUtils, SynCommons, SynDB, SynOleDB, mORMot;
type
TSQLDatabaseConnection = class(TSQLRecord)
private
fUserName: RawUTF8;
fPassword: RawUTF8;
fServer: RawUTF8;
fIsWinAuth: Boolean;
fPortNo: Integer;
fDataBase: RawUTF8;
public
function FullServerName : RawUTF8;
published
property Server: RawUTF8 read fServer write fServer;
property UserName: RawUTF8 read fUserName write fUserName;
property Password: RawUTF8 read fPassword write fPassword;
property IsWinAuth: Boolean read fIsWinAuth write fIsWinAuth;
property PortNo: Integer read fPortNo write fPortNo;
property Database: RawUTF8 read fDataBase write fDataBase;
end;
TSQLDBConnectionProp = class(TSQLDBConnectionProperties)
private
function GetConnected: Boolean;
public
class function CreateMSSQLConnection(const ADatabaseConnection: TSQLDatabaseConnection): TSQLDBConnectionProp; static;
procedure Connect;
procedure Disconnect;
function ExecuteJSON(const CommandText: RawUTF8): RawUTF8;
function ExecuteTableJSON(const CommandText: RawUTF8): TSQLTableJSON;
// function GetValue(const CommandText, ValueField : RawUTF8) : Variant;
// function GetIntValue(const CommandText, ValueField : RawUTF8) : Integer;
// function GetInt64Value(const CommandText, ValueField: RawUTF8): Int64;
// function GetStrValue(const CommandText, ValueField : RawUTF8) : RawUTF8;
// function GetBoolValue(const CommandText, ValueField : RawUTF8) : Boolean;
// function GetFloatValue(const CommandText, ValueField: RawUTF8): Double;
// function CheckColumnExists(ATableName, AColumnName: RawUTF8): Boolean;
// function CheckFunctionExists(AFunctionName: RawUTF8): Boolean;
// function CheckProcedureExists(AProcedureName: RawUTF8): Boolean;
// function CheckTableExists(ATableName: RawUTF8): Boolean;
// function CheckViewExists(AViewName: RawUTF8): Boolean;
// function CheckDefaultValueExists(ATableName, AColumnName: RawUTF8): Boolean;
property Connected: Boolean read GetConnected;
end;
implementation
{ TSQLDatabaseConnection }
function TSQLDatabaseConnection.FullServerName: RawUTF8;
begin
if fPortNo > 0
then Result := fServer + ',' + IntToStr(fPortNo)
else Result := fServer;
end;
{ TSQLDBConnectionProp }
class function TSQLDBConnectionProp.CreateMSSQLConnection(const ADatabaseConnection: TSQLDatabaseConnection): TSQLDBConnectionProp;
type
TMSSQLConnectionType = (msctGeneric, msct2005, msct2008, msct2012);
function CreateConnection(aType: TMSSQLConnectionType; bIfFailSetNil: Boolean) : TSQLDBConnectionProp;
begin
Result := nil;
with ADatabaseConnection do
case aType of
msctGeneric: TSQLDBConnectionProperties(Result) := TOleDBMSSQLConnectionProperties .Create(FullServerName, Database, UserName, Password);
msct2005: TSQLDBConnectionProperties(Result) := TOleDBMSSQL2005ConnectionProperties.Create(FullServerName, Database, UserName, Password);
msct2008: TSQLDBConnectionProperties(Result) := TOleDBMSSQL2008ConnectionProperties.Create(FullServerName, Database, UserName, Password);
msct2012: TSQLDBConnectionProperties(Result) := TOleDBMSSQL2012ConnectionProperties.Create(FullServerName, Database, UserName, Password);
end;
try
Result.Connect;
except
on e:Exception do
begin
if SameText(E.ClassName, 'EOleSysError') then
if bIfFailSetNil then
Result := nil;
end;
end;
end;
begin
Result := CreateConnection(msct2012, True);
if not Assigned(Result) then Result := CreateConnection(msct2008, True);
if not Assigned(Result) then Result := CreateConnection(msct2005, True);
if not Assigned(Result) then Result := CreateConnection(msctGeneric, False);
Result.ConnectionTimeOutMinutes := 60;
Result.ReconnectAfterConnectionError := True;
Result.UseCache := False;
end;
procedure TSQLDBConnectionProp.Connect;
begin
ThreadSafeConnection.Connect;
end;
procedure TSQLDBConnectionProp.Disconnect;
begin
ThreadSafeConnection.Disconnect;
end;
function TSQLDBConnectionProp.ExecuteJSON(const CommandText: RawUTF8): RawUTF8;
procedure GetResult;
var RowCount: PPtrInt;
var I: ISQLDBRows;
begin
try
I := Execute(CommandText, []);
try
Result := I.FetchAllAsJSON(True, @RowCount);
if Integer(Pointer(RowCount)) = 0
then Result := ''
else Result := StringToUTF8(UTF8ToString(Result)); // tr karakter problemini çözmek için
finally
I := nil;
end;
except
on e:Exception do begin
Result := '';
TSQLLog.Add.Log(sllError,'An error occured inside TSQLDBConnectionProp.ExecuteJSON. SqlText: ?, Message: ?', [CommandText, e.Message]);
end;
end;
end;
begin
if not ThreadSafeConnection.IsConnected then
ThreadSafeConnection.Connect;
GetResult;
end;
function TSQLDBConnectionProp.ExecuteTableJSON(const CommandText: RawUTF8): TSQLTableJSON;
var fResultJSON: RawUTF8;
begin
fResultJSON := ExecuteJSON(CommandText);
Result := TSQLTableJSON.Create(CommandText,pointer(fResultJSON),Length(fResultJSON));
end;
function TSQLDBConnectionProp.GetConnected: Boolean;
begin
Result := ThreadSafeConnection.Connected;
end;
end.