1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| //---------------------------------------------------------------------------
/**
* Save Build and Execute a SQL INSERT OR UPDATE Statment
* @return success or failure
*/
bool TShaiORPersistent::Save()
// throw(EShaiORPersistentError)
{
bool Result = false;
if (IsRecordable())
{
CheckDataBeforeSave(); // throw EShaiORPersistentError;
bool UpdateMode = IsUpdateModeForSave();
// Construction du SQL - Récupérer la liste des champs !
TFieldName FieldName;
#pragma region Try Create TStringList
TStringList *ValuesParamsClause = new TStringList();
try
{
TStringList *FieldsClause = new TStringList();
try
{
TStringList *PropList = new TStringList();
try
{
#pragma end_region
// Génération puis Execution du SQL !
if (GetPropListForSave(PropList, UpdateMode))
{
// Construction de la liste des champs selon INSERT ou UPDATE
#pragma region Build Field Clause
FieldsClause->Capacity = PropList->Count;
if (UpdateMode)
ValuesParamsClause->Capacity = PropList->Count;
for (int i = 0; i < PropList->Count; i++)
{
FieldName = this->FieldNameByPropertyName(PropList->Strings[i]);
if (UpdateMode)
{
FieldsClause->Add(ShaiFormatString("%s = %s",
(FieldName, ParamNameByPropertyName(PropList->Strings[i], true))));
}
else
{
FieldsClause->Add(FieldName);
ValuesParamsClause->Add(ParamNameByPropertyName(PropList->Strings[i], true));
}
}
#pragma end_region
// Fin de la Génération du SQL puis Execution de celui-ci !
#pragma region Execute SQL Statment
TSQLQuery* Query = CreateQuery();
try
{
Query->SQL->BeginUpdate();
Query->SQL->Add(FormatSQLForSave(FieldsClause, ValuesParamsClause, UpdateMode));
bool UseIdentity = ! UpdateMode && ! IsRelation();
if (UseIdentity)
Query->SQL->Add(";SELECT @@Identity;"); // @@Identity => Spécifique à Sybase
Query->SQL->EndUpdate();
// Remplissage des Paramètres (Valeurs du INSERT ou UPDATE) !
for (int i = 0; i < PropList->Count; i++)
Query->ParamByName(ParamNameByPropertyName(PropList->Strings[i]))->Value = GetPropValue(this, PropList->Strings[i], false);
// Execution du SQL !
// Cela peut déclencher une Exception, si elle se produit, je la laisse se propager !
GetUtilShai()->RunParameterizedQuery(Query, UseIdentity);
// Vérifie le bon déroulement de la requête et récupère l'identifiant inséré !
if (UseIdentity)
{
PrimaryID = Query->FieldByName("@@Identity")->AsInteger; // @@Identity => Spécifique à Sybase
Result = PrimaryID;
}
else
{
Result = (Query->RowsAffected == 1);
}
}
__finally
{
delete Query;
}
#pragma end_region
}
#pragma region Finally Free TStringList
}
__finally
{
delete PropList;
}
}
__finally
{
delete FieldsClause;
}
}
__finally
{
delete ValuesParamsClause;
}
#pragma end_region
}
return Result;
}
//---------------------------------------------------------------------------
String TShaiORPersistent::FormatSQLForSave(TStrings *FieldsClause, TStrings *ValuesParamsClause, bool UpdateMode)
{
String Result;
if (UpdateMode)
{
FieldsClause->Delimiter = ',';
FieldsClause->QuoteChar = ' ';
Result = ShaiFormatString("UPDATE %s \nSET %s \nWHERE %s",
(TableName, FieldsClause->DelimitedText, BuildWhereConditionForSaveUpdate()));
}
else
{
ValuesParamsClause->Delimiter = ',';
ValuesParamsClause->QuoteChar = ' ';
Result = ShaiFormatString("INSERT INTO %s (%s) \nVALUES (%s)",
(TableName, FieldsClause->CommaText, ValuesParamsClause->DelimitedText));
}
return Result;
}
//---------------------------------------------------------------------------
TShaiORPersistent::TFieldName TShaiORPersistent::ParamNameByPropertyName(const TPropertyName APropertyName, bool AddColonPrefix/* = false*/)
{
if (AddColonPrefix)
return ":Param_" + APropertyName;
else
return "Param_" + APropertyName;
}
//---------------------------------------------------------------------------
bool TShaiORPersistent::GetPropListForSave(TStrings *PropList, bool UpdateMode)
{
bool Result = false;
if (TShaiRTTIWrapper::GetPersistentProperties(this->ClassType(), PropList))
{
if (IsRelation())
{
// Pour une clé composée, on insère leur valeur la première fois mais pas de mise à jour !
if (UpdateMode)
{
for (int i = 0; i < FMetaData->RelationMemberProperties->Count; i++)
PropList->Delete(PropList->IndexOf(FMetaData->RelationMemberProperties->Strings[i])); // IndexOf Ne peut pas échouer !
}
}
else
PropList->Delete(PropList->IndexOf(TShaiORPersistent::PROP_NAME_PRIMARY_ID)); // IndexOf Ne peut pas échouer !
Result = PropList->Count;
}
return Result;
} |
Partager