Exemplo n.º 1
0
        /// <summary>
        /// Create a new instance into database.
        /// </summary>
        /// <param name="instance">Instance to create.</param>
        /// <returns>The instance unique identifier.</returns>
        private static long InsertDatabaseRecord(T instance)
        {
            object        value;
            ORMSqlCommand cmd = ORMEntity <T> .SqlDialect.GetInsertCommand();

            // Connecto to database
            ORMEntity <T> .Connect();

            // Set command parameters
            foreach (ORMEntityMember param in cmd.Parameters)
            {
                value = param.GetValue(instance);
                ORMEntity <T> .SetParameter(param, value is null?DBNull.Value : value);
            }

            // Execute the SELECT sentence to retrieve the instance properties
            ORMEntity <T> .ExecuteNonQuery(cmd.SqlCommand);

            long id = ExecuteScalar(SqlDialect.GetNewIDCommand().SqlCommand);

            // Set the generated new record unique identifier to the current instance
            ORMEntity <T> .ORMStructure.PrimaryKey.SetValue(instance, id);

            // Close the connection to database
            ORMEntity <T> .Disconnect();

            // Created project is added to in-memory table
            ORMEntity <T> .AddInMemoryTable(id, instance);

            return(((ORMIdentifiableEntity)instance).ID);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Update an existing database record.
        /// </summary>
        /// <param name="instance">Instance to update.</param>
        /// <returns>The instance unique identifier.</returns>
        private static long UpdateDatabaseRecord(T instance)
        {
            object        value;
            ORMSqlCommand cmd = ORMEntity <T> .SqlDialect.GetUpdateCommand();

            // Connecto to database
            ORMEntity <T> .Connect();

            // Set command parameters
            ORMEntity <T> .SetParameter(cmd.PrimaryKeyName, cmd.PrimaryKeyName.GetValue(instance));

            foreach (ORMEntityMember param in cmd.Parameters)
            {
                value = param.GetValue(instance);
                ORMEntity <T> .SetParameter(param, value is null?DBNull.Value : value);
            }

            // Execute the SELECT sentence to retrieve the instance properties
            ORMEntity <T> .ExecuteNonQuery(cmd.SqlCommand);

            // Close the connection to database
            ORMEntity <T> .Disconnect();

            return(((ORMIdentifiableEntity)instance).ID);
        }