Ejemplo n.º 1
0
/**
 * gda_sql_select_target_serialize
 * @target: a #GdaSqlSelectTarget structure
 *
 * Creates a new string representing a target used in a SELECT statement
 * after the FROM clause.
 *
 * Returns: a new string with the description of the expression or "null" in case @field is invalid.
 */
gchar *
gda_sql_select_target_serialize (GdaSqlSelectTarget *target)
{
	if (!target)
		return g_strdup ("null");
	else {
		GString *string;
		gchar *str;

		string = g_string_new ("{");

		g_string_append (string, "\"expr\":");
		str = gda_sql_expr_serialize (target->expr);
		g_string_append (string, str);
		g_free (str);

		if (target->table_name) {
			g_string_append (string, ",\"table_name\":");
			str = _json_quote_string (target->table_name);
			g_string_append (string, str);
			g_free (str);
		}
		if (target->as) {
			g_string_append (string, ",\"as\":");
			str = _json_quote_string (target->as);
			g_string_append (string, str);
			g_free (str);
		}

		g_string_append_c (string, '}');
		str = string->str;
		g_string_free (string, FALSE);
		return str;
	}
}
Ejemplo n.º 2
0
/**
 * gda_sql_operation_serialize
 * @operation: a #GdaSqlOperation structure
 *
 * Creates a new string representing an operator. You need to free the returned string
 * using g_free();
 *
 * Returns: a new string with the description of the operator or "null" in case @operation is invalid.
 */
gchar *
gda_sql_operation_serialize (GdaSqlOperation *operation)
{
	if (!operation)
		return g_strdup ("null");
	else {
		GString *string;
		gchar *str;
		GSList *list;
		gint i = 0;

		string = g_string_new ("{");

		g_string_append (string, "\"operator\":");
		str = _json_quote_string (gda_sql_operation_operator_to_string (operation->operator_type));
		g_string_append (string, str);
		g_free (str);

		for (list = operation->operands; list; list = list->next) {
			g_string_append_printf (string, ",\"operand%d\":", i++);
			if (list->data) {
				str = gda_sql_expr_serialize ((GdaSqlExpr*) list->data);
				g_string_append (string, str);
				g_free (str);
			}
			else
				g_string_append (string, "null");
		}

		g_string_append_c (string, '}');
		str = string->str;
		g_string_free (string, FALSE);
		return str;
	}
}
Ejemplo n.º 3
0
/**
 * gda_sql_select_order_serialize
 * @order: a #GdaSqlSelectOrder structure
 *
 * Creates a new string description of the ORDER BY clause used in a SELECT statement.
 *
 * Returns: a new string with the description of the ORDER BY or "null" in case @order is invalid.
 */
gchar *
gda_sql_select_order_serialize (GdaSqlSelectOrder *order)
{
	if (!order)
		return g_strdup ("null");
	else {
		GString *string;
		gchar *str;

		string = g_string_new ("{");

		g_string_append (string, "\"expr\":");
		str = gda_sql_expr_serialize (order->expr);
		g_string_append (string, str);
		g_free (str);

		g_string_append (string, ",\"sort\":");
		g_string_append (string, order->asc ? "\"ASC\"" : "\"DESC\"");

		if (order->collation_name) {
			g_string_append (string, ",\"collation\":");
			str = _json_quote_string (order->collation_name);
			g_string_append (string, str);
			g_free (str);
		}

		g_string_append_c (string, '}');
		str = string->str;
		g_string_free (string, FALSE);
		return str;
	}
}
Ejemplo n.º 4
0
/**
 * gda_sql_case_serialize
 * @sc: a #GdaSqlCase structure
 *
 * Creates a new string representing a CASE clause. You need to free the returned string
 * using g_free();
 *
 * Returns: a new string with the description of the CASE clause or "null" in case @sc is invalid.
 */
