Skip to content

Commit

Permalink
Merge pull request #846 from nsipplswezey/examples-fix-table-names
Browse files Browse the repository at this point in the history
Explicit table naming in examples to prevent errors from shared tables across examples
  • Loading branch information
Lorak-mmk authored Jan 12, 2024
2 parents e86823b + 86e42f6 commit 8c20d39
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 107 deletions.
8 changes: 4 additions & 4 deletions examples/allocations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ async fn main() -> Result<()> {
let session: Session = SessionBuilder::new().known_node(args.node).build().await?;
let session = Arc::new(session);

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.await_schema_agreement().await.unwrap();

session
.query(
"CREATE TABLE IF NOT EXISTS ks.alloc_test (a int, b int, c text, primary key (a, b))",
"CREATE TABLE IF NOT EXISTS examples_ks.allocations (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;
Expand All @@ -145,13 +145,13 @@ async fn main() -> Result<()> {

let prepared_inserts = Arc::new(
session
.prepare("INSERT INTO ks.alloc_test (a, b, c) VALUES (?, ?, 'abc')")
.prepare("INSERT INTO examples_ks.allocations (a, b, c) VALUES (?, ?, 'abc')")
.await?,
);

let prepared_selects = Arc::new(
session
.prepare("SELECT * FROM ks.alloc_test WHERE a = ? and b = ?")
.prepare("SELECT * FROM examples_ks.allocations WHERE a = ? and b = ?")
.await?,
);

Expand Down
4 changes: 2 additions & 2 deletions examples/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ async fn main() -> Result<()> {
.await
.unwrap();

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await.unwrap();
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await.unwrap();
session
.query("DROP TABLE IF EXISTS ks.t;", &[])
.query("DROP TABLE IF EXISTS examples_ks.auth;", &[])
.await
.unwrap();

Expand Down
34 changes: 26 additions & 8 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ async fn main() -> Result<()> {

let session: Session = SessionBuilder::new().known_node(uri).build().await?;

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;

session
.query(
"CREATE TABLE IF NOT EXISTS ks.t (a int, b int, c text, primary key (a, b))",
"CREATE TABLE IF NOT EXISTS examples_ks.basic (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;

session
.query("INSERT INTO ks.t (a, b, c) VALUES (?, ?, ?)", (3, 4, "def"))
.query(
"INSERT INTO examples_ks.basic (a, b, c) VALUES (?, ?, ?)",
(3, 4, "def"),
)
.await?;

session
.query("INSERT INTO ks.t (a, b, c) VALUES (1, 2, 'abc')", &[])
.query(
"INSERT INTO examples_ks.basic (a, b, c) VALUES (1, 2, 'abc')",
&[],
)
.await?;

let prepared = session
.prepare("INSERT INTO ks.t (a, b, c) VALUES (?, 7, ?)")
.prepare("INSERT INTO examples_ks.basic (a, b, c) VALUES (?, 7, ?)")
.await?;
session
.execute(&prepared, (42_i32, "I'm prepared!"))
Expand All @@ -43,7 +49,11 @@ async fn main() -> Result<()> {
.await?;

// Rows can be parsed as tuples
if let Some(rows) = session.query("SELECT a, b, c FROM ks.t", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT a, b, c FROM examples_ks.basic", &[])
.await?
.rows
{
for row in rows.into_typed::<(i32, i32, String)>() {
let (a, b, c) = row?;
println!("a, b, c: {}, {}, {}", a, b, c);
Expand All @@ -58,15 +68,23 @@ async fn main() -> Result<()> {
_c: String,
}

if let Some(rows) = session.query("SELECT a, b, c FROM ks.t", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT a, b, c FROM examples_ks.basic", &[])
.await?
.rows
{
for row_data in rows.into_typed::<RowData>() {
let row_data = row_data?;
println!("row_data: {:?}", row_data);
}
}

// Or simply as untyped rows
if let Some(rows) = session.query("SELECT a, b, c FROM ks.t", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT a, b, c FROM examples_ks.basic", &[])
.await?
.rows
{
for row in rows {
let a = row.columns[0].as_ref().unwrap().as_int().unwrap();
let b = row.columns[1].as_ref().unwrap().as_int().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ async fn main() -> Result<()> {
.await
.unwrap();

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}",
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}",
&[]).await.unwrap();
session
.query("DROP TABLE IF EXISTS ks.t;", &[])
.query("DROP TABLE IF EXISTS examples_ks.cloud;", &[])
.await
.unwrap();

Expand Down
23 changes: 17 additions & 6 deletions examples/compare-tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ async fn main() -> Result<()> {

let session: Session = SessionBuilder::new().known_node(uri).build().await?;

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;

session
.query(
"CREATE TABLE IF NOT EXISTS ks.t (pk bigint primary key)",
"CREATE TABLE IF NOT EXISTS examples_ks.compare_tokens (pk bigint primary key)",
&[],
)
.await?;

let prepared = session.prepare("INSERT INTO ks.t (pk) VALUES (?)").await?;
let prepared = session
.prepare("INSERT INTO examples_ks.compare_tokens (pk) VALUES (?)")
.await?;

for pk in (0..100_i64).chain(99840..99936_i64) {
session
.query("INSERT INTO ks.t (pk) VALUES (?)", (pk,))
.query(
"INSERT INTO examples_ks.compare_tokens (pk) VALUES (?)",
(pk,),
)
.await?;

let t = prepared.calculate_token(&(pk,))?.unwrap().value;
Expand All @@ -34,14 +39,20 @@ async fn main() -> Result<()> {
"Token endpoints for query: {:?}",
session
.get_cluster_data()
.get_token_endpoints("ks", Token { value: t })
.get_token_endpoints("examples_ks", Token { value: t })
.iter()
.map(|n| n.address)
.collect::<Vec<NodeAddr>>()
);

let qt = session
.query(format!("SELECT token(pk) FROM ks.t where pk = {}", pk), &[])
.query(
format!(
"SELECT token(pk) FROM examples_ks.compare_tokens where pk = {}",
pk
),
&[],
)
.await?
.rows
.unwrap()
Expand Down
83 changes: 61 additions & 22 deletions examples/cql-time-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ async fn main() -> Result<()> {

let session: Session = SessionBuilder::new().known_node(uri).build().await?;

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;

// Date
// Date is a year, month and day in the range -5877641-06-23 to -5877641-06-23

session
.query(
"CREATE TABLE IF NOT EXISTS ks.dates (d date primary key)",
"CREATE TABLE IF NOT EXISTS examples_ks.dates (d date primary key)",
&[],
)
.await?;
Expand All @@ -34,10 +34,17 @@ async fn main() -> Result<()> {
let chrono_date = NaiveDate::from_ymd_opt(2020, 2, 20).unwrap();

session
.query("INSERT INTO ks.dates (d) VALUES (?)", (chrono_date,))
.query(
"INSERT INTO examples_ks.dates (d) VALUES (?)",
(chrono_date,),
)
.await?;

if let Some(rows) = session.query("SELECT d from ks.dates", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT d from examples_ks.dates", &[])
.await?
.rows
{
for row in rows.into_typed::<(NaiveDate,)>() {
let (read_date,): (NaiveDate,) = match row {
Ok(read_date) => read_date,
Expand All @@ -54,10 +61,14 @@ async fn main() -> Result<()> {
let time_date = time::Date::from_calendar_date(2020, time::Month::March, 21).unwrap();

session
.query("INSERT INTO ks.dates (d) VALUES (?)", (time_date,))
.query("INSERT INTO examples_ks.dates (d) VALUES (?)", (time_date,))
.await?;

if let Some(rows) = session.query("SELECT d from ks.dates", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT d from examples_ks.dates", &[])
.await?
.rows
{
for row in rows.into_typed::<(time::Date,)>() {
let (read_date,) = match row {
Ok(read_date) => read_date,
Expand All @@ -71,10 +82,17 @@ async fn main() -> Result<()> {
// Dates outside this range must be represented in the raw form - an u32 describing days since -5877641-06-23
let example_big_date: CqlDate = CqlDate(u32::MAX);
session
.query("INSERT INTO ks.dates (d) VALUES (?)", (example_big_date,))
.query(
"INSERT INTO examples_ks.dates (d) VALUES (?)",
(example_big_date,),
)
.await?;

if let Some(rows) = session.query("SELECT d from ks.dates", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT d from examples_ks.dates", &[])
.await?
.rows
{
for row in rows {
let read_days: u32 = match row.columns[0] {
Some(CqlValue::Date(CqlDate(days))) => days,
Expand All @@ -90,7 +108,7 @@ async fn main() -> Result<()> {

session
.query(
"CREATE TABLE IF NOT EXISTS ks.times (t time primary key)",
"CREATE TABLE IF NOT EXISTS examples_ks.times (t time primary key)",
&[],
)
.await?;
Expand All @@ -102,10 +120,17 @@ async fn main() -> Result<()> {
let chrono_time = NaiveTime::from_hms_nano_opt(1, 2, 3, 456_789_012).unwrap();

session
.query("INSERT INTO ks.times (t) VALUES (?)", (chrono_time,))
.query(
"INSERT INTO examples_ks.times (t) VALUES (?)",
(chrono_time,),
)
.await?;

if let Some(rows) = session.query("SELECT t from ks.times", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT t from examples_ks.times", &[])
.await?
.rows
{
for row in rows.into_typed::<(NaiveTime,)>() {
let (read_time,) = row?;

Expand All @@ -117,10 +142,14 @@ async fn main() -> Result<()> {
let time_time = time::Time::from_hms_nano(2, 3, 4, 567_890_123).unwrap();

session
.query("INSERT INTO ks.times (t) VALUES (?)", (time_time,))
.query("INSERT INTO examples_ks.times (t) VALUES (?)", (time_time,))
.await?;

if let Some(rows) = session.query("SELECT t from ks.times", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT t from examples_ks.times", &[])
.await?
.rows
{
for row in rows.into_typed::<(time::Time,)>() {
let (read_time,) = row?;

Expand All @@ -132,10 +161,14 @@ async fn main() -> Result<()> {
let time_time = CqlTime(((3 * 60 + 4) * 60 + 5) * 1_000_000_000 + 678_901_234);

session
.query("INSERT INTO ks.times (t) VALUES (?)", (time_time,))
.query("INSERT INTO examples_ks.times (t) VALUES (?)", (time_time,))
.await?;

if let Some(rows) = session.query("SELECT t from ks.times", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT t from examples_ks.times", &[])
.await?
.rows
{
for row in rows.into_typed::<(CqlTime,)>() {
let (read_time,) = row?;

Expand All @@ -148,7 +181,7 @@ async fn main() -> Result<()> {

session
.query(
"CREATE TABLE IF NOT EXISTS ks.timestamps (t timestamp primary key)",
"CREATE TABLE IF NOT EXISTS examples_ks.timestamps (t timestamp primary key)",
&[],
)
.await?;
Expand All @@ -161,13 +194,13 @@ async fn main() -> Result<()> {

session
.query(
"INSERT INTO ks.timestamps (t) VALUES (?)",
"INSERT INTO examples_ks.timestamps (t) VALUES (?)",
(chrono_datetime,),
)
.await?;

if let Some(rows) = session
.query("SELECT t from ks.timestamps", &[])
.query("SELECT t from examples_ks.timestamps", &[])
.await?
.rows
{
Expand All @@ -185,11 +218,14 @@ async fn main() -> Result<()> {
let time_datetime = time::OffsetDateTime::now_utc();

session
.query("INSERT INTO ks.timestamps (t) VALUES (?)", (time_datetime,))
.query(
"INSERT INTO examples_ks.timestamps (t) VALUES (?)",
(time_datetime,),
)
.await?;

if let Some(rows) = session
.query("SELECT t from ks.timestamps", &[])
.query("SELECT t from examples_ks.timestamps", &[])
.await?
.rows
{
Expand All @@ -207,11 +243,14 @@ async fn main() -> Result<()> {
let cql_datetime = CqlTimestamp(1 << 31);

session
.query("INSERT INTO ks.timestamps (t) VALUES (?)", (cql_datetime,))
.query(
"INSERT INTO examples_ks.timestamps (t) VALUES (?)",
(cql_datetime,),
)
.await?;

if let Some(rows) = session
.query("SELECT t from ks.timestamps", &[])
.query("SELECT t from examples_ks.timestamps", &[])
.await?
.rows
{
Expand Down
Loading

0 comments on commit 8c20d39

Please sign in to comment.