Esempio n. 1
0
 public int ExecuteCommand(Query query)
 {
     if (IsTransactionActive)
     {
         return _currentTransaction.ExecuteCommand(query);
     }
     else
     {
         using (var con = OpenConnection())
         using (var cmd = con.CreateCommand())
         {
             cmd.CommandText = query.Text;
             SetCommandParameters(query, cmd);
             var rowsAffected = cmd.ExecuteNonQuery();
             return rowsAffected;
         }
     }
 }
        protected void SetQueryParameters(Query cmd, object data, string paramPrefix)
        {
            var type = data.GetType();
            foreach (var field in _mapping.FieldMapping)
            {
                var prop = type.GetProperty(field.ObjectProperty);
                if (prop != null)
                {
                    object paramValue;
                    if (prop.PropertyType == typeof(Identifier))
                    {
                        var id = (Identifier)prop.GetValue(data, null);
                        paramValue = id.Value;
                    }
                    else
                    {
                        paramValue = prop.GetValue(data, null);
                    }

                    cmd.Parameters.Add(paramPrefix + field.DbField, paramValue);
                }

            }
        }
 protected void SetQueryParameters(Query cmd, object data)
 {
     SetQueryParameters(cmd, data, "");
 }
 protected void CreateTableIfNeeded(IDatabase db)
 {
     var query = new Query();
     query.Text = string.Format("SELECT name FROM sqlite_master WHERE type='table' AND name='{0}'",
                 _mapping.TableName);
     using (var reader = db.ExecuteReader(query))
     {
         if (!reader.HasRows)
         {
             var createQry = new Query(BuildCreateTable());
             db.ExecuteCommand(createQry);
         }
     }
 }
Esempio n. 5
0
 public object ExecuteScalar(Query query)
 {
     using (var command = _connection.CreateCommand())
     {
         command.Transaction = _transaction;
         command.CommandText = query.Text;
         SQLiteDatabase.SetCommandParameters(query, command);
         return command.ExecuteScalar();
     }
 }
Esempio n. 6
0
            public IQueryReader ExecuteReader(Query query)
            {
                var command = _connection.CreateCommand();
                command.Transaction = _transaction;

                var reader = new SQLiteQueryReader();
                reader.Execute(query, command, false);
                return reader;
            }
Esempio n. 7
0
            public void Execute(Query query, SQLiteCommand command, bool autoClose)
            {
                command.CommandText = query.Text;

                SQLiteDatabase.SetCommandParameters(query, command);

                var behavior = autoClose ? CommandBehavior.CloseConnection : CommandBehavior.Default;
                try
                {
                    _reader = command.ExecuteReader(behavior);
                }
                finally
                {
                    command.Dispose();
                }
            }
Esempio n. 8
0
 private static void SetCommandParameters(Query query, SQLiteCommand command)
 {
     foreach (var param in query.Parameters)
     {
         command.Parameters.AddWithValue("@" + param.Key, param.Value);
     }
 }
Esempio n. 9
0
 public object ExecuteScalar(Query query)
 {
     if (IsTransactionActive)
     {
         return _currentTransaction.ExecuteScalar(query);
     }
     else
     {
         using (var con = OpenConnection())
         using (var cmd = con.CreateCommand())
         {
             cmd.CommandText = query.Text;
             SetCommandParameters(query, cmd);
             return cmd.ExecuteScalar();
         }
     }
 }
Esempio n. 10
0
        public IQueryReader ExecuteReader(Query query)
        {
            if (IsTransactionActive)
            {
                return _currentTransaction.ExecuteReader(query);
            }
            else
            {
                var con = OpenConnection();
                var cmd = con.CreateCommand();

                var reader = new SQLiteQueryReader();
                try
                {
                    reader.Execute(query, cmd, true);
                }
                catch
                {
                    cmd.Dispose();
                    con.Dispose();
                    throw;
                }
                return reader;

            }
        }