gchar *
gda_sql_case_serialize (GdaSqlCase *sc)
{
	if (!sc)
		return g_strdup ("null");
	else {
		GString *string;
		gchar *str;
		GSList *wlist, *tlist;

		string = g_string_new ("{");

		g_string_append (string, "\"base_expr\":");
		str = gda_sql_expr_serialize (sc->base_expr);
		g_string_append (string, str);
		g_free (str);

		g_string_append (string, ",\"body\":[");
		for (wlist = sc->when_expr_list, tlist = sc->then_expr_list;
		     wlist && tlist; wlist = wlist->next, tlist = tlist->next) {
			if (wlist != sc->when_expr_list)
				g_string_append_c (string, ',');
			g_string_append_c (string, '{');
			g_string_append (string, "\"when\":");
			str = gda_sql_expr_serialize ((GdaSqlExpr*) wlist->data);
			g_string_append (string, str);
			g_free (str);
			g_string_append (string, ",\"then\":");
			str = gda_sql_expr_serialize ((GdaSqlExpr*) tlist->data);
			g_string_append (string, str);
			g_free (str);
			g_string_append_c (string, '}');
		}
		g_string_append_c (string, ']');
		g_assert (!wlist && !tlist);

		g_string_append (string, ",\"else_expr\":");
		str = gda_sql_expr_serialize (sc->else_expr);
		g_string_append (string, str);
		g_free (str);

		g_string_append_c (string, '}');
		str = string->str;
		g_string_free (string, FALSE);
		return str;
	}
}
Ejemplo n.º 5
0
/**
 * gda_sql_select_join_serialize
 * @join: a #GdaSqlSelectJoin structure
 *
 * Creates a new string description of the join used in a SELECT statement.
 *
 * Returns: a new string with the description of the join or "null" in case @join is invalid.
 */
gchar *
gda_sql_select_join_serialize (GdaSqlSelectJoin *join)
{
	if (!join)
		return g_strdup ("null");
	else {
		GString *string;
		gchar *str;

		string = g_string_new ("{");

		g_string_append (string, "\"join_type\":");
		g_string_append_c (string, '"');
		g_string_append (string, gda_sql_select_join_type_to_string (join->type));
		g_string_append_c (string, '"');

		g_string_append (string, ",\"join_pos\":");
		str = g_strdup_printf ("\"%d\"", join->position);
		g_string_append (string, str);
		g_free (str);

		if (join->expr) {
			g_string_append (string, ",\"on_cond\":");
			str = gda_sql_expr_serialize (join->expr);
			g_string_append (string, str);
			g_free (str);
		}

		if (join->use) {
			GSList *list;
			g_string_append (string, ",\"using\":");
			g_string_append_c (string, '[');
			for (list = join->use; list; list = list->next) {
				if (list != join->use)
					g_string_append_c (string, ',');
				str = gda_sql_field_serialize ((GdaSqlField*) list->data);
				g_string_append (string, str);
				g_free (str);
			}
			g_string_append_c (string, ']');
		}

		g_string_append_c (string, '}');
		str = string->str;
		g_string_free (string, FALSE);
		return str;
	}
}
Ejemplo n.º 6
0
/**
 * gda_sql_function_serialize
 * @function: a #GdaSqlFunction structure
 *
 * Creates a new string representing a function. You need to free the returned string
 * using g_free();
 *
 * Returns: a new string with the description of the function or "null" in case @function is invalid.
 */
