initial: Lucee CRUD — table name from json_env (TABLE_NAME)
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
<cfcomponent displayname="Application" output="true">
|
||||||
|
|
||||||
|
<cfset this.Name = "tfluceecrud" />
|
||||||
|
<cfset this.applicationTimeout = createTimeSpan(0, 0, 3, 0) />
|
||||||
|
<cfset this.sessionmanagement = "Yes" />
|
||||||
|
<cfset this.sessiontimeout = CreateTimeSpan(0, 0, 120, 0) />
|
||||||
|
|
||||||
|
<cfset this.datasource = "testds" />
|
||||||
|
<cfset getDS(this.datasource, this.datasource) />
|
||||||
|
|
||||||
|
<cfsetting requesttimeout="20" showdebugoutput="false" />
|
||||||
|
|
||||||
|
<cffunction name="OnApplicationStart" access="public" returntype="boolean">
|
||||||
|
<cfreturn true />
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
<cffunction name="OnRequestStart" access="public" returntype="boolean">
|
||||||
|
<cfset request.tableName = createObject("java", "java.lang.System").getEnv().get("TABLE_NAME") />
|
||||||
|
<cfif NOT isDefined("request.tableName") OR NOT len(request.tableName)>
|
||||||
|
<cfset request.tableName = "check_point" />
|
||||||
|
</cfif>
|
||||||
|
<cfreturn true />
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
<cffunction name="OnRequest" access="public" returntype="void">
|
||||||
|
<cfargument name="template" type="string" required="true" />
|
||||||
|
|
||||||
|
<cfset setEncoding("FORM", "UTF-8") />
|
||||||
|
<cfset setEncoding("URL", "UTF-8") />
|
||||||
|
|
||||||
|
<cfset local.basePath = getDirectoryFromPath(getCurrentTemplatePath()) />
|
||||||
|
<cfset request.webRoot = "../" />
|
||||||
|
<cfset request.thisPage = Replace(ReplaceNoCase(expandPath(arguments.template), local.basePath, ""), "\", "/") />
|
||||||
|
|
||||||
|
<cfinclude template="#arguments.template#" />
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
<cffunction name="getDS" access="private" returntype="void">
|
||||||
|
<cfargument name="dsname" type="string" required="true" />
|
||||||
|
<cfargument name="prefix" type="string" default="#dsname#" />
|
||||||
|
|
||||||
|
<cfset system = createObject("java", "java.lang.System") />
|
||||||
|
<cfset var ds = {} />
|
||||||
|
|
||||||
|
<cfloop list="class,connectionString,username,password,bundleName,bundleVersion,connectionLimit,liveTimeout,validate" item="field">
|
||||||
|
<cfset var value = system.getEnv("#arguments.prefix#_#field#") />
|
||||||
|
<cfif isDefined("value") AND len(value)>
|
||||||
|
<cfset structInsert(ds, field, value) />
|
||||||
|
</cfif>
|
||||||
|
</cfloop>
|
||||||
|
|
||||||
|
<cfset THIS.datasources[dsname] = ds />
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
</cfcomponent>
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
<cfset tableName = request.tableName />
|
||||||
|
|
||||||
|
<!--- Обработка действий --->
|
||||||
|
<cfif isDefined("FORM.action")>
|
||||||
|
|
||||||
|
<cfif FORM.action EQ "create">
|
||||||
|
<cfquery name="insertRow">
|
||||||
|
INSERT INTO #tableName# (value)
|
||||||
|
VALUES (<cfqueryparam value="#FORM.value#" cfsqltype="cf_sql_integer" />)
|
||||||
|
</cfquery>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<cfif FORM.action EQ "update">
|
||||||
|
<cfquery name="updateRow">
|
||||||
|
UPDATE #tableName#
|
||||||
|
SET value = <cfqueryparam value="#FORM.value#" cfsqltype="cf_sql_integer" />
|
||||||
|
WHERE id = <cfqueryparam value="#FORM.id#" cfsqltype="cf_sql_integer" />
|
||||||
|
</cfquery>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<cfif FORM.action EQ "delete">
|
||||||
|
<cfquery name="deleteRow">
|
||||||
|
DELETE FROM #tableName#
|
||||||
|
WHERE id = <cfqueryparam value="#FORM.id#" cfsqltype="cf_sql_integer" />
|
||||||
|
</cfquery>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<cflocation url="index.cfm" addtoken="no" />
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<!--- Режим редактирования --->
|
||||||
|
<cfset editRow = {} />
|
||||||
|
<cfif isDefined("URL.edit")>
|
||||||
|
<cfquery name="editQuery">
|
||||||
|
SELECT id, value FROM #tableName#
|
||||||
|
WHERE id = <cfqueryparam value="#URL.edit#" cfsqltype="cf_sql_integer" />
|
||||||
|
</cfquery>
|
||||||
|
<cfif editQuery.recordCount GT 0>
|
||||||
|
<cfset editRow = { id = editQuery.id, value = editQuery.value } />
|
||||||
|
</cfif>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<!--- Все записи --->
|
||||||
|
<cfquery name="rows">
|
||||||
|
SELECT id, value FROM #tableName# ORDER BY id
|
||||||
|
</cfquery>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>CRUD — #tableName#</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: sans-serif; max-width: 600px; margin: 40px auto; }
|
||||||
|
table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
|
||||||
|
th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
|
||||||
|
th { background: #f5f5f5; }
|
||||||
|
form { margin-bottom: 20px; }
|
||||||
|
input, button { padding: 6px 10px; margin: 2px; }
|
||||||
|
.actions a { margin: 0 4px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>#tableName#</h1>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>ID</th>
|
||||||
|
<th>Value</th>
|
||||||
|
<th></th>
|
||||||
|
</tr>
|
||||||
|
<cfoutput query="rows">
|
||||||
|
<tr>
|
||||||
|
<td>#id#</td>
|
||||||
|
<td>#value#</td>
|
||||||
|
<td class="actions">
|
||||||
|
<a href="index.cfm?edit=#id#">✏️</a>
|
||||||
|
<form method="post" style="display:inline">
|
||||||
|
<input type="hidden" name="action" value="delete" />
|
||||||
|
<input type="hidden" name="id" value="#id#" />
|
||||||
|
<button type="submit" onclick="return confirm('Удалить #id#?')">🗑️</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</cfoutput>
|
||||||
|
<cfif rows.recordCount EQ 0>
|
||||||
|
<tr><td colspan="3">Нет записей. Создайте первую.</td></tr>
|
||||||
|
</cfif>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<cfif NOT structIsEmpty(editRow)>
|
||||||
|
<h2>Редактировать #editRow.id#</h2>
|
||||||
|
<form method="post">
|
||||||
|
<input type="hidden" name="action" value="update" />
|
||||||
|
<input type="hidden" name="id" value="#editRow.id#" />
|
||||||
|
<input type="number" name="value" value="#editRow.value#" required />
|
||||||
|
<button type="submit">Сохранить</button>
|
||||||
|
<a href="index.cfm">Отмена</a>
|
||||||
|
</form>
|
||||||
|
<cfelse>
|
||||||
|
<h2>Добавить</h2>
|
||||||
|
<form method="post">
|
||||||
|
<input type="hidden" name="action" value="create" />
|
||||||
|
<input type="number" name="value" placeholder="Значение" required />
|
||||||
|
<button type="submit">Добавить</button>
|
||||||
|
</form>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user