Hello Paolo,
On Wednesday 11 May 2011, 13:33, Paolo Mantovani wrote:
> Hallo,
>
> I need to change (via API) the database bound to a given document.
>
> In practice I need to perform via API the same operation that you
> normally do via the menu Modify->Change Database.
In Writer I see Edit - Exchange Database
>
> I tried with:
>
> oSet = oDoc.createInstance("com.sun.star.text.DocumentSettings")
> oSet.CurrentDatabaseDataSource = "newsource"
> oSet.CurrentDatabaseCommandType = com.sun.star.sdb.CommandType.TABLE
> oSet.CurrentDatabaseCommand = "newtable"
>
In fact, this *does* exchange the database, just see the dialog in the Edit -
Exchange Database menu, at the bottom you'll see that settings are changed and
stored; for example, I had Database1.Contacts and after executing your code I
see in this dialog
"Database applied to document: newsource.newtable"
Changing the document settings does not even test if there is actually a DB
and a table with the given names.
It is the client code responsibility to check this and register the new data
source in case the test fails.
> But this does not affect the database fieldmasters in the document, that
> are still pointing to the old datasource.
Updating/refreshing the fields as Fernand suggested does not work; as far as I
could understand reading the implementation, you have to instantiate new field
masters for your new data source (because Writer does not seem to create them
until they are "in use", that is, until the user really inserts a field), and
then update all the fields in the documents iterating on the text field
collection (not via XRefreshable.refresh()).
I haven't test any of these, but it seems to be what happens internally when
".uno:ChangeDatabaseField" is dispatched:
* the slot id FN_CHANGE_DBFIELD is executed by the SwBaseShell, void
SwBaseShell::ExecField
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/ui/shells/basesh.cxx#2914
* the dialog impl. is SwChangeDBDlg
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/ui/inc/changedb.hxx
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/ui/fldui/changedb.cxx
You can see how two different things happen there:
a) changing the data source, will lead to
void SwDoc::ChgDBData(const SwDBData& rNewData)
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/core/doc/doc.cxx#682
b) updating the document's field will lead to
void SwDoc::ChangeDBFields( const SvStringsDtor& rOldNames,
const String& rNewName )
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/core/doc/docfld.cxx#1908
Using the API to change the com.sun.star.text.DocumentSettings does the same
as (a), it calls SwDoc::ChgDBData
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/ui/uno/SwXDocumentSettings.cxx#465
>It seems that I'm missing something.
the above shows you are missing (b), it is not enough to change the
com.sun.star.text.DocumentSettings, you have to instantiate the field masters,
and modify the existing fields.
Note that SwDoc::ChangeDBFields does a few things more:
it updates the text section's condition (a text section may bi hidden
according to a db related condition like Datasource.Table.Field EQ Something
so you have to change Dataource.Table to newdatasource.newtable in your
example)
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/core/doc/docfld.cxx#1918
it updates *not only* database fields, but also other text fields, see the
switch in
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/core/doc/docfld.cxx#1947
Hope this helps.
Regards
--
Ariel Constenla-Haile
La Plata, Argentina
signature.asc
Description: This is a digitally signed message part.