diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index 67c254da..a5fc7b20 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -289,6 +289,9 @@ pub trait MCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { Err(e) => Err(e), } } + + /// Get all psuedo IDs + async fn analytics_get_all_psuedo_ids(&self, page: usize) -> DBResult>; } #[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq)] diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index 283e47b6..675e25d7 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -258,6 +258,12 @@ pub async fn database_works<'a, T: MCDatabase>( .analytics_get_psuedo_id_from_capmaign_id(c.key) .await .unwrap(); + assert_eq!( + vec![psuedo_id.clone()], + db.analytics_get_all_psuedo_ids(0).await.unwrap() + ); + assert!(db.analytics_get_all_psuedo_ids(1).await.unwrap().is_empty()); + db.analytics_create_psuedo_id_if_not_exists(c.key) .await .unwrap(); @@ -267,6 +273,7 @@ pub async fn database_works<'a, T: MCDatabase>( .await .unwrap() ); + assert_eq!( c.key, db.analytics_get_capmaign_id_from_psuedo_id(&psuedo_id) diff --git a/db/db-sqlx-maria/.sqlx/query-e2c30dafa790b388a193ad8785c0a7d88d8e7a7558775e238fe009f478003e46.json b/db/db-sqlx-maria/.sqlx/query-e2c30dafa790b388a193ad8785c0a7d88d8e7a7558775e238fe009f478003e46.json new file mode 100644 index 00000000..ec1265e6 --- /dev/null +++ b/db/db-sqlx-maria/.sqlx/query-e2c30dafa790b388a193ad8785c0a7d88d8e7a7558775e238fe009f478003e46.json @@ -0,0 +1,25 @@ +{ + "db_name": "MySQL", + "query": "\n SELECT\n psuedo_id\n FROM\n mcaptcha_psuedo_campaign_id\n ORDER BY ID ASC LIMIT ? OFFSET ?;", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "psuedo_id", + "type_info": { + "type": "VarString", + "flags": "NOT_NULL | UNIQUE_KEY | NO_DEFAULT_VALUE", + "char_set": 224, + "max_size": 400 + } + } + ], + "parameters": { + "Right": 2 + }, + "nullable": [ + false + ] + }, + "hash": "e2c30dafa790b388a193ad8785c0a7d88d8e7a7558775e238fe009f478003e46" +} diff --git a/db/db-sqlx-maria/src/lib.rs b/db/db-sqlx-maria/src/lib.rs index 94c359a3..f0d12bc5 100644 --- a/db/db-sqlx-maria/src/lib.rs +++ b/db/db-sqlx-maria/src/lib.rs @@ -987,12 +987,8 @@ impl MCDatabase for Database { &self, captcha_id: &str, ) -> DBResult { - struct ID { - psuedo_id: String, - } - let res = sqlx::query_as!( - ID, + PsuedoID, "SELECT psuedo_id FROM mcaptcha_psuedo_campaign_id WHERE @@ -1069,6 +1065,28 @@ impl MCDatabase for Database { Ok(()) } + /// Get all psuedo IDs + async fn analytics_get_all_psuedo_ids(&self, page: usize) -> DBResult> { + const LIMIT: usize = 50; + let offset = LIMIT * page; + + let mut res = sqlx::query_as!( + PsuedoID, + " + SELECT + psuedo_id + FROM + mcaptcha_psuedo_campaign_id + ORDER BY ID ASC LIMIT ? OFFSET ?;", + LIMIT as i64, + offset as i64 + ) + .fetch_all(&self.pool) + .await + .map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?; + + Ok(res.drain(0..).map(|r| r.psuedo_id).collect()) + } } #[derive(Clone)] @@ -1134,3 +1152,7 @@ impl From for Captcha { } } } + +struct PsuedoID { + psuedo_id: String, +} diff --git a/db/db-sqlx-postgres/.sqlx/query-d6b89b032e3a65bb5739dde8901a0d6363939bdd87739b4292dd1d88e03ce6f7.json b/db/db-sqlx-postgres/.sqlx/query-d6b89b032e3a65bb5739dde8901a0d6363939bdd87739b4292dd1d88e03ce6f7.json new file mode 100644 index 00000000..66def49c --- /dev/null +++ b/db/db-sqlx-postgres/.sqlx/query-d6b89b032e3a65bb5739dde8901a0d6363939bdd87739b4292dd1d88e03ce6f7.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "\n SELECT\n psuedo_id\n FROM\n mcaptcha_psuedo_campaign_id\n ORDER BY ID ASC LIMIT $1 OFFSET $2;", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "psuedo_id", + "type_info": "Varchar" + } + ], + "parameters": { + "Left": [ + "Int8", + "Int8" + ] + }, + "nullable": [ + false + ] + }, + "hash": "d6b89b032e3a65bb5739dde8901a0d6363939bdd87739b4292dd1d88e03ce6f7" +} diff --git a/db/db-sqlx-postgres/src/lib.rs b/db/db-sqlx-postgres/src/lib.rs index d06dfeeb..d0b2eb7b 100644 --- a/db/db-sqlx-postgres/src/lib.rs +++ b/db/db-sqlx-postgres/src/lib.rs @@ -994,12 +994,8 @@ impl MCDatabase for Database { &self, captcha_id: &str, ) -> DBResult { - struct ID { - psuedo_id: String, - } - let res = sqlx::query_as!( - ID, + PsuedoID, "SELECT psuedo_id FROM mcaptcha_psuedo_campaign_id WHERE @@ -1078,6 +1074,29 @@ impl MCDatabase for Database { Ok(()) } + + /// Get all psuedo IDs + async fn analytics_get_all_psuedo_ids(&self, page: usize) -> DBResult> { + const LIMIT: usize = 50; + let offset = LIMIT * page; + + let mut res = sqlx::query_as!( + PsuedoID, + " + SELECT + psuedo_id + FROM + mcaptcha_psuedo_campaign_id + ORDER BY ID ASC LIMIT $1 OFFSET $2;", + LIMIT as i64, + offset as i64 + ) + .fetch_all(&self.pool) + .await + .map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?; + + Ok(res.drain(0..).map(|r| r.psuedo_id).collect()) + } } #[derive(Clone)] @@ -1125,6 +1144,10 @@ impl From for Notification { } } +struct PsuedoID { + psuedo_id: String, +} + #[derive(Clone)] struct InternaleCaptchaConfig { config_id: i32,