0

I have a table with CREATED and MODIFIED columns. I only want to insert the CREATED value once and have it be thereafter immutable. I know how to do this in a tedious way (write a "DoesRecordExist()" method and then alter the query and number of query parameters based on that), but surely there is a slicker way to accomplish this. After all, this has to be a common requirement (a "database pattern" if you will).

My code is this:

public void InsertUserSiteRecord(UserSite us)
{
    using (SQLiteConnection conn = new SQLiteConnection(HHSUtils.GetDBConnection()))
    {
        conn.Open();
        using (SQLiteCommand cmd = new SQLiteCommand(conn))
        {
            cmd.CommandText =
                String.Format(
                    @"INSERT INTO UserSite (SiteNum, SerialNum, UserName, Created, Modified) 
                                VALUES (@SiteNum, @SerialNum, @UserName, @Created, @Modified)");
            cmd.Parameters.Add(new SQLiteParameter("SiteNum", us.SiteNum));
            cmd.Parameters.Add(new SQLiteParameter("SerialNum", us.SerialNum));
            cmd.Parameters.Add(new SQLiteParameter("UserName", us.UserName));
            cmd.Parameters.Add(new SQLiteParameter("Created", us.Created));
            cmd.Parameters.Add(new SQLiteParameter("Modified", us.Modified));

            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }
}

...and I want to avoid having to do something like this:

public void InsertUserSiteRecord(UserSite us)
{
    using (SQLiteConnection conn = new SQLiteConnection(HHSUtils.GetDBConnection()))
    {
        conn.Open();
        using (SQLiteCommand cmd = new SQLiteCommand(conn))
        {
            if (!RecordExists(us.SiteNum, us.SerialNum, us.UserName))
            {
                cmd.CommandText =
                    String.Format(
                        @"INSERT INTO UserSite (SiteNum, SerialNum, UserName, Created, Modified) 
                                    VALUES (@SiteNum, @SerialNum, @UserName, @Created, @Modified)");
            else
            {
                cmd.CommandText =
                    String.Format(
                        @"INSERT INTO UserSite (SiteNum, SerialNum, UserName, Modified) 
                                    VALUES (@SiteNum, @SerialNum, @UserName, @Modified)");
            }
            cmd.Parameters.Add(new SQLiteParameter("SiteNum", us.SiteNum));
            cmd.Parameters.Add(new SQLiteParameter("SerialNum", us.SerialNum));
            cmd.Parameters.Add(new SQLiteParameter("UserName", us.UserName));
            if (!RecordExists(us.SiteNum, us.SerialNum, us.UserName))
            {
                cmd.Parameters.Add(new SQLiteParameter("Created", us.Created));
            }
            cmd.Parameters.Add(new SQLiteParameter("Modified", us.Modified));

            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }
}

private bool RecordExists(String SiteNum, String SerialNum, String UserId)
{
    // query the table to see if those three values exist in any record
}

Is there a SQL[ite] construct that is something like:

cmd.Parameters.AddOnlyIfColumnIsEmpty(new SQLiteParameter("Created", us.Created));

? Or how can this be best tackled?

user4157124
  • 2,809
  • 13
  • 27
  • 42
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

2 Answers2

2

You may be able to accomplish this using a trigger. See the SQLite documentation on triggers here: https://www.sqlite.org/lang_createtrigger.html

Basically, you would create an INSTEAD OF trigger, then set up your query accordingly. From the documentation:

For an example of an INSTEAD OF trigger, consider the following schema:

CREATE TABLE customer(
  cust_id INTEGER PRIMARY KEY,
  cust_name TEXT,
  cust_addr TEXT
);
CREATE VIEW customer_address AS
   SELECT cust_id, cust_addr FROM customer;
CREATE TRIGGER cust_addr_chng
INSTEAD OF UPDATE OF cust_addr ON customer_address
BEGIN
  UPDATE customer SET cust_addr=NEW.cust_addr
   WHERE cust_id=NEW.cust_id;
END;
With the schema above, a statement of the form:

UPDATE customer_address SET cust_addr=$new_address WHERE cust_id=$cust_id;

Causes the customer.cust_addr field to be updated for a specific customer entry that has customer.cust_id equal to the $cust_id parameter. Note how the values assigned to the view are made available as field in the special "NEW" table within the trigger body.

What you would want to do is just set up the trigger to not update that column even if the original query passes in that column to be updated.

smac89
  • 39,374
  • 15
  • 132
  • 179
dub stylee
  • 3,252
  • 5
  • 38
  • 59
2

You need to check for existance of the record; then do an UPDATE if it exists (not changing your Created value), and an INSERT if it doesn't.

Your original code will give a duplicate key error, assuming you have a PK on the table.