]> lifelog.hopto.org Git - LifeLog.git/commitdiff
Found way to store RTF notes to PG db properly.
authorwbudic <redacted>
Tue, 10 Aug 2021 00:38:29 +0000 (10:38 +1000)
committerwbudic <redacted>
Tue, 10 Aug 2021 00:38:29 +0000 (10:38 +1000)
htdocs/cgi-bin/json.cgi
htdocs/cgi-bin/system/modules/Settings.pm

index 36b6c7a5aec7942751994e8b8079ee4a3c4367ac..82dfdab037cdd4226e2918ebafd9fc260835eb4a 100755 (executable)
@@ -11,6 +11,8 @@ use CGI;
 use CGI::Session '-ip_match';
 use CGI::Carp qw ( fatalsToBrowser );
 use DBI;
+use DBD::Pg;
+use DBD::Pg qw(:pg_types);
 
 use DateTime;
 use DateTime::Format::SQLite;
@@ -103,26 +105,30 @@ sub processSubmit {
         if($action eq 'store'){
 
            my $cipher = Crypt::CBC->new(-key  => cryptKey(), -cipher => 'Blowfish');
-              $doc = qq({
-                                "lid": "$lid",
-                                "bg":  "$bg",
-                                "doc": $doc
-              });       
-        
-                   
-               $doc = compress($cipher->encrypt($doc), Z_BEST_COMPRESSION) if !Settings::isProgressDB(); 
-               @arr = Settings::selectRecords($db, "SELECT LID FROM NOTES WHERE LID = $lid;")->fetchrow_array();
-           if (!@arr) {
-                        $st = $db->prepare("INSERT INTO NOTES(LID, DOC) VALUES (?, ?);");
-                        $st->execute($lid, $doc);
-                        $response = "Stored Document (id:$lid)!";
-           }
-           else{
-                        $st = $db->prepare("UPDATE NOTES SET DOC = ? WHERE LID = $lid;");
-                        $st->execute($doc);
-                        $response = "Updated Document (id:$lid)!";
-           }
-           
+            $doc = qq({
+                        "lid": "$lid",
+                        "bg":  "$bg",
+                        "doc": $doc
+            });
+            $doc = compress($cipher->encrypt($doc), Z_BEST_COMPRESSION);
+            @arr = Settings::selectRecords($db, "SELECT LID FROM NOTES WHERE LID = $lid;")->fetchrow_array();
+            if (!@arr) {
+                            $st = $db->prepare("INSERT INTO NOTES(LID, DOC) VALUES (?, ?);");
+                            $st->bind_param(1, $lid);
+                            if(Settings::isProgressDB()){
+                                $st->bind_param(2, $doc, { pg_type => DBD::Pg::PG_BYTEA })
+                            }else{
+                                $st->bind_param(2, $doc)
+                            }
+                            $st->execute();
+                            $response = "Stored Document (id:$lid)!";
+            }
+            else{
+                            $st = $db->prepare("UPDATE NOTES SET DOC = ? WHERE LID = $lid;");
+                            if(Settings::isProgressDB()){$st->bind_param(1, $doc, { pg_type => DBD::Pg::PG_BYTEA })}else{$st->bind_param(1,$doc);}
+                            $st->execute();                                
+                            $response = "Updated Document (id:$lid)!";
+            }
         }
         elsif($action eq 'load'){
 
@@ -131,11 +137,11 @@ sub processSubmit {
                 @arr = Settings::selectRecords($db,"SELECT DOC FROM NOTES WHERE LID = '0';")->fetchrow_array();
            }            
            $doc = $arr[0];
-           if(!Settings::isProgressDB()){
+          
             my $d = uncompress($doc);
             my $cipher = Crypt::CBC->new(-key  => cryptKey(), -cipher => 'Blowfish');
             $doc = $cipher->decrypt($d);
-           }
+          
             # print $cgi->header( -expires => "+0s", -charset => "UTF-8" );
             # print($doc);
             # exit;
index f36a4772579e9aac06f77593c3d3ed868b299c0d..1d1e70349616385117966304355ea7e0cf8d2cd0 100644 (file)
@@ -361,9 +361,8 @@ return qq(
 )}
 sub createNOTEStmt {
     if($IS_PG_DB){
-        #TODO 09082021 - Couldn't figure out how to PGDB via STD driver send binary data in one go.
-        # Hence data there is not compressed and encrypted.
-       return qq(CREATE TABLE NOTES (LID INT PRIMARY KEY NOT NULL, DOC jsonb);) 
+      # return qq(CREATE TABLE NOTES (LID INT PRIMARY KEY NOT NULL, DOC jsonb);)
+      return qq(CREATE TABLE NOTES (LID INT PRIMARY KEY NOT NULL, DOC bytea);) 
     }
     return qq(CREATE TABLE NOTES (LID INT PRIMARY KEY NOT NULL, DOC TEXT);)
 }