DBConnection.LoadIdByField implemented

main
Inga 🏳‍🌈 15 years ago
parent 6456fb4cea
commit 1f6465d3f4
  1. 2
      Builder/IISMainHandler/build.txt
  2. 32
      Common/dataobjects/Account.cs
  3. 16
      Common/dataobjects/QuickLink.cs
  4. 16
      Common/dataobjects/User.cs
  5. 30
      Core/DB/IDBConnection.cs
  6. 5
      Core/exceptions/NotFoundInDBException.cs

@ -74,22 +74,12 @@ namespace FLocal.Common.dataobjects {
if(!name2id.ContainsKey(name)) {
lock(name2id) {
if(!name2id.ContainsKey(name)) {
List<string> ids = Config.instance.mainConnection.LoadIdsByConditions(
TableSpec.instance,
new ComparisonCondition(
name2id[name] = int.Parse(
Config.instance.mainConnection.LoadIdByField(
TableSpec.instance.getColumnSpec(TableSpec.FIELD_NAME),
ComparisonType.EQUAL,
name
),
Diapasone.unlimited
)
);
if(ids.Count > 1) {
throw new CriticalException("not unique");
} else if(ids.Count == 1) {
name2id[name] = int.Parse(ids[0]);
} else {
throw new NotFoundInDBException();
}
}
}
}
@ -101,22 +91,12 @@ namespace FLocal.Common.dataobjects {
if(!userid2id.ContainsKey(user.id)) {
lock(userid2id) {
if(!userid2id.ContainsKey(user.id)) {
List<string> ids = Config.instance.mainConnection.LoadIdsByConditions(
TableSpec.instance,
new ComparisonCondition(
userid2id[user.id] = int.Parse(
Config.instance.mainConnection.LoadIdByField(
TableSpec.instance.getColumnSpec(TableSpec.FIELD_USERID),
ComparisonType.EQUAL,
user.id.ToString()
),
Diapasone.unlimited
)
);
if(ids.Count > 1) {
throw new CriticalException("not unique");
} else if(ids.Count == 1) {
userid2id[user.id] = int.Parse(ids[0]);
} else {
throw new NotFoundInDBException();
}
}
}
}

@ -45,22 +45,12 @@ namespace FLocal.Common.dataobjects {
if(!name2id.ContainsKey(name)) {
lock(name2id) {
if(!name2id.ContainsKey(name)) {
List<string> ids = Config.instance.mainConnection.LoadIdsByConditions(
TableSpec.instance,
new ComparisonCondition(
name2id[name] = int.Parse(
Config.instance.mainConnection.LoadIdByField(
TableSpec.instance.getColumnSpec(TableSpec.FIELD_NAME),
ComparisonType.EQUAL,
name
),
Diapasone.unlimited
)
);
if(ids.Count > 1) {
throw new CriticalException("not unique");
} else if(ids.Count == 1) {
name2id[name] = int.Parse(ids[0]);
} else {
throw new NotFoundInDBException();
}
}
}
}

@ -131,22 +131,12 @@ namespace FLocal.Common.dataobjects {
if(!username2id.ContainsKey(name)) {
lock(username2id) {
if(!username2id.ContainsKey(name)) {
List<string> ids = Config.instance.mainConnection.LoadIdsByConditions(
TableSpec.instance,
new ComparisonCondition(
username2id[name] = int.Parse(
Config.instance.mainConnection.LoadIdByField(
TableSpec.instance.getColumnSpec(TableSpec.FIELD_NAME),
ComparisonType.EQUAL,
name
),
Diapasone.unlimited
)
);
if(ids.Count > 1) {
throw new CriticalException("not unique");
} else if(ids.Count == 1) {
username2id[name] = int.Parse(ids[0]);
} else {
throw new NotFoundInDBException();
}
}
}
}

@ -2,15 +2,16 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLocal.Core.DB.conditions;
namespace FLocal.Core.DB {
public interface IDBConnection : IDisposable {
List<Dictionary<string, string>> LoadByIds(ITableSpec table, List<string> ids);
List<string> LoadIdsByConditions(ITableSpec table, conditions.AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts, bool allowHugeLists);
List<string> LoadIdsByConditions(ITableSpec table, AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts, bool allowHugeLists);
long GetCountByConditions(ITableSpec table, conditions.AbstractCondition conditions, params JoinSpec[] joins);
long GetCountByConditions(ITableSpec table, AbstractCondition conditions, params JoinSpec[] joins);
Transaction beginTransaction(System.Data.IsolationLevel iso);
@ -20,7 +21,7 @@ namespace FLocal.Core.DB {
List<Dictionary<string, string>> LoadByIds(Transaction transaction, ITableSpec table, List<string> ids);
List<string> LoadIdsByConditions(Transaction transaction, ITableSpec table, conditions.AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts, bool allowHugeLists);
List<string> LoadIdsByConditions(Transaction transaction, ITableSpec table, AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts, bool allowHugeLists);
void update(Transaction transaction, ITableSpec table, string id, Dictionary<string, string> data);
@ -32,11 +33,11 @@ namespace FLocal.Core.DB {
public static class IDBConnectionExtensions {
public static List<string> LoadIdsByConditions(this IDBConnection connection, ITableSpec table, conditions.AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts) {
public static List<string> LoadIdsByConditions(this IDBConnection connection, ITableSpec table, AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts) {
return connection.LoadIdsByConditions(table, conditions, diapasone, joins, sorts, false);
}
public static List<string> LoadIdsByConditions(this IDBConnection connection, ITableSpec table, conditions.AbstractCondition conditions, Diapasone diapasone, params JoinSpec[] joins) {
public static List<string> LoadIdsByConditions(this IDBConnection connection, ITableSpec table, AbstractCondition conditions, Diapasone diapasone, params JoinSpec[] joins) {
return connection.LoadIdsByConditions(table, conditions, diapasone, joins, new SortSpec[] { new SortSpec(table.getIdSpec(), true) });
}
@ -55,6 +56,25 @@ namespace FLocal.Core.DB {
return rows[0];
}
public static string LoadIdByField(this IDBConnection connection, ColumnSpec column, string value) {
List<string> ids = connection.LoadIdsByConditions(
column.table,
new ComparisonCondition(
column,
ComparisonType.EQUAL,
value
),
Diapasone.unlimited
);
if(ids.Count > 1) {
throw new CriticalException("not unique");
} else if(ids.Count == 1) {
return ids[0];
} else {
throw new NotFoundInDBException(column, value);
}
}
}
}

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLocal.Core.DB;
namespace FLocal.Core {
@ -9,7 +10,9 @@ namespace FLocal.Core {
public NotFoundInDBException() : base("Object not found in DB") { }
public NotFoundInDBException(DB.ITableSpec tableSpec, string id) : base("Object " + tableSpec.name + "[" + tableSpec.idName + "=" + id + "] not found in db") {}
public NotFoundInDBException(ColumnSpec columnSpec, string value) : base("Object " + columnSpec.table.name + "[" + columnSpec.name + "=" + value + "] not found in db") {}
public NotFoundInDBException(ITableSpec tableSpec, string id) : this(tableSpec.getIdSpec(), id) {}
}

Loading…
Cancel
Save