private bool IsReserved(ReservedKey reservation) { for (int i = 0; i < ReservedKeys.Count; i++) { if (reservation == ReservedKeys[i]) { return(true); } } return(false); }
protected ReservedKey ReserveKey(ReservedKey reservation) { while (IsReserved(reservation)) { reservation++; } ReservedKeys.Add(reservation); return(reservation); }
protected void ReleaseKey(ReservedKey key) { for (int i = 0; i < ReservedKeys.Count; i++) { if (ReservedKeys[i] == key) { ReservedKeys[i].Release(); ReservedKeys.Remove(i); break; } } }
/// <summary> /// Saves the current record to the database. /// </summary> public virtual void Save() { string Sql; try { // Ensure the object is ready to be saved ValidateSave(); ReservedKey savekey = null; if (_dataRow.RowState == DataRowState.Detached) { // Build SQL Insert Statement // Set All Auto Keys if (PrimaryKeys.Count > 1) { for (int i = 0; i < PrimaryKeys.Count; i++) { if (_dataRow[PrimaryKeys[i].Name].ToString() == String.Empty) { // Error Primary Key Not Set DataObjectException PrimaryKeyError = new DataObjectException("One or more required fields do not have valid values."); throw PrimaryKeyError; } } } else { // Only a single primary key, check to see if it has already been assigned if (_dataRow[PrimaryKeys[0].Name].ToString() == String.Empty) { // Autonumber savekey = GetNextKey(); Row[PrimaryKeys[0].Name] = savekey.Value; } // Else key has already been assigned } _dataTable.Rows.Add(_dataRow); Sql = "INSERT INTO " + TableName + "("; // Column names for (int i = 0; i < _dataTable.Columns.Count; i++) { Sql += "[" + _dataTable.Columns[i].ColumnName + "], "; } Sql = Sql.Substring(0, Sql.Length - 2) + ") VALUES("; // Column values for (int i = 0; i < _dataTable.Columns.Count; i++) { if (_dataTable.Columns[i].DataType == System.Type.GetType("System.DateTime")) { if (_dataRow[i].ToString() == String.Empty) { Sql += "null"; } else { Sql += "'" + ((DateTime?)_dataRow[i]).Value.ToString("yyyy/MM/dd HH:mm:ss") + "'"; } } else if (_dataTable.Columns[i].DataType == System.Type.GetType("System.String")) { Sql += "'" + _dataRow[i].ToString().Replace("'", "''") + "'"; } else if (_dataTable.Columns[i].DataType == System.Type.GetType("System.Int32")) { if (_dataRow[i].ToString() == String.Empty) { Sql += "null"; } else { Sql += _dataRow[i].ToString(); } } else if ((_dataTable.Columns[i].DataType == System.Type.GetType("System.Single")) || (_dataTable.Columns[i].DataType == System.Type.GetType("System.Double")) || (_dataTable.Columns[i].DataType == System.Type.GetType("System.Decimal")) || (_dataTable.Columns[i].DataType == System.Type.GetType("System.Int64")) || (_dataTable.Columns[i].DataType == System.Type.GetType("System.Int16"))) { if (_dataRow[i].ToString() == String.Empty) { Sql += "null"; } else { Sql += _dataRow[i].ToString(); } } else if (_dataTable.Columns[i].DataType == System.Type.GetType("System.Boolean")) { if ((bool)_dataRow[i] == true) { Sql += "1"; } else { Sql += "0"; } } else { DataObjectException InvalidDataType = new DataObjectException("Unsupported data type. [" + _dataTable.Columns[i].DataType.ToString() + "]"); throw InvalidDataType; } if (i != _dataTable.Columns.Count - 1) { Sql += ", "; } } Sql += ")"; } else { // Build SQL Update Statement Sql = "UPDATE " + TableName + " SET "; for (int i = 0; i < _dataTable.Columns.Count; i++) { if (!IsPrimaryKey(_dataTable.Columns[i].ColumnName)) { Sql += "[" + _dataTable.Columns[i].ColumnName + "]" + " = "; if (_dataTable.Columns[i].DataType == System.Type.GetType("System.DateTime")) { if (_dataRow[i].ToString() == String.Empty) { Sql += "null"; } else { Sql += "'" + ((DateTime)_dataRow[i]).ToString("yyyy/MM/dd HH:mm:ss") + "'"; } } else if ((_dataTable.Columns[i].DataType == System.Type.GetType("System.Int32")) || (_dataTable.Columns[i].DataType == System.Type.GetType("System.Decimal")) || (_dataTable.Columns[i].DataType == System.Type.GetType("System.Double")) || (_dataTable.Columns[i].DataType == System.Type.GetType("System.Int64")) || (_dataTable.Columns[i].DataType == System.Type.GetType("System.Int16"))) { if (_dataRow[i].ToString() == String.Empty) { Sql += "null"; } else { Sql += _dataRow[i].ToString(); } } else if (_dataTable.Columns[i].DataType == System.Type.GetType("System.String")) { Sql += "'" + _dataRow[i].ToString().Replace("'", "''") + "'"; } else if (_dataTable.Columns[i].DataType == System.Type.GetType("System.Boolean")) { if ((bool)_dataRow[i] == true) { Sql += "1"; } else { Sql += "0"; } } else { Sql += _dataRow[i].ToString(); } if (i != _dataTable.Columns.Count - 1) { Sql += ", "; } } } Sql += " WHERE"; string GetID = "0"; for (int i = 0; i < PrimaryKeys.Count; i++) { if (i > 0) { Sql += " AND"; } if (_dataRow[PrimaryKeys[i].Name] is int) { GetID = ((int)_dataRow[PrimaryKeys[i].Name]).ToString(); } else if (_dataRow[PrimaryKeys[i].Name] is string) { GetID = (string)_dataRow[PrimaryKeys[i].Name]; } else { DataObjectException InvalidDataType = new DataObjectException("Unsupported data type. [" + _dataTable.Columns[i].DataType.ToString() + "]"); throw InvalidDataType; } Sql += " " + PrimaryKeys[i].Name + " = " + GetID; } } DataCommand.CommandText = Sql; DataCommand.Connection = ConnHandler.RequestConnection(Alias, this); while (true) // Loop until Break for success { try { DataCommand.ExecuteNonQuery(); break; // Save Successful } catch (OleDbException err) { if (err.ErrorCode == -2147217873) // Primary Key violation { // Primary Key that was used already existed savekey++; DataCommand.CommandText = DataCommand.CommandText.Replace("VALUES (" + (savekey.Value - 1) + ",", "VALUES (" + savekey.Value.ToString() + ","); } else { throw err; } } } if (Sql.StartsWith("INSERT INTO")) { ReleaseKey(savekey); } } catch (Exception err) { throw err; } finally { ConnHandler.ReleaseConnection(this, DataCommand.Connection); } }
public void Add(ReservedKey keyObject) { List.Add(keyObject); }