Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add Condition to ODBCDataSource #896

Closed
PepiBobanov opened this issue Aug 4, 2020 · 13 comments
Closed

How to add Condition to ODBCDataSource #896

PepiBobanov opened this issue Aug 4, 2020 · 13 comments

Comments

@PepiBobanov
Copy link

Hi,
I have 2 scenarios , 1 - to pick from existing odbc source or 2 - to create new with 'new ODBCDataSource' , but i'm little noob, and still can't figure out how to implement, there is no Condition property for ODBCDataSource .
I am building ManagedProject.

@oleg-shilo
Copy link
Owner

Yes, it is a problem. This is because MSI/WiX does not support conditions for ODBCDataSource element.

Though you can have a condition associated with the parent component. Similar problem (but for RegValue element) is discussed here:

regVal.AttributesDefinition += $";Component:Condition={yourCondition}";

@PepiBobanov
Copy link
Author

PepiBobanov commented Aug 5, 2020

I read that but i don't understand it well, i need more complete sample :)

My code is like this:

project = new ManagedProject("Setup",
                            new Dir($@"%ProgramFiles%\{myFolder}",
                                new File( binaries, @"Files\app.exe"
                                    ,new FileShortcut(binaries, "MyApp", $@"%ProgramMenu%\{myFolder}")
                                    ,new FileShortcut(binaries, "MyApp", @"%Desktop%")
                                    )
                                ,new DirFiles(binaries, @"Files\*.dll")
                                ,new Dir("Documentation", new DirFiles(docs, @"Files\docs\*.*")
                                )
                            )
                            , new Property("INSTALLODBC", "no")
                        );

Where to put ODBC , and how to add AttributesDefinition

@oleg-shilo
Copy link
Owner

oleg-shilo commented Aug 5, 2020

:)

You should use triple ` to create code block
image

I have updated your code sample

@oleg-shilo
Copy link
Owner

Have a look at the ODBC sample:
https://github.com/oleg-shilo/wixsharp/blob/6007f95ec3391969563da4ed4163ce5430cde814/Source/src/WixSharp.Samples/Wix%23%20Samples/ODBCDataSource/setup.cs

Basically you will need:

new ODBCDataSource(...)
{
    AttributesDefinition = $";Component:Condition={yourCondition}";
}

@PepiBobanov
Copy link
Author

PepiBobanov commented Aug 5, 2020

            project = new ManagedProject("Setup",
                            new Dir($@"%ProgramFiles%\{myFolder}",
                                new File( binaries, @"Files\app.exe"
                                    ,new FileShortcut(binaries, "MyApp", $@"%ProgramMenu%\{myFolder}")
                                    ,new FileShortcut(binaries, "MyApp", @"%Desktop%")
                                    )
                                ,new DirFiles(binaries, @"Files\*.dll")
                                ,new Dir("Documentation", new DirFiles(docs, @"Files\docs\*.*")
                                ,new ODBCDataSource("[ODBC_DSN]", "[ODBC_DRIVER]", true, true,
                                    new Property("Database", "[ODBC_DATABASE]"),
                                    new Property("Server", "[ODBC_SERVER]")
                                    )
                                    {
                                        AttributesDefinition = $";Component:Condition={new Condition("INSTALLODBC=\"yes\"")}"
                                    }

                                )
                            )
                            , new Property("INSTALLODBC", "no")
                        );

error CNDL0004: The Component element contains an unexpected attribute 'Condition'.
`(

@oleg-shilo
Copy link
Owner

my bad, component does not have a condition as an attribute but as an element.
will have a look at it. you will need to use XML-injection then

@oleg-shilo
Copy link
Owner

OK. this is how you can do it:

project.WixSourceGenerated +=
    doc => doc.FindFirst("ODBCDataSource")
              .Parent("Component")
              .AddElement(new XElement("Condition", "INSTALLODBC = \"yes\""));

I will have a look and provide a moreconvenient way of defining component conditions.

@PepiBobanov
Copy link
Author

This way is working .
Now i need to find how to change ODBCDataSource( source_name and driver dinamically, with session[source_name] doesn't work 👍

oleg-shilo pushed a commit that referenced this issue Aug 5, 2020
@oleg-shilo
Copy link
Owner

Done. In next release you will be able to use:

new ODBCDataSource("DsnName", ...)
{
    ComponentCondition = "INSTALLODBC = \"yes\"" 
}

@PepiBobanov
Copy link
Author

PepiBobanov commented Aug 5, 2020

Any clue how to set ODBC( name , driver ,.. ) from session, they are strings ?
i cane get in msi_BeforeInstall
...
e.Session.Components["ODBCDataSource"] , and thats all :)

if it is possible ofcourse...

@oleg-shilo
Copy link
Owner

I am not sure it is possible.
The documentations does not indicate that these attributes can be determined dynamically for session properties:
https://wixtoolset.org/documentation/manual/v3/xsd/wix/odbcdatasource.html

:(

@PepiBobanov
Copy link
Author

PepiBobanov commented Aug 6, 2020

Np, so i will search other ways, like set registry keys, or post install app, that will make that kind of changes, or set odbc, and after install change name,driver , i wasn't familiar with limits of wix, and persisted, beg your pardon 👍
But still, wix# have great possibilities than VS installer project :)

@PepiBobanov
Copy link
Author

PepiBobanov commented Aug 7, 2020

Way founded:

        [DllImport("ODBCCP32.DLL", CharSet = CharSet.Unicode, SetLastError = true)]
        static extern bool SQLConfigDataSourceW(IntPtr hwndParent, RequestFlags fRequest, string lpszDriver, string lpszAttributes);

        enum RequestFlags : ushort
        {

            ODBC_ADD_DSN = 1,
            ODBC_CONFIG_DSN = 2,
            ODBC_REMOVE_DSN = 3,
            ODBC_ADD_SYS_DSN = 4,
            ODBC_CONFIG_SYS_DSN = 5,
            ODBC_REMOVE_SYS_DSN = 6,
            ODBC_REMOVE_DEFAULT_DSN = 7
        };
        public static bool AddDSN(string name, string dbn, string eng,string host="")
        {
            return SQLConfigDataSourceW(
                (IntPtr)null, RequestFlags.ODBC_ADD_DSN
                , Driver
                , $"DSN={name}\0DBN={dbn}\0ENG={eng}\0LINKS=TCPIP(HOST={host})\0"
                );
        }

PS: lpszDriver, and lpszAttributes are for my case

oleg-shilo added a commit that referenced this issue Aug 29, 2020
* - Issue #902: candle.exe : error CNDL0125 when building bootstrapper application from existing msi and add bootstrapper variable
* Issue #862: I can't seem to get it working
* Issue #900: Preserve messageType value for ExternalUI setup with Custom Actions
* Merge pull request #899 from Q-Sharp/master
* Added Condition Net48_Installed
* Implemented/fixed `WixEntity.ComponentCondition`. Triggered by "How to add Condition to ODBCDataSource #896 "
* Issue #894: LicenseBootstrapperApplication do not add payloads in case of rtf license
* - Added `Process.StartElevated` extension method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants