예제 #1
0
        /// <summary>
        /// Save the configuration of the Sync SQLite Database
        /// </summary>
        /// <param name="configuration">Sync Configuration </param>
        internal void SaveConfiguration(SQLiteConfiguration configuration)
        {
            XElement xScopeInfoTable = new XElement("ScopeInfoTable");

            // Create Types xml doc.
            foreach (var t in configuration.Types)
            {
                xScopeInfoTable.Add(new XElement("Types", t));
            }

            XDocument doc = new XDocument(xScopeInfoTable);

            var scopeInfoTable = new ScopeInfoTable
            {
                ScopeName     = configuration.ScopeName,
                ServiceUri    = configuration.ServiceUri.AbsoluteUri,
                Configuration = doc.ToString(),
                AnchorBlob    = configuration.AnchorBlob,
                LastSyncDate  = configuration.LastSyncDate
            };

            // Saving Configuration
            using (var connection = SQLitePCL.pretty.SQLite3.Open(localFilePath))
            {
                try
                {
                    string tableScope = connection.Query(SQLiteConstants.ScopeExist).Select(r => r[0].ToString()).FirstOrDefault();

                    bool scopeTableExist = tableScope == "ScopeInfoTable";

                    if (scopeTableExist)
                    {
                        String commandSelect = "Select * From ScopeInfoTable Where ScopeName = ?;";
                        var    exist         = connection.Query(commandSelect, SQLiteHelper.P(configuration.ScopeName)).Select(r => true).Any();

                        string stmtText = exist
                            ? "Update ScopeInfoTable Set ServiceUri = ?, LastSyncDate = ?, Configuration = ?, AnchorBlob = ? Where ScopeName = ?;"
                            : "Insert into ScopeInfoTable (ServiceUri, LastSyncDate, Configuration, AnchorBlob, ScopeName) Values (?, ?, ?, ?, ?);";

                        connection.Execute(stmtText,
                                           SQLiteHelper.P(scopeInfoTable.ServiceUri),
                                           SQLiteHelper.P(scopeInfoTable.LastSyncDate),
                                           SQLiteHelper.P(scopeInfoTable.Configuration),
                                           SQLiteHelper.P(scopeInfoTable.AnchorBlob),
                                           SQLiteHelper.P(scopeInfoTable.ScopeName));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Impossible to save Sync Configuration", ex);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Read Internal Configuration
        /// </summary>
        internal SQLiteConfiguration ReadConfiguration(string databaseScopeName)
        {
            SQLiteConfiguration configuration = new SQLiteConfiguration();

            using (var connection = SQLitePCL.pretty.SQLite3.Open(localFilePath))
            {
                string        s = null;
                List <String> t = new List <string>();
                bool          scopeInfoTableFounded;
                DateTime      d    = new DateTime(1900, 1, 1);
                Byte[]        blob = null;
                try
                {
                    string name = databaseScopeName;

                    ScopeInfoTable scopeInfoTable = null;
                    // Check if Scope Table Exist
                    string tableScope = connection.Query(SQLiteConstants.ScopeExist).Select(r => r[0].ToString()).FirstOrDefault();

                    bool scopeTableExist = tableScope == "ScopeInfoTable";

                    if (scopeTableExist)
                    {
                        String commandSelect = "Select * From ScopeInfoTable Where ScopeName = ?;";
                        foreach (var row in connection.Query(commandSelect, SQLiteHelper.P(name)))
                        {
                            scopeInfoTable               = new ScopeInfoTable();
                            scopeInfoTable.ScopeName     = (String)SQLiteHelper.ReadCol(row, 0, typeof(String));
                            scopeInfoTable.ServiceUri    = (String)SQLiteHelper.ReadCol(row, 1, typeof(String));
                            scopeInfoTable.LastSyncDate  = (DateTime)SQLiteHelper.ReadCol(row, 2, typeof(DateTime));
                            scopeInfoTable.AnchorBlob    = (Byte[])SQLiteHelper.ReadCol(row, 3, typeof(Byte[]));
                            scopeInfoTable.Configuration = (String)SQLiteHelper.ReadCol(row, 4, typeof(String));
                        }
                    }


                    if (scopeInfoTable == null)
                    {
                        return(null);
                    }

                    XDocument document = XDocument.Parse(scopeInfoTable.Configuration);

                    s = scopeInfoTable.ServiceUri;

                    t = (from tt in document.Descendants()
                         where tt.Name == "Types"
                         select tt.Value).ToList();

                    d = scopeInfoTable.LastSyncDate;

                    blob = scopeInfoTable.AnchorBlob;

                    scopeInfoTableFounded = true;
                }
                catch
                {
                    scopeInfoTableFounded = false;
                }

                if (!scopeInfoTableFounded)
                {
                    return(null);
                }

                // Configure Configuration en return it
                configuration.ScopeName    = databaseScopeName;
                configuration.ServiceUri   = new Uri(s);
                configuration.Types        = t;
                configuration.LastSyncDate = d;
                configuration.AnchorBlob   = blob;
            }

            return(configuration);
        }