You are not logged in.
I'm trying to log in as a guest user.
TSQLAuthGroup.InitializeTable (line 19325 of SQLite3Commons.pas) creates a guest group but not a guest user like with Admin, Supervisor and User. Are guest logins handled differently?
Seems that I was looking at an old version of the code. It's working now, thanks.
If a session has been closed it's possible to get an AV in TSQLRestServer.URI.
Session := SessionGet(SessionID); in line 14322 of SQLite3Commons can return nil. This causes an AV in the following line: if Session.IsValidURL...
Could a check be added.
Problem is I'm truncating the javascript tickcount to 32bits to fit mORMot's signed url format and it's based on the time since 1970. But just realised I can simply create an tick count offset at login and start the tickcount from zero.
Thanks for the feedback. ![]()
Below is some javascript to authenticate against mORMmot. It doesn't guard against the client tick count wrapping round - any help on that would be great. Also can't see the point of working out the offset between server and client time which is copied from TSQLRestClientURI.URI - is this needed? I haven't written much javascript before so any tips or criticism welcome.
<html>
<head>
<script src="lib/jquery-1.4.3.min.js"></script>
<script src="lib/SHA256.js"></script> <!-- found at: www.movable-type.co.uk -->
</head>
<body>
<form>
username: <input id="username" type="text" value="User" />
password: <input id="password" type="text" value="synopse"/>
<button id="LoginButton" type="button">Log in</button>
</form>
<script>
var fUser = "",
fRoot = "",
fSessionID = 0,
fSessionIDHexa8 = "",
fSessionPrivateKey = 0,
//fSessionLastTickCount = 0,
//fSessionTickCountOffset = 0,
PasswordHashHexa = "",
fServerTimeStampOffset = 0;
$("#LoginButton").click(function(){LogIn("root", $("#username").val(), $("#password").val())});
function LogIn(root, username, password){
fRoot = root;
fUser = username;
PasswordHashHexa = Sha256.hash("salt"+password);
$.get("/"+root+"/TimeStamp", gotTimeStamp);
}
function gotTimeStamp(timestamp) {
var s = '', d = new Date(), clientTime = '';
timestamp = parseInt(timestamp);
s = d.getFullYear().toString(2);
while(s.length < 13) { s = '0'+s;}
clientTime = s;
s = d.getMonth().toString(2);
while(s.length < 4) { s = '0'+s;}
clientTime = clientTime +s;
s = (d.getDate()-1).toString(2);
while(s.length < 5) { s = '0'+s;}
clientTime = clientTime +s;
s = d.getHours().toString(2);
while(s.length < 5) { s = '0'+s;}
clientTime = clientTime +s;
s = d.getMinutes().toString(2);
while(s.length < 6) { s = '0'+s;}
clientTime = clientTime +s;
s = d.getSeconds().toString(2);
while(s.length < 6) { s = '0'+s;}
clientTime = clientTime +s;
fServerTimeStampOffset = (timestamp - parseInt(clientTime,2));
$.get("/"+fRoot+"/auth?UserName="+fUser, gotNonce);
}
function gotNonce(aNonce){
//create client nonce
var aClientNonce = "", s = "", d = new Date();
aClientNonce = d.getFullYear().toString();
s = d.getMonth().toString();
if (s.length === 1) { s = '0'+s;}
aClientNonce = aClientNonce + '-' + s;
s = d.getDate().toString();
if (s.length === 1) { s = '0'+s;}
aClientNonce = aClientNonce + '-' + s + ' ';
s = d.getHours().toString();
if (s.length === 1) { s = '0'+s;}
aClientNonce = aClientNonce + s;
s = d.getMinutes().toString();
if (s.length === 1) { s = '0'+s;}
aClientNonce = aClientNonce + ':' + s;
s = d.getSeconds().toString();
if (s.length === 1) { s = '0'+s;}
aClientNonce = aClientNonce + ':' + s;
aClientNonce = Sha256.hash(aClientNonce);
s = "/"+fRoot+"/auth?UserName="+fUser+"&Password=" +
Sha256.hash( fRoot+aNonce+aClientNonce+fUser+PasswordHashHexa )+
"&ClientNonce="+aClientNonce;
$.get(s, gotSession);
};
function gotSession(aSessionKey){
var i = aSessionKey.indexOf("+");
fSessionID = parseInt(aSessionKey.slice(0, i));
fSessionIDHexa8 = fSessionID.toString(16);
while(fSessionIDHexa8.length < 8) { fSessionIDHexa8 = '0'+fSessionIDHexa8; }
fSessionPrivateKey = crc32(PasswordHashHexa, crc32(aSessionKey, 0));
//test it
$.get("/"+SessionSign("root/SampleRecord?SELECT=*&STARTINDEX=0&RESULTS=50"), function () {alert("url was signed OK");});
}
function SessionSign(url) {
var Tix, Nonce, s, ss, d = new Date();
Tix = d.getTime();
Nonce = Tix.toString(16);
while(Nonce.length < 8) { Nonce = '0'+Nonce; }
if (Nonce.length > 8) { Nonce = Nonce.slice(Nonce.length-8) }
ss = crc32(url, crc32(Nonce, fSessionPrivateKey)).toString(16);
while(ss.length < 8) { ss = '0'+ss; }
s = url.indexOf("?") == -1 ? url+'?session_signature=' : url+'&session_signature=';
return s + fSessionIDHexa8 + Nonce + ss;
}
/**
*
* Javascript crc32
* http://www.webtoolkit.info/
*
**/
function crc32 (str, crc) {
function Utf8Encode(string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
};
str = Utf8Encode(str);
var table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D";
if (typeof(crc) == "undefined") { crc = 0; }
var x = 0;
var y = 0;
crc = crc ^ (-1);
if (crc < 0) {crc = 4294967296 + crc;}
for( var i = 0, iTop = str.length; i < iTop; i++ ) {
y = ( crc ^ str.charCodeAt( i ) ) & 0xFF;
x = "0x" + table.substr( y * 9, 8 );
crc = ( crc >>> 8 ) ^ x;
}
crc = ~crc;
if (crc < 0) {crc = 4294967296 + crc;}
return crc;
};
</script>
</html>Thanks a lot.
I've only got as far as decoding the server timestamp and working out the offset in javascript but I'll post when I've got something working.
The timestamp is returned from the server with a text/plain content-type but the nonce is return with content-type set to JSON. Should
aHead := 'Content-Type: '+TEXT_CONTENT_TYPE; be added to TSQLRestServer.Auth in SQLite3Commons.pas?
I'm trying to use javascript to authenticate.
I would have to implement paging at the server level.
That is, make the whole query in the Server, store the result in server cache, then publish only the given page to the client.
If a client makes a query that returns say 50,000 results but sorts them and is only interested the extreme results they may only need a few pages at each end.
In this case it would be fairly inefficient to run the whole query. Could the server cache act a bit like a sparse array, slowly increaseing in size as required?
Thanks, using TableRowCount works fine but the new EngineList isn't quite what I was looking for.
I'm using Slickgrid in 'ajax' mode so the table data is loaded incrementally as the users scrolls, using STARTINDEX and RESULTS parameters in the url. EngineList only returns the row count of the chunk table data requested instead of the full table - could this be the full table row count instead?
It could also be useful to be able to pass a 'where' clause to TableRowCount to get a count of a query's result.
I've managed to link up slickgrid ( https://github.com/mleibman/SlickGrid ) using the YUI syntax for the url.
Slickgrid needs the total records returned to set the scrollbar range. To get this I appended the code below to TSQLRestServerDB.EngineList (SQLite3.pas line 947) but this seems a bit crude. Can you suggest a better way?
i := PosEx(' FROM ', SQL)+6;
tableName := copy(SQL, i, PosEx(' ',SQL,i)-i);
s := 'SELECT COUNT(*) AS total FROM '+tableName+';';
recCount := DB.LockJSON(s);
if recCount = '' then try
try
Req := GetAndPrepareStatement(s);
if Req<>nil then begin
MS := TRawByteStringStream.Create;
try
try
Req^.Execute(0,'',MS,ForceAJAX or (not NoAJAXJSON));
recCount := MS.DataString;
finally
if Req=@fStaticStatement then
Req^.Close;
end;
finally
MS.Free;
end;
end;
except
on ESQLException do
recCount := '';
end;
finally
DB.UnLockJSON(recCount);
end;
result := copy(recCount,2,length(trim(recCount))-3)+',"'+tableName+'":'+trim(result)+'}'; Good point, sounds like a "refresh" button would be best.
Parsing would be nicer. I guess you would only need to look for 'DROP TABLE' and 'CREATE TABLE' but I'm not sure how trivial that would be to implement reliably. I would be happy with a 'refresh' button too.
After executing a 'DROP TABLE' command in synDB Explorer (sample 12) the list of tables doesn't update so you subsequently get errors clicking on them. (low priority issue)
I'm having trouble with authentication too. I've tried to add authentciation to sample 04 - HTTP Client-Server.
On the server side I've added 'true' on line 41 of unit2.pas:
DB := TSQLRestServerDB.Create(Model,ChangeFileExt(paramstr(0),'.db3'), true);In Project04Client.dproj added:
TSQLRestClientURI(Form1.Database).SetUser('User','synopse');on the line before application.run
The 'Add message' button works but the 'Find' button doesn't.
What seems stange is the url created has two session_signature paramaters - could SessionSign(url) be getting called twice?
As I need the row count for the UI I'll go with a separate SQL query.
Was hoping that OData might turn out to be a KISS version of SOAP but from what you say above that seems unlikely.
If I'm using TSQLRestServer.URI() with the yui datatable syntax to get some paginated data how do I get the total records in the set?
Also could it be worth using the odata (www.odata.org) syntax instead of yui's - it seems more thoroughly thought out.
Many thanks.
Yes, this works, giving a fail on duplicate data. But the next record added always gives an ESQLException with the message 'column Name is not unique'.
I've tried using 'stored false' to make a field unique. It works giving a ESQLException message 'constraint failed'. The problem is that the next record added always gives the error 'column Name is not unique' no matter whether the key field has unique data or not. Could you have a look? It can be reproduced in the sample app 2 by changing the TSQLSampleRecord.Name property to stored false.
Thanks for explaining that - not looked at Delphi's currency type before. I've been working with some data created by programs written with CB86 (an old BASIC like language) which uses a variable length binary-coded decimal and thought they looked interesting.
Would it be worth adding a binary-coded decimal type to the synopse library? It would avoid the use of delphi floating point types and hopefully be quick to convert to text/json.
Worked out why I'm getting two colons after the content-type response header. It looks like there needs to be
inc(P); after line 3621 of HTTP_RESPONSE.SetHeaders in synCRTsocks.pas. If that's right could you change it.
Out of interest why does it need to distinguish between known and unknown headers?
Thanks
It's a virtual Windows 2008 R2 server. Using Delphi 2007.
I've found the install is working (I was looking for the service name instead of display name) but I get a windows error 1053 - 'The service did not respond to the start or control request in a timely fashion.' when I try to start the service.
I'm experimenting with this modification of your code above:
/// implements a background Service
program SQLite3HttpServerService;
uses
Windows,
Classes,
SysUtils,
WinSvc,
SQLite3Service,
SQLite3Commons,
SQLite3,
SQLite3HTTPServer,
SampleData in '..\01 - In Memory ORM\SampleData.pas';
// define this conditional if you want the GDI messages to be accessible
// from the background service
{.$define USEMESSAGES}
type
/// class implementing the background Service
TSQLite3HttpService = class(TService)
public
/// the background Server processing all requests
// - TThread should be replaced by your own process
//Server: TThread;
Model: TSQLModel;
DB: TSQLRestServerDB;
Server: TSQLite3HttpServer;
/// event trigerred to start the service
// - e.g. create the Server instance
procedure DoStart(Sender: TService);
/// event trigerred to stop the service
// - e.g. destroy the Server instance
procedure DoStop(Sender: TService);
/// initialize the background Service
constructor Create; reintroduce;
/// release memory
destructor Destroy; override;
end;
const
SERVICENAME = 'SQLite3HttpServerService';
SERVICEDISPLAYNAME = 'SQLite3 Http Server Service';
{ TSQLite3HttpService }
constructor TSQLite3HttpService.Create;
begin
inherited Create(SERVICENAME,SERVICEDISPLAYNAME);
OnStart := DoStart;
OnStop := DoStop;
OnResume := DoStart; // trivial Pause/Resume actions
OnPause := DoStop;
Model := nil;
end;
destructor TSQLite3HttpService.Destroy;
begin
//FreeAndNil(Server);
inherited;
end;
procedure TSQLite3HttpService.DoStart(Sender: TService);
begin
if Model<>nil then
DoStop(nil); // should never happen
//Server := TThread.Create(false);
Model := CreateSampleModel;
DB := TSQLRestServerDB.Create(Model,ChangeFileExt(paramstr(0),'.db3'));
DB.CreateMissingTables(0);
Server := TSQLite3HttpServer.Create('8080',[DB]);
end;
procedure TSQLite3HttpService.DoStop(Sender: TService);
begin
FreeAndNil(Server);
FreeAndNil(DB);
FreeAndNil(Model);
end;
procedure CheckParameters;
var i: integer;
param: string;
begin
with TServiceController.CreateOpenService('','',SERVICENAME) do
// allow to control the service
try
if State<>ssErrorRetrievingState then
for i := 1 to ParamCount do begin
param := paramstr(i);
if param='/install' then
TServiceController.CreateNewService('','',SERVICENAME,
SERVICEDISPLAYNAME, paramstr(0),'','','','',
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS
{$ifdef USEMESSAGES}or SERVICE_INTERACTIVE_PROCESS{$endif},
SERVICE_AUTO_START). // auto start at every boot
Free else
if param='/remove' then begin
Stop;
Delete;
end else
if param='/stop' then
Stop else
if param='/start' then
Start([]);
end;
finally
Free;
end;
end;
var Service: TSQLite3HttpService;
begin
if ParamCount<>0 then
CheckParameters else begin
Service := TSQLite3HttpService.Create;
try
// launches the registered Services execution = do all the magic
ServicesRun;
finally
Service.Free;
end;
end;
end.I'm having trouble getting this working.
When I call TServiceController.CreateNewService like in the param='/install' part of the sample above, no errors are reported. But I can't see my new service in the Windows services control panel.
If I then call TServiceController.Start I get the error: 'The service did not respond to the start or control request in a timely fashion'.
Can you suggest where it could be going wrong?
Thanks
Thanks for the example. How would you go about converting the HTTP server in Sample 4 to a service? Is it along the lines of replacing Server: TThread; with Server: TSQLite3HttpServer; or does a TThread sub-class need to be created?
Thanks, that seems a much better way.
Could an overloaded version of DestGetJoinedTable be added which can be passed a TSQLRestServer instead of client? I've put a version below which seems to work for me.
function TSQLRecordMany.DestGetJoinedTable(aServer: TSQLRestServer;
const aDestWhereSQL: RawUTF8; aSourceID: Integer; JoinKind: TSQLRecordManyJoinKind;
const aCustomFieldsCSV: RawUTF8): TSQLTable;
var Select, SQL, Resp, resphead: RawUTF8;
SelfProps, DestProps: TSQLRecordProperties;
procedure SelectFields(const Classes: array of TSQLRecordProperties);
var i: integer;
begin
for i := 0 to high(Classes) do begin
Select := Select+Classes[i].SQLTableSimpleFields[True,True];
if i<high(Classes) then
Select := Select+',';
end;
end;
begin
result := nil;
if (Self=nil) or (fSourceClass=nil) or (fDestClass=nil) or (aServer=nil) then
exit;
if aSourceID=0 then
if fSourceID<>nil then
aSourceID := fSourceID^;
if aSourceID=0 then
exit;
DestProps := fDestClass.RecordProps;
SelfProps := self.RecordProps;
case JoinKind of
jkDestID:
Select := DestProps.SQLTableName+'.ID';
jkPivotID:
Select := SelfProps.SQLTableName+'.ID';
jkDestFields:
if aCustomFieldsCSV='' then
SelectFields([DestProps]) else
Select := AddPrefixToCSV(pointer(aCustomFieldsCSV),DestProps.SQLTableName+'.');
jkPivotFields:
if aCustomFieldsCSV='' then
SelectFields([SelfProps]) else
Select := AddPrefixToCSV(pointer(aCustomFieldsCSV),SelfProps.SQLTableName+'.');
jkPivotAndDestFields:
if aCustomFieldsCSV='' then
SelectFields([SelfProps,DestProps]) else
Select := aCustomFieldsCSV;
end;
if aDestWhereSQL='' then
// fast inlined prepared statement
SQL := 'SELECT % FROM %,% WHERE %.Source=:(%): AND %.Dest=%.ID' else
if PosEx(':(',aDestWhereSQL,1)>0 then
// statement is globaly inlined -> cache prepared statement
SQL := 'SELECT % FROM %,% WHERE %.Source=:(%): AND %.Dest=%.ID AND %' else
// statement is not globaly inlined -> no caching of prepared statement
SQL := 'SELECT % FROM %,% WHERE %.Source=% AND %.Dest=%.ID AND %';
SQL := FormatUTF8(pointer(SQL),
[Select, DestProps.SQLTableName, SelfProps.SQLTableName,
SelfProps.SQLTableName, aSourceID, SelfProps.SQLTableName, DestProps.SQLTableName, aDestWhereSQL]);
with aServer.URI(aServer.Model.Root,'GET',SQL,Resp,resphead,SUPERVISOR_ACCESS_RIGHTS) do
if Lo=200 then begin // GET with SQL sent
result := TSQLTableJSON.Create([PPointer(Self)^,fDestClass],SQL,Resp);
result.fInternalState := Hi;
end else // get data
result := nil;
end;Thanks, got it working but noticed you must put a space between 'Content-Type' and ':' otherwise the colon gets repeated in the final header.
Could it be worth mentioning GetMimeContentType function in SynCommons.pas in the source code comment as a way to get the content type?
I've put a function below to get the content type from a file extension instead which might be useful - does this look a sensible way to go about it?
function GetMIMEfromFileExt(ext: string): string;
const
extList = 'HTMLJSCSSJPGIFPDFZIPNG';
begin
result := '';
case posex(uppercase(ext), extList) of
1 : result := 'text/html';
5 : result := 'application/javascript';
7 : result := 'text/css';
10 : result := 'image/jpeg';
12 : result := 'image/gif';
15 : result := 'application/pdf';
18 : result := 'application/zip';
20 : result := 'image/png';
end;
end;I've been trying the http.sys implementation using HTTP_RESP_STATICFILE to send files which works great. Only small niggle is *.css and *.js are being sent with a content type of text/plain which can give a warning in the web browser. Html and jpeg static files get sent with the right content type.
Is this a problem in http.sys or should I set the content type as a custom header?
Thanks, it works fine now.
If I save a record with an empty RAWUTF8 value I'm getting an error reading it back. The first line of IsJSONString is giving an AV accessing a nil string (called by GetJSONValues). I've found that sqlite3_column_type is returning SQLITE_NULL for these fields which seems to cause the problem.
Strangely old data is read fine, it only when adding new records.
Can you give any suggestions where I am going wrong. (using delphi 2007)
In the Gibberish implementation the salt is twice the length and it doesn't put checksums in the header. It doesn't look too simple to make them compatible and at any rate I've since realised SHA256 is more appropriate for what I'm doing, so I've left this for now.
Thanks for such a quick response. I'm on my way again! (you were right, I was using cookies)
Thanks for the suggestion. I think I need to spend some more time getting my head round this.
Are the AES routines in SynCrypto.pas compatible with the Gibberish javascript AES library?
https://github.com/mdp/gibberish-aes/bl … ish-aes.js
I've tried to un-encrypt some text encrypted by the Gibberish library but haven't had much success so far.
It seems that the function definition for THttpServerGeneric.request has recently changed and it no longer gets passed the full THttpServerSocket from the request. This has caused me a problem as I was deriving a class which needed the request headers and these aren't now available. Could request headers be added to the function def?
Thanks
I don't seem to be calling TSQLRecordMany.Create constructor directly, only through a standard TSQLRecord which contains one. I'm using the framework embedded and below is the code thats giving me trouble. Can you see whats going wrong?
selectionRec := TSQLSelRec.Create(fdbServers[i],SQLWhere);
sl := TStringList.Create;
try
sl.CommaText := Params;
if (sl.Count > 0) and (LightboxRec.ID > 0) then begin
if TransactionBegin(TSQLSelRec) then try
fi := StrToIntDef(sl.strings[0],-1);
if fi > 0 then begin
SelectionRec.DestList.ManyAdd(fdbServers[i],LightboxRec.ID,fi,true);
for si := 1 to sl.Count - 1 do begin
fi := StrToIntDef(sl.strings[si],-1);
if fi > 0 then
SelectionRec.DestList.ManyAdd(fdbServers[i],fi,true);
end;
end;
finally
Commit;
end;
end;
finally
sl.Free;
LightboxRec.Free;
end;Going back to the first post in the thread, I've got a problem with the new ManyAdd feature. If I do something like (pseudocode):
ManyAdd(client, sourceID, destID, noDupes=True);
for i := 1 to 'rest of items' do
ManyAdd(client, destID[i], noDupes=True); //no sourceIDif the first ManyAdd fails because of the noDuplicates condition it exits before this line is run:
fSourceID^ := aSourceID;Then subsequent calls to ManyAdd all fail because because fSourceID hasn't been set.
Could the line be moved to near the beginning of the function to solve this?
Also found this line doesn't appear at all in ManyDelete so the code construct above wont work - can it be added?
Many Thanks
Didn't think of protecting memory, Thanks.
Apart from the change of nomenclature in unicode delphi which I can see is a compatibility pain is there much loss of functionality? If you use record in place of object do you only loose 'inheritance' which at a low level doesn't seem that important?
Thanks for explaining objects (which even the delphi 3 docs hardly mention). You say above that with records there's "no need to a try..finally block for the stack use". Could you explain this a bit more?
Looks like my source code was out of date. It went:
tmpWidth := FFont.GetAnsiCharWidth(Text, i) * FFontSize / 1000;
if FHorizontalScaling <> 100 then
tmpWidth := tmpWidth * FHorizontalScaling / 100;Thanks for putting in createJpegDirect.
All working well
Just did a test program (was using cgi so hard to debug). The problem seemed to be HorizontalScaling was being initialised to zero. Setting this to 100 solved it. Could this property be initialised to 100 in TPdfPage, to save having to set it each time?
Thanks
I'm having trouble with this line:
e := pdfDoc.Canvas.TextWidth('some text');
It always returns 0 but doesn't give any errors.
Any ideas on where it could be going wrong?
Thanks,
Esmond
Is there a way to measure the height of some text?
Thanks
Would it be worth building in some routines to get the dimensions from image files? It seems slight overkill to use GDI+. I've put an example of what I mean below which seems more efficient. The JPEG dimensions routine is based on something posted on www.delphi3000.com
constructor TPdfImage.CreateJpegDirect(aDoc: TPdfDocument; JpegFileName: TFileName;
var W, H: integer); //overridden in decl
var
ms : TMemoryStream;
b : byte;
begin
inherited Create(aDoc,true);
FAttributes.AddItem('Type','XObject');
FAttributes.AddItem('Subtype','Image');
FFilter.RemoveName('FlateDecode');
FFilter.AddItem(TPdfName.Create('DCTDecode'));
FWriter.Save; // flush to allow direct access to fDestStream
W:=0; H:=0;
ms := TMemoryStream.Create;
try
ms.LoadFromFile(JpegFileName);
GetJpegSize(ms,W,H,b);
FWriter.Add(ms.Memory, ms.Size);
finally
ms.Free;
end;
FWriter.fDestStreamPosition := FWriter.fDestStream.Seek(0,soFromCurrent);
FAttributes.AddItem('Width',W);
FAttributes.AddItem('Height',H);
if b = 24 then
FAttributes.AddItem('ColorSpace','DeviceRGB')
else if b = 8 then
FAttributes.AddItem('ColorSpace','DeviceGray');
FAttributes.AddItem('BitsPerComponent',8);
end;
function GetJpegSize(ms: TMemoryStream; var width, height: integer; var BitDepth: byte): boolean;
var
n : int64;
i : integer;
b, bb : byte;
begin
n := ms.Size - 8;
result := false;
ms.Position := 0;
if n > 0 then begin
ms.ReadBuffer(b,1);
if (b = $FF) then begin
ms.ReadBuffer(b,1);
if b = $D8 then
ms.Read(b,1)
else exit;
end else exit;
end else exit;
while (ms.Position < n) and (b = $FF) do begin
ms.ReadBuffer(b,1);
case b of
$C0 .. $C3:
begin
ms.Seek(3,soCurrent);
ms.Read(b,1);
ms.Read(bb,1);
height := b * 256 + bb;
ms.Read(b,1);
ms.Read(bb,1);
width := b * 256 + bb;
ms.Read(b,1);
BitDepth := b * 8;
Result := True;
exit;
end;
$FF:
ms.Read(b,1);
$D0 .. $D9, $01:
begin
ms.Seek(1,soCurrent);
ms.Read(b,1);
end;
else begin
ms.Read(b,1);
ms.Read(bb,1);
i := b * 256 + bb -2 ;
ms.Seek(i, soCurrent);
ms.Read(b,1);
end;
end;
end;
end;Thanks for your work. I was being vague with 'umbrella' object but I think your TSQLRawFTS3 design covers it.
It could be useful to make the similar treatment of freetext and blob fileds optional. There can be situations where a freetext index is useful with small or maybe fixed length fields. In these cases it would be convenient to get a JSON encoded table of results from queries rather than individually retrieving fields.
From what you say my idea was probably a bit of a 'black box' object without enough switches on the side but treating freetext fields like BLOBs looks like it could have it's merits.
Descending TSQLRecordFTS3 from TSQLRecord doesn't quite seem right. Shouldn't a parent object be distilled from them to make them siblings, otherwise a lot of functions would be overridden (not sure if this is an issue)?
With the queries from my experience there normally needs to be some intervention in the 'n-tier' to make the search interface simple enough for the end user, so as you say it's not an issue.
Would it make sense is to have an 'umbrella' object for tables that use both freetext and non-freetext fields?
Didn't think my solution was that good but it seemed to work in my particular situation. Thanks for the feedback.
Re the new fts type do you think it could be worth extending TSQLRecord instead of descending from it? What I'm thinking is that there could be a new field type of 'fts text'. When this field type is used in a TSQLRecord child definition behind the scenes two tables would be created and both kept in step. Or if no other field types are used then a single fts3 table would be created. I guess this would be harder to implement in the framework but could make using it simpler.
TSQLRecord do you think
I haven't had much luck with debugging. I've got round it by changing TSQLRequest.Close to what's below. Could this have any unwanted effects?
procedure TSQLRequest.Close;
begin
if Request=0 then exit;
repeat
sqlite3_finalize(fRequest);
fRequest := sqlite3_next_stmt(RequestDB,0);
if fRequest = 0 then break;
until not (sqlite3_finalize(fRequest) in [SQLITE_OK,SQLITE_ABORT]);
end;I've been trying out the FTS3 by modifying the Sample 02 - Embedded SQLite ORM project
Adding the following before Application.run; works fine:
TSQLRestServerDB(Form1.Database).DB.GetTableNames(TableNamesAtCreation);
if FindRawUTF8(TableNamesAtCreation, 'fts3index') = -1 then
TSQLRestServerDB(Form1.Database).EngineExecuteAll(FormatUTF8(
'CREATE VIRTUAL TABLE fts3index USING fts3(%, %);',['title', 'notes']));The problem is if I change the add and find buttons onclick event as below I get an assertion failure at line1935 of SQLite3.pas when closing the app. Can you suggest what I'm doing wrong?
procedure TForm1.AddButtonClick(Sender: TObject);
var
SQL: rawUTF8;
begin
SQL := FormatUTF8('INSERT INTO fts3index(title, notes) VALUES(''%'', ''%'');',
[StringToUTF8(NameEdit.Text), StringToUTF8(QuestionMemo.Text)]);
TSQLRestServerDB(Database).EngineExecuteAll(SQL);
end;
procedure TForm1.FindButtonClick(Sender: TObject);
begin
questionmemo.Text := TSQLRestServerDB(Database).DB.ExecuteJSON(
'SELECT * FROM fts3index WHERE fts3index MATCH '''+NAMEEDIT.Text+''';');
end;I would be great if a Free text searching option could be incorporated into your ORM framework.
Also seem to have found a problem with sample03 (pipes) - The server part can't find unit2.pas
Thanks