mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-05 09:48:44 +00:00
Queries for insert and get prompt
This commit is contained in:
1 parent
ef2f827fed
commit
14fbb2e29c
6 files changed
+303
No files matched your search
+12
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"db_name": "MySQL",
|
||||
"query": "\nINSERT INTO prompts\nSET\n token = ?,\n prompt_type = ?,\n\n maybe_creator_user_token = ?,\n\n maybe_positive_prompt = ?,\n maybe_negative_prompt = ?,\n maybe_other_args = ?,\n\n creator_ip_address = ?\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Right": 7
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "13ca06bcbb4a576f8a2229a8554b7488d379baa381d8d5cb60972750e81b2173"
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
{
|
||||
"db_name": "MySQL",
|
||||
"query": "\nSELECT\n p.token as `token: tokens::tokens::prompts::PromptToken`,\n\n p.prompt_type as `prompt_type: enums::by_table::prompts::prompt_type::PromptType`,\n\n p.maybe_creator_user_token as `maybe_creator_user_token: tokens::tokens::users::UserToken`,\n\n p.maybe_positive_prompt,\n p.maybe_negative_prompt,\n p.maybe_other_args,\n\n p.creator_ip_address,\n p.created_at\n\nFROM prompts as p\nWHERE\n p.token = ?\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "token: tokens::tokens::prompts::PromptToken",
|
||||
"type_info": {
|
||||
"type": "VarString",
|
||||
"flags": "NOT_NULL | UNIQUE_KEY | BINARY | NO_DEFAULT_VALUE",
|
||||
"char_set": 224,
|
||||
"max_size": 128
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "prompt_type: enums::by_table::prompts::prompt_type::PromptType",
|
||||
"type_info": {
|
||||
"type": "VarString",
|
||||
"flags": "NOT_NULL | BINARY | NO_DEFAULT_VALUE",
|
||||
"char_set": 224,
|
||||
"max_size": 64
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "maybe_creator_user_token: tokens::tokens::users::UserToken",
|
||||
"type_info": {
|
||||
"type": "VarString",
|
||||
"flags": "MULTIPLE_KEY | BINARY",
|
||||
"char_set": 224,
|
||||
"max_size": 128
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "maybe_positive_prompt",
|
||||
"type_info": {
|
||||
"type": "Blob",
|
||||
"flags": "BLOB | BINARY",
|
||||
"char_set": 224,
|
||||
"max_size": 262140
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "maybe_negative_prompt",
|
||||
"type_info": {
|
||||
"type": "Blob",
|
||||
"flags": "BLOB | BINARY",
|
||||
"char_set": 224,
|
||||
"max_size": 262140
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"name": "maybe_other_args",
|
||||
"type_info": {
|
||||
"type": "Blob",
|
||||
"flags": "BLOB | BINARY",
|
||||
"char_set": 224,
|
||||
"max_size": 262140
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"name": "creator_ip_address",
|
||||
"type_info": {
|
||||
"type": "VarString",
|
||||
"flags": "NOT_NULL | BINARY | NO_DEFAULT_VALUE",
|
||||
"char_set": 224,
|
||||
"max_size": 160
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 7,
|
||||
"name": "created_at",
|
||||
"type_info": {
|
||||
"type": "Timestamp",
|
||||
"flags": "NOT_NULL | MULTIPLE_KEY | BINARY | TIMESTAMP",
|
||||
"char_set": 63,
|
||||
"max_size": 19
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Right": 1
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "61744bbf83f7cf00348bcc21e868d9c506fec46da4a33ac50d63c6d9e00c0cec"
|
||||
}
|
||||
@@ -15,6 +15,7 @@ pub mod media_files;
|
||||
pub mod media_uploads;
|
||||
pub mod model_categories;
|
||||
pub mod model_weights;
|
||||
pub mod prompts;
|
||||
pub mod public_event_feed;
|
||||
pub mod stats;
|
||||
pub mod trending_model_analytics;
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
// NB: Incrementally getting rid of build warnings...
|
||||
#![forbid(unused_imports)]
|
||||
#![forbid(unused_mut)]
|
||||
#![forbid(unused_variables)]
|
||||
|
||||
use anyhow::anyhow;
|
||||
use chrono::{DateTime, Utc};
|
||||
use sqlx::MySqlPool;
|
||||
|
||||
use enums::by_table::prompts::prompt_type::PromptType;
|
||||
use errors::AnyhowResult;
|
||||
use tokens::tokens::prompts::PromptToken;
|
||||
use tokens::tokens::users::UserToken;
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct Prompt {
|
||||
pub token: PromptToken,
|
||||
|
||||
pub prompt_type: PromptType,
|
||||
|
||||
pub maybe_creator_user_token: Option<UserToken>,
|
||||
|
||||
pub maybe_positive_prompt: Option<String>,
|
||||
pub maybe_negative_prompt: Option<String>,
|
||||
|
||||
// TODO(bt,2024-02-22): Expose this as structured fields.
|
||||
pub maybe_other_args: Option<String>,
|
||||
|
||||
/// For moderators only
|
||||
pub creator_ip_address: Option<String>,
|
||||
|
||||
pub created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct PromptRaw {
|
||||
pub token: PromptToken,
|
||||
|
||||
pub prompt_type: PromptType,
|
||||
|
||||
pub maybe_creator_user_token: Option<UserToken>,
|
||||
|
||||
pub maybe_positive_prompt: Option<String>,
|
||||
pub maybe_negative_prompt: Option<String>,
|
||||
|
||||
// TODO(bt,2024-02-22): Expose this as structured fields.
|
||||
pub maybe_other_args: Option<String>,
|
||||
|
||||
/// For moderators only
|
||||
pub creator_ip_address: Option<String>,
|
||||
|
||||
pub created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
pub async fn get_prompt(
|
||||
prompt_token: &PromptToken,
|
||||
mysql_pool: &MySqlPool
|
||||
) -> AnyhowResult<Option<Prompt>> {
|
||||
|
||||
let record = select_record(prompt_token, mysql_pool).await;
|
||||
|
||||
let record = match record {
|
||||
Ok(record) => record,
|
||||
Err(ref err) => {
|
||||
return match err {
|
||||
sqlx::Error::RowNotFound => Ok(None),
|
||||
_ => Err(anyhow!("database error: {:?}", err)),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Some(Prompt {
|
||||
token: record.token,
|
||||
prompt_type: record.prompt_type,
|
||||
maybe_creator_user_token: record.maybe_creator_user_token,
|
||||
maybe_positive_prompt: record.maybe_positive_prompt,
|
||||
maybe_negative_prompt: record.maybe_negative_prompt,
|
||||
maybe_other_args: record.maybe_other_args,
|
||||
creator_ip_address: record.creator_ip_address,
|
||||
created_at: record.created_at,
|
||||
}))
|
||||
}
|
||||
|
||||
async fn select_record(
|
||||
prompt_token: &PromptToken,
|
||||
mysql_pool: &MySqlPool
|
||||
) -> Result<PromptRaw, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
PromptRaw,
|
||||
r#"
|
||||
SELECT
|
||||
p.token as `token: tokens::tokens::prompts::PromptToken`,
|
||||
|
||||
p.prompt_type as `prompt_type: enums::by_table::prompts::prompt_type::PromptType`,
|
||||
|
||||
p.maybe_creator_user_token as `maybe_creator_user_token: tokens::tokens::users::UserToken`,
|
||||
|
||||
p.maybe_positive_prompt,
|
||||
p.maybe_negative_prompt,
|
||||
p.maybe_other_args,
|
||||
|
||||
p.creator_ip_address,
|
||||
p.created_at
|
||||
|
||||
FROM prompts as p
|
||||
WHERE
|
||||
p.token = ?
|
||||
"#,
|
||||
prompt_token
|
||||
)
|
||||
.fetch_one(mysql_pool)
|
||||
.await
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use sqlx;
|
||||
use sqlx::{Executor, MySql};
|
||||
|
||||
use enums::by_table::prompts::prompt_type::PromptType;
|
||||
use errors::AnyhowResult;
|
||||
use tokens::tokens::prompts::PromptToken;
|
||||
use tokens::tokens::users::UserToken;
|
||||
|
||||
pub struct InsertArgs<'e, 'c, E>
|
||||
where E: 'e + Executor<'c, Database = MySql>
|
||||
{
|
||||
/// If we need to generate the prompt token upfront, this will be the token to use for the insert.
|
||||
pub maybe_apriori_prompt_token: Option<&'e PromptToken>,
|
||||
|
||||
pub prompt_type: PromptType,
|
||||
|
||||
pub maybe_creator_user_token: Option<&'e UserToken>,
|
||||
|
||||
pub maybe_positive_prompt: Option<&'e str>,
|
||||
|
||||
pub maybe_negative_prompt: Option<&'e str>,
|
||||
|
||||
// TODO(bt,2024-02-22): This needs to be its own JSON serializable type.
|
||||
pub maybe_other_args: Option<&'e str>,
|
||||
|
||||
pub creator_ip_address: &'e str,
|
||||
|
||||
pub mysql_executor: E,
|
||||
|
||||
// TODO: Not sure if this works to tell the compiler we need the lifetime annotation.
|
||||
// See: https://doc.rust-lang.org/std/marker/struct.PhantomData.html#unused-lifetime-parameters
|
||||
pub phantom: PhantomData<&'c E>,
|
||||
}
|
||||
|
||||
pub async fn insert_prompt<'e, 'c : 'e, E>(args: InsertArgs<'e, 'c, E>)
|
||||
-> AnyhowResult<PromptToken>
|
||||
where E: 'e + Executor<'c, Database = MySql>
|
||||
{
|
||||
let prompt_token = match args.maybe_apriori_prompt_token {
|
||||
Some(token) => token.clone(),
|
||||
None => PromptToken::generate(),
|
||||
};
|
||||
|
||||
let query = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO prompts
|
||||
SET
|
||||
token = ?,
|
||||
prompt_type = ?,
|
||||
|
||||
maybe_creator_user_token = ?,
|
||||
|
||||
maybe_positive_prompt = ?,
|
||||
maybe_negative_prompt = ?,
|
||||
maybe_other_args = ?,
|
||||
|
||||
creator_ip_address = ?
|
||||
"#,
|
||||
prompt_token.as_str(),
|
||||
args.prompt_type.to_str(),
|
||||
args.maybe_creator_user_token.map(|t| t.as_str()),
|
||||
args.maybe_positive_prompt,
|
||||
args.maybe_negative_prompt,
|
||||
args.maybe_other_args,
|
||||
args.creator_ip_address,
|
||||
);
|
||||
|
||||
let _result = query.execute(args.mysql_executor).await?;
|
||||
|
||||
Ok(prompt_token)
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod get_prompt;
|
||||
pub mod insert_prompt;
|
||||
Reference in new issue
Block a user