group AccountDaoSql;

accountFields(prefix) ::= <<
    <prefix>id,
    <prefix>external_key,
    <prefix>email,
    <prefix>name,
    <prefix>first_name_length,
    <prefix>currency,
    <prefix>billing_cycle_day,
    <prefix>payment_method_id,
    <prefix>time_zone, 
    <prefix>locale,
    <prefix>address1, 
    <prefix>address2, 
    <prefix>company_name, 
    <prefix>city, 
	<prefix>state_or_province, 
    <prefix>country, 
    <prefix>postal_code,
    <prefix>phone,
    <prefix>migrated,
    <prefix>is_notified_for_invoices,
    <prefix>created_by,
    <prefix>created_date,
    <prefix>updated_by,
    <prefix>updated_date
>>

create() ::= <<
    INSERT INTO accounts
      (<accountFields()>)
    VALUES
      (:id, :externalKey, :email, :name, :firstNameLength, :currency, :billingCycleDay,
      :paymentMethodId, :timeZone, :locale,
      :address1, :address2, :companyName, :city, :stateOrProvince, :country, :postalCode, :phone,
      :migrated, :isNotifiedForInvoices, :userName, :createdDate, :userName, :updatedDate);
>>

update() ::= <<
    UPDATE accounts
    SET email = :email, name = :name, first_name_length = :firstNameLength,
        currency = :currency, billing_cycle_day = :billingCycleDay, payment_method_id = :paymentMethodId,
        time_zone = :timeZone, locale = :locale,
        address1 = :address1, address2 = :address2, company_name = :companyName, city = :city, state_or_province = :stateOrProvince,
        country = :country, postal_code = :postalCode, phone = :phone,
        is_notified_for_invoices = :isNotifiedForInvoices, updated_date = :updatedDate, updated_by = :userName
    WHERE id = :id;
>>

historyFields() ::= <<
    record_id,
    id,
    external_key,
    email,
    name,
    first_name_length,
    currency,
    billing_cycle_day,
    payment_method_id,
    time_zone,
    locale,
    address1,
    address2,
    company_name,
    city,
    state_or_province,
    country,
    postal_code,
    phone,
    migrated,
    is_notified_for_invoices,
    change_type,
    updated_by,
    date
>>

getRecordId() ::= <<
    SELECT record_id
    FROM accounts
    WHERE id = :id;
>>

getHistoryRecordId() ::= <<
    SELECT MAX(history_record_id)
    FROM account_history
    WHERE record_id = :recordId;
>>

insertHistoryFromTransaction() ::= <<
    INSERT INTO account_history(<historyFields()>)
    VALUES
    (:recordId, :id, :externalKey, :email, :name, :firstNameLength, :currency,
     :billingCycleDay, :paymentMethodId, :timeZone, :locale,
     :address1, :address2, :companyName, :city, :stateOrProvince,
     :country, :postalCode, :phone, :migrated, :isNotifiedForInvoices, :changeType, :userName, :createdDate);
>>

getAccountByKey() ::= <<
    select <accountFields()>
    from accounts
    where external_key = :externalKey;
>>

getById() ::= <<
    SELECT <accountFields()>
    FROM accounts
    WHERE id = :id;
>>

get() ::= <<
    SELECT <accountFields()>
    FROM accounts;
>>

getIdFromKey() ::= <<
    SELECT id
    FROM accounts
    WHERE external_key = :externalKey;
>>

auditFields(prefix) ::= <<
    <prefix>table_name,
    <prefix>record_id,
    <prefix>change_type,
    <prefix>change_date,
    <prefix>changed_by,
    <prefix>reason_code,
    <prefix>comments,
    <prefix>user_token
>>

insertAuditFromTransaction() ::= <<
    INSERT INTO audit_log(<auditFields()>)
    VALUES(:tableName, :recordId, :changeType, :createdDate, :userName, :reasonCode, :comment, :userToken);
>>

test() ::= <<
    SELECT 1 FROM accounts;
>>
;
