59 lines
3.4 KiB
Plaintext
59 lines
3.4 KiB
Plaintext
<cfcomponent displayname="Application" output="true">
|
|
<!--- Базовая конфигурация Lucee и datasource. --->
|
|
<cfset this.Name = "nubes-app-v8" />
|
|
<cfset this.sessionmanagement = "Yes" />
|
|
<cfset this.datasource = "testds" />
|
|
<!--- Инициализируем datasource из переменных окружения. --->
|
|
<cfset getDS(this.datasource) />
|
|
|
|
<!--- Собираем datasource из *_field переменных окружения. --->
|
|
<cffunction name="getDS" access="private" returntype="void">
|
|
<cfargument name="dsname" type="string" required="true"/>
|
|
<cfset var system = createObject("java", "java.lang.System")/>
|
|
<cfset var ds = {} />
|
|
<cfloop list="class,connectionString,database,driver,host,port,type,url,username,password,bundleName,bundleVersion,connectionLimit,liveTimeout,validate" item="field">
|
|
<cfset var envVal = system.getEnv("#arguments.dsname#_#field#") />
|
|
<cfif isDefined("envVal") AND len(envVal)><cfset ds[field] = envVal /></cfif>
|
|
</cfloop>
|
|
<cfset this.datasources[arguments.dsname] = ds />
|
|
</cffunction>
|
|
|
|
<!--- CRUD над таблицей nubes_test_table по POST запросам. --->
|
|
<cffunction name="OnRequest" access="public" returntype="void" output="true">
|
|
<cfargument name="template" type="string" required="true" />
|
|
<cfset request.DS = this.datasource />
|
|
|
|
<!--- Обработка insert/update/delete через form.crud_action. --->
|
|
<cfif CGI.REQUEST_METHOD EQ "POST" AND structKeyExists(form, "crud_action")>
|
|
<cftry>
|
|
<cfswitch expression="#form.crud_action#">
|
|
<cfcase value="insert">
|
|
<cfquery datasource="#request.DS#">
|
|
INSERT INTO nubes_test_table (test_data) VALUES (<cfqueryparam value="#form.txt_content#" cfsqltype="cf_sql_varchar">)
|
|
</cfquery>
|
|
</cfcase>
|
|
<cfcase value="update">
|
|
<cfquery datasource="#request.DS#">
|
|
UPDATE nubes_test_table SET test_data = <cfqueryparam value="#form.txt_content#" cfsqltype="cf_sql_varchar"> WHERE id = <cfqueryparam value="#form.id#" cfsqltype="cf_sql_integer">
|
|
</cfquery>
|
|
</cfcase>
|
|
<cfcase value="delete">
|
|
<cfquery datasource="#request.DS#">
|
|
DELETE FROM nubes_test_table WHERE id = <cfqueryparam value="#form.id#" cfsqltype="cf_sql_integer">
|
|
</cfquery>
|
|
</cfcase>
|
|
</cfswitch>
|
|
<cflocation url="#CGI.SCRIPT_NAME#" addtoken="false">
|
|
<cfcatch><cfset request.db_error = cfcatch.message /></cfcatch>
|
|
</cftry>
|
|
</cfif>
|
|
|
|
<!--- Гарантируем наличие таблицы при первом заходе. --->
|
|
<cftry>
|
|
<cfquery datasource="#request.DS#">CREATE TABLE IF NOT EXISTS nubes_test_table (id SERIAL PRIMARY KEY, test_data TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);</cfquery>
|
|
<cfcatch><cfset request.db_error = cfcatch.message /></cfcatch>
|
|
</cftry>
|
|
<cfinclude template="#arguments.template#" />
|
|
</cffunction>
|
|
</cfcomponent>
|