Changeset 88 for branches/DirectWeb/UWebServer.pas
- Timestamp:
- Dec 18, 2009, 12:21:39 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DirectWeb/UWebServer.pas
r87 r88 7 7 uses 8 8 Classes, SysUtils, UHTTPServer, UHTTPSessionFile, 9 UTCPServer, 10 UCommon, 9 UTCPServer, Contnrs, 10 UCommon, syncobjs, 11 11 UMemoryStreamEx, 12 12 UMIMEType, Synautil, 13 USqlDatabase, DOM, XMLRead ;13 USqlDatabase, DOM, XMLRead, UHTMLControls; 14 14 15 15 const … … 17 17 18 18 type 19 { TWebServer } 20 21 TWebServer = class 19 20 { TDatabasePool } 21 22 TDatabasePool = class(TObjectList) 22 23 private 23 HTTPSessionFile: THTTPSessionFile; 24 function PagesList(URL: string; Page, TotalCount, CountPerPage: Integer 25 ): string; 26 procedure ServerInfo(Request: THTTPRequest; Response: THTTPResponse); 24 Lock: TCriticalSection; 27 25 public 28 HTTPServer: THTTPServer;26 PoolSize: Integer; 29 27 HostName: string; 30 28 Schema: string; 31 29 UserName: string; 32 30 Password: string; 33 34 procedure ViewList(Request: THTTPRequest; Response: THTTPResponse); 35 procedure ViewItem(Request: THTTPRequest; Response: THTTPResponse); 36 procedure SendIndex(Request: THTTPRequest; Response: THTTPResponse); 31 procedure Allocate; 32 function Acquire: TSqlDatabase; 33 procedure Release(Database: TSqlDatabase); 34 constructor Create; 35 destructor Destroy; override; 36 end; 37 38 { TWebServer } 39 40 TWebServer = class 41 private 42 SessionStorage: TFileHTTPSessionStorage; 43 procedure ServerInfo(HandlerData: THTTPHandlerData); 44 public 45 HTTPServer: THTTPServer; 46 DatabasePool: TDatabasePool; 47 48 procedure ViewList(HandlerData: THTTPHandlerData); 49 procedure ViewItem(HandlerData: THTTPHandlerData); 50 procedure SendIndex(HandlerData: THTTPHandlerData); 37 51 procedure WriteHeader(Stream: TMemoryStreamEx); 38 52 procedure WriteFooter(Stream: TMemoryStreamEx); … … 63 77 with ChildNodes[I3] do begin 64 78 if NodeName = 'hostname' then 65 HostName := TextContent;79 DatabasePool.HostName := TextContent; 66 80 if NodeName = 'schema' then 67 Schema := TextContent;81 DatabasePool.Schema := TextContent; 68 82 if NodeName = 'username' then 69 UserName := TextContent;83 DatabasePool.UserName := TextContent; 70 84 if NodeName = 'password' then 71 Password := TextContent;85 DatabasePool.Password := TextContent; 72 86 end; 73 87 if NodeName = 'server' then … … 102 116 end; 103 117 104 function TWebServer.PagesList(URL: string; Page, TotalCount, CountPerPage: Integer): string; 105 const 106 Around: Integer = 10; 107 var 108 Count: Integer; 109 PagesMax: Integer; 110 PagesMin: Integer; 111 I: Integer; 112 begin 113 Count := Round(TotalCount / CountPerPage); 114 Result := ''; 115 if Count > 1 then begin 116 if Page > 0 then begin 117 Result := Result + '<a href="' + URL + '0"><<</a> '; 118 Result := Result + '<a href="' + URL + IntToStr(Page - 1) + '"><</a> '; 119 end; 120 PagesMax := Count - 1; 121 PagesMin := 0; 122 if PagesMax > (Page + Around) then PagesMax := Page + Around; 123 if PagesMin < (Page - Around) then begin 124 Result := Result + ' .. '; 125 PagesMin := Page - Around; 126 end; 127 for I := PagesMin to PagesMax do begin 128 if I = Page then Result := Result + '<strong>'; 129 Result := Result + '<a href="' + URL + IntToStr(I) + '">' + IntToStr(I + 1) + '</a> '; 130 if I = Page then Result := Result + '</strong>'; 131 end; 132 if PagesMax < (Count - 1) then Result := Result + ' .. '; 133 if Page < (Count - 1) then begin 134 Result := Result + '<a href="' + URL + IntToStr(Page + 1) + '">></a> '; 135 Result := Result + '<a href="' + URL + IntToStr(Count - 1) + '">>></a>'; 136 end; 137 end; 138 end; 139 140 procedure TWebServer.ViewList(Request: THTTPRequest; Response: THTTPResponse); 118 procedure TWebServer.ViewList(HandlerData: THTTPHandlerData); 141 119 var 142 120 SqlDatabase: TSqlDatabase; … … 147 125 Title: string; 148 126 LinkQuery: TQueryParameterList; 149 begin 150 SqlDatabase := TSqlDatabase.Create; 151 with Response, Stream, SqlDatabase do 127 PageList: TPageList; 128 begin 129 SqlDatabase := DatabasePool.Acquire; 130 with HandlerData, Response, Stream, SqlDatabase do 152 131 begin 153 132 WriteHeader(Stream); 154 HostName := Self.HostName; 155 UserName := Self.UserName; 156 Password := Self.Password; 157 Database := Self.Schema; 158 Connect; 159 WriteString(Request.Query.Values['Table']); 133 134 WriteString('<div align="center">'); 135 136 // Prepare table paging 137 DbRows := Query('SELECT COUNT(*) FROM ' + Request.Query.Values['Table']); 138 PageList := TPageList.Create; 139 PageList.HandlerData := HandlerData; 140 with PageList do begin 141 TotalCount := StrToInt(DbRows[0].ValuesAtIndex[0]); 142 ItemPerPage := 20; 143 NavigatorVisibleItems := 5; 144 Process; 145 end; 146 DbRows.Destroy; 147 148 //WriteString(Request.Query.Values['Table']); 160 149 OrderColumn := Request.Query.Values['OrderCol']; 161 150 OrderDirection := Request.Query.Values['OrderDir']; … … 165 154 OrderDirection := 'DESC'; 166 155 DbRows := Query('SELECT * FROM ' + Request.Query.Values['Table'] + 167 ' ORDER BY ' + OrderColumn + ' ' + OrderDirection );156 ' ORDER BY ' + OrderColumn + ' ' + OrderDirection + PageList.SQLLimit); 168 157 169 158 LinkQuery := TQueryParameterList.Create; 170 159 LinkQuery.Assign(Request.Query); 171 160 172 WriteString('<strong>Seznam typů karet</strong>'); 161 WriteString('<strong>Seznam typů karet</strong><br/>'); 162 WriteString(PageList.Output); 173 163 WriteString('<table><tr>'); 174 164 if DbRows.Count > 0 then … … 197 187 end; 198 188 WriteString('</table>'); 189 WriteString(PageList.Output); 190 WriteString('</div>'); 191 199 192 DbRows.Destroy; 200 193 Destroy; 201 194 WriteFooter(Stream); 202 195 end; 203 end; 204 205 procedure TWebServer.ViewItem(Request: THTTPRequest; Response: THTTPResponse); 196 PageList.Destroy; 197 end; 198 199 procedure TWebServer.ViewItem(HandlerData: THTTPHandlerData); 206 200 var 207 201 SqlDatabase: TSqlDatabase; … … 214 208 begin 215 209 SqlDatabase := TSqlDatabase.Create; 216 with Response, Stream, SqlDatabase do210 with HandlerData, Response, Stream, SqlDatabase do 217 211 begin 218 212 WriteHeader(Stream); 219 HostName := Self.HostName;220 UserName := Self.UserName;221 Password := Self.Password;222 Database := Self.Schema;223 Connect;224 213 DbRows := Query('SELECT * FROM ' + Request.Query.Values['Table'] + 225 214 ' WHERE Id=' + Request.Query.Values['Id']); … … 239 228 end; 240 229 241 procedure TWebServer.ServerInfo( Request: THTTPRequest; Response: THTTPResponse);230 procedure TWebServer.ServerInfo(HandlerData: THTTPHandlerData); 242 231 var 243 232 I: Integer; 244 233 begin 245 with Response.Stream do begin246 Response.Cookies.Values['Test'] := 'Halo';234 with HandlerData, Response.Stream do begin 235 //Response.Cookies.Values['Test'] := 'Halo'; 247 236 //Response.Cookies.Values['Test2'] := 'Halo2'; 248 237 249 HTTPServer.SessionHandler.Variables.Values['Session1'] := 'Value1';238 //HTTPServer.SessionHandler.Variables.Values['Session1'] := 'Value1'; 250 239 //HTTPServer.SessionHandler.Variables.Values['Session2'] := 'Value2'; 251 240 … … 263 252 264 253 WriteString('<h5>Session variables</h5>'); 265 for I := 0 to HTTPServer.SessionHandler.Variables.Count - 1 do begin;266 WriteString( HTTPServer.SessionHandler.Variables.Strings[I] + '<br/>');254 for I := 0 to Session.Count - 1 do begin; 255 WriteString(Session.Strings[I] + '<br/>'); 267 256 end; 268 257 … … 275 264 end; 276 265 277 procedure TWebServer.SendIndex( Request: THTTPRequest; Response: THTTPResponse);278 begin 279 with Response, Stream do266 procedure TWebServer.SendIndex(HandlerData: THTTPHandlerData); 267 begin 268 with HandlerData, Response, Stream do 280 269 begin 281 270 WriteHeader(Stream); 282 WriteString('<img src="logo.png"/><br/>'); 283 WriteString('<strong>Congratulations</strong>'); 284 WriteString('<hr/>'); 285 WriteString('Your PWU LightWebServer based application is running.<br/>'); 286 WriteString('<hr/>'); 287 WriteString('Generated ' + RFC822DateTime(Now) + ' by PWU LightWebServer.'); 271 272 288 273 WriteFooter(Stream); 289 274 end; … … 324 309 begin 325 310 inherited Create; 326 HTTPSessionFile := THTTPSessionFile.Create; 327 with HTTPSessionFile do begin 328 end; 311 DatabasePool := TDatabasePool.Create; 312 DatabasePool.Capacity := 20; 313 DatabasePool.Allocate; 314 315 SessionStorage := TFileHTTPSessionStorage.Create; 329 316 HTTPServer := THTTPServer.Create; 330 317 with HTTPServer, Socket do begin 331 Session Handler := HTTPSessionFile;318 SessionStorage := Self.SessionStorage; 332 319 DocumentRoot := 'Data'; 333 320 with RequestHandlerList do begin … … 346 333 destructor TWebServer.Destroy; 347 334 begin 335 DatabasePool.Destroy; 348 336 HTTPServer.Destroy; 349 HTTPSessionFile.Destroy;337 SessionStorage.Destroy; 350 338 inherited Destroy; 351 339 end; 352 340 341 { TDatabasePool } 342 343 procedure TDatabasePool.Allocate; 344 var 345 I: Integer; 346 begin 347 for I := 0 to PoolSize - 1 do 348 with TSqlDatabase(Items[Add(TSqlDatabase.Create)]) do begin 349 HostName := Self.HostName; 350 UserName := Self.UserName; 351 Password := Self.Password; 352 Database := Self.Schema; 353 Connect; 354 end; 355 end; 356 357 function TDatabasePool.Acquire: TSqlDatabase; 358 begin 359 360 end; 361 362 procedure TDatabasePool.Release(Database: TSqlDatabase); 363 begin 364 try 365 Lock.Acquire; 366 367 finally 368 Lock.Release; 369 end; 370 end; 371 372 constructor TDatabasePool.Create; 373 begin 374 Lock := TCriticalSection.Create; 375 end; 376 377 destructor TDatabasePool.Destroy; 378 begin 379 Lock.Destroy; 380 inherited Destroy; 381 end; 382 353 383 end. 354 384
Note:
See TracChangeset
for help on using the changeset viewer.