use sqlx::{SqliteExecutor, query};
use tracing::debug;
use crate::errors::MbtResult;
use crate::queries::create_schema;
pub async fn is_cache_tables_type<T>(conn: &mut T) -> MbtResult<bool>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
let sql = query!(
"SELECT (
-- Has a 'tile_cache' table
SELECT COUNT(*) = 1
FROM sqlite_master
WHERE name = 'tile_cache' AND type = 'table'
--
) AND (
-- 'tile_cache' table's columns and their types are as expected:
-- 7 columns (zoom_level, tile_column, tile_row, fetched, expires, etag,
-- tile_data). The order is not important
SELECT COUNT(*) = 7
FROM pragma_table_info('tile_cache')
WHERE ((name = 'zoom_level' AND type LIKE '%INT%')
OR (name = 'tile_column' AND type LIKE '%INT%')
OR (name = 'tile_row' AND type LIKE '%INT%')
OR (name = 'fetched' AND type LIKE '%INT%')
OR (name = 'expires' AND type LIKE '%INT%')
OR (name = 'etag' AND type = 'TEXT')
OR (name = 'tile_data' AND type = 'BLOB'))
--
) AS is_valid;"
);
Ok(sql.fetch_one(&mut *conn).await?.is_valid == 1)
}
pub async fn create_cache_tables<T>(conn: &mut T, strict: bool) -> MbtResult<()>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
debug!(
"Creating if needed cache table and tiles view: tile_cache(z,x,y,fetched,expires,etag,data)"
);
create_schema(conn, include_str!("../../sql/init-cache.sql"), strict).await
}
#[cfg(test)]
mod tests {
use super::*;
use crate::metadata::anonymous_mbtiles;
use crate::{
is_dedup_id_normalized_tables_type, is_flat_tables_type, is_flat_with_hash_tables_type,
is_normalized_tables_type,
};
#[actix_rt::test]
async fn create_and_detect_cache() {
let (_mbt, mut conn) = anonymous_mbtiles("").await;
create_cache_tables(&mut conn, false).await.unwrap();
assert!(is_cache_tables_type(&mut conn).await.unwrap());
assert!(!is_flat_tables_type(&mut conn).await.unwrap());
assert!(!is_flat_with_hash_tables_type(&mut conn).await.unwrap());
assert!(!is_normalized_tables_type(&mut conn).await.unwrap());
assert!(!is_dedup_id_normalized_tables_type(&mut conn).await.unwrap());
}
}