예제 #1
0
 /// <summary>
 /// Executes a transaction (a sequence of SQL sentences) from
 /// <para/>the "buffer". After completion of the "buffer" excecution,
 /// <para/>the "buffer" is cleared and buffering is disabled.
 /// </summary>
 public void EndBufferSQLite()
 {
     if (buffer)
     {
         buffer = false;
         using (SQLiteTransaction trans = DBLangConnnection.BeginTransaction())
         {
             using (SQLiteCommand command = new SQLiteCommand(DBLangConnnection))
             {
                 command.CommandText = sql_entry;
                 command.ExecuteNonQuery();
             }
             trans.Commit();
         }
         sql_entry = string.Empty;
     }
 }
예제 #2
0
        /// <summary>
        /// The most important method in LangLib. This creates the database,
        /// <para/>the tables to it (MESSAGES, FORMITEMS, CULTURES).
        /// <para/><para/>Also the FallBackCulture is updated for the underlying form/window.
        /// <para/>Messages from the given <paramref name="messageResource"/> are inserted to
        /// <para/>the database if their don't exists.
        /// <para/><para/>The table fields FORMITEMS.INUSE and MESSAGES.INUSE are updated
        /// <para/>for the FallBackCulture.
        /// </summary>
        /// <param name="messageResource">A resource name that contains the application
        /// <para/>messages in the fall FallBackCulture language.
        /// <para/>For example if I have an application which assembly name is
        /// <para/>LangLibTestWinforms and it has a .resx file called Messages
        /// <para/>I would give this parameter a value of "LangLibTestWinforms.Messages".</param>
        /// <param name="culture">The culture to use for the localization.
        /// <para/>If no culture is given the current system culture is used and
        /// <para/>the FallBackCulture is used as fallback culture.</param>
        /// <param name="loadItems">To load language items or not.</param>
        public void InitalizeLanguageSQLite(string messageResource, CultureInfo culture = null, bool loadItems = true)
        {
            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }

            if (!Design)
            {
                try
                {
                    DateTime dt = DateTime.Now;
                    if (!Directory.Exists(DataDir))
                    {
                        Directory.CreateDirectory(DataDir);
                    }

                    if (DBLangConnnection == null)
                    {
                        string dbFile = DBName == string.Empty ? dbHost : DataDir + DBName;
                        DBLangConnnection = new SQLiteConnection(dbConnectionStr == string.Empty ? "Data Source=" + dbFile + ";Pooling=true;FailIfMissing=false" : dbConnectionStr);
                        DBLangConnnection.Open();
                    }

                    if (!tablesCreated && !dbNoTables)
                    {
                        using (SQLiteCommand command = new SQLiteCommand(TableCreateMessages, DBLangConnnection))
                        {
                            command.ExecuteNonQuery();
                        }

                        using (SQLiteCommand command = new SQLiteCommand(TableCreateFormItems, DBLangConnnection))
                        {
                            command.ExecuteNonQuery();
                        }

                        using (SQLiteCommand command = new SQLiteCommand(TableCreateCultures, DBLangConnnection))
                        {
                            command.ExecuteNonQuery();
                        }
                        tablesCreated = true;
                    }

                    if (!culturesInserted && !loadItems)
                    {
                        string        sql = string.Empty;
                        CultureInfo[] allCultures;
                        allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

                        foreach (CultureInfo ci in allCultures)
                        {
                            sql += InsertCulture(ci);
                        }

                        using (SQLiteTransaction trans = DBLangConnnection.BeginTransaction())
                        {
                            using (SQLiteCommand command = new SQLiteCommand(DBLangConnnection))
                            {
                                command.CommandText = sql;
                                command.ExecuteNonQuery();
                            }
                            trans.Commit();
                        }
                        culturesInserted = true;
                    }

                    if (!loadItems)
                    {
                        GetGuiObjets(fallBackCulture);
                        if (!langUseUpdated)
                        {
                            using (SQLiteTransaction trans = DBLangConnnection.BeginTransaction())
                            {
                                using (SQLiteCommand command = new SQLiteCommand(DBLangConnnection))
                                {
                                    command.CommandText = "UPDATE FORMITEMS SET INUSE = 0 WHERE CULTURE = '" + fallBackCulture + "' ";
                                    command.ExecuteNonQuery();
                                }
                                trans.Commit();
                            }
                            langUseUpdated = true;
                        }
                        SaveLanguageItems(this.BaseInstance, fallBackCulture);
                    }

                    if (loadItems)
                    {
                        List <string> localProps = LocalizedPropsSQLite(AppForm, CultureInfo.CurrentCulture);
                        GetGuiObjets(CultureInfo.CurrentCulture, localProps);
                        LoadLanguageItems(CultureInfo.CurrentCulture);
                    }

                    if (!loadItems)
                    {
                        SaveMessages(messageResource, Assembly.GetExecutingAssembly(), ref DBLangConnnection);
                    }

                    ts = ts.Add((DateTime.Now - dt));
                }
                catch (Exception ex)
                {
                    // invalid processor architecture or missing library parts
                    if (ex.GetType() == typeof(BadImageFormatException))
                    {
                        throw new InvalidSQLIteLibException();
                    }
                    else if (ex.GetType() == typeof(MissingManifestResourceException)) // This is fun. The user actually gets some help from the exception message ;-)
                    {
                        throw new LangLibException(string.Format("Missing resource '{0}'.{1}Perhaps {2}.[Resource file name] would do,{3}without the .resx file extension.",
                                                                 messageResource, Environment.NewLine, GetAssemblyName(this.appType), Environment.NewLine));
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }