I have 50000 RTF files which I want to save them to DB. Because of large size of Blob fields I save them as plain text in DB and save their text styles (only TextColor and BackColor) in another table. My tables is:
Books: BookID, BookName.
BookPages: PageID, BookID, PageNumber, PageText.
Styles: StyleID, StyleName, R_Color, G_Color, B_Color, Back_R_Color, Back_G_Color, Back_B_Color.
PageStyles: PageStyleID, PageID, StyleID, FromPosition, ToPosition.
I load the RTF file to TSRichViewEdit by this code:
Code: Select all
rveMain.Clear;
rveMain.Format;
rveMain.Update;
rveMain.RichViewEdit.LoadRTF(dlgOpen.FileName);
rveMain.Format;
Code: Select all
i := 1;
while i <= rveMain.PageCount do
begin
MyText := '';
rveMain.GetPageStartItemNo(i, FirstItemNo, FirstItemOffset);
rveMain.GetPageLastItemNo(i, LastItemNo, LastItemOffset);
rveMain.RichViewEdit.Deselect;
rveMain.RichViewEdit.SetSelectionBounds(FirstItemNo,FirstItemOffset,LastItemNo, LastItemOffset);
Sleep(50);
rveMain.RichViewEdit.Copy;
Sleep(50);
rveTemp.Clear;
rveTemp.Format;
rveTemp.Update;
rveTemp.RichViewEdit.Paste;
rveTemp.Format;
MyText := rveMain.RichViewEdit.GetSelText;
InsertPages (MyText); // This is a function that inserts page text and other fields in table.
Application.ProcessMessages;
///////////////////////////////////////////////////////
qry2.SQL.Text := 'SELECT SCOPE_IDENTITY() AS MyID';
qry2.Open;
InsertedPageID := qry2.FieldByName('MyID').AsString;
SavePageStylesToDB(InsertedPageID, rveTemp); // A procedure that saves text styles;
////////////////////////////////////////////////////////
i := i + 1;
end;
Code: Select all
procedure SavePageStylesToDB(PageID: string; rveObject : TSRichViewEdit);
var i : Integer;
qryStyles : TADOQuery;
RGB, R, G, B, Back_R, Back_G, Back_B : Integer;
StyleID : string;
FromPos ,tmpLength , ToPos : Integer;
begin
qryStyles := TADOQuery.Create(Self);
qryStyles.Connection := ADOConnection1;
for I := 0 to rveObject.RichViewEdit.ItemCount - 1 do
if rveObject.RichViewEdit.GetItem(i) is TRVTextItemInfo then
begin
RGB := ColorToRGB(rveObject.ExternalRVStyle.TextStyles[rveObject.RichViewEdit.GetItemStyle(i)].Color);
R := GetRValue(RGB);
G := GetGValue(RGB);
B := GetBValue(RGB);
RGB := ColorToRGB(rveObject.ExternalRVStyle.TextStyles[rveObject.RichViewEdit.GetItemStyle(i)].BackColor);
Back_R := GetRValue(RGB);
Back_G := GetGValue(RGB);
Back_B := GetBValue(RGB);
qryStyles.SQL.Text := 'SELECT * FROM Styles ' +
' WHERE RColor = ' + IntToStr(R) +
' and GColor = ' + IntToStr(G) +
' and BColor = ' + IntToStr(B) +
' and Back_RColor = ' + IntToStr(Back_R)+
' and Back_GColor = ' + IntToStr(Back_G)+
' and Back_BColor = ' + IntToStr(Back_B);
qryStyles.Open;
if qryStyles.RecordCount > 0 then
begin
StyleID := qryStyles.FieldByName('StyleID').AsString;
rveObject.RichViewEdit.SetSelectionBounds(i,rveObject.RichViewEdit.GetOffsBeforeItem(i),i,rveObject.RichViewEdit.GetOffsAfterItem(i));
RVGetSelection(rveObject.RichViewEdit,FromPos, tmpLength);
ToPos := FromPos + tmpLength;
qryStyles.SQL.Text := 'INSERT INTO PageStyles (PageID, StyleID, FromPosition, ToPosition) ' +
'Values (' + PageID + ',' + StyleID + ',' + IntToStr(FromPos) + ',' + IntToStr(ToPos) + ')';
qryStyles.ExecSQL;
end;
end;
end;
Code: Select all
AdoQuery1.SQL.Text := 'SELECT PageText FROM BookPages WHERE PageID = ' + MyPageID;
AdoQuery1.Open;
rve.Clear;
rve.RichViewEdit.AddText(AdoQuery1.FieldByName('PageText').AsString,0);
rve.Format;
GetStyles(rve, MyPageID); // A procedure that get and apply styles to page text.
Code: Select all
procedure GetStyles(rveObject : TSRichViewEdit; PageID : string);
begin
AdoQuery2.SQL.Text := 'SELECT PageStyles.PageID, PageStyles.FromPosition, PageStyles.ToPosition , Styles.* ' +
'FROM PageStyles INNER JOIN Styles On PageStyles.StyleID = Styles.StyleID ' +
'WHERE PageID = '+ PageID;
AdoQuery2.Open;
if AdoQuery2.RecordCount > 0 then
begin
while not AdoQuery2.Eof do
begin
RVSetSelection(rveObject.RichViewEdit,AdoQuery2.FieldByName('FromPosition').AsInteger,AdoQuery2.FieldByName('ToPosition').AsInteger-AdoQuery2.FieldByName('FromPosition').AsInteger);
TextColor := RGB(AdoQuery2.FieldByName('R_Color').AsInteger,AdoQuery2.FieldByName('G_Color').AsInteger,AdoQuery2.FieldByName('B_Color').AsInteger);
BackColor := RGB(AdoQuery2.FieldByName('Back_R_Color').AsInteger, AdoQuery2.FieldByName('Back_G_Color').AsInteger, AdoQuery2.FieldByName('Back_B_Color').AsInteger);
rveObject.RichViewEdit.ApplyStyleConversion(TEXT_COLOR);
rveObject.RichViewEdit.ApplyStyleConversion(TEXT_BACKCOLOR);
AdoQuery2.Next;
end;
end;
end;
Now my questions is that:
1. why this code does not work correctly to all of my RTF files?
2. Is this a right idea to save text styles in another table?
My DB is Sql Server and my RTF files content in Arabic Texts.