public void delete( DatabaseToken token, string table, ulong itemId ) { // Are there any binary columns in this table? If so, we need to pay attention // to eliminating the files from the file system too. IEnumerable<string> binaryCols = findBinaryColumns( table ); if( binaryCols.Any() ) { // Find the row we're going to delete. var results = selectRaw( MySqlToken.Crack( token ), table, new Dictionary<string, object>() { { _tableInfo.getIdColumn( table ), itemId } } ); if( !results.Any() ) return; deleteBinaryColumns( table, binaryCols, results ); } // Build a parameterized SQL query. string idName = _tableInfo.getIdColumn( table ); string sql = CultureFree.Format( "delete from {0} where {1}=@{1}", table, idName ); // Add in the PK for the where clause. MySqlCommand cmd = new MySqlCommand( sql, MySqlToken.Crack( token ) ); cmd.Parameters.AddWithValue( "@" + idName, itemId ); // Do the delete. cmd.ExecuteNonQuery(); }
public void delete( DatabaseToken token, string table, IDictionary<string, object> constraints ) { lock( _lock ) { // Look for the matching table. if( !_tables.ContainsKey( table ) ) _tables[table] = new Table(); // Search through the rows to look for ones that match the given key/value pairs. var toDelete = new List<ulong>(); foreach( var i in _tables[table].rows ) { if( match( i.Value, constraints ) ) toDelete.Add( i.Key ); } // And go back and delete them. We do this separately because you can't modify // a collection while you're iterating over it. foreach( var i in toDelete ) { _tables[table].rows.Remove( i ); deletedRow( table, i ); } } }
public void delete( DatabaseToken token, string table, IDictionary<string, object> constraints ) { // Are there any binary columns in this table? If so, we need to pay attention // to eliminating the files from the file system too. IEnumerable<string> binaryCols = findBinaryColumns( table ); if( binaryCols.Any() ) { // Find the row we're going to delete. var results = selectRaw( MySqlToken.Crack( token ), table, constraints ); if( !results.Any() ) return; deleteBinaryColumns( table, binaryCols, results ); } string sql = CultureFree.Format( "delete from {0}", table ); if( constraints.Count() > 0 ) sql += makeWhereClause( constraints ); // Add in the updated values, plus the ID for the where clause. MySqlCommand cmd = new MySqlCommand( sql, MySqlToken.Crack( token ) ); insertQueryParameters( cmd, table, constraints ); // Do the delete. cmd.ExecuteNonQuery(); }
public void delete( DatabaseToken token, string table, ulong itemId ) { lock (_lock) { // Look for the matching table. if (!_tables.ContainsKey(table)) _tables[table] = new Table(); // If the row is in there, delete it. if (_tables[table].rows.ContainsKey(itemId)) { _tables[table].rows.Remove(itemId); deletedRow( table, itemId ); } } }
public ulong insert( DatabaseToken token, string table, IDictionary<string, object> values ) { ulong id; lock (_lock) { // Look for the matching table. if (!_tables.ContainsKey(table)) _tables[table] = new Table(); // Add a new row with the next row ID, and fill its key/value pairs. Item row = new Item(); id = ++_tables[table].highId; _tables[table].rows[id] = row; row.values["id"] = id; foreach (var pair in values) row.values[pair.Key] = pair.Value; insertedRow( table, id, row ); } return id; }
public void update( DatabaseToken token, string table, ulong itemId, IDictionary<string, object> values ) { lock (_lock) { // Look for the matching table. if (!_tables.ContainsKey(table)) _tables[table] = new Table(); // Look for the specified row. If we have it, update the key/value pairs. Item row; if (_tables[table].rows.TryGetValue(itemId, out row)) { foreach (var pair in values) row.values[pair.Key] = pair.Value; updatedRow( table, itemId, row ); } } }
public DatabaseTransaction transaction( DatabaseToken token ) { // For now, we do nothing here. return new BlankTransaction(); }
public IDictionary<ulong, IDictionary<string, object>> select( DatabaseToken token, string table, IDictionary<string, object> constraints ) { var results = new Dictionary<ulong, IDictionary<string, object>>(); lock (_lock) { // Look for the matching table. if (!_tables.ContainsKey(table)) _tables[table] = new Table(); // Search through the rows to look for ones that match the given key/value pairs. foreach (var i in _tables[table].rows) { if (match(i.Value, constraints)) { var newResults = new Dictionary<string, object>(); results[i.Key] = newResults; foreach (var pair in i.Value.values) newResults[pair.Key] = pair.Value; } } } return results; }
// Returns the most recent (and current) checkpoint. ulong getLatestCheckpoint( DatabaseToken token ) { if( _latestCheckpoint == ulong.MaxValue ) { var sorted = getCheckpoints( token ).OrderByDescending( c => c.time ); if( !sorted.Any() ) { // If there isn't an existing one, go ahead and make the first one -- empty database. DBCheckpoint dbcheckpoint = new DBCheckpoint() { name = "initial", time = DateTimeOffset.UtcNow }; _db.insert( token, dbcheckpoint ); _latestCheckpoint = dbcheckpoint.id; } else _latestCheckpoint = sorted.First().id; } return _latestCheckpoint; }
// Get a list of all checkpoints. IEnumerable<WorldCheckpoint> getCheckpoints( DatabaseToken token ) { var results = _db.select( token, new DBCheckpoint(), null ); return (from w in results select new WorldCheckpoint() { id = w.id, name = w.name, time = w.time }); }
// Performs the actual deletion of a mob based on a mob.id rather than mob.object. void deleteMobInternal( DatabaseToken token, ulong mobId ) { _db.delete( token, new DBAttr() { mob = mobId }, new string[] { "mob" } ); _db.delete( token, new DBVerb() { mob = mobId }, new string[] { "mob" } ); _db.delete( token, new DBMob() { id = mobId }, new string[] { "id" } ); }
public static MySqlConnection Crack( DatabaseToken token ) { return ((MySqlToken)token).conn; }
public void update( DatabaseToken token, string table, ulong itemId, IDictionary<string, object> values ) { // Build a parameterized SQL query. string idName = _tableInfo.getIdColumn( table ); string sql = CultureFree.Format( "update {0} set {1} where {2}=@{2}", table, String.Join( ",", (from c in values.Keys select CultureFree.Format( "{0}=@{0}", c )).ToArray() ), idName ); // Add in the updated values, plus the ID for the where clause. MySqlCommand cmd = new MySqlCommand( sql, MySqlToken.Crack( token ) ); insertQueryParameters( cmd, table, values, new string[] { idName } ); cmd.Parameters.AddWithValue( "@" + idName, itemId ); // Do the update. cmd.ExecuteNonQuery(); }
public DatabaseTransaction transaction( DatabaseToken token ) { MySqlTransaction trans = MySqlToken.Crack( token ).BeginTransaction(); return new MySqlDBTransaction( trans ); }
// Peforms a transformation pass on the raw selected data to take binary columns // and other gremlins into account. public IDictionary<ulong, IDictionary<string, object>> select( DatabaseToken token, string table, IDictionary<string, object> constraints ) { var raw = selectRaw( MySqlToken.Crack( token ), table, constraints ); var rv = new Dictionary<ulong, IDictionary<string, object>>(); foreach( var inrow in raw ) { var outrow = new Dictionary<string, object>(); foreach( var incol in inrow.Value ) { object val = incol.Value; // DBNulls become nulls. if( val is System.DBNull ) val = null; // Convert DateTime to DateTimeOffset; assume UTC. if( val is System.DateTime ) val = new DateTimeOffset( (System.DateTime)val, new TimeSpan() ); // Is it a boolean type? We store these in MySQL as integers. Type colType = _tableInfo.getColumnType( table, incol.Key ); if( colType == typeof( bool ) ) val = Convert.ToBoolean( val ); outrow[incol.Key] = val; } rv[inrow.Key] = outrow; } return rv; }
public ulong insert( DatabaseToken token, string table, IDictionary<string, object> values ) { // Build a parameterized SQL query. string sql = CultureFree.Format( "insert into {0} ({1}) values ({2})", table, String.Join( ",", (from c in values.Keys select c).ToArray() ), String.Join( ",", (from c in values.Keys select "@" + c).ToArray() ) ); MySqlCommand cmd = new MySqlCommand( sql, MySqlToken.Crack( token ) ); insertQueryParameters( cmd, table, values ); // Do the insert and return the PK ID. cmd.ExecuteNonQuery(); return (ulong)cmd.LastInsertedId; }