Exemple #1
0
        public static IEnumerable <Item> GetChildItems(int uniqueItemId)
        {
            parentUniqueIdParam.Value = uniqueItemId;
            SQLiteDataReader reader = ((SQLiteCommand)selectChildItemsCommand.Clone()).ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    int    uniqueId = reader.GetInt32(0);
                    ushort itemId   = (ushort)reader.GetInt32(1);
                    byte   extra    = reader.GetByte(2);
                    Item   item     = Item.Create(itemId);
                    item.Extra = extra;

                    if (item is Container)
                    {
                        foreach (var i in GetChildItems(uniqueId))
                        {
                            ((Container)item).AddItem(i);
                        }
                    }

                    yield return(item);
                }
            }
            finally
            {
                reader.Close();
            }
        }
 protected void QueueCommand(SQLiteCommand cmd)
 {
     lock (delayedCommands)
     {
         delayedCommands.Add((cmd.Clone() as SQLiteCommand));
     }
 }
Exemple #3
0
        public void ExecuteNonQueryBatch(List <string> sqlTexts)
        {
            try
            {
                SQLiteConnection  con   = OpenConnection();
                SQLiteTransaction trans = con.BeginTransaction();
                SQLiteCommand     cmd   = new SQLiteCommand(con);

                foreach (var item in sqlTexts)
                {
                    cmd.CommandText = item;
                    cmd.ExecuteNonQuery();

                    if (_connectionCurrent != _connectionDisk)
                    {
                        SQLiteCommand newCmd;
                        if (_sqlCmdPool.TryDequeue(out newCmd) == false)
                        {
                            newCmd = (SQLiteCommand)cmd.Clone();
                        }
                        newCmd.CommandText = cmd.CommandText;
                        foreach (SQLiteParameter param in cmd.Parameters)
                        {
                            newCmd.Parameters.Add(param.Clone());
                        }

                        _currWriteQueue.Enqueue(newCmd);
                    }
                }
                trans.Commit();
            }
            catch (Exception) { throw; }
        }
Exemple #4
0
        /// <summary>
        /// 执行SQL命令 - 增、删、改
        /// </summary>
        /// <param name="cmd">已经设置好连接/命令字符串/参数/事务的命令</param>
        /// <returns></returns>
        public int ExecuteNonQuery(SQLiteCommand cmd)
        {
            int affectRows = 0;

            try
            {
                affectRows = cmd.ExecuteNonQuery();

                if (_connectionCurrent != _connectionDisk)
                {
                    SQLiteCommand newCmd;
                    if (_sqlCmdPool.TryDequeue(out newCmd) == false)
                    {
                        newCmd = (SQLiteCommand)cmd.Clone();
                    }
                    newCmd.CommandText = cmd.CommandText;
                    foreach (SQLiteParameter param in cmd.Parameters)
                    {
                        newCmd.Parameters.Add(param.Clone());
                    }

                    _currWriteQueue.Enqueue(newCmd);

                    //_currWriteQueue.Enqueue((SQLiteCommand)cmd.Clone());
                }
            }
            catch (Exception) { throw; }

            return(affectRows);
        }
Exemple #5
0
 public void Dispose()
 {
     _context.Dispose();
     _command.Clone();
     _command.Dispose();
     _connection.Close();
     _connection.Dispose();
 }
Exemple #6
0
        /// <summary>
        /// 执行SQL命令 - 增、删、改
        /// </summary>
        /// <param name="sqlText">SQL命令字符串</param>
        /// <param name="parameters">其他参数</param>
        /// <returns>影响的行数</returns>
        public int ExecuteNonQuery(string sqlText, params SQLiteParameter[] parameters)
        {
            int affectRows = 0;

            try
            {
                SQLiteConnection con = OpenConnection();
                SQLiteCommand    cmd = new SQLiteCommand(sqlText, con);

                if (parameters != null && parameters.Length > 0)
                {
                    cmd.Parameters.AddRange(parameters);
                }

                affectRows = cmd.ExecuteNonQuery();

                if (_connectionCurrent != _connectionDisk)
                {
                    SQLiteCommand newCmd;
                    if (_sqlCmdPool.TryDequeue(out newCmd) == false)
                    {
                        newCmd = (SQLiteCommand)cmd.Clone();
                    }
                    newCmd.CommandText = cmd.CommandText;
                    foreach (SQLiteParameter param in cmd.Parameters)
                    {
                        newCmd.Parameters.Add(param.Clone());
                    }

                    _currWriteQueue.Enqueue(newCmd);
                }
            }
            catch (Exception) { throw; }

            return(affectRows);
        }
Exemple #7
0
        public void InsertRows <TYPE>(string table_name, List <TYPE> rows, StringCollection included_columns)
        {
            // Pre-create most of the insert command
            SQLiteCommand command = CreateCommand("INSERT INTO '" + table_name + "'");

            List <string>    columns = new List <string>();
            List <FieldInfo> fields  = GetSpecifiedFields(typeof(TYPE), null);

            // Specify all columns
            foreach (FieldInfo field in fields)
            {
                if (GetSerialiseTableAttr(field) == null)
                {
                    string column_name = ConstructColumnName(field.Name);
                    columns.Add(column_name);
                }
            }

            command.CommandText += " (" + ConstructCommaSeparatedList(columns) + ")";
            command.CommandText += " VALUES(";

            SQLiteTransaction transaction = m_Connection.BeginTransaction();

            List <string> values = new List <string>();

            foreach (TYPE row in rows)
            {
                // Complete a copy of the pre-created command
                SQLiteCommand row_command = command.Clone() as SQLiteCommand;

                foreach (FieldInfo field in fields)
                {
                    RflDB.SerialiseTable serialise_table_attr = GetSerialiseTableAttr(field);
                    if (serialise_table_attr != null)
                    {
                        string sub_table_name = serialise_table_attr.ExecuteGetTableName(row);
                        CreateTable(sub_table_name, field.FieldType);
                    }
                    else
                    {
                        // Use integers for enumerations
                        object value = field.GetValue(row);
                        if (field.FieldType.IsEnum)
                        {
                            value = (int)value;
                        }

                        // Specify the value as a string
                        string str_value = m_SQLTypes[field.FieldType].ValueToString(value);
                        values.Add(str_value);
                    }
                }

                row_command.CommandText += ConstructCommaSeparatedList(values) + ")";
                values.Clear();

                // Defer query exection until the transaction commit
                row_command.Transaction = transaction;
                row_command.ExecuteNonQuery();
            }

            Console.WriteLine("Comitting.");
            transaction.Commit();
        }