gchar *
gda_sql_function_serialize (GdaSqlFunction *function)
{
	if (!function)
		return g_strdup ("null");
	else {
		GString *string;
		gchar *str;

		string = g_string_new ("{");

		g_string_append (string, "\"function_name\":");
		str = _json_quote_string (function->function_name);
		g_string_append (string, str);
		g_free (str);

		g_string_append (string, ",\"function_args\":");
		if (function->args_list) {
			GSList *list;
			g_string_append_c (string, '[');
			for (list = function->args_list; list; list = list->next) {
				if (list != function->args_list)
					g_string_append_c (string, ',');
				str = gda_sql_expr_serialize ((GdaSqlExpr*) list->data);
				g_string_append (string, str);
				g_free (str);
			}
			g_string_append_c (string, ']');
		}
		else
			g_string_append (string, "null");
		g_string_append_c (string, '}');
		str = string->str;
		g_string_free (string, FALSE);
		return str;
	}
}
Ejemplo n.º 7
0
static gchar *
gda_sql_statement_unknown_serialize (gpointer stmt)
{
	GString *string;
	gchar *str;
	GSList *list;
	GdaSqlStatementUnknown *unknown = (GdaSqlStatementUnknown *) stmt;

	g_return_val_if_fail (stmt, NULL);

	string = g_string_new ("\"contents\":[");
	for (list = unknown->expressions; list; list = list->next) {
		gchar *str;
		str = gda_sql_expr_serialize ((GdaSqlExpr*) list->data);
		if (list != unknown->expressions)
			g_string_append_c (string, ',');
		g_string_append (string, str);
		g_free (str);
	}
	g_string_append_c (string, ']');
	str = string->str;
	g_string_free (string, FALSE);
	return str;	
}
Ejemplo n.º 8
0
static gchar *
gda_sql_statement_insert_serialize (gpointer stmt)
{
	GString *string;
	gchar *str;
	GSList *list;
	GdaSqlStatementInsert *insert = (GdaSqlStatementInsert *) stmt;

	g_return_val_if_fail (stmt, NULL);

	string = g_string_new ("\"contents\":{");

	/* table name */
	g_string_append (string, "\"table\":");
	str = gda_sql_table_serialize (insert->table);
	g_string_append (string, str);
	g_free (str);

	/* fields */
	g_string_append (string, ",\"fields\":");
	if (insert->fields_list) {
		g_string_append_c (string, '[');
		for (list = insert->fields_list; list; list = list->next) {
			if (list != insert->fields_list)
				g_string_append_c (string, ',');
			str = gda_sql_field_serialize ((GdaSqlField*) list->data);
			g_string_append (string, str);
			g_free (str);
		}
		g_string_append_c (string, ']');
	}
	else
		g_string_append (string, "null");	

	/* values */
	if (insert->values_list) {
		g_string_append (string, ",\"values\":[");
		for (list = insert->values_list; list; list = list->next) {
			if (list != insert->values_list)
				g_string_append_c (string, ',');
			if (list->data) {
				GSList *vlist;
				g_string_append_c (string, '[');
				for (vlist = (GSList *) list->data; vlist; vlist = vlist->next) {
					if (vlist != (GSList *) list->data)
						g_string_append_c (string, ',');
					str = gda_sql_expr_serialize ((GdaSqlExpr*) vlist->data);
					g_string_append (string, str);
					g_free (str);
				}
				g_string_append_c (string, ']');
			}
			else
				g_string_append (string, "null");
		}
		g_string_append_c (string, ']');
	}
	
	/* select statement */
	if (insert->select) {
		g_string_append (string, ",\"select\":{");
		if (GDA_SQL_ANY_PART (insert->select)->type == GDA_SQL_ANY_STMT_SELECT)
			str = _gda_sql_statement_select_serialize (insert->select);
		else if (GDA_SQL_ANY_PART (insert->select)->type == GDA_SQL_ANY_STMT_COMPOUND)
			str = _gda_sql_statement_compound_serialize (insert->select);
		else
			g_assert_not_reached ();
		
		g_string_append (string, str);
		g_free (str);
		g_string_append_c (string, '}');
	}

	/* conflict clause */
	if (insert->on_conflict) {
		g_string_append (string, ",\"on_conflict\":");
		str = _json_quote_string (insert->on_conflict);
		g_string_append (string, str);
		g_free (str);
	}
	g_string_append_c (string, '}');
	str = string->str;
	g_string_free (string, FALSE);
	return str;	
}
Ejemplo n.º 9
0
gchar *
_gda_sql_statement_select_serialize (gpointer stmt)
{
	GString *string;
	gchar *str;
	GSList *list;
	GdaSqlStatementSelect *select = (GdaSqlStatementSelect *) stmt;

	g_return_val_if_fail (stmt, NULL);

	string = g_string_new ("\"contents\":{");
	/* distinct */
	g_string_append (string, "\"distinct\":");
	g_string_append (string, select->distinct ? "\"true\"" : "\"false\"");
	if (select->distinct_expr) {
		g_string_append (string, ",\"distinct_on\":");
		str = gda_sql_expr_serialize (select->distinct_expr);
		g_string_append (string, str);
		g_free (str);
	}
	g_string_append (string, ",\"fields\":");
	if (select->expr_list) {
		g_string_append_c (string, '[');
		for (list = select->expr_list; list; list = list->next) {
			if (list != select->expr_list)
				g_string_append_c (string, ',');
			str = gda_sql_select_field_serialize ((GdaSqlSelectField*) list->data);
			g_string_append (string, str);
			g_free (str);
		}
		g_string_append_c (string, ']');
	}
	else
		g_string_append (string, "null");

	if (select->from) {
		g_string_append (string, ",\"from\":");
		str = gda_sql_select_from_serialize (select->from);
		g_string_append (string, str);
		g_free (str);
	}

	if (select->where_cond) {
		g_string_append (string, ",\"where\":");
		str = gda_sql_expr_serialize (select->where_cond);
		g_string_append (string, str);
		g_free (str);
	}

	if (select->group_by) {
		g_string_append (string, ",\"group_by\":");
		g_string_append_c (string, '[');
		for (list = select->group_by; list; list = list->next) {
			if (list != select->group_by)
				g_string_append_c (string, ',');
			str = gda_sql_expr_serialize ((GdaSqlExpr*) list->data);
			g_string_append (string, str);
			g_free (str);
		}
		g_string_append_c (string, ']');
	}

	if (select->having_cond) {
		g_string_append (string, ",\"having\":");
		str = gda_sql_expr_serialize (select->having_cond);
		g_string_append (string, str);
		g_free (str);
	}

	if (select->order_by) {
		g_string_append (string, ",\"order_by\":");
		g_string_append_c (string, '[');
		for (list = select->order_by; list; list = list->next) {
			if (list != select->order_by)
				g_string_append_c (string, ',');
			str = gda_sql_select_order_serialize ((GdaSqlSelectOrder*) list->data);
			g_string_append (string, str);
			g_free (str);
		}
		g_string_append_c (string, ']');
	}

	if (select->limit_count) {
		g_string_append (string, ",\"limit\":");
		str = gda_sql_expr_serialize (select->limit_count);
		g_string_append (string, str);
		g_free (str);

		if (select->limit_offset) {
			g_string_append (string, ",\"offset\":");
			str = gda_sql_expr_serialize (select->limit_offset);
			g_string_append (string, str);
			g_free (str);
		}
	}

	g_string_append_c (string, '}');
	str = string->str;
	g_string_free (string, FALSE);
	return str;	
}
Ejemplo n.º 10
0
static gchar *
gda_sql_statement_update_serialize (gpointer stmt)
{
	GString *string;
	gchar *str;
	GSList *list;
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) stmt;

	g_return_val_if_fail (stmt, NULL);

	string = g_string_new ("\"contents\":{");

	/* table name */
	g_string_append (string, "\"table\":");
	str = gda_sql_table_serialize (update->table);
	g_string_append (string, str);
	g_free (str);

	/* fields */
	g_string_append (string, ",\"fields\":");
	if (update->fields_list) {
		g_string_append_c (string, '[');
		for (list = update->fields_list; list; list = list->next) {
			if (list != update->fields_list)
				g_string_append_c (string, ',');
			str = gda_sql_field_serialize ((GdaSqlField *) list->data);
			g_string_append (string, str);
			g_free (str);
		}
		g_string_append_c (string, ']');
	}
	else
		g_string_append (string, "null");

	/* expressions */
	g_string_append (string, ",\"expressions\":");
	if (update->expr_list) {
		g_string_append_c (string, '[');
		for (list = update->expr_list; list; list = list->next) {
			if (list != update->expr_list)
				g_string_append_c (string, ',');
			str = gda_sql_expr_serialize ((GdaSqlExpr *) list->data);
			g_string_append (string, str);
			g_free (str);
		}
		g_string_append_c (string, ']');
	}
	else
		g_string_append (string, "null");	

	/* condition */
	if (update->cond) {
		g_string_append (string, ",\"condition\":");
		str = gda_sql_expr_serialize (update->cond);
		g_string_append (string, str);
		g_free (str);
	}

	/* conflict clause */
        if (update->on_conflict) {
                g_string_append (string, ",\"on_conflict\":");
                str = _json_quote_string (update->on_conflict);
                g_string_append (string, str);
                g_free (str);
        }
	g_string_append_c (string, '}');
	str = string->str;
	g_string_free (string, FALSE);
	return str;	
}