trunk/src/LythumOSL.Core/Data/Database.cs
/// <summary>
/// Crafts save query, depends on RowState it will generate Update or Insert SQL query
/// </summary>
/// <param name="info">Database metadata information</param>
/// <param name="r">DataRow with data</param>
/// <param name="fieldInfo">Fields which need to update</param>
/// <param name="tableName">DB table name</param>
/// <param name="primaryKeyName">Primary key's name</param>
/// <param name="cleanup">
/// Cleanup will remove from query fields:
/// NowOnUpdate - while will be done insert
/// NowOnInsert - while will be done update
/// </param>
/// <returns>sql query</returns>
Crafts save query, depends on RowState it will generate Update or Insert SQL query
Crafts save query depends on RowState it will generate Update or Insert SQL query
public static string CraftSaveQuery (
			DatabasesInfo info, 
			DataRow r, 
			DbFieldInfo[] fieldInfo, 
			string tableName, 
			string primaryKeyName,
			bool cleanup)
		{
			string sql = string.Empty;
			Validation.RequireValid (r, "r");
			System.Diagnostics.Debug.Print (r.RowState.ToString ());
			switch (r.RowState)
			{
				case DataRowState.Detached:
				case DataRowState.Added:
				case DataRowState.Modified:
					bool newItem = (r[primaryKeyName] == null || 
						DBNull.Value.Equals( r[primaryKeyName]) || 
						r[primaryKeyName].ToString().Equals("0") || 
						r.RowState == DataRowState.Added || r.RowState == DataRowState.Detached 
					);
					if(newItem)
					{
						sql = CraftInsertQuery (info, r, fieldInfo, tableName);
					}
					else
					{
						sql = CraftUpdateQuery (info, r, fieldInfo, tableName, primaryKeyName);
					}
					break;
				default:
					break;
			}
			return sql;
		}
public static string CraftSaveQuery ( DatabasesInfo info , DataRow r , DbFieldInfo [ ] fieldInfo , string tableName , string primaryKeyName , bool cleanup ) { string sql = string . Empty ; Validation . RequireValid ( r , " " ) ; System . Diagnostics . Debug . Print ( r . RowState . ToString ( ) ) ; switch ( r . RowState ) { case DataRowState . Detached : case DataRowState . Added : case DataRowState . Modified : bool newItem = ( r [ primaryKeyName ] == null || DBNull . Value . Equals ( r [ primaryKeyName ] ) || r [ primaryKeyName ] . ToString ( ) . Equals ( " " ) || r . RowState == DataRowState . Added || r . RowState == DataRowState . Detached ) ; if ( newItem ) { sql = CraftInsertQuery ( info , r , fieldInfo , tableName ) ; } else { sql = CraftUpdateQuery ( info , r , fieldInfo , tableName , primaryKeyName ) ; } break ; default : break ; } return sql ; }
Crafts save query, depends on RowState it will generate Update or Insert SQL query
Crafts save query depends on RowState it will generate Update or Insert SQL query
// PK is NULL // PK is DBNull // PK is 0 // row states is // don't process it
None
info
DatabasesInfo
Database metadata information
Database metadata information
r
DataRow
DataRow with data
DataRow with data
fieldInfo
DbFieldInfo[]
Fields which need to update
Fields which need to update
tableName
string
DB table name
DB table name
primaryKeyName
string
Primary key's name
Primary key ' s name
cleanup
bool
Cleanup will remove from query fields:
NowOnUpdate - while will be done insert
NowOnInsert - while will be done update
Cleanup will remove from query fields : NowOnUpdate - while will be done insert NowOnInsert - while will be done update