Beispiel #1
0
        private static List <DataBasePathTO> GetCustomTableDataBasePaths(Session session, bool isCustomActionData)
        {
            try
            {
                List <DataBasePathTO> listPaths;

                if (isCustomActionData)
                {
                    listPaths = session.CustomActionData.GetObject <List <DataBasePathTO> >("DATABASE_PATHS");
                }
                else
                {
                    listPaths = new List <DataBasePathTO>();
                    DataBasePathTO path;
                    string         sPath;
                    using (Microsoft.Deployment.WindowsInstaller.View v = session.Database.OpenView
                                                                              ("SELECT * FROM `TABLE_DATABASE_PATHS`"))
                    {
                        if (v != null)
                        {
                            v.Execute();
                            for (Record r = v.Fetch(); r != null; r = v.Fetch())
                            {
                                sPath = r.GetString(3);
                                if (string.IsNullOrWhiteSpace(sPath))
                                {
                                    sPath = GetSessionProperty(session, "INSTALLLOCATION", isCustomActionData);
                                }

                                path = new DataBasePathTO()
                                {
                                    Name        = r.GetString(1),
                                    Description = r.GetString(2),
                                    Path        = sPath
                                };
                                listPaths.Add(path);
                                r.Dispose();
                            }
                        }
                    }
                }

                if (listPaths == null || listPaths.Count == 0)
                {
                    throw new InstallerException("No installation paths configured");
                }
                return(listPaths);
            }
            catch (Exception ex)
            {
                InstallUtilities.WriteLogInstall(session, "Exception, GetCustomTableDataBasePaths", ex, true);
                throw;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Query an MSI table for all records
        /// </summary>
        /// <param name="msi">The path to an MSI</param>
        /// <param name="sql">An MSI query</param>
        /// <returns>A list of records is returned</returns>
        /// <remarks>Uses DTF</remarks>
        public static List <DTF.Record> QueryAllRecords(string msi, string query)
        {
            List <DTF.Record> result = new List <DTF.Record>();

            using (DTF.Database database = new DTF.Database(msi, DTF.DatabaseOpenMode.ReadOnly))
            {
                using (DTF.View view = database.OpenView(query, null))
                {
                    view.Execute();

                    DTF.Record record = null;
                    while (null != (record = view.Fetch()))
                    {
                        // Copy record created by Fetch to record created manually to remove View reference
                        DTF.Record copyRecord = new DTF.Record(record.FieldCount);
                        for (int i = 0; i <= record.FieldCount; i++)
                        {
                            copyRecord[i] = record[i];
                        }
                        record.Close();
                        result.Add(copyRecord);
                    }
                }
            }

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Executes the specified SQL SELECT query and returns a single result.
        /// </summary>
        /// <param name="sql">SQL SELECT query string</param>
        /// <param name="record">Optional Record object containing the values that replace
        /// the parameter tokens (?) in the SQL query.</param>
        /// <returns>First field of the first result</returns>
        /// <exception cref="BadQuerySyntaxException">the SQL syntax is invalid</exception>
        /// <exception cref="InstallerException">the View could not be executed
        /// or the query returned 0 results</exception>
        /// <exception cref="InvalidHandleException">the Database handle is invalid</exception>
        /// <remarks><p>
        /// Win32 MSI APIs:
        /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msidatabaseopenview.asp">MsiDatabaseOpenView</a>,
        /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiviewexecute.asp">MsiViewExecute</a>,
        /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiviewfetch.asp">MsiViewFetch</a>
        /// </p></remarks>
        public object ExecuteScalar(string sql, Record record)
        {
            if (String.IsNullOrEmpty(sql))
            {
                throw new ArgumentNullException("sql");
            }

            View   view = this.OpenView(sql);
            Record rec  = null;

            try
            {
                view.Execute(record);
                rec = view.Fetch();
                if (rec == null)
                {
                    throw InstallerException.ExceptionFromReturnCode((uint)NativeMethods.Error.NO_MORE_ITEMS);
                }
                return(rec[1]);
            }
            finally
            {
                if (rec != null)
                {
                    rec.Close();
                }
                view.Close();
            }
        }
Beispiel #4
0
        public void DeleteRecord()
        {
            string dbFile = "DeleteRecord.msi";

            using (Database db = new Database(dbFile, DatabaseOpenMode.CreateDirect))
            {
                WindowsInstallerUtils.InitializeProductDatabase(db);
                WindowsInstallerUtils.CreateTestProduct(db);

                string query = "SELECT `Property`, `Value` FROM `Property` WHERE `Property` = 'UpgradeCode'";

                using (View view = db.OpenView(query))
                {
                    view.Execute();

                    Record rec = view.Fetch();

                    Console.WriteLine("Calling ToString() : " + rec);

                    view.Delete(rec);
                }

                Assert.AreEqual(0, db.ExecuteStringQuery(query).Count);
            }
        }
Beispiel #5
0
 /// <summary>
 /// Checks whether a table contains a persistent column with a given name.
 /// </summary>
 /// <param name="table">The table to the checked</param>
 /// <param name="column">The name of the column to be checked</param>
 /// <returns>true if the column exists in the table; false if the column is temporary or does not exist.</returns>
 /// <exception cref="InstallerException">the View could not be executed</exception>
 /// <exception cref="InvalidHandleException">the Database handle is invalid</exception>
 /// <remarks><p>
 /// To check whether a column exists regardless of persistence,
 /// use <see cref="ColumnCollection.Contains"/>.
 /// </p></remarks>
 public bool IsColumnPersistent(string table, string column)
 {
     if (String.IsNullOrEmpty(table))
     {
         throw new ArgumentNullException("table");
     }
     if (String.IsNullOrEmpty(column))
     {
         throw new ArgumentNullException("column");
     }
     using (View view = this.OpenView(
                "SELECT `Number` FROM `_Columns` WHERE `Table` = '{0}' AND `Name` = '{1}'", table, column))
     {
         view.Execute();
         using (Record rec = view.Fetch())
         {
             return(rec != null);
         }
     }
 }
Beispiel #6
0
        private static ActionResult GetScriptsFromMsiDatabase(Session session, int elevated, string XmlDataProperty, string installAction)
        {
            Database db = session.Database;

            if (!db.Tables.Contains("PowerShellScripts"))
            {
                return(ActionResult.Success);
            }

            try
            {
                CustomActionData data;
                using (View view = db.OpenView(string.Format("SELECT `Id`, `Script` FROM `PowerShellScripts` WHERE `Elevated` = {0}", elevated)))
                {
                    view.Execute();

                    data = new CustomActionData();

                    Record row = view.Fetch();

                    while (row != null)
                    {
                        string script = Encoding.Unicode.GetString(Convert.FromBase64String(row["Script"].ToString()));
                        script = session.Format(script);
                        script = Convert.ToBase64String(Encoding.Unicode.GetBytes(script));

                        data.Add(row["Id"].ToString(), script);
                        session.Log("Adding {0} to CustomActionData", row["Id"]);

                        row = view.Fetch();
                    }
                }

                session[XmlDataProperty] = data.ToString();

                // Tell the installer to increase the value of the final total
                // length of the progress bar by the total number of ticks in
                // the custom action.
                MessageResult iResult;
                using (var hProgressRec = new Record(2))
                {
                    hProgressRec[1] = 3;
                    hProgressRec[2] = TotalTicks;
                    iResult         = session.Message(InstallMessage.Progress, hProgressRec);
                }

                if (iResult == MessageResult.Cancel)
                {
                    return(ActionResult.UserExit);
                }

                return(ActionResult.Success);
            }
            catch (Exception ex)
            {
                session.Log(ex.ToString());
                return(ActionResult.Failure);
            }
            finally
            {
                db.Close();
            }
        }
        private static ActionResult ScriptsImmediate(Session session, int elevated, string deferredProperty)
        {
            Database db = session.Database;

            if (!db.Tables.Contains("PowerShellScripts"))
            {
                return(ActionResult.Success);
            }

            try
            {
                List <ScriptActionData> scripts = new List <ScriptActionData>();
                using (View view = db.OpenView(string.Format("SELECT `Id`, `Script`, `IgnoreErrors`, `Condition` FROM `PowerShellScripts` WHERE `Elevated` = {0} ORDER BY `Order`", elevated)))
                {
                    view.Execute();

                    Record row;
                    while ((row = view.Fetch()) != null)
                    {
                        string condition = row["Condition"]?.ToString();
                        if (!string.IsNullOrEmpty(condition) && !session.EvaluateCondition(condition))
                        {
                            session.Log($"Condition evaluated to false. Skip PS script {row["Id"]?.ToString()}");
                            continue;
                        }

                        string script = Encoding.Unicode.GetString(Convert.FromBase64String(row["Script"].ToString()));
                        script = session.Format(script);
                        script = Convert.ToBase64String(Encoding.Unicode.GetBytes(script));

                        ScriptActionData data = new ScriptActionData()
                        {
                            Id           = row["Id"].ToString(),
                            Script       = script,
                            IgnoreErrors = row["IgnoreErrors"].ToString() == "1"
                        };

                        scripts.Add(data);
                        session.Log("Adding {0} to CustomActionData", data.Id);
                    }
                }

                XmlSerializer srlz = new XmlSerializer(scripts.GetType());
                using (StringWriter sw = new StringWriter())
                {
                    srlz.Serialize(sw, scripts);
                    session[deferredProperty] = sw.ToString();
                }

                // Tell the installer to increase the value of the final total
                // length of the progress bar by the total number of ticks in
                // the custom action.
                MessageResult iResult;
                using (var hProgressRec = new Record(2))
                {
                    hProgressRec[1] = 3;
                    hProgressRec[2] = TotalTicks;
                    iResult         = session.Message(InstallMessage.Progress, hProgressRec);
                }

                if (iResult == MessageResult.Cancel)
                {
                    return(ActionResult.UserExit);
                }

                return(ActionResult.Success);
            }
            catch (Exception ex)
            {
                session.Log(ex.ToString());
                return(ActionResult.Failure);
            }
            finally
            {
                db.Close();
            }
        }
Beispiel #8
0
        private static List <FeactureInstallTO> GetFeactureScriptDataBase(Session session, List <string> listFeactureNames)
        {
            string sFileName = string.Empty;

            try
            {
                List <FeactureInstallTO> listF;

                string sLevel = session["INSTALLLEVEL"];
                listF = new List <FeactureInstallTO>();
                FeactureInstallTO f;
                int i;

                string sQuery = "SELECT `Feature`.`Feature`, `Feature`.`Title`, `Feature`.`Display`, " +
                                " `Component`.`Directory_`, `File`.`FileName` " +
                                " FROM `FeatureComponents`, `Feature`, `Component`, `File` " +
                                " WHERE `FeatureComponents`.`Feature_` = `Feature`.`Feature` " +
                                " AND `FeatureComponents`.`Component_` = `Component`.`Component` " +
                                " AND `File`.`Component_` = `Component`.`Component` " +
                                " AND `Feature`.`RuntimeLevel` > 0 AND `Feature`.`Level` > 0 " +
                                // " AND `Feature`.`Level` <= " + sLevel +
                                " ORDER BY `Feature`.`Display`";
                using (Microsoft.Deployment.WindowsInstaller.View v = session.Database.OpenView(sQuery))
                {
                    if (v != null)
                    {
                        v.Execute();

                        //var str = string.Empty;
                        //foreach (var fea in listFeactureNames)
                        //    str += fea.ToString();

                        //MessageBox.Show(str);
                        //str = string.Empty ;

                        //var frm = new FeatureList( session,v,listFeactureNames);
                        //frm.ShowDialog();

                        for (Record r = v.Fetch(); r != null; r = v.Fetch())
                        {
                            if (listFeactureNames.Contains(r.GetString("Feature")) && r.GetString("FileName").ToUpper().EndsWith(".SQL"))
                            {
                                i = r.GetString("FileName").IndexOf("|");
                                if (i > 0)
                                {
                                    sFileName = r.GetString("FileName").Substring(i + 1);
                                }
                                else
                                {
                                    sFileName = r.GetString("FileName");
                                }

                                f = new FeactureInstallTO()
                                {
                                    Feature       = r.GetString("Feature"),
                                    Title         = r.GetString("Title"),
                                    DisplayOrder  = r.GetInteger("Display"),
                                    FileName      = sFileName,
                                    DirectoryPath = session.GetTargetPath(r.GetString("Directory_"))
                                };
                                listF.Add(f);
                            }
                            r.Dispose();
                        }
                    }
                }
                return(listF);
            }
            catch (Exception ex)
            {
                InstallUtilities.WriteLogInstall(session, "Fail to Read Feature Script", ex, true);
                throw;
            }
        }
        /// <summary>
        ///     Returns data from the MSI tables: `FeatureComponents`, `Feature`, `Component`, `File`.
        ///     We filter to just get the files that end in (*.SQL).
        /// </summary>
        /// <param name="session">Windows Installer Session.</param>
        /// <param name="listFeatureNames">List list with names to be installed.</param>
        /// <returns>Returns scripts database files to be installed.</returns>
        private static List <FeatureInstallTO> GetFeatureScriptDataBase(Session session, List <string> listFeatureNames)
        {
            try
            {
                List <FeatureInstallTO> listFeatures;

                string sLevel = session["INSTALLLEVEL"];
                listFeatures = new List <FeatureInstallTO>();
                FeatureInstallTO f;
                int    i;
                string sFileName;
                string sQuery = "SELECT `Feature`.`Feature`, `Feature`.`Title`, `Feature`.`Display`, " +
                                " `Component`.`Directory_`, `File`.`FileName` " +
                                " FROM `FeatureComponents`, `Feature`, `Component`, `File` " +
                                " WHERE `FeatureComponents`.`Feature_` = `Feature`.`Feature` " +
                                " AND `FeatureComponents`.`Component_` = `Component`.`Component` " +
                                " AND `File`.`Component_` = `Component`.`Component` " +
                                " AND `Feature`.`RuntimeLevel` > 0 AND `Feature`.`Level` > 0 " +
                                // " AND `Feature`.`Level` <= " + sLevel +
                                " ORDER BY `Feature`.`Display`";
                using (View v = session.Database.OpenView(sQuery))
                {
                    if (v != null)
                    {
                        v.Execute();
                        for (Record r = v.Fetch(); r != null; r = v.Fetch())
                        {
                            if (listFeatureNames.Contains(r.GetString("Feature")) &&
                                r.GetString("FileName").ToUpper().EndsWith(".SQL"))
                            {
                                i = r.GetString("FileName").IndexOf("|", StringComparison.Ordinal);
                                if (i > 0)
                                {
                                    sFileName = r.GetString("FileName").Substring(i + 1);
                                }
                                else
                                {
                                    sFileName = r.GetString("FileName");
                                }

                                f = new FeatureInstallTO
                                {
                                    Feature       = r.GetString("Feature"),
                                    Title         = r.GetString("Title"),
                                    DisplayOrder  = r.GetInteger("Display"),
                                    FileName      = sFileName,
                                    DirectoryPath = session.GetTargetPath(r.GetString("Directory_"))
                                };
                                listFeatures.Add(f);
                            }
                            r.Dispose();
                        }
                    }
                }

                return(listFeatures);
            }
            catch (Exception ex)
            {
                InstallUtilities.WriteLogInstall(session, "Exception, ReadFeactureScriptDataBase", ex, true);
                throw;
            }
        }