/// <summary> /// Delete an existing instance from database. /// </summary> /// <param name="instance">Instance unique identifier.</param> private int DeleteDatabaseRecord(long id) { int rowsAffected = 0; // Get the SQL sentence if (this.SqlDelete == null) { this.SqlDelete = ORMSqlManager <T> .GetDeleteCommand(); } // Connecto to database this.Connect(); // Set command parameters foreach (ORMProperty param in this.SqlDelete.Parameters) { this.SetParameter(param.FieldName, id); } // Execute the SQL command rowsAffected = this.ExecuteNonQuery(this.SqlDelete.SqlCommand); // Close the connection to database this.Disconnect(); // Delete the instance from in-memory database if (this.InMemoryTable.ContainsKey(id)) { this.InMemoryTable.Remove(id); } return(rowsAffected); }
/// <summary> /// Create a new instance into database. /// </summary> /// <param name="instance">Instance to create.</param> /// <returns>The instance unique identifier.</returns> private long InsertDatabaseRecord(T instance) { long id = 0; // Get the SQL sentence if (this.SqlInsert == null) { this.SqlInsert = ORMSqlManager <T> .GetInsertCommand(typeof(T)); } // Connecto to database this.Connect(); // Set command parameters foreach (ORMProperty param in this.SqlInsert.Parameters) { this.SetParameter(param.FieldName, this.GetPropertyValue(param, instance)); } // Execute the SELECT sentence to retrieve the instance properties this.ExecuteNonQuery(this.SqlInsert.SqlCommand); // Get the generated new record unique identifier id = this.ExecuteScalar("select max(" + this.GetPrimaryKey(instance).FieldName + ") from " + this.GetTableName(instance)); this.SetPrimaryKeyValue(instance, id); // Close the connection to database this.Disconnect(); // Created project is added to in-memory table this.InMemoryTable.Add(id, instance); return(this.GetPrimaryKeyValue(instance)); }
/// <summary> /// Get the specified instance of the type. /// </summary> /// <param name="id">Instance primary key.</param> /// <returns>The specified instance.</returns> private T ReadFromDatabase(long id) { T instance = default(T); // Get the SQL sentence if (this.SqlSelectByPrimaryKey == null) { this.SqlSelectByPrimaryKey = ORMSqlManager <T> .GetSelectCommand(typeof(T)); } // Connecto to database this.Connect(); // Set command parameters this.SetParameter(this.SqlSelectByPrimaryKey.Parameters[0].FieldName, id); // Execute the SELECT sentence to retrieve the instance properties using (SQLiteDataReader reader = this.ExecuteReader(this.SqlSelectByPrimaryKey.SqlCommand)) { if (reader.Read()) { instance = this.MapData(typeof(T), reader); } } // Close the connection to database this.Disconnect(); return(instance); }
/// <summary> /// Update an existing database record. /// </summary> /// <param name="instance">Instance to update.</param> /// <returns>The instance unique identifier.</returns> private long UpdateDatabaseRecord(T instance) { // Get the SQL sentence if (this.SqlUpdate == null) { this.SqlUpdate = ORMSqlManager <T> .GetUpdateCommand(typeof(T)); } // Connecto to database this.Connect(); // Set command parameters foreach (ORMProperty param in this.SqlUpdate.Parameters) { this.SetParameter(param.FieldName, this.GetPropertyValue(param, instance)); } // Execute the SELECT sentence to retrieve the instance properties this.ExecuteNonQuery(this.SqlUpdate.SqlCommand); // Close the connection to database this.Disconnect(); return(this.GetPrimaryKeyValue(instance)); }
/// <summary> /// Get the specified instance of the type. /// </summary> /// <param name="id">Instance primary key.</param> /// <returns>The specified instance.</returns> public static T Get(long id) { T instance = default(T); String tableName = ORMSqlManager <T> .GetTypeTableName(); // Check if the instance is in memory if (PersistentEntity <T> .database == null) { PersistentEntity <T> .database = new Dictionary <long, object>(); } if (PersistentEntity <T> .database.ContainsKey(id)) { return((T)PersistentEntity <T> .database[id]); } // Get the SQL sentence if (PersistentEntity <T> .SqlSelectByPrimaryKey == null) { // PersistentEntity<T>.SqlSelectByPrimaryKey = ORMSqlManager<T>.SelectByPrimaryKey(typeof(T)); } // Connecto to database PersistentEntity <T> .Connect(); //PersistentEntity<T>. //// Execute the SELECT sentence to retrieve the instance properties //using (SQLiteDataReader reader = ExecuteReader(PersistentEntity<T>.SqlSelectByPrimaryKey)) //{ // if (reader.Read()) // { // instance = PersistentEntity<T>.MapData(typeof(T), reader); // } //} // Close the connection to database PersistentEntity <T> .Disconnect(); // Store instance in memory database PersistentEntity <T> .database.Add(id, instance); return(instance); }