//dbReader private static Text createText(IDataReader dbReader) { Text text = new Text(); text.textId = Convert.ToInt32(dbReader["TextId"]); text.textData = Convert.ToString(dbReader["TextData"]); return text; }
//full constructor internal Upload( int uploadId, Text text, File file ) { this.uploadId = uploadId; this.text = text; this.file = file; }
public ReturnedObject insertText(string publicKey, string privateKey, Text text) { ReturnedObject returnedObject = new ReturnedObject(); if (Security.authorizeClient(publicKey, privateKey)) { CtrText _CtrText = new CtrText(); returnedObject.code = _CtrText.insertText(text); } else { returnedObject.code = (int)CODE.CLIENT_NOT_AUTHORIZED; } return returnedObject; }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not public int insertText(Text text) { int returnCode = (int)CODE.ZERO; string sqlQuery = "INSERT INTO Texts OUTPUT INSERTED.TextId VALUES (" + "@TextData " + ")"; using (SqlConnection sqlConnection = new SqlConnection(AccessTranslatoDb.sqlConnectionString)) { try { regenSqlParams(); using (SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection)) { param_textData.Value = text.textData; sqlCommand.Parameters.Add(param_textData); sqlCommand.Connection.Open(); returnCode = (int)sqlCommand.ExecuteScalar(); sqlCommand.Connection.Close(); sqlCommand.Parameters.Clear(); } } catch (InvalidOperationException ioEx) { returnCode = (int)CODE.DBTEXTS_INSERTTEXT_EXCEPTION; Log.Add(ioEx.ToString()); } catch (SqlException sqlEx) { returnCode = (int)CODE.DBTEXTS_INSERTTEXT_EXCEPTION; Log.Add(sqlEx.ToString()); } catch (ArgumentException argEx) { returnCode = (int)CODE.DBTEXTS_INSERTTEXT_EXCEPTION; Log.Add(argEx.ToString()); } catch (Exception ex) { returnCode = (int)CODE.DBTEXTS_INSERTTEXT_EXCEPTION; Log.Add(ex.ToString()); } return returnCode; } }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not internal int insertText(Text text) { int returnCode = (int)CODE.ZERO; int result = (int)CODE.MINUS_ONE; //validate textData if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(text.textData) || !Validate.hasMinLength(text.textData, 1) || !Validate.hasMaxLength(text.textData, 40000) ) {returnCode = (int)CODE.CTRTEXT_INSERTTEXT_INVALID_TEXTDATA; result = (int)CODE.ZERO; } if (returnCode == (int)CODE.ZERO && result != (int)CODE.ZERO)//safe to proceed { text.textData = text.textData; ITexts _DbTexts = new DbTexts(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { returnCode = _DbTexts.insertText(text); trScope.Complete(); } } catch (TransactionAbortedException taEx) { returnCode = (int)CODE.CTRTEXT_INSERTTEXT_EXCEPTION; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { returnCode = (int)CODE.CTRTEXT_INSERTTEXT_EXCEPTION; Log.Add(aEx.ToString()); } catch (Exception ex) { returnCode = (int)CODE.CTRTEXT_INSERTTEXT_EXCEPTION; Log.Add(ex.ToString()); } } else { } return returnCode; }
//returns "MODEL.Text" object if successful //returns "null" if not public Text findTextByTextId(int textId) { string sqlQuery = "SELECT * FROM Texts WHERE " + "TextId = @TextId"; using (SqlConnection sqlConnection = new SqlConnection(AccessTranslatoDb.sqlConnectionString)) { Text text = new Text(); IDataReader dbReader; try { regenSqlParams(); using (SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection)) { param_textId.Value = textId; sqlCommand.Parameters.Add(param_textId); sqlCommand.Connection.Open(); dbReader = sqlCommand.ExecuteReader(); if (dbReader.Read()) { text = createText(dbReader); } else { text = null; } sqlCommand.Connection.Close(); sqlCommand.Parameters.Clear(); } } catch (InvalidOperationException ioEx) { text = null; Log.Add(ioEx.ToString()); } catch (SqlException sqlEx) { text = null; Log.Add(sqlEx.ToString()); } catch (ArgumentException argEx) { text = null; Log.Add(argEx.ToString()); } catch (Exception ex) { text = null; Log.Add(ex.ToString()); } return text; } }