(***UNTESTED***) Implemented SqlDataObject; implemented Category and Board dataobjects; fixed some bugs in registry and abstractdataobject

main
Inga 🏳‍🌈 14 years ago
parent fc74f4d06d
commit f330e5a73a
  1. 3
      Common/Common.csproj
  2. 8
      Common/Config.cs
  3. 95
      Common/SqlObject.cs
  4. 62
      Common/dataobjects/Board.cs
  5. 30
      Common/dataobjects/Category.cs
  6. 5
      Core/Config.cs
  7. 10
      Core/DataObject.cs
  8. 5
      Core/Registry.cs
  9. 14
      IISMainHandler/handlers/DebugHandler.cs

@ -46,7 +46,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="dataobjects\Board.cs" />
<Compile Include="dataobjects\Category.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SqlObject.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj">

@ -10,8 +10,11 @@ namespace FLocal.Common {
public readonly string InitTime;
public readonly Core.DB.IDBConnection mainConnection;
protected Config(NameValueCollection data) : base(data) {
this.InitTime = DateTime.Now.ToLongTimeString();
this.mainConnection = new MySQLConnector.Connection(data["connectionString"]);
}
public static void Init(NameValueCollection data) {
@ -22,6 +25,11 @@ namespace FLocal.Common {
doReInit(() => new Config(data));
}
public override void Dispose() {
this.mainConnection.Dispose();
base.Dispose();
}
}
}

@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLocal.Core;
using FLocal.Core.DB;
namespace FLocal.Common {
abstract public class SqlObject<T> : Core.DataObject<int, T> where T : SqlObject<T>, new() {
protected SqlObject() : base() {
}
abstract protected ITableSpec table {
get;
}
private bool isLoaded = false;
private object lockFiller = new object();
private object lockInitializer = new object();
abstract protected void doFromHash(Dictionary<string, string> data);
protected void fromHash(Dictionary<string, string> data) {
lock(this.lockFiller) {
if(data[this.table.idName] != this.id.ToString()) {
throw new CriticalException("Id mismatch");
}
this.doFromHash(data);
}
}
private void doLoad() {
this.fromHash(Config.instance.mainConnection.LoadById(this.table, this.id.ToString()));
}
protected void Load() {
lock(this.lockInitializer) {
if(this.isLoaded) throw new CriticalException("already initialized");
this.doLoad();
this.isLoaded = true;
}
}
protected void LoadIfNotLoaded() {
lock(this.lockInitializer) {
if(!this.isLoaded) {
this.doLoad();
this.isLoaded = true;
}
}
}
public void ReLoad() {
this.doLoad();
}
protected override void AfterCreate(bool forLoadingFromHash) {
base.AfterCreate(forLoadingFromHash);
if(!forLoadingFromHash) this.Load();
}
public static List<T> LoadByIds(List<int> ids) {
Dictionary<int, T> rawRes = LoadByIdsForLoadingFromHash(ids);
List<int> idsToQuery = new List<int>();
foreach(int id in ids) {
if(!rawRes[id].isLoaded) {
idsToQuery.Add(id);
}
}
if(idsToQuery.Count > 0) {
ITableSpec table = rawRes[idsToQuery[0]].table;
List<Dictionary<string, string>> rawData = Config.instance.mainConnection.LoadByIds(table, new List<string>(from int id in idsToQuery select id.ToString()));
foreach(Dictionary<string, string> row in rawData) {
int id = int.Parse(row[table.idName]);
if(!rawRes.ContainsKey(id)) throw new CriticalException("wrong id");
rawRes[id].fromHash(row);
}
}
List<T> res = new List<T>();
foreach(int id in ids) {
if(!rawRes[id].isLoaded) throw new CriticalException("not loaded");
res.Add(rawRes[id]);
}
return res;
}
}
}

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FLocal.Common.dataobjects {
public class Board : SqlObject<Board> {
private class TableSpec : FLocal.Core.DB.ITableSpec {
public static readonly TableSpec instance = new TableSpec();
public string name { get { return "boards"; } }
public string idName { get { return "id"; } }
}
protected override FLocal.Core.DB.ITableSpec table { get { return TableSpec.instance; } }
private string _name;
public string name {
get {
this.LoadIfNotLoaded();
return this._name;
}
}
private string _description;
public string description {
get {
this.LoadIfNotLoaded();
return this._description;
}
}
private int _lastPostId;
public int lastPostId {
get {
this.LoadIfNotLoaded();
return this._lastPostId;
}
}
private int _categoryId;
public int categoryId {
get {
this.LoadIfNotLoaded();
return this._categoryId;
}
}
public Category category {
get {
return Category.LoadById(this.categoryId);
}
}
protected override void doFromHash(Dictionary<string, string> data) {
this._name = data["name"];
this._description = data["description"];
this._lastPostId = int.Parse(data["lastPostId"]);
this._categoryId = int.Parse(data["categoryId"]);
}
}
}

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FLocal.Common.dataobjects {
public class Category : SqlObject<Category> {
private class TableSpec : FLocal.Core.DB.ITableSpec {
public static readonly TableSpec instance = new TableSpec();
public string name { get { return "categories"; } }
public string idName { get { return "id"; } }
}
protected override FLocal.Core.DB.ITableSpec table { get { return TableSpec.instance; } }
private string _name;
public string name {
get {
this.LoadIfNotLoaded();
return this._name;
}
}
protected override void doFromHash(Dictionary<string, string> data) {
this._name = data["name"];
}
}
}

