46 #ifndef __WTX_DBO_BASEITEMTPL_H_234e0174_6b7d_11e8_b503_fb1ef3651ec0__ 47 #define __WTX_DBO_BASEITEMTPL_H_234e0174_6b7d_11e8_b503_fb1ef3651ec0__ 49 #include <Wt/Dbo/Dbo.h> 50 #include <Wt/WFormModel.h> 51 #include <Wt/Dbo/WtSqlTraits.h> 53 #include "BaseTableDef.h" 54 #include "FieldNumber.h" 55 #include "FieldTextLine.h" 58 #include "FieldDate.h" 60 #include <Wt/Dbo/ptr.h> 66 :
public Wt::Dbo::ptr<C>
70 int lastVersion()
const 75 void setLastVersion(
int value )
77 m_lastVersion = value;
80 int m_lastVersion = -1;
85 :
public Wt::Dbo::Dbo<C>
89 using Ptr = Wt::Dbo::ptr< C >;
90 using Collection = Wt::Dbo::collection< Ptr >;
91 using Vector = std::vector< Ptr >;
98 BaseItemTpl(
const std::string & key,
const std::string & cfy =
"" )
102 keyField().setValue( key );
103 cfyField().setValue( cfy );
106 #ifdef NEVER_BUGBUG_NEEDS_COPY_OPERATOR 111 keyField.setValue( other.keyField.value() );
112 cfyField.setValue( other.cfyField.value() );
120 static void MapClass( Wt::Dbo::Session & session )
122 Wt::Dbo::Transaction t(session);
123 session.mapClass<C>( C::TableDef().tableName() );
124 Wt::registerType<C>();
125 Wt::registerType< Wt::Dbo::ptr<C> >();
126 Wt::registerType< Wt::Dbo::ptr<C> >();
144 Wt::Dbo::Transaction t(session);
145 for(
auto fieldDef :
TableDef().fieldDefs() )
147 if( fieldDef-> isUnique() )
150 #ifndef WTX_USING_POSTGRES 151 #ifndef WTX_USING_SQLITE 152 #ifndef WTX_USING_MSSQL 153 #error you must define either WTX_USING_POSTGRES or WTX_USING_SQLITE or WTX_USING_MSSQL 158 #ifdef WTX_USING_POSTGRES 160 Wt::WString(
"ALTER TABLE \"{1}\" ADD UNIQUE (\"{2}\")")
162 .arg( fieldDef-> fieldName() )
167 #ifdef WTX_USING_MSSQL 169 Wt::WString(
"ALTER TABLE \"{1}\" ADD UNIQUE (\"{2}\")")
171 .arg( fieldDef-> fieldName() )
176 #ifdef WTX_USING_SQLITE 183 "CREATE UNIQUE INDEX \"{1}_{2}\" ON \"{1}\" (\"{2}\")" 186 .arg( fieldDef-> fieldName() )
190 session.execute( sql );
199 return C::TableDef();
204 return other.keyField().value() == keyField().value();
207 bool operator< ( const BaseItemTpl<C> & other )
const 209 return other.keyField().value() < keyField().value();
212 bool isInitialized()
const 214 return m_initialized;
217 int lastVersion()
const 219 return m_lastVersion;
222 void setLastVersion(
int value )
const 224 m_lastVersion = value;
251 return m_versionSync;
289 std::string varField(
const std::string & fieldName )
const 291 return varField().value( fieldName );
296 return m_versionSync;
344 #ifdef THIS_DOESNT_WORK_PROPERLY_USE_THE_STATIC_VERSIONS_OF_THIS_PROGRAM 345 bool setId(
int newId )
347 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
349 if( !(this-> session()) )
352 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
354 int thisId = this->
self().
id();
355 auto s = this-> session();
356 auto tn = this-> session()->
template tableName<C>();
358 Wt::Dbo::Transaction t( *s );
360 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
362 this-> session()-> flush();
363 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
365 this->
self().purge();
367 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << thisId <<
" " << newId << std::endl;
369 if( thisId != newId )
371 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
373 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
376 Wt::WString(
"UPDATE \"{1}\" SET id = ? WHERE id = ?")
381 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
391 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << std::endl;
410 for(
auto fieldDef : C::TableDef().fieldDefs() )
412 if( f == fieldDef-> fieldName() )
418 std::cout << __FILE__ <<
":" << __LINE__ <<
" unable to create widget: '" << f <<
"'" << std::endl;
420 return std::make_unique<Wt::WLineEdit>();
423 template<
class Action>
void persist( Action & a )
425 m_xid .persist ( a, &
TableDef() .XidFieldDef );
426 m_versionSync .persist ( a, &
TableDef() .VersionSyncFieldDef );
427 m_isActive .persist ( a, &
TableDef() .IsActiveFieldDef );
428 m_isVisible .persist ( a, &
TableDef() .IsVisibleFieldDef );
429 m_isDeleted .persist ( a, &
TableDef() .IsDeletedFieldDef );
430 m_keyField .persist ( a, &
TableDef() .KeyFieldDef );
431 m_cfyField .persist ( a, &
TableDef() .CfyFieldDef );
432 m_varField .persist ( a, &
TableDef() .VarFieldDef );
433 m_tag .persist ( a, &
TableDef() .TagFieldDef );
440 versionSync() .setValue( -1 );
441 isActive() .setValue(
true );
442 isVisible() .setValue(
true );
443 isDeleted() .setValue(
false );
444 m_initialized =
true;
450 bool m_initialized =
false;
462 mutable int m_lastVersion = -1;
467 std::ostream & operator<< ( std::ostream & s, const BaseItemTpl<C> & o )
469 return s << o.keyField().value();
virtual void postRead()
Post-read.
static void PostCreateTable(Wt::Dbo::Session &session)
Post Table Create.
virtual std::unique_ptr< Wt::WWidget > createEditWidget(Wt::WFormModel::Field f) const
Create an Edit Widget.
virtual void preWrite()
Pre-write.