本文整理汇总了C#中IDataStoreKey类的典型用法代码示例。如果您正苦于以下问题:C# IDataStoreKey类的具体用法?C# IDataStoreKey怎么用?C# IDataStoreKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IDataStoreKey类属于命名空间,在下文中一共展示了IDataStoreKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Delete
public static void Delete(MySQLDataStoreBase store, MySqlConnection cnn, ModelBase instance, IDataStoreKey key, object[] extra)
{
var record = GeneratorUtils.AsSuitableRecordInstance(instance, true);
using (var cmd = cnn.CreateCommand())
{
var pk = key ?? record.DataStoreKey;
if (pk == null)
throw new MySQLDataAccessException(StringConsts.KEY_UNAVAILABLE_ERROR);
var where = GeneratorUtils.KeyToWhere(pk, cmd.Parameters);
if (!string.IsNullOrEmpty(where))
cmd.CommandText = string.Format("DELETE T1 FROM `{0}` T1 WHERE {1}", record.TableName, where);
else
cmd.CommandText = string.Format("DELETE T1 FROM `{0}` T1", record.TableName);
var affected = 0;
try
{
affected = cmd.ExecuteNonQuery();
GeneratorUtils.LogCommand(store.LogLevel, "rmdelete-ok", cmd, null);
}
catch(Exception error)
{
GeneratorUtils.LogCommand(store.LogLevel, "rmdelete-error", cmd, error);
throw;
}
if (affected == 0)
throw new MySQLDataAccessException(string.Format(StringConsts.NO_ROWS_AFFECTED_ERROR, "Delete"));
}//using command
}
示例2: Load
/// <summary>
/// Auto generates select sql and params. If sqlStatement!=null then params are added to that statement
/// </summary>
public static void Load(MySQLDataStoreBase store, MySqlConnection cnn, string sqlStatement, ModelBase instance, IDataStoreKey key, object[] extra)
{
var autoSql = string.IsNullOrEmpty(sqlStatement);
var record = GeneratorUtils.AsSuitableRecordInstance(instance, autoSql);
var select = new StringBuilder();
if (autoSql)
{
foreach (var fld in record.Fields)
if (fld.StoreFlag == StoreFlag.LoadAndStore || fld.StoreFlag == StoreFlag.OnlyLoad)
select.AppendFormat(" T1.`{0}`,", fld.FieldName);
if (select.Length > 0)
select.Remove(select.Length - 1, 1);// remove ","
else throw new MySQLDataAccessException(StringConsts.LOAD_NO_SELECT_COLUMNS_ERROR);
}
var pk = key ?? record.DataStoreKey;
if (pk == null)
throw new MySQLDataAccessException(StringConsts.KEY_UNAVAILABLE_ERROR);
using (var cmd = cnn.CreateCommand())
{
var where = GeneratorUtils.KeyToWhere(pk, cmd.Parameters);
if (autoSql)
cmd.CommandText = string.Format("SELECT {0} FROM `{1}` T1 WHERE {2}", select, record.TableName, where);
else
cmd.CommandText = string.Format(sqlStatement, where);
MySqlDataReader reader = null;
try
{
reader = cmd.ExecuteReader();
GeneratorUtils.LogCommand(store.LogLevel, "rmload-ok", cmd, null);
}
catch(Exception error)
{
GeneratorUtils.LogCommand(store.LogLevel, "rmload-error", cmd, error);
throw;
}
using (reader)
{
if (reader.Read())
reader.CopyFieldsToRecordFields(record);
else
throw new MySQLDataAccessException(string.Format(StringConsts.LOADING_ENTITY_NOT_FOUND_ERROR, pk));
}//using reader
}//using command
}
示例3: CRUDDelete
public static int CRUDDelete(MySQLDataStoreBase store, MySqlConnection cnn, MySqlTransaction trans, Row row, IDataStoreKey key)
{
try
{
return crudDelete(store, cnn, trans, row, key);
}
catch(Exception error)
{
throw new MySQLDataAccessException(StringConsts.CRUD_STATEMENT_EXECUTION_ERROR.Args("delete", error.ToMessageWithType(), error), error);
}
}
示例4: KeyToWhere
public static string KeyToWhere(IDataStoreKey key, MySqlParameterCollection parameters)
{
string where = null;
if (key is CounterDataStoreKey)
{
where = "T1.COUNTER = ?CTR";
var par = new MySqlParameter();
par.ParameterName = "?CTR";
par.Value = ((CounterDataStoreKey)key).Counter;
parameters.Add(par);
}
else
if (key is GDID)
{
where = "T1.GDID = ?CTR";
var par = new MySqlParameter();
par.ParameterName = "?CTR";
par.Value = key;
parameters.Add(par);
}
else
if (key is NameValueDataStoreKey)
{
var dict = key as NameValueDataStoreKey;
var s = new StringBuilder();
var idx = 0;
foreach (var e in dict)
{
s.AppendFormat(" (T1.`{0}` = ?P{1}) AND", e.Key, idx);
var par = new MySqlParameter();
par.ParameterName = "?P" + idx.ToString();
par.Value = e.Value;
parameters.Add(par);
idx++;
}
if (s.Length > 0) s.Remove(s.Length - 3, 3);//cut "AND"
where = s.ToString();
}
else
throw new MySQLDataAccessException(StringConsts.INVALID_KEY_TYPE_ERROR);
return where;
}
示例5: CRUDUpdate
public static int CRUDUpdate(MySQLDataStoreBase store, MySqlConnection cnn, MySqlTransaction trans, Row row, IDataStoreKey key, FieldFilterFunc filter)
{
try
{
return crudUpdate(store, cnn, trans, row, key, filter);
}
catch(Exception error)
{
throw new MySQLDataAccessException(
StringConsts.CRUD_STATEMENT_EXECUTION_ERROR.Args("update",
error.ToMessageWithType(), error),
error,
KeyViolationKind.Unspecified,
keyViolationName(error)
);
}
}
示例6: Delete
public static void Delete(SqlConnection cnn, ModelBase instance, IDataStoreKey key, object[] extra)
{
var record = asSuitableRecordInstance(instance, true);
using (var cmd = cnn.CreateCommand())
{
var pk = key ?? record.DataStoreKey;
if (pk == null)
throw new MsSQLDataAccessException(StringConsts.KEY_UNAVAILABLE_ERROR);
var where = keyToWhere(pk, cmd.Parameters);
if (!string.IsNullOrEmpty(where))
cmd.CommandText = string.Format("DELETE T1 FROM [{0}] T1 WHERE {1}", record.TableName, where);
else
cmd.CommandText = string.Format("DELETE T1 FROM [{0}] T1", record.TableName);
if (cmd.ExecuteNonQuery() == 0)
throw new MsSQLDataAccessException(string.Format(StringConsts.NO_ROWS_AFFECTED_ERROR, "Delete"));
}//using command
}
示例7: Delete
public int Delete(Row row, IDataStoreKey key = null)
{
CheckOpenStatus("Delete");
return DoDelete(row, key);
}
示例8: DoUpdate
/// <summary>
/// Performs CRUD row update. Override to do custom update
/// </summary>
protected internal virtual int DoUpdate(MySqlConnection cnn, MySqlTransaction transaction, Row row, IDataStoreKey key = null)
{
checkReadOnly(row.Schema, "update");
return CRUDGenerator.CRUDUpdate(this, cnn, transaction, row, key);
}
示例9: Delete
public int Delete(Row row, IDataStoreKey key = null)
{
using (var cnn = GetConnection())
return DoDelete(cnn, null, row);
}
示例10: Save
public static void Save(SqlConnection cnn, ModelBase instance, IDataStoreKey key, object[] extra)
{
var record = asSuitableRecordInstance(instance, true);
if (record.LastPostedChange!=ChangeType.Created && record.LastPostedChange!=ChangeType.Edited)
throw new MsSQLDataAccessException(string.Format(StringConsts.MODEL_INVALID_STATE_ERROR, "Created || Edited", instance.LastPostedChange));
bool insert = instance.LastPostedChange==ChangeType.Created;
var cnames = new StringBuilder();
var values = new StringBuilder();
var vparams = new List<SqlParameter>();
var vpidx = 0;
foreach (var fld in record.Fields)
if (fld.StoreFlag == StoreFlag.LoadAndStore || fld.StoreFlag == StoreFlag.OnlyStore)
if (
insert || fld.Modified
)
{
cnames.AppendFormat(" [{0}],", fld.FieldName);
if (fld.HasValue)
{
var pname = string.Format("@VAL{0}", vpidx);
if (insert)
values.AppendFormat(" {0},", pname);
else
values.AppendFormat(" [{0}] = {1},", fld.FieldName, pname);
var par = new SqlParameter();
par.ParameterName = pname;
par.Value = fld.ValueAsObject;
vparams.Add(par);
vpidx++;
}
else
{
if (insert)
values.Append(" NULL,");
else
values.AppendFormat(" [{0}] = NULL,", fld.FieldName);
}
}
if (cnames.Length > 0)
{
cnames.Remove(cnames.Length - 1, 1);// remove ","
if (values.Length > 0) values.Remove(values.Length - 1, 1);// remove ","
}
else
return;//nothing has been modified
using (var cmd = cnn.CreateCommand())
{
var sql = string.Empty;
if (insert) //INSERT
{
sql =
string.Format("INSERT INTO [{0}] ({1}) VALUES ({2})", record.TableName, cnames, values);
}
else //UPDATE
{
var pk = key ?? record.DataStoreKey;
if (pk == null)
throw new MsSQLDataAccessException(StringConsts.KEY_UNAVAILABLE_ERROR);
var where = keyToWhere(pk, cmd.Parameters);
if (!string.IsNullOrEmpty(where))
sql = string.Format("UPDATE [{0}] T1 SET {1} WHERE {2}", record.TableName, values, where);
else
sql = string.Format("UPDATE [{0}] T1 SET {1}", record.TableName, values);
}
cmd.CommandText = sql;
cmd.Parameters.AddRange(vparams.ToArray());
if (cmd.ExecuteNonQuery()==0)
throw new MsSQLDataAccessException(string.Format(StringConsts.NO_ROWS_AFFECTED_ERROR, instance.LastPostedChange == ChangeType.Created ? "Insert":"Update"));
}//using command
}
示例11: crudDelete
private static int crudDelete(MySQLDataStoreBase store, MySqlConnection cnn, MySqlTransaction trans, Row row, IDataStoreKey key)
{
var target = store.TargetName;
string tableName = getTableName(row.Schema, target);
using (var cmd = cnn.CreateCommand())
{
var pk = key ?? row.GetDataStoreKey(target);
if (pk == null)
throw new MySQLDataAccessException(StringConsts.KEY_UNAVAILABLE_ERROR);
var where = GeneratorUtils.KeyToWhere(pk, cmd.Parameters);
cmd.Transaction = trans;
if (!string.IsNullOrEmpty(where))
cmd.CommandText = string.Format("DELETE T1 FROM `{0}` T1 WHERE {1}",tableName, where);
else
cmd.CommandText = string.Format("DELETE T1 FROM `{0}` T1", tableName);
ConvertParameters(store, cmd.Parameters);
try
{
var affected = cmd.ExecuteNonQuery();
GeneratorUtils.LogCommand(store.LogLevel, "delete-ok", cmd, null);
return affected;
}
catch(Exception error)
{
GeneratorUtils.LogCommand(store.LogLevel, "delete-error", cmd, error);
throw;
}
}//using command
}
示例12: DoUpdate
protected abstract int DoUpdate(Row row, IDataStoreKey key, FieldFilterFunc filter = null);
示例13: DoDelete
protected abstract int DoDelete(Row row, IDataStoreKey key);
示例14: DoUpdate
protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key)
{
var doc = convertRowToBSONDocumentWith_ID(row, "update");
var tname = GetCollectionName(row.Schema);
var collection = db[tname];
var qry = new Connector.Query();
qry.Set( doc[Connector.Protocol._ID] );
var upd = new Connector.UpdateEntry(qry, doc, false, false);
var result = collection.Update( upd );
checkCRUDResult(result, row.Schema.Name, "update");
return result.TotalDocumentsAffected;
}
示例15: DeleteAsync
public virtual Task<int> DeleteAsync(Row row, IDataStoreKey key = null)
{
return TaskUtils.AsCompletedTask( () => this.Delete(row, key) );
}