@ -6,7 +6,7 @@ using System.Collections.Specialized;
namespace FLocal.Core {
public class Config<T> where T : Config<T> {
public abstract class Config<T> : IDisposable where T : Config<T> {
private static T _instance = null;
@ -49,5 +49,8 @@ namespace FLocal.Core {
}
}
public virtual void Dispose() {
}
}
}

@ -29,7 +29,7 @@ namespace FLocal.Core {
private bool isJustCreated;
internal DataObject() {
protected DataObject() {
Debug.Assert(this is T);
this.isJustCreated = true;
this._id = null;
@ -39,6 +39,14 @@ namespace FLocal.Core {
return registry.Get(id, false);
}
protected static Dictionary<TKey, T> LoadByIdsForLoadingFromHash(List<TKey> ids) {
Dictionary<TKey, T> res = new Dictionary<TKey,T>();
foreach(TKey id in ids) {
res[id] = registry.Get(id, true);
}
return res;
}
protected virtual void AfterCreate(bool forLoadingFromHash) { }
internal override void CreateByIdFromRegistry(TKey id, bool forLoadingFromHash) {

@ -47,10 +47,9 @@ namespace FLocal.Core {
return this.Get(id, forLoadingFromHash);
}
/* public bool IsCached(TKey id) {
if(this.locks.Contains(id)) throw new CriticalException("locked");
public bool IsCached(TKey id) {
return this.storage.ContainsKey(id);
}*/
}
internal void Delete(TKey[] idsToDelete) {
foreach(TKey id in idsToDelete) {

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using FLocal.Common.dataobjects;
namespace FLocal.IISHandler.handlers {
class DebugHandler : ISpecificHandler {
@ -20,6 +21,19 @@ namespace FLocal.IISHandler.handlers {
context.httpresponse.WriteLine("PathInfo: " + context.httprequest.PathInfo);
context.httpresponse.WriteLine("AppInfo: " + context.config.AppInfo);
context.httpresponse.WriteLine("InitTime: " + context.config.InitTime);
if(context.httprequest.Path == "/test/") {
using(Core.DB.Transaction transaction = context.config.mainConnection.beginTransaction(System.Data.IsolationLevel.Snapshot)) {
context.httpresponse.WriteLine(transaction.GetHashCode().ToString());
}
}
if(context.httprequest.Path == "/boards") {
/*Board board = Board.LoadById(1);
context.httpresponse.WriteLine("name: " + board.name);
context.httpresponse.WriteLine("description: " + board.description);
context.httpresponse.WriteLine("categoryname: " + board.category.name);*/
Category category = Category.LoadById(1);
context.httpresponse.WriteLine("categoryname: " + category.name);
}
}
}

Loading…
Cancel
Save