tutorial: Skylark12

git-svn-id: svn://ultimatepp.org/upp/trunk@5343 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-09-10 18:10:25 +00:00
parent 4c24235f90
commit 7797e6786a
9 changed files with 235 additions and 0 deletions

View file

@ -0,0 +1,44 @@
#include "Skylark12.h"
void CreateEditDelete::Create(Http& http)
{
http("ACTION", THISLINK(SubmitCreate))
.RenderResult(dialog);
}
void CreateEditDelete::SubmitCreate(Http& http)
{
SQL * http.Insert(table);
http.Redirect(back);
}
void CreateEditDelete::Edit(Http& http)
{
int id = http.Int(0);
http
(Select(columns).From(table).Where(key == id))
("ID", id)
("ACTION", THISLINK(SubmitEdit), id)
.RenderResult(dialog);
}
void CreateEditDelete::SubmitEdit(Http& http)
{
SQL * http.Update(table).Where(key == http.Int(0));
http.Redirect(back);
}
void CreateEditDelete::Delete(Http& http)
{
SQL * SqlDelete(table).Where(key == atoi(http[0]));
http.Redirect(back);
}
void CreateEditDelete::Use()
{
SKYLARK_METHOD(Create, "create");
SKYLARK_METHOD(SubmitCreate, "create_submit:POST");
SKYLARK_METHOD(Edit, "edit/*");
SKYLARK_METHOD(SubmitEdit, "submit_edit/*:POST");
SKYLARK_METHOD(Delete, "delete/*");
}

View file

@ -0,0 +1,23 @@
#include "Skylark12.h"
void HomePage(Http&);
SKYLARK_USE(CreateEditDelete, Person, "person")
{
Person.back = HomePage;
Person.table = PERSON;
Person.dialog = "Skylark12/dialog";
}
SKYLARK(HomePage, "")
{
http("PERSON", Select(ID, FIRSTNAME, LASTNAME, EMAIL).From(PERSON)
.OrderBy(LASTNAME, FIRSTNAME))
("CREATE", LINK(Person, Create))
.RenderResult("Skylark12/index");
}
SKYLARK(CatchAll, "**")
{
http.Redirect(HomePage);
}

View file

@ -0,0 +1,59 @@
#include "Skylark12.h"
#include <Sql/sch_schema.h>
#include <Sql/sch_source.h>
Skylark12::Skylark12()
{
#ifdef _DEBUG
prefork = 0;
use_caching = false;
#endif
threads = 1; // Sqlite3 does not work well with multiple threads
port = 8001;
}
void OpenSQL(Sqlite3Session& session)
{
if(!session.Open(ConfigFile("db.sqlite3"))) {
SKYLARKLOG("Can't create or open database file");
Exit(1);
}
#ifdef _DEBUG
session.LogErrors();
session.SetTrace();
#endif
SQL = session;
}
void InitModel()
{
#ifdef _DEBUG
Sqlite3Session session;
OpenSQL(session);
SqlSchema sch(SQLITE3);
All_Tables(sch);
SqlPerformScript(sch.Upgrade());
SqlPerformScript(sch.Attributes());
sch.SaveNormal();
#endif
}
void Skylark12::WorkThread()
{
Sqlite3Session session;
OpenSQL(session);
RunThread();
}
// Local server URL: 127.0.0.1:8001
CONSOLE_APP_MAIN
{
#ifdef _DEBUG
StdLogSetup(LOG_FILE|LOG_COUT);
Ini::skylark_log = true;
#endif
InitModel();
Skylark12().Run();
}

View file

@ -0,0 +1,6 @@
TABLE_(PERSON)
INT_ (ID) PRIMARY_KEY AUTO_INCREMENT
STRING_ (FIRSTNAME, 200)
STRING_ (LASTNAME, 200)
STRING_ (EMAIL, 200)
END_TABLE

View file

@ -0,0 +1,41 @@
#ifndef _Skylark12_Skylark12_h
#define _Skylark12_Skylark12_h
#include <Skylark/Skylark.h>
#include <plugin/sqlite3/Sqlite3.h>
using namespace Upp;
#define MODEL <Skylark12/Model.sch>
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
#include <Sql/sch_header.h>
struct CreateEditDelete : SkylarkPack {
HandlerId back;
SqlId table;
SqlId key;
SqlSet columns;
String dialog;
void Create(Http& http);
void SubmitCreate(Http& http);
void Edit(Http& http);
void SubmitEdit(Http& http);
void Delete(Http& http);
typedef CreateEditDelete CLASSNAME;
void Use();
CreateEditDelete() { key = SqlId("ID"); columns = SqlSet(SqlAll()); }
};
class Skylark12 : public SkylarkApp {
public:
virtual void WorkThread();
typedef Skylark12 CLASSNAME;
Skylark12();
};
#endif

View file

@ -0,0 +1,16 @@
uses
Skylark,
plugin/sqlite3;
file
Skylark12.h,
Model.sch,
Ced.cpp,
Handlers.icpp,
index.witz,
dialog.witz,
Main.cpp;
mainconfig
"" = "MT SSE2";

View file

@ -0,0 +1,12 @@
#include Skylark/Base
#define BODY
<FORM action=$ACTION method="post" accept-charset="utf-8" enctype="multipart/form-data">
$post_identity()
<P>
First name: <INPUT type="text" name="firstname" value="$FIRSTNAME"><BR>
Last name: <INPUT type="text" name="lastname" value="$LASTNAME"><BR>
Email: <INPUT type="text" name="email" value="$EMAIL"><BR>
<INPUT type="submit" value="Send" name="OK"/><BR>
</P>
</FORM>

View file

@ -0,0 +1,26 @@
#include Skylark/Base
#define BODY
<table border="1" id="persons">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
$for(i in PERSON)
<tr>
<td>$i.FIRSTNAME</td>
<td>$i.LASTNAME</td>
<td>$i.EMAIL</td>
<td>
<a href=$Person:Edit(i.ID)>Edit</a>
<a href=$Person:Delete(i.ID)>Delete</a>
</td>
</tr>
$/
</table>
<p/>
<a href=$Person:Create>Insert new person</a>
<a href=$CREATE>Insert new person using LINK</a>

8
tutorial/Skylark12/init Normal file
View file

@ -0,0 +1,8 @@
#ifndef _Skylark12_icpp_init_stub
#define _Skylark12_icpp_init_stub
#include "Skylark/init"
#include "plugin/sqlite3/init"
#define BLITZ_INDEX__ F70b142af5a1a021ab945c31f2df8abbb
#include "Handlers.icpp"
#undef BLITZ_INDEX__
#endif