/
AccountSqlDao.sql.stg
180 lines (166 loc) · 4.14 KB
/
AccountSqlDao.sql.stg
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
171
172
173
174
175
176
177
178
179
180
group AccountSqlDao: EntitySqlDao;
tableName() ::= "accounts"
historyTableName() ::= "account_history"
tableFields(prefix) ::= <<
<prefix>external_key
, <prefix>email
, <prefix>name
, <prefix>first_name_length
, <prefix>currency
, <prefix>billing_cycle_day_local
, <prefix>parent_account_id
, <prefix>is_payment_delegated_to_parent
, <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>notes
, <prefix>migrated
, <prefix>is_notified_for_invoices
, <prefix>created_by
, <prefix>created_date
, <prefix>updated_by
, <prefix>updated_date
>>
tableValues() ::= <<
:externalKey
, :email
, :name
, :firstNameLength
, :currency
, :billingCycleDayLocal
, :parentAccountId
, :isPaymentDelegatedToParent
, :paymentMethodId
, :timeZone
, :locale
, :address1
, :address2
, :companyName
, :city
, :stateOrProvince
, :country
, :postalCode
, :phone
, :notes
, :migrated
, :isNotifiedForInvoices
, :createdBy
, :createdDate
, :updatedBy
, :updatedDate
>>
/** The accounts table doesn't have an account_record_id column (it's the record_id) **/
accountRecordIdFieldWithComma(prefix) ::= ""
accountRecordIdValueWithComma(prefix) ::= ""
update() ::= <<
UPDATE accounts
SET email = :email, name = :name, first_name_length = :firstNameLength,
currency = :currency, billing_cycle_day_local = :billingCycleDayLocal,
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, notes = :notes,
is_notified_for_invoices = :isNotifiedForInvoices, updated_date = :updatedDate, updated_by = :updatedBy
WHERE id = :id <AND_CHECK_TENANT()>;
>>
updatePaymentMethod() ::= <<
UPDATE accounts
SET payment_method_id = :paymentMethodId
, updated_date = :updatedDate
, updated_by = :updatedBy
WHERE id = :id <AND_CHECK_TENANT()>;
>>
getAccountByKey() ::= <<
select <allTableFields()>
from accounts
where external_key = :externalKey <AND_CHECK_TENANT()>;
>>
getBCD() ::= <<
select billing_cycle_day_local
from accounts
where id = :id <AND_CHECK_TENANT()>;
>>
searchQuery(prefix) ::= <<
<idField(prefix)> = :searchKey
or <prefix>name like :likeSearchKey
or <prefix>email like :likeSearchKey
or <prefix>external_key like :likeSearchKey
or <prefix>company_name like :likeSearchKey
>>
/** Unfortunately, we need to force MySQL to use the individual indexes */
luckySearch() ::= <<
select
<allTableFields("t.")>
from <tableName()> t
join (
select distinct <recordIdField()>
from (
(
select <recordIdField()>
from <tableName()>
where <idField()> = :searchKey
<andCheckSoftDeletionWithComma()>
<AND_CHECK_TENANT()>
limit 1
)
union all
(
select <recordIdField()>
from <tableName()>
where name = :searchKey
<andCheckSoftDeletionWithComma()>
<AND_CHECK_TENANT()>
limit 1
)
union all
(
select <recordIdField()>
from <tableName()>
where email = :searchKey
<andCheckSoftDeletionWithComma()>
<AND_CHECK_TENANT()>
limit 1
)
union all
(
select <recordIdField()>
from <tableName()>
where external_key = :searchKey
<andCheckSoftDeletionWithComma()>
<AND_CHECK_TENANT()>
limit 1
)
union all
(
select <recordIdField()>
from <tableName()>
where company_name = :searchKey
<andCheckSoftDeletionWithComma()>
<AND_CHECK_TENANT()>
limit 1
)
) search_with_idx
) optimization on <recordIdField("optimization.")> = <recordIdField("t.")>
limit 1
;
>>
getIdFromKey() ::= <<
SELECT id
FROM accounts
WHERE external_key = :externalKey <AND_CHECK_TENANT()>;
>>
getAccountsByParentId() ::= <<
select <allTableFields()>
from accounts
where parent_account_id = :parentAccountId
<AND_CHECK_TENANT()>
<defaultOrderBy()>
;
>>