29 struct Data_Set :
public std::vector<Self::Data>
34 StringBuffer json_buffer;
35 PrettyWriter<StringBuffer> json_writer(json_buffer);
37 json_writer.StartArray();
38 for (
const auto & item: *
this)
40 json_writer.StartObject();
41 item.as_json(json_writer);
42 json_writer.EndObject();
44 json_writer.EndArray();
46 return json_buffer.GetString();
51 typedef std::vector<Self::Data*>
Cache;
67 std::for_each(cache_.begin(), cache_.end(), std::mem_fn(&
Data::destroy));
79 db->ExecuteUpdate(
"CREATE TABLE TRANSLINK_V1 (TRANSLINKID integer NOT NULL primary key, CHECKINGACCOUNTID integer NOT NULL, LINKTYPE TEXT NOT NULL /* Asset, Stock */, LINKRECORDID integer NOT NULL)");
82 catch(
const wxSQLite3Exception &e)
84 wxLogError(
"TRANSLINK_V1: Exception %s", e.GetMessage().utf8_str());
98 db->ExecuteUpdate(
"CREATE INDEX IF NOT EXISTS IDX_CHECKINGACCOUNT ON TRANSLINK_V1 (CHECKINGACCOUNTID)");
99 db->ExecuteUpdate(
"CREATE INDEX IF NOT EXISTS IDX_LINKRECORD ON TRANSLINK_V1 (LINKTYPE, LINKRECORDID)");
101 catch(
const wxSQLite3Exception &e)
103 wxLogError(
"TRANSLINK_V1: Exception %s", e.GetMessage().utf8_str());
118 static wxString
name() {
return "TRANSLINKID"; }
124 static wxString
name() {
return "CHECKINGACCOUNTID"; }
130 static wxString
name() {
return "LINKTYPE"; }
136 static wxString
name() {
return "LINKRECORDID"; }
197 auto operator < (
const Data& other)
const 199 return this->
id() < other.
id();
202 auto operator < (
const Data* other)
const 204 return this->
id() < other->
id();
211 if(!LINKTYPE.IsSameAs(r->
LINKTYPE))
return false;
216 explicit Data(Self* table =
nullptr )
221 CHECKINGACCOUNTID = -1;
225 explicit Data(wxSQLite3ResultSet& q, Self* table =
nullptr )
229 TRANSLINKID = q.GetInt64(0);
230 CHECKINGACCOUNTID = q.GetInt64(1);
231 LINKTYPE = q.GetString(2);
232 LINKRECORDID = q.GetInt64(3);
239 if (
this == &other)
return *
this;
256 return this->TRANSLINKID == in.
v_;
261 return this->CHECKINGACCOUNTID == in.
v_;
266 return this->LINKTYPE.CmpNoCase(in.
v_) == 0;
271 return this->LINKRECORDID == in.
v_;
277 StringBuffer json_buffer;
278 PrettyWriter<StringBuffer> json_writer(json_buffer);
280 json_writer.StartObject();
281 this->as_json(json_writer);
282 json_writer.EndObject();
284 return json_buffer.GetString();
288 void as_json(PrettyWriter<StringBuffer>& json_writer)
const 290 json_writer.Key(
"TRANSLINKID");
291 json_writer.Int64(this->TRANSLINKID.GetValue());
292 json_writer.Key(
"CHECKINGACCOUNTID");
293 json_writer.Int64(this->CHECKINGACCOUNTID.GetValue());
294 json_writer.Key(
"LINKTYPE");
295 json_writer.String(this->LINKTYPE.utf8_str());
296 json_writer.Key(
"LINKRECORDID");
297 json_writer.Int64(this->LINKRECORDID.GetValue());
303 row(L
"TRANSLINKID") = TRANSLINKID.GetValue();
304 row(L
"CHECKINGACCOUNTID") = CHECKINGACCOUNTID.GetValue();
305 row(L
"LINKTYPE") = LINKTYPE;
306 row(L
"LINKRECORDID") = LINKRECORDID.GetValue();
312 t(L
"TRANSLINKID") = TRANSLINKID.GetValue();
313 t(L
"CHECKINGACCOUNTID") = CHECKINGACCOUNTID.GetValue();
314 t(L
"LINKTYPE") = LINKTYPE;
315 t(L
"LINKRECORDID") = LINKRECORDID.GetValue();
319 bool save(wxSQLite3Database* db,
bool force_insert =
false)
321 if (db && db->IsReadOnly())
return false;
324 wxLogError(
"can not save TRANSLINK_V1");
328 return table_->
save(
this, db, force_insert);
332 bool remove(wxSQLite3Database* db)
336 wxLogError(
"can not remove TRANSLINK_V1");
340 return table_->
remove(
this, db);
357 wxString
name()
const {
return "TRANSLINK_V1"; }
361 query_ =
"SELECT TRANSLINKID, CHECKINGACCOUNTID, LINKTYPE, LINKRECORDID FROM TRANSLINK_V1 ";
368 cache_.push_back(entity);
388 wxString sql = wxEmptyString;
389 if (entity->
id() <= 0 || force_insert)
391 sql =
"INSERT INTO TRANSLINK_V1(CHECKINGACCOUNTID, LINKTYPE, LINKRECORDID, TRANSLINKID) VALUES(?, ?, ?, ?)";
395 sql =
"UPDATE TRANSLINK_V1 SET CHECKINGACCOUNTID = ?, LINKTYPE = ?, LINKRECORDID = ? WHERE TRANSLINKID = ?";
400 wxSQLite3Statement stmt = db->PrepareStatement(sql);
407 stmt.ExecuteUpdate();
410 if (entity->
id() > 0)
412 for(Cache::iterator it = cache_.begin(); it != cache_.end(); ++ it)
415 if (e->
id() == entity->
id())
420 catch(
const wxSQLite3Exception &e)
422 wxLogError(
"TRANSLINK_V1: Exception %s, %s", e.GetMessage().utf8_str(), entity->
to_json());
426 if (entity->
id() <= 0)
428 entity->
id(db->GetLastRowId());
429 index_by_id_.insert(std::make_pair(entity->
id(), entity));
435 bool remove(
const int64 id, wxSQLite3Database* db)
437 if (
id <= 0)
return false;
440 wxString sql =
"DELETE FROM TRANSLINK_V1 WHERE TRANSLINKID = ?";
441 wxSQLite3Statement stmt = db->PrepareStatement(sql);
443 stmt.ExecuteUpdate();
447 for(Cache::iterator it = cache_.begin(); it != cache_.end(); ++ it)
450 if (entity->
id() ==
id)
452 index_by_id_.erase(entity->
id());
463 catch(
const wxSQLite3Exception &e)
465 wxLogError(
"TRANSLINK_V1: Exception %s", e.GetMessage().utf8_str());
475 if (
remove(entity->id(), db))
484 template<
typename... Args>
487 for (
auto& [_, item] : index_by_id_)
489 if (item->id() > 0 &&
match(item, args...))
513 if (
auto it = index_by_id_.find(
id); it != index_by_id_.end())
521 wxString where = wxString::Format(
" WHERE %s = ?",
PRIMARY::name().utf8_str());
524 wxSQLite3Statement stmt = db->PrepareStatement(this->
query() + where);
527 wxSQLite3ResultSet q = stmt.ExecuteQuery();
531 cache_.push_back(entity);
532 index_by_id_.insert(std::make_pair(
id, entity));
536 catch(
const wxSQLite3Exception &e)
538 wxLogError(
"%s: Exception %s", this->
name().utf8_str(), e.GetMessage().utf8_str());
543 entity = this->
fake_;
561 wxString where = wxString::Format(
" WHERE %s = ?",
PRIMARY::name().utf8_str());
564 wxSQLite3Statement stmt = db->PrepareStatement(this->
query() + where);
567 wxSQLite3ResultSet q = stmt.ExecuteQuery();
574 catch(
const wxSQLite3Exception &e)
576 wxLogError(
"%s: Exception %s", this->
name().utf8_str(), e.GetMessage().utf8_str());
581 entity = this->
fake_;
597 wxSQLite3ResultSet q = db->ExecuteQuery(col ==
COLUMN(0) ? this->
query() : this->
query() +
" ORDER BY " +
column_to_name(col) +
" COLLATE NOCASE " + (asc ?
" ASC " :
" DESC "));
602 result.push_back(std::move(entity));
607 catch(
const wxSQLite3Exception &e)
609 wxLogError(
"%s: Exception %s", this->
name().utf8_str(), e.GetMessage().utf8_str());
bool exists(wxSQLite3Database *db) const
Definition: DB_Table.h:65
bool match(const DATA *data, const Arg1 &arg1)
Definition: DB_Table.h:170
Definition: DB_Table_Translink_V1.h:134
DB_Table_TRANSLINK_V1 Self
Definition: DB_Table_Translink_V1.h:25
Copyright: (c) 2013 - 2025 Guan Lisheng (guanlisheng@gmail.com) Copyright: (c) 2017 - 2018 Stefano Gi...
V v_
Definition: DB_Table.h:47
size_t hit_
Definition: DB_Table.h:60
Data & operator=(const Data &other)
Definition: DB_Table_Translink_V1.h:237
static wxString name()
Definition: DB_Table_Translink_V1.h:118
Self * table_
This is a instance pointer to itself in memory.
Definition: DB_Table_Translink_V1.h:180
OP
Definition: DB_Table.h:42
void id(const int64 id)
Definition: DB_Table_Translink_V1.h:192
static wxString name()
Definition: DB_Table_Translink_V1.h:124
bool ensure_index(wxSQLite3Database *db)
Definition: DB_Table_Translink_V1.h:94
Self::Data * create()
Create a new Data record and add to memory table (cache)
Definition: DB_Table_Translink_V1.h:365
bool equals(const Data *r) const
Definition: DB_Table_Translink_V1.h:207
int64 TRANSLINKID
Definition: DB_Table_Translink_V1.h:182
static COLUMN name_to_column(const wxString &name)
Returns the column number from the given column name.
Definition: DB_Table_Translink_V1.h:165
static int64 newId()
Definition: DB_Table.h:75
id
Definition: constants.h:125
bool save(wxSQLite3Database *db, bool force_insert=false)
Save the record instance in memory to the database.
Definition: DB_Table_Translink_V1.h:319
void as_json(PrettyWriter< StringBuffer > &json_writer) const
Definition: DB_Table_Translink_V1.h:288
bool match(const Self::TRANSLINKID &in) const
Definition: DB_Table_Translink_V1.h:254
bool match(const Self::LINKTYPE &in) const
Definition: DB_Table_Translink_V1.h:264
Definition: DB_Table_Translink_V1.h:351
static wxString name()
Definition: DB_Table_Translink_V1.h:136
std::vector< Self::Data * > Cache
A container to hold a list of Data record pointers for the table in memory.
Definition: DB_Table_Translink_V1.h:51
wxString to_json() const
Definition: DB_Table_Translink_V1.h:275
wxString LINKTYPE
Definition: DB_Table_Translink_V1.h:184
DB_Table_TRANSLINK_V1()
Definition: DB_Table_Translink_V1.h:359
~DB_Table_TRANSLINK_V1()
Destructor: clears any data records stored in memory.
Definition: DB_Table_Translink_V1.h:58
const Data_Set all(wxSQLite3Database *db, const COLUMN col=COLUMN(0), const bool asc=true)
Return a list of Data records (Data_Set) derived directly from the database.
Definition: DB_Table_Translink_V1.h:592
Data(Self *table=nullptr)
Definition: DB_Table_Translink_V1.h:216
Definition: DB_Table_Translink_V1.h:128
size_t skip_
Definition: DB_Table.h:60
void ensure_data(wxSQLite3Database *db)
Definition: DB_Table_Translink_V1.h:110
Definition: DB_Table_Translink_V1.h:145
bool match(const C &) const
Definition: DB_Table_Translink_V1.h:249
TRANSLINKID(const int64 &v, OP op=EQUAL)
Definition: DB_Table_Translink_V1.h:119
Definition: DB_Table_Translink_V1.h:23
Definition: DB_Table_Translink_V1.h:144
void destroy_cache()
Removes all records stored in memory (cache) for the table.
Definition: DB_Table_Translink_V1.h:65
void to_template(html_template &t) const
Definition: DB_Table_Translink_V1.h:310
bool remove(const int64 id, wxSQLite3Database *db)
Remove the Data record from the database and the memory table (cache)
Definition: DB_Table_Translink_V1.h:435
Definition: DB_Table_Translink_V1.h:146
static wxString name()
Definition: DB_Table_Translink_V1.h:130
Self::Data * clone(const Data *e)
Create a copy of the Data record and add to memory table (cache)
Definition: DB_Table_Translink_V1.h:373
Data * fake_
Definition: DB_Table_Translink_V1.h:55
A container to hold list of Data records for the table.
Definition: DB_Table_Translink_V1.h:29
Definition: DB_Table.h:42
int64 id() const
Definition: DB_Table_Translink_V1.h:187
int64 LINKRECORDID
Definition: DB_Table_Translink_V1.h:185
Definition: DB_Table.h:45
Cache cache_
Definition: DB_Table_Translink_V1.h:53
virtual wxString query() const
Definition: DB_Table.h:61
Self::Data * get_record(const int64 id, wxSQLite3Database *db)
Search the database for the data record, bypassing the cache.
Definition: DB_Table_Translink_V1.h:552
CHECKINGACCOUNTID(const int64 &v, OP op=EQUAL)
Definition: DB_Table_Translink_V1.h:125
Index_By_Id index_by_id_
Definition: DB_Table_Translink_V1.h:54
size_t miss_
Definition: DB_Table.h:60
bool ensure(wxSQLite3Database *db)
Creates the database table if the table does not exist.
Definition: DB_Table_Translink_V1.h:73
Data is a single record in the database table.
Definition: DB_Table_Translink_V1.h:176
static wxString column_to_name(const COLUMN col)
Returns the column name as a string.
Definition: DB_Table_Translink_V1.h:150
size_t num_columns() const
Definition: DB_Table_Translink_V1.h:354
COLUMN
Definition: DB_Table_Translink_V1.h:141
wxString to_json() const
Return the data records as a json array string.
Definition: DB_Table_Translink_V1.h:32
Definition: DB_Table_Translink_V1.h:116
void destroy()
Definition: DB_Table_Translink_V1.h:343
Definition: DB_Table.h:55
row_t to_row_t() const
Definition: DB_Table_Translink_V1.h:300
TRANSLINKID PRIMARY
Definition: DB_Table_Translink_V1.h:140
Self::Data * get_one(const Args &... args)
Definition: DB_Table_Translink_V1.h:485
LINKTYPE(const wxString &v, OP op=EQUAL)
Definition: DB_Table_Translink_V1.h:131
wxLongLong int64
Definition: customfieldlistdialog.h:26
bool match(const Self::LINKRECORDID &in) const
Definition: DB_Table_Translink_V1.h:269
bool save(Self::Data *entity, wxSQLite3Database *db, bool force_insert=false)
Saves the Data record to the database table.
Definition: DB_Table_Translink_V1.h:386
bool match(const Self::CHECKINGACCOUNTID &in) const
Definition: DB_Table_Translink_V1.h:259
wxString query_
Definition: DB_Table.h:58
Definition: DB_Table_Translink_V1.h:143
LINKRECORDID(const int64 &v, OP op=EQUAL)
Definition: DB_Table_Translink_V1.h:137
wxString name() const
Name of the table.
Definition: DB_Table_Translink_V1.h:357
Definition: DB_Table_Translink_V1.h:122
int64 CHECKINGACCOUNTID
Definition: DB_Table_Translink_V1.h:183
std::map< int64, Self::Data * > Index_By_Id
Definition: DB_Table_Translink_V1.h:52
Data(wxSQLite3ResultSet &q, Self *table=nullptr)
Definition: DB_Table_Translink_V1.